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 * Processing queue (flush/barriers)
148 struct workqueue_struct *wq;
151 * The current mapping.
153 struct dm_table *map;
156 * io objects are allocated from here.
167 wait_queue_head_t eventq;
169 struct list_head uevent_list;
170 spinlock_t uevent_lock; /* Protect access to uevent_list */
173 * freeze/thaw support require holding onto a super block
175 struct super_block *frozen_sb;
176 struct block_device *bdev;
178 /* forced geometry settings */
179 struct hd_geometry geometry;
181 /* marker of flush suspend for request-based dm */
182 struct request suspend_rq;
184 /* For saving the address of __make_request for request based dm */
185 make_request_fn *saved_make_request_fn;
190 /* zero-length barrier that will be cloned and submitted to targets */
191 struct bio barrier_bio;
195 * For mempools pre-allocation at the table loading time.
197 struct dm_md_mempools {
204 static struct kmem_cache *_io_cache;
205 static struct kmem_cache *_tio_cache;
206 static struct kmem_cache *_rq_tio_cache;
207 static struct kmem_cache *_rq_bio_info_cache;
209 static int __init local_init(void)
213 /* allocate a slab for the dm_ios */
214 _io_cache = KMEM_CACHE(dm_io, 0);
218 /* allocate a slab for the target ios */
219 _tio_cache = KMEM_CACHE(dm_target_io, 0);
221 goto out_free_io_cache;
223 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
225 goto out_free_tio_cache;
227 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
228 if (!_rq_bio_info_cache)
229 goto out_free_rq_tio_cache;
231 r = dm_uevent_init();
233 goto out_free_rq_bio_info_cache;
236 r = register_blkdev(_major, _name);
238 goto out_uevent_exit;
247 out_free_rq_bio_info_cache:
248 kmem_cache_destroy(_rq_bio_info_cache);
249 out_free_rq_tio_cache:
250 kmem_cache_destroy(_rq_tio_cache);
252 kmem_cache_destroy(_tio_cache);
254 kmem_cache_destroy(_io_cache);
259 static void local_exit(void)
261 kmem_cache_destroy(_rq_bio_info_cache);
262 kmem_cache_destroy(_rq_tio_cache);
263 kmem_cache_destroy(_tio_cache);
264 kmem_cache_destroy(_io_cache);
265 unregister_blkdev(_major, _name);
270 DMINFO("cleaned up");
273 static int (*_inits[])(void) __initdata = {
282 static void (*_exits[])(void) = {
291 static int __init dm_init(void)
293 const int count = ARRAY_SIZE(_inits);
297 for (i = 0; i < count; i++) {
312 static void __exit dm_exit(void)
314 int i = ARRAY_SIZE(_exits);
321 * Block device functions
323 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
325 struct mapped_device *md;
327 spin_lock(&_minor_lock);
329 md = bdev->bd_disk->private_data;
333 if (test_bit(DMF_FREEING, &md->flags) ||
334 test_bit(DMF_DELETING, &md->flags)) {
340 atomic_inc(&md->open_count);
343 spin_unlock(&_minor_lock);
345 return md ? 0 : -ENXIO;
348 static int dm_blk_close(struct gendisk *disk, fmode_t mode)
350 struct mapped_device *md = disk->private_data;
351 atomic_dec(&md->open_count);
356 int dm_open_count(struct mapped_device *md)
358 return atomic_read(&md->open_count);
362 * Guarantees nothing is using the device before it's deleted.
364 int dm_lock_for_deletion(struct mapped_device *md)
368 spin_lock(&_minor_lock);
370 if (dm_open_count(md))
373 set_bit(DMF_DELETING, &md->flags);
375 spin_unlock(&_minor_lock);
380 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
382 struct mapped_device *md = bdev->bd_disk->private_data;
384 return dm_get_geometry(md, geo);
387 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
388 unsigned int cmd, unsigned long arg)
390 struct mapped_device *md = bdev->bd_disk->private_data;
391 struct dm_table *map = dm_get_table(md);
392 struct dm_target *tgt;
395 if (!map || !dm_table_get_size(map))
398 /* We only support devices that have a single target */
399 if (dm_table_get_num_targets(map) != 1)
402 tgt = dm_table_get_target(map, 0);
404 if (dm_suspended(md)) {
409 if (tgt->type->ioctl)
410 r = tgt->type->ioctl(tgt, cmd, arg);
418 static struct dm_io *alloc_io(struct mapped_device *md)
420 return mempool_alloc(md->io_pool, GFP_NOIO);
423 static void free_io(struct mapped_device *md, struct dm_io *io)
425 mempool_free(io, md->io_pool);
428 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
430 mempool_free(tio, md->tio_pool);
433 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md)
435 return mempool_alloc(md->tio_pool, GFP_ATOMIC);
438 static void free_rq_tio(struct dm_rq_target_io *tio)
440 mempool_free(tio, tio->md->tio_pool);
443 static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
445 return mempool_alloc(md->io_pool, GFP_ATOMIC);
448 static void free_bio_info(struct dm_rq_clone_bio_info *info)
450 mempool_free(info, info->tio->md->io_pool);
453 static void start_io_acct(struct dm_io *io)
455 struct mapped_device *md = io->md;
457 int rw = bio_data_dir(io->bio);
459 io->start_time = jiffies;
461 cpu = part_stat_lock();
462 part_round_stats(cpu, &dm_disk(md)->part0);
464 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
467 static void end_io_acct(struct dm_io *io)
469 struct mapped_device *md = io->md;
470 struct bio *bio = io->bio;
471 unsigned long duration = jiffies - io->start_time;
473 int rw = bio_data_dir(bio);
475 cpu = part_stat_lock();
476 part_round_stats(cpu, &dm_disk(md)->part0);
477 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
481 * After this is decremented the bio must not be touched if it is
484 dm_disk(md)->part0.in_flight[rw] = pending =
485 atomic_dec_return(&md->pending[rw]);
486 pending += atomic_read(&md->pending[rw^0x1]);
488 /* nudge anyone waiting on suspend queue */
494 * Add the bio to the list of deferred io.
496 static void queue_io(struct mapped_device *md, struct bio *bio)
498 down_write(&md->io_lock);
500 spin_lock_irq(&md->deferred_lock);
501 bio_list_add(&md->deferred, bio);
502 spin_unlock_irq(&md->deferred_lock);
504 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
505 queue_work(md->wq, &md->work);
507 up_write(&md->io_lock);
511 * Everyone (including functions in this file), should use this
512 * function to access the md->map field, and make sure they call
513 * dm_table_put() when finished.
515 struct dm_table *dm_get_table(struct mapped_device *md)
520 read_lock_irqsave(&md->map_lock, flags);
524 read_unlock_irqrestore(&md->map_lock, flags);
530 * Get the geometry associated with a dm device
532 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
540 * Set the geometry of a device.
542 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
544 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
546 if (geo->start > sz) {
547 DMWARN("Start sector is beyond the geometry limits.");
556 /*-----------------------------------------------------------------
558 * A more elegant soln is in the works that uses the queue
559 * merge fn, unfortunately there are a couple of changes to
560 * the block layer that I want to make for this. So in the
561 * interests of getting something for people to use I give
562 * you this clearly demarcated crap.
563 *---------------------------------------------------------------*/
565 static int __noflush_suspending(struct mapped_device *md)
567 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
571 * Decrements the number of outstanding ios that a bio has been
572 * cloned into, completing the original io if necc.
574 static void dec_pending(struct dm_io *io, int error)
579 struct mapped_device *md = io->md;
581 /* Push-back supersedes any I/O errors */
582 if (unlikely(error)) {
583 spin_lock_irqsave(&io->endio_lock, flags);
584 if (!(io->error > 0 && __noflush_suspending(md)))
586 spin_unlock_irqrestore(&io->endio_lock, flags);
589 if (atomic_dec_and_test(&io->io_count)) {
590 if (io->error == DM_ENDIO_REQUEUE) {
592 * Target requested pushing back the I/O.
594 spin_lock_irqsave(&md->deferred_lock, flags);
595 if (__noflush_suspending(md)) {
596 if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
597 bio_list_add_head(&md->deferred,
600 /* noflush suspend was interrupted. */
602 spin_unlock_irqrestore(&md->deferred_lock, flags);
605 io_error = io->error;
608 if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
610 * There can be just one barrier request so we use
611 * a per-device variable for error reporting.
612 * Note that you can't touch the bio after end_io_acct
614 if (!md->barrier_error && io_error != -EOPNOTSUPP)
615 md->barrier_error = io_error;
622 if (io_error != DM_ENDIO_REQUEUE) {
623 trace_block_bio_complete(md->queue, bio);
625 bio_endio(bio, io_error);
631 static void clone_endio(struct bio *bio, int error)
634 struct dm_target_io *tio = bio->bi_private;
635 struct dm_io *io = tio->io;
636 struct mapped_device *md = tio->io->md;
637 dm_endio_fn endio = tio->ti->type->end_io;
639 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
643 r = endio(tio->ti, bio, error, &tio->info);
644 if (r < 0 || r == DM_ENDIO_REQUEUE)
646 * error and requeue request are handled
650 else if (r == DM_ENDIO_INCOMPLETE)
651 /* The target will handle the io */
654 DMWARN("unimplemented target endio return value: %d", r);
660 * Store md for cleanup instead of tio which is about to get freed.
662 bio->bi_private = md->bs;
666 dec_pending(io, error);
670 * Partial completion handling for request-based dm
672 static void end_clone_bio(struct bio *clone, int error)
674 struct dm_rq_clone_bio_info *info = clone->bi_private;
675 struct dm_rq_target_io *tio = info->tio;
676 struct bio *bio = info->orig;
677 unsigned int nr_bytes = info->orig->bi_size;
683 * An error has already been detected on the request.
684 * Once error occurred, just let clone->end_io() handle
690 * Don't notice the error to the upper layer yet.
691 * The error handling decision is made by the target driver,
692 * when the request is completed.
699 * I/O for the bio successfully completed.
700 * Notice the data completion to the upper layer.
704 * bios are processed from the head of the list.
705 * So the completing bio should always be rq->bio.
706 * If it's not, something wrong is happening.
708 if (tio->orig->bio != bio)
709 DMERR("bio completion is going in the middle of the request");
712 * Update the original request.
713 * Do not use blk_end_request() here, because it may complete
714 * the original request before the clone, and break the ordering.
716 blk_update_request(tio->orig, 0, nr_bytes);
720 * Don't touch any member of the md after calling this function because
721 * the md may be freed in dm_put() at the end of this function.
722 * Or do dm_get() before calling this function and dm_put() later.
724 static void rq_completed(struct mapped_device *md, int run_queue)
726 int wakeup_waiters = 0;
727 struct request_queue *q = md->queue;
730 spin_lock_irqsave(q->queue_lock, flags);
731 if (!queue_in_flight(q))
733 spin_unlock_irqrestore(q->queue_lock, flags);
735 /* nudge anyone waiting on suspend queue */
743 * dm_put() must be at the end of this function. See the comment above
748 static void free_rq_clone(struct request *clone)
750 struct dm_rq_target_io *tio = clone->end_io_data;
752 blk_rq_unprep_clone(clone);
756 static void dm_unprep_request(struct request *rq)
758 struct request *clone = rq->special;
761 rq->cmd_flags &= ~REQ_DONTPREP;
763 free_rq_clone(clone);
767 * Requeue the original request of a clone.
769 void dm_requeue_unmapped_request(struct request *clone)
771 struct dm_rq_target_io *tio = clone->end_io_data;
772 struct mapped_device *md = tio->md;
773 struct request *rq = tio->orig;
774 struct request_queue *q = rq->q;
777 dm_unprep_request(rq);
779 spin_lock_irqsave(q->queue_lock, flags);
780 if (elv_queue_empty(q))
782 blk_requeue_request(q, rq);
783 spin_unlock_irqrestore(q->queue_lock, flags);
787 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
789 static void __stop_queue(struct request_queue *q)
794 static void stop_queue(struct request_queue *q)
798 spin_lock_irqsave(q->queue_lock, flags);
800 spin_unlock_irqrestore(q->queue_lock, flags);
803 static void __start_queue(struct request_queue *q)
805 if (blk_queue_stopped(q))
809 static void start_queue(struct request_queue *q)
813 spin_lock_irqsave(q->queue_lock, flags);
815 spin_unlock_irqrestore(q->queue_lock, flags);
819 * Complete the clone and the original request.
820 * Must be called without queue lock.
822 static void dm_end_request(struct request *clone, int error)
824 struct dm_rq_target_io *tio = clone->end_io_data;
825 struct mapped_device *md = tio->md;
826 struct request *rq = tio->orig;
828 if (blk_pc_request(rq)) {
829 rq->errors = clone->errors;
830 rq->resid_len = clone->resid_len;
834 * We are using the sense buffer of the original
836 * So setting the length of the sense data is enough.
838 rq->sense_len = clone->sense_len;
841 free_rq_clone(clone);
843 blk_end_request_all(rq, error);
849 * Request completion handler for request-based dm
851 static void dm_softirq_done(struct request *rq)
853 struct request *clone = rq->completion_data;
854 struct dm_rq_target_io *tio = clone->end_io_data;
855 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
856 int error = tio->error;
858 if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
859 error = rq_end_io(tio->ti, clone, error, &tio->info);
862 /* The target wants to complete the I/O */
863 dm_end_request(clone, error);
864 else if (error == DM_ENDIO_INCOMPLETE)
865 /* The target will handle the I/O */
867 else if (error == DM_ENDIO_REQUEUE)
868 /* The target wants to requeue the I/O */
869 dm_requeue_unmapped_request(clone);
871 DMWARN("unimplemented target endio return value: %d", error);
877 * Complete the clone and the original request with the error status
878 * through softirq context.
880 static void dm_complete_request(struct request *clone, int error)
882 struct dm_rq_target_io *tio = clone->end_io_data;
883 struct request *rq = tio->orig;
886 rq->completion_data = clone;
887 blk_complete_request(rq);
891 * Complete the not-mapped clone and the original request with the error status
892 * through softirq context.
893 * Target's rq_end_io() function isn't called.
894 * This may be used when the target's map_rq() function fails.
896 void dm_kill_unmapped_request(struct request *clone, int error)
898 struct dm_rq_target_io *tio = clone->end_io_data;
899 struct request *rq = tio->orig;
901 rq->cmd_flags |= REQ_FAILED;
902 dm_complete_request(clone, error);
904 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
907 * Called with the queue lock held
909 static void end_clone_request(struct request *clone, int error)
912 * For just cleaning up the information of the queue in which
913 * the clone was dispatched.
914 * The clone is *NOT* freed actually here because it is alloced from
915 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
917 __blk_put_request(clone->q, clone);
920 * Actual request completion is done in a softirq context which doesn't
921 * hold the queue lock. Otherwise, deadlock could occur because:
922 * - another request may be submitted by the upper level driver
923 * of the stacking during the completion
924 * - the submission which requires queue lock may be done
927 dm_complete_request(clone, error);
930 static sector_t max_io_len(struct mapped_device *md,
931 sector_t sector, struct dm_target *ti)
933 sector_t offset = sector - ti->begin;
934 sector_t len = ti->len - offset;
937 * Does the target need to split even further ?
941 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
950 static void __map_bio(struct dm_target *ti, struct bio *clone,
951 struct dm_target_io *tio)
955 struct mapped_device *md;
957 clone->bi_end_io = clone_endio;
958 clone->bi_private = tio;
961 * Map the clone. If r == 0 we don't need to do
962 * anything, the target has assumed ownership of
965 atomic_inc(&tio->io->io_count);
966 sector = clone->bi_sector;
967 r = ti->type->map(ti, clone, &tio->info);
968 if (r == DM_MAPIO_REMAPPED) {
969 /* the bio has been remapped so dispatch it */
971 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
972 tio->io->bio->bi_bdev->bd_dev, sector);
974 generic_make_request(clone);
975 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
976 /* error the io and bail out, or requeue it if needed */
978 dec_pending(tio->io, r);
980 * Store bio_set for cleanup.
982 clone->bi_private = md->bs;
986 DMWARN("unimplemented target map return value: %d", r);
992 struct mapped_device *md;
993 struct dm_table *map;
997 sector_t sector_count;
1001 static void dm_bio_destructor(struct bio *bio)
1003 struct bio_set *bs = bio->bi_private;
1009 * Creates a little bio that is just does part of a bvec.
1011 static struct bio *split_bvec(struct bio *bio, sector_t sector,
1012 unsigned short idx, unsigned int offset,
1013 unsigned int len, struct bio_set *bs)
1016 struct bio_vec *bv = bio->bi_io_vec + idx;
1018 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
1019 clone->bi_destructor = dm_bio_destructor;
1020 *clone->bi_io_vec = *bv;
1022 clone->bi_sector = sector;
1023 clone->bi_bdev = bio->bi_bdev;
1024 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
1026 clone->bi_size = to_bytes(len);
1027 clone->bi_io_vec->bv_offset = offset;
1028 clone->bi_io_vec->bv_len = clone->bi_size;
1029 clone->bi_flags |= 1 << BIO_CLONED;
1031 if (bio_integrity(bio)) {
1032 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1033 bio_integrity_trim(clone,
1034 bio_sector_offset(bio, idx, offset), len);
1041 * Creates a bio that consists of range of complete bvecs.
1043 static struct bio *clone_bio(struct bio *bio, sector_t sector,
1044 unsigned short idx, unsigned short bv_count,
1045 unsigned int len, struct bio_set *bs)
1049 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1050 __bio_clone(clone, bio);
1051 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
1052 clone->bi_destructor = dm_bio_destructor;
1053 clone->bi_sector = sector;
1054 clone->bi_idx = idx;
1055 clone->bi_vcnt = idx + bv_count;
1056 clone->bi_size = to_bytes(len);
1057 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1059 if (bio_integrity(bio)) {
1060 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1062 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1063 bio_integrity_trim(clone,
1064 bio_sector_offset(bio, idx, 0), len);
1070 static struct dm_target_io *alloc_tio(struct clone_info *ci,
1071 struct dm_target *ti)
1073 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1077 memset(&tio->info, 0, sizeof(tio->info));
1082 static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1085 struct dm_target_io *tio = alloc_tio(ci, ti);
1088 tio->info.flush_request = flush_nr;
1090 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1091 __bio_clone(clone, ci->bio);
1092 clone->bi_destructor = dm_bio_destructor;
1094 __map_bio(ti, clone, tio);
1097 static int __clone_and_map_empty_barrier(struct clone_info *ci)
1099 unsigned target_nr = 0, flush_nr;
1100 struct dm_target *ti;
1102 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1103 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1105 __flush_target(ci, ti, flush_nr);
1107 ci->sector_count = 0;
1112 static int __clone_and_map(struct clone_info *ci)
1114 struct bio *clone, *bio = ci->bio;
1115 struct dm_target *ti;
1116 sector_t len = 0, max;
1117 struct dm_target_io *tio;
1119 if (unlikely(bio_empty_barrier(bio)))
1120 return __clone_and_map_empty_barrier(ci);
1122 ti = dm_table_find_target(ci->map, ci->sector);
1123 if (!dm_target_is_valid(ti))
1126 max = max_io_len(ci->md, ci->sector, ti);
1129 * Allocate a target io object.
1131 tio = alloc_tio(ci, ti);
1133 if (ci->sector_count <= max) {
1135 * Optimise for the simple case where we can do all of
1136 * the remaining io with a single clone.
1138 clone = clone_bio(bio, ci->sector, ci->idx,
1139 bio->bi_vcnt - ci->idx, ci->sector_count,
1141 __map_bio(ti, clone, tio);
1142 ci->sector_count = 0;
1144 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1146 * There are some bvecs that don't span targets.
1147 * Do as many of these as possible.
1150 sector_t remaining = max;
1153 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1154 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1156 if (bv_len > remaining)
1159 remaining -= bv_len;
1163 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1165 __map_bio(ti, clone, tio);
1168 ci->sector_count -= len;
1173 * Handle a bvec that must be split between two or more targets.
1175 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1176 sector_t remaining = to_sector(bv->bv_len);
1177 unsigned int offset = 0;
1181 ti = dm_table_find_target(ci->map, ci->sector);
1182 if (!dm_target_is_valid(ti))
1185 max = max_io_len(ci->md, ci->sector, ti);
1187 tio = alloc_tio(ci, ti);
1190 len = min(remaining, max);
1192 clone = split_bvec(bio, ci->sector, ci->idx,
1193 bv->bv_offset + offset, len,
1196 __map_bio(ti, clone, tio);
1199 ci->sector_count -= len;
1200 offset += to_bytes(len);
1201 } while (remaining -= len);
1210 * Split the bio into several clones and submit it to targets.
1212 static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
1214 struct clone_info ci;
1217 ci.map = dm_get_table(md);
1218 if (unlikely(!ci.map)) {
1219 if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
1222 if (!md->barrier_error)
1223 md->barrier_error = -EIO;
1229 ci.io = alloc_io(md);
1231 atomic_set(&ci.io->io_count, 1);
1234 spin_lock_init(&ci.io->endio_lock);
1235 ci.sector = bio->bi_sector;
1236 ci.sector_count = bio_sectors(bio);
1237 if (unlikely(bio_empty_barrier(bio)))
1238 ci.sector_count = 1;
1239 ci.idx = bio->bi_idx;
1241 start_io_acct(ci.io);
1242 while (ci.sector_count && !error)
1243 error = __clone_and_map(&ci);
1245 /* drop the extra reference count */
1246 dec_pending(ci.io, error);
1247 dm_table_put(ci.map);
1249 /*-----------------------------------------------------------------
1251 *---------------------------------------------------------------*/
1253 static int dm_merge_bvec(struct request_queue *q,
1254 struct bvec_merge_data *bvm,
1255 struct bio_vec *biovec)
1257 struct mapped_device *md = q->queuedata;
1258 struct dm_table *map = dm_get_table(md);
1259 struct dm_target *ti;
1260 sector_t max_sectors;
1266 ti = dm_table_find_target(map, bvm->bi_sector);
1267 if (!dm_target_is_valid(ti))
1271 * Find maximum amount of I/O that won't need splitting
1273 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1274 (sector_t) BIO_MAX_SECTORS);
1275 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1280 * merge_bvec_fn() returns number of bytes
1281 * it can accept at this offset
1282 * max is precomputed maximal io size
1284 if (max_size && ti->type->merge)
1285 max_size = ti->type->merge(ti, bvm, biovec, max_size);
1287 * If the target doesn't support merge method and some of the devices
1288 * provided their merge_bvec method (we know this by looking at
1289 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1290 * entries. So always set max_size to 0, and the code below allows
1293 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1302 * Always allow an entire first page
1304 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1305 max_size = biovec->bv_len;
1311 * The request function that just remaps the bio built up by
1314 static int _dm_request(struct request_queue *q, struct bio *bio)
1316 int rw = bio_data_dir(bio);
1317 struct mapped_device *md = q->queuedata;
1320 down_read(&md->io_lock);
1322 cpu = part_stat_lock();
1323 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1324 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1328 * If we're suspended or the thread is processing barriers
1329 * we have to queue this io for later.
1331 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1332 unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
1333 up_read(&md->io_lock);
1335 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1336 bio_rw(bio) == READA) {
1346 __split_and_process_bio(md, bio);
1347 up_read(&md->io_lock);
1351 static int dm_make_request(struct request_queue *q, struct bio *bio)
1353 struct mapped_device *md = q->queuedata;
1355 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
1356 bio_endio(bio, -EOPNOTSUPP);
1360 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1363 static int dm_request_based(struct mapped_device *md)
1365 return blk_queue_stackable(md->queue);
1368 static int dm_request(struct request_queue *q, struct bio *bio)
1370 struct mapped_device *md = q->queuedata;
1372 if (dm_request_based(md))
1373 return dm_make_request(q, bio);
1375 return _dm_request(q, bio);
1378 void dm_dispatch_request(struct request *rq)
1382 if (blk_queue_io_stat(rq->q))
1383 rq->cmd_flags |= REQ_IO_STAT;
1385 rq->start_time = jiffies;
1386 r = blk_insert_cloned_request(rq->q, rq);
1388 dm_complete_request(rq, r);
1390 EXPORT_SYMBOL_GPL(dm_dispatch_request);
1392 static void dm_rq_bio_destructor(struct bio *bio)
1394 struct dm_rq_clone_bio_info *info = bio->bi_private;
1395 struct mapped_device *md = info->tio->md;
1397 free_bio_info(info);
1398 bio_free(bio, md->bs);
1401 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1404 struct dm_rq_target_io *tio = data;
1405 struct mapped_device *md = tio->md;
1406 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1411 info->orig = bio_orig;
1413 bio->bi_end_io = end_clone_bio;
1414 bio->bi_private = info;
1415 bio->bi_destructor = dm_rq_bio_destructor;
1420 static int setup_clone(struct request *clone, struct request *rq,
1421 struct dm_rq_target_io *tio)
1423 int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1424 dm_rq_bio_constructor, tio);
1429 clone->cmd = rq->cmd;
1430 clone->cmd_len = rq->cmd_len;
1431 clone->sense = rq->sense;
1432 clone->buffer = rq->buffer;
1433 clone->end_io = end_clone_request;
1434 clone->end_io_data = tio;
1439 static int dm_rq_flush_suspending(struct mapped_device *md)
1441 return !md->suspend_rq.special;
1445 * Called with the queue lock held.
1447 static int dm_prep_fn(struct request_queue *q, struct request *rq)
1449 struct mapped_device *md = q->queuedata;
1450 struct dm_rq_target_io *tio;
1451 struct request *clone;
1453 if (unlikely(rq == &md->suspend_rq)) {
1454 if (dm_rq_flush_suspending(md))
1457 /* The flush suspend was interrupted */
1458 return BLKPREP_KILL;
1461 if (unlikely(rq->special)) {
1462 DMWARN("Already has something in rq->special.");
1463 return BLKPREP_KILL;
1466 tio = alloc_rq_tio(md); /* Only one for each original request */
1469 return BLKPREP_DEFER;
1475 memset(&tio->info, 0, sizeof(tio->info));
1477 clone = &tio->clone;
1478 if (setup_clone(clone, rq, tio)) {
1481 return BLKPREP_DEFER;
1484 rq->special = clone;
1485 rq->cmd_flags |= REQ_DONTPREP;
1492 * 0 : the request has been processed (not requeued)
1493 * !0 : the request has been requeued
1495 static int map_request(struct dm_target *ti, struct request *rq,
1496 struct mapped_device *md)
1498 int r, requeued = 0;
1499 struct request *clone = rq->special;
1500 struct dm_rq_target_io *tio = clone->end_io_data;
1503 * Hold the md reference here for the in-flight I/O.
1504 * We can't rely on the reference count by device opener,
1505 * because the device may be closed during the request completion
1506 * when all bios are completed.
1507 * See the comment in rq_completed() too.
1512 r = ti->type->map_rq(ti, clone, &tio->info);
1514 case DM_MAPIO_SUBMITTED:
1515 /* The target has taken the I/O to submit by itself later */
1517 case DM_MAPIO_REMAPPED:
1518 /* The target has remapped the I/O so dispatch it */
1519 dm_dispatch_request(clone);
1521 case DM_MAPIO_REQUEUE:
1522 /* The target wants to requeue the I/O */
1523 dm_requeue_unmapped_request(clone);
1528 DMWARN("unimplemented target map return value: %d", r);
1532 /* The target wants to complete the I/O */
1533 dm_kill_unmapped_request(clone, r);
1541 * q->request_fn for request-based dm.
1542 * Called with the queue lock held.
1544 static void dm_request_fn(struct request_queue *q)
1546 struct mapped_device *md = q->queuedata;
1547 struct dm_table *map = dm_get_table(md);
1548 struct dm_target *ti;
1552 * For noflush suspend, check blk_queue_stopped() to immediately
1553 * quit I/O dispatching.
1555 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1556 rq = blk_peek_request(q);
1560 if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1561 if (queue_in_flight(q))
1562 /* Not quiet yet. Wait more */
1565 /* This device should be quiet now */
1567 blk_start_request(rq);
1568 __blk_end_request_all(rq, 0);
1573 ti = dm_table_find_target(map, blk_rq_pos(rq));
1574 if (ti->type->busy && ti->type->busy(ti))
1577 blk_start_request(rq);
1578 spin_unlock(q->queue_lock);
1579 if (map_request(ti, rq, md))
1582 spin_lock_irq(q->queue_lock);
1588 spin_lock_irq(q->queue_lock);
1591 if (!elv_queue_empty(q))
1592 /* Some requests still remain, retry later */
1601 int dm_underlying_device_busy(struct request_queue *q)
1603 return blk_lld_busy(q);
1605 EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1607 static int dm_lld_busy(struct request_queue *q)
1610 struct mapped_device *md = q->queuedata;
1611 struct dm_table *map = dm_get_table(md);
1613 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1616 r = dm_table_any_busy_target(map);
1623 static void dm_unplug_all(struct request_queue *q)
1625 struct mapped_device *md = q->queuedata;
1626 struct dm_table *map = dm_get_table(md);
1629 if (dm_request_based(md))
1630 generic_unplug_device(q);
1632 dm_table_unplug_all(map);
1637 static int dm_any_congested(void *congested_data, int bdi_bits)
1640 struct mapped_device *md = congested_data;
1641 struct dm_table *map;
1643 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1644 map = dm_get_table(md);
1647 * Request-based dm cares about only own queue for
1648 * the query about congestion status of request_queue
1650 if (dm_request_based(md))
1651 r = md->queue->backing_dev_info.state &
1654 r = dm_table_any_congested(map, bdi_bits);
1663 /*-----------------------------------------------------------------
1664 * An IDR is used to keep track of allocated minor numbers.
1665 *---------------------------------------------------------------*/
1666 static DEFINE_IDR(_minor_idr);
1668 static void free_minor(int minor)
1670 spin_lock(&_minor_lock);
1671 idr_remove(&_minor_idr, minor);
1672 spin_unlock(&_minor_lock);
1676 * See if the device with a specific minor # is free.
1678 static int specific_minor(int minor)
1682 if (minor >= (1 << MINORBITS))
1685 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1689 spin_lock(&_minor_lock);
1691 if (idr_find(&_minor_idr, minor)) {
1696 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
1701 idr_remove(&_minor_idr, m);
1707 spin_unlock(&_minor_lock);
1711 static int next_free_minor(int *minor)
1715 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1719 spin_lock(&_minor_lock);
1721 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1725 if (m >= (1 << MINORBITS)) {
1726 idr_remove(&_minor_idr, m);
1734 spin_unlock(&_minor_lock);
1738 static const struct block_device_operations dm_blk_dops;
1740 static void dm_wq_work(struct work_struct *work);
1743 * Allocate and initialise a blank device with a given minor.
1745 static struct mapped_device *alloc_dev(int minor)
1748 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1752 DMWARN("unable to allocate device, out of memory.");
1756 if (!try_module_get(THIS_MODULE))
1757 goto bad_module_get;
1759 /* get a minor number for the dev */
1760 if (minor == DM_ANY_MINOR)
1761 r = next_free_minor(&minor);
1763 r = specific_minor(minor);
1767 init_rwsem(&md->io_lock);
1768 mutex_init(&md->suspend_lock);
1769 spin_lock_init(&md->deferred_lock);
1770 rwlock_init(&md->map_lock);
1771 atomic_set(&md->holders, 1);
1772 atomic_set(&md->open_count, 0);
1773 atomic_set(&md->event_nr, 0);
1774 atomic_set(&md->uevent_seq, 0);
1775 INIT_LIST_HEAD(&md->uevent_list);
1776 spin_lock_init(&md->uevent_lock);
1778 md->queue = blk_init_queue(dm_request_fn, NULL);
1783 * Request-based dm devices cannot be stacked on top of bio-based dm
1784 * devices. The type of this dm device has not been decided yet,
1785 * although we initialized the queue using blk_init_queue().
1786 * The type is decided at the first table loading time.
1787 * To prevent problematic device stacking, clear the queue flag
1788 * for request stacking support until then.
1790 * This queue is new, so no concurrency on the queue_flags.
1792 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1793 md->saved_make_request_fn = md->queue->make_request_fn;
1794 md->queue->queuedata = md;
1795 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1796 md->queue->backing_dev_info.congested_data = md;
1797 blk_queue_make_request(md->queue, dm_request);
1798 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1799 md->queue->unplug_fn = dm_unplug_all;
1800 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1801 blk_queue_softirq_done(md->queue, dm_softirq_done);
1802 blk_queue_prep_rq(md->queue, dm_prep_fn);
1803 blk_queue_lld_busy(md->queue, dm_lld_busy);
1805 md->disk = alloc_disk(1);
1809 atomic_set(&md->pending[0], 0);
1810 atomic_set(&md->pending[1], 0);
1811 init_waitqueue_head(&md->wait);
1812 INIT_WORK(&md->work, dm_wq_work);
1813 init_waitqueue_head(&md->eventq);
1815 md->disk->major = _major;
1816 md->disk->first_minor = minor;
1817 md->disk->fops = &dm_blk_dops;
1818 md->disk->queue = md->queue;
1819 md->disk->private_data = md;
1820 sprintf(md->disk->disk_name, "dm-%d", minor);
1822 format_dev_t(md->name, MKDEV(_major, minor));
1824 md->wq = create_singlethread_workqueue("kdmflush");
1828 md->bdev = bdget_disk(md->disk, 0);
1832 /* Populate the mapping, nobody knows we exist yet */
1833 spin_lock(&_minor_lock);
1834 old_md = idr_replace(&_minor_idr, md, minor);
1835 spin_unlock(&_minor_lock);
1837 BUG_ON(old_md != MINOR_ALLOCED);
1842 destroy_workqueue(md->wq);
1844 del_gendisk(md->disk);
1847 blk_cleanup_queue(md->queue);
1851 module_put(THIS_MODULE);
1857 static void unlock_fs(struct mapped_device *md);
1859 static void free_dev(struct mapped_device *md)
1861 int minor = MINOR(disk_devt(md->disk));
1865 destroy_workqueue(md->wq);
1867 mempool_destroy(md->tio_pool);
1869 mempool_destroy(md->io_pool);
1871 bioset_free(md->bs);
1872 blk_integrity_unregister(md->disk);
1873 del_gendisk(md->disk);
1876 spin_lock(&_minor_lock);
1877 md->disk->private_data = NULL;
1878 spin_unlock(&_minor_lock);
1881 blk_cleanup_queue(md->queue);
1882 module_put(THIS_MODULE);
1886 static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1888 struct dm_md_mempools *p;
1890 if (md->io_pool && md->tio_pool && md->bs)
1891 /* the md already has necessary mempools */
1894 p = dm_table_get_md_mempools(t);
1895 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1897 md->io_pool = p->io_pool;
1899 md->tio_pool = p->tio_pool;
1905 /* mempool bind completed, now no need any mempools in the table */
1906 dm_table_free_md_mempools(t);
1910 * Bind a table to the device.
1912 static void event_callback(void *context)
1914 unsigned long flags;
1916 struct mapped_device *md = (struct mapped_device *) context;
1918 spin_lock_irqsave(&md->uevent_lock, flags);
1919 list_splice_init(&md->uevent_list, &uevents);
1920 spin_unlock_irqrestore(&md->uevent_lock, flags);
1922 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
1924 atomic_inc(&md->event_nr);
1925 wake_up(&md->eventq);
1929 * Protected by md->suspend_lock obtained by dm_swap_table().
1931 static void __set_size(struct mapped_device *md, sector_t size)
1933 set_capacity(md->disk, size);
1935 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1938 static int __bind(struct mapped_device *md, struct dm_table *t,
1939 struct queue_limits *limits)
1941 struct request_queue *q = md->queue;
1943 unsigned long flags;
1945 size = dm_table_get_size(t);
1948 * Wipe any geometry if the size of the table changed.
1950 if (size != get_capacity(md->disk))
1951 memset(&md->geometry, 0, sizeof(md->geometry));
1953 __set_size(md, size);
1956 dm_table_destroy(t);
1960 dm_table_event_callback(t, event_callback, md);
1963 * The queue hasn't been stopped yet, if the old table type wasn't
1964 * for request-based during suspension. So stop it to prevent
1965 * I/O mapping before resume.
1966 * This must be done before setting the queue restrictions,
1967 * because request-based dm may be run just after the setting.
1969 if (dm_table_request_based(t) && !blk_queue_stopped(q))
1972 __bind_mempools(md, t);
1974 write_lock_irqsave(&md->map_lock, flags);
1976 dm_table_set_restrictions(t, q, limits);
1977 write_unlock_irqrestore(&md->map_lock, flags);
1982 static void __unbind(struct mapped_device *md)
1984 struct dm_table *map = md->map;
1985 unsigned long flags;
1990 dm_table_event_callback(map, NULL, NULL);
1991 write_lock_irqsave(&md->map_lock, flags);
1993 write_unlock_irqrestore(&md->map_lock, flags);
1994 dm_table_destroy(map);
1998 * Constructor for a new device.
2000 int dm_create(int minor, struct mapped_device **result)
2002 struct mapped_device *md;
2004 md = alloc_dev(minor);
2014 static struct mapped_device *dm_find_md(dev_t dev)
2016 struct mapped_device *md;
2017 unsigned minor = MINOR(dev);
2019 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2022 spin_lock(&_minor_lock);
2024 md = idr_find(&_minor_idr, minor);
2025 if (md && (md == MINOR_ALLOCED ||
2026 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2027 test_bit(DMF_FREEING, &md->flags))) {
2033 spin_unlock(&_minor_lock);
2038 struct mapped_device *dm_get_md(dev_t dev)
2040 struct mapped_device *md = dm_find_md(dev);
2048 void *dm_get_mdptr(struct mapped_device *md)
2050 return md->interface_ptr;
2053 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2055 md->interface_ptr = ptr;
2058 void dm_get(struct mapped_device *md)
2060 atomic_inc(&md->holders);
2063 const char *dm_device_name(struct mapped_device *md)
2067 EXPORT_SYMBOL_GPL(dm_device_name);
2069 void dm_put(struct mapped_device *md)
2071 struct dm_table *map;
2073 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2075 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
2076 map = dm_get_table(md);
2077 idr_replace(&_minor_idr, MINOR_ALLOCED,
2078 MINOR(disk_devt(dm_disk(md))));
2079 set_bit(DMF_FREEING, &md->flags);
2080 spin_unlock(&_minor_lock);
2081 if (!dm_suspended(md)) {
2082 dm_table_presuspend_targets(map);
2083 dm_table_postsuspend_targets(map);
2091 EXPORT_SYMBOL_GPL(dm_put);
2093 static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
2096 DECLARE_WAITQUEUE(wait, current);
2097 struct request_queue *q = md->queue;
2098 unsigned long flags;
2100 dm_unplug_all(md->queue);
2102 add_wait_queue(&md->wait, &wait);
2105 set_current_state(interruptible);
2108 if (dm_request_based(md)) {
2109 spin_lock_irqsave(q->queue_lock, flags);
2110 if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2111 spin_unlock_irqrestore(q->queue_lock, flags);
2114 spin_unlock_irqrestore(q->queue_lock, flags);
2115 } else if (!atomic_read(&md->pending[0]) &&
2116 !atomic_read(&md->pending[1]))
2119 if (interruptible == TASK_INTERRUPTIBLE &&
2120 signal_pending(current)) {
2127 set_current_state(TASK_RUNNING);
2129 remove_wait_queue(&md->wait, &wait);
2134 static void dm_flush(struct mapped_device *md)
2136 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2138 bio_init(&md->barrier_bio);
2139 md->barrier_bio.bi_bdev = md->bdev;
2140 md->barrier_bio.bi_rw = WRITE_BARRIER;
2141 __split_and_process_bio(md, &md->barrier_bio);
2143 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2146 static void process_barrier(struct mapped_device *md, struct bio *bio)
2148 md->barrier_error = 0;
2152 if (!bio_empty_barrier(bio)) {
2153 __split_and_process_bio(md, bio);
2157 if (md->barrier_error != DM_ENDIO_REQUEUE)
2158 bio_endio(bio, md->barrier_error);
2160 spin_lock_irq(&md->deferred_lock);
2161 bio_list_add_head(&md->deferred, bio);
2162 spin_unlock_irq(&md->deferred_lock);
2167 * Process the deferred bios
2169 static void dm_wq_work(struct work_struct *work)
2171 struct mapped_device *md = container_of(work, struct mapped_device,
2175 down_write(&md->io_lock);
2177 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2178 spin_lock_irq(&md->deferred_lock);
2179 c = bio_list_pop(&md->deferred);
2180 spin_unlock_irq(&md->deferred_lock);
2183 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2187 up_write(&md->io_lock);
2189 if (dm_request_based(md))
2190 generic_make_request(c);
2192 if (bio_rw_flagged(c, BIO_RW_BARRIER))
2193 process_barrier(md, c);
2195 __split_and_process_bio(md, c);
2198 down_write(&md->io_lock);
2201 up_write(&md->io_lock);
2204 static void dm_queue_flush(struct mapped_device *md)
2206 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2207 smp_mb__after_clear_bit();
2208 queue_work(md->wq, &md->work);
2212 * Swap in a new table (destroying old one).
2214 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2216 struct queue_limits limits;
2219 mutex_lock(&md->suspend_lock);
2221 /* device must be suspended */
2222 if (!dm_suspended(md))
2225 r = dm_calculate_queue_limits(table, &limits);
2229 /* cannot change the device type, once a table is bound */
2231 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2232 DMWARN("can't change the device type after a table is bound");
2237 r = __bind(md, table, &limits);
2240 mutex_unlock(&md->suspend_lock);
2244 static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2246 md->suspend_rq.special = (void *)0x1;
2249 static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2251 struct request_queue *q = md->queue;
2252 unsigned long flags;
2254 spin_lock_irqsave(q->queue_lock, flags);
2256 dm_rq_invalidate_suspend_marker(md);
2258 spin_unlock_irqrestore(q->queue_lock, flags);
2261 static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2263 struct request *rq = &md->suspend_rq;
2264 struct request_queue *q = md->queue;
2270 blk_insert_request(q, rq, 0, NULL);
2274 static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2277 struct request *rq = &md->suspend_rq;
2278 struct request_queue *q = md->queue;
2279 unsigned long flags;
2284 /* The marker must be protected by queue lock if it is in use */
2285 spin_lock_irqsave(q->queue_lock, flags);
2286 if (unlikely(rq->ref_count)) {
2288 * This can happen, when the previous flush suspend was
2289 * interrupted, the marker is still in the queue and
2290 * this flush suspend has been invoked, because we don't
2291 * remove the marker at the time of suspend interruption.
2292 * We have only one marker per mapped_device, so we can't
2293 * start another flush suspend while it is in use.
2295 BUG_ON(!rq->special); /* The marker should be invalidated */
2296 DMWARN("Invalidating the previous flush suspend is still in"
2297 " progress. Please retry later.");
2300 spin_unlock_irqrestore(q->queue_lock, flags);
2306 * Functions to lock and unlock any filesystem running on the
2309 static int lock_fs(struct mapped_device *md)
2313 WARN_ON(md->frozen_sb);
2315 md->frozen_sb = freeze_bdev(md->bdev);
2316 if (IS_ERR(md->frozen_sb)) {
2317 r = PTR_ERR(md->frozen_sb);
2318 md->frozen_sb = NULL;
2322 set_bit(DMF_FROZEN, &md->flags);
2327 static void unlock_fs(struct mapped_device *md)
2329 if (!test_bit(DMF_FROZEN, &md->flags))
2332 thaw_bdev(md->bdev, md->frozen_sb);
2333 md->frozen_sb = NULL;
2334 clear_bit(DMF_FROZEN, &md->flags);
2338 * We need to be able to change a mapping table under a mounted
2339 * filesystem. For example we might want to move some data in
2340 * the background. Before the table can be swapped with
2341 * dm_bind_table, dm_suspend must be called to flush any in
2342 * flight bios and ensure that any further io gets deferred.
2345 * Suspend mechanism in request-based dm.
2347 * After the suspend starts, further incoming requests are kept in
2348 * the request_queue and deferred.
2349 * Remaining requests in the request_queue at the start of suspend are flushed
2350 * if it is flush suspend.
2351 * The suspend completes when the following conditions have been satisfied,
2353 * 1. q->in_flight is 0 (which means no in_flight request)
2354 * 2. queue has been stopped (which means no request dispatching)
2359 * Noflush suspend doesn't need to dispatch remaining requests.
2360 * So stop the queue immediately. Then, wait for all in_flight requests
2361 * to be completed or requeued.
2363 * To abort noflush suspend, start the queue.
2368 * Flush suspend needs to dispatch remaining requests. So stop the queue
2369 * after the remaining requests are completed. (Requeued request must be also
2370 * re-dispatched and completed. Until then, we can't stop the queue.)
2372 * During flushing the remaining requests, further incoming requests are also
2373 * inserted to the same queue. To distinguish which requests are to be
2374 * flushed, we insert a marker request to the queue at the time of starting
2375 * flush suspend, like a barrier.
2376 * The dispatching is blocked when the marker is found on the top of the queue.
2377 * And the queue is stopped when all in_flight requests are completed, since
2378 * that means the remaining requests are completely flushed.
2379 * Then, the marker is removed from the queue.
2381 * To abort flush suspend, we also need to take care of the marker, not only
2382 * starting the queue.
2383 * We don't remove the marker forcibly from the queue since it's against
2384 * the block-layer manner. Instead, we put a invalidated mark on the marker.
2385 * When the invalidated marker is found on the top of the queue, it is
2386 * immediately removed from the queue, so it doesn't block dispatching.
2387 * Because we have only one marker per mapped_device, we can't start another
2388 * flush suspend until the invalidated marker is removed from the queue.
2389 * So fail and return with -EBUSY in such a case.
2391 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2393 struct dm_table *map = NULL;
2395 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2396 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
2398 mutex_lock(&md->suspend_lock);
2400 if (dm_suspended(md)) {
2405 if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2410 map = dm_get_table(md);
2413 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2414 * This flag is cleared before dm_suspend returns.
2417 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2419 /* This does not get reverted if there's an error later. */
2420 dm_table_presuspend_targets(map);
2423 * Flush I/O to the device. noflush supersedes do_lockfs,
2424 * because lock_fs() needs to flush I/Os.
2426 if (!noflush && do_lockfs) {
2433 * Here we must make sure that no processes are submitting requests
2434 * to target drivers i.e. no one may be executing
2435 * __split_and_process_bio. This is called from dm_request and
2438 * To get all processes out of __split_and_process_bio in dm_request,
2439 * we take the write lock. To prevent any process from reentering
2440 * __split_and_process_bio from dm_request, we set
2441 * DMF_QUEUE_IO_TO_THREAD.
2443 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2444 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2445 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2446 * further calls to __split_and_process_bio from dm_wq_work.
2448 down_write(&md->io_lock);
2449 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2450 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2451 up_write(&md->io_lock);
2453 flush_workqueue(md->wq);
2455 if (dm_request_based(md))
2456 dm_rq_start_suspend(md, noflush);
2459 * At this point no more requests are entering target request routines.
2460 * We call dm_wait_for_completion to wait for all existing requests
2463 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
2465 down_write(&md->io_lock);
2467 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2468 up_write(&md->io_lock);
2470 /* were we interrupted ? */
2474 if (dm_request_based(md))
2475 dm_rq_abort_suspend(md, noflush);
2478 goto out; /* pushback list is already flushed, so skip flush */
2482 * If dm_wait_for_completion returned 0, the device is completely
2483 * quiescent now. There is no request-processing activity. All new
2484 * requests are being added to md->deferred list.
2487 dm_table_postsuspend_targets(map);
2489 set_bit(DMF_SUSPENDED, &md->flags);
2495 mutex_unlock(&md->suspend_lock);
2499 int dm_resume(struct mapped_device *md)
2502 struct dm_table *map = NULL;
2504 mutex_lock(&md->suspend_lock);
2505 if (!dm_suspended(md))
2508 map = dm_get_table(md);
2509 if (!map || !dm_table_get_size(map))
2512 r = dm_table_resume_targets(map);
2519 * Flushing deferred I/Os must be done after targets are resumed
2520 * so that mapping of targets can work correctly.
2521 * Request-based dm is queueing the deferred I/Os in its request_queue.
2523 if (dm_request_based(md))
2524 start_queue(md->queue);
2528 clear_bit(DMF_SUSPENDED, &md->flags);
2530 dm_table_unplug_all(map);
2534 mutex_unlock(&md->suspend_lock);
2539 /*-----------------------------------------------------------------
2540 * Event notification.
2541 *---------------------------------------------------------------*/
2542 void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2545 char udev_cookie[DM_COOKIE_LENGTH];
2546 char *envp[] = { udev_cookie, NULL };
2549 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2551 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2552 DM_COOKIE_ENV_VAR_NAME, cookie);
2553 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2557 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2559 return atomic_add_return(1, &md->uevent_seq);
2562 uint32_t dm_get_event_nr(struct mapped_device *md)
2564 return atomic_read(&md->event_nr);
2567 int dm_wait_event(struct mapped_device *md, int event_nr)
2569 return wait_event_interruptible(md->eventq,
2570 (event_nr != atomic_read(&md->event_nr)));
2573 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2575 unsigned long flags;
2577 spin_lock_irqsave(&md->uevent_lock, flags);
2578 list_add(elist, &md->uevent_list);
2579 spin_unlock_irqrestore(&md->uevent_lock, flags);
2583 * The gendisk is only valid as long as you have a reference
2586 struct gendisk *dm_disk(struct mapped_device *md)
2591 struct kobject *dm_kobject(struct mapped_device *md)
2597 * struct mapped_device should not be exported outside of dm.c
2598 * so use this check to verify that kobj is part of md structure
2600 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2602 struct mapped_device *md;
2604 md = container_of(kobj, struct mapped_device, kobj);
2605 if (&md->kobj != kobj)
2608 if (test_bit(DMF_FREEING, &md->flags) ||
2609 test_bit(DMF_DELETING, &md->flags))
2616 int dm_suspended(struct mapped_device *md)
2618 return test_bit(DMF_SUSPENDED, &md->flags);
2621 int dm_noflush_suspending(struct dm_target *ti)
2623 struct mapped_device *md = dm_table_get_md(ti->table);
2624 int r = __noflush_suspending(md);
2630 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2632 struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2634 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2639 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2640 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2641 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2642 if (!pools->io_pool)
2643 goto free_pools_and_out;
2645 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2646 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2647 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2648 if (!pools->tio_pool)
2649 goto free_io_pool_and_out;
2651 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2652 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2654 goto free_tio_pool_and_out;
2658 free_tio_pool_and_out:
2659 mempool_destroy(pools->tio_pool);
2661 free_io_pool_and_out:
2662 mempool_destroy(pools->io_pool);
2670 void dm_free_md_mempools(struct dm_md_mempools *pools)
2676 mempool_destroy(pools->io_pool);
2678 if (pools->tio_pool)
2679 mempool_destroy(pools->tio_pool);
2682 bioset_free(pools->bs);
2687 static const struct block_device_operations dm_blk_dops = {
2688 .open = dm_blk_open,
2689 .release = dm_blk_close,
2690 .ioctl = dm_blk_ioctl,
2691 .getgeo = dm_blk_getgeo,
2692 .owner = THIS_MODULE
2695 EXPORT_SYMBOL(dm_get_mapinfo);
2700 module_init(dm_init);
2701 module_exit(dm_exit);
2703 module_param(major, uint, 0);
2704 MODULE_PARM_DESC(major, "The major number of the device mapper");
2705 MODULE_DESCRIPTION(DM_NAME " driver");
2706 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2707 MODULE_LICENSE("GPL");