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>
24 #include <linux/vs_base.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 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;
85 request_queue_t *queue;
92 * A list of ios that arrived while we were suspended.
95 wait_queue_head_t wait;
96 struct bio_list deferred;
97 struct bio_list pushback;
100 * The current mapping.
102 struct dm_table *map;
105 * io objects are allocated from here.
116 wait_queue_head_t eventq;
119 * freeze/thaw support require holding onto a super block
121 struct super_block *frozen_sb;
122 struct block_device *suspended_bdev;
124 /* forced geometry settings */
125 struct hd_geometry geometry;
129 static struct kmem_cache *_io_cache;
130 static struct kmem_cache *_tio_cache;
132 static int __init local_init(void)
136 /* allocate a slab for the dm_ios */
137 _io_cache = kmem_cache_create("dm_io",
138 sizeof(struct dm_io), 0, 0, NULL, NULL);
142 /* allocate a slab for the target ios */
143 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
146 kmem_cache_destroy(_io_cache);
151 r = register_blkdev(_major, _name);
153 kmem_cache_destroy(_tio_cache);
154 kmem_cache_destroy(_io_cache);
164 static void local_exit(void)
166 kmem_cache_destroy(_tio_cache);
167 kmem_cache_destroy(_io_cache);
169 if (unregister_blkdev(_major, _name) < 0)
170 DMERR("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;
230 spin_lock(&_minor_lock);
232 md = inode->i_bdev->bd_disk->private_data;
236 if (test_bit(DMF_FREEING, &md->flags) ||
237 test_bit(DMF_DELETING, &md->flags))
241 if (!vx_check(md->xid, VS_IDENT|VS_HOSTID))
245 atomic_inc(&md->open_count);
248 spin_unlock(&_minor_lock);
252 static int dm_blk_close(struct inode *inode, struct file *file)
254 struct mapped_device *md;
256 md = inode->i_bdev->bd_disk->private_data;
257 atomic_dec(&md->open_count);
262 int dm_open_count(struct mapped_device *md)
264 return atomic_read(&md->open_count);
268 * Guarantees nothing is using the device before it's deleted.
270 int dm_lock_for_deletion(struct mapped_device *md)
274 spin_lock(&_minor_lock);
276 if (dm_open_count(md))
279 set_bit(DMF_DELETING, &md->flags);
281 spin_unlock(&_minor_lock);
286 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
288 struct mapped_device *md = bdev->bd_disk->private_data;
290 return dm_get_geometry(md, geo);
293 static int dm_blk_ioctl(struct inode *inode, struct file *file,
294 unsigned int cmd, unsigned long arg)
296 struct mapped_device *md;
297 struct dm_table *map;
298 struct dm_target *tgt;
301 /* We don't really need this lock, but we do need 'inode'. */
304 md = inode->i_bdev->bd_disk->private_data;
306 map = dm_get_table(md);
308 if (!map || !dm_table_get_size(map))
311 /* We only support devices that have a single target */
312 if (dm_table_get_num_targets(map) != 1)
315 tgt = dm_table_get_target(map, 0);
317 if (dm_suspended(md)) {
322 if (tgt->type->ioctl)
323 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
332 static inline struct dm_io *alloc_io(struct mapped_device *md)
334 return mempool_alloc(md->io_pool, GFP_NOIO);
337 static inline void free_io(struct mapped_device *md, struct dm_io *io)
339 mempool_free(io, md->io_pool);
342 static inline struct target_io *alloc_tio(struct mapped_device *md)
344 return mempool_alloc(md->tio_pool, GFP_NOIO);
347 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
349 mempool_free(tio, md->tio_pool);
352 static void start_io_acct(struct dm_io *io)
354 struct mapped_device *md = io->md;
356 io->start_time = jiffies;
359 disk_round_stats(dm_disk(md));
361 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
364 static int end_io_acct(struct dm_io *io)
366 struct mapped_device *md = io->md;
367 struct bio *bio = io->bio;
368 unsigned long duration = jiffies - io->start_time;
370 int rw = bio_data_dir(bio);
373 disk_round_stats(dm_disk(md));
375 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
377 disk_stat_add(dm_disk(md), ticks[rw], duration);
383 * Add the bio to the list of deferred io.
385 static int queue_io(struct mapped_device *md, struct bio *bio)
387 down_write(&md->io_lock);
389 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
390 up_write(&md->io_lock);
394 bio_list_add(&md->deferred, bio);
396 up_write(&md->io_lock);
397 return 0; /* deferred successfully */
401 * Everyone (including functions in this file), should use this
402 * function to access the md->map field, and make sure they call
403 * dm_table_put() when finished.
405 struct dm_table *dm_get_table(struct mapped_device *md)
409 read_lock(&md->map_lock);
413 read_unlock(&md->map_lock);
419 * Get the geometry associated with a dm device
421 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
429 * Set the geometry of a device.
431 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
433 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
435 if (geo->start > sz) {
436 DMWARN("Start sector is beyond the geometry limits.");
446 * Get the xid associated with a dm device
448 xid_t dm_get_xid(struct mapped_device *md)
453 /*-----------------------------------------------------------------
455 * A more elegant soln is in the works that uses the queue
456 * merge fn, unfortunately there are a couple of changes to
457 * the block layer that I want to make for this. So in the
458 * interests of getting something for people to use I give
459 * you this clearly demarcated crap.
460 *---------------------------------------------------------------*/
462 static int __noflush_suspending(struct mapped_device *md)
464 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
468 * Decrements the number of outstanding ios that a bio has been
469 * cloned into, completing the original io if necc.
471 static void dec_pending(struct dm_io *io, int error)
475 /* Push-back supersedes any I/O errors */
476 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
479 if (atomic_dec_and_test(&io->io_count)) {
480 if (io->error == DM_ENDIO_REQUEUE) {
482 * Target requested pushing back the I/O.
483 * This must be handled before the sleeper on
484 * suspend queue merges the pushback list.
486 spin_lock_irqsave(&io->md->pushback_lock, flags);
487 if (__noflush_suspending(io->md))
488 bio_list_add(&io->md->pushback, io->bio);
490 /* noflush suspend was interrupted. */
492 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
496 /* nudge anyone waiting on suspend queue */
497 wake_up(&io->md->wait);
499 if (io->error != DM_ENDIO_REQUEUE) {
500 blk_add_trace_bio(io->md->queue, io->bio,
503 bio_endio(io->bio, io->bio->bi_size, io->error);
510 static int clone_endio(struct bio *bio, unsigned int done, int error)
513 struct target_io *tio = bio->bi_private;
514 struct mapped_device *md = tio->io->md;
515 dm_endio_fn endio = tio->ti->type->end_io;
520 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
524 r = endio(tio->ti, bio, error, &tio->info);
525 if (r < 0 || r == DM_ENDIO_REQUEUE)
527 * error and requeue request are handled
531 else if (r == DM_ENDIO_INCOMPLETE)
532 /* The target will handle the io */
535 DMWARN("unimplemented target endio return value: %d", r);
540 dec_pending(tio->io, error);
543 * Store md for cleanup instead of tio which is about to get freed.
545 bio->bi_private = md->bs;
552 static sector_t max_io_len(struct mapped_device *md,
553 sector_t sector, struct dm_target *ti)
555 sector_t offset = sector - ti->begin;
556 sector_t len = ti->len - offset;
559 * Does the target need to split even further ?
563 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
572 static void __map_bio(struct dm_target *ti, struct bio *clone,
573 struct target_io *tio)
577 struct mapped_device *md;
582 BUG_ON(!clone->bi_size);
584 clone->bi_end_io = clone_endio;
585 clone->bi_private = tio;
588 * Map the clone. If r == 0 we don't need to do
589 * anything, the target has assumed ownership of
592 atomic_inc(&tio->io->io_count);
593 sector = clone->bi_sector;
594 r = ti->type->map(ti, clone, &tio->info);
595 if (r == DM_MAPIO_REMAPPED) {
596 /* the bio has been remapped so dispatch it */
598 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
599 tio->io->bio->bi_bdev->bd_dev, sector,
602 generic_make_request(clone);
603 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
604 /* error the io and bail out, or requeue it if needed */
606 dec_pending(tio->io, r);
608 * Store bio_set for cleanup.
610 clone->bi_private = md->bs;
614 DMWARN("unimplemented target map return value: %d", r);
620 struct mapped_device *md;
621 struct dm_table *map;
625 sector_t sector_count;
629 static void dm_bio_destructor(struct bio *bio)
631 struct bio_set *bs = bio->bi_private;
637 * Creates a little bio that is just does part of a bvec.
639 static struct bio *split_bvec(struct bio *bio, sector_t sector,
640 unsigned short idx, unsigned int offset,
641 unsigned int len, struct bio_set *bs)
644 struct bio_vec *bv = bio->bi_io_vec + idx;
646 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
647 clone->bi_destructor = dm_bio_destructor;
648 *clone->bi_io_vec = *bv;
650 clone->bi_sector = sector;
651 clone->bi_bdev = bio->bi_bdev;
652 clone->bi_rw = bio->bi_rw;
654 clone->bi_size = to_bytes(len);
655 clone->bi_io_vec->bv_offset = offset;
656 clone->bi_io_vec->bv_len = clone->bi_size;
662 * Creates a bio that consists of range of complete bvecs.
664 static struct bio *clone_bio(struct bio *bio, sector_t sector,
665 unsigned short idx, unsigned short bv_count,
666 unsigned int len, struct bio_set *bs)
670 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
671 __bio_clone(clone, bio);
672 clone->bi_destructor = dm_bio_destructor;
673 clone->bi_sector = sector;
675 clone->bi_vcnt = idx + bv_count;
676 clone->bi_size = to_bytes(len);
677 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
682 static void __clone_and_map(struct clone_info *ci)
684 struct bio *clone, *bio = ci->bio;
685 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
686 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
687 struct target_io *tio;
690 * Allocate a target io object.
692 tio = alloc_tio(ci->md);
695 memset(&tio->info, 0, sizeof(tio->info));
697 if (ci->sector_count <= max) {
699 * Optimise for the simple case where we can do all of
700 * the remaining io with a single clone.
702 clone = clone_bio(bio, ci->sector, ci->idx,
703 bio->bi_vcnt - ci->idx, ci->sector_count,
705 __map_bio(ti, clone, tio);
706 ci->sector_count = 0;
708 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
710 * There are some bvecs that don't span targets.
711 * Do as many of these as possible.
714 sector_t remaining = max;
717 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
718 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
720 if (bv_len > remaining)
727 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
729 __map_bio(ti, clone, tio);
732 ci->sector_count -= len;
737 * Handle a bvec that must be split between two or more targets.
739 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
740 sector_t remaining = to_sector(bv->bv_len);
741 unsigned int offset = 0;
745 ti = dm_table_find_target(ci->map, ci->sector);
746 max = max_io_len(ci->md, ci->sector, ti);
748 tio = alloc_tio(ci->md);
751 memset(&tio->info, 0, sizeof(tio->info));
754 len = min(remaining, max);
756 clone = split_bvec(bio, ci->sector, ci->idx,
757 bv->bv_offset + offset, len,
760 __map_bio(ti, clone, tio);
763 ci->sector_count -= len;
764 offset += to_bytes(len);
765 } while (remaining -= len);
772 * Split the bio into several clones.
774 static void __split_bio(struct mapped_device *md, struct bio *bio)
776 struct clone_info ci;
778 ci.map = dm_get_table(md);
780 bio_io_error(bio, bio->bi_size);
786 ci.io = alloc_io(md);
788 atomic_set(&ci.io->io_count, 1);
791 ci.sector = bio->bi_sector;
792 ci.sector_count = bio_sectors(bio);
793 ci.idx = bio->bi_idx;
795 start_io_acct(ci.io);
796 while (ci.sector_count)
797 __clone_and_map(&ci);
799 /* drop the extra reference count */
800 dec_pending(ci.io, 0);
801 dm_table_put(ci.map);
803 /*-----------------------------------------------------------------
805 *---------------------------------------------------------------*/
808 * The request function that just remaps the bio built up by
811 static int dm_request(request_queue_t *q, struct bio *bio)
814 int rw = bio_data_dir(bio);
815 struct mapped_device *md = q->queuedata;
817 down_read(&md->io_lock);
819 disk_stat_inc(dm_disk(md), ios[rw]);
820 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
823 * If we're suspended we have to queue
826 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
827 up_read(&md->io_lock);
829 if (bio_rw(bio) == READA) {
830 bio_io_error(bio, bio->bi_size);
834 r = queue_io(md, bio);
836 bio_io_error(bio, bio->bi_size);
840 return 0; /* deferred successfully */
843 * We're in a while loop, because someone could suspend
844 * before we get to the following read lock.
846 down_read(&md->io_lock);
849 __split_bio(md, bio);
850 up_read(&md->io_lock);
854 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
855 sector_t *error_sector)
857 struct mapped_device *md = q->queuedata;
858 struct dm_table *map = dm_get_table(md);
862 ret = dm_table_flush_all(map);
869 static void dm_unplug_all(request_queue_t *q)
871 struct mapped_device *md = q->queuedata;
872 struct dm_table *map = dm_get_table(md);
875 dm_table_unplug_all(map);
880 static int dm_any_congested(void *congested_data, int bdi_bits)
883 struct mapped_device *md = (struct mapped_device *) congested_data;
884 struct dm_table *map = dm_get_table(md);
886 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
889 r = dm_table_any_congested(map, bdi_bits);
895 /*-----------------------------------------------------------------
896 * An IDR is used to keep track of allocated minor numbers.
897 *---------------------------------------------------------------*/
898 static DEFINE_IDR(_minor_idr);
900 static void free_minor(int minor)
902 spin_lock(&_minor_lock);
903 idr_remove(&_minor_idr, minor);
904 spin_unlock(&_minor_lock);
908 * See if the device with a specific minor # is free.
910 static int specific_minor(struct mapped_device *md, int minor)
914 if (minor >= (1 << MINORBITS))
917 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
921 spin_lock(&_minor_lock);
923 if (idr_find(&_minor_idr, minor)) {
928 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
933 idr_remove(&_minor_idr, m);
939 spin_unlock(&_minor_lock);
943 static int next_free_minor(struct mapped_device *md, int *minor)
947 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
951 spin_lock(&_minor_lock);
953 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
958 if (m >= (1 << MINORBITS)) {
959 idr_remove(&_minor_idr, m);
967 spin_unlock(&_minor_lock);
971 static struct block_device_operations dm_blk_dops;
974 * Allocate and initialise a blank device with a given minor.
976 static struct mapped_device *alloc_dev(int minor)
979 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
983 DMWARN("unable to allocate device, out of memory.");
987 if (!try_module_get(THIS_MODULE))
990 /* get a minor number for the dev */
991 if (minor == DM_ANY_MINOR)
992 r = next_free_minor(md, &minor);
994 r = specific_minor(md, minor);
998 memset(md, 0, sizeof(*md));
999 init_rwsem(&md->io_lock);
1000 init_MUTEX(&md->suspend_lock);
1001 spin_lock_init(&md->pushback_lock);
1002 rwlock_init(&md->map_lock);
1003 atomic_set(&md->holders, 1);
1004 atomic_set(&md->open_count, 0);
1005 atomic_set(&md->event_nr, 0);
1006 md->xid = vx_current_xid();
1008 md->queue = blk_alloc_queue(GFP_KERNEL);
1010 goto bad1_free_minor;
1012 md->queue->queuedata = md;
1013 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1014 md->queue->backing_dev_info.congested_data = md;
1015 blk_queue_make_request(md->queue, dm_request);
1016 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1017 md->queue->unplug_fn = dm_unplug_all;
1018 md->queue->issue_flush_fn = dm_flush_all;
1020 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
1024 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1028 md->bs = bioset_create(16, 16, 4);
1032 md->disk = alloc_disk(1);
1036 atomic_set(&md->pending, 0);
1037 init_waitqueue_head(&md->wait);
1038 init_waitqueue_head(&md->eventq);
1040 md->disk->major = _major;
1041 md->disk->first_minor = minor;
1042 md->disk->fops = &dm_blk_dops;
1043 md->disk->queue = md->queue;
1044 md->disk->private_data = md;
1045 sprintf(md->disk->disk_name, "dm-%d", minor);
1047 format_dev_t(md->name, MKDEV(_major, minor));
1049 /* Populate the mapping, nobody knows we exist yet */
1050 spin_lock(&_minor_lock);
1051 old_md = idr_replace(&_minor_idr, md, minor);
1052 spin_unlock(&_minor_lock);
1054 BUG_ON(old_md != MINOR_ALLOCED);
1059 bioset_free(md->bs);
1061 mempool_destroy(md->tio_pool);
1063 mempool_destroy(md->io_pool);
1065 blk_cleanup_queue(md->queue);
1069 module_put(THIS_MODULE);
1075 static void free_dev(struct mapped_device *md)
1077 int minor = md->disk->first_minor;
1079 if (md->suspended_bdev) {
1080 thaw_bdev(md->suspended_bdev, NULL);
1081 bdput(md->suspended_bdev);
1083 mempool_destroy(md->tio_pool);
1084 mempool_destroy(md->io_pool);
1085 bioset_free(md->bs);
1086 del_gendisk(md->disk);
1089 spin_lock(&_minor_lock);
1090 md->disk->private_data = NULL;
1091 spin_unlock(&_minor_lock);
1094 blk_cleanup_queue(md->queue);
1095 module_put(THIS_MODULE);
1100 * Bind a table to the device.
1102 static void event_callback(void *context)
1104 struct mapped_device *md = (struct mapped_device *) context;
1106 atomic_inc(&md->event_nr);
1107 wake_up(&md->eventq);
1110 static void __set_size(struct mapped_device *md, sector_t size)
1112 set_capacity(md->disk, size);
1114 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1115 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1116 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1119 static int __bind(struct mapped_device *md, struct dm_table *t)
1121 request_queue_t *q = md->queue;
1124 size = dm_table_get_size(t);
1127 * Wipe any geometry if the size of the table changed.
1129 if (size != get_capacity(md->disk))
1130 memset(&md->geometry, 0, sizeof(md->geometry));
1132 if (md->suspended_bdev)
1133 __set_size(md, size);
1138 dm_table_event_callback(t, event_callback, md);
1140 write_lock(&md->map_lock);
1142 dm_table_set_restrictions(t, q);
1143 write_unlock(&md->map_lock);
1148 static void __unbind(struct mapped_device *md)
1150 struct dm_table *map = md->map;
1155 dm_table_event_callback(map, NULL, NULL);
1156 write_lock(&md->map_lock);
1158 write_unlock(&md->map_lock);
1163 * Constructor for a new device.
1165 int dm_create(int minor, struct mapped_device **result)
1167 struct mapped_device *md;
1169 md = alloc_dev(minor);
1177 static struct mapped_device *dm_find_md(dev_t dev)
1179 struct mapped_device *md;
1180 unsigned minor = MINOR(dev);
1182 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1185 spin_lock(&_minor_lock);
1187 md = idr_find(&_minor_idr, minor);
1188 if (md && (md == MINOR_ALLOCED ||
1189 (dm_disk(md)->first_minor != minor) ||
1190 test_bit(DMF_FREEING, &md->flags))) {
1196 spin_unlock(&_minor_lock);
1201 struct mapped_device *dm_get_md(dev_t dev)
1203 struct mapped_device *md = dm_find_md(dev);
1211 void *dm_get_mdptr(struct mapped_device *md)
1213 return md->interface_ptr;
1216 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1218 md->interface_ptr = ptr;
1221 void dm_get(struct mapped_device *md)
1223 atomic_inc(&md->holders);
1226 const char *dm_device_name(struct mapped_device *md)
1230 EXPORT_SYMBOL_GPL(dm_device_name);
1232 void dm_put(struct mapped_device *md)
1234 struct dm_table *map;
1236 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1238 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1239 map = dm_get_table(md);
1240 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1241 set_bit(DMF_FREEING, &md->flags);
1242 spin_unlock(&_minor_lock);
1243 if (!dm_suspended(md)) {
1244 dm_table_presuspend_targets(map);
1245 dm_table_postsuspend_targets(map);
1254 * Process the deferred bios
1256 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1269 * Swap in a new table (destroying old one).
1271 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1275 down(&md->suspend_lock);
1277 /* device must be suspended */
1278 if (!dm_suspended(md))
1281 /* without bdev, the device size cannot be changed */
1282 if (!md->suspended_bdev)
1283 if (get_capacity(md->disk) != dm_table_get_size(table))
1287 r = __bind(md, table);
1290 up(&md->suspend_lock);
1295 * Functions to lock and unlock any filesystem running on the
1298 static int lock_fs(struct mapped_device *md)
1302 WARN_ON(md->frozen_sb);
1304 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1305 if (IS_ERR(md->frozen_sb)) {
1306 r = PTR_ERR(md->frozen_sb);
1307 md->frozen_sb = NULL;
1311 set_bit(DMF_FROZEN, &md->flags);
1313 /* don't bdput right now, we don't want the bdev
1314 * to go away while it is locked.
1319 static void unlock_fs(struct mapped_device *md)
1321 if (!test_bit(DMF_FROZEN, &md->flags))
1324 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1325 md->frozen_sb = NULL;
1326 clear_bit(DMF_FROZEN, &md->flags);
1330 * We need to be able to change a mapping table under a mounted
1331 * filesystem. For example we might want to move some data in
1332 * the background. Before the table can be swapped with
1333 * dm_bind_table, dm_suspend must be called to flush any in
1334 * flight bios and ensure that any further io gets deferred.
1336 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1338 struct dm_table *map = NULL;
1339 unsigned long flags;
1340 DECLARE_WAITQUEUE(wait, current);
1343 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1344 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1346 down(&md->suspend_lock);
1348 if (dm_suspended(md))
1351 map = dm_get_table(md);
1354 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1355 * This flag is cleared before dm_suspend returns.
1358 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1360 /* This does not get reverted if there's an error later. */
1361 dm_table_presuspend_targets(map);
1363 /* bdget() can stall if the pending I/Os are not flushed */
1365 md->suspended_bdev = bdget_disk(md->disk, 0);
1366 if (!md->suspended_bdev) {
1367 DMWARN("bdget failed in dm_suspend");
1374 * Flush I/O to the device.
1375 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1377 if (do_lockfs && !noflush) {
1384 * First we set the BLOCK_IO flag so no more ios will be mapped.
1386 down_write(&md->io_lock);
1387 set_bit(DMF_BLOCK_IO, &md->flags);
1389 add_wait_queue(&md->wait, &wait);
1390 up_write(&md->io_lock);
1394 dm_table_unplug_all(map);
1397 * Then we wait for the already mapped ios to
1401 set_current_state(TASK_INTERRUPTIBLE);
1403 if (!atomic_read(&md->pending) || signal_pending(current))
1408 set_current_state(TASK_RUNNING);
1410 down_write(&md->io_lock);
1411 remove_wait_queue(&md->wait, &wait);
1414 spin_lock_irqsave(&md->pushback_lock, flags);
1415 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1416 bio_list_merge_head(&md->deferred, &md->pushback);
1417 bio_list_init(&md->pushback);
1418 spin_unlock_irqrestore(&md->pushback_lock, flags);
1421 /* were we interrupted ? */
1423 if (atomic_read(&md->pending)) {
1424 clear_bit(DMF_BLOCK_IO, &md->flags);
1425 def = bio_list_get(&md->deferred);
1426 __flush_deferred_io(md, def);
1427 up_write(&md->io_lock);
1429 goto out; /* pushback list is already flushed, so skip flush */
1431 up_write(&md->io_lock);
1433 dm_table_postsuspend_targets(map);
1435 set_bit(DMF_SUSPENDED, &md->flags);
1442 * Because there may be already I/Os in the pushback list,
1443 * flush them before return.
1445 down_write(&md->io_lock);
1447 spin_lock_irqsave(&md->pushback_lock, flags);
1448 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1449 bio_list_merge_head(&md->deferred, &md->pushback);
1450 bio_list_init(&md->pushback);
1451 spin_unlock_irqrestore(&md->pushback_lock, flags);
1453 def = bio_list_get(&md->deferred);
1454 __flush_deferred_io(md, def);
1455 up_write(&md->io_lock);
1459 if (r && md->suspended_bdev) {
1460 bdput(md->suspended_bdev);
1461 md->suspended_bdev = NULL;
1467 up(&md->suspend_lock);
1471 int dm_resume(struct mapped_device *md)
1475 struct dm_table *map = NULL;
1477 down(&md->suspend_lock);
1478 if (!dm_suspended(md))
1481 map = dm_get_table(md);
1482 if (!map || !dm_table_get_size(map))
1485 r = dm_table_resume_targets(map);
1489 down_write(&md->io_lock);
1490 clear_bit(DMF_BLOCK_IO, &md->flags);
1492 def = bio_list_get(&md->deferred);
1493 __flush_deferred_io(md, def);
1494 up_write(&md->io_lock);
1498 if (md->suspended_bdev) {
1499 bdput(md->suspended_bdev);
1500 md->suspended_bdev = NULL;
1503 clear_bit(DMF_SUSPENDED, &md->flags);
1505 dm_table_unplug_all(map);
1507 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1513 up(&md->suspend_lock);
1518 /*-----------------------------------------------------------------
1519 * Event notification.
1520 *---------------------------------------------------------------*/
1521 uint32_t dm_get_event_nr(struct mapped_device *md)
1523 return atomic_read(&md->event_nr);
1526 int dm_wait_event(struct mapped_device *md, int event_nr)
1528 return wait_event_interruptible(md->eventq,
1529 (event_nr != atomic_read(&md->event_nr)));
1533 * The gendisk is only valid as long as you have a reference
1536 struct gendisk *dm_disk(struct mapped_device *md)
1541 int dm_suspended(struct mapped_device *md)
1543 return test_bit(DMF_SUSPENDED, &md->flags);
1546 int dm_noflush_suspending(struct dm_target *ti)
1548 struct mapped_device *md = dm_table_get_md(ti->table);
1549 int r = __noflush_suspending(md);
1555 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1557 static struct block_device_operations dm_blk_dops = {
1558 .open = dm_blk_open,
1559 .release = dm_blk_close,
1560 .ioctl = dm_blk_ioctl,
1561 .getgeo = dm_blk_getgeo,
1562 .owner = THIS_MODULE
1565 EXPORT_SYMBOL(dm_get_mapinfo);
1570 module_init(dm_init);
1571 module_exit(dm_exit);
1573 module_param(major, uint, 0);
1574 MODULE_PARM_DESC(major, "The major number of the device mapper");
1575 MODULE_DESCRIPTION(DM_NAME " driver");
1576 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1577 MODULE_LICENSE("GPL");