[CONNECTOR]: Initialize subsystem earlier.
[linux-2.6/verdex.git] / drivers / md / dm.c
blob4d710b7a133ba69570f0de74c5f7c78ec2aff63f
1 /*
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.
6 */
8 #include "dm.h"
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>
24 static const char *_name = DM_NAME;
26 static unsigned int major = 0;
27 static unsigned int _major = 0;
30 * One of these is allocated per bio.
32 struct dm_io {
33 struct mapped_device *md;
34 int error;
35 struct bio *bio;
36 atomic_t io_count;
37 unsigned long start_time;
41 * One of these is allocated per target within a bio. Hopefully
42 * this will be simplified out one day.
44 struct target_io {
45 struct dm_io *io;
46 struct dm_target *ti;
47 union map_info info;
50 union map_info *dm_get_mapinfo(struct bio *bio)
52 if (bio && bio->bi_private)
53 return &((struct target_io *)bio->bi_private)->info;
54 return NULL;
58 * Bits for the md->flags field.
60 #define DMF_BLOCK_IO 0
61 #define DMF_SUSPENDED 1
62 #define DMF_FROZEN 2
64 struct mapped_device {
65 struct rw_semaphore io_lock;
66 struct semaphore suspend_lock;
67 rwlock_t map_lock;
68 atomic_t holders;
70 unsigned long flags;
72 request_queue_t *queue;
73 struct gendisk *disk;
74 char name[16];
76 void *interface_ptr;
79 * A list of ios that arrived while we were suspended.
81 atomic_t pending;
82 wait_queue_head_t wait;
83 struct bio_list deferred;
86 * The current mapping.
88 struct dm_table *map;
91 * io objects are allocated from here.
93 mempool_t *io_pool;
94 mempool_t *tio_pool;
97 * Event handling.
99 atomic_t event_nr;
100 wait_queue_head_t eventq;
103 * freeze/thaw support require holding onto a super block
105 struct super_block *frozen_sb;
106 struct block_device *suspended_bdev;
108 /* forced geometry settings */
109 struct hd_geometry geometry;
112 #define MIN_IOS 256
113 static kmem_cache_t *_io_cache;
114 static kmem_cache_t *_tio_cache;
116 static struct bio_set *dm_set;
118 static int __init local_init(void)
120 int r;
122 dm_set = bioset_create(16, 16, 4);
123 if (!dm_set)
124 return -ENOMEM;
126 /* allocate a slab for the dm_ios */
127 _io_cache = kmem_cache_create("dm_io",
128 sizeof(struct dm_io), 0, 0, NULL, NULL);
129 if (!_io_cache)
130 return -ENOMEM;
132 /* allocate a slab for the target ios */
133 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
134 0, 0, NULL, NULL);
135 if (!_tio_cache) {
136 kmem_cache_destroy(_io_cache);
137 return -ENOMEM;
140 _major = major;
141 r = register_blkdev(_major, _name);
142 if (r < 0) {
143 kmem_cache_destroy(_tio_cache);
144 kmem_cache_destroy(_io_cache);
145 return r;
148 if (!_major)
149 _major = r;
151 return 0;
154 static void local_exit(void)
156 kmem_cache_destroy(_tio_cache);
157 kmem_cache_destroy(_io_cache);
159 bioset_free(dm_set);
161 if (unregister_blkdev(_major, _name) < 0)
162 DMERR("devfs_unregister_blkdev failed");
164 _major = 0;
166 DMINFO("cleaned up");
169 int (*_inits[])(void) __initdata = {
170 local_init,
171 dm_target_init,
172 dm_linear_init,
173 dm_stripe_init,
174 dm_interface_init,
177 void (*_exits[])(void) = {
178 local_exit,
179 dm_target_exit,
180 dm_linear_exit,
181 dm_stripe_exit,
182 dm_interface_exit,
185 static int __init dm_init(void)
187 const int count = ARRAY_SIZE(_inits);
189 int r, i;
191 for (i = 0; i < count; i++) {
192 r = _inits[i]();
193 if (r)
194 goto bad;
197 return 0;
199 bad:
200 while (i--)
201 _exits[i]();
203 return r;
206 static void __exit dm_exit(void)
208 int i = ARRAY_SIZE(_exits);
210 while (i--)
211 _exits[i]();
215 * Block device functions
217 static int dm_blk_open(struct inode *inode, struct file *file)
219 struct mapped_device *md;
221 md = inode->i_bdev->bd_disk->private_data;
222 dm_get(md);
223 return 0;
226 static int dm_blk_close(struct inode *inode, struct file *file)
228 struct mapped_device *md;
230 md = inode->i_bdev->bd_disk->private_data;
231 dm_put(md);
232 return 0;
235 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
237 struct mapped_device *md = bdev->bd_disk->private_data;
239 return dm_get_geometry(md, geo);
242 static inline struct dm_io *alloc_io(struct mapped_device *md)
244 return mempool_alloc(md->io_pool, GFP_NOIO);
247 static inline void free_io(struct mapped_device *md, struct dm_io *io)
249 mempool_free(io, md->io_pool);
252 static inline struct target_io *alloc_tio(struct mapped_device *md)
254 return mempool_alloc(md->tio_pool, GFP_NOIO);
257 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
259 mempool_free(tio, md->tio_pool);
262 static void start_io_acct(struct dm_io *io)
264 struct mapped_device *md = io->md;
266 io->start_time = jiffies;
268 preempt_disable();
269 disk_round_stats(dm_disk(md));
270 preempt_enable();
271 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
274 static int end_io_acct(struct dm_io *io)
276 struct mapped_device *md = io->md;
277 struct bio *bio = io->bio;
278 unsigned long duration = jiffies - io->start_time;
279 int pending;
280 int rw = bio_data_dir(bio);
282 preempt_disable();
283 disk_round_stats(dm_disk(md));
284 preempt_enable();
285 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
287 disk_stat_add(dm_disk(md), ticks[rw], duration);
289 return !pending;
293 * Add the bio to the list of deferred io.
295 static int queue_io(struct mapped_device *md, struct bio *bio)
297 down_write(&md->io_lock);
299 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
300 up_write(&md->io_lock);
301 return 1;
304 bio_list_add(&md->deferred, bio);
306 up_write(&md->io_lock);
307 return 0; /* deferred successfully */
311 * Everyone (including functions in this file), should use this
312 * function to access the md->map field, and make sure they call
313 * dm_table_put() when finished.
315 struct dm_table *dm_get_table(struct mapped_device *md)
317 struct dm_table *t;
319 read_lock(&md->map_lock);
320 t = md->map;
321 if (t)
322 dm_table_get(t);
323 read_unlock(&md->map_lock);
325 return t;
329 * Get the geometry associated with a dm device
331 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
333 *geo = md->geometry;
335 return 0;
339 * Set the geometry of a device.
341 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
343 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
345 if (geo->start > sz) {
346 DMWARN("Start sector is beyond the geometry limits.");
347 return -EINVAL;
350 md->geometry = *geo;
352 return 0;
355 /*-----------------------------------------------------------------
356 * CRUD START:
357 * A more elegant soln is in the works that uses the queue
358 * merge fn, unfortunately there are a couple of changes to
359 * the block layer that I want to make for this. So in the
360 * interests of getting something for people to use I give
361 * you this clearly demarcated crap.
362 *---------------------------------------------------------------*/
365 * Decrements the number of outstanding ios that a bio has been
366 * cloned into, completing the original io if necc.
368 static void dec_pending(struct dm_io *io, int error)
370 if (error)
371 io->error = error;
373 if (atomic_dec_and_test(&io->io_count)) {
374 if (end_io_acct(io))
375 /* nudge anyone waiting on suspend queue */
376 wake_up(&io->md->wait);
378 blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
380 bio_endio(io->bio, io->bio->bi_size, io->error);
381 free_io(io->md, io);
385 static int clone_endio(struct bio *bio, unsigned int done, int error)
387 int r = 0;
388 struct target_io *tio = bio->bi_private;
389 struct dm_io *io = tio->io;
390 dm_endio_fn endio = tio->ti->type->end_io;
392 if (bio->bi_size)
393 return 1;
395 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
396 error = -EIO;
398 if (endio) {
399 r = endio(tio->ti, bio, error, &tio->info);
400 if (r < 0)
401 error = r;
403 else if (r > 0)
404 /* the target wants another shot at the io */
405 return 1;
408 free_tio(io->md, tio);
409 dec_pending(io, error);
410 bio_put(bio);
411 return r;
414 static sector_t max_io_len(struct mapped_device *md,
415 sector_t sector, struct dm_target *ti)
417 sector_t offset = sector - ti->begin;
418 sector_t len = ti->len - offset;
421 * Does the target need to split even further ?
423 if (ti->split_io) {
424 sector_t boundary;
425 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
426 - offset;
427 if (len > boundary)
428 len = boundary;
431 return len;
434 static void __map_bio(struct dm_target *ti, struct bio *clone,
435 struct target_io *tio)
437 int r;
438 sector_t sector;
441 * Sanity checks.
443 BUG_ON(!clone->bi_size);
445 clone->bi_end_io = clone_endio;
446 clone->bi_private = tio;
449 * Map the clone. If r == 0 we don't need to do
450 * anything, the target has assumed ownership of
451 * this io.
453 atomic_inc(&tio->io->io_count);
454 sector = clone->bi_sector;
455 r = ti->type->map(ti, clone, &tio->info);
456 if (r > 0) {
457 /* the bio has been remapped so dispatch it */
459 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
460 tio->io->bio->bi_bdev->bd_dev, sector,
461 clone->bi_sector);
463 generic_make_request(clone);
466 else if (r < 0) {
467 /* error the io and bail out */
468 struct dm_io *io = tio->io;
469 free_tio(tio->io->md, tio);
470 dec_pending(io, r);
471 bio_put(clone);
475 struct clone_info {
476 struct mapped_device *md;
477 struct dm_table *map;
478 struct bio *bio;
479 struct dm_io *io;
480 sector_t sector;
481 sector_t sector_count;
482 unsigned short idx;
485 static void dm_bio_destructor(struct bio *bio)
487 bio_free(bio, dm_set);
491 * Creates a little bio that is just does part of a bvec.
493 static struct bio *split_bvec(struct bio *bio, sector_t sector,
494 unsigned short idx, unsigned int offset,
495 unsigned int len)
497 struct bio *clone;
498 struct bio_vec *bv = bio->bi_io_vec + idx;
500 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
501 clone->bi_destructor = dm_bio_destructor;
502 *clone->bi_io_vec = *bv;
504 clone->bi_sector = sector;
505 clone->bi_bdev = bio->bi_bdev;
506 clone->bi_rw = bio->bi_rw;
507 clone->bi_vcnt = 1;
508 clone->bi_size = to_bytes(len);
509 clone->bi_io_vec->bv_offset = offset;
510 clone->bi_io_vec->bv_len = clone->bi_size;
512 return clone;
516 * Creates a bio that consists of range of complete bvecs.
518 static struct bio *clone_bio(struct bio *bio, sector_t sector,
519 unsigned short idx, unsigned short bv_count,
520 unsigned int len)
522 struct bio *clone;
524 clone = bio_clone(bio, GFP_NOIO);
525 clone->bi_sector = sector;
526 clone->bi_idx = idx;
527 clone->bi_vcnt = idx + bv_count;
528 clone->bi_size = to_bytes(len);
529 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
531 return clone;
534 static void __clone_and_map(struct clone_info *ci)
536 struct bio *clone, *bio = ci->bio;
537 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
538 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
539 struct target_io *tio;
542 * Allocate a target io object.
544 tio = alloc_tio(ci->md);
545 tio->io = ci->io;
546 tio->ti = ti;
547 memset(&tio->info, 0, sizeof(tio->info));
549 if (ci->sector_count <= max) {
551 * Optimise for the simple case where we can do all of
552 * the remaining io with a single clone.
554 clone = clone_bio(bio, ci->sector, ci->idx,
555 bio->bi_vcnt - ci->idx, ci->sector_count);
556 __map_bio(ti, clone, tio);
557 ci->sector_count = 0;
559 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
561 * There are some bvecs that don't span targets.
562 * Do as many of these as possible.
564 int i;
565 sector_t remaining = max;
566 sector_t bv_len;
568 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
569 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
571 if (bv_len > remaining)
572 break;
574 remaining -= bv_len;
575 len += bv_len;
578 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
579 __map_bio(ti, clone, tio);
581 ci->sector += len;
582 ci->sector_count -= len;
583 ci->idx = i;
585 } else {
587 * Handle a bvec that must be split between two or more targets.
589 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
590 sector_t remaining = to_sector(bv->bv_len);
591 unsigned int offset = 0;
593 do {
594 if (offset) {
595 ti = dm_table_find_target(ci->map, ci->sector);
596 max = max_io_len(ci->md, ci->sector, ti);
598 tio = alloc_tio(ci->md);
599 tio->io = ci->io;
600 tio->ti = ti;
601 memset(&tio->info, 0, sizeof(tio->info));
604 len = min(remaining, max);
606 clone = split_bvec(bio, ci->sector, ci->idx,
607 bv->bv_offset + offset, len);
609 __map_bio(ti, clone, tio);
611 ci->sector += len;
612 ci->sector_count -= len;
613 offset += to_bytes(len);
614 } while (remaining -= len);
616 ci->idx++;
621 * Split the bio into several clones.
623 static void __split_bio(struct mapped_device *md, struct bio *bio)
625 struct clone_info ci;
627 ci.map = dm_get_table(md);
628 if (!ci.map) {
629 bio_io_error(bio, bio->bi_size);
630 return;
633 ci.md = md;
634 ci.bio = bio;
635 ci.io = alloc_io(md);
636 ci.io->error = 0;
637 atomic_set(&ci.io->io_count, 1);
638 ci.io->bio = bio;
639 ci.io->md = md;
640 ci.sector = bio->bi_sector;
641 ci.sector_count = bio_sectors(bio);
642 ci.idx = bio->bi_idx;
644 start_io_acct(ci.io);
645 while (ci.sector_count)
646 __clone_and_map(&ci);
648 /* drop the extra reference count */
649 dec_pending(ci.io, 0);
650 dm_table_put(ci.map);
652 /*-----------------------------------------------------------------
653 * CRUD END
654 *---------------------------------------------------------------*/
657 * The request function that just remaps the bio built up by
658 * dm_merge_bvec.
660 static int dm_request(request_queue_t *q, struct bio *bio)
662 int r;
663 int rw = bio_data_dir(bio);
664 struct mapped_device *md = q->queuedata;
666 down_read(&md->io_lock);
668 disk_stat_inc(dm_disk(md), ios[rw]);
669 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
672 * If we're suspended we have to queue
673 * this io for later.
675 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
676 up_read(&md->io_lock);
678 if (bio_rw(bio) == READA) {
679 bio_io_error(bio, bio->bi_size);
680 return 0;
683 r = queue_io(md, bio);
684 if (r < 0) {
685 bio_io_error(bio, bio->bi_size);
686 return 0;
688 } else if (r == 0)
689 return 0; /* deferred successfully */
692 * We're in a while loop, because someone could suspend
693 * before we get to the following read lock.
695 down_read(&md->io_lock);
698 __split_bio(md, bio);
699 up_read(&md->io_lock);
700 return 0;
703 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
704 sector_t *error_sector)
706 struct mapped_device *md = q->queuedata;
707 struct dm_table *map = dm_get_table(md);
708 int ret = -ENXIO;
710 if (map) {
711 ret = dm_table_flush_all(map);
712 dm_table_put(map);
715 return ret;
718 static void dm_unplug_all(request_queue_t *q)
720 struct mapped_device *md = q->queuedata;
721 struct dm_table *map = dm_get_table(md);
723 if (map) {
724 dm_table_unplug_all(map);
725 dm_table_put(map);
729 static int dm_any_congested(void *congested_data, int bdi_bits)
731 int r;
732 struct mapped_device *md = (struct mapped_device *) congested_data;
733 struct dm_table *map = dm_get_table(md);
735 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
736 r = bdi_bits;
737 else
738 r = dm_table_any_congested(map, bdi_bits);
740 dm_table_put(map);
741 return r;
744 /*-----------------------------------------------------------------
745 * An IDR is used to keep track of allocated minor numbers.
746 *---------------------------------------------------------------*/
747 static DEFINE_MUTEX(_minor_lock);
748 static DEFINE_IDR(_minor_idr);
750 static void free_minor(unsigned int minor)
752 mutex_lock(&_minor_lock);
753 idr_remove(&_minor_idr, minor);
754 mutex_unlock(&_minor_lock);
758 * See if the device with a specific minor # is free.
760 static int specific_minor(struct mapped_device *md, unsigned int minor)
762 int r, m;
764 if (minor >= (1 << MINORBITS))
765 return -EINVAL;
767 mutex_lock(&_minor_lock);
769 if (idr_find(&_minor_idr, minor)) {
770 r = -EBUSY;
771 goto out;
774 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
775 if (!r) {
776 r = -ENOMEM;
777 goto out;
780 r = idr_get_new_above(&_minor_idr, md, minor, &m);
781 if (r) {
782 goto out;
785 if (m != minor) {
786 idr_remove(&_minor_idr, m);
787 r = -EBUSY;
788 goto out;
791 out:
792 mutex_unlock(&_minor_lock);
793 return r;
796 static int next_free_minor(struct mapped_device *md, unsigned int *minor)
798 int r;
799 unsigned int m;
801 mutex_lock(&_minor_lock);
803 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
804 if (!r) {
805 r = -ENOMEM;
806 goto out;
809 r = idr_get_new(&_minor_idr, md, &m);
810 if (r) {
811 goto out;
814 if (m >= (1 << MINORBITS)) {
815 idr_remove(&_minor_idr, m);
816 r = -ENOSPC;
817 goto out;
820 *minor = m;
822 out:
823 mutex_unlock(&_minor_lock);
824 return r;
827 static struct block_device_operations dm_blk_dops;
830 * Allocate and initialise a blank device with a given minor.
832 static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
834 int r;
835 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
837 if (!md) {
838 DMWARN("unable to allocate device, out of memory.");
839 return NULL;
842 /* get a minor number for the dev */
843 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
844 if (r < 0)
845 goto bad1;
847 memset(md, 0, sizeof(*md));
848 init_rwsem(&md->io_lock);
849 init_MUTEX(&md->suspend_lock);
850 rwlock_init(&md->map_lock);
851 atomic_set(&md->holders, 1);
852 atomic_set(&md->event_nr, 0);
854 md->queue = blk_alloc_queue(GFP_KERNEL);
855 if (!md->queue)
856 goto bad1;
858 md->queue->queuedata = md;
859 md->queue->backing_dev_info.congested_fn = dm_any_congested;
860 md->queue->backing_dev_info.congested_data = md;
861 blk_queue_make_request(md->queue, dm_request);
862 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
863 md->queue->unplug_fn = dm_unplug_all;
864 md->queue->issue_flush_fn = dm_flush_all;
866 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
867 if (!md->io_pool)
868 goto bad2;
870 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
871 if (!md->tio_pool)
872 goto bad3;
874 md->disk = alloc_disk(1);
875 if (!md->disk)
876 goto bad4;
878 md->disk->major = _major;
879 md->disk->first_minor = minor;
880 md->disk->fops = &dm_blk_dops;
881 md->disk->queue = md->queue;
882 md->disk->private_data = md;
883 sprintf(md->disk->disk_name, "dm-%d", minor);
884 add_disk(md->disk);
885 format_dev_t(md->name, MKDEV(_major, minor));
887 atomic_set(&md->pending, 0);
888 init_waitqueue_head(&md->wait);
889 init_waitqueue_head(&md->eventq);
891 return md;
893 bad4:
894 mempool_destroy(md->tio_pool);
895 bad3:
896 mempool_destroy(md->io_pool);
897 bad2:
898 blk_cleanup_queue(md->queue);
899 free_minor(minor);
900 bad1:
901 kfree(md);
902 return NULL;
905 static void free_dev(struct mapped_device *md)
907 unsigned int minor = md->disk->first_minor;
909 if (md->suspended_bdev) {
910 thaw_bdev(md->suspended_bdev, NULL);
911 bdput(md->suspended_bdev);
913 mempool_destroy(md->tio_pool);
914 mempool_destroy(md->io_pool);
915 del_gendisk(md->disk);
916 free_minor(minor);
917 put_disk(md->disk);
918 blk_cleanup_queue(md->queue);
919 kfree(md);
923 * Bind a table to the device.
925 static void event_callback(void *context)
927 struct mapped_device *md = (struct mapped_device *) context;
929 atomic_inc(&md->event_nr);
930 wake_up(&md->eventq);
933 static void __set_size(struct mapped_device *md, sector_t size)
935 set_capacity(md->disk, size);
937 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
938 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
939 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
942 static int __bind(struct mapped_device *md, struct dm_table *t)
944 request_queue_t *q = md->queue;
945 sector_t size;
947 size = dm_table_get_size(t);
950 * Wipe any geometry if the size of the table changed.
952 if (size != get_capacity(md->disk))
953 memset(&md->geometry, 0, sizeof(md->geometry));
955 __set_size(md, size);
956 if (size == 0)
957 return 0;
959 dm_table_get(t);
960 dm_table_event_callback(t, event_callback, md);
962 write_lock(&md->map_lock);
963 md->map = t;
964 dm_table_set_restrictions(t, q);
965 write_unlock(&md->map_lock);
967 return 0;
970 static void __unbind(struct mapped_device *md)
972 struct dm_table *map = md->map;
974 if (!map)
975 return;
977 dm_table_event_callback(map, NULL, NULL);
978 write_lock(&md->map_lock);
979 md->map = NULL;
980 write_unlock(&md->map_lock);
981 dm_table_put(map);
985 * Constructor for a new device.
987 static int create_aux(unsigned int minor, int persistent,
988 struct mapped_device **result)
990 struct mapped_device *md;
992 md = alloc_dev(minor, persistent);
993 if (!md)
994 return -ENXIO;
996 *result = md;
997 return 0;
1000 int dm_create(struct mapped_device **result)
1002 return create_aux(0, 0, result);
1005 int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
1007 return create_aux(minor, 1, result);
1010 static struct mapped_device *dm_find_md(dev_t dev)
1012 struct mapped_device *md;
1013 unsigned minor = MINOR(dev);
1015 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1016 return NULL;
1018 mutex_lock(&_minor_lock);
1020 md = idr_find(&_minor_idr, minor);
1021 if (!md || (dm_disk(md)->first_minor != minor))
1022 md = NULL;
1024 mutex_unlock(&_minor_lock);
1026 return md;
1029 struct mapped_device *dm_get_md(dev_t dev)
1031 struct mapped_device *md = dm_find_md(dev);
1033 if (md)
1034 dm_get(md);
1036 return md;
1039 void *dm_get_mdptr(struct mapped_device *md)
1041 return md->interface_ptr;
1044 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1046 md->interface_ptr = ptr;
1049 void dm_get(struct mapped_device *md)
1051 atomic_inc(&md->holders);
1054 void dm_put(struct mapped_device *md)
1056 struct dm_table *map;
1058 if (atomic_dec_and_test(&md->holders)) {
1059 map = dm_get_table(md);
1060 if (!dm_suspended(md)) {
1061 dm_table_presuspend_targets(map);
1062 dm_table_postsuspend_targets(map);
1064 __unbind(md);
1065 dm_table_put(map);
1066 free_dev(md);
1071 * Process the deferred bios
1073 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1075 struct bio *n;
1077 while (c) {
1078 n = c->bi_next;
1079 c->bi_next = NULL;
1080 __split_bio(md, c);
1081 c = n;
1086 * Swap in a new table (destroying old one).
1088 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1090 int r = -EINVAL;
1092 down(&md->suspend_lock);
1094 /* device must be suspended */
1095 if (!dm_suspended(md))
1096 goto out;
1098 __unbind(md);
1099 r = __bind(md, table);
1101 out:
1102 up(&md->suspend_lock);
1103 return r;
1107 * Functions to lock and unlock any filesystem running on the
1108 * device.
1110 static int lock_fs(struct mapped_device *md)
1112 int r;
1114 WARN_ON(md->frozen_sb);
1116 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1117 if (IS_ERR(md->frozen_sb)) {
1118 r = PTR_ERR(md->frozen_sb);
1119 md->frozen_sb = NULL;
1120 return r;
1123 set_bit(DMF_FROZEN, &md->flags);
1125 /* don't bdput right now, we don't want the bdev
1126 * to go away while it is locked.
1128 return 0;
1131 static void unlock_fs(struct mapped_device *md)
1133 if (!test_bit(DMF_FROZEN, &md->flags))
1134 return;
1136 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1137 md->frozen_sb = NULL;
1138 clear_bit(DMF_FROZEN, &md->flags);
1142 * We need to be able to change a mapping table under a mounted
1143 * filesystem. For example we might want to move some data in
1144 * the background. Before the table can be swapped with
1145 * dm_bind_table, dm_suspend must be called to flush any in
1146 * flight bios and ensure that any further io gets deferred.
1148 int dm_suspend(struct mapped_device *md, int do_lockfs)
1150 struct dm_table *map = NULL;
1151 DECLARE_WAITQUEUE(wait, current);
1152 struct bio *def;
1153 int r = -EINVAL;
1155 down(&md->suspend_lock);
1157 if (dm_suspended(md))
1158 goto out;
1160 map = dm_get_table(md);
1162 /* This does not get reverted if there's an error later. */
1163 dm_table_presuspend_targets(map);
1165 md->suspended_bdev = bdget_disk(md->disk, 0);
1166 if (!md->suspended_bdev) {
1167 DMWARN("bdget failed in dm_suspend");
1168 r = -ENOMEM;
1169 goto out;
1172 /* Flush I/O to the device. */
1173 if (do_lockfs) {
1174 r = lock_fs(md);
1175 if (r)
1176 goto out;
1180 * First we set the BLOCK_IO flag so no more ios will be mapped.
1182 down_write(&md->io_lock);
1183 set_bit(DMF_BLOCK_IO, &md->flags);
1185 add_wait_queue(&md->wait, &wait);
1186 up_write(&md->io_lock);
1188 /* unplug */
1189 if (map)
1190 dm_table_unplug_all(map);
1193 * Then we wait for the already mapped ios to
1194 * complete.
1196 while (1) {
1197 set_current_state(TASK_INTERRUPTIBLE);
1199 if (!atomic_read(&md->pending) || signal_pending(current))
1200 break;
1202 io_schedule();
1204 set_current_state(TASK_RUNNING);
1206 down_write(&md->io_lock);
1207 remove_wait_queue(&md->wait, &wait);
1209 /* were we interrupted ? */
1210 r = -EINTR;
1211 if (atomic_read(&md->pending)) {
1212 clear_bit(DMF_BLOCK_IO, &md->flags);
1213 def = bio_list_get(&md->deferred);
1214 __flush_deferred_io(md, def);
1215 up_write(&md->io_lock);
1216 unlock_fs(md);
1217 goto out;
1219 up_write(&md->io_lock);
1221 dm_table_postsuspend_targets(map);
1223 set_bit(DMF_SUSPENDED, &md->flags);
1225 r = 0;
1227 out:
1228 if (r && md->suspended_bdev) {
1229 bdput(md->suspended_bdev);
1230 md->suspended_bdev = NULL;
1233 dm_table_put(map);
1234 up(&md->suspend_lock);
1235 return r;
1238 int dm_resume(struct mapped_device *md)
1240 int r = -EINVAL;
1241 struct bio *def;
1242 struct dm_table *map = NULL;
1244 down(&md->suspend_lock);
1245 if (!dm_suspended(md))
1246 goto out;
1248 map = dm_get_table(md);
1249 if (!map || !dm_table_get_size(map))
1250 goto out;
1252 dm_table_resume_targets(map);
1254 down_write(&md->io_lock);
1255 clear_bit(DMF_BLOCK_IO, &md->flags);
1257 def = bio_list_get(&md->deferred);
1258 __flush_deferred_io(md, def);
1259 up_write(&md->io_lock);
1261 unlock_fs(md);
1263 bdput(md->suspended_bdev);
1264 md->suspended_bdev = NULL;
1266 clear_bit(DMF_SUSPENDED, &md->flags);
1268 dm_table_unplug_all(map);
1270 r = 0;
1272 out:
1273 dm_table_put(map);
1274 up(&md->suspend_lock);
1276 return r;
1279 /*-----------------------------------------------------------------
1280 * Event notification.
1281 *---------------------------------------------------------------*/
1282 uint32_t dm_get_event_nr(struct mapped_device *md)
1284 return atomic_read(&md->event_nr);
1287 int dm_wait_event(struct mapped_device *md, int event_nr)
1289 return wait_event_interruptible(md->eventq,
1290 (event_nr != atomic_read(&md->event_nr)));
1294 * The gendisk is only valid as long as you have a reference
1295 * count on 'md'.
1297 struct gendisk *dm_disk(struct mapped_device *md)
1299 return md->disk;
1302 int dm_suspended(struct mapped_device *md)
1304 return test_bit(DMF_SUSPENDED, &md->flags);
1307 static struct block_device_operations dm_blk_dops = {
1308 .open = dm_blk_open,
1309 .release = dm_blk_close,
1310 .getgeo = dm_blk_getgeo,
1311 .owner = THIS_MODULE
1314 EXPORT_SYMBOL(dm_get_mapinfo);
1317 * module hooks
1319 module_init(dm_init);
1320 module_exit(dm_exit);
1322 module_param(major, uint, 0);
1323 MODULE_PARM_DESC(major, "The major number of the device mapper");
1324 MODULE_DESCRIPTION(DM_NAME " driver");
1325 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1326 MODULE_LICENSE("GPL");