Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include "dm.h" |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 9 | #include "dm-uevent.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 13 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 21 | #include <linux/hdreg.h> |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 22 | |
| 23 | #include <trace/events/block.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 25 | #define DM_MSG_PREFIX "core" |
| 26 | |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 27 | /* |
| 28 | * Cookies are numeric values sent with CHANGE and REMOVE |
| 29 | * uevents while resuming, removing or renaming the device. |
| 30 | */ |
| 31 | #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" |
| 32 | #define DM_COOKIE_LENGTH 24 |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | static const char *_name = DM_NAME; |
| 35 | |
| 36 | static unsigned int major = 0; |
| 37 | static unsigned int _major = 0; |
| 38 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 39 | static DEFINE_SPINLOCK(_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 41 | * For bio-based dm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * One of these is allocated per bio. |
| 43 | */ |
| 44 | struct dm_io { |
| 45 | struct mapped_device *md; |
| 46 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | atomic_t io_count; |
Richard Kennedy | 6ae2fa6 | 2008-07-21 12:00:28 +0100 | [diff] [blame] | 48 | struct bio *bio; |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 49 | unsigned long start_time; |
Kiyoshi Ueda | f88fb98 | 2009-10-16 23:18:15 +0100 | [diff] [blame] | 50 | spinlock_t endio_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /* |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 54 | * For bio-based dm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | * One of these is allocated per target within a bio. Hopefully |
| 56 | * this will be simplified out one day. |
| 57 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 58 | struct dm_target_io { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct dm_io *io; |
| 60 | struct dm_target *ti; |
| 61 | union map_info info; |
| 62 | }; |
| 63 | |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 64 | /* |
| 65 | * For request-based dm. |
| 66 | * One of these is allocated per request. |
| 67 | */ |
| 68 | struct dm_rq_target_io { |
| 69 | struct mapped_device *md; |
| 70 | struct dm_target *ti; |
| 71 | struct request *orig, clone; |
| 72 | int error; |
| 73 | union map_info info; |
| 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * For request-based dm. |
| 78 | * One of these is allocated per bio. |
| 79 | */ |
| 80 | struct dm_rq_clone_bio_info { |
| 81 | struct bio *orig; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 82 | struct dm_rq_target_io *tio; |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | union map_info *dm_get_mapinfo(struct bio *bio) |
| 86 | { |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 87 | if (bio && bio->bi_private) |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 88 | return &((struct dm_target_io *)bio->bi_private)->info; |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 89 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 92 | union map_info *dm_get_rq_mapinfo(struct request *rq) |
| 93 | { |
| 94 | if (rq && rq->end_io_data) |
| 95 | return &((struct dm_rq_target_io *)rq->end_io_data)->info; |
| 96 | return NULL; |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo); |
| 99 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 100 | #define MINOR_ALLOCED ((void *)-1) |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Bits for the md->flags field. |
| 104 | */ |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 105 | #define DMF_BLOCK_IO_FOR_SUSPEND 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #define DMF_SUSPENDED 1 |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 107 | #define DMF_FROZEN 2 |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 108 | #define DMF_FREEING 3 |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 109 | #define DMF_DELETING 4 |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 110 | #define DMF_NOFLUSH_SUSPENDING 5 |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 111 | #define DMF_QUEUE_IO_TO_THREAD 6 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 113 | /* |
| 114 | * Work processed by per-device workqueue. |
| 115 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | struct mapped_device { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 117 | struct rw_semaphore io_lock; |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 118 | struct mutex suspend_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | rwlock_t map_lock; |
| 120 | atomic_t holders; |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 121 | atomic_t open_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | unsigned long flags; |
| 124 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 125 | struct request_queue *queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | struct gendisk *disk; |
Mike Anderson | 7e51f25 | 2006-03-27 01:17:52 -0800 | [diff] [blame] | 127 | char name[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | void *interface_ptr; |
| 130 | |
| 131 | /* |
| 132 | * A list of ios that arrived while we were suspended. |
| 133 | */ |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 134 | atomic_t pending[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | wait_queue_head_t wait; |
Mikulas Patocka | 53d5914 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 136 | struct work_struct work; |
Kiyoshi Ueda | 7485936 | 2006-12-08 02:41:02 -0800 | [diff] [blame] | 137 | struct bio_list deferred; |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 138 | spinlock_t deferred_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 141 | * An error from the barrier request currently being processed. |
| 142 | */ |
| 143 | int barrier_error; |
| 144 | |
| 145 | /* |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 146 | * Processing queue (flush/barriers) |
| 147 | */ |
| 148 | struct workqueue_struct *wq; |
| 149 | |
| 150 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * The current mapping. |
| 152 | */ |
| 153 | struct dm_table *map; |
| 154 | |
| 155 | /* |
| 156 | * io objects are allocated from here. |
| 157 | */ |
| 158 | mempool_t *io_pool; |
| 159 | mempool_t *tio_pool; |
| 160 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 161 | struct bio_set *bs; |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* |
| 164 | * Event handling. |
| 165 | */ |
| 166 | atomic_t event_nr; |
| 167 | wait_queue_head_t eventq; |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 168 | atomic_t uevent_seq; |
| 169 | struct list_head uevent_list; |
| 170 | spinlock_t uevent_lock; /* Protect access to uevent_list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * freeze/thaw support require holding onto a super block |
| 174 | */ |
| 175 | struct super_block *frozen_sb; |
Mikulas Patocka | db8fef4 | 2009-06-22 10:12:15 +0100 | [diff] [blame] | 176 | struct block_device *bdev; |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 177 | |
| 178 | /* forced geometry settings */ |
| 179 | struct hd_geometry geometry; |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 180 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 181 | /* For saving the address of __make_request for request based dm */ |
| 182 | make_request_fn *saved_make_request_fn; |
| 183 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 184 | /* sysfs handle */ |
| 185 | struct kobject kobj; |
Mikulas Patocka | 52b1fd5 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 186 | |
| 187 | /* zero-length barrier that will be cloned and submitted to targets */ |
| 188 | struct bio barrier_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 191 | /* |
| 192 | * For mempools pre-allocation at the table loading time. |
| 193 | */ |
| 194 | struct dm_md_mempools { |
| 195 | mempool_t *io_pool; |
| 196 | mempool_t *tio_pool; |
| 197 | struct bio_set *bs; |
| 198 | }; |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | #define MIN_IOS 256 |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 201 | static struct kmem_cache *_io_cache; |
| 202 | static struct kmem_cache *_tio_cache; |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 203 | static struct kmem_cache *_rq_tio_cache; |
| 204 | static struct kmem_cache *_rq_bio_info_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | static int __init local_init(void) |
| 207 | { |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 208 | int r = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* allocate a slab for the dm_ios */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 211 | _io_cache = KMEM_CACHE(dm_io, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (!_io_cache) |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 213 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | /* allocate a slab for the target ios */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 216 | _tio_cache = KMEM_CACHE(dm_target_io, 0); |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 217 | if (!_tio_cache) |
| 218 | goto out_free_io_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 220 | _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); |
| 221 | if (!_rq_tio_cache) |
| 222 | goto out_free_tio_cache; |
| 223 | |
| 224 | _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0); |
| 225 | if (!_rq_bio_info_cache) |
| 226 | goto out_free_rq_tio_cache; |
| 227 | |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 228 | r = dm_uevent_init(); |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 229 | if (r) |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 230 | goto out_free_rq_bio_info_cache; |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | _major = major; |
| 233 | r = register_blkdev(_major, _name); |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 234 | if (r < 0) |
| 235 | goto out_uevent_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | if (!_major) |
| 238 | _major = r; |
| 239 | |
| 240 | return 0; |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 241 | |
| 242 | out_uevent_exit: |
| 243 | dm_uevent_exit(); |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 244 | out_free_rq_bio_info_cache: |
| 245 | kmem_cache_destroy(_rq_bio_info_cache); |
| 246 | out_free_rq_tio_cache: |
| 247 | kmem_cache_destroy(_rq_tio_cache); |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 248 | out_free_tio_cache: |
| 249 | kmem_cache_destroy(_tio_cache); |
| 250 | out_free_io_cache: |
| 251 | kmem_cache_destroy(_io_cache); |
| 252 | |
| 253 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void local_exit(void) |
| 257 | { |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 258 | kmem_cache_destroy(_rq_bio_info_cache); |
| 259 | kmem_cache_destroy(_rq_tio_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | kmem_cache_destroy(_tio_cache); |
| 261 | kmem_cache_destroy(_io_cache); |
Akinobu Mita | 00d5940 | 2007-07-17 04:03:46 -0700 | [diff] [blame] | 262 | unregister_blkdev(_major, _name); |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 263 | dm_uevent_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | _major = 0; |
| 266 | |
| 267 | DMINFO("cleaned up"); |
| 268 | } |
| 269 | |
Alasdair G Kergon | b9249e5 | 2008-02-08 02:09:51 +0000 | [diff] [blame] | 270 | static int (*_inits[])(void) __initdata = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | local_init, |
| 272 | dm_target_init, |
| 273 | dm_linear_init, |
| 274 | dm_stripe_init, |
Mikulas Patocka | 952b355 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 275 | dm_io_init, |
Mikulas Patocka | 945fa4d | 2008-04-24 21:43:49 +0100 | [diff] [blame] | 276 | dm_kcopyd_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | dm_interface_init, |
| 278 | }; |
| 279 | |
Alasdair G Kergon | b9249e5 | 2008-02-08 02:09:51 +0000 | [diff] [blame] | 280 | static void (*_exits[])(void) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | local_exit, |
| 282 | dm_target_exit, |
| 283 | dm_linear_exit, |
| 284 | dm_stripe_exit, |
Mikulas Patocka | 952b355 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 285 | dm_io_exit, |
Mikulas Patocka | 945fa4d | 2008-04-24 21:43:49 +0100 | [diff] [blame] | 286 | dm_kcopyd_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | dm_interface_exit, |
| 288 | }; |
| 289 | |
| 290 | static int __init dm_init(void) |
| 291 | { |
| 292 | const int count = ARRAY_SIZE(_inits); |
| 293 | |
| 294 | int r, i; |
| 295 | |
| 296 | for (i = 0; i < count; i++) { |
| 297 | r = _inits[i](); |
| 298 | if (r) |
| 299 | goto bad; |
| 300 | } |
| 301 | |
| 302 | return 0; |
| 303 | |
| 304 | bad: |
| 305 | while (i--) |
| 306 | _exits[i](); |
| 307 | |
| 308 | return r; |
| 309 | } |
| 310 | |
| 311 | static void __exit dm_exit(void) |
| 312 | { |
| 313 | int i = ARRAY_SIZE(_exits); |
| 314 | |
| 315 | while (i--) |
| 316 | _exits[i](); |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Block device functions |
| 321 | */ |
Al Viro | fe5f9f2 | 2008-03-02 10:29:31 -0500 | [diff] [blame] | 322 | static int dm_blk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | struct mapped_device *md; |
| 325 | |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 326 | spin_lock(&_minor_lock); |
| 327 | |
Al Viro | fe5f9f2 | 2008-03-02 10:29:31 -0500 | [diff] [blame] | 328 | md = bdev->bd_disk->private_data; |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 329 | if (!md) |
| 330 | goto out; |
| 331 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 332 | if (test_bit(DMF_FREEING, &md->flags) || |
| 333 | test_bit(DMF_DELETING, &md->flags)) { |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 334 | md = NULL; |
| 335 | goto out; |
| 336 | } |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | dm_get(md); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 339 | atomic_inc(&md->open_count); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 340 | |
| 341 | out: |
| 342 | spin_unlock(&_minor_lock); |
| 343 | |
| 344 | return md ? 0 : -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Al Viro | fe5f9f2 | 2008-03-02 10:29:31 -0500 | [diff] [blame] | 347 | static int dm_blk_close(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
Al Viro | fe5f9f2 | 2008-03-02 10:29:31 -0500 | [diff] [blame] | 349 | struct mapped_device *md = disk->private_data; |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 350 | atomic_dec(&md->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | dm_put(md); |
| 352 | return 0; |
| 353 | } |
| 354 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 355 | int dm_open_count(struct mapped_device *md) |
| 356 | { |
| 357 | return atomic_read(&md->open_count); |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Guarantees nothing is using the device before it's deleted. |
| 362 | */ |
| 363 | int dm_lock_for_deletion(struct mapped_device *md) |
| 364 | { |
| 365 | int r = 0; |
| 366 | |
| 367 | spin_lock(&_minor_lock); |
| 368 | |
| 369 | if (dm_open_count(md)) |
| 370 | r = -EBUSY; |
| 371 | else |
| 372 | set_bit(DMF_DELETING, &md->flags); |
| 373 | |
| 374 | spin_unlock(&_minor_lock); |
| 375 | |
| 376 | return r; |
| 377 | } |
| 378 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 379 | static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 380 | { |
| 381 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 382 | |
| 383 | return dm_get_geometry(md, geo); |
| 384 | } |
| 385 | |
Al Viro | fe5f9f2 | 2008-03-02 10:29:31 -0500 | [diff] [blame] | 386 | static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 387 | unsigned int cmd, unsigned long arg) |
| 388 | { |
Al Viro | fe5f9f2 | 2008-03-02 10:29:31 -0500 | [diff] [blame] | 389 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 390 | struct dm_table *map = dm_get_table(md); |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 391 | struct dm_target *tgt; |
| 392 | int r = -ENOTTY; |
| 393 | |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 394 | if (!map || !dm_table_get_size(map)) |
| 395 | goto out; |
| 396 | |
| 397 | /* We only support devices that have a single target */ |
| 398 | if (dm_table_get_num_targets(map) != 1) |
| 399 | goto out; |
| 400 | |
| 401 | tgt = dm_table_get_target(map, 0); |
| 402 | |
| 403 | if (dm_suspended(md)) { |
| 404 | r = -EAGAIN; |
| 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | if (tgt->type->ioctl) |
Al Viro | 647b3d0 | 2007-08-28 22:15:59 -0400 | [diff] [blame] | 409 | r = tgt->type->ioctl(tgt, cmd, arg); |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 410 | |
| 411 | out: |
| 412 | dm_table_put(map); |
| 413 | |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 414 | return r; |
| 415 | } |
| 416 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 417 | static struct dm_io *alloc_io(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
| 419 | return mempool_alloc(md->io_pool, GFP_NOIO); |
| 420 | } |
| 421 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 422 | static void free_io(struct mapped_device *md, struct dm_io *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
| 424 | mempool_free(io, md->io_pool); |
| 425 | } |
| 426 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 427 | static void free_tio(struct mapped_device *md, struct dm_target_io *tio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
| 429 | mempool_free(tio, md->tio_pool); |
| 430 | } |
| 431 | |
Kiyoshi Ueda | 0888564 | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 432 | static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, |
| 433 | gfp_t gfp_mask) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 434 | { |
Kiyoshi Ueda | 0888564 | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 435 | return mempool_alloc(md->tio_pool, gfp_mask); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static void free_rq_tio(struct dm_rq_target_io *tio) |
| 439 | { |
| 440 | mempool_free(tio, tio->md->tio_pool); |
| 441 | } |
| 442 | |
| 443 | static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md) |
| 444 | { |
| 445 | return mempool_alloc(md->io_pool, GFP_ATOMIC); |
| 446 | } |
| 447 | |
| 448 | static void free_bio_info(struct dm_rq_clone_bio_info *info) |
| 449 | { |
| 450 | mempool_free(info, info->tio->md->io_pool); |
| 451 | } |
| 452 | |
Kiyoshi Ueda | 90abb8c | 2009-12-10 23:52:13 +0000 | [diff] [blame] | 453 | static int md_in_flight(struct mapped_device *md) |
| 454 | { |
| 455 | return atomic_read(&md->pending[READ]) + |
| 456 | atomic_read(&md->pending[WRITE]); |
| 457 | } |
| 458 | |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 459 | static void start_io_acct(struct dm_io *io) |
| 460 | { |
| 461 | struct mapped_device *md = io->md; |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 462 | int cpu; |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 463 | int rw = bio_data_dir(io->bio); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 464 | |
| 465 | io->start_time = jiffies; |
| 466 | |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 467 | cpu = part_stat_lock(); |
| 468 | part_round_stats(cpu, &dm_disk(md)->part0); |
| 469 | part_stat_unlock(); |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 470 | dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Mikulas Patocka | d221d2e | 2008-11-13 23:39:10 +0000 | [diff] [blame] | 473 | static void end_io_acct(struct dm_io *io) |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 474 | { |
| 475 | struct mapped_device *md = io->md; |
| 476 | struct bio *bio = io->bio; |
| 477 | unsigned long duration = jiffies - io->start_time; |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 478 | int pending, cpu; |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 479 | int rw = bio_data_dir(bio); |
| 480 | |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 481 | cpu = part_stat_lock(); |
| 482 | part_round_stats(cpu, &dm_disk(md)->part0); |
| 483 | part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration); |
| 484 | part_stat_unlock(); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 485 | |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 486 | /* |
| 487 | * After this is decremented the bio must not be touched if it is |
| 488 | * a barrier. |
| 489 | */ |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 490 | dm_disk(md)->part0.in_flight[rw] = pending = |
| 491 | atomic_dec_return(&md->pending[rw]); |
| 492 | pending += atomic_read(&md->pending[rw^0x1]); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 493 | |
Mikulas Patocka | d221d2e | 2008-11-13 23:39:10 +0000 | [diff] [blame] | 494 | /* nudge anyone waiting on suspend queue */ |
| 495 | if (!pending) |
| 496 | wake_up(&md->wait); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | /* |
| 500 | * Add the bio to the list of deferred io. |
| 501 | */ |
Mikulas Patocka | 92c6390 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 502 | static void queue_io(struct mapped_device *md, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 504 | down_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 506 | spin_lock_irq(&md->deferred_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | bio_list_add(&md->deferred, bio); |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 508 | spin_unlock_irq(&md->deferred_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Mikulas Patocka | 92c6390 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 510 | if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) |
| 511 | queue_work(md->wq, &md->work); |
| 512 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 513 | up_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Everyone (including functions in this file), should use this |
| 518 | * function to access the md->map field, and make sure they call |
| 519 | * dm_table_put() when finished. |
| 520 | */ |
| 521 | struct dm_table *dm_get_table(struct mapped_device *md) |
| 522 | { |
| 523 | struct dm_table *t; |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 524 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 526 | read_lock_irqsave(&md->map_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | t = md->map; |
| 528 | if (t) |
| 529 | dm_table_get(t); |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 530 | read_unlock_irqrestore(&md->map_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
| 532 | return t; |
| 533 | } |
| 534 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 535 | /* |
| 536 | * Get the geometry associated with a dm device |
| 537 | */ |
| 538 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) |
| 539 | { |
| 540 | *geo = md->geometry; |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Set the geometry of a device. |
| 547 | */ |
| 548 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) |
| 549 | { |
| 550 | sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; |
| 551 | |
| 552 | if (geo->start > sz) { |
| 553 | DMWARN("Start sector is beyond the geometry limits."); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
| 557 | md->geometry = *geo; |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /*----------------------------------------------------------------- |
| 563 | * CRUD START: |
| 564 | * A more elegant soln is in the works that uses the queue |
| 565 | * merge fn, unfortunately there are a couple of changes to |
| 566 | * the block layer that I want to make for this. So in the |
| 567 | * interests of getting something for people to use I give |
| 568 | * you this clearly demarcated crap. |
| 569 | *---------------------------------------------------------------*/ |
| 570 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 571 | static int __noflush_suspending(struct mapped_device *md) |
| 572 | { |
| 573 | return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
| 574 | } |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | /* |
| 577 | * Decrements the number of outstanding ios that a bio has been |
| 578 | * cloned into, completing the original io if necc. |
| 579 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 580 | static void dec_pending(struct dm_io *io, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 582 | unsigned long flags; |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 583 | int io_error; |
| 584 | struct bio *bio; |
| 585 | struct mapped_device *md = io->md; |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 586 | |
| 587 | /* Push-back supersedes any I/O errors */ |
Kiyoshi Ueda | f88fb98 | 2009-10-16 23:18:15 +0100 | [diff] [blame] | 588 | if (unlikely(error)) { |
| 589 | spin_lock_irqsave(&io->endio_lock, flags); |
| 590 | if (!(io->error > 0 && __noflush_suspending(md))) |
| 591 | io->error = error; |
| 592 | spin_unlock_irqrestore(&io->endio_lock, flags); |
| 593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
| 595 | if (atomic_dec_and_test(&io->io_count)) { |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 596 | if (io->error == DM_ENDIO_REQUEUE) { |
| 597 | /* |
| 598 | * Target requested pushing back the I/O. |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 599 | */ |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 600 | spin_lock_irqsave(&md->deferred_lock, flags); |
Mikulas Patocka | 2761e95 | 2009-06-22 10:12:18 +0100 | [diff] [blame] | 601 | if (__noflush_suspending(md)) { |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 602 | if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER)) |
Mikulas Patocka | 2761e95 | 2009-06-22 10:12:18 +0100 | [diff] [blame] | 603 | bio_list_add_head(&md->deferred, |
| 604 | io->bio); |
| 605 | } else |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 606 | /* noflush suspend was interrupted. */ |
| 607 | io->error = -EIO; |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 608 | spin_unlock_irqrestore(&md->deferred_lock, flags); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 611 | io_error = io->error; |
| 612 | bio = io->bio; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 613 | |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 614 | if (bio_rw_flagged(bio, BIO_RW_BARRIER)) { |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 615 | /* |
| 616 | * There can be just one barrier request so we use |
| 617 | * a per-device variable for error reporting. |
| 618 | * Note that you can't touch the bio after end_io_acct |
| 619 | */ |
Mikulas Patocka | fdb9572 | 2009-06-22 10:12:19 +0100 | [diff] [blame] | 620 | if (!md->barrier_error && io_error != -EOPNOTSUPP) |
Mikulas Patocka | 5aa2781 | 2009-06-22 10:12:18 +0100 | [diff] [blame] | 621 | md->barrier_error = io_error; |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 622 | end_io_acct(io); |
| 623 | } else { |
| 624 | end_io_acct(io); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 625 | |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 626 | if (io_error != DM_ENDIO_REQUEUE) { |
| 627 | trace_block_bio_complete(md->queue, bio); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 628 | |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 629 | bio_endio(bio, io_error); |
| 630 | } |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 631 | } |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 632 | |
| 633 | free_io(md, io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 637 | static void clone_endio(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
| 639 | int r = 0; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 640 | struct dm_target_io *tio = bio->bi_private; |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 641 | struct dm_io *io = tio->io; |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 642 | struct mapped_device *md = tio->io->md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | dm_endio_fn endio = tio->ti->type->end_io; |
| 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (!bio_flagged(bio, BIO_UPTODATE) && !error) |
| 646 | error = -EIO; |
| 647 | |
| 648 | if (endio) { |
| 649 | r = endio(tio->ti, bio, error, &tio->info); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 650 | if (r < 0 || r == DM_ENDIO_REQUEUE) |
| 651 | /* |
| 652 | * error and requeue request are handled |
| 653 | * in dec_pending(). |
| 654 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | error = r; |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 656 | else if (r == DM_ENDIO_INCOMPLETE) |
| 657 | /* The target will handle the io */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 658 | return; |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 659 | else if (r) { |
| 660 | DMWARN("unimplemented target endio return value: %d", r); |
| 661 | BUG(); |
| 662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 665 | /* |
| 666 | * Store md for cleanup instead of tio which is about to get freed. |
| 667 | */ |
| 668 | bio->bi_private = md->bs; |
| 669 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 670 | free_tio(md, tio); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 671 | bio_put(bio); |
| 672 | dec_pending(io, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 675 | /* |
| 676 | * Partial completion handling for request-based dm |
| 677 | */ |
| 678 | static void end_clone_bio(struct bio *clone, int error) |
| 679 | { |
| 680 | struct dm_rq_clone_bio_info *info = clone->bi_private; |
| 681 | struct dm_rq_target_io *tio = info->tio; |
| 682 | struct bio *bio = info->orig; |
| 683 | unsigned int nr_bytes = info->orig->bi_size; |
| 684 | |
| 685 | bio_put(clone); |
| 686 | |
| 687 | if (tio->error) |
| 688 | /* |
| 689 | * An error has already been detected on the request. |
| 690 | * Once error occurred, just let clone->end_io() handle |
| 691 | * the remainder. |
| 692 | */ |
| 693 | return; |
| 694 | else if (error) { |
| 695 | /* |
| 696 | * Don't notice the error to the upper layer yet. |
| 697 | * The error handling decision is made by the target driver, |
| 698 | * when the request is completed. |
| 699 | */ |
| 700 | tio->error = error; |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * I/O for the bio successfully completed. |
| 706 | * Notice the data completion to the upper layer. |
| 707 | */ |
| 708 | |
| 709 | /* |
| 710 | * bios are processed from the head of the list. |
| 711 | * So the completing bio should always be rq->bio. |
| 712 | * If it's not, something wrong is happening. |
| 713 | */ |
| 714 | if (tio->orig->bio != bio) |
| 715 | DMERR("bio completion is going in the middle of the request"); |
| 716 | |
| 717 | /* |
| 718 | * Update the original request. |
| 719 | * Do not use blk_end_request() here, because it may complete |
| 720 | * the original request before the clone, and break the ordering. |
| 721 | */ |
| 722 | blk_update_request(tio->orig, 0, nr_bytes); |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * Don't touch any member of the md after calling this function because |
| 727 | * the md may be freed in dm_put() at the end of this function. |
| 728 | * Or do dm_get() before calling this function and dm_put() later. |
| 729 | */ |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 730 | static void rq_completed(struct mapped_device *md, int rw, int run_queue) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 731 | { |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 732 | atomic_dec(&md->pending[rw]); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 733 | |
| 734 | /* nudge anyone waiting on suspend queue */ |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 735 | if (!md_in_flight(md)) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 736 | wake_up(&md->wait); |
| 737 | |
| 738 | if (run_queue) |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 739 | blk_run_queue(md->queue); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | * dm_put() must be at the end of this function. See the comment above |
| 743 | */ |
| 744 | dm_put(md); |
| 745 | } |
| 746 | |
Kiyoshi Ueda | a77e28c | 2009-09-04 20:40:16 +0100 | [diff] [blame] | 747 | static void free_rq_clone(struct request *clone) |
| 748 | { |
| 749 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 750 | |
| 751 | blk_rq_unprep_clone(clone); |
| 752 | free_rq_tio(tio); |
| 753 | } |
| 754 | |
Kiyoshi Ueda | 980691e | 2009-12-10 23:52:17 +0000 | [diff] [blame^] | 755 | /* |
| 756 | * Complete the clone and the original request. |
| 757 | * Must be called without queue lock. |
| 758 | */ |
| 759 | static void dm_end_request(struct request *clone, int error) |
| 760 | { |
| 761 | int rw = rq_data_dir(clone); |
| 762 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 763 | struct mapped_device *md = tio->md; |
| 764 | struct request *rq = tio->orig; |
| 765 | |
| 766 | if (blk_pc_request(rq)) { |
| 767 | rq->errors = clone->errors; |
| 768 | rq->resid_len = clone->resid_len; |
| 769 | |
| 770 | if (rq->sense) |
| 771 | /* |
| 772 | * We are using the sense buffer of the original |
| 773 | * request. |
| 774 | * So setting the length of the sense data is enough. |
| 775 | */ |
| 776 | rq->sense_len = clone->sense_len; |
| 777 | } |
| 778 | |
| 779 | free_rq_clone(clone); |
| 780 | |
| 781 | blk_end_request_all(rq, error); |
| 782 | |
| 783 | rq_completed(md, rw, 1); |
| 784 | } |
| 785 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 786 | static void dm_unprep_request(struct request *rq) |
| 787 | { |
| 788 | struct request *clone = rq->special; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 789 | |
| 790 | rq->special = NULL; |
| 791 | rq->cmd_flags &= ~REQ_DONTPREP; |
| 792 | |
Kiyoshi Ueda | a77e28c | 2009-09-04 20:40:16 +0100 | [diff] [blame] | 793 | free_rq_clone(clone); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Requeue the original request of a clone. |
| 798 | */ |
| 799 | void dm_requeue_unmapped_request(struct request *clone) |
| 800 | { |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 801 | int rw = rq_data_dir(clone); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 802 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 803 | struct mapped_device *md = tio->md; |
| 804 | struct request *rq = tio->orig; |
| 805 | struct request_queue *q = rq->q; |
| 806 | unsigned long flags; |
| 807 | |
| 808 | dm_unprep_request(rq); |
| 809 | |
| 810 | spin_lock_irqsave(q->queue_lock, flags); |
| 811 | if (elv_queue_empty(q)) |
| 812 | blk_plug_device(q); |
| 813 | blk_requeue_request(q, rq); |
| 814 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 815 | |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 816 | rq_completed(md, rw, 0); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 817 | } |
| 818 | EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request); |
| 819 | |
| 820 | static void __stop_queue(struct request_queue *q) |
| 821 | { |
| 822 | blk_stop_queue(q); |
| 823 | } |
| 824 | |
| 825 | static void stop_queue(struct request_queue *q) |
| 826 | { |
| 827 | unsigned long flags; |
| 828 | |
| 829 | spin_lock_irqsave(q->queue_lock, flags); |
| 830 | __stop_queue(q); |
| 831 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 832 | } |
| 833 | |
| 834 | static void __start_queue(struct request_queue *q) |
| 835 | { |
| 836 | if (blk_queue_stopped(q)) |
| 837 | blk_start_queue(q); |
| 838 | } |
| 839 | |
| 840 | static void start_queue(struct request_queue *q) |
| 841 | { |
| 842 | unsigned long flags; |
| 843 | |
| 844 | spin_lock_irqsave(q->queue_lock, flags); |
| 845 | __start_queue(q); |
| 846 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 847 | } |
| 848 | |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 849 | static void dm_done(struct request *clone, int error, bool mapped) |
| 850 | { |
| 851 | int r = error; |
| 852 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 853 | dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io; |
| 854 | |
| 855 | if (mapped && rq_end_io) |
| 856 | r = rq_end_io(tio->ti, clone, error, &tio->info); |
| 857 | |
| 858 | if (r <= 0) |
| 859 | /* The target wants to complete the I/O */ |
| 860 | dm_end_request(clone, r); |
| 861 | else if (r == DM_ENDIO_INCOMPLETE) |
| 862 | /* The target will handle the I/O */ |
| 863 | return; |
| 864 | else if (r == DM_ENDIO_REQUEUE) |
| 865 | /* The target wants to requeue the I/O */ |
| 866 | dm_requeue_unmapped_request(clone); |
| 867 | else { |
| 868 | DMWARN("unimplemented target endio return value: %d", r); |
| 869 | BUG(); |
| 870 | } |
| 871 | } |
| 872 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 873 | /* |
| 874 | * Request completion handler for request-based dm |
| 875 | */ |
| 876 | static void dm_softirq_done(struct request *rq) |
| 877 | { |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 878 | bool mapped = true; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 879 | struct request *clone = rq->completion_data; |
| 880 | struct dm_rq_target_io *tio = clone->end_io_data; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 881 | |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 882 | if (rq->cmd_flags & REQ_FAILED) |
| 883 | mapped = false; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 884 | |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 885 | dm_done(clone, tio->error, mapped); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Complete the clone and the original request with the error status |
| 890 | * through softirq context. |
| 891 | */ |
| 892 | static void dm_complete_request(struct request *clone, int error) |
| 893 | { |
| 894 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 895 | struct request *rq = tio->orig; |
| 896 | |
| 897 | tio->error = error; |
| 898 | rq->completion_data = clone; |
| 899 | blk_complete_request(rq); |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * Complete the not-mapped clone and the original request with the error status |
| 904 | * through softirq context. |
| 905 | * Target's rq_end_io() function isn't called. |
| 906 | * This may be used when the target's map_rq() function fails. |
| 907 | */ |
| 908 | void dm_kill_unmapped_request(struct request *clone, int error) |
| 909 | { |
| 910 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 911 | struct request *rq = tio->orig; |
| 912 | |
| 913 | rq->cmd_flags |= REQ_FAILED; |
| 914 | dm_complete_request(clone, error); |
| 915 | } |
| 916 | EXPORT_SYMBOL_GPL(dm_kill_unmapped_request); |
| 917 | |
| 918 | /* |
| 919 | * Called with the queue lock held |
| 920 | */ |
| 921 | static void end_clone_request(struct request *clone, int error) |
| 922 | { |
| 923 | /* |
| 924 | * For just cleaning up the information of the queue in which |
| 925 | * the clone was dispatched. |
| 926 | * The clone is *NOT* freed actually here because it is alloced from |
| 927 | * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags. |
| 928 | */ |
| 929 | __blk_put_request(clone->q, clone); |
| 930 | |
| 931 | /* |
| 932 | * Actual request completion is done in a softirq context which doesn't |
| 933 | * hold the queue lock. Otherwise, deadlock could occur because: |
| 934 | * - another request may be submitted by the upper level driver |
| 935 | * of the stacking during the completion |
| 936 | * - the submission which requires queue lock may be done |
| 937 | * against this queue |
| 938 | */ |
| 939 | dm_complete_request(clone, error); |
| 940 | } |
| 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | static sector_t max_io_len(struct mapped_device *md, |
| 943 | sector_t sector, struct dm_target *ti) |
| 944 | { |
| 945 | sector_t offset = sector - ti->begin; |
| 946 | sector_t len = ti->len - offset; |
| 947 | |
| 948 | /* |
| 949 | * Does the target need to split even further ? |
| 950 | */ |
| 951 | if (ti->split_io) { |
| 952 | sector_t boundary; |
| 953 | boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) |
| 954 | - offset; |
| 955 | if (len > boundary) |
| 956 | len = boundary; |
| 957 | } |
| 958 | |
| 959 | return len; |
| 960 | } |
| 961 | |
| 962 | static void __map_bio(struct dm_target *ti, struct bio *clone, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 963 | struct dm_target_io *tio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | { |
| 965 | int r; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 966 | sector_t sector; |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 967 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | clone->bi_end_io = clone_endio; |
| 970 | clone->bi_private = tio; |
| 971 | |
| 972 | /* |
| 973 | * Map the clone. If r == 0 we don't need to do |
| 974 | * anything, the target has assumed ownership of |
| 975 | * this io. |
| 976 | */ |
| 977 | atomic_inc(&tio->io->io_count); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 978 | sector = clone->bi_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | r = ti->type->map(ti, clone, &tio->info); |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 980 | if (r == DM_MAPIO_REMAPPED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | /* the bio has been remapped so dispatch it */ |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 982 | |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 983 | trace_block_remap(bdev_get_queue(clone->bi_bdev), clone, |
Alan D. Brunelle | 22a7c31 | 2009-05-04 16:35:08 -0400 | [diff] [blame] | 984 | tio->io->bio->bi_bdev->bd_dev, sector); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | generic_make_request(clone); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 987 | } else if (r < 0 || r == DM_MAPIO_REQUEUE) { |
| 988 | /* error the io and bail out, or requeue it if needed */ |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 989 | md = tio->io->md; |
| 990 | dec_pending(tio->io, r); |
| 991 | /* |
| 992 | * Store bio_set for cleanup. |
| 993 | */ |
| 994 | clone->bi_private = md->bs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | bio_put(clone); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 996 | free_tio(md, tio); |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 997 | } else if (r) { |
| 998 | DMWARN("unimplemented target map return value: %d", r); |
| 999 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | struct clone_info { |
| 1004 | struct mapped_device *md; |
| 1005 | struct dm_table *map; |
| 1006 | struct bio *bio; |
| 1007 | struct dm_io *io; |
| 1008 | sector_t sector; |
| 1009 | sector_t sector_count; |
| 1010 | unsigned short idx; |
| 1011 | }; |
| 1012 | |
Peter Osterlund | 3676347 | 2005-09-06 15:16:42 -0700 | [diff] [blame] | 1013 | static void dm_bio_destructor(struct bio *bio) |
| 1014 | { |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1015 | struct bio_set *bs = bio->bi_private; |
| 1016 | |
| 1017 | bio_free(bio, bs); |
Peter Osterlund | 3676347 | 2005-09-06 15:16:42 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * Creates a little bio that is just does part of a bvec. |
| 1022 | */ |
| 1023 | static struct bio *split_bvec(struct bio *bio, sector_t sector, |
| 1024 | unsigned short idx, unsigned int offset, |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1025 | unsigned int len, struct bio_set *bs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | { |
| 1027 | struct bio *clone; |
| 1028 | struct bio_vec *bv = bio->bi_io_vec + idx; |
| 1029 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1030 | clone = bio_alloc_bioset(GFP_NOIO, 1, bs); |
Peter Osterlund | 3676347 | 2005-09-06 15:16:42 -0700 | [diff] [blame] | 1031 | clone->bi_destructor = dm_bio_destructor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | *clone->bi_io_vec = *bv; |
| 1033 | |
| 1034 | clone->bi_sector = sector; |
| 1035 | clone->bi_bdev = bio->bi_bdev; |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 1036 | clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | clone->bi_vcnt = 1; |
| 1038 | clone->bi_size = to_bytes(len); |
| 1039 | clone->bi_io_vec->bv_offset = offset; |
| 1040 | clone->bi_io_vec->bv_len = clone->bi_size; |
Martin K. Petersen | f3e1d26 | 2008-10-21 17:45:04 +0100 | [diff] [blame] | 1041 | clone->bi_flags |= 1 << BIO_CLONED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 1043 | if (bio_integrity(bio)) { |
Martin K. Petersen | 7878cba | 2009-06-26 15:37:49 +0200 | [diff] [blame] | 1044 | bio_integrity_clone(clone, bio, GFP_NOIO, bs); |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 1045 | bio_integrity_trim(clone, |
| 1046 | bio_sector_offset(bio, idx, offset), len); |
| 1047 | } |
| 1048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | return clone; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * Creates a bio that consists of range of complete bvecs. |
| 1054 | */ |
| 1055 | static struct bio *clone_bio(struct bio *bio, sector_t sector, |
| 1056 | unsigned short idx, unsigned short bv_count, |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1057 | unsigned int len, struct bio_set *bs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | { |
| 1059 | struct bio *clone; |
| 1060 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1061 | clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); |
| 1062 | __bio_clone(clone, bio); |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 1063 | clone->bi_rw &= ~(1 << BIO_RW_BARRIER); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1064 | clone->bi_destructor = dm_bio_destructor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | clone->bi_sector = sector; |
| 1066 | clone->bi_idx = idx; |
| 1067 | clone->bi_vcnt = idx + bv_count; |
| 1068 | clone->bi_size = to_bytes(len); |
| 1069 | clone->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 1070 | |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 1071 | if (bio_integrity(bio)) { |
Martin K. Petersen | 7878cba | 2009-06-26 15:37:49 +0200 | [diff] [blame] | 1072 | bio_integrity_clone(clone, bio, GFP_NOIO, bs); |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 1073 | |
| 1074 | if (idx != bio->bi_idx || clone->bi_size < bio->bi_size) |
| 1075 | bio_integrity_trim(clone, |
| 1076 | bio_sector_offset(bio, idx, 0), len); |
| 1077 | } |
| 1078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | return clone; |
| 1080 | } |
| 1081 | |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1082 | static struct dm_target_io *alloc_tio(struct clone_info *ci, |
| 1083 | struct dm_target *ti) |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1084 | { |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1085 | struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO); |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1086 | |
| 1087 | tio->io = ci->io; |
| 1088 | tio->ti = ti; |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1089 | memset(&tio->info, 0, sizeof(tio->info)); |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1090 | |
| 1091 | return tio; |
| 1092 | } |
| 1093 | |
| 1094 | static void __flush_target(struct clone_info *ci, struct dm_target *ti, |
| 1095 | unsigned flush_nr) |
| 1096 | { |
| 1097 | struct dm_target_io *tio = alloc_tio(ci, ti); |
| 1098 | struct bio *clone; |
| 1099 | |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1100 | tio->info.flush_request = flush_nr; |
| 1101 | |
| 1102 | clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs); |
| 1103 | __bio_clone(clone, ci->bio); |
| 1104 | clone->bi_destructor = dm_bio_destructor; |
| 1105 | |
| 1106 | __map_bio(ti, clone, tio); |
| 1107 | } |
| 1108 | |
| 1109 | static int __clone_and_map_empty_barrier(struct clone_info *ci) |
| 1110 | { |
| 1111 | unsigned target_nr = 0, flush_nr; |
| 1112 | struct dm_target *ti; |
| 1113 | |
| 1114 | while ((ti = dm_table_get_target(ci->map, target_nr++))) |
| 1115 | for (flush_nr = 0; flush_nr < ti->num_flush_requests; |
| 1116 | flush_nr++) |
| 1117 | __flush_target(ci, ti, flush_nr); |
| 1118 | |
| 1119 | ci->sector_count = 0; |
| 1120 | |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1124 | static int __clone_and_map(struct clone_info *ci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | { |
| 1126 | struct bio *clone, *bio = ci->bio; |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1127 | struct dm_target *ti; |
| 1128 | sector_t len = 0, max; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1129 | struct dm_target_io *tio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1131 | if (unlikely(bio_empty_barrier(bio))) |
| 1132 | return __clone_and_map_empty_barrier(ci); |
| 1133 | |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1134 | ti = dm_table_find_target(ci->map, ci->sector); |
| 1135 | if (!dm_target_is_valid(ti)) |
| 1136 | return -EIO; |
| 1137 | |
| 1138 | max = max_io_len(ci->md, ci->sector, ti); |
| 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | /* |
| 1141 | * Allocate a target io object. |
| 1142 | */ |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1143 | tio = alloc_tio(ci, ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
| 1145 | if (ci->sector_count <= max) { |
| 1146 | /* |
| 1147 | * Optimise for the simple case where we can do all of |
| 1148 | * the remaining io with a single clone. |
| 1149 | */ |
| 1150 | clone = clone_bio(bio, ci->sector, ci->idx, |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1151 | bio->bi_vcnt - ci->idx, ci->sector_count, |
| 1152 | ci->md->bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | __map_bio(ti, clone, tio); |
| 1154 | ci->sector_count = 0; |
| 1155 | |
| 1156 | } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { |
| 1157 | /* |
| 1158 | * There are some bvecs that don't span targets. |
| 1159 | * Do as many of these as possible. |
| 1160 | */ |
| 1161 | int i; |
| 1162 | sector_t remaining = max; |
| 1163 | sector_t bv_len; |
| 1164 | |
| 1165 | for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { |
| 1166 | bv_len = to_sector(bio->bi_io_vec[i].bv_len); |
| 1167 | |
| 1168 | if (bv_len > remaining) |
| 1169 | break; |
| 1170 | |
| 1171 | remaining -= bv_len; |
| 1172 | len += bv_len; |
| 1173 | } |
| 1174 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1175 | clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, |
| 1176 | ci->md->bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | __map_bio(ti, clone, tio); |
| 1178 | |
| 1179 | ci->sector += len; |
| 1180 | ci->sector_count -= len; |
| 1181 | ci->idx = i; |
| 1182 | |
| 1183 | } else { |
| 1184 | /* |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 1185 | * Handle a bvec that must be split between two or more targets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | */ |
| 1187 | struct bio_vec *bv = bio->bi_io_vec + ci->idx; |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 1188 | sector_t remaining = to_sector(bv->bv_len); |
| 1189 | unsigned int offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 1191 | do { |
| 1192 | if (offset) { |
| 1193 | ti = dm_table_find_target(ci->map, ci->sector); |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1194 | if (!dm_target_is_valid(ti)) |
| 1195 | return -EIO; |
| 1196 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 1197 | max = max_io_len(ci->md, ci->sector, ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1199 | tio = alloc_tio(ci, ti); |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 1200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 1202 | len = min(remaining, max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 1204 | clone = split_bvec(bio, ci->sector, ci->idx, |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1205 | bv->bv_offset + offset, len, |
| 1206 | ci->md->bs); |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 1207 | |
| 1208 | __map_bio(ti, clone, tio); |
| 1209 | |
| 1210 | ci->sector += len; |
| 1211 | ci->sector_count -= len; |
| 1212 | offset += to_bytes(len); |
| 1213 | } while (remaining -= len); |
| 1214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | ci->idx++; |
| 1216 | } |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1217 | |
| 1218 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /* |
Mikulas Patocka | 8a53c28 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 1222 | * Split the bio into several clones and submit it to targets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | */ |
Mikulas Patocka | f0b9a45 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 1224 | static void __split_and_process_bio(struct mapped_device *md, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | { |
| 1226 | struct clone_info ci; |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1227 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
| 1229 | ci.map = dm_get_table(md); |
Mikulas Patocka | f0b9a45 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 1230 | if (unlikely(!ci.map)) { |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 1231 | if (!bio_rw_flagged(bio, BIO_RW_BARRIER)) |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 1232 | bio_io_error(bio); |
| 1233 | else |
Mikulas Patocka | 5aa2781 | 2009-06-22 10:12:18 +0100 | [diff] [blame] | 1234 | if (!md->barrier_error) |
| 1235 | md->barrier_error = -EIO; |
Mikulas Patocka | f0b9a45 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 1236 | return; |
| 1237 | } |
Mikulas Patocka | 692d0eb | 2009-04-09 00:27:13 +0100 | [diff] [blame] | 1238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | ci.md = md; |
| 1240 | ci.bio = bio; |
| 1241 | ci.io = alloc_io(md); |
| 1242 | ci.io->error = 0; |
| 1243 | atomic_set(&ci.io->io_count, 1); |
| 1244 | ci.io->bio = bio; |
| 1245 | ci.io->md = md; |
Kiyoshi Ueda | f88fb98 | 2009-10-16 23:18:15 +0100 | [diff] [blame] | 1246 | spin_lock_init(&ci.io->endio_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | ci.sector = bio->bi_sector; |
| 1248 | ci.sector_count = bio_sectors(bio); |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1249 | if (unlikely(bio_empty_barrier(bio))) |
| 1250 | ci.sector_count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | ci.idx = bio->bi_idx; |
| 1252 | |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 1253 | start_io_acct(ci.io); |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1254 | while (ci.sector_count && !error) |
| 1255 | error = __clone_and_map(&ci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
| 1257 | /* drop the extra reference count */ |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1258 | dec_pending(ci.io, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | dm_table_put(ci.map); |
| 1260 | } |
| 1261 | /*----------------------------------------------------------------- |
| 1262 | * CRUD END |
| 1263 | *---------------------------------------------------------------*/ |
| 1264 | |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 1265 | static int dm_merge_bvec(struct request_queue *q, |
| 1266 | struct bvec_merge_data *bvm, |
| 1267 | struct bio_vec *biovec) |
| 1268 | { |
| 1269 | struct mapped_device *md = q->queuedata; |
| 1270 | struct dm_table *map = dm_get_table(md); |
| 1271 | struct dm_target *ti; |
| 1272 | sector_t max_sectors; |
Mikulas Patocka | 5037108 | 2008-10-01 14:39:17 +0100 | [diff] [blame] | 1273 | int max_size = 0; |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 1274 | |
| 1275 | if (unlikely(!map)) |
Mikulas Patocka | 5037108 | 2008-10-01 14:39:17 +0100 | [diff] [blame] | 1276 | goto out; |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 1277 | |
| 1278 | ti = dm_table_find_target(map, bvm->bi_sector); |
Mikulas Patocka | b01cd5a | 2008-10-01 14:39:24 +0100 | [diff] [blame] | 1279 | if (!dm_target_is_valid(ti)) |
| 1280 | goto out_table; |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 1281 | |
| 1282 | /* |
| 1283 | * Find maximum amount of I/O that won't need splitting |
| 1284 | */ |
| 1285 | max_sectors = min(max_io_len(md, bvm->bi_sector, ti), |
| 1286 | (sector_t) BIO_MAX_SECTORS); |
| 1287 | max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; |
| 1288 | if (max_size < 0) |
| 1289 | max_size = 0; |
| 1290 | |
| 1291 | /* |
| 1292 | * merge_bvec_fn() returns number of bytes |
| 1293 | * it can accept at this offset |
| 1294 | * max is precomputed maximal io size |
| 1295 | */ |
| 1296 | if (max_size && ti->type->merge) |
| 1297 | max_size = ti->type->merge(ti, bvm, biovec, max_size); |
Mikulas Patocka | 8cbeb67 | 2009-06-22 10:12:14 +0100 | [diff] [blame] | 1298 | /* |
| 1299 | * If the target doesn't support merge method and some of the devices |
| 1300 | * provided their merge_bvec method (we know this by looking at |
| 1301 | * queue_max_hw_sectors), then we can't allow bios with multiple vector |
| 1302 | * entries. So always set max_size to 0, and the code below allows |
| 1303 | * just one page. |
| 1304 | */ |
| 1305 | else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9) |
| 1306 | |
| 1307 | max_size = 0; |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 1308 | |
Mikulas Patocka | b01cd5a | 2008-10-01 14:39:24 +0100 | [diff] [blame] | 1309 | out_table: |
Mikulas Patocka | 5037108 | 2008-10-01 14:39:17 +0100 | [diff] [blame] | 1310 | dm_table_put(map); |
| 1311 | |
| 1312 | out: |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 1313 | /* |
| 1314 | * Always allow an entire first page |
| 1315 | */ |
| 1316 | if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) |
| 1317 | max_size = biovec->bv_len; |
| 1318 | |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 1319 | return max_size; |
| 1320 | } |
| 1321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | /* |
| 1323 | * The request function that just remaps the bio built up by |
| 1324 | * dm_merge_bvec. |
| 1325 | */ |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1326 | static int _dm_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | { |
Kevin Corry | 12f03a4 | 2006-02-01 03:04:52 -0800 | [diff] [blame] | 1328 | int rw = bio_data_dir(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | struct mapped_device *md = q->queuedata; |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 1330 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1332 | down_read(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 1334 | cpu = part_stat_lock(); |
| 1335 | part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]); |
| 1336 | part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio)); |
| 1337 | part_stat_unlock(); |
Kevin Corry | 12f03a4 | 2006-02-01 03:04:52 -0800 | [diff] [blame] | 1338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | /* |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 1340 | * If we're suspended or the thread is processing barriers |
| 1341 | * we have to queue this io for later. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | */ |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 1343 | if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) || |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 1344 | unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1345 | up_read(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
Alasdair G Kergon | 54d9a1b | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 1347 | if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) && |
| 1348 | bio_rw(bio) == READA) { |
| 1349 | bio_io_error(bio); |
| 1350 | return 0; |
| 1351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
Mikulas Patocka | 92c6390 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 1353 | queue_io(md, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
Mikulas Patocka | 92c6390 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 1355 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Mikulas Patocka | f0b9a45 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 1358 | __split_and_process_bio(md, bio); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1359 | up_read(&md->io_lock); |
Mikulas Patocka | f0b9a45 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 1360 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1363 | static int dm_make_request(struct request_queue *q, struct bio *bio) |
| 1364 | { |
| 1365 | struct mapped_device *md = q->queuedata; |
| 1366 | |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 1367 | if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1368 | bio_endio(bio, -EOPNOTSUPP); |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | return md->saved_make_request_fn(q, bio); /* call __make_request() */ |
| 1373 | } |
| 1374 | |
| 1375 | static int dm_request_based(struct mapped_device *md) |
| 1376 | { |
| 1377 | return blk_queue_stackable(md->queue); |
| 1378 | } |
| 1379 | |
| 1380 | static int dm_request(struct request_queue *q, struct bio *bio) |
| 1381 | { |
| 1382 | struct mapped_device *md = q->queuedata; |
| 1383 | |
| 1384 | if (dm_request_based(md)) |
| 1385 | return dm_make_request(q, bio); |
| 1386 | |
| 1387 | return _dm_request(q, bio); |
| 1388 | } |
| 1389 | |
| 1390 | void dm_dispatch_request(struct request *rq) |
| 1391 | { |
| 1392 | int r; |
| 1393 | |
| 1394 | if (blk_queue_io_stat(rq->q)) |
| 1395 | rq->cmd_flags |= REQ_IO_STAT; |
| 1396 | |
| 1397 | rq->start_time = jiffies; |
| 1398 | r = blk_insert_cloned_request(rq->q, rq); |
| 1399 | if (r) |
| 1400 | dm_complete_request(rq, r); |
| 1401 | } |
| 1402 | EXPORT_SYMBOL_GPL(dm_dispatch_request); |
| 1403 | |
| 1404 | static void dm_rq_bio_destructor(struct bio *bio) |
| 1405 | { |
| 1406 | struct dm_rq_clone_bio_info *info = bio->bi_private; |
| 1407 | struct mapped_device *md = info->tio->md; |
| 1408 | |
| 1409 | free_bio_info(info); |
| 1410 | bio_free(bio, md->bs); |
| 1411 | } |
| 1412 | |
| 1413 | static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, |
| 1414 | void *data) |
| 1415 | { |
| 1416 | struct dm_rq_target_io *tio = data; |
| 1417 | struct mapped_device *md = tio->md; |
| 1418 | struct dm_rq_clone_bio_info *info = alloc_bio_info(md); |
| 1419 | |
| 1420 | if (!info) |
| 1421 | return -ENOMEM; |
| 1422 | |
| 1423 | info->orig = bio_orig; |
| 1424 | info->tio = tio; |
| 1425 | bio->bi_end_io = end_clone_bio; |
| 1426 | bio->bi_private = info; |
| 1427 | bio->bi_destructor = dm_rq_bio_destructor; |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | static int setup_clone(struct request *clone, struct request *rq, |
| 1433 | struct dm_rq_target_io *tio) |
| 1434 | { |
| 1435 | int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC, |
| 1436 | dm_rq_bio_constructor, tio); |
| 1437 | |
| 1438 | if (r) |
| 1439 | return r; |
| 1440 | |
| 1441 | clone->cmd = rq->cmd; |
| 1442 | clone->cmd_len = rq->cmd_len; |
| 1443 | clone->sense = rq->sense; |
| 1444 | clone->buffer = rq->buffer; |
| 1445 | clone->end_io = end_clone_request; |
| 1446 | clone->end_io_data = tio; |
| 1447 | |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1451 | static struct request *clone_rq(struct request *rq, struct mapped_device *md, |
| 1452 | gfp_t gfp_mask) |
| 1453 | { |
| 1454 | struct request *clone; |
| 1455 | struct dm_rq_target_io *tio; |
| 1456 | |
| 1457 | tio = alloc_rq_tio(md, gfp_mask); |
| 1458 | if (!tio) |
| 1459 | return NULL; |
| 1460 | |
| 1461 | tio->md = md; |
| 1462 | tio->ti = NULL; |
| 1463 | tio->orig = rq; |
| 1464 | tio->error = 0; |
| 1465 | memset(&tio->info, 0, sizeof(tio->info)); |
| 1466 | |
| 1467 | clone = &tio->clone; |
| 1468 | if (setup_clone(clone, rq, tio)) { |
| 1469 | /* -ENOMEM */ |
| 1470 | free_rq_tio(tio); |
| 1471 | return NULL; |
| 1472 | } |
| 1473 | |
| 1474 | return clone; |
| 1475 | } |
| 1476 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1477 | /* |
| 1478 | * Called with the queue lock held. |
| 1479 | */ |
| 1480 | static int dm_prep_fn(struct request_queue *q, struct request *rq) |
| 1481 | { |
| 1482 | struct mapped_device *md = q->queuedata; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1483 | struct request *clone; |
| 1484 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1485 | if (unlikely(rq->special)) { |
| 1486 | DMWARN("Already has something in rq->special."); |
| 1487 | return BLKPREP_KILL; |
| 1488 | } |
| 1489 | |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1490 | clone = clone_rq(rq, md, GFP_ATOMIC); |
| 1491 | if (!clone) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1492 | return BLKPREP_DEFER; |
| 1493 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1494 | rq->special = clone; |
| 1495 | rq->cmd_flags |= REQ_DONTPREP; |
| 1496 | |
| 1497 | return BLKPREP_OK; |
| 1498 | } |
| 1499 | |
Kiyoshi Ueda | 598de40 | 2009-12-10 23:52:14 +0000 | [diff] [blame] | 1500 | static void map_request(struct dm_target *ti, struct request *clone, |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1501 | struct mapped_device *md) |
| 1502 | { |
| 1503 | int r; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1504 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 1505 | |
| 1506 | /* |
| 1507 | * Hold the md reference here for the in-flight I/O. |
| 1508 | * We can't rely on the reference count by device opener, |
| 1509 | * because the device may be closed during the request completion |
| 1510 | * when all bios are completed. |
| 1511 | * See the comment in rq_completed() too. |
| 1512 | */ |
| 1513 | dm_get(md); |
| 1514 | |
| 1515 | tio->ti = ti; |
| 1516 | r = ti->type->map_rq(ti, clone, &tio->info); |
| 1517 | switch (r) { |
| 1518 | case DM_MAPIO_SUBMITTED: |
| 1519 | /* The target has taken the I/O to submit by itself later */ |
| 1520 | break; |
| 1521 | case DM_MAPIO_REMAPPED: |
| 1522 | /* The target has remapped the I/O so dispatch it */ |
| 1523 | dm_dispatch_request(clone); |
| 1524 | break; |
| 1525 | case DM_MAPIO_REQUEUE: |
| 1526 | /* The target wants to requeue the I/O */ |
| 1527 | dm_requeue_unmapped_request(clone); |
| 1528 | break; |
| 1529 | default: |
| 1530 | if (r > 0) { |
| 1531 | DMWARN("unimplemented target map return value: %d", r); |
| 1532 | BUG(); |
| 1533 | } |
| 1534 | |
| 1535 | /* The target wants to complete the I/O */ |
| 1536 | dm_kill_unmapped_request(clone, r); |
| 1537 | break; |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | * q->request_fn for request-based dm. |
| 1543 | * Called with the queue lock held. |
| 1544 | */ |
| 1545 | static void dm_request_fn(struct request_queue *q) |
| 1546 | { |
| 1547 | struct mapped_device *md = q->queuedata; |
| 1548 | struct dm_table *map = dm_get_table(md); |
| 1549 | struct dm_target *ti; |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 1550 | struct request *rq, *clone; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1551 | |
| 1552 | /* |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 1553 | * For suspend, check blk_queue_stopped() and increment |
| 1554 | * ->pending within a single queue_lock not to increment the |
| 1555 | * number of in-flight I/Os after the queue is stopped in |
| 1556 | * dm_suspend(). |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1557 | */ |
| 1558 | while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) { |
| 1559 | rq = blk_peek_request(q); |
| 1560 | if (!rq) |
| 1561 | goto plug_and_out; |
| 1562 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1563 | ti = dm_table_find_target(map, blk_rq_pos(rq)); |
| 1564 | if (ti->type->busy && ti->type->busy(ti)) |
| 1565 | goto plug_and_out; |
| 1566 | |
| 1567 | blk_start_request(rq); |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 1568 | clone = rq->special; |
| 1569 | atomic_inc(&md->pending[rq_data_dir(clone)]); |
| 1570 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1571 | spin_unlock(q->queue_lock); |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 1572 | map_request(ti, clone, md); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1573 | spin_lock_irq(q->queue_lock); |
| 1574 | } |
| 1575 | |
| 1576 | goto out; |
| 1577 | |
| 1578 | plug_and_out: |
| 1579 | if (!elv_queue_empty(q)) |
| 1580 | /* Some requests still remain, retry later */ |
| 1581 | blk_plug_device(q); |
| 1582 | |
| 1583 | out: |
| 1584 | dm_table_put(map); |
| 1585 | |
| 1586 | return; |
| 1587 | } |
| 1588 | |
| 1589 | int dm_underlying_device_busy(struct request_queue *q) |
| 1590 | { |
| 1591 | return blk_lld_busy(q); |
| 1592 | } |
| 1593 | EXPORT_SYMBOL_GPL(dm_underlying_device_busy); |
| 1594 | |
| 1595 | static int dm_lld_busy(struct request_queue *q) |
| 1596 | { |
| 1597 | int r; |
| 1598 | struct mapped_device *md = q->queuedata; |
| 1599 | struct dm_table *map = dm_get_table(md); |
| 1600 | |
| 1601 | if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) |
| 1602 | r = 1; |
| 1603 | else |
| 1604 | r = dm_table_any_busy_target(map); |
| 1605 | |
| 1606 | dm_table_put(map); |
| 1607 | |
| 1608 | return r; |
| 1609 | } |
| 1610 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1611 | static void dm_unplug_all(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | { |
| 1613 | struct mapped_device *md = q->queuedata; |
| 1614 | struct dm_table *map = dm_get_table(md); |
| 1615 | |
| 1616 | if (map) { |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1617 | if (dm_request_based(md)) |
| 1618 | generic_unplug_device(q); |
| 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | dm_table_unplug_all(map); |
| 1621 | dm_table_put(map); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | static int dm_any_congested(void *congested_data, int bdi_bits) |
| 1626 | { |
Chandra Seetharaman | 8a57dfc | 2008-11-13 23:39:14 +0000 | [diff] [blame] | 1627 | int r = bdi_bits; |
| 1628 | struct mapped_device *md = congested_data; |
| 1629 | struct dm_table *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 1631 | if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { |
Chandra Seetharaman | 8a57dfc | 2008-11-13 23:39:14 +0000 | [diff] [blame] | 1632 | map = dm_get_table(md); |
| 1633 | if (map) { |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1634 | /* |
| 1635 | * Request-based dm cares about only own queue for |
| 1636 | * the query about congestion status of request_queue |
| 1637 | */ |
| 1638 | if (dm_request_based(md)) |
| 1639 | r = md->queue->backing_dev_info.state & |
| 1640 | bdi_bits; |
| 1641 | else |
| 1642 | r = dm_table_any_congested(map, bdi_bits); |
| 1643 | |
Chandra Seetharaman | 8a57dfc | 2008-11-13 23:39:14 +0000 | [diff] [blame] | 1644 | dm_table_put(map); |
| 1645 | } |
| 1646 | } |
| 1647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | return r; |
| 1649 | } |
| 1650 | |
| 1651 | /*----------------------------------------------------------------- |
| 1652 | * An IDR is used to keep track of allocated minor numbers. |
| 1653 | *---------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | static DEFINE_IDR(_minor_idr); |
| 1655 | |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1656 | static void free_minor(int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | { |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1658 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | idr_remove(&_minor_idr, minor); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1660 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * See if the device with a specific minor # is free. |
| 1665 | */ |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 1666 | static int specific_minor(int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | { |
| 1668 | int r, m; |
| 1669 | |
| 1670 | if (minor >= (1 << MINORBITS)) |
| 1671 | return -EINVAL; |
| 1672 | |
Jeff Mahoney | 62f75c2 | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1673 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); |
| 1674 | if (!r) |
| 1675 | return -ENOMEM; |
| 1676 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1677 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
| 1679 | if (idr_find(&_minor_idr, minor)) { |
| 1680 | r = -EBUSY; |
| 1681 | goto out; |
| 1682 | } |
| 1683 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1684 | r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); |
Jeff Mahoney | 62f75c2 | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1685 | if (r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
| 1688 | if (m != minor) { |
| 1689 | idr_remove(&_minor_idr, m); |
| 1690 | r = -EBUSY; |
| 1691 | goto out; |
| 1692 | } |
| 1693 | |
| 1694 | out: |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1695 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | return r; |
| 1697 | } |
| 1698 | |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 1699 | static int next_free_minor(int *minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | { |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1701 | int r, m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); |
Jeff Mahoney | 62f75c2 | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1704 | if (!r) |
| 1705 | return -ENOMEM; |
| 1706 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1707 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1709 | r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 1710 | if (r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | |
| 1713 | if (m >= (1 << MINORBITS)) { |
| 1714 | idr_remove(&_minor_idr, m); |
| 1715 | r = -ENOSPC; |
| 1716 | goto out; |
| 1717 | } |
| 1718 | |
| 1719 | *minor = m; |
| 1720 | |
| 1721 | out: |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1722 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | return r; |
| 1724 | } |
| 1725 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1726 | static const struct block_device_operations dm_blk_dops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | |
Mikulas Patocka | 53d5914 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 1728 | static void dm_wq_work(struct work_struct *work); |
| 1729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | /* |
| 1731 | * Allocate and initialise a blank device with a given minor. |
| 1732 | */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1733 | static struct mapped_device *alloc_dev(int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | { |
| 1735 | int r; |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 1736 | struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1737 | void *old_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | |
| 1739 | if (!md) { |
| 1740 | DMWARN("unable to allocate device, out of memory."); |
| 1741 | return NULL; |
| 1742 | } |
| 1743 | |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1744 | if (!try_module_get(THIS_MODULE)) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1745 | goto bad_module_get; |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | /* get a minor number for the dev */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1748 | if (minor == DM_ANY_MINOR) |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 1749 | r = next_free_minor(&minor); |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1750 | else |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 1751 | r = specific_minor(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | if (r < 0) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1753 | goto bad_minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1755 | init_rwsem(&md->io_lock); |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 1756 | mutex_init(&md->suspend_lock); |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 1757 | spin_lock_init(&md->deferred_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | rwlock_init(&md->map_lock); |
| 1759 | atomic_set(&md->holders, 1); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 1760 | atomic_set(&md->open_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | atomic_set(&md->event_nr, 0); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1762 | atomic_set(&md->uevent_seq, 0); |
| 1763 | INIT_LIST_HEAD(&md->uevent_list); |
| 1764 | spin_lock_init(&md->uevent_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 1766 | md->queue = blk_init_queue(dm_request_fn, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | if (!md->queue) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1768 | goto bad_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 1770 | /* |
| 1771 | * Request-based dm devices cannot be stacked on top of bio-based dm |
| 1772 | * devices. The type of this dm device has not been decided yet, |
| 1773 | * although we initialized the queue using blk_init_queue(). |
| 1774 | * The type is decided at the first table loading time. |
| 1775 | * To prevent problematic device stacking, clear the queue flag |
| 1776 | * for request stacking support until then. |
| 1777 | * |
| 1778 | * This queue is new, so no concurrency on the queue_flags. |
| 1779 | */ |
| 1780 | queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); |
| 1781 | md->saved_make_request_fn = md->queue->make_request_fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | md->queue->queuedata = md; |
| 1783 | md->queue->backing_dev_info.congested_fn = dm_any_congested; |
| 1784 | md->queue->backing_dev_info.congested_data = md; |
| 1785 | blk_queue_make_request(md->queue, dm_request); |
Jens Axboe | daef265 | 2006-01-10 10:48:02 +0100 | [diff] [blame] | 1786 | blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | md->queue->unplug_fn = dm_unplug_all; |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 1788 | blk_queue_merge_bvec(md->queue, dm_merge_bvec); |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 1789 | blk_queue_softirq_done(md->queue, dm_softirq_done); |
| 1790 | blk_queue_prep_rq(md->queue, dm_prep_fn); |
| 1791 | blk_queue_lld_busy(md->queue, dm_lld_busy); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | md->disk = alloc_disk(1); |
| 1794 | if (!md->disk) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1795 | goto bad_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 1797 | atomic_set(&md->pending[0], 0); |
| 1798 | atomic_set(&md->pending[1], 0); |
Jeff Mahoney | f0b0411 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1799 | init_waitqueue_head(&md->wait); |
Mikulas Patocka | 53d5914 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 1800 | INIT_WORK(&md->work, dm_wq_work); |
Jeff Mahoney | f0b0411 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1801 | init_waitqueue_head(&md->eventq); |
| 1802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | md->disk->major = _major; |
| 1804 | md->disk->first_minor = minor; |
| 1805 | md->disk->fops = &dm_blk_dops; |
| 1806 | md->disk->queue = md->queue; |
| 1807 | md->disk->private_data = md; |
| 1808 | sprintf(md->disk->disk_name, "dm-%d", minor); |
| 1809 | add_disk(md->disk); |
Mike Anderson | 7e51f25 | 2006-03-27 01:17:52 -0800 | [diff] [blame] | 1810 | format_dev_t(md->name, MKDEV(_major, minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 1812 | md->wq = create_singlethread_workqueue("kdmflush"); |
| 1813 | if (!md->wq) |
| 1814 | goto bad_thread; |
| 1815 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 1816 | md->bdev = bdget_disk(md->disk, 0); |
| 1817 | if (!md->bdev) |
| 1818 | goto bad_bdev; |
| 1819 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1820 | /* Populate the mapping, nobody knows we exist yet */ |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1821 | spin_lock(&_minor_lock); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1822 | old_md = idr_replace(&_minor_idr, md, minor); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1823 | spin_unlock(&_minor_lock); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1824 | |
| 1825 | BUG_ON(old_md != MINOR_ALLOCED); |
| 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | return md; |
| 1828 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 1829 | bad_bdev: |
| 1830 | destroy_workqueue(md->wq); |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 1831 | bad_thread: |
Zdenek Kabelac | 03022c5 | 2009-10-16 23:18:15 +0100 | [diff] [blame] | 1832 | del_gendisk(md->disk); |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 1833 | put_disk(md->disk); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1834 | bad_disk: |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 1835 | blk_cleanup_queue(md->queue); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1836 | bad_queue: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | free_minor(minor); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1838 | bad_minor: |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1839 | module_put(THIS_MODULE); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1840 | bad_module_get: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | kfree(md); |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | |
Jun'ichi Nomura | ae9da83 | 2007-10-19 22:38:43 +0100 | [diff] [blame] | 1845 | static void unlock_fs(struct mapped_device *md); |
| 1846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | static void free_dev(struct mapped_device *md) |
| 1848 | { |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 1849 | int minor = MINOR(disk_devt(md->disk)); |
Jun'ichi Nomura | 63d94e4 | 2006-02-24 13:04:25 -0800 | [diff] [blame] | 1850 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 1851 | unlock_fs(md); |
| 1852 | bdput(md->bdev); |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 1853 | destroy_workqueue(md->wq); |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 1854 | if (md->tio_pool) |
| 1855 | mempool_destroy(md->tio_pool); |
| 1856 | if (md->io_pool) |
| 1857 | mempool_destroy(md->io_pool); |
| 1858 | if (md->bs) |
| 1859 | bioset_free(md->bs); |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 1860 | blk_integrity_unregister(md->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | del_gendisk(md->disk); |
Jun'ichi Nomura | 63d94e4 | 2006-02-24 13:04:25 -0800 | [diff] [blame] | 1862 | free_minor(minor); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 1863 | |
| 1864 | spin_lock(&_minor_lock); |
| 1865 | md->disk->private_data = NULL; |
| 1866 | spin_unlock(&_minor_lock); |
| 1867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | put_disk(md->disk); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 1869 | blk_cleanup_queue(md->queue); |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1870 | module_put(THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | kfree(md); |
| 1872 | } |
| 1873 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 1874 | static void __bind_mempools(struct mapped_device *md, struct dm_table *t) |
| 1875 | { |
| 1876 | struct dm_md_mempools *p; |
| 1877 | |
| 1878 | if (md->io_pool && md->tio_pool && md->bs) |
| 1879 | /* the md already has necessary mempools */ |
| 1880 | goto out; |
| 1881 | |
| 1882 | p = dm_table_get_md_mempools(t); |
| 1883 | BUG_ON(!p || md->io_pool || md->tio_pool || md->bs); |
| 1884 | |
| 1885 | md->io_pool = p->io_pool; |
| 1886 | p->io_pool = NULL; |
| 1887 | md->tio_pool = p->tio_pool; |
| 1888 | p->tio_pool = NULL; |
| 1889 | md->bs = p->bs; |
| 1890 | p->bs = NULL; |
| 1891 | |
| 1892 | out: |
| 1893 | /* mempool bind completed, now no need any mempools in the table */ |
| 1894 | dm_table_free_md_mempools(t); |
| 1895 | } |
| 1896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | /* |
| 1898 | * Bind a table to the device. |
| 1899 | */ |
| 1900 | static void event_callback(void *context) |
| 1901 | { |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1902 | unsigned long flags; |
| 1903 | LIST_HEAD(uevents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | struct mapped_device *md = (struct mapped_device *) context; |
| 1905 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1906 | spin_lock_irqsave(&md->uevent_lock, flags); |
| 1907 | list_splice_init(&md->uevent_list, &uevents); |
| 1908 | spin_unlock_irqrestore(&md->uevent_lock, flags); |
| 1909 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 1910 | dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | atomic_inc(&md->event_nr); |
| 1913 | wake_up(&md->eventq); |
| 1914 | } |
| 1915 | |
Alasdair G Kergon | 4e90188be | 2005-07-28 21:15:59 -0700 | [diff] [blame] | 1916 | static void __set_size(struct mapped_device *md, sector_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | { |
Alasdair G Kergon | 4e90188be | 2005-07-28 21:15:59 -0700 | [diff] [blame] | 1918 | set_capacity(md->disk, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | |
Mikulas Patocka | db8fef4 | 2009-06-22 10:12:15 +0100 | [diff] [blame] | 1920 | mutex_lock(&md->bdev->bd_inode->i_mutex); |
| 1921 | i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); |
| 1922 | mutex_unlock(&md->bdev->bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Mike Snitzer | 754c5fc | 2009-06-22 10:12:34 +0100 | [diff] [blame] | 1925 | static int __bind(struct mapped_device *md, struct dm_table *t, |
| 1926 | struct queue_limits *limits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1928 | struct request_queue *q = md->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | sector_t size; |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1930 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | |
| 1932 | size = dm_table_get_size(t); |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1933 | |
| 1934 | /* |
| 1935 | * Wipe any geometry if the size of the table changed. |
| 1936 | */ |
| 1937 | if (size != get_capacity(md->disk)) |
| 1938 | memset(&md->geometry, 0, sizeof(md->geometry)); |
| 1939 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 1940 | __set_size(md, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 1942 | if (!size) { |
| 1943 | dm_table_destroy(t); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1947 | dm_table_event_callback(t, event_callback, md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1948 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 1949 | /* |
| 1950 | * The queue hasn't been stopped yet, if the old table type wasn't |
| 1951 | * for request-based during suspension. So stop it to prevent |
| 1952 | * I/O mapping before resume. |
| 1953 | * This must be done before setting the queue restrictions, |
| 1954 | * because request-based dm may be run just after the setting. |
| 1955 | */ |
| 1956 | if (dm_table_request_based(t) && !blk_queue_stopped(q)) |
| 1957 | stop_queue(q); |
| 1958 | |
| 1959 | __bind_mempools(md, t); |
| 1960 | |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1961 | write_lock_irqsave(&md->map_lock, flags); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1962 | md->map = t; |
Mike Snitzer | 754c5fc | 2009-06-22 10:12:34 +0100 | [diff] [blame] | 1963 | dm_table_set_restrictions(t, q, limits); |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1964 | write_unlock_irqrestore(&md->map_lock, flags); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | return 0; |
| 1967 | } |
| 1968 | |
| 1969 | static void __unbind(struct mapped_device *md) |
| 1970 | { |
| 1971 | struct dm_table *map = md->map; |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1972 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | |
| 1974 | if (!map) |
| 1975 | return; |
| 1976 | |
| 1977 | dm_table_event_callback(map, NULL, NULL); |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1978 | write_lock_irqsave(&md->map_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | md->map = NULL; |
Kiyoshi Ueda | 523d929 | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1980 | write_unlock_irqrestore(&md->map_lock, flags); |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 1981 | dm_table_destroy(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | * Constructor for a new device. |
| 1986 | */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1987 | int dm_create(int minor, struct mapped_device **result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | { |
| 1989 | struct mapped_device *md; |
| 1990 | |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1991 | md = alloc_dev(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | if (!md) |
| 1993 | return -ENXIO; |
| 1994 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 1995 | dm_sysfs_init(md); |
| 1996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | *result = md; |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 2001 | static struct mapped_device *dm_find_md(dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | { |
| 2003 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | unsigned minor = MINOR(dev); |
| 2005 | |
| 2006 | if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) |
| 2007 | return NULL; |
| 2008 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2009 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
| 2011 | md = idr_find(&_minor_idr, minor); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 2012 | if (md && (md == MINOR_ALLOCED || |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 2013 | (MINOR(disk_devt(dm_disk(md))) != minor) || |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 2014 | test_bit(DMF_FREEING, &md->flags))) { |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 2015 | md = NULL; |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 2016 | goto out; |
| 2017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 2019 | out: |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2020 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 2022 | return md; |
| 2023 | } |
| 2024 | |
David Teigland | d229a95 | 2006-01-06 00:20:01 -0800 | [diff] [blame] | 2025 | struct mapped_device *dm_get_md(dev_t dev) |
| 2026 | { |
| 2027 | struct mapped_device *md = dm_find_md(dev); |
| 2028 | |
| 2029 | if (md) |
| 2030 | dm_get(md); |
| 2031 | |
| 2032 | return md; |
| 2033 | } |
| 2034 | |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 2035 | void *dm_get_mdptr(struct mapped_device *md) |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 2036 | { |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 2037 | return md->interface_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | void dm_set_mdptr(struct mapped_device *md, void *ptr) |
| 2041 | { |
| 2042 | md->interface_ptr = ptr; |
| 2043 | } |
| 2044 | |
| 2045 | void dm_get(struct mapped_device *md) |
| 2046 | { |
| 2047 | atomic_inc(&md->holders); |
| 2048 | } |
| 2049 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2050 | const char *dm_device_name(struct mapped_device *md) |
| 2051 | { |
| 2052 | return md->name; |
| 2053 | } |
| 2054 | EXPORT_SYMBOL_GPL(dm_device_name); |
| 2055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | void dm_put(struct mapped_device *md) |
| 2057 | { |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 2058 | struct dm_table *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 2060 | BUG_ON(test_bit(DMF_FREEING, &md->flags)); |
| 2061 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2062 | if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 2063 | map = dm_get_table(md); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 2064 | idr_replace(&_minor_idr, MINOR_ALLOCED, |
| 2065 | MINOR(disk_devt(dm_disk(md)))); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 2066 | set_bit(DMF_FREEING, &md->flags); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2067 | spin_unlock(&_minor_lock); |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2068 | if (!dm_suspended(md)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | dm_table_presuspend_targets(map); |
| 2070 | dm_table_postsuspend_targets(map); |
| 2071 | } |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 2072 | dm_sysfs_exit(md); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 2073 | dm_table_put(map); |
Mikulas Patocka | a1b51e9 | 2009-01-06 03:04:53 +0000 | [diff] [blame] | 2074 | __unbind(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | free_dev(md); |
| 2076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | } |
Edward Goggin | 79eb885 | 2007-05-09 02:32:56 -0700 | [diff] [blame] | 2078 | EXPORT_SYMBOL_GPL(dm_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | |
Mikulas Patocka | 401600d | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2080 | static int dm_wait_for_completion(struct mapped_device *md, int interruptible) |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2081 | { |
| 2082 | int r = 0; |
Mikulas Patocka | b44ebeb | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2083 | DECLARE_WAITQUEUE(wait, current); |
| 2084 | |
| 2085 | dm_unplug_all(md->queue); |
| 2086 | |
| 2087 | add_wait_queue(&md->wait, &wait); |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2088 | |
| 2089 | while (1) { |
Mikulas Patocka | 401600d | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2090 | set_current_state(interruptible); |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2091 | |
| 2092 | smp_mb(); |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2093 | if (!md_in_flight(md)) |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2094 | break; |
| 2095 | |
Mikulas Patocka | 401600d | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2096 | if (interruptible == TASK_INTERRUPTIBLE && |
| 2097 | signal_pending(current)) { |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2098 | r = -EINTR; |
| 2099 | break; |
| 2100 | } |
| 2101 | |
| 2102 | io_schedule(); |
| 2103 | } |
| 2104 | set_current_state(TASK_RUNNING); |
| 2105 | |
Mikulas Patocka | b44ebeb | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2106 | remove_wait_queue(&md->wait, &wait); |
| 2107 | |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2108 | return r; |
| 2109 | } |
| 2110 | |
Mikulas Patocka | 531fe96 | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2111 | static void dm_flush(struct mapped_device *md) |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 2112 | { |
| 2113 | dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); |
Mikulas Patocka | 52b1fd5 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 2114 | |
| 2115 | bio_init(&md->barrier_bio); |
| 2116 | md->barrier_bio.bi_bdev = md->bdev; |
| 2117 | md->barrier_bio.bi_rw = WRITE_BARRIER; |
| 2118 | __split_and_process_bio(md, &md->barrier_bio); |
| 2119 | |
| 2120 | dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | static void process_barrier(struct mapped_device *md, struct bio *bio) |
| 2124 | { |
Mikulas Patocka | 5aa2781 | 2009-06-22 10:12:18 +0100 | [diff] [blame] | 2125 | md->barrier_error = 0; |
| 2126 | |
Mikulas Patocka | 531fe96 | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2127 | dm_flush(md); |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 2128 | |
Mikulas Patocka | 5aa2781 | 2009-06-22 10:12:18 +0100 | [diff] [blame] | 2129 | if (!bio_empty_barrier(bio)) { |
| 2130 | __split_and_process_bio(md, bio); |
| 2131 | dm_flush(md); |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 2134 | if (md->barrier_error != DM_ENDIO_REQUEUE) |
Mikulas Patocka | 531fe96 | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2135 | bio_endio(bio, md->barrier_error); |
Mikulas Patocka | 2761e95 | 2009-06-22 10:12:18 +0100 | [diff] [blame] | 2136 | else { |
| 2137 | spin_lock_irq(&md->deferred_lock); |
| 2138 | bio_list_add_head(&md->deferred, bio); |
| 2139 | spin_unlock_irq(&md->deferred_lock); |
| 2140 | } |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 2141 | } |
| 2142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | /* |
| 2144 | * Process the deferred bios |
| 2145 | */ |
Mikulas Patocka | ef20858 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2146 | static void dm_wq_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | { |
Mikulas Patocka | ef20858 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2148 | struct mapped_device *md = container_of(work, struct mapped_device, |
| 2149 | work); |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 2150 | struct bio *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | |
Mikulas Patocka | ef20858 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2152 | down_write(&md->io_lock); |
| 2153 | |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2154 | while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { |
Alasdair G Kergon | df12ee9 | 2009-04-09 00:27:13 +0100 | [diff] [blame] | 2155 | spin_lock_irq(&md->deferred_lock); |
| 2156 | c = bio_list_pop(&md->deferred); |
| 2157 | spin_unlock_irq(&md->deferred_lock); |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2158 | |
Alasdair G Kergon | df12ee9 | 2009-04-09 00:27:13 +0100 | [diff] [blame] | 2159 | if (!c) { |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 2160 | clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags); |
Alasdair G Kergon | df12ee9 | 2009-04-09 00:27:13 +0100 | [diff] [blame] | 2161 | break; |
| 2162 | } |
| 2163 | |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2164 | up_write(&md->io_lock); |
| 2165 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2166 | if (dm_request_based(md)) |
| 2167 | generic_make_request(c); |
| 2168 | else { |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 2169 | if (bio_rw_flagged(c, BIO_RW_BARRIER)) |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2170 | process_barrier(md, c); |
| 2171 | else |
| 2172 | __split_and_process_bio(md, c); |
| 2173 | } |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2174 | |
| 2175 | down_write(&md->io_lock); |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2176 | } |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 2177 | |
Mikulas Patocka | ef20858 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2178 | up_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Mikulas Patocka | 9a1fb46 | 2009-04-02 19:55:36 +0100 | [diff] [blame] | 2181 | static void dm_queue_flush(struct mapped_device *md) |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 2182 | { |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2183 | clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
| 2184 | smp_mb__after_clear_bit(); |
Mikulas Patocka | 53d5914 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 2185 | queue_work(md->wq, &md->work); |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | /* |
| 2189 | * Swap in a new table (destroying old one). |
| 2190 | */ |
| 2191 | int dm_swap_table(struct mapped_device *md, struct dm_table *table) |
| 2192 | { |
Mike Snitzer | 754c5fc | 2009-06-22 10:12:34 +0100 | [diff] [blame] | 2193 | struct queue_limits limits; |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 2194 | int r = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 2196 | mutex_lock(&md->suspend_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
| 2198 | /* device must be suspended */ |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2199 | if (!dm_suspended(md)) |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 2200 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | |
Mike Snitzer | 754c5fc | 2009-06-22 10:12:34 +0100 | [diff] [blame] | 2202 | r = dm_calculate_queue_limits(table, &limits); |
| 2203 | if (r) |
| 2204 | goto out; |
| 2205 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2206 | /* cannot change the device type, once a table is bound */ |
| 2207 | if (md->map && |
| 2208 | (dm_table_get_type(md->map) != dm_table_get_type(table))) { |
| 2209 | DMWARN("can't change the device type after a table is bound"); |
| 2210 | goto out; |
| 2211 | } |
| 2212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | __unbind(md); |
Mike Snitzer | 754c5fc | 2009-06-22 10:12:34 +0100 | [diff] [blame] | 2214 | r = __bind(md, table, &limits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 2216 | out: |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 2217 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 2218 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | /* |
| 2222 | * Functions to lock and unlock any filesystem running on the |
| 2223 | * device. |
| 2224 | */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2225 | static int lock_fs(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | { |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 2227 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
| 2229 | WARN_ON(md->frozen_sb); |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 2230 | |
Mikulas Patocka | db8fef4 | 2009-06-22 10:12:15 +0100 | [diff] [blame] | 2231 | md->frozen_sb = freeze_bdev(md->bdev); |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 2232 | if (IS_ERR(md->frozen_sb)) { |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2233 | r = PTR_ERR(md->frozen_sb); |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 2234 | md->frozen_sb = NULL; |
| 2235 | return r; |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 2238 | set_bit(DMF_FROZEN, &md->flags); |
| 2239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | return 0; |
| 2241 | } |
| 2242 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2243 | static void unlock_fs(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | { |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 2245 | if (!test_bit(DMF_FROZEN, &md->flags)) |
| 2246 | return; |
| 2247 | |
Mikulas Patocka | db8fef4 | 2009-06-22 10:12:15 +0100 | [diff] [blame] | 2248 | thaw_bdev(md->bdev, md->frozen_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | md->frozen_sb = NULL; |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 2250 | clear_bit(DMF_FROZEN, &md->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | /* |
| 2254 | * We need to be able to change a mapping table under a mounted |
| 2255 | * filesystem. For example we might want to move some data in |
| 2256 | * the background. Before the table can be swapped with |
| 2257 | * dm_bind_table, dm_suspend must be called to flush any in |
| 2258 | * flight bios and ensure that any further io gets deferred. |
| 2259 | */ |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2260 | /* |
| 2261 | * Suspend mechanism in request-based dm. |
| 2262 | * |
Kiyoshi Ueda | 9f518b2 | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2263 | * 1. Flush all I/Os by lock_fs() if needed. |
| 2264 | * 2. Stop dispatching any I/O by stopping the request_queue. |
| 2265 | * 3. Wait for all in-flight I/Os to be completed or requeued. |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2266 | * |
Kiyoshi Ueda | 9f518b2 | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2267 | * To abort suspend, start the request_queue. |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2268 | */ |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 2269 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2271 | struct dm_table *map = NULL; |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2272 | int r = 0; |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 2273 | int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 2274 | int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 2276 | mutex_lock(&md->suspend_lock); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2277 | |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 2278 | if (dm_suspended(md)) { |
| 2279 | r = -EINVAL; |
Alasdair G Kergon | d287483 | 2006-11-08 17:44:43 -0800 | [diff] [blame] | 2280 | goto out_unlock; |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 2281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | |
| 2283 | map = dm_get_table(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 2285 | /* |
| 2286 | * DMF_NOFLUSH_SUSPENDING must be set before presuspend. |
| 2287 | * This flag is cleared before dm_suspend returns. |
| 2288 | */ |
| 2289 | if (noflush) |
| 2290 | set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
| 2291 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2292 | /* This does not get reverted if there's an error later. */ |
| 2293 | dm_table_presuspend_targets(map); |
| 2294 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2295 | /* |
Kiyoshi Ueda | 9f518b2 | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2296 | * Flush I/O to the device. |
| 2297 | * Any I/O submitted after lock_fs() may not be flushed. |
| 2298 | * noflush takes precedence over do_lockfs. |
| 2299 | * (lock_fs() flushes I/Os and waits for them to complete.) |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2300 | */ |
| 2301 | if (!noflush && do_lockfs) { |
| 2302 | r = lock_fs(md); |
| 2303 | if (r) |
Kiyoshi Ueda | f431d96 | 2008-10-21 17:45:07 +0100 | [diff] [blame] | 2304 | goto out; |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 2305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | |
| 2307 | /* |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2308 | * Here we must make sure that no processes are submitting requests |
| 2309 | * to target drivers i.e. no one may be executing |
| 2310 | * __split_and_process_bio. This is called from dm_request and |
| 2311 | * dm_wq_work. |
| 2312 | * |
| 2313 | * To get all processes out of __split_and_process_bio in dm_request, |
| 2314 | * we take the write lock. To prevent any process from reentering |
| 2315 | * __split_and_process_bio from dm_request, we set |
| 2316 | * DMF_QUEUE_IO_TO_THREAD. |
| 2317 | * |
| 2318 | * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND |
| 2319 | * and call flush_workqueue(md->wq). flush_workqueue will wait until |
| 2320 | * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any |
| 2321 | * further calls to __split_and_process_bio from dm_wq_work. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2323 | down_write(&md->io_lock); |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 2324 | set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
| 2325 | set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2326 | up_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2328 | flush_workqueue(md->wq); |
| 2329 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2330 | if (dm_request_based(md)) |
Kiyoshi Ueda | 9f518b2 | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2331 | stop_queue(md->queue); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | /* |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2334 | * At this point no more requests are entering target request routines. |
| 2335 | * We call dm_wait_for_completion to wait for all existing requests |
| 2336 | * to finish. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | */ |
Mikulas Patocka | 401600d | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2338 | r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2340 | down_write(&md->io_lock); |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 2341 | if (noflush) |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2342 | clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
Milan Broz | 94d6351 | 2008-02-08 02:10:27 +0000 | [diff] [blame] | 2343 | up_write(&md->io_lock); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 2344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | /* were we interrupted ? */ |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2346 | if (r < 0) { |
Mikulas Patocka | 9a1fb46 | 2009-04-02 19:55:36 +0100 | [diff] [blame] | 2347 | dm_queue_flush(md); |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 2348 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2349 | if (dm_request_based(md)) |
Kiyoshi Ueda | 9f518b2 | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2350 | start_queue(md->queue); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2351 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2352 | unlock_fs(md); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 2353 | goto out; /* pushback list is already flushed, so skip flush */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2354 | } |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2355 | |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2356 | /* |
| 2357 | * If dm_wait_for_completion returned 0, the device is completely |
| 2358 | * quiescent now. There is no request-processing activity. All new |
| 2359 | * requests are being added to md->deferred list. |
| 2360 | */ |
| 2361 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2362 | dm_table_postsuspend_targets(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | |
| 2364 | set_bit(DMF_SUSPENDED, &md->flags); |
| 2365 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2366 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | dm_table_put(map); |
Alasdair G Kergon | d287483 | 2006-11-08 17:44:43 -0800 | [diff] [blame] | 2368 | |
| 2369 | out_unlock: |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 2370 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2371 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | int dm_resume(struct mapped_device *md) |
| 2375 | { |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2376 | int r = -EINVAL; |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2377 | struct dm_table *map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 2379 | mutex_lock(&md->suspend_lock); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2380 | if (!dm_suspended(md)) |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2381 | goto out; |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2382 | |
| 2383 | map = dm_get_table(md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2384 | if (!map || !dm_table_get_size(map)) |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2385 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 2387 | r = dm_table_resume_targets(map); |
| 2388 | if (r) |
| 2389 | goto out; |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2390 | |
Mikulas Patocka | 9a1fb46 | 2009-04-02 19:55:36 +0100 | [diff] [blame] | 2391 | dm_queue_flush(md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2392 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2393 | /* |
| 2394 | * Flushing deferred I/Os must be done after targets are resumed |
| 2395 | * so that mapping of targets can work correctly. |
| 2396 | * Request-based dm is queueing the deferred I/Os in its request_queue. |
| 2397 | */ |
| 2398 | if (dm_request_based(md)) |
| 2399 | start_queue(md->queue); |
| 2400 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2401 | unlock_fs(md); |
| 2402 | |
| 2403 | clear_bit(DMF_SUSPENDED, &md->flags); |
| 2404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | dm_table_unplug_all(map); |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2406 | r = 0; |
| 2407 | out: |
| 2408 | dm_table_put(map); |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 2409 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2410 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2411 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | /*----------------------------------------------------------------- |
| 2415 | * Event notification. |
| 2416 | *---------------------------------------------------------------*/ |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 2417 | void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, |
| 2418 | unsigned cookie) |
Alasdair G Kergon | 69267a3 | 2007-12-13 14:15:57 +0000 | [diff] [blame] | 2419 | { |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 2420 | char udev_cookie[DM_COOKIE_LENGTH]; |
| 2421 | char *envp[] = { udev_cookie, NULL }; |
| 2422 | |
| 2423 | if (!cookie) |
| 2424 | kobject_uevent(&disk_to_dev(md->disk)->kobj, action); |
| 2425 | else { |
| 2426 | snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", |
| 2427 | DM_COOKIE_ENV_VAR_NAME, cookie); |
| 2428 | kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp); |
| 2429 | } |
Alasdair G Kergon | 69267a3 | 2007-12-13 14:15:57 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 2432 | uint32_t dm_next_uevent_seq(struct mapped_device *md) |
| 2433 | { |
| 2434 | return atomic_add_return(1, &md->uevent_seq); |
| 2435 | } |
| 2436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | uint32_t dm_get_event_nr(struct mapped_device *md) |
| 2438 | { |
| 2439 | return atomic_read(&md->event_nr); |
| 2440 | } |
| 2441 | |
| 2442 | int dm_wait_event(struct mapped_device *md, int event_nr) |
| 2443 | { |
| 2444 | return wait_event_interruptible(md->eventq, |
| 2445 | (event_nr != atomic_read(&md->event_nr))); |
| 2446 | } |
| 2447 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 2448 | void dm_uevent_add(struct mapped_device *md, struct list_head *elist) |
| 2449 | { |
| 2450 | unsigned long flags; |
| 2451 | |
| 2452 | spin_lock_irqsave(&md->uevent_lock, flags); |
| 2453 | list_add(elist, &md->uevent_list); |
| 2454 | spin_unlock_irqrestore(&md->uevent_lock, flags); |
| 2455 | } |
| 2456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | /* |
| 2458 | * The gendisk is only valid as long as you have a reference |
| 2459 | * count on 'md'. |
| 2460 | */ |
| 2461 | struct gendisk *dm_disk(struct mapped_device *md) |
| 2462 | { |
| 2463 | return md->disk; |
| 2464 | } |
| 2465 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 2466 | struct kobject *dm_kobject(struct mapped_device *md) |
| 2467 | { |
| 2468 | return &md->kobj; |
| 2469 | } |
| 2470 | |
| 2471 | /* |
| 2472 | * struct mapped_device should not be exported outside of dm.c |
| 2473 | * so use this check to verify that kobj is part of md structure |
| 2474 | */ |
| 2475 | struct mapped_device *dm_get_from_kobject(struct kobject *kobj) |
| 2476 | { |
| 2477 | struct mapped_device *md; |
| 2478 | |
| 2479 | md = container_of(kobj, struct mapped_device, kobj); |
| 2480 | if (&md->kobj != kobj) |
| 2481 | return NULL; |
| 2482 | |
Milan Broz | 4d89b7b | 2009-06-22 10:12:11 +0100 | [diff] [blame] | 2483 | if (test_bit(DMF_FREEING, &md->flags) || |
| 2484 | test_bit(DMF_DELETING, &md->flags)) |
| 2485 | return NULL; |
| 2486 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 2487 | dm_get(md); |
| 2488 | return md; |
| 2489 | } |
| 2490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | int dm_suspended(struct mapped_device *md) |
| 2492 | { |
| 2493 | return test_bit(DMF_SUSPENDED, &md->flags); |
| 2494 | } |
| 2495 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 2496 | int dm_noflush_suspending(struct dm_target *ti) |
| 2497 | { |
| 2498 | struct mapped_device *md = dm_table_get_md(ti->table); |
| 2499 | int r = __noflush_suspending(md); |
| 2500 | |
| 2501 | dm_put(md); |
| 2502 | |
| 2503 | return r; |
| 2504 | } |
| 2505 | EXPORT_SYMBOL_GPL(dm_noflush_suspending); |
| 2506 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2507 | struct dm_md_mempools *dm_alloc_md_mempools(unsigned type) |
| 2508 | { |
| 2509 | struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL); |
| 2510 | |
| 2511 | if (!pools) |
| 2512 | return NULL; |
| 2513 | |
| 2514 | pools->io_pool = (type == DM_TYPE_BIO_BASED) ? |
| 2515 | mempool_create_slab_pool(MIN_IOS, _io_cache) : |
| 2516 | mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache); |
| 2517 | if (!pools->io_pool) |
| 2518 | goto free_pools_and_out; |
| 2519 | |
| 2520 | pools->tio_pool = (type == DM_TYPE_BIO_BASED) ? |
| 2521 | mempool_create_slab_pool(MIN_IOS, _tio_cache) : |
| 2522 | mempool_create_slab_pool(MIN_IOS, _rq_tio_cache); |
| 2523 | if (!pools->tio_pool) |
| 2524 | goto free_io_pool_and_out; |
| 2525 | |
| 2526 | pools->bs = (type == DM_TYPE_BIO_BASED) ? |
| 2527 | bioset_create(16, 0) : bioset_create(MIN_IOS, 0); |
| 2528 | if (!pools->bs) |
| 2529 | goto free_tio_pool_and_out; |
| 2530 | |
| 2531 | return pools; |
| 2532 | |
| 2533 | free_tio_pool_and_out: |
| 2534 | mempool_destroy(pools->tio_pool); |
| 2535 | |
| 2536 | free_io_pool_and_out: |
| 2537 | mempool_destroy(pools->io_pool); |
| 2538 | |
| 2539 | free_pools_and_out: |
| 2540 | kfree(pools); |
| 2541 | |
| 2542 | return NULL; |
| 2543 | } |
| 2544 | |
| 2545 | void dm_free_md_mempools(struct dm_md_mempools *pools) |
| 2546 | { |
| 2547 | if (!pools) |
| 2548 | return; |
| 2549 | |
| 2550 | if (pools->io_pool) |
| 2551 | mempool_destroy(pools->io_pool); |
| 2552 | |
| 2553 | if (pools->tio_pool) |
| 2554 | mempool_destroy(pools->tio_pool); |
| 2555 | |
| 2556 | if (pools->bs) |
| 2557 | bioset_free(pools->bs); |
| 2558 | |
| 2559 | kfree(pools); |
| 2560 | } |
| 2561 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 2562 | static const struct block_device_operations dm_blk_dops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | .open = dm_blk_open, |
| 2564 | .release = dm_blk_close, |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 2565 | .ioctl = dm_blk_ioctl, |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 2566 | .getgeo = dm_blk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | .owner = THIS_MODULE |
| 2568 | }; |
| 2569 | |
| 2570 | EXPORT_SYMBOL(dm_get_mapinfo); |
| 2571 | |
| 2572 | /* |
| 2573 | * module hooks |
| 2574 | */ |
| 2575 | module_init(dm_init); |
| 2576 | module_exit(dm_exit); |
| 2577 | |
| 2578 | module_param(major, uint, 0); |
| 2579 | MODULE_PARM_DESC(major, "The major number of the device mapper"); |
| 2580 | MODULE_DESCRIPTION(DM_NAME " driver"); |
| 2581 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); |
| 2582 | MODULE_LICENSE("GPL"); |