2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
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>
21 #include <linux/hdreg.h>
23 #include <trace/events/block.h>
25 #define DM_MSG_PREFIX "core"
28 * Cookies are numeric values sent with CHANGE and REMOVE
29 * uevents while resuming, removing or renaming the device.
31 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
32 #define DM_COOKIE_LENGTH 24
34 static const char *_name = DM_NAME;
36 static unsigned int major = 0;
37 static unsigned int _major = 0;
39 static DEFINE_SPINLOCK(_minor_lock);
42 * One of these is allocated per bio.
45 struct mapped_device *md;
49 unsigned long start_time;
50 spinlock_t endio_lock;
55 * One of these is allocated per target within a bio. Hopefully
56 * this will be simplified out one day.
65 * For request-based dm.
66 * One of these is allocated per request.
68 struct dm_rq_target_io {
69 struct mapped_device *md;
71 struct request *orig, clone;
77 * For request-based dm.
78 * One of these is allocated per bio.
80 struct dm_rq_clone_bio_info {
82 struct dm_rq_target_io *tio;
85 union map_info *dm_get_mapinfo(struct bio *bio)
87 if (bio && bio->bi_private)
88 return &((struct dm_target_io *)bio->bi_private)->info;
92 union map_info *dm_get_rq_mapinfo(struct request *rq)
94 if (rq && rq->end_io_data)
95 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
98 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
100 #define MINOR_ALLOCED ((void *)-1)
103 * Bits for the md->flags field.
105 #define DMF_BLOCK_IO_FOR_SUSPEND 0
106 #define DMF_SUSPENDED 1
108 #define DMF_FREEING 3
109 #define DMF_DELETING 4
110 #define DMF_NOFLUSH_SUSPENDING 5
111 #define DMF_QUEUE_IO_TO_THREAD 6
114 * Work processed by per-device workqueue.
116 struct mapped_device {
117 struct rw_semaphore io_lock;
118 struct mutex suspend_lock;
125 struct request_queue *queue;
126 struct gendisk *disk;
132 * A list of ios that arrived while we were suspended.
135 wait_queue_head_t wait;
136 struct work_struct work;
137 struct bio_list deferred;
138 spinlock_t deferred_lock;
141 * An error from the barrier request currently being processed.
146 * Protect barrier_error from concurrent endio processing
147 * in request-based dm.
149 spinlock_t barrier_error_lock;
152 * Processing queue (flush/barriers)
154 struct workqueue_struct *wq;
155 struct work_struct barrier_work;
157 /* A pointer to the currently processing pre/post flush request */
158 struct request *flush_request;
161 * The current mapping.
163 struct dm_table *map;
166 * io objects are allocated from here.
177 wait_queue_head_t eventq;
179 struct list_head uevent_list;
180 spinlock_t uevent_lock; /* Protect access to uevent_list */
183 * freeze/thaw support require holding onto a super block
185 struct super_block *frozen_sb;
186 struct block_device *bdev;
188 /* forced geometry settings */
189 struct hd_geometry geometry;
191 /* For saving the address of __make_request for request based dm */
192 make_request_fn *saved_make_request_fn;
197 /* zero-length barrier that will be cloned and submitted to targets */
198 struct bio barrier_bio;
202 * For mempools pre-allocation at the table loading time.
204 struct dm_md_mempools {
211 static struct kmem_cache *_io_cache;
212 static struct kmem_cache *_tio_cache;
213 static struct kmem_cache *_rq_tio_cache;
214 static struct kmem_cache *_rq_bio_info_cache;
216 static int __init local_init(void)
220 /* allocate a slab for the dm_ios */
221 _io_cache = KMEM_CACHE(dm_io, 0);
225 /* allocate a slab for the target ios */
226 _tio_cache = KMEM_CACHE(dm_target_io, 0);
228 goto out_free_io_cache;
230 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
232 goto out_free_tio_cache;
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;
238 r = dm_uevent_init();
240 goto out_free_rq_bio_info_cache;
243 r = register_blkdev(_major, _name);
245 goto out_uevent_exit;
254 out_free_rq_bio_info_cache:
255 kmem_cache_destroy(_rq_bio_info_cache);
256 out_free_rq_tio_cache:
257 kmem_cache_destroy(_rq_tio_cache);
259 kmem_cache_destroy(_tio_cache);
261 kmem_cache_destroy(_io_cache);
266 static void local_exit(void)
268 kmem_cache_destroy(_rq_bio_info_cache);
269 kmem_cache_destroy(_rq_tio_cache);
270 kmem_cache_destroy(_tio_cache);
271 kmem_cache_destroy(_io_cache);
272 unregister_blkdev(_major, _name);
277 DMINFO("cleaned up");
280 static int (*_inits[])(void) __initdata = {
290 static void (*_exits[])(void) = {
300 static int __init dm_init(void)
302 const int count = ARRAY_SIZE(_inits);
306 for (i = 0; i < count; i++) {
321 static void __exit dm_exit(void)
323 int i = ARRAY_SIZE(_exits);
330 * Block device functions
332 int dm_deleting_md(struct mapped_device *md)
334 return test_bit(DMF_DELETING, &md->flags);
337 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
339 struct mapped_device *md;
341 spin_lock(&_minor_lock);
343 md = bdev->bd_disk->private_data;
347 if (test_bit(DMF_FREEING, &md->flags) ||
348 dm_deleting_md(md)) {
354 atomic_inc(&md->open_count);
357 spin_unlock(&_minor_lock);
359 return md ? 0 : -ENXIO;
362 static int dm_blk_close(struct gendisk *disk, fmode_t mode)
364 struct mapped_device *md = disk->private_data;
365 atomic_dec(&md->open_count);
370 int dm_open_count(struct mapped_device *md)
372 return atomic_read(&md->open_count);
376 * Guarantees nothing is using the device before it's deleted.
378 int dm_lock_for_deletion(struct mapped_device *md)
382 spin_lock(&_minor_lock);
384 if (dm_open_count(md))
387 set_bit(DMF_DELETING, &md->flags);
389 spin_unlock(&_minor_lock);
394 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
396 struct mapped_device *md = bdev->bd_disk->private_data;
398 return dm_get_geometry(md, geo);
401 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
402 unsigned int cmd, unsigned long arg)
404 struct mapped_device *md = bdev->bd_disk->private_data;
405 struct dm_table *map = dm_get_live_table(md);
406 struct dm_target *tgt;
409 if (!map || !dm_table_get_size(map))
412 /* We only support devices that have a single target */
413 if (dm_table_get_num_targets(map) != 1)
416 tgt = dm_table_get_target(map, 0);
418 if (dm_suspended_md(md)) {
423 if (tgt->type->ioctl)
424 r = tgt->type->ioctl(tgt, cmd, arg);
432 static struct dm_io *alloc_io(struct mapped_device *md)
434 return mempool_alloc(md->io_pool, GFP_NOIO);
437 static void free_io(struct mapped_device *md, struct dm_io *io)
439 mempool_free(io, md->io_pool);
442 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
444 mempool_free(tio, md->tio_pool);
447 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
450 return mempool_alloc(md->tio_pool, gfp_mask);
453 static void free_rq_tio(struct dm_rq_target_io *tio)
455 mempool_free(tio, tio->md->tio_pool);
458 static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
460 return mempool_alloc(md->io_pool, GFP_ATOMIC);
463 static void free_bio_info(struct dm_rq_clone_bio_info *info)
465 mempool_free(info, info->tio->md->io_pool);
468 static int md_in_flight(struct mapped_device *md)
470 return atomic_read(&md->pending[READ]) +
471 atomic_read(&md->pending[WRITE]);
474 static void start_io_acct(struct dm_io *io)
476 struct mapped_device *md = io->md;
478 int rw = bio_data_dir(io->bio);
480 io->start_time = jiffies;
482 cpu = part_stat_lock();
483 part_round_stats(cpu, &dm_disk(md)->part0);
485 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
488 static void end_io_acct(struct dm_io *io)
490 struct mapped_device *md = io->md;
491 struct bio *bio = io->bio;
492 unsigned long duration = jiffies - io->start_time;
494 int rw = bio_data_dir(bio);
496 cpu = part_stat_lock();
497 part_round_stats(cpu, &dm_disk(md)->part0);
498 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
502 * After this is decremented the bio must not be touched if it is
505 dm_disk(md)->part0.in_flight[rw] = pending =
506 atomic_dec_return(&md->pending[rw]);
507 pending += atomic_read(&md->pending[rw^0x1]);
509 /* nudge anyone waiting on suspend queue */
515 * Add the bio to the list of deferred io.
517 static void queue_io(struct mapped_device *md, struct bio *bio)
519 down_write(&md->io_lock);
521 spin_lock_irq(&md->deferred_lock);
522 bio_list_add(&md->deferred, bio);
523 spin_unlock_irq(&md->deferred_lock);
525 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
526 queue_work(md->wq, &md->work);
528 up_write(&md->io_lock);
532 * Everyone (including functions in this file), should use this
533 * function to access the md->map field, and make sure they call
534 * dm_table_put() when finished.
536 struct dm_table *dm_get_live_table(struct mapped_device *md)
541 read_lock_irqsave(&md->map_lock, flags);
545 read_unlock_irqrestore(&md->map_lock, flags);
551 * Get the geometry associated with a dm device
553 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
561 * Set the geometry of a device.
563 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
565 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
567 if (geo->start > sz) {
568 DMWARN("Start sector is beyond the geometry limits.");
577 /*-----------------------------------------------------------------
579 * A more elegant soln is in the works that uses the queue
580 * merge fn, unfortunately there are a couple of changes to
581 * the block layer that I want to make for this. So in the
582 * interests of getting something for people to use I give
583 * you this clearly demarcated crap.
584 *---------------------------------------------------------------*/
586 static int __noflush_suspending(struct mapped_device *md)
588 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
592 * Decrements the number of outstanding ios that a bio has been
593 * cloned into, completing the original io if necc.
595 static void dec_pending(struct dm_io *io, int error)
600 struct mapped_device *md = io->md;
602 /* Push-back supersedes any I/O errors */
603 if (unlikely(error)) {
604 spin_lock_irqsave(&io->endio_lock, flags);
605 if (!(io->error > 0 && __noflush_suspending(md)))
607 spin_unlock_irqrestore(&io->endio_lock, flags);
610 if (atomic_dec_and_test(&io->io_count)) {
611 if (io->error == DM_ENDIO_REQUEUE) {
613 * Target requested pushing back the I/O.
615 spin_lock_irqsave(&md->deferred_lock, flags);
616 if (__noflush_suspending(md)) {
617 if (!(io->bio->bi_rw & REQ_HARDBARRIER))
618 bio_list_add_head(&md->deferred,
621 /* noflush suspend was interrupted. */
623 spin_unlock_irqrestore(&md->deferred_lock, flags);
626 io_error = io->error;
629 if (bio->bi_rw & REQ_HARDBARRIER) {
631 * There can be just one barrier request so we use
632 * a per-device variable for error reporting.
633 * Note that you can't touch the bio after end_io_acct
635 if (!md->barrier_error && io_error != -EOPNOTSUPP)
636 md->barrier_error = io_error;
643 if (io_error != DM_ENDIO_REQUEUE) {
644 trace_block_bio_complete(md->queue, bio);
646 bio_endio(bio, io_error);
652 static void clone_endio(struct bio *bio, int error)
655 struct dm_target_io *tio = bio->bi_private;
656 struct dm_io *io = tio->io;
657 struct mapped_device *md = tio->io->md;
658 dm_endio_fn endio = tio->ti->type->end_io;
660 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
664 r = endio(tio->ti, bio, error, &tio->info);
665 if (r < 0 || r == DM_ENDIO_REQUEUE)
667 * error and requeue request are handled
671 else if (r == DM_ENDIO_INCOMPLETE)
672 /* The target will handle the io */
675 DMWARN("unimplemented target endio return value: %d", r);
681 * Store md for cleanup instead of tio which is about to get freed.
683 bio->bi_private = md->bs;
687 dec_pending(io, error);
691 * Partial completion handling for request-based dm
693 static void end_clone_bio(struct bio *clone, int error)
695 struct dm_rq_clone_bio_info *info = clone->bi_private;
696 struct dm_rq_target_io *tio = info->tio;
697 struct bio *bio = info->orig;
698 unsigned int nr_bytes = info->orig->bi_size;
704 * An error has already been detected on the request.
705 * Once error occurred, just let clone->end_io() handle
711 * Don't notice the error to the upper layer yet.
712 * The error handling decision is made by the target driver,
713 * when the request is completed.
720 * I/O for the bio successfully completed.
721 * Notice the data completion to the upper layer.
725 * bios are processed from the head of the list.
726 * So the completing bio should always be rq->bio.
727 * If it's not, something wrong is happening.
729 if (tio->orig->bio != bio)
730 DMERR("bio completion is going in the middle of the request");
733 * Update the original request.
734 * Do not use blk_end_request() here, because it may complete
735 * the original request before the clone, and break the ordering.
737 blk_update_request(tio->orig, 0, nr_bytes);
740 static void store_barrier_error(struct mapped_device *md, int error)
744 spin_lock_irqsave(&md->barrier_error_lock, flags);
746 * Basically, the first error is taken, but:
747 * -EOPNOTSUPP supersedes any I/O error.
748 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
750 if (!md->barrier_error || error == -EOPNOTSUPP ||
751 (md->barrier_error != -EOPNOTSUPP &&
752 error == DM_ENDIO_REQUEUE))
753 md->barrier_error = error;
754 spin_unlock_irqrestore(&md->barrier_error_lock, flags);
758 * Don't touch any member of the md after calling this function because
759 * the md may be freed in dm_put() at the end of this function.
760 * Or do dm_get() before calling this function and dm_put() later.
762 static void rq_completed(struct mapped_device *md, int rw, int run_queue)
764 atomic_dec(&md->pending[rw]);
766 /* nudge anyone waiting on suspend queue */
767 if (!md_in_flight(md))
771 blk_run_queue(md->queue);
774 * dm_put() must be at the end of this function. See the comment above
779 static void free_rq_clone(struct request *clone)
781 struct dm_rq_target_io *tio = clone->end_io_data;
783 blk_rq_unprep_clone(clone);
788 * Complete the clone and the original request.
789 * Must be called without queue lock.
791 static void dm_end_request(struct request *clone, int error)
793 int rw = rq_data_dir(clone);
795 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
796 struct dm_rq_target_io *tio = clone->end_io_data;
797 struct mapped_device *md = tio->md;
798 struct request *rq = tio->orig;
800 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
801 rq->errors = clone->errors;
802 rq->resid_len = clone->resid_len;
806 * We are using the sense buffer of the original
808 * So setting the length of the sense data is enough.
810 rq->sense_len = clone->sense_len;
813 free_rq_clone(clone);
815 if (unlikely(is_barrier)) {
817 store_barrier_error(md, error);
820 blk_end_request_all(rq, error);
822 rq_completed(md, rw, run_queue);
825 static void dm_unprep_request(struct request *rq)
827 struct request *clone = rq->special;
830 rq->cmd_flags &= ~REQ_DONTPREP;
832 free_rq_clone(clone);
836 * Requeue the original request of a clone.
838 void dm_requeue_unmapped_request(struct request *clone)
840 int rw = rq_data_dir(clone);
841 struct dm_rq_target_io *tio = clone->end_io_data;
842 struct mapped_device *md = tio->md;
843 struct request *rq = tio->orig;
844 struct request_queue *q = rq->q;
847 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
849 * Barrier clones share an original request.
850 * Leave it to dm_end_request(), which handles this special
853 dm_end_request(clone, DM_ENDIO_REQUEUE);
857 dm_unprep_request(rq);
859 spin_lock_irqsave(q->queue_lock, flags);
860 if (elv_queue_empty(q))
862 blk_requeue_request(q, rq);
863 spin_unlock_irqrestore(q->queue_lock, flags);
865 rq_completed(md, rw, 0);
867 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
869 static void __stop_queue(struct request_queue *q)
874 static void stop_queue(struct request_queue *q)
878 spin_lock_irqsave(q->queue_lock, flags);
880 spin_unlock_irqrestore(q->queue_lock, flags);
883 static void __start_queue(struct request_queue *q)
885 if (blk_queue_stopped(q))
889 static void start_queue(struct request_queue *q)
893 spin_lock_irqsave(q->queue_lock, flags);
895 spin_unlock_irqrestore(q->queue_lock, flags);
898 static void dm_done(struct request *clone, int error, bool mapped)
901 struct dm_rq_target_io *tio = clone->end_io_data;
902 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
904 if (mapped && rq_end_io)
905 r = rq_end_io(tio->ti, clone, error, &tio->info);
908 /* The target wants to complete the I/O */
909 dm_end_request(clone, r);
910 else if (r == DM_ENDIO_INCOMPLETE)
911 /* The target will handle the I/O */
913 else if (r == DM_ENDIO_REQUEUE)
914 /* The target wants to requeue the I/O */
915 dm_requeue_unmapped_request(clone);
917 DMWARN("unimplemented target endio return value: %d", r);
923 * Request completion handler for request-based dm
925 static void dm_softirq_done(struct request *rq)
928 struct request *clone = rq->completion_data;
929 struct dm_rq_target_io *tio = clone->end_io_data;
931 if (rq->cmd_flags & REQ_FAILED)
934 dm_done(clone, tio->error, mapped);
938 * Complete the clone and the original request with the error status
939 * through softirq context.
941 static void dm_complete_request(struct request *clone, int error)
943 struct dm_rq_target_io *tio = clone->end_io_data;
944 struct request *rq = tio->orig;
946 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
948 * Barrier clones share an original request. So can't use
949 * softirq_done with the original.
950 * Pass the clone to dm_done() directly in this special case.
951 * It is safe (even if clone->q->queue_lock is held here)
952 * because there is no I/O dispatching during the completion
955 dm_done(clone, error, true);
960 rq->completion_data = clone;
961 blk_complete_request(rq);
965 * Complete the not-mapped clone and the original request with the error status
966 * through softirq context.
967 * Target's rq_end_io() function isn't called.
968 * This may be used when the target's map_rq() function fails.
970 void dm_kill_unmapped_request(struct request *clone, int error)
972 struct dm_rq_target_io *tio = clone->end_io_data;
973 struct request *rq = tio->orig;
975 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
977 * Barrier clones share an original request.
978 * Leave it to dm_end_request(), which handles this special
982 dm_end_request(clone, error);
986 rq->cmd_flags |= REQ_FAILED;
987 dm_complete_request(clone, error);
989 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
992 * Called with the queue lock held
994 static void end_clone_request(struct request *clone, int error)
997 * For just cleaning up the information of the queue in which
998 * the clone was dispatched.
999 * The clone is *NOT* freed actually here because it is alloced from
1000 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1002 __blk_put_request(clone->q, clone);
1005 * Actual request completion is done in a softirq context which doesn't
1006 * hold the queue lock. Otherwise, deadlock could occur because:
1007 * - another request may be submitted by the upper level driver
1008 * of the stacking during the completion
1009 * - the submission which requires queue lock may be done
1010 * against this queue
1012 dm_complete_request(clone, error);
1015 static sector_t max_io_len(struct mapped_device *md,
1016 sector_t sector, struct dm_target *ti)
1018 sector_t offset = sector - ti->begin;
1019 sector_t len = ti->len - offset;
1022 * Does the target need to split even further ?
1026 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
1035 static void __map_bio(struct dm_target *ti, struct bio *clone,
1036 struct dm_target_io *tio)
1040 struct mapped_device *md;
1042 clone->bi_end_io = clone_endio;
1043 clone->bi_private = tio;
1046 * Map the clone. If r == 0 we don't need to do
1047 * anything, the target has assumed ownership of
1050 atomic_inc(&tio->io->io_count);
1051 sector = clone->bi_sector;
1052 r = ti->type->map(ti, clone, &tio->info);
1053 if (r == DM_MAPIO_REMAPPED) {
1054 /* the bio has been remapped so dispatch it */
1056 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
1057 tio->io->bio->bi_bdev->bd_dev, sector);
1059 generic_make_request(clone);
1060 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1061 /* error the io and bail out, or requeue it if needed */
1063 dec_pending(tio->io, r);
1065 * Store bio_set for cleanup.
1067 clone->bi_private = md->bs;
1071 DMWARN("unimplemented target map return value: %d", r);
1077 struct mapped_device *md;
1078 struct dm_table *map;
1082 sector_t sector_count;
1086 static void dm_bio_destructor(struct bio *bio)
1088 struct bio_set *bs = bio->bi_private;
1094 * Creates a little bio that is just does part of a bvec.
1096 static struct bio *split_bvec(struct bio *bio, sector_t sector,
1097 unsigned short idx, unsigned int offset,
1098 unsigned int len, struct bio_set *bs)
1101 struct bio_vec *bv = bio->bi_io_vec + idx;
1103 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
1104 clone->bi_destructor = dm_bio_destructor;
1105 *clone->bi_io_vec = *bv;
1107 clone->bi_sector = sector;
1108 clone->bi_bdev = bio->bi_bdev;
1109 clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER;
1111 clone->bi_size = to_bytes(len);
1112 clone->bi_io_vec->bv_offset = offset;
1113 clone->bi_io_vec->bv_len = clone->bi_size;
1114 clone->bi_flags |= 1 << BIO_CLONED;
1116 if (bio_integrity(bio)) {
1117 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1118 bio_integrity_trim(clone,
1119 bio_sector_offset(bio, idx, offset), len);
1126 * Creates a bio that consists of range of complete bvecs.
1128 static struct bio *clone_bio(struct bio *bio, sector_t sector,
1129 unsigned short idx, unsigned short bv_count,
1130 unsigned int len, struct bio_set *bs)
1134 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1135 __bio_clone(clone, bio);
1136 clone->bi_rw &= ~REQ_HARDBARRIER;
1137 clone->bi_destructor = dm_bio_destructor;
1138 clone->bi_sector = sector;
1139 clone->bi_idx = idx;
1140 clone->bi_vcnt = idx + bv_count;
1141 clone->bi_size = to_bytes(len);
1142 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1144 if (bio_integrity(bio)) {
1145 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1147 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1148 bio_integrity_trim(clone,
1149 bio_sector_offset(bio, idx, 0), len);
1155 static struct dm_target_io *alloc_tio(struct clone_info *ci,
1156 struct dm_target *ti)
1158 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1162 memset(&tio->info, 0, sizeof(tio->info));
1167 static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1170 struct dm_target_io *tio = alloc_tio(ci, ti);
1173 tio->info.flush_request = flush_nr;
1175 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1176 __bio_clone(clone, ci->bio);
1177 clone->bi_destructor = dm_bio_destructor;
1179 __map_bio(ti, clone, tio);
1182 static int __clone_and_map_empty_barrier(struct clone_info *ci)
1184 unsigned target_nr = 0, flush_nr;
1185 struct dm_target *ti;
1187 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1188 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1190 __flush_target(ci, ti, flush_nr);
1192 ci->sector_count = 0;
1197 static int __clone_and_map(struct clone_info *ci)
1199 struct bio *clone, *bio = ci->bio;
1200 struct dm_target *ti;
1201 sector_t len = 0, max;
1202 struct dm_target_io *tio;
1204 if (unlikely(bio_empty_barrier(bio)))
1205 return __clone_and_map_empty_barrier(ci);
1207 ti = dm_table_find_target(ci->map, ci->sector);
1208 if (!dm_target_is_valid(ti))
1211 max = max_io_len(ci->md, ci->sector, ti);
1214 * Allocate a target io object.
1216 tio = alloc_tio(ci, ti);
1218 if (ci->sector_count <= max) {
1220 * Optimise for the simple case where we can do all of
1221 * the remaining io with a single clone.
1223 clone = clone_bio(bio, ci->sector, ci->idx,
1224 bio->bi_vcnt - ci->idx, ci->sector_count,
1226 __map_bio(ti, clone, tio);
1227 ci->sector_count = 0;
1229 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1231 * There are some bvecs that don't span targets.
1232 * Do as many of these as possible.
1235 sector_t remaining = max;
1238 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1239 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1241 if (bv_len > remaining)
1244 remaining -= bv_len;
1248 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1250 __map_bio(ti, clone, tio);
1253 ci->sector_count -= len;
1258 * Handle a bvec that must be split between two or more targets.
1260 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1261 sector_t remaining = to_sector(bv->bv_len);
1262 unsigned int offset = 0;
1266 ti = dm_table_find_target(ci->map, ci->sector);
1267 if (!dm_target_is_valid(ti))
1270 max = max_io_len(ci->md, ci->sector, ti);
1272 tio = alloc_tio(ci, ti);
1275 len = min(remaining, max);
1277 clone = split_bvec(bio, ci->sector, ci->idx,
1278 bv->bv_offset + offset, len,
1281 __map_bio(ti, clone, tio);
1284 ci->sector_count -= len;
1285 offset += to_bytes(len);
1286 } while (remaining -= len);
1295 * Split the bio into several clones and submit it to targets.
1297 static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
1299 struct clone_info ci;
1302 ci.map = dm_get_live_table(md);
1303 if (unlikely(!ci.map)) {
1304 if (!(bio->bi_rw & REQ_HARDBARRIER))
1307 if (!md->barrier_error)
1308 md->barrier_error = -EIO;
1314 ci.io = alloc_io(md);
1316 atomic_set(&ci.io->io_count, 1);
1319 spin_lock_init(&ci.io->endio_lock);
1320 ci.sector = bio->bi_sector;
1321 ci.sector_count = bio_sectors(bio);
1322 if (unlikely(bio_empty_barrier(bio)))
1323 ci.sector_count = 1;
1324 ci.idx = bio->bi_idx;
1326 start_io_acct(ci.io);
1327 while (ci.sector_count && !error)
1328 error = __clone_and_map(&ci);
1330 /* drop the extra reference count */
1331 dec_pending(ci.io, error);
1332 dm_table_put(ci.map);
1334 /*-----------------------------------------------------------------
1336 *---------------------------------------------------------------*/
1338 static int dm_merge_bvec(struct request_queue *q,
1339 struct bvec_merge_data *bvm,
1340 struct bio_vec *biovec)
1342 struct mapped_device *md = q->queuedata;
1343 struct dm_table *map = dm_get_live_table(md);
1344 struct dm_target *ti;
1345 sector_t max_sectors;
1351 ti = dm_table_find_target(map, bvm->bi_sector);
1352 if (!dm_target_is_valid(ti))
1356 * Find maximum amount of I/O that won't need splitting
1358 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1359 (sector_t) BIO_MAX_SECTORS);
1360 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1365 * merge_bvec_fn() returns number of bytes
1366 * it can accept at this offset
1367 * max is precomputed maximal io size
1369 if (max_size && ti->type->merge)
1370 max_size = ti->type->merge(ti, bvm, biovec, max_size);
1372 * If the target doesn't support merge method and some of the devices
1373 * provided their merge_bvec method (we know this by looking at
1374 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1375 * entries. So always set max_size to 0, and the code below allows
1378 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1387 * Always allow an entire first page
1389 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1390 max_size = biovec->bv_len;
1396 * The request function that just remaps the bio built up by
1399 static int _dm_request(struct request_queue *q, struct bio *bio)
1401 int rw = bio_data_dir(bio);
1402 struct mapped_device *md = q->queuedata;
1405 down_read(&md->io_lock);
1407 cpu = part_stat_lock();
1408 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1409 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1413 * If we're suspended or the thread is processing barriers
1414 * we have to queue this io for later.
1416 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1417 unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
1418 up_read(&md->io_lock);
1420 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1421 bio_rw(bio) == READA) {
1431 __split_and_process_bio(md, bio);
1432 up_read(&md->io_lock);
1436 static int dm_make_request(struct request_queue *q, struct bio *bio)
1438 struct mapped_device *md = q->queuedata;
1440 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1443 static int dm_request_based(struct mapped_device *md)
1445 return blk_queue_stackable(md->queue);
1448 static int dm_request(struct request_queue *q, struct bio *bio)
1450 struct mapped_device *md = q->queuedata;
1452 if (dm_request_based(md))
1453 return dm_make_request(q, bio);
1455 return _dm_request(q, bio);
1458 static bool dm_rq_is_flush_request(struct request *rq)
1460 if (rq->cmd_flags & REQ_FLUSH)
1466 void dm_dispatch_request(struct request *rq)
1470 if (blk_queue_io_stat(rq->q))
1471 rq->cmd_flags |= REQ_IO_STAT;
1473 rq->start_time = jiffies;
1474 r = blk_insert_cloned_request(rq->q, rq);
1476 dm_complete_request(rq, r);
1478 EXPORT_SYMBOL_GPL(dm_dispatch_request);
1480 static void dm_rq_bio_destructor(struct bio *bio)
1482 struct dm_rq_clone_bio_info *info = bio->bi_private;
1483 struct mapped_device *md = info->tio->md;
1485 free_bio_info(info);
1486 bio_free(bio, md->bs);
1489 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1492 struct dm_rq_target_io *tio = data;
1493 struct mapped_device *md = tio->md;
1494 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1499 info->orig = bio_orig;
1501 bio->bi_end_io = end_clone_bio;
1502 bio->bi_private = info;
1503 bio->bi_destructor = dm_rq_bio_destructor;
1508 static int setup_clone(struct request *clone, struct request *rq,
1509 struct dm_rq_target_io *tio)
1513 if (dm_rq_is_flush_request(rq)) {
1514 blk_rq_init(NULL, clone);
1515 clone->cmd_type = REQ_TYPE_FS;
1516 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1518 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1519 dm_rq_bio_constructor, tio);
1523 clone->cmd = rq->cmd;
1524 clone->cmd_len = rq->cmd_len;
1525 clone->sense = rq->sense;
1526 clone->buffer = rq->buffer;
1529 clone->end_io = end_clone_request;
1530 clone->end_io_data = tio;
1535 static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1538 struct request *clone;
1539 struct dm_rq_target_io *tio;
1541 tio = alloc_rq_tio(md, gfp_mask);
1549 memset(&tio->info, 0, sizeof(tio->info));
1551 clone = &tio->clone;
1552 if (setup_clone(clone, rq, tio)) {
1562 * Called with the queue lock held.
1564 static int dm_prep_fn(struct request_queue *q, struct request *rq)
1566 struct mapped_device *md = q->queuedata;
1567 struct request *clone;
1569 if (unlikely(dm_rq_is_flush_request(rq)))
1572 if (unlikely(rq->special)) {
1573 DMWARN("Already has something in rq->special.");
1574 return BLKPREP_KILL;
1577 clone = clone_rq(rq, md, GFP_ATOMIC);
1579 return BLKPREP_DEFER;
1581 rq->special = clone;
1582 rq->cmd_flags |= REQ_DONTPREP;
1589 * 0 : the request has been processed (not requeued)
1590 * !0 : the request has been requeued
1592 static int map_request(struct dm_target *ti, struct request *clone,
1593 struct mapped_device *md)
1595 int r, requeued = 0;
1596 struct dm_rq_target_io *tio = clone->end_io_data;
1599 * Hold the md reference here for the in-flight I/O.
1600 * We can't rely on the reference count by device opener,
1601 * because the device may be closed during the request completion
1602 * when all bios are completed.
1603 * See the comment in rq_completed() too.
1608 r = ti->type->map_rq(ti, clone, &tio->info);
1610 case DM_MAPIO_SUBMITTED:
1611 /* The target has taken the I/O to submit by itself later */
1613 case DM_MAPIO_REMAPPED:
1614 /* The target has remapped the I/O so dispatch it */
1615 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1616 blk_rq_pos(tio->orig));
1617 dm_dispatch_request(clone);
1619 case DM_MAPIO_REQUEUE:
1620 /* The target wants to requeue the I/O */
1621 dm_requeue_unmapped_request(clone);
1626 DMWARN("unimplemented target map return value: %d", r);
1630 /* The target wants to complete the I/O */
1631 dm_kill_unmapped_request(clone, r);
1639 * q->request_fn for request-based dm.
1640 * Called with the queue lock held.
1642 static void dm_request_fn(struct request_queue *q)
1644 struct mapped_device *md = q->queuedata;
1645 struct dm_table *map = dm_get_live_table(md);
1646 struct dm_target *ti;
1647 struct request *rq, *clone;
1650 * For suspend, check blk_queue_stopped() and increment
1651 * ->pending within a single queue_lock not to increment the
1652 * number of in-flight I/Os after the queue is stopped in
1655 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1656 rq = blk_peek_request(q);
1660 if (unlikely(dm_rq_is_flush_request(rq))) {
1661 BUG_ON(md->flush_request);
1662 md->flush_request = rq;
1663 blk_start_request(rq);
1664 queue_work(md->wq, &md->barrier_work);
1668 ti = dm_table_find_target(map, blk_rq_pos(rq));
1669 if (ti->type->busy && ti->type->busy(ti))
1672 blk_start_request(rq);
1673 clone = rq->special;
1674 atomic_inc(&md->pending[rq_data_dir(clone)]);
1676 spin_unlock(q->queue_lock);
1677 if (map_request(ti, clone, md))
1680 spin_lock_irq(q->queue_lock);
1686 spin_lock_irq(q->queue_lock);
1689 if (!elv_queue_empty(q))
1690 /* Some requests still remain, retry later */
1699 int dm_underlying_device_busy(struct request_queue *q)
1701 return blk_lld_busy(q);
1703 EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1705 static int dm_lld_busy(struct request_queue *q)
1708 struct mapped_device *md = q->queuedata;
1709 struct dm_table *map = dm_get_live_table(md);
1711 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1714 r = dm_table_any_busy_target(map);
1721 static void dm_unplug_all(struct request_queue *q)
1723 struct mapped_device *md = q->queuedata;
1724 struct dm_table *map = dm_get_live_table(md);
1727 if (dm_request_based(md))
1728 generic_unplug_device(q);
1730 dm_table_unplug_all(map);
1735 static int dm_any_congested(void *congested_data, int bdi_bits)
1738 struct mapped_device *md = congested_data;
1739 struct dm_table *map;
1741 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1742 map = dm_get_live_table(md);
1745 * Request-based dm cares about only own queue for
1746 * the query about congestion status of request_queue
1748 if (dm_request_based(md))
1749 r = md->queue->backing_dev_info.state &
1752 r = dm_table_any_congested(map, bdi_bits);
1761 /*-----------------------------------------------------------------
1762 * An IDR is used to keep track of allocated minor numbers.
1763 *---------------------------------------------------------------*/
1764 static DEFINE_IDR(_minor_idr);
1766 static void free_minor(int minor)
1768 spin_lock(&_minor_lock);
1769 idr_remove(&_minor_idr, minor);
1770 spin_unlock(&_minor_lock);
1774 * See if the device with a specific minor # is free.
1776 static int specific_minor(int minor)
1780 if (minor >= (1 << MINORBITS))
1783 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1787 spin_lock(&_minor_lock);
1789 if (idr_find(&_minor_idr, minor)) {
1794 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
1799 idr_remove(&_minor_idr, m);
1805 spin_unlock(&_minor_lock);
1809 static int next_free_minor(int *minor)
1813 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1817 spin_lock(&_minor_lock);
1819 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1823 if (m >= (1 << MINORBITS)) {
1824 idr_remove(&_minor_idr, m);
1832 spin_unlock(&_minor_lock);
1836 static const struct block_device_operations dm_blk_dops;
1838 static void dm_wq_work(struct work_struct *work);
1839 static void dm_rq_barrier_work(struct work_struct *work);
1842 * Allocate and initialise a blank device with a given minor.
1844 static struct mapped_device *alloc_dev(int minor)
1847 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1851 DMWARN("unable to allocate device, out of memory.");
1855 if (!try_module_get(THIS_MODULE))
1856 goto bad_module_get;
1858 /* get a minor number for the dev */
1859 if (minor == DM_ANY_MINOR)
1860 r = next_free_minor(&minor);
1862 r = specific_minor(minor);
1866 init_rwsem(&md->io_lock);
1867 mutex_init(&md->suspend_lock);
1868 spin_lock_init(&md->deferred_lock);
1869 spin_lock_init(&md->barrier_error_lock);
1870 rwlock_init(&md->map_lock);
1871 atomic_set(&md->holders, 1);
1872 atomic_set(&md->open_count, 0);
1873 atomic_set(&md->event_nr, 0);
1874 atomic_set(&md->uevent_seq, 0);
1875 INIT_LIST_HEAD(&md->uevent_list);
1876 spin_lock_init(&md->uevent_lock);
1878 md->queue = blk_init_queue(dm_request_fn, NULL);
1883 * Request-based dm devices cannot be stacked on top of bio-based dm
1884 * devices. The type of this dm device has not been decided yet,
1885 * although we initialized the queue using blk_init_queue().
1886 * The type is decided at the first table loading time.
1887 * To prevent problematic device stacking, clear the queue flag
1888 * for request stacking support until then.
1890 * This queue is new, so no concurrency on the queue_flags.
1892 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1893 md->saved_make_request_fn = md->queue->make_request_fn;
1894 md->queue->queuedata = md;
1895 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1896 md->queue->backing_dev_info.congested_data = md;
1897 blk_queue_make_request(md->queue, dm_request);
1898 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1899 md->queue->unplug_fn = dm_unplug_all;
1900 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1901 blk_queue_softirq_done(md->queue, dm_softirq_done);
1902 blk_queue_prep_rq(md->queue, dm_prep_fn);
1903 blk_queue_lld_busy(md->queue, dm_lld_busy);
1904 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH);
1906 md->disk = alloc_disk(1);
1910 atomic_set(&md->pending[0], 0);
1911 atomic_set(&md->pending[1], 0);
1912 init_waitqueue_head(&md->wait);
1913 INIT_WORK(&md->work, dm_wq_work);
1914 INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
1915 init_waitqueue_head(&md->eventq);
1917 md->disk->major = _major;
1918 md->disk->first_minor = minor;
1919 md->disk->fops = &dm_blk_dops;
1920 md->disk->queue = md->queue;
1921 md->disk->private_data = md;
1922 sprintf(md->disk->disk_name, "dm-%d", minor);
1924 format_dev_t(md->name, MKDEV(_major, minor));
1926 md->wq = create_singlethread_workqueue("kdmflush");
1930 md->bdev = bdget_disk(md->disk, 0);
1934 /* Populate the mapping, nobody knows we exist yet */
1935 spin_lock(&_minor_lock);
1936 old_md = idr_replace(&_minor_idr, md, minor);
1937 spin_unlock(&_minor_lock);
1939 BUG_ON(old_md != MINOR_ALLOCED);
1944 destroy_workqueue(md->wq);
1946 del_gendisk(md->disk);
1949 blk_cleanup_queue(md->queue);
1953 module_put(THIS_MODULE);
1959 static void unlock_fs(struct mapped_device *md);
1961 static void free_dev(struct mapped_device *md)
1963 int minor = MINOR(disk_devt(md->disk));
1967 destroy_workqueue(md->wq);
1969 mempool_destroy(md->tio_pool);
1971 mempool_destroy(md->io_pool);
1973 bioset_free(md->bs);
1974 blk_integrity_unregister(md->disk);
1975 del_gendisk(md->disk);
1978 spin_lock(&_minor_lock);
1979 md->disk->private_data = NULL;
1980 spin_unlock(&_minor_lock);
1983 blk_cleanup_queue(md->queue);
1984 module_put(THIS_MODULE);
1988 static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1990 struct dm_md_mempools *p;
1992 if (md->io_pool && md->tio_pool && md->bs)
1993 /* the md already has necessary mempools */
1996 p = dm_table_get_md_mempools(t);
1997 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1999 md->io_pool = p->io_pool;
2001 md->tio_pool = p->tio_pool;
2007 /* mempool bind completed, now no need any mempools in the table */
2008 dm_table_free_md_mempools(t);
2012 * Bind a table to the device.
2014 static void event_callback(void *context)
2016 unsigned long flags;
2018 struct mapped_device *md = (struct mapped_device *) context;
2020 spin_lock_irqsave(&md->uevent_lock, flags);
2021 list_splice_init(&md->uevent_list, &uevents);
2022 spin_unlock_irqrestore(&md->uevent_lock, flags);
2024 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
2026 atomic_inc(&md->event_nr);
2027 wake_up(&md->eventq);
2030 static void __set_size(struct mapped_device *md, sector_t size)
2032 set_capacity(md->disk, size);
2034 mutex_lock(&md->bdev->bd_inode->i_mutex);
2035 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2036 mutex_unlock(&md->bdev->bd_inode->i_mutex);
2040 * Returns old map, which caller must destroy.
2042 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2043 struct queue_limits *limits)
2045 struct dm_table *old_map;
2046 struct request_queue *q = md->queue;
2048 unsigned long flags;
2050 size = dm_table_get_size(t);
2053 * Wipe any geometry if the size of the table changed.
2055 if (size != get_capacity(md->disk))
2056 memset(&md->geometry, 0, sizeof(md->geometry));
2058 __set_size(md, size);
2060 dm_table_event_callback(t, event_callback, md);
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.
2069 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2072 __bind_mempools(md, t);
2074 write_lock_irqsave(&md->map_lock, flags);
2077 dm_table_set_restrictions(t, q, limits);
2078 write_unlock_irqrestore(&md->map_lock, flags);
2084 * Returns unbound table for the caller to free.
2086 static struct dm_table *__unbind(struct mapped_device *md)
2088 struct dm_table *map = md->map;
2089 unsigned long flags;
2094 dm_table_event_callback(map, NULL, NULL);
2095 write_lock_irqsave(&md->map_lock, flags);
2097 write_unlock_irqrestore(&md->map_lock, flags);
2103 * Constructor for a new device.
2105 int dm_create(int minor, struct mapped_device **result)
2107 struct mapped_device *md;
2109 md = alloc_dev(minor);
2119 static struct mapped_device *dm_find_md(dev_t dev)
2121 struct mapped_device *md;
2122 unsigned minor = MINOR(dev);
2124 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2127 spin_lock(&_minor_lock);
2129 md = idr_find(&_minor_idr, minor);
2130 if (md && (md == MINOR_ALLOCED ||
2131 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2132 test_bit(DMF_FREEING, &md->flags))) {
2138 spin_unlock(&_minor_lock);
2143 struct mapped_device *dm_get_md(dev_t dev)
2145 struct mapped_device *md = dm_find_md(dev);
2153 void *dm_get_mdptr(struct mapped_device *md)
2155 return md->interface_ptr;
2158 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2160 md->interface_ptr = ptr;
2163 void dm_get(struct mapped_device *md)
2165 atomic_inc(&md->holders);
2168 const char *dm_device_name(struct mapped_device *md)
2172 EXPORT_SYMBOL_GPL(dm_device_name);
2174 void dm_put(struct mapped_device *md)
2176 struct dm_table *map;
2178 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2180 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
2181 map = dm_get_live_table(md);
2182 idr_replace(&_minor_idr, MINOR_ALLOCED,
2183 MINOR(disk_devt(dm_disk(md))));
2184 set_bit(DMF_FREEING, &md->flags);
2185 spin_unlock(&_minor_lock);
2186 if (!dm_suspended_md(md)) {
2187 dm_table_presuspend_targets(map);
2188 dm_table_postsuspend_targets(map);
2192 dm_table_destroy(__unbind(md));
2196 EXPORT_SYMBOL_GPL(dm_put);
2198 static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
2201 DECLARE_WAITQUEUE(wait, current);
2203 dm_unplug_all(md->queue);
2205 add_wait_queue(&md->wait, &wait);
2208 set_current_state(interruptible);
2211 if (!md_in_flight(md))
2214 if (interruptible == TASK_INTERRUPTIBLE &&
2215 signal_pending(current)) {
2222 set_current_state(TASK_RUNNING);
2224 remove_wait_queue(&md->wait, &wait);
2229 static void dm_flush(struct mapped_device *md)
2231 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2233 bio_init(&md->barrier_bio);
2234 md->barrier_bio.bi_bdev = md->bdev;
2235 md->barrier_bio.bi_rw = WRITE_BARRIER;
2236 __split_and_process_bio(md, &md->barrier_bio);
2238 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2241 static void process_barrier(struct mapped_device *md, struct bio *bio)
2243 md->barrier_error = 0;
2247 if (!bio_empty_barrier(bio)) {
2248 __split_and_process_bio(md, bio);
2252 if (md->barrier_error != DM_ENDIO_REQUEUE)
2253 bio_endio(bio, md->barrier_error);
2255 spin_lock_irq(&md->deferred_lock);
2256 bio_list_add_head(&md->deferred, bio);
2257 spin_unlock_irq(&md->deferred_lock);
2262 * Process the deferred bios
2264 static void dm_wq_work(struct work_struct *work)
2266 struct mapped_device *md = container_of(work, struct mapped_device,
2270 down_write(&md->io_lock);
2272 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2273 spin_lock_irq(&md->deferred_lock);
2274 c = bio_list_pop(&md->deferred);
2275 spin_unlock_irq(&md->deferred_lock);
2278 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2282 up_write(&md->io_lock);
2284 if (dm_request_based(md))
2285 generic_make_request(c);
2287 if (c->bi_rw & REQ_HARDBARRIER)
2288 process_barrier(md, c);
2290 __split_and_process_bio(md, c);
2293 down_write(&md->io_lock);
2296 up_write(&md->io_lock);
2299 static void dm_queue_flush(struct mapped_device *md)
2301 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2302 smp_mb__after_clear_bit();
2303 queue_work(md->wq, &md->work);
2306 static void dm_rq_set_flush_nr(struct request *clone, unsigned flush_nr)
2308 struct dm_rq_target_io *tio = clone->end_io_data;
2310 tio->info.flush_request = flush_nr;
2313 /* Issue barrier requests to targets and wait for their completion. */
2314 static int dm_rq_barrier(struct mapped_device *md)
2317 struct dm_table *map = dm_get_live_table(md);
2318 unsigned num_targets = dm_table_get_num_targets(map);
2319 struct dm_target *ti;
2320 struct request *clone;
2322 md->barrier_error = 0;
2324 for (i = 0; i < num_targets; i++) {
2325 ti = dm_table_get_target(map, i);
2326 for (j = 0; j < ti->num_flush_requests; j++) {
2327 clone = clone_rq(md->flush_request, md, GFP_NOIO);
2328 dm_rq_set_flush_nr(clone, j);
2329 atomic_inc(&md->pending[rq_data_dir(clone)]);
2330 map_request(ti, clone, md);
2334 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2337 return md->barrier_error;
2340 static void dm_rq_barrier_work(struct work_struct *work)
2343 struct mapped_device *md = container_of(work, struct mapped_device,
2345 struct request_queue *q = md->queue;
2347 unsigned long flags;
2350 * Hold the md reference here and leave it at the last part so that
2351 * the md can't be deleted by device opener when the barrier request
2356 error = dm_rq_barrier(md);
2358 rq = md->flush_request;
2359 md->flush_request = NULL;
2361 if (error == DM_ENDIO_REQUEUE) {
2362 spin_lock_irqsave(q->queue_lock, flags);
2363 blk_requeue_request(q, rq);
2364 spin_unlock_irqrestore(q->queue_lock, flags);
2366 blk_end_request_all(rq, error);
2374 * Swap in a new table, returning the old one for the caller to destroy.
2376 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
2378 struct dm_table *map = ERR_PTR(-EINVAL);
2379 struct queue_limits limits;
2382 mutex_lock(&md->suspend_lock);
2384 /* device must be suspended */
2385 if (!dm_suspended_md(md))
2388 r = dm_calculate_queue_limits(table, &limits);
2394 /* cannot change the device type, once a table is bound */
2396 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2397 DMWARN("can't change the device type after a table is bound");
2401 map = __bind(md, table, &limits);
2404 mutex_unlock(&md->suspend_lock);
2409 * Functions to lock and unlock any filesystem running on the
2412 static int lock_fs(struct mapped_device *md)
2416 WARN_ON(md->frozen_sb);
2418 md->frozen_sb = freeze_bdev(md->bdev);
2419 if (IS_ERR(md->frozen_sb)) {
2420 r = PTR_ERR(md->frozen_sb);
2421 md->frozen_sb = NULL;
2425 set_bit(DMF_FROZEN, &md->flags);
2430 static void unlock_fs(struct mapped_device *md)
2432 if (!test_bit(DMF_FROZEN, &md->flags))
2435 thaw_bdev(md->bdev, md->frozen_sb);
2436 md->frozen_sb = NULL;
2437 clear_bit(DMF_FROZEN, &md->flags);
2441 * We need to be able to change a mapping table under a mounted
2442 * filesystem. For example we might want to move some data in
2443 * the background. Before the table can be swapped with
2444 * dm_bind_table, dm_suspend must be called to flush any in
2445 * flight bios and ensure that any further io gets deferred.
2448 * Suspend mechanism in request-based dm.
2450 * 1. Flush all I/Os by lock_fs() if needed.
2451 * 2. Stop dispatching any I/O by stopping the request_queue.
2452 * 3. Wait for all in-flight I/Os to be completed or requeued.
2454 * To abort suspend, start the request_queue.
2456 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2458 struct dm_table *map = NULL;
2460 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2461 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
2463 mutex_lock(&md->suspend_lock);
2465 if (dm_suspended_md(md)) {
2470 map = dm_get_live_table(md);
2473 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2474 * This flag is cleared before dm_suspend returns.
2477 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2479 /* This does not get reverted if there's an error later. */
2480 dm_table_presuspend_targets(map);
2483 * Flush I/O to the device.
2484 * Any I/O submitted after lock_fs() may not be flushed.
2485 * noflush takes precedence over do_lockfs.
2486 * (lock_fs() flushes I/Os and waits for them to complete.)
2488 if (!noflush && do_lockfs) {
2495 * Here we must make sure that no processes are submitting requests
2496 * to target drivers i.e. no one may be executing
2497 * __split_and_process_bio. This is called from dm_request and
2500 * To get all processes out of __split_and_process_bio in dm_request,
2501 * we take the write lock. To prevent any process from reentering
2502 * __split_and_process_bio from dm_request, we set
2503 * DMF_QUEUE_IO_TO_THREAD.
2505 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2506 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2507 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2508 * further calls to __split_and_process_bio from dm_wq_work.
2510 down_write(&md->io_lock);
2511 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2512 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2513 up_write(&md->io_lock);
2516 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2517 * can be kicked until md->queue is stopped. So stop md->queue before
2520 if (dm_request_based(md))
2521 stop_queue(md->queue);
2523 flush_workqueue(md->wq);
2526 * At this point no more requests are entering target request routines.
2527 * We call dm_wait_for_completion to wait for all existing requests
2530 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
2532 down_write(&md->io_lock);
2534 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2535 up_write(&md->io_lock);
2537 /* were we interrupted ? */
2541 if (dm_request_based(md))
2542 start_queue(md->queue);
2545 goto out; /* pushback list is already flushed, so skip flush */
2549 * If dm_wait_for_completion returned 0, the device is completely
2550 * quiescent now. There is no request-processing activity. All new
2551 * requests are being added to md->deferred list.
2554 set_bit(DMF_SUSPENDED, &md->flags);
2556 dm_table_postsuspend_targets(map);
2562 mutex_unlock(&md->suspend_lock);
2566 int dm_resume(struct mapped_device *md)
2569 struct dm_table *map = NULL;
2571 mutex_lock(&md->suspend_lock);
2572 if (!dm_suspended_md(md))
2575 map = dm_get_live_table(md);
2576 if (!map || !dm_table_get_size(map))
2579 r = dm_table_resume_targets(map);
2586 * Flushing deferred I/Os must be done after targets are resumed
2587 * so that mapping of targets can work correctly.
2588 * Request-based dm is queueing the deferred I/Os in its request_queue.
2590 if (dm_request_based(md))
2591 start_queue(md->queue);
2595 clear_bit(DMF_SUSPENDED, &md->flags);
2597 dm_table_unplug_all(map);
2601 mutex_unlock(&md->suspend_lock);
2606 /*-----------------------------------------------------------------
2607 * Event notification.
2608 *---------------------------------------------------------------*/
2609 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2612 char udev_cookie[DM_COOKIE_LENGTH];
2613 char *envp[] = { udev_cookie, NULL };
2616 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2618 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2619 DM_COOKIE_ENV_VAR_NAME, cookie);
2620 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2625 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2627 return atomic_add_return(1, &md->uevent_seq);
2630 uint32_t dm_get_event_nr(struct mapped_device *md)
2632 return atomic_read(&md->event_nr);
2635 int dm_wait_event(struct mapped_device *md, int event_nr)
2637 return wait_event_interruptible(md->eventq,
2638 (event_nr != atomic_read(&md->event_nr)));
2641 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2643 unsigned long flags;
2645 spin_lock_irqsave(&md->uevent_lock, flags);
2646 list_add(elist, &md->uevent_list);
2647 spin_unlock_irqrestore(&md->uevent_lock, flags);
2651 * The gendisk is only valid as long as you have a reference
2654 struct gendisk *dm_disk(struct mapped_device *md)
2659 struct kobject *dm_kobject(struct mapped_device *md)
2665 * struct mapped_device should not be exported outside of dm.c
2666 * so use this check to verify that kobj is part of md structure
2668 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2670 struct mapped_device *md;
2672 md = container_of(kobj, struct mapped_device, kobj);
2673 if (&md->kobj != kobj)
2676 if (test_bit(DMF_FREEING, &md->flags) ||
2684 int dm_suspended_md(struct mapped_device *md)
2686 return test_bit(DMF_SUSPENDED, &md->flags);
2689 int dm_suspended(struct dm_target *ti)
2691 return dm_suspended_md(dm_table_get_md(ti->table));
2693 EXPORT_SYMBOL_GPL(dm_suspended);
2695 int dm_noflush_suspending(struct dm_target *ti)
2697 return __noflush_suspending(dm_table_get_md(ti->table));
2699 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2701 struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2703 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2708 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2709 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2710 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2711 if (!pools->io_pool)
2712 goto free_pools_and_out;
2714 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2715 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2716 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2717 if (!pools->tio_pool)
2718 goto free_io_pool_and_out;
2720 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2721 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2723 goto free_tio_pool_and_out;
2727 free_tio_pool_and_out:
2728 mempool_destroy(pools->tio_pool);
2730 free_io_pool_and_out:
2731 mempool_destroy(pools->io_pool);
2739 void dm_free_md_mempools(struct dm_md_mempools *pools)
2745 mempool_destroy(pools->io_pool);
2747 if (pools->tio_pool)
2748 mempool_destroy(pools->tio_pool);
2751 bioset_free(pools->bs);
2756 static const struct block_device_operations dm_blk_dops = {
2757 .open = dm_blk_open,
2758 .release = dm_blk_close,
2759 .ioctl = dm_blk_ioctl,
2760 .getgeo = dm_blk_getgeo,
2761 .owner = THIS_MODULE
2764 EXPORT_SYMBOL(dm_get_mapinfo);
2769 module_init(dm_init);
2770 module_exit(dm_exit);
2772 module_param(major, uint, 0);
2773 MODULE_PARM_DESC(major, "The major number of the device mapper");
2774 MODULE_DESCRIPTION(DM_NAME " driver");
2775 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2776 MODULE_LICENSE("GPL");