Linux-2.6.12-rc2
[linux-2.6/kvm.git] / drivers / md / dm.c
blob243ff6884e83f4c0b44d1bbbfbb14ddb5384ffa8
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/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.
29 struct dm_io {
30 struct mapped_device *md;
31 int error;
32 struct bio *bio;
33 atomic_t io_count;
37 * One of these is allocated per target within a bio. Hopefully
38 * this will be simplified out one day.
40 struct target_io {
41 struct dm_io *io;
42 struct dm_target *ti;
43 union map_info info;
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;
50 return NULL;
54 * Bits for the md->flags field.
56 #define DMF_BLOCK_IO 0
57 #define DMF_SUSPENDED 1
58 #define DMF_FS_LOCKED 2
60 struct mapped_device {
61 struct rw_semaphore lock;
62 rwlock_t map_lock;
63 atomic_t holders;
65 unsigned long flags;
67 request_queue_t *queue;
68 struct gendisk *disk;
70 void *interface_ptr;
73 * A list of ios that arrived while we were suspended.
75 atomic_t pending;
76 wait_queue_head_t wait;
77 struct bio_list deferred;
80 * The current mapping.
82 struct dm_table *map;
85 * io objects are allocated from here.
87 mempool_t *io_pool;
88 mempool_t *tio_pool;
91 * Event handling.
93 atomic_t event_nr;
94 wait_queue_head_t eventq;
97 * freeze/thaw support require holding onto a super block
99 struct super_block *frozen_sb;
102 #define MIN_IOS 256
103 static kmem_cache_t *_io_cache;
104 static kmem_cache_t *_tio_cache;
106 static struct bio_set *dm_set;
108 static int __init local_init(void)
110 int r;
112 dm_set = bioset_create(16, 16, 4);
113 if (!dm_set)
114 return -ENOMEM;
116 /* allocate a slab for the dm_ios */
117 _io_cache = kmem_cache_create("dm_io",
118 sizeof(struct dm_io), 0, 0, NULL, NULL);
119 if (!_io_cache)
120 return -ENOMEM;
122 /* allocate a slab for the target ios */
123 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
124 0, 0, NULL, NULL);
125 if (!_tio_cache) {
126 kmem_cache_destroy(_io_cache);
127 return -ENOMEM;
130 _major = major;
131 r = register_blkdev(_major, _name);
132 if (r < 0) {
133 kmem_cache_destroy(_tio_cache);
134 kmem_cache_destroy(_io_cache);
135 return r;
138 if (!_major)
139 _major = r;
141 return 0;
144 static void local_exit(void)
146 kmem_cache_destroy(_tio_cache);
147 kmem_cache_destroy(_io_cache);
149 bioset_free(dm_set);
151 if (unregister_blkdev(_major, _name) < 0)
152 DMERR("devfs_unregister_blkdev failed");
154 _major = 0;
156 DMINFO("cleaned up");
159 int (*_inits[])(void) __initdata = {
160 local_init,
161 dm_target_init,
162 dm_linear_init,
163 dm_stripe_init,
164 dm_interface_init,
167 void (*_exits[])(void) = {
168 local_exit,
169 dm_target_exit,
170 dm_linear_exit,
171 dm_stripe_exit,
172 dm_interface_exit,
175 static int __init dm_init(void)
177 const int count = ARRAY_SIZE(_inits);
179 int r, i;
181 for (i = 0; i < count; i++) {
182 r = _inits[i]();
183 if (r)
184 goto bad;
187 return 0;
189 bad:
190 while (i--)
191 _exits[i]();
193 return r;
196 static void __exit dm_exit(void)
198 int i = ARRAY_SIZE(_exits);
200 while (i--)
201 _exits[i]();
205 * Block device functions
207 static int dm_blk_open(struct inode *inode, struct file *file)
209 struct mapped_device *md;
211 md = inode->i_bdev->bd_disk->private_data;
212 dm_get(md);
213 return 0;
216 static int dm_blk_close(struct inode *inode, struct file *file)
218 struct mapped_device *md;
220 md = inode->i_bdev->bd_disk->private_data;
221 dm_put(md);
222 return 0;
225 static inline struct dm_io *alloc_io(struct mapped_device *md)
227 return mempool_alloc(md->io_pool, GFP_NOIO);
230 static inline void free_io(struct mapped_device *md, struct dm_io *io)
232 mempool_free(io, md->io_pool);
235 static inline struct target_io *alloc_tio(struct mapped_device *md)
237 return mempool_alloc(md->tio_pool, GFP_NOIO);
240 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
242 mempool_free(tio, md->tio_pool);
246 * Add the bio to the list of deferred io.
248 static int queue_io(struct mapped_device *md, struct bio *bio)
250 down_write(&md->lock);
252 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
253 up_write(&md->lock);
254 return 1;
257 bio_list_add(&md->deferred, bio);
259 up_write(&md->lock);
260 return 0; /* deferred successfully */
264 * Everyone (including functions in this file), should use this
265 * function to access the md->map field, and make sure they call
266 * dm_table_put() when finished.
268 struct dm_table *dm_get_table(struct mapped_device *md)
270 struct dm_table *t;
272 read_lock(&md->map_lock);
273 t = md->map;
274 if (t)
275 dm_table_get(t);
276 read_unlock(&md->map_lock);
278 return t;
281 /*-----------------------------------------------------------------
282 * CRUD START:
283 * A more elegant soln is in the works that uses the queue
284 * merge fn, unfortunately there are a couple of changes to
285 * the block layer that I want to make for this. So in the
286 * interests of getting something for people to use I give
287 * you this clearly demarcated crap.
288 *---------------------------------------------------------------*/
291 * Decrements the number of outstanding ios that a bio has been
292 * cloned into, completing the original io if necc.
294 static inline void dec_pending(struct dm_io *io, int error)
296 if (error)
297 io->error = error;
299 if (atomic_dec_and_test(&io->io_count)) {
300 if (atomic_dec_and_test(&io->md->pending))
301 /* nudge anyone waiting on suspend queue */
302 wake_up(&io->md->wait);
304 bio_endio(io->bio, io->bio->bi_size, io->error);
305 free_io(io->md, io);
309 static int clone_endio(struct bio *bio, unsigned int done, int error)
311 int r = 0;
312 struct target_io *tio = bio->bi_private;
313 struct dm_io *io = tio->io;
314 dm_endio_fn endio = tio->ti->type->end_io;
316 if (bio->bi_size)
317 return 1;
319 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
320 error = -EIO;
322 if (endio) {
323 r = endio(tio->ti, bio, error, &tio->info);
324 if (r < 0)
325 error = r;
327 else if (r > 0)
328 /* the target wants another shot at the io */
329 return 1;
332 free_tio(io->md, tio);
333 dec_pending(io, error);
334 bio_put(bio);
335 return r;
338 static sector_t max_io_len(struct mapped_device *md,
339 sector_t sector, struct dm_target *ti)
341 sector_t offset = sector - ti->begin;
342 sector_t len = ti->len - offset;
345 * Does the target need to split even further ?
347 if (ti->split_io) {
348 sector_t boundary;
349 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
350 - offset;
351 if (len > boundary)
352 len = boundary;
355 return len;
358 static void __map_bio(struct dm_target *ti, struct bio *clone,
359 struct target_io *tio)
361 int r;
364 * Sanity checks.
366 BUG_ON(!clone->bi_size);
368 clone->bi_end_io = clone_endio;
369 clone->bi_private = tio;
372 * Map the clone. If r == 0 we don't need to do
373 * anything, the target has assumed ownership of
374 * this io.
376 atomic_inc(&tio->io->io_count);
377 r = ti->type->map(ti, clone, &tio->info);
378 if (r > 0)
379 /* the bio has been remapped so dispatch it */
380 generic_make_request(clone);
382 else if (r < 0) {
383 /* error the io and bail out */
384 struct dm_io *io = tio->io;
385 free_tio(tio->io->md, tio);
386 dec_pending(io, -EIO);
387 bio_put(clone);
391 struct clone_info {
392 struct mapped_device *md;
393 struct dm_table *map;
394 struct bio *bio;
395 struct dm_io *io;
396 sector_t sector;
397 sector_t sector_count;
398 unsigned short idx;
402 * Creates a little bio that is just does part of a bvec.
404 static struct bio *split_bvec(struct bio *bio, sector_t sector,
405 unsigned short idx, unsigned int offset,
406 unsigned int len)
408 struct bio *clone;
409 struct bio_vec *bv = bio->bi_io_vec + idx;
411 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
412 *clone->bi_io_vec = *bv;
414 clone->bi_sector = sector;
415 clone->bi_bdev = bio->bi_bdev;
416 clone->bi_rw = bio->bi_rw;
417 clone->bi_vcnt = 1;
418 clone->bi_size = to_bytes(len);
419 clone->bi_io_vec->bv_offset = offset;
420 clone->bi_io_vec->bv_len = clone->bi_size;
422 return clone;
426 * Creates a bio that consists of range of complete bvecs.
428 static struct bio *clone_bio(struct bio *bio, sector_t sector,
429 unsigned short idx, unsigned short bv_count,
430 unsigned int len)
432 struct bio *clone;
434 clone = bio_clone(bio, GFP_NOIO);
435 clone->bi_sector = sector;
436 clone->bi_idx = idx;
437 clone->bi_vcnt = idx + bv_count;
438 clone->bi_size = to_bytes(len);
439 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
441 return clone;
444 static void __clone_and_map(struct clone_info *ci)
446 struct bio *clone, *bio = ci->bio;
447 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
448 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
449 struct target_io *tio;
452 * Allocate a target io object.
454 tio = alloc_tio(ci->md);
455 tio->io = ci->io;
456 tio->ti = ti;
457 memset(&tio->info, 0, sizeof(tio->info));
459 if (ci->sector_count <= max) {
461 * Optimise for the simple case where we can do all of
462 * the remaining io with a single clone.
464 clone = clone_bio(bio, ci->sector, ci->idx,
465 bio->bi_vcnt - ci->idx, ci->sector_count);
466 __map_bio(ti, clone, tio);
467 ci->sector_count = 0;
469 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
471 * There are some bvecs that don't span targets.
472 * Do as many of these as possible.
474 int i;
475 sector_t remaining = max;
476 sector_t bv_len;
478 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
479 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
481 if (bv_len > remaining)
482 break;
484 remaining -= bv_len;
485 len += bv_len;
488 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
489 __map_bio(ti, clone, tio);
491 ci->sector += len;
492 ci->sector_count -= len;
493 ci->idx = i;
495 } else {
497 * Create two copy bios to deal with io that has
498 * been split across a target.
500 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
502 clone = split_bvec(bio, ci->sector, ci->idx,
503 bv->bv_offset, max);
504 __map_bio(ti, clone, tio);
506 ci->sector += max;
507 ci->sector_count -= max;
508 ti = dm_table_find_target(ci->map, ci->sector);
510 len = to_sector(bv->bv_len) - max;
511 clone = split_bvec(bio, ci->sector, ci->idx,
512 bv->bv_offset + to_bytes(max), len);
513 tio = alloc_tio(ci->md);
514 tio->io = ci->io;
515 tio->ti = ti;
516 memset(&tio->info, 0, sizeof(tio->info));
517 __map_bio(ti, clone, tio);
519 ci->sector += len;
520 ci->sector_count -= len;
521 ci->idx++;
526 * Split the bio into several clones.
528 static void __split_bio(struct mapped_device *md, struct bio *bio)
530 struct clone_info ci;
532 ci.map = dm_get_table(md);
533 if (!ci.map) {
534 bio_io_error(bio, bio->bi_size);
535 return;
538 ci.md = md;
539 ci.bio = bio;
540 ci.io = alloc_io(md);
541 ci.io->error = 0;
542 atomic_set(&ci.io->io_count, 1);
543 ci.io->bio = bio;
544 ci.io->md = md;
545 ci.sector = bio->bi_sector;
546 ci.sector_count = bio_sectors(bio);
547 ci.idx = bio->bi_idx;
549 atomic_inc(&md->pending);
550 while (ci.sector_count)
551 __clone_and_map(&ci);
553 /* drop the extra reference count */
554 dec_pending(ci.io, 0);
555 dm_table_put(ci.map);
557 /*-----------------------------------------------------------------
558 * CRUD END
559 *---------------------------------------------------------------*/
562 * The request function that just remaps the bio built up by
563 * dm_merge_bvec.
565 static int dm_request(request_queue_t *q, struct bio *bio)
567 int r;
568 struct mapped_device *md = q->queuedata;
570 down_read(&md->lock);
573 * If we're suspended we have to queue
574 * this io for later.
576 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
577 up_read(&md->lock);
579 if (bio_rw(bio) == READA) {
580 bio_io_error(bio, bio->bi_size);
581 return 0;
584 r = queue_io(md, bio);
585 if (r < 0) {
586 bio_io_error(bio, bio->bi_size);
587 return 0;
589 } else if (r == 0)
590 return 0; /* deferred successfully */
593 * We're in a while loop, because someone could suspend
594 * before we get to the following read lock.
596 down_read(&md->lock);
599 __split_bio(md, bio);
600 up_read(&md->lock);
601 return 0;
604 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
605 sector_t *error_sector)
607 struct mapped_device *md = q->queuedata;
608 struct dm_table *map = dm_get_table(md);
609 int ret = -ENXIO;
611 if (map) {
612 ret = dm_table_flush_all(md->map);
613 dm_table_put(map);
616 return ret;
619 static void dm_unplug_all(request_queue_t *q)
621 struct mapped_device *md = q->queuedata;
622 struct dm_table *map = dm_get_table(md);
624 if (map) {
625 dm_table_unplug_all(map);
626 dm_table_put(map);
630 static int dm_any_congested(void *congested_data, int bdi_bits)
632 int r;
633 struct mapped_device *md = (struct mapped_device *) congested_data;
634 struct dm_table *map = dm_get_table(md);
636 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
637 r = bdi_bits;
638 else
639 r = dm_table_any_congested(map, bdi_bits);
641 dm_table_put(map);
642 return r;
645 /*-----------------------------------------------------------------
646 * An IDR is used to keep track of allocated minor numbers.
647 *---------------------------------------------------------------*/
648 static DECLARE_MUTEX(_minor_lock);
649 static DEFINE_IDR(_minor_idr);
651 static void free_minor(unsigned int minor)
653 down(&_minor_lock);
654 idr_remove(&_minor_idr, minor);
655 up(&_minor_lock);
659 * See if the device with a specific minor # is free.
661 static int specific_minor(struct mapped_device *md, unsigned int minor)
663 int r, m;
665 if (minor >= (1 << MINORBITS))
666 return -EINVAL;
668 down(&_minor_lock);
670 if (idr_find(&_minor_idr, minor)) {
671 r = -EBUSY;
672 goto out;
675 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
676 if (!r) {
677 r = -ENOMEM;
678 goto out;
681 r = idr_get_new_above(&_minor_idr, md, minor, &m);
682 if (r) {
683 goto out;
686 if (m != minor) {
687 idr_remove(&_minor_idr, m);
688 r = -EBUSY;
689 goto out;
692 out:
693 up(&_minor_lock);
694 return r;
697 static int next_free_minor(struct mapped_device *md, unsigned int *minor)
699 int r;
700 unsigned int m;
702 down(&_minor_lock);
704 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
705 if (!r) {
706 r = -ENOMEM;
707 goto out;
710 r = idr_get_new(&_minor_idr, md, &m);
711 if (r) {
712 goto out;
715 if (m >= (1 << MINORBITS)) {
716 idr_remove(&_minor_idr, m);
717 r = -ENOSPC;
718 goto out;
721 *minor = m;
723 out:
724 up(&_minor_lock);
725 return r;
728 static struct block_device_operations dm_blk_dops;
731 * Allocate and initialise a blank device with a given minor.
733 static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
735 int r;
736 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
738 if (!md) {
739 DMWARN("unable to allocate device, out of memory.");
740 return NULL;
743 /* get a minor number for the dev */
744 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
745 if (r < 0)
746 goto bad1;
748 memset(md, 0, sizeof(*md));
749 init_rwsem(&md->lock);
750 rwlock_init(&md->map_lock);
751 atomic_set(&md->holders, 1);
752 atomic_set(&md->event_nr, 0);
754 md->queue = blk_alloc_queue(GFP_KERNEL);
755 if (!md->queue)
756 goto bad1;
758 md->queue->queuedata = md;
759 md->queue->backing_dev_info.congested_fn = dm_any_congested;
760 md->queue->backing_dev_info.congested_data = md;
761 blk_queue_make_request(md->queue, dm_request);
762 md->queue->unplug_fn = dm_unplug_all;
763 md->queue->issue_flush_fn = dm_flush_all;
765 md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
766 mempool_free_slab, _io_cache);
767 if (!md->io_pool)
768 goto bad2;
770 md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
771 mempool_free_slab, _tio_cache);
772 if (!md->tio_pool)
773 goto bad3;
775 md->disk = alloc_disk(1);
776 if (!md->disk)
777 goto bad4;
779 md->disk->major = _major;
780 md->disk->first_minor = minor;
781 md->disk->fops = &dm_blk_dops;
782 md->disk->queue = md->queue;
783 md->disk->private_data = md;
784 sprintf(md->disk->disk_name, "dm-%d", minor);
785 add_disk(md->disk);
787 atomic_set(&md->pending, 0);
788 init_waitqueue_head(&md->wait);
789 init_waitqueue_head(&md->eventq);
791 return md;
793 bad4:
794 mempool_destroy(md->tio_pool);
795 bad3:
796 mempool_destroy(md->io_pool);
797 bad2:
798 blk_put_queue(md->queue);
799 free_minor(minor);
800 bad1:
801 kfree(md);
802 return NULL;
805 static void free_dev(struct mapped_device *md)
807 free_minor(md->disk->first_minor);
808 mempool_destroy(md->tio_pool);
809 mempool_destroy(md->io_pool);
810 del_gendisk(md->disk);
811 put_disk(md->disk);
812 blk_put_queue(md->queue);
813 kfree(md);
817 * Bind a table to the device.
819 static void event_callback(void *context)
821 struct mapped_device *md = (struct mapped_device *) context;
823 atomic_inc(&md->event_nr);
824 wake_up(&md->eventq);
827 static void __set_size(struct gendisk *disk, sector_t size)
829 struct block_device *bdev;
831 set_capacity(disk, size);
832 bdev = bdget_disk(disk, 0);
833 if (bdev) {
834 down(&bdev->bd_inode->i_sem);
835 i_size_write(bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
836 up(&bdev->bd_inode->i_sem);
837 bdput(bdev);
841 static int __bind(struct mapped_device *md, struct dm_table *t)
843 request_queue_t *q = md->queue;
844 sector_t size;
846 size = dm_table_get_size(t);
847 __set_size(md->disk, size);
848 if (size == 0)
849 return 0;
851 write_lock(&md->map_lock);
852 md->map = t;
853 write_unlock(&md->map_lock);
855 dm_table_get(t);
856 dm_table_event_callback(md->map, event_callback, md);
857 dm_table_set_restrictions(t, q);
858 return 0;
861 static void __unbind(struct mapped_device *md)
863 struct dm_table *map = md->map;
865 if (!map)
866 return;
868 dm_table_event_callback(map, NULL, NULL);
869 write_lock(&md->map_lock);
870 md->map = NULL;
871 write_unlock(&md->map_lock);
872 dm_table_put(map);
876 * Constructor for a new device.
878 static int create_aux(unsigned int minor, int persistent,
879 struct mapped_device **result)
881 struct mapped_device *md;
883 md = alloc_dev(minor, persistent);
884 if (!md)
885 return -ENXIO;
887 *result = md;
888 return 0;
891 int dm_create(struct mapped_device **result)
893 return create_aux(0, 0, result);
896 int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
898 return create_aux(minor, 1, result);
901 void *dm_get_mdptr(dev_t dev)
903 struct mapped_device *md;
904 void *mdptr = NULL;
905 unsigned minor = MINOR(dev);
907 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
908 return NULL;
910 down(&_minor_lock);
912 md = idr_find(&_minor_idr, minor);
914 if (md && (dm_disk(md)->first_minor == minor))
915 mdptr = md->interface_ptr;
917 up(&_minor_lock);
919 return mdptr;
922 void dm_set_mdptr(struct mapped_device *md, void *ptr)
924 md->interface_ptr = ptr;
927 void dm_get(struct mapped_device *md)
929 atomic_inc(&md->holders);
932 void dm_put(struct mapped_device *md)
934 struct dm_table *map = dm_get_table(md);
936 if (atomic_dec_and_test(&md->holders)) {
937 if (!test_bit(DMF_SUSPENDED, &md->flags) && map) {
938 dm_table_presuspend_targets(map);
939 dm_table_postsuspend_targets(map);
941 __unbind(md);
942 free_dev(md);
945 dm_table_put(map);
949 * Process the deferred bios
951 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
953 struct bio *n;
955 while (c) {
956 n = c->bi_next;
957 c->bi_next = NULL;
958 __split_bio(md, c);
959 c = n;
964 * Swap in a new table (destroying old one).
966 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
968 int r;
970 down_write(&md->lock);
972 /* device must be suspended */
973 if (!test_bit(DMF_SUSPENDED, &md->flags)) {
974 up_write(&md->lock);
975 return -EPERM;
978 __unbind(md);
979 r = __bind(md, table);
980 if (r)
981 return r;
983 up_write(&md->lock);
984 return 0;
988 * Functions to lock and unlock any filesystem running on the
989 * device.
991 static int __lock_fs(struct mapped_device *md)
993 struct block_device *bdev;
995 if (test_and_set_bit(DMF_FS_LOCKED, &md->flags))
996 return 0;
998 bdev = bdget_disk(md->disk, 0);
999 if (!bdev) {
1000 DMWARN("bdget failed in __lock_fs");
1001 return -ENOMEM;
1004 WARN_ON(md->frozen_sb);
1005 md->frozen_sb = freeze_bdev(bdev);
1006 /* don't bdput right now, we don't want the bdev
1007 * to go away while it is locked. We'll bdput
1008 * in __unlock_fs
1010 return 0;
1013 static int __unlock_fs(struct mapped_device *md)
1015 struct block_device *bdev;
1017 if (!test_and_clear_bit(DMF_FS_LOCKED, &md->flags))
1018 return 0;
1020 bdev = bdget_disk(md->disk, 0);
1021 if (!bdev) {
1022 DMWARN("bdget failed in __unlock_fs");
1023 return -ENOMEM;
1026 thaw_bdev(bdev, md->frozen_sb);
1027 md->frozen_sb = NULL;
1028 bdput(bdev);
1029 bdput(bdev);
1030 return 0;
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;
1043 DECLARE_WAITQUEUE(wait, current);
1045 /* Flush I/O to the device. */
1046 down_read(&md->lock);
1047 if (test_bit(DMF_BLOCK_IO, &md->flags)) {
1048 up_read(&md->lock);
1049 return -EINVAL;
1052 map = dm_get_table(md);
1053 if (map)
1054 dm_table_presuspend_targets(map);
1055 __lock_fs(md);
1057 up_read(&md->lock);
1060 * First we set the BLOCK_IO flag so no more ios will be
1061 * mapped.
1063 down_write(&md->lock);
1064 if (test_bit(DMF_BLOCK_IO, &md->flags)) {
1066 * If we get here we know another thread is
1067 * trying to suspend as well, so we leave the fs
1068 * locked for this thread.
1070 up_write(&md->lock);
1071 return -EINVAL;
1074 set_bit(DMF_BLOCK_IO, &md->flags);
1075 add_wait_queue(&md->wait, &wait);
1076 up_write(&md->lock);
1078 /* unplug */
1079 if (map) {
1080 dm_table_unplug_all(map);
1081 dm_table_put(map);
1085 * Then we wait for the already mapped ios to
1086 * complete.
1088 while (1) {
1089 set_current_state(TASK_INTERRUPTIBLE);
1091 if (!atomic_read(&md->pending) || signal_pending(current))
1092 break;
1094 io_schedule();
1096 set_current_state(TASK_RUNNING);
1098 down_write(&md->lock);
1099 remove_wait_queue(&md->wait, &wait);
1101 /* were we interrupted ? */
1102 if (atomic_read(&md->pending)) {
1103 __unlock_fs(md);
1104 clear_bit(DMF_BLOCK_IO, &md->flags);
1105 up_write(&md->lock);
1106 return -EINTR;
1109 set_bit(DMF_SUSPENDED, &md->flags);
1111 map = dm_get_table(md);
1112 if (map)
1113 dm_table_postsuspend_targets(map);
1114 dm_table_put(map);
1115 up_write(&md->lock);
1117 return 0;
1120 int dm_resume(struct mapped_device *md)
1122 struct bio *def;
1123 struct dm_table *map = dm_get_table(md);
1125 down_write(&md->lock);
1126 if (!map ||
1127 !test_bit(DMF_SUSPENDED, &md->flags) ||
1128 !dm_table_get_size(map)) {
1129 up_write(&md->lock);
1130 dm_table_put(map);
1131 return -EINVAL;
1134 dm_table_resume_targets(map);
1135 clear_bit(DMF_SUSPENDED, &md->flags);
1136 clear_bit(DMF_BLOCK_IO, &md->flags);
1138 def = bio_list_get(&md->deferred);
1139 __flush_deferred_io(md, def);
1140 up_write(&md->lock);
1141 __unlock_fs(md);
1142 dm_table_unplug_all(map);
1143 dm_table_put(map);
1145 return 0;
1148 /*-----------------------------------------------------------------
1149 * Event notification.
1150 *---------------------------------------------------------------*/
1151 uint32_t dm_get_event_nr(struct mapped_device *md)
1153 return atomic_read(&md->event_nr);
1156 int dm_wait_event(struct mapped_device *md, int event_nr)
1158 return wait_event_interruptible(md->eventq,
1159 (event_nr != atomic_read(&md->event_nr)));
1163 * The gendisk is only valid as long as you have a reference
1164 * count on 'md'.
1166 struct gendisk *dm_disk(struct mapped_device *md)
1168 return md->disk;
1171 int dm_suspended(struct mapped_device *md)
1173 return test_bit(DMF_SUSPENDED, &md->flags);
1176 static struct block_device_operations dm_blk_dops = {
1177 .open = dm_blk_open,
1178 .release = dm_blk_close,
1179 .owner = THIS_MODULE
1182 EXPORT_SYMBOL(dm_get_mapinfo);
1185 * module hooks
1187 module_init(dm_init);
1188 module_exit(dm_exit);
1190 module_param(major, uint, 0);
1191 MODULE_PARM_DESC(major, "The major number of the device mapper");
1192 MODULE_DESCRIPTION(DM_NAME " driver");
1193 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1194 MODULE_LICENSE("GPL");