dm: add uevent to core
[firewire-audio.git] / drivers / md / dm.c
blobbb5c1eaca52b3688000f51bc841c69470fc7600a
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;
118 * freeze/thaw support require holding onto a super block
120 struct super_block *frozen_sb;
121 struct block_device *suspended_bdev;
123 /* forced geometry settings */
124 struct hd_geometry geometry;
127 #define MIN_IOS 256
128 static struct kmem_cache *_io_cache;
129 static struct kmem_cache *_tio_cache;
131 static int __init local_init(void)
133 int r;
135 /* allocate a slab for the dm_ios */
136 _io_cache = KMEM_CACHE(dm_io, 0);
137 if (!_io_cache)
138 return -ENOMEM;
140 /* allocate a slab for the target ios */
141 _tio_cache = KMEM_CACHE(dm_target_io, 0);
142 if (!_tio_cache) {
143 kmem_cache_destroy(_io_cache);
144 return -ENOMEM;
147 r = dm_uevent_init();
148 if (r) {
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
151 return r;
154 _major = major;
155 r = register_blkdev(_major, _name);
156 if (r < 0) {
157 kmem_cache_destroy(_tio_cache);
158 kmem_cache_destroy(_io_cache);
159 dm_uevent_exit();
160 return r;
163 if (!_major)
164 _major = r;
166 return 0;
169 static void local_exit(void)
171 kmem_cache_destroy(_tio_cache);
172 kmem_cache_destroy(_io_cache);
173 unregister_blkdev(_major, _name);
174 dm_uevent_exit();
176 _major = 0;
178 DMINFO("cleaned up");
181 int (*_inits[])(void) __initdata = {
182 local_init,
183 dm_target_init,
184 dm_linear_init,
185 dm_stripe_init,
186 dm_interface_init,
189 void (*_exits[])(void) = {
190 local_exit,
191 dm_target_exit,
192 dm_linear_exit,
193 dm_stripe_exit,
194 dm_interface_exit,
197 static int __init dm_init(void)
199 const int count = ARRAY_SIZE(_inits);
201 int r, i;
203 for (i = 0; i < count; i++) {
204 r = _inits[i]();
205 if (r)
206 goto bad;
209 return 0;
211 bad:
212 while (i--)
213 _exits[i]();
215 return r;
218 static void __exit dm_exit(void)
220 int i = ARRAY_SIZE(_exits);
222 while (i--)
223 _exits[i]();
227 * Block device functions
229 static int dm_blk_open(struct inode *inode, struct file *file)
231 struct mapped_device *md;
233 spin_lock(&_minor_lock);
235 md = inode->i_bdev->bd_disk->private_data;
236 if (!md)
237 goto out;
239 if (test_bit(DMF_FREEING, &md->flags) ||
240 test_bit(DMF_DELETING, &md->flags)) {
241 md = NULL;
242 goto out;
245 dm_get(md);
246 atomic_inc(&md->open_count);
248 out:
249 spin_unlock(&_minor_lock);
251 return md ? 0 : -ENXIO;
254 static int dm_blk_close(struct inode *inode, struct file *file)
256 struct mapped_device *md;
258 md = inode->i_bdev->bd_disk->private_data;
259 atomic_dec(&md->open_count);
260 dm_put(md);
261 return 0;
264 int dm_open_count(struct mapped_device *md)
266 return atomic_read(&md->open_count);
270 * Guarantees nothing is using the device before it's deleted.
272 int dm_lock_for_deletion(struct mapped_device *md)
274 int r = 0;
276 spin_lock(&_minor_lock);
278 if (dm_open_count(md))
279 r = -EBUSY;
280 else
281 set_bit(DMF_DELETING, &md->flags);
283 spin_unlock(&_minor_lock);
285 return r;
288 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
290 struct mapped_device *md = bdev->bd_disk->private_data;
292 return dm_get_geometry(md, geo);
295 static int dm_blk_ioctl(struct inode *inode, struct file *file,
296 unsigned int cmd, unsigned long arg)
298 struct mapped_device *md;
299 struct dm_table *map;
300 struct dm_target *tgt;
301 int r = -ENOTTY;
303 /* We don't really need this lock, but we do need 'inode'. */
304 unlock_kernel();
306 md = inode->i_bdev->bd_disk->private_data;
308 map = dm_get_table(md);
310 if (!map || !dm_table_get_size(map))
311 goto out;
313 /* We only support devices that have a single target */
314 if (dm_table_get_num_targets(map) != 1)
315 goto out;
317 tgt = dm_table_get_target(map, 0);
319 if (dm_suspended(md)) {
320 r = -EAGAIN;
321 goto out;
324 if (tgt->type->ioctl)
325 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
327 out:
328 dm_table_put(map);
330 lock_kernel();
331 return r;
334 static struct dm_io *alloc_io(struct mapped_device *md)
336 return mempool_alloc(md->io_pool, GFP_NOIO);
339 static void free_io(struct mapped_device *md, struct dm_io *io)
341 mempool_free(io, md->io_pool);
344 static struct dm_target_io *alloc_tio(struct mapped_device *md)
346 return mempool_alloc(md->tio_pool, GFP_NOIO);
349 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
351 mempool_free(tio, md->tio_pool);
354 static void start_io_acct(struct dm_io *io)
356 struct mapped_device *md = io->md;
358 io->start_time = jiffies;
360 preempt_disable();
361 disk_round_stats(dm_disk(md));
362 preempt_enable();
363 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
366 static int end_io_acct(struct dm_io *io)
368 struct mapped_device *md = io->md;
369 struct bio *bio = io->bio;
370 unsigned long duration = jiffies - io->start_time;
371 int pending;
372 int rw = bio_data_dir(bio);
374 preempt_disable();
375 disk_round_stats(dm_disk(md));
376 preempt_enable();
377 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
379 disk_stat_add(dm_disk(md), ticks[rw], duration);
381 return !pending;
385 * Add the bio to the list of deferred io.
387 static int queue_io(struct mapped_device *md, struct bio *bio)
389 down_write(&md->io_lock);
391 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
392 up_write(&md->io_lock);
393 return 1;
396 bio_list_add(&md->deferred, bio);
398 up_write(&md->io_lock);
399 return 0; /* deferred successfully */
403 * Everyone (including functions in this file), should use this
404 * function to access the md->map field, and make sure they call
405 * dm_table_put() when finished.
407 struct dm_table *dm_get_table(struct mapped_device *md)
409 struct dm_table *t;
411 read_lock(&md->map_lock);
412 t = md->map;
413 if (t)
414 dm_table_get(t);
415 read_unlock(&md->map_lock);
417 return t;
421 * Get the geometry associated with a dm device
423 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
425 *geo = md->geometry;
427 return 0;
431 * Set the geometry of a device.
433 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
435 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
437 if (geo->start > sz) {
438 DMWARN("Start sector is beyond the geometry limits.");
439 return -EINVAL;
442 md->geometry = *geo;
444 return 0;
447 /*-----------------------------------------------------------------
448 * CRUD START:
449 * A more elegant soln is in the works that uses the queue
450 * merge fn, unfortunately there are a couple of changes to
451 * the block layer that I want to make for this. So in the
452 * interests of getting something for people to use I give
453 * you this clearly demarcated crap.
454 *---------------------------------------------------------------*/
456 static int __noflush_suspending(struct mapped_device *md)
458 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
462 * Decrements the number of outstanding ios that a bio has been
463 * cloned into, completing the original io if necc.
465 static void dec_pending(struct dm_io *io, int error)
467 unsigned long flags;
469 /* Push-back supersedes any I/O errors */
470 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
471 io->error = error;
473 if (atomic_dec_and_test(&io->io_count)) {
474 if (io->error == DM_ENDIO_REQUEUE) {
476 * Target requested pushing back the I/O.
477 * This must be handled before the sleeper on
478 * suspend queue merges the pushback list.
480 spin_lock_irqsave(&io->md->pushback_lock, flags);
481 if (__noflush_suspending(io->md))
482 bio_list_add(&io->md->pushback, io->bio);
483 else
484 /* noflush suspend was interrupted. */
485 io->error = -EIO;
486 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
489 if (end_io_acct(io))
490 /* nudge anyone waiting on suspend queue */
491 wake_up(&io->md->wait);
493 if (io->error != DM_ENDIO_REQUEUE) {
494 blk_add_trace_bio(io->md->queue, io->bio,
495 BLK_TA_COMPLETE);
497 bio_endio(io->bio, io->error);
500 free_io(io->md, io);
504 static void clone_endio(struct bio *bio, int error)
506 int r = 0;
507 struct dm_target_io *tio = bio->bi_private;
508 struct mapped_device *md = tio->io->md;
509 dm_endio_fn endio = tio->ti->type->end_io;
511 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
512 error = -EIO;
514 if (endio) {
515 r = endio(tio->ti, bio, error, &tio->info);
516 if (r < 0 || r == DM_ENDIO_REQUEUE)
518 * error and requeue request are handled
519 * in dec_pending().
521 error = r;
522 else if (r == DM_ENDIO_INCOMPLETE)
523 /* The target will handle the io */
524 return;
525 else if (r) {
526 DMWARN("unimplemented target endio return value: %d", r);
527 BUG();
531 dec_pending(tio->io, error);
534 * Store md for cleanup instead of tio which is about to get freed.
536 bio->bi_private = md->bs;
538 bio_put(bio);
539 free_tio(md, tio);
542 static sector_t max_io_len(struct mapped_device *md,
543 sector_t sector, struct dm_target *ti)
545 sector_t offset = sector - ti->begin;
546 sector_t len = ti->len - offset;
549 * Does the target need to split even further ?
551 if (ti->split_io) {
552 sector_t boundary;
553 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
554 - offset;
555 if (len > boundary)
556 len = boundary;
559 return len;
562 static void __map_bio(struct dm_target *ti, struct bio *clone,
563 struct dm_target_io *tio)
565 int r;
566 sector_t sector;
567 struct mapped_device *md;
570 * Sanity checks.
572 BUG_ON(!clone->bi_size);
574 clone->bi_end_io = clone_endio;
575 clone->bi_private = tio;
578 * Map the clone. If r == 0 we don't need to do
579 * anything, the target has assumed ownership of
580 * this io.
582 atomic_inc(&tio->io->io_count);
583 sector = clone->bi_sector;
584 r = ti->type->map(ti, clone, &tio->info);
585 if (r == DM_MAPIO_REMAPPED) {
586 /* the bio has been remapped so dispatch it */
588 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
589 tio->io->bio->bi_bdev->bd_dev,
590 clone->bi_sector, sector);
592 generic_make_request(clone);
593 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
594 /* error the io and bail out, or requeue it if needed */
595 md = tio->io->md;
596 dec_pending(tio->io, r);
598 * Store bio_set for cleanup.
600 clone->bi_private = md->bs;
601 bio_put(clone);
602 free_tio(md, tio);
603 } else if (r) {
604 DMWARN("unimplemented target map return value: %d", r);
605 BUG();
609 struct clone_info {
610 struct mapped_device *md;
611 struct dm_table *map;
612 struct bio *bio;
613 struct dm_io *io;
614 sector_t sector;
615 sector_t sector_count;
616 unsigned short idx;
619 static void dm_bio_destructor(struct bio *bio)
621 struct bio_set *bs = bio->bi_private;
623 bio_free(bio, bs);
627 * Creates a little bio that is just does part of a bvec.
629 static struct bio *split_bvec(struct bio *bio, sector_t sector,
630 unsigned short idx, unsigned int offset,
631 unsigned int len, struct bio_set *bs)
633 struct bio *clone;
634 struct bio_vec *bv = bio->bi_io_vec + idx;
636 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
637 clone->bi_destructor = dm_bio_destructor;
638 *clone->bi_io_vec = *bv;
640 clone->bi_sector = sector;
641 clone->bi_bdev = bio->bi_bdev;
642 clone->bi_rw = bio->bi_rw;
643 clone->bi_vcnt = 1;
644 clone->bi_size = to_bytes(len);
645 clone->bi_io_vec->bv_offset = offset;
646 clone->bi_io_vec->bv_len = clone->bi_size;
648 return clone;
652 * Creates a bio that consists of range of complete bvecs.
654 static struct bio *clone_bio(struct bio *bio, sector_t sector,
655 unsigned short idx, unsigned short bv_count,
656 unsigned int len, struct bio_set *bs)
658 struct bio *clone;
660 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
661 __bio_clone(clone, bio);
662 clone->bi_destructor = dm_bio_destructor;
663 clone->bi_sector = sector;
664 clone->bi_idx = idx;
665 clone->bi_vcnt = idx + bv_count;
666 clone->bi_size = to_bytes(len);
667 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
669 return clone;
672 static void __clone_and_map(struct clone_info *ci)
674 struct bio *clone, *bio = ci->bio;
675 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
676 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
677 struct dm_target_io *tio;
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 max = max_io_len(ci->md, ci->sector, ti);
738 tio = alloc_tio(ci->md);
739 tio->io = ci->io;
740 tio->ti = ti;
741 memset(&tio->info, 0, sizeof(tio->info));
744 len = min(remaining, max);
746 clone = split_bvec(bio, ci->sector, ci->idx,
747 bv->bv_offset + offset, len,
748 ci->md->bs);
750 __map_bio(ti, clone, tio);
752 ci->sector += len;
753 ci->sector_count -= len;
754 offset += to_bytes(len);
755 } while (remaining -= len);
757 ci->idx++;
762 * Split the bio into several clones.
764 static int __split_bio(struct mapped_device *md, struct bio *bio)
766 struct clone_info ci;
768 ci.map = dm_get_table(md);
769 if (unlikely(!ci.map))
770 return -EIO;
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 return 0;
793 /*-----------------------------------------------------------------
794 * CRUD END
795 *---------------------------------------------------------------*/
798 * The request function that just remaps the bio built up by
799 * dm_merge_bvec.
801 static int dm_request(struct request_queue *q, struct bio *bio)
803 int r = -EIO;
804 int rw = bio_data_dir(bio);
805 struct mapped_device *md = q->queuedata;
808 * There is no use in forwarding any barrier request since we can't
809 * guarantee it is (or can be) handled by the targets correctly.
811 if (unlikely(bio_barrier(bio))) {
812 bio_endio(bio, -EOPNOTSUPP);
813 return 0;
816 down_read(&md->io_lock);
818 disk_stat_inc(dm_disk(md), ios[rw]);
819 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
822 * If we're suspended we have to queue
823 * this io for later.
825 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
826 up_read(&md->io_lock);
828 if (bio_rw(bio) != READA)
829 r = queue_io(md, bio);
831 if (r <= 0)
832 goto out_req;
835 * We're in a while loop, because someone could suspend
836 * before we get to the following read lock.
838 down_read(&md->io_lock);
841 r = __split_bio(md, bio);
842 up_read(&md->io_lock);
844 out_req:
845 if (r < 0)
846 bio_io_error(bio);
848 return 0;
851 static void dm_unplug_all(struct request_queue *q)
853 struct mapped_device *md = q->queuedata;
854 struct dm_table *map = dm_get_table(md);
856 if (map) {
857 dm_table_unplug_all(map);
858 dm_table_put(map);
862 static int dm_any_congested(void *congested_data, int bdi_bits)
864 int r;
865 struct mapped_device *md = (struct mapped_device *) congested_data;
866 struct dm_table *map = dm_get_table(md);
868 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
869 r = bdi_bits;
870 else
871 r = dm_table_any_congested(map, bdi_bits);
873 dm_table_put(map);
874 return r;
877 /*-----------------------------------------------------------------
878 * An IDR is used to keep track of allocated minor numbers.
879 *---------------------------------------------------------------*/
880 static DEFINE_IDR(_minor_idr);
882 static void free_minor(int minor)
884 spin_lock(&_minor_lock);
885 idr_remove(&_minor_idr, minor);
886 spin_unlock(&_minor_lock);
890 * See if the device with a specific minor # is free.
892 static int specific_minor(struct mapped_device *md, int minor)
894 int r, m;
896 if (minor >= (1 << MINORBITS))
897 return -EINVAL;
899 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
900 if (!r)
901 return -ENOMEM;
903 spin_lock(&_minor_lock);
905 if (idr_find(&_minor_idr, minor)) {
906 r = -EBUSY;
907 goto out;
910 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
911 if (r)
912 goto out;
914 if (m != minor) {
915 idr_remove(&_minor_idr, m);
916 r = -EBUSY;
917 goto out;
920 out:
921 spin_unlock(&_minor_lock);
922 return r;
925 static int next_free_minor(struct mapped_device *md, int *minor)
927 int r, m;
929 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
930 if (!r)
931 return -ENOMEM;
933 spin_lock(&_minor_lock);
935 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
936 if (r) {
937 goto out;
940 if (m >= (1 << MINORBITS)) {
941 idr_remove(&_minor_idr, m);
942 r = -ENOSPC;
943 goto out;
946 *minor = m;
948 out:
949 spin_unlock(&_minor_lock);
950 return r;
953 static struct block_device_operations dm_blk_dops;
956 * Allocate and initialise a blank device with a given minor.
958 static struct mapped_device *alloc_dev(int minor)
960 int r;
961 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
962 void *old_md;
964 if (!md) {
965 DMWARN("unable to allocate device, out of memory.");
966 return NULL;
969 if (!try_module_get(THIS_MODULE))
970 goto bad0;
972 /* get a minor number for the dev */
973 if (minor == DM_ANY_MINOR)
974 r = next_free_minor(md, &minor);
975 else
976 r = specific_minor(md, minor);
977 if (r < 0)
978 goto bad1;
980 memset(md, 0, sizeof(*md));
981 init_rwsem(&md->io_lock);
982 init_MUTEX(&md->suspend_lock);
983 spin_lock_init(&md->pushback_lock);
984 rwlock_init(&md->map_lock);
985 atomic_set(&md->holders, 1);
986 atomic_set(&md->open_count, 0);
987 atomic_set(&md->event_nr, 0);
989 md->queue = blk_alloc_queue(GFP_KERNEL);
990 if (!md->queue)
991 goto bad1_free_minor;
993 md->queue->queuedata = md;
994 md->queue->backing_dev_info.congested_fn = dm_any_congested;
995 md->queue->backing_dev_info.congested_data = md;
996 blk_queue_make_request(md->queue, dm_request);
997 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
998 md->queue->unplug_fn = dm_unplug_all;
1000 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
1001 if (!md->io_pool)
1002 goto bad2;
1004 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1005 if (!md->tio_pool)
1006 goto bad3;
1008 md->bs = bioset_create(16, 16);
1009 if (!md->bs)
1010 goto bad_no_bioset;
1012 md->disk = alloc_disk(1);
1013 if (!md->disk)
1014 goto bad4;
1016 atomic_set(&md->pending, 0);
1017 init_waitqueue_head(&md->wait);
1018 init_waitqueue_head(&md->eventq);
1020 md->disk->major = _major;
1021 md->disk->first_minor = minor;
1022 md->disk->fops = &dm_blk_dops;
1023 md->disk->queue = md->queue;
1024 md->disk->private_data = md;
1025 sprintf(md->disk->disk_name, "dm-%d", minor);
1026 add_disk(md->disk);
1027 format_dev_t(md->name, MKDEV(_major, minor));
1029 /* Populate the mapping, nobody knows we exist yet */
1030 spin_lock(&_minor_lock);
1031 old_md = idr_replace(&_minor_idr, md, minor);
1032 spin_unlock(&_minor_lock);
1034 BUG_ON(old_md != MINOR_ALLOCED);
1036 return md;
1038 bad4:
1039 bioset_free(md->bs);
1040 bad_no_bioset:
1041 mempool_destroy(md->tio_pool);
1042 bad3:
1043 mempool_destroy(md->io_pool);
1044 bad2:
1045 blk_cleanup_queue(md->queue);
1046 bad1_free_minor:
1047 free_minor(minor);
1048 bad1:
1049 module_put(THIS_MODULE);
1050 bad0:
1051 kfree(md);
1052 return NULL;
1055 static void unlock_fs(struct mapped_device *md);
1057 static void free_dev(struct mapped_device *md)
1059 int minor = md->disk->first_minor;
1061 if (md->suspended_bdev) {
1062 unlock_fs(md);
1063 bdput(md->suspended_bdev);
1065 mempool_destroy(md->tio_pool);
1066 mempool_destroy(md->io_pool);
1067 bioset_free(md->bs);
1068 del_gendisk(md->disk);
1069 free_minor(minor);
1071 spin_lock(&_minor_lock);
1072 md->disk->private_data = NULL;
1073 spin_unlock(&_minor_lock);
1075 put_disk(md->disk);
1076 blk_cleanup_queue(md->queue);
1077 module_put(THIS_MODULE);
1078 kfree(md);
1082 * Bind a table to the device.
1084 static void event_callback(void *context)
1086 struct mapped_device *md = (struct mapped_device *) context;
1088 atomic_inc(&md->event_nr);
1089 wake_up(&md->eventq);
1092 static void __set_size(struct mapped_device *md, sector_t size)
1094 set_capacity(md->disk, size);
1096 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1097 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1098 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1101 static int __bind(struct mapped_device *md, struct dm_table *t)
1103 struct request_queue *q = md->queue;
1104 sector_t size;
1106 size = dm_table_get_size(t);
1109 * Wipe any geometry if the size of the table changed.
1111 if (size != get_capacity(md->disk))
1112 memset(&md->geometry, 0, sizeof(md->geometry));
1114 if (md->suspended_bdev)
1115 __set_size(md, size);
1116 if (size == 0)
1117 return 0;
1119 dm_table_get(t);
1120 dm_table_event_callback(t, event_callback, md);
1122 write_lock(&md->map_lock);
1123 md->map = t;
1124 dm_table_set_restrictions(t, q);
1125 write_unlock(&md->map_lock);
1127 return 0;
1130 static void __unbind(struct mapped_device *md)
1132 struct dm_table *map = md->map;
1134 if (!map)
1135 return;
1137 dm_table_event_callback(map, NULL, NULL);
1138 write_lock(&md->map_lock);
1139 md->map = NULL;
1140 write_unlock(&md->map_lock);
1141 dm_table_put(map);
1145 * Constructor for a new device.
1147 int dm_create(int minor, struct mapped_device **result)
1149 struct mapped_device *md;
1151 md = alloc_dev(minor);
1152 if (!md)
1153 return -ENXIO;
1155 *result = md;
1156 return 0;
1159 static struct mapped_device *dm_find_md(dev_t dev)
1161 struct mapped_device *md;
1162 unsigned minor = MINOR(dev);
1164 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1165 return NULL;
1167 spin_lock(&_minor_lock);
1169 md = idr_find(&_minor_idr, minor);
1170 if (md && (md == MINOR_ALLOCED ||
1171 (dm_disk(md)->first_minor != minor) ||
1172 test_bit(DMF_FREEING, &md->flags))) {
1173 md = NULL;
1174 goto out;
1177 out:
1178 spin_unlock(&_minor_lock);
1180 return md;
1183 struct mapped_device *dm_get_md(dev_t dev)
1185 struct mapped_device *md = dm_find_md(dev);
1187 if (md)
1188 dm_get(md);
1190 return md;
1193 void *dm_get_mdptr(struct mapped_device *md)
1195 return md->interface_ptr;
1198 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1200 md->interface_ptr = ptr;
1203 void dm_get(struct mapped_device *md)
1205 atomic_inc(&md->holders);
1208 const char *dm_device_name(struct mapped_device *md)
1210 return md->name;
1212 EXPORT_SYMBOL_GPL(dm_device_name);
1214 void dm_put(struct mapped_device *md)
1216 struct dm_table *map;
1218 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1220 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1221 map = dm_get_table(md);
1222 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1223 set_bit(DMF_FREEING, &md->flags);
1224 spin_unlock(&_minor_lock);
1225 if (!dm_suspended(md)) {
1226 dm_table_presuspend_targets(map);
1227 dm_table_postsuspend_targets(map);
1229 __unbind(md);
1230 dm_table_put(map);
1231 free_dev(md);
1234 EXPORT_SYMBOL_GPL(dm_put);
1237 * Process the deferred bios
1239 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1241 struct bio *n;
1243 while (c) {
1244 n = c->bi_next;
1245 c->bi_next = NULL;
1246 if (__split_bio(md, c))
1247 bio_io_error(c);
1248 c = n;
1253 * Swap in a new table (destroying old one).
1255 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1257 int r = -EINVAL;
1259 down(&md->suspend_lock);
1261 /* device must be suspended */
1262 if (!dm_suspended(md))
1263 goto out;
1265 /* without bdev, the device size cannot be changed */
1266 if (!md->suspended_bdev)
1267 if (get_capacity(md->disk) != dm_table_get_size(table))
1268 goto out;
1270 __unbind(md);
1271 r = __bind(md, table);
1273 out:
1274 up(&md->suspend_lock);
1275 return r;
1279 * Functions to lock and unlock any filesystem running on the
1280 * device.
1282 static int lock_fs(struct mapped_device *md)
1284 int r;
1286 WARN_ON(md->frozen_sb);
1288 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1289 if (IS_ERR(md->frozen_sb)) {
1290 r = PTR_ERR(md->frozen_sb);
1291 md->frozen_sb = NULL;
1292 return r;
1295 set_bit(DMF_FROZEN, &md->flags);
1297 /* don't bdput right now, we don't want the bdev
1298 * to go away while it is locked.
1300 return 0;
1303 static void unlock_fs(struct mapped_device *md)
1305 if (!test_bit(DMF_FROZEN, &md->flags))
1306 return;
1308 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1309 md->frozen_sb = NULL;
1310 clear_bit(DMF_FROZEN, &md->flags);
1314 * We need to be able to change a mapping table under a mounted
1315 * filesystem. For example we might want to move some data in
1316 * the background. Before the table can be swapped with
1317 * dm_bind_table, dm_suspend must be called to flush any in
1318 * flight bios and ensure that any further io gets deferred.
1320 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1322 struct dm_table *map = NULL;
1323 unsigned long flags;
1324 DECLARE_WAITQUEUE(wait, current);
1325 struct bio *def;
1326 int r = -EINVAL;
1327 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1328 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1330 down(&md->suspend_lock);
1332 if (dm_suspended(md))
1333 goto out_unlock;
1335 map = dm_get_table(md);
1338 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1339 * This flag is cleared before dm_suspend returns.
1341 if (noflush)
1342 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1344 /* This does not get reverted if there's an error later. */
1345 dm_table_presuspend_targets(map);
1347 /* bdget() can stall if the pending I/Os are not flushed */
1348 if (!noflush) {
1349 md->suspended_bdev = bdget_disk(md->disk, 0);
1350 if (!md->suspended_bdev) {
1351 DMWARN("bdget failed in dm_suspend");
1352 r = -ENOMEM;
1353 goto flush_and_out;
1358 * Flush I/O to the device.
1359 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1361 if (do_lockfs && !noflush) {
1362 r = lock_fs(md);
1363 if (r)
1364 goto out;
1368 * First we set the BLOCK_IO flag so no more ios will be mapped.
1370 down_write(&md->io_lock);
1371 set_bit(DMF_BLOCK_IO, &md->flags);
1373 add_wait_queue(&md->wait, &wait);
1374 up_write(&md->io_lock);
1376 /* unplug */
1377 if (map)
1378 dm_table_unplug_all(map);
1381 * Then we wait for the already mapped ios to
1382 * complete.
1384 while (1) {
1385 set_current_state(TASK_INTERRUPTIBLE);
1387 if (!atomic_read(&md->pending) || signal_pending(current))
1388 break;
1390 io_schedule();
1392 set_current_state(TASK_RUNNING);
1394 down_write(&md->io_lock);
1395 remove_wait_queue(&md->wait, &wait);
1397 if (noflush) {
1398 spin_lock_irqsave(&md->pushback_lock, flags);
1399 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1400 bio_list_merge_head(&md->deferred, &md->pushback);
1401 bio_list_init(&md->pushback);
1402 spin_unlock_irqrestore(&md->pushback_lock, flags);
1405 /* were we interrupted ? */
1406 r = -EINTR;
1407 if (atomic_read(&md->pending)) {
1408 clear_bit(DMF_BLOCK_IO, &md->flags);
1409 def = bio_list_get(&md->deferred);
1410 __flush_deferred_io(md, def);
1411 up_write(&md->io_lock);
1412 unlock_fs(md);
1413 goto out; /* pushback list is already flushed, so skip flush */
1415 up_write(&md->io_lock);
1417 dm_table_postsuspend_targets(map);
1419 set_bit(DMF_SUSPENDED, &md->flags);
1421 r = 0;
1423 flush_and_out:
1424 if (r && noflush) {
1426 * Because there may be already I/Os in the pushback list,
1427 * flush them before return.
1429 down_write(&md->io_lock);
1431 spin_lock_irqsave(&md->pushback_lock, flags);
1432 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1433 bio_list_merge_head(&md->deferred, &md->pushback);
1434 bio_list_init(&md->pushback);
1435 spin_unlock_irqrestore(&md->pushback_lock, flags);
1437 def = bio_list_get(&md->deferred);
1438 __flush_deferred_io(md, def);
1439 up_write(&md->io_lock);
1442 out:
1443 if (r && md->suspended_bdev) {
1444 bdput(md->suspended_bdev);
1445 md->suspended_bdev = NULL;
1448 dm_table_put(map);
1450 out_unlock:
1451 up(&md->suspend_lock);
1452 return r;
1455 int dm_resume(struct mapped_device *md)
1457 int r = -EINVAL;
1458 struct bio *def;
1459 struct dm_table *map = NULL;
1461 down(&md->suspend_lock);
1462 if (!dm_suspended(md))
1463 goto out;
1465 map = dm_get_table(md);
1466 if (!map || !dm_table_get_size(map))
1467 goto out;
1469 r = dm_table_resume_targets(map);
1470 if (r)
1471 goto out;
1473 down_write(&md->io_lock);
1474 clear_bit(DMF_BLOCK_IO, &md->flags);
1476 def = bio_list_get(&md->deferred);
1477 __flush_deferred_io(md, def);
1478 up_write(&md->io_lock);
1480 unlock_fs(md);
1482 if (md->suspended_bdev) {
1483 bdput(md->suspended_bdev);
1484 md->suspended_bdev = NULL;
1487 clear_bit(DMF_SUSPENDED, &md->flags);
1489 dm_table_unplug_all(map);
1491 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1493 r = 0;
1495 out:
1496 dm_table_put(map);
1497 up(&md->suspend_lock);
1499 return r;
1502 /*-----------------------------------------------------------------
1503 * Event notification.
1504 *---------------------------------------------------------------*/
1505 uint32_t dm_get_event_nr(struct mapped_device *md)
1507 return atomic_read(&md->event_nr);
1510 int dm_wait_event(struct mapped_device *md, int event_nr)
1512 return wait_event_interruptible(md->eventq,
1513 (event_nr != atomic_read(&md->event_nr)));
1517 * The gendisk is only valid as long as you have a reference
1518 * count on 'md'.
1520 struct gendisk *dm_disk(struct mapped_device *md)
1522 return md->disk;
1525 int dm_suspended(struct mapped_device *md)
1527 return test_bit(DMF_SUSPENDED, &md->flags);
1530 int dm_noflush_suspending(struct dm_target *ti)
1532 struct mapped_device *md = dm_table_get_md(ti->table);
1533 int r = __noflush_suspending(md);
1535 dm_put(md);
1537 return r;
1539 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1541 static struct block_device_operations dm_blk_dops = {
1542 .open = dm_blk_open,
1543 .release = dm_blk_close,
1544 .ioctl = dm_blk_ioctl,
1545 .getgeo = dm_blk_getgeo,
1546 .owner = THIS_MODULE
1549 EXPORT_SYMBOL(dm_get_mapinfo);
1552 * module hooks
1554 module_init(dm_init);
1555 module_exit(dm_exit);
1557 module_param(major, uint, 0);
1558 MODULE_PARM_DESC(major, "The major number of the device mapper");
1559 MODULE_DESCRIPTION(DM_NAME " driver");
1560 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1561 MODULE_LICENSE("GPL");