ACPI: thinkpad-acpi: bump up version to 0.22
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm.c
blobb798bd36713883d0437a10d377a2ff0f08650241
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>
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 atomic_t io_count;
40 struct bio *bio;
41 unsigned long start_time;
45 * One of these is allocated per target within a bio. Hopefully
46 * this will be simplified out one day.
48 struct dm_target_io {
49 struct dm_io *io;
50 struct dm_target *ti;
51 union map_info info;
54 union map_info *dm_get_mapinfo(struct bio *bio)
56 if (bio && bio->bi_private)
57 return &((struct dm_target_io *)bio->bi_private)->info;
58 return NULL;
61 #define MINOR_ALLOCED ((void *)-1)
64 * Bits for the md->flags field.
66 #define DMF_BLOCK_IO 0
67 #define DMF_SUSPENDED 1
68 #define DMF_FROZEN 2
69 #define DMF_FREEING 3
70 #define DMF_DELETING 4
71 #define DMF_NOFLUSH_SUSPENDING 5
74 * Work processed by per-device workqueue.
76 struct dm_wq_req {
77 enum {
78 DM_WQ_FLUSH_DEFERRED,
79 } type;
80 struct work_struct work;
81 struct mapped_device *md;
82 void *context;
85 struct mapped_device {
86 struct rw_semaphore io_lock;
87 struct mutex suspend_lock;
88 spinlock_t pushback_lock;
89 rwlock_t map_lock;
90 atomic_t holders;
91 atomic_t open_count;
93 unsigned long flags;
95 struct request_queue *queue;
96 struct gendisk *disk;
97 char name[16];
99 void *interface_ptr;
102 * A list of ios that arrived while we were suspended.
104 atomic_t pending;
105 wait_queue_head_t wait;
106 struct bio_list deferred;
107 struct bio_list pushback;
110 * Processing queue (flush/barriers)
112 struct workqueue_struct *wq;
115 * The current mapping.
117 struct dm_table *map;
120 * io objects are allocated from here.
122 mempool_t *io_pool;
123 mempool_t *tio_pool;
125 struct bio_set *bs;
128 * Event handling.
130 atomic_t event_nr;
131 wait_queue_head_t eventq;
132 atomic_t uevent_seq;
133 struct list_head uevent_list;
134 spinlock_t uevent_lock; /* Protect access to uevent_list */
137 * freeze/thaw support require holding onto a super block
139 struct super_block *frozen_sb;
140 struct block_device *suspended_bdev;
142 /* forced geometry settings */
143 struct hd_geometry geometry;
146 #define MIN_IOS 256
147 static struct kmem_cache *_io_cache;
148 static struct kmem_cache *_tio_cache;
150 static int __init local_init(void)
152 int r = -ENOMEM;
154 /* allocate a slab for the dm_ios */
155 _io_cache = KMEM_CACHE(dm_io, 0);
156 if (!_io_cache)
157 return r;
159 /* allocate a slab for the target ios */
160 _tio_cache = KMEM_CACHE(dm_target_io, 0);
161 if (!_tio_cache)
162 goto out_free_io_cache;
164 r = dm_uevent_init();
165 if (r)
166 goto out_free_tio_cache;
168 _major = major;
169 r = register_blkdev(_major, _name);
170 if (r < 0)
171 goto out_uevent_exit;
173 if (!_major)
174 _major = r;
176 return 0;
178 out_uevent_exit:
179 dm_uevent_exit();
180 out_free_tio_cache:
181 kmem_cache_destroy(_tio_cache);
182 out_free_io_cache:
183 kmem_cache_destroy(_io_cache);
185 return r;
188 static void local_exit(void)
190 kmem_cache_destroy(_tio_cache);
191 kmem_cache_destroy(_io_cache);
192 unregister_blkdev(_major, _name);
193 dm_uevent_exit();
195 _major = 0;
197 DMINFO("cleaned up");
200 static int (*_inits[])(void) __initdata = {
201 local_init,
202 dm_target_init,
203 dm_linear_init,
204 dm_stripe_init,
205 dm_kcopyd_init,
206 dm_interface_init,
209 static void (*_exits[])(void) = {
210 local_exit,
211 dm_target_exit,
212 dm_linear_exit,
213 dm_stripe_exit,
214 dm_kcopyd_exit,
215 dm_interface_exit,
218 static int __init dm_init(void)
220 const int count = ARRAY_SIZE(_inits);
222 int r, i;
224 for (i = 0; i < count; i++) {
225 r = _inits[i]();
226 if (r)
227 goto bad;
230 return 0;
232 bad:
233 while (i--)
234 _exits[i]();
236 return r;
239 static void __exit dm_exit(void)
241 int i = ARRAY_SIZE(_exits);
243 while (i--)
244 _exits[i]();
248 * Block device functions
250 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
252 struct mapped_device *md;
254 spin_lock(&_minor_lock);
256 md = bdev->bd_disk->private_data;
257 if (!md)
258 goto out;
260 if (test_bit(DMF_FREEING, &md->flags) ||
261 test_bit(DMF_DELETING, &md->flags)) {
262 md = NULL;
263 goto out;
266 dm_get(md);
267 atomic_inc(&md->open_count);
269 out:
270 spin_unlock(&_minor_lock);
272 return md ? 0 : -ENXIO;
275 static int dm_blk_close(struct gendisk *disk, fmode_t mode)
277 struct mapped_device *md = disk->private_data;
278 atomic_dec(&md->open_count);
279 dm_put(md);
280 return 0;
283 int dm_open_count(struct mapped_device *md)
285 return atomic_read(&md->open_count);
289 * Guarantees nothing is using the device before it's deleted.
291 int dm_lock_for_deletion(struct mapped_device *md)
293 int r = 0;
295 spin_lock(&_minor_lock);
297 if (dm_open_count(md))
298 r = -EBUSY;
299 else
300 set_bit(DMF_DELETING, &md->flags);
302 spin_unlock(&_minor_lock);
304 return r;
307 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
309 struct mapped_device *md = bdev->bd_disk->private_data;
311 return dm_get_geometry(md, geo);
314 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
315 unsigned int cmd, unsigned long arg)
317 struct mapped_device *md = bdev->bd_disk->private_data;
318 struct dm_table *map = dm_get_table(md);
319 struct dm_target *tgt;
320 int r = -ENOTTY;
322 if (!map || !dm_table_get_size(map))
323 goto out;
325 /* We only support devices that have a single target */
326 if (dm_table_get_num_targets(map) != 1)
327 goto out;
329 tgt = dm_table_get_target(map, 0);
331 if (dm_suspended(md)) {
332 r = -EAGAIN;
333 goto out;
336 if (tgt->type->ioctl)
337 r = tgt->type->ioctl(tgt, cmd, arg);
339 out:
340 dm_table_put(map);
342 return r;
345 static struct dm_io *alloc_io(struct mapped_device *md)
347 return mempool_alloc(md->io_pool, GFP_NOIO);
350 static void free_io(struct mapped_device *md, struct dm_io *io)
352 mempool_free(io, md->io_pool);
355 static struct dm_target_io *alloc_tio(struct mapped_device *md)
357 return mempool_alloc(md->tio_pool, GFP_NOIO);
360 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
362 mempool_free(tio, md->tio_pool);
365 static void start_io_acct(struct dm_io *io)
367 struct mapped_device *md = io->md;
368 int cpu;
370 io->start_time = jiffies;
372 cpu = part_stat_lock();
373 part_round_stats(cpu, &dm_disk(md)->part0);
374 part_stat_unlock();
375 dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending);
378 static void end_io_acct(struct dm_io *io)
380 struct mapped_device *md = io->md;
381 struct bio *bio = io->bio;
382 unsigned long duration = jiffies - io->start_time;
383 int pending, cpu;
384 int rw = bio_data_dir(bio);
386 cpu = part_stat_lock();
387 part_round_stats(cpu, &dm_disk(md)->part0);
388 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
389 part_stat_unlock();
391 dm_disk(md)->part0.in_flight = pending =
392 atomic_dec_return(&md->pending);
394 /* nudge anyone waiting on suspend queue */
395 if (!pending)
396 wake_up(&md->wait);
400 * Add the bio to the list of deferred io.
402 static int queue_io(struct mapped_device *md, struct bio *bio)
404 down_write(&md->io_lock);
406 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
407 up_write(&md->io_lock);
408 return 1;
411 bio_list_add(&md->deferred, bio);
413 up_write(&md->io_lock);
414 return 0; /* deferred successfully */
418 * Everyone (including functions in this file), should use this
419 * function to access the md->map field, and make sure they call
420 * dm_table_put() when finished.
422 struct dm_table *dm_get_table(struct mapped_device *md)
424 struct dm_table *t;
426 read_lock(&md->map_lock);
427 t = md->map;
428 if (t)
429 dm_table_get(t);
430 read_unlock(&md->map_lock);
432 return t;
436 * Get the geometry associated with a dm device
438 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
440 *geo = md->geometry;
442 return 0;
446 * Set the geometry of a device.
448 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
450 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
452 if (geo->start > sz) {
453 DMWARN("Start sector is beyond the geometry limits.");
454 return -EINVAL;
457 md->geometry = *geo;
459 return 0;
462 /*-----------------------------------------------------------------
463 * CRUD START:
464 * A more elegant soln is in the works that uses the queue
465 * merge fn, unfortunately there are a couple of changes to
466 * the block layer that I want to make for this. So in the
467 * interests of getting something for people to use I give
468 * you this clearly demarcated crap.
469 *---------------------------------------------------------------*/
471 static int __noflush_suspending(struct mapped_device *md)
473 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
477 * Decrements the number of outstanding ios that a bio has been
478 * cloned into, completing the original io if necc.
480 static void dec_pending(struct dm_io *io, int error)
482 unsigned long flags;
483 int io_error;
484 struct bio *bio;
485 struct mapped_device *md = io->md;
487 /* Push-back supersedes any I/O errors */
488 if (error && !(io->error > 0 && __noflush_suspending(md)))
489 io->error = error;
491 if (atomic_dec_and_test(&io->io_count)) {
492 if (io->error == DM_ENDIO_REQUEUE) {
494 * Target requested pushing back the I/O.
495 * This must be handled before the sleeper on
496 * suspend queue merges the pushback list.
498 spin_lock_irqsave(&md->pushback_lock, flags);
499 if (__noflush_suspending(md))
500 bio_list_add(&md->pushback, io->bio);
501 else
502 /* noflush suspend was interrupted. */
503 io->error = -EIO;
504 spin_unlock_irqrestore(&md->pushback_lock, flags);
507 end_io_acct(io);
509 io_error = io->error;
510 bio = io->bio;
512 free_io(md, io);
514 if (io_error != DM_ENDIO_REQUEUE) {
515 blk_add_trace_bio(md->queue, io->bio,
516 BLK_TA_COMPLETE);
518 bio_endio(bio, io_error);
523 static void clone_endio(struct bio *bio, int error)
525 int r = 0;
526 struct dm_target_io *tio = bio->bi_private;
527 struct dm_io *io = tio->io;
528 struct mapped_device *md = tio->io->md;
529 dm_endio_fn endio = tio->ti->type->end_io;
531 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
532 error = -EIO;
534 if (endio) {
535 r = endio(tio->ti, bio, error, &tio->info);
536 if (r < 0 || r == DM_ENDIO_REQUEUE)
538 * error and requeue request are handled
539 * in dec_pending().
541 error = r;
542 else if (r == DM_ENDIO_INCOMPLETE)
543 /* The target will handle the io */
544 return;
545 else if (r) {
546 DMWARN("unimplemented target endio return value: %d", r);
547 BUG();
552 * Store md for cleanup instead of tio which is about to get freed.
554 bio->bi_private = md->bs;
556 free_tio(md, tio);
557 bio_put(bio);
558 dec_pending(io, error);
561 static sector_t max_io_len(struct mapped_device *md,
562 sector_t sector, struct dm_target *ti)
564 sector_t offset = sector - ti->begin;
565 sector_t len = ti->len - offset;
568 * Does the target need to split even further ?
570 if (ti->split_io) {
571 sector_t boundary;
572 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
573 - offset;
574 if (len > boundary)
575 len = boundary;
578 return len;
581 static void __map_bio(struct dm_target *ti, struct bio *clone,
582 struct dm_target_io *tio)
584 int r;
585 sector_t sector;
586 struct mapped_device *md;
589 * Sanity checks.
591 BUG_ON(!clone->bi_size);
593 clone->bi_end_io = clone_endio;
594 clone->bi_private = tio;
597 * Map the clone. If r == 0 we don't need to do
598 * anything, the target has assumed ownership of
599 * this io.
601 atomic_inc(&tio->io->io_count);
602 sector = clone->bi_sector;
603 r = ti->type->map(ti, clone, &tio->info);
604 if (r == DM_MAPIO_REMAPPED) {
605 /* the bio has been remapped so dispatch it */
607 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
608 tio->io->bio->bi_bdev->bd_dev,
609 clone->bi_sector, sector);
611 generic_make_request(clone);
612 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
613 /* error the io and bail out, or requeue it if needed */
614 md = tio->io->md;
615 dec_pending(tio->io, r);
617 * Store bio_set for cleanup.
619 clone->bi_private = md->bs;
620 bio_put(clone);
621 free_tio(md, tio);
622 } else if (r) {
623 DMWARN("unimplemented target map return value: %d", r);
624 BUG();
628 struct clone_info {
629 struct mapped_device *md;
630 struct dm_table *map;
631 struct bio *bio;
632 struct dm_io *io;
633 sector_t sector;
634 sector_t sector_count;
635 unsigned short idx;
638 static void dm_bio_destructor(struct bio *bio)
640 struct bio_set *bs = bio->bi_private;
642 bio_free(bio, bs);
646 * Creates a little bio that is just does part of a bvec.
648 static struct bio *split_bvec(struct bio *bio, sector_t sector,
649 unsigned short idx, unsigned int offset,
650 unsigned int len, struct bio_set *bs)
652 struct bio *clone;
653 struct bio_vec *bv = bio->bi_io_vec + idx;
655 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
656 clone->bi_destructor = dm_bio_destructor;
657 *clone->bi_io_vec = *bv;
659 clone->bi_sector = sector;
660 clone->bi_bdev = bio->bi_bdev;
661 clone->bi_rw = bio->bi_rw;
662 clone->bi_vcnt = 1;
663 clone->bi_size = to_bytes(len);
664 clone->bi_io_vec->bv_offset = offset;
665 clone->bi_io_vec->bv_len = clone->bi_size;
666 clone->bi_flags |= 1 << BIO_CLONED;
668 return clone;
672 * Creates a bio that consists of range of complete bvecs.
674 static struct bio *clone_bio(struct bio *bio, sector_t sector,
675 unsigned short idx, unsigned short bv_count,
676 unsigned int len, struct bio_set *bs)
678 struct bio *clone;
680 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
681 __bio_clone(clone, bio);
682 clone->bi_destructor = dm_bio_destructor;
683 clone->bi_sector = sector;
684 clone->bi_idx = idx;
685 clone->bi_vcnt = idx + bv_count;
686 clone->bi_size = to_bytes(len);
687 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
689 return clone;
692 static int __clone_and_map(struct clone_info *ci)
694 struct bio *clone, *bio = ci->bio;
695 struct dm_target *ti;
696 sector_t len = 0, max;
697 struct dm_target_io *tio;
699 ti = dm_table_find_target(ci->map, ci->sector);
700 if (!dm_target_is_valid(ti))
701 return -EIO;
703 max = max_io_len(ci->md, ci->sector, ti);
706 * Allocate a target io object.
708 tio = alloc_tio(ci->md);
709 tio->io = ci->io;
710 tio->ti = ti;
711 memset(&tio->info, 0, sizeof(tio->info));
713 if (ci->sector_count <= max) {
715 * Optimise for the simple case where we can do all of
716 * the remaining io with a single clone.
718 clone = clone_bio(bio, ci->sector, ci->idx,
719 bio->bi_vcnt - ci->idx, ci->sector_count,
720 ci->md->bs);
721 __map_bio(ti, clone, tio);
722 ci->sector_count = 0;
724 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
726 * There are some bvecs that don't span targets.
727 * Do as many of these as possible.
729 int i;
730 sector_t remaining = max;
731 sector_t bv_len;
733 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
734 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
736 if (bv_len > remaining)
737 break;
739 remaining -= bv_len;
740 len += bv_len;
743 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
744 ci->md->bs);
745 __map_bio(ti, clone, tio);
747 ci->sector += len;
748 ci->sector_count -= len;
749 ci->idx = i;
751 } else {
753 * Handle a bvec that must be split between two or more targets.
755 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
756 sector_t remaining = to_sector(bv->bv_len);
757 unsigned int offset = 0;
759 do {
760 if (offset) {
761 ti = dm_table_find_target(ci->map, ci->sector);
762 if (!dm_target_is_valid(ti))
763 return -EIO;
765 max = max_io_len(ci->md, ci->sector, ti);
767 tio = alloc_tio(ci->md);
768 tio->io = ci->io;
769 tio->ti = ti;
770 memset(&tio->info, 0, sizeof(tio->info));
773 len = min(remaining, max);
775 clone = split_bvec(bio, ci->sector, ci->idx,
776 bv->bv_offset + offset, len,
777 ci->md->bs);
779 __map_bio(ti, clone, tio);
781 ci->sector += len;
782 ci->sector_count -= len;
783 offset += to_bytes(len);
784 } while (remaining -= len);
786 ci->idx++;
789 return 0;
793 * Split the bio into several clones.
795 static int __split_bio(struct mapped_device *md, struct bio *bio)
797 struct clone_info ci;
798 int error = 0;
800 ci.map = dm_get_table(md);
801 if (unlikely(!ci.map))
802 return -EIO;
804 ci.md = md;
805 ci.bio = bio;
806 ci.io = alloc_io(md);
807 ci.io->error = 0;
808 atomic_set(&ci.io->io_count, 1);
809 ci.io->bio = bio;
810 ci.io->md = md;
811 ci.sector = bio->bi_sector;
812 ci.sector_count = bio_sectors(bio);
813 ci.idx = bio->bi_idx;
815 start_io_acct(ci.io);
816 while (ci.sector_count && !error)
817 error = __clone_and_map(&ci);
819 /* drop the extra reference count */
820 dec_pending(ci.io, error);
821 dm_table_put(ci.map);
823 return 0;
825 /*-----------------------------------------------------------------
826 * CRUD END
827 *---------------------------------------------------------------*/
829 static int dm_merge_bvec(struct request_queue *q,
830 struct bvec_merge_data *bvm,
831 struct bio_vec *biovec)
833 struct mapped_device *md = q->queuedata;
834 struct dm_table *map = dm_get_table(md);
835 struct dm_target *ti;
836 sector_t max_sectors;
837 int max_size = 0;
839 if (unlikely(!map))
840 goto out;
842 ti = dm_table_find_target(map, bvm->bi_sector);
843 if (!dm_target_is_valid(ti))
844 goto out_table;
847 * Find maximum amount of I/O that won't need splitting
849 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
850 (sector_t) BIO_MAX_SECTORS);
851 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
852 if (max_size < 0)
853 max_size = 0;
856 * merge_bvec_fn() returns number of bytes
857 * it can accept at this offset
858 * max is precomputed maximal io size
860 if (max_size && ti->type->merge)
861 max_size = ti->type->merge(ti, bvm, biovec, max_size);
863 out_table:
864 dm_table_put(map);
866 out:
868 * Always allow an entire first page
870 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
871 max_size = biovec->bv_len;
873 return max_size;
877 * The request function that just remaps the bio built up by
878 * dm_merge_bvec.
880 static int dm_request(struct request_queue *q, struct bio *bio)
882 int r = -EIO;
883 int rw = bio_data_dir(bio);
884 struct mapped_device *md = q->queuedata;
885 int cpu;
888 * There is no use in forwarding any barrier request since we can't
889 * guarantee it is (or can be) handled by the targets correctly.
891 if (unlikely(bio_barrier(bio))) {
892 bio_endio(bio, -EOPNOTSUPP);
893 return 0;
896 down_read(&md->io_lock);
898 cpu = part_stat_lock();
899 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
900 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
901 part_stat_unlock();
904 * If we're suspended we have to queue
905 * this io for later.
907 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
908 up_read(&md->io_lock);
910 if (bio_rw(bio) != READA)
911 r = queue_io(md, bio);
913 if (r <= 0)
914 goto out_req;
917 * We're in a while loop, because someone could suspend
918 * before we get to the following read lock.
920 down_read(&md->io_lock);
923 r = __split_bio(md, bio);
924 up_read(&md->io_lock);
926 out_req:
927 if (r < 0)
928 bio_io_error(bio);
930 return 0;
933 static void dm_unplug_all(struct request_queue *q)
935 struct mapped_device *md = q->queuedata;
936 struct dm_table *map = dm_get_table(md);
938 if (map) {
939 dm_table_unplug_all(map);
940 dm_table_put(map);
944 static int dm_any_congested(void *congested_data, int bdi_bits)
946 int r = bdi_bits;
947 struct mapped_device *md = congested_data;
948 struct dm_table *map;
950 atomic_inc(&md->pending);
952 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
953 map = dm_get_table(md);
954 if (map) {
955 r = dm_table_any_congested(map, bdi_bits);
956 dm_table_put(map);
960 if (!atomic_dec_return(&md->pending))
961 /* nudge anyone waiting on suspend queue */
962 wake_up(&md->wait);
964 return r;
967 /*-----------------------------------------------------------------
968 * An IDR is used to keep track of allocated minor numbers.
969 *---------------------------------------------------------------*/
970 static DEFINE_IDR(_minor_idr);
972 static void free_minor(int minor)
974 spin_lock(&_minor_lock);
975 idr_remove(&_minor_idr, minor);
976 spin_unlock(&_minor_lock);
980 * See if the device with a specific minor # is free.
982 static int specific_minor(int minor)
984 int r, m;
986 if (minor >= (1 << MINORBITS))
987 return -EINVAL;
989 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
990 if (!r)
991 return -ENOMEM;
993 spin_lock(&_minor_lock);
995 if (idr_find(&_minor_idr, minor)) {
996 r = -EBUSY;
997 goto out;
1000 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
1001 if (r)
1002 goto out;
1004 if (m != minor) {
1005 idr_remove(&_minor_idr, m);
1006 r = -EBUSY;
1007 goto out;
1010 out:
1011 spin_unlock(&_minor_lock);
1012 return r;
1015 static int next_free_minor(int *minor)
1017 int r, m;
1019 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1020 if (!r)
1021 return -ENOMEM;
1023 spin_lock(&_minor_lock);
1025 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1026 if (r)
1027 goto out;
1029 if (m >= (1 << MINORBITS)) {
1030 idr_remove(&_minor_idr, m);
1031 r = -ENOSPC;
1032 goto out;
1035 *minor = m;
1037 out:
1038 spin_unlock(&_minor_lock);
1039 return r;
1042 static struct block_device_operations dm_blk_dops;
1045 * Allocate and initialise a blank device with a given minor.
1047 static struct mapped_device *alloc_dev(int minor)
1049 int r;
1050 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1051 void *old_md;
1053 if (!md) {
1054 DMWARN("unable to allocate device, out of memory.");
1055 return NULL;
1058 if (!try_module_get(THIS_MODULE))
1059 goto bad_module_get;
1061 /* get a minor number for the dev */
1062 if (minor == DM_ANY_MINOR)
1063 r = next_free_minor(&minor);
1064 else
1065 r = specific_minor(minor);
1066 if (r < 0)
1067 goto bad_minor;
1069 init_rwsem(&md->io_lock);
1070 mutex_init(&md->suspend_lock);
1071 spin_lock_init(&md->pushback_lock);
1072 rwlock_init(&md->map_lock);
1073 atomic_set(&md->holders, 1);
1074 atomic_set(&md->open_count, 0);
1075 atomic_set(&md->event_nr, 0);
1076 atomic_set(&md->uevent_seq, 0);
1077 INIT_LIST_HEAD(&md->uevent_list);
1078 spin_lock_init(&md->uevent_lock);
1080 md->queue = blk_alloc_queue(GFP_KERNEL);
1081 if (!md->queue)
1082 goto bad_queue;
1084 md->queue->queuedata = md;
1085 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1086 md->queue->backing_dev_info.congested_data = md;
1087 blk_queue_make_request(md->queue, dm_request);
1088 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1089 md->queue->unplug_fn = dm_unplug_all;
1090 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1092 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
1093 if (!md->io_pool)
1094 goto bad_io_pool;
1096 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1097 if (!md->tio_pool)
1098 goto bad_tio_pool;
1100 md->bs = bioset_create(16, 16);
1101 if (!md->bs)
1102 goto bad_no_bioset;
1104 md->disk = alloc_disk(1);
1105 if (!md->disk)
1106 goto bad_disk;
1108 atomic_set(&md->pending, 0);
1109 init_waitqueue_head(&md->wait);
1110 init_waitqueue_head(&md->eventq);
1112 md->disk->major = _major;
1113 md->disk->first_minor = minor;
1114 md->disk->fops = &dm_blk_dops;
1115 md->disk->queue = md->queue;
1116 md->disk->private_data = md;
1117 sprintf(md->disk->disk_name, "dm-%d", minor);
1118 add_disk(md->disk);
1119 format_dev_t(md->name, MKDEV(_major, minor));
1121 md->wq = create_singlethread_workqueue("kdmflush");
1122 if (!md->wq)
1123 goto bad_thread;
1125 /* Populate the mapping, nobody knows we exist yet */
1126 spin_lock(&_minor_lock);
1127 old_md = idr_replace(&_minor_idr, md, minor);
1128 spin_unlock(&_minor_lock);
1130 BUG_ON(old_md != MINOR_ALLOCED);
1132 return md;
1134 bad_thread:
1135 put_disk(md->disk);
1136 bad_disk:
1137 bioset_free(md->bs);
1138 bad_no_bioset:
1139 mempool_destroy(md->tio_pool);
1140 bad_tio_pool:
1141 mempool_destroy(md->io_pool);
1142 bad_io_pool:
1143 blk_cleanup_queue(md->queue);
1144 bad_queue:
1145 free_minor(minor);
1146 bad_minor:
1147 module_put(THIS_MODULE);
1148 bad_module_get:
1149 kfree(md);
1150 return NULL;
1153 static void unlock_fs(struct mapped_device *md);
1155 static void free_dev(struct mapped_device *md)
1157 int minor = MINOR(disk_devt(md->disk));
1159 if (md->suspended_bdev) {
1160 unlock_fs(md);
1161 bdput(md->suspended_bdev);
1163 destroy_workqueue(md->wq);
1164 mempool_destroy(md->tio_pool);
1165 mempool_destroy(md->io_pool);
1166 bioset_free(md->bs);
1167 del_gendisk(md->disk);
1168 free_minor(minor);
1170 spin_lock(&_minor_lock);
1171 md->disk->private_data = NULL;
1172 spin_unlock(&_minor_lock);
1174 put_disk(md->disk);
1175 blk_cleanup_queue(md->queue);
1176 module_put(THIS_MODULE);
1177 kfree(md);
1181 * Bind a table to the device.
1183 static void event_callback(void *context)
1185 unsigned long flags;
1186 LIST_HEAD(uevents);
1187 struct mapped_device *md = (struct mapped_device *) context;
1189 spin_lock_irqsave(&md->uevent_lock, flags);
1190 list_splice_init(&md->uevent_list, &uevents);
1191 spin_unlock_irqrestore(&md->uevent_lock, flags);
1193 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
1195 atomic_inc(&md->event_nr);
1196 wake_up(&md->eventq);
1199 static void __set_size(struct mapped_device *md, sector_t size)
1201 set_capacity(md->disk, size);
1203 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1204 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1205 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1208 static int __bind(struct mapped_device *md, struct dm_table *t)
1210 struct request_queue *q = md->queue;
1211 sector_t size;
1213 size = dm_table_get_size(t);
1216 * Wipe any geometry if the size of the table changed.
1218 if (size != get_capacity(md->disk))
1219 memset(&md->geometry, 0, sizeof(md->geometry));
1221 if (md->suspended_bdev)
1222 __set_size(md, size);
1223 if (size == 0)
1224 return 0;
1226 dm_table_get(t);
1227 dm_table_event_callback(t, event_callback, md);
1229 write_lock(&md->map_lock);
1230 md->map = t;
1231 dm_table_set_restrictions(t, q);
1232 write_unlock(&md->map_lock);
1234 return 0;
1237 static void __unbind(struct mapped_device *md)
1239 struct dm_table *map = md->map;
1241 if (!map)
1242 return;
1244 dm_table_event_callback(map, NULL, NULL);
1245 write_lock(&md->map_lock);
1246 md->map = NULL;
1247 write_unlock(&md->map_lock);
1248 dm_table_put(map);
1252 * Constructor for a new device.
1254 int dm_create(int minor, struct mapped_device **result)
1256 struct mapped_device *md;
1258 md = alloc_dev(minor);
1259 if (!md)
1260 return -ENXIO;
1262 *result = md;
1263 return 0;
1266 static struct mapped_device *dm_find_md(dev_t dev)
1268 struct mapped_device *md;
1269 unsigned minor = MINOR(dev);
1271 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1272 return NULL;
1274 spin_lock(&_minor_lock);
1276 md = idr_find(&_minor_idr, minor);
1277 if (md && (md == MINOR_ALLOCED ||
1278 (MINOR(disk_devt(dm_disk(md))) != minor) ||
1279 test_bit(DMF_FREEING, &md->flags))) {
1280 md = NULL;
1281 goto out;
1284 out:
1285 spin_unlock(&_minor_lock);
1287 return md;
1290 struct mapped_device *dm_get_md(dev_t dev)
1292 struct mapped_device *md = dm_find_md(dev);
1294 if (md)
1295 dm_get(md);
1297 return md;
1300 void *dm_get_mdptr(struct mapped_device *md)
1302 return md->interface_ptr;
1305 void dm_set_mdptr(struct mapped_device *md, void *ptr)
1307 md->interface_ptr = ptr;
1310 void dm_get(struct mapped_device *md)
1312 atomic_inc(&md->holders);
1315 const char *dm_device_name(struct mapped_device *md)
1317 return md->name;
1319 EXPORT_SYMBOL_GPL(dm_device_name);
1321 void dm_put(struct mapped_device *md)
1323 struct dm_table *map;
1325 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1327 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1328 map = dm_get_table(md);
1329 idr_replace(&_minor_idr, MINOR_ALLOCED,
1330 MINOR(disk_devt(dm_disk(md))));
1331 set_bit(DMF_FREEING, &md->flags);
1332 spin_unlock(&_minor_lock);
1333 if (!dm_suspended(md)) {
1334 dm_table_presuspend_targets(map);
1335 dm_table_postsuspend_targets(map);
1337 __unbind(md);
1338 dm_table_put(map);
1339 free_dev(md);
1342 EXPORT_SYMBOL_GPL(dm_put);
1344 static int dm_wait_for_completion(struct mapped_device *md)
1346 int r = 0;
1348 while (1) {
1349 set_current_state(TASK_INTERRUPTIBLE);
1351 smp_mb();
1352 if (!atomic_read(&md->pending))
1353 break;
1355 if (signal_pending(current)) {
1356 r = -EINTR;
1357 break;
1360 io_schedule();
1362 set_current_state(TASK_RUNNING);
1364 return r;
1368 * Process the deferred bios
1370 static void __flush_deferred_io(struct mapped_device *md)
1372 struct bio *c;
1374 while ((c = bio_list_pop(&md->deferred))) {
1375 if (__split_bio(md, c))
1376 bio_io_error(c);
1379 clear_bit(DMF_BLOCK_IO, &md->flags);
1382 static void __merge_pushback_list(struct mapped_device *md)
1384 unsigned long flags;
1386 spin_lock_irqsave(&md->pushback_lock, flags);
1387 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1388 bio_list_merge_head(&md->deferred, &md->pushback);
1389 bio_list_init(&md->pushback);
1390 spin_unlock_irqrestore(&md->pushback_lock, flags);
1393 static void dm_wq_work(struct work_struct *work)
1395 struct dm_wq_req *req = container_of(work, struct dm_wq_req, work);
1396 struct mapped_device *md = req->md;
1398 down_write(&md->io_lock);
1399 switch (req->type) {
1400 case DM_WQ_FLUSH_DEFERRED:
1401 __flush_deferred_io(md);
1402 break;
1403 default:
1404 DMERR("dm_wq_work: unrecognised work type %d", req->type);
1405 BUG();
1407 up_write(&md->io_lock);
1410 static void dm_wq_queue(struct mapped_device *md, int type, void *context,
1411 struct dm_wq_req *req)
1413 req->type = type;
1414 req->md = md;
1415 req->context = context;
1416 INIT_WORK(&req->work, dm_wq_work);
1417 queue_work(md->wq, &req->work);
1420 static void dm_queue_flush(struct mapped_device *md, int type, void *context)
1422 struct dm_wq_req req;
1424 dm_wq_queue(md, type, context, &req);
1425 flush_workqueue(md->wq);
1429 * Swap in a new table (destroying old one).
1431 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1433 int r = -EINVAL;
1435 mutex_lock(&md->suspend_lock);
1437 /* device must be suspended */
1438 if (!dm_suspended(md))
1439 goto out;
1441 /* without bdev, the device size cannot be changed */
1442 if (!md->suspended_bdev)
1443 if (get_capacity(md->disk) != dm_table_get_size(table))
1444 goto out;
1446 __unbind(md);
1447 r = __bind(md, table);
1449 out:
1450 mutex_unlock(&md->suspend_lock);
1451 return r;
1455 * Functions to lock and unlock any filesystem running on the
1456 * device.
1458 static int lock_fs(struct mapped_device *md)
1460 int r;
1462 WARN_ON(md->frozen_sb);
1464 md->frozen_sb = freeze_bdev(md->suspended_bdev);
1465 if (IS_ERR(md->frozen_sb)) {
1466 r = PTR_ERR(md->frozen_sb);
1467 md->frozen_sb = NULL;
1468 return r;
1471 set_bit(DMF_FROZEN, &md->flags);
1473 /* don't bdput right now, we don't want the bdev
1474 * to go away while it is locked.
1476 return 0;
1479 static void unlock_fs(struct mapped_device *md)
1481 if (!test_bit(DMF_FROZEN, &md->flags))
1482 return;
1484 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1485 md->frozen_sb = NULL;
1486 clear_bit(DMF_FROZEN, &md->flags);
1490 * We need to be able to change a mapping table under a mounted
1491 * filesystem. For example we might want to move some data in
1492 * the background. Before the table can be swapped with
1493 * dm_bind_table, dm_suspend must be called to flush any in
1494 * flight bios and ensure that any further io gets deferred.
1496 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1498 struct dm_table *map = NULL;
1499 DECLARE_WAITQUEUE(wait, current);
1500 int r = 0;
1501 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1502 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1504 mutex_lock(&md->suspend_lock);
1506 if (dm_suspended(md)) {
1507 r = -EINVAL;
1508 goto out_unlock;
1511 map = dm_get_table(md);
1514 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1515 * This flag is cleared before dm_suspend returns.
1517 if (noflush)
1518 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1520 /* This does not get reverted if there's an error later. */
1521 dm_table_presuspend_targets(map);
1523 /* bdget() can stall if the pending I/Os are not flushed */
1524 if (!noflush) {
1525 md->suspended_bdev = bdget_disk(md->disk, 0);
1526 if (!md->suspended_bdev) {
1527 DMWARN("bdget failed in dm_suspend");
1528 r = -ENOMEM;
1529 goto out;
1533 * Flush I/O to the device. noflush supersedes do_lockfs,
1534 * because lock_fs() needs to flush I/Os.
1536 if (do_lockfs) {
1537 r = lock_fs(md);
1538 if (r)
1539 goto out;
1544 * First we set the BLOCK_IO flag so no more ios will be mapped.
1546 down_write(&md->io_lock);
1547 set_bit(DMF_BLOCK_IO, &md->flags);
1549 add_wait_queue(&md->wait, &wait);
1550 up_write(&md->io_lock);
1552 /* unplug */
1553 if (map)
1554 dm_table_unplug_all(map);
1557 * Wait for the already-mapped ios to complete.
1559 r = dm_wait_for_completion(md);
1561 down_write(&md->io_lock);
1562 remove_wait_queue(&md->wait, &wait);
1564 if (noflush)
1565 __merge_pushback_list(md);
1566 up_write(&md->io_lock);
1568 /* were we interrupted ? */
1569 if (r < 0) {
1570 dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL);
1572 unlock_fs(md);
1573 goto out; /* pushback list is already flushed, so skip flush */
1576 dm_table_postsuspend_targets(map);
1578 set_bit(DMF_SUSPENDED, &md->flags);
1580 out:
1581 if (r && md->suspended_bdev) {
1582 bdput(md->suspended_bdev);
1583 md->suspended_bdev = NULL;
1586 dm_table_put(map);
1588 out_unlock:
1589 mutex_unlock(&md->suspend_lock);
1590 return r;
1593 int dm_resume(struct mapped_device *md)
1595 int r = -EINVAL;
1596 struct dm_table *map = NULL;
1598 mutex_lock(&md->suspend_lock);
1599 if (!dm_suspended(md))
1600 goto out;
1602 map = dm_get_table(md);
1603 if (!map || !dm_table_get_size(map))
1604 goto out;
1606 r = dm_table_resume_targets(map);
1607 if (r)
1608 goto out;
1610 dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL);
1612 unlock_fs(md);
1614 if (md->suspended_bdev) {
1615 bdput(md->suspended_bdev);
1616 md->suspended_bdev = NULL;
1619 clear_bit(DMF_SUSPENDED, &md->flags);
1621 dm_table_unplug_all(map);
1623 dm_kobject_uevent(md);
1625 r = 0;
1627 out:
1628 dm_table_put(map);
1629 mutex_unlock(&md->suspend_lock);
1631 return r;
1634 /*-----------------------------------------------------------------
1635 * Event notification.
1636 *---------------------------------------------------------------*/
1637 void dm_kobject_uevent(struct mapped_device *md)
1639 kobject_uevent(&disk_to_dev(md->disk)->kobj, KOBJ_CHANGE);
1642 uint32_t dm_next_uevent_seq(struct mapped_device *md)
1644 return atomic_add_return(1, &md->uevent_seq);
1647 uint32_t dm_get_event_nr(struct mapped_device *md)
1649 return atomic_read(&md->event_nr);
1652 int dm_wait_event(struct mapped_device *md, int event_nr)
1654 return wait_event_interruptible(md->eventq,
1655 (event_nr != atomic_read(&md->event_nr)));
1658 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
1660 unsigned long flags;
1662 spin_lock_irqsave(&md->uevent_lock, flags);
1663 list_add(elist, &md->uevent_list);
1664 spin_unlock_irqrestore(&md->uevent_lock, flags);
1668 * The gendisk is only valid as long as you have a reference
1669 * count on 'md'.
1671 struct gendisk *dm_disk(struct mapped_device *md)
1673 return md->disk;
1676 int dm_suspended(struct mapped_device *md)
1678 return test_bit(DMF_SUSPENDED, &md->flags);
1681 int dm_noflush_suspending(struct dm_target *ti)
1683 struct mapped_device *md = dm_table_get_md(ti->table);
1684 int r = __noflush_suspending(md);
1686 dm_put(md);
1688 return r;
1690 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1692 static struct block_device_operations dm_blk_dops = {
1693 .open = dm_blk_open,
1694 .release = dm_blk_close,
1695 .ioctl = dm_blk_ioctl,
1696 .getgeo = dm_blk_getgeo,
1697 .owner = THIS_MODULE
1700 EXPORT_SYMBOL(dm_get_mapinfo);
1703 * module hooks
1705 module_init(dm_init);
1706 module_exit(dm_exit);
1708 module_param(major, uint, 0);
1709 MODULE_PARM_DESC(major, "The major number of the device mapper");
1710 MODULE_DESCRIPTION(DM_NAME " driver");
1711 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1712 MODULE_LICENSE("GPL");