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