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