dm: add reserved_rq_based_ios module parameter
[deliverable/linux.git] / drivers / md / dm.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
784aae73 3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
1da177e4
LT
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
51e5b2bd 9#include "dm-uevent.h"
1da177e4
LT
10
11#include <linux/init.h>
12#include <linux/module.h>
48c9c27b 13#include <linux/mutex.h>
1da177e4
LT
14#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
1da177e4
LT
17#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
3ac51e74 20#include <linux/hdreg.h>
3f77316d 21#include <linux/delay.h>
55782138
LZ
22
23#include <trace/events/block.h>
1da177e4 24
72d94861
AK
25#define DM_MSG_PREFIX "core"
26
71a16736
NK
27#ifdef CONFIG_PRINTK
28/*
29 * ratelimit state to be used in DMXXX_LIMIT().
30 */
31DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
32 DEFAULT_RATELIMIT_INTERVAL,
33 DEFAULT_RATELIMIT_BURST);
34EXPORT_SYMBOL(dm_ratelimit_state);
35#endif
36
60935eb2
MB
37/*
38 * Cookies are numeric values sent with CHANGE and REMOVE
39 * uevents while resuming, removing or renaming the device.
40 */
41#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42#define DM_COOKIE_LENGTH 24
43
1da177e4
LT
44static const char *_name = DM_NAME;
45
46static unsigned int major = 0;
47static unsigned int _major = 0;
48
d15b774c
AK
49static DEFINE_IDR(_minor_idr);
50
f32c10b0 51static DEFINE_SPINLOCK(_minor_lock);
1da177e4 52/*
8fbf26ad 53 * For bio-based dm.
1da177e4
LT
54 * One of these is allocated per bio.
55 */
56struct dm_io {
57 struct mapped_device *md;
58 int error;
1da177e4 59 atomic_t io_count;
6ae2fa67 60 struct bio *bio;
3eaf840e 61 unsigned long start_time;
f88fb981 62 spinlock_t endio_lock;
fd2ed4d2 63 struct dm_stats_aux stats_aux;
1da177e4
LT
64};
65
8fbf26ad
KU
66/*
67 * For request-based dm.
68 * One of these is allocated per request.
69 */
70struct dm_rq_target_io {
71 struct mapped_device *md;
72 struct dm_target *ti;
73 struct request *orig, clone;
74 int error;
75 union map_info info;
76};
77
78/*
94818742
KO
79 * For request-based dm - the bio clones we allocate are embedded in these
80 * structs.
81 *
82 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
83 * the bioset is created - this means the bio has to come at the end of the
84 * struct.
8fbf26ad
KU
85 */
86struct dm_rq_clone_bio_info {
87 struct bio *orig;
cec47e3d 88 struct dm_rq_target_io *tio;
94818742 89 struct bio clone;
8fbf26ad
KU
90};
91
1da177e4
LT
92union map_info *dm_get_mapinfo(struct bio *bio)
93{
17b2f66f 94 if (bio && bio->bi_private)
028867ac 95 return &((struct dm_target_io *)bio->bi_private)->info;
17b2f66f 96 return NULL;
1da177e4
LT
97}
98
cec47e3d
KU
99union map_info *dm_get_rq_mapinfo(struct request *rq)
100{
101 if (rq && rq->end_io_data)
102 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
103 return NULL;
104}
105EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
106
ba61fdd1
JM
107#define MINOR_ALLOCED ((void *)-1)
108
1da177e4
LT
109/*
110 * Bits for the md->flags field.
111 */
1eb787ec 112#define DMF_BLOCK_IO_FOR_SUSPEND 0
1da177e4 113#define DMF_SUSPENDED 1
aa8d7c2f 114#define DMF_FROZEN 2
fba9f90e 115#define DMF_FREEING 3
5c6bd75d 116#define DMF_DELETING 4
2e93ccc1 117#define DMF_NOFLUSH_SUSPENDING 5
d5b9dd04 118#define DMF_MERGE_IS_OPTIONAL 6
1da177e4 119
83d5e5b0
MP
120/*
121 * A dummy definition to make RCU happy.
122 * struct dm_table should never be dereferenced in this file.
123 */
124struct dm_table {
125 int undefined__;
126};
127
304f3f6a
MB
128/*
129 * Work processed by per-device workqueue.
130 */
1da177e4 131struct mapped_device {
83d5e5b0 132 struct srcu_struct io_barrier;
e61290a4 133 struct mutex suspend_lock;
1da177e4 134 atomic_t holders;
5c6bd75d 135 atomic_t open_count;
1da177e4 136
2a7faeb1
MP
137 /*
138 * The current mapping.
139 * Use dm_get_live_table{_fast} or take suspend_lock for
140 * dereference.
141 */
142 struct dm_table *map;
143
1da177e4
LT
144 unsigned long flags;
145
165125e1 146 struct request_queue *queue;
a5664dad 147 unsigned type;
4a0b4ddf 148 /* Protect queue and type against concurrent access. */
a5664dad
MS
149 struct mutex type_lock;
150
36a0456f
AK
151 struct target_type *immutable_target_type;
152
1da177e4 153 struct gendisk *disk;
7e51f257 154 char name[16];
1da177e4
LT
155
156 void *interface_ptr;
157
158 /*
159 * A list of ios that arrived while we were suspended.
160 */
316d315b 161 atomic_t pending[2];
1da177e4 162 wait_queue_head_t wait;
53d5914f 163 struct work_struct work;
74859364 164 struct bio_list deferred;
022c2611 165 spinlock_t deferred_lock;
1da177e4 166
af7e466a 167 /*
29e4013d 168 * Processing queue (flush)
304f3f6a
MB
169 */
170 struct workqueue_struct *wq;
171
1da177e4
LT
172 /*
173 * io objects are allocated from here.
174 */
175 mempool_t *io_pool;
1da177e4 176
9faf400f
SB
177 struct bio_set *bs;
178
1da177e4
LT
179 /*
180 * Event handling.
181 */
182 atomic_t event_nr;
183 wait_queue_head_t eventq;
7a8c3d3b
MA
184 atomic_t uevent_seq;
185 struct list_head uevent_list;
186 spinlock_t uevent_lock; /* Protect access to uevent_list */
1da177e4
LT
187
188 /*
189 * freeze/thaw support require holding onto a super block
190 */
191 struct super_block *frozen_sb;
db8fef4f 192 struct block_device *bdev;
3ac51e74
DW
193
194 /* forced geometry settings */
195 struct hd_geometry geometry;
784aae73
MB
196
197 /* sysfs handle */
198 struct kobject kobj;
52b1fd5a 199
d87f4c14
TH
200 /* zero-length flush that will be cloned and submitted to targets */
201 struct bio flush_bio;
fd2ed4d2
MP
202
203 struct dm_stats stats;
1da177e4
LT
204};
205
e6ee8c0b
KU
206/*
207 * For mempools pre-allocation at the table loading time.
208 */
209struct dm_md_mempools {
210 mempool_t *io_pool;
e6ee8c0b
KU
211 struct bio_set *bs;
212};
213
6cfa5857
MS
214#define RESERVED_BIO_BASED_IOS 16
215#define RESERVED_REQUEST_BASED_IOS 256
f4790826 216#define RESERVED_MAX_IOS 1024
e18b890b 217static struct kmem_cache *_io_cache;
8fbf26ad 218static struct kmem_cache *_rq_tio_cache;
94818742 219
f4790826
MS
220/*
221 * Request-based DM's mempools' reserved IOs set by the user.
222 */
223static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
224
225static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
226 unsigned def, unsigned max)
227{
228 unsigned ios = ACCESS_ONCE(*reserved_ios);
229 unsigned modified_ios = 0;
230
231 if (!ios)
232 modified_ios = def;
233 else if (ios > max)
234 modified_ios = max;
235
236 if (modified_ios) {
237 (void)cmpxchg(reserved_ios, ios, modified_ios);
238 ios = modified_ios;
239 }
240
241 return ios;
242}
243
244unsigned dm_get_reserved_rq_based_ios(void)
245{
246 return __dm_get_reserved_ios(&reserved_rq_based_ios,
247 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
248}
249EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
250
1da177e4
LT
251static int __init local_init(void)
252{
51157b4a 253 int r = -ENOMEM;
1da177e4 254
1da177e4 255 /* allocate a slab for the dm_ios */
028867ac 256 _io_cache = KMEM_CACHE(dm_io, 0);
1da177e4 257 if (!_io_cache)
51157b4a 258 return r;
1da177e4 259
8fbf26ad
KU
260 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
261 if (!_rq_tio_cache)
dba14160 262 goto out_free_io_cache;
8fbf26ad 263
51e5b2bd 264 r = dm_uevent_init();
51157b4a 265 if (r)
23e5083b 266 goto out_free_rq_tio_cache;
51e5b2bd 267
1da177e4
LT
268 _major = major;
269 r = register_blkdev(_major, _name);
51157b4a
KU
270 if (r < 0)
271 goto out_uevent_exit;
1da177e4
LT
272
273 if (!_major)
274 _major = r;
275
276 return 0;
51157b4a
KU
277
278out_uevent_exit:
279 dm_uevent_exit();
8fbf26ad
KU
280out_free_rq_tio_cache:
281 kmem_cache_destroy(_rq_tio_cache);
51157b4a
KU
282out_free_io_cache:
283 kmem_cache_destroy(_io_cache);
284
285 return r;
1da177e4
LT
286}
287
288static void local_exit(void)
289{
8fbf26ad 290 kmem_cache_destroy(_rq_tio_cache);
1da177e4 291 kmem_cache_destroy(_io_cache);
00d59405 292 unregister_blkdev(_major, _name);
51e5b2bd 293 dm_uevent_exit();
1da177e4
LT
294
295 _major = 0;
296
297 DMINFO("cleaned up");
298}
299
b9249e55 300static int (*_inits[])(void) __initdata = {
1da177e4
LT
301 local_init,
302 dm_target_init,
303 dm_linear_init,
304 dm_stripe_init,
952b3557 305 dm_io_init,
945fa4d2 306 dm_kcopyd_init,
1da177e4 307 dm_interface_init,
fd2ed4d2 308 dm_statistics_init,
1da177e4
LT
309};
310
b9249e55 311static void (*_exits[])(void) = {
1da177e4
LT
312 local_exit,
313 dm_target_exit,
314 dm_linear_exit,
315 dm_stripe_exit,
952b3557 316 dm_io_exit,
945fa4d2 317 dm_kcopyd_exit,
1da177e4 318 dm_interface_exit,
fd2ed4d2 319 dm_statistics_exit,
1da177e4
LT
320};
321
322static int __init dm_init(void)
323{
324 const int count = ARRAY_SIZE(_inits);
325
326 int r, i;
327
328 for (i = 0; i < count; i++) {
329 r = _inits[i]();
330 if (r)
331 goto bad;
332 }
333
334 return 0;
335
336 bad:
337 while (i--)
338 _exits[i]();
339
340 return r;
341}
342
343static void __exit dm_exit(void)
344{
345 int i = ARRAY_SIZE(_exits);
346
347 while (i--)
348 _exits[i]();
d15b774c
AK
349
350 /*
351 * Should be empty by this point.
352 */
d15b774c 353 idr_destroy(&_minor_idr);
1da177e4
LT
354}
355
356/*
357 * Block device functions
358 */
432a212c
MA
359int dm_deleting_md(struct mapped_device *md)
360{
361 return test_bit(DMF_DELETING, &md->flags);
362}
363
fe5f9f2c 364static int dm_blk_open(struct block_device *bdev, fmode_t mode)
1da177e4
LT
365{
366 struct mapped_device *md;
367
fba9f90e
JM
368 spin_lock(&_minor_lock);
369
fe5f9f2c 370 md = bdev->bd_disk->private_data;
fba9f90e
JM
371 if (!md)
372 goto out;
373
5c6bd75d 374 if (test_bit(DMF_FREEING, &md->flags) ||
432a212c 375 dm_deleting_md(md)) {
fba9f90e
JM
376 md = NULL;
377 goto out;
378 }
379
1da177e4 380 dm_get(md);
5c6bd75d 381 atomic_inc(&md->open_count);
fba9f90e
JM
382
383out:
384 spin_unlock(&_minor_lock);
385
386 return md ? 0 : -ENXIO;
1da177e4
LT
387}
388
db2a144b 389static void dm_blk_close(struct gendisk *disk, fmode_t mode)
1da177e4 390{
fe5f9f2c 391 struct mapped_device *md = disk->private_data;
6e9624b8 392
4a1aeb98
MB
393 spin_lock(&_minor_lock);
394
5c6bd75d 395 atomic_dec(&md->open_count);
1da177e4 396 dm_put(md);
4a1aeb98
MB
397
398 spin_unlock(&_minor_lock);
1da177e4
LT
399}
400
5c6bd75d
AK
401int dm_open_count(struct mapped_device *md)
402{
403 return atomic_read(&md->open_count);
404}
405
406/*
407 * Guarantees nothing is using the device before it's deleted.
408 */
409int dm_lock_for_deletion(struct mapped_device *md)
410{
411 int r = 0;
412
413 spin_lock(&_minor_lock);
414
415 if (dm_open_count(md))
416 r = -EBUSY;
417 else
418 set_bit(DMF_DELETING, &md->flags);
419
420 spin_unlock(&_minor_lock);
421
422 return r;
423}
424
fd2ed4d2
MP
425sector_t dm_get_size(struct mapped_device *md)
426{
427 return get_capacity(md->disk);
428}
429
430struct dm_stats *dm_get_stats(struct mapped_device *md)
431{
432 return &md->stats;
433}
434
3ac51e74
DW
435static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
436{
437 struct mapped_device *md = bdev->bd_disk->private_data;
438
439 return dm_get_geometry(md, geo);
440}
441
fe5f9f2c 442static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
aa129a22
MB
443 unsigned int cmd, unsigned long arg)
444{
fe5f9f2c 445 struct mapped_device *md = bdev->bd_disk->private_data;
83d5e5b0 446 int srcu_idx;
6c182cd8 447 struct dm_table *map;
aa129a22
MB
448 struct dm_target *tgt;
449 int r = -ENOTTY;
450
6c182cd8 451retry:
83d5e5b0
MP
452 map = dm_get_live_table(md, &srcu_idx);
453
aa129a22
MB
454 if (!map || !dm_table_get_size(map))
455 goto out;
456
457 /* We only support devices that have a single target */
458 if (dm_table_get_num_targets(map) != 1)
459 goto out;
460
461 tgt = dm_table_get_target(map, 0);
462
4f186f8b 463 if (dm_suspended_md(md)) {
aa129a22
MB
464 r = -EAGAIN;
465 goto out;
466 }
467
468 if (tgt->type->ioctl)
647b3d00 469 r = tgt->type->ioctl(tgt, cmd, arg);
aa129a22
MB
470
471out:
83d5e5b0 472 dm_put_live_table(md, srcu_idx);
aa129a22 473
6c182cd8
HR
474 if (r == -ENOTCONN) {
475 msleep(10);
476 goto retry;
477 }
478
aa129a22
MB
479 return r;
480}
481
028867ac 482static struct dm_io *alloc_io(struct mapped_device *md)
1da177e4
LT
483{
484 return mempool_alloc(md->io_pool, GFP_NOIO);
485}
486
028867ac 487static void free_io(struct mapped_device *md, struct dm_io *io)
1da177e4
LT
488{
489 mempool_free(io, md->io_pool);
490}
491
028867ac 492static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
1da177e4 493{
dba14160 494 bio_put(&tio->clone);
1da177e4
LT
495}
496
08885643
KU
497static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
498 gfp_t gfp_mask)
cec47e3d 499{
5f015204 500 return mempool_alloc(md->io_pool, gfp_mask);
cec47e3d
KU
501}
502
503static void free_rq_tio(struct dm_rq_target_io *tio)
504{
5f015204 505 mempool_free(tio, tio->md->io_pool);
cec47e3d
KU
506}
507
90abb8c4
KU
508static int md_in_flight(struct mapped_device *md)
509{
510 return atomic_read(&md->pending[READ]) +
511 atomic_read(&md->pending[WRITE]);
512}
513
3eaf840e
JNN
514static void start_io_acct(struct dm_io *io)
515{
516 struct mapped_device *md = io->md;
fd2ed4d2 517 struct bio *bio = io->bio;
c9959059 518 int cpu;
fd2ed4d2 519 int rw = bio_data_dir(bio);
3eaf840e
JNN
520
521 io->start_time = jiffies;
522
074a7aca
TH
523 cpu = part_stat_lock();
524 part_round_stats(cpu, &dm_disk(md)->part0);
525 part_stat_unlock();
1e9bb880
SL
526 atomic_set(&dm_disk(md)->part0.in_flight[rw],
527 atomic_inc_return(&md->pending[rw]));
fd2ed4d2
MP
528
529 if (unlikely(dm_stats_used(&md->stats)))
530 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
531 bio_sectors(bio), false, 0, &io->stats_aux);
3eaf840e
JNN
532}
533
d221d2e7 534static void end_io_acct(struct dm_io *io)
3eaf840e
JNN
535{
536 struct mapped_device *md = io->md;
537 struct bio *bio = io->bio;
538 unsigned long duration = jiffies - io->start_time;
c9959059 539 int pending, cpu;
3eaf840e
JNN
540 int rw = bio_data_dir(bio);
541
074a7aca
TH
542 cpu = part_stat_lock();
543 part_round_stats(cpu, &dm_disk(md)->part0);
544 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
545 part_stat_unlock();
3eaf840e 546
fd2ed4d2
MP
547 if (unlikely(dm_stats_used(&md->stats)))
548 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
549 bio_sectors(bio), true, duration, &io->stats_aux);
550
af7e466a
MP
551 /*
552 * After this is decremented the bio must not be touched if it is
d87f4c14 553 * a flush.
af7e466a 554 */
1e9bb880
SL
555 pending = atomic_dec_return(&md->pending[rw]);
556 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
316d315b 557 pending += atomic_read(&md->pending[rw^0x1]);
3eaf840e 558
d221d2e7
MP
559 /* nudge anyone waiting on suspend queue */
560 if (!pending)
561 wake_up(&md->wait);
3eaf840e
JNN
562}
563
1da177e4
LT
564/*
565 * Add the bio to the list of deferred io.
566 */
92c63902 567static void queue_io(struct mapped_device *md, struct bio *bio)
1da177e4 568{
05447420 569 unsigned long flags;
1da177e4 570
05447420 571 spin_lock_irqsave(&md->deferred_lock, flags);
1da177e4 572 bio_list_add(&md->deferred, bio);
05447420 573 spin_unlock_irqrestore(&md->deferred_lock, flags);
6a8736d1 574 queue_work(md->wq, &md->work);
1da177e4
LT
575}
576
577/*
578 * Everyone (including functions in this file), should use this
579 * function to access the md->map field, and make sure they call
83d5e5b0 580 * dm_put_live_table() when finished.
1da177e4 581 */
83d5e5b0 582struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
1da177e4 583{
83d5e5b0
MP
584 *srcu_idx = srcu_read_lock(&md->io_barrier);
585
586 return srcu_dereference(md->map, &md->io_barrier);
587}
1da177e4 588
83d5e5b0
MP
589void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
590{
591 srcu_read_unlock(&md->io_barrier, srcu_idx);
592}
593
594void dm_sync_table(struct mapped_device *md)
595{
596 synchronize_srcu(&md->io_barrier);
597 synchronize_rcu_expedited();
598}
599
600/*
601 * A fast alternative to dm_get_live_table/dm_put_live_table.
602 * The caller must not block between these two functions.
603 */
604static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
605{
606 rcu_read_lock();
607 return rcu_dereference(md->map);
608}
1da177e4 609
83d5e5b0
MP
610static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
611{
612 rcu_read_unlock();
1da177e4
LT
613}
614
3ac51e74
DW
615/*
616 * Get the geometry associated with a dm device
617 */
618int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
619{
620 *geo = md->geometry;
621
622 return 0;
623}
624
625/*
626 * Set the geometry of a device.
627 */
628int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
629{
630 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
631
632 if (geo->start > sz) {
633 DMWARN("Start sector is beyond the geometry limits.");
634 return -EINVAL;
635 }
636
637 md->geometry = *geo;
638
639 return 0;
640}
641
1da177e4
LT
642/*-----------------------------------------------------------------
643 * CRUD START:
644 * A more elegant soln is in the works that uses the queue
645 * merge fn, unfortunately there are a couple of changes to
646 * the block layer that I want to make for this. So in the
647 * interests of getting something for people to use I give
648 * you this clearly demarcated crap.
649 *---------------------------------------------------------------*/
650
2e93ccc1
KU
651static int __noflush_suspending(struct mapped_device *md)
652{
653 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
654}
655
1da177e4
LT
656/*
657 * Decrements the number of outstanding ios that a bio has been
658 * cloned into, completing the original io if necc.
659 */
858119e1 660static void dec_pending(struct dm_io *io, int error)
1da177e4 661{
2e93ccc1 662 unsigned long flags;
b35f8caa
MB
663 int io_error;
664 struct bio *bio;
665 struct mapped_device *md = io->md;
2e93ccc1
KU
666
667 /* Push-back supersedes any I/O errors */
f88fb981
KU
668 if (unlikely(error)) {
669 spin_lock_irqsave(&io->endio_lock, flags);
670 if (!(io->error > 0 && __noflush_suspending(md)))
671 io->error = error;
672 spin_unlock_irqrestore(&io->endio_lock, flags);
673 }
1da177e4
LT
674
675 if (atomic_dec_and_test(&io->io_count)) {
2e93ccc1
KU
676 if (io->error == DM_ENDIO_REQUEUE) {
677 /*
678 * Target requested pushing back the I/O.
2e93ccc1 679 */
022c2611 680 spin_lock_irqsave(&md->deferred_lock, flags);
6a8736d1
TH
681 if (__noflush_suspending(md))
682 bio_list_add_head(&md->deferred, io->bio);
683 else
2e93ccc1
KU
684 /* noflush suspend was interrupted. */
685 io->error = -EIO;
022c2611 686 spin_unlock_irqrestore(&md->deferred_lock, flags);
2e93ccc1
KU
687 }
688
b35f8caa
MB
689 io_error = io->error;
690 bio = io->bio;
6a8736d1
TH
691 end_io_acct(io);
692 free_io(md, io);
693
694 if (io_error == DM_ENDIO_REQUEUE)
695 return;
2e93ccc1 696
b372d360 697 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
af7e466a 698 /*
6a8736d1
TH
699 * Preflush done for flush with data, reissue
700 * without REQ_FLUSH.
af7e466a 701 */
6a8736d1
TH
702 bio->bi_rw &= ~REQ_FLUSH;
703 queue_io(md, bio);
af7e466a 704 } else {
b372d360 705 /* done with normal IO or empty flush */
0a82a8d1 706 trace_block_bio_complete(md->queue, bio, io_error);
b372d360 707 bio_endio(bio, io_error);
b35f8caa 708 }
1da177e4
LT
709 }
710}
711
6712ecf8 712static void clone_endio(struct bio *bio, int error)
1da177e4
LT
713{
714 int r = 0;
028867ac 715 struct dm_target_io *tio = bio->bi_private;
b35f8caa 716 struct dm_io *io = tio->io;
9faf400f 717 struct mapped_device *md = tio->io->md;
1da177e4
LT
718 dm_endio_fn endio = tio->ti->type->end_io;
719
1da177e4
LT
720 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
721 error = -EIO;
722
723 if (endio) {
7de3ee57 724 r = endio(tio->ti, bio, error);
2e93ccc1
KU
725 if (r < 0 || r == DM_ENDIO_REQUEUE)
726 /*
727 * error and requeue request are handled
728 * in dec_pending().
729 */
1da177e4 730 error = r;
45cbcd79
KU
731 else if (r == DM_ENDIO_INCOMPLETE)
732 /* The target will handle the io */
6712ecf8 733 return;
45cbcd79
KU
734 else if (r) {
735 DMWARN("unimplemented target endio return value: %d", r);
736 BUG();
737 }
1da177e4
LT
738 }
739
9faf400f 740 free_tio(md, tio);
b35f8caa 741 dec_pending(io, error);
1da177e4
LT
742}
743
cec47e3d
KU
744/*
745 * Partial completion handling for request-based dm
746 */
747static void end_clone_bio(struct bio *clone, int error)
748{
749 struct dm_rq_clone_bio_info *info = clone->bi_private;
750 struct dm_rq_target_io *tio = info->tio;
751 struct bio *bio = info->orig;
752 unsigned int nr_bytes = info->orig->bi_size;
753
754 bio_put(clone);
755
756 if (tio->error)
757 /*
758 * An error has already been detected on the request.
759 * Once error occurred, just let clone->end_io() handle
760 * the remainder.
761 */
762 return;
763 else if (error) {
764 /*
765 * Don't notice the error to the upper layer yet.
766 * The error handling decision is made by the target driver,
767 * when the request is completed.
768 */
769 tio->error = error;
770 return;
771 }
772
773 /*
774 * I/O for the bio successfully completed.
775 * Notice the data completion to the upper layer.
776 */
777
778 /*
779 * bios are processed from the head of the list.
780 * So the completing bio should always be rq->bio.
781 * If it's not, something wrong is happening.
782 */
783 if (tio->orig->bio != bio)
784 DMERR("bio completion is going in the middle of the request");
785
786 /*
787 * Update the original request.
788 * Do not use blk_end_request() here, because it may complete
789 * the original request before the clone, and break the ordering.
790 */
791 blk_update_request(tio->orig, 0, nr_bytes);
792}
793
794/*
795 * Don't touch any member of the md after calling this function because
796 * the md may be freed in dm_put() at the end of this function.
797 * Or do dm_get() before calling this function and dm_put() later.
798 */
b4324fee 799static void rq_completed(struct mapped_device *md, int rw, int run_queue)
cec47e3d 800{
b4324fee 801 atomic_dec(&md->pending[rw]);
cec47e3d
KU
802
803 /* nudge anyone waiting on suspend queue */
b4324fee 804 if (!md_in_flight(md))
cec47e3d
KU
805 wake_up(&md->wait);
806
a8c32a5c
JA
807 /*
808 * Run this off this callpath, as drivers could invoke end_io while
809 * inside their request_fn (and holding the queue lock). Calling
810 * back into ->request_fn() could deadlock attempting to grab the
811 * queue lock again.
812 */
cec47e3d 813 if (run_queue)
a8c32a5c 814 blk_run_queue_async(md->queue);
cec47e3d
KU
815
816 /*
817 * dm_put() must be at the end of this function. See the comment above
818 */
819 dm_put(md);
820}
821
a77e28c7
KU
822static void free_rq_clone(struct request *clone)
823{
824 struct dm_rq_target_io *tio = clone->end_io_data;
825
826 blk_rq_unprep_clone(clone);
827 free_rq_tio(tio);
828}
829
980691e5
KU
830/*
831 * Complete the clone and the original request.
832 * Must be called without queue lock.
833 */
834static void dm_end_request(struct request *clone, int error)
835{
836 int rw = rq_data_dir(clone);
837 struct dm_rq_target_io *tio = clone->end_io_data;
838 struct mapped_device *md = tio->md;
839 struct request *rq = tio->orig;
840
29e4013d 841 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
980691e5
KU
842 rq->errors = clone->errors;
843 rq->resid_len = clone->resid_len;
844
845 if (rq->sense)
846 /*
847 * We are using the sense buffer of the original
848 * request.
849 * So setting the length of the sense data is enough.
850 */
851 rq->sense_len = clone->sense_len;
852 }
853
854 free_rq_clone(clone);
29e4013d
TH
855 blk_end_request_all(rq, error);
856 rq_completed(md, rw, true);
980691e5
KU
857}
858
cec47e3d
KU
859static void dm_unprep_request(struct request *rq)
860{
861 struct request *clone = rq->special;
cec47e3d
KU
862
863 rq->special = NULL;
864 rq->cmd_flags &= ~REQ_DONTPREP;
865
a77e28c7 866 free_rq_clone(clone);
cec47e3d
KU
867}
868
869/*
870 * Requeue the original request of a clone.
871 */
872void dm_requeue_unmapped_request(struct request *clone)
873{
b4324fee 874 int rw = rq_data_dir(clone);
cec47e3d
KU
875 struct dm_rq_target_io *tio = clone->end_io_data;
876 struct mapped_device *md = tio->md;
877 struct request *rq = tio->orig;
878 struct request_queue *q = rq->q;
879 unsigned long flags;
880
881 dm_unprep_request(rq);
882
883 spin_lock_irqsave(q->queue_lock, flags);
cec47e3d
KU
884 blk_requeue_request(q, rq);
885 spin_unlock_irqrestore(q->queue_lock, flags);
886
b4324fee 887 rq_completed(md, rw, 0);
cec47e3d
KU
888}
889EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
890
891static void __stop_queue(struct request_queue *q)
892{
893 blk_stop_queue(q);
894}
895
896static void stop_queue(struct request_queue *q)
897{
898 unsigned long flags;
899
900 spin_lock_irqsave(q->queue_lock, flags);
901 __stop_queue(q);
902 spin_unlock_irqrestore(q->queue_lock, flags);
903}
904
905static void __start_queue(struct request_queue *q)
906{
907 if (blk_queue_stopped(q))
908 blk_start_queue(q);
909}
910
911static void start_queue(struct request_queue *q)
912{
913 unsigned long flags;
914
915 spin_lock_irqsave(q->queue_lock, flags);
916 __start_queue(q);
917 spin_unlock_irqrestore(q->queue_lock, flags);
918}
919
11a68244 920static void dm_done(struct request *clone, int error, bool mapped)
cec47e3d 921{
11a68244 922 int r = error;
cec47e3d 923 struct dm_rq_target_io *tio = clone->end_io_data;
ba1cbad9 924 dm_request_endio_fn rq_end_io = NULL;
cec47e3d 925
ba1cbad9
MS
926 if (tio->ti) {
927 rq_end_io = tio->ti->type->rq_end_io;
928
929 if (mapped && rq_end_io)
930 r = rq_end_io(tio->ti, clone, error, &tio->info);
931 }
cec47e3d 932
11a68244 933 if (r <= 0)
cec47e3d 934 /* The target wants to complete the I/O */
11a68244
KU
935 dm_end_request(clone, r);
936 else if (r == DM_ENDIO_INCOMPLETE)
cec47e3d
KU
937 /* The target will handle the I/O */
938 return;
11a68244 939 else if (r == DM_ENDIO_REQUEUE)
cec47e3d
KU
940 /* The target wants to requeue the I/O */
941 dm_requeue_unmapped_request(clone);
942 else {
11a68244 943 DMWARN("unimplemented target endio return value: %d", r);
cec47e3d
KU
944 BUG();
945 }
946}
947
11a68244
KU
948/*
949 * Request completion handler for request-based dm
950 */
951static void dm_softirq_done(struct request *rq)
952{
953 bool mapped = true;
954 struct request *clone = rq->completion_data;
955 struct dm_rq_target_io *tio = clone->end_io_data;
956
957 if (rq->cmd_flags & REQ_FAILED)
958 mapped = false;
959
960 dm_done(clone, tio->error, mapped);
961}
962
cec47e3d
KU
963/*
964 * Complete the clone and the original request with the error status
965 * through softirq context.
966 */
967static void dm_complete_request(struct request *clone, int error)
968{
969 struct dm_rq_target_io *tio = clone->end_io_data;
970 struct request *rq = tio->orig;
971
972 tio->error = error;
973 rq->completion_data = clone;
974 blk_complete_request(rq);
975}
976
977/*
978 * Complete the not-mapped clone and the original request with the error status
979 * through softirq context.
980 * Target's rq_end_io() function isn't called.
981 * This may be used when the target's map_rq() function fails.
982 */
983void dm_kill_unmapped_request(struct request *clone, int error)
984{
985 struct dm_rq_target_io *tio = clone->end_io_data;
986 struct request *rq = tio->orig;
987
988 rq->cmd_flags |= REQ_FAILED;
989 dm_complete_request(clone, error);
990}
991EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
992
993/*
994 * Called with the queue lock held
995 */
996static void end_clone_request(struct request *clone, int error)
997{
998 /*
999 * For just cleaning up the information of the queue in which
1000 * the clone was dispatched.
1001 * The clone is *NOT* freed actually here because it is alloced from
1002 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1003 */
1004 __blk_put_request(clone->q, clone);
1005
1006 /*
1007 * Actual request completion is done in a softirq context which doesn't
1008 * hold the queue lock. Otherwise, deadlock could occur because:
1009 * - another request may be submitted by the upper level driver
1010 * of the stacking during the completion
1011 * - the submission which requires queue lock may be done
1012 * against this queue
1013 */
1014 dm_complete_request(clone, error);
1015}
1016
56a67df7
MS
1017/*
1018 * Return maximum size of I/O possible at the supplied sector up to the current
1019 * target boundary.
1020 */
1021static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
1022{
1023 sector_t target_offset = dm_target_offset(ti, sector);
1024
1025 return ti->len - target_offset;
1026}
1027
1028static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1da177e4 1029{
56a67df7 1030 sector_t len = max_io_len_target_boundary(sector, ti);
542f9038 1031 sector_t offset, max_len;
1da177e4
LT
1032
1033 /*
542f9038 1034 * Does the target need to split even further?
1da177e4 1035 */
542f9038
MS
1036 if (ti->max_io_len) {
1037 offset = dm_target_offset(ti, sector);
1038 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1039 max_len = sector_div(offset, ti->max_io_len);
1040 else
1041 max_len = offset & (ti->max_io_len - 1);
1042 max_len = ti->max_io_len - max_len;
1043
1044 if (len > max_len)
1045 len = max_len;
1da177e4
LT
1046 }
1047
1048 return len;
1049}
1050
542f9038
MS
1051int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1052{
1053 if (len > UINT_MAX) {
1054 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1055 (unsigned long long)len, UINT_MAX);
1056 ti->error = "Maximum size of target IO is too large";
1057 return -EINVAL;
1058 }
1059
1060 ti->max_io_len = (uint32_t) len;
1061
1062 return 0;
1063}
1064EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1065
bd2a49b8 1066static void __map_bio(struct dm_target_io *tio)
1da177e4
LT
1067{
1068 int r;
2056a782 1069 sector_t sector;
9faf400f 1070 struct mapped_device *md;
dba14160 1071 struct bio *clone = &tio->clone;
bd2a49b8 1072 struct dm_target *ti = tio->ti;
1da177e4 1073
1da177e4
LT
1074 clone->bi_end_io = clone_endio;
1075 clone->bi_private = tio;
1076
1077 /*
1078 * Map the clone. If r == 0 we don't need to do
1079 * anything, the target has assumed ownership of
1080 * this io.
1081 */
1082 atomic_inc(&tio->io->io_count);
2056a782 1083 sector = clone->bi_sector;
7de3ee57 1084 r = ti->type->map(ti, clone);
45cbcd79 1085 if (r == DM_MAPIO_REMAPPED) {
1da177e4 1086 /* the bio has been remapped so dispatch it */
2056a782 1087
d07335e5
MS
1088 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1089 tio->io->bio->bi_bdev->bd_dev, sector);
2056a782 1090
1da177e4 1091 generic_make_request(clone);
2e93ccc1
KU
1092 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1093 /* error the io and bail out, or requeue it if needed */
9faf400f
SB
1094 md = tio->io->md;
1095 dec_pending(tio->io, r);
9faf400f 1096 free_tio(md, tio);
45cbcd79
KU
1097 } else if (r) {
1098 DMWARN("unimplemented target map return value: %d", r);
1099 BUG();
1da177e4
LT
1100 }
1101}
1102
1103struct clone_info {
1104 struct mapped_device *md;
1105 struct dm_table *map;
1106 struct bio *bio;
1107 struct dm_io *io;
1108 sector_t sector;
1109 sector_t sector_count;
1110 unsigned short idx;
1111};
1112
bd2a49b8
AK
1113static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1114{
1115 bio->bi_sector = sector;
1116 bio->bi_size = to_bytes(len);
1117}
1118
1119static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1120{
1121 bio->bi_idx = idx;
1122 bio->bi_vcnt = idx + bv_count;
1123 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1124}
1125
1126static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1127 unsigned short idx, unsigned len, unsigned offset,
1128 unsigned trim)
1129{
1130 if (!bio_integrity(bio))
1131 return;
1132
1133 bio_integrity_clone(clone, bio, GFP_NOIO);
1134
1135 if (trim)
1136 bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1137}
1138
1da177e4 1139/*
d87f4c14 1140 * Creates a little bio that just does part of a bvec.
1da177e4 1141 */
14fe594d
AK
1142static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
1143 sector_t sector, unsigned short idx,
1144 unsigned offset, unsigned len)
1da177e4 1145{
dba14160 1146 struct bio *clone = &tio->clone;
1da177e4
LT
1147 struct bio_vec *bv = bio->bi_io_vec + idx;
1148
1da177e4
LT
1149 *clone->bi_io_vec = *bv;
1150
bd2a49b8
AK
1151 bio_setup_sector(clone, sector, len);
1152
1da177e4 1153 clone->bi_bdev = bio->bi_bdev;
d87f4c14 1154 clone->bi_rw = bio->bi_rw;
1da177e4 1155 clone->bi_vcnt = 1;
1da177e4
LT
1156 clone->bi_io_vec->bv_offset = offset;
1157 clone->bi_io_vec->bv_len = clone->bi_size;
f3e1d26e 1158 clone->bi_flags |= 1 << BIO_CLONED;
1da177e4 1159
bd2a49b8 1160 clone_bio_integrity(bio, clone, idx, len, offset, 1);
1da177e4
LT
1161}
1162
1163/*
1164 * Creates a bio that consists of range of complete bvecs.
1165 */
dba14160
MP
1166static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1167 sector_t sector, unsigned short idx,
e4c93811 1168 unsigned short bv_count, unsigned len)
1da177e4 1169{
dba14160 1170 struct bio *clone = &tio->clone;
bd2a49b8 1171 unsigned trim = 0;
1da177e4 1172
9faf400f 1173 __bio_clone(clone, bio);
bd2a49b8
AK
1174 bio_setup_sector(clone, sector, len);
1175 bio_setup_bv(clone, idx, bv_count);
1176
1177 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1178 trim = 1;
1179 clone_bio_integrity(bio, clone, idx, len, 0, trim);
1da177e4
LT
1180}
1181
9015df24 1182static struct dm_target_io *alloc_tio(struct clone_info *ci,
bd2a49b8 1183 struct dm_target *ti, int nr_iovecs,
55a62eef 1184 unsigned target_bio_nr)
f9ab94ce 1185{
dba14160
MP
1186 struct dm_target_io *tio;
1187 struct bio *clone;
1188
1189 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1190 tio = container_of(clone, struct dm_target_io, clone);
f9ab94ce
MP
1191
1192 tio->io = ci->io;
1193 tio->ti = ti;
f9ab94ce 1194 memset(&tio->info, 0, sizeof(tio->info));
55a62eef 1195 tio->target_bio_nr = target_bio_nr;
9015df24
AK
1196
1197 return tio;
1198}
1199
14fe594d
AK
1200static void __clone_and_map_simple_bio(struct clone_info *ci,
1201 struct dm_target *ti,
1202 unsigned target_bio_nr, sector_t len)
9015df24 1203{
55a62eef 1204 struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
dba14160 1205 struct bio *clone = &tio->clone;
9015df24 1206
06a426ce
MS
1207 /*
1208 * Discard requests require the bio's inline iovecs be initialized.
1209 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1210 * and discard, so no need for concern about wasted bvec allocations.
1211 */
dba14160 1212 __bio_clone(clone, ci->bio);
bd2a49b8
AK
1213 if (len)
1214 bio_setup_sector(clone, ci->sector, len);
f9ab94ce 1215
bd2a49b8 1216 __map_bio(tio);
f9ab94ce
MP
1217}
1218
14fe594d
AK
1219static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1220 unsigned num_bios, sector_t len)
06a426ce 1221{
55a62eef 1222 unsigned target_bio_nr;
06a426ce 1223
55a62eef 1224 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
14fe594d 1225 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
06a426ce
MS
1226}
1227
14fe594d 1228static int __send_empty_flush(struct clone_info *ci)
f9ab94ce 1229{
06a426ce 1230 unsigned target_nr = 0;
f9ab94ce
MP
1231 struct dm_target *ti;
1232
b372d360 1233 BUG_ON(bio_has_data(ci->bio));
f9ab94ce 1234 while ((ti = dm_table_get_target(ci->map, target_nr++)))
14fe594d 1235 __send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
f9ab94ce 1236
f9ab94ce
MP
1237 return 0;
1238}
1239
e4c93811
AK
1240static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1241 sector_t sector, int nr_iovecs,
1242 unsigned short idx, unsigned short bv_count,
1243 unsigned offset, unsigned len,
1244 unsigned split_bvec)
5ae89a87 1245{
dba14160 1246 struct bio *bio = ci->bio;
5ae89a87 1247 struct dm_target_io *tio;
b0d8ed4d
AK
1248 unsigned target_bio_nr;
1249 unsigned num_target_bios = 1;
5ae89a87 1250
b0d8ed4d
AK
1251 /*
1252 * Does the target want to receive duplicate copies of the bio?
1253 */
1254 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1255 num_target_bios = ti->num_write_bios(ti, bio);
e4c93811 1256
b0d8ed4d
AK
1257 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1258 tio = alloc_tio(ci, ti, nr_iovecs, target_bio_nr);
1259 if (split_bvec)
1260 clone_split_bio(tio, bio, sector, idx, offset, len);
1261 else
1262 clone_bio(tio, bio, sector, idx, bv_count, len);
1263 __map_bio(tio);
1264 }
5ae89a87
MS
1265}
1266
55a62eef 1267typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
23508a96 1268
55a62eef 1269static unsigned get_num_discard_bios(struct dm_target *ti)
23508a96 1270{
55a62eef 1271 return ti->num_discard_bios;
23508a96
MS
1272}
1273
55a62eef 1274static unsigned get_num_write_same_bios(struct dm_target *ti)
23508a96 1275{
55a62eef 1276 return ti->num_write_same_bios;
23508a96
MS
1277}
1278
1279typedef bool (*is_split_required_fn)(struct dm_target *ti);
1280
1281static bool is_split_required_for_discard(struct dm_target *ti)
1282{
55a62eef 1283 return ti->split_discard_bios;
23508a96
MS
1284}
1285
14fe594d
AK
1286static int __send_changing_extent_only(struct clone_info *ci,
1287 get_num_bios_fn get_num_bios,
1288 is_split_required_fn is_split_required)
5ae89a87
MS
1289{
1290 struct dm_target *ti;
a79245b3 1291 sector_t len;
55a62eef 1292 unsigned num_bios;
5ae89a87 1293
a79245b3
MS
1294 do {
1295 ti = dm_table_find_target(ci->map, ci->sector);
1296 if (!dm_target_is_valid(ti))
1297 return -EIO;
5ae89a87 1298
5ae89a87 1299 /*
23508a96
MS
1300 * Even though the device advertised support for this type of
1301 * request, that does not mean every target supports it, and
936688d7 1302 * reconfiguration might also have changed that since the
a79245b3 1303 * check was performed.
5ae89a87 1304 */
55a62eef
AK
1305 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1306 if (!num_bios)
a79245b3 1307 return -EOPNOTSUPP;
5ae89a87 1308
23508a96 1309 if (is_split_required && !is_split_required(ti))
7acf0277
MP
1310 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1311 else
1312 len = min(ci->sector_count, max_io_len(ci->sector, ti));
06a426ce 1313
14fe594d 1314 __send_duplicate_bios(ci, ti, num_bios, len);
a79245b3
MS
1315
1316 ci->sector += len;
1317 } while (ci->sector_count -= len);
5ae89a87
MS
1318
1319 return 0;
1320}
1321
14fe594d 1322static int __send_discard(struct clone_info *ci)
23508a96 1323{
14fe594d
AK
1324 return __send_changing_extent_only(ci, get_num_discard_bios,
1325 is_split_required_for_discard);
23508a96
MS
1326}
1327
14fe594d 1328static int __send_write_same(struct clone_info *ci)
23508a96 1329{
14fe594d 1330 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
23508a96
MS
1331}
1332
e4c93811
AK
1333/*
1334 * Find maximum number of sectors / bvecs we can process with a single bio.
1335 */
1336static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
1337{
1338 struct bio *bio = ci->bio;
1339 sector_t bv_len, total_len = 0;
1340
1341 for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1342 bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
1343
1344 if (bv_len > max)
1345 break;
1346
1347 max -= bv_len;
1348 total_len += bv_len;
1349 }
1350
1351 return total_len;
1352}
1353
1354static int __split_bvec_across_targets(struct clone_info *ci,
1355 struct dm_target *ti, sector_t max)
1356{
1357 struct bio *bio = ci->bio;
1358 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1359 sector_t remaining = to_sector(bv->bv_len);
1360 unsigned offset = 0;
1361 sector_t len;
1362
1363 do {
1364 if (offset) {
1365 ti = dm_table_find_target(ci->map, ci->sector);
1366 if (!dm_target_is_valid(ti))
1367 return -EIO;
1368
1369 max = max_io_len(ci->sector, ti);
1370 }
1371
1372 len = min(remaining, max);
1373
1374 __clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1375 bv->bv_offset + offset, len, 1);
1376
1377 ci->sector += len;
1378 ci->sector_count -= len;
1379 offset += to_bytes(len);
1380 } while (remaining -= len);
1381
1382 ci->idx++;
1383
1384 return 0;
1385}
1386
1387/*
1388 * Select the correct strategy for processing a non-flush bio.
1389 */
14fe594d 1390static int __split_and_process_non_flush(struct clone_info *ci)
1da177e4 1391{
dba14160 1392 struct bio *bio = ci->bio;
512875bd 1393 struct dm_target *ti;
e4c93811
AK
1394 sector_t len, max;
1395 int idx;
1da177e4 1396
5ae89a87 1397 if (unlikely(bio->bi_rw & REQ_DISCARD))
14fe594d 1398 return __send_discard(ci);
23508a96 1399 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
14fe594d 1400 return __send_write_same(ci);
5ae89a87 1401
512875bd
JN
1402 ti = dm_table_find_target(ci->map, ci->sector);
1403 if (!dm_target_is_valid(ti))
1404 return -EIO;
1405
56a67df7 1406 max = max_io_len(ci->sector, ti);
512875bd 1407
e4c93811
AK
1408 /*
1409 * Optimise for the simple case where we can do all of
1410 * the remaining io with a single clone.
1411 */
1da177e4 1412 if (ci->sector_count <= max) {
e4c93811
AK
1413 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1414 ci->idx, bio->bi_vcnt - ci->idx, 0,
1415 ci->sector_count, 0);
1416 ci->sector_count = 0;
1417 return 0;
1418 }
1da177e4 1419
e4c93811
AK
1420 /*
1421 * There are some bvecs that don't span targets.
1422 * Do as many of these as possible.
1423 */
1424 if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1425 len = __len_within_target(ci, max, &idx);
1da177e4 1426
e4c93811
AK
1427 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1428 ci->idx, idx - ci->idx, 0, len, 0);
1da177e4
LT
1429
1430 ci->sector += len;
1431 ci->sector_count -= len;
e4c93811 1432 ci->idx = idx;
d2044a94 1433
e4c93811 1434 return 0;
1da177e4 1435 }
512875bd 1436
e4c93811
AK
1437 /*
1438 * Handle a bvec that must be split between two or more targets.
1439 */
1440 return __split_bvec_across_targets(ci, ti, max);
1da177e4
LT
1441}
1442
1443/*
14fe594d 1444 * Entry point to split a bio into clones and submit them to the targets.
1da177e4 1445 */
83d5e5b0
MP
1446static void __split_and_process_bio(struct mapped_device *md,
1447 struct dm_table *map, struct bio *bio)
1da177e4
LT
1448{
1449 struct clone_info ci;
512875bd 1450 int error = 0;
1da177e4 1451
83d5e5b0 1452 if (unlikely(!map)) {
6a8736d1 1453 bio_io_error(bio);
f0b9a450
MP
1454 return;
1455 }
692d0eb9 1456
83d5e5b0 1457 ci.map = map;
1da177e4 1458 ci.md = md;
1da177e4
LT
1459 ci.io = alloc_io(md);
1460 ci.io->error = 0;
1461 atomic_set(&ci.io->io_count, 1);
1462 ci.io->bio = bio;
1463 ci.io->md = md;
f88fb981 1464 spin_lock_init(&ci.io->endio_lock);
1da177e4 1465 ci.sector = bio->bi_sector;
1da177e4
LT
1466 ci.idx = bio->bi_idx;
1467
3eaf840e 1468 start_io_acct(ci.io);
bd2a49b8 1469
b372d360
MS
1470 if (bio->bi_rw & REQ_FLUSH) {
1471 ci.bio = &ci.md->flush_bio;
1472 ci.sector_count = 0;
14fe594d 1473 error = __send_empty_flush(&ci);
b372d360
MS
1474 /* dec_pending submits any data associated with flush */
1475 } else {
6a8736d1 1476 ci.bio = bio;
d87f4c14 1477 ci.sector_count = bio_sectors(bio);
b372d360 1478 while (ci.sector_count && !error)
14fe594d 1479 error = __split_and_process_non_flush(&ci);
d87f4c14 1480 }
1da177e4
LT
1481
1482 /* drop the extra reference count */
512875bd 1483 dec_pending(ci.io, error);
1da177e4
LT
1484}
1485/*-----------------------------------------------------------------
1486 * CRUD END
1487 *---------------------------------------------------------------*/
1488
f6fccb12
MB
1489static int dm_merge_bvec(struct request_queue *q,
1490 struct bvec_merge_data *bvm,
1491 struct bio_vec *biovec)
1492{
1493 struct mapped_device *md = q->queuedata;
83d5e5b0 1494 struct dm_table *map = dm_get_live_table_fast(md);
f6fccb12
MB
1495 struct dm_target *ti;
1496 sector_t max_sectors;
5037108a 1497 int max_size = 0;
f6fccb12
MB
1498
1499 if (unlikely(!map))
5037108a 1500 goto out;
f6fccb12
MB
1501
1502 ti = dm_table_find_target(map, bvm->bi_sector);
b01cd5ac 1503 if (!dm_target_is_valid(ti))
83d5e5b0 1504 goto out;
f6fccb12
MB
1505
1506 /*
1507 * Find maximum amount of I/O that won't need splitting
1508 */
56a67df7 1509 max_sectors = min(max_io_len(bvm->bi_sector, ti),
f6fccb12
MB
1510 (sector_t) BIO_MAX_SECTORS);
1511 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1512 if (max_size < 0)
1513 max_size = 0;
1514
1515 /*
1516 * merge_bvec_fn() returns number of bytes
1517 * it can accept at this offset
1518 * max is precomputed maximal io size
1519 */
1520 if (max_size && ti->type->merge)
1521 max_size = ti->type->merge(ti, bvm, biovec, max_size);
8cbeb67a
MP
1522 /*
1523 * If the target doesn't support merge method and some of the devices
1524 * provided their merge_bvec method (we know this by looking at
1525 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1526 * entries. So always set max_size to 0, and the code below allows
1527 * just one page.
1528 */
1529 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1530
1531 max_size = 0;
f6fccb12 1532
5037108a 1533out:
83d5e5b0 1534 dm_put_live_table_fast(md);
f6fccb12
MB
1535 /*
1536 * Always allow an entire first page
1537 */
1538 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1539 max_size = biovec->bv_len;
1540
f6fccb12
MB
1541 return max_size;
1542}
1543
1da177e4
LT
1544/*
1545 * The request function that just remaps the bio built up by
1546 * dm_merge_bvec.
1547 */
5a7bbad2 1548static void _dm_request(struct request_queue *q, struct bio *bio)
1da177e4 1549{
12f03a49 1550 int rw = bio_data_dir(bio);
1da177e4 1551 struct mapped_device *md = q->queuedata;
c9959059 1552 int cpu;
83d5e5b0
MP
1553 int srcu_idx;
1554 struct dm_table *map;
1da177e4 1555
83d5e5b0 1556 map = dm_get_live_table(md, &srcu_idx);
1da177e4 1557
074a7aca
TH
1558 cpu = part_stat_lock();
1559 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1560 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1561 part_stat_unlock();
12f03a49 1562
6a8736d1
TH
1563 /* if we're suspended, we have to queue this io for later */
1564 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
83d5e5b0 1565 dm_put_live_table(md, srcu_idx);
1da177e4 1566
6a8736d1
TH
1567 if (bio_rw(bio) != READA)
1568 queue_io(md, bio);
1569 else
54d9a1b4 1570 bio_io_error(bio);
5a7bbad2 1571 return;
1da177e4
LT
1572 }
1573
83d5e5b0
MP
1574 __split_and_process_bio(md, map, bio);
1575 dm_put_live_table(md, srcu_idx);
5a7bbad2 1576 return;
cec47e3d
KU
1577}
1578
fd2ed4d2 1579int dm_request_based(struct mapped_device *md)
cec47e3d
KU
1580{
1581 return blk_queue_stackable(md->queue);
1582}
1583
5a7bbad2 1584static void dm_request(struct request_queue *q, struct bio *bio)
cec47e3d
KU
1585{
1586 struct mapped_device *md = q->queuedata;
1587
1588 if (dm_request_based(md))
5a7bbad2
CH
1589 blk_queue_bio(q, bio);
1590 else
1591 _dm_request(q, bio);
cec47e3d
KU
1592}
1593
1594void dm_dispatch_request(struct request *rq)
1595{
1596 int r;
1597
1598 if (blk_queue_io_stat(rq->q))
1599 rq->cmd_flags |= REQ_IO_STAT;
1600
1601 rq->start_time = jiffies;
1602 r = blk_insert_cloned_request(rq->q, rq);
1603 if (r)
1604 dm_complete_request(rq, r);
1605}
1606EXPORT_SYMBOL_GPL(dm_dispatch_request);
1607
cec47e3d
KU
1608static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1609 void *data)
1610{
1611 struct dm_rq_target_io *tio = data;
94818742
KO
1612 struct dm_rq_clone_bio_info *info =
1613 container_of(bio, struct dm_rq_clone_bio_info, clone);
cec47e3d
KU
1614
1615 info->orig = bio_orig;
1616 info->tio = tio;
1617 bio->bi_end_io = end_clone_bio;
1618 bio->bi_private = info;
cec47e3d
KU
1619
1620 return 0;
1621}
1622
1623static int setup_clone(struct request *clone, struct request *rq,
1624 struct dm_rq_target_io *tio)
1625{
d0bcb878 1626 int r;
cec47e3d 1627
29e4013d
TH
1628 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1629 dm_rq_bio_constructor, tio);
1630 if (r)
1631 return r;
cec47e3d 1632
29e4013d
TH
1633 clone->cmd = rq->cmd;
1634 clone->cmd_len = rq->cmd_len;
1635 clone->sense = rq->sense;
1636 clone->buffer = rq->buffer;
cec47e3d
KU
1637 clone->end_io = end_clone_request;
1638 clone->end_io_data = tio;
1639
1640 return 0;
1641}
1642
6facdaff
KU
1643static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1644 gfp_t gfp_mask)
1645{
1646 struct request *clone;
1647 struct dm_rq_target_io *tio;
1648
1649 tio = alloc_rq_tio(md, gfp_mask);
1650 if (!tio)
1651 return NULL;
1652
1653 tio->md = md;
1654 tio->ti = NULL;
1655 tio->orig = rq;
1656 tio->error = 0;
1657 memset(&tio->info, 0, sizeof(tio->info));
1658
1659 clone = &tio->clone;
1660 if (setup_clone(clone, rq, tio)) {
1661 /* -ENOMEM */
1662 free_rq_tio(tio);
1663 return NULL;
1664 }
1665
1666 return clone;
1667}
1668
cec47e3d
KU
1669/*
1670 * Called with the queue lock held.
1671 */
1672static int dm_prep_fn(struct request_queue *q, struct request *rq)
1673{
1674 struct mapped_device *md = q->queuedata;
cec47e3d
KU
1675 struct request *clone;
1676
cec47e3d
KU
1677 if (unlikely(rq->special)) {
1678 DMWARN("Already has something in rq->special.");
1679 return BLKPREP_KILL;
1680 }
1681
6facdaff
KU
1682 clone = clone_rq(rq, md, GFP_ATOMIC);
1683 if (!clone)
cec47e3d 1684 return BLKPREP_DEFER;
cec47e3d
KU
1685
1686 rq->special = clone;
1687 rq->cmd_flags |= REQ_DONTPREP;
1688
1689 return BLKPREP_OK;
1690}
1691
9eef87da
KU
1692/*
1693 * Returns:
1694 * 0 : the request has been processed (not requeued)
1695 * !0 : the request has been requeued
1696 */
1697static int map_request(struct dm_target *ti, struct request *clone,
1698 struct mapped_device *md)
cec47e3d 1699{
9eef87da 1700 int r, requeued = 0;
cec47e3d
KU
1701 struct dm_rq_target_io *tio = clone->end_io_data;
1702
cec47e3d
KU
1703 tio->ti = ti;
1704 r = ti->type->map_rq(ti, clone, &tio->info);
1705 switch (r) {
1706 case DM_MAPIO_SUBMITTED:
1707 /* The target has taken the I/O to submit by itself later */
1708 break;
1709 case DM_MAPIO_REMAPPED:
1710 /* The target has remapped the I/O so dispatch it */
6db4ccd6
JN
1711 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1712 blk_rq_pos(tio->orig));
cec47e3d
KU
1713 dm_dispatch_request(clone);
1714 break;
1715 case DM_MAPIO_REQUEUE:
1716 /* The target wants to requeue the I/O */
1717 dm_requeue_unmapped_request(clone);
9eef87da 1718 requeued = 1;
cec47e3d
KU
1719 break;
1720 default:
1721 if (r > 0) {
1722 DMWARN("unimplemented target map return value: %d", r);
1723 BUG();
1724 }
1725
1726 /* The target wants to complete the I/O */
1727 dm_kill_unmapped_request(clone, r);
1728 break;
1729 }
9eef87da
KU
1730
1731 return requeued;
cec47e3d
KU
1732}
1733
ba1cbad9
MS
1734static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1735{
1736 struct request *clone;
1737
1738 blk_start_request(orig);
1739 clone = orig->special;
1740 atomic_inc(&md->pending[rq_data_dir(clone)]);
1741
1742 /*
1743 * Hold the md reference here for the in-flight I/O.
1744 * We can't rely on the reference count by device opener,
1745 * because the device may be closed during the request completion
1746 * when all bios are completed.
1747 * See the comment in rq_completed() too.
1748 */
1749 dm_get(md);
1750
1751 return clone;
1752}
1753
cec47e3d
KU
1754/*
1755 * q->request_fn for request-based dm.
1756 * Called with the queue lock held.
1757 */
1758static void dm_request_fn(struct request_queue *q)
1759{
1760 struct mapped_device *md = q->queuedata;
83d5e5b0
MP
1761 int srcu_idx;
1762 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
cec47e3d 1763 struct dm_target *ti;
b4324fee 1764 struct request *rq, *clone;
29e4013d 1765 sector_t pos;
cec47e3d
KU
1766
1767 /*
b4324fee
KU
1768 * For suspend, check blk_queue_stopped() and increment
1769 * ->pending within a single queue_lock not to increment the
1770 * number of in-flight I/Os after the queue is stopped in
1771 * dm_suspend().
cec47e3d 1772 */
7eaceacc 1773 while (!blk_queue_stopped(q)) {
cec47e3d
KU
1774 rq = blk_peek_request(q);
1775 if (!rq)
7eaceacc 1776 goto delay_and_out;
cec47e3d 1777
29e4013d
TH
1778 /* always use block 0 to find the target for flushes for now */
1779 pos = 0;
1780 if (!(rq->cmd_flags & REQ_FLUSH))
1781 pos = blk_rq_pos(rq);
1782
1783 ti = dm_table_find_target(map, pos);
ba1cbad9
MS
1784 if (!dm_target_is_valid(ti)) {
1785 /*
1786 * Must perform setup, that dm_done() requires,
1787 * before calling dm_kill_unmapped_request
1788 */
1789 DMERR_LIMIT("request attempted access beyond the end of device");
1790 clone = dm_start_request(md, rq);
1791 dm_kill_unmapped_request(clone, -EIO);
1792 continue;
1793 }
d0bcb878 1794
cec47e3d 1795 if (ti->type->busy && ti->type->busy(ti))
7eaceacc 1796 goto delay_and_out;
cec47e3d 1797
ba1cbad9 1798 clone = dm_start_request(md, rq);
b4324fee 1799
cec47e3d 1800 spin_unlock(q->queue_lock);
9eef87da
KU
1801 if (map_request(ti, clone, md))
1802 goto requeued;
1803
052189a2
KU
1804 BUG_ON(!irqs_disabled());
1805 spin_lock(q->queue_lock);
cec47e3d
KU
1806 }
1807
1808 goto out;
1809
9eef87da 1810requeued:
052189a2
KU
1811 BUG_ON(!irqs_disabled());
1812 spin_lock(q->queue_lock);
9eef87da 1813
7eaceacc
JA
1814delay_and_out:
1815 blk_delay_queue(q, HZ / 10);
cec47e3d 1816out:
83d5e5b0 1817 dm_put_live_table(md, srcu_idx);
cec47e3d
KU
1818}
1819
1820int dm_underlying_device_busy(struct request_queue *q)
1821{
1822 return blk_lld_busy(q);
1823}
1824EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1825
1826static int dm_lld_busy(struct request_queue *q)
1827{
1828 int r;
1829 struct mapped_device *md = q->queuedata;
83d5e5b0 1830 struct dm_table *map = dm_get_live_table_fast(md);
cec47e3d
KU
1831
1832 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1833 r = 1;
1834 else
1835 r = dm_table_any_busy_target(map);
1836
83d5e5b0 1837 dm_put_live_table_fast(md);
cec47e3d
KU
1838
1839 return r;
1840}
1841
1da177e4
LT
1842static int dm_any_congested(void *congested_data, int bdi_bits)
1843{
8a57dfc6
CS
1844 int r = bdi_bits;
1845 struct mapped_device *md = congested_data;
1846 struct dm_table *map;
1da177e4 1847
1eb787ec 1848 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
83d5e5b0 1849 map = dm_get_live_table_fast(md);
8a57dfc6 1850 if (map) {
cec47e3d
KU
1851 /*
1852 * Request-based dm cares about only own queue for
1853 * the query about congestion status of request_queue
1854 */
1855 if (dm_request_based(md))
1856 r = md->queue->backing_dev_info.state &
1857 bdi_bits;
1858 else
1859 r = dm_table_any_congested(map, bdi_bits);
8a57dfc6 1860 }
83d5e5b0 1861 dm_put_live_table_fast(md);
8a57dfc6
CS
1862 }
1863
1da177e4
LT
1864 return r;
1865}
1866
1867/*-----------------------------------------------------------------
1868 * An IDR is used to keep track of allocated minor numbers.
1869 *---------------------------------------------------------------*/
2b06cfff 1870static void free_minor(int minor)
1da177e4 1871{
f32c10b0 1872 spin_lock(&_minor_lock);
1da177e4 1873 idr_remove(&_minor_idr, minor);
f32c10b0 1874 spin_unlock(&_minor_lock);
1da177e4
LT
1875}
1876
1877/*
1878 * See if the device with a specific minor # is free.
1879 */
cf13ab8e 1880static int specific_minor(int minor)
1da177e4 1881{
c9d76be6 1882 int r;
1da177e4
LT
1883
1884 if (minor >= (1 << MINORBITS))
1885 return -EINVAL;
1886
c9d76be6 1887 idr_preload(GFP_KERNEL);
f32c10b0 1888 spin_lock(&_minor_lock);
1da177e4 1889
c9d76be6 1890 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
1da177e4 1891
f32c10b0 1892 spin_unlock(&_minor_lock);
c9d76be6
TH
1893 idr_preload_end();
1894 if (r < 0)
1895 return r == -ENOSPC ? -EBUSY : r;
1896 return 0;
1da177e4
LT
1897}
1898
cf13ab8e 1899static int next_free_minor(int *minor)
1da177e4 1900{
c9d76be6 1901 int r;
62f75c2f 1902
c9d76be6 1903 idr_preload(GFP_KERNEL);
f32c10b0 1904 spin_lock(&_minor_lock);
1da177e4 1905
c9d76be6 1906 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
1da177e4 1907
f32c10b0 1908 spin_unlock(&_minor_lock);
c9d76be6
TH
1909 idr_preload_end();
1910 if (r < 0)
1911 return r;
1912 *minor = r;
1913 return 0;
1da177e4
LT
1914}
1915
83d5cde4 1916static const struct block_device_operations dm_blk_dops;
1da177e4 1917
53d5914f
MP
1918static void dm_wq_work(struct work_struct *work);
1919
4a0b4ddf
MS
1920static void dm_init_md_queue(struct mapped_device *md)
1921{
1922 /*
1923 * Request-based dm devices cannot be stacked on top of bio-based dm
1924 * devices. The type of this dm device has not been decided yet.
1925 * The type is decided at the first table loading time.
1926 * To prevent problematic device stacking, clear the queue flag
1927 * for request stacking support until then.
1928 *
1929 * This queue is new, so no concurrency on the queue_flags.
1930 */
1931 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1932
1933 md->queue->queuedata = md;
1934 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1935 md->queue->backing_dev_info.congested_data = md;
1936 blk_queue_make_request(md->queue, dm_request);
1937 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
4a0b4ddf
MS
1938 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1939}
1940
1da177e4
LT
1941/*
1942 * Allocate and initialise a blank device with a given minor.
1943 */
2b06cfff 1944static struct mapped_device *alloc_dev(int minor)
1da177e4
LT
1945{
1946 int r;
cf13ab8e 1947 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
ba61fdd1 1948 void *old_md;
1da177e4
LT
1949
1950 if (!md) {
1951 DMWARN("unable to allocate device, out of memory.");
1952 return NULL;
1953 }
1954
10da4f79 1955 if (!try_module_get(THIS_MODULE))
6ed7ade8 1956 goto bad_module_get;
10da4f79 1957
1da177e4 1958 /* get a minor number for the dev */
2b06cfff 1959 if (minor == DM_ANY_MINOR)
cf13ab8e 1960 r = next_free_minor(&minor);
2b06cfff 1961 else
cf13ab8e 1962 r = specific_minor(minor);
1da177e4 1963 if (r < 0)
6ed7ade8 1964 goto bad_minor;
1da177e4 1965
83d5e5b0
MP
1966 r = init_srcu_struct(&md->io_barrier);
1967 if (r < 0)
1968 goto bad_io_barrier;
1969
a5664dad 1970 md->type = DM_TYPE_NONE;
e61290a4 1971 mutex_init(&md->suspend_lock);
a5664dad 1972 mutex_init(&md->type_lock);
022c2611 1973 spin_lock_init(&md->deferred_lock);
1da177e4 1974 atomic_set(&md->holders, 1);
5c6bd75d 1975 atomic_set(&md->open_count, 0);
1da177e4 1976 atomic_set(&md->event_nr, 0);
7a8c3d3b
MA
1977 atomic_set(&md->uevent_seq, 0);
1978 INIT_LIST_HEAD(&md->uevent_list);
1979 spin_lock_init(&md->uevent_lock);
1da177e4 1980
4a0b4ddf 1981 md->queue = blk_alloc_queue(GFP_KERNEL);
1da177e4 1982 if (!md->queue)
6ed7ade8 1983 goto bad_queue;
1da177e4 1984
4a0b4ddf 1985 dm_init_md_queue(md);
9faf400f 1986
1da177e4
LT
1987 md->disk = alloc_disk(1);
1988 if (!md->disk)
6ed7ade8 1989 goto bad_disk;
1da177e4 1990
316d315b
NK
1991 atomic_set(&md->pending[0], 0);
1992 atomic_set(&md->pending[1], 0);
f0b04115 1993 init_waitqueue_head(&md->wait);
53d5914f 1994 INIT_WORK(&md->work, dm_wq_work);
f0b04115
JM
1995 init_waitqueue_head(&md->eventq);
1996
1da177e4
LT
1997 md->disk->major = _major;
1998 md->disk->first_minor = minor;
1999 md->disk->fops = &dm_blk_dops;
2000 md->disk->queue = md->queue;
2001 md->disk->private_data = md;
2002 sprintf(md->disk->disk_name, "dm-%d", minor);
2003 add_disk(md->disk);
7e51f257 2004 format_dev_t(md->name, MKDEV(_major, minor));
1da177e4 2005
670368a8 2006 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
304f3f6a
MB
2007 if (!md->wq)
2008 goto bad_thread;
2009
32a926da
MP
2010 md->bdev = bdget_disk(md->disk, 0);
2011 if (!md->bdev)
2012 goto bad_bdev;
2013
6a8736d1
TH
2014 bio_init(&md->flush_bio);
2015 md->flush_bio.bi_bdev = md->bdev;
2016 md->flush_bio.bi_rw = WRITE_FLUSH;
2017
fd2ed4d2
MP
2018 dm_stats_init(&md->stats);
2019
ba61fdd1 2020 /* Populate the mapping, nobody knows we exist yet */
f32c10b0 2021 spin_lock(&_minor_lock);
ba61fdd1 2022 old_md = idr_replace(&_minor_idr, md, minor);
f32c10b0 2023 spin_unlock(&_minor_lock);
ba61fdd1
JM
2024
2025 BUG_ON(old_md != MINOR_ALLOCED);
2026
1da177e4
LT
2027 return md;
2028
32a926da
MP
2029bad_bdev:
2030 destroy_workqueue(md->wq);
304f3f6a 2031bad_thread:
03022c54 2032 del_gendisk(md->disk);
304f3f6a 2033 put_disk(md->disk);
6ed7ade8 2034bad_disk:
1312f40e 2035 blk_cleanup_queue(md->queue);
6ed7ade8 2036bad_queue:
83d5e5b0
MP
2037 cleanup_srcu_struct(&md->io_barrier);
2038bad_io_barrier:
1da177e4 2039 free_minor(minor);
6ed7ade8 2040bad_minor:
10da4f79 2041 module_put(THIS_MODULE);
6ed7ade8 2042bad_module_get:
1da177e4
LT
2043 kfree(md);
2044 return NULL;
2045}
2046
ae9da83f
JN
2047static void unlock_fs(struct mapped_device *md);
2048
1da177e4
LT
2049static void free_dev(struct mapped_device *md)
2050{
f331c029 2051 int minor = MINOR(disk_devt(md->disk));
63d94e48 2052
32a926da
MP
2053 unlock_fs(md);
2054 bdput(md->bdev);
304f3f6a 2055 destroy_workqueue(md->wq);
e6ee8c0b
KU
2056 if (md->io_pool)
2057 mempool_destroy(md->io_pool);
2058 if (md->bs)
2059 bioset_free(md->bs);
9c47008d 2060 blk_integrity_unregister(md->disk);
1da177e4 2061 del_gendisk(md->disk);
83d5e5b0 2062 cleanup_srcu_struct(&md->io_barrier);
63d94e48 2063 free_minor(minor);
fba9f90e
JM
2064
2065 spin_lock(&_minor_lock);
2066 md->disk->private_data = NULL;
2067 spin_unlock(&_minor_lock);
2068
1da177e4 2069 put_disk(md->disk);
1312f40e 2070 blk_cleanup_queue(md->queue);
fd2ed4d2 2071 dm_stats_cleanup(&md->stats);
10da4f79 2072 module_put(THIS_MODULE);
1da177e4
LT
2073 kfree(md);
2074}
2075
e6ee8c0b
KU
2076static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2077{
c0820cf5 2078 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
e6ee8c0b 2079
5f015204 2080 if (md->io_pool && md->bs) {
16245bdc
JN
2081 /* The md already has necessary mempools. */
2082 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2083 /*
2084 * Reload bioset because front_pad may have changed
2085 * because a different table was loaded.
2086 */
2087 bioset_free(md->bs);
2088 md->bs = p->bs;
2089 p->bs = NULL;
2090 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
16245bdc
JN
2091 /*
2092 * There's no need to reload with request-based dm
2093 * because the size of front_pad doesn't change.
2094 * Note for future: If you are to reload bioset,
2095 * prep-ed requests in the queue may refer
2096 * to bio from the old bioset, so you must walk
2097 * through the queue to unprep.
2098 */
2099 }
e6ee8c0b 2100 goto out;
c0820cf5 2101 }
e6ee8c0b 2102
5f015204 2103 BUG_ON(!p || md->io_pool || md->bs);
e6ee8c0b
KU
2104
2105 md->io_pool = p->io_pool;
2106 p->io_pool = NULL;
e6ee8c0b
KU
2107 md->bs = p->bs;
2108 p->bs = NULL;
2109
2110out:
2111 /* mempool bind completed, now no need any mempools in the table */
2112 dm_table_free_md_mempools(t);
2113}
2114
1da177e4
LT
2115/*
2116 * Bind a table to the device.
2117 */
2118static void event_callback(void *context)
2119{
7a8c3d3b
MA
2120 unsigned long flags;
2121 LIST_HEAD(uevents);
1da177e4
LT
2122 struct mapped_device *md = (struct mapped_device *) context;
2123
7a8c3d3b
MA
2124 spin_lock_irqsave(&md->uevent_lock, flags);
2125 list_splice_init(&md->uevent_list, &uevents);
2126 spin_unlock_irqrestore(&md->uevent_lock, flags);
2127
ed9e1982 2128 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
7a8c3d3b 2129
1da177e4
LT
2130 atomic_inc(&md->event_nr);
2131 wake_up(&md->eventq);
2132}
2133
c217649b
MS
2134/*
2135 * Protected by md->suspend_lock obtained by dm_swap_table().
2136 */
4e90188b 2137static void __set_size(struct mapped_device *md, sector_t size)
1da177e4 2138{
4e90188b 2139 set_capacity(md->disk, size);
1da177e4 2140
db8fef4f 2141 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1da177e4
LT
2142}
2143
d5b9dd04
MP
2144/*
2145 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2146 *
2147 * If this function returns 0, then the device is either a non-dm
2148 * device without a merge_bvec_fn, or it is a dm device that is
2149 * able to split any bios it receives that are too big.
2150 */
2151int dm_queue_merge_is_compulsory(struct request_queue *q)
2152{
2153 struct mapped_device *dev_md;
2154
2155 if (!q->merge_bvec_fn)
2156 return 0;
2157
2158 if (q->make_request_fn == dm_request) {
2159 dev_md = q->queuedata;
2160 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2161 return 0;
2162 }
2163
2164 return 1;
2165}
2166
2167static int dm_device_merge_is_compulsory(struct dm_target *ti,
2168 struct dm_dev *dev, sector_t start,
2169 sector_t len, void *data)
2170{
2171 struct block_device *bdev = dev->bdev;
2172 struct request_queue *q = bdev_get_queue(bdev);
2173
2174 return dm_queue_merge_is_compulsory(q);
2175}
2176
2177/*
2178 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2179 * on the properties of the underlying devices.
2180 */
2181static int dm_table_merge_is_optional(struct dm_table *table)
2182{
2183 unsigned i = 0;
2184 struct dm_target *ti;
2185
2186 while (i < dm_table_get_num_targets(table)) {
2187 ti = dm_table_get_target(table, i++);
2188
2189 if (ti->type->iterate_devices &&
2190 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2191 return 0;
2192 }
2193
2194 return 1;
2195}
2196
042d2a9b
AK
2197/*
2198 * Returns old map, which caller must destroy.
2199 */
2200static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2201 struct queue_limits *limits)
1da177e4 2202{
042d2a9b 2203 struct dm_table *old_map;
165125e1 2204 struct request_queue *q = md->queue;
1da177e4 2205 sector_t size;
d5b9dd04 2206 int merge_is_optional;
1da177e4
LT
2207
2208 size = dm_table_get_size(t);
3ac51e74
DW
2209
2210 /*
2211 * Wipe any geometry if the size of the table changed.
2212 */
fd2ed4d2 2213 if (size != dm_get_size(md))
3ac51e74
DW
2214 memset(&md->geometry, 0, sizeof(md->geometry));
2215
32a926da 2216 __set_size(md, size);
d5816876 2217
2ca3310e
AK
2218 dm_table_event_callback(t, event_callback, md);
2219
e6ee8c0b
KU
2220 /*
2221 * The queue hasn't been stopped yet, if the old table type wasn't
2222 * for request-based during suspension. So stop it to prevent
2223 * I/O mapping before resume.
2224 * This must be done before setting the queue restrictions,
2225 * because request-based dm may be run just after the setting.
2226 */
2227 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2228 stop_queue(q);
2229
2230 __bind_mempools(md, t);
2231
d5b9dd04
MP
2232 merge_is_optional = dm_table_merge_is_optional(t);
2233
042d2a9b 2234 old_map = md->map;
83d5e5b0 2235 rcu_assign_pointer(md->map, t);
36a0456f
AK
2236 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2237
754c5fc7 2238 dm_table_set_restrictions(t, q, limits);
d5b9dd04
MP
2239 if (merge_is_optional)
2240 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2241 else
2242 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
83d5e5b0 2243 dm_sync_table(md);
1da177e4 2244
042d2a9b 2245 return old_map;
1da177e4
LT
2246}
2247
a7940155
AK
2248/*
2249 * Returns unbound table for the caller to free.
2250 */
2251static struct dm_table *__unbind(struct mapped_device *md)
1da177e4
LT
2252{
2253 struct dm_table *map = md->map;
2254
2255 if (!map)
a7940155 2256 return NULL;
1da177e4
LT
2257
2258 dm_table_event_callback(map, NULL, NULL);
83d5e5b0
MP
2259 rcu_assign_pointer(md->map, NULL);
2260 dm_sync_table(md);
a7940155
AK
2261
2262 return map;
1da177e4
LT
2263}
2264
2265/*
2266 * Constructor for a new device.
2267 */
2b06cfff 2268int dm_create(int minor, struct mapped_device **result)
1da177e4
LT
2269{
2270 struct mapped_device *md;
2271
2b06cfff 2272 md = alloc_dev(minor);
1da177e4
LT
2273 if (!md)
2274 return -ENXIO;
2275
784aae73
MB
2276 dm_sysfs_init(md);
2277
1da177e4
LT
2278 *result = md;
2279 return 0;
2280}
2281
a5664dad
MS
2282/*
2283 * Functions to manage md->type.
2284 * All are required to hold md->type_lock.
2285 */
2286void dm_lock_md_type(struct mapped_device *md)
2287{
2288 mutex_lock(&md->type_lock);
2289}
2290
2291void dm_unlock_md_type(struct mapped_device *md)
2292{
2293 mutex_unlock(&md->type_lock);
2294}
2295
2296void dm_set_md_type(struct mapped_device *md, unsigned type)
2297{
00c4fc3b 2298 BUG_ON(!mutex_is_locked(&md->type_lock));
a5664dad
MS
2299 md->type = type;
2300}
2301
2302unsigned dm_get_md_type(struct mapped_device *md)
2303{
00c4fc3b 2304 BUG_ON(!mutex_is_locked(&md->type_lock));
a5664dad
MS
2305 return md->type;
2306}
2307
36a0456f
AK
2308struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2309{
2310 return md->immutable_target_type;
2311}
2312
f84cb8a4
MS
2313/*
2314 * The queue_limits are only valid as long as you have a reference
2315 * count on 'md'.
2316 */
2317struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2318{
2319 BUG_ON(!atomic_read(&md->holders));
2320 return &md->queue->limits;
2321}
2322EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2323
4a0b4ddf
MS
2324/*
2325 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2326 */
2327static int dm_init_request_based_queue(struct mapped_device *md)
2328{
2329 struct request_queue *q = NULL;
2330
2331 if (md->queue->elevator)
2332 return 1;
2333
2334 /* Fully initialize the queue */
2335 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2336 if (!q)
2337 return 0;
2338
2339 md->queue = q;
4a0b4ddf
MS
2340 dm_init_md_queue(md);
2341 blk_queue_softirq_done(md->queue, dm_softirq_done);
2342 blk_queue_prep_rq(md->queue, dm_prep_fn);
2343 blk_queue_lld_busy(md->queue, dm_lld_busy);
4a0b4ddf
MS
2344
2345 elv_register_queue(md->queue);
2346
2347 return 1;
2348}
2349
2350/*
2351 * Setup the DM device's queue based on md's type
2352 */
2353int dm_setup_md_queue(struct mapped_device *md)
2354{
2355 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2356 !dm_init_request_based_queue(md)) {
2357 DMWARN("Cannot initialize queue for request-based mapped device");
2358 return -EINVAL;
2359 }
2360
2361 return 0;
2362}
2363
637842cf 2364static struct mapped_device *dm_find_md(dev_t dev)
1da177e4
LT
2365{
2366 struct mapped_device *md;
1da177e4
LT
2367 unsigned minor = MINOR(dev);
2368
2369 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2370 return NULL;
2371
f32c10b0 2372 spin_lock(&_minor_lock);
1da177e4
LT
2373
2374 md = idr_find(&_minor_idr, minor);
fba9f90e 2375 if (md && (md == MINOR_ALLOCED ||
f331c029 2376 (MINOR(disk_devt(dm_disk(md))) != minor) ||
abdc568b 2377 dm_deleting_md(md) ||
17b2f66f 2378 test_bit(DMF_FREEING, &md->flags))) {
637842cf 2379 md = NULL;
fba9f90e
JM
2380 goto out;
2381 }
1da177e4 2382
fba9f90e 2383out:
f32c10b0 2384 spin_unlock(&_minor_lock);
1da177e4 2385
637842cf
DT
2386 return md;
2387}
2388
d229a958
DT
2389struct mapped_device *dm_get_md(dev_t dev)
2390{
2391 struct mapped_device *md = dm_find_md(dev);
2392
2393 if (md)
2394 dm_get(md);
2395
2396 return md;
2397}
3cf2e4ba 2398EXPORT_SYMBOL_GPL(dm_get_md);
d229a958 2399
9ade92a9 2400void *dm_get_mdptr(struct mapped_device *md)
637842cf 2401{
9ade92a9 2402 return md->interface_ptr;
1da177e4
LT
2403}
2404
2405void dm_set_mdptr(struct mapped_device *md, void *ptr)
2406{
2407 md->interface_ptr = ptr;
2408}
2409
2410void dm_get(struct mapped_device *md)
2411{
2412 atomic_inc(&md->holders);
3f77316d 2413 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1da177e4
LT
2414}
2415
72d94861
AK
2416const char *dm_device_name(struct mapped_device *md)
2417{
2418 return md->name;
2419}
2420EXPORT_SYMBOL_GPL(dm_device_name);
2421
3f77316d 2422static void __dm_destroy(struct mapped_device *md, bool wait)
1da177e4 2423{
1134e5ae 2424 struct dm_table *map;
83d5e5b0 2425 int srcu_idx;
1da177e4 2426
3f77316d 2427 might_sleep();
fba9f90e 2428
3f77316d 2429 spin_lock(&_minor_lock);
83d5e5b0 2430 map = dm_get_live_table(md, &srcu_idx);
3f77316d
KU
2431 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2432 set_bit(DMF_FREEING, &md->flags);
2433 spin_unlock(&_minor_lock);
2434
2435 if (!dm_suspended_md(md)) {
2436 dm_table_presuspend_targets(map);
2437 dm_table_postsuspend_targets(map);
1da177e4 2438 }
3f77316d 2439
83d5e5b0
MP
2440 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2441 dm_put_live_table(md, srcu_idx);
2442
3f77316d
KU
2443 /*
2444 * Rare, but there may be I/O requests still going to complete,
2445 * for example. Wait for all references to disappear.
2446 * No one should increment the reference count of the mapped_device,
2447 * after the mapped_device state becomes DMF_FREEING.
2448 */
2449 if (wait)
2450 while (atomic_read(&md->holders))
2451 msleep(1);
2452 else if (atomic_read(&md->holders))
2453 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2454 dm_device_name(md), atomic_read(&md->holders));
2455
2456 dm_sysfs_exit(md);
3f77316d
KU
2457 dm_table_destroy(__unbind(md));
2458 free_dev(md);
2459}
2460
2461void dm_destroy(struct mapped_device *md)
2462{
2463 __dm_destroy(md, true);
2464}
2465
2466void dm_destroy_immediate(struct mapped_device *md)
2467{
2468 __dm_destroy(md, false);
2469}
2470
2471void dm_put(struct mapped_device *md)
2472{
2473 atomic_dec(&md->holders);
1da177e4 2474}
79eb885c 2475EXPORT_SYMBOL_GPL(dm_put);
1da177e4 2476
401600df 2477static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
46125c1c
MB
2478{
2479 int r = 0;
b44ebeb0
MP
2480 DECLARE_WAITQUEUE(wait, current);
2481
b44ebeb0 2482 add_wait_queue(&md->wait, &wait);
46125c1c
MB
2483
2484 while (1) {
401600df 2485 set_current_state(interruptible);
46125c1c 2486
b4324fee 2487 if (!md_in_flight(md))
46125c1c
MB
2488 break;
2489
401600df
MP
2490 if (interruptible == TASK_INTERRUPTIBLE &&
2491 signal_pending(current)) {
46125c1c
MB
2492 r = -EINTR;
2493 break;
2494 }
2495
2496 io_schedule();
2497 }
2498 set_current_state(TASK_RUNNING);
2499
b44ebeb0
MP
2500 remove_wait_queue(&md->wait, &wait);
2501
46125c1c
MB
2502 return r;
2503}
2504
1da177e4
LT
2505/*
2506 * Process the deferred bios
2507 */
ef208587 2508static void dm_wq_work(struct work_struct *work)
1da177e4 2509{
ef208587
MP
2510 struct mapped_device *md = container_of(work, struct mapped_device,
2511 work);
6d6f10df 2512 struct bio *c;
83d5e5b0
MP
2513 int srcu_idx;
2514 struct dm_table *map;
1da177e4 2515
83d5e5b0 2516 map = dm_get_live_table(md, &srcu_idx);
ef208587 2517
3b00b203 2518 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
df12ee99
AK
2519 spin_lock_irq(&md->deferred_lock);
2520 c = bio_list_pop(&md->deferred);
2521 spin_unlock_irq(&md->deferred_lock);
2522
6a8736d1 2523 if (!c)
df12ee99 2524 break;
022c2611 2525
e6ee8c0b
KU
2526 if (dm_request_based(md))
2527 generic_make_request(c);
6a8736d1 2528 else
83d5e5b0 2529 __split_and_process_bio(md, map, c);
022c2611 2530 }
73d410c0 2531
83d5e5b0 2532 dm_put_live_table(md, srcu_idx);
1da177e4
LT
2533}
2534
9a1fb464 2535static void dm_queue_flush(struct mapped_device *md)
304f3f6a 2536{
3b00b203
MP
2537 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2538 smp_mb__after_clear_bit();
53d5914f 2539 queue_work(md->wq, &md->work);
304f3f6a
MB
2540}
2541
1da177e4 2542/*
042d2a9b 2543 * Swap in a new table, returning the old one for the caller to destroy.
1da177e4 2544 */
042d2a9b 2545struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
1da177e4 2546{
87eb5b21 2547 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
754c5fc7 2548 struct queue_limits limits;
042d2a9b 2549 int r;
1da177e4 2550
e61290a4 2551 mutex_lock(&md->suspend_lock);
1da177e4
LT
2552
2553 /* device must be suspended */
4f186f8b 2554 if (!dm_suspended_md(md))
93c534ae 2555 goto out;
1da177e4 2556
3ae70656
MS
2557 /*
2558 * If the new table has no data devices, retain the existing limits.
2559 * This helps multipath with queue_if_no_path if all paths disappear,
2560 * then new I/O is queued based on these limits, and then some paths
2561 * reappear.
2562 */
2563 if (dm_table_has_no_data_devices(table)) {
83d5e5b0 2564 live_map = dm_get_live_table_fast(md);
3ae70656
MS
2565 if (live_map)
2566 limits = md->queue->limits;
83d5e5b0 2567 dm_put_live_table_fast(md);
3ae70656
MS
2568 }
2569
87eb5b21
MC
2570 if (!live_map) {
2571 r = dm_calculate_queue_limits(table, &limits);
2572 if (r) {
2573 map = ERR_PTR(r);
2574 goto out;
2575 }
042d2a9b 2576 }
754c5fc7 2577
042d2a9b 2578 map = __bind(md, table, &limits);
1da177e4 2579
93c534ae 2580out:
e61290a4 2581 mutex_unlock(&md->suspend_lock);
042d2a9b 2582 return map;
1da177e4
LT
2583}
2584
2585/*
2586 * Functions to lock and unlock any filesystem running on the
2587 * device.
2588 */
2ca3310e 2589static int lock_fs(struct mapped_device *md)
1da177e4 2590{
e39e2e95 2591 int r;
1da177e4
LT
2592
2593 WARN_ON(md->frozen_sb);
dfbe03f6 2594
db8fef4f 2595 md->frozen_sb = freeze_bdev(md->bdev);
dfbe03f6 2596 if (IS_ERR(md->frozen_sb)) {
cf222b37 2597 r = PTR_ERR(md->frozen_sb);
e39e2e95
AK
2598 md->frozen_sb = NULL;
2599 return r;
dfbe03f6
AK
2600 }
2601
aa8d7c2f
AK
2602 set_bit(DMF_FROZEN, &md->flags);
2603
1da177e4
LT
2604 return 0;
2605}
2606
2ca3310e 2607static void unlock_fs(struct mapped_device *md)
1da177e4 2608{
aa8d7c2f
AK
2609 if (!test_bit(DMF_FROZEN, &md->flags))
2610 return;
2611
db8fef4f 2612 thaw_bdev(md->bdev, md->frozen_sb);
1da177e4 2613 md->frozen_sb = NULL;
aa8d7c2f 2614 clear_bit(DMF_FROZEN, &md->flags);
1da177e4
LT
2615}
2616
2617/*
2618 * We need to be able to change a mapping table under a mounted
2619 * filesystem. For example we might want to move some data in
2620 * the background. Before the table can be swapped with
2621 * dm_bind_table, dm_suspend must be called to flush any in
2622 * flight bios and ensure that any further io gets deferred.
2623 */
cec47e3d
KU
2624/*
2625 * Suspend mechanism in request-based dm.
2626 *
9f518b27
KU
2627 * 1. Flush all I/Os by lock_fs() if needed.
2628 * 2. Stop dispatching any I/O by stopping the request_queue.
2629 * 3. Wait for all in-flight I/Os to be completed or requeued.
cec47e3d 2630 *
9f518b27 2631 * To abort suspend, start the request_queue.
cec47e3d 2632 */
a3d77d35 2633int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1da177e4 2634{
2ca3310e 2635 struct dm_table *map = NULL;
46125c1c 2636 int r = 0;
a3d77d35 2637 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2e93ccc1 2638 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1da177e4 2639
e61290a4 2640 mutex_lock(&md->suspend_lock);
2ca3310e 2641
4f186f8b 2642 if (dm_suspended_md(md)) {
73d410c0 2643 r = -EINVAL;
d287483d 2644 goto out_unlock;
73d410c0 2645 }
1da177e4 2646
83d5e5b0 2647 map = md->map;
1da177e4 2648
2e93ccc1
KU
2649 /*
2650 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2651 * This flag is cleared before dm_suspend returns.
2652 */
2653 if (noflush)
2654 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2655
cf222b37
AK
2656 /* This does not get reverted if there's an error later. */
2657 dm_table_presuspend_targets(map);
2658
32a926da 2659 /*
9f518b27
KU
2660 * Flush I/O to the device.
2661 * Any I/O submitted after lock_fs() may not be flushed.
2662 * noflush takes precedence over do_lockfs.
2663 * (lock_fs() flushes I/Os and waits for them to complete.)
32a926da
MP
2664 */
2665 if (!noflush && do_lockfs) {
2666 r = lock_fs(md);
2667 if (r)
83d5e5b0 2668 goto out_unlock;
aa8d7c2f 2669 }
1da177e4
LT
2670
2671 /*
3b00b203
MP
2672 * Here we must make sure that no processes are submitting requests
2673 * to target drivers i.e. no one may be executing
2674 * __split_and_process_bio. This is called from dm_request and
2675 * dm_wq_work.
2676 *
2677 * To get all processes out of __split_and_process_bio in dm_request,
2678 * we take the write lock. To prevent any process from reentering
6a8736d1
TH
2679 * __split_and_process_bio from dm_request and quiesce the thread
2680 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2681 * flush_workqueue(md->wq).
1da177e4 2682 */
1eb787ec 2683 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
83d5e5b0 2684 synchronize_srcu(&md->io_barrier);
1da177e4 2685
d0bcb878 2686 /*
29e4013d
TH
2687 * Stop md->queue before flushing md->wq in case request-based
2688 * dm defers requests to md->wq from md->queue.
d0bcb878 2689 */
cec47e3d 2690 if (dm_request_based(md))
9f518b27 2691 stop_queue(md->queue);
cec47e3d 2692
d0bcb878
KU
2693 flush_workqueue(md->wq);
2694
1da177e4 2695 /*
3b00b203
MP
2696 * At this point no more requests are entering target request routines.
2697 * We call dm_wait_for_completion to wait for all existing requests
2698 * to finish.
1da177e4 2699 */
401600df 2700 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
1da177e4 2701
6d6f10df 2702 if (noflush)
022c2611 2703 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
83d5e5b0 2704 synchronize_srcu(&md->io_barrier);
2e93ccc1 2705
1da177e4 2706 /* were we interrupted ? */
46125c1c 2707 if (r < 0) {
9a1fb464 2708 dm_queue_flush(md);
73d410c0 2709
cec47e3d 2710 if (dm_request_based(md))
9f518b27 2711 start_queue(md->queue);
cec47e3d 2712
2ca3310e 2713 unlock_fs(md);
83d5e5b0 2714 goto out_unlock; /* pushback list is already flushed, so skip flush */
2ca3310e 2715 }
1da177e4 2716
3b00b203
MP
2717 /*
2718 * If dm_wait_for_completion returned 0, the device is completely
2719 * quiescent now. There is no request-processing activity. All new
2720 * requests are being added to md->deferred list.
2721 */
2722
2ca3310e 2723 set_bit(DMF_SUSPENDED, &md->flags);
b84b0287 2724
4d4471cb
KU
2725 dm_table_postsuspend_targets(map);
2726
d287483d 2727out_unlock:
e61290a4 2728 mutex_unlock(&md->suspend_lock);
cf222b37 2729 return r;
1da177e4
LT
2730}
2731
2732int dm_resume(struct mapped_device *md)
2733{
cf222b37 2734 int r = -EINVAL;
cf222b37 2735 struct dm_table *map = NULL;
1da177e4 2736
e61290a4 2737 mutex_lock(&md->suspend_lock);
4f186f8b 2738 if (!dm_suspended_md(md))
cf222b37 2739 goto out;
cf222b37 2740
83d5e5b0 2741 map = md->map;
2ca3310e 2742 if (!map || !dm_table_get_size(map))
cf222b37 2743 goto out;
1da177e4 2744
8757b776
MB
2745 r = dm_table_resume_targets(map);
2746 if (r)
2747 goto out;
2ca3310e 2748
9a1fb464 2749 dm_queue_flush(md);
2ca3310e 2750
cec47e3d
KU
2751 /*
2752 * Flushing deferred I/Os must be done after targets are resumed
2753 * so that mapping of targets can work correctly.
2754 * Request-based dm is queueing the deferred I/Os in its request_queue.
2755 */
2756 if (dm_request_based(md))
2757 start_queue(md->queue);
2758
2ca3310e
AK
2759 unlock_fs(md);
2760
2761 clear_bit(DMF_SUSPENDED, &md->flags);
2762
cf222b37
AK
2763 r = 0;
2764out:
e61290a4 2765 mutex_unlock(&md->suspend_lock);
2ca3310e 2766
cf222b37 2767 return r;
1da177e4
LT
2768}
2769
fd2ed4d2
MP
2770/*
2771 * Internal suspend/resume works like userspace-driven suspend. It waits
2772 * until all bios finish and prevents issuing new bios to the target drivers.
2773 * It may be used only from the kernel.
2774 *
2775 * Internal suspend holds md->suspend_lock, which prevents interaction with
2776 * userspace-driven suspend.
2777 */
2778
2779void dm_internal_suspend(struct mapped_device *md)
2780{
2781 mutex_lock(&md->suspend_lock);
2782 if (dm_suspended_md(md))
2783 return;
2784
2785 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2786 synchronize_srcu(&md->io_barrier);
2787 flush_workqueue(md->wq);
2788 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2789}
2790
2791void dm_internal_resume(struct mapped_device *md)
2792{
2793 if (dm_suspended_md(md))
2794 goto done;
2795
2796 dm_queue_flush(md);
2797
2798done:
2799 mutex_unlock(&md->suspend_lock);
2800}
2801
1da177e4
LT
2802/*-----------------------------------------------------------------
2803 * Event notification.
2804 *---------------------------------------------------------------*/
3abf85b5 2805int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
60935eb2 2806 unsigned cookie)
69267a30 2807{
60935eb2
MB
2808 char udev_cookie[DM_COOKIE_LENGTH];
2809 char *envp[] = { udev_cookie, NULL };
2810
2811 if (!cookie)
3abf85b5 2812 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
60935eb2
MB
2813 else {
2814 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2815 DM_COOKIE_ENV_VAR_NAME, cookie);
3abf85b5
PR
2816 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2817 action, envp);
60935eb2 2818 }
69267a30
AK
2819}
2820
7a8c3d3b
MA
2821uint32_t dm_next_uevent_seq(struct mapped_device *md)
2822{
2823 return atomic_add_return(1, &md->uevent_seq);
2824}
2825
1da177e4
LT
2826uint32_t dm_get_event_nr(struct mapped_device *md)
2827{
2828 return atomic_read(&md->event_nr);
2829}
2830
2831int dm_wait_event(struct mapped_device *md, int event_nr)
2832{
2833 return wait_event_interruptible(md->eventq,
2834 (event_nr != atomic_read(&md->event_nr)));
2835}
2836
7a8c3d3b
MA
2837void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2838{
2839 unsigned long flags;
2840
2841 spin_lock_irqsave(&md->uevent_lock, flags);
2842 list_add(elist, &md->uevent_list);
2843 spin_unlock_irqrestore(&md->uevent_lock, flags);
2844}
2845
1da177e4
LT
2846/*
2847 * The gendisk is only valid as long as you have a reference
2848 * count on 'md'.
2849 */
2850struct gendisk *dm_disk(struct mapped_device *md)
2851{
2852 return md->disk;
2853}
2854
784aae73
MB
2855struct kobject *dm_kobject(struct mapped_device *md)
2856{
2857 return &md->kobj;
2858}
2859
2860/*
2861 * struct mapped_device should not be exported outside of dm.c
2862 * so use this check to verify that kobj is part of md structure
2863 */
2864struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2865{
2866 struct mapped_device *md;
2867
2868 md = container_of(kobj, struct mapped_device, kobj);
2869 if (&md->kobj != kobj)
2870 return NULL;
2871
4d89b7b4 2872 if (test_bit(DMF_FREEING, &md->flags) ||
432a212c 2873 dm_deleting_md(md))
4d89b7b4
MB
2874 return NULL;
2875
784aae73
MB
2876 dm_get(md);
2877 return md;
2878}
2879
4f186f8b 2880int dm_suspended_md(struct mapped_device *md)
1da177e4
LT
2881{
2882 return test_bit(DMF_SUSPENDED, &md->flags);
2883}
2884
64dbce58
KU
2885int dm_suspended(struct dm_target *ti)
2886{
ecdb2e25 2887 return dm_suspended_md(dm_table_get_md(ti->table));
64dbce58
KU
2888}
2889EXPORT_SYMBOL_GPL(dm_suspended);
2890
2e93ccc1
KU
2891int dm_noflush_suspending(struct dm_target *ti)
2892{
ecdb2e25 2893 return __noflush_suspending(dm_table_get_md(ti->table));
2e93ccc1
KU
2894}
2895EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2896
c0820cf5 2897struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
e6ee8c0b 2898{
5f015204
JN
2899 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
2900 struct kmem_cache *cachep;
2901 unsigned int pool_size;
2902 unsigned int front_pad;
e6ee8c0b
KU
2903
2904 if (!pools)
2905 return NULL;
2906
23e5083b 2907 if (type == DM_TYPE_BIO_BASED) {
5f015204 2908 cachep = _io_cache;
6cfa5857 2909 pool_size = RESERVED_BIO_BASED_IOS;
5f015204
JN
2910 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2911 } else if (type == DM_TYPE_REQUEST_BASED) {
2912 cachep = _rq_tio_cache;
f4790826 2913 pool_size = dm_get_reserved_rq_based_ios();
5f015204
JN
2914 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2915 /* per_bio_data_size is not used. See __bind_mempools(). */
2916 WARN_ON(per_bio_data_size != 0);
2917 } else
2918 goto out;
e6ee8c0b 2919
6cfa5857 2920 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
5f015204
JN
2921 if (!pools->io_pool)
2922 goto out;
e6ee8c0b 2923
5f015204 2924 pools->bs = bioset_create(pool_size, front_pad);
e6ee8c0b 2925 if (!pools->bs)
5f015204 2926 goto out;
e6ee8c0b 2927
a91a2785 2928 if (integrity && bioset_integrity_create(pools->bs, pool_size))
5f015204 2929 goto out;
a91a2785 2930
e6ee8c0b
KU
2931 return pools;
2932
5f015204
JN
2933out:
2934 dm_free_md_mempools(pools);
e6ee8c0b
KU
2935
2936 return NULL;
2937}
2938
2939void dm_free_md_mempools(struct dm_md_mempools *pools)
2940{
2941 if (!pools)
2942 return;
2943
2944 if (pools->io_pool)
2945 mempool_destroy(pools->io_pool);
2946
e6ee8c0b
KU
2947 if (pools->bs)
2948 bioset_free(pools->bs);
2949
2950 kfree(pools);
2951}
2952
83d5cde4 2953static const struct block_device_operations dm_blk_dops = {
1da177e4
LT
2954 .open = dm_blk_open,
2955 .release = dm_blk_close,
aa129a22 2956 .ioctl = dm_blk_ioctl,
3ac51e74 2957 .getgeo = dm_blk_getgeo,
1da177e4
LT
2958 .owner = THIS_MODULE
2959};
2960
2961EXPORT_SYMBOL(dm_get_mapinfo);
2962
2963/*
2964 * module hooks
2965 */
2966module_init(dm_init);
2967module_exit(dm_exit);
2968
2969module_param(major, uint, 0);
2970MODULE_PARM_DESC(major, "The major number of the device mapper");
f4790826
MS
2971
2972module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
2973MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
2974
1da177e4
LT
2975MODULE_DESCRIPTION(DM_NAME " driver");
2976MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2977MODULE_LICENSE("GPL");
This page took 1.000173 seconds and 5 git commands to generate.