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 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
72 struct mapped_device
{
73 struct rw_semaphore io_lock
;
74 struct semaphore suspend_lock
;
81 request_queue_t
*queue
;
88 * A list of ios that arrived while we were suspended.
91 wait_queue_head_t wait
;
92 struct bio_list deferred
;
95 * The current mapping.
100 * io objects are allocated from here.
109 wait_queue_head_t eventq
;
112 * freeze/thaw support require holding onto a super block
114 struct super_block
*frozen_sb
;
115 struct block_device
*suspended_bdev
;
117 /* forced geometry settings */
118 struct hd_geometry geometry
;
122 static kmem_cache_t
*_io_cache
;
123 static kmem_cache_t
*_tio_cache
;
125 static struct bio_set
*dm_set
;
127 static int __init
local_init(void)
131 dm_set
= bioset_create(16, 16, 4);
135 /* allocate a slab for the dm_ios */
136 _io_cache
= kmem_cache_create("dm_io",
137 sizeof(struct dm_io
), 0, 0, NULL
, NULL
);
141 /* allocate a slab for the target ios */
142 _tio_cache
= kmem_cache_create("dm_tio", sizeof(struct target_io
),
145 kmem_cache_destroy(_io_cache
);
150 r
= register_blkdev(_major
, _name
);
152 kmem_cache_destroy(_tio_cache
);
153 kmem_cache_destroy(_io_cache
);
163 static void local_exit(void)
165 kmem_cache_destroy(_tio_cache
);
166 kmem_cache_destroy(_io_cache
);
170 if (unregister_blkdev(_major
, _name
) < 0)
171 DMERR("unregister_blkdev failed");
175 DMINFO("cleaned up");
178 int (*_inits
[])(void) __initdata
= {
186 void (*_exits
[])(void) = {
194 static int __init
dm_init(void)
196 const int count
= ARRAY_SIZE(_inits
);
200 for (i
= 0; i
< count
; i
++) {
215 static void __exit
dm_exit(void)
217 int i
= ARRAY_SIZE(_exits
);
224 * Block device functions
226 static int dm_blk_open(struct inode
*inode
, struct file
*file
)
228 struct mapped_device
*md
;
230 spin_lock(&_minor_lock
);
232 md
= inode
->i_bdev
->bd_disk
->private_data
;
236 if (test_bit(DMF_FREEING
, &md
->flags
) ||
237 test_bit(DMF_DELETING
, &md
->flags
)) {
243 atomic_inc(&md
->open_count
);
246 spin_unlock(&_minor_lock
);
248 return md
? 0 : -ENXIO
;
251 static int dm_blk_close(struct inode
*inode
, struct file
*file
)
253 struct mapped_device
*md
;
255 md
= inode
->i_bdev
->bd_disk
->private_data
;
256 atomic_dec(&md
->open_count
);
261 int dm_open_count(struct mapped_device
*md
)
263 return atomic_read(&md
->open_count
);
267 * Guarantees nothing is using the device before it's deleted.
269 int dm_lock_for_deletion(struct mapped_device
*md
)
273 spin_lock(&_minor_lock
);
275 if (dm_open_count(md
))
278 set_bit(DMF_DELETING
, &md
->flags
);
280 spin_unlock(&_minor_lock
);
285 static int dm_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
287 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
289 return dm_get_geometry(md
, geo
);
292 static int dm_blk_ioctl(struct inode
*inode
, struct file
*file
,
293 unsigned int cmd
, unsigned long arg
)
295 struct mapped_device
*md
;
296 struct dm_table
*map
;
297 struct dm_target
*tgt
;
300 /* We don't really need this lock, but we do need 'inode'. */
303 md
= inode
->i_bdev
->bd_disk
->private_data
;
305 map
= dm_get_table(md
);
307 if (!map
|| !dm_table_get_size(map
))
310 /* We only support devices that have a single target */
311 if (dm_table_get_num_targets(map
) != 1)
314 tgt
= dm_table_get_target(map
, 0);
316 if (dm_suspended(md
)) {
321 if (tgt
->type
->ioctl
)
322 r
= tgt
->type
->ioctl(tgt
, inode
, file
, cmd
, arg
);
331 static inline struct dm_io
*alloc_io(struct mapped_device
*md
)
333 return mempool_alloc(md
->io_pool
, GFP_NOIO
);
336 static inline void free_io(struct mapped_device
*md
, struct dm_io
*io
)
338 mempool_free(io
, md
->io_pool
);
341 static inline struct target_io
*alloc_tio(struct mapped_device
*md
)
343 return mempool_alloc(md
->tio_pool
, GFP_NOIO
);
346 static inline void free_tio(struct mapped_device
*md
, struct target_io
*tio
)
348 mempool_free(tio
, md
->tio_pool
);
351 static void start_io_acct(struct dm_io
*io
)
353 struct mapped_device
*md
= io
->md
;
355 io
->start_time
= jiffies
;
358 disk_round_stats(dm_disk(md
));
360 dm_disk(md
)->in_flight
= atomic_inc_return(&md
->pending
);
363 static int end_io_acct(struct dm_io
*io
)
365 struct mapped_device
*md
= io
->md
;
366 struct bio
*bio
= io
->bio
;
367 unsigned long duration
= jiffies
- io
->start_time
;
369 int rw
= bio_data_dir(bio
);
372 disk_round_stats(dm_disk(md
));
374 dm_disk(md
)->in_flight
= pending
= atomic_dec_return(&md
->pending
);
376 disk_stat_add(dm_disk(md
), ticks
[rw
], duration
);
382 * Add the bio to the list of deferred io.
384 static int queue_io(struct mapped_device
*md
, struct bio
*bio
)
386 down_write(&md
->io_lock
);
388 if (!test_bit(DMF_BLOCK_IO
, &md
->flags
)) {
389 up_write(&md
->io_lock
);
393 bio_list_add(&md
->deferred
, bio
);
395 up_write(&md
->io_lock
);
396 return 0; /* deferred successfully */
400 * Everyone (including functions in this file), should use this
401 * function to access the md->map field, and make sure they call
402 * dm_table_put() when finished.
404 struct dm_table
*dm_get_table(struct mapped_device
*md
)
408 read_lock(&md
->map_lock
);
412 read_unlock(&md
->map_lock
);
418 * Get the geometry associated with a dm device
420 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
428 * Set the geometry of a device.
430 int dm_set_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
432 sector_t sz
= (sector_t
)geo
->cylinders
* geo
->heads
* geo
->sectors
;
434 if (geo
->start
> sz
) {
435 DMWARN("Start sector is beyond the geometry limits.");
444 /*-----------------------------------------------------------------
446 * A more elegant soln is in the works that uses the queue
447 * merge fn, unfortunately there are a couple of changes to
448 * the block layer that I want to make for this. So in the
449 * interests of getting something for people to use I give
450 * you this clearly demarcated crap.
451 *---------------------------------------------------------------*/
454 * Decrements the number of outstanding ios that a bio has been
455 * cloned into, completing the original io if necc.
457 static void dec_pending(struct dm_io
*io
, int error
)
462 if (atomic_dec_and_test(&io
->io_count
)) {
464 /* nudge anyone waiting on suspend queue */
465 wake_up(&io
->md
->wait
);
467 blk_add_trace_bio(io
->md
->queue
, io
->bio
, BLK_TA_COMPLETE
);
469 bio_endio(io
->bio
, io
->bio
->bi_size
, io
->error
);
474 static int clone_endio(struct bio
*bio
, unsigned int done
, int error
)
477 struct target_io
*tio
= bio
->bi_private
;
478 struct dm_io
*io
= tio
->io
;
479 dm_endio_fn endio
= tio
->ti
->type
->end_io
;
484 if (!bio_flagged(bio
, BIO_UPTODATE
) && !error
)
488 r
= endio(tio
->ti
, bio
, error
, &tio
->info
);
493 /* the target wants another shot at the io */
497 free_tio(io
->md
, tio
);
498 dec_pending(io
, error
);
503 static sector_t
max_io_len(struct mapped_device
*md
,
504 sector_t sector
, struct dm_target
*ti
)
506 sector_t offset
= sector
- ti
->begin
;
507 sector_t len
= ti
->len
- offset
;
510 * Does the target need to split even further ?
514 boundary
= ((offset
+ ti
->split_io
) & ~(ti
->split_io
- 1))
523 static void __map_bio(struct dm_target
*ti
, struct bio
*clone
,
524 struct target_io
*tio
)
532 BUG_ON(!clone
->bi_size
);
534 clone
->bi_end_io
= clone_endio
;
535 clone
->bi_private
= tio
;
538 * Map the clone. If r == 0 we don't need to do
539 * anything, the target has assumed ownership of
542 atomic_inc(&tio
->io
->io_count
);
543 sector
= clone
->bi_sector
;
544 r
= ti
->type
->map(ti
, clone
, &tio
->info
);
546 /* the bio has been remapped so dispatch it */
548 blk_add_trace_remap(bdev_get_queue(clone
->bi_bdev
), clone
,
549 tio
->io
->bio
->bi_bdev
->bd_dev
, sector
,
552 generic_make_request(clone
);
556 /* error the io and bail out */
557 struct dm_io
*io
= tio
->io
;
558 free_tio(tio
->io
->md
, tio
);
565 struct mapped_device
*md
;
566 struct dm_table
*map
;
570 sector_t sector_count
;
574 static void dm_bio_destructor(struct bio
*bio
)
576 bio_free(bio
, dm_set
);
580 * Creates a little bio that is just does part of a bvec.
582 static struct bio
*split_bvec(struct bio
*bio
, sector_t sector
,
583 unsigned short idx
, unsigned int offset
,
587 struct bio_vec
*bv
= bio
->bi_io_vec
+ idx
;
589 clone
= bio_alloc_bioset(GFP_NOIO
, 1, dm_set
);
590 clone
->bi_destructor
= dm_bio_destructor
;
591 *clone
->bi_io_vec
= *bv
;
593 clone
->bi_sector
= sector
;
594 clone
->bi_bdev
= bio
->bi_bdev
;
595 clone
->bi_rw
= bio
->bi_rw
;
597 clone
->bi_size
= to_bytes(len
);
598 clone
->bi_io_vec
->bv_offset
= offset
;
599 clone
->bi_io_vec
->bv_len
= clone
->bi_size
;
605 * Creates a bio that consists of range of complete bvecs.
607 static struct bio
*clone_bio(struct bio
*bio
, sector_t sector
,
608 unsigned short idx
, unsigned short bv_count
,
613 clone
= bio_clone(bio
, GFP_NOIO
);
614 clone
->bi_sector
= sector
;
616 clone
->bi_vcnt
= idx
+ bv_count
;
617 clone
->bi_size
= to_bytes(len
);
618 clone
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
623 static void __clone_and_map(struct clone_info
*ci
)
625 struct bio
*clone
, *bio
= ci
->bio
;
626 struct dm_target
*ti
= dm_table_find_target(ci
->map
, ci
->sector
);
627 sector_t len
= 0, max
= max_io_len(ci
->md
, ci
->sector
, ti
);
628 struct target_io
*tio
;
631 * Allocate a target io object.
633 tio
= alloc_tio(ci
->md
);
636 memset(&tio
->info
, 0, sizeof(tio
->info
));
638 if (ci
->sector_count
<= max
) {
640 * Optimise for the simple case where we can do all of
641 * the remaining io with a single clone.
643 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
,
644 bio
->bi_vcnt
- ci
->idx
, ci
->sector_count
);
645 __map_bio(ti
, clone
, tio
);
646 ci
->sector_count
= 0;
648 } else if (to_sector(bio
->bi_io_vec
[ci
->idx
].bv_len
) <= max
) {
650 * There are some bvecs that don't span targets.
651 * Do as many of these as possible.
654 sector_t remaining
= max
;
657 for (i
= ci
->idx
; remaining
&& (i
< bio
->bi_vcnt
); i
++) {
658 bv_len
= to_sector(bio
->bi_io_vec
[i
].bv_len
);
660 if (bv_len
> remaining
)
667 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
, i
- ci
->idx
, len
);
668 __map_bio(ti
, clone
, tio
);
671 ci
->sector_count
-= len
;
676 * Handle a bvec that must be split between two or more targets.
678 struct bio_vec
*bv
= bio
->bi_io_vec
+ ci
->idx
;
679 sector_t remaining
= to_sector(bv
->bv_len
);
680 unsigned int offset
= 0;
684 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
685 max
= max_io_len(ci
->md
, ci
->sector
, ti
);
687 tio
= alloc_tio(ci
->md
);
690 memset(&tio
->info
, 0, sizeof(tio
->info
));
693 len
= min(remaining
, max
);
695 clone
= split_bvec(bio
, ci
->sector
, ci
->idx
,
696 bv
->bv_offset
+ offset
, len
);
698 __map_bio(ti
, clone
, tio
);
701 ci
->sector_count
-= len
;
702 offset
+= to_bytes(len
);
703 } while (remaining
-= len
);
710 * Split the bio into several clones.
712 static void __split_bio(struct mapped_device
*md
, struct bio
*bio
)
714 struct clone_info ci
;
716 ci
.map
= dm_get_table(md
);
718 bio_io_error(bio
, bio
->bi_size
);
724 ci
.io
= alloc_io(md
);
726 atomic_set(&ci
.io
->io_count
, 1);
729 ci
.sector
= bio
->bi_sector
;
730 ci
.sector_count
= bio_sectors(bio
);
731 ci
.idx
= bio
->bi_idx
;
733 start_io_acct(ci
.io
);
734 while (ci
.sector_count
)
735 __clone_and_map(&ci
);
737 /* drop the extra reference count */
738 dec_pending(ci
.io
, 0);
739 dm_table_put(ci
.map
);
741 /*-----------------------------------------------------------------
743 *---------------------------------------------------------------*/
746 * The request function that just remaps the bio built up by
749 static int dm_request(request_queue_t
*q
, struct bio
*bio
)
752 int rw
= bio_data_dir(bio
);
753 struct mapped_device
*md
= q
->queuedata
;
755 down_read(&md
->io_lock
);
757 disk_stat_inc(dm_disk(md
), ios
[rw
]);
758 disk_stat_add(dm_disk(md
), sectors
[rw
], bio_sectors(bio
));
761 * If we're suspended we have to queue
764 while (test_bit(DMF_BLOCK_IO
, &md
->flags
)) {
765 up_read(&md
->io_lock
);
767 if (bio_rw(bio
) == READA
) {
768 bio_io_error(bio
, bio
->bi_size
);
772 r
= queue_io(md
, bio
);
774 bio_io_error(bio
, bio
->bi_size
);
778 return 0; /* deferred successfully */
781 * We're in a while loop, because someone could suspend
782 * before we get to the following read lock.
784 down_read(&md
->io_lock
);
787 __split_bio(md
, bio
);
788 up_read(&md
->io_lock
);
792 static int dm_flush_all(request_queue_t
*q
, struct gendisk
*disk
,
793 sector_t
*error_sector
)
795 struct mapped_device
*md
= q
->queuedata
;
796 struct dm_table
*map
= dm_get_table(md
);
800 ret
= dm_table_flush_all(map
);
807 static void dm_unplug_all(request_queue_t
*q
)
809 struct mapped_device
*md
= q
->queuedata
;
810 struct dm_table
*map
= dm_get_table(md
);
813 dm_table_unplug_all(map
);
818 static int dm_any_congested(void *congested_data
, int bdi_bits
)
821 struct mapped_device
*md
= (struct mapped_device
*) congested_data
;
822 struct dm_table
*map
= dm_get_table(md
);
824 if (!map
|| test_bit(DMF_BLOCK_IO
, &md
->flags
))
827 r
= dm_table_any_congested(map
, bdi_bits
);
833 /*-----------------------------------------------------------------
834 * An IDR is used to keep track of allocated minor numbers.
835 *---------------------------------------------------------------*/
836 static DEFINE_IDR(_minor_idr
);
838 static void free_minor(int minor
)
840 spin_lock(&_minor_lock
);
841 idr_remove(&_minor_idr
, minor
);
842 spin_unlock(&_minor_lock
);
846 * See if the device with a specific minor # is free.
848 static int specific_minor(struct mapped_device
*md
, int minor
)
852 if (minor
>= (1 << MINORBITS
))
855 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
859 spin_lock(&_minor_lock
);
861 if (idr_find(&_minor_idr
, minor
)) {
866 r
= idr_get_new_above(&_minor_idr
, MINOR_ALLOCED
, minor
, &m
);
871 idr_remove(&_minor_idr
, m
);
877 spin_unlock(&_minor_lock
);
881 static int next_free_minor(struct mapped_device
*md
, int *minor
)
885 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
889 spin_lock(&_minor_lock
);
891 r
= idr_get_new(&_minor_idr
, MINOR_ALLOCED
, &m
);
896 if (m
>= (1 << MINORBITS
)) {
897 idr_remove(&_minor_idr
, m
);
905 spin_unlock(&_minor_lock
);
909 static struct block_device_operations dm_blk_dops
;
912 * Allocate and initialise a blank device with a given minor.
914 static struct mapped_device
*alloc_dev(int minor
)
917 struct mapped_device
*md
= kmalloc(sizeof(*md
), GFP_KERNEL
);
921 DMWARN("unable to allocate device, out of memory.");
925 if (!try_module_get(THIS_MODULE
))
928 /* get a minor number for the dev */
929 if (minor
== DM_ANY_MINOR
)
930 r
= next_free_minor(md
, &minor
);
932 r
= specific_minor(md
, minor
);
936 memset(md
, 0, sizeof(*md
));
937 init_rwsem(&md
->io_lock
);
938 init_MUTEX(&md
->suspend_lock
);
939 rwlock_init(&md
->map_lock
);
940 atomic_set(&md
->holders
, 1);
941 atomic_set(&md
->open_count
, 0);
942 atomic_set(&md
->event_nr
, 0);
944 md
->queue
= blk_alloc_queue(GFP_KERNEL
);
948 md
->queue
->queuedata
= md
;
949 md
->queue
->backing_dev_info
.congested_fn
= dm_any_congested
;
950 md
->queue
->backing_dev_info
.congested_data
= md
;
951 blk_queue_make_request(md
->queue
, dm_request
);
952 blk_queue_bounce_limit(md
->queue
, BLK_BOUNCE_ANY
);
953 md
->queue
->unplug_fn
= dm_unplug_all
;
954 md
->queue
->issue_flush_fn
= dm_flush_all
;
956 md
->io_pool
= mempool_create_slab_pool(MIN_IOS
, _io_cache
);
960 md
->tio_pool
= mempool_create_slab_pool(MIN_IOS
, _tio_cache
);
964 md
->disk
= alloc_disk(1);
968 atomic_set(&md
->pending
, 0);
969 init_waitqueue_head(&md
->wait
);
970 init_waitqueue_head(&md
->eventq
);
972 md
->disk
->major
= _major
;
973 md
->disk
->first_minor
= minor
;
974 md
->disk
->fops
= &dm_blk_dops
;
975 md
->disk
->queue
= md
->queue
;
976 md
->disk
->private_data
= md
;
977 sprintf(md
->disk
->disk_name
, "dm-%d", minor
);
979 format_dev_t(md
->name
, MKDEV(_major
, minor
));
981 /* Populate the mapping, nobody knows we exist yet */
982 spin_lock(&_minor_lock
);
983 old_md
= idr_replace(&_minor_idr
, md
, minor
);
984 spin_unlock(&_minor_lock
);
986 BUG_ON(old_md
!= MINOR_ALLOCED
);
991 mempool_destroy(md
->tio_pool
);
993 mempool_destroy(md
->io_pool
);
995 blk_cleanup_queue(md
->queue
);
998 module_put(THIS_MODULE
);
1004 static void free_dev(struct mapped_device
*md
)
1006 int minor
= md
->disk
->first_minor
;
1008 if (md
->suspended_bdev
) {
1009 thaw_bdev(md
->suspended_bdev
, NULL
);
1010 bdput(md
->suspended_bdev
);
1012 mempool_destroy(md
->tio_pool
);
1013 mempool_destroy(md
->io_pool
);
1014 del_gendisk(md
->disk
);
1017 spin_lock(&_minor_lock
);
1018 md
->disk
->private_data
= NULL
;
1019 spin_unlock(&_minor_lock
);
1022 blk_cleanup_queue(md
->queue
);
1023 module_put(THIS_MODULE
);
1028 * Bind a table to the device.
1030 static void event_callback(void *context
)
1032 struct mapped_device
*md
= (struct mapped_device
*) context
;
1034 atomic_inc(&md
->event_nr
);
1035 wake_up(&md
->eventq
);
1038 static void __set_size(struct mapped_device
*md
, sector_t size
)
1040 set_capacity(md
->disk
, size
);
1042 mutex_lock(&md
->suspended_bdev
->bd_inode
->i_mutex
);
1043 i_size_write(md
->suspended_bdev
->bd_inode
, (loff_t
)size
<< SECTOR_SHIFT
);
1044 mutex_unlock(&md
->suspended_bdev
->bd_inode
->i_mutex
);
1047 static int __bind(struct mapped_device
*md
, struct dm_table
*t
)
1049 request_queue_t
*q
= md
->queue
;
1052 size
= dm_table_get_size(t
);
1055 * Wipe any geometry if the size of the table changed.
1057 if (size
!= get_capacity(md
->disk
))
1058 memset(&md
->geometry
, 0, sizeof(md
->geometry
));
1060 __set_size(md
, size
);
1065 dm_table_event_callback(t
, event_callback
, md
);
1067 write_lock(&md
->map_lock
);
1069 dm_table_set_restrictions(t
, q
);
1070 write_unlock(&md
->map_lock
);
1075 static void __unbind(struct mapped_device
*md
)
1077 struct dm_table
*map
= md
->map
;
1082 dm_table_event_callback(map
, NULL
, NULL
);
1083 write_lock(&md
->map_lock
);
1085 write_unlock(&md
->map_lock
);
1090 * Constructor for a new device.
1092 int dm_create(int minor
, struct mapped_device
**result
)
1094 struct mapped_device
*md
;
1096 md
= alloc_dev(minor
);
1104 static struct mapped_device
*dm_find_md(dev_t dev
)
1106 struct mapped_device
*md
;
1107 unsigned minor
= MINOR(dev
);
1109 if (MAJOR(dev
) != _major
|| minor
>= (1 << MINORBITS
))
1112 spin_lock(&_minor_lock
);
1114 md
= idr_find(&_minor_idr
, minor
);
1115 if (md
&& (md
== MINOR_ALLOCED
||
1116 (dm_disk(md
)->first_minor
!= minor
) ||
1117 test_bit(DMF_FREEING
, &md
->flags
))) {
1123 spin_unlock(&_minor_lock
);
1128 struct mapped_device
*dm_get_md(dev_t dev
)
1130 struct mapped_device
*md
= dm_find_md(dev
);
1138 void *dm_get_mdptr(struct mapped_device
*md
)
1140 return md
->interface_ptr
;
1143 void dm_set_mdptr(struct mapped_device
*md
, void *ptr
)
1145 md
->interface_ptr
= ptr
;
1148 void dm_get(struct mapped_device
*md
)
1150 atomic_inc(&md
->holders
);
1153 const char *dm_device_name(struct mapped_device
*md
)
1157 EXPORT_SYMBOL_GPL(dm_device_name
);
1159 void dm_put(struct mapped_device
*md
)
1161 struct dm_table
*map
;
1163 BUG_ON(test_bit(DMF_FREEING
, &md
->flags
));
1165 if (atomic_dec_and_lock(&md
->holders
, &_minor_lock
)) {
1166 map
= dm_get_table(md
);
1167 idr_replace(&_minor_idr
, MINOR_ALLOCED
, dm_disk(md
)->first_minor
);
1168 set_bit(DMF_FREEING
, &md
->flags
);
1169 spin_unlock(&_minor_lock
);
1170 if (!dm_suspended(md
)) {
1171 dm_table_presuspend_targets(map
);
1172 dm_table_postsuspend_targets(map
);
1181 * Process the deferred bios
1183 static void __flush_deferred_io(struct mapped_device
*md
, struct bio
*c
)
1196 * Swap in a new table (destroying old one).
1198 int dm_swap_table(struct mapped_device
*md
, struct dm_table
*table
)
1202 down(&md
->suspend_lock
);
1204 /* device must be suspended */
1205 if (!dm_suspended(md
))
1209 r
= __bind(md
, table
);
1212 up(&md
->suspend_lock
);
1217 * Functions to lock and unlock any filesystem running on the
1220 static int lock_fs(struct mapped_device
*md
)
1224 WARN_ON(md
->frozen_sb
);
1226 md
->frozen_sb
= freeze_bdev(md
->suspended_bdev
);
1227 if (IS_ERR(md
->frozen_sb
)) {
1228 r
= PTR_ERR(md
->frozen_sb
);
1229 md
->frozen_sb
= NULL
;
1233 set_bit(DMF_FROZEN
, &md
->flags
);
1235 /* don't bdput right now, we don't want the bdev
1236 * to go away while it is locked.
1241 static void unlock_fs(struct mapped_device
*md
)
1243 if (!test_bit(DMF_FROZEN
, &md
->flags
))
1246 thaw_bdev(md
->suspended_bdev
, md
->frozen_sb
);
1247 md
->frozen_sb
= NULL
;
1248 clear_bit(DMF_FROZEN
, &md
->flags
);
1252 * We need to be able to change a mapping table under a mounted
1253 * filesystem. For example we might want to move some data in
1254 * the background. Before the table can be swapped with
1255 * dm_bind_table, dm_suspend must be called to flush any in
1256 * flight bios and ensure that any further io gets deferred.
1258 int dm_suspend(struct mapped_device
*md
, int do_lockfs
)
1260 struct dm_table
*map
= NULL
;
1261 DECLARE_WAITQUEUE(wait
, current
);
1265 down(&md
->suspend_lock
);
1267 if (dm_suspended(md
))
1270 map
= dm_get_table(md
);
1272 /* This does not get reverted if there's an error later. */
1273 dm_table_presuspend_targets(map
);
1275 md
->suspended_bdev
= bdget_disk(md
->disk
, 0);
1276 if (!md
->suspended_bdev
) {
1277 DMWARN("bdget failed in dm_suspend");
1282 /* Flush I/O to the device. */
1290 * First we set the BLOCK_IO flag so no more ios will be mapped.
1292 down_write(&md
->io_lock
);
1293 set_bit(DMF_BLOCK_IO
, &md
->flags
);
1295 add_wait_queue(&md
->wait
, &wait
);
1296 up_write(&md
->io_lock
);
1300 dm_table_unplug_all(map
);
1303 * Then we wait for the already mapped ios to
1307 set_current_state(TASK_INTERRUPTIBLE
);
1309 if (!atomic_read(&md
->pending
) || signal_pending(current
))
1314 set_current_state(TASK_RUNNING
);
1316 down_write(&md
->io_lock
);
1317 remove_wait_queue(&md
->wait
, &wait
);
1319 /* were we interrupted ? */
1321 if (atomic_read(&md
->pending
)) {
1322 clear_bit(DMF_BLOCK_IO
, &md
->flags
);
1323 def
= bio_list_get(&md
->deferred
);
1324 __flush_deferred_io(md
, def
);
1325 up_write(&md
->io_lock
);
1329 up_write(&md
->io_lock
);
1331 dm_table_postsuspend_targets(map
);
1333 set_bit(DMF_SUSPENDED
, &md
->flags
);
1338 if (r
&& md
->suspended_bdev
) {
1339 bdput(md
->suspended_bdev
);
1340 md
->suspended_bdev
= NULL
;
1344 up(&md
->suspend_lock
);
1348 int dm_resume(struct mapped_device
*md
)
1352 struct dm_table
*map
= NULL
;
1354 down(&md
->suspend_lock
);
1355 if (!dm_suspended(md
))
1358 map
= dm_get_table(md
);
1359 if (!map
|| !dm_table_get_size(map
))
1362 dm_table_resume_targets(map
);
1364 down_write(&md
->io_lock
);
1365 clear_bit(DMF_BLOCK_IO
, &md
->flags
);
1367 def
= bio_list_get(&md
->deferred
);
1368 __flush_deferred_io(md
, def
);
1369 up_write(&md
->io_lock
);
1373 bdput(md
->suspended_bdev
);
1374 md
->suspended_bdev
= NULL
;
1376 clear_bit(DMF_SUSPENDED
, &md
->flags
);
1378 dm_table_unplug_all(map
);
1384 up(&md
->suspend_lock
);
1389 /*-----------------------------------------------------------------
1390 * Event notification.
1391 *---------------------------------------------------------------*/
1392 uint32_t dm_get_event_nr(struct mapped_device
*md
)
1394 return atomic_read(&md
->event_nr
);
1397 int dm_wait_event(struct mapped_device
*md
, int event_nr
)
1399 return wait_event_interruptible(md
->eventq
,
1400 (event_nr
!= atomic_read(&md
->event_nr
)));
1404 * The gendisk is only valid as long as you have a reference
1407 struct gendisk
*dm_disk(struct mapped_device
*md
)
1412 int dm_suspended(struct mapped_device
*md
)
1414 return test_bit(DMF_SUSPENDED
, &md
->flags
);
1417 static struct block_device_operations dm_blk_dops
= {
1418 .open
= dm_blk_open
,
1419 .release
= dm_blk_close
,
1420 .ioctl
= dm_blk_ioctl
,
1421 .getgeo
= dm_blk_getgeo
,
1422 .owner
= THIS_MODULE
1425 EXPORT_SYMBOL(dm_get_mapinfo
);
1430 module_init(dm_init
);
1431 module_exit(dm_exit
);
1433 module_param(major
, uint
, 0);
1434 MODULE_PARM_DESC(major
, "The major number of the device mapper");
1435 MODULE_DESCRIPTION(DM_NAME
" driver");
1436 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1437 MODULE_LICENSE("GPL");