2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
9 #include "dm-bio-list.h"
10 #include "dm-uevent.h"
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/moduleparam.h>
16 #include <linux/blkpg.h>
17 #include <linux/bio.h>
18 #include <linux/buffer_head.h>
19 #include <linux/mempool.h>
20 #include <linux/slab.h>
21 #include <linux/idr.h>
22 #include <linux/hdreg.h>
23 #include <linux/blktrace_api.h>
24 #include <linux/smp_lock.h>
26 #define DM_MSG_PREFIX "core"
28 static const char *_name = DM_NAME;
30 static unsigned int major = 0;
31 static unsigned int _major = 0;
33 static DEFINE_SPINLOCK(_minor_lock);
35 * One of these is allocated per bio.
38 struct mapped_device *md;
42 unsigned long start_time;
46 * One of these is allocated per target within a bio. Hopefully
47 * this will be simplified out one day.
55 union map_info *dm_get_mapinfo(struct bio *bio)
57 if (bio && bio->bi_private)
58 return &((struct dm_target_io *)bio->bi_private)->info;
62 #define MINOR_ALLOCED ((void *)-1)
65 * Bits for the md->flags field.
67 #define DMF_BLOCK_IO 0
68 #define DMF_SUSPENDED 1
71 #define DMF_DELETING 4
72 #define DMF_NOFLUSH_SUSPENDING 5
74 struct mapped_device {
75 struct rw_semaphore io_lock;
76 struct semaphore suspend_lock;
77 spinlock_t pushback_lock;
84 struct request_queue *queue;
91 * A list of ios that arrived while we were suspended.
94 wait_queue_head_t wait;
95 struct bio_list deferred;
96 struct bio_list pushback;
99 * The current mapping.
101 struct dm_table *map;
104 * io objects are allocated from here.
115 wait_queue_head_t eventq;
118 * freeze/thaw support require holding onto a super block
120 struct super_block *frozen_sb;
121 struct block_device *suspended_bdev;
123 /* forced geometry settings */
124 struct hd_geometry geometry;
128 static struct kmem_cache *_io_cache;
129 static struct kmem_cache *_tio_cache;
131 static int __init local_init(void)
135 /* allocate a slab for the dm_ios */
136 _io_cache = KMEM_CACHE(dm_io, 0);
140 /* allocate a slab for the target ios */
141 _tio_cache = KMEM_CACHE(dm_target_io, 0);
143 kmem_cache_destroy(_io_cache);
147 r = dm_uevent_init();
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
155 r = register_blkdev(_major, _name);
157 kmem_cache_destroy(_tio_cache);
158 kmem_cache_destroy(_io_cache);
169 static void local_exit(void)
171 kmem_cache_destroy(_tio_cache);
172 kmem_cache_destroy(_io_cache);
173 unregister_blkdev(_major, _name);
178 DMINFO("cleaned up");
181 int (*_inits[])(void) __initdata = {
189 void (*_exits[])(void) = {
197 static int __init dm_init(void)
199 const int count = ARRAY_SIZE(_inits);
203 for (i = 0; i < count; i++) {
218 static void __exit dm_exit(void)
220 int i = ARRAY_SIZE(_exits);
227 * Block device functions
229 static int dm_blk_open(struct inode *inode, struct file *file)
231 struct mapped_device *md;
233 spin_lock(&_minor_lock);
235 md = inode->i_bdev->bd_disk->private_data;
239 if (test_bit(DMF_FREEING, &md->flags) ||
240 test_bit(DMF_DELETING, &md->flags)) {
246 atomic_inc(&md->open_count);
249 spin_unlock(&_minor_lock);
251 return md ? 0 : -ENXIO;
254 static int dm_blk_close(struct inode *inode, struct file *file)
256 struct mapped_device *md;
258 md = inode->i_bdev->bd_disk->private_data;
259 atomic_dec(&md->open_count);
264 int dm_open_count(struct mapped_device *md)
266 return atomic_read(&md->open_count);
270 * Guarantees nothing is using the device before it's deleted.
272 int dm_lock_for_deletion(struct mapped_device *md)
276 spin_lock(&_minor_lock);
278 if (dm_open_count(md))
281 set_bit(DMF_DELETING, &md->flags);
283 spin_unlock(&_minor_lock);
288 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
290 struct mapped_device *md = bdev->bd_disk->private_data;
292 return dm_get_geometry(md, geo);
295 static int dm_blk_ioctl(struct inode *inode, struct file *file,
296 unsigned int cmd, unsigned long arg)
298 struct mapped_device *md;
299 struct dm_table *map;
300 struct dm_target *tgt;
303 /* We don't really need this lock, but we do need 'inode'. */
306 md = inode->i_bdev->bd_disk->private_data;
308 map = dm_get_table(md);
310 if (!map || !dm_table_get_size(map))
313 /* We only support devices that have a single target */
314 if (dm_table_get_num_targets(map) != 1)
317 tgt = dm_table_get_target(map, 0);
319 if (dm_suspended(md)) {
324 if (tgt->type->ioctl)
325 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
334 static struct dm_io *alloc_io(struct mapped_device *md)
336 return mempool_alloc(md->io_pool, GFP_NOIO);
339 static void free_io(struct mapped_device *md, struct dm_io *io)
341 mempool_free(io, md->io_pool);
344 static struct dm_target_io *alloc_tio(struct mapped_device *md)
346 return mempool_alloc(md->tio_pool, GFP_NOIO);
349 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
351 mempool_free(tio, md->tio_pool);
354 static void start_io_acct(struct dm_io *io)
356 struct mapped_device *md = io->md;
358 io->start_time = jiffies;
361 disk_round_stats(dm_disk(md));
363 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
366 static int end_io_acct(struct dm_io *io)
368 struct mapped_device *md = io->md;
369 struct bio *bio = io->bio;
370 unsigned long duration = jiffies - io->start_time;
372 int rw = bio_data_dir(bio);
375 disk_round_stats(dm_disk(md));
377 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
379 disk_stat_add(dm_disk(md), ticks[rw], duration);
385 * Add the bio to the list of deferred io.
387 static int queue_io(struct mapped_device *md, struct bio *bio)
389 down_write(&md->io_lock);
391 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
392 up_write(&md->io_lock);
396 bio_list_add(&md->deferred, bio);
398 up_write(&md->io_lock);
399 return 0; /* deferred successfully */
403 * Everyone (including functions in this file), should use this
404 * function to access the md->map field, and make sure they call
405 * dm_table_put() when finished.
407 struct dm_table *dm_get_table(struct mapped_device *md)
411 read_lock(&md->map_lock);
415 read_unlock(&md->map_lock);
421 * Get the geometry associated with a dm device
423 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
431 * Set the geometry of a device.
433 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
435 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
437 if (geo->start > sz) {
438 DMWARN("Start sector is beyond the geometry limits.");
447 /*-----------------------------------------------------------------
449 * A more elegant soln is in the works that uses the queue
450 * merge fn, unfortunately there are a couple of changes to
451 * the block layer that I want to make for this. So in the
452 * interests of getting something for people to use I give
453 * you this clearly demarcated crap.
454 *---------------------------------------------------------------*/
456 static int __noflush_suspending(struct mapped_device *md)
458 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
462 * Decrements the number of outstanding ios that a bio has been
463 * cloned into, completing the original io if necc.
465 static void dec_pending(struct dm_io *io, int error)
469 /* Push-back supersedes any I/O errors */
470 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
473 if (atomic_dec_and_test(&io->io_count)) {
474 if (io->error == DM_ENDIO_REQUEUE) {
476 * Target requested pushing back the I/O.
477 * This must be handled before the sleeper on
478 * suspend queue merges the pushback list.
480 spin_lock_irqsave(&io->md->pushback_lock, flags);
481 if (__noflush_suspending(io->md))
482 bio_list_add(&io->md->pushback, io->bio);
484 /* noflush suspend was interrupted. */
486 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
490 /* nudge anyone waiting on suspend queue */
491 wake_up(&io->md->wait);
493 if (io->error != DM_ENDIO_REQUEUE) {
494 blk_add_trace_bio(io->md->queue, io->bio,
497 bio_endio(io->bio, io->error);
504 static void clone_endio(struct bio *bio, int error)
507 struct dm_target_io *tio = bio->bi_private;
508 struct mapped_device *md = tio->io->md;
509 dm_endio_fn endio = tio->ti->type->end_io;
511 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
515 r = endio(tio->ti, bio, error, &tio->info);
516 if (r < 0 || r == DM_ENDIO_REQUEUE)
518 * error and requeue request are handled
522 else if (r == DM_ENDIO_INCOMPLETE)
523 /* The target will handle the io */
526 DMWARN("unimplemented target endio return value: %d", r);
531 dec_pending(tio->io, error);
534 * Store md for cleanup instead of tio which is about to get freed.
536 bio->bi_private = md->bs;
542 static sector_t max_io_len(struct mapped_device *md,
543 sector_t sector, struct dm_target *ti)
545 sector_t offset = sector - ti->begin;
546 sector_t len = ti->len - offset;
549 * Does the target need to split even further ?
553 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
562 static void __map_bio(struct dm_target *ti, struct bio *clone,
563 struct dm_target_io *tio)
567 struct mapped_device *md;
572 BUG_ON(!clone->bi_size);
574 clone->bi_end_io = clone_endio;
575 clone->bi_private = tio;
578 * Map the clone. If r == 0 we don't need to do
579 * anything, the target has assumed ownership of
582 atomic_inc(&tio->io->io_count);
583 sector = clone->bi_sector;
584 r = ti->type->map(ti, clone, &tio->info);
585 if (r == DM_MAPIO_REMAPPED) {
586 /* the bio has been remapped so dispatch it */
588 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
589 tio->io->bio->bi_bdev->bd_dev,
590 clone->bi_sector, sector);
592 generic_make_request(clone);
593 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
594 /* error the io and bail out, or requeue it if needed */
596 dec_pending(tio->io, r);
598 * Store bio_set for cleanup.
600 clone->bi_private = md->bs;
604 DMWARN("unimplemented target map return value: %d", r);
610 struct mapped_device *md;
611 struct dm_table *map;
615 sector_t sector_count;
619 static void dm_bio_destructor(struct bio *bio)
621 struct bio_set *bs = bio->bi_private;
627 * Creates a little bio that is just does part of a bvec.
629 static struct bio *split_bvec(struct bio *bio, sector_t sector,
630 unsigned short idx, unsigned int offset,
631 unsigned int len, struct bio_set *bs)
634 struct bio_vec *bv = bio->bi_io_vec + idx;
636 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
637 clone->bi_destructor = dm_bio_destructor;
638 *clone->bi_io_vec = *bv;
640 clone->bi_sector = sector;
641 clone->bi_bdev = bio->bi_bdev;
642 clone->bi_rw = bio->bi_rw;
644 clone->bi_size = to_bytes(len);
645 clone->bi_io_vec->bv_offset = offset;
646 clone->bi_io_vec->bv_len = clone->bi_size;
652 * Creates a bio that consists of range of complete bvecs.
654 static struct bio *clone_bio(struct bio *bio, sector_t sector,
655 unsigned short idx, unsigned short bv_count,
656 unsigned int len, struct bio_set *bs)
660 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
661 __bio_clone(clone, bio);
662 clone->bi_destructor = dm_bio_destructor;
663 clone->bi_sector = sector;
665 clone->bi_vcnt = idx + bv_count;
666 clone->bi_size = to_bytes(len);
667 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
672 static void __clone_and_map(struct clone_info *ci)
674 struct bio *clone, *bio = ci->bio;
675 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
676 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
677 struct dm_target_io *tio;
680 * Allocate a target io object.
682 tio = alloc_tio(ci->md);
685 memset(&tio->info, 0, sizeof(tio->info));
687 if (ci->sector_count <= max) {
689 * Optimise for the simple case where we can do all of
690 * the remaining io with a single clone.
692 clone = clone_bio(bio, ci->sector, ci->idx,
693 bio->bi_vcnt - ci->idx, ci->sector_count,
695 __map_bio(ti, clone, tio);
696 ci->sector_count = 0;
698 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
700 * There are some bvecs that don't span targets.
701 * Do as many of these as possible.
704 sector_t remaining = max;
707 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
708 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
710 if (bv_len > remaining)
717 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
719 __map_bio(ti, clone, tio);
722 ci->sector_count -= len;
727 * Handle a bvec that must be split between two or more targets.
729 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
730 sector_t remaining = to_sector(bv->bv_len);
731 unsigned int offset = 0;
735 ti = dm_table_find_target(ci->map, ci->sector);
736 max = max_io_len(ci->md, ci->sector, ti);
738 tio = alloc_tio(ci->md);
741 memset(&tio->info, 0, sizeof(tio->info));
744 len = min(remaining, max);
746 clone = split_bvec(bio, ci->sector, ci->idx,
747 bv->bv_offset + offset, len,
750 __map_bio(ti, clone, tio);
753 ci->sector_count -= len;
754 offset += to_bytes(len);
755 } while (remaining -= len);
762 * Split the bio into several clones.
764 static int __split_bio(struct mapped_device *md, struct bio *bio)
766 struct clone_info ci;
768 ci.map = dm_get_table(md);
769 if (unlikely(!ci.map))
774 ci.io = alloc_io(md);
776 atomic_set(&ci.io->io_count, 1);
779 ci.sector = bio->bi_sector;
780 ci.sector_count = bio_sectors(bio);
781 ci.idx = bio->bi_idx;
783 start_io_acct(ci.io);
784 while (ci.sector_count)
785 __clone_and_map(&ci);
787 /* drop the extra reference count */
788 dec_pending(ci.io, 0);
789 dm_table_put(ci.map);
793 /*-----------------------------------------------------------------
795 *---------------------------------------------------------------*/
798 * The request function that just remaps the bio built up by
801 static int dm_request(struct request_queue *q, struct bio *bio)
804 int rw = bio_data_dir(bio);
805 struct mapped_device *md = q->queuedata;
808 * There is no use in forwarding any barrier request since we can't
809 * guarantee it is (or can be) handled by the targets correctly.
811 if (unlikely(bio_barrier(bio))) {
812 bio_endio(bio, -EOPNOTSUPP);
816 down_read(&md->io_lock);
818 disk_stat_inc(dm_disk(md), ios[rw]);
819 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
822 * If we're suspended we have to queue
825 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
826 up_read(&md->io_lock);
828 if (bio_rw(bio) != READA)
829 r = queue_io(md, bio);
835 * We're in a while loop, because someone could suspend
836 * before we get to the following read lock.
838 down_read(&md->io_lock);
841 r = __split_bio(md, bio);
842 up_read(&md->io_lock);
851 static void dm_unplug_all(struct request_queue *q)
853 struct mapped_device *md = q->queuedata;
854 struct dm_table *map = dm_get_table(md);
857 dm_table_unplug_all(map);
862 static int dm_any_congested(void *congested_data, int bdi_bits)
865 struct mapped_device *md = (struct mapped_device *) congested_data;
866 struct dm_table *map = dm_get_table(md);
868 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
871 r = dm_table_any_congested(map, bdi_bits);
877 /*-----------------------------------------------------------------
878 * An IDR is used to keep track of allocated minor numbers.
879 *---------------------------------------------------------------*/
880 static DEFINE_IDR(_minor_idr);
882 static void free_minor(int minor)
884 spin_lock(&_minor_lock);
885 idr_remove(&_minor_idr, minor);
886 spin_unlock(&_minor_lock);
890 * See if the device with a specific minor # is free.
892 static int specific_minor(struct mapped_device *md, int minor)
896 if (minor >= (1 << MINORBITS))
899 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
903 spin_lock(&_minor_lock);
905 if (idr_find(&_minor_idr, minor)) {
910 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
915 idr_remove(&_minor_idr, m);
921 spin_unlock(&_minor_lock);
925 static int next_free_minor(struct mapped_device *md, int *minor)
929 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
933 spin_lock(&_minor_lock);
935 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
940 if (m >= (1 << MINORBITS)) {
941 idr_remove(&_minor_idr, m);
949 spin_unlock(&_minor_lock);
953 static struct block_device_operations dm_blk_dops;
956 * Allocate and initialise a blank device with a given minor.
958 static struct mapped_device *alloc_dev(int minor)
961 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
965 DMWARN("unable to allocate device, out of memory.");
969 if (!try_module_get(THIS_MODULE))
972 /* get a minor number for the dev */
973 if (minor == DM_ANY_MINOR)
974 r = next_free_minor(md, &minor);
976 r = specific_minor(md, minor);
980 memset(md, 0, sizeof(*md));
981 init_rwsem(&md->io_lock);
982 init_MUTEX(&md->suspend_lock);
983 spin_lock_init(&md->pushback_lock);
984 rwlock_init(&md->map_lock);
985 atomic_set(&md->holders, 1);
986 atomic_set(&md->open_count, 0);
987 atomic_set(&md->event_nr, 0);
989 md->queue = blk_alloc_queue(GFP_KERNEL);
991 goto bad1_free_minor;
993 md->queue->queuedata = md;
994 md->queue->backing_dev_info.congested_fn = dm_any_congested;
995 md->queue->backing_dev_info.congested_data = md;
996 blk_queue_make_request(md->queue, dm_request);
997 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
998 md->queue->unplug_fn = dm_unplug_all;
1000 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
1004 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1008 md->bs = bioset_create(16, 16);
1012 md->disk = alloc_disk(1);
1016 atomic_set(&md->pending, 0);
1017 init_waitqueue_head(&md->wait);
1018 init_waitqueue_head(&md->eventq);
1020 md->disk->major = _major;
1021 md->disk->first_minor = minor;
1022 md->disk->fops = &dm_blk_dops;
1023 md->disk->queue = md->queue;
1024 md->disk->private_data = md;
1025 sprintf(md->disk->disk_name, "dm-%d", minor);
1027 format_dev_t(md->name, MKDEV(_major, minor));
1029 /* Populate the mapping, nobody knows we exist yet */
1030 spin_lock(&_minor_lock);
1031 old_md = idr_replace(&_minor_idr, md, minor);
1032 spin_unlock(&_minor_lock);
1034 BUG_ON(old_md != MINOR_ALLOCED);
1039 bioset_free(md->bs);
1041 mempool_destroy(md->tio_pool);
1043 mempool_destroy(md->io_pool);
1045 blk_cleanup_queue(md->queue);
1049 module_put(THIS_MODULE);
1055 static void unlock_fs(struct mapped_device *md);
1057 static void free_dev(struct mapped_device *md)
1059 int minor = md->disk->first_minor;
1061 if (md->suspended_bdev) {
1063 bdput(md->suspended_bdev);
1065 mempool_destroy(md->tio_pool);
1066 mempool_destroy(md->io_pool);
1067 bioset_free(md->bs);
1068 del_gendisk(md->disk);
1071 spin_lock(&_minor_lock);
1072 md->disk->private_data = NULL;
1073 spin_unlock(&_minor_lock);
1076 blk_cleanup_queue(md->queue);
1077 module_put(THIS_MODULE);
1082 * Bind a table to the device.
1084 static void event_callback(void *context)
1086 struct mapped_device *md = (struct mapped_device *) context;
1088 atomic_inc(&md->event_nr);
1089 wake_up(&md->eventq);
1092 static void __set_size(struct mapped_device *md, sector_t size)
1094 set_capacity(md->disk, size);
1096 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1097 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1098 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1101 static int __bind(struct mapped_device *md, struct dm_table *t)
1103 struct request_queue *q = md->queue;
1106 size = dm_table_get_size(t);
1109 * Wipe any geometry if the size of the table changed.
1111 if (size != get_capacity(md->disk))
1112 memset(&md->geometry, 0, sizeof(md->geometry));
1114 if (md->suspended_bdev)
1115 __set_size(md, size);
1120 dm_table_event_callback(t, event_callback, md);
1122 write_lock(&md->map_lock);
1124 dm_table_set_restrictions(t, q);
1125 write_unlock(&md->map_lock);
1130 static void __unbind(struct mapped_device *md)
1132 struct dm_table *map = md->map;
1137 dm_table_event_callback(map, NULL, NULL);
1138 write_lock(&md->map_lock);
1140 write_unlock(&md->map_lock);
1145 * Constructor for a new device.
1147 int dm_create(int minor, struct mapped_device **result)
1149 struct mapped_device *md;
1151 md = alloc_dev(minor);
1159 static struct mapped_device *dm_find_md(dev_t dev)
1161 struct mapped_device *md;
1162 unsigned minor = MINOR(dev);
1164 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1167 spin_lock(&_minor_lock);
1169 md = idr_find(&_minor_idr, minor);
1170 if (md && (md == MINOR_ALLOCED ||
1171 (dm_disk(md)->first_minor != minor) ||
1172 test_bit(DMF_FREEING, &md->flags))) {
1178 spin_unlock(&_minor_lock);
1183 struct mapped_device *dm_get_md(dev_t dev)
1185 struct mapped_device *md = dm_find_md(dev);
1193 void *dm_get_mdptr(struct mapped_device *md)
1195 return md->interface_ptr;
1198 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1200 md->interface_ptr = ptr;
1203 void dm_get(struct mapped_device *md)
1205 atomic_inc(&md->holders);
1208 const char *dm_device_name(struct mapped_device *md)
1212 EXPORT_SYMBOL_GPL(dm_device_name);
1214 void dm_put(struct mapped_device *md)
1216 struct dm_table *map;
1218 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1220 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1221 map = dm_get_table(md);
1222 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1223 set_bit(DMF_FREEING, &md->flags);
1224 spin_unlock(&_minor_lock);
1225 if (!dm_suspended(md)) {
1226 dm_table_presuspend_targets(map);
1227 dm_table_postsuspend_targets(map);
1234 EXPORT_SYMBOL_GPL(dm_put);
1237 * Process the deferred bios
1239 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1246 if (__split_bio(md, c))
1253 * Swap in a new table (destroying old one).
1255 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1259 down(&md->suspend_lock);
1261 /* device must be suspended */
1262 if (!dm_suspended(md))
1265 /* without bdev, the device size cannot be changed */
1266 if (!md->suspended_bdev)
1267 if (get_capacity(md->disk) != dm_table_get_size(table))
1271 r = __bind(md, table);
1274 up(&md->suspend_lock);
1279 * Functions to lock and unlock any filesystem running on the
1282 static int lock_fs(struct mapped_device *md)
1286 WARN_ON(md->frozen_sb);
1288 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1289 if (IS_ERR(md->frozen_sb)) {
1290 r = PTR_ERR(md->frozen_sb);
1291 md->frozen_sb = NULL;
1295 set_bit(DMF_FROZEN, &md->flags);
1297 /* don't bdput right now, we don't want the bdev
1298 * to go away while it is locked.
1303 static void unlock_fs(struct mapped_device *md)
1305 if (!test_bit(DMF_FROZEN, &md->flags))
1308 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1309 md->frozen_sb = NULL;
1310 clear_bit(DMF_FROZEN, &md->flags);
1314 * We need to be able to change a mapping table under a mounted
1315 * filesystem. For example we might want to move some data in
1316 * the background. Before the table can be swapped with
1317 * dm_bind_table, dm_suspend must be called to flush any in
1318 * flight bios and ensure that any further io gets deferred.
1320 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1322 struct dm_table *map = NULL;
1323 unsigned long flags;
1324 DECLARE_WAITQUEUE(wait, current);
1327 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1328 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1330 down(&md->suspend_lock);
1332 if (dm_suspended(md))
1335 map = dm_get_table(md);
1338 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1339 * This flag is cleared before dm_suspend returns.
1342 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1344 /* This does not get reverted if there's an error later. */
1345 dm_table_presuspend_targets(map);
1347 /* bdget() can stall if the pending I/Os are not flushed */
1349 md->suspended_bdev = bdget_disk(md->disk, 0);
1350 if (!md->suspended_bdev) {
1351 DMWARN("bdget failed in dm_suspend");
1358 * Flush I/O to the device.
1359 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1361 if (do_lockfs && !noflush) {
1368 * First we set the BLOCK_IO flag so no more ios will be mapped.
1370 down_write(&md->io_lock);
1371 set_bit(DMF_BLOCK_IO, &md->flags);
1373 add_wait_queue(&md->wait, &wait);
1374 up_write(&md->io_lock);
1378 dm_table_unplug_all(map);
1381 * Then we wait for the already mapped ios to
1385 set_current_state(TASK_INTERRUPTIBLE);
1387 if (!atomic_read(&md->pending) || signal_pending(current))
1392 set_current_state(TASK_RUNNING);
1394 down_write(&md->io_lock);
1395 remove_wait_queue(&md->wait, &wait);
1398 spin_lock_irqsave(&md->pushback_lock, flags);
1399 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1400 bio_list_merge_head(&md->deferred, &md->pushback);
1401 bio_list_init(&md->pushback);
1402 spin_unlock_irqrestore(&md->pushback_lock, flags);
1405 /* were we interrupted ? */
1407 if (atomic_read(&md->pending)) {
1408 clear_bit(DMF_BLOCK_IO, &md->flags);
1409 def = bio_list_get(&md->deferred);
1410 __flush_deferred_io(md, def);
1411 up_write(&md->io_lock);
1413 goto out; /* pushback list is already flushed, so skip flush */
1415 up_write(&md->io_lock);
1417 dm_table_postsuspend_targets(map);
1419 set_bit(DMF_SUSPENDED, &md->flags);
1426 * Because there may be already I/Os in the pushback list,
1427 * flush them before return.
1429 down_write(&md->io_lock);
1431 spin_lock_irqsave(&md->pushback_lock, flags);
1432 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1433 bio_list_merge_head(&md->deferred, &md->pushback);
1434 bio_list_init(&md->pushback);
1435 spin_unlock_irqrestore(&md->pushback_lock, flags);
1437 def = bio_list_get(&md->deferred);
1438 __flush_deferred_io(md, def);
1439 up_write(&md->io_lock);
1443 if (r && md->suspended_bdev) {
1444 bdput(md->suspended_bdev);
1445 md->suspended_bdev = NULL;
1451 up(&md->suspend_lock);
1455 int dm_resume(struct mapped_device *md)
1459 struct dm_table *map = NULL;
1461 down(&md->suspend_lock);
1462 if (!dm_suspended(md))
1465 map = dm_get_table(md);
1466 if (!map || !dm_table_get_size(map))
1469 r = dm_table_resume_targets(map);
1473 down_write(&md->io_lock);
1474 clear_bit(DMF_BLOCK_IO, &md->flags);
1476 def = bio_list_get(&md->deferred);
1477 __flush_deferred_io(md, def);
1478 up_write(&md->io_lock);
1482 if (md->suspended_bdev) {
1483 bdput(md->suspended_bdev);
1484 md->suspended_bdev = NULL;
1487 clear_bit(DMF_SUSPENDED, &md->flags);
1489 dm_table_unplug_all(map);
1491 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1497 up(&md->suspend_lock);
1502 /*-----------------------------------------------------------------
1503 * Event notification.
1504 *---------------------------------------------------------------*/
1505 uint32_t dm_get_event_nr(struct mapped_device *md)
1507 return atomic_read(&md->event_nr);
1510 int dm_wait_event(struct mapped_device *md, int event_nr)
1512 return wait_event_interruptible(md->eventq,
1513 (event_nr != atomic_read(&md->event_nr)));
1517 * The gendisk is only valid as long as you have a reference
1520 struct gendisk *dm_disk(struct mapped_device *md)
1525 int dm_suspended(struct mapped_device *md)
1527 return test_bit(DMF_SUSPENDED, &md->flags);
1530 int dm_noflush_suspending(struct dm_target *ti)
1532 struct mapped_device *md = dm_table_get_md(ti->table);
1533 int r = __noflush_suspending(md);
1539 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1541 static struct block_device_operations dm_blk_dops = {
1542 .open = dm_blk_open,
1543 .release = dm_blk_close,
1544 .ioctl = dm_blk_ioctl,
1545 .getgeo = dm_blk_getgeo,
1546 .owner = THIS_MODULE
1549 EXPORT_SYMBOL(dm_get_mapinfo);
1554 module_init(dm_init);
1555 module_exit(dm_exit);
1557 module_param(major, uint, 0);
1558 MODULE_PARM_DESC(major, "The major number of the device mapper");
1559 MODULE_DESCRIPTION(DM_NAME " driver");
1560 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1561 MODULE_LICENSE("GPL");