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
59 struct mapped_device {
60 struct rw_semaphore io_lock;
61 struct semaphore suspend_lock;
67 request_queue_t *queue;
73 * A list of ios that arrived while we were suspended.
76 wait_queue_head_t wait;
77 struct bio_list deferred;
80 * The current mapping.
85 * io objects are allocated from here.
94 wait_queue_head_t eventq;
97 * freeze/thaw support require holding onto a super block
99 struct super_block *frozen_sb;
100 struct block_device *frozen_bdev;
104 static kmem_cache_t *_io_cache;
105 static kmem_cache_t *_tio_cache;
107 static struct bio_set *dm_set;
109 static int __init local_init(void)
113 dm_set = bioset_create(16, 16, 4);
117 /* allocate a slab for the dm_ios */
118 _io_cache = kmem_cache_create("dm_io",
119 sizeof(struct dm_io), 0, 0, NULL, NULL);
123 /* allocate a slab for the target ios */
124 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
127 kmem_cache_destroy(_io_cache);
132 r = register_blkdev(_major, _name);
134 kmem_cache_destroy(_tio_cache);
135 kmem_cache_destroy(_io_cache);
145 static void local_exit(void)
147 kmem_cache_destroy(_tio_cache);
148 kmem_cache_destroy(_io_cache);
152 if (unregister_blkdev(_major, _name) < 0)
153 DMERR("devfs_unregister_blkdev failed");
157 DMINFO("cleaned up");
160 int (*_inits[])(void) __initdata = {
168 void (*_exits[])(void) = {
176 static int __init dm_init(void)
178 const int count = ARRAY_SIZE(_inits);
182 for (i = 0; i < count; i++) {
197 static void __exit dm_exit(void)
199 int i = ARRAY_SIZE(_exits);
206 * Block device functions
208 static int dm_blk_open(struct inode *inode, struct file *file)
210 struct mapped_device *md;
212 md = inode->i_bdev->bd_disk->private_data;
217 static int dm_blk_close(struct inode *inode, struct file *file)
219 struct mapped_device *md;
221 md = inode->i_bdev->bd_disk->private_data;
226 static inline struct dm_io *alloc_io(struct mapped_device *md)
228 return mempool_alloc(md->io_pool, GFP_NOIO);
231 static inline void free_io(struct mapped_device *md, struct dm_io *io)
233 mempool_free(io, md->io_pool);
236 static inline struct target_io *alloc_tio(struct mapped_device *md)
238 return mempool_alloc(md->tio_pool, GFP_NOIO);
241 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
243 mempool_free(tio, md->tio_pool);
247 * Add the bio to the list of deferred io.
249 static int queue_io(struct mapped_device *md, struct bio *bio)
251 down_write(&md->io_lock);
253 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
254 up_write(&md->io_lock);
258 bio_list_add(&md->deferred, bio);
260 up_write(&md->io_lock);
261 return 0; /* deferred successfully */
265 * Everyone (including functions in this file), should use this
266 * function to access the md->map field, and make sure they call
267 * dm_table_put() when finished.
269 struct dm_table *dm_get_table(struct mapped_device *md)
273 read_lock(&md->map_lock);
277 read_unlock(&md->map_lock);
282 /*-----------------------------------------------------------------
284 * A more elegant soln is in the works that uses the queue
285 * merge fn, unfortunately there are a couple of changes to
286 * the block layer that I want to make for this. So in the
287 * interests of getting something for people to use I give
288 * you this clearly demarcated crap.
289 *---------------------------------------------------------------*/
292 * Decrements the number of outstanding ios that a bio has been
293 * cloned into, completing the original io if necc.
295 static inline void dec_pending(struct dm_io *io, int error)
300 if (atomic_dec_and_test(&io->io_count)) {
301 if (atomic_dec_and_test(&io->md->pending))
302 /* nudge anyone waiting on suspend queue */
303 wake_up(&io->md->wait);
305 bio_endio(io->bio, io->bio->bi_size, io->error);
310 static int clone_endio(struct bio *bio, unsigned int done, int error)
313 struct target_io *tio = bio->bi_private;
314 struct dm_io *io = tio->io;
315 dm_endio_fn endio = tio->ti->type->end_io;
320 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
324 r = endio(tio->ti, bio, error, &tio->info);
329 /* the target wants another shot at the io */
333 free_tio(io->md, tio);
334 dec_pending(io, error);
339 static sector_t max_io_len(struct mapped_device *md,
340 sector_t sector, struct dm_target *ti)
342 sector_t offset = sector - ti->begin;
343 sector_t len = ti->len - offset;
346 * Does the target need to split even further ?
350 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
359 static void __map_bio(struct dm_target *ti, struct bio *clone,
360 struct target_io *tio)
367 BUG_ON(!clone->bi_size);
369 clone->bi_end_io = clone_endio;
370 clone->bi_private = tio;
373 * Map the clone. If r == 0 we don't need to do
374 * anything, the target has assumed ownership of
377 atomic_inc(&tio->io->io_count);
378 r = ti->type->map(ti, clone, &tio->info);
380 /* the bio has been remapped so dispatch it */
381 generic_make_request(clone);
384 /* error the io and bail out */
385 struct dm_io *io = tio->io;
386 free_tio(tio->io->md, tio);
393 struct mapped_device *md;
394 struct dm_table *map;
398 sector_t sector_count;
402 static void dm_bio_destructor(struct bio *bio)
404 bio_free(bio, dm_set);
408 * Creates a little bio that is just does part of a bvec.
410 static struct bio *split_bvec(struct bio *bio, sector_t sector,
411 unsigned short idx, unsigned int offset,
415 struct bio_vec *bv = bio->bi_io_vec + idx;
417 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
418 clone->bi_destructor = dm_bio_destructor;
419 *clone->bi_io_vec = *bv;
421 clone->bi_sector = sector;
422 clone->bi_bdev = bio->bi_bdev;
423 clone->bi_rw = bio->bi_rw;
425 clone->bi_size = to_bytes(len);
426 clone->bi_io_vec->bv_offset = offset;
427 clone->bi_io_vec->bv_len = clone->bi_size;
433 * Creates a bio that consists of range of complete bvecs.
435 static struct bio *clone_bio(struct bio *bio, sector_t sector,
436 unsigned short idx, unsigned short bv_count,
441 clone = bio_clone(bio, GFP_NOIO);
442 clone->bi_sector = sector;
444 clone->bi_vcnt = idx + bv_count;
445 clone->bi_size = to_bytes(len);
446 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
451 static void __clone_and_map(struct clone_info *ci)
453 struct bio *clone, *bio = ci->bio;
454 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
455 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
456 struct target_io *tio;
459 * Allocate a target io object.
461 tio = alloc_tio(ci->md);
464 memset(&tio->info, 0, sizeof(tio->info));
466 if (ci->sector_count <= max) {
468 * Optimise for the simple case where we can do all of
469 * the remaining io with a single clone.
471 clone = clone_bio(bio, ci->sector, ci->idx,
472 bio->bi_vcnt - ci->idx, ci->sector_count);
473 __map_bio(ti, clone, tio);
474 ci->sector_count = 0;
476 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
478 * There are some bvecs that don't span targets.
479 * Do as many of these as possible.
482 sector_t remaining = max;
485 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
486 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
488 if (bv_len > remaining)
495 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
496 __map_bio(ti, clone, tio);
499 ci->sector_count -= len;
504 * Create two copy bios to deal with io that has
505 * been split across a target.
507 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
509 clone = split_bvec(bio, ci->sector, ci->idx,
511 __map_bio(ti, clone, tio);
514 ci->sector_count -= max;
515 ti = dm_table_find_target(ci->map, ci->sector);
517 len = to_sector(bv->bv_len) - max;
518 clone = split_bvec(bio, ci->sector, ci->idx,
519 bv->bv_offset + to_bytes(max), len);
520 tio = alloc_tio(ci->md);
523 memset(&tio->info, 0, sizeof(tio->info));
524 __map_bio(ti, clone, tio);
527 ci->sector_count -= len;
533 * Split the bio into several clones.
535 static void __split_bio(struct mapped_device *md, struct bio *bio)
537 struct clone_info ci;
539 ci.map = dm_get_table(md);
541 bio_io_error(bio, bio->bi_size);
547 ci.io = alloc_io(md);
549 atomic_set(&ci.io->io_count, 1);
552 ci.sector = bio->bi_sector;
553 ci.sector_count = bio_sectors(bio);
554 ci.idx = bio->bi_idx;
556 atomic_inc(&md->pending);
557 while (ci.sector_count)
558 __clone_and_map(&ci);
560 /* drop the extra reference count */
561 dec_pending(ci.io, 0);
562 dm_table_put(ci.map);
564 /*-----------------------------------------------------------------
566 *---------------------------------------------------------------*/
569 * The request function that just remaps the bio built up by
572 static int dm_request(request_queue_t *q, struct bio *bio)
575 struct mapped_device *md = q->queuedata;
577 down_read(&md->io_lock);
580 * If we're suspended we have to queue
583 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
584 up_read(&md->io_lock);
586 if (bio_rw(bio) == READA) {
587 bio_io_error(bio, bio->bi_size);
591 r = queue_io(md, bio);
593 bio_io_error(bio, bio->bi_size);
597 return 0; /* deferred successfully */
600 * We're in a while loop, because someone could suspend
601 * before we get to the following read lock.
603 down_read(&md->io_lock);
606 __split_bio(md, bio);
607 up_read(&md->io_lock);
611 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
612 sector_t *error_sector)
614 struct mapped_device *md = q->queuedata;
615 struct dm_table *map = dm_get_table(md);
619 ret = dm_table_flush_all(map);
626 static void dm_unplug_all(request_queue_t *q)
628 struct mapped_device *md = q->queuedata;
629 struct dm_table *map = dm_get_table(md);
632 dm_table_unplug_all(map);
637 static int dm_any_congested(void *congested_data, int bdi_bits)
640 struct mapped_device *md = (struct mapped_device *) congested_data;
641 struct dm_table *map = dm_get_table(md);
643 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
646 r = dm_table_any_congested(map, bdi_bits);
652 /*-----------------------------------------------------------------
653 * An IDR is used to keep track of allocated minor numbers.
654 *---------------------------------------------------------------*/
655 static DECLARE_MUTEX(_minor_lock);
656 static DEFINE_IDR(_minor_idr);
658 static void free_minor(unsigned int minor)
661 idr_remove(&_minor_idr, minor);
666 * See if the device with a specific minor # is free.
668 static int specific_minor(struct mapped_device *md, unsigned int minor)
672 if (minor >= (1 << MINORBITS))
677 if (idr_find(&_minor_idr, minor)) {
682 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
688 r = idr_get_new_above(&_minor_idr, md, minor, &m);
694 idr_remove(&_minor_idr, m);
704 static int next_free_minor(struct mapped_device *md, unsigned int *minor)
711 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
717 r = idr_get_new(&_minor_idr, md, &m);
722 if (m >= (1 << MINORBITS)) {
723 idr_remove(&_minor_idr, m);
735 static struct block_device_operations dm_blk_dops;
738 * Allocate and initialise a blank device with a given minor.
740 static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
743 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
746 DMWARN("unable to allocate device, out of memory.");
750 /* get a minor number for the dev */
751 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
755 memset(md, 0, sizeof(*md));
756 init_rwsem(&md->io_lock);
757 init_MUTEX(&md->suspend_lock);
758 rwlock_init(&md->map_lock);
759 atomic_set(&md->holders, 1);
760 atomic_set(&md->event_nr, 0);
762 md->queue = blk_alloc_queue(GFP_KERNEL);
766 md->queue->queuedata = md;
767 md->queue->backing_dev_info.congested_fn = dm_any_congested;
768 md->queue->backing_dev_info.congested_data = md;
769 blk_queue_make_request(md->queue, dm_request);
770 md->queue->unplug_fn = dm_unplug_all;
771 md->queue->issue_flush_fn = dm_flush_all;
773 md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
774 mempool_free_slab, _io_cache);
778 md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
779 mempool_free_slab, _tio_cache);
783 md->disk = alloc_disk(1);
787 md->disk->major = _major;
788 md->disk->first_minor = minor;
789 md->disk->fops = &dm_blk_dops;
790 md->disk->queue = md->queue;
791 md->disk->private_data = md;
792 sprintf(md->disk->disk_name, "dm-%d", minor);
795 atomic_set(&md->pending, 0);
796 init_waitqueue_head(&md->wait);
797 init_waitqueue_head(&md->eventq);
802 mempool_destroy(md->tio_pool);
804 mempool_destroy(md->io_pool);
806 blk_put_queue(md->queue);
813 static void free_dev(struct mapped_device *md)
815 free_minor(md->disk->first_minor);
816 mempool_destroy(md->tio_pool);
817 mempool_destroy(md->io_pool);
818 del_gendisk(md->disk);
820 blk_put_queue(md->queue);
825 * Bind a table to the device.
827 static void event_callback(void *context)
829 struct mapped_device *md = (struct mapped_device *) context;
831 atomic_inc(&md->event_nr);
832 wake_up(&md->eventq);
835 static void __set_size(struct mapped_device *md, sector_t size)
837 set_capacity(md->disk, size);
839 down(&md->frozen_bdev->bd_inode->i_sem);
840 i_size_write(md->frozen_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
841 up(&md->frozen_bdev->bd_inode->i_sem);
844 static int __bind(struct mapped_device *md, struct dm_table *t)
846 request_queue_t *q = md->queue;
849 size = dm_table_get_size(t);
850 __set_size(md, size);
855 dm_table_event_callback(t, event_callback, md);
857 write_lock(&md->map_lock);
859 dm_table_set_restrictions(t, q);
860 write_unlock(&md->map_lock);
865 static void __unbind(struct mapped_device *md)
867 struct dm_table *map = md->map;
872 dm_table_event_callback(map, NULL, NULL);
873 write_lock(&md->map_lock);
875 write_unlock(&md->map_lock);
880 * Constructor for a new device.
882 static int create_aux(unsigned int minor, int persistent,
883 struct mapped_device **result)
885 struct mapped_device *md;
887 md = alloc_dev(minor, persistent);
895 int dm_create(struct mapped_device **result)
897 return create_aux(0, 0, result);
900 int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
902 return create_aux(minor, 1, result);
905 void *dm_get_mdptr(dev_t dev)
907 struct mapped_device *md;
909 unsigned minor = MINOR(dev);
911 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
916 md = idr_find(&_minor_idr, minor);
918 if (md && (dm_disk(md)->first_minor == minor))
919 mdptr = md->interface_ptr;
926 void dm_set_mdptr(struct mapped_device *md, void *ptr)
928 md->interface_ptr = ptr;
931 void dm_get(struct mapped_device *md)
933 atomic_inc(&md->holders);
936 void dm_put(struct mapped_device *md)
938 struct dm_table *map = dm_get_table(md);
940 if (atomic_dec_and_test(&md->holders)) {
941 if (!dm_suspended(md)) {
942 dm_table_presuspend_targets(map);
943 dm_table_postsuspend_targets(map);
953 * Process the deferred bios
955 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
968 * Swap in a new table (destroying old one).
970 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
974 down(&md->suspend_lock);
976 /* device must be suspended */
977 if (!dm_suspended(md))
981 r = __bind(md, table);
984 up(&md->suspend_lock);
989 * Functions to lock and unlock any filesystem running on the
992 static int lock_fs(struct mapped_device *md)
996 md->frozen_bdev = bdget_disk(md->disk, 0);
997 if (!md->frozen_bdev) {
998 DMWARN("bdget failed in lock_fs");
1002 WARN_ON(md->frozen_sb);
1004 md->frozen_sb = freeze_bdev(md->frozen_bdev);
1005 if (IS_ERR(md->frozen_sb)) {
1006 r = PTR_ERR(md->frozen_sb);
1010 /* don't bdput right now, we don't want the bdev
1011 * to go away while it is locked. We'll bdput
1017 bdput(md->frozen_bdev);
1018 md->frozen_sb = NULL;
1019 md->frozen_bdev = NULL;
1024 static void unlock_fs(struct mapped_device *md)
1026 thaw_bdev(md->frozen_bdev, md->frozen_sb);
1027 bdput(md->frozen_bdev);
1029 md->frozen_sb = NULL;
1030 md->frozen_bdev = NULL;
1034 * We need to be able to change a mapping table under a mounted
1035 * filesystem. For example we might want to move some data in
1036 * the background. Before the table can be swapped with
1037 * dm_bind_table, dm_suspend must be called to flush any in
1038 * flight bios and ensure that any further io gets deferred.
1040 int dm_suspend(struct mapped_device *md)
1042 struct dm_table *map = NULL;
1043 DECLARE_WAITQUEUE(wait, current);
1046 down(&md->suspend_lock);
1048 if (dm_suspended(md))
1051 map = dm_get_table(md);
1053 /* This does not get reverted if there's an error later. */
1054 dm_table_presuspend_targets(map);
1056 /* Flush I/O to the device. */
1062 * First we set the BLOCK_IO flag so no more ios will be mapped.
1064 down_write(&md->io_lock);
1065 set_bit(DMF_BLOCK_IO, &md->flags);
1067 add_wait_queue(&md->wait, &wait);
1068 up_write(&md->io_lock);
1072 dm_table_unplug_all(map);
1075 * Then we wait for the already mapped ios to
1079 set_current_state(TASK_INTERRUPTIBLE);
1081 if (!atomic_read(&md->pending) || signal_pending(current))
1086 set_current_state(TASK_RUNNING);
1088 down_write(&md->io_lock);
1089 remove_wait_queue(&md->wait, &wait);
1091 /* were we interrupted ? */
1093 if (atomic_read(&md->pending)) {
1094 up_write(&md->io_lock);
1096 clear_bit(DMF_BLOCK_IO, &md->flags);
1099 up_write(&md->io_lock);
1101 dm_table_postsuspend_targets(map);
1103 set_bit(DMF_SUSPENDED, &md->flags);
1109 up(&md->suspend_lock);
1113 int dm_resume(struct mapped_device *md)
1117 struct dm_table *map = NULL;
1119 down(&md->suspend_lock);
1120 if (!dm_suspended(md))
1123 map = dm_get_table(md);
1124 if (!map || !dm_table_get_size(map))
1127 dm_table_resume_targets(map);
1129 down_write(&md->io_lock);
1130 clear_bit(DMF_BLOCK_IO, &md->flags);
1132 def = bio_list_get(&md->deferred);
1133 __flush_deferred_io(md, def);
1134 up_write(&md->io_lock);
1138 clear_bit(DMF_SUSPENDED, &md->flags);
1140 dm_table_unplug_all(map);
1146 up(&md->suspend_lock);
1151 /*-----------------------------------------------------------------
1152 * Event notification.
1153 *---------------------------------------------------------------*/
1154 uint32_t dm_get_event_nr(struct mapped_device *md)
1156 return atomic_read(&md->event_nr);
1159 int dm_wait_event(struct mapped_device *md, int event_nr)
1161 return wait_event_interruptible(md->eventq,
1162 (event_nr != atomic_read(&md->event_nr)));
1166 * The gendisk is only valid as long as you have a reference
1169 struct gendisk *dm_disk(struct mapped_device *md)
1174 int dm_suspended(struct mapped_device *md)
1176 return test_bit(DMF_SUSPENDED, &md->flags);
1179 static struct block_device_operations dm_blk_dops = {
1180 .open = dm_blk_open,
1181 .release = dm_blk_close,
1182 .owner = THIS_MODULE
1185 EXPORT_SYMBOL(dm_get_mapinfo);
1190 module_init(dm_init);
1191 module_exit(dm_exit);
1193 module_param(major, uint, 0);
1194 MODULE_PARM_DESC(major, "The major number of the device mapper");
1195 MODULE_DESCRIPTION(DM_NAME " driver");
1196 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1197 MODULE_LICENSE("GPL");