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 static const char *_name = DM_NAME;
26 static unsigned int major = 0;
27 static unsigned int _major = 0;
29 static DEFINE_SPINLOCK(_minor_lock);
31 * One of these is allocated per bio.
34 struct mapped_device *md;
38 unsigned long start_time;
42 * One of these is allocated per target within a bio. Hopefully
43 * this will be simplified out one day.
51 union map_info *dm_get_mapinfo(struct bio *bio)
53 if (bio && bio->bi_private)
54 return &((struct target_io *)bio->bi_private)->info;
58 #define MINOR_ALLOCED ((void *)-1)
61 * Bits for the md->flags field.
63 #define DMF_BLOCK_IO 0
64 #define DMF_SUSPENDED 1
67 #define DMF_DELETING 4
69 struct mapped_device {
70 struct rw_semaphore io_lock;
71 struct semaphore suspend_lock;
78 request_queue_t *queue;
85 * A list of ios that arrived while we were suspended.
88 wait_queue_head_t wait;
89 struct bio_list deferred;
92 * The current mapping.
97 * io objects are allocated from here.
106 wait_queue_head_t eventq;
109 * freeze/thaw support require holding onto a super block
111 struct super_block *frozen_sb;
112 struct block_device *suspended_bdev;
114 /* forced geometry settings */
115 struct hd_geometry geometry;
119 static kmem_cache_t *_io_cache;
120 static kmem_cache_t *_tio_cache;
122 static struct bio_set *dm_set;
124 static int __init local_init(void)
128 dm_set = bioset_create(16, 16, 4);
132 /* allocate a slab for the dm_ios */
133 _io_cache = kmem_cache_create("dm_io",
134 sizeof(struct dm_io), 0, 0, NULL, NULL);
138 /* allocate a slab for the target ios */
139 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
142 kmem_cache_destroy(_io_cache);
147 r = register_blkdev(_major, _name);
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
160 static void local_exit(void)
162 kmem_cache_destroy(_tio_cache);
163 kmem_cache_destroy(_io_cache);
167 if (unregister_blkdev(_major, _name) < 0)
168 DMERR("devfs_unregister_blkdev failed");
172 DMINFO("cleaned up");
175 int (*_inits[])(void) __initdata = {
183 void (*_exits[])(void) = {
191 static int __init dm_init(void)
193 const int count = ARRAY_SIZE(_inits);
197 for (i = 0; i < count; i++) {
212 static void __exit dm_exit(void)
214 int i = ARRAY_SIZE(_exits);
221 * Block device functions
223 static int dm_blk_open(struct inode *inode, struct file *file)
225 struct mapped_device *md;
227 spin_lock(&_minor_lock);
229 md = inode->i_bdev->bd_disk->private_data;
233 if (test_bit(DMF_FREEING, &md->flags) ||
234 test_bit(DMF_DELETING, &md->flags)) {
240 atomic_inc(&md->open_count);
243 spin_unlock(&_minor_lock);
245 return md ? 0 : -ENXIO;
248 static int dm_blk_close(struct inode *inode, struct file *file)
250 struct mapped_device *md;
252 md = inode->i_bdev->bd_disk->private_data;
253 atomic_dec(&md->open_count);
258 int dm_open_count(struct mapped_device *md)
260 return atomic_read(&md->open_count);
264 * Guarantees nothing is using the device before it's deleted.
266 int dm_lock_for_deletion(struct mapped_device *md)
270 spin_lock(&_minor_lock);
272 if (dm_open_count(md))
275 set_bit(DMF_DELETING, &md->flags);
277 spin_unlock(&_minor_lock);
282 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
284 struct mapped_device *md = bdev->bd_disk->private_data;
286 return dm_get_geometry(md, geo);
289 static inline struct dm_io *alloc_io(struct mapped_device *md)
291 return mempool_alloc(md->io_pool, GFP_NOIO);
294 static inline void free_io(struct mapped_device *md, struct dm_io *io)
296 mempool_free(io, md->io_pool);
299 static inline struct target_io *alloc_tio(struct mapped_device *md)
301 return mempool_alloc(md->tio_pool, GFP_NOIO);
304 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
306 mempool_free(tio, md->tio_pool);
309 static void start_io_acct(struct dm_io *io)
311 struct mapped_device *md = io->md;
313 io->start_time = jiffies;
316 disk_round_stats(dm_disk(md));
318 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
321 static int end_io_acct(struct dm_io *io)
323 struct mapped_device *md = io->md;
324 struct bio *bio = io->bio;
325 unsigned long duration = jiffies - io->start_time;
327 int rw = bio_data_dir(bio);
330 disk_round_stats(dm_disk(md));
332 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
334 disk_stat_add(dm_disk(md), ticks[rw], duration);
340 * Add the bio to the list of deferred io.
342 static int queue_io(struct mapped_device *md, struct bio *bio)
344 down_write(&md->io_lock);
346 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
347 up_write(&md->io_lock);
351 bio_list_add(&md->deferred, bio);
353 up_write(&md->io_lock);
354 return 0; /* deferred successfully */
358 * Everyone (including functions in this file), should use this
359 * function to access the md->map field, and make sure they call
360 * dm_table_put() when finished.
362 struct dm_table *dm_get_table(struct mapped_device *md)
366 read_lock(&md->map_lock);
370 read_unlock(&md->map_lock);
376 * Get the geometry associated with a dm device
378 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
386 * Set the geometry of a device.
388 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
390 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
392 if (geo->start > sz) {
393 DMWARN("Start sector is beyond the geometry limits.");
402 /*-----------------------------------------------------------------
404 * A more elegant soln is in the works that uses the queue
405 * merge fn, unfortunately there are a couple of changes to
406 * the block layer that I want to make for this. So in the
407 * interests of getting something for people to use I give
408 * you this clearly demarcated crap.
409 *---------------------------------------------------------------*/
412 * Decrements the number of outstanding ios that a bio has been
413 * cloned into, completing the original io if necc.
415 static void dec_pending(struct dm_io *io, int error)
420 if (atomic_dec_and_test(&io->io_count)) {
422 /* nudge anyone waiting on suspend queue */
423 wake_up(&io->md->wait);
425 blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
427 bio_endio(io->bio, io->bio->bi_size, io->error);
432 static int clone_endio(struct bio *bio, unsigned int done, int error)
435 struct target_io *tio = bio->bi_private;
436 struct dm_io *io = tio->io;
437 dm_endio_fn endio = tio->ti->type->end_io;
442 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
446 r = endio(tio->ti, bio, error, &tio->info);
451 /* the target wants another shot at the io */
455 free_tio(io->md, tio);
456 dec_pending(io, error);
461 static sector_t max_io_len(struct mapped_device *md,
462 sector_t sector, struct dm_target *ti)
464 sector_t offset = sector - ti->begin;
465 sector_t len = ti->len - offset;
468 * Does the target need to split even further ?
472 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
481 static void __map_bio(struct dm_target *ti, struct bio *clone,
482 struct target_io *tio)
490 BUG_ON(!clone->bi_size);
492 clone->bi_end_io = clone_endio;
493 clone->bi_private = tio;
496 * Map the clone. If r == 0 we don't need to do
497 * anything, the target has assumed ownership of
500 atomic_inc(&tio->io->io_count);
501 sector = clone->bi_sector;
502 r = ti->type->map(ti, clone, &tio->info);
504 /* the bio has been remapped so dispatch it */
506 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
507 tio->io->bio->bi_bdev->bd_dev, sector,
510 generic_make_request(clone);
514 /* error the io and bail out */
515 struct dm_io *io = tio->io;
516 free_tio(tio->io->md, tio);
523 struct mapped_device *md;
524 struct dm_table *map;
528 sector_t sector_count;
532 static void dm_bio_destructor(struct bio *bio)
534 bio_free(bio, dm_set);
538 * Creates a little bio that is just does part of a bvec.
540 static struct bio *split_bvec(struct bio *bio, sector_t sector,
541 unsigned short idx, unsigned int offset,
545 struct bio_vec *bv = bio->bi_io_vec + idx;
547 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
548 clone->bi_destructor = dm_bio_destructor;
549 *clone->bi_io_vec = *bv;
551 clone->bi_sector = sector;
552 clone->bi_bdev = bio->bi_bdev;
553 clone->bi_rw = bio->bi_rw;
555 clone->bi_size = to_bytes(len);
556 clone->bi_io_vec->bv_offset = offset;
557 clone->bi_io_vec->bv_len = clone->bi_size;
563 * Creates a bio that consists of range of complete bvecs.
565 static struct bio *clone_bio(struct bio *bio, sector_t sector,
566 unsigned short idx, unsigned short bv_count,
571 clone = bio_clone(bio, GFP_NOIO);
572 clone->bi_sector = sector;
574 clone->bi_vcnt = idx + bv_count;
575 clone->bi_size = to_bytes(len);
576 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
581 static void __clone_and_map(struct clone_info *ci)
583 struct bio *clone, *bio = ci->bio;
584 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
585 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
586 struct target_io *tio;
589 * Allocate a target io object.
591 tio = alloc_tio(ci->md);
594 memset(&tio->info, 0, sizeof(tio->info));
596 if (ci->sector_count <= max) {
598 * Optimise for the simple case where we can do all of
599 * the remaining io with a single clone.
601 clone = clone_bio(bio, ci->sector, ci->idx,
602 bio->bi_vcnt - ci->idx, ci->sector_count);
603 __map_bio(ti, clone, tio);
604 ci->sector_count = 0;
606 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
608 * There are some bvecs that don't span targets.
609 * Do as many of these as possible.
612 sector_t remaining = max;
615 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
616 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
618 if (bv_len > remaining)
625 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
626 __map_bio(ti, clone, tio);
629 ci->sector_count -= len;
634 * Handle a bvec that must be split between two or more targets.
636 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
637 sector_t remaining = to_sector(bv->bv_len);
638 unsigned int offset = 0;
642 ti = dm_table_find_target(ci->map, ci->sector);
643 max = max_io_len(ci->md, ci->sector, ti);
645 tio = alloc_tio(ci->md);
648 memset(&tio->info, 0, sizeof(tio->info));
651 len = min(remaining, max);
653 clone = split_bvec(bio, ci->sector, ci->idx,
654 bv->bv_offset + offset, len);
656 __map_bio(ti, clone, tio);
659 ci->sector_count -= len;
660 offset += to_bytes(len);
661 } while (remaining -= len);
668 * Split the bio into several clones.
670 static void __split_bio(struct mapped_device *md, struct bio *bio)
672 struct clone_info ci;
674 ci.map = dm_get_table(md);
676 bio_io_error(bio, bio->bi_size);
682 ci.io = alloc_io(md);
684 atomic_set(&ci.io->io_count, 1);
687 ci.sector = bio->bi_sector;
688 ci.sector_count = bio_sectors(bio);
689 ci.idx = bio->bi_idx;
691 start_io_acct(ci.io);
692 while (ci.sector_count)
693 __clone_and_map(&ci);
695 /* drop the extra reference count */
696 dec_pending(ci.io, 0);
697 dm_table_put(ci.map);
699 /*-----------------------------------------------------------------
701 *---------------------------------------------------------------*/
704 * The request function that just remaps the bio built up by
707 static int dm_request(request_queue_t *q, struct bio *bio)
710 int rw = bio_data_dir(bio);
711 struct mapped_device *md = q->queuedata;
713 down_read(&md->io_lock);
715 disk_stat_inc(dm_disk(md), ios[rw]);
716 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
719 * If we're suspended we have to queue
722 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
723 up_read(&md->io_lock);
725 if (bio_rw(bio) == READA) {
726 bio_io_error(bio, bio->bi_size);
730 r = queue_io(md, bio);
732 bio_io_error(bio, bio->bi_size);
736 return 0; /* deferred successfully */
739 * We're in a while loop, because someone could suspend
740 * before we get to the following read lock.
742 down_read(&md->io_lock);
745 __split_bio(md, bio);
746 up_read(&md->io_lock);
750 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
751 sector_t *error_sector)
753 struct mapped_device *md = q->queuedata;
754 struct dm_table *map = dm_get_table(md);
758 ret = dm_table_flush_all(map);
765 static void dm_unplug_all(request_queue_t *q)
767 struct mapped_device *md = q->queuedata;
768 struct dm_table *map = dm_get_table(md);
771 dm_table_unplug_all(map);
776 static int dm_any_congested(void *congested_data, int bdi_bits)
779 struct mapped_device *md = (struct mapped_device *) congested_data;
780 struct dm_table *map = dm_get_table(md);
782 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
785 r = dm_table_any_congested(map, bdi_bits);
791 /*-----------------------------------------------------------------
792 * An IDR is used to keep track of allocated minor numbers.
793 *---------------------------------------------------------------*/
794 static DEFINE_IDR(_minor_idr);
796 static void free_minor(int minor)
798 spin_lock(&_minor_lock);
799 idr_remove(&_minor_idr, minor);
800 spin_unlock(&_minor_lock);
804 * See if the device with a specific minor # is free.
806 static int specific_minor(struct mapped_device *md, int minor)
810 if (minor >= (1 << MINORBITS))
813 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
817 spin_lock(&_minor_lock);
819 if (idr_find(&_minor_idr, minor)) {
824 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
829 idr_remove(&_minor_idr, m);
835 spin_unlock(&_minor_lock);
839 static int next_free_minor(struct mapped_device *md, int *minor)
843 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
847 spin_lock(&_minor_lock);
849 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
854 if (m >= (1 << MINORBITS)) {
855 idr_remove(&_minor_idr, m);
863 spin_unlock(&_minor_lock);
867 static struct block_device_operations dm_blk_dops;
870 * Allocate and initialise a blank device with a given minor.
872 static struct mapped_device *alloc_dev(int minor)
875 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
879 DMWARN("unable to allocate device, out of memory.");
883 if (!try_module_get(THIS_MODULE))
886 /* get a minor number for the dev */
887 if (minor == DM_ANY_MINOR)
888 r = next_free_minor(md, &minor);
890 r = specific_minor(md, minor);
894 memset(md, 0, sizeof(*md));
895 init_rwsem(&md->io_lock);
896 init_MUTEX(&md->suspend_lock);
897 rwlock_init(&md->map_lock);
898 atomic_set(&md->holders, 1);
899 atomic_set(&md->open_count, 0);
900 atomic_set(&md->event_nr, 0);
902 md->queue = blk_alloc_queue(GFP_KERNEL);
906 md->queue->queuedata = md;
907 md->queue->backing_dev_info.congested_fn = dm_any_congested;
908 md->queue->backing_dev_info.congested_data = md;
909 blk_queue_make_request(md->queue, dm_request);
910 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
911 md->queue->unplug_fn = dm_unplug_all;
912 md->queue->issue_flush_fn = dm_flush_all;
914 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
918 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
922 md->disk = alloc_disk(1);
926 atomic_set(&md->pending, 0);
927 init_waitqueue_head(&md->wait);
928 init_waitqueue_head(&md->eventq);
930 md->disk->major = _major;
931 md->disk->first_minor = minor;
932 md->disk->fops = &dm_blk_dops;
933 md->disk->queue = md->queue;
934 md->disk->private_data = md;
935 sprintf(md->disk->disk_name, "dm-%d", minor);
937 format_dev_t(md->name, MKDEV(_major, minor));
939 /* Populate the mapping, nobody knows we exist yet */
940 spin_lock(&_minor_lock);
941 old_md = idr_replace(&_minor_idr, md, minor);
942 spin_unlock(&_minor_lock);
944 BUG_ON(old_md != MINOR_ALLOCED);
949 mempool_destroy(md->tio_pool);
951 mempool_destroy(md->io_pool);
953 blk_cleanup_queue(md->queue);
956 module_put(THIS_MODULE);
962 static void free_dev(struct mapped_device *md)
964 int minor = md->disk->first_minor;
966 if (md->suspended_bdev) {
967 thaw_bdev(md->suspended_bdev, NULL);
968 bdput(md->suspended_bdev);
970 mempool_destroy(md->tio_pool);
971 mempool_destroy(md->io_pool);
972 del_gendisk(md->disk);
975 spin_lock(&_minor_lock);
976 md->disk->private_data = NULL;
977 spin_unlock(&_minor_lock);
980 blk_cleanup_queue(md->queue);
981 module_put(THIS_MODULE);
986 * Bind a table to the device.
988 static void event_callback(void *context)
990 struct mapped_device *md = (struct mapped_device *) context;
992 atomic_inc(&md->event_nr);
993 wake_up(&md->eventq);
996 static void __set_size(struct mapped_device *md, sector_t size)
998 set_capacity(md->disk, size);
1000 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1001 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1002 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1005 static int __bind(struct mapped_device *md, struct dm_table *t)
1007 request_queue_t *q = md->queue;
1010 size = dm_table_get_size(t);
1013 * Wipe any geometry if the size of the table changed.
1015 if (size != get_capacity(md->disk))
1016 memset(&md->geometry, 0, sizeof(md->geometry));
1018 __set_size(md, size);
1023 dm_table_event_callback(t, event_callback, md);
1025 write_lock(&md->map_lock);
1027 dm_table_set_restrictions(t, q);
1028 write_unlock(&md->map_lock);
1033 static void __unbind(struct mapped_device *md)
1035 struct dm_table *map = md->map;
1040 dm_table_event_callback(map, NULL, NULL);
1041 write_lock(&md->map_lock);
1043 write_unlock(&md->map_lock);
1048 * Constructor for a new device.
1050 int dm_create(int minor, struct mapped_device **result)
1052 struct mapped_device *md;
1054 md = alloc_dev(minor);
1062 static struct mapped_device *dm_find_md(dev_t dev)
1064 struct mapped_device *md;
1065 unsigned minor = MINOR(dev);
1067 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1070 spin_lock(&_minor_lock);
1072 md = idr_find(&_minor_idr, minor);
1073 if (md && (md == MINOR_ALLOCED ||
1074 (dm_disk(md)->first_minor != minor) ||
1075 test_bit(DMF_FREEING, &md->flags))) {
1081 spin_unlock(&_minor_lock);
1086 struct mapped_device *dm_get_md(dev_t dev)
1088 struct mapped_device *md = dm_find_md(dev);
1096 void *dm_get_mdptr(struct mapped_device *md)
1098 return md->interface_ptr;
1101 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1103 md->interface_ptr = ptr;
1106 void dm_get(struct mapped_device *md)
1108 atomic_inc(&md->holders);
1111 void dm_put(struct mapped_device *md)
1113 struct dm_table *map;
1115 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1117 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1118 map = dm_get_table(md);
1119 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1120 set_bit(DMF_FREEING, &md->flags);
1121 spin_unlock(&_minor_lock);
1122 if (!dm_suspended(md)) {
1123 dm_table_presuspend_targets(map);
1124 dm_table_postsuspend_targets(map);
1133 * Process the deferred bios
1135 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1148 * Swap in a new table (destroying old one).
1150 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1154 down(&md->suspend_lock);
1156 /* device must be suspended */
1157 if (!dm_suspended(md))
1161 r = __bind(md, table);
1164 up(&md->suspend_lock);
1169 * Functions to lock and unlock any filesystem running on the
1172 static int lock_fs(struct mapped_device *md)
1176 WARN_ON(md->frozen_sb);
1178 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1179 if (IS_ERR(md->frozen_sb)) {
1180 r = PTR_ERR(md->frozen_sb);
1181 md->frozen_sb = NULL;
1185 set_bit(DMF_FROZEN, &md->flags);
1187 /* don't bdput right now, we don't want the bdev
1188 * to go away while it is locked.
1193 static void unlock_fs(struct mapped_device *md)
1195 if (!test_bit(DMF_FROZEN, &md->flags))
1198 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1199 md->frozen_sb = NULL;
1200 clear_bit(DMF_FROZEN, &md->flags);
1204 * We need to be able to change a mapping table under a mounted
1205 * filesystem. For example we might want to move some data in
1206 * the background. Before the table can be swapped with
1207 * dm_bind_table, dm_suspend must be called to flush any in
1208 * flight bios and ensure that any further io gets deferred.
1210 int dm_suspend(struct mapped_device *md, int do_lockfs)
1212 struct dm_table *map = NULL;
1213 DECLARE_WAITQUEUE(wait, current);
1217 down(&md->suspend_lock);
1219 if (dm_suspended(md))
1222 map = dm_get_table(md);
1224 /* This does not get reverted if there's an error later. */
1225 dm_table_presuspend_targets(map);
1227 md->suspended_bdev = bdget_disk(md->disk, 0);
1228 if (!md->suspended_bdev) {
1229 DMWARN("bdget failed in dm_suspend");
1234 /* Flush I/O to the device. */
1242 * First we set the BLOCK_IO flag so no more ios will be mapped.
1244 down_write(&md->io_lock);
1245 set_bit(DMF_BLOCK_IO, &md->flags);
1247 add_wait_queue(&md->wait, &wait);
1248 up_write(&md->io_lock);
1252 dm_table_unplug_all(map);
1255 * Then we wait for the already mapped ios to
1259 set_current_state(TASK_INTERRUPTIBLE);
1261 if (!atomic_read(&md->pending) || signal_pending(current))
1266 set_current_state(TASK_RUNNING);
1268 down_write(&md->io_lock);
1269 remove_wait_queue(&md->wait, &wait);
1271 /* were we interrupted ? */
1273 if (atomic_read(&md->pending)) {
1274 clear_bit(DMF_BLOCK_IO, &md->flags);
1275 def = bio_list_get(&md->deferred);
1276 __flush_deferred_io(md, def);
1277 up_write(&md->io_lock);
1281 up_write(&md->io_lock);
1283 dm_table_postsuspend_targets(map);
1285 set_bit(DMF_SUSPENDED, &md->flags);
1290 if (r && md->suspended_bdev) {
1291 bdput(md->suspended_bdev);
1292 md->suspended_bdev = NULL;
1296 up(&md->suspend_lock);
1300 int dm_resume(struct mapped_device *md)
1304 struct dm_table *map = NULL;
1306 down(&md->suspend_lock);
1307 if (!dm_suspended(md))
1310 map = dm_get_table(md);
1311 if (!map || !dm_table_get_size(map))
1314 dm_table_resume_targets(map);
1316 down_write(&md->io_lock);
1317 clear_bit(DMF_BLOCK_IO, &md->flags);
1319 def = bio_list_get(&md->deferred);
1320 __flush_deferred_io(md, def);
1321 up_write(&md->io_lock);
1325 bdput(md->suspended_bdev);
1326 md->suspended_bdev = NULL;
1328 clear_bit(DMF_SUSPENDED, &md->flags);
1330 dm_table_unplug_all(map);
1336 up(&md->suspend_lock);
1341 /*-----------------------------------------------------------------
1342 * Event notification.
1343 *---------------------------------------------------------------*/
1344 uint32_t dm_get_event_nr(struct mapped_device *md)
1346 return atomic_read(&md->event_nr);
1349 int dm_wait_event(struct mapped_device *md, int event_nr)
1351 return wait_event_interruptible(md->eventq,
1352 (event_nr != atomic_read(&md->event_nr)));
1356 * The gendisk is only valid as long as you have a reference
1359 struct gendisk *dm_disk(struct mapped_device *md)
1364 int dm_suspended(struct mapped_device *md)
1366 return test_bit(DMF_SUSPENDED, &md->flags);
1369 static struct block_device_operations dm_blk_dops = {
1370 .open = dm_blk_open,
1371 .release = dm_blk_close,
1372 .getgeo = dm_blk_getgeo,
1373 .owner = THIS_MODULE
1376 EXPORT_SYMBOL(dm_get_mapinfo);
1381 module_init(dm_init);
1382 module_exit(dm_exit);
1384 module_param(major, uint, 0);
1385 MODULE_PARM_DESC(major, "The major number of the device mapper");
1386 MODULE_DESCRIPTION(DM_NAME " driver");
1387 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1388 MODULE_LICENSE("GPL");