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