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