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"
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>
22 #include <linux/blktrace_api.h>
24 #define DM_MSG_PREFIX "core"
26 static const char *_name = DM_NAME;
28 static unsigned int major = 0;
29 static unsigned int _major = 0;
31 static DEFINE_SPINLOCK(_minor_lock);
33 * One of these is allocated per bio.
36 struct mapped_device *md;
40 unsigned long start_time;
44 * One of these is allocated per target within a bio. Hopefully
45 * this will be simplified out one day.
53 union map_info *dm_get_mapinfo(struct bio *bio)
55 if (bio && bio->bi_private)
56 return &((struct target_io *)bio->bi_private)->info;
60 #define MINOR_ALLOCED ((void *)-1)
63 * Bits for the md->flags field.
65 #define DMF_BLOCK_IO 0
66 #define DMF_SUSPENDED 1
69 #define DMF_DELETING 4
71 struct mapped_device {
72 struct rw_semaphore io_lock;
73 struct semaphore suspend_lock;
80 request_queue_t *queue;
87 * A list of ios that arrived while we were suspended.
90 wait_queue_head_t wait;
91 struct bio_list deferred;
94 * The current mapping.
99 * io objects are allocated from here.
108 wait_queue_head_t eventq;
111 * freeze/thaw support require holding onto a super block
113 struct super_block *frozen_sb;
114 struct block_device *suspended_bdev;
116 /* forced geometry settings */
117 struct hd_geometry geometry;
121 static kmem_cache_t *_io_cache;
122 static kmem_cache_t *_tio_cache;
124 static struct bio_set *dm_set;
126 static int __init local_init(void)
130 dm_set = bioset_create(16, 16, 4);
134 /* allocate a slab for the dm_ios */
135 _io_cache = kmem_cache_create("dm_io",
136 sizeof(struct dm_io), 0, 0, NULL, NULL);
140 /* allocate a slab for the target ios */
141 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
144 kmem_cache_destroy(_io_cache);
149 r = register_blkdev(_major, _name);
151 kmem_cache_destroy(_tio_cache);
152 kmem_cache_destroy(_io_cache);
162 static void local_exit(void)
164 kmem_cache_destroy(_tio_cache);
165 kmem_cache_destroy(_io_cache);
169 if (unregister_blkdev(_major, _name) < 0)
170 DMERR("devfs_unregister_blkdev failed");
174 DMINFO("cleaned up");
177 int (*_inits[])(void) __initdata = {
185 void (*_exits[])(void) = {
193 static int __init dm_init(void)
195 const int count = ARRAY_SIZE(_inits);
199 for (i = 0; i < count; i++) {
214 static void __exit dm_exit(void)
216 int i = ARRAY_SIZE(_exits);
223 * Block device functions
225 static int dm_blk_open(struct inode *inode, struct file *file)
227 struct mapped_device *md;
229 spin_lock(&_minor_lock);
231 md = inode->i_bdev->bd_disk->private_data;
235 if (test_bit(DMF_FREEING, &md->flags) ||
236 test_bit(DMF_DELETING, &md->flags)) {
242 atomic_inc(&md->open_count);
245 spin_unlock(&_minor_lock);
247 return md ? 0 : -ENXIO;
250 static int dm_blk_close(struct inode *inode, struct file *file)
252 struct mapped_device *md;
254 md = inode->i_bdev->bd_disk->private_data;
255 atomic_dec(&md->open_count);
260 int dm_open_count(struct mapped_device *md)
262 return atomic_read(&md->open_count);
266 * Guarantees nothing is using the device before it's deleted.
268 int dm_lock_for_deletion(struct mapped_device *md)
272 spin_lock(&_minor_lock);
274 if (dm_open_count(md))
277 set_bit(DMF_DELETING, &md->flags);
279 spin_unlock(&_minor_lock);
284 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
286 struct mapped_device *md = bdev->bd_disk->private_data;
288 return dm_get_geometry(md, geo);
291 static inline struct dm_io *alloc_io(struct mapped_device *md)
293 return mempool_alloc(md->io_pool, GFP_NOIO);
296 static inline void free_io(struct mapped_device *md, struct dm_io *io)
298 mempool_free(io, md->io_pool);
301 static inline struct target_io *alloc_tio(struct mapped_device *md)
303 return mempool_alloc(md->tio_pool, GFP_NOIO);
306 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
308 mempool_free(tio, md->tio_pool);
311 static void start_io_acct(struct dm_io *io)
313 struct mapped_device *md = io->md;
315 io->start_time = jiffies;
318 disk_round_stats(dm_disk(md));
320 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
323 static int end_io_acct(struct dm_io *io)
325 struct mapped_device *md = io->md;
326 struct bio *bio = io->bio;
327 unsigned long duration = jiffies - io->start_time;
329 int rw = bio_data_dir(bio);
332 disk_round_stats(dm_disk(md));
334 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
336 disk_stat_add(dm_disk(md), ticks[rw], duration);
342 * Add the bio to the list of deferred io.
344 static int queue_io(struct mapped_device *md, struct bio *bio)
346 down_write(&md->io_lock);
348 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
349 up_write(&md->io_lock);
353 bio_list_add(&md->deferred, bio);
355 up_write(&md->io_lock);
356 return 0; /* deferred successfully */
360 * Everyone (including functions in this file), should use this
361 * function to access the md->map field, and make sure they call
362 * dm_table_put() when finished.
364 struct dm_table *dm_get_table(struct mapped_device *md)
368 read_lock(&md->map_lock);
372 read_unlock(&md->map_lock);
378 * Get the geometry associated with a dm device
380 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
388 * Set the geometry of a device.
390 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
392 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
394 if (geo->start > sz) {
395 DMWARN("Start sector is beyond the geometry limits.");
404 /*-----------------------------------------------------------------
406 * A more elegant soln is in the works that uses the queue
407 * merge fn, unfortunately there are a couple of changes to
408 * the block layer that I want to make for this. So in the
409 * interests of getting something for people to use I give
410 * you this clearly demarcated crap.
411 *---------------------------------------------------------------*/
414 * Decrements the number of outstanding ios that a bio has been
415 * cloned into, completing the original io if necc.
417 static void dec_pending(struct dm_io *io, int error)
422 if (atomic_dec_and_test(&io->io_count)) {
424 /* nudge anyone waiting on suspend queue */
425 wake_up(&io->md->wait);
427 blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
429 bio_endio(io->bio, io->bio->bi_size, io->error);
434 static int clone_endio(struct bio *bio, unsigned int done, int error)
437 struct target_io *tio = bio->bi_private;
438 struct dm_io *io = tio->io;
439 dm_endio_fn endio = tio->ti->type->end_io;
444 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
448 r = endio(tio->ti, bio, error, &tio->info);
453 /* the target wants another shot at the io */
457 free_tio(io->md, tio);
458 dec_pending(io, error);
463 static sector_t max_io_len(struct mapped_device *md,
464 sector_t sector, struct dm_target *ti)
466 sector_t offset = sector - ti->begin;
467 sector_t len = ti->len - offset;
470 * Does the target need to split even further ?
474 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
483 static void __map_bio(struct dm_target *ti, struct bio *clone,
484 struct target_io *tio)
492 BUG_ON(!clone->bi_size);
494 clone->bi_end_io = clone_endio;
495 clone->bi_private = tio;
498 * Map the clone. If r == 0 we don't need to do
499 * anything, the target has assumed ownership of
502 atomic_inc(&tio->io->io_count);
503 sector = clone->bi_sector;
504 r = ti->type->map(ti, clone, &tio->info);
506 /* the bio has been remapped so dispatch it */
508 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
509 tio->io->bio->bi_bdev->bd_dev, sector,
512 generic_make_request(clone);
516 /* error the io and bail out */
517 struct dm_io *io = tio->io;
518 free_tio(tio->io->md, tio);
525 struct mapped_device *md;
526 struct dm_table *map;
530 sector_t sector_count;
534 static void dm_bio_destructor(struct bio *bio)
536 bio_free(bio, dm_set);
540 * Creates a little bio that is just does part of a bvec.
542 static struct bio *split_bvec(struct bio *bio, sector_t sector,
543 unsigned short idx, unsigned int offset,
547 struct bio_vec *bv = bio->bi_io_vec + idx;
549 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
550 clone->bi_destructor = dm_bio_destructor;
551 *clone->bi_io_vec = *bv;
553 clone->bi_sector = sector;
554 clone->bi_bdev = bio->bi_bdev;
555 clone->bi_rw = bio->bi_rw;
557 clone->bi_size = to_bytes(len);
558 clone->bi_io_vec->bv_offset = offset;
559 clone->bi_io_vec->bv_len = clone->bi_size;
565 * Creates a bio that consists of range of complete bvecs.
567 static struct bio *clone_bio(struct bio *bio, sector_t sector,
568 unsigned short idx, unsigned short bv_count,
573 clone = bio_clone(bio, GFP_NOIO);
574 clone->bi_sector = sector;
576 clone->bi_vcnt = idx + bv_count;
577 clone->bi_size = to_bytes(len);
578 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
583 static void __clone_and_map(struct clone_info *ci)
585 struct bio *clone, *bio = ci->bio;
586 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
587 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
588 struct target_io *tio;
591 * Allocate a target io object.
593 tio = alloc_tio(ci->md);
596 memset(&tio->info, 0, sizeof(tio->info));
598 if (ci->sector_count <= max) {
600 * Optimise for the simple case where we can do all of
601 * the remaining io with a single clone.
603 clone = clone_bio(bio, ci->sector, ci->idx,
604 bio->bi_vcnt - ci->idx, ci->sector_count);
605 __map_bio(ti, clone, tio);
606 ci->sector_count = 0;
608 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
610 * There are some bvecs that don't span targets.
611 * Do as many of these as possible.
614 sector_t remaining = max;
617 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
618 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
620 if (bv_len > remaining)
627 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
628 __map_bio(ti, clone, tio);
631 ci->sector_count -= len;
636 * Handle a bvec that must be split between two or more targets.
638 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
639 sector_t remaining = to_sector(bv->bv_len);
640 unsigned int offset = 0;
644 ti = dm_table_find_target(ci->map, ci->sector);
645 max = max_io_len(ci->md, ci->sector, ti);
647 tio = alloc_tio(ci->md);
650 memset(&tio->info, 0, sizeof(tio->info));
653 len = min(remaining, max);
655 clone = split_bvec(bio, ci->sector, ci->idx,
656 bv->bv_offset + offset, len);
658 __map_bio(ti, clone, tio);
661 ci->sector_count -= len;
662 offset += to_bytes(len);
663 } while (remaining -= len);
670 * Split the bio into several clones.
672 static void __split_bio(struct mapped_device *md, struct bio *bio)
674 struct clone_info ci;
676 ci.map = dm_get_table(md);
678 bio_io_error(bio, bio->bi_size);
684 ci.io = alloc_io(md);
686 atomic_set(&ci.io->io_count, 1);
689 ci.sector = bio->bi_sector;
690 ci.sector_count = bio_sectors(bio);
691 ci.idx = bio->bi_idx;
693 start_io_acct(ci.io);
694 while (ci.sector_count)
695 __clone_and_map(&ci);
697 /* drop the extra reference count */
698 dec_pending(ci.io, 0);
699 dm_table_put(ci.map);
701 /*-----------------------------------------------------------------
703 *---------------------------------------------------------------*/
706 * The request function that just remaps the bio built up by
709 static int dm_request(request_queue_t *q, struct bio *bio)
712 int rw = bio_data_dir(bio);
713 struct mapped_device *md = q->queuedata;
715 down_read(&md->io_lock);
717 disk_stat_inc(dm_disk(md), ios[rw]);
718 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
721 * If we're suspended we have to queue
724 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
725 up_read(&md->io_lock);
727 if (bio_rw(bio) == READA) {
728 bio_io_error(bio, bio->bi_size);
732 r = queue_io(md, bio);
734 bio_io_error(bio, bio->bi_size);
738 return 0; /* deferred successfully */
741 * We're in a while loop, because someone could suspend
742 * before we get to the following read lock.
744 down_read(&md->io_lock);
747 __split_bio(md, bio);
748 up_read(&md->io_lock);
752 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
753 sector_t *error_sector)
755 struct mapped_device *md = q->queuedata;
756 struct dm_table *map = dm_get_table(md);
760 ret = dm_table_flush_all(map);
767 static void dm_unplug_all(request_queue_t *q)
769 struct mapped_device *md = q->queuedata;
770 struct dm_table *map = dm_get_table(md);
773 dm_table_unplug_all(map);
778 static int dm_any_congested(void *congested_data, int bdi_bits)
781 struct mapped_device *md = (struct mapped_device *) congested_data;
782 struct dm_table *map = dm_get_table(md);
784 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
787 r = dm_table_any_congested(map, bdi_bits);
793 /*-----------------------------------------------------------------
794 * An IDR is used to keep track of allocated minor numbers.
795 *---------------------------------------------------------------*/
796 static DEFINE_IDR(_minor_idr);
798 static void free_minor(int minor)
800 spin_lock(&_minor_lock);
801 idr_remove(&_minor_idr, minor);
802 spin_unlock(&_minor_lock);
806 * See if the device with a specific minor # is free.
808 static int specific_minor(struct mapped_device *md, int minor)
812 if (minor >= (1 << MINORBITS))
815 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
819 spin_lock(&_minor_lock);
821 if (idr_find(&_minor_idr, minor)) {
826 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
831 idr_remove(&_minor_idr, m);
837 spin_unlock(&_minor_lock);
841 static int next_free_minor(struct mapped_device *md, int *minor)
845 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
849 spin_lock(&_minor_lock);
851 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
856 if (m >= (1 << MINORBITS)) {
857 idr_remove(&_minor_idr, m);
865 spin_unlock(&_minor_lock);
869 static struct block_device_operations dm_blk_dops;
872 * Allocate and initialise a blank device with a given minor.
874 static struct mapped_device *alloc_dev(int minor)
877 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
881 DMWARN("unable to allocate device, out of memory.");
885 if (!try_module_get(THIS_MODULE))
888 /* get a minor number for the dev */
889 if (minor == DM_ANY_MINOR)
890 r = next_free_minor(md, &minor);
892 r = specific_minor(md, minor);
896 memset(md, 0, sizeof(*md));
897 init_rwsem(&md->io_lock);
898 init_MUTEX(&md->suspend_lock);
899 rwlock_init(&md->map_lock);
900 atomic_set(&md->holders, 1);
901 atomic_set(&md->open_count, 0);
902 atomic_set(&md->event_nr, 0);
904 md->queue = blk_alloc_queue(GFP_KERNEL);
908 md->queue->queuedata = md;
909 md->queue->backing_dev_info.congested_fn = dm_any_congested;
910 md->queue->backing_dev_info.congested_data = md;
911 blk_queue_make_request(md->queue, dm_request);
912 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
913 md->queue->unplug_fn = dm_unplug_all;
914 md->queue->issue_flush_fn = dm_flush_all;
916 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
920 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
924 md->disk = alloc_disk(1);
928 atomic_set(&md->pending, 0);
929 init_waitqueue_head(&md->wait);
930 init_waitqueue_head(&md->eventq);
932 md->disk->major = _major;
933 md->disk->first_minor = minor;
934 md->disk->fops = &dm_blk_dops;
935 md->disk->queue = md->queue;
936 md->disk->private_data = md;
937 sprintf(md->disk->disk_name, "dm-%d", minor);
939 format_dev_t(md->name, MKDEV(_major, minor));
941 /* Populate the mapping, nobody knows we exist yet */
942 spin_lock(&_minor_lock);
943 old_md = idr_replace(&_minor_idr, md, minor);
944 spin_unlock(&_minor_lock);
946 BUG_ON(old_md != MINOR_ALLOCED);
951 mempool_destroy(md->tio_pool);
953 mempool_destroy(md->io_pool);
955 blk_cleanup_queue(md->queue);
958 module_put(THIS_MODULE);
964 static void free_dev(struct mapped_device *md)
966 int minor = md->disk->first_minor;
968 if (md->suspended_bdev) {
969 thaw_bdev(md->suspended_bdev, NULL);
970 bdput(md->suspended_bdev);
972 mempool_destroy(md->tio_pool);
973 mempool_destroy(md->io_pool);
974 del_gendisk(md->disk);
977 spin_lock(&_minor_lock);
978 md->disk->private_data = NULL;
979 spin_unlock(&_minor_lock);
982 blk_cleanup_queue(md->queue);
983 module_put(THIS_MODULE);
988 * Bind a table to the device.
990 static void event_callback(void *context)
992 struct mapped_device *md = (struct mapped_device *) context;
994 atomic_inc(&md->event_nr);
995 wake_up(&md->eventq);
998 static void __set_size(struct mapped_device *md, sector_t size)
1000 set_capacity(md->disk, size);
1002 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1003 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1004 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1007 static int __bind(struct mapped_device *md, struct dm_table *t)
1009 request_queue_t *q = md->queue;
1012 size = dm_table_get_size(t);
1015 * Wipe any geometry if the size of the table changed.
1017 if (size != get_capacity(md->disk))
1018 memset(&md->geometry, 0, sizeof(md->geometry));
1020 __set_size(md, size);
1025 dm_table_event_callback(t, event_callback, md);
1027 write_lock(&md->map_lock);
1029 dm_table_set_restrictions(t, q);
1030 write_unlock(&md->map_lock);
1035 static void __unbind(struct mapped_device *md)
1037 struct dm_table *map = md->map;
1042 dm_table_event_callback(map, NULL, NULL);
1043 write_lock(&md->map_lock);
1045 write_unlock(&md->map_lock);
1050 * Constructor for a new device.
1052 int dm_create(int minor, struct mapped_device **result)
1054 struct mapped_device *md;
1056 md = alloc_dev(minor);
1064 static struct mapped_device *dm_find_md(dev_t dev)
1066 struct mapped_device *md;
1067 unsigned minor = MINOR(dev);
1069 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1072 spin_lock(&_minor_lock);
1074 md = idr_find(&_minor_idr, minor);
1075 if (md && (md == MINOR_ALLOCED ||
1076 (dm_disk(md)->first_minor != minor) ||
1077 test_bit(DMF_FREEING, &md->flags))) {
1083 spin_unlock(&_minor_lock);
1088 struct mapped_device *dm_get_md(dev_t dev)
1090 struct mapped_device *md = dm_find_md(dev);
1098 void *dm_get_mdptr(struct mapped_device *md)
1100 return md->interface_ptr;
1103 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1105 md->interface_ptr = ptr;
1108 void dm_get(struct mapped_device *md)
1110 atomic_inc(&md->holders);
1113 const char *dm_device_name(struct mapped_device *md)
1117 EXPORT_SYMBOL_GPL(dm_device_name);
1119 void dm_put(struct mapped_device *md)
1121 struct dm_table *map;
1123 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1125 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1126 map = dm_get_table(md);
1127 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1128 set_bit(DMF_FREEING, &md->flags);
1129 spin_unlock(&_minor_lock);
1130 if (!dm_suspended(md)) {
1131 dm_table_presuspend_targets(map);
1132 dm_table_postsuspend_targets(map);
1141 * Process the deferred bios
1143 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1156 * Swap in a new table (destroying old one).
1158 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1162 down(&md->suspend_lock);
1164 /* device must be suspended */
1165 if (!dm_suspended(md))
1169 r = __bind(md, table);
1172 up(&md->suspend_lock);
1177 * Functions to lock and unlock any filesystem running on the
1180 static int lock_fs(struct mapped_device *md)
1184 WARN_ON(md->frozen_sb);
1186 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1187 if (IS_ERR(md->frozen_sb)) {
1188 r = PTR_ERR(md->frozen_sb);
1189 md->frozen_sb = NULL;
1193 set_bit(DMF_FROZEN, &md->flags);
1195 /* don't bdput right now, we don't want the bdev
1196 * to go away while it is locked.
1201 static void unlock_fs(struct mapped_device *md)
1203 if (!test_bit(DMF_FROZEN, &md->flags))
1206 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1207 md->frozen_sb = NULL;
1208 clear_bit(DMF_FROZEN, &md->flags);
1212 * We need to be able to change a mapping table under a mounted
1213 * filesystem. For example we might want to move some data in
1214 * the background. Before the table can be swapped with
1215 * dm_bind_table, dm_suspend must be called to flush any in
1216 * flight bios and ensure that any further io gets deferred.
1218 int dm_suspend(struct mapped_device *md, int do_lockfs)
1220 struct dm_table *map = NULL;
1221 DECLARE_WAITQUEUE(wait, current);
1225 down(&md->suspend_lock);
1227 if (dm_suspended(md))
1230 map = dm_get_table(md);
1232 /* This does not get reverted if there's an error later. */
1233 dm_table_presuspend_targets(map);
1235 md->suspended_bdev = bdget_disk(md->disk, 0);
1236 if (!md->suspended_bdev) {
1237 DMWARN("bdget failed in dm_suspend");
1242 /* Flush I/O to the device. */
1250 * First we set the BLOCK_IO flag so no more ios will be mapped.
1252 down_write(&md->io_lock);
1253 set_bit(DMF_BLOCK_IO, &md->flags);
1255 add_wait_queue(&md->wait, &wait);
1256 up_write(&md->io_lock);
1260 dm_table_unplug_all(map);
1263 * Then we wait for the already mapped ios to
1267 set_current_state(TASK_INTERRUPTIBLE);
1269 if (!atomic_read(&md->pending) || signal_pending(current))
1274 set_current_state(TASK_RUNNING);
1276 down_write(&md->io_lock);
1277 remove_wait_queue(&md->wait, &wait);
1279 /* were we interrupted ? */
1281 if (atomic_read(&md->pending)) {
1282 clear_bit(DMF_BLOCK_IO, &md->flags);
1283 def = bio_list_get(&md->deferred);
1284 __flush_deferred_io(md, def);
1285 up_write(&md->io_lock);
1289 up_write(&md->io_lock);
1291 dm_table_postsuspend_targets(map);
1293 set_bit(DMF_SUSPENDED, &md->flags);
1298 if (r && md->suspended_bdev) {
1299 bdput(md->suspended_bdev);
1300 md->suspended_bdev = NULL;
1304 up(&md->suspend_lock);
1308 int dm_resume(struct mapped_device *md)
1312 struct dm_table *map = NULL;
1314 down(&md->suspend_lock);
1315 if (!dm_suspended(md))
1318 map = dm_get_table(md);
1319 if (!map || !dm_table_get_size(map))
1322 dm_table_resume_targets(map);
1324 down_write(&md->io_lock);
1325 clear_bit(DMF_BLOCK_IO, &md->flags);
1327 def = bio_list_get(&md->deferred);
1328 __flush_deferred_io(md, def);
1329 up_write(&md->io_lock);
1333 bdput(md->suspended_bdev);
1334 md->suspended_bdev = NULL;
1336 clear_bit(DMF_SUSPENDED, &md->flags);
1338 dm_table_unplug_all(map);
1344 up(&md->suspend_lock);
1349 /*-----------------------------------------------------------------
1350 * Event notification.
1351 *---------------------------------------------------------------*/
1352 uint32_t dm_get_event_nr(struct mapped_device *md)
1354 return atomic_read(&md->event_nr);
1357 int dm_wait_event(struct mapped_device *md, int event_nr)
1359 return wait_event_interruptible(md->eventq,
1360 (event_nr != atomic_read(&md->event_nr)));
1364 * The gendisk is only valid as long as you have a reference
1367 struct gendisk *dm_disk(struct mapped_device *md)
1372 int dm_suspended(struct mapped_device *md)
1374 return test_bit(DMF_SUSPENDED, &md->flags);
1377 static struct block_device_operations dm_blk_dops = {
1378 .open = dm_blk_open,
1379 .release = dm_blk_close,
1380 .getgeo = dm_blk_getgeo,
1381 .owner = THIS_MODULE
1384 EXPORT_SYMBOL(dm_get_mapinfo);
1389 module_init(dm_init);
1390 module_exit(dm_exit);
1392 module_param(major, uint, 0);
1393 MODULE_PARM_DESC(major, "The major number of the device mapper");
1394 MODULE_DESCRIPTION(DM_NAME " driver");
1395 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1396 MODULE_LICENSE("GPL");