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