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.
9 #include "dm-bio-list.h"
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/moduleparam.h>
15 #include <linux/blkpg.h>
16 #include <linux/bio.h>
17 #include <linux/buffer_head.h>
18 #include <linux/mempool.h>
19 #include <linux/slab.h>
20 #include <linux/idr.h>
21 #include <linux/hdreg.h>
22 #include <linux/blktrace_api.h>
23 #include <linux/smp_lock.h>
25 #define DM_MSG_PREFIX "core"
27 static const char *_name
= DM_NAME
;
29 static unsigned int major
= 0;
30 static unsigned int _major
= 0;
32 static DEFINE_SPINLOCK(_minor_lock
);
34 * One of these is allocated per bio.
37 struct mapped_device
*md
;
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.
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
;
61 #define MINOR_ALLOCED ((void *)-1)
64 * Bits for the md->flags field.
66 #define DMF_BLOCK_IO 0
67 #define DMF_SUSPENDED 1
70 #define DMF_DELETING 4
71 #define DMF_NOFLUSH_SUSPENDING 5
73 struct mapped_device
{
74 struct rw_semaphore io_lock
;
75 struct semaphore suspend_lock
;
76 spinlock_t pushback_lock
;
83 request_queue_t
*queue
;
90 * A list of ios that arrived while we were suspended.
93 wait_queue_head_t wait
;
94 struct bio_list deferred
;
95 struct bio_list pushback
;
98 * The current mapping.
100 struct dm_table
*map
;
103 * io objects are allocated from here.
114 wait_queue_head_t eventq
;
117 * freeze/thaw support require holding onto a super block
119 struct super_block
*frozen_sb
;
120 struct block_device
*suspended_bdev
;
122 /* forced geometry settings */
123 struct hd_geometry geometry
;
127 static struct kmem_cache
*_io_cache
;
128 static struct kmem_cache
*_tio_cache
;
130 static int __init
local_init(void)
134 /* allocate a slab for the dm_ios */
135 _io_cache
= KMEM_CACHE(dm_io
, 0);
139 /* allocate a slab for the target ios */
140 _tio_cache
= KMEM_CACHE(dm_target_io
, 0);
142 kmem_cache_destroy(_io_cache
);
147 r
= register_blkdev(_major
, _name
);
149 kmem_cache_destroy(_tio_cache
);
150 kmem_cache_destroy(_io_cache
);
160 static void local_exit(void)
162 kmem_cache_destroy(_tio_cache
);
163 kmem_cache_destroy(_io_cache
);
164 unregister_blkdev(_major
, _name
);
168 DMINFO("cleaned up");
171 int (*_inits
[])(void) __initdata
= {
179 void (*_exits
[])(void) = {
187 static int __init
dm_init(void)
189 const int count
= ARRAY_SIZE(_inits
);
193 for (i
= 0; i
< count
; i
++) {
208 static void __exit
dm_exit(void)
210 int i
= ARRAY_SIZE(_exits
);
217 * Block device functions
219 static int dm_blk_open(struct inode
*inode
, struct file
*file
)
221 struct mapped_device
*md
;
223 spin_lock(&_minor_lock
);
225 md
= inode
->i_bdev
->bd_disk
->private_data
;
229 if (test_bit(DMF_FREEING
, &md
->flags
) ||
230 test_bit(DMF_DELETING
, &md
->flags
)) {
236 atomic_inc(&md
->open_count
);
239 spin_unlock(&_minor_lock
);
241 return md
? 0 : -ENXIO
;
244 static int dm_blk_close(struct inode
*inode
, struct file
*file
)
246 struct mapped_device
*md
;
248 md
= inode
->i_bdev
->bd_disk
->private_data
;
249 atomic_dec(&md
->open_count
);
254 int dm_open_count(struct mapped_device
*md
)
256 return atomic_read(&md
->open_count
);
260 * Guarantees nothing is using the device before it's deleted.
262 int dm_lock_for_deletion(struct mapped_device
*md
)
266 spin_lock(&_minor_lock
);
268 if (dm_open_count(md
))
271 set_bit(DMF_DELETING
, &md
->flags
);
273 spin_unlock(&_minor_lock
);
278 static int dm_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
280 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
282 return dm_get_geometry(md
, geo
);
285 static int dm_blk_ioctl(struct inode
*inode
, struct file
*file
,
286 unsigned int cmd
, unsigned long arg
)
288 struct mapped_device
*md
;
289 struct dm_table
*map
;
290 struct dm_target
*tgt
;
293 /* We don't really need this lock, but we do need 'inode'. */
296 md
= inode
->i_bdev
->bd_disk
->private_data
;
298 map
= dm_get_table(md
);
300 if (!map
|| !dm_table_get_size(map
))
303 /* We only support devices that have a single target */
304 if (dm_table_get_num_targets(map
) != 1)
307 tgt
= dm_table_get_target(map
, 0);
309 if (dm_suspended(md
)) {
314 if (tgt
->type
->ioctl
)
315 r
= tgt
->type
->ioctl(tgt
, inode
, file
, cmd
, arg
);
324 static struct dm_io
*alloc_io(struct mapped_device
*md
)
326 return mempool_alloc(md
->io_pool
, GFP_NOIO
);
329 static void free_io(struct mapped_device
*md
, struct dm_io
*io
)
331 mempool_free(io
, md
->io_pool
);
334 static struct dm_target_io
*alloc_tio(struct mapped_device
*md
)
336 return mempool_alloc(md
->tio_pool
, GFP_NOIO
);
339 static void free_tio(struct mapped_device
*md
, struct dm_target_io
*tio
)
341 mempool_free(tio
, md
->tio_pool
);
344 static void start_io_acct(struct dm_io
*io
)
346 struct mapped_device
*md
= io
->md
;
348 io
->start_time
= jiffies
;
351 disk_round_stats(dm_disk(md
));
353 dm_disk(md
)->in_flight
= atomic_inc_return(&md
->pending
);
356 static int end_io_acct(struct dm_io
*io
)
358 struct mapped_device
*md
= io
->md
;
359 struct bio
*bio
= io
->bio
;
360 unsigned long duration
= jiffies
- io
->start_time
;
362 int rw
= bio_data_dir(bio
);
365 disk_round_stats(dm_disk(md
));
367 dm_disk(md
)->in_flight
= pending
= atomic_dec_return(&md
->pending
);
369 disk_stat_add(dm_disk(md
), ticks
[rw
], duration
);
375 * Add the bio to the list of deferred io.
377 static int queue_io(struct mapped_device
*md
, struct bio
*bio
)
379 down_write(&md
->io_lock
);
381 if (!test_bit(DMF_BLOCK_IO
, &md
->flags
)) {
382 up_write(&md
->io_lock
);
386 bio_list_add(&md
->deferred
, bio
);
388 up_write(&md
->io_lock
);
389 return 0; /* deferred successfully */
393 * Everyone (including functions in this file), should use this
394 * function to access the md->map field, and make sure they call
395 * dm_table_put() when finished.
397 struct dm_table
*dm_get_table(struct mapped_device
*md
)
401 read_lock(&md
->map_lock
);
405 read_unlock(&md
->map_lock
);
411 * Get the geometry associated with a dm device
413 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
421 * Set the geometry of a device.
423 int dm_set_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
425 sector_t sz
= (sector_t
)geo
->cylinders
* geo
->heads
* geo
->sectors
;
427 if (geo
->start
> sz
) {
428 DMWARN("Start sector is beyond the geometry limits.");
437 /*-----------------------------------------------------------------
439 * A more elegant soln is in the works that uses the queue
440 * merge fn, unfortunately there are a couple of changes to
441 * the block layer that I want to make for this. So in the
442 * interests of getting something for people to use I give
443 * you this clearly demarcated crap.
444 *---------------------------------------------------------------*/
446 static int __noflush_suspending(struct mapped_device
*md
)
448 return test_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
452 * Decrements the number of outstanding ios that a bio has been
453 * cloned into, completing the original io if necc.
455 static void dec_pending(struct dm_io
*io
, int error
)
459 /* Push-back supersedes any I/O errors */
460 if (error
&& !(io
->error
> 0 && __noflush_suspending(io
->md
)))
463 if (atomic_dec_and_test(&io
->io_count
)) {
464 if (io
->error
== DM_ENDIO_REQUEUE
) {
466 * Target requested pushing back the I/O.
467 * This must be handled before the sleeper on
468 * suspend queue merges the pushback list.
470 spin_lock_irqsave(&io
->md
->pushback_lock
, flags
);
471 if (__noflush_suspending(io
->md
))
472 bio_list_add(&io
->md
->pushback
, io
->bio
);
474 /* noflush suspend was interrupted. */
476 spin_unlock_irqrestore(&io
->md
->pushback_lock
, flags
);
480 /* nudge anyone waiting on suspend queue */
481 wake_up(&io
->md
->wait
);
483 if (io
->error
!= DM_ENDIO_REQUEUE
) {
484 blk_add_trace_bio(io
->md
->queue
, io
->bio
,
487 bio_endio(io
->bio
, io
->bio
->bi_size
, io
->error
);
494 static int clone_endio(struct bio
*bio
, unsigned int done
, int error
)
497 struct dm_target_io
*tio
= bio
->bi_private
;
498 struct mapped_device
*md
= tio
->io
->md
;
499 dm_endio_fn endio
= tio
->ti
->type
->end_io
;
504 if (!bio_flagged(bio
, BIO_UPTODATE
) && !error
)
508 r
= endio(tio
->ti
, bio
, error
, &tio
->info
);
509 if (r
< 0 || r
== DM_ENDIO_REQUEUE
)
511 * error and requeue request are handled
515 else if (r
== DM_ENDIO_INCOMPLETE
)
516 /* The target will handle the io */
519 DMWARN("unimplemented target endio return value: %d", r
);
524 dec_pending(tio
->io
, error
);
527 * Store md for cleanup instead of tio which is about to get freed.
529 bio
->bi_private
= md
->bs
;
536 static sector_t
max_io_len(struct mapped_device
*md
,
537 sector_t sector
, struct dm_target
*ti
)
539 sector_t offset
= sector
- ti
->begin
;
540 sector_t len
= ti
->len
- offset
;
543 * Does the target need to split even further ?
547 boundary
= ((offset
+ ti
->split_io
) & ~(ti
->split_io
- 1))
556 static void __map_bio(struct dm_target
*ti
, struct bio
*clone
,
557 struct dm_target_io
*tio
)
561 struct mapped_device
*md
;
566 BUG_ON(!clone
->bi_size
);
568 clone
->bi_end_io
= clone_endio
;
569 clone
->bi_private
= tio
;
572 * Map the clone. If r == 0 we don't need to do
573 * anything, the target has assumed ownership of
576 atomic_inc(&tio
->io
->io_count
);
577 sector
= clone
->bi_sector
;
578 r
= ti
->type
->map(ti
, clone
, &tio
->info
);
579 if (r
== DM_MAPIO_REMAPPED
) {
580 /* the bio has been remapped so dispatch it */
582 blk_add_trace_remap(bdev_get_queue(clone
->bi_bdev
), clone
,
583 tio
->io
->bio
->bi_bdev
->bd_dev
, sector
,
586 generic_make_request(clone
);
587 } else if (r
< 0 || r
== DM_MAPIO_REQUEUE
) {
588 /* error the io and bail out, or requeue it if needed */
590 dec_pending(tio
->io
, r
);
592 * Store bio_set for cleanup.
594 clone
->bi_private
= md
->bs
;
598 DMWARN("unimplemented target map return value: %d", r
);
604 struct mapped_device
*md
;
605 struct dm_table
*map
;
609 sector_t sector_count
;
613 static void dm_bio_destructor(struct bio
*bio
)
615 struct bio_set
*bs
= bio
->bi_private
;
621 * Creates a little bio that is just does part of a bvec.
623 static struct bio
*split_bvec(struct bio
*bio
, sector_t sector
,
624 unsigned short idx
, unsigned int offset
,
625 unsigned int len
, struct bio_set
*bs
)
628 struct bio_vec
*bv
= bio
->bi_io_vec
+ idx
;
630 clone
= bio_alloc_bioset(GFP_NOIO
, 1, bs
);
631 clone
->bi_destructor
= dm_bio_destructor
;
632 *clone
->bi_io_vec
= *bv
;
634 clone
->bi_sector
= sector
;
635 clone
->bi_bdev
= bio
->bi_bdev
;
636 clone
->bi_rw
= bio
->bi_rw
;
638 clone
->bi_size
= to_bytes(len
);
639 clone
->bi_io_vec
->bv_offset
= offset
;
640 clone
->bi_io_vec
->bv_len
= clone
->bi_size
;
646 * Creates a bio that consists of range of complete bvecs.
648 static struct bio
*clone_bio(struct bio
*bio
, sector_t sector
,
649 unsigned short idx
, unsigned short bv_count
,
650 unsigned int len
, struct bio_set
*bs
)
654 clone
= bio_alloc_bioset(GFP_NOIO
, bio
->bi_max_vecs
, bs
);
655 __bio_clone(clone
, bio
);
656 clone
->bi_destructor
= dm_bio_destructor
;
657 clone
->bi_sector
= sector
;
659 clone
->bi_vcnt
= idx
+ bv_count
;
660 clone
->bi_size
= to_bytes(len
);
661 clone
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
666 static void __clone_and_map(struct clone_info
*ci
)
668 struct bio
*clone
, *bio
= ci
->bio
;
669 struct dm_target
*ti
= dm_table_find_target(ci
->map
, ci
->sector
);
670 sector_t len
= 0, max
= max_io_len(ci
->md
, ci
->sector
, ti
);
671 struct dm_target_io
*tio
;
674 * Allocate a target io object.
676 tio
= alloc_tio(ci
->md
);
679 memset(&tio
->info
, 0, sizeof(tio
->info
));
681 if (ci
->sector_count
<= max
) {
683 * Optimise for the simple case where we can do all of
684 * the remaining io with a single clone.
686 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
,
687 bio
->bi_vcnt
- ci
->idx
, ci
->sector_count
,
689 __map_bio(ti
, clone
, tio
);
690 ci
->sector_count
= 0;
692 } else if (to_sector(bio
->bi_io_vec
[ci
->idx
].bv_len
) <= max
) {
694 * There are some bvecs that don't span targets.
695 * Do as many of these as possible.
698 sector_t remaining
= max
;
701 for (i
= ci
->idx
; remaining
&& (i
< bio
->bi_vcnt
); i
++) {
702 bv_len
= to_sector(bio
->bi_io_vec
[i
].bv_len
);
704 if (bv_len
> remaining
)
711 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
, i
- ci
->idx
, len
,
713 __map_bio(ti
, clone
, tio
);
716 ci
->sector_count
-= len
;
721 * Handle a bvec that must be split between two or more targets.
723 struct bio_vec
*bv
= bio
->bi_io_vec
+ ci
->idx
;
724 sector_t remaining
= to_sector(bv
->bv_len
);
725 unsigned int offset
= 0;
729 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
730 max
= max_io_len(ci
->md
, ci
->sector
, ti
);
732 tio
= alloc_tio(ci
->md
);
735 memset(&tio
->info
, 0, sizeof(tio
->info
));
738 len
= min(remaining
, max
);
740 clone
= split_bvec(bio
, ci
->sector
, ci
->idx
,
741 bv
->bv_offset
+ offset
, len
,
744 __map_bio(ti
, clone
, tio
);
747 ci
->sector_count
-= len
;
748 offset
+= to_bytes(len
);
749 } while (remaining
-= len
);
756 * Split the bio into several clones.
758 static void __split_bio(struct mapped_device
*md
, struct bio
*bio
)
760 struct clone_info ci
;
762 ci
.map
= dm_get_table(md
);
764 bio_io_error(bio
, bio
->bi_size
);
770 ci
.io
= alloc_io(md
);
772 atomic_set(&ci
.io
->io_count
, 1);
775 ci
.sector
= bio
->bi_sector
;
776 ci
.sector_count
= bio_sectors(bio
);
777 ci
.idx
= bio
->bi_idx
;
779 start_io_acct(ci
.io
);
780 while (ci
.sector_count
)
781 __clone_and_map(&ci
);
783 /* drop the extra reference count */
784 dec_pending(ci
.io
, 0);
785 dm_table_put(ci
.map
);
787 /*-----------------------------------------------------------------
789 *---------------------------------------------------------------*/
792 * The request function that just remaps the bio built up by
795 static int dm_request(request_queue_t
*q
, struct bio
*bio
)
798 int rw
= bio_data_dir(bio
);
799 struct mapped_device
*md
= q
->queuedata
;
802 * There is no use in forwarding any barrier request since we can't
803 * guarantee it is (or can be) handled by the targets correctly.
805 if (unlikely(bio_barrier(bio
))) {
806 bio_endio(bio
, bio
->bi_size
, -EOPNOTSUPP
);
810 down_read(&md
->io_lock
);
812 disk_stat_inc(dm_disk(md
), ios
[rw
]);
813 disk_stat_add(dm_disk(md
), sectors
[rw
], bio_sectors(bio
));
816 * If we're suspended we have to queue
819 while (test_bit(DMF_BLOCK_IO
, &md
->flags
)) {
820 up_read(&md
->io_lock
);
822 if (bio_rw(bio
) == READA
) {
823 bio_io_error(bio
, bio
->bi_size
);
827 r
= queue_io(md
, bio
);
829 bio_io_error(bio
, bio
->bi_size
);
833 return 0; /* deferred successfully */
836 * We're in a while loop, because someone could suspend
837 * before we get to the following read lock.
839 down_read(&md
->io_lock
);
842 __split_bio(md
, bio
);
843 up_read(&md
->io_lock
);
847 static int dm_flush_all(request_queue_t
*q
, struct gendisk
*disk
,
848 sector_t
*error_sector
)
850 struct mapped_device
*md
= q
->queuedata
;
851 struct dm_table
*map
= dm_get_table(md
);
855 ret
= dm_table_flush_all(map
);
862 static void dm_unplug_all(request_queue_t
*q
)
864 struct mapped_device
*md
= q
->queuedata
;
865 struct dm_table
*map
= dm_get_table(md
);
868 dm_table_unplug_all(map
);
873 static int dm_any_congested(void *congested_data
, int bdi_bits
)
876 struct mapped_device
*md
= (struct mapped_device
*) congested_data
;
877 struct dm_table
*map
= dm_get_table(md
);
879 if (!map
|| test_bit(DMF_BLOCK_IO
, &md
->flags
))
882 r
= dm_table_any_congested(map
, bdi_bits
);
888 /*-----------------------------------------------------------------
889 * An IDR is used to keep track of allocated minor numbers.
890 *---------------------------------------------------------------*/
891 static DEFINE_IDR(_minor_idr
);
893 static void free_minor(int minor
)
895 spin_lock(&_minor_lock
);
896 idr_remove(&_minor_idr
, minor
);
897 spin_unlock(&_minor_lock
);
901 * See if the device with a specific minor # is free.
903 static int specific_minor(struct mapped_device
*md
, int minor
)
907 if (minor
>= (1 << MINORBITS
))
910 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
914 spin_lock(&_minor_lock
);
916 if (idr_find(&_minor_idr
, minor
)) {
921 r
= idr_get_new_above(&_minor_idr
, MINOR_ALLOCED
, minor
, &m
);
926 idr_remove(&_minor_idr
, m
);
932 spin_unlock(&_minor_lock
);
936 static int next_free_minor(struct mapped_device
*md
, int *minor
)
940 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
944 spin_lock(&_minor_lock
);
946 r
= idr_get_new(&_minor_idr
, MINOR_ALLOCED
, &m
);
951 if (m
>= (1 << MINORBITS
)) {
952 idr_remove(&_minor_idr
, m
);
960 spin_unlock(&_minor_lock
);
964 static struct block_device_operations dm_blk_dops
;
967 * Allocate and initialise a blank device with a given minor.
969 static struct mapped_device
*alloc_dev(int minor
)
972 struct mapped_device
*md
= kmalloc(sizeof(*md
), GFP_KERNEL
);
976 DMWARN("unable to allocate device, out of memory.");
980 if (!try_module_get(THIS_MODULE
))
983 /* get a minor number for the dev */
984 if (minor
== DM_ANY_MINOR
)
985 r
= next_free_minor(md
, &minor
);
987 r
= specific_minor(md
, minor
);
991 memset(md
, 0, sizeof(*md
));
992 init_rwsem(&md
->io_lock
);
993 init_MUTEX(&md
->suspend_lock
);
994 spin_lock_init(&md
->pushback_lock
);
995 rwlock_init(&md
->map_lock
);
996 atomic_set(&md
->holders
, 1);
997 atomic_set(&md
->open_count
, 0);
998 atomic_set(&md
->event_nr
, 0);
1000 md
->queue
= blk_alloc_queue(GFP_KERNEL
);
1002 goto bad1_free_minor
;
1004 md
->queue
->queuedata
= md
;
1005 md
->queue
->backing_dev_info
.congested_fn
= dm_any_congested
;
1006 md
->queue
->backing_dev_info
.congested_data
= md
;
1007 blk_queue_make_request(md
->queue
, dm_request
);
1008 blk_queue_bounce_limit(md
->queue
, BLK_BOUNCE_ANY
);
1009 md
->queue
->unplug_fn
= dm_unplug_all
;
1010 md
->queue
->issue_flush_fn
= dm_flush_all
;
1012 md
->io_pool
= mempool_create_slab_pool(MIN_IOS
, _io_cache
);
1016 md
->tio_pool
= mempool_create_slab_pool(MIN_IOS
, _tio_cache
);
1020 md
->bs
= bioset_create(16, 16);
1024 md
->disk
= alloc_disk(1);
1028 atomic_set(&md
->pending
, 0);
1029 init_waitqueue_head(&md
->wait
);
1030 init_waitqueue_head(&md
->eventq
);
1032 md
->disk
->major
= _major
;
1033 md
->disk
->first_minor
= minor
;
1034 md
->disk
->fops
= &dm_blk_dops
;
1035 md
->disk
->queue
= md
->queue
;
1036 md
->disk
->private_data
= md
;
1037 sprintf(md
->disk
->disk_name
, "dm-%d", minor
);
1039 format_dev_t(md
->name
, MKDEV(_major
, minor
));
1041 /* Populate the mapping, nobody knows we exist yet */
1042 spin_lock(&_minor_lock
);
1043 old_md
= idr_replace(&_minor_idr
, md
, minor
);
1044 spin_unlock(&_minor_lock
);
1046 BUG_ON(old_md
!= MINOR_ALLOCED
);
1051 bioset_free(md
->bs
);
1053 mempool_destroy(md
->tio_pool
);
1055 mempool_destroy(md
->io_pool
);
1057 blk_cleanup_queue(md
->queue
);
1061 module_put(THIS_MODULE
);
1067 static void free_dev(struct mapped_device
*md
)
1069 int minor
= md
->disk
->first_minor
;
1071 if (md
->suspended_bdev
) {
1072 thaw_bdev(md
->suspended_bdev
, NULL
);
1073 bdput(md
->suspended_bdev
);
1075 mempool_destroy(md
->tio_pool
);
1076 mempool_destroy(md
->io_pool
);
1077 bioset_free(md
->bs
);
1078 del_gendisk(md
->disk
);
1081 spin_lock(&_minor_lock
);
1082 md
->disk
->private_data
= NULL
;
1083 spin_unlock(&_minor_lock
);
1086 blk_cleanup_queue(md
->queue
);
1087 module_put(THIS_MODULE
);
1092 * Bind a table to the device.
1094 static void event_callback(void *context
)
1096 struct mapped_device
*md
= (struct mapped_device
*) context
;
1098 atomic_inc(&md
->event_nr
);
1099 wake_up(&md
->eventq
);
1102 static void __set_size(struct mapped_device
*md
, sector_t size
)
1104 set_capacity(md
->disk
, size
);
1106 mutex_lock(&md
->suspended_bdev
->bd_inode
->i_mutex
);
1107 i_size_write(md
->suspended_bdev
->bd_inode
, (loff_t
)size
<< SECTOR_SHIFT
);
1108 mutex_unlock(&md
->suspended_bdev
->bd_inode
->i_mutex
);
1111 static int __bind(struct mapped_device
*md
, struct dm_table
*t
)
1113 request_queue_t
*q
= md
->queue
;
1116 size
= dm_table_get_size(t
);
1119 * Wipe any geometry if the size of the table changed.
1121 if (size
!= get_capacity(md
->disk
))
1122 memset(&md
->geometry
, 0, sizeof(md
->geometry
));
1124 if (md
->suspended_bdev
)
1125 __set_size(md
, size
);
1130 dm_table_event_callback(t
, event_callback
, md
);
1132 write_lock(&md
->map_lock
);
1134 dm_table_set_restrictions(t
, q
);
1135 write_unlock(&md
->map_lock
);
1140 static void __unbind(struct mapped_device
*md
)
1142 struct dm_table
*map
= md
->map
;
1147 dm_table_event_callback(map
, NULL
, NULL
);
1148 write_lock(&md
->map_lock
);
1150 write_unlock(&md
->map_lock
);
1155 * Constructor for a new device.
1157 int dm_create(int minor
, struct mapped_device
**result
)
1159 struct mapped_device
*md
;
1161 md
= alloc_dev(minor
);
1169 static struct mapped_device
*dm_find_md(dev_t dev
)
1171 struct mapped_device
*md
;
1172 unsigned minor
= MINOR(dev
);
1174 if (MAJOR(dev
) != _major
|| minor
>= (1 << MINORBITS
))
1177 spin_lock(&_minor_lock
);
1179 md
= idr_find(&_minor_idr
, minor
);
1180 if (md
&& (md
== MINOR_ALLOCED
||
1181 (dm_disk(md
)->first_minor
!= minor
) ||
1182 test_bit(DMF_FREEING
, &md
->flags
))) {
1188 spin_unlock(&_minor_lock
);
1193 struct mapped_device
*dm_get_md(dev_t dev
)
1195 struct mapped_device
*md
= dm_find_md(dev
);
1203 void *dm_get_mdptr(struct mapped_device
*md
)
1205 return md
->interface_ptr
;
1208 void dm_set_mdptr(struct mapped_device
*md
, void *ptr
)
1210 md
->interface_ptr
= ptr
;
1213 void dm_get(struct mapped_device
*md
)
1215 atomic_inc(&md
->holders
);
1218 const char *dm_device_name(struct mapped_device
*md
)
1222 EXPORT_SYMBOL_GPL(dm_device_name
);
1224 void dm_put(struct mapped_device
*md
)
1226 struct dm_table
*map
;
1228 BUG_ON(test_bit(DMF_FREEING
, &md
->flags
));
1230 if (atomic_dec_and_lock(&md
->holders
, &_minor_lock
)) {
1231 map
= dm_get_table(md
);
1232 idr_replace(&_minor_idr
, MINOR_ALLOCED
, dm_disk(md
)->first_minor
);
1233 set_bit(DMF_FREEING
, &md
->flags
);
1234 spin_unlock(&_minor_lock
);
1235 if (!dm_suspended(md
)) {
1236 dm_table_presuspend_targets(map
);
1237 dm_table_postsuspend_targets(map
);
1244 EXPORT_SYMBOL_GPL(dm_put
);
1247 * Process the deferred bios
1249 static void __flush_deferred_io(struct mapped_device
*md
, struct bio
*c
)
1262 * Swap in a new table (destroying old one).
1264 int dm_swap_table(struct mapped_device
*md
, struct dm_table
*table
)
1268 down(&md
->suspend_lock
);
1270 /* device must be suspended */
1271 if (!dm_suspended(md
))
1274 /* without bdev, the device size cannot be changed */
1275 if (!md
->suspended_bdev
)
1276 if (get_capacity(md
->disk
) != dm_table_get_size(table
))
1280 r
= __bind(md
, table
);
1283 up(&md
->suspend_lock
);
1288 * Functions to lock and unlock any filesystem running on the
1291 static int lock_fs(struct mapped_device
*md
)
1295 WARN_ON(md
->frozen_sb
);
1297 md
->frozen_sb
= freeze_bdev(md
->suspended_bdev
);
1298 if (IS_ERR(md
->frozen_sb
)) {
1299 r
= PTR_ERR(md
->frozen_sb
);
1300 md
->frozen_sb
= NULL
;
1304 set_bit(DMF_FROZEN
, &md
->flags
);
1306 /* don't bdput right now, we don't want the bdev
1307 * to go away while it is locked.
1312 static void unlock_fs(struct mapped_device
*md
)
1314 if (!test_bit(DMF_FROZEN
, &md
->flags
))
1317 thaw_bdev(md
->suspended_bdev
, md
->frozen_sb
);
1318 md
->frozen_sb
= NULL
;
1319 clear_bit(DMF_FROZEN
, &md
->flags
);
1323 * We need to be able to change a mapping table under a mounted
1324 * filesystem. For example we might want to move some data in
1325 * the background. Before the table can be swapped with
1326 * dm_bind_table, dm_suspend must be called to flush any in
1327 * flight bios and ensure that any further io gets deferred.
1329 int dm_suspend(struct mapped_device
*md
, unsigned suspend_flags
)
1331 struct dm_table
*map
= NULL
;
1332 unsigned long flags
;
1333 DECLARE_WAITQUEUE(wait
, current
);
1336 int do_lockfs
= suspend_flags
& DM_SUSPEND_LOCKFS_FLAG
? 1 : 0;
1337 int noflush
= suspend_flags
& DM_SUSPEND_NOFLUSH_FLAG
? 1 : 0;
1339 down(&md
->suspend_lock
);
1341 if (dm_suspended(md
))
1344 map
= dm_get_table(md
);
1347 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1348 * This flag is cleared before dm_suspend returns.
1351 set_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
1353 /* This does not get reverted if there's an error later. */
1354 dm_table_presuspend_targets(map
);
1356 /* bdget() can stall if the pending I/Os are not flushed */
1358 md
->suspended_bdev
= bdget_disk(md
->disk
, 0);
1359 if (!md
->suspended_bdev
) {
1360 DMWARN("bdget failed in dm_suspend");
1367 * Flush I/O to the device.
1368 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1370 if (do_lockfs
&& !noflush
) {
1377 * First we set the BLOCK_IO flag so no more ios will be mapped.
1379 down_write(&md
->io_lock
);
1380 set_bit(DMF_BLOCK_IO
, &md
->flags
);
1382 add_wait_queue(&md
->wait
, &wait
);
1383 up_write(&md
->io_lock
);
1387 dm_table_unplug_all(map
);
1390 * Then we wait for the already mapped ios to
1394 set_current_state(TASK_INTERRUPTIBLE
);
1396 if (!atomic_read(&md
->pending
) || signal_pending(current
))
1401 set_current_state(TASK_RUNNING
);
1403 down_write(&md
->io_lock
);
1404 remove_wait_queue(&md
->wait
, &wait
);
1407 spin_lock_irqsave(&md
->pushback_lock
, flags
);
1408 clear_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
1409 bio_list_merge_head(&md
->deferred
, &md
->pushback
);
1410 bio_list_init(&md
->pushback
);
1411 spin_unlock_irqrestore(&md
->pushback_lock
, flags
);
1414 /* were we interrupted ? */
1416 if (atomic_read(&md
->pending
)) {
1417 clear_bit(DMF_BLOCK_IO
, &md
->flags
);
1418 def
= bio_list_get(&md
->deferred
);
1419 __flush_deferred_io(md
, def
);
1420 up_write(&md
->io_lock
);
1422 goto out
; /* pushback list is already flushed, so skip flush */
1424 up_write(&md
->io_lock
);
1426 dm_table_postsuspend_targets(map
);
1428 set_bit(DMF_SUSPENDED
, &md
->flags
);
1435 * Because there may be already I/Os in the pushback list,
1436 * flush them before return.
1438 down_write(&md
->io_lock
);
1440 spin_lock_irqsave(&md
->pushback_lock
, flags
);
1441 clear_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
1442 bio_list_merge_head(&md
->deferred
, &md
->pushback
);
1443 bio_list_init(&md
->pushback
);
1444 spin_unlock_irqrestore(&md
->pushback_lock
, flags
);
1446 def
= bio_list_get(&md
->deferred
);
1447 __flush_deferred_io(md
, def
);
1448 up_write(&md
->io_lock
);
1452 if (r
&& md
->suspended_bdev
) {
1453 bdput(md
->suspended_bdev
);
1454 md
->suspended_bdev
= NULL
;
1460 up(&md
->suspend_lock
);
1464 int dm_resume(struct mapped_device
*md
)
1468 struct dm_table
*map
= NULL
;
1470 down(&md
->suspend_lock
);
1471 if (!dm_suspended(md
))
1474 map
= dm_get_table(md
);
1475 if (!map
|| !dm_table_get_size(map
))
1478 r
= dm_table_resume_targets(map
);
1482 down_write(&md
->io_lock
);
1483 clear_bit(DMF_BLOCK_IO
, &md
->flags
);
1485 def
= bio_list_get(&md
->deferred
);
1486 __flush_deferred_io(md
, def
);
1487 up_write(&md
->io_lock
);
1491 if (md
->suspended_bdev
) {
1492 bdput(md
->suspended_bdev
);
1493 md
->suspended_bdev
= NULL
;
1496 clear_bit(DMF_SUSPENDED
, &md
->flags
);
1498 dm_table_unplug_all(map
);
1500 kobject_uevent(&md
->disk
->kobj
, KOBJ_CHANGE
);
1506 up(&md
->suspend_lock
);
1511 /*-----------------------------------------------------------------
1512 * Event notification.
1513 *---------------------------------------------------------------*/
1514 uint32_t dm_get_event_nr(struct mapped_device
*md
)
1516 return atomic_read(&md
->event_nr
);
1519 int dm_wait_event(struct mapped_device
*md
, int event_nr
)
1521 return wait_event_interruptible(md
->eventq
,
1522 (event_nr
!= atomic_read(&md
->event_nr
)));
1526 * The gendisk is only valid as long as you have a reference
1529 struct gendisk
*dm_disk(struct mapped_device
*md
)
1534 int dm_suspended(struct mapped_device
*md
)
1536 return test_bit(DMF_SUSPENDED
, &md
->flags
);
1539 int dm_noflush_suspending(struct dm_target
*ti
)
1541 struct mapped_device
*md
= dm_table_get_md(ti
->table
);
1542 int r
= __noflush_suspending(md
);
1548 EXPORT_SYMBOL_GPL(dm_noflush_suspending
);
1550 static struct block_device_operations dm_blk_dops
= {
1551 .open
= dm_blk_open
,
1552 .release
= dm_blk_close
,
1553 .ioctl
= dm_blk_ioctl
,
1554 .getgeo
= dm_blk_getgeo
,
1555 .owner
= THIS_MODULE
1558 EXPORT_SYMBOL(dm_get_mapinfo
);
1563 module_init(dm_init
);
1564 module_exit(dm_exit
);
1566 module_param(major
, uint
, 0);
1567 MODULE_PARM_DESC(major
, "The major number of the device mapper");
1568 MODULE_DESCRIPTION(DM_NAME
" driver");
1569 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1570 MODULE_LICENSE("GPL");