2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 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>
24 static const char *_name
= DM_NAME
;
26 static unsigned int major
= 0;
27 static unsigned int _major
= 0;
30 * One of these is allocated per bio.
33 struct mapped_device
*md
;
37 unsigned long start_time
;
41 * One of these is allocated per target within a bio. Hopefully
42 * this will be simplified out one day.
50 union map_info
*dm_get_mapinfo(struct bio
*bio
)
52 if (bio
&& bio
->bi_private
)
53 return &((struct target_io
*)bio
->bi_private
)->info
;
58 * Bits for the md->flags field.
60 #define DMF_BLOCK_IO 0
61 #define DMF_SUSPENDED 1
64 struct mapped_device
{
65 struct rw_semaphore io_lock
;
66 struct semaphore suspend_lock
;
72 request_queue_t
*queue
;
79 * A list of ios that arrived while we were suspended.
82 wait_queue_head_t wait
;
83 struct bio_list deferred
;
86 * The current mapping.
91 * io objects are allocated from here.
100 wait_queue_head_t eventq
;
103 * freeze/thaw support require holding onto a super block
105 struct super_block
*frozen_sb
;
106 struct block_device
*suspended_bdev
;
108 /* forced geometry settings */
109 struct hd_geometry geometry
;
113 static kmem_cache_t
*_io_cache
;
114 static kmem_cache_t
*_tio_cache
;
116 static struct bio_set
*dm_set
;
118 static int __init
local_init(void)
122 dm_set
= bioset_create(16, 16, 4);
126 /* allocate a slab for the dm_ios */
127 _io_cache
= kmem_cache_create("dm_io",
128 sizeof(struct dm_io
), 0, 0, NULL
, NULL
);
132 /* allocate a slab for the target ios */
133 _tio_cache
= kmem_cache_create("dm_tio", sizeof(struct target_io
),
136 kmem_cache_destroy(_io_cache
);
141 r
= register_blkdev(_major
, _name
);
143 kmem_cache_destroy(_tio_cache
);
144 kmem_cache_destroy(_io_cache
);
154 static void local_exit(void)
156 kmem_cache_destroy(_tio_cache
);
157 kmem_cache_destroy(_io_cache
);
161 if (unregister_blkdev(_major
, _name
) < 0)
162 DMERR("devfs_unregister_blkdev failed");
166 DMINFO("cleaned up");
169 int (*_inits
[])(void) __initdata
= {
177 void (*_exits
[])(void) = {
185 static int __init
dm_init(void)
187 const int count
= ARRAY_SIZE(_inits
);
191 for (i
= 0; i
< count
; i
++) {
206 static void __exit
dm_exit(void)
208 int i
= ARRAY_SIZE(_exits
);
215 * Block device functions
217 static int dm_blk_open(struct inode
*inode
, struct file
*file
)
219 struct mapped_device
*md
;
221 md
= inode
->i_bdev
->bd_disk
->private_data
;
226 static int dm_blk_close(struct inode
*inode
, struct file
*file
)
228 struct mapped_device
*md
;
230 md
= inode
->i_bdev
->bd_disk
->private_data
;
235 static int dm_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
237 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
239 return dm_get_geometry(md
, geo
);
242 static inline struct dm_io
*alloc_io(struct mapped_device
*md
)
244 return mempool_alloc(md
->io_pool
, GFP_NOIO
);
247 static inline void free_io(struct mapped_device
*md
, struct dm_io
*io
)
249 mempool_free(io
, md
->io_pool
);
252 static inline struct target_io
*alloc_tio(struct mapped_device
*md
)
254 return mempool_alloc(md
->tio_pool
, GFP_NOIO
);
257 static inline void free_tio(struct mapped_device
*md
, struct target_io
*tio
)
259 mempool_free(tio
, md
->tio_pool
);
262 static void start_io_acct(struct dm_io
*io
)
264 struct mapped_device
*md
= io
->md
;
266 io
->start_time
= jiffies
;
269 disk_round_stats(dm_disk(md
));
271 dm_disk(md
)->in_flight
= atomic_inc_return(&md
->pending
);
274 static int end_io_acct(struct dm_io
*io
)
276 struct mapped_device
*md
= io
->md
;
277 struct bio
*bio
= io
->bio
;
278 unsigned long duration
= jiffies
- io
->start_time
;
280 int rw
= bio_data_dir(bio
);
283 disk_round_stats(dm_disk(md
));
285 dm_disk(md
)->in_flight
= pending
= atomic_dec_return(&md
->pending
);
287 disk_stat_add(dm_disk(md
), ticks
[rw
], duration
);
293 * Add the bio to the list of deferred io.
295 static int queue_io(struct mapped_device
*md
, struct bio
*bio
)
297 down_write(&md
->io_lock
);
299 if (!test_bit(DMF_BLOCK_IO
, &md
->flags
)) {
300 up_write(&md
->io_lock
);
304 bio_list_add(&md
->deferred
, bio
);
306 up_write(&md
->io_lock
);
307 return 0; /* deferred successfully */
311 * Everyone (including functions in this file), should use this
312 * function to access the md->map field, and make sure they call
313 * dm_table_put() when finished.
315 struct dm_table
*dm_get_table(struct mapped_device
*md
)
319 read_lock(&md
->map_lock
);
323 read_unlock(&md
->map_lock
);
329 * Get the geometry associated with a dm device
331 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
339 * Set the geometry of a device.
341 int dm_set_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
343 sector_t sz
= (sector_t
)geo
->cylinders
* geo
->heads
* geo
->sectors
;
345 if (geo
->start
> sz
) {
346 DMWARN("Start sector is beyond the geometry limits.");
355 /*-----------------------------------------------------------------
357 * A more elegant soln is in the works that uses the queue
358 * merge fn, unfortunately there are a couple of changes to
359 * the block layer that I want to make for this. So in the
360 * interests of getting something for people to use I give
361 * you this clearly demarcated crap.
362 *---------------------------------------------------------------*/
365 * Decrements the number of outstanding ios that a bio has been
366 * cloned into, completing the original io if necc.
368 static void dec_pending(struct dm_io
*io
, int error
)
373 if (atomic_dec_and_test(&io
->io_count
)) {
375 /* nudge anyone waiting on suspend queue */
376 wake_up(&io
->md
->wait
);
378 blk_add_trace_bio(io
->md
->queue
, io
->bio
, BLK_TA_COMPLETE
);
380 bio_endio(io
->bio
, io
->bio
->bi_size
, io
->error
);
385 static int clone_endio(struct bio
*bio
, unsigned int done
, int error
)
388 struct target_io
*tio
= bio
->bi_private
;
389 struct dm_io
*io
= tio
->io
;
390 dm_endio_fn endio
= tio
->ti
->type
->end_io
;
395 if (!bio_flagged(bio
, BIO_UPTODATE
) && !error
)
399 r
= endio(tio
->ti
, bio
, error
, &tio
->info
);
404 /* the target wants another shot at the io */
408 free_tio(io
->md
, tio
);
409 dec_pending(io
, error
);
414 static sector_t
max_io_len(struct mapped_device
*md
,
415 sector_t sector
, struct dm_target
*ti
)
417 sector_t offset
= sector
- ti
->begin
;
418 sector_t len
= ti
->len
- offset
;
421 * Does the target need to split even further ?
425 boundary
= ((offset
+ ti
->split_io
) & ~(ti
->split_io
- 1))
434 static void __map_bio(struct dm_target
*ti
, struct bio
*clone
,
435 struct target_io
*tio
)
443 BUG_ON(!clone
->bi_size
);
445 clone
->bi_end_io
= clone_endio
;
446 clone
->bi_private
= tio
;
449 * Map the clone. If r == 0 we don't need to do
450 * anything, the target has assumed ownership of
453 atomic_inc(&tio
->io
->io_count
);
454 sector
= clone
->bi_sector
;
455 r
= ti
->type
->map(ti
, clone
, &tio
->info
);
457 /* the bio has been remapped so dispatch it */
459 blk_add_trace_remap(bdev_get_queue(clone
->bi_bdev
), clone
,
460 tio
->io
->bio
->bi_bdev
->bd_dev
, sector
,
463 generic_make_request(clone
);
467 /* error the io and bail out */
468 struct dm_io
*io
= tio
->io
;
469 free_tio(tio
->io
->md
, tio
);
476 struct mapped_device
*md
;
477 struct dm_table
*map
;
481 sector_t sector_count
;
485 static void dm_bio_destructor(struct bio
*bio
)
487 bio_free(bio
, dm_set
);
491 * Creates a little bio that is just does part of a bvec.
493 static struct bio
*split_bvec(struct bio
*bio
, sector_t sector
,
494 unsigned short idx
, unsigned int offset
,
498 struct bio_vec
*bv
= bio
->bi_io_vec
+ idx
;
500 clone
= bio_alloc_bioset(GFP_NOIO
, 1, dm_set
);
501 clone
->bi_destructor
= dm_bio_destructor
;
502 *clone
->bi_io_vec
= *bv
;
504 clone
->bi_sector
= sector
;
505 clone
->bi_bdev
= bio
->bi_bdev
;
506 clone
->bi_rw
= bio
->bi_rw
;
508 clone
->bi_size
= to_bytes(len
);
509 clone
->bi_io_vec
->bv_offset
= offset
;
510 clone
->bi_io_vec
->bv_len
= clone
->bi_size
;
516 * Creates a bio that consists of range of complete bvecs.
518 static struct bio
*clone_bio(struct bio
*bio
, sector_t sector
,
519 unsigned short idx
, unsigned short bv_count
,
524 clone
= bio_clone(bio
, GFP_NOIO
);
525 clone
->bi_sector
= sector
;
527 clone
->bi_vcnt
= idx
+ bv_count
;
528 clone
->bi_size
= to_bytes(len
);
529 clone
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
534 static void __clone_and_map(struct clone_info
*ci
)
536 struct bio
*clone
, *bio
= ci
->bio
;
537 struct dm_target
*ti
= dm_table_find_target(ci
->map
, ci
->sector
);
538 sector_t len
= 0, max
= max_io_len(ci
->md
, ci
->sector
, ti
);
539 struct target_io
*tio
;
542 * Allocate a target io object.
544 tio
= alloc_tio(ci
->md
);
547 memset(&tio
->info
, 0, sizeof(tio
->info
));
549 if (ci
->sector_count
<= max
) {
551 * Optimise for the simple case where we can do all of
552 * the remaining io with a single clone.
554 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
,
555 bio
->bi_vcnt
- ci
->idx
, ci
->sector_count
);
556 __map_bio(ti
, clone
, tio
);
557 ci
->sector_count
= 0;
559 } else if (to_sector(bio
->bi_io_vec
[ci
->idx
].bv_len
) <= max
) {
561 * There are some bvecs that don't span targets.
562 * Do as many of these as possible.
565 sector_t remaining
= max
;
568 for (i
= ci
->idx
; remaining
&& (i
< bio
->bi_vcnt
); i
++) {
569 bv_len
= to_sector(bio
->bi_io_vec
[i
].bv_len
);
571 if (bv_len
> remaining
)
578 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
, i
- ci
->idx
, len
);
579 __map_bio(ti
, clone
, tio
);
582 ci
->sector_count
-= len
;
587 * Handle a bvec that must be split between two or more targets.
589 struct bio_vec
*bv
= bio
->bi_io_vec
+ ci
->idx
;
590 sector_t remaining
= to_sector(bv
->bv_len
);
591 unsigned int offset
= 0;
595 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
596 max
= max_io_len(ci
->md
, ci
->sector
, ti
);
598 tio
= alloc_tio(ci
->md
);
601 memset(&tio
->info
, 0, sizeof(tio
->info
));
604 len
= min(remaining
, max
);
606 clone
= split_bvec(bio
, ci
->sector
, ci
->idx
,
607 bv
->bv_offset
+ offset
, len
);
609 __map_bio(ti
, clone
, tio
);
612 ci
->sector_count
-= len
;
613 offset
+= to_bytes(len
);
614 } while (remaining
-= len
);
621 * Split the bio into several clones.
623 static void __split_bio(struct mapped_device
*md
, struct bio
*bio
)
625 struct clone_info ci
;
627 ci
.map
= dm_get_table(md
);
629 bio_io_error(bio
, bio
->bi_size
);
635 ci
.io
= alloc_io(md
);
637 atomic_set(&ci
.io
->io_count
, 1);
640 ci
.sector
= bio
->bi_sector
;
641 ci
.sector_count
= bio_sectors(bio
);
642 ci
.idx
= bio
->bi_idx
;
644 start_io_acct(ci
.io
);
645 while (ci
.sector_count
)
646 __clone_and_map(&ci
);
648 /* drop the extra reference count */
649 dec_pending(ci
.io
, 0);
650 dm_table_put(ci
.map
);
652 /*-----------------------------------------------------------------
654 *---------------------------------------------------------------*/
657 * The request function that just remaps the bio built up by
660 static int dm_request(request_queue_t
*q
, struct bio
*bio
)
663 int rw
= bio_data_dir(bio
);
664 struct mapped_device
*md
= q
->queuedata
;
666 down_read(&md
->io_lock
);
668 disk_stat_inc(dm_disk(md
), ios
[rw
]);
669 disk_stat_add(dm_disk(md
), sectors
[rw
], bio_sectors(bio
));
672 * If we're suspended we have to queue
675 while (test_bit(DMF_BLOCK_IO
, &md
->flags
)) {
676 up_read(&md
->io_lock
);
678 if (bio_rw(bio
) == READA
) {
679 bio_io_error(bio
, bio
->bi_size
);
683 r
= queue_io(md
, bio
);
685 bio_io_error(bio
, bio
->bi_size
);
689 return 0; /* deferred successfully */
692 * We're in a while loop, because someone could suspend
693 * before we get to the following read lock.
695 down_read(&md
->io_lock
);
698 __split_bio(md
, bio
);
699 up_read(&md
->io_lock
);
703 static int dm_flush_all(request_queue_t
*q
, struct gendisk
*disk
,
704 sector_t
*error_sector
)
706 struct mapped_device
*md
= q
->queuedata
;
707 struct dm_table
*map
= dm_get_table(md
);
711 ret
= dm_table_flush_all(map
);
718 static void dm_unplug_all(request_queue_t
*q
)
720 struct mapped_device
*md
= q
->queuedata
;
721 struct dm_table
*map
= dm_get_table(md
);
724 dm_table_unplug_all(map
);
729 static int dm_any_congested(void *congested_data
, int bdi_bits
)
732 struct mapped_device
*md
= (struct mapped_device
*) congested_data
;
733 struct dm_table
*map
= dm_get_table(md
);
735 if (!map
|| test_bit(DMF_BLOCK_IO
, &md
->flags
))
738 r
= dm_table_any_congested(map
, bdi_bits
);
744 /*-----------------------------------------------------------------
745 * An IDR is used to keep track of allocated minor numbers.
746 *---------------------------------------------------------------*/
747 static DEFINE_MUTEX(_minor_lock
);
748 static DEFINE_IDR(_minor_idr
);
750 static void free_minor(unsigned int minor
)
752 mutex_lock(&_minor_lock
);
753 idr_remove(&_minor_idr
, minor
);
754 mutex_unlock(&_minor_lock
);
758 * See if the device with a specific minor # is free.
760 static int specific_minor(struct mapped_device
*md
, unsigned int minor
)
764 if (minor
>= (1 << MINORBITS
))
767 mutex_lock(&_minor_lock
);
769 if (idr_find(&_minor_idr
, minor
)) {
774 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
780 r
= idr_get_new_above(&_minor_idr
, md
, minor
, &m
);
786 idr_remove(&_minor_idr
, m
);
792 mutex_unlock(&_minor_lock
);
796 static int next_free_minor(struct mapped_device
*md
, unsigned int *minor
)
801 mutex_lock(&_minor_lock
);
803 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
809 r
= idr_get_new(&_minor_idr
, md
, &m
);
814 if (m
>= (1 << MINORBITS
)) {
815 idr_remove(&_minor_idr
, m
);
823 mutex_unlock(&_minor_lock
);
827 static struct block_device_operations dm_blk_dops
;
830 * Allocate and initialise a blank device with a given minor.
832 static struct mapped_device
*alloc_dev(unsigned int minor
, int persistent
)
835 struct mapped_device
*md
= kmalloc(sizeof(*md
), GFP_KERNEL
);
838 DMWARN("unable to allocate device, out of memory.");
842 /* get a minor number for the dev */
843 r
= persistent
? specific_minor(md
, minor
) : next_free_minor(md
, &minor
);
847 memset(md
, 0, sizeof(*md
));
848 init_rwsem(&md
->io_lock
);
849 init_MUTEX(&md
->suspend_lock
);
850 rwlock_init(&md
->map_lock
);
851 atomic_set(&md
->holders
, 1);
852 atomic_set(&md
->event_nr
, 0);
854 md
->queue
= blk_alloc_queue(GFP_KERNEL
);
858 md
->queue
->queuedata
= md
;
859 md
->queue
->backing_dev_info
.congested_fn
= dm_any_congested
;
860 md
->queue
->backing_dev_info
.congested_data
= md
;
861 blk_queue_make_request(md
->queue
, dm_request
);
862 blk_queue_bounce_limit(md
->queue
, BLK_BOUNCE_ANY
);
863 md
->queue
->unplug_fn
= dm_unplug_all
;
864 md
->queue
->issue_flush_fn
= dm_flush_all
;
866 md
->io_pool
= mempool_create_slab_pool(MIN_IOS
, _io_cache
);
870 md
->tio_pool
= mempool_create_slab_pool(MIN_IOS
, _tio_cache
);
874 md
->disk
= alloc_disk(1);
878 md
->disk
->major
= _major
;
879 md
->disk
->first_minor
= minor
;
880 md
->disk
->fops
= &dm_blk_dops
;
881 md
->disk
->queue
= md
->queue
;
882 md
->disk
->private_data
= md
;
883 sprintf(md
->disk
->disk_name
, "dm-%d", minor
);
885 format_dev_t(md
->name
, MKDEV(_major
, minor
));
887 atomic_set(&md
->pending
, 0);
888 init_waitqueue_head(&md
->wait
);
889 init_waitqueue_head(&md
->eventq
);
894 mempool_destroy(md
->tio_pool
);
896 mempool_destroy(md
->io_pool
);
898 blk_cleanup_queue(md
->queue
);
905 static void free_dev(struct mapped_device
*md
)
907 unsigned int minor
= md
->disk
->first_minor
;
909 if (md
->suspended_bdev
) {
910 thaw_bdev(md
->suspended_bdev
, NULL
);
911 bdput(md
->suspended_bdev
);
913 mempool_destroy(md
->tio_pool
);
914 mempool_destroy(md
->io_pool
);
915 del_gendisk(md
->disk
);
918 blk_cleanup_queue(md
->queue
);
923 * Bind a table to the device.
925 static void event_callback(void *context
)
927 struct mapped_device
*md
= (struct mapped_device
*) context
;
929 atomic_inc(&md
->event_nr
);
930 wake_up(&md
->eventq
);
933 static void __set_size(struct mapped_device
*md
, sector_t size
)
935 set_capacity(md
->disk
, size
);
937 mutex_lock(&md
->suspended_bdev
->bd_inode
->i_mutex
);
938 i_size_write(md
->suspended_bdev
->bd_inode
, (loff_t
)size
<< SECTOR_SHIFT
);
939 mutex_unlock(&md
->suspended_bdev
->bd_inode
->i_mutex
);
942 static int __bind(struct mapped_device
*md
, struct dm_table
*t
)
944 request_queue_t
*q
= md
->queue
;
947 size
= dm_table_get_size(t
);
950 * Wipe any geometry if the size of the table changed.
952 if (size
!= get_capacity(md
->disk
))
953 memset(&md
->geometry
, 0, sizeof(md
->geometry
));
955 __set_size(md
, size
);
960 dm_table_event_callback(t
, event_callback
, md
);
962 write_lock(&md
->map_lock
);
964 dm_table_set_restrictions(t
, q
);
965 write_unlock(&md
->map_lock
);
970 static void __unbind(struct mapped_device
*md
)
972 struct dm_table
*map
= md
->map
;
977 dm_table_event_callback(map
, NULL
, NULL
);
978 write_lock(&md
->map_lock
);
980 write_unlock(&md
->map_lock
);
985 * Constructor for a new device.
987 static int create_aux(unsigned int minor
, int persistent
,
988 struct mapped_device
**result
)
990 struct mapped_device
*md
;
992 md
= alloc_dev(minor
, persistent
);
1000 int dm_create(struct mapped_device
**result
)
1002 return create_aux(0, 0, result
);
1005 int dm_create_with_minor(unsigned int minor
, struct mapped_device
**result
)
1007 return create_aux(minor
, 1, result
);
1010 static struct mapped_device
*dm_find_md(dev_t dev
)
1012 struct mapped_device
*md
;
1013 unsigned minor
= MINOR(dev
);
1015 if (MAJOR(dev
) != _major
|| minor
>= (1 << MINORBITS
))
1018 mutex_lock(&_minor_lock
);
1020 md
= idr_find(&_minor_idr
, minor
);
1021 if (!md
|| (dm_disk(md
)->first_minor
!= minor
))
1024 mutex_unlock(&_minor_lock
);
1029 struct mapped_device
*dm_get_md(dev_t dev
)
1031 struct mapped_device
*md
= dm_find_md(dev
);
1039 void *dm_get_mdptr(struct mapped_device
*md
)
1041 return md
->interface_ptr
;
1044 void dm_set_mdptr(struct mapped_device
*md
, void *ptr
)
1046 md
->interface_ptr
= ptr
;
1049 void dm_get(struct mapped_device
*md
)
1051 atomic_inc(&md
->holders
);
1054 void dm_put(struct mapped_device
*md
)
1056 struct dm_table
*map
;
1058 if (atomic_dec_and_test(&md
->holders
)) {
1059 map
= dm_get_table(md
);
1060 if (!dm_suspended(md
)) {
1061 dm_table_presuspend_targets(map
);
1062 dm_table_postsuspend_targets(map
);
1071 * Process the deferred bios
1073 static void __flush_deferred_io(struct mapped_device
*md
, struct bio
*c
)
1086 * Swap in a new table (destroying old one).
1088 int dm_swap_table(struct mapped_device
*md
, struct dm_table
*table
)
1092 down(&md
->suspend_lock
);
1094 /* device must be suspended */
1095 if (!dm_suspended(md
))
1099 r
= __bind(md
, table
);
1102 up(&md
->suspend_lock
);
1107 * Functions to lock and unlock any filesystem running on the
1110 static int lock_fs(struct mapped_device
*md
)
1114 WARN_ON(md
->frozen_sb
);
1116 md
->frozen_sb
= freeze_bdev(md
->suspended_bdev
);
1117 if (IS_ERR(md
->frozen_sb
)) {
1118 r
= PTR_ERR(md
->frozen_sb
);
1119 md
->frozen_sb
= NULL
;
1123 set_bit(DMF_FROZEN
, &md
->flags
);
1125 /* don't bdput right now, we don't want the bdev
1126 * to go away while it is locked.
1131 static void unlock_fs(struct mapped_device
*md
)
1133 if (!test_bit(DMF_FROZEN
, &md
->flags
))
1136 thaw_bdev(md
->suspended_bdev
, md
->frozen_sb
);
1137 md
->frozen_sb
= NULL
;
1138 clear_bit(DMF_FROZEN
, &md
->flags
);
1142 * We need to be able to change a mapping table under a mounted
1143 * filesystem. For example we might want to move some data in
1144 * the background. Before the table can be swapped with
1145 * dm_bind_table, dm_suspend must be called to flush any in
1146 * flight bios and ensure that any further io gets deferred.
1148 int dm_suspend(struct mapped_device
*md
, int do_lockfs
)
1150 struct dm_table
*map
= NULL
;
1151 DECLARE_WAITQUEUE(wait
, current
);
1155 down(&md
->suspend_lock
);
1157 if (dm_suspended(md
))
1160 map
= dm_get_table(md
);
1162 /* This does not get reverted if there's an error later. */
1163 dm_table_presuspend_targets(map
);
1165 md
->suspended_bdev
= bdget_disk(md
->disk
, 0);
1166 if (!md
->suspended_bdev
) {
1167 DMWARN("bdget failed in dm_suspend");
1172 /* Flush I/O to the device. */
1180 * First we set the BLOCK_IO flag so no more ios will be mapped.
1182 down_write(&md
->io_lock
);
1183 set_bit(DMF_BLOCK_IO
, &md
->flags
);
1185 add_wait_queue(&md
->wait
, &wait
);
1186 up_write(&md
->io_lock
);
1190 dm_table_unplug_all(map
);
1193 * Then we wait for the already mapped ios to
1197 set_current_state(TASK_INTERRUPTIBLE
);
1199 if (!atomic_read(&md
->pending
) || signal_pending(current
))
1204 set_current_state(TASK_RUNNING
);
1206 down_write(&md
->io_lock
);
1207 remove_wait_queue(&md
->wait
, &wait
);
1209 /* were we interrupted ? */
1211 if (atomic_read(&md
->pending
)) {
1212 clear_bit(DMF_BLOCK_IO
, &md
->flags
);
1213 def
= bio_list_get(&md
->deferred
);
1214 __flush_deferred_io(md
, def
);
1215 up_write(&md
->io_lock
);
1219 up_write(&md
->io_lock
);
1221 dm_table_postsuspend_targets(map
);
1223 set_bit(DMF_SUSPENDED
, &md
->flags
);
1228 if (r
&& md
->suspended_bdev
) {
1229 bdput(md
->suspended_bdev
);
1230 md
->suspended_bdev
= NULL
;
1234 up(&md
->suspend_lock
);
1238 int dm_resume(struct mapped_device
*md
)
1242 struct dm_table
*map
= NULL
;
1244 down(&md
->suspend_lock
);
1245 if (!dm_suspended(md
))
1248 map
= dm_get_table(md
);
1249 if (!map
|| !dm_table_get_size(map
))
1252 dm_table_resume_targets(map
);
1254 down_write(&md
->io_lock
);
1255 clear_bit(DMF_BLOCK_IO
, &md
->flags
);
1257 def
= bio_list_get(&md
->deferred
);
1258 __flush_deferred_io(md
, def
);
1259 up_write(&md
->io_lock
);
1263 bdput(md
->suspended_bdev
);
1264 md
->suspended_bdev
= NULL
;
1266 clear_bit(DMF_SUSPENDED
, &md
->flags
);
1268 dm_table_unplug_all(map
);
1274 up(&md
->suspend_lock
);
1279 /*-----------------------------------------------------------------
1280 * Event notification.
1281 *---------------------------------------------------------------*/
1282 uint32_t dm_get_event_nr(struct mapped_device
*md
)
1284 return atomic_read(&md
->event_nr
);
1287 int dm_wait_event(struct mapped_device
*md
, int event_nr
)
1289 return wait_event_interruptible(md
->eventq
,
1290 (event_nr
!= atomic_read(&md
->event_nr
)));
1294 * The gendisk is only valid as long as you have a reference
1297 struct gendisk
*dm_disk(struct mapped_device
*md
)
1302 int dm_suspended(struct mapped_device
*md
)
1304 return test_bit(DMF_SUSPENDED
, &md
->flags
);
1307 static struct block_device_operations dm_blk_dops
= {
1308 .open
= dm_blk_open
,
1309 .release
= dm_blk_close
,
1310 .getgeo
= dm_blk_getgeo
,
1311 .owner
= THIS_MODULE
1314 EXPORT_SYMBOL(dm_get_mapinfo
);
1319 module_init(dm_init
);
1320 module_exit(dm_exit
);
1322 module_param(major
, uint
, 0);
1323 MODULE_PARM_DESC(major
, "The major number of the device mapper");
1324 MODULE_DESCRIPTION(DM_NAME
" driver");
1325 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1326 MODULE_LICENSE("GPL");