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