2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 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/moduleparam.h>
14 #include <linux/blkpg.h>
15 #include <linux/bio.h>
16 #include <linux/buffer_head.h>
17 #include <linux/mempool.h>
18 #include <linux/slab.h>
19 #include <linux/idr.h>
21 static const char *_name = DM_NAME;
23 static unsigned int major = 0;
24 static unsigned int _major = 0;
27 * One of these is allocated per bio.
30 struct mapped_device *md;
37 * One of these is allocated per target within a bio. Hopefully
38 * this will be simplified out one day.
46 union map_info *dm_get_mapinfo(struct bio *bio)
48 if (bio && bio->bi_private)
49 return &((struct target_io *)bio->bi_private)->info;
54 * Bits for the md->flags field.
56 #define DMF_BLOCK_IO 0
57 #define DMF_SUSPENDED 1
60 struct mapped_device {
61 struct rw_semaphore io_lock;
62 struct semaphore suspend_lock;
68 request_queue_t *queue;
74 * A list of ios that arrived while we were suspended.
77 wait_queue_head_t wait;
78 struct bio_list deferred;
81 * The current mapping.
86 * io objects are allocated from here.
95 wait_queue_head_t eventq;
98 * freeze/thaw support require holding onto a super block
100 struct super_block *frozen_sb;
101 struct block_device *suspended_bdev;
105 static kmem_cache_t *_io_cache;
106 static kmem_cache_t *_tio_cache;
108 static struct bio_set *dm_set;
110 static int __init local_init(void)
114 dm_set = bioset_create(16, 16, 4);
118 /* allocate a slab for the dm_ios */
119 _io_cache = kmem_cache_create("dm_io",
120 sizeof(struct dm_io), 0, 0, NULL, NULL);
124 /* allocate a slab for the target ios */
125 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
128 kmem_cache_destroy(_io_cache);
133 r = register_blkdev(_major, _name);
135 kmem_cache_destroy(_tio_cache);
136 kmem_cache_destroy(_io_cache);
146 static void local_exit(void)
148 kmem_cache_destroy(_tio_cache);
149 kmem_cache_destroy(_io_cache);
153 if (unregister_blkdev(_major, _name) < 0)
154 DMERR("devfs_unregister_blkdev failed");
158 DMINFO("cleaned up");
161 int (*_inits[])(void) __initdata = {
169 void (*_exits[])(void) = {
177 static int __init dm_init(void)
179 const int count = ARRAY_SIZE(_inits);
183 for (i = 0; i < count; i++) {
198 static void __exit dm_exit(void)
200 int i = ARRAY_SIZE(_exits);
207 * Block device functions
209 static int dm_blk_open(struct inode *inode, struct file *file)
211 struct mapped_device *md;
213 md = inode->i_bdev->bd_disk->private_data;
218 static int dm_blk_close(struct inode *inode, struct file *file)
220 struct mapped_device *md;
222 md = inode->i_bdev->bd_disk->private_data;
227 static inline struct dm_io *alloc_io(struct mapped_device *md)
229 return mempool_alloc(md->io_pool, GFP_NOIO);
232 static inline void free_io(struct mapped_device *md, struct dm_io *io)
234 mempool_free(io, md->io_pool);
237 static inline struct target_io *alloc_tio(struct mapped_device *md)
239 return mempool_alloc(md->tio_pool, GFP_NOIO);
242 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
244 mempool_free(tio, md->tio_pool);
248 * Add the bio to the list of deferred io.
250 static int queue_io(struct mapped_device *md, struct bio *bio)
252 down_write(&md->io_lock);
254 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
255 up_write(&md->io_lock);
259 bio_list_add(&md->deferred, bio);
261 up_write(&md->io_lock);
262 return 0; /* deferred successfully */
266 * Everyone (including functions in this file), should use this
267 * function to access the md->map field, and make sure they call
268 * dm_table_put() when finished.
270 struct dm_table *dm_get_table(struct mapped_device *md)
274 read_lock(&md->map_lock);
278 read_unlock(&md->map_lock);
283 /*-----------------------------------------------------------------
285 * A more elegant soln is in the works that uses the queue
286 * merge fn, unfortunately there are a couple of changes to
287 * the block layer that I want to make for this. So in the
288 * interests of getting something for people to use I give
289 * you this clearly demarcated crap.
290 *---------------------------------------------------------------*/
293 * Decrements the number of outstanding ios that a bio has been
294 * cloned into, completing the original io if necc.
296 static void dec_pending(struct dm_io *io, int error)
301 if (atomic_dec_and_test(&io->io_count)) {
302 if (atomic_dec_and_test(&io->md->pending))
303 /* nudge anyone waiting on suspend queue */
304 wake_up(&io->md->wait);
306 bio_endio(io->bio, io->bio->bi_size, io->error);
311 static int clone_endio(struct bio *bio, unsigned int done, int error)
314 struct target_io *tio = bio->bi_private;
315 struct dm_io *io = tio->io;
316 dm_endio_fn endio = tio->ti->type->end_io;
321 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
325 r = endio(tio->ti, bio, error, &tio->info);
330 /* the target wants another shot at the io */
334 free_tio(io->md, tio);
335 dec_pending(io, error);
340 static sector_t max_io_len(struct mapped_device *md,
341 sector_t sector, struct dm_target *ti)
343 sector_t offset = sector - ti->begin;
344 sector_t len = ti->len - offset;
347 * Does the target need to split even further ?
351 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
360 static void __map_bio(struct dm_target *ti, struct bio *clone,
361 struct target_io *tio)
368 BUG_ON(!clone->bi_size);
370 clone->bi_end_io = clone_endio;
371 clone->bi_private = tio;
374 * Map the clone. If r == 0 we don't need to do
375 * anything, the target has assumed ownership of
378 atomic_inc(&tio->io->io_count);
379 r = ti->type->map(ti, clone, &tio->info);
381 /* the bio has been remapped so dispatch it */
382 generic_make_request(clone);
385 /* error the io and bail out */
386 struct dm_io *io = tio->io;
387 free_tio(tio->io->md, tio);
394 struct mapped_device *md;
395 struct dm_table *map;
399 sector_t sector_count;
403 static void dm_bio_destructor(struct bio *bio)
405 bio_free(bio, dm_set);
409 * Creates a little bio that is just does part of a bvec.
411 static struct bio *split_bvec(struct bio *bio, sector_t sector,
412 unsigned short idx, unsigned int offset,
416 struct bio_vec *bv = bio->bi_io_vec + idx;
418 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
419 clone->bi_destructor = dm_bio_destructor;
420 *clone->bi_io_vec = *bv;
422 clone->bi_sector = sector;
423 clone->bi_bdev = bio->bi_bdev;
424 clone->bi_rw = bio->bi_rw;
426 clone->bi_size = to_bytes(len);
427 clone->bi_io_vec->bv_offset = offset;
428 clone->bi_io_vec->bv_len = clone->bi_size;
434 * Creates a bio that consists of range of complete bvecs.
436 static struct bio *clone_bio(struct bio *bio, sector_t sector,
437 unsigned short idx, unsigned short bv_count,
442 clone = bio_clone(bio, GFP_NOIO);
443 clone->bi_sector = sector;
445 clone->bi_vcnt = idx + bv_count;
446 clone->bi_size = to_bytes(len);
447 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
452 static void __clone_and_map(struct clone_info *ci)
454 struct bio *clone, *bio = ci->bio;
455 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
456 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
457 struct target_io *tio;
460 * Allocate a target io object.
462 tio = alloc_tio(ci->md);
465 memset(&tio->info, 0, sizeof(tio->info));
467 if (ci->sector_count <= max) {
469 * Optimise for the simple case where we can do all of
470 * the remaining io with a single clone.
472 clone = clone_bio(bio, ci->sector, ci->idx,
473 bio->bi_vcnt - ci->idx, ci->sector_count);
474 __map_bio(ti, clone, tio);
475 ci->sector_count = 0;
477 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
479 * There are some bvecs that don't span targets.
480 * Do as many of these as possible.
483 sector_t remaining = max;
486 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
487 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
489 if (bv_len > remaining)
496 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
497 __map_bio(ti, clone, tio);
500 ci->sector_count -= len;
505 * Create two copy bios to deal with io that has
506 * been split across a target.
508 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
510 clone = split_bvec(bio, ci->sector, ci->idx,
512 __map_bio(ti, clone, tio);
515 ci->sector_count -= max;
516 ti = dm_table_find_target(ci->map, ci->sector);
518 len = to_sector(bv->bv_len) - max;
519 clone = split_bvec(bio, ci->sector, ci->idx,
520 bv->bv_offset + to_bytes(max), len);
521 tio = alloc_tio(ci->md);
524 memset(&tio->info, 0, sizeof(tio->info));
525 __map_bio(ti, clone, tio);
528 ci->sector_count -= len;
534 * Split the bio into several clones.
536 static void __split_bio(struct mapped_device *md, struct bio *bio)
538 struct clone_info ci;
540 ci.map = dm_get_table(md);
542 bio_io_error(bio, bio->bi_size);
548 ci.io = alloc_io(md);
550 atomic_set(&ci.io->io_count, 1);
553 ci.sector = bio->bi_sector;
554 ci.sector_count = bio_sectors(bio);
555 ci.idx = bio->bi_idx;
557 atomic_inc(&md->pending);
558 while (ci.sector_count)
559 __clone_and_map(&ci);
561 /* drop the extra reference count */
562 dec_pending(ci.io, 0);
563 dm_table_put(ci.map);
565 /*-----------------------------------------------------------------
567 *---------------------------------------------------------------*/
570 * The request function that just remaps the bio built up by
573 static int dm_request(request_queue_t *q, struct bio *bio)
576 struct mapped_device *md = q->queuedata;
578 down_read(&md->io_lock);
581 * If we're suspended we have to queue
584 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
585 up_read(&md->io_lock);
587 if (bio_rw(bio) == READA) {
588 bio_io_error(bio, bio->bi_size);
592 r = queue_io(md, bio);
594 bio_io_error(bio, bio->bi_size);
598 return 0; /* deferred successfully */
601 * We're in a while loop, because someone could suspend
602 * before we get to the following read lock.
604 down_read(&md->io_lock);
607 __split_bio(md, bio);
608 up_read(&md->io_lock);
612 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
613 sector_t *error_sector)
615 struct mapped_device *md = q->queuedata;
616 struct dm_table *map = dm_get_table(md);
620 ret = dm_table_flush_all(map);
627 static void dm_unplug_all(request_queue_t *q)
629 struct mapped_device *md = q->queuedata;
630 struct dm_table *map = dm_get_table(md);
633 dm_table_unplug_all(map);
638 static int dm_any_congested(void *congested_data, int bdi_bits)
641 struct mapped_device *md = (struct mapped_device *) congested_data;
642 struct dm_table *map = dm_get_table(md);
644 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
647 r = dm_table_any_congested(map, bdi_bits);
653 /*-----------------------------------------------------------------
654 * An IDR is used to keep track of allocated minor numbers.
655 *---------------------------------------------------------------*/
656 static DECLARE_MUTEX(_minor_lock);
657 static DEFINE_IDR(_minor_idr);
659 static void free_minor(unsigned int minor)
662 idr_remove(&_minor_idr, minor);
667 * See if the device with a specific minor # is free.
669 static int specific_minor(struct mapped_device *md, unsigned int minor)
673 if (minor >= (1 << MINORBITS))
678 if (idr_find(&_minor_idr, minor)) {
683 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
689 r = idr_get_new_above(&_minor_idr, md, minor, &m);
695 idr_remove(&_minor_idr, m);
705 static int next_free_minor(struct mapped_device *md, unsigned int *minor)
712 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
718 r = idr_get_new(&_minor_idr, md, &m);
723 if (m >= (1 << MINORBITS)) {
724 idr_remove(&_minor_idr, m);
736 static struct block_device_operations dm_blk_dops;
739 * Allocate and initialise a blank device with a given minor.
741 static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
744 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
747 DMWARN("unable to allocate device, out of memory.");
751 /* get a minor number for the dev */
752 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
756 memset(md, 0, sizeof(*md));
757 init_rwsem(&md->io_lock);
758 init_MUTEX(&md->suspend_lock);
759 rwlock_init(&md->map_lock);
760 atomic_set(&md->holders, 1);
761 atomic_set(&md->event_nr, 0);
763 md->queue = blk_alloc_queue(GFP_KERNEL);
767 md->queue->queuedata = md;
768 md->queue->backing_dev_info.congested_fn = dm_any_congested;
769 md->queue->backing_dev_info.congested_data = md;
770 blk_queue_make_request(md->queue, dm_request);
771 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
772 md->queue->unplug_fn = dm_unplug_all;
773 md->queue->issue_flush_fn = dm_flush_all;
775 md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
776 mempool_free_slab, _io_cache);
780 md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
781 mempool_free_slab, _tio_cache);
785 md->disk = alloc_disk(1);
789 md->disk->major = _major;
790 md->disk->first_minor = minor;
791 md->disk->fops = &dm_blk_dops;
792 md->disk->queue = md->queue;
793 md->disk->private_data = md;
794 sprintf(md->disk->disk_name, "dm-%d", minor);
797 atomic_set(&md->pending, 0);
798 init_waitqueue_head(&md->wait);
799 init_waitqueue_head(&md->eventq);
804 mempool_destroy(md->tio_pool);
806 mempool_destroy(md->io_pool);
808 blk_put_queue(md->queue);
815 static void free_dev(struct mapped_device *md)
817 free_minor(md->disk->first_minor);
818 mempool_destroy(md->tio_pool);
819 mempool_destroy(md->io_pool);
820 del_gendisk(md->disk);
822 blk_put_queue(md->queue);
827 * Bind a table to the device.
829 static void event_callback(void *context)
831 struct mapped_device *md = (struct mapped_device *) context;
833 atomic_inc(&md->event_nr);
834 wake_up(&md->eventq);
837 static void __set_size(struct mapped_device *md, sector_t size)
839 set_capacity(md->disk, size);
841 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
842 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
843 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
846 static int __bind(struct mapped_device *md, struct dm_table *t)
848 request_queue_t *q = md->queue;
851 size = dm_table_get_size(t);
852 __set_size(md, size);
857 dm_table_event_callback(t, event_callback, md);
859 write_lock(&md->map_lock);
861 dm_table_set_restrictions(t, q);
862 write_unlock(&md->map_lock);
867 static void __unbind(struct mapped_device *md)
869 struct dm_table *map = md->map;
874 dm_table_event_callback(map, NULL, NULL);
875 write_lock(&md->map_lock);
877 write_unlock(&md->map_lock);
882 * Constructor for a new device.
884 static int create_aux(unsigned int minor, int persistent,
885 struct mapped_device **result)
887 struct mapped_device *md;
889 md = alloc_dev(minor, persistent);
897 int dm_create(struct mapped_device **result)
899 return create_aux(0, 0, result);
902 int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
904 return create_aux(minor, 1, result);
907 static struct mapped_device *dm_find_md(dev_t dev)
909 struct mapped_device *md;
910 unsigned minor = MINOR(dev);
912 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
917 md = idr_find(&_minor_idr, minor);
918 if (!md || (dm_disk(md)->first_minor != minor))
926 struct mapped_device *dm_get_md(dev_t dev)
928 struct mapped_device *md = dm_find_md(dev);
936 void *dm_get_mdptr(dev_t dev)
938 struct mapped_device *md;
941 md = dm_find_md(dev);
943 mdptr = md->interface_ptr;
947 void dm_set_mdptr(struct mapped_device *md, void *ptr)
949 md->interface_ptr = ptr;
952 void dm_get(struct mapped_device *md)
954 atomic_inc(&md->holders);
957 void dm_put(struct mapped_device *md)
959 struct dm_table *map = dm_get_table(md);
961 if (atomic_dec_and_test(&md->holders)) {
962 if (!dm_suspended(md)) {
963 dm_table_presuspend_targets(map);
964 dm_table_postsuspend_targets(map);
974 * Process the deferred bios
976 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
989 * Swap in a new table (destroying old one).
991 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
995 down(&md->suspend_lock);
997 /* device must be suspended */
998 if (!dm_suspended(md))
1002 r = __bind(md, table);
1005 up(&md->suspend_lock);
1010 * Functions to lock and unlock any filesystem running on the
1013 static int lock_fs(struct mapped_device *md)
1017 WARN_ON(md->frozen_sb);
1019 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1020 if (IS_ERR(md->frozen_sb)) {
1021 r = PTR_ERR(md->frozen_sb);
1022 md->frozen_sb = NULL;
1026 set_bit(DMF_FROZEN, &md->flags);
1028 /* don't bdput right now, we don't want the bdev
1029 * to go away while it is locked.
1034 static void unlock_fs(struct mapped_device *md)
1036 if (!test_bit(DMF_FROZEN, &md->flags))
1039 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1040 md->frozen_sb = NULL;
1041 clear_bit(DMF_FROZEN, &md->flags);
1045 * We need to be able to change a mapping table under a mounted
1046 * filesystem. For example we might want to move some data in
1047 * the background. Before the table can be swapped with
1048 * dm_bind_table, dm_suspend must be called to flush any in
1049 * flight bios and ensure that any further io gets deferred.
1051 int dm_suspend(struct mapped_device *md, int do_lockfs)
1053 struct dm_table *map = NULL;
1054 DECLARE_WAITQUEUE(wait, current);
1057 down(&md->suspend_lock);
1059 if (dm_suspended(md))
1062 map = dm_get_table(md);
1064 /* This does not get reverted if there's an error later. */
1065 dm_table_presuspend_targets(map);
1067 md->suspended_bdev = bdget_disk(md->disk, 0);
1068 if (!md->suspended_bdev) {
1069 DMWARN("bdget failed in dm_suspend");
1074 /* Flush I/O to the device. */
1082 * First we set the BLOCK_IO flag so no more ios will be mapped.
1084 down_write(&md->io_lock);
1085 set_bit(DMF_BLOCK_IO, &md->flags);
1087 add_wait_queue(&md->wait, &wait);
1088 up_write(&md->io_lock);
1092 dm_table_unplug_all(map);
1095 * Then we wait for the already mapped ios to
1099 set_current_state(TASK_INTERRUPTIBLE);
1101 if (!atomic_read(&md->pending) || signal_pending(current))
1106 set_current_state(TASK_RUNNING);
1108 down_write(&md->io_lock);
1109 remove_wait_queue(&md->wait, &wait);
1111 /* were we interrupted ? */
1113 if (atomic_read(&md->pending)) {
1114 up_write(&md->io_lock);
1116 clear_bit(DMF_BLOCK_IO, &md->flags);
1119 up_write(&md->io_lock);
1121 dm_table_postsuspend_targets(map);
1123 set_bit(DMF_SUSPENDED, &md->flags);
1128 if (r && md->suspended_bdev) {
1129 bdput(md->suspended_bdev);
1130 md->suspended_bdev = NULL;
1134 up(&md->suspend_lock);
1138 int dm_resume(struct mapped_device *md)
1142 struct dm_table *map = NULL;
1144 down(&md->suspend_lock);
1145 if (!dm_suspended(md))
1148 map = dm_get_table(md);
1149 if (!map || !dm_table_get_size(map))
1152 dm_table_resume_targets(map);
1154 down_write(&md->io_lock);
1155 clear_bit(DMF_BLOCK_IO, &md->flags);
1157 def = bio_list_get(&md->deferred);
1158 __flush_deferred_io(md, def);
1159 up_write(&md->io_lock);
1163 bdput(md->suspended_bdev);
1164 md->suspended_bdev = NULL;
1166 clear_bit(DMF_SUSPENDED, &md->flags);
1168 dm_table_unplug_all(map);
1174 up(&md->suspend_lock);
1179 /*-----------------------------------------------------------------
1180 * Event notification.
1181 *---------------------------------------------------------------*/
1182 uint32_t dm_get_event_nr(struct mapped_device *md)
1184 return atomic_read(&md->event_nr);
1187 int dm_wait_event(struct mapped_device *md, int event_nr)
1189 return wait_event_interruptible(md->eventq,
1190 (event_nr != atomic_read(&md->event_nr)));
1194 * The gendisk is only valid as long as you have a reference
1197 struct gendisk *dm_disk(struct mapped_device *md)
1202 int dm_suspended(struct mapped_device *md)
1204 return test_bit(DMF_SUSPENDED, &md->flags);
1207 static struct block_device_operations dm_blk_dops = {
1208 .open = dm_blk_open,
1209 .release = dm_blk_close,
1210 .owner = THIS_MODULE
1213 EXPORT_SYMBOL(dm_get_mapinfo);
1218 module_init(dm_init);
1219 module_exit(dm_exit);
1221 module_param(major, uint, 0);
1222 MODULE_PARM_DESC(major, "The major number of the device mapper");
1223 MODULE_DESCRIPTION(DM_NAME " driver");
1224 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1225 MODULE_LICENSE("GPL");