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;
34 unsigned long start_time;
38 * One of these is allocated per target within a bio. Hopefully
39 * this will be simplified out one day.
47 union map_info *dm_get_mapinfo(struct bio *bio)
49 if (bio && bio->bi_private)
50 return &((struct target_io *)bio->bi_private)->info;
55 * Bits for the md->flags field.
57 #define DMF_BLOCK_IO 0
58 #define DMF_SUSPENDED 1
61 struct mapped_device {
62 struct rw_semaphore io_lock;
63 struct semaphore suspend_lock;
69 request_queue_t *queue;
75 * A list of ios that arrived while we were suspended.
78 wait_queue_head_t wait;
79 struct bio_list deferred;
82 * The current mapping.
87 * io objects are allocated from here.
96 wait_queue_head_t eventq;
99 * freeze/thaw support require holding onto a super block
101 struct super_block *frozen_sb;
102 struct block_device *suspended_bdev;
106 static kmem_cache_t *_io_cache;
107 static kmem_cache_t *_tio_cache;
109 static struct bio_set *dm_set;
111 static int __init local_init(void)
115 dm_set = bioset_create(16, 16, 4);
119 /* allocate a slab for the dm_ios */
120 _io_cache = kmem_cache_create("dm_io",
121 sizeof(struct dm_io), 0, 0, NULL, NULL);
125 /* allocate a slab for the target ios */
126 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
129 kmem_cache_destroy(_io_cache);
134 r = register_blkdev(_major, _name);
136 kmem_cache_destroy(_tio_cache);
137 kmem_cache_destroy(_io_cache);
147 static void local_exit(void)
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
154 if (unregister_blkdev(_major, _name) < 0)
155 DMERR("devfs_unregister_blkdev failed");
159 DMINFO("cleaned up");
162 int (*_inits[])(void) __initdata = {
170 void (*_exits[])(void) = {
178 static int __init dm_init(void)
180 const int count = ARRAY_SIZE(_inits);
184 for (i = 0; i < count; i++) {
199 static void __exit dm_exit(void)
201 int i = ARRAY_SIZE(_exits);
208 * Block device functions
210 static int dm_blk_open(struct inode *inode, struct file *file)
212 struct mapped_device *md;
214 md = inode->i_bdev->bd_disk->private_data;
219 static int dm_blk_close(struct inode *inode, struct file *file)
221 struct mapped_device *md;
223 md = inode->i_bdev->bd_disk->private_data;
228 static inline struct dm_io *alloc_io(struct mapped_device *md)
230 return mempool_alloc(md->io_pool, GFP_NOIO);
233 static inline void free_io(struct mapped_device *md, struct dm_io *io)
235 mempool_free(io, md->io_pool);
238 static inline struct target_io *alloc_tio(struct mapped_device *md)
240 return mempool_alloc(md->tio_pool, GFP_NOIO);
243 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
245 mempool_free(tio, md->tio_pool);
248 static void start_io_acct(struct dm_io *io)
250 struct mapped_device *md = io->md;
252 io->start_time = jiffies;
255 disk_round_stats(dm_disk(md));
257 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
260 static int end_io_acct(struct dm_io *io)
262 struct mapped_device *md = io->md;
263 struct bio *bio = io->bio;
264 unsigned long duration = jiffies - io->start_time;
266 int rw = bio_data_dir(bio);
269 disk_round_stats(dm_disk(md));
271 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
273 disk_stat_add(dm_disk(md), ticks[rw], duration);
279 * Add the bio to the list of deferred io.
281 static int queue_io(struct mapped_device *md, struct bio *bio)
283 down_write(&md->io_lock);
285 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
286 up_write(&md->io_lock);
290 bio_list_add(&md->deferred, bio);
292 up_write(&md->io_lock);
293 return 0; /* deferred successfully */
297 * Everyone (including functions in this file), should use this
298 * function to access the md->map field, and make sure they call
299 * dm_table_put() when finished.
301 struct dm_table *dm_get_table(struct mapped_device *md)
305 read_lock(&md->map_lock);
309 read_unlock(&md->map_lock);
314 /*-----------------------------------------------------------------
316 * A more elegant soln is in the works that uses the queue
317 * merge fn, unfortunately there are a couple of changes to
318 * the block layer that I want to make for this. So in the
319 * interests of getting something for people to use I give
320 * you this clearly demarcated crap.
321 *---------------------------------------------------------------*/
324 * Decrements the number of outstanding ios that a bio has been
325 * cloned into, completing the original io if necc.
327 static void dec_pending(struct dm_io *io, int error)
332 if (atomic_dec_and_test(&io->io_count)) {
334 /* nudge anyone waiting on suspend queue */
335 wake_up(&io->md->wait);
337 bio_endio(io->bio, io->bio->bi_size, io->error);
342 static int clone_endio(struct bio *bio, unsigned int done, int error)
345 struct target_io *tio = bio->bi_private;
346 struct dm_io *io = tio->io;
347 dm_endio_fn endio = tio->ti->type->end_io;
352 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
356 r = endio(tio->ti, bio, error, &tio->info);
361 /* the target wants another shot at the io */
365 free_tio(io->md, tio);
366 dec_pending(io, error);
371 static sector_t max_io_len(struct mapped_device *md,
372 sector_t sector, struct dm_target *ti)
374 sector_t offset = sector - ti->begin;
375 sector_t len = ti->len - offset;
378 * Does the target need to split even further ?
382 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
391 static void __map_bio(struct dm_target *ti, struct bio *clone,
392 struct target_io *tio)
399 BUG_ON(!clone->bi_size);
401 clone->bi_end_io = clone_endio;
402 clone->bi_private = tio;
405 * Map the clone. If r == 0 we don't need to do
406 * anything, the target has assumed ownership of
409 atomic_inc(&tio->io->io_count);
410 r = ti->type->map(ti, clone, &tio->info);
412 /* the bio has been remapped so dispatch it */
413 generic_make_request(clone);
416 /* error the io and bail out */
417 struct dm_io *io = tio->io;
418 free_tio(tio->io->md, tio);
425 struct mapped_device *md;
426 struct dm_table *map;
430 sector_t sector_count;
434 static void dm_bio_destructor(struct bio *bio)
436 bio_free(bio, dm_set);
440 * Creates a little bio that is just does part of a bvec.
442 static struct bio *split_bvec(struct bio *bio, sector_t sector,
443 unsigned short idx, unsigned int offset,
447 struct bio_vec *bv = bio->bi_io_vec + idx;
449 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
450 clone->bi_destructor = dm_bio_destructor;
451 *clone->bi_io_vec = *bv;
453 clone->bi_sector = sector;
454 clone->bi_bdev = bio->bi_bdev;
455 clone->bi_rw = bio->bi_rw;
457 clone->bi_size = to_bytes(len);
458 clone->bi_io_vec->bv_offset = offset;
459 clone->bi_io_vec->bv_len = clone->bi_size;
465 * Creates a bio that consists of range of complete bvecs.
467 static struct bio *clone_bio(struct bio *bio, sector_t sector,
468 unsigned short idx, unsigned short bv_count,
473 clone = bio_clone(bio, GFP_NOIO);
474 clone->bi_sector = sector;
476 clone->bi_vcnt = idx + bv_count;
477 clone->bi_size = to_bytes(len);
478 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
483 static void __clone_and_map(struct clone_info *ci)
485 struct bio *clone, *bio = ci->bio;
486 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
487 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
488 struct target_io *tio;
491 * Allocate a target io object.
493 tio = alloc_tio(ci->md);
496 memset(&tio->info, 0, sizeof(tio->info));
498 if (ci->sector_count <= max) {
500 * Optimise for the simple case where we can do all of
501 * the remaining io with a single clone.
503 clone = clone_bio(bio, ci->sector, ci->idx,
504 bio->bi_vcnt - ci->idx, ci->sector_count);
505 __map_bio(ti, clone, tio);
506 ci->sector_count = 0;
508 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
510 * There are some bvecs that don't span targets.
511 * Do as many of these as possible.
514 sector_t remaining = max;
517 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
518 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
520 if (bv_len > remaining)
527 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
528 __map_bio(ti, clone, tio);
531 ci->sector_count -= len;
536 * Handle a bvec that must be split between two or more targets.
538 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
539 sector_t remaining = to_sector(bv->bv_len);
540 unsigned int offset = 0;
544 ti = dm_table_find_target(ci->map, ci->sector);
545 max = max_io_len(ci->md, ci->sector, ti);
547 tio = alloc_tio(ci->md);
550 memset(&tio->info, 0, sizeof(tio->info));
553 len = min(remaining, max);
555 clone = split_bvec(bio, ci->sector, ci->idx,
556 bv->bv_offset + offset, len);
558 __map_bio(ti, clone, tio);
561 ci->sector_count -= len;
562 offset += to_bytes(len);
563 } while (remaining -= len);
570 * Split the bio into several clones.
572 static void __split_bio(struct mapped_device *md, struct bio *bio)
574 struct clone_info ci;
576 ci.map = dm_get_table(md);
578 bio_io_error(bio, bio->bi_size);
584 ci.io = alloc_io(md);
586 atomic_set(&ci.io->io_count, 1);
589 ci.sector = bio->bi_sector;
590 ci.sector_count = bio_sectors(bio);
591 ci.idx = bio->bi_idx;
593 start_io_acct(ci.io);
594 while (ci.sector_count)
595 __clone_and_map(&ci);
597 /* drop the extra reference count */
598 dec_pending(ci.io, 0);
599 dm_table_put(ci.map);
601 /*-----------------------------------------------------------------
603 *---------------------------------------------------------------*/
606 * The request function that just remaps the bio built up by
609 static int dm_request(request_queue_t *q, struct bio *bio)
612 int rw = bio_data_dir(bio);
613 struct mapped_device *md = q->queuedata;
615 down_read(&md->io_lock);
617 disk_stat_inc(dm_disk(md), ios[rw]);
618 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
621 * If we're suspended we have to queue
624 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
625 up_read(&md->io_lock);
627 if (bio_rw(bio) == READA) {
628 bio_io_error(bio, bio->bi_size);
632 r = queue_io(md, bio);
634 bio_io_error(bio, bio->bi_size);
638 return 0; /* deferred successfully */
641 * We're in a while loop, because someone could suspend
642 * before we get to the following read lock.
644 down_read(&md->io_lock);
647 __split_bio(md, bio);
648 up_read(&md->io_lock);
652 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
653 sector_t *error_sector)
655 struct mapped_device *md = q->queuedata;
656 struct dm_table *map = dm_get_table(md);
660 ret = dm_table_flush_all(map);
667 static void dm_unplug_all(request_queue_t *q)
669 struct mapped_device *md = q->queuedata;
670 struct dm_table *map = dm_get_table(md);
673 dm_table_unplug_all(map);
678 static int dm_any_congested(void *congested_data, int bdi_bits)
681 struct mapped_device *md = (struct mapped_device *) congested_data;
682 struct dm_table *map = dm_get_table(md);
684 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
687 r = dm_table_any_congested(map, bdi_bits);
693 /*-----------------------------------------------------------------
694 * An IDR is used to keep track of allocated minor numbers.
695 *---------------------------------------------------------------*/
696 static DECLARE_MUTEX(_minor_lock);
697 static DEFINE_IDR(_minor_idr);
699 static void free_minor(unsigned int minor)
702 idr_remove(&_minor_idr, minor);
707 * See if the device with a specific minor # is free.
709 static int specific_minor(struct mapped_device *md, unsigned int minor)
713 if (minor >= (1 << MINORBITS))
718 if (idr_find(&_minor_idr, minor)) {
723 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
729 r = idr_get_new_above(&_minor_idr, md, minor, &m);
735 idr_remove(&_minor_idr, m);
745 static int next_free_minor(struct mapped_device *md, unsigned int *minor)
752 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
758 r = idr_get_new(&_minor_idr, md, &m);
763 if (m >= (1 << MINORBITS)) {
764 idr_remove(&_minor_idr, m);
776 static struct block_device_operations dm_blk_dops;
779 * Allocate and initialise a blank device with a given minor.
781 static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
784 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
787 DMWARN("unable to allocate device, out of memory.");
791 if (!try_module_get(THIS_MODULE))
794 /* get a minor number for the dev */
795 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
799 memset(md, 0, sizeof(*md));
800 init_rwsem(&md->io_lock);
801 init_MUTEX(&md->suspend_lock);
802 rwlock_init(&md->map_lock);
803 atomic_set(&md->holders, 1);
804 atomic_set(&md->event_nr, 0);
806 md->queue = blk_alloc_queue(GFP_KERNEL);
810 md->queue->queuedata = md;
811 md->queue->backing_dev_info.congested_fn = dm_any_congested;
812 md->queue->backing_dev_info.congested_data = md;
813 blk_queue_make_request(md->queue, dm_request);
814 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
815 md->queue->unplug_fn = dm_unplug_all;
816 md->queue->issue_flush_fn = dm_flush_all;
818 md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
819 mempool_free_slab, _io_cache);
823 md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
824 mempool_free_slab, _tio_cache);
828 md->disk = alloc_disk(1);
832 md->disk->major = _major;
833 md->disk->first_minor = minor;
834 md->disk->fops = &dm_blk_dops;
835 md->disk->queue = md->queue;
836 md->disk->private_data = md;
837 sprintf(md->disk->disk_name, "dm-%d", minor);
840 atomic_set(&md->pending, 0);
841 init_waitqueue_head(&md->wait);
842 init_waitqueue_head(&md->eventq);
847 mempool_destroy(md->tio_pool);
849 mempool_destroy(md->io_pool);
851 blk_put_queue(md->queue);
854 module_put(THIS_MODULE);
860 static void free_dev(struct mapped_device *md)
862 unsigned int minor = md->disk->first_minor;
864 if (md->suspended_bdev) {
865 thaw_bdev(md->suspended_bdev, NULL);
866 bdput(md->suspended_bdev);
868 mempool_destroy(md->tio_pool);
869 mempool_destroy(md->io_pool);
870 del_gendisk(md->disk);
873 blk_put_queue(md->queue);
874 module_put(THIS_MODULE);
879 * Bind a table to the device.
881 static void event_callback(void *context)
883 struct mapped_device *md = (struct mapped_device *) context;
885 atomic_inc(&md->event_nr);
886 wake_up(&md->eventq);
889 static void __set_size(struct mapped_device *md, sector_t size)
891 set_capacity(md->disk, size);
893 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
894 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
895 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
898 static int __bind(struct mapped_device *md, struct dm_table *t)
900 request_queue_t *q = md->queue;
903 size = dm_table_get_size(t);
904 __set_size(md, size);
909 dm_table_event_callback(t, event_callback, md);
911 write_lock(&md->map_lock);
913 dm_table_set_restrictions(t, q);
914 write_unlock(&md->map_lock);
919 static void __unbind(struct mapped_device *md)
921 struct dm_table *map = md->map;
926 dm_table_event_callback(map, NULL, NULL);
927 write_lock(&md->map_lock);
929 write_unlock(&md->map_lock);
934 * Constructor for a new device.
936 static int create_aux(unsigned int minor, int persistent,
937 struct mapped_device **result)
939 struct mapped_device *md;
941 md = alloc_dev(minor, persistent);
949 int dm_create(struct mapped_device **result)
951 return create_aux(0, 0, result);
954 int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
956 return create_aux(minor, 1, result);
959 static struct mapped_device *dm_find_md(dev_t dev)
961 struct mapped_device *md;
962 unsigned minor = MINOR(dev);
964 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
969 md = idr_find(&_minor_idr, minor);
970 if (!md || (dm_disk(md)->first_minor != minor))
978 struct mapped_device *dm_get_md(dev_t dev)
980 struct mapped_device *md = dm_find_md(dev);
988 void *dm_get_mdptr(dev_t dev)
990 struct mapped_device *md;
993 md = dm_find_md(dev);
995 mdptr = md->interface_ptr;
999 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1001 md->interface_ptr = ptr;
1004 void dm_get(struct mapped_device *md)
1006 atomic_inc(&md->holders);
1009 void dm_put(struct mapped_device *md)
1011 struct dm_table *map = dm_get_table(md);
1013 if (atomic_dec_and_test(&md->holders)) {
1014 if (!dm_suspended(md)) {
1015 dm_table_presuspend_targets(map);
1016 dm_table_postsuspend_targets(map);
1026 * Process the deferred bios
1028 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1041 * Swap in a new table (destroying old one).
1043 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1047 down(&md->suspend_lock);
1049 /* device must be suspended */
1050 if (!dm_suspended(md))
1054 r = __bind(md, table);
1057 up(&md->suspend_lock);
1062 * Functions to lock and unlock any filesystem running on the
1065 static int lock_fs(struct mapped_device *md)
1069 WARN_ON(md->frozen_sb);
1071 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1072 if (IS_ERR(md->frozen_sb)) {
1073 r = PTR_ERR(md->frozen_sb);
1074 md->frozen_sb = NULL;
1078 set_bit(DMF_FROZEN, &md->flags);
1080 /* don't bdput right now, we don't want the bdev
1081 * to go away while it is locked.
1086 static void unlock_fs(struct mapped_device *md)
1088 if (!test_bit(DMF_FROZEN, &md->flags))
1091 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1092 md->frozen_sb = NULL;
1093 clear_bit(DMF_FROZEN, &md->flags);
1097 * We need to be able to change a mapping table under a mounted
1098 * filesystem. For example we might want to move some data in
1099 * the background. Before the table can be swapped with
1100 * dm_bind_table, dm_suspend must be called to flush any in
1101 * flight bios and ensure that any further io gets deferred.
1103 int dm_suspend(struct mapped_device *md, int do_lockfs)
1105 struct dm_table *map = NULL;
1106 DECLARE_WAITQUEUE(wait, current);
1110 down(&md->suspend_lock);
1112 if (dm_suspended(md))
1115 map = dm_get_table(md);
1117 /* This does not get reverted if there's an error later. */
1118 dm_table_presuspend_targets(map);
1120 md->suspended_bdev = bdget_disk(md->disk, 0);
1121 if (!md->suspended_bdev) {
1122 DMWARN("bdget failed in dm_suspend");
1127 /* Flush I/O to the device. */
1135 * First we set the BLOCK_IO flag so no more ios will be mapped.
1137 down_write(&md->io_lock);
1138 set_bit(DMF_BLOCK_IO, &md->flags);
1140 add_wait_queue(&md->wait, &wait);
1141 up_write(&md->io_lock);
1145 dm_table_unplug_all(map);
1148 * Then we wait for the already mapped ios to
1152 set_current_state(TASK_INTERRUPTIBLE);
1154 if (!atomic_read(&md->pending) || signal_pending(current))
1159 set_current_state(TASK_RUNNING);
1161 down_write(&md->io_lock);
1162 remove_wait_queue(&md->wait, &wait);
1164 /* were we interrupted ? */
1166 if (atomic_read(&md->pending)) {
1167 clear_bit(DMF_BLOCK_IO, &md->flags);
1168 def = bio_list_get(&md->deferred);
1169 __flush_deferred_io(md, def);
1170 up_write(&md->io_lock);
1174 up_write(&md->io_lock);
1176 dm_table_postsuspend_targets(map);
1178 set_bit(DMF_SUSPENDED, &md->flags);
1183 if (r && md->suspended_bdev) {
1184 bdput(md->suspended_bdev);
1185 md->suspended_bdev = NULL;
1189 up(&md->suspend_lock);
1193 int dm_resume(struct mapped_device *md)
1197 struct dm_table *map = NULL;
1199 down(&md->suspend_lock);
1200 if (!dm_suspended(md))
1203 map = dm_get_table(md);
1204 if (!map || !dm_table_get_size(map))
1207 dm_table_resume_targets(map);
1209 down_write(&md->io_lock);
1210 clear_bit(DMF_BLOCK_IO, &md->flags);
1212 def = bio_list_get(&md->deferred);
1213 __flush_deferred_io(md, def);
1214 up_write(&md->io_lock);
1218 bdput(md->suspended_bdev);
1219 md->suspended_bdev = NULL;
1221 clear_bit(DMF_SUSPENDED, &md->flags);
1223 dm_table_unplug_all(map);
1229 up(&md->suspend_lock);
1234 /*-----------------------------------------------------------------
1235 * Event notification.
1236 *---------------------------------------------------------------*/
1237 uint32_t dm_get_event_nr(struct mapped_device *md)
1239 return atomic_read(&md->event_nr);
1242 int dm_wait_event(struct mapped_device *md, int event_nr)
1244 return wait_event_interruptible(md->eventq,
1245 (event_nr != atomic_read(&md->event_nr)));
1249 * The gendisk is only valid as long as you have a reference
1252 struct gendisk *dm_disk(struct mapped_device *md)
1257 int dm_suspended(struct mapped_device *md)
1259 return test_bit(DMF_SUSPENDED, &md->flags);
1262 static struct block_device_operations dm_blk_dops = {
1263 .open = dm_blk_open,
1264 .release = dm_blk_close,
1265 .owner = THIS_MODULE
1268 EXPORT_SYMBOL(dm_get_mapinfo);
1273 module_init(dm_init);
1274 module_exit(dm_exit);
1276 module_param(major, uint, 0);
1277 MODULE_PARM_DESC(major, "The major number of the device mapper");
1278 MODULE_DESCRIPTION(DM_NAME " driver");
1279 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1280 MODULE_LICENSE("GPL");