allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / md / dm.c
blob75bd2fd8575e12418db71bf2f15a49c6e1b3a096
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 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 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 request_queue_t *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_create("dm_io",
136 sizeof(struct dm_io), 0, 0, NULL, NULL);
137 if (!_io_cache)
138 return -ENOMEM;
140 /* allocate a slab for the target ios */
141 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
142 0, 0, NULL, NULL);
143 if (!_tio_cache) {
144 kmem_cache_destroy(_io_cache);
145 return -ENOMEM;
148 _major = major;
149 r = register_blkdev(_major, _name);
150 if (r < 0) {
151 kmem_cache_destroy(_tio_cache);
152 kmem_cache_destroy(_io_cache);
153 return r;
156 if (!_major)
157 _major = r;
159 return 0;
162 static void local_exit(void)
164 kmem_cache_destroy(_tio_cache);
165 kmem_cache_destroy(_io_cache);
167 if (unregister_blkdev(_major, _name) < 0)
168 DMERR("unregister_blkdev failed");
170 _major = 0;
172 DMINFO("cleaned up");
175 int (*_inits[])(void) __initdata = {
176 local_init,
177 dm_target_init,
178 dm_linear_init,
179 dm_stripe_init,
180 dm_interface_init,
183 void (*_exits[])(void) = {
184 local_exit,
185 dm_target_exit,
186 dm_linear_exit,
187 dm_stripe_exit,
188 dm_interface_exit,
191 static int __init dm_init(void)
193 const int count = ARRAY_SIZE(_inits);
195 int r, i;
197 for (i = 0; i < count; i++) {
198 r = _inits[i]();
199 if (r)
200 goto bad;
203 return 0;
205 bad:
206 while (i--)
207 _exits[i]();
209 return r;
212 static void __exit dm_exit(void)
214 int i = ARRAY_SIZE(_exits);
216 while (i--)
217 _exits[i]();
221 * Block device functions
223 static int dm_blk_open(struct inode *inode, struct file *file)
225 struct mapped_device *md;
227 spin_lock(&_minor_lock);
229 md = inode->i_bdev->bd_disk->private_data;
230 if (!md)
231 goto out;
233 if (test_bit(DMF_FREEING, &md->flags) ||
234 test_bit(DMF_DELETING, &md->flags)) {
235 md = NULL;
236 goto out;
239 dm_get(md);
240 atomic_inc(&md->open_count);
242 out:
243 spin_unlock(&_minor_lock);
245 return md ? 0 : -ENXIO;
248 static int dm_blk_close(struct inode *inode, struct file *file)
250 struct mapped_device *md;
252 md = inode->i_bdev->bd_disk->private_data;
253 atomic_dec(&md->open_count);
254 dm_put(md);
255 return 0;
258 int dm_open_count(struct mapped_device *md)
260 return atomic_read(&md->open_count);
264 * Guarantees nothing is using the device before it's deleted.
266 int dm_lock_for_deletion(struct mapped_device *md)
268 int r = 0;
270 spin_lock(&_minor_lock);
272 if (dm_open_count(md))
273 r = -EBUSY;
274 else
275 set_bit(DMF_DELETING, &md->flags);
277 spin_unlock(&_minor_lock);
279 return r;
282 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
284 struct mapped_device *md = bdev->bd_disk->private_data;
286 return dm_get_geometry(md, geo);
289 static int dm_blk_ioctl(struct inode *inode, struct file *file,
290 unsigned int cmd, unsigned long arg)
292 struct mapped_device *md;
293 struct dm_table *map;
294 struct dm_target *tgt;
295 int r = -ENOTTY;
297 /* We don't really need this lock, but we do need 'inode'. */
298 unlock_kernel();
300 md = inode->i_bdev->bd_disk->private_data;
302 map = dm_get_table(md);
304 if (!map || !dm_table_get_size(map))
305 goto out;
307 /* We only support devices that have a single target */
308 if (dm_table_get_num_targets(map) != 1)
309 goto out;
311 tgt = dm_table_get_target(map, 0);
313 if (dm_suspended(md)) {
314 r = -EAGAIN;
315 goto out;
318 if (tgt->type->ioctl)
319 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
321 out:
322 dm_table_put(map);
324 lock_kernel();
325 return r;
328 static inline struct dm_io *alloc_io(struct mapped_device *md)
330 return mempool_alloc(md->io_pool, GFP_NOIO);
333 static inline void free_io(struct mapped_device *md, struct dm_io *io)
335 mempool_free(io, md->io_pool);
338 static inline struct target_io *alloc_tio(struct mapped_device *md)
340 return mempool_alloc(md->tio_pool, GFP_NOIO);
343 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
345 mempool_free(tio, md->tio_pool);
348 static void start_io_acct(struct dm_io *io)
350 struct mapped_device *md = io->md;
352 io->start_time = jiffies;
354 preempt_disable();
355 disk_round_stats(dm_disk(md));
356 preempt_enable();
357 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
360 static int end_io_acct(struct dm_io *io)
362 struct mapped_device *md = io->md;
363 struct bio *bio = io->bio;
364 unsigned long duration = jiffies - io->start_time;
365 int pending;
366 int rw = bio_data_dir(bio);
368 preempt_disable();
369 disk_round_stats(dm_disk(md));
370 preempt_enable();
371 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
373 disk_stat_add(dm_disk(md), ticks[rw], duration);
375 return !pending;
379 * Add the bio to the list of deferred io.
381 static int queue_io(struct mapped_device *md, struct bio *bio)
383 down_write(&md->io_lock);
385 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
386 up_write(&md->io_lock);
387 return 1;
390 bio_list_add(&md->deferred, bio);
392 up_write(&md->io_lock);
393 return 0; /* deferred successfully */
397 * Everyone (including functions in this file), should use this
398 * function to access the md->map field, and make sure they call
399 * dm_table_put() when finished.
401 struct dm_table *dm_get_table(struct mapped_device *md)
403 struct dm_table *t;
405 read_lock(&md->map_lock);
406 t = md->map;
407 if (t)
408 dm_table_get(t);
409 read_unlock(&md->map_lock);
411 return t;
415 * Get the geometry associated with a dm device
417 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
419 *geo = md->geometry;
421 return 0;
425 * Set the geometry of a device.
427 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
429 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
431 if (geo->start > sz) {
432 DMWARN("Start sector is beyond the geometry limits.");
433 return -EINVAL;
436 md->geometry = *geo;
438 return 0;
441 /*-----------------------------------------------------------------
442 * CRUD START:
443 * A more elegant soln is in the works that uses the queue
444 * merge fn, unfortunately there are a couple of changes to
445 * the block layer that I want to make for this. So in the
446 * interests of getting something for people to use I give
447 * you this clearly demarcated crap.
448 *---------------------------------------------------------------*/
450 static int __noflush_suspending(struct mapped_device *md)
452 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
456 * Decrements the number of outstanding ios that a bio has been
457 * cloned into, completing the original io if necc.
459 static void dec_pending(struct dm_io *io, int error)
461 unsigned long flags;
463 /* Push-back supersedes any I/O errors */
464 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
465 io->error = error;
467 if (atomic_dec_and_test(&io->io_count)) {
468 if (io->error == DM_ENDIO_REQUEUE) {
470 * Target requested pushing back the I/O.
471 * This must be handled before the sleeper on
472 * suspend queue merges the pushback list.
474 spin_lock_irqsave(&io->md->pushback_lock, flags);
475 if (__noflush_suspending(io->md))
476 bio_list_add(&io->md->pushback, io->bio);
477 else
478 /* noflush suspend was interrupted. */
479 io->error = -EIO;
480 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
483 if (end_io_acct(io))
484 /* nudge anyone waiting on suspend queue */
485 wake_up(&io->md->wait);
487 if (io->error != DM_ENDIO_REQUEUE) {
488 blk_add_trace_bio(io->md->queue, io->bio,
489 BLK_TA_COMPLETE);
491 bio_endio(io->bio, io->bio->bi_size, io->error);
494 free_io(io->md, io);
498 static int clone_endio(struct bio *bio, unsigned int done, int error)
500 int r = 0;
501 struct target_io *tio = bio->bi_private;
502 struct mapped_device *md = tio->io->md;
503 dm_endio_fn endio = tio->ti->type->end_io;
505 if (bio->bi_size)
506 return 1;
508 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
509 error = -EIO;
511 if (endio) {
512 r = endio(tio->ti, bio, error, &tio->info);
513 if (r < 0 || r == DM_ENDIO_REQUEUE)
515 * error and requeue request are handled
516 * in dec_pending().
518 error = r;
519 else if (r == DM_ENDIO_INCOMPLETE)
520 /* The target will handle the io */
521 return 1;
522 else if (r) {
523 DMWARN("unimplemented target endio return value: %d", r);
524 BUG();
528 dec_pending(tio->io, error);
531 * Store md for cleanup instead of tio which is about to get freed.
533 bio->bi_private = md->bs;
535 bio_put(bio);
536 free_tio(md, tio);
537 return r;
540 static sector_t max_io_len(struct mapped_device *md,
541 sector_t sector, struct dm_target *ti)
543 sector_t offset = sector - ti->begin;
544 sector_t len = ti->len - offset;
547 * Does the target need to split even further ?
549 if (ti->split_io) {
550 sector_t boundary;
551 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
552 - offset;
553 if (len > boundary)
554 len = boundary;
557 return len;
560 static void __map_bio(struct dm_target *ti, struct bio *clone,
561 struct target_io *tio)
563 int r;
564 sector_t sector;
565 struct mapped_device *md;
568 * Sanity checks.
570 BUG_ON(!clone->bi_size);
572 clone->bi_end_io = clone_endio;
573 clone->bi_private = tio;
576 * Map the clone. If r == 0 we don't need to do
577 * anything, the target has assumed ownership of
578 * this io.
580 atomic_inc(&tio->io->io_count);
581 sector = clone->bi_sector;
582 r = ti->type->map(ti, clone, &tio->info);
583 if (r == DM_MAPIO_REMAPPED) {
584 /* the bio has been remapped so dispatch it */
586 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
587 tio->io->bio->bi_bdev->bd_dev, sector,
588 clone->bi_sector);
590 generic_make_request(clone);
591 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
592 /* error the io and bail out, or requeue it if needed */
593 md = tio->io->md;
594 dec_pending(tio->io, r);
596 * Store bio_set for cleanup.
598 clone->bi_private = md->bs;
599 bio_put(clone);
600 free_tio(md, tio);
601 } else if (r) {
602 DMWARN("unimplemented target map return value: %d", r);
603 BUG();
607 struct clone_info {
608 struct mapped_device *md;
609 struct dm_table *map;
610 struct bio *bio;
611 struct dm_io *io;
612 sector_t sector;
613 sector_t sector_count;
614 unsigned short idx;
617 static void dm_bio_destructor(struct bio *bio)
619 struct bio_set *bs = bio->bi_private;
621 bio_free(bio, bs);
625 * Creates a little bio that is just does part of a bvec.
627 static struct bio *split_bvec(struct bio *bio, sector_t sector,
628 unsigned short idx, unsigned int offset,
629 unsigned int len, struct bio_set *bs)
631 struct bio *clone;
632 struct bio_vec *bv = bio->bi_io_vec + idx;
634 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
635 clone->bi_destructor = dm_bio_destructor;
636 *clone->bi_io_vec = *bv;
638 clone->bi_sector = sector;
639 clone->bi_bdev = bio->bi_bdev;
640 clone->bi_rw = bio->bi_rw;
641 clone->bi_vcnt = 1;
642 clone->bi_size = to_bytes(len);
643 clone->bi_io_vec->bv_offset = offset;
644 clone->bi_io_vec->bv_len = clone->bi_size;
646 return clone;
650 * Creates a bio that consists of range of complete bvecs.
652 static struct bio *clone_bio(struct bio *bio, sector_t sector,
653 unsigned short idx, unsigned short bv_count,
654 unsigned int len, struct bio_set *bs)
656 struct bio *clone;
658 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
659 __bio_clone(clone, bio);
660 clone->bi_destructor = dm_bio_destructor;
661 clone->bi_sector = sector;
662 clone->bi_idx = idx;
663 clone->bi_vcnt = idx + bv_count;
664 clone->bi_size = to_bytes(len);
665 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
667 return clone;
670 static void __clone_and_map(struct clone_info *ci)
672 struct bio *clone, *bio = ci->bio;
673 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
674 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
675 struct target_io *tio;
678 * Allocate a target io object.
680 tio = alloc_tio(ci->md);
681 tio->io = ci->io;
682 tio->ti = ti;
683 memset(&tio->info, 0, sizeof(tio->info));
685 if (ci->sector_count <= max) {
687 * Optimise for the simple case where we can do all of
688 * the remaining io with a single clone.
690 clone = clone_bio(bio, ci->sector, ci->idx,
691 bio->bi_vcnt - ci->idx, ci->sector_count,
692 ci->md->bs);
693 __map_bio(ti, clone, tio);
694 ci->sector_count = 0;
696 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
698 * There are some bvecs that don't span targets.
699 * Do as many of these as possible.
701 int i;
702 sector_t remaining = max;
703 sector_t bv_len;
705 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
706 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
708 if (bv_len > remaining)
709 break;
711 remaining -= bv_len;
712 len += bv_len;
715 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
716 ci->md->bs);
717 __map_bio(ti, clone, tio);
719 ci->sector += len;
720 ci->sector_count -= len;
721 ci->idx = i;
723 } else {
725 * Handle a bvec that must be split between two or more targets.
727 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
728 sector_t remaining = to_sector(bv->bv_len);
729 unsigned int offset = 0;
731 do {
732 if (offset) {
733 ti = dm_table_find_target(ci->map, ci->sector);
734 max = max_io_len(ci->md, ci->sector, ti);
736 tio = alloc_tio(ci->md);
737 tio->io = ci->io;
738 tio->ti = ti;
739 memset(&tio->info, 0, sizeof(tio->info));
742 len = min(remaining, max);
744 clone = split_bvec(bio, ci->sector, ci->idx,
745 bv->bv_offset + offset, len,
746 ci->md->bs);
748 __map_bio(ti, clone, tio);
750 ci->sector += len;
751 ci->sector_count -= len;
752 offset += to_bytes(len);
753 } while (remaining -= len);
755 ci->idx++;
760 * Split the bio into several clones.
762 static void __split_bio(struct mapped_device *md, struct bio *bio)
764 struct clone_info ci;
766 ci.map = dm_get_table(md);
767 if (!ci.map) {
768 bio_io_error(bio, bio->bi_size);
769 return;
772 ci.md = md;
773 ci.bio = bio;
774 ci.io = alloc_io(md);
775 ci.io->error = 0;
776 atomic_set(&ci.io->io_count, 1);
777 ci.io->bio = bio;
778 ci.io->md = md;
779 ci.sector = bio->bi_sector;
780 ci.sector_count = bio_sectors(bio);
781 ci.idx = bio->bi_idx;
783 start_io_acct(ci.io);
784 while (ci.sector_count)
785 __clone_and_map(&ci);
787 /* drop the extra reference count */
788 dec_pending(ci.io, 0);
789 dm_table_put(ci.map);
791 /*-----------------------------------------------------------------
792 * CRUD END
793 *---------------------------------------------------------------*/
796 * The request function that just remaps the bio built up by
797 * dm_merge_bvec.
799 static int dm_request(request_queue_t *q, struct bio *bio)
801 int r;
802 int rw = bio_data_dir(bio);
803 struct mapped_device *md = q->queuedata;
806 * There is no use in forwarding any barrier request since we can't
807 * guarantee it is (or can be) handled by the targets correctly.
809 if (unlikely(bio_barrier(bio))) {
810 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
811 return 0;
814 down_read(&md->io_lock);
816 disk_stat_inc(dm_disk(md), ios[rw]);
817 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
820 * If we're suspended we have to queue
821 * this io for later.
823 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
824 up_read(&md->io_lock);
826 if (bio_rw(bio) == READA) {
827 bio_io_error(bio, bio->bi_size);
828 return 0;
831 r = queue_io(md, bio);
832 if (r < 0) {
833 bio_io_error(bio, bio->bi_size);
834 return 0;
836 } else if (r == 0)
837 return 0; /* deferred successfully */
840 * We're in a while loop, because someone could suspend
841 * before we get to the following read lock.
843 down_read(&md->io_lock);
846 __split_bio(md, bio);
847 up_read(&md->io_lock);
848 return 0;
851 static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
852 sector_t *error_sector)
854 struct mapped_device *md = q->queuedata;
855 struct dm_table *map = dm_get_table(md);
856 int ret = -ENXIO;
858 if (map) {
859 ret = dm_table_flush_all(map);
860 dm_table_put(map);
863 return ret;
866 static void dm_unplug_all(request_queue_t *q)
868 struct mapped_device *md = q->queuedata;
869 struct dm_table *map = dm_get_table(md);
871 if (map) {
872 dm_table_unplug_all(map);
873 dm_table_put(map);
877 static int dm_any_congested(void *congested_data, int bdi_bits)
879 int r;
880 struct mapped_device *md = (struct mapped_device *) congested_data;
881 struct dm_table *map = dm_get_table(md);
883 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
884 r = bdi_bits;
885 else
886 r = dm_table_any_congested(map, bdi_bits);
888 dm_table_put(map);
889 return r;
892 /*-----------------------------------------------------------------
893 * An IDR is used to keep track of allocated minor numbers.
894 *---------------------------------------------------------------*/
895 static DEFINE_IDR(_minor_idr);
897 static void free_minor(int minor)
899 spin_lock(&_minor_lock);
900 idr_remove(&_minor_idr, minor);
901 spin_unlock(&_minor_lock);
905 * See if the device with a specific minor # is free.
907 static int specific_minor(struct mapped_device *md, int minor)
909 int r, m;
911 if (minor >= (1 << MINORBITS))
912 return -EINVAL;
914 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
915 if (!r)
916 return -ENOMEM;
918 spin_lock(&_minor_lock);
920 if (idr_find(&_minor_idr, minor)) {
921 r = -EBUSY;
922 goto out;
925 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
926 if (r)
927 goto out;
929 if (m != minor) {
930 idr_remove(&_minor_idr, m);
931 r = -EBUSY;
932 goto out;
935 out:
936 spin_unlock(&_minor_lock);
937 return r;
940 static int next_free_minor(struct mapped_device *md, int *minor)
942 int r, m;
944 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
945 if (!r)
946 return -ENOMEM;
948 spin_lock(&_minor_lock);
950 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
951 if (r) {
952 goto out;
955 if (m >= (1 << MINORBITS)) {
956 idr_remove(&_minor_idr, m);
957 r = -ENOSPC;
958 goto out;
961 *minor = m;
963 out:
964 spin_unlock(&_minor_lock);
965 return r;
968 static struct block_device_operations dm_blk_dops;
971 * Allocate and initialise a blank device with a given minor.
973 static struct mapped_device *alloc_dev(int minor)
975 int r;
976 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
977 void *old_md;
979 if (!md) {
980 DMWARN("unable to allocate device, out of memory.");
981 return NULL;
984 if (!try_module_get(THIS_MODULE))
985 goto bad0;
987 /* get a minor number for the dev */
988 if (minor == DM_ANY_MINOR)
989 r = next_free_minor(md, &minor);
990 else
991 r = specific_minor(md, minor);
992 if (r < 0)
993 goto bad1;
995 memset(md, 0, sizeof(*md));
996 init_rwsem(&md->io_lock);
997 init_MUTEX(&md->suspend_lock);
998 spin_lock_init(&md->pushback_lock);
999 rwlock_init(&md->map_lock);
1000 atomic_set(&md->holders, 1);
1001 atomic_set(&md->open_count, 0);
1002 atomic_set(&md->event_nr, 0);
1004 md->queue = blk_alloc_queue(GFP_KERNEL);
1005 if (!md->queue)
1006 goto bad1_free_minor;
1008 md->queue->queuedata = md;
1009 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1010 md->queue->backing_dev_info.congested_data = md;
1011 blk_queue_make_request(md->queue, dm_request);
1012 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1013 md->queue->unplug_fn = dm_unplug_all;
1014 md->queue->issue_flush_fn = dm_flush_all;
1016 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
1017 if (!md->io_pool)
1018 goto bad2;
1020 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1021 if (!md->tio_pool)
1022 goto bad3;
1024 md->bs = bioset_create(16, 16);
1025 if (!md->bs)
1026 goto bad_no_bioset;
1028 md->disk = alloc_disk(1);
1029 if (!md->disk)
1030 goto bad4;
1032 atomic_set(&md->pending, 0);
1033 init_waitqueue_head(&md->wait);
1034 init_waitqueue_head(&md->eventq);
1036 md->disk->major = _major;
1037 md->disk->first_minor = minor;
1038 md->disk->fops = &dm_blk_dops;
1039 md->disk->queue = md->queue;
1040 md->disk->private_data = md;
1041 sprintf(md->disk->disk_name, "dm-%d", minor);
1042 add_disk(md->disk);
1043 format_dev_t(md->name, MKDEV(_major, minor));
1045 /* Populate the mapping, nobody knows we exist yet */
1046 spin_lock(&_minor_lock);
1047 old_md = idr_replace(&_minor_idr, md, minor);
1048 spin_unlock(&_minor_lock);
1050 BUG_ON(old_md != MINOR_ALLOCED);
1052 return md;
1054 bad4:
1055 bioset_free(md->bs);
1056 bad_no_bioset:
1057 mempool_destroy(md->tio_pool);
1058 bad3:
1059 mempool_destroy(md->io_pool);
1060 bad2:
1061 blk_cleanup_queue(md->queue);
1062 bad1_free_minor:
1063 free_minor(minor);
1064 bad1:
1065 module_put(THIS_MODULE);
1066 bad0:
1067 kfree(md);
1068 return NULL;
1071 static void free_dev(struct mapped_device *md)
1073 int minor = md->disk->first_minor;
1075 if (md->suspended_bdev) {
1076 thaw_bdev(md->suspended_bdev, NULL);
1077 bdput(md->suspended_bdev);
1079 mempool_destroy(md->tio_pool);
1080 mempool_destroy(md->io_pool);
1081 bioset_free(md->bs);
1082 del_gendisk(md->disk);
1083 free_minor(minor);
1085 spin_lock(&_minor_lock);
1086 md->disk->private_data = NULL;
1087 spin_unlock(&_minor_lock);
1089 put_disk(md->disk);
1090 blk_cleanup_queue(md->queue);
1091 module_put(THIS_MODULE);
1092 kfree(md);
1096 * Bind a table to the device.
1098 static void event_callback(void *context)
1100 struct mapped_device *md = (struct mapped_device *) context;
1102 atomic_inc(&md->event_nr);
1103 wake_up(&md->eventq);
1106 static void __set_size(struct mapped_device *md, sector_t size)
1108 set_capacity(md->disk, size);
1110 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1111 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1112 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1115 static int __bind(struct mapped_device *md, struct dm_table *t)
1117 request_queue_t *q = md->queue;
1118 sector_t size;
1120 size = dm_table_get_size(t);
1123 * Wipe any geometry if the size of the table changed.
1125 if (size != get_capacity(md->disk))
1126 memset(&md->geometry, 0, sizeof(md->geometry));
1128 if (md->suspended_bdev)
1129 __set_size(md, size);
1130 if (size == 0)
1131 return 0;
1133 dm_table_get(t);
1134 dm_table_event_callback(t, event_callback, md);
1136 write_lock(&md->map_lock);
1137 md->map = t;
1138 dm_table_set_restrictions(t, q);
1139 write_unlock(&md->map_lock);
1141 return 0;
1144 static void __unbind(struct mapped_device *md)
1146 struct dm_table *map = md->map;
1148 if (!map)
1149 return;
1151 dm_table_event_callback(map, NULL, NULL);
1152 write_lock(&md->map_lock);
1153 md->map = NULL;
1154 write_unlock(&md->map_lock);
1155 dm_table_put(map);
1159 * Constructor for a new device.
1161 int dm_create(int minor, struct mapped_device **result)
1163 struct mapped_device *md;
1165 md = alloc_dev(minor);
1166 if (!md)
1167 return -ENXIO;
1169 *result = md;
1170 return 0;
1173 static struct mapped_device *dm_find_md(dev_t dev)
1175 struct mapped_device *md;
1176 unsigned minor = MINOR(dev);
1178 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1179 return NULL;
1181 spin_lock(&_minor_lock);
1183 md = idr_find(&_minor_idr, minor);
1184 if (md && (md == MINOR_ALLOCED ||
1185 (dm_disk(md)->first_minor != minor) ||
1186 test_bit(DMF_FREEING, &md->flags))) {
1187 md = NULL;
1188 goto out;
1191 out:
1192 spin_unlock(&_minor_lock);
1194 return md;
1197 struct mapped_device *dm_get_md(dev_t dev)
1199 struct mapped_device *md = dm_find_md(dev);
1201 if (md)
1202 dm_get(md);
1204 return md;
1207 void *dm_get_mdptr(struct mapped_device *md)
1209 return md->interface_ptr;
1212 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1214 md->interface_ptr = ptr;
1217 void dm_get(struct mapped_device *md)
1219 atomic_inc(&md->holders);
1222 const char *dm_device_name(struct mapped_device *md)
1224 return md->name;
1226 EXPORT_SYMBOL_GPL(dm_device_name);
1228 void dm_put(struct mapped_device *md)
1230 struct dm_table *map;
1232 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1234 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1235 map = dm_get_table(md);
1236 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1237 set_bit(DMF_FREEING, &md->flags);
1238 spin_unlock(&_minor_lock);
1239 if (!dm_suspended(md)) {
1240 dm_table_presuspend_targets(map);
1241 dm_table_postsuspend_targets(map);
1243 __unbind(md);
1244 dm_table_put(map);
1245 free_dev(md);
1248 EXPORT_SYMBOL_GPL(dm_put);
1251 * Process the deferred bios
1253 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1255 struct bio *n;
1257 while (c) {
1258 n = c->bi_next;
1259 c->bi_next = NULL;
1260 __split_bio(md, c);
1261 c = n;
1266 * Swap in a new table (destroying old one).
1268 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1270 int r = -EINVAL;
1272 down(&md->suspend_lock);
1274 /* device must be suspended */
1275 if (!dm_suspended(md))
1276 goto out;
1278 /* without bdev, the device size cannot be changed */
1279 if (!md->suspended_bdev)
1280 if (get_capacity(md->disk) != dm_table_get_size(table))
1281 goto out;
1283 __unbind(md);
1284 r = __bind(md, table);
1286 out:
1287 up(&md->suspend_lock);
1288 return r;
1292 * Functions to lock and unlock any filesystem running on the
1293 * device.
1295 static int lock_fs(struct mapped_device *md)
1297 int r;
1299 WARN_ON(md->frozen_sb);
1301 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1302 if (IS_ERR(md->frozen_sb)) {
1303 r = PTR_ERR(md->frozen_sb);
1304 md->frozen_sb = NULL;
1305 return r;
1308 set_bit(DMF_FROZEN, &md->flags);
1310 /* don't bdput right now, we don't want the bdev
1311 * to go away while it is locked.
1313 return 0;
1316 static void unlock_fs(struct mapped_device *md)
1318 if (!test_bit(DMF_FROZEN, &md->flags))
1319 return;
1321 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1322 md->frozen_sb = NULL;
1323 clear_bit(DMF_FROZEN, &md->flags);
1327 * We need to be able to change a mapping table under a mounted
1328 * filesystem. For example we might want to move some data in
1329 * the background. Before the table can be swapped with
1330 * dm_bind_table, dm_suspend must be called to flush any in
1331 * flight bios and ensure that any further io gets deferred.
1333 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1335 struct dm_table *map = NULL;
1336 unsigned long flags;
1337 DECLARE_WAITQUEUE(wait, current);
1338 struct bio *def;
1339 int r = -EINVAL;
1340 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1341 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1343 down(&md->suspend_lock);
1345 if (dm_suspended(md))
1346 goto out_unlock;
1348 map = dm_get_table(md);
1351 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1352 * This flag is cleared before dm_suspend returns.
1354 if (noflush)
1355 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1357 /* This does not get reverted if there's an error later. */
1358 dm_table_presuspend_targets(map);
1360 /* bdget() can stall if the pending I/Os are not flushed */
1361 if (!noflush) {
1362 md->suspended_bdev = bdget_disk(md->disk, 0);
1363 if (!md->suspended_bdev) {
1364 DMWARN("bdget failed in dm_suspend");
1365 r = -ENOMEM;
1366 goto flush_and_out;
1371 * Flush I/O to the device.
1372 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1374 if (do_lockfs && !noflush) {
1375 r = lock_fs(md);
1376 if (r)
1377 goto out;
1381 * First we set the BLOCK_IO flag so no more ios will be mapped.
1383 down_write(&md->io_lock);
1384 set_bit(DMF_BLOCK_IO, &md->flags);
1386 add_wait_queue(&md->wait, &wait);
1387 up_write(&md->io_lock);
1389 /* unplug */
1390 if (map)
1391 dm_table_unplug_all(map);
1394 * Then we wait for the already mapped ios to
1395 * complete.
1397 while (1) {
1398 set_current_state(TASK_INTERRUPTIBLE);
1400 if (!atomic_read(&md->pending) || signal_pending(current))
1401 break;
1403 io_schedule();
1405 set_current_state(TASK_RUNNING);
1407 down_write(&md->io_lock);
1408 remove_wait_queue(&md->wait, &wait);
1410 if (noflush) {
1411 spin_lock_irqsave(&md->pushback_lock, flags);
1412 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1413 bio_list_merge_head(&md->deferred, &md->pushback);
1414 bio_list_init(&md->pushback);
1415 spin_unlock_irqrestore(&md->pushback_lock, flags);
1418 /* were we interrupted ? */
1419 r = -EINTR;
1420 if (atomic_read(&md->pending)) {
1421 clear_bit(DMF_BLOCK_IO, &md->flags);
1422 def = bio_list_get(&md->deferred);
1423 __flush_deferred_io(md, def);
1424 up_write(&md->io_lock);
1425 unlock_fs(md);
1426 goto out; /* pushback list is already flushed, so skip flush */
1428 up_write(&md->io_lock);
1430 dm_table_postsuspend_targets(map);
1432 set_bit(DMF_SUSPENDED, &md->flags);
1434 r = 0;
1436 flush_and_out:
1437 if (r && noflush) {
1439 * Because there may be already I/Os in the pushback list,
1440 * flush them before return.
1442 down_write(&md->io_lock);
1444 spin_lock_irqsave(&md->pushback_lock, flags);
1445 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1446 bio_list_merge_head(&md->deferred, &md->pushback);
1447 bio_list_init(&md->pushback);
1448 spin_unlock_irqrestore(&md->pushback_lock, flags);
1450 def = bio_list_get(&md->deferred);
1451 __flush_deferred_io(md, def);
1452 up_write(&md->io_lock);
1455 out:
1456 if (r && md->suspended_bdev) {
1457 bdput(md->suspended_bdev);
1458 md->suspended_bdev = NULL;
1461 dm_table_put(map);
1463 out_unlock:
1464 up(&md->suspend_lock);
1465 return r;
1468 int dm_resume(struct mapped_device *md)
1470 int r = -EINVAL;
1471 struct bio *def;
1472 struct dm_table *map = NULL;
1474 down(&md->suspend_lock);
1475 if (!dm_suspended(md))
1476 goto out;
1478 map = dm_get_table(md);
1479 if (!map || !dm_table_get_size(map))
1480 goto out;
1482 r = dm_table_resume_targets(map);
1483 if (r)
1484 goto out;
1486 down_write(&md->io_lock);
1487 clear_bit(DMF_BLOCK_IO, &md->flags);
1489 def = bio_list_get(&md->deferred);
1490 __flush_deferred_io(md, def);
1491 up_write(&md->io_lock);
1493 unlock_fs(md);
1495 if (md->suspended_bdev) {
1496 bdput(md->suspended_bdev);
1497 md->suspended_bdev = NULL;
1500 clear_bit(DMF_SUSPENDED, &md->flags);
1502 dm_table_unplug_all(map);
1504 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1506 r = 0;
1508 out:
1509 dm_table_put(map);
1510 up(&md->suspend_lock);
1512 return r;
1515 /*-----------------------------------------------------------------
1516 * Event notification.
1517 *---------------------------------------------------------------*/
1518 uint32_t dm_get_event_nr(struct mapped_device *md)
1520 return atomic_read(&md->event_nr);
1523 int dm_wait_event(struct mapped_device *md, int event_nr)
1525 return wait_event_interruptible(md->eventq,
1526 (event_nr != atomic_read(&md->event_nr)));
1530 * The gendisk is only valid as long as you have a reference
1531 * count on 'md'.
1533 struct gendisk *dm_disk(struct mapped_device *md)
1535 return md->disk;
1538 int dm_suspended(struct mapped_device *md)
1540 return test_bit(DMF_SUSPENDED, &md->flags);
1543 int dm_noflush_suspending(struct dm_target *ti)
1545 struct mapped_device *md = dm_table_get_md(ti->table);
1546 int r = __noflush_suspending(md);
1548 dm_put(md);
1550 return r;
1552 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1554 static struct block_device_operations dm_blk_dops = {
1555 .open = dm_blk_open,
1556 .release = dm_blk_close,
1557 .ioctl = dm_blk_ioctl,
1558 .getgeo = dm_blk_getgeo,
1559 .owner = THIS_MODULE
1562 EXPORT_SYMBOL(dm_get_mapinfo);
1565 * module hooks
1567 module_init(dm_init);
1568 module_exit(dm_exit);
1570 module_param(major, uint, 0);
1571 MODULE_PARM_DESC(major, "The major number of the device mapper");
1572 MODULE_DESCRIPTION(DM_NAME " driver");
1573 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1574 MODULE_LICENSE("GPL");