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/smp_lock.h>
19 #include <linux/mempool.h>
20 #include <linux/slab.h>
21 #include <linux/idr.h>
22 #include <linux/hdreg.h>
23 #include <linux/delay.h>
25 #include <trace/events/block.h>
27 #define DM_MSG_PREFIX "core"
30 * Cookies are numeric values sent with CHANGE and REMOVE
31 * uevents while resuming, removing or renaming the device.
33 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
34 #define DM_COOKIE_LENGTH 24
36 static const char *_name = DM_NAME;
38 static unsigned int major = 0;
39 static unsigned int _major = 0;
41 static DEFINE_SPINLOCK(_minor_lock);
44 * One of these is allocated per bio.
47 struct mapped_device *md;
51 unsigned long start_time;
52 spinlock_t endio_lock;
57 * One of these is allocated per target within a bio. Hopefully
58 * this will be simplified out one day.
67 * For request-based dm.
68 * One of these is allocated per request.
70 struct dm_rq_target_io {
71 struct mapped_device *md;
73 struct request *orig, clone;
79 * For request-based dm.
80 * One of these is allocated per bio.
82 struct dm_rq_clone_bio_info {
84 struct dm_rq_target_io *tio;
87 union map_info *dm_get_mapinfo(struct bio *bio)
89 if (bio && bio->bi_private)
90 return &((struct dm_target_io *)bio->bi_private)->info;
94 union map_info *dm_get_rq_mapinfo(struct request *rq)
96 if (rq && rq->end_io_data)
97 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
100 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
102 #define MINOR_ALLOCED ((void *)-1)
105 * Bits for the md->flags field.
107 #define DMF_BLOCK_IO_FOR_SUSPEND 0
108 #define DMF_SUSPENDED 1
110 #define DMF_FREEING 3
111 #define DMF_DELETING 4
112 #define DMF_NOFLUSH_SUSPENDING 5
113 #define DMF_QUEUE_IO_TO_THREAD 6
116 * Work processed by per-device workqueue.
118 struct mapped_device {
119 struct rw_semaphore io_lock;
120 struct mutex suspend_lock;
127 struct request_queue *queue;
128 struct gendisk *disk;
134 * A list of ios that arrived while we were suspended.
137 wait_queue_head_t wait;
138 struct work_struct work;
139 struct bio_list deferred;
140 spinlock_t deferred_lock;
143 * An error from the barrier request currently being processed.
148 * Protect barrier_error from concurrent endio processing
149 * in request-based dm.
151 spinlock_t barrier_error_lock;
154 * Processing queue (flush/barriers)
156 struct workqueue_struct *wq;
157 struct work_struct barrier_work;
159 /* A pointer to the currently processing pre/post flush request */
160 struct request *flush_request;
163 * The current mapping.
165 struct dm_table *map;
168 * io objects are allocated from here.
179 wait_queue_head_t eventq;
181 struct list_head uevent_list;
182 spinlock_t uevent_lock; /* Protect access to uevent_list */
185 * freeze/thaw support require holding onto a super block
187 struct super_block *frozen_sb;
188 struct block_device *bdev;
190 /* forced geometry settings */
191 struct hd_geometry geometry;
193 /* For saving the address of __make_request for request based dm */
194 make_request_fn *saved_make_request_fn;
199 /* zero-length barrier that will be cloned and submitted to targets */
200 struct bio barrier_bio;
204 * For mempools pre-allocation at the table loading time.
206 struct dm_md_mempools {
213 static struct kmem_cache *_io_cache;
214 static struct kmem_cache *_tio_cache;
215 static struct kmem_cache *_rq_tio_cache;
216 static struct kmem_cache *_rq_bio_info_cache;
218 static int __init local_init(void)
222 /* allocate a slab for the dm_ios */
223 _io_cache = KMEM_CACHE(dm_io, 0);
227 /* allocate a slab for the target ios */
228 _tio_cache = KMEM_CACHE(dm_target_io, 0);
230 goto out_free_io_cache;
232 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
234 goto out_free_tio_cache;
236 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
237 if (!_rq_bio_info_cache)
238 goto out_free_rq_tio_cache;
240 r = dm_uevent_init();
242 goto out_free_rq_bio_info_cache;
245 r = register_blkdev(_major, _name);
247 goto out_uevent_exit;
256 out_free_rq_bio_info_cache:
257 kmem_cache_destroy(_rq_bio_info_cache);
258 out_free_rq_tio_cache:
259 kmem_cache_destroy(_rq_tio_cache);
261 kmem_cache_destroy(_tio_cache);
263 kmem_cache_destroy(_io_cache);
268 static void local_exit(void)
270 kmem_cache_destroy(_rq_bio_info_cache);
271 kmem_cache_destroy(_rq_tio_cache);
272 kmem_cache_destroy(_tio_cache);
273 kmem_cache_destroy(_io_cache);
274 unregister_blkdev(_major, _name);
279 DMINFO("cleaned up");
282 static int (*_inits[])(void) __initdata = {
292 static void (*_exits[])(void) = {
302 static int __init dm_init(void)
304 const int count = ARRAY_SIZE(_inits);
308 for (i = 0; i < count; i++) {
323 static void __exit dm_exit(void)
325 int i = ARRAY_SIZE(_exits);
332 * Block device functions
334 int dm_deleting_md(struct mapped_device *md)
336 return test_bit(DMF_DELETING, &md->flags);
339 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
341 struct mapped_device *md;
344 spin_lock(&_minor_lock);
346 md = bdev->bd_disk->private_data;
350 if (test_bit(DMF_FREEING, &md->flags) ||
351 dm_deleting_md(md)) {
357 atomic_inc(&md->open_count);
360 spin_unlock(&_minor_lock);
363 return md ? 0 : -ENXIO;
366 static int dm_blk_close(struct gendisk *disk, fmode_t mode)
368 struct mapped_device *md = disk->private_data;
371 atomic_dec(&md->open_count);
378 int dm_open_count(struct mapped_device *md)
380 return atomic_read(&md->open_count);
384 * Guarantees nothing is using the device before it's deleted.
386 int dm_lock_for_deletion(struct mapped_device *md)
390 spin_lock(&_minor_lock);
392 if (dm_open_count(md))
395 set_bit(DMF_DELETING, &md->flags);
397 spin_unlock(&_minor_lock);
402 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
404 struct mapped_device *md = bdev->bd_disk->private_data;
406 return dm_get_geometry(md, geo);
409 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
410 unsigned int cmd, unsigned long arg)
412 struct mapped_device *md = bdev->bd_disk->private_data;
413 struct dm_table *map = dm_get_live_table(md);
414 struct dm_target *tgt;
417 if (!map || !dm_table_get_size(map))
420 /* We only support devices that have a single target */
421 if (dm_table_get_num_targets(map) != 1)
424 tgt = dm_table_get_target(map, 0);
426 if (dm_suspended_md(md)) {
431 if (tgt->type->ioctl)
432 r = tgt->type->ioctl(tgt, cmd, arg);
440 static struct dm_io *alloc_io(struct mapped_device *md)
442 return mempool_alloc(md->io_pool, GFP_NOIO);
445 static void free_io(struct mapped_device *md, struct dm_io *io)
447 mempool_free(io, md->io_pool);
450 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
452 mempool_free(tio, md->tio_pool);
455 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
458 return mempool_alloc(md->tio_pool, gfp_mask);
461 static void free_rq_tio(struct dm_rq_target_io *tio)
463 mempool_free(tio, tio->md->tio_pool);
466 static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
468 return mempool_alloc(md->io_pool, GFP_ATOMIC);
471 static void free_bio_info(struct dm_rq_clone_bio_info *info)
473 mempool_free(info, info->tio->md->io_pool);
476 static int md_in_flight(struct mapped_device *md)
478 return atomic_read(&md->pending[READ]) +
479 atomic_read(&md->pending[WRITE]);
482 static void start_io_acct(struct dm_io *io)
484 struct mapped_device *md = io->md;
486 int rw = bio_data_dir(io->bio);
488 io->start_time = jiffies;
490 cpu = part_stat_lock();
491 part_round_stats(cpu, &dm_disk(md)->part0);
493 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
496 static void end_io_acct(struct dm_io *io)
498 struct mapped_device *md = io->md;
499 struct bio *bio = io->bio;
500 unsigned long duration = jiffies - io->start_time;
502 int rw = bio_data_dir(bio);
504 cpu = part_stat_lock();
505 part_round_stats(cpu, &dm_disk(md)->part0);
506 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
510 * After this is decremented the bio must not be touched if it is
513 dm_disk(md)->part0.in_flight[rw] = pending =
514 atomic_dec_return(&md->pending[rw]);
515 pending += atomic_read(&md->pending[rw^0x1]);
517 /* nudge anyone waiting on suspend queue */
523 * Add the bio to the list of deferred io.
525 static void queue_io(struct mapped_device *md, struct bio *bio)
527 down_write(&md->io_lock);
529 spin_lock_irq(&md->deferred_lock);
530 bio_list_add(&md->deferred, bio);
531 spin_unlock_irq(&md->deferred_lock);
533 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
534 queue_work(md->wq, &md->work);
536 up_write(&md->io_lock);
540 * Everyone (including functions in this file), should use this
541 * function to access the md->map field, and make sure they call
542 * dm_table_put() when finished.
544 struct dm_table *dm_get_live_table(struct mapped_device *md)
549 read_lock_irqsave(&md->map_lock, flags);
553 read_unlock_irqrestore(&md->map_lock, flags);
559 * Get the geometry associated with a dm device
561 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
569 * Set the geometry of a device.
571 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
573 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
575 if (geo->start > sz) {
576 DMWARN("Start sector is beyond the geometry limits.");
585 /*-----------------------------------------------------------------
587 * A more elegant soln is in the works that uses the queue
588 * merge fn, unfortunately there are a couple of changes to
589 * the block layer that I want to make for this. So in the
590 * interests of getting something for people to use I give
591 * you this clearly demarcated crap.
592 *---------------------------------------------------------------*/
594 static int __noflush_suspending(struct mapped_device *md)
596 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
600 * Decrements the number of outstanding ios that a bio has been
601 * cloned into, completing the original io if necc.
603 static void dec_pending(struct dm_io *io, int error)
608 struct mapped_device *md = io->md;
610 /* Push-back supersedes any I/O errors */
611 if (unlikely(error)) {
612 spin_lock_irqsave(&io->endio_lock, flags);
613 if (!(io->error > 0 && __noflush_suspending(md)))
615 spin_unlock_irqrestore(&io->endio_lock, flags);
618 if (atomic_dec_and_test(&io->io_count)) {
619 if (io->error == DM_ENDIO_REQUEUE) {
621 * Target requested pushing back the I/O.
623 spin_lock_irqsave(&md->deferred_lock, flags);
624 if (__noflush_suspending(md)) {
625 if (!(io->bio->bi_rw & REQ_HARDBARRIER))
626 bio_list_add_head(&md->deferred,
629 /* noflush suspend was interrupted. */
631 spin_unlock_irqrestore(&md->deferred_lock, flags);
634 io_error = io->error;
637 if (bio->bi_rw & REQ_HARDBARRIER) {
639 * There can be just one barrier request so we use
640 * a per-device variable for error reporting.
641 * Note that you can't touch the bio after end_io_acct
643 * We ignore -EOPNOTSUPP for empty flush reported by
644 * underlying devices. We assume that if the device
645 * doesn't support empty barriers, it doesn't need
646 * cache flushing commands.
648 if (!md->barrier_error &&
649 !(bio_empty_barrier(bio) && io_error == -EOPNOTSUPP))
650 md->barrier_error = io_error;
657 if (io_error != DM_ENDIO_REQUEUE) {
658 trace_block_bio_complete(md->queue, bio);
660 bio_endio(bio, io_error);
666 static void clone_endio(struct bio *bio, int error)
669 struct dm_target_io *tio = bio->bi_private;
670 struct dm_io *io = tio->io;
671 struct mapped_device *md = tio->io->md;
672 dm_endio_fn endio = tio->ti->type->end_io;
674 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
678 r = endio(tio->ti, bio, error, &tio->info);
679 if (r < 0 || r == DM_ENDIO_REQUEUE)
681 * error and requeue request are handled
685 else if (r == DM_ENDIO_INCOMPLETE)
686 /* The target will handle the io */
689 DMWARN("unimplemented target endio return value: %d", r);
695 * Store md for cleanup instead of tio which is about to get freed.
697 bio->bi_private = md->bs;
701 dec_pending(io, error);
705 * Partial completion handling for request-based dm
707 static void end_clone_bio(struct bio *clone, int error)
709 struct dm_rq_clone_bio_info *info = clone->bi_private;
710 struct dm_rq_target_io *tio = info->tio;
711 struct bio *bio = info->orig;
712 unsigned int nr_bytes = info->orig->bi_size;
718 * An error has already been detected on the request.
719 * Once error occurred, just let clone->end_io() handle
725 * Don't notice the error to the upper layer yet.
726 * The error handling decision is made by the target driver,
727 * when the request is completed.
734 * I/O for the bio successfully completed.
735 * Notice the data completion to the upper layer.
739 * bios are processed from the head of the list.
740 * So the completing bio should always be rq->bio.
741 * If it's not, something wrong is happening.
743 if (tio->orig->bio != bio)
744 DMERR("bio completion is going in the middle of the request");
747 * Update the original request.
748 * Do not use blk_end_request() here, because it may complete
749 * the original request before the clone, and break the ordering.
751 blk_update_request(tio->orig, 0, nr_bytes);
754 static void store_barrier_error(struct mapped_device *md, int error)
758 spin_lock_irqsave(&md->barrier_error_lock, flags);
760 * Basically, the first error is taken, but:
761 * -EOPNOTSUPP supersedes any I/O error.
762 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
764 if (!md->barrier_error || error == -EOPNOTSUPP ||
765 (md->barrier_error != -EOPNOTSUPP &&
766 error == DM_ENDIO_REQUEUE))
767 md->barrier_error = error;
768 spin_unlock_irqrestore(&md->barrier_error_lock, flags);
772 * Don't touch any member of the md after calling this function because
773 * the md may be freed in dm_put() at the end of this function.
774 * Or do dm_get() before calling this function and dm_put() later.
776 static void rq_completed(struct mapped_device *md, int rw, int run_queue)
778 atomic_dec(&md->pending[rw]);
780 /* nudge anyone waiting on suspend queue */
781 if (!md_in_flight(md))
785 blk_run_queue(md->queue);
788 * dm_put() must be at the end of this function. See the comment above
793 static void free_rq_clone(struct request *clone)
795 struct dm_rq_target_io *tio = clone->end_io_data;
797 blk_rq_unprep_clone(clone);
802 * Complete the clone and the original request.
803 * Must be called without queue lock.
805 static void dm_end_request(struct request *clone, int error)
807 int rw = rq_data_dir(clone);
809 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
810 struct dm_rq_target_io *tio = clone->end_io_data;
811 struct mapped_device *md = tio->md;
812 struct request *rq = tio->orig;
814 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
815 rq->errors = clone->errors;
816 rq->resid_len = clone->resid_len;
820 * We are using the sense buffer of the original
822 * So setting the length of the sense data is enough.
824 rq->sense_len = clone->sense_len;
827 free_rq_clone(clone);
829 if (unlikely(is_barrier)) {
831 store_barrier_error(md, error);
834 blk_end_request_all(rq, error);
836 rq_completed(md, rw, run_queue);
839 static void dm_unprep_request(struct request *rq)
841 struct request *clone = rq->special;
844 rq->cmd_flags &= ~REQ_DONTPREP;
846 free_rq_clone(clone);
850 * Requeue the original request of a clone.
852 void dm_requeue_unmapped_request(struct request *clone)
854 int rw = rq_data_dir(clone);
855 struct dm_rq_target_io *tio = clone->end_io_data;
856 struct mapped_device *md = tio->md;
857 struct request *rq = tio->orig;
858 struct request_queue *q = rq->q;
861 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
863 * Barrier clones share an original request.
864 * Leave it to dm_end_request(), which handles this special
867 dm_end_request(clone, DM_ENDIO_REQUEUE);
871 dm_unprep_request(rq);
873 spin_lock_irqsave(q->queue_lock, flags);
874 if (elv_queue_empty(q))
876 blk_requeue_request(q, rq);
877 spin_unlock_irqrestore(q->queue_lock, flags);
879 rq_completed(md, rw, 0);
881 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
883 static void __stop_queue(struct request_queue *q)
888 static void stop_queue(struct request_queue *q)
892 spin_lock_irqsave(q->queue_lock, flags);
894 spin_unlock_irqrestore(q->queue_lock, flags);
897 static void __start_queue(struct request_queue *q)
899 if (blk_queue_stopped(q))
903 static void start_queue(struct request_queue *q)
907 spin_lock_irqsave(q->queue_lock, flags);
909 spin_unlock_irqrestore(q->queue_lock, flags);
912 static void dm_done(struct request *clone, int error, bool mapped)
915 struct dm_rq_target_io *tio = clone->end_io_data;
916 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
918 if (mapped && rq_end_io)
919 r = rq_end_io(tio->ti, clone, error, &tio->info);
922 /* The target wants to complete the I/O */
923 dm_end_request(clone, r);
924 else if (r == DM_ENDIO_INCOMPLETE)
925 /* The target will handle the I/O */
927 else if (r == DM_ENDIO_REQUEUE)
928 /* The target wants to requeue the I/O */
929 dm_requeue_unmapped_request(clone);
931 DMWARN("unimplemented target endio return value: %d", r);
937 * Request completion handler for request-based dm
939 static void dm_softirq_done(struct request *rq)
942 struct request *clone = rq->completion_data;
943 struct dm_rq_target_io *tio = clone->end_io_data;
945 if (rq->cmd_flags & REQ_FAILED)
948 dm_done(clone, tio->error, mapped);
952 * Complete the clone and the original request with the error status
953 * through softirq context.
955 static void dm_complete_request(struct request *clone, int error)
957 struct dm_rq_target_io *tio = clone->end_io_data;
958 struct request *rq = tio->orig;
960 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
962 * Barrier clones share an original request. So can't use
963 * softirq_done with the original.
964 * Pass the clone to dm_done() directly in this special case.
965 * It is safe (even if clone->q->queue_lock is held here)
966 * because there is no I/O dispatching during the completion
969 dm_done(clone, error, true);
974 rq->completion_data = clone;
975 blk_complete_request(rq);
979 * Complete the not-mapped clone and the original request with the error status
980 * through softirq context.
981 * Target's rq_end_io() function isn't called.
982 * This may be used when the target's map_rq() function fails.
984 void dm_kill_unmapped_request(struct request *clone, int error)
986 struct dm_rq_target_io *tio = clone->end_io_data;
987 struct request *rq = tio->orig;
989 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
991 * Barrier clones share an original request.
992 * Leave it to dm_end_request(), which handles this special
996 dm_end_request(clone, error);
1000 rq->cmd_flags |= REQ_FAILED;
1001 dm_complete_request(clone, error);
1003 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1006 * Called with the queue lock held
1008 static void end_clone_request(struct request *clone, int error)
1011 * For just cleaning up the information of the queue in which
1012 * the clone was dispatched.
1013 * The clone is *NOT* freed actually here because it is alloced from
1014 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1016 __blk_put_request(clone->q, clone);
1019 * Actual request completion is done in a softirq context which doesn't
1020 * hold the queue lock. Otherwise, deadlock could occur because:
1021 * - another request may be submitted by the upper level driver
1022 * of the stacking during the completion
1023 * - the submission which requires queue lock may be done
1024 * against this queue
1026 dm_complete_request(clone, error);
1029 static sector_t max_io_len(struct mapped_device *md,
1030 sector_t sector, struct dm_target *ti)
1032 sector_t offset = sector - ti->begin;
1033 sector_t len = ti->len - offset;
1036 * Does the target need to split even further ?
1040 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
1049 static void __map_bio(struct dm_target *ti, struct bio *clone,
1050 struct dm_target_io *tio)
1054 struct mapped_device *md;
1056 clone->bi_end_io = clone_endio;
1057 clone->bi_private = tio;
1060 * Map the clone. If r == 0 we don't need to do
1061 * anything, the target has assumed ownership of
1064 atomic_inc(&tio->io->io_count);
1065 sector = clone->bi_sector;
1066 r = ti->type->map(ti, clone, &tio->info);
1067 if (r == DM_MAPIO_REMAPPED) {
1068 /* the bio has been remapped so dispatch it */
1070 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
1071 tio->io->bio->bi_bdev->bd_dev, sector);
1073 generic_make_request(clone);
1074 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1075 /* error the io and bail out, or requeue it if needed */
1077 dec_pending(tio->io, r);
1079 * Store bio_set for cleanup.
1081 clone->bi_private = md->bs;
1085 DMWARN("unimplemented target map return value: %d", r);
1091 struct mapped_device *md;
1092 struct dm_table *map;
1096 sector_t sector_count;
1100 static void dm_bio_destructor(struct bio *bio)
1102 struct bio_set *bs = bio->bi_private;
1108 * Creates a little bio that is just does part of a bvec.
1110 static struct bio *split_bvec(struct bio *bio, sector_t sector,
1111 unsigned short idx, unsigned int offset,
1112 unsigned int len, struct bio_set *bs)
1115 struct bio_vec *bv = bio->bi_io_vec + idx;
1117 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
1118 clone->bi_destructor = dm_bio_destructor;
1119 *clone->bi_io_vec = *bv;
1121 clone->bi_sector = sector;
1122 clone->bi_bdev = bio->bi_bdev;
1123 clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER;
1125 clone->bi_size = to_bytes(len);
1126 clone->bi_io_vec->bv_offset = offset;
1127 clone->bi_io_vec->bv_len = clone->bi_size;
1128 clone->bi_flags |= 1 << BIO_CLONED;
1130 if (bio_integrity(bio)) {
1131 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1132 bio_integrity_trim(clone,
1133 bio_sector_offset(bio, idx, offset), len);
1140 * Creates a bio that consists of range of complete bvecs.
1142 static struct bio *clone_bio(struct bio *bio, sector_t sector,
1143 unsigned short idx, unsigned short bv_count,
1144 unsigned int len, struct bio_set *bs)
1148 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1149 __bio_clone(clone, bio);
1150 clone->bi_rw &= ~REQ_HARDBARRIER;
1151 clone->bi_destructor = dm_bio_destructor;
1152 clone->bi_sector = sector;
1153 clone->bi_idx = idx;
1154 clone->bi_vcnt = idx + bv_count;
1155 clone->bi_size = to_bytes(len);
1156 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1158 if (bio_integrity(bio)) {
1159 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1161 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1162 bio_integrity_trim(clone,
1163 bio_sector_offset(bio, idx, 0), len);
1169 static struct dm_target_io *alloc_tio(struct clone_info *ci,
1170 struct dm_target *ti)
1172 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1176 memset(&tio->info, 0, sizeof(tio->info));
1181 static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1184 struct dm_target_io *tio = alloc_tio(ci, ti);
1187 tio->info.flush_request = flush_nr;
1189 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1190 __bio_clone(clone, ci->bio);
1191 clone->bi_destructor = dm_bio_destructor;
1193 __map_bio(ti, clone, tio);
1196 static int __clone_and_map_empty_barrier(struct clone_info *ci)
1198 unsigned target_nr = 0, flush_nr;
1199 struct dm_target *ti;
1201 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1202 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1204 __flush_target(ci, ti, flush_nr);
1206 ci->sector_count = 0;
1211 static int __clone_and_map(struct clone_info *ci)
1213 struct bio *clone, *bio = ci->bio;
1214 struct dm_target *ti;
1215 sector_t len = 0, max;
1216 struct dm_target_io *tio;
1218 if (unlikely(bio_empty_barrier(bio)))
1219 return __clone_and_map_empty_barrier(ci);
1221 ti = dm_table_find_target(ci->map, ci->sector);
1222 if (!dm_target_is_valid(ti))
1225 max = max_io_len(ci->md, ci->sector, ti);
1228 * Allocate a target io object.
1230 tio = alloc_tio(ci, ti);
1232 if (ci->sector_count <= max) {
1234 * Optimise for the simple case where we can do all of
1235 * the remaining io with a single clone.
1237 clone = clone_bio(bio, ci->sector, ci->idx,
1238 bio->bi_vcnt - ci->idx, ci->sector_count,
1240 __map_bio(ti, clone, tio);
1241 ci->sector_count = 0;
1243 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1245 * There are some bvecs that don't span targets.
1246 * Do as many of these as possible.
1249 sector_t remaining = max;
1252 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1253 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1255 if (bv_len > remaining)
1258 remaining -= bv_len;
1262 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1264 __map_bio(ti, clone, tio);
1267 ci->sector_count -= len;
1272 * Handle a bvec that must be split between two or more targets.
1274 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1275 sector_t remaining = to_sector(bv->bv_len);
1276 unsigned int offset = 0;
1280 ti = dm_table_find_target(ci->map, ci->sector);
1281 if (!dm_target_is_valid(ti))
1284 max = max_io_len(ci->md, ci->sector, ti);
1286 tio = alloc_tio(ci, ti);
1289 len = min(remaining, max);
1291 clone = split_bvec(bio, ci->sector, ci->idx,
1292 bv->bv_offset + offset, len,
1295 __map_bio(ti, clone, tio);
1298 ci->sector_count -= len;
1299 offset += to_bytes(len);
1300 } while (remaining -= len);
1309 * Split the bio into several clones and submit it to targets.
1311 static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
1313 struct clone_info ci;
1316 ci.map = dm_get_live_table(md);
1317 if (unlikely(!ci.map)) {
1318 if (!(bio->bi_rw & REQ_HARDBARRIER))
1321 if (!md->barrier_error)
1322 md->barrier_error = -EIO;
1328 ci.io = alloc_io(md);
1330 atomic_set(&ci.io->io_count, 1);
1333 spin_lock_init(&ci.io->endio_lock);
1334 ci.sector = bio->bi_sector;
1335 ci.sector_count = bio_sectors(bio);
1336 if (unlikely(bio_empty_barrier(bio)))
1337 ci.sector_count = 1;
1338 ci.idx = bio->bi_idx;
1340 start_io_acct(ci.io);
1341 while (ci.sector_count && !error)
1342 error = __clone_and_map(&ci);
1344 /* drop the extra reference count */
1345 dec_pending(ci.io, error);
1346 dm_table_put(ci.map);
1348 /*-----------------------------------------------------------------
1350 *---------------------------------------------------------------*/
1352 static int dm_merge_bvec(struct request_queue *q,
1353 struct bvec_merge_data *bvm,
1354 struct bio_vec *biovec)
1356 struct mapped_device *md = q->queuedata;
1357 struct dm_table *map = dm_get_live_table(md);
1358 struct dm_target *ti;
1359 sector_t max_sectors;
1365 ti = dm_table_find_target(map, bvm->bi_sector);
1366 if (!dm_target_is_valid(ti))
1370 * Find maximum amount of I/O that won't need splitting
1372 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1373 (sector_t) BIO_MAX_SECTORS);
1374 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1379 * merge_bvec_fn() returns number of bytes
1380 * it can accept at this offset
1381 * max is precomputed maximal io size
1383 if (max_size && ti->type->merge)
1384 max_size = ti->type->merge(ti, bvm, biovec, max_size);
1386 * If the target doesn't support merge method and some of the devices
1387 * provided their merge_bvec method (we know this by looking at
1388 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1389 * entries. So always set max_size to 0, and the code below allows
1392 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1401 * Always allow an entire first page
1403 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1404 max_size = biovec->bv_len;
1410 * The request function that just remaps the bio built up by
1413 static int _dm_request(struct request_queue *q, struct bio *bio)
1415 int rw = bio_data_dir(bio);
1416 struct mapped_device *md = q->queuedata;
1419 down_read(&md->io_lock);
1421 cpu = part_stat_lock();
1422 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1423 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1427 * If we're suspended or the thread is processing barriers
1428 * we have to queue this io for later.
1430 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1431 unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
1432 up_read(&md->io_lock);
1434 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1435 bio_rw(bio) == READA) {
1445 __split_and_process_bio(md, bio);
1446 up_read(&md->io_lock);
1450 static int dm_make_request(struct request_queue *q, struct bio *bio)
1452 struct mapped_device *md = q->queuedata;
1454 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1457 static int dm_request_based(struct mapped_device *md)
1459 return blk_queue_stackable(md->queue);
1462 static int dm_request(struct request_queue *q, struct bio *bio)
1464 struct mapped_device *md = q->queuedata;
1466 if (dm_request_based(md))
1467 return dm_make_request(q, bio);
1469 return _dm_request(q, bio);
1472 static bool dm_rq_is_flush_request(struct request *rq)
1474 if (rq->cmd_flags & REQ_FLUSH)
1480 void dm_dispatch_request(struct request *rq)
1484 if (blk_queue_io_stat(rq->q))
1485 rq->cmd_flags |= REQ_IO_STAT;
1487 rq->start_time = jiffies;
1488 r = blk_insert_cloned_request(rq->q, rq);
1490 dm_complete_request(rq, r);
1492 EXPORT_SYMBOL_GPL(dm_dispatch_request);
1494 static void dm_rq_bio_destructor(struct bio *bio)
1496 struct dm_rq_clone_bio_info *info = bio->bi_private;
1497 struct mapped_device *md = info->tio->md;
1499 free_bio_info(info);
1500 bio_free(bio, md->bs);
1503 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1506 struct dm_rq_target_io *tio = data;
1507 struct mapped_device *md = tio->md;
1508 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1513 info->orig = bio_orig;
1515 bio->bi_end_io = end_clone_bio;
1516 bio->bi_private = info;
1517 bio->bi_destructor = dm_rq_bio_destructor;
1522 static int setup_clone(struct request *clone, struct request *rq,
1523 struct dm_rq_target_io *tio)
1527 if (dm_rq_is_flush_request(rq)) {
1528 blk_rq_init(NULL, clone);
1529 clone->cmd_type = REQ_TYPE_FS;
1530 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1532 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1533 dm_rq_bio_constructor, tio);
1537 clone->cmd = rq->cmd;
1538 clone->cmd_len = rq->cmd_len;
1539 clone->sense = rq->sense;
1540 clone->buffer = rq->buffer;
1543 clone->end_io = end_clone_request;
1544 clone->end_io_data = tio;
1549 static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1552 struct request *clone;
1553 struct dm_rq_target_io *tio;
1555 tio = alloc_rq_tio(md, gfp_mask);
1563 memset(&tio->info, 0, sizeof(tio->info));
1565 clone = &tio->clone;
1566 if (setup_clone(clone, rq, tio)) {
1576 * Called with the queue lock held.
1578 static int dm_prep_fn(struct request_queue *q, struct request *rq)
1580 struct mapped_device *md = q->queuedata;
1581 struct request *clone;
1583 if (unlikely(dm_rq_is_flush_request(rq)))
1586 if (unlikely(rq->special)) {
1587 DMWARN("Already has something in rq->special.");
1588 return BLKPREP_KILL;
1591 clone = clone_rq(rq, md, GFP_ATOMIC);
1593 return BLKPREP_DEFER;
1595 rq->special = clone;
1596 rq->cmd_flags |= REQ_DONTPREP;
1603 * 0 : the request has been processed (not requeued)
1604 * !0 : the request has been requeued
1606 static int map_request(struct dm_target *ti, struct request *clone,
1607 struct mapped_device *md)
1609 int r, requeued = 0;
1610 struct dm_rq_target_io *tio = clone->end_io_data;
1613 * Hold the md reference here for the in-flight I/O.
1614 * We can't rely on the reference count by device opener,
1615 * because the device may be closed during the request completion
1616 * when all bios are completed.
1617 * See the comment in rq_completed() too.
1622 r = ti->type->map_rq(ti, clone, &tio->info);
1624 case DM_MAPIO_SUBMITTED:
1625 /* The target has taken the I/O to submit by itself later */
1627 case DM_MAPIO_REMAPPED:
1628 /* The target has remapped the I/O so dispatch it */
1629 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1630 blk_rq_pos(tio->orig));
1631 dm_dispatch_request(clone);
1633 case DM_MAPIO_REQUEUE:
1634 /* The target wants to requeue the I/O */
1635 dm_requeue_unmapped_request(clone);
1640 DMWARN("unimplemented target map return value: %d", r);
1644 /* The target wants to complete the I/O */
1645 dm_kill_unmapped_request(clone, r);
1653 * q->request_fn for request-based dm.
1654 * Called with the queue lock held.
1656 static void dm_request_fn(struct request_queue *q)
1658 struct mapped_device *md = q->queuedata;
1659 struct dm_table *map = dm_get_live_table(md);
1660 struct dm_target *ti;
1661 struct request *rq, *clone;
1664 * For suspend, check blk_queue_stopped() and increment
1665 * ->pending within a single queue_lock not to increment the
1666 * number of in-flight I/Os after the queue is stopped in
1669 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1670 rq = blk_peek_request(q);
1674 if (unlikely(dm_rq_is_flush_request(rq))) {
1675 BUG_ON(md->flush_request);
1676 md->flush_request = rq;
1677 blk_start_request(rq);
1678 queue_work(md->wq, &md->barrier_work);
1682 ti = dm_table_find_target(map, blk_rq_pos(rq));
1683 if (ti->type->busy && ti->type->busy(ti))
1686 blk_start_request(rq);
1687 clone = rq->special;
1688 atomic_inc(&md->pending[rq_data_dir(clone)]);
1690 spin_unlock(q->queue_lock);
1691 if (map_request(ti, clone, md))
1694 spin_lock_irq(q->queue_lock);
1700 spin_lock_irq(q->queue_lock);
1703 if (!elv_queue_empty(q))
1704 /* Some requests still remain, retry later */
1713 int dm_underlying_device_busy(struct request_queue *q)
1715 return blk_lld_busy(q);
1717 EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1719 static int dm_lld_busy(struct request_queue *q)
1722 struct mapped_device *md = q->queuedata;
1723 struct dm_table *map = dm_get_live_table(md);
1725 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1728 r = dm_table_any_busy_target(map);
1735 static void dm_unplug_all(struct request_queue *q)
1737 struct mapped_device *md = q->queuedata;
1738 struct dm_table *map = dm_get_live_table(md);
1741 if (dm_request_based(md))
1742 generic_unplug_device(q);
1744 dm_table_unplug_all(map);
1749 static int dm_any_congested(void *congested_data, int bdi_bits)
1752 struct mapped_device *md = congested_data;
1753 struct dm_table *map;
1755 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1756 map = dm_get_live_table(md);
1759 * Request-based dm cares about only own queue for
1760 * the query about congestion status of request_queue
1762 if (dm_request_based(md))
1763 r = md->queue->backing_dev_info.state &
1766 r = dm_table_any_congested(map, bdi_bits);
1775 /*-----------------------------------------------------------------
1776 * An IDR is used to keep track of allocated minor numbers.
1777 *---------------------------------------------------------------*/
1778 static DEFINE_IDR(_minor_idr);
1780 static void free_minor(int minor)
1782 spin_lock(&_minor_lock);
1783 idr_remove(&_minor_idr, minor);
1784 spin_unlock(&_minor_lock);
1788 * See if the device with a specific minor # is free.
1790 static int specific_minor(int minor)
1794 if (minor >= (1 << MINORBITS))
1797 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1801 spin_lock(&_minor_lock);
1803 if (idr_find(&_minor_idr, minor)) {
1808 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
1813 idr_remove(&_minor_idr, m);
1819 spin_unlock(&_minor_lock);
1823 static int next_free_minor(int *minor)
1827 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1831 spin_lock(&_minor_lock);
1833 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1837 if (m >= (1 << MINORBITS)) {
1838 idr_remove(&_minor_idr, m);
1846 spin_unlock(&_minor_lock);
1850 static const struct block_device_operations dm_blk_dops;
1852 static void dm_wq_work(struct work_struct *work);
1853 static void dm_rq_barrier_work(struct work_struct *work);
1856 * Allocate and initialise a blank device with a given minor.
1858 static struct mapped_device *alloc_dev(int minor)
1861 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1865 DMWARN("unable to allocate device, out of memory.");
1869 if (!try_module_get(THIS_MODULE))
1870 goto bad_module_get;
1872 /* get a minor number for the dev */
1873 if (minor == DM_ANY_MINOR)
1874 r = next_free_minor(&minor);
1876 r = specific_minor(minor);
1880 init_rwsem(&md->io_lock);
1881 mutex_init(&md->suspend_lock);
1882 spin_lock_init(&md->deferred_lock);
1883 spin_lock_init(&md->barrier_error_lock);
1884 rwlock_init(&md->map_lock);
1885 atomic_set(&md->holders, 1);
1886 atomic_set(&md->open_count, 0);
1887 atomic_set(&md->event_nr, 0);
1888 atomic_set(&md->uevent_seq, 0);
1889 INIT_LIST_HEAD(&md->uevent_list);
1890 spin_lock_init(&md->uevent_lock);
1892 md->queue = blk_init_queue(dm_request_fn, NULL);
1897 * Request-based dm devices cannot be stacked on top of bio-based dm
1898 * devices. The type of this dm device has not been decided yet,
1899 * although we initialized the queue using blk_init_queue().
1900 * The type is decided at the first table loading time.
1901 * To prevent problematic device stacking, clear the queue flag
1902 * for request stacking support until then.
1904 * This queue is new, so no concurrency on the queue_flags.
1906 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1907 md->saved_make_request_fn = md->queue->make_request_fn;
1908 md->queue->queuedata = md;
1909 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1910 md->queue->backing_dev_info.congested_data = md;
1911 blk_queue_make_request(md->queue, dm_request);
1912 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1913 md->queue->unplug_fn = dm_unplug_all;
1914 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1915 blk_queue_softirq_done(md->queue, dm_softirq_done);
1916 blk_queue_prep_rq(md->queue, dm_prep_fn);
1917 blk_queue_lld_busy(md->queue, dm_lld_busy);
1918 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH);
1920 md->disk = alloc_disk(1);
1924 atomic_set(&md->pending[0], 0);
1925 atomic_set(&md->pending[1], 0);
1926 init_waitqueue_head(&md->wait);
1927 INIT_WORK(&md->work, dm_wq_work);
1928 INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
1929 init_waitqueue_head(&md->eventq);
1931 md->disk->major = _major;
1932 md->disk->first_minor = minor;
1933 md->disk->fops = &dm_blk_dops;
1934 md->disk->queue = md->queue;
1935 md->disk->private_data = md;
1936 sprintf(md->disk->disk_name, "dm-%d", minor);
1938 format_dev_t(md->name, MKDEV(_major, minor));
1940 md->wq = create_singlethread_workqueue("kdmflush");
1944 md->bdev = bdget_disk(md->disk, 0);
1948 /* Populate the mapping, nobody knows we exist yet */
1949 spin_lock(&_minor_lock);
1950 old_md = idr_replace(&_minor_idr, md, minor);
1951 spin_unlock(&_minor_lock);
1953 BUG_ON(old_md != MINOR_ALLOCED);
1958 destroy_workqueue(md->wq);
1960 del_gendisk(md->disk);
1963 blk_cleanup_queue(md->queue);
1967 module_put(THIS_MODULE);
1973 static void unlock_fs(struct mapped_device *md);
1975 static void free_dev(struct mapped_device *md)
1977 int minor = MINOR(disk_devt(md->disk));
1981 destroy_workqueue(md->wq);
1983 mempool_destroy(md->tio_pool);
1985 mempool_destroy(md->io_pool);
1987 bioset_free(md->bs);
1988 blk_integrity_unregister(md->disk);
1989 del_gendisk(md->disk);
1992 spin_lock(&_minor_lock);
1993 md->disk->private_data = NULL;
1994 spin_unlock(&_minor_lock);
1997 blk_cleanup_queue(md->queue);
1998 module_put(THIS_MODULE);
2002 static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2004 struct dm_md_mempools *p;
2006 if (md->io_pool && md->tio_pool && md->bs)
2007 /* the md already has necessary mempools */
2010 p = dm_table_get_md_mempools(t);
2011 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
2013 md->io_pool = p->io_pool;
2015 md->tio_pool = p->tio_pool;
2021 /* mempool bind completed, now no need any mempools in the table */
2022 dm_table_free_md_mempools(t);
2026 * Bind a table to the device.
2028 static void event_callback(void *context)
2030 unsigned long flags;
2032 struct mapped_device *md = (struct mapped_device *) context;
2034 spin_lock_irqsave(&md->uevent_lock, flags);
2035 list_splice_init(&md->uevent_list, &uevents);
2036 spin_unlock_irqrestore(&md->uevent_lock, flags);
2038 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
2040 atomic_inc(&md->event_nr);
2041 wake_up(&md->eventq);
2044 static void __set_size(struct mapped_device *md, sector_t size)
2046 set_capacity(md->disk, size);
2048 mutex_lock(&md->bdev->bd_inode->i_mutex);
2049 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2050 mutex_unlock(&md->bdev->bd_inode->i_mutex);
2054 * Returns old map, which caller must destroy.
2056 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2057 struct queue_limits *limits)
2059 struct dm_table *old_map;
2060 struct request_queue *q = md->queue;
2062 unsigned long flags;
2064 size = dm_table_get_size(t);
2067 * Wipe any geometry if the size of the table changed.
2069 if (size != get_capacity(md->disk))
2070 memset(&md->geometry, 0, sizeof(md->geometry));
2072 __set_size(md, size);
2074 dm_table_event_callback(t, event_callback, md);
2077 * The queue hasn't been stopped yet, if the old table type wasn't
2078 * for request-based during suspension. So stop it to prevent
2079 * I/O mapping before resume.
2080 * This must be done before setting the queue restrictions,
2081 * because request-based dm may be run just after the setting.
2083 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2086 __bind_mempools(md, t);
2088 write_lock_irqsave(&md->map_lock, flags);
2091 dm_table_set_restrictions(t, q, limits);
2092 write_unlock_irqrestore(&md->map_lock, flags);
2098 * Returns unbound table for the caller to free.
2100 static struct dm_table *__unbind(struct mapped_device *md)
2102 struct dm_table *map = md->map;
2103 unsigned long flags;
2108 dm_table_event_callback(map, NULL, NULL);
2109 write_lock_irqsave(&md->map_lock, flags);
2111 write_unlock_irqrestore(&md->map_lock, flags);
2117 * Constructor for a new device.
2119 int dm_create(int minor, struct mapped_device **result)
2121 struct mapped_device *md;
2123 md = alloc_dev(minor);
2133 static struct mapped_device *dm_find_md(dev_t dev)
2135 struct mapped_device *md;
2136 unsigned minor = MINOR(dev);
2138 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2141 spin_lock(&_minor_lock);
2143 md = idr_find(&_minor_idr, minor);
2144 if (md && (md == MINOR_ALLOCED ||
2145 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2146 dm_deleting_md(md) ||
2147 test_bit(DMF_FREEING, &md->flags))) {
2153 spin_unlock(&_minor_lock);
2158 struct mapped_device *dm_get_md(dev_t dev)
2160 struct mapped_device *md = dm_find_md(dev);
2168 void *dm_get_mdptr(struct mapped_device *md)
2170 return md->interface_ptr;
2173 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2175 md->interface_ptr = ptr;
2178 void dm_get(struct mapped_device *md)
2180 atomic_inc(&md->holders);
2181 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2184 const char *dm_device_name(struct mapped_device *md)
2188 EXPORT_SYMBOL_GPL(dm_device_name);
2190 static void __dm_destroy(struct mapped_device *md, bool wait)
2192 struct dm_table *map;
2196 spin_lock(&_minor_lock);
2197 map = dm_get_live_table(md);
2198 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2199 set_bit(DMF_FREEING, &md->flags);
2200 spin_unlock(&_minor_lock);
2202 if (!dm_suspended_md(md)) {
2203 dm_table_presuspend_targets(map);
2204 dm_table_postsuspend_targets(map);
2208 * Rare, but there may be I/O requests still going to complete,
2209 * for example. Wait for all references to disappear.
2210 * No one should increment the reference count of the mapped_device,
2211 * after the mapped_device state becomes DMF_FREEING.
2214 while (atomic_read(&md->holders))
2216 else if (atomic_read(&md->holders))
2217 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2218 dm_device_name(md), atomic_read(&md->holders));
2222 dm_table_destroy(__unbind(md));
2226 void dm_destroy(struct mapped_device *md)
2228 __dm_destroy(md, true);
2231 void dm_destroy_immediate(struct mapped_device *md)
2233 __dm_destroy(md, false);
2236 void dm_put(struct mapped_device *md)
2238 atomic_dec(&md->holders);
2240 EXPORT_SYMBOL_GPL(dm_put);
2242 static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
2245 DECLARE_WAITQUEUE(wait, current);
2247 dm_unplug_all(md->queue);
2249 add_wait_queue(&md->wait, &wait);
2252 set_current_state(interruptible);
2255 if (!md_in_flight(md))
2258 if (interruptible == TASK_INTERRUPTIBLE &&
2259 signal_pending(current)) {
2266 set_current_state(TASK_RUNNING);
2268 remove_wait_queue(&md->wait, &wait);
2273 static void dm_flush(struct mapped_device *md)
2275 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2277 bio_init(&md->barrier_bio);
2278 md->barrier_bio.bi_bdev = md->bdev;
2279 md->barrier_bio.bi_rw = WRITE_BARRIER;
2280 __split_and_process_bio(md, &md->barrier_bio);
2282 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2285 static void process_barrier(struct mapped_device *md, struct bio *bio)
2287 md->barrier_error = 0;
2291 if (!bio_empty_barrier(bio)) {
2292 __split_and_process_bio(md, bio);
2294 * If the request isn't supported, don't waste time with
2297 if (md->barrier_error != -EOPNOTSUPP)
2301 if (md->barrier_error != DM_ENDIO_REQUEUE)
2302 bio_endio(bio, md->barrier_error);
2304 spin_lock_irq(&md->deferred_lock);
2305 bio_list_add_head(&md->deferred, bio);
2306 spin_unlock_irq(&md->deferred_lock);
2311 * Process the deferred bios
2313 static void dm_wq_work(struct work_struct *work)
2315 struct mapped_device *md = container_of(work, struct mapped_device,
2319 down_write(&md->io_lock);
2321 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2322 spin_lock_irq(&md->deferred_lock);
2323 c = bio_list_pop(&md->deferred);
2324 spin_unlock_irq(&md->deferred_lock);
2327 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2331 up_write(&md->io_lock);
2333 if (dm_request_based(md))
2334 generic_make_request(c);
2336 if (c->bi_rw & REQ_HARDBARRIER)
2337 process_barrier(md, c);
2339 __split_and_process_bio(md, c);
2342 down_write(&md->io_lock);
2345 up_write(&md->io_lock);
2348 static void dm_queue_flush(struct mapped_device *md)
2350 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2351 smp_mb__after_clear_bit();
2352 queue_work(md->wq, &md->work);
2355 static void dm_rq_set_flush_nr(struct request *clone, unsigned flush_nr)
2357 struct dm_rq_target_io *tio = clone->end_io_data;
2359 tio->info.flush_request = flush_nr;
2362 /* Issue barrier requests to targets and wait for their completion. */
2363 static int dm_rq_barrier(struct mapped_device *md)
2366 struct dm_table *map = dm_get_live_table(md);
2367 unsigned num_targets = dm_table_get_num_targets(map);
2368 struct dm_target *ti;
2369 struct request *clone;
2371 md->barrier_error = 0;
2373 for (i = 0; i < num_targets; i++) {
2374 ti = dm_table_get_target(map, i);
2375 for (j = 0; j < ti->num_flush_requests; j++) {
2376 clone = clone_rq(md->flush_request, md, GFP_NOIO);
2377 dm_rq_set_flush_nr(clone, j);
2378 atomic_inc(&md->pending[rq_data_dir(clone)]);
2379 map_request(ti, clone, md);
2383 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2386 return md->barrier_error;
2389 static void dm_rq_barrier_work(struct work_struct *work)
2392 struct mapped_device *md = container_of(work, struct mapped_device,
2394 struct request_queue *q = md->queue;
2396 unsigned long flags;
2399 * Hold the md reference here and leave it at the last part so that
2400 * the md can't be deleted by device opener when the barrier request
2405 error = dm_rq_barrier(md);
2407 rq = md->flush_request;
2408 md->flush_request = NULL;
2410 if (error == DM_ENDIO_REQUEUE) {
2411 spin_lock_irqsave(q->queue_lock, flags);
2412 blk_requeue_request(q, rq);
2413 spin_unlock_irqrestore(q->queue_lock, flags);
2415 blk_end_request_all(rq, error);
2423 * Swap in a new table, returning the old one for the caller to destroy.
2425 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
2427 struct dm_table *map = ERR_PTR(-EINVAL);
2428 struct queue_limits limits;
2431 mutex_lock(&md->suspend_lock);
2433 /* device must be suspended */
2434 if (!dm_suspended_md(md))
2437 r = dm_calculate_queue_limits(table, &limits);
2443 /* cannot change the device type, once a table is bound */
2445 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2446 DMWARN("can't change the device type after a table is bound");
2450 map = __bind(md, table, &limits);
2453 mutex_unlock(&md->suspend_lock);
2458 * Functions to lock and unlock any filesystem running on the
2461 static int lock_fs(struct mapped_device *md)
2465 WARN_ON(md->frozen_sb);
2467 md->frozen_sb = freeze_bdev(md->bdev);
2468 if (IS_ERR(md->frozen_sb)) {
2469 r = PTR_ERR(md->frozen_sb);
2470 md->frozen_sb = NULL;
2474 set_bit(DMF_FROZEN, &md->flags);
2479 static void unlock_fs(struct mapped_device *md)
2481 if (!test_bit(DMF_FROZEN, &md->flags))
2484 thaw_bdev(md->bdev, md->frozen_sb);
2485 md->frozen_sb = NULL;
2486 clear_bit(DMF_FROZEN, &md->flags);
2490 * We need to be able to change a mapping table under a mounted
2491 * filesystem. For example we might want to move some data in
2492 * the background. Before the table can be swapped with
2493 * dm_bind_table, dm_suspend must be called to flush any in
2494 * flight bios and ensure that any further io gets deferred.
2497 * Suspend mechanism in request-based dm.
2499 * 1. Flush all I/Os by lock_fs() if needed.
2500 * 2. Stop dispatching any I/O by stopping the request_queue.
2501 * 3. Wait for all in-flight I/Os to be completed or requeued.
2503 * To abort suspend, start the request_queue.
2505 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2507 struct dm_table *map = NULL;
2509 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2510 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
2512 mutex_lock(&md->suspend_lock);
2514 if (dm_suspended_md(md)) {
2519 map = dm_get_live_table(md);
2522 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2523 * This flag is cleared before dm_suspend returns.
2526 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2528 /* This does not get reverted if there's an error later. */
2529 dm_table_presuspend_targets(map);
2532 * Flush I/O to the device.
2533 * Any I/O submitted after lock_fs() may not be flushed.
2534 * noflush takes precedence over do_lockfs.
2535 * (lock_fs() flushes I/Os and waits for them to complete.)
2537 if (!noflush && do_lockfs) {
2544 * Here we must make sure that no processes are submitting requests
2545 * to target drivers i.e. no one may be executing
2546 * __split_and_process_bio. This is called from dm_request and
2549 * To get all processes out of __split_and_process_bio in dm_request,
2550 * we take the write lock. To prevent any process from reentering
2551 * __split_and_process_bio from dm_request, we set
2552 * DMF_QUEUE_IO_TO_THREAD.
2554 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2555 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2556 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2557 * further calls to __split_and_process_bio from dm_wq_work.
2559 down_write(&md->io_lock);
2560 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2561 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2562 up_write(&md->io_lock);
2565 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2566 * can be kicked until md->queue is stopped. So stop md->queue before
2569 if (dm_request_based(md))
2570 stop_queue(md->queue);
2572 flush_workqueue(md->wq);
2575 * At this point no more requests are entering target request routines.
2576 * We call dm_wait_for_completion to wait for all existing requests
2579 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
2581 down_write(&md->io_lock);
2583 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2584 up_write(&md->io_lock);
2586 /* were we interrupted ? */
2590 if (dm_request_based(md))
2591 start_queue(md->queue);
2594 goto out; /* pushback list is already flushed, so skip flush */
2598 * If dm_wait_for_completion returned 0, the device is completely
2599 * quiescent now. There is no request-processing activity. All new
2600 * requests are being added to md->deferred list.
2603 set_bit(DMF_SUSPENDED, &md->flags);
2605 dm_table_postsuspend_targets(map);
2611 mutex_unlock(&md->suspend_lock);
2615 int dm_resume(struct mapped_device *md)
2618 struct dm_table *map = NULL;
2620 mutex_lock(&md->suspend_lock);
2621 if (!dm_suspended_md(md))
2624 map = dm_get_live_table(md);
2625 if (!map || !dm_table_get_size(map))
2628 r = dm_table_resume_targets(map);
2635 * Flushing deferred I/Os must be done after targets are resumed
2636 * so that mapping of targets can work correctly.
2637 * Request-based dm is queueing the deferred I/Os in its request_queue.
2639 if (dm_request_based(md))
2640 start_queue(md->queue);
2644 clear_bit(DMF_SUSPENDED, &md->flags);
2646 dm_table_unplug_all(map);
2650 mutex_unlock(&md->suspend_lock);
2655 /*-----------------------------------------------------------------
2656 * Event notification.
2657 *---------------------------------------------------------------*/
2658 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2661 char udev_cookie[DM_COOKIE_LENGTH];
2662 char *envp[] = { udev_cookie, NULL };
2665 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2667 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2668 DM_COOKIE_ENV_VAR_NAME, cookie);
2669 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2674 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2676 return atomic_add_return(1, &md->uevent_seq);
2679 uint32_t dm_get_event_nr(struct mapped_device *md)
2681 return atomic_read(&md->event_nr);
2684 int dm_wait_event(struct mapped_device *md, int event_nr)
2686 return wait_event_interruptible(md->eventq,
2687 (event_nr != atomic_read(&md->event_nr)));
2690 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2692 unsigned long flags;
2694 spin_lock_irqsave(&md->uevent_lock, flags);
2695 list_add(elist, &md->uevent_list);
2696 spin_unlock_irqrestore(&md->uevent_lock, flags);
2700 * The gendisk is only valid as long as you have a reference
2703 struct gendisk *dm_disk(struct mapped_device *md)
2708 struct kobject *dm_kobject(struct mapped_device *md)
2714 * struct mapped_device should not be exported outside of dm.c
2715 * so use this check to verify that kobj is part of md structure
2717 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2719 struct mapped_device *md;
2721 md = container_of(kobj, struct mapped_device, kobj);
2722 if (&md->kobj != kobj)
2725 if (test_bit(DMF_FREEING, &md->flags) ||
2733 int dm_suspended_md(struct mapped_device *md)
2735 return test_bit(DMF_SUSPENDED, &md->flags);
2738 int dm_suspended(struct dm_target *ti)
2740 return dm_suspended_md(dm_table_get_md(ti->table));
2742 EXPORT_SYMBOL_GPL(dm_suspended);
2744 int dm_noflush_suspending(struct dm_target *ti)
2746 return __noflush_suspending(dm_table_get_md(ti->table));
2748 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2750 struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2752 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2757 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2758 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2759 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2760 if (!pools->io_pool)
2761 goto free_pools_and_out;
2763 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2764 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2765 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2766 if (!pools->tio_pool)
2767 goto free_io_pool_and_out;
2769 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2770 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2772 goto free_tio_pool_and_out;
2776 free_tio_pool_and_out:
2777 mempool_destroy(pools->tio_pool);
2779 free_io_pool_and_out:
2780 mempool_destroy(pools->io_pool);
2788 void dm_free_md_mempools(struct dm_md_mempools *pools)
2794 mempool_destroy(pools->io_pool);
2796 if (pools->tio_pool)
2797 mempool_destroy(pools->tio_pool);
2800 bioset_free(pools->bs);
2805 static const struct block_device_operations dm_blk_dops = {
2806 .open = dm_blk_open,
2807 .release = dm_blk_close,
2808 .ioctl = dm_blk_ioctl,
2809 .getgeo = dm_blk_getgeo,
2810 .owner = THIS_MODULE
2813 EXPORT_SYMBOL(dm_get_mapinfo);
2818 module_init(dm_init);
2819 module_exit(dm_exit);
2821 module_param(major, uint, 0);
2822 MODULE_PARM_DESC(major, "The major number of the device mapper");
2823 MODULE_DESCRIPTION(DM_NAME " driver");
2824 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2825 MODULE_LICENSE("GPL");