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>
23 #include <linux/smp_lock.h>
25 #define DM_MSG_PREFIX "core"
27 static const char *_name = DM_NAME;
29 static unsigned int major = 0;
30 static unsigned int _major = 0;
32 static DEFINE_SPINLOCK(_minor_lock);
34 * One of these is allocated per bio.
37 struct mapped_device *md;
41 unsigned long start_time;
45 * One of these is allocated per target within a bio. Hopefully
46 * this will be simplified out one day.
54 union map_info *dm_get_mapinfo(struct bio *bio)
56 if (bio && bio->bi_private)
57 return &((struct target_io *)bio->bi_private)->info;
61 #define MINOR_ALLOCED ((void *)-1)
64 * Bits for the md->flags field.
66 #define DMF_BLOCK_IO 0
67 #define DMF_SUSPENDED 1
70 #define DMF_DELETING 4
72 struct mapped_device {
73 struct rw_semaphore io_lock;
74 struct semaphore suspend_lock;
81 request_queue_t *queue;
88 * A list of ios that arrived while we were suspended.
91 wait_queue_head_t wait;
92 struct bio_list deferred;
95 * The current mapping.
100 * io objects are allocated from here.
111 wait_queue_head_t eventq;
114 * freeze/thaw support require holding onto a super block
116 struct super_block *frozen_sb;
117 struct block_device *suspended_bdev;
119 /* forced geometry settings */
120 struct hd_geometry geometry;
124 static struct kmem_cache *_io_cache;
125 static struct kmem_cache *_tio_cache;
127 static int __init local_init(void)
131 /* allocate a slab for the dm_ios */
132 _io_cache = kmem_cache_create("dm_io",
133 sizeof(struct dm_io), 0, 0, NULL, NULL);
137 /* allocate a slab for the target ios */
138 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
141 kmem_cache_destroy(_io_cache);
146 r = register_blkdev(_major, _name);
148 kmem_cache_destroy(_tio_cache);
149 kmem_cache_destroy(_io_cache);
159 static void local_exit(void)
161 kmem_cache_destroy(_tio_cache);
162 kmem_cache_destroy(_io_cache);
164 if (unregister_blkdev(_major, _name) < 0)
165 DMERR("unregister_blkdev failed");
169 DMINFO("cleaned up");
172 int (*_inits[])(void) __initdata = {
180 void (*_exits[])(void) = {
188 static int __init dm_init(void)
190 const int count = ARRAY_SIZE(_inits);
194 for (i = 0; i < count; i++) {
209 static void __exit dm_exit(void)
211 int i = ARRAY_SIZE(_exits);
218 * Block device functions
220 static int dm_blk_open(struct inode *inode, struct file *file)
222 struct mapped_device *md;
224 spin_lock(&_minor_lock);
226 md = inode->i_bdev->bd_disk->private_data;
230 if (test_bit(DMF_FREEING, &md->flags) ||
231 test_bit(DMF_DELETING, &md->flags)) {
237 atomic_inc(&md->open_count);
240 spin_unlock(&_minor_lock);
242 return md ? 0 : -ENXIO;
245 static int dm_blk_close(struct inode *inode, struct file *file)
247 struct mapped_device *md;
249 md = inode->i_bdev->bd_disk->private_data;
250 atomic_dec(&md->open_count);
255 int dm_open_count(struct mapped_device *md)
257 return atomic_read(&md->open_count);
261 * Guarantees nothing is using the device before it's deleted.
263 int dm_lock_for_deletion(struct mapped_device *md)
267 spin_lock(&_minor_lock);
269 if (dm_open_count(md))
272 set_bit(DMF_DELETING, &md->flags);
274 spin_unlock(&_minor_lock);
279 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
281 struct mapped_device *md = bdev->bd_disk->private_data;
283 return dm_get_geometry(md, geo);
286 static int dm_blk_ioctl(struct inode *inode, struct file *file,
287 unsigned int cmd, unsigned long arg)
289 struct mapped_device *md;
290 struct dm_table *map;
291 struct dm_target *tgt;
294 /* We don't really need this lock, but we do need 'inode'. */
297 md = inode->i_bdev->bd_disk->private_data;
299 map = dm_get_table(md);
301 if (!map || !dm_table_get_size(map))
304 /* We only support devices that have a single target */
305 if (dm_table_get_num_targets(map) != 1)
308 tgt = dm_table_get_target(map, 0);
310 if (dm_suspended(md)) {
315 if (tgt->type->ioctl)
316 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
325 static inline struct dm_io *alloc_io(struct mapped_device *md)
327 return mempool_alloc(md->io_pool, GFP_NOIO);
330 static inline void free_io(struct mapped_device *md, struct dm_io *io)
332 mempool_free(io, md->io_pool);
335 static inline struct target_io *alloc_tio(struct mapped_device *md)
337 return mempool_alloc(md->tio_pool, GFP_NOIO);
340 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
342 mempool_free(tio, md->tio_pool);
345 static void start_io_acct(struct dm_io *io)
347 struct mapped_device *md = io->md;
349 io->start_time = jiffies;
352 disk_round_stats(dm_disk(md));
354 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
357 static int end_io_acct(struct dm_io *io)
359 struct mapped_device *md = io->md;
360 struct bio *bio = io->bio;
361 unsigned long duration = jiffies - io->start_time;
363 int rw = bio_data_dir(bio);
366 disk_round_stats(dm_disk(md));
368 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
370 disk_stat_add(dm_disk(md), ticks[rw], duration);
376 * Add the bio to the list of deferred io.
378 static int queue_io(struct mapped_device *md, struct bio *bio)
380 down_write(&md->io_lock);
382 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
383 up_write(&md->io_lock);
387 bio_list_add(&md->deferred, bio);
389 up_write(&md->io_lock);
390 return 0; /* deferred successfully */
394 * Everyone (including functions in this file), should use this
395 * function to access the md->map field, and make sure they call
396 * dm_table_put() when finished.
398 struct dm_table *dm_get_table(struct mapped_device *md)
402 read_lock(&md->map_lock);
406 read_unlock(&md->map_lock);
412 * Get the geometry associated with a dm device
414 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
422 * Set the geometry of a device.
424 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
426 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
428 if (geo->start > sz) {
429 DMWARN("Start sector is beyond the geometry limits.");
438 /*-----------------------------------------------------------------
440 * A more elegant soln is in the works that uses the queue
441 * merge fn, unfortunately there are a couple of changes to
442 * the block layer that I want to make for this. So in the
443 * interests of getting something for people to use I give
444 * you this clearly demarcated crap.
445 *---------------------------------------------------------------*/
448 * Decrements the number of outstanding ios that a bio has been
449 * cloned into, completing the original io if necc.
451 static void dec_pending(struct dm_io *io, int error)
456 if (atomic_dec_and_test(&io->io_count)) {
458 /* nudge anyone waiting on suspend queue */
459 wake_up(&io->md->wait);
461 blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
463 bio_endio(io->bio, io->bio->bi_size, io->error);
468 static int clone_endio(struct bio *bio, unsigned int done, int error)
471 struct target_io *tio = bio->bi_private;
472 struct mapped_device *md = tio->io->md;
473 dm_endio_fn endio = tio->ti->type->end_io;
478 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
482 r = endio(tio->ti, bio, error, &tio->info);
485 else if (r == DM_ENDIO_INCOMPLETE)
486 /* The target will handle the io */
489 DMWARN("unimplemented target endio return value: %d", r);
494 dec_pending(tio->io, error);
497 * Store md for cleanup instead of tio which is about to get freed.
499 bio->bi_private = md->bs;
506 static sector_t max_io_len(struct mapped_device *md,
507 sector_t sector, struct dm_target *ti)
509 sector_t offset = sector - ti->begin;
510 sector_t len = ti->len - offset;
513 * Does the target need to split even further ?
517 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
526 static void __map_bio(struct dm_target *ti, struct bio *clone,
527 struct target_io *tio)
531 struct mapped_device *md;
536 BUG_ON(!clone->bi_size);
538 clone->bi_end_io = clone_endio;
539 clone->bi_private = tio;
542 * Map the clone. If r == 0 we don't need to do
543 * anything, the target has assumed ownership of
546 atomic_inc(&tio->io->io_count);
547 sector = clone->bi_sector;
548 r = ti->type->map(ti, clone, &tio->info);
549 if (r == DM_MAPIO_REMAPPED) {
550 /* the bio has been remapped so dispatch it */
552 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
553 tio->io->bio->bi_bdev->bd_dev, sector,
556 generic_make_request(clone);
558 /* error the io and bail out */
560 dec_pending(tio->io, r);
562 * Store bio_set for cleanup.
564 clone->bi_private = md->bs;
568 DMWARN("unimplemented target map return value: %d", r);
574 struct mapped_device *md;
575 struct dm_table *map;
579 sector_t sector_count;
583 static void dm_bio_destructor(struct bio *bio)
585 struct bio_set *bs = bio->bi_private;
591 * Creates a little bio that is just does part of a bvec.
593 static struct bio *split_bvec(struct bio *bio, sector_t sector,
594 unsigned short idx, unsigned int offset,
595 unsigned int len, struct bio_set *bs)
598 struct bio_vec *bv = bio->bi_io_vec + idx;
600 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
601 clone->bi_destructor = dm_bio_destructor;
602 *clone->bi_io_vec = *bv;
604 clone->bi_sector = sector;
605 clone->bi_bdev = bio->bi_bdev;
606 clone->bi_rw = bio->bi_rw;
608 clone->bi_size = to_bytes(len);
609 clone->bi_io_vec->bv_offset = offset;
610 clone->bi_io_vec->bv_len = clone->bi_size;
616 * Creates a bio that consists of range of complete bvecs.
618 static struct bio *clone_bio(struct bio *bio, sector_t sector,
619 unsigned short idx, unsigned short bv_count,
620 unsigned int len, struct bio_set *bs)
624 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
625 __bio_clone(clone, bio);
626 clone->bi_destructor = dm_bio_destructor;
627 clone->bi_sector = sector;
629 clone->bi_vcnt = idx + bv_count;
630 clone->bi_size = to_bytes(len);
631 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
636 static void __clone_and_map(struct clone_info *ci)
638 struct bio *clone, *bio = ci->bio;
639 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
640 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
641 struct target_io *tio;
644 * Allocate a target io object.
646 tio = alloc_tio(ci->md);
649 memset(&tio->info, 0, sizeof(tio->info));
651 if (ci->sector_count <= max) {
653 * Optimise for the simple case where we can do all of
654 * the remaining io with a single clone.
656 clone = clone_bio(bio, ci->sector, ci->idx,
657 bio->bi_vcnt - ci->idx, ci->sector_count,
659 __map_bio(ti, clone, tio);
660 ci->sector_count = 0;
662 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
664 * There are some bvecs that don't span targets.
665 * Do as many of these as possible.
668 sector_t remaining = max;
671 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
672 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
674 if (bv_len > remaining)
681 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
683 __map_bio(ti, clone, tio);
686 ci->sector_count -= len;
691 * Handle a bvec that must be split between two or more targets.
693 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
694 sector_t remaining = to_sector(bv->bv_len);
695 unsigned int offset = 0;
699 ti = dm_table_find_target(ci->map, ci->sector);
700 max = max_io_len(ci->md, ci->sector, ti);
702 tio = alloc_tio(ci->md);
705 memset(&tio->info, 0, sizeof(tio->info));
708 len = min(remaining, max);
710 clone = split_bvec(bio, ci->sector, ci->idx,
711 bv->bv_offset + offset, len,
714 __map_bio(ti, clone, tio);
717 ci->sector_count -= len;
718 offset += to_bytes(len);
719 } while (remaining -= len);
726 * Split the bio into several clones.
728 static void __split_bio(struct mapped_device *md, struct bio *bio)
730 struct clone_info ci;
732 ci.map = dm_get_table(md);
734 bio_io_error(bio, bio->bi_size);
740 ci.io = alloc_io(md);
742 atomic_set(&ci.io->io_count, 1);
745 ci.sector = bio->bi_sector;
746 ci.sector_count = bio_sectors(bio);
747 ci.idx = bio->bi_idx;
749 start_io_acct(ci.io);
750 while (ci.sector_count)
751 __clone_and_map(&ci);
753 /* drop the extra reference count */
754 dec_pending(ci.io, 0);
755 dm_table_put(ci.map);
757 /*-----------------------------------------------------------------
759 *---------------------------------------------------------------*/
762 * The request function that just remaps the bio built up by
765 static int dm_request(request_queue_t *q, struct bio *bio)
768 int rw = bio_data_dir(bio);
769 struct mapped_device *md = q->queuedata;
771 down_read(&md->io_lock);
773 disk_stat_inc(dm_disk(md), ios[rw]);
774 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
777 * If we're suspended we have to queue
780 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
781 up_read(&md->io_lock);
783 if (bio_rw(bio) == READA) {
784 bio_io_error(bio, bio->bi_size);
788 r = queue_io(md, bio);
790 bio_io_error(bio, bio->bi_size);
794 return 0; /* deferred successfully */
797 * We're in a while loop, because someone could suspend
798 * before we get to the following read lock.
800 down_read(&md->io_lock);
803 __split_bio(md, bio);
804 up_read(&md->io_lock);
808 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
809 sector_t *error_sector)
811 struct mapped_device *md = q->queuedata;
812 struct dm_table *map = dm_get_table(md);
816 ret = dm_table_flush_all(map);
823 static void dm_unplug_all(request_queue_t *q)
825 struct mapped_device *md = q->queuedata;
826 struct dm_table *map = dm_get_table(md);
829 dm_table_unplug_all(map);
834 static int dm_any_congested(void *congested_data, int bdi_bits)
837 struct mapped_device *md = (struct mapped_device *) congested_data;
838 struct dm_table *map = dm_get_table(md);
840 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
843 r = dm_table_any_congested(map, bdi_bits);
849 /*-----------------------------------------------------------------
850 * An IDR is used to keep track of allocated minor numbers.
851 *---------------------------------------------------------------*/
852 static DEFINE_IDR(_minor_idr);
854 static void free_minor(int minor)
856 spin_lock(&_minor_lock);
857 idr_remove(&_minor_idr, minor);
858 spin_unlock(&_minor_lock);
862 * See if the device with a specific minor # is free.
864 static int specific_minor(struct mapped_device *md, int minor)
868 if (minor >= (1 << MINORBITS))
871 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
875 spin_lock(&_minor_lock);
877 if (idr_find(&_minor_idr, minor)) {
882 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
887 idr_remove(&_minor_idr, m);
893 spin_unlock(&_minor_lock);
897 static int next_free_minor(struct mapped_device *md, int *minor)
901 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
905 spin_lock(&_minor_lock);
907 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
912 if (m >= (1 << MINORBITS)) {
913 idr_remove(&_minor_idr, m);
921 spin_unlock(&_minor_lock);
925 static struct block_device_operations dm_blk_dops;
928 * Allocate and initialise a blank device with a given minor.
930 static struct mapped_device *alloc_dev(int minor)
933 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
937 DMWARN("unable to allocate device, out of memory.");
941 if (!try_module_get(THIS_MODULE))
944 /* get a minor number for the dev */
945 if (minor == DM_ANY_MINOR)
946 r = next_free_minor(md, &minor);
948 r = specific_minor(md, minor);
952 memset(md, 0, sizeof(*md));
953 init_rwsem(&md->io_lock);
954 init_MUTEX(&md->suspend_lock);
955 rwlock_init(&md->map_lock);
956 atomic_set(&md->holders, 1);
957 atomic_set(&md->open_count, 0);
958 atomic_set(&md->event_nr, 0);
960 md->queue = blk_alloc_queue(GFP_KERNEL);
962 goto bad1_free_minor;
964 md->queue->queuedata = md;
965 md->queue->backing_dev_info.congested_fn = dm_any_congested;
966 md->queue->backing_dev_info.congested_data = md;
967 blk_queue_make_request(md->queue, dm_request);
968 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
969 md->queue->unplug_fn = dm_unplug_all;
970 md->queue->issue_flush_fn = dm_flush_all;
972 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
976 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
980 md->bs = bioset_create(16, 16, 4);
984 md->disk = alloc_disk(1);
988 atomic_set(&md->pending, 0);
989 init_waitqueue_head(&md->wait);
990 init_waitqueue_head(&md->eventq);
992 md->disk->major = _major;
993 md->disk->first_minor = minor;
994 md->disk->fops = &dm_blk_dops;
995 md->disk->queue = md->queue;
996 md->disk->private_data = md;
997 sprintf(md->disk->disk_name, "dm-%d", minor);
999 format_dev_t(md->name, MKDEV(_major, minor));
1001 /* Populate the mapping, nobody knows we exist yet */
1002 spin_lock(&_minor_lock);
1003 old_md = idr_replace(&_minor_idr, md, minor);
1004 spin_unlock(&_minor_lock);
1006 BUG_ON(old_md != MINOR_ALLOCED);
1011 bioset_free(md->bs);
1013 mempool_destroy(md->tio_pool);
1015 mempool_destroy(md->io_pool);
1017 blk_cleanup_queue(md->queue);
1021 module_put(THIS_MODULE);
1027 static void free_dev(struct mapped_device *md)
1029 int minor = md->disk->first_minor;
1031 if (md->suspended_bdev) {
1032 thaw_bdev(md->suspended_bdev, NULL);
1033 bdput(md->suspended_bdev);
1035 mempool_destroy(md->tio_pool);
1036 mempool_destroy(md->io_pool);
1037 bioset_free(md->bs);
1038 del_gendisk(md->disk);
1041 spin_lock(&_minor_lock);
1042 md->disk->private_data = NULL;
1043 spin_unlock(&_minor_lock);
1046 blk_cleanup_queue(md->queue);
1047 module_put(THIS_MODULE);
1052 * Bind a table to the device.
1054 static void event_callback(void *context)
1056 struct mapped_device *md = (struct mapped_device *) context;
1058 atomic_inc(&md->event_nr);
1059 wake_up(&md->eventq);
1062 static void __set_size(struct mapped_device *md, sector_t size)
1064 set_capacity(md->disk, size);
1066 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1067 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1068 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1071 static int __bind(struct mapped_device *md, struct dm_table *t)
1073 request_queue_t *q = md->queue;
1076 size = dm_table_get_size(t);
1079 * Wipe any geometry if the size of the table changed.
1081 if (size != get_capacity(md->disk))
1082 memset(&md->geometry, 0, sizeof(md->geometry));
1084 __set_size(md, size);
1089 dm_table_event_callback(t, event_callback, md);
1091 write_lock(&md->map_lock);
1093 dm_table_set_restrictions(t, q);
1094 write_unlock(&md->map_lock);
1099 static void __unbind(struct mapped_device *md)
1101 struct dm_table *map = md->map;
1106 dm_table_event_callback(map, NULL, NULL);
1107 write_lock(&md->map_lock);
1109 write_unlock(&md->map_lock);
1114 * Constructor for a new device.
1116 int dm_create(int minor, struct mapped_device **result)
1118 struct mapped_device *md;
1120 md = alloc_dev(minor);
1128 static struct mapped_device *dm_find_md(dev_t dev)
1130 struct mapped_device *md;
1131 unsigned minor = MINOR(dev);
1133 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1136 spin_lock(&_minor_lock);
1138 md = idr_find(&_minor_idr, minor);
1139 if (md && (md == MINOR_ALLOCED ||
1140 (dm_disk(md)->first_minor != minor) ||
1141 test_bit(DMF_FREEING, &md->flags))) {
1147 spin_unlock(&_minor_lock);
1152 struct mapped_device *dm_get_md(dev_t dev)
1154 struct mapped_device *md = dm_find_md(dev);
1162 void *dm_get_mdptr(struct mapped_device *md)
1164 return md->interface_ptr;
1167 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1169 md->interface_ptr = ptr;
1172 void dm_get(struct mapped_device *md)
1174 atomic_inc(&md->holders);
1177 const char *dm_device_name(struct mapped_device *md)
1181 EXPORT_SYMBOL_GPL(dm_device_name);
1183 void dm_put(struct mapped_device *md)
1185 struct dm_table *map;
1187 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1189 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1190 map = dm_get_table(md);
1191 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1192 set_bit(DMF_FREEING, &md->flags);
1193 spin_unlock(&_minor_lock);
1194 if (!dm_suspended(md)) {
1195 dm_table_presuspend_targets(map);
1196 dm_table_postsuspend_targets(map);
1205 * Process the deferred bios
1207 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1220 * Swap in a new table (destroying old one).
1222 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1226 down(&md->suspend_lock);
1228 /* device must be suspended */
1229 if (!dm_suspended(md))
1233 r = __bind(md, table);
1236 up(&md->suspend_lock);
1241 * Functions to lock and unlock any filesystem running on the
1244 static int lock_fs(struct mapped_device *md)
1248 WARN_ON(md->frozen_sb);
1250 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1251 if (IS_ERR(md->frozen_sb)) {
1252 r = PTR_ERR(md->frozen_sb);
1253 md->frozen_sb = NULL;
1257 set_bit(DMF_FROZEN, &md->flags);
1259 /* don't bdput right now, we don't want the bdev
1260 * to go away while it is locked.
1265 static void unlock_fs(struct mapped_device *md)
1267 if (!test_bit(DMF_FROZEN, &md->flags))
1270 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1271 md->frozen_sb = NULL;
1272 clear_bit(DMF_FROZEN, &md->flags);
1276 * We need to be able to change a mapping table under a mounted
1277 * filesystem. For example we might want to move some data in
1278 * the background. Before the table can be swapped with
1279 * dm_bind_table, dm_suspend must be called to flush any in
1280 * flight bios and ensure that any further io gets deferred.
1282 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1284 struct dm_table *map = NULL;
1285 DECLARE_WAITQUEUE(wait, current);
1288 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1290 down(&md->suspend_lock);
1292 if (dm_suspended(md))
1295 map = dm_get_table(md);
1297 /* This does not get reverted if there's an error later. */
1298 dm_table_presuspend_targets(map);
1300 md->suspended_bdev = bdget_disk(md->disk, 0);
1301 if (!md->suspended_bdev) {
1302 DMWARN("bdget failed in dm_suspend");
1307 /* Flush I/O to the device. */
1315 * First we set the BLOCK_IO flag so no more ios will be mapped.
1317 down_write(&md->io_lock);
1318 set_bit(DMF_BLOCK_IO, &md->flags);
1320 add_wait_queue(&md->wait, &wait);
1321 up_write(&md->io_lock);
1325 dm_table_unplug_all(map);
1328 * Then we wait for the already mapped ios to
1332 set_current_state(TASK_INTERRUPTIBLE);
1334 if (!atomic_read(&md->pending) || signal_pending(current))
1339 set_current_state(TASK_RUNNING);
1341 down_write(&md->io_lock);
1342 remove_wait_queue(&md->wait, &wait);
1344 /* were we interrupted ? */
1346 if (atomic_read(&md->pending)) {
1347 clear_bit(DMF_BLOCK_IO, &md->flags);
1348 def = bio_list_get(&md->deferred);
1349 __flush_deferred_io(md, def);
1350 up_write(&md->io_lock);
1354 up_write(&md->io_lock);
1356 dm_table_postsuspend_targets(map);
1358 set_bit(DMF_SUSPENDED, &md->flags);
1363 if (r && md->suspended_bdev) {
1364 bdput(md->suspended_bdev);
1365 md->suspended_bdev = NULL;
1371 up(&md->suspend_lock);
1375 int dm_resume(struct mapped_device *md)
1379 struct dm_table *map = NULL;
1381 down(&md->suspend_lock);
1382 if (!dm_suspended(md))
1385 map = dm_get_table(md);
1386 if (!map || !dm_table_get_size(map))
1389 r = dm_table_resume_targets(map);
1393 down_write(&md->io_lock);
1394 clear_bit(DMF_BLOCK_IO, &md->flags);
1396 def = bio_list_get(&md->deferred);
1397 __flush_deferred_io(md, def);
1398 up_write(&md->io_lock);
1402 bdput(md->suspended_bdev);
1403 md->suspended_bdev = NULL;
1405 clear_bit(DMF_SUSPENDED, &md->flags);
1407 dm_table_unplug_all(map);
1409 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1415 up(&md->suspend_lock);
1420 /*-----------------------------------------------------------------
1421 * Event notification.
1422 *---------------------------------------------------------------*/
1423 uint32_t dm_get_event_nr(struct mapped_device *md)
1425 return atomic_read(&md->event_nr);
1428 int dm_wait_event(struct mapped_device *md, int event_nr)
1430 return wait_event_interruptible(md->eventq,
1431 (event_nr != atomic_read(&md->event_nr)));
1435 * The gendisk is only valid as long as you have a reference
1438 struct gendisk *dm_disk(struct mapped_device *md)
1443 int dm_suspended(struct mapped_device *md)
1445 return test_bit(DMF_SUSPENDED, &md->flags);
1448 static struct block_device_operations dm_blk_dops = {
1449 .open = dm_blk_open,
1450 .release = dm_blk_close,
1451 .ioctl = dm_blk_ioctl,
1452 .getgeo = dm_blk_getgeo,
1453 .owner = THIS_MODULE
1456 EXPORT_SYMBOL(dm_get_mapinfo);
1461 module_init(dm_init);
1462 module_exit(dm_exit);
1464 module_param(major, uint, 0);
1465 MODULE_PARM_DESC(major, "The major number of the device mapper");
1466 MODULE_DESCRIPTION(DM_NAME " driver");
1467 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1468 MODULE_LICENSE("GPL");