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"
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.
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
74 * Work processed by per-device workqueue.
80 struct work_struct work
;
81 struct mapped_device
*md
;
85 struct mapped_device
{
86 struct rw_semaphore io_lock
;
87 struct mutex suspend_lock
;
88 spinlock_t pushback_lock
;
95 struct request_queue
*queue
;
102 * A list of ios that arrived while we were suspended.
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.
131 wait_queue_head_t eventq
;
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
;
147 static struct kmem_cache
*_io_cache
;
148 static struct kmem_cache
*_tio_cache
;
150 static int __init
local_init(void)
154 /* allocate a slab for the dm_ios */
155 _io_cache
= KMEM_CACHE(dm_io
, 0);
159 /* allocate a slab for the target ios */
160 _tio_cache
= KMEM_CACHE(dm_target_io
, 0);
162 goto out_free_io_cache
;
164 r
= dm_uevent_init();
166 goto out_free_tio_cache
;
169 r
= register_blkdev(_major
, _name
);
171 goto out_uevent_exit
;
181 kmem_cache_destroy(_tio_cache
);
183 kmem_cache_destroy(_io_cache
);
188 static void local_exit(void)
190 kmem_cache_destroy(_tio_cache
);
191 kmem_cache_destroy(_io_cache
);
192 unregister_blkdev(_major
, _name
);
197 DMINFO("cleaned up");
200 static int (*_inits
[])(void) __initdata
= {
209 static void (*_exits
[])(void) = {
218 static int __init
dm_init(void)
220 const int count
= ARRAY_SIZE(_inits
);
224 for (i
= 0; i
< count
; i
++) {
239 static void __exit
dm_exit(void)
241 int i
= ARRAY_SIZE(_exits
);
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
;
260 if (test_bit(DMF_FREEING
, &md
->flags
) ||
261 test_bit(DMF_DELETING
, &md
->flags
)) {
267 atomic_inc(&md
->open_count
);
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
);
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
)
295 spin_lock(&_minor_lock
);
297 if (dm_open_count(md
))
300 set_bit(DMF_DELETING
, &md
->flags
);
302 spin_unlock(&_minor_lock
);
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
;
322 if (!map
|| !dm_table_get_size(map
))
325 /* We only support devices that have a single target */
326 if (dm_table_get_num_targets(map
) != 1)
329 tgt
= dm_table_get_target(map
, 0);
331 if (dm_suspended(md
)) {
336 if (tgt
->type
->ioctl
)
337 r
= tgt
->type
->ioctl(tgt
, cmd
, arg
);
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
;
370 io
->start_time
= jiffies
;
372 cpu
= part_stat_lock();
373 part_round_stats(cpu
, &dm_disk(md
)->part0
);
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
;
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
);
391 dm_disk(md
)->part0
.in_flight
= pending
=
392 atomic_dec_return(&md
->pending
);
394 /* nudge anyone waiting on suspend queue */
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
);
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
)
426 read_lock(&md
->map_lock
);
430 read_unlock(&md
->map_lock
);
436 * Get the geometry associated with a dm device
438 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
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.");
462 /*-----------------------------------------------------------------
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
)
485 struct mapped_device
*md
= io
->md
;
487 /* Push-back supersedes any I/O errors */
488 if (error
&& !(io
->error
> 0 && __noflush_suspending(md
)))
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
);
502 /* noflush suspend was interrupted. */
504 spin_unlock_irqrestore(&md
->pushback_lock
, flags
);
509 io_error
= io
->error
;
514 if (io_error
!= DM_ENDIO_REQUEUE
) {
515 blk_add_trace_bio(md
->queue
, io
->bio
,
518 bio_endio(bio
, io_error
);
523 static void clone_endio(struct bio
*bio
, int error
)
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
)
535 r
= endio(tio
->ti
, bio
, error
, &tio
->info
);
536 if (r
< 0 || r
== DM_ENDIO_REQUEUE
)
538 * error and requeue request are handled
542 else if (r
== DM_ENDIO_INCOMPLETE
)
543 /* The target will handle the io */
546 DMWARN("unimplemented target endio return value: %d", r
);
552 * Store md for cleanup instead of tio which is about to get freed.
554 bio
->bi_private
= md
->bs
;
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 ?
572 boundary
= ((offset
+ ti
->split_io
) & ~(ti
->split_io
- 1))
581 static void __map_bio(struct dm_target
*ti
, struct bio
*clone
,
582 struct dm_target_io
*tio
)
586 struct mapped_device
*md
;
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
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 */
615 dec_pending(tio
->io
, r
);
617 * Store bio_set for cleanup.
619 clone
->bi_private
= md
->bs
;
623 DMWARN("unimplemented target map return value: %d", r
);
629 struct mapped_device
*md
;
630 struct dm_table
*map
;
634 sector_t sector_count
;
638 static void dm_bio_destructor(struct bio
*bio
)
640 struct bio_set
*bs
= bio
->bi_private
;
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
)
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
;
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
;
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
)
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
;
685 clone
->bi_vcnt
= idx
+ bv_count
;
686 clone
->bi_size
= to_bytes(len
);
687 clone
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
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
))
703 max
= max_io_len(ci
->md
, ci
->sector
, ti
);
706 * Allocate a target io object.
708 tio
= alloc_tio(ci
->md
);
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
,
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.
730 sector_t remaining
= max
;
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
)
743 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
, i
- ci
->idx
, len
,
745 __map_bio(ti
, clone
, tio
);
748 ci
->sector_count
-= len
;
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;
761 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
762 if (!dm_target_is_valid(ti
))
765 max
= max_io_len(ci
->md
, ci
->sector
, ti
);
767 tio
= alloc_tio(ci
->md
);
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
,
779 __map_bio(ti
, clone
, tio
);
782 ci
->sector_count
-= len
;
783 offset
+= to_bytes(len
);
784 } while (remaining
-= len
);
793 * Split the bio into several clones.
795 static int __split_bio(struct mapped_device
*md
, struct bio
*bio
)
797 struct clone_info ci
;
800 ci
.map
= dm_get_table(md
);
801 if (unlikely(!ci
.map
))
806 ci
.io
= alloc_io(md
);
808 atomic_set(&ci
.io
->io_count
, 1);
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
);
825 /*-----------------------------------------------------------------
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
;
842 ti
= dm_table_find_target(map
, bvm
->bi_sector
);
843 if (!dm_target_is_valid(ti
))
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
;
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
);
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
;
877 * The request function that just remaps the bio built up by
880 static int dm_request(struct request_queue
*q
, struct bio
*bio
)
883 int rw
= bio_data_dir(bio
);
884 struct mapped_device
*md
= q
->queuedata
;
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
);
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
));
904 * If we're suspended we have to queue
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
);
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
);
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
);
939 dm_table_unplug_all(map
);
944 static int dm_any_congested(void *congested_data
, int 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
);
955 r
= dm_table_any_congested(map
, bdi_bits
);
960 if (!atomic_dec_return(&md
->pending
))
961 /* nudge anyone waiting on suspend queue */
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
)
986 if (minor
>= (1 << MINORBITS
))
989 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
993 spin_lock(&_minor_lock
);
995 if (idr_find(&_minor_idr
, minor
)) {
1000 r
= idr_get_new_above(&_minor_idr
, MINOR_ALLOCED
, minor
, &m
);
1005 idr_remove(&_minor_idr
, m
);
1011 spin_unlock(&_minor_lock
);
1015 static int next_free_minor(int *minor
)
1019 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
1023 spin_lock(&_minor_lock
);
1025 r
= idr_get_new(&_minor_idr
, MINOR_ALLOCED
, &m
);
1029 if (m
>= (1 << MINORBITS
)) {
1030 idr_remove(&_minor_idr
, m
);
1038 spin_unlock(&_minor_lock
);
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
)
1050 struct mapped_device
*md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
1054 DMWARN("unable to allocate device, out of memory.");
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
);
1065 r
= specific_minor(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
);
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
);
1096 md
->tio_pool
= mempool_create_slab_pool(MIN_IOS
, _tio_cache
);
1100 md
->bs
= bioset_create(16, 16);
1104 md
->disk
= alloc_disk(1);
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
);
1119 format_dev_t(md
->name
, MKDEV(_major
, minor
));
1121 md
->wq
= create_singlethread_workqueue("kdmflush");
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
);
1137 bioset_free(md
->bs
);
1139 mempool_destroy(md
->tio_pool
);
1141 mempool_destroy(md
->io_pool
);
1143 blk_cleanup_queue(md
->queue
);
1147 module_put(THIS_MODULE
);
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
) {
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
);
1170 spin_lock(&_minor_lock
);
1171 md
->disk
->private_data
= NULL
;
1172 spin_unlock(&_minor_lock
);
1175 blk_cleanup_queue(md
->queue
);
1176 module_put(THIS_MODULE
);
1181 * Bind a table to the device.
1183 static void event_callback(void *context
)
1185 unsigned long flags
;
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
;
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
);
1227 dm_table_event_callback(t
, event_callback
, md
);
1229 write_lock(&md
->map_lock
);
1231 dm_table_set_restrictions(t
, q
);
1232 write_unlock(&md
->map_lock
);
1237 static void __unbind(struct mapped_device
*md
)
1239 struct dm_table
*map
= md
->map
;
1244 dm_table_event_callback(map
, NULL
, NULL
);
1245 write_lock(&md
->map_lock
);
1247 write_unlock(&md
->map_lock
);
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
);
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
))
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
))) {
1285 spin_unlock(&_minor_lock
);
1290 struct mapped_device
*dm_get_md(dev_t dev
)
1292 struct mapped_device
*md
= dm_find_md(dev
);
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
)
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
);
1342 EXPORT_SYMBOL_GPL(dm_put
);
1344 static int dm_wait_for_completion(struct mapped_device
*md
)
1349 set_current_state(TASK_INTERRUPTIBLE
);
1352 if (!atomic_read(&md
->pending
))
1355 if (signal_pending(current
)) {
1362 set_current_state(TASK_RUNNING
);
1368 * Process the deferred bios
1370 static void __flush_deferred_io(struct mapped_device
*md
)
1374 while ((c
= bio_list_pop(&md
->deferred
))) {
1375 if (__split_bio(md
, 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
);
1404 DMERR("dm_wq_work: unrecognised work type %d", req
->type
);
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
)
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
)
1435 mutex_lock(&md
->suspend_lock
);
1437 /* device must be suspended */
1438 if (!dm_suspended(md
))
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
))
1447 r
= __bind(md
, table
);
1450 mutex_unlock(&md
->suspend_lock
);
1455 * Functions to lock and unlock any filesystem running on the
1458 static int lock_fs(struct mapped_device
*md
)
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
;
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.
1479 static void unlock_fs(struct mapped_device
*md
)
1481 if (!test_bit(DMF_FROZEN
, &md
->flags
))
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
);
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
)) {
1511 map
= dm_get_table(md
);
1514 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1515 * This flag is cleared before dm_suspend returns.
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 */
1525 md
->suspended_bdev
= bdget_disk(md
->disk
, 0);
1526 if (!md
->suspended_bdev
) {
1527 DMWARN("bdget failed in dm_suspend");
1533 * Flush I/O to the device. noflush supersedes do_lockfs,
1534 * because lock_fs() needs to flush I/Os.
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
);
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
);
1565 __merge_pushback_list(md
);
1566 up_write(&md
->io_lock
);
1568 /* were we interrupted ? */
1570 dm_queue_flush(md
, DM_WQ_FLUSH_DEFERRED
, NULL
);
1573 goto out
; /* pushback list is already flushed, so skip flush */
1576 dm_table_postsuspend_targets(map
);
1578 set_bit(DMF_SUSPENDED
, &md
->flags
);
1581 if (r
&& md
->suspended_bdev
) {
1582 bdput(md
->suspended_bdev
);
1583 md
->suspended_bdev
= NULL
;
1589 mutex_unlock(&md
->suspend_lock
);
1593 int dm_resume(struct mapped_device
*md
)
1596 struct dm_table
*map
= NULL
;
1598 mutex_lock(&md
->suspend_lock
);
1599 if (!dm_suspended(md
))
1602 map
= dm_get_table(md
);
1603 if (!map
|| !dm_table_get_size(map
))
1606 r
= dm_table_resume_targets(map
);
1610 dm_queue_flush(md
, DM_WQ_FLUSH_DEFERRED
, NULL
);
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
);
1629 mutex_unlock(&md
->suspend_lock
);
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
1671 struct gendisk
*dm_disk(struct mapped_device
*md
)
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
);
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
);
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");