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