[S390] sclp: sysfs interface for SCLP cpi
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm.c
blobf2d24eb3208c97e79bdf33b9d8c396f855f55566
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"
10 #include "dm-uevent.h"
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/moduleparam.h>
16 #include <linux/blkpg.h>
17 #include <linux/bio.h>
18 #include <linux/buffer_head.h>
19 #include <linux/mempool.h>
20 #include <linux/slab.h>
21 #include <linux/idr.h>
22 #include <linux/hdreg.h>
23 #include <linux/blktrace_api.h>
24 #include <linux/smp_lock.h>
26 #define DM_MSG_PREFIX "core"
28 static const char *_name = DM_NAME;
30 static unsigned int major = 0;
31 static unsigned int _major = 0;
33 static DEFINE_SPINLOCK(_minor_lock);
35 * One of these is allocated per bio.
37 struct dm_io {
38 struct mapped_device *md;
39 int error;
40 struct bio *bio;
41 atomic_t io_count;
42 unsigned long start_time;
46 * One of these is allocated per target within a bio. Hopefully
47 * this will be simplified out one day.
49 struct dm_target_io {
50 struct dm_io *io;
51 struct dm_target *ti;
52 union map_info info;
55 union map_info *dm_get_mapinfo(struct bio *bio)
57 if (bio && bio->bi_private)
58 return &((struct dm_target_io *)bio->bi_private)->info;
59 return NULL;
62 #define MINOR_ALLOCED ((void *)-1)
65 * Bits for the md->flags field.
67 #define DMF_BLOCK_IO 0
68 #define DMF_SUSPENDED 1
69 #define DMF_FROZEN 2
70 #define DMF_FREEING 3
71 #define DMF_DELETING 4
72 #define DMF_NOFLUSH_SUSPENDING 5
74 struct mapped_device {
75 struct rw_semaphore io_lock;
76 struct semaphore suspend_lock;
77 spinlock_t pushback_lock;
78 rwlock_t map_lock;
79 atomic_t holders;
80 atomic_t open_count;
82 unsigned long flags;
84 struct request_queue *queue;
85 struct gendisk *disk;
86 char name[16];
88 void *interface_ptr;
91 * A list of ios that arrived while we were suspended.
93 atomic_t pending;
94 wait_queue_head_t wait;
95 struct bio_list deferred;
96 struct bio_list pushback;
99 * The current mapping.
101 struct dm_table *map;
104 * io objects are allocated from here.
106 mempool_t *io_pool;
107 mempool_t *tio_pool;
109 struct bio_set *bs;
112 * Event handling.
114 atomic_t event_nr;
115 wait_queue_head_t eventq;
116 atomic_t uevent_seq;
117 struct list_head uevent_list;
118 spinlock_t uevent_lock; /* Protect access to uevent_list */
121 * freeze/thaw support require holding onto a super block
123 struct super_block *frozen_sb;
124 struct block_device *suspended_bdev;
126 /* forced geometry settings */
127 struct hd_geometry geometry;
130 #define MIN_IOS 256
131 static struct kmem_cache *_io_cache;
132 static struct kmem_cache *_tio_cache;
134 static int __init local_init(void)
136 int r;
138 /* allocate a slab for the dm_ios */
139 _io_cache = KMEM_CACHE(dm_io, 0);
140 if (!_io_cache)
141 return -ENOMEM;
143 /* allocate a slab for the target ios */
144 _tio_cache = KMEM_CACHE(dm_target_io, 0);
145 if (!_tio_cache) {
146 kmem_cache_destroy(_io_cache);
147 return -ENOMEM;
150 r = dm_uevent_init();
151 if (r) {
152 kmem_cache_destroy(_tio_cache);
153 kmem_cache_destroy(_io_cache);
154 return r;
157 _major = major;
158 r = register_blkdev(_major, _name);
159 if (r < 0) {
160 kmem_cache_destroy(_tio_cache);
161 kmem_cache_destroy(_io_cache);
162 dm_uevent_exit();
163 return r;
166 if (!_major)
167 _major = r;
169 return 0;
172 static void local_exit(void)
174 kmem_cache_destroy(_tio_cache);
175 kmem_cache_destroy(_io_cache);
176 unregister_blkdev(_major, _name);
177 dm_uevent_exit();
179 _major = 0;
181 DMINFO("cleaned up");
184 int (*_inits[])(void) __initdata = {
185 local_init,
186 dm_target_init,
187 dm_linear_init,
188 dm_stripe_init,
189 dm_interface_init,
192 void (*_exits[])(void) = {
193 local_exit,
194 dm_target_exit,
195 dm_linear_exit,
196 dm_stripe_exit,
197 dm_interface_exit,
200 static int __init dm_init(void)
202 const int count = ARRAY_SIZE(_inits);
204 int r, i;
206 for (i = 0; i < count; i++) {
207 r = _inits[i]();
208 if (r)
209 goto bad;
212 return 0;
214 bad:
215 while (i--)
216 _exits[i]();
218 return r;
221 static void __exit dm_exit(void)
223 int i = ARRAY_SIZE(_exits);
225 while (i--)
226 _exits[i]();
230 * Block device functions
232 static int dm_blk_open(struct inode *inode, struct file *file)
234 struct mapped_device *md;
236 spin_lock(&_minor_lock);
238 md = inode->i_bdev->bd_disk->private_data;
239 if (!md)
240 goto out;
242 if (test_bit(DMF_FREEING, &md->flags) ||
243 test_bit(DMF_DELETING, &md->flags)) {
244 md = NULL;
245 goto out;
248 dm_get(md);
249 atomic_inc(&md->open_count);
251 out:
252 spin_unlock(&_minor_lock);
254 return md ? 0 : -ENXIO;
257 static int dm_blk_close(struct inode *inode, struct file *file)
259 struct mapped_device *md;
261 md = inode->i_bdev->bd_disk->private_data;
262 atomic_dec(&md->open_count);
263 dm_put(md);
264 return 0;
267 int dm_open_count(struct mapped_device *md)
269 return atomic_read(&md->open_count);
273 * Guarantees nothing is using the device before it's deleted.
275 int dm_lock_for_deletion(struct mapped_device *md)
277 int r = 0;
279 spin_lock(&_minor_lock);
281 if (dm_open_count(md))
282 r = -EBUSY;
283 else
284 set_bit(DMF_DELETING, &md->flags);
286 spin_unlock(&_minor_lock);
288 return r;
291 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
293 struct mapped_device *md = bdev->bd_disk->private_data;
295 return dm_get_geometry(md, geo);
298 static int dm_blk_ioctl(struct inode *inode, struct file *file,
299 unsigned int cmd, unsigned long arg)
301 struct mapped_device *md;
302 struct dm_table *map;
303 struct dm_target *tgt;
304 int r = -ENOTTY;
306 /* We don't really need this lock, but we do need 'inode'. */
307 unlock_kernel();
309 md = inode->i_bdev->bd_disk->private_data;
311 map = dm_get_table(md);
313 if (!map || !dm_table_get_size(map))
314 goto out;
316 /* We only support devices that have a single target */
317 if (dm_table_get_num_targets(map) != 1)
318 goto out;
320 tgt = dm_table_get_target(map, 0);
322 if (dm_suspended(md)) {
323 r = -EAGAIN;
324 goto out;
327 if (tgt->type->ioctl)
328 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
330 out:
331 dm_table_put(map);
333 lock_kernel();
334 return r;
337 static struct dm_io *alloc_io(struct mapped_device *md)
339 return mempool_alloc(md->io_pool, GFP_NOIO);
342 static void free_io(struct mapped_device *md, struct dm_io *io)
344 mempool_free(io, md->io_pool);
347 static struct dm_target_io *alloc_tio(struct mapped_device *md)
349 return mempool_alloc(md->tio_pool, GFP_NOIO);
352 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
354 mempool_free(tio, md->tio_pool);
357 static void start_io_acct(struct dm_io *io)
359 struct mapped_device *md = io->md;
361 io->start_time = jiffies;
363 preempt_disable();
364 disk_round_stats(dm_disk(md));
365 preempt_enable();
366 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
369 static int end_io_acct(struct dm_io *io)
371 struct mapped_device *md = io->md;
372 struct bio *bio = io->bio;
373 unsigned long duration = jiffies - io->start_time;
374 int pending;
375 int rw = bio_data_dir(bio);
377 preempt_disable();
378 disk_round_stats(dm_disk(md));
379 preempt_enable();
380 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
382 disk_stat_add(dm_disk(md), ticks[rw], duration);
384 return !pending;
388 * Add the bio to the list of deferred io.
390 static int queue_io(struct mapped_device *md, struct bio *bio)
392 down_write(&md->io_lock);
394 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
395 up_write(&md->io_lock);
396 return 1;
399 bio_list_add(&md->deferred, bio);
401 up_write(&md->io_lock);
402 return 0; /* deferred successfully */
406 * Everyone (including functions in this file), should use this
407 * function to access the md->map field, and make sure they call
408 * dm_table_put() when finished.
410 struct dm_table *dm_get_table(struct mapped_device *md)
412 struct dm_table *t;
414 read_lock(&md->map_lock);
415 t = md->map;
416 if (t)
417 dm_table_get(t);
418 read_unlock(&md->map_lock);
420 return t;
424 * Get the geometry associated with a dm device
426 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
428 *geo = md->geometry;
430 return 0;
434 * Set the geometry of a device.
436 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
438 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
440 if (geo->start > sz) {
441 DMWARN("Start sector is beyond the geometry limits.");
442 return -EINVAL;
445 md->geometry = *geo;
447 return 0;
450 /*-----------------------------------------------------------------
451 * CRUD START:
452 * A more elegant soln is in the works that uses the queue
453 * merge fn, unfortunately there are a couple of changes to
454 * the block layer that I want to make for this. So in the
455 * interests of getting something for people to use I give
456 * you this clearly demarcated crap.
457 *---------------------------------------------------------------*/
459 static int __noflush_suspending(struct mapped_device *md)
461 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
465 * Decrements the number of outstanding ios that a bio has been
466 * cloned into, completing the original io if necc.
468 static void dec_pending(struct dm_io *io, int error)
470 unsigned long flags;
472 /* Push-back supersedes any I/O errors */
473 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
474 io->error = error;
476 if (atomic_dec_and_test(&io->io_count)) {
477 if (io->error == DM_ENDIO_REQUEUE) {
479 * Target requested pushing back the I/O.
480 * This must be handled before the sleeper on
481 * suspend queue merges the pushback list.
483 spin_lock_irqsave(&io->md->pushback_lock, flags);
484 if (__noflush_suspending(io->md))
485 bio_list_add(&io->md->pushback, io->bio);
486 else
487 /* noflush suspend was interrupted. */
488 io->error = -EIO;
489 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
492 if (end_io_acct(io))
493 /* nudge anyone waiting on suspend queue */
494 wake_up(&io->md->wait);
496 if (io->error != DM_ENDIO_REQUEUE) {
497 blk_add_trace_bio(io->md->queue, io->bio,
498 BLK_TA_COMPLETE);
500 bio_endio(io->bio, io->error);
503 free_io(io->md, io);
507 static void clone_endio(struct bio *bio, int error)
509 int r = 0;
510 struct dm_target_io *tio = bio->bi_private;
511 struct mapped_device *md = tio->io->md;
512 dm_endio_fn endio = tio->ti->type->end_io;
514 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
515 error = -EIO;
517 if (endio) {
518 r = endio(tio->ti, bio, error, &tio->info);
519 if (r < 0 || r == DM_ENDIO_REQUEUE)
521 * error and requeue request are handled
522 * in dec_pending().
524 error = r;
525 else if (r == DM_ENDIO_INCOMPLETE)
526 /* The target will handle the io */
527 return;
528 else if (r) {
529 DMWARN("unimplemented target endio return value: %d", r);
530 BUG();
534 dec_pending(tio->io, error);
537 * Store md for cleanup instead of tio which is about to get freed.
539 bio->bi_private = md->bs;
541 bio_put(bio);
542 free_tio(md, tio);
545 static sector_t max_io_len(struct mapped_device *md,
546 sector_t sector, struct dm_target *ti)
548 sector_t offset = sector - ti->begin;
549 sector_t len = ti->len - offset;
552 * Does the target need to split even further ?
554 if (ti->split_io) {
555 sector_t boundary;
556 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
557 - offset;
558 if (len > boundary)
559 len = boundary;
562 return len;
565 static void __map_bio(struct dm_target *ti, struct bio *clone,
566 struct dm_target_io *tio)
568 int r;
569 sector_t sector;
570 struct mapped_device *md;
573 * Sanity checks.
575 BUG_ON(!clone->bi_size);
577 clone->bi_end_io = clone_endio;
578 clone->bi_private = tio;
581 * Map the clone. If r == 0 we don't need to do
582 * anything, the target has assumed ownership of
583 * this io.
585 atomic_inc(&tio->io->io_count);
586 sector = clone->bi_sector;
587 r = ti->type->map(ti, clone, &tio->info);
588 if (r == DM_MAPIO_REMAPPED) {
589 /* the bio has been remapped so dispatch it */
591 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
592 tio->io->bio->bi_bdev->bd_dev,
593 clone->bi_sector, sector);
595 generic_make_request(clone);
596 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
597 /* error the io and bail out, or requeue it if needed */
598 md = tio->io->md;
599 dec_pending(tio->io, r);
601 * Store bio_set for cleanup.
603 clone->bi_private = md->bs;
604 bio_put(clone);
605 free_tio(md, tio);
606 } else if (r) {
607 DMWARN("unimplemented target map return value: %d", r);
608 BUG();
612 struct clone_info {
613 struct mapped_device *md;
614 struct dm_table *map;
615 struct bio *bio;
616 struct dm_io *io;
617 sector_t sector;
618 sector_t sector_count;
619 unsigned short idx;
622 static void dm_bio_destructor(struct bio *bio)
624 struct bio_set *bs = bio->bi_private;
626 bio_free(bio, bs);
630 * Creates a little bio that is just does part of a bvec.
632 static struct bio *split_bvec(struct bio *bio, sector_t sector,
633 unsigned short idx, unsigned int offset,
634 unsigned int len, struct bio_set *bs)
636 struct bio *clone;
637 struct bio_vec *bv = bio->bi_io_vec + idx;
639 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
640 clone->bi_destructor = dm_bio_destructor;
641 *clone->bi_io_vec = *bv;
643 clone->bi_sector = sector;
644 clone->bi_bdev = bio->bi_bdev;
645 clone->bi_rw = bio->bi_rw;
646 clone->bi_vcnt = 1;
647 clone->bi_size = to_bytes(len);
648 clone->bi_io_vec->bv_offset = offset;
649 clone->bi_io_vec->bv_len = clone->bi_size;
651 return clone;
655 * Creates a bio that consists of range of complete bvecs.
657 static struct bio *clone_bio(struct bio *bio, sector_t sector,
658 unsigned short idx, unsigned short bv_count,
659 unsigned int len, struct bio_set *bs)
661 struct bio *clone;
663 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
664 __bio_clone(clone, bio);
665 clone->bi_destructor = dm_bio_destructor;
666 clone->bi_sector = sector;
667 clone->bi_idx = idx;
668 clone->bi_vcnt = idx + bv_count;
669 clone->bi_size = to_bytes(len);
670 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
672 return clone;
675 static int __clone_and_map(struct clone_info *ci)
677 struct bio *clone, *bio = ci->bio;
678 struct dm_target *ti;
679 sector_t len = 0, max;
680 struct dm_target_io *tio;
682 ti = dm_table_find_target(ci->map, ci->sector);
683 if (!dm_target_is_valid(ti))
684 return -EIO;
686 max = max_io_len(ci->md, ci->sector, ti);
689 * Allocate a target io object.
691 tio = alloc_tio(ci->md);
692 tio->io = ci->io;
693 tio->ti = ti;
694 memset(&tio->info, 0, sizeof(tio->info));
696 if (ci->sector_count <= max) {
698 * Optimise for the simple case where we can do all of
699 * the remaining io with a single clone.
701 clone = clone_bio(bio, ci->sector, ci->idx,
702 bio->bi_vcnt - ci->idx, ci->sector_count,
703 ci->md->bs);
704 __map_bio(ti, clone, tio);
705 ci->sector_count = 0;
707 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
709 * There are some bvecs that don't span targets.
710 * Do as many of these as possible.
712 int i;
713 sector_t remaining = max;
714 sector_t bv_len;
716 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
717 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
719 if (bv_len > remaining)
720 break;
722 remaining -= bv_len;
723 len += bv_len;
726 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
727 ci->md->bs);
728 __map_bio(ti, clone, tio);
730 ci->sector += len;
731 ci->sector_count -= len;
732 ci->idx = i;
734 } else {
736 * Handle a bvec that must be split between two or more targets.
738 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
739 sector_t remaining = to_sector(bv->bv_len);
740 unsigned int offset = 0;
742 do {
743 if (offset) {
744 ti = dm_table_find_target(ci->map, ci->sector);
745 if (!dm_target_is_valid(ti))
746 return -EIO;
748 max = max_io_len(ci->md, ci->sector, ti);
750 tio = alloc_tio(ci->md);
751 tio->io = ci->io;
752 tio->ti = ti;
753 memset(&tio->info, 0, sizeof(tio->info));
756 len = min(remaining, max);
758 clone = split_bvec(bio, ci->sector, ci->idx,
759 bv->bv_offset + offset, len,
760 ci->md->bs);
762 __map_bio(ti, clone, tio);
764 ci->sector += len;
765 ci->sector_count -= len;
766 offset += to_bytes(len);
767 } while (remaining -= len);
769 ci->idx++;
772 return 0;
776 * Split the bio into several clones.
778 static int __split_bio(struct mapped_device *md, struct bio *bio)
780 struct clone_info ci;
781 int error = 0;
783 ci.map = dm_get_table(md);
784 if (unlikely(!ci.map))
785 return -EIO;
787 ci.md = md;
788 ci.bio = bio;
789 ci.io = alloc_io(md);
790 ci.io->error = 0;
791 atomic_set(&ci.io->io_count, 1);
792 ci.io->bio = bio;
793 ci.io->md = md;
794 ci.sector = bio->bi_sector;
795 ci.sector_count = bio_sectors(bio);
796 ci.idx = bio->bi_idx;
798 start_io_acct(ci.io);
799 while (ci.sector_count && !error)
800 error = __clone_and_map(&ci);
802 /* drop the extra reference count */
803 dec_pending(ci.io, error);
804 dm_table_put(ci.map);
806 return 0;
808 /*-----------------------------------------------------------------
809 * CRUD END
810 *---------------------------------------------------------------*/
813 * The request function that just remaps the bio built up by
814 * dm_merge_bvec.
816 static int dm_request(struct request_queue *q, struct bio *bio)
818 int r = -EIO;
819 int rw = bio_data_dir(bio);
820 struct mapped_device *md = q->queuedata;
823 * There is no use in forwarding any barrier request since we can't
824 * guarantee it is (or can be) handled by the targets correctly.
826 if (unlikely(bio_barrier(bio))) {
827 bio_endio(bio, -EOPNOTSUPP);
828 return 0;
831 down_read(&md->io_lock);
833 disk_stat_inc(dm_disk(md), ios[rw]);
834 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
837 * If we're suspended we have to queue
838 * this io for later.
840 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
841 up_read(&md->io_lock);
843 if (bio_rw(bio) != READA)
844 r = queue_io(md, bio);
846 if (r <= 0)
847 goto out_req;
850 * We're in a while loop, because someone could suspend
851 * before we get to the following read lock.
853 down_read(&md->io_lock);
856 r = __split_bio(md, bio);
857 up_read(&md->io_lock);
859 out_req:
860 if (r < 0)
861 bio_io_error(bio);
863 return 0;
866 static void dm_unplug_all(struct request_queue *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);
1003 atomic_set(&md->uevent_seq, 0);
1004 INIT_LIST_HEAD(&md->uevent_list);
1005 spin_lock_init(&md->uevent_lock);
1007 md->queue = blk_alloc_queue(GFP_KERNEL);
1008 if (!md->queue)
1009 goto bad1_free_minor;
1011 md->queue->queuedata = md;
1012 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1013 md->queue->backing_dev_info.congested_data = md;
1014 blk_queue_make_request(md->queue, dm_request);
1015 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1016 md->queue->unplug_fn = dm_unplug_all;
1018 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
1019 if (!md->io_pool)
1020 goto bad2;
1022 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1023 if (!md->tio_pool)
1024 goto bad3;
1026 md->bs = bioset_create(16, 16);
1027 if (!md->bs)
1028 goto bad_no_bioset;
1030 md->disk = alloc_disk(1);
1031 if (!md->disk)
1032 goto bad4;
1034 atomic_set(&md->pending, 0);
1035 init_waitqueue_head(&md->wait);
1036 init_waitqueue_head(&md->eventq);
1038 md->disk->major = _major;
1039 md->disk->first_minor = minor;
1040 md->disk->fops = &dm_blk_dops;
1041 md->disk->queue = md->queue;
1042 md->disk->private_data = md;
1043 sprintf(md->disk->disk_name, "dm-%d", minor);
1044 add_disk(md->disk);
1045 format_dev_t(md->name, MKDEV(_major, minor));
1047 /* Populate the mapping, nobody knows we exist yet */
1048 spin_lock(&_minor_lock);
1049 old_md = idr_replace(&_minor_idr, md, minor);
1050 spin_unlock(&_minor_lock);
1052 BUG_ON(old_md != MINOR_ALLOCED);
1054 return md;
1056 bad4:
1057 bioset_free(md->bs);
1058 bad_no_bioset:
1059 mempool_destroy(md->tio_pool);
1060 bad3:
1061 mempool_destroy(md->io_pool);
1062 bad2:
1063 blk_cleanup_queue(md->queue);
1064 bad1_free_minor:
1065 free_minor(minor);
1066 bad1:
1067 module_put(THIS_MODULE);
1068 bad0:
1069 kfree(md);
1070 return NULL;
1073 static void unlock_fs(struct mapped_device *md);
1075 static void free_dev(struct mapped_device *md)
1077 int minor = md->disk->first_minor;
1079 if (md->suspended_bdev) {
1080 unlock_fs(md);
1081 bdput(md->suspended_bdev);
1083 mempool_destroy(md->tio_pool);
1084 mempool_destroy(md->io_pool);
1085 bioset_free(md->bs);
1086 del_gendisk(md->disk);
1087 free_minor(minor);
1089 spin_lock(&_minor_lock);
1090 md->disk->private_data = NULL;
1091 spin_unlock(&_minor_lock);
1093 put_disk(md->disk);
1094 blk_cleanup_queue(md->queue);
1095 module_put(THIS_MODULE);
1096 kfree(md);
1100 * Bind a table to the device.
1102 static void event_callback(void *context)
1104 unsigned long flags;
1105 LIST_HEAD(uevents);
1106 struct mapped_device *md = (struct mapped_device *) context;
1108 spin_lock_irqsave(&md->uevent_lock, flags);
1109 list_splice_init(&md->uevent_list, &uevents);
1110 spin_unlock_irqrestore(&md->uevent_lock, flags);
1112 dm_send_uevents(&uevents, &md->disk->dev.kobj);
1114 atomic_inc(&md->event_nr);
1115 wake_up(&md->eventq);
1118 static void __set_size(struct mapped_device *md, sector_t size)
1120 set_capacity(md->disk, size);
1122 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1123 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1124 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1127 static int __bind(struct mapped_device *md, struct dm_table *t)
1129 struct request_queue *q = md->queue;
1130 sector_t size;
1132 size = dm_table_get_size(t);
1135 * Wipe any geometry if the size of the table changed.
1137 if (size != get_capacity(md->disk))
1138 memset(&md->geometry, 0, sizeof(md->geometry));
1140 if (md->suspended_bdev)
1141 __set_size(md, size);
1142 if (size == 0)
1143 return 0;
1145 dm_table_get(t);
1146 dm_table_event_callback(t, event_callback, md);
1148 write_lock(&md->map_lock);
1149 md->map = t;
1150 dm_table_set_restrictions(t, q);
1151 write_unlock(&md->map_lock);
1153 return 0;
1156 static void __unbind(struct mapped_device *md)
1158 struct dm_table *map = md->map;
1160 if (!map)
1161 return;
1163 dm_table_event_callback(map, NULL, NULL);
1164 write_lock(&md->map_lock);
1165 md->map = NULL;
1166 write_unlock(&md->map_lock);
1167 dm_table_put(map);
1171 * Constructor for a new device.
1173 int dm_create(int minor, struct mapped_device **result)
1175 struct mapped_device *md;
1177 md = alloc_dev(minor);
1178 if (!md)
1179 return -ENXIO;
1181 *result = md;
1182 return 0;
1185 static struct mapped_device *dm_find_md(dev_t dev)
1187 struct mapped_device *md;
1188 unsigned minor = MINOR(dev);
1190 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1191 return NULL;
1193 spin_lock(&_minor_lock);
1195 md = idr_find(&_minor_idr, minor);
1196 if (md && (md == MINOR_ALLOCED ||
1197 (dm_disk(md)->first_minor != minor) ||
1198 test_bit(DMF_FREEING, &md->flags))) {
1199 md = NULL;
1200 goto out;
1203 out:
1204 spin_unlock(&_minor_lock);
1206 return md;
1209 struct mapped_device *dm_get_md(dev_t dev)
1211 struct mapped_device *md = dm_find_md(dev);
1213 if (md)
1214 dm_get(md);
1216 return md;
1219 void *dm_get_mdptr(struct mapped_device *md)
1221 return md->interface_ptr;
1224 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1226 md->interface_ptr = ptr;
1229 void dm_get(struct mapped_device *md)
1231 atomic_inc(&md->holders);
1234 const char *dm_device_name(struct mapped_device *md)
1236 return md->name;
1238 EXPORT_SYMBOL_GPL(dm_device_name);
1240 void dm_put(struct mapped_device *md)
1242 struct dm_table *map;
1244 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1246 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1247 map = dm_get_table(md);
1248 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1249 set_bit(DMF_FREEING, &md->flags);
1250 spin_unlock(&_minor_lock);
1251 if (!dm_suspended(md)) {
1252 dm_table_presuspend_targets(map);
1253 dm_table_postsuspend_targets(map);
1255 __unbind(md);
1256 dm_table_put(map);
1257 free_dev(md);
1260 EXPORT_SYMBOL_GPL(dm_put);
1263 * Process the deferred bios
1265 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1267 struct bio *n;
1269 while (c) {
1270 n = c->bi_next;
1271 c->bi_next = NULL;
1272 if (__split_bio(md, c))
1273 bio_io_error(c);
1274 c = n;
1279 * Swap in a new table (destroying old one).
1281 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1283 int r = -EINVAL;
1285 down(&md->suspend_lock);
1287 /* device must be suspended */
1288 if (!dm_suspended(md))
1289 goto out;
1291 /* without bdev, the device size cannot be changed */
1292 if (!md->suspended_bdev)
1293 if (get_capacity(md->disk) != dm_table_get_size(table))
1294 goto out;
1296 __unbind(md);
1297 r = __bind(md, table);
1299 out:
1300 up(&md->suspend_lock);
1301 return r;
1305 * Functions to lock and unlock any filesystem running on the
1306 * device.
1308 static int lock_fs(struct mapped_device *md)
1310 int r;
1312 WARN_ON(md->frozen_sb);
1314 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1315 if (IS_ERR(md->frozen_sb)) {
1316 r = PTR_ERR(md->frozen_sb);
1317 md->frozen_sb = NULL;
1318 return r;
1321 set_bit(DMF_FROZEN, &md->flags);
1323 /* don't bdput right now, we don't want the bdev
1324 * to go away while it is locked.
1326 return 0;
1329 static void unlock_fs(struct mapped_device *md)
1331 if (!test_bit(DMF_FROZEN, &md->flags))
1332 return;
1334 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1335 md->frozen_sb = NULL;
1336 clear_bit(DMF_FROZEN, &md->flags);
1340 * We need to be able to change a mapping table under a mounted
1341 * filesystem. For example we might want to move some data in
1342 * the background. Before the table can be swapped with
1343 * dm_bind_table, dm_suspend must be called to flush any in
1344 * flight bios and ensure that any further io gets deferred.
1346 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1348 struct dm_table *map = NULL;
1349 unsigned long flags;
1350 DECLARE_WAITQUEUE(wait, current);
1351 struct bio *def;
1352 int r = -EINVAL;
1353 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1354 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1356 down(&md->suspend_lock);
1358 if (dm_suspended(md))
1359 goto out_unlock;
1361 map = dm_get_table(md);
1364 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1365 * This flag is cleared before dm_suspend returns.
1367 if (noflush)
1368 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1370 /* This does not get reverted if there's an error later. */
1371 dm_table_presuspend_targets(map);
1373 /* bdget() can stall if the pending I/Os are not flushed */
1374 if (!noflush) {
1375 md->suspended_bdev = bdget_disk(md->disk, 0);
1376 if (!md->suspended_bdev) {
1377 DMWARN("bdget failed in dm_suspend");
1378 r = -ENOMEM;
1379 goto flush_and_out;
1384 * Flush I/O to the device.
1385 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1387 if (do_lockfs && !noflush) {
1388 r = lock_fs(md);
1389 if (r)
1390 goto out;
1394 * First we set the BLOCK_IO flag so no more ios will be mapped.
1396 down_write(&md->io_lock);
1397 set_bit(DMF_BLOCK_IO, &md->flags);
1399 add_wait_queue(&md->wait, &wait);
1400 up_write(&md->io_lock);
1402 /* unplug */
1403 if (map)
1404 dm_table_unplug_all(map);
1407 * Then we wait for the already mapped ios to
1408 * complete.
1410 while (1) {
1411 set_current_state(TASK_INTERRUPTIBLE);
1413 if (!atomic_read(&md->pending) || signal_pending(current))
1414 break;
1416 io_schedule();
1418 set_current_state(TASK_RUNNING);
1420 down_write(&md->io_lock);
1421 remove_wait_queue(&md->wait, &wait);
1423 if (noflush) {
1424 spin_lock_irqsave(&md->pushback_lock, flags);
1425 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1426 bio_list_merge_head(&md->deferred, &md->pushback);
1427 bio_list_init(&md->pushback);
1428 spin_unlock_irqrestore(&md->pushback_lock, flags);
1431 /* were we interrupted ? */
1432 r = -EINTR;
1433 if (atomic_read(&md->pending)) {
1434 clear_bit(DMF_BLOCK_IO, &md->flags);
1435 def = bio_list_get(&md->deferred);
1436 __flush_deferred_io(md, def);
1437 up_write(&md->io_lock);
1438 unlock_fs(md);
1439 goto out; /* pushback list is already flushed, so skip flush */
1441 up_write(&md->io_lock);
1443 dm_table_postsuspend_targets(map);
1445 set_bit(DMF_SUSPENDED, &md->flags);
1447 r = 0;
1449 flush_and_out:
1450 if (r && noflush) {
1452 * Because there may be already I/Os in the pushback list,
1453 * flush them before return.
1455 down_write(&md->io_lock);
1457 spin_lock_irqsave(&md->pushback_lock, flags);
1458 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1459 bio_list_merge_head(&md->deferred, &md->pushback);
1460 bio_list_init(&md->pushback);
1461 spin_unlock_irqrestore(&md->pushback_lock, flags);
1463 def = bio_list_get(&md->deferred);
1464 __flush_deferred_io(md, def);
1465 up_write(&md->io_lock);
1468 out:
1469 if (r && md->suspended_bdev) {
1470 bdput(md->suspended_bdev);
1471 md->suspended_bdev = NULL;
1474 dm_table_put(map);
1476 out_unlock:
1477 up(&md->suspend_lock);
1478 return r;
1481 int dm_resume(struct mapped_device *md)
1483 int r = -EINVAL;
1484 struct bio *def;
1485 struct dm_table *map = NULL;
1487 down(&md->suspend_lock);
1488 if (!dm_suspended(md))
1489 goto out;
1491 map = dm_get_table(md);
1492 if (!map || !dm_table_get_size(map))
1493 goto out;
1495 r = dm_table_resume_targets(map);
1496 if (r)
1497 goto out;
1499 down_write(&md->io_lock);
1500 clear_bit(DMF_BLOCK_IO, &md->flags);
1502 def = bio_list_get(&md->deferred);
1503 __flush_deferred_io(md, def);
1504 up_write(&md->io_lock);
1506 unlock_fs(md);
1508 if (md->suspended_bdev) {
1509 bdput(md->suspended_bdev);
1510 md->suspended_bdev = NULL;
1513 clear_bit(DMF_SUSPENDED, &md->flags);
1515 dm_table_unplug_all(map);
1517 dm_kobject_uevent(md);
1519 r = 0;
1521 out:
1522 dm_table_put(map);
1523 up(&md->suspend_lock);
1525 return r;
1528 /*-----------------------------------------------------------------
1529 * Event notification.
1530 *---------------------------------------------------------------*/
1531 void dm_kobject_uevent(struct mapped_device *md)
1533 kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE);
1536 uint32_t dm_next_uevent_seq(struct mapped_device *md)
1538 return atomic_add_return(1, &md->uevent_seq);
1541 uint32_t dm_get_event_nr(struct mapped_device *md)
1543 return atomic_read(&md->event_nr);
1546 int dm_wait_event(struct mapped_device *md, int event_nr)
1548 return wait_event_interruptible(md->eventq,
1549 (event_nr != atomic_read(&md->event_nr)));
1552 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
1554 unsigned long flags;
1556 spin_lock_irqsave(&md->uevent_lock, flags);
1557 list_add(elist, &md->uevent_list);
1558 spin_unlock_irqrestore(&md->uevent_lock, flags);
1562 * The gendisk is only valid as long as you have a reference
1563 * count on 'md'.
1565 struct gendisk *dm_disk(struct mapped_device *md)
1567 return md->disk;
1570 int dm_suspended(struct mapped_device *md)
1572 return test_bit(DMF_SUSPENDED, &md->flags);
1575 int dm_noflush_suspending(struct dm_target *ti)
1577 struct mapped_device *md = dm_table_get_md(ti->table);
1578 int r = __noflush_suspending(md);
1580 dm_put(md);
1582 return r;
1584 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1586 static struct block_device_operations dm_blk_dops = {
1587 .open = dm_blk_open,
1588 .release = dm_blk_close,
1589 .ioctl = dm_blk_ioctl,
1590 .getgeo = dm_blk_getgeo,
1591 .owner = THIS_MODULE
1594 EXPORT_SYMBOL(dm_get_mapinfo);
1597 * module hooks
1599 module_init(dm_init);
1600 module_exit(dm_exit);
1602 module_param(major, uint, 0);
1603 MODULE_PARM_DESC(major, "The major number of the device mapper");
1604 MODULE_DESCRIPTION(DM_NAME " driver");
1605 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1606 MODULE_LICENSE("GPL");