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