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