ACPI: thinkpad-acpi: keep track of module state
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm.c
blobfac09d52535a47bdbdc014464d51a9ee62a9fa5e
1 /*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004-2006 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>
23 #include <linux/smp_lock.h>
25 #define DM_MSG_PREFIX "core"
27 static const char *_name = DM_NAME;
29 static unsigned int major = 0;
30 static unsigned int _major = 0;
32 static DEFINE_SPINLOCK(_minor_lock);
34 * One of these is allocated per bio.
36 struct dm_io {
37 struct mapped_device *md;
38 int error;
39 struct bio *bio;
40 atomic_t io_count;
41 unsigned long start_time;
45 * One of these is allocated per target within a bio. Hopefully
46 * this will be simplified out one day.
48 struct dm_target_io {
49 struct dm_io *io;
50 struct dm_target *ti;
51 union map_info info;
54 union map_info *dm_get_mapinfo(struct bio *bio)
56 if (bio && bio->bi_private)
57 return &((struct dm_target_io *)bio->bi_private)->info;
58 return NULL;
61 #define MINOR_ALLOCED ((void *)-1)
64 * Bits for the md->flags field.
66 #define DMF_BLOCK_IO 0
67 #define DMF_SUSPENDED 1
68 #define DMF_FROZEN 2
69 #define DMF_FREEING 3
70 #define DMF_DELETING 4
71 #define DMF_NOFLUSH_SUSPENDING 5
73 struct mapped_device {
74 struct rw_semaphore io_lock;
75 struct semaphore suspend_lock;
76 spinlock_t pushback_lock;
77 rwlock_t map_lock;
78 atomic_t holders;
79 atomic_t open_count;
81 unsigned long flags;
83 struct request_queue *queue;
84 struct gendisk *disk;
85 char name[16];
87 void *interface_ptr;
90 * A list of ios that arrived while we were suspended.
92 atomic_t pending;
93 wait_queue_head_t wait;
94 struct bio_list deferred;
95 struct bio_list pushback;
98 * The current mapping.
100 struct dm_table *map;
103 * io objects are allocated from here.
105 mempool_t *io_pool;
106 mempool_t *tio_pool;
108 struct bio_set *bs;
111 * Event handling.
113 atomic_t event_nr;
114 wait_queue_head_t eventq;
117 * freeze/thaw support require holding onto a super block
119 struct super_block *frozen_sb;
120 struct block_device *suspended_bdev;
122 /* forced geometry settings */
123 struct hd_geometry geometry;
126 #define MIN_IOS 256
127 static struct kmem_cache *_io_cache;
128 static struct kmem_cache *_tio_cache;
130 static int __init local_init(void)
132 int r;
134 /* allocate a slab for the dm_ios */
135 _io_cache = KMEM_CACHE(dm_io, 0);
136 if (!_io_cache)
137 return -ENOMEM;
139 /* allocate a slab for the target ios */
140 _tio_cache = KMEM_CACHE(dm_target_io, 0);
141 if (!_tio_cache) {
142 kmem_cache_destroy(_io_cache);
143 return -ENOMEM;
146 _major = major;
147 r = register_blkdev(_major, _name);
148 if (r < 0) {
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
151 return r;
154 if (!_major)
155 _major = r;
157 return 0;
160 static void local_exit(void)
162 kmem_cache_destroy(_tio_cache);
163 kmem_cache_destroy(_io_cache);
164 unregister_blkdev(_major, _name);
166 _major = 0;
168 DMINFO("cleaned up");
171 int (*_inits[])(void) __initdata = {
172 local_init,
173 dm_target_init,
174 dm_linear_init,
175 dm_stripe_init,
176 dm_interface_init,
179 void (*_exits[])(void) = {
180 local_exit,
181 dm_target_exit,
182 dm_linear_exit,
183 dm_stripe_exit,
184 dm_interface_exit,
187 static int __init dm_init(void)
189 const int count = ARRAY_SIZE(_inits);
191 int r, i;
193 for (i = 0; i < count; i++) {
194 r = _inits[i]();
195 if (r)
196 goto bad;
199 return 0;
201 bad:
202 while (i--)
203 _exits[i]();
205 return r;
208 static void __exit dm_exit(void)
210 int i = ARRAY_SIZE(_exits);
212 while (i--)
213 _exits[i]();
217 * Block device functions
219 static int dm_blk_open(struct inode *inode, struct file *file)
221 struct mapped_device *md;
223 spin_lock(&_minor_lock);
225 md = inode->i_bdev->bd_disk->private_data;
226 if (!md)
227 goto out;
229 if (test_bit(DMF_FREEING, &md->flags) ||
230 test_bit(DMF_DELETING, &md->flags)) {
231 md = NULL;
232 goto out;
235 dm_get(md);
236 atomic_inc(&md->open_count);
238 out:
239 spin_unlock(&_minor_lock);
241 return md ? 0 : -ENXIO;
244 static int dm_blk_close(struct inode *inode, struct file *file)
246 struct mapped_device *md;
248 md = inode->i_bdev->bd_disk->private_data;
249 atomic_dec(&md->open_count);
250 dm_put(md);
251 return 0;
254 int dm_open_count(struct mapped_device *md)
256 return atomic_read(&md->open_count);
260 * Guarantees nothing is using the device before it's deleted.
262 int dm_lock_for_deletion(struct mapped_device *md)
264 int r = 0;
266 spin_lock(&_minor_lock);
268 if (dm_open_count(md))
269 r = -EBUSY;
270 else
271 set_bit(DMF_DELETING, &md->flags);
273 spin_unlock(&_minor_lock);
275 return r;
278 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
280 struct mapped_device *md = bdev->bd_disk->private_data;
282 return dm_get_geometry(md, geo);
285 static int dm_blk_ioctl(struct inode *inode, struct file *file,
286 unsigned int cmd, unsigned long arg)
288 struct mapped_device *md;
289 struct dm_table *map;
290 struct dm_target *tgt;
291 int r = -ENOTTY;
293 /* We don't really need this lock, but we do need 'inode'. */
294 unlock_kernel();
296 md = inode->i_bdev->bd_disk->private_data;
298 map = dm_get_table(md);
300 if (!map || !dm_table_get_size(map))
301 goto out;
303 /* We only support devices that have a single target */
304 if (dm_table_get_num_targets(map) != 1)
305 goto out;
307 tgt = dm_table_get_target(map, 0);
309 if (dm_suspended(md)) {
310 r = -EAGAIN;
311 goto out;
314 if (tgt->type->ioctl)
315 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
317 out:
318 dm_table_put(map);
320 lock_kernel();
321 return r;
324 static struct dm_io *alloc_io(struct mapped_device *md)
326 return mempool_alloc(md->io_pool, GFP_NOIO);
329 static void free_io(struct mapped_device *md, struct dm_io *io)
331 mempool_free(io, md->io_pool);
334 static struct dm_target_io *alloc_tio(struct mapped_device *md)
336 return mempool_alloc(md->tio_pool, GFP_NOIO);
339 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
341 mempool_free(tio, md->tio_pool);
344 static void start_io_acct(struct dm_io *io)
346 struct mapped_device *md = io->md;
348 io->start_time = jiffies;
350 preempt_disable();
351 disk_round_stats(dm_disk(md));
352 preempt_enable();
353 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
356 static int end_io_acct(struct dm_io *io)
358 struct mapped_device *md = io->md;
359 struct bio *bio = io->bio;
360 unsigned long duration = jiffies - io->start_time;
361 int pending;
362 int rw = bio_data_dir(bio);
364 preempt_disable();
365 disk_round_stats(dm_disk(md));
366 preempt_enable();
367 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
369 disk_stat_add(dm_disk(md), ticks[rw], duration);
371 return !pending;
375 * Add the bio to the list of deferred io.
377 static int queue_io(struct mapped_device *md, struct bio *bio)
379 down_write(&md->io_lock);
381 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
382 up_write(&md->io_lock);
383 return 1;
386 bio_list_add(&md->deferred, bio);
388 up_write(&md->io_lock);
389 return 0; /* deferred successfully */
393 * Everyone (including functions in this file), should use this
394 * function to access the md->map field, and make sure they call
395 * dm_table_put() when finished.
397 struct dm_table *dm_get_table(struct mapped_device *md)
399 struct dm_table *t;
401 read_lock(&md->map_lock);
402 t = md->map;
403 if (t)
404 dm_table_get(t);
405 read_unlock(&md->map_lock);
407 return t;
411 * Get the geometry associated with a dm device
413 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
415 *geo = md->geometry;
417 return 0;
421 * Set the geometry of a device.
423 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
425 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
427 if (geo->start > sz) {
428 DMWARN("Start sector is beyond the geometry limits.");
429 return -EINVAL;
432 md->geometry = *geo;
434 return 0;
437 /*-----------------------------------------------------------------
438 * CRUD START:
439 * A more elegant soln is in the works that uses the queue
440 * merge fn, unfortunately there are a couple of changes to
441 * the block layer that I want to make for this. So in the
442 * interests of getting something for people to use I give
443 * you this clearly demarcated crap.
444 *---------------------------------------------------------------*/
446 static int __noflush_suspending(struct mapped_device *md)
448 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
452 * Decrements the number of outstanding ios that a bio has been
453 * cloned into, completing the original io if necc.
455 static void dec_pending(struct dm_io *io, int error)
457 unsigned long flags;
459 /* Push-back supersedes any I/O errors */
460 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
461 io->error = error;
463 if (atomic_dec_and_test(&io->io_count)) {
464 if (io->error == DM_ENDIO_REQUEUE) {
466 * Target requested pushing back the I/O.
467 * This must be handled before the sleeper on
468 * suspend queue merges the pushback list.
470 spin_lock_irqsave(&io->md->pushback_lock, flags);
471 if (__noflush_suspending(io->md))
472 bio_list_add(&io->md->pushback, io->bio);
473 else
474 /* noflush suspend was interrupted. */
475 io->error = -EIO;
476 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
479 if (end_io_acct(io))
480 /* nudge anyone waiting on suspend queue */
481 wake_up(&io->md->wait);
483 if (io->error != DM_ENDIO_REQUEUE) {
484 blk_add_trace_bio(io->md->queue, io->bio,
485 BLK_TA_COMPLETE);
487 bio_endio(io->bio, io->bio->bi_size, io->error);
490 free_io(io->md, io);
494 static int clone_endio(struct bio *bio, unsigned int done, int error)
496 int r = 0;
497 struct dm_target_io *tio = bio->bi_private;
498 struct mapped_device *md = tio->io->md;
499 dm_endio_fn endio = tio->ti->type->end_io;
501 if (bio->bi_size)
502 return 1;
504 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
505 error = -EIO;
507 if (endio) {
508 r = endio(tio->ti, bio, error, &tio->info);
509 if (r < 0 || r == DM_ENDIO_REQUEUE)
511 * error and requeue request are handled
512 * in dec_pending().
514 error = r;
515 else if (r == DM_ENDIO_INCOMPLETE)
516 /* The target will handle the io */
517 return 1;
518 else if (r) {
519 DMWARN("unimplemented target endio return value: %d", r);
520 BUG();
524 dec_pending(tio->io, error);
527 * Store md for cleanup instead of tio which is about to get freed.
529 bio->bi_private = md->bs;
531 bio_put(bio);
532 free_tio(md, tio);
533 return r;
536 static sector_t max_io_len(struct mapped_device *md,
537 sector_t sector, struct dm_target *ti)
539 sector_t offset = sector - ti->begin;
540 sector_t len = ti->len - offset;
543 * Does the target need to split even further ?
545 if (ti->split_io) {
546 sector_t boundary;
547 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
548 - offset;
549 if (len > boundary)
550 len = boundary;
553 return len;
556 static void __map_bio(struct dm_target *ti, struct bio *clone,
557 struct dm_target_io *tio)
559 int r;
560 sector_t sector;
561 struct mapped_device *md;
564 * Sanity checks.
566 BUG_ON(!clone->bi_size);
568 clone->bi_end_io = clone_endio;
569 clone->bi_private = tio;
572 * Map the clone. If r == 0 we don't need to do
573 * anything, the target has assumed ownership of
574 * this io.
576 atomic_inc(&tio->io->io_count);
577 sector = clone->bi_sector;
578 r = ti->type->map(ti, clone, &tio->info);
579 if (r == DM_MAPIO_REMAPPED) {
580 /* the bio has been remapped so dispatch it */
582 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
583 tio->io->bio->bi_bdev->bd_dev,
584 clone->bi_sector, sector);
586 generic_make_request(clone);
587 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
588 /* error the io and bail out, or requeue it if needed */
589 md = tio->io->md;
590 dec_pending(tio->io, r);
592 * Store bio_set for cleanup.
594 clone->bi_private = md->bs;
595 bio_put(clone);
596 free_tio(md, tio);
597 } else if (r) {
598 DMWARN("unimplemented target map return value: %d", r);
599 BUG();
603 struct clone_info {
604 struct mapped_device *md;
605 struct dm_table *map;
606 struct bio *bio;
607 struct dm_io *io;
608 sector_t sector;
609 sector_t sector_count;
610 unsigned short idx;
613 static void dm_bio_destructor(struct bio *bio)
615 struct bio_set *bs = bio->bi_private;
617 bio_free(bio, bs);
621 * Creates a little bio that is just does part of a bvec.
623 static struct bio *split_bvec(struct bio *bio, sector_t sector,
624 unsigned short idx, unsigned int offset,
625 unsigned int len, struct bio_set *bs)
627 struct bio *clone;
628 struct bio_vec *bv = bio->bi_io_vec + idx;
630 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
631 clone->bi_destructor = dm_bio_destructor;
632 *clone->bi_io_vec = *bv;
634 clone->bi_sector = sector;
635 clone->bi_bdev = bio->bi_bdev;
636 clone->bi_rw = bio->bi_rw;
637 clone->bi_vcnt = 1;
638 clone->bi_size = to_bytes(len);
639 clone->bi_io_vec->bv_offset = offset;
640 clone->bi_io_vec->bv_len = clone->bi_size;
642 return clone;
646 * Creates a bio that consists of range of complete bvecs.
648 static struct bio *clone_bio(struct bio *bio, sector_t sector,
649 unsigned short idx, unsigned short bv_count,
650 unsigned int len, struct bio_set *bs)
652 struct bio *clone;
654 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
655 __bio_clone(clone, bio);
656 clone->bi_destructor = dm_bio_destructor;
657 clone->bi_sector = sector;
658 clone->bi_idx = idx;
659 clone->bi_vcnt = idx + bv_count;
660 clone->bi_size = to_bytes(len);
661 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
663 return clone;
666 static int __clone_and_map(struct clone_info *ci)
668 struct bio *clone, *bio = ci->bio;
669 struct dm_target *ti;
670 sector_t len = 0, max;
671 struct dm_target_io *tio;
673 ti = dm_table_find_target(ci->map, ci->sector);
674 if (!dm_target_is_valid(ti))
675 return -EIO;
677 max = max_io_len(ci->md, ci->sector, ti);
680 * Allocate a target io object.
682 tio = alloc_tio(ci->md);
683 tio->io = ci->io;
684 tio->ti = ti;
685 memset(&tio->info, 0, sizeof(tio->info));
687 if (ci->sector_count <= max) {
689 * Optimise for the simple case where we can do all of
690 * the remaining io with a single clone.
692 clone = clone_bio(bio, ci->sector, ci->idx,
693 bio->bi_vcnt - ci->idx, ci->sector_count,
694 ci->md->bs);
695 __map_bio(ti, clone, tio);
696 ci->sector_count = 0;
698 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
700 * There are some bvecs that don't span targets.
701 * Do as many of these as possible.
703 int i;
704 sector_t remaining = max;
705 sector_t bv_len;
707 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
708 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
710 if (bv_len > remaining)
711 break;
713 remaining -= bv_len;
714 len += bv_len;
717 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
718 ci->md->bs);
719 __map_bio(ti, clone, tio);
721 ci->sector += len;
722 ci->sector_count -= len;
723 ci->idx = i;
725 } else {
727 * Handle a bvec that must be split between two or more targets.
729 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
730 sector_t remaining = to_sector(bv->bv_len);
731 unsigned int offset = 0;
733 do {
734 if (offset) {
735 ti = dm_table_find_target(ci->map, ci->sector);
736 if (!dm_target_is_valid(ti))
737 return -EIO;
739 max = max_io_len(ci->md, ci->sector, ti);
741 tio = alloc_tio(ci->md);
742 tio->io = ci->io;
743 tio->ti = ti;
744 memset(&tio->info, 0, sizeof(tio->info));
747 len = min(remaining, max);
749 clone = split_bvec(bio, ci->sector, ci->idx,
750 bv->bv_offset + offset, len,
751 ci->md->bs);
753 __map_bio(ti, clone, tio);
755 ci->sector += len;
756 ci->sector_count -= len;
757 offset += to_bytes(len);
758 } while (remaining -= len);
760 ci->idx++;
763 return 0;
767 * Split the bio into several clones.
769 static void __split_bio(struct mapped_device *md, struct bio *bio)
771 struct clone_info ci;
772 int error = 0;
774 ci.map = dm_get_table(md);
775 if (!ci.map) {
776 bio_io_error(bio, bio->bi_size);
777 return;
780 ci.md = md;
781 ci.bio = bio;
782 ci.io = alloc_io(md);
783 ci.io->error = 0;
784 atomic_set(&ci.io->io_count, 1);
785 ci.io->bio = bio;
786 ci.io->md = md;
787 ci.sector = bio->bi_sector;
788 ci.sector_count = bio_sectors(bio);
789 ci.idx = bio->bi_idx;
791 start_io_acct(ci.io);
792 while (ci.sector_count && !error)
793 error = __clone_and_map(&ci);
795 /* drop the extra reference count */
796 dec_pending(ci.io, error);
797 dm_table_put(ci.map);
799 /*-----------------------------------------------------------------
800 * CRUD END
801 *---------------------------------------------------------------*/
804 * The request function that just remaps the bio built up by
805 * dm_merge_bvec.
807 static int dm_request(struct request_queue *q, struct bio *bio)
809 int r;
810 int rw = bio_data_dir(bio);
811 struct mapped_device *md = q->queuedata;
814 * There is no use in forwarding any barrier request since we can't
815 * guarantee it is (or can be) handled by the targets correctly.
817 if (unlikely(bio_barrier(bio))) {
818 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
819 return 0;
822 down_read(&md->io_lock);
824 disk_stat_inc(dm_disk(md), ios[rw]);
825 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
828 * If we're suspended we have to queue
829 * this io for later.
831 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
832 up_read(&md->io_lock);
834 if (bio_rw(bio) == READA) {
835 bio_io_error(bio, bio->bi_size);
836 return 0;
839 r = queue_io(md, bio);
840 if (r < 0) {
841 bio_io_error(bio, bio->bi_size);
842 return 0;
844 } else if (r == 0)
845 return 0; /* deferred successfully */
848 * We're in a while loop, because someone could suspend
849 * before we get to the following read lock.
851 down_read(&md->io_lock);
854 __split_bio(md, bio);
855 up_read(&md->io_lock);
856 return 0;
859 static int dm_flush_all(struct request_queue *q, struct gendisk *disk,
860 sector_t *error_sector)
862 struct mapped_device *md = q->queuedata;
863 struct dm_table *map = dm_get_table(md);
864 int ret = -ENXIO;
866 if (map) {
867 ret = dm_table_flush_all(map);
868 dm_table_put(map);
871 return ret;
874 static void dm_unplug_all(struct request_queue *q)
876 struct mapped_device *md = q->queuedata;
877 struct dm_table *map = dm_get_table(md);
879 if (map) {
880 dm_table_unplug_all(map);
881 dm_table_put(map);
885 static int dm_any_congested(void *congested_data, int bdi_bits)
887 int r;
888 struct mapped_device *md = (struct mapped_device *) congested_data;
889 struct dm_table *map = dm_get_table(md);
891 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
892 r = bdi_bits;
893 else
894 r = dm_table_any_congested(map, bdi_bits);
896 dm_table_put(map);
897 return r;
900 /*-----------------------------------------------------------------
901 * An IDR is used to keep track of allocated minor numbers.
902 *---------------------------------------------------------------*/
903 static DEFINE_IDR(_minor_idr);
905 static void free_minor(int minor)
907 spin_lock(&_minor_lock);
908 idr_remove(&_minor_idr, minor);
909 spin_unlock(&_minor_lock);
913 * See if the device with a specific minor # is free.
915 static int specific_minor(struct mapped_device *md, int minor)
917 int r, m;
919 if (minor >= (1 << MINORBITS))
920 return -EINVAL;
922 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
923 if (!r)
924 return -ENOMEM;
926 spin_lock(&_minor_lock);
928 if (idr_find(&_minor_idr, minor)) {
929 r = -EBUSY;
930 goto out;
933 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
934 if (r)
935 goto out;
937 if (m != minor) {
938 idr_remove(&_minor_idr, m);
939 r = -EBUSY;
940 goto out;
943 out:
944 spin_unlock(&_minor_lock);
945 return r;
948 static int next_free_minor(struct mapped_device *md, int *minor)
950 int r, m;
952 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
953 if (!r)
954 return -ENOMEM;
956 spin_lock(&_minor_lock);
958 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
959 if (r) {
960 goto out;
963 if (m >= (1 << MINORBITS)) {
964 idr_remove(&_minor_idr, m);
965 r = -ENOSPC;
966 goto out;
969 *minor = m;
971 out:
972 spin_unlock(&_minor_lock);
973 return r;
976 static struct block_device_operations dm_blk_dops;
979 * Allocate and initialise a blank device with a given minor.
981 static struct mapped_device *alloc_dev(int minor)
983 int r;
984 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
985 void *old_md;
987 if (!md) {
988 DMWARN("unable to allocate device, out of memory.");
989 return NULL;
992 if (!try_module_get(THIS_MODULE))
993 goto bad0;
995 /* get a minor number for the dev */
996 if (minor == DM_ANY_MINOR)
997 r = next_free_minor(md, &minor);
998 else
999 r = specific_minor(md, minor);
1000 if (r < 0)
1001 goto bad1;
1003 memset(md, 0, sizeof(*md));
1004 init_rwsem(&md->io_lock);
1005 init_MUTEX(&md->suspend_lock);
1006 spin_lock_init(&md->pushback_lock);
1007 rwlock_init(&md->map_lock);
1008 atomic_set(&md->holders, 1);
1009 atomic_set(&md->open_count, 0);
1010 atomic_set(&md->event_nr, 0);
1012 md->queue = blk_alloc_queue(GFP_KERNEL);
1013 if (!md->queue)
1014 goto bad1_free_minor;
1016 md->queue->queuedata = md;
1017 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1018 md->queue->backing_dev_info.congested_data = md;
1019 blk_queue_make_request(md->queue, dm_request);
1020 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1021 md->queue->unplug_fn = dm_unplug_all;
1022 md->queue->issue_flush_fn = dm_flush_all;
1024 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
1025 if (!md->io_pool)
1026 goto bad2;
1028 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1029 if (!md->tio_pool)
1030 goto bad3;
1032 md->bs = bioset_create(16, 16);
1033 if (!md->bs)
1034 goto bad_no_bioset;
1036 md->disk = alloc_disk(1);
1037 if (!md->disk)
1038 goto bad4;
1040 atomic_set(&md->pending, 0);
1041 init_waitqueue_head(&md->wait);
1042 init_waitqueue_head(&md->eventq);
1044 md->disk->major = _major;
1045 md->disk->first_minor = minor;
1046 md->disk->fops = &dm_blk_dops;
1047 md->disk->queue = md->queue;
1048 md->disk->private_data = md;
1049 sprintf(md->disk->disk_name, "dm-%d", minor);
1050 add_disk(md->disk);
1051 format_dev_t(md->name, MKDEV(_major, minor));
1053 /* Populate the mapping, nobody knows we exist yet */
1054 spin_lock(&_minor_lock);
1055 old_md = idr_replace(&_minor_idr, md, minor);
1056 spin_unlock(&_minor_lock);
1058 BUG_ON(old_md != MINOR_ALLOCED);
1060 return md;
1062 bad4:
1063 bioset_free(md->bs);
1064 bad_no_bioset:
1065 mempool_destroy(md->tio_pool);
1066 bad3:
1067 mempool_destroy(md->io_pool);
1068 bad2:
1069 blk_cleanup_queue(md->queue);
1070 bad1_free_minor:
1071 free_minor(minor);
1072 bad1:
1073 module_put(THIS_MODULE);
1074 bad0:
1075 kfree(md);
1076 return NULL;
1079 static void unlock_fs(struct mapped_device *md);
1081 static void free_dev(struct mapped_device *md)
1083 int minor = md->disk->first_minor;
1085 if (md->suspended_bdev) {
1086 unlock_fs(md);
1087 bdput(md->suspended_bdev);
1089 mempool_destroy(md->tio_pool);
1090 mempool_destroy(md->io_pool);
1091 bioset_free(md->bs);
1092 del_gendisk(md->disk);
1093 free_minor(minor);
1095 spin_lock(&_minor_lock);
1096 md->disk->private_data = NULL;
1097 spin_unlock(&_minor_lock);
1099 put_disk(md->disk);
1100 blk_cleanup_queue(md->queue);
1101 module_put(THIS_MODULE);
1102 kfree(md);
1106 * Bind a table to the device.
1108 static void event_callback(void *context)
1110 struct mapped_device *md = (struct mapped_device *) context;
1112 atomic_inc(&md->event_nr);
1113 wake_up(&md->eventq);
1116 static void __set_size(struct mapped_device *md, sector_t size)
1118 set_capacity(md->disk, size);
1120 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1121 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1122 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1125 static int __bind(struct mapped_device *md, struct dm_table *t)
1127 struct request_queue *q = md->queue;
1128 sector_t size;
1130 size = dm_table_get_size(t);
1133 * Wipe any geometry if the size of the table changed.
1135 if (size != get_capacity(md->disk))
1136 memset(&md->geometry, 0, sizeof(md->geometry));
1138 if (md->suspended_bdev)
1139 __set_size(md, size);
1140 if (size == 0)
1141 return 0;
1143 dm_table_get(t);
1144 dm_table_event_callback(t, event_callback, md);
1146 write_lock(&md->map_lock);
1147 md->map = t;
1148 dm_table_set_restrictions(t, q);
1149 write_unlock(&md->map_lock);
1151 return 0;
1154 static void __unbind(struct mapped_device *md)
1156 struct dm_table *map = md->map;
1158 if (!map)
1159 return;
1161 dm_table_event_callback(map, NULL, NULL);
1162 write_lock(&md->map_lock);
1163 md->map = NULL;
1164 write_unlock(&md->map_lock);
1165 dm_table_put(map);
1169 * Constructor for a new device.
1171 int dm_create(int minor, struct mapped_device **result)
1173 struct mapped_device *md;
1175 md = alloc_dev(minor);
1176 if (!md)
1177 return -ENXIO;
1179 *result = md;
1180 return 0;
1183 static struct mapped_device *dm_find_md(dev_t dev)
1185 struct mapped_device *md;
1186 unsigned minor = MINOR(dev);
1188 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1189 return NULL;
1191 spin_lock(&_minor_lock);
1193 md = idr_find(&_minor_idr, minor);
1194 if (md && (md == MINOR_ALLOCED ||
1195 (dm_disk(md)->first_minor != minor) ||
1196 test_bit(DMF_FREEING, &md->flags))) {
1197 md = NULL;
1198 goto out;
1201 out:
1202 spin_unlock(&_minor_lock);
1204 return md;
1207 struct mapped_device *dm_get_md(dev_t dev)
1209 struct mapped_device *md = dm_find_md(dev);
1211 if (md)
1212 dm_get(md);
1214 return md;
1217 void *dm_get_mdptr(struct mapped_device *md)
1219 return md->interface_ptr;
1222 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1224 md->interface_ptr = ptr;
1227 void dm_get(struct mapped_device *md)
1229 atomic_inc(&md->holders);
1232 const char *dm_device_name(struct mapped_device *md)
1234 return md->name;
1236 EXPORT_SYMBOL_GPL(dm_device_name);
1238 void dm_put(struct mapped_device *md)
1240 struct dm_table *map;
1242 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1244 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1245 map = dm_get_table(md);
1246 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1247 set_bit(DMF_FREEING, &md->flags);
1248 spin_unlock(&_minor_lock);
1249 if (!dm_suspended(md)) {
1250 dm_table_presuspend_targets(map);
1251 dm_table_postsuspend_targets(map);
1253 __unbind(md);
1254 dm_table_put(map);
1255 free_dev(md);
1258 EXPORT_SYMBOL_GPL(dm_put);
1261 * Process the deferred bios
1263 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1265 struct bio *n;
1267 while (c) {
1268 n = c->bi_next;
1269 c->bi_next = NULL;
1270 __split_bio(md, c);
1271 c = n;
1276 * Swap in a new table (destroying old one).
1278 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1280 int r = -EINVAL;
1282 down(&md->suspend_lock);
1284 /* device must be suspended */
1285 if (!dm_suspended(md))
1286 goto out;
1288 /* without bdev, the device size cannot be changed */
1289 if (!md->suspended_bdev)
1290 if (get_capacity(md->disk) != dm_table_get_size(table))
1291 goto out;
1293 __unbind(md);
1294 r = __bind(md, table);
1296 out:
1297 up(&md->suspend_lock);
1298 return r;
1302 * Functions to lock and unlock any filesystem running on the
1303 * device.
1305 static int lock_fs(struct mapped_device *md)
1307 int r;
1309 WARN_ON(md->frozen_sb);
1311 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1312 if (IS_ERR(md->frozen_sb)) {
1313 r = PTR_ERR(md->frozen_sb);
1314 md->frozen_sb = NULL;
1315 return r;
1318 set_bit(DMF_FROZEN, &md->flags);
1320 /* don't bdput right now, we don't want the bdev
1321 * to go away while it is locked.
1323 return 0;
1326 static void unlock_fs(struct mapped_device *md)
1328 if (!test_bit(DMF_FROZEN, &md->flags))
1329 return;
1331 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1332 md->frozen_sb = NULL;
1333 clear_bit(DMF_FROZEN, &md->flags);
1337 * We need to be able to change a mapping table under a mounted
1338 * filesystem. For example we might want to move some data in
1339 * the background. Before the table can be swapped with
1340 * dm_bind_table, dm_suspend must be called to flush any in
1341 * flight bios and ensure that any further io gets deferred.
1343 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1345 struct dm_table *map = NULL;
1346 unsigned long flags;
1347 DECLARE_WAITQUEUE(wait, current);
1348 struct bio *def;
1349 int r = -EINVAL;
1350 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1351 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1353 down(&md->suspend_lock);
1355 if (dm_suspended(md))
1356 goto out_unlock;
1358 map = dm_get_table(md);
1361 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1362 * This flag is cleared before dm_suspend returns.
1364 if (noflush)
1365 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1367 /* This does not get reverted if there's an error later. */
1368 dm_table_presuspend_targets(map);
1370 /* bdget() can stall if the pending I/Os are not flushed */
1371 if (!noflush) {
1372 md->suspended_bdev = bdget_disk(md->disk, 0);
1373 if (!md->suspended_bdev) {
1374 DMWARN("bdget failed in dm_suspend");
1375 r = -ENOMEM;
1376 goto flush_and_out;
1381 * Flush I/O to the device.
1382 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1384 if (do_lockfs && !noflush) {
1385 r = lock_fs(md);
1386 if (r)
1387 goto out;
1391 * First we set the BLOCK_IO flag so no more ios will be mapped.
1393 down_write(&md->io_lock);
1394 set_bit(DMF_BLOCK_IO, &md->flags);
1396 add_wait_queue(&md->wait, &wait);
1397 up_write(&md->io_lock);
1399 /* unplug */
1400 if (map)
1401 dm_table_unplug_all(map);
1404 * Then we wait for the already mapped ios to
1405 * complete.
1407 while (1) {
1408 set_current_state(TASK_INTERRUPTIBLE);
1410 if (!atomic_read(&md->pending) || signal_pending(current))
1411 break;
1413 io_schedule();
1415 set_current_state(TASK_RUNNING);
1417 down_write(&md->io_lock);
1418 remove_wait_queue(&md->wait, &wait);
1420 if (noflush) {
1421 spin_lock_irqsave(&md->pushback_lock, flags);
1422 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1423 bio_list_merge_head(&md->deferred, &md->pushback);
1424 bio_list_init(&md->pushback);
1425 spin_unlock_irqrestore(&md->pushback_lock, flags);
1428 /* were we interrupted ? */
1429 r = -EINTR;
1430 if (atomic_read(&md->pending)) {
1431 clear_bit(DMF_BLOCK_IO, &md->flags);
1432 def = bio_list_get(&md->deferred);
1433 __flush_deferred_io(md, def);
1434 up_write(&md->io_lock);
1435 unlock_fs(md);
1436 goto out; /* pushback list is already flushed, so skip flush */
1438 up_write(&md->io_lock);
1440 dm_table_postsuspend_targets(map);
1442 set_bit(DMF_SUSPENDED, &md->flags);
1444 r = 0;
1446 flush_and_out:
1447 if (r && noflush) {
1449 * Because there may be already I/Os in the pushback list,
1450 * flush them before return.
1452 down_write(&md->io_lock);
1454 spin_lock_irqsave(&md->pushback_lock, flags);
1455 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1456 bio_list_merge_head(&md->deferred, &md->pushback);
1457 bio_list_init(&md->pushback);
1458 spin_unlock_irqrestore(&md->pushback_lock, flags);
1460 def = bio_list_get(&md->deferred);
1461 __flush_deferred_io(md, def);
1462 up_write(&md->io_lock);
1465 out:
1466 if (r && md->suspended_bdev) {
1467 bdput(md->suspended_bdev);
1468 md->suspended_bdev = NULL;
1471 dm_table_put(map);
1473 out_unlock:
1474 up(&md->suspend_lock);
1475 return r;
1478 int dm_resume(struct mapped_device *md)
1480 int r = -EINVAL;
1481 struct bio *def;
1482 struct dm_table *map = NULL;
1484 down(&md->suspend_lock);
1485 if (!dm_suspended(md))
1486 goto out;
1488 map = dm_get_table(md);
1489 if (!map || !dm_table_get_size(map))
1490 goto out;
1492 r = dm_table_resume_targets(map);
1493 if (r)
1494 goto out;
1496 down_write(&md->io_lock);
1497 clear_bit(DMF_BLOCK_IO, &md->flags);
1499 def = bio_list_get(&md->deferred);
1500 __flush_deferred_io(md, def);
1501 up_write(&md->io_lock);
1503 unlock_fs(md);
1505 if (md->suspended_bdev) {
1506 bdput(md->suspended_bdev);
1507 md->suspended_bdev = NULL;
1510 clear_bit(DMF_SUSPENDED, &md->flags);
1512 dm_table_unplug_all(map);
1514 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1516 r = 0;
1518 out:
1519 dm_table_put(map);
1520 up(&md->suspend_lock);
1522 return r;
1525 /*-----------------------------------------------------------------
1526 * Event notification.
1527 *---------------------------------------------------------------*/
1528 uint32_t dm_get_event_nr(struct mapped_device *md)
1530 return atomic_read(&md->event_nr);
1533 int dm_wait_event(struct mapped_device *md, int event_nr)
1535 return wait_event_interruptible(md->eventq,
1536 (event_nr != atomic_read(&md->event_nr)));
1540 * The gendisk is only valid as long as you have a reference
1541 * count on 'md'.
1543 struct gendisk *dm_disk(struct mapped_device *md)
1545 return md->disk;
1548 int dm_suspended(struct mapped_device *md)
1550 return test_bit(DMF_SUSPENDED, &md->flags);
1553 int dm_noflush_suspending(struct dm_target *ti)
1555 struct mapped_device *md = dm_table_get_md(ti->table);
1556 int r = __noflush_suspending(md);
1558 dm_put(md);
1560 return r;
1562 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1564 static struct block_device_operations dm_blk_dops = {
1565 .open = dm_blk_open,
1566 .release = dm_blk_close,
1567 .ioctl = dm_blk_ioctl,
1568 .getgeo = dm_blk_getgeo,
1569 .owner = THIS_MODULE
1572 EXPORT_SYMBOL(dm_get_mapinfo);
1575 * module hooks
1577 module_init(dm_init);
1578 module_exit(dm_exit);
1580 module_param(major, uint, 0);
1581 MODULE_PARM_DESC(major, "The major number of the device mapper");
1582 MODULE_DESCRIPTION(DM_NAME " driver");
1583 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1584 MODULE_LICENSE("GPL");