Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. | |
784aae73 | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
1da177e4 LT |
4 | * |
5 | * This file is released under the GPL. | |
6 | */ | |
7 | ||
8 | #include "dm.h" | |
51e5b2bd | 9 | #include "dm-uevent.h" |
1da177e4 LT |
10 | |
11 | #include <linux/init.h> | |
12 | #include <linux/module.h> | |
48c9c27b | 13 | #include <linux/mutex.h> |
1da177e4 LT |
14 | #include <linux/moduleparam.h> |
15 | #include <linux/blkpg.h> | |
16 | #include <linux/bio.h> | |
1da177e4 LT |
17 | #include <linux/mempool.h> |
18 | #include <linux/slab.h> | |
19 | #include <linux/idr.h> | |
3ac51e74 | 20 | #include <linux/hdreg.h> |
3f77316d | 21 | #include <linux/delay.h> |
ffcc3936 | 22 | #include <linux/wait.h> |
2eb6e1e3 | 23 | #include <linux/kthread.h> |
0ce65797 | 24 | #include <linux/ktime.h> |
de3ec86d | 25 | #include <linux/elevator.h> /* for rq_end_sector() */ |
bfebd1cd | 26 | #include <linux/blk-mq.h> |
71cdb697 | 27 | #include <linux/pr.h> |
55782138 LZ |
28 | |
29 | #include <trace/events/block.h> | |
1da177e4 | 30 | |
72d94861 AK |
31 | #define DM_MSG_PREFIX "core" |
32 | ||
71a16736 NK |
33 | #ifdef CONFIG_PRINTK |
34 | /* | |
35 | * ratelimit state to be used in DMXXX_LIMIT(). | |
36 | */ | |
37 | DEFINE_RATELIMIT_STATE(dm_ratelimit_state, | |
38 | DEFAULT_RATELIMIT_INTERVAL, | |
39 | DEFAULT_RATELIMIT_BURST); | |
40 | EXPORT_SYMBOL(dm_ratelimit_state); | |
41 | #endif | |
42 | ||
60935eb2 MB |
43 | /* |
44 | * Cookies are numeric values sent with CHANGE and REMOVE | |
45 | * uevents while resuming, removing or renaming the device. | |
46 | */ | |
47 | #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" | |
48 | #define DM_COOKIE_LENGTH 24 | |
49 | ||
1da177e4 LT |
50 | static const char *_name = DM_NAME; |
51 | ||
52 | static unsigned int major = 0; | |
53 | static unsigned int _major = 0; | |
54 | ||
d15b774c AK |
55 | static DEFINE_IDR(_minor_idr); |
56 | ||
f32c10b0 | 57 | static DEFINE_SPINLOCK(_minor_lock); |
2c140a24 MP |
58 | |
59 | static void do_deferred_remove(struct work_struct *w); | |
60 | ||
61 | static DECLARE_WORK(deferred_remove_work, do_deferred_remove); | |
62 | ||
acfe0ad7 MP |
63 | static struct workqueue_struct *deferred_remove_workqueue; |
64 | ||
1da177e4 | 65 | /* |
8fbf26ad | 66 | * For bio-based dm. |
1da177e4 LT |
67 | * One of these is allocated per bio. |
68 | */ | |
69 | struct dm_io { | |
70 | struct mapped_device *md; | |
71 | int error; | |
1da177e4 | 72 | atomic_t io_count; |
6ae2fa67 | 73 | struct bio *bio; |
3eaf840e | 74 | unsigned long start_time; |
f88fb981 | 75 | spinlock_t endio_lock; |
fd2ed4d2 | 76 | struct dm_stats_aux stats_aux; |
1da177e4 LT |
77 | }; |
78 | ||
8fbf26ad KU |
79 | /* |
80 | * For request-based dm. | |
81 | * One of these is allocated per request. | |
82 | */ | |
83 | struct dm_rq_target_io { | |
84 | struct mapped_device *md; | |
85 | struct dm_target *ti; | |
1ae49ea2 | 86 | struct request *orig, *clone; |
2eb6e1e3 | 87 | struct kthread_work work; |
8fbf26ad KU |
88 | int error; |
89 | union map_info info; | |
e262f347 MP |
90 | struct dm_stats_aux stats_aux; |
91 | unsigned long duration_jiffies; | |
92 | unsigned n_sectors; | |
8fbf26ad KU |
93 | }; |
94 | ||
95 | /* | |
94818742 KO |
96 | * For request-based dm - the bio clones we allocate are embedded in these |
97 | * structs. | |
98 | * | |
99 | * We allocate these with bio_alloc_bioset, using the front_pad parameter when | |
100 | * the bioset is created - this means the bio has to come at the end of the | |
101 | * struct. | |
8fbf26ad KU |
102 | */ |
103 | struct dm_rq_clone_bio_info { | |
104 | struct bio *orig; | |
cec47e3d | 105 | struct dm_rq_target_io *tio; |
94818742 | 106 | struct bio clone; |
8fbf26ad KU |
107 | }; |
108 | ||
ba61fdd1 JM |
109 | #define MINOR_ALLOCED ((void *)-1) |
110 | ||
1da177e4 LT |
111 | /* |
112 | * Bits for the md->flags field. | |
113 | */ | |
1eb787ec | 114 | #define DMF_BLOCK_IO_FOR_SUSPEND 0 |
1da177e4 | 115 | #define DMF_SUSPENDED 1 |
aa8d7c2f | 116 | #define DMF_FROZEN 2 |
fba9f90e | 117 | #define DMF_FREEING 3 |
5c6bd75d | 118 | #define DMF_DELETING 4 |
2e93ccc1 | 119 | #define DMF_NOFLUSH_SUSPENDING 5 |
8ae12666 KO |
120 | #define DMF_DEFERRED_REMOVE 6 |
121 | #define DMF_SUSPENDED_INTERNALLY 7 | |
1da177e4 | 122 | |
83d5e5b0 MP |
123 | /* |
124 | * A dummy definition to make RCU happy. | |
125 | * struct dm_table should never be dereferenced in this file. | |
126 | */ | |
127 | struct dm_table { | |
128 | int undefined__; | |
129 | }; | |
130 | ||
304f3f6a MB |
131 | /* |
132 | * Work processed by per-device workqueue. | |
133 | */ | |
1da177e4 | 134 | struct mapped_device { |
83d5e5b0 | 135 | struct srcu_struct io_barrier; |
e61290a4 | 136 | struct mutex suspend_lock; |
1da177e4 | 137 | atomic_t holders; |
5c6bd75d | 138 | atomic_t open_count; |
1da177e4 | 139 | |
2a7faeb1 MP |
140 | /* |
141 | * The current mapping. | |
142 | * Use dm_get_live_table{_fast} or take suspend_lock for | |
143 | * dereference. | |
144 | */ | |
6fa99520 | 145 | struct dm_table __rcu *map; |
2a7faeb1 | 146 | |
86f1152b BM |
147 | struct list_head table_devices; |
148 | struct mutex table_devices_lock; | |
149 | ||
1da177e4 LT |
150 | unsigned long flags; |
151 | ||
165125e1 | 152 | struct request_queue *queue; |
115485e8 MS |
153 | int numa_node_id; |
154 | ||
a5664dad | 155 | unsigned type; |
4a0b4ddf | 156 | /* Protect queue and type against concurrent access. */ |
a5664dad MS |
157 | struct mutex type_lock; |
158 | ||
16f12266 | 159 | struct dm_target *immutable_target; |
36a0456f AK |
160 | struct target_type *immutable_target_type; |
161 | ||
1da177e4 | 162 | struct gendisk *disk; |
7e51f257 | 163 | char name[16]; |
1da177e4 LT |
164 | |
165 | void *interface_ptr; | |
166 | ||
167 | /* | |
168 | * A list of ios that arrived while we were suspended. | |
169 | */ | |
316d315b | 170 | atomic_t pending[2]; |
1da177e4 | 171 | wait_queue_head_t wait; |
53d5914f | 172 | struct work_struct work; |
74859364 | 173 | struct bio_list deferred; |
022c2611 | 174 | spinlock_t deferred_lock; |
1da177e4 | 175 | |
af7e466a | 176 | /* |
29e4013d | 177 | * Processing queue (flush) |
304f3f6a MB |
178 | */ |
179 | struct workqueue_struct *wq; | |
180 | ||
1da177e4 LT |
181 | /* |
182 | * io objects are allocated from here. | |
183 | */ | |
184 | mempool_t *io_pool; | |
1ae49ea2 | 185 | mempool_t *rq_pool; |
1da177e4 | 186 | |
9faf400f SB |
187 | struct bio_set *bs; |
188 | ||
1da177e4 LT |
189 | /* |
190 | * Event handling. | |
191 | */ | |
192 | atomic_t event_nr; | |
193 | wait_queue_head_t eventq; | |
7a8c3d3b MA |
194 | atomic_t uevent_seq; |
195 | struct list_head uevent_list; | |
196 | spinlock_t uevent_lock; /* Protect access to uevent_list */ | |
1da177e4 LT |
197 | |
198 | /* | |
199 | * freeze/thaw support require holding onto a super block | |
200 | */ | |
201 | struct super_block *frozen_sb; | |
db8fef4f | 202 | struct block_device *bdev; |
3ac51e74 DW |
203 | |
204 | /* forced geometry settings */ | |
205 | struct hd_geometry geometry; | |
784aae73 | 206 | |
2995fa78 MP |
207 | /* kobject and completion */ |
208 | struct dm_kobject_holder kobj_holder; | |
be35f486 | 209 | |
d87f4c14 TH |
210 | /* zero-length flush that will be cloned and submitted to targets */ |
211 | struct bio flush_bio; | |
fd2ed4d2 | 212 | |
96b26c8c MP |
213 | /* the number of internal suspends */ |
214 | unsigned internal_suspend_count; | |
215 | ||
fd2ed4d2 | 216 | struct dm_stats stats; |
2eb6e1e3 KB |
217 | |
218 | struct kthread_worker kworker; | |
219 | struct task_struct *kworker_task; | |
de3ec86d MS |
220 | |
221 | /* for request-based merge heuristic in dm_request_fn() */ | |
0ce65797 | 222 | unsigned seq_rq_merge_deadline_usecs; |
de3ec86d | 223 | int last_rq_rw; |
0ce65797 MS |
224 | sector_t last_rq_pos; |
225 | ktime_t last_rq_start_time; | |
bfebd1cd MS |
226 | |
227 | /* for blk-mq request-based DM support */ | |
1c357a1e | 228 | struct blk_mq_tag_set *tag_set; |
591ddcfc MS |
229 | bool use_blk_mq:1; |
230 | bool init_tio_pdu:1; | |
1da177e4 LT |
231 | }; |
232 | ||
17e149b8 MS |
233 | #ifdef CONFIG_DM_MQ_DEFAULT |
234 | static bool use_blk_mq = true; | |
235 | #else | |
236 | static bool use_blk_mq = false; | |
237 | #endif | |
238 | ||
faad87df MS |
239 | #define DM_MQ_NR_HW_QUEUES 1 |
240 | #define DM_MQ_QUEUE_DEPTH 2048 | |
115485e8 | 241 | #define DM_NUMA_NODE NUMA_NO_NODE |
faad87df MS |
242 | |
243 | static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES; | |
244 | static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH; | |
115485e8 | 245 | static int dm_numa_node = DM_NUMA_NODE; |
faad87df | 246 | |
17e149b8 MS |
247 | bool dm_use_blk_mq(struct mapped_device *md) |
248 | { | |
249 | return md->use_blk_mq; | |
250 | } | |
591ddcfc | 251 | EXPORT_SYMBOL_GPL(dm_use_blk_mq); |
17e149b8 | 252 | |
e6ee8c0b KU |
253 | /* |
254 | * For mempools pre-allocation at the table loading time. | |
255 | */ | |
256 | struct dm_md_mempools { | |
257 | mempool_t *io_pool; | |
1ae49ea2 | 258 | mempool_t *rq_pool; |
e6ee8c0b KU |
259 | struct bio_set *bs; |
260 | }; | |
261 | ||
86f1152b BM |
262 | struct table_device { |
263 | struct list_head list; | |
264 | atomic_t count; | |
265 | struct dm_dev dm_dev; | |
266 | }; | |
267 | ||
6cfa5857 MS |
268 | #define RESERVED_BIO_BASED_IOS 16 |
269 | #define RESERVED_REQUEST_BASED_IOS 256 | |
f4790826 | 270 | #define RESERVED_MAX_IOS 1024 |
e18b890b | 271 | static struct kmem_cache *_io_cache; |
8fbf26ad | 272 | static struct kmem_cache *_rq_tio_cache; |
1ae49ea2 | 273 | static struct kmem_cache *_rq_cache; |
94818742 | 274 | |
e8603136 MS |
275 | /* |
276 | * Bio-based DM's mempools' reserved IOs set by the user. | |
277 | */ | |
278 | static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; | |
279 | ||
f4790826 MS |
280 | /* |
281 | * Request-based DM's mempools' reserved IOs set by the user. | |
282 | */ | |
283 | static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS; | |
284 | ||
115485e8 MS |
285 | static int __dm_get_module_param_int(int *module_param, int min, int max) |
286 | { | |
287 | int param = ACCESS_ONCE(*module_param); | |
288 | int modified_param = 0; | |
289 | bool modified = true; | |
290 | ||
291 | if (param < min) | |
292 | modified_param = min; | |
293 | else if (param > max) | |
294 | modified_param = max; | |
295 | else | |
296 | modified = false; | |
297 | ||
298 | if (modified) { | |
299 | (void)cmpxchg(module_param, param, modified_param); | |
300 | param = modified_param; | |
301 | } | |
302 | ||
303 | return param; | |
304 | } | |
305 | ||
09c2d531 | 306 | static unsigned __dm_get_module_param(unsigned *module_param, |
f4790826 MS |
307 | unsigned def, unsigned max) |
308 | { | |
09c2d531 MS |
309 | unsigned param = ACCESS_ONCE(*module_param); |
310 | unsigned modified_param = 0; | |
f4790826 | 311 | |
09c2d531 MS |
312 | if (!param) |
313 | modified_param = def; | |
314 | else if (param > max) | |
315 | modified_param = max; | |
f4790826 | 316 | |
09c2d531 MS |
317 | if (modified_param) { |
318 | (void)cmpxchg(module_param, param, modified_param); | |
319 | param = modified_param; | |
f4790826 MS |
320 | } |
321 | ||
09c2d531 | 322 | return param; |
f4790826 MS |
323 | } |
324 | ||
e8603136 MS |
325 | unsigned dm_get_reserved_bio_based_ios(void) |
326 | { | |
09c2d531 | 327 | return __dm_get_module_param(&reserved_bio_based_ios, |
e8603136 MS |
328 | RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS); |
329 | } | |
330 | EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); | |
331 | ||
f4790826 MS |
332 | unsigned dm_get_reserved_rq_based_ios(void) |
333 | { | |
09c2d531 | 334 | return __dm_get_module_param(&reserved_rq_based_ios, |
f4790826 MS |
335 | RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS); |
336 | } | |
337 | EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios); | |
338 | ||
faad87df MS |
339 | static unsigned dm_get_blk_mq_nr_hw_queues(void) |
340 | { | |
341 | return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32); | |
342 | } | |
343 | ||
344 | static unsigned dm_get_blk_mq_queue_depth(void) | |
345 | { | |
346 | return __dm_get_module_param(&dm_mq_queue_depth, | |
347 | DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH); | |
348 | } | |
349 | ||
115485e8 MS |
350 | static unsigned dm_get_numa_node(void) |
351 | { | |
352 | return __dm_get_module_param_int(&dm_numa_node, | |
353 | DM_NUMA_NODE, num_online_nodes() - 1); | |
354 | } | |
355 | ||
1da177e4 LT |
356 | static int __init local_init(void) |
357 | { | |
51157b4a | 358 | int r = -ENOMEM; |
1da177e4 | 359 | |
1da177e4 | 360 | /* allocate a slab for the dm_ios */ |
028867ac | 361 | _io_cache = KMEM_CACHE(dm_io, 0); |
1da177e4 | 362 | if (!_io_cache) |
51157b4a | 363 | return r; |
1da177e4 | 364 | |
8fbf26ad KU |
365 | _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); |
366 | if (!_rq_tio_cache) | |
dba14160 | 367 | goto out_free_io_cache; |
8fbf26ad | 368 | |
eca7ee6d | 369 | _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request), |
1ae49ea2 MS |
370 | __alignof__(struct request), 0, NULL); |
371 | if (!_rq_cache) | |
372 | goto out_free_rq_tio_cache; | |
373 | ||
51e5b2bd | 374 | r = dm_uevent_init(); |
51157b4a | 375 | if (r) |
1ae49ea2 | 376 | goto out_free_rq_cache; |
51e5b2bd | 377 | |
acfe0ad7 MP |
378 | deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); |
379 | if (!deferred_remove_workqueue) { | |
380 | r = -ENOMEM; | |
381 | goto out_uevent_exit; | |
382 | } | |
383 | ||
1da177e4 LT |
384 | _major = major; |
385 | r = register_blkdev(_major, _name); | |
51157b4a | 386 | if (r < 0) |
acfe0ad7 | 387 | goto out_free_workqueue; |
1da177e4 LT |
388 | |
389 | if (!_major) | |
390 | _major = r; | |
391 | ||
392 | return 0; | |
51157b4a | 393 | |
acfe0ad7 MP |
394 | out_free_workqueue: |
395 | destroy_workqueue(deferred_remove_workqueue); | |
51157b4a KU |
396 | out_uevent_exit: |
397 | dm_uevent_exit(); | |
1ae49ea2 MS |
398 | out_free_rq_cache: |
399 | kmem_cache_destroy(_rq_cache); | |
8fbf26ad KU |
400 | out_free_rq_tio_cache: |
401 | kmem_cache_destroy(_rq_tio_cache); | |
51157b4a KU |
402 | out_free_io_cache: |
403 | kmem_cache_destroy(_io_cache); | |
404 | ||
405 | return r; | |
1da177e4 LT |
406 | } |
407 | ||
408 | static void local_exit(void) | |
409 | { | |
2c140a24 | 410 | flush_scheduled_work(); |
acfe0ad7 | 411 | destroy_workqueue(deferred_remove_workqueue); |
2c140a24 | 412 | |
1ae49ea2 | 413 | kmem_cache_destroy(_rq_cache); |
8fbf26ad | 414 | kmem_cache_destroy(_rq_tio_cache); |
1da177e4 | 415 | kmem_cache_destroy(_io_cache); |
00d59405 | 416 | unregister_blkdev(_major, _name); |
51e5b2bd | 417 | dm_uevent_exit(); |
1da177e4 LT |
418 | |
419 | _major = 0; | |
420 | ||
421 | DMINFO("cleaned up"); | |
422 | } | |
423 | ||
b9249e55 | 424 | static int (*_inits[])(void) __initdata = { |
1da177e4 LT |
425 | local_init, |
426 | dm_target_init, | |
427 | dm_linear_init, | |
428 | dm_stripe_init, | |
952b3557 | 429 | dm_io_init, |
945fa4d2 | 430 | dm_kcopyd_init, |
1da177e4 | 431 | dm_interface_init, |
fd2ed4d2 | 432 | dm_statistics_init, |
1da177e4 LT |
433 | }; |
434 | ||
b9249e55 | 435 | static void (*_exits[])(void) = { |
1da177e4 LT |
436 | local_exit, |
437 | dm_target_exit, | |
438 | dm_linear_exit, | |
439 | dm_stripe_exit, | |
952b3557 | 440 | dm_io_exit, |
945fa4d2 | 441 | dm_kcopyd_exit, |
1da177e4 | 442 | dm_interface_exit, |
fd2ed4d2 | 443 | dm_statistics_exit, |
1da177e4 LT |
444 | }; |
445 | ||
446 | static int __init dm_init(void) | |
447 | { | |
448 | const int count = ARRAY_SIZE(_inits); | |
449 | ||
450 | int r, i; | |
451 | ||
452 | for (i = 0; i < count; i++) { | |
453 | r = _inits[i](); | |
454 | if (r) | |
455 | goto bad; | |
456 | } | |
457 | ||
458 | return 0; | |
459 | ||
460 | bad: | |
461 | while (i--) | |
462 | _exits[i](); | |
463 | ||
464 | return r; | |
465 | } | |
466 | ||
467 | static void __exit dm_exit(void) | |
468 | { | |
469 | int i = ARRAY_SIZE(_exits); | |
470 | ||
471 | while (i--) | |
472 | _exits[i](); | |
d15b774c AK |
473 | |
474 | /* | |
475 | * Should be empty by this point. | |
476 | */ | |
d15b774c | 477 | idr_destroy(&_minor_idr); |
1da177e4 LT |
478 | } |
479 | ||
480 | /* | |
481 | * Block device functions | |
482 | */ | |
432a212c MA |
483 | int dm_deleting_md(struct mapped_device *md) |
484 | { | |
485 | return test_bit(DMF_DELETING, &md->flags); | |
486 | } | |
487 | ||
fe5f9f2c | 488 | static int dm_blk_open(struct block_device *bdev, fmode_t mode) |
1da177e4 LT |
489 | { |
490 | struct mapped_device *md; | |
491 | ||
fba9f90e JM |
492 | spin_lock(&_minor_lock); |
493 | ||
fe5f9f2c | 494 | md = bdev->bd_disk->private_data; |
fba9f90e JM |
495 | if (!md) |
496 | goto out; | |
497 | ||
5c6bd75d | 498 | if (test_bit(DMF_FREEING, &md->flags) || |
432a212c | 499 | dm_deleting_md(md)) { |
fba9f90e JM |
500 | md = NULL; |
501 | goto out; | |
502 | } | |
503 | ||
1da177e4 | 504 | dm_get(md); |
5c6bd75d | 505 | atomic_inc(&md->open_count); |
fba9f90e JM |
506 | out: |
507 | spin_unlock(&_minor_lock); | |
508 | ||
509 | return md ? 0 : -ENXIO; | |
1da177e4 LT |
510 | } |
511 | ||
db2a144b | 512 | static void dm_blk_close(struct gendisk *disk, fmode_t mode) |
1da177e4 | 513 | { |
63a4f065 | 514 | struct mapped_device *md; |
6e9624b8 | 515 | |
4a1aeb98 MB |
516 | spin_lock(&_minor_lock); |
517 | ||
63a4f065 MS |
518 | md = disk->private_data; |
519 | if (WARN_ON(!md)) | |
520 | goto out; | |
521 | ||
2c140a24 MP |
522 | if (atomic_dec_and_test(&md->open_count) && |
523 | (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) | |
acfe0ad7 | 524 | queue_work(deferred_remove_workqueue, &deferred_remove_work); |
2c140a24 | 525 | |
1da177e4 | 526 | dm_put(md); |
63a4f065 | 527 | out: |
4a1aeb98 | 528 | spin_unlock(&_minor_lock); |
1da177e4 LT |
529 | } |
530 | ||
5c6bd75d AK |
531 | int dm_open_count(struct mapped_device *md) |
532 | { | |
533 | return atomic_read(&md->open_count); | |
534 | } | |
535 | ||
536 | /* | |
537 | * Guarantees nothing is using the device before it's deleted. | |
538 | */ | |
2c140a24 | 539 | int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) |
5c6bd75d AK |
540 | { |
541 | int r = 0; | |
542 | ||
543 | spin_lock(&_minor_lock); | |
544 | ||
2c140a24 | 545 | if (dm_open_count(md)) { |
5c6bd75d | 546 | r = -EBUSY; |
2c140a24 MP |
547 | if (mark_deferred) |
548 | set_bit(DMF_DEFERRED_REMOVE, &md->flags); | |
549 | } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) | |
550 | r = -EEXIST; | |
5c6bd75d AK |
551 | else |
552 | set_bit(DMF_DELETING, &md->flags); | |
553 | ||
554 | spin_unlock(&_minor_lock); | |
555 | ||
556 | return r; | |
557 | } | |
558 | ||
2c140a24 MP |
559 | int dm_cancel_deferred_remove(struct mapped_device *md) |
560 | { | |
561 | int r = 0; | |
562 | ||
563 | spin_lock(&_minor_lock); | |
564 | ||
565 | if (test_bit(DMF_DELETING, &md->flags)) | |
566 | r = -EBUSY; | |
567 | else | |
568 | clear_bit(DMF_DEFERRED_REMOVE, &md->flags); | |
569 | ||
570 | spin_unlock(&_minor_lock); | |
571 | ||
572 | return r; | |
573 | } | |
574 | ||
575 | static void do_deferred_remove(struct work_struct *w) | |
576 | { | |
577 | dm_deferred_remove(); | |
578 | } | |
579 | ||
fd2ed4d2 MP |
580 | sector_t dm_get_size(struct mapped_device *md) |
581 | { | |
582 | return get_capacity(md->disk); | |
583 | } | |
584 | ||
9974fa2c MS |
585 | struct request_queue *dm_get_md_queue(struct mapped_device *md) |
586 | { | |
587 | return md->queue; | |
588 | } | |
589 | ||
fd2ed4d2 MP |
590 | struct dm_stats *dm_get_stats(struct mapped_device *md) |
591 | { | |
592 | return &md->stats; | |
593 | } | |
594 | ||
3ac51e74 DW |
595 | static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
596 | { | |
597 | struct mapped_device *md = bdev->bd_disk->private_data; | |
598 | ||
599 | return dm_get_geometry(md, geo); | |
600 | } | |
601 | ||
956a4025 MS |
602 | static int dm_grab_bdev_for_ioctl(struct mapped_device *md, |
603 | struct block_device **bdev, | |
604 | fmode_t *mode) | |
aa129a22 | 605 | { |
66482026 | 606 | struct dm_target *tgt; |
6c182cd8 | 607 | struct dm_table *map; |
956a4025 | 608 | int srcu_idx, r; |
aa129a22 | 609 | |
6c182cd8 | 610 | retry: |
e56f81e0 | 611 | r = -ENOTTY; |
956a4025 | 612 | map = dm_get_live_table(md, &srcu_idx); |
aa129a22 MB |
613 | if (!map || !dm_table_get_size(map)) |
614 | goto out; | |
615 | ||
616 | /* We only support devices that have a single target */ | |
617 | if (dm_table_get_num_targets(map) != 1) | |
618 | goto out; | |
619 | ||
66482026 MS |
620 | tgt = dm_table_get_target(map, 0); |
621 | if (!tgt->type->prepare_ioctl) | |
4d341d82 | 622 | goto out; |
aa129a22 | 623 | |
4f186f8b | 624 | if (dm_suspended_md(md)) { |
aa129a22 MB |
625 | r = -EAGAIN; |
626 | goto out; | |
627 | } | |
628 | ||
66482026 | 629 | r = tgt->type->prepare_ioctl(tgt, bdev, mode); |
e56f81e0 CH |
630 | if (r < 0) |
631 | goto out; | |
aa129a22 | 632 | |
956a4025 MS |
633 | bdgrab(*bdev); |
634 | dm_put_live_table(md, srcu_idx); | |
e56f81e0 | 635 | return r; |
aa129a22 | 636 | |
aa129a22 | 637 | out: |
956a4025 | 638 | dm_put_live_table(md, srcu_idx); |
5bbbfdf6 | 639 | if (r == -ENOTCONN && !fatal_signal_pending(current)) { |
6c182cd8 HR |
640 | msleep(10); |
641 | goto retry; | |
642 | } | |
e56f81e0 CH |
643 | return r; |
644 | } | |
645 | ||
646 | static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, | |
647 | unsigned int cmd, unsigned long arg) | |
648 | { | |
649 | struct mapped_device *md = bdev->bd_disk->private_data; | |
956a4025 | 650 | int r; |
e56f81e0 | 651 | |
956a4025 | 652 | r = dm_grab_bdev_for_ioctl(md, &bdev, &mode); |
e56f81e0 CH |
653 | if (r < 0) |
654 | return r; | |
6c182cd8 | 655 | |
e56f81e0 CH |
656 | if (r > 0) { |
657 | /* | |
658 | * Target determined this ioctl is being issued against | |
659 | * a logical partition of the parent bdev; so extra | |
660 | * validation is needed. | |
661 | */ | |
662 | r = scsi_verify_blk_ioctl(NULL, cmd); | |
663 | if (r) | |
664 | goto out; | |
665 | } | |
6c182cd8 | 666 | |
66482026 | 667 | r = __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
e56f81e0 | 668 | out: |
956a4025 | 669 | bdput(bdev); |
aa129a22 MB |
670 | return r; |
671 | } | |
672 | ||
028867ac | 673 | static struct dm_io *alloc_io(struct mapped_device *md) |
1da177e4 LT |
674 | { |
675 | return mempool_alloc(md->io_pool, GFP_NOIO); | |
676 | } | |
677 | ||
028867ac | 678 | static void free_io(struct mapped_device *md, struct dm_io *io) |
1da177e4 LT |
679 | { |
680 | mempool_free(io, md->io_pool); | |
681 | } | |
682 | ||
028867ac | 683 | static void free_tio(struct mapped_device *md, struct dm_target_io *tio) |
1da177e4 | 684 | { |
dba14160 | 685 | bio_put(&tio->clone); |
1da177e4 LT |
686 | } |
687 | ||
eca7ee6d MS |
688 | static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md, |
689 | gfp_t gfp_mask) | |
cec47e3d | 690 | { |
5f015204 | 691 | return mempool_alloc(md->io_pool, gfp_mask); |
cec47e3d KU |
692 | } |
693 | ||
eca7ee6d | 694 | static void free_old_rq_tio(struct dm_rq_target_io *tio) |
cec47e3d | 695 | { |
5f015204 | 696 | mempool_free(tio, tio->md->io_pool); |
cec47e3d KU |
697 | } |
698 | ||
eca7ee6d MS |
699 | static struct request *alloc_old_clone_request(struct mapped_device *md, |
700 | gfp_t gfp_mask) | |
1ae49ea2 MS |
701 | { |
702 | return mempool_alloc(md->rq_pool, gfp_mask); | |
703 | } | |
704 | ||
eca7ee6d | 705 | static void free_old_clone_request(struct mapped_device *md, struct request *rq) |
1ae49ea2 MS |
706 | { |
707 | mempool_free(rq, md->rq_pool); | |
708 | } | |
709 | ||
90abb8c4 KU |
710 | static int md_in_flight(struct mapped_device *md) |
711 | { | |
712 | return atomic_read(&md->pending[READ]) + | |
713 | atomic_read(&md->pending[WRITE]); | |
714 | } | |
715 | ||
3eaf840e JNN |
716 | static void start_io_acct(struct dm_io *io) |
717 | { | |
718 | struct mapped_device *md = io->md; | |
fd2ed4d2 | 719 | struct bio *bio = io->bio; |
c9959059 | 720 | int cpu; |
fd2ed4d2 | 721 | int rw = bio_data_dir(bio); |
3eaf840e JNN |
722 | |
723 | io->start_time = jiffies; | |
724 | ||
074a7aca TH |
725 | cpu = part_stat_lock(); |
726 | part_round_stats(cpu, &dm_disk(md)->part0); | |
727 | part_stat_unlock(); | |
1e9bb880 SL |
728 | atomic_set(&dm_disk(md)->part0.in_flight[rw], |
729 | atomic_inc_return(&md->pending[rw])); | |
fd2ed4d2 MP |
730 | |
731 | if (unlikely(dm_stats_used(&md->stats))) | |
4f024f37 | 732 | dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector, |
fd2ed4d2 | 733 | bio_sectors(bio), false, 0, &io->stats_aux); |
3eaf840e JNN |
734 | } |
735 | ||
d221d2e7 | 736 | static void end_io_acct(struct dm_io *io) |
3eaf840e JNN |
737 | { |
738 | struct mapped_device *md = io->md; | |
739 | struct bio *bio = io->bio; | |
740 | unsigned long duration = jiffies - io->start_time; | |
18c0b223 | 741 | int pending; |
3eaf840e JNN |
742 | int rw = bio_data_dir(bio); |
743 | ||
18c0b223 | 744 | generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time); |
3eaf840e | 745 | |
fd2ed4d2 | 746 | if (unlikely(dm_stats_used(&md->stats))) |
4f024f37 | 747 | dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector, |
fd2ed4d2 MP |
748 | bio_sectors(bio), true, duration, &io->stats_aux); |
749 | ||
af7e466a MP |
750 | /* |
751 | * After this is decremented the bio must not be touched if it is | |
d87f4c14 | 752 | * a flush. |
af7e466a | 753 | */ |
1e9bb880 SL |
754 | pending = atomic_dec_return(&md->pending[rw]); |
755 | atomic_set(&dm_disk(md)->part0.in_flight[rw], pending); | |
316d315b | 756 | pending += atomic_read(&md->pending[rw^0x1]); |
3eaf840e | 757 | |
d221d2e7 MP |
758 | /* nudge anyone waiting on suspend queue */ |
759 | if (!pending) | |
760 | wake_up(&md->wait); | |
3eaf840e JNN |
761 | } |
762 | ||
1da177e4 LT |
763 | /* |
764 | * Add the bio to the list of deferred io. | |
765 | */ | |
92c63902 | 766 | static void queue_io(struct mapped_device *md, struct bio *bio) |
1da177e4 | 767 | { |
05447420 | 768 | unsigned long flags; |
1da177e4 | 769 | |
05447420 | 770 | spin_lock_irqsave(&md->deferred_lock, flags); |
1da177e4 | 771 | bio_list_add(&md->deferred, bio); |
05447420 | 772 | spin_unlock_irqrestore(&md->deferred_lock, flags); |
6a8736d1 | 773 | queue_work(md->wq, &md->work); |
1da177e4 LT |
774 | } |
775 | ||
776 | /* | |
777 | * Everyone (including functions in this file), should use this | |
778 | * function to access the md->map field, and make sure they call | |
83d5e5b0 | 779 | * dm_put_live_table() when finished. |
1da177e4 | 780 | */ |
83d5e5b0 | 781 | struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) |
1da177e4 | 782 | { |
83d5e5b0 MP |
783 | *srcu_idx = srcu_read_lock(&md->io_barrier); |
784 | ||
785 | return srcu_dereference(md->map, &md->io_barrier); | |
786 | } | |
1da177e4 | 787 | |
83d5e5b0 MP |
788 | void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) |
789 | { | |
790 | srcu_read_unlock(&md->io_barrier, srcu_idx); | |
791 | } | |
792 | ||
793 | void dm_sync_table(struct mapped_device *md) | |
794 | { | |
795 | synchronize_srcu(&md->io_barrier); | |
796 | synchronize_rcu_expedited(); | |
797 | } | |
798 | ||
799 | /* | |
800 | * A fast alternative to dm_get_live_table/dm_put_live_table. | |
801 | * The caller must not block between these two functions. | |
802 | */ | |
803 | static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) | |
804 | { | |
805 | rcu_read_lock(); | |
806 | return rcu_dereference(md->map); | |
807 | } | |
1da177e4 | 808 | |
83d5e5b0 MP |
809 | static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) |
810 | { | |
811 | rcu_read_unlock(); | |
1da177e4 LT |
812 | } |
813 | ||
86f1152b BM |
814 | /* |
815 | * Open a table device so we can use it as a map destination. | |
816 | */ | |
817 | static int open_table_device(struct table_device *td, dev_t dev, | |
818 | struct mapped_device *md) | |
819 | { | |
820 | static char *_claim_ptr = "I belong to device-mapper"; | |
821 | struct block_device *bdev; | |
822 | ||
823 | int r; | |
824 | ||
825 | BUG_ON(td->dm_dev.bdev); | |
826 | ||
827 | bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr); | |
828 | if (IS_ERR(bdev)) | |
829 | return PTR_ERR(bdev); | |
830 | ||
831 | r = bd_link_disk_holder(bdev, dm_disk(md)); | |
832 | if (r) { | |
833 | blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); | |
834 | return r; | |
835 | } | |
836 | ||
837 | td->dm_dev.bdev = bdev; | |
838 | return 0; | |
839 | } | |
840 | ||
841 | /* | |
842 | * Close a table device that we've been using. | |
843 | */ | |
844 | static void close_table_device(struct table_device *td, struct mapped_device *md) | |
845 | { | |
846 | if (!td->dm_dev.bdev) | |
847 | return; | |
848 | ||
849 | bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); | |
850 | blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); | |
851 | td->dm_dev.bdev = NULL; | |
852 | } | |
853 | ||
854 | static struct table_device *find_table_device(struct list_head *l, dev_t dev, | |
855 | fmode_t mode) { | |
856 | struct table_device *td; | |
857 | ||
858 | list_for_each_entry(td, l, list) | |
859 | if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) | |
860 | return td; | |
861 | ||
862 | return NULL; | |
863 | } | |
864 | ||
865 | int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, | |
866 | struct dm_dev **result) { | |
867 | int r; | |
868 | struct table_device *td; | |
869 | ||
870 | mutex_lock(&md->table_devices_lock); | |
871 | td = find_table_device(&md->table_devices, dev, mode); | |
872 | if (!td) { | |
115485e8 | 873 | td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); |
86f1152b BM |
874 | if (!td) { |
875 | mutex_unlock(&md->table_devices_lock); | |
876 | return -ENOMEM; | |
877 | } | |
878 | ||
879 | td->dm_dev.mode = mode; | |
880 | td->dm_dev.bdev = NULL; | |
881 | ||
882 | if ((r = open_table_device(td, dev, md))) { | |
883 | mutex_unlock(&md->table_devices_lock); | |
884 | kfree(td); | |
885 | return r; | |
886 | } | |
887 | ||
888 | format_dev_t(td->dm_dev.name, dev); | |
889 | ||
890 | atomic_set(&td->count, 0); | |
891 | list_add(&td->list, &md->table_devices); | |
892 | } | |
893 | atomic_inc(&td->count); | |
894 | mutex_unlock(&md->table_devices_lock); | |
895 | ||
896 | *result = &td->dm_dev; | |
897 | return 0; | |
898 | } | |
899 | EXPORT_SYMBOL_GPL(dm_get_table_device); | |
900 | ||
901 | void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) | |
902 | { | |
903 | struct table_device *td = container_of(d, struct table_device, dm_dev); | |
904 | ||
905 | mutex_lock(&md->table_devices_lock); | |
906 | if (atomic_dec_and_test(&td->count)) { | |
907 | close_table_device(td, md); | |
908 | list_del(&td->list); | |
909 | kfree(td); | |
910 | } | |
911 | mutex_unlock(&md->table_devices_lock); | |
912 | } | |
913 | EXPORT_SYMBOL(dm_put_table_device); | |
914 | ||
915 | static void free_table_devices(struct list_head *devices) | |
916 | { | |
917 | struct list_head *tmp, *next; | |
918 | ||
919 | list_for_each_safe(tmp, next, devices) { | |
920 | struct table_device *td = list_entry(tmp, struct table_device, list); | |
921 | ||
922 | DMWARN("dm_destroy: %s still exists with %d references", | |
923 | td->dm_dev.name, atomic_read(&td->count)); | |
924 | kfree(td); | |
925 | } | |
926 | } | |
927 | ||
3ac51e74 DW |
928 | /* |
929 | * Get the geometry associated with a dm device | |
930 | */ | |
931 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) | |
932 | { | |
933 | *geo = md->geometry; | |
934 | ||
935 | return 0; | |
936 | } | |
937 | ||
938 | /* | |
939 | * Set the geometry of a device. | |
940 | */ | |
941 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) | |
942 | { | |
943 | sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; | |
944 | ||
945 | if (geo->start > sz) { | |
946 | DMWARN("Start sector is beyond the geometry limits."); | |
947 | return -EINVAL; | |
948 | } | |
949 | ||
950 | md->geometry = *geo; | |
951 | ||
952 | return 0; | |
953 | } | |
954 | ||
1da177e4 LT |
955 | /*----------------------------------------------------------------- |
956 | * CRUD START: | |
957 | * A more elegant soln is in the works that uses the queue | |
958 | * merge fn, unfortunately there are a couple of changes to | |
959 | * the block layer that I want to make for this. So in the | |
960 | * interests of getting something for people to use I give | |
961 | * you this clearly demarcated crap. | |
962 | *---------------------------------------------------------------*/ | |
963 | ||
2e93ccc1 KU |
964 | static int __noflush_suspending(struct mapped_device *md) |
965 | { | |
966 | return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); | |
967 | } | |
968 | ||
1da177e4 LT |
969 | /* |
970 | * Decrements the number of outstanding ios that a bio has been | |
971 | * cloned into, completing the original io if necc. | |
972 | */ | |
858119e1 | 973 | static void dec_pending(struct dm_io *io, int error) |
1da177e4 | 974 | { |
2e93ccc1 | 975 | unsigned long flags; |
b35f8caa MB |
976 | int io_error; |
977 | struct bio *bio; | |
978 | struct mapped_device *md = io->md; | |
2e93ccc1 KU |
979 | |
980 | /* Push-back supersedes any I/O errors */ | |
f88fb981 KU |
981 | if (unlikely(error)) { |
982 | spin_lock_irqsave(&io->endio_lock, flags); | |
983 | if (!(io->error > 0 && __noflush_suspending(md))) | |
984 | io->error = error; | |
985 | spin_unlock_irqrestore(&io->endio_lock, flags); | |
986 | } | |
1da177e4 LT |
987 | |
988 | if (atomic_dec_and_test(&io->io_count)) { | |
2e93ccc1 KU |
989 | if (io->error == DM_ENDIO_REQUEUE) { |
990 | /* | |
991 | * Target requested pushing back the I/O. | |
2e93ccc1 | 992 | */ |
022c2611 | 993 | spin_lock_irqsave(&md->deferred_lock, flags); |
6a8736d1 TH |
994 | if (__noflush_suspending(md)) |
995 | bio_list_add_head(&md->deferred, io->bio); | |
996 | else | |
2e93ccc1 KU |
997 | /* noflush suspend was interrupted. */ |
998 | io->error = -EIO; | |
022c2611 | 999 | spin_unlock_irqrestore(&md->deferred_lock, flags); |
2e93ccc1 KU |
1000 | } |
1001 | ||
b35f8caa MB |
1002 | io_error = io->error; |
1003 | bio = io->bio; | |
6a8736d1 TH |
1004 | end_io_acct(io); |
1005 | free_io(md, io); | |
1006 | ||
1007 | if (io_error == DM_ENDIO_REQUEUE) | |
1008 | return; | |
2e93ccc1 | 1009 | |
4f024f37 | 1010 | if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) { |
af7e466a | 1011 | /* |
6a8736d1 TH |
1012 | * Preflush done for flush with data, reissue |
1013 | * without REQ_FLUSH. | |
af7e466a | 1014 | */ |
6a8736d1 TH |
1015 | bio->bi_rw &= ~REQ_FLUSH; |
1016 | queue_io(md, bio); | |
af7e466a | 1017 | } else { |
b372d360 | 1018 | /* done with normal IO or empty flush */ |
0a82a8d1 | 1019 | trace_block_bio_complete(md->queue, bio, io_error); |
4246a0b6 CH |
1020 | bio->bi_error = io_error; |
1021 | bio_endio(bio); | |
b35f8caa | 1022 | } |
1da177e4 LT |
1023 | } |
1024 | } | |
1025 | ||
7eee4ae2 MS |
1026 | static void disable_write_same(struct mapped_device *md) |
1027 | { | |
1028 | struct queue_limits *limits = dm_get_queue_limits(md); | |
1029 | ||
1030 | /* device doesn't really support WRITE SAME, disable it */ | |
1031 | limits->max_write_same_sectors = 0; | |
1032 | } | |
1033 | ||
4246a0b6 | 1034 | static void clone_endio(struct bio *bio) |
1da177e4 | 1035 | { |
4246a0b6 | 1036 | int error = bio->bi_error; |
5164bece | 1037 | int r = error; |
bfc6d41c | 1038 | struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); |
b35f8caa | 1039 | struct dm_io *io = tio->io; |
9faf400f | 1040 | struct mapped_device *md = tio->io->md; |
1da177e4 LT |
1041 | dm_endio_fn endio = tio->ti->type->end_io; |
1042 | ||
1da177e4 | 1043 | if (endio) { |
7de3ee57 | 1044 | r = endio(tio->ti, bio, error); |
2e93ccc1 KU |
1045 | if (r < 0 || r == DM_ENDIO_REQUEUE) |
1046 | /* | |
1047 | * error and requeue request are handled | |
1048 | * in dec_pending(). | |
1049 | */ | |
1da177e4 | 1050 | error = r; |
45cbcd79 KU |
1051 | else if (r == DM_ENDIO_INCOMPLETE) |
1052 | /* The target will handle the io */ | |
6712ecf8 | 1053 | return; |
45cbcd79 KU |
1054 | else if (r) { |
1055 | DMWARN("unimplemented target endio return value: %d", r); | |
1056 | BUG(); | |
1057 | } | |
1da177e4 LT |
1058 | } |
1059 | ||
7eee4ae2 MS |
1060 | if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) && |
1061 | !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors)) | |
1062 | disable_write_same(md); | |
1063 | ||
9faf400f | 1064 | free_tio(md, tio); |
b35f8caa | 1065 | dec_pending(io, error); |
1da177e4 LT |
1066 | } |
1067 | ||
78d8e58a MS |
1068 | /* |
1069 | * Partial completion handling for request-based dm | |
1070 | */ | |
4246a0b6 | 1071 | static void end_clone_bio(struct bio *clone) |
78d8e58a MS |
1072 | { |
1073 | struct dm_rq_clone_bio_info *info = | |
1074 | container_of(clone, struct dm_rq_clone_bio_info, clone); | |
1075 | struct dm_rq_target_io *tio = info->tio; | |
1076 | struct bio *bio = info->orig; | |
1077 | unsigned int nr_bytes = info->orig->bi_iter.bi_size; | |
50887bd1 | 1078 | int error = clone->bi_error; |
78d8e58a MS |
1079 | |
1080 | bio_put(clone); | |
1081 | ||
1082 | if (tio->error) | |
1083 | /* | |
1084 | * An error has already been detected on the request. | |
1085 | * Once error occurred, just let clone->end_io() handle | |
1086 | * the remainder. | |
1087 | */ | |
1088 | return; | |
50887bd1 | 1089 | else if (error) { |
78d8e58a MS |
1090 | /* |
1091 | * Don't notice the error to the upper layer yet. | |
1092 | * The error handling decision is made by the target driver, | |
1093 | * when the request is completed. | |
1094 | */ | |
50887bd1 | 1095 | tio->error = error; |
78d8e58a MS |
1096 | return; |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * I/O for the bio successfully completed. | |
1101 | * Notice the data completion to the upper layer. | |
1102 | */ | |
1103 | ||
1104 | /* | |
1105 | * bios are processed from the head of the list. | |
1106 | * So the completing bio should always be rq->bio. | |
1107 | * If it's not, something wrong is happening. | |
1108 | */ | |
1109 | if (tio->orig->bio != bio) | |
1110 | DMERR("bio completion is going in the middle of the request"); | |
1111 | ||
1112 | /* | |
1113 | * Update the original request. | |
1114 | * Do not use blk_end_request() here, because it may complete | |
1115 | * the original request before the clone, and break the ordering. | |
1116 | */ | |
1117 | blk_update_request(tio->orig, 0, nr_bytes); | |
1118 | } | |
1119 | ||
bfebd1cd MS |
1120 | static struct dm_rq_target_io *tio_from_request(struct request *rq) |
1121 | { | |
1122 | return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special); | |
1123 | } | |
1124 | ||
e262f347 MP |
1125 | static void rq_end_stats(struct mapped_device *md, struct request *orig) |
1126 | { | |
1127 | if (unlikely(dm_stats_used(&md->stats))) { | |
1128 | struct dm_rq_target_io *tio = tio_from_request(orig); | |
1129 | tio->duration_jiffies = jiffies - tio->duration_jiffies; | |
1130 | dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig), | |
1131 | tio->n_sectors, true, tio->duration_jiffies, | |
1132 | &tio->stats_aux); | |
1133 | } | |
1134 | } | |
1135 | ||
cec47e3d KU |
1136 | /* |
1137 | * Don't touch any member of the md after calling this function because | |
1138 | * the md may be freed in dm_put() at the end of this function. | |
1139 | * Or do dm_get() before calling this function and dm_put() later. | |
1140 | */ | |
466d89a6 | 1141 | static void rq_completed(struct mapped_device *md, int rw, bool run_queue) |
cec47e3d | 1142 | { |
b4324fee | 1143 | atomic_dec(&md->pending[rw]); |
cec47e3d KU |
1144 | |
1145 | /* nudge anyone waiting on suspend queue */ | |
621739b0 | 1146 | if (!md_in_flight(md)) |
cec47e3d KU |
1147 | wake_up(&md->wait); |
1148 | ||
a8c32a5c JA |
1149 | /* |
1150 | * Run this off this callpath, as drivers could invoke end_io while | |
1151 | * inside their request_fn (and holding the queue lock). Calling | |
1152 | * back into ->request_fn() could deadlock attempting to grab the | |
1153 | * queue lock again. | |
1154 | */ | |
6acfe68b MS |
1155 | if (!md->queue->mq_ops && run_queue) |
1156 | blk_run_queue_async(md->queue); | |
cec47e3d KU |
1157 | |
1158 | /* | |
1159 | * dm_put() must be at the end of this function. See the comment above | |
1160 | */ | |
1161 | dm_put(md); | |
1162 | } | |
1163 | ||
e5d8de32 | 1164 | static void free_rq_clone(struct request *clone) |
a77e28c7 KU |
1165 | { |
1166 | struct dm_rq_target_io *tio = clone->end_io_data; | |
bfebd1cd | 1167 | struct mapped_device *md = tio->md; |
a77e28c7 | 1168 | |
78d8e58a MS |
1169 | blk_rq_unprep_clone(clone); |
1170 | ||
aa6df8dd MS |
1171 | if (md->type == DM_TYPE_MQ_REQUEST_BASED) |
1172 | /* stacked on blk-mq queue(s) */ | |
e5863d9a | 1173 | tio->ti->type->release_clone_rq(clone); |
02233342 MS |
1174 | else if (!md->queue->mq_ops) |
1175 | /* request_fn queue stacked on request_fn queue(s) */ | |
eca7ee6d | 1176 | free_old_clone_request(md, clone); |
bfebd1cd MS |
1177 | |
1178 | if (!md->queue->mq_ops) | |
eca7ee6d | 1179 | free_old_rq_tio(tio); |
a77e28c7 KU |
1180 | } |
1181 | ||
980691e5 KU |
1182 | /* |
1183 | * Complete the clone and the original request. | |
466d89a6 KB |
1184 | * Must be called without clone's queue lock held, |
1185 | * see end_clone_request() for more details. | |
980691e5 KU |
1186 | */ |
1187 | static void dm_end_request(struct request *clone, int error) | |
1188 | { | |
1189 | int rw = rq_data_dir(clone); | |
1190 | struct dm_rq_target_io *tio = clone->end_io_data; | |
1191 | struct mapped_device *md = tio->md; | |
1192 | struct request *rq = tio->orig; | |
1193 | ||
29e4013d | 1194 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { |
980691e5 KU |
1195 | rq->errors = clone->errors; |
1196 | rq->resid_len = clone->resid_len; | |
1197 | ||
1198 | if (rq->sense) | |
1199 | /* | |
1200 | * We are using the sense buffer of the original | |
1201 | * request. | |
1202 | * So setting the length of the sense data is enough. | |
1203 | */ | |
1204 | rq->sense_len = clone->sense_len; | |
1205 | } | |
1206 | ||
e5d8de32 | 1207 | free_rq_clone(clone); |
e262f347 | 1208 | rq_end_stats(md, rq); |
bfebd1cd MS |
1209 | if (!rq->q->mq_ops) |
1210 | blk_end_request_all(rq, error); | |
1211 | else | |
1212 | blk_mq_end_request(rq, error); | |
29e4013d | 1213 | rq_completed(md, rw, true); |
980691e5 KU |
1214 | } |
1215 | ||
cec47e3d KU |
1216 | static void dm_unprep_request(struct request *rq) |
1217 | { | |
bfebd1cd | 1218 | struct dm_rq_target_io *tio = tio_from_request(rq); |
466d89a6 | 1219 | struct request *clone = tio->clone; |
cec47e3d | 1220 | |
bfebd1cd MS |
1221 | if (!rq->q->mq_ops) { |
1222 | rq->special = NULL; | |
1223 | rq->cmd_flags &= ~REQ_DONTPREP; | |
1224 | } | |
cec47e3d | 1225 | |
e5863d9a | 1226 | if (clone) |
e5d8de32 | 1227 | free_rq_clone(clone); |
4328daa2 | 1228 | else if (!tio->md->queue->mq_ops) |
eca7ee6d | 1229 | free_old_rq_tio(tio); |
cec47e3d KU |
1230 | } |
1231 | ||
1232 | /* | |
1233 | * Requeue the original request of a clone. | |
1234 | */ | |
eca7ee6d | 1235 | static void dm_old_requeue_request(struct request *rq) |
cec47e3d | 1236 | { |
cec47e3d KU |
1237 | struct request_queue *q = rq->q; |
1238 | unsigned long flags; | |
1239 | ||
cec47e3d | 1240 | spin_lock_irqsave(q->queue_lock, flags); |
cec47e3d | 1241 | blk_requeue_request(q, rq); |
4ae9944d | 1242 | blk_run_queue_async(q); |
cec47e3d | 1243 | spin_unlock_irqrestore(q->queue_lock, flags); |
bfebd1cd MS |
1244 | } |
1245 | ||
818c5f3b MS |
1246 | static void dm_mq_requeue_request(struct request *rq) |
1247 | { | |
1248 | struct request_queue *q = rq->q; | |
1249 | unsigned long flags; | |
1250 | ||
1251 | blk_mq_requeue_request(rq); | |
1252 | spin_lock_irqsave(q->queue_lock, flags); | |
1253 | if (!blk_queue_stopped(q)) | |
1254 | blk_mq_kick_requeue_list(q); | |
1255 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1256 | } | |
1257 | ||
2d76fff1 MS |
1258 | static void dm_requeue_original_request(struct mapped_device *md, |
1259 | struct request *rq) | |
bfebd1cd MS |
1260 | { |
1261 | int rw = rq_data_dir(rq); | |
1262 | ||
1263 | dm_unprep_request(rq); | |
1264 | ||
e262f347 | 1265 | rq_end_stats(md, rq); |
bfebd1cd | 1266 | if (!rq->q->mq_ops) |
eca7ee6d | 1267 | dm_old_requeue_request(rq); |
818c5f3b MS |
1268 | else |
1269 | dm_mq_requeue_request(rq); | |
cec47e3d | 1270 | |
466d89a6 KB |
1271 | rq_completed(md, rw, false); |
1272 | } | |
1273 | ||
eca7ee6d | 1274 | static void dm_old_stop_queue(struct request_queue *q) |
cec47e3d KU |
1275 | { |
1276 | unsigned long flags; | |
1277 | ||
818c5f3b MS |
1278 | spin_lock_irqsave(q->queue_lock, flags); |
1279 | if (blk_queue_stopped(q)) { | |
1280 | spin_unlock_irqrestore(q->queue_lock, flags); | |
bfebd1cd | 1281 | return; |
818c5f3b | 1282 | } |
bfebd1cd | 1283 | |
bfebd1cd | 1284 | blk_stop_queue(q); |
cec47e3d KU |
1285 | spin_unlock_irqrestore(q->queue_lock, flags); |
1286 | } | |
1287 | ||
eca7ee6d | 1288 | static void dm_stop_queue(struct request_queue *q) |
cec47e3d | 1289 | { |
bfebd1cd | 1290 | if (!q->mq_ops) |
eca7ee6d | 1291 | dm_old_stop_queue(q); |
bfebd1cd MS |
1292 | else |
1293 | blk_mq_stop_hw_queues(q); | |
cec47e3d KU |
1294 | } |
1295 | ||
eca7ee6d | 1296 | static void dm_old_start_queue(struct request_queue *q) |
cec47e3d KU |
1297 | { |
1298 | unsigned long flags; | |
1299 | ||
1300 | spin_lock_irqsave(q->queue_lock, flags); | |
bfebd1cd MS |
1301 | if (blk_queue_stopped(q)) |
1302 | blk_start_queue(q); | |
cec47e3d KU |
1303 | spin_unlock_irqrestore(q->queue_lock, flags); |
1304 | } | |
1305 | ||
eca7ee6d | 1306 | static void dm_start_queue(struct request_queue *q) |
bfebd1cd MS |
1307 | { |
1308 | if (!q->mq_ops) | |
eca7ee6d | 1309 | dm_old_start_queue(q); |
818c5f3b | 1310 | else { |
bfebd1cd | 1311 | blk_mq_start_stopped_hw_queues(q, true); |
818c5f3b MS |
1312 | blk_mq_kick_requeue_list(q); |
1313 | } | |
bfebd1cd MS |
1314 | } |
1315 | ||
11a68244 | 1316 | static void dm_done(struct request *clone, int error, bool mapped) |
cec47e3d | 1317 | { |
11a68244 | 1318 | int r = error; |
cec47e3d | 1319 | struct dm_rq_target_io *tio = clone->end_io_data; |
ba1cbad9 | 1320 | dm_request_endio_fn rq_end_io = NULL; |
cec47e3d | 1321 | |
ba1cbad9 MS |
1322 | if (tio->ti) { |
1323 | rq_end_io = tio->ti->type->rq_end_io; | |
1324 | ||
1325 | if (mapped && rq_end_io) | |
1326 | r = rq_end_io(tio->ti, clone, error, &tio->info); | |
1327 | } | |
cec47e3d | 1328 | |
7eee4ae2 MS |
1329 | if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) && |
1330 | !clone->q->limits.max_write_same_sectors)) | |
1331 | disable_write_same(tio->md); | |
1332 | ||
11a68244 | 1333 | if (r <= 0) |
cec47e3d | 1334 | /* The target wants to complete the I/O */ |
11a68244 KU |
1335 | dm_end_request(clone, r); |
1336 | else if (r == DM_ENDIO_INCOMPLETE) | |
cec47e3d KU |
1337 | /* The target will handle the I/O */ |
1338 | return; | |
11a68244 | 1339 | else if (r == DM_ENDIO_REQUEUE) |
cec47e3d | 1340 | /* The target wants to requeue the I/O */ |
2d76fff1 | 1341 | dm_requeue_original_request(tio->md, tio->orig); |
cec47e3d | 1342 | else { |
11a68244 | 1343 | DMWARN("unimplemented target endio return value: %d", r); |
cec47e3d KU |
1344 | BUG(); |
1345 | } | |
1346 | } | |
1347 | ||
11a68244 KU |
1348 | /* |
1349 | * Request completion handler for request-based dm | |
1350 | */ | |
1351 | static void dm_softirq_done(struct request *rq) | |
1352 | { | |
1353 | bool mapped = true; | |
bfebd1cd | 1354 | struct dm_rq_target_io *tio = tio_from_request(rq); |
466d89a6 | 1355 | struct request *clone = tio->clone; |
bfebd1cd | 1356 | int rw; |
11a68244 | 1357 | |
e5863d9a | 1358 | if (!clone) { |
e262f347 | 1359 | rq_end_stats(tio->md, rq); |
bfebd1cd MS |
1360 | rw = rq_data_dir(rq); |
1361 | if (!rq->q->mq_ops) { | |
1362 | blk_end_request_all(rq, tio->error); | |
1363 | rq_completed(tio->md, rw, false); | |
eca7ee6d | 1364 | free_old_rq_tio(tio); |
bfebd1cd MS |
1365 | } else { |
1366 | blk_mq_end_request(rq, tio->error); | |
1367 | rq_completed(tio->md, rw, false); | |
1368 | } | |
e5863d9a MS |
1369 | return; |
1370 | } | |
11a68244 KU |
1371 | |
1372 | if (rq->cmd_flags & REQ_FAILED) | |
1373 | mapped = false; | |
1374 | ||
1375 | dm_done(clone, tio->error, mapped); | |
1376 | } | |
1377 | ||
cec47e3d KU |
1378 | /* |
1379 | * Complete the clone and the original request with the error status | |
1380 | * through softirq context. | |
1381 | */ | |
466d89a6 | 1382 | static void dm_complete_request(struct request *rq, int error) |
cec47e3d | 1383 | { |
bfebd1cd | 1384 | struct dm_rq_target_io *tio = tio_from_request(rq); |
cec47e3d KU |
1385 | |
1386 | tio->error = error; | |
6acfe68b MS |
1387 | if (!rq->q->mq_ops) |
1388 | blk_complete_request(rq); | |
1389 | else | |
1390 | blk_mq_complete_request(rq, error); | |
cec47e3d KU |
1391 | } |
1392 | ||
1393 | /* | |
1394 | * Complete the not-mapped clone and the original request with the error status | |
1395 | * through softirq context. | |
1396 | * Target's rq_end_io() function isn't called. | |
e5863d9a | 1397 | * This may be used when the target's map_rq() or clone_and_map_rq() functions fail. |
cec47e3d | 1398 | */ |
466d89a6 | 1399 | static void dm_kill_unmapped_request(struct request *rq, int error) |
cec47e3d | 1400 | { |
cec47e3d | 1401 | rq->cmd_flags |= REQ_FAILED; |
466d89a6 | 1402 | dm_complete_request(rq, error); |
cec47e3d | 1403 | } |
cec47e3d KU |
1404 | |
1405 | /* | |
eca7ee6d | 1406 | * Called with the clone's queue lock held (in the case of .request_fn) |
cec47e3d KU |
1407 | */ |
1408 | static void end_clone_request(struct request *clone, int error) | |
1409 | { | |
466d89a6 KB |
1410 | struct dm_rq_target_io *tio = clone->end_io_data; |
1411 | ||
e5863d9a MS |
1412 | if (!clone->q->mq_ops) { |
1413 | /* | |
1414 | * For just cleaning up the information of the queue in which | |
1415 | * the clone was dispatched. | |
1416 | * The clone is *NOT* freed actually here because it is alloced | |
1417 | * from dm own mempool (REQ_ALLOCED isn't set). | |
1418 | */ | |
1419 | __blk_put_request(clone->q, clone); | |
1420 | } | |
cec47e3d KU |
1421 | |
1422 | /* | |
1423 | * Actual request completion is done in a softirq context which doesn't | |
466d89a6 | 1424 | * hold the clone's queue lock. Otherwise, deadlock could occur because: |
cec47e3d KU |
1425 | * - another request may be submitted by the upper level driver |
1426 | * of the stacking during the completion | |
1427 | * - the submission which requires queue lock may be done | |
466d89a6 | 1428 | * against this clone's queue |
cec47e3d | 1429 | */ |
466d89a6 | 1430 | dm_complete_request(tio->orig, error); |
cec47e3d KU |
1431 | } |
1432 | ||
56a67df7 MS |
1433 | /* |
1434 | * Return maximum size of I/O possible at the supplied sector up to the current | |
1435 | * target boundary. | |
1436 | */ | |
1437 | static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) | |
1438 | { | |
1439 | sector_t target_offset = dm_target_offset(ti, sector); | |
1440 | ||
1441 | return ti->len - target_offset; | |
1442 | } | |
1443 | ||
1444 | static sector_t max_io_len(sector_t sector, struct dm_target *ti) | |
1da177e4 | 1445 | { |
56a67df7 | 1446 | sector_t len = max_io_len_target_boundary(sector, ti); |
542f9038 | 1447 | sector_t offset, max_len; |
1da177e4 LT |
1448 | |
1449 | /* | |
542f9038 | 1450 | * Does the target need to split even further? |
1da177e4 | 1451 | */ |
542f9038 MS |
1452 | if (ti->max_io_len) { |
1453 | offset = dm_target_offset(ti, sector); | |
1454 | if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) | |
1455 | max_len = sector_div(offset, ti->max_io_len); | |
1456 | else | |
1457 | max_len = offset & (ti->max_io_len - 1); | |
1458 | max_len = ti->max_io_len - max_len; | |
1459 | ||
1460 | if (len > max_len) | |
1461 | len = max_len; | |
1da177e4 LT |
1462 | } |
1463 | ||
1464 | return len; | |
1465 | } | |
1466 | ||
542f9038 MS |
1467 | int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) |
1468 | { | |
1469 | if (len > UINT_MAX) { | |
1470 | DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", | |
1471 | (unsigned long long)len, UINT_MAX); | |
1472 | ti->error = "Maximum size of target IO is too large"; | |
1473 | return -EINVAL; | |
1474 | } | |
1475 | ||
1476 | ti->max_io_len = (uint32_t) len; | |
1477 | ||
1478 | return 0; | |
1479 | } | |
1480 | EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); | |
1481 | ||
1dd40c3e MP |
1482 | /* |
1483 | * A target may call dm_accept_partial_bio only from the map routine. It is | |
1484 | * allowed for all bio types except REQ_FLUSH. | |
1485 | * | |
1486 | * dm_accept_partial_bio informs the dm that the target only wants to process | |
1487 | * additional n_sectors sectors of the bio and the rest of the data should be | |
1488 | * sent in a next bio. | |
1489 | * | |
1490 | * A diagram that explains the arithmetics: | |
1491 | * +--------------------+---------------+-------+ | |
1492 | * | 1 | 2 | 3 | | |
1493 | * +--------------------+---------------+-------+ | |
1494 | * | |
1495 | * <-------------- *tio->len_ptr ---------------> | |
1496 | * <------- bi_size -------> | |
1497 | * <-- n_sectors --> | |
1498 | * | |
1499 | * Region 1 was already iterated over with bio_advance or similar function. | |
1500 | * (it may be empty if the target doesn't use bio_advance) | |
1501 | * Region 2 is the remaining bio size that the target wants to process. | |
1502 | * (it may be empty if region 1 is non-empty, although there is no reason | |
1503 | * to make it empty) | |
1504 | * The target requires that region 3 is to be sent in the next bio. | |
1505 | * | |
1506 | * If the target wants to receive multiple copies of the bio (via num_*bios, etc), | |
1507 | * the partially processed part (the sum of regions 1+2) must be the same for all | |
1508 | * copies of the bio. | |
1509 | */ | |
1510 | void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) | |
1511 | { | |
1512 | struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); | |
1513 | unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; | |
1514 | BUG_ON(bio->bi_rw & REQ_FLUSH); | |
1515 | BUG_ON(bi_size > *tio->len_ptr); | |
1516 | BUG_ON(n_sectors > bi_size); | |
1517 | *tio->len_ptr -= bi_size - n_sectors; | |
1518 | bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; | |
1519 | } | |
1520 | EXPORT_SYMBOL_GPL(dm_accept_partial_bio); | |
1521 | ||
bd2a49b8 | 1522 | static void __map_bio(struct dm_target_io *tio) |
1da177e4 LT |
1523 | { |
1524 | int r; | |
2056a782 | 1525 | sector_t sector; |
9faf400f | 1526 | struct mapped_device *md; |
dba14160 | 1527 | struct bio *clone = &tio->clone; |
bd2a49b8 | 1528 | struct dm_target *ti = tio->ti; |
1da177e4 | 1529 | |
1da177e4 | 1530 | clone->bi_end_io = clone_endio; |
1da177e4 LT |
1531 | |
1532 | /* | |
1533 | * Map the clone. If r == 0 we don't need to do | |
1534 | * anything, the target has assumed ownership of | |
1535 | * this io. | |
1536 | */ | |
1537 | atomic_inc(&tio->io->io_count); | |
4f024f37 | 1538 | sector = clone->bi_iter.bi_sector; |
7de3ee57 | 1539 | r = ti->type->map(ti, clone); |
45cbcd79 | 1540 | if (r == DM_MAPIO_REMAPPED) { |
1da177e4 | 1541 | /* the bio has been remapped so dispatch it */ |
2056a782 | 1542 | |
d07335e5 MS |
1543 | trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone, |
1544 | tio->io->bio->bi_bdev->bd_dev, sector); | |
2056a782 | 1545 | |
1da177e4 | 1546 | generic_make_request(clone); |
2e93ccc1 KU |
1547 | } else if (r < 0 || r == DM_MAPIO_REQUEUE) { |
1548 | /* error the io and bail out, or requeue it if needed */ | |
9faf400f SB |
1549 | md = tio->io->md; |
1550 | dec_pending(tio->io, r); | |
9faf400f | 1551 | free_tio(md, tio); |
ab37844d | 1552 | } else if (r != DM_MAPIO_SUBMITTED) { |
45cbcd79 KU |
1553 | DMWARN("unimplemented target map return value: %d", r); |
1554 | BUG(); | |
1da177e4 LT |
1555 | } |
1556 | } | |
1557 | ||
1558 | struct clone_info { | |
1559 | struct mapped_device *md; | |
1560 | struct dm_table *map; | |
1561 | struct bio *bio; | |
1562 | struct dm_io *io; | |
1563 | sector_t sector; | |
e0d6609a | 1564 | unsigned sector_count; |
1da177e4 LT |
1565 | }; |
1566 | ||
e0d6609a | 1567 | static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) |
bd2a49b8 | 1568 | { |
4f024f37 KO |
1569 | bio->bi_iter.bi_sector = sector; |
1570 | bio->bi_iter.bi_size = to_bytes(len); | |
1da177e4 LT |
1571 | } |
1572 | ||
1573 | /* | |
1574 | * Creates a bio that consists of range of complete bvecs. | |
1575 | */ | |
dba14160 | 1576 | static void clone_bio(struct dm_target_io *tio, struct bio *bio, |
1c3b13e6 | 1577 | sector_t sector, unsigned len) |
1da177e4 | 1578 | { |
dba14160 | 1579 | struct bio *clone = &tio->clone; |
1da177e4 | 1580 | |
1c3b13e6 KO |
1581 | __bio_clone_fast(clone, bio); |
1582 | ||
1583 | if (bio_integrity(bio)) | |
1584 | bio_integrity_clone(clone, bio, GFP_NOIO); | |
bd2a49b8 | 1585 | |
1c3b13e6 KO |
1586 | bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); |
1587 | clone->bi_iter.bi_size = to_bytes(len); | |
1588 | ||
1589 | if (bio_integrity(bio)) | |
1590 | bio_integrity_trim(clone, 0, len); | |
1da177e4 LT |
1591 | } |
1592 | ||
9015df24 | 1593 | static struct dm_target_io *alloc_tio(struct clone_info *ci, |
99778273 | 1594 | struct dm_target *ti, |
55a62eef | 1595 | unsigned target_bio_nr) |
f9ab94ce | 1596 | { |
dba14160 MP |
1597 | struct dm_target_io *tio; |
1598 | struct bio *clone; | |
1599 | ||
99778273 | 1600 | clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs); |
dba14160 | 1601 | tio = container_of(clone, struct dm_target_io, clone); |
f9ab94ce MP |
1602 | |
1603 | tio->io = ci->io; | |
1604 | tio->ti = ti; | |
55a62eef | 1605 | tio->target_bio_nr = target_bio_nr; |
9015df24 AK |
1606 | |
1607 | return tio; | |
1608 | } | |
1609 | ||
14fe594d AK |
1610 | static void __clone_and_map_simple_bio(struct clone_info *ci, |
1611 | struct dm_target *ti, | |
1dd40c3e | 1612 | unsigned target_bio_nr, unsigned *len) |
9015df24 | 1613 | { |
99778273 | 1614 | struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr); |
dba14160 | 1615 | struct bio *clone = &tio->clone; |
9015df24 | 1616 | |
1dd40c3e MP |
1617 | tio->len_ptr = len; |
1618 | ||
99778273 | 1619 | __bio_clone_fast(clone, ci->bio); |
bd2a49b8 | 1620 | if (len) |
1dd40c3e | 1621 | bio_setup_sector(clone, ci->sector, *len); |
f9ab94ce | 1622 | |
bd2a49b8 | 1623 | __map_bio(tio); |
f9ab94ce MP |
1624 | } |
1625 | ||
14fe594d | 1626 | static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, |
1dd40c3e | 1627 | unsigned num_bios, unsigned *len) |
06a426ce | 1628 | { |
55a62eef | 1629 | unsigned target_bio_nr; |
06a426ce | 1630 | |
55a62eef | 1631 | for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++) |
14fe594d | 1632 | __clone_and_map_simple_bio(ci, ti, target_bio_nr, len); |
06a426ce MS |
1633 | } |
1634 | ||
14fe594d | 1635 | static int __send_empty_flush(struct clone_info *ci) |
f9ab94ce | 1636 | { |
06a426ce | 1637 | unsigned target_nr = 0; |
f9ab94ce MP |
1638 | struct dm_target *ti; |
1639 | ||
b372d360 | 1640 | BUG_ON(bio_has_data(ci->bio)); |
f9ab94ce | 1641 | while ((ti = dm_table_get_target(ci->map, target_nr++))) |
1dd40c3e | 1642 | __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); |
f9ab94ce | 1643 | |
f9ab94ce MP |
1644 | return 0; |
1645 | } | |
1646 | ||
e4c93811 | 1647 | static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, |
1dd40c3e | 1648 | sector_t sector, unsigned *len) |
5ae89a87 | 1649 | { |
dba14160 | 1650 | struct bio *bio = ci->bio; |
5ae89a87 | 1651 | struct dm_target_io *tio; |
b0d8ed4d AK |
1652 | unsigned target_bio_nr; |
1653 | unsigned num_target_bios = 1; | |
5ae89a87 | 1654 | |
b0d8ed4d AK |
1655 | /* |
1656 | * Does the target want to receive duplicate copies of the bio? | |
1657 | */ | |
1658 | if (bio_data_dir(bio) == WRITE && ti->num_write_bios) | |
1659 | num_target_bios = ti->num_write_bios(ti, bio); | |
e4c93811 | 1660 | |
b0d8ed4d | 1661 | for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) { |
99778273 | 1662 | tio = alloc_tio(ci, ti, target_bio_nr); |
1dd40c3e MP |
1663 | tio->len_ptr = len; |
1664 | clone_bio(tio, bio, sector, *len); | |
b0d8ed4d AK |
1665 | __map_bio(tio); |
1666 | } | |
5ae89a87 MS |
1667 | } |
1668 | ||
55a62eef | 1669 | typedef unsigned (*get_num_bios_fn)(struct dm_target *ti); |
23508a96 | 1670 | |
55a62eef | 1671 | static unsigned get_num_discard_bios(struct dm_target *ti) |
23508a96 | 1672 | { |
55a62eef | 1673 | return ti->num_discard_bios; |
23508a96 MS |
1674 | } |
1675 | ||
55a62eef | 1676 | static unsigned get_num_write_same_bios(struct dm_target *ti) |
23508a96 | 1677 | { |
55a62eef | 1678 | return ti->num_write_same_bios; |
23508a96 MS |
1679 | } |
1680 | ||
1681 | typedef bool (*is_split_required_fn)(struct dm_target *ti); | |
1682 | ||
1683 | static bool is_split_required_for_discard(struct dm_target *ti) | |
1684 | { | |
55a62eef | 1685 | return ti->split_discard_bios; |
23508a96 MS |
1686 | } |
1687 | ||
14fe594d AK |
1688 | static int __send_changing_extent_only(struct clone_info *ci, |
1689 | get_num_bios_fn get_num_bios, | |
1690 | is_split_required_fn is_split_required) | |
5ae89a87 MS |
1691 | { |
1692 | struct dm_target *ti; | |
e0d6609a | 1693 | unsigned len; |
55a62eef | 1694 | unsigned num_bios; |
5ae89a87 | 1695 | |
a79245b3 MS |
1696 | do { |
1697 | ti = dm_table_find_target(ci->map, ci->sector); | |
1698 | if (!dm_target_is_valid(ti)) | |
1699 | return -EIO; | |
5ae89a87 | 1700 | |
5ae89a87 | 1701 | /* |
23508a96 MS |
1702 | * Even though the device advertised support for this type of |
1703 | * request, that does not mean every target supports it, and | |
936688d7 | 1704 | * reconfiguration might also have changed that since the |
a79245b3 | 1705 | * check was performed. |
5ae89a87 | 1706 | */ |
55a62eef AK |
1707 | num_bios = get_num_bios ? get_num_bios(ti) : 0; |
1708 | if (!num_bios) | |
a79245b3 | 1709 | return -EOPNOTSUPP; |
5ae89a87 | 1710 | |
23508a96 | 1711 | if (is_split_required && !is_split_required(ti)) |
e0d6609a | 1712 | len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); |
7acf0277 | 1713 | else |
e0d6609a | 1714 | len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti)); |
06a426ce | 1715 | |
1dd40c3e | 1716 | __send_duplicate_bios(ci, ti, num_bios, &len); |
a79245b3 MS |
1717 | |
1718 | ci->sector += len; | |
1719 | } while (ci->sector_count -= len); | |
5ae89a87 MS |
1720 | |
1721 | return 0; | |
1722 | } | |
1723 | ||
14fe594d | 1724 | static int __send_discard(struct clone_info *ci) |
23508a96 | 1725 | { |
14fe594d AK |
1726 | return __send_changing_extent_only(ci, get_num_discard_bios, |
1727 | is_split_required_for_discard); | |
23508a96 MS |
1728 | } |
1729 | ||
14fe594d | 1730 | static int __send_write_same(struct clone_info *ci) |
23508a96 | 1731 | { |
14fe594d | 1732 | return __send_changing_extent_only(ci, get_num_write_same_bios, NULL); |
23508a96 MS |
1733 | } |
1734 | ||
e4c93811 AK |
1735 | /* |
1736 | * Select the correct strategy for processing a non-flush bio. | |
1737 | */ | |
14fe594d | 1738 | static int __split_and_process_non_flush(struct clone_info *ci) |
1da177e4 | 1739 | { |
dba14160 | 1740 | struct bio *bio = ci->bio; |
512875bd | 1741 | struct dm_target *ti; |
1c3b13e6 | 1742 | unsigned len; |
1da177e4 | 1743 | |
5ae89a87 | 1744 | if (unlikely(bio->bi_rw & REQ_DISCARD)) |
14fe594d | 1745 | return __send_discard(ci); |
23508a96 | 1746 | else if (unlikely(bio->bi_rw & REQ_WRITE_SAME)) |
14fe594d | 1747 | return __send_write_same(ci); |
5ae89a87 | 1748 | |
512875bd JN |
1749 | ti = dm_table_find_target(ci->map, ci->sector); |
1750 | if (!dm_target_is_valid(ti)) | |
1751 | return -EIO; | |
1752 | ||
1c3b13e6 | 1753 | len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count); |
1da177e4 | 1754 | |
1dd40c3e | 1755 | __clone_and_map_data_bio(ci, ti, ci->sector, &len); |
1da177e4 | 1756 | |
1c3b13e6 KO |
1757 | ci->sector += len; |
1758 | ci->sector_count -= len; | |
1da177e4 | 1759 | |
1c3b13e6 | 1760 | return 0; |
1da177e4 LT |
1761 | } |
1762 | ||
1763 | /* | |
14fe594d | 1764 | * Entry point to split a bio into clones and submit them to the targets. |
1da177e4 | 1765 | */ |
83d5e5b0 MP |
1766 | static void __split_and_process_bio(struct mapped_device *md, |
1767 | struct dm_table *map, struct bio *bio) | |
1da177e4 LT |
1768 | { |
1769 | struct clone_info ci; | |
512875bd | 1770 | int error = 0; |
1da177e4 | 1771 | |
83d5e5b0 | 1772 | if (unlikely(!map)) { |
6a8736d1 | 1773 | bio_io_error(bio); |
f0b9a450 MP |
1774 | return; |
1775 | } | |
692d0eb9 | 1776 | |
83d5e5b0 | 1777 | ci.map = map; |
1da177e4 | 1778 | ci.md = md; |
1da177e4 LT |
1779 | ci.io = alloc_io(md); |
1780 | ci.io->error = 0; | |
1781 | atomic_set(&ci.io->io_count, 1); | |
1782 | ci.io->bio = bio; | |
1783 | ci.io->md = md; | |
f88fb981 | 1784 | spin_lock_init(&ci.io->endio_lock); |
4f024f37 | 1785 | ci.sector = bio->bi_iter.bi_sector; |
1da177e4 | 1786 | |
3eaf840e | 1787 | start_io_acct(ci.io); |
bd2a49b8 | 1788 | |
b372d360 MS |
1789 | if (bio->bi_rw & REQ_FLUSH) { |
1790 | ci.bio = &ci.md->flush_bio; | |
1791 | ci.sector_count = 0; | |
14fe594d | 1792 | error = __send_empty_flush(&ci); |
b372d360 MS |
1793 | /* dec_pending submits any data associated with flush */ |
1794 | } else { | |
6a8736d1 | 1795 | ci.bio = bio; |
d87f4c14 | 1796 | ci.sector_count = bio_sectors(bio); |
b372d360 | 1797 | while (ci.sector_count && !error) |
14fe594d | 1798 | error = __split_and_process_non_flush(&ci); |
d87f4c14 | 1799 | } |
1da177e4 LT |
1800 | |
1801 | /* drop the extra reference count */ | |
512875bd | 1802 | dec_pending(ci.io, error); |
1da177e4 LT |
1803 | } |
1804 | /*----------------------------------------------------------------- | |
1805 | * CRUD END | |
1806 | *---------------------------------------------------------------*/ | |
1807 | ||
1808 | /* | |
1809 | * The request function that just remaps the bio built up by | |
1810 | * dm_merge_bvec. | |
1811 | */ | |
dece1635 | 1812 | static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio) |
1da177e4 | 1813 | { |
12f03a49 | 1814 | int rw = bio_data_dir(bio); |
1da177e4 | 1815 | struct mapped_device *md = q->queuedata; |
83d5e5b0 MP |
1816 | int srcu_idx; |
1817 | struct dm_table *map; | |
1da177e4 | 1818 | |
83d5e5b0 | 1819 | map = dm_get_live_table(md, &srcu_idx); |
1da177e4 | 1820 | |
18c0b223 | 1821 | generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0); |
12f03a49 | 1822 | |
6a8736d1 TH |
1823 | /* if we're suspended, we have to queue this io for later */ |
1824 | if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { | |
83d5e5b0 | 1825 | dm_put_live_table(md, srcu_idx); |
1da177e4 | 1826 | |
6a8736d1 TH |
1827 | if (bio_rw(bio) != READA) |
1828 | queue_io(md, bio); | |
1829 | else | |
54d9a1b4 | 1830 | bio_io_error(bio); |
dece1635 | 1831 | return BLK_QC_T_NONE; |
1da177e4 LT |
1832 | } |
1833 | ||
83d5e5b0 MP |
1834 | __split_and_process_bio(md, map, bio); |
1835 | dm_put_live_table(md, srcu_idx); | |
dece1635 | 1836 | return BLK_QC_T_NONE; |
cec47e3d KU |
1837 | } |
1838 | ||
fd2ed4d2 | 1839 | int dm_request_based(struct mapped_device *md) |
cec47e3d KU |
1840 | { |
1841 | return blk_queue_stackable(md->queue); | |
1842 | } | |
1843 | ||
466d89a6 | 1844 | static void dm_dispatch_clone_request(struct request *clone, struct request *rq) |
cec47e3d KU |
1845 | { |
1846 | int r; | |
1847 | ||
466d89a6 KB |
1848 | if (blk_queue_io_stat(clone->q)) |
1849 | clone->cmd_flags |= REQ_IO_STAT; | |
cec47e3d | 1850 | |
466d89a6 KB |
1851 | clone->start_time = jiffies; |
1852 | r = blk_insert_cloned_request(clone->q, clone); | |
cec47e3d | 1853 | if (r) |
466d89a6 | 1854 | /* must complete clone in terms of original request */ |
cec47e3d KU |
1855 | dm_complete_request(rq, r); |
1856 | } | |
cec47e3d | 1857 | |
78d8e58a MS |
1858 | static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, |
1859 | void *data) | |
cec47e3d | 1860 | { |
78d8e58a MS |
1861 | struct dm_rq_target_io *tio = data; |
1862 | struct dm_rq_clone_bio_info *info = | |
1863 | container_of(bio, struct dm_rq_clone_bio_info, clone); | |
1864 | ||
1865 | info->orig = bio_orig; | |
1866 | info->tio = tio; | |
1867 | bio->bi_end_io = end_clone_bio; | |
1868 | ||
1869 | return 0; | |
1870 | } | |
1871 | ||
1872 | static int setup_clone(struct request *clone, struct request *rq, | |
1873 | struct dm_rq_target_io *tio, gfp_t gfp_mask) | |
cec47e3d | 1874 | { |
78d8e58a MS |
1875 | int r; |
1876 | ||
1877 | r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask, | |
1878 | dm_rq_bio_constructor, tio); | |
1879 | if (r) | |
1880 | return r; | |
1881 | ||
1882 | clone->cmd = rq->cmd; | |
1883 | clone->cmd_len = rq->cmd_len; | |
1884 | clone->sense = rq->sense; | |
cec47e3d KU |
1885 | clone->end_io = end_clone_request; |
1886 | clone->end_io_data = tio; | |
78d8e58a | 1887 | |
1ae49ea2 | 1888 | tio->clone = clone; |
78d8e58a MS |
1889 | |
1890 | return 0; | |
cec47e3d KU |
1891 | } |
1892 | ||
eca7ee6d MS |
1893 | static struct request *clone_old_rq(struct request *rq, struct mapped_device *md, |
1894 | struct dm_rq_target_io *tio, gfp_t gfp_mask) | |
1ae49ea2 | 1895 | { |
02233342 | 1896 | /* |
c5248f79 | 1897 | * Create clone for use with .request_fn request_queue |
02233342 | 1898 | */ |
02233342 | 1899 | struct request *clone; |
1ae49ea2 | 1900 | |
eca7ee6d | 1901 | clone = alloc_old_clone_request(md, gfp_mask); |
c5248f79 MS |
1902 | if (!clone) |
1903 | return NULL; | |
1ae49ea2 MS |
1904 | |
1905 | blk_rq_init(NULL, clone); | |
78d8e58a MS |
1906 | if (setup_clone(clone, rq, tio, gfp_mask)) { |
1907 | /* -ENOMEM */ | |
eca7ee6d | 1908 | free_old_clone_request(md, clone); |
78d8e58a MS |
1909 | return NULL; |
1910 | } | |
1ae49ea2 MS |
1911 | |
1912 | return clone; | |
1913 | } | |
1914 | ||
2eb6e1e3 KB |
1915 | static void map_tio_request(struct kthread_work *work); |
1916 | ||
bfebd1cd MS |
1917 | static void init_tio(struct dm_rq_target_io *tio, struct request *rq, |
1918 | struct mapped_device *md) | |
1919 | { | |
1920 | tio->md = md; | |
1921 | tio->ti = NULL; | |
1922 | tio->clone = NULL; | |
1923 | tio->orig = rq; | |
1924 | tio->error = 0; | |
591ddcfc MS |
1925 | /* |
1926 | * Avoid initializing info for blk-mq; it passes | |
1927 | * target-specific data through info.ptr | |
1928 | * (see: dm_mq_init_request) | |
1929 | */ | |
1930 | if (!md->init_tio_pdu) | |
1931 | memset(&tio->info, 0, sizeof(tio->info)); | |
02233342 MS |
1932 | if (md->kworker_task) |
1933 | init_kthread_work(&tio->work, map_tio_request); | |
bfebd1cd MS |
1934 | } |
1935 | ||
eca7ee6d MS |
1936 | static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq, |
1937 | struct mapped_device *md, | |
1938 | gfp_t gfp_mask) | |
6facdaff | 1939 | { |
6facdaff | 1940 | struct dm_rq_target_io *tio; |
e5863d9a MS |
1941 | int srcu_idx; |
1942 | struct dm_table *table; | |
6facdaff | 1943 | |
eca7ee6d | 1944 | tio = alloc_old_rq_tio(md, gfp_mask); |
6facdaff KU |
1945 | if (!tio) |
1946 | return NULL; | |
1947 | ||
bfebd1cd | 1948 | init_tio(tio, rq, md); |
6facdaff | 1949 | |
e5863d9a | 1950 | table = dm_get_live_table(md, &srcu_idx); |
eca7ee6d MS |
1951 | /* |
1952 | * Must clone a request if this .request_fn DM device | |
1953 | * is stacked on .request_fn device(s). | |
1954 | */ | |
e5863d9a | 1955 | if (!dm_table_mq_request_based(table)) { |
eca7ee6d | 1956 | if (!clone_old_rq(rq, md, tio, gfp_mask)) { |
e5863d9a | 1957 | dm_put_live_table(md, srcu_idx); |
eca7ee6d | 1958 | free_old_rq_tio(tio); |
e5863d9a MS |
1959 | return NULL; |
1960 | } | |
6facdaff | 1961 | } |
e5863d9a | 1962 | dm_put_live_table(md, srcu_idx); |
6facdaff | 1963 | |
466d89a6 | 1964 | return tio; |
6facdaff KU |
1965 | } |
1966 | ||
cec47e3d KU |
1967 | /* |
1968 | * Called with the queue lock held. | |
1969 | */ | |
eca7ee6d | 1970 | static int dm_old_prep_fn(struct request_queue *q, struct request *rq) |
cec47e3d KU |
1971 | { |
1972 | struct mapped_device *md = q->queuedata; | |
466d89a6 | 1973 | struct dm_rq_target_io *tio; |
cec47e3d | 1974 | |
cec47e3d KU |
1975 | if (unlikely(rq->special)) { |
1976 | DMWARN("Already has something in rq->special."); | |
1977 | return BLKPREP_KILL; | |
1978 | } | |
1979 | ||
eca7ee6d | 1980 | tio = dm_old_prep_tio(rq, md, GFP_ATOMIC); |
466d89a6 | 1981 | if (!tio) |
cec47e3d | 1982 | return BLKPREP_DEFER; |
cec47e3d | 1983 | |
466d89a6 | 1984 | rq->special = tio; |
cec47e3d KU |
1985 | rq->cmd_flags |= REQ_DONTPREP; |
1986 | ||
1987 | return BLKPREP_OK; | |
1988 | } | |
1989 | ||
9eef87da KU |
1990 | /* |
1991 | * Returns: | |
e5863d9a MS |
1992 | * 0 : the request has been processed |
1993 | * DM_MAPIO_REQUEUE : the original request needs to be requeued | |
1994 | * < 0 : the request was completed due to failure | |
9eef87da | 1995 | */ |
bfebd1cd | 1996 | static int map_request(struct dm_rq_target_io *tio, struct request *rq, |
9eef87da | 1997 | struct mapped_device *md) |
cec47e3d | 1998 | { |
e5863d9a | 1999 | int r; |
bfebd1cd | 2000 | struct dm_target *ti = tio->ti; |
e5863d9a MS |
2001 | struct request *clone = NULL; |
2002 | ||
2003 | if (tio->clone) { | |
2004 | clone = tio->clone; | |
2005 | r = ti->type->map_rq(ti, clone, &tio->info); | |
2006 | } else { | |
2007 | r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone); | |
2008 | if (r < 0) { | |
2009 | /* The target wants to complete the I/O */ | |
2010 | dm_kill_unmapped_request(rq, r); | |
2011 | return r; | |
2012 | } | |
3a140755 JN |
2013 | if (r != DM_MAPIO_REMAPPED) |
2014 | return r; | |
78d8e58a MS |
2015 | if (setup_clone(clone, rq, tio, GFP_ATOMIC)) { |
2016 | /* -ENOMEM */ | |
2017 | ti->type->release_clone_rq(clone); | |
2018 | return DM_MAPIO_REQUEUE; | |
2019 | } | |
e5863d9a | 2020 | } |
cec47e3d | 2021 | |
cec47e3d KU |
2022 | switch (r) { |
2023 | case DM_MAPIO_SUBMITTED: | |
2024 | /* The target has taken the I/O to submit by itself later */ | |
2025 | break; | |
2026 | case DM_MAPIO_REMAPPED: | |
2027 | /* The target has remapped the I/O so dispatch it */ | |
6db4ccd6 | 2028 | trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)), |
466d89a6 KB |
2029 | blk_rq_pos(rq)); |
2030 | dm_dispatch_clone_request(clone, rq); | |
cec47e3d KU |
2031 | break; |
2032 | case DM_MAPIO_REQUEUE: | |
2033 | /* The target wants to requeue the I/O */ | |
2d76fff1 | 2034 | dm_requeue_original_request(md, tio->orig); |
cec47e3d KU |
2035 | break; |
2036 | default: | |
2037 | if (r > 0) { | |
2038 | DMWARN("unimplemented target map return value: %d", r); | |
2039 | BUG(); | |
2040 | } | |
2041 | ||
2042 | /* The target wants to complete the I/O */ | |
466d89a6 | 2043 | dm_kill_unmapped_request(rq, r); |
e5863d9a | 2044 | return r; |
cec47e3d | 2045 | } |
9eef87da | 2046 | |
e5863d9a | 2047 | return 0; |
cec47e3d KU |
2048 | } |
2049 | ||
2eb6e1e3 | 2050 | static void map_tio_request(struct kthread_work *work) |
ba1cbad9 | 2051 | { |
2eb6e1e3 | 2052 | struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work); |
e5863d9a MS |
2053 | struct request *rq = tio->orig; |
2054 | struct mapped_device *md = tio->md; | |
ba1cbad9 | 2055 | |
bfebd1cd | 2056 | if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) |
2d76fff1 | 2057 | dm_requeue_original_request(md, rq); |
2eb6e1e3 KB |
2058 | } |
2059 | ||
466d89a6 | 2060 | static void dm_start_request(struct mapped_device *md, struct request *orig) |
ba1cbad9 | 2061 | { |
bfebd1cd MS |
2062 | if (!orig->q->mq_ops) |
2063 | blk_start_request(orig); | |
2064 | else | |
2065 | blk_mq_start_request(orig); | |
466d89a6 | 2066 | atomic_inc(&md->pending[rq_data_dir(orig)]); |
ba1cbad9 | 2067 | |
0ce65797 MS |
2068 | if (md->seq_rq_merge_deadline_usecs) { |
2069 | md->last_rq_pos = rq_end_sector(orig); | |
2070 | md->last_rq_rw = rq_data_dir(orig); | |
2071 | md->last_rq_start_time = ktime_get(); | |
2072 | } | |
de3ec86d | 2073 | |
e262f347 MP |
2074 | if (unlikely(dm_stats_used(&md->stats))) { |
2075 | struct dm_rq_target_io *tio = tio_from_request(orig); | |
2076 | tio->duration_jiffies = jiffies; | |
2077 | tio->n_sectors = blk_rq_sectors(orig); | |
2078 | dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig), | |
2079 | tio->n_sectors, false, 0, &tio->stats_aux); | |
2080 | } | |
2081 | ||
ba1cbad9 MS |
2082 | /* |
2083 | * Hold the md reference here for the in-flight I/O. | |
2084 | * We can't rely on the reference count by device opener, | |
2085 | * because the device may be closed during the request completion | |
2086 | * when all bios are completed. | |
2087 | * See the comment in rq_completed() too. | |
2088 | */ | |
2089 | dm_get(md); | |
ba1cbad9 MS |
2090 | } |
2091 | ||
0ce65797 MS |
2092 | #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000 |
2093 | ||
2094 | ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf) | |
2095 | { | |
2096 | return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs); | |
2097 | } | |
2098 | ||
2099 | ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md, | |
2100 | const char *buf, size_t count) | |
2101 | { | |
2102 | unsigned deadline; | |
2103 | ||
17e149b8 | 2104 | if (!dm_request_based(md) || md->use_blk_mq) |
0ce65797 MS |
2105 | return count; |
2106 | ||
2107 | if (kstrtouint(buf, 10, &deadline)) | |
2108 | return -EINVAL; | |
2109 | ||
2110 | if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS) | |
2111 | deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS; | |
2112 | ||
2113 | md->seq_rq_merge_deadline_usecs = deadline; | |
2114 | ||
2115 | return count; | |
2116 | } | |
2117 | ||
2118 | static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md) | |
2119 | { | |
2120 | ktime_t kt_deadline; | |
2121 | ||
2122 | if (!md->seq_rq_merge_deadline_usecs) | |
2123 | return false; | |
2124 | ||
2125 | kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC); | |
2126 | kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline); | |
2127 | ||
2128 | return !ktime_after(ktime_get(), kt_deadline); | |
2129 | } | |
2130 | ||
cec47e3d KU |
2131 | /* |
2132 | * q->request_fn for request-based dm. | |
2133 | * Called with the queue lock held. | |
2134 | */ | |
2135 | static void dm_request_fn(struct request_queue *q) | |
2136 | { | |
2137 | struct mapped_device *md = q->queuedata; | |
c91852ff | 2138 | struct dm_target *ti = md->immutable_target; |
466d89a6 | 2139 | struct request *rq; |
2eb6e1e3 | 2140 | struct dm_rq_target_io *tio; |
c91852ff MS |
2141 | sector_t pos = 0; |
2142 | ||
2143 | if (unlikely(!ti)) { | |
2144 | int srcu_idx; | |
2145 | struct dm_table *map = dm_get_live_table(md, &srcu_idx); | |
2146 | ||
2147 | ti = dm_table_find_target(map, pos); | |
2148 | dm_put_live_table(md, srcu_idx); | |
2149 | } | |
cec47e3d KU |
2150 | |
2151 | /* | |
b4324fee KU |
2152 | * For suspend, check blk_queue_stopped() and increment |
2153 | * ->pending within a single queue_lock not to increment the | |
2154 | * number of in-flight I/Os after the queue is stopped in | |
2155 | * dm_suspend(). | |
cec47e3d | 2156 | */ |
7eaceacc | 2157 | while (!blk_queue_stopped(q)) { |
cec47e3d KU |
2158 | rq = blk_peek_request(q); |
2159 | if (!rq) | |
c91852ff | 2160 | return; |
cec47e3d | 2161 | |
29e4013d TH |
2162 | /* always use block 0 to find the target for flushes for now */ |
2163 | pos = 0; | |
2164 | if (!(rq->cmd_flags & REQ_FLUSH)) | |
2165 | pos = blk_rq_pos(rq); | |
2166 | ||
c91852ff MS |
2167 | if ((dm_request_peeked_before_merge_deadline(md) && |
2168 | md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 && | |
2169 | md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) || | |
2170 | (ti->type->busy && ti->type->busy(ti))) { | |
2171 | blk_delay_queue(q, HZ / 100); | |
2172 | return; | |
ba1cbad9 | 2173 | } |
d0bcb878 | 2174 | |
466d89a6 | 2175 | dm_start_request(md, rq); |
9eef87da | 2176 | |
bfebd1cd | 2177 | tio = tio_from_request(rq); |
2eb6e1e3 KB |
2178 | /* Establish tio->ti before queuing work (map_tio_request) */ |
2179 | tio->ti = ti; | |
2180 | queue_kthread_work(&md->kworker, &tio->work); | |
052189a2 | 2181 | BUG_ON(!irqs_disabled()); |
cec47e3d | 2182 | } |
cec47e3d KU |
2183 | } |
2184 | ||
1da177e4 LT |
2185 | static int dm_any_congested(void *congested_data, int bdi_bits) |
2186 | { | |
8a57dfc6 CS |
2187 | int r = bdi_bits; |
2188 | struct mapped_device *md = congested_data; | |
2189 | struct dm_table *map; | |
1da177e4 | 2190 | |
1eb787ec | 2191 | if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { |
e522c039 | 2192 | if (dm_request_based(md)) { |
cec47e3d | 2193 | /* |
e522c039 MS |
2194 | * With request-based DM we only need to check the |
2195 | * top-level queue for congestion. | |
cec47e3d | 2196 | */ |
e522c039 MS |
2197 | r = md->queue->backing_dev_info.wb.state & bdi_bits; |
2198 | } else { | |
2199 | map = dm_get_live_table_fast(md); | |
2200 | if (map) | |
cec47e3d | 2201 | r = dm_table_any_congested(map, bdi_bits); |
e522c039 | 2202 | dm_put_live_table_fast(md); |
8a57dfc6 CS |
2203 | } |
2204 | } | |
2205 | ||
1da177e4 LT |
2206 | return r; |
2207 | } | |
2208 | ||
2209 | /*----------------------------------------------------------------- | |
2210 | * An IDR is used to keep track of allocated minor numbers. | |
2211 | *---------------------------------------------------------------*/ | |
2b06cfff | 2212 | static void free_minor(int minor) |
1da177e4 | 2213 | { |
f32c10b0 | 2214 | spin_lock(&_minor_lock); |
1da177e4 | 2215 | idr_remove(&_minor_idr, minor); |
f32c10b0 | 2216 | spin_unlock(&_minor_lock); |
1da177e4 LT |
2217 | } |
2218 | ||
2219 | /* | |
2220 | * See if the device with a specific minor # is free. | |
2221 | */ | |
cf13ab8e | 2222 | static int specific_minor(int minor) |
1da177e4 | 2223 | { |
c9d76be6 | 2224 | int r; |
1da177e4 LT |
2225 | |
2226 | if (minor >= (1 << MINORBITS)) | |
2227 | return -EINVAL; | |
2228 | ||
c9d76be6 | 2229 | idr_preload(GFP_KERNEL); |
f32c10b0 | 2230 | spin_lock(&_minor_lock); |
1da177e4 | 2231 | |
c9d76be6 | 2232 | r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); |
1da177e4 | 2233 | |
f32c10b0 | 2234 | spin_unlock(&_minor_lock); |
c9d76be6 TH |
2235 | idr_preload_end(); |
2236 | if (r < 0) | |
2237 | return r == -ENOSPC ? -EBUSY : r; | |
2238 | return 0; | |
1da177e4 LT |
2239 | } |
2240 | ||
cf13ab8e | 2241 | static int next_free_minor(int *minor) |
1da177e4 | 2242 | { |
c9d76be6 | 2243 | int r; |
62f75c2f | 2244 | |
c9d76be6 | 2245 | idr_preload(GFP_KERNEL); |
f32c10b0 | 2246 | spin_lock(&_minor_lock); |
1da177e4 | 2247 | |
c9d76be6 | 2248 | r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); |
1da177e4 | 2249 | |
f32c10b0 | 2250 | spin_unlock(&_minor_lock); |
c9d76be6 TH |
2251 | idr_preload_end(); |
2252 | if (r < 0) | |
2253 | return r; | |
2254 | *minor = r; | |
2255 | return 0; | |
1da177e4 LT |
2256 | } |
2257 | ||
83d5cde4 | 2258 | static const struct block_device_operations dm_blk_dops; |
1da177e4 | 2259 | |
53d5914f MP |
2260 | static void dm_wq_work(struct work_struct *work); |
2261 | ||
4a0b4ddf MS |
2262 | static void dm_init_md_queue(struct mapped_device *md) |
2263 | { | |
2264 | /* | |
2265 | * Request-based dm devices cannot be stacked on top of bio-based dm | |
bfebd1cd | 2266 | * devices. The type of this dm device may not have been decided yet. |
4a0b4ddf MS |
2267 | * The type is decided at the first table loading time. |
2268 | * To prevent problematic device stacking, clear the queue flag | |
2269 | * for request stacking support until then. | |
2270 | * | |
2271 | * This queue is new, so no concurrency on the queue_flags. | |
2272 | */ | |
2273 | queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); | |
ad5f498f MP |
2274 | |
2275 | /* | |
2276 | * Initialize data that will only be used by a non-blk-mq DM queue | |
2277 | * - must do so here (in alloc_dev callchain) before queue is used | |
2278 | */ | |
2279 | md->queue->queuedata = md; | |
2280 | md->queue->backing_dev_info.congested_data = md; | |
bfebd1cd | 2281 | } |
4a0b4ddf | 2282 | |
eca7ee6d | 2283 | static void dm_init_normal_md_queue(struct mapped_device *md) |
bfebd1cd | 2284 | { |
17e149b8 | 2285 | md->use_blk_mq = false; |
bfebd1cd MS |
2286 | dm_init_md_queue(md); |
2287 | ||
2288 | /* | |
2289 | * Initialize aspects of queue that aren't relevant for blk-mq | |
2290 | */ | |
4a0b4ddf | 2291 | md->queue->backing_dev_info.congested_fn = dm_any_congested; |
4a0b4ddf | 2292 | blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); |
4a0b4ddf MS |
2293 | } |
2294 | ||
0f20972f MS |
2295 | static void cleanup_mapped_device(struct mapped_device *md) |
2296 | { | |
0f20972f MS |
2297 | if (md->wq) |
2298 | destroy_workqueue(md->wq); | |
2299 | if (md->kworker_task) | |
2300 | kthread_stop(md->kworker_task); | |
6f65985e JL |
2301 | mempool_destroy(md->io_pool); |
2302 | mempool_destroy(md->rq_pool); | |
0f20972f MS |
2303 | if (md->bs) |
2304 | bioset_free(md->bs); | |
2305 | ||
b06075a9 MP |
2306 | cleanup_srcu_struct(&md->io_barrier); |
2307 | ||
0f20972f MS |
2308 | if (md->disk) { |
2309 | spin_lock(&_minor_lock); | |
2310 | md->disk->private_data = NULL; | |
2311 | spin_unlock(&_minor_lock); | |
0f20972f MS |
2312 | del_gendisk(md->disk); |
2313 | put_disk(md->disk); | |
2314 | } | |
2315 | ||
2316 | if (md->queue) | |
2317 | blk_cleanup_queue(md->queue); | |
2318 | ||
2319 | if (md->bdev) { | |
2320 | bdput(md->bdev); | |
2321 | md->bdev = NULL; | |
2322 | } | |
2323 | } | |
2324 | ||
1da177e4 LT |
2325 | /* |
2326 | * Allocate and initialise a blank device with a given minor. | |
2327 | */ | |
2b06cfff | 2328 | static struct mapped_device *alloc_dev(int minor) |
1da177e4 | 2329 | { |
115485e8 MS |
2330 | int r, numa_node_id = dm_get_numa_node(); |
2331 | struct mapped_device *md; | |
ba61fdd1 | 2332 | void *old_md; |
1da177e4 | 2333 | |
115485e8 | 2334 | md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); |
1da177e4 LT |
2335 | if (!md) { |
2336 | DMWARN("unable to allocate device, out of memory."); | |
2337 | return NULL; | |
2338 | } | |
2339 | ||
10da4f79 | 2340 | if (!try_module_get(THIS_MODULE)) |
6ed7ade8 | 2341 | goto bad_module_get; |
10da4f79 | 2342 | |
1da177e4 | 2343 | /* get a minor number for the dev */ |
2b06cfff | 2344 | if (minor == DM_ANY_MINOR) |
cf13ab8e | 2345 | r = next_free_minor(&minor); |
2b06cfff | 2346 | else |
cf13ab8e | 2347 | r = specific_minor(minor); |
1da177e4 | 2348 | if (r < 0) |
6ed7ade8 | 2349 | goto bad_minor; |
1da177e4 | 2350 | |
83d5e5b0 MP |
2351 | r = init_srcu_struct(&md->io_barrier); |
2352 | if (r < 0) | |
2353 | goto bad_io_barrier; | |
2354 | ||
115485e8 | 2355 | md->numa_node_id = numa_node_id; |
17e149b8 | 2356 | md->use_blk_mq = use_blk_mq; |
591ddcfc | 2357 | md->init_tio_pdu = false; |
a5664dad | 2358 | md->type = DM_TYPE_NONE; |
e61290a4 | 2359 | mutex_init(&md->suspend_lock); |
a5664dad | 2360 | mutex_init(&md->type_lock); |
86f1152b | 2361 | mutex_init(&md->table_devices_lock); |
022c2611 | 2362 | spin_lock_init(&md->deferred_lock); |
1da177e4 | 2363 | atomic_set(&md->holders, 1); |
5c6bd75d | 2364 | atomic_set(&md->open_count, 0); |
1da177e4 | 2365 | atomic_set(&md->event_nr, 0); |
7a8c3d3b MA |
2366 | atomic_set(&md->uevent_seq, 0); |
2367 | INIT_LIST_HEAD(&md->uevent_list); | |
86f1152b | 2368 | INIT_LIST_HEAD(&md->table_devices); |
7a8c3d3b | 2369 | spin_lock_init(&md->uevent_lock); |
1da177e4 | 2370 | |
115485e8 | 2371 | md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id); |
1da177e4 | 2372 | if (!md->queue) |
0f20972f | 2373 | goto bad; |
1da177e4 | 2374 | |
4a0b4ddf | 2375 | dm_init_md_queue(md); |
9faf400f | 2376 | |
115485e8 | 2377 | md->disk = alloc_disk_node(1, numa_node_id); |
1da177e4 | 2378 | if (!md->disk) |
0f20972f | 2379 | goto bad; |
1da177e4 | 2380 | |
316d315b NK |
2381 | atomic_set(&md->pending[0], 0); |
2382 | atomic_set(&md->pending[1], 0); | |
f0b04115 | 2383 | init_waitqueue_head(&md->wait); |
53d5914f | 2384 | INIT_WORK(&md->work, dm_wq_work); |
f0b04115 | 2385 | init_waitqueue_head(&md->eventq); |
2995fa78 | 2386 | init_completion(&md->kobj_holder.completion); |
2eb6e1e3 | 2387 | md->kworker_task = NULL; |
f0b04115 | 2388 | |
1da177e4 LT |
2389 | md->disk->major = _major; |
2390 | md->disk->first_minor = minor; | |
2391 | md->disk->fops = &dm_blk_dops; | |
2392 | md->disk->queue = md->queue; | |
2393 | md->disk->private_data = md; | |
2394 | sprintf(md->disk->disk_name, "dm-%d", minor); | |
2395 | add_disk(md->disk); | |
7e51f257 | 2396 | format_dev_t(md->name, MKDEV(_major, minor)); |
1da177e4 | 2397 | |
670368a8 | 2398 | md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); |
304f3f6a | 2399 | if (!md->wq) |
0f20972f | 2400 | goto bad; |
304f3f6a | 2401 | |
32a926da MP |
2402 | md->bdev = bdget_disk(md->disk, 0); |
2403 | if (!md->bdev) | |
0f20972f | 2404 | goto bad; |
32a926da | 2405 | |
6a8736d1 TH |
2406 | bio_init(&md->flush_bio); |
2407 | md->flush_bio.bi_bdev = md->bdev; | |
2408 | md->flush_bio.bi_rw = WRITE_FLUSH; | |
2409 | ||
fd2ed4d2 MP |
2410 | dm_stats_init(&md->stats); |
2411 | ||
ba61fdd1 | 2412 | /* Populate the mapping, nobody knows we exist yet */ |
f32c10b0 | 2413 | spin_lock(&_minor_lock); |
ba61fdd1 | 2414 | old_md = idr_replace(&_minor_idr, md, minor); |
f32c10b0 | 2415 | spin_unlock(&_minor_lock); |
ba61fdd1 JM |
2416 | |
2417 | BUG_ON(old_md != MINOR_ALLOCED); | |
2418 | ||
1da177e4 LT |
2419 | return md; |
2420 | ||
0f20972f MS |
2421 | bad: |
2422 | cleanup_mapped_device(md); | |
83d5e5b0 | 2423 | bad_io_barrier: |
1da177e4 | 2424 | free_minor(minor); |
6ed7ade8 | 2425 | bad_minor: |
10da4f79 | 2426 | module_put(THIS_MODULE); |
6ed7ade8 | 2427 | bad_module_get: |
1da177e4 LT |
2428 | kfree(md); |
2429 | return NULL; | |
2430 | } | |
2431 | ||
ae9da83f JN |
2432 | static void unlock_fs(struct mapped_device *md); |
2433 | ||
1da177e4 LT |
2434 | static void free_dev(struct mapped_device *md) |
2435 | { | |
f331c029 | 2436 | int minor = MINOR(disk_devt(md->disk)); |
63d94e48 | 2437 | |
32a926da | 2438 | unlock_fs(md); |
2eb6e1e3 | 2439 | |
0f20972f | 2440 | cleanup_mapped_device(md); |
1c357a1e MS |
2441 | if (md->tag_set) { |
2442 | blk_mq_free_tag_set(md->tag_set); | |
2443 | kfree(md->tag_set); | |
2444 | } | |
63a4f065 | 2445 | |
86f1152b | 2446 | free_table_devices(&md->table_devices); |
63a4f065 | 2447 | dm_stats_cleanup(&md->stats); |
63a4f065 MS |
2448 | free_minor(minor); |
2449 | ||
10da4f79 | 2450 | module_put(THIS_MODULE); |
1da177e4 LT |
2451 | kfree(md); |
2452 | } | |
2453 | ||
e6ee8c0b KU |
2454 | static void __bind_mempools(struct mapped_device *md, struct dm_table *t) |
2455 | { | |
c0820cf5 | 2456 | struct dm_md_mempools *p = dm_table_get_md_mempools(t); |
e6ee8c0b | 2457 | |
4e6e36c3 MS |
2458 | if (md->bs) { |
2459 | /* The md already has necessary mempools. */ | |
2460 | if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) { | |
16245bdc JN |
2461 | /* |
2462 | * Reload bioset because front_pad may have changed | |
2463 | * because a different table was loaded. | |
2464 | */ | |
2465 | bioset_free(md->bs); | |
2466 | md->bs = p->bs; | |
2467 | p->bs = NULL; | |
16245bdc | 2468 | } |
4e6e36c3 MS |
2469 | /* |
2470 | * There's no need to reload with request-based dm | |
2471 | * because the size of front_pad doesn't change. | |
2472 | * Note for future: If you are to reload bioset, | |
2473 | * prep-ed requests in the queue may refer | |
2474 | * to bio from the old bioset, so you must walk | |
2475 | * through the queue to unprep. | |
2476 | */ | |
2477 | goto out; | |
c0820cf5 | 2478 | } |
e6ee8c0b | 2479 | |
cbc4e3c1 MS |
2480 | BUG_ON(!p || md->io_pool || md->rq_pool || md->bs); |
2481 | ||
e6ee8c0b KU |
2482 | md->io_pool = p->io_pool; |
2483 | p->io_pool = NULL; | |
1ae49ea2 MS |
2484 | md->rq_pool = p->rq_pool; |
2485 | p->rq_pool = NULL; | |
e6ee8c0b KU |
2486 | md->bs = p->bs; |
2487 | p->bs = NULL; | |
4e6e36c3 | 2488 | |
e6ee8c0b | 2489 | out: |
02233342 | 2490 | /* mempool bind completed, no longer need any mempools in the table */ |
e6ee8c0b KU |
2491 | dm_table_free_md_mempools(t); |
2492 | } | |
2493 | ||
1da177e4 LT |
2494 | /* |
2495 | * Bind a table to the device. | |
2496 | */ | |
2497 | static void event_callback(void *context) | |
2498 | { | |
7a8c3d3b MA |
2499 | unsigned long flags; |
2500 | LIST_HEAD(uevents); | |
1da177e4 LT |
2501 | struct mapped_device *md = (struct mapped_device *) context; |
2502 | ||
7a8c3d3b MA |
2503 | spin_lock_irqsave(&md->uevent_lock, flags); |
2504 | list_splice_init(&md->uevent_list, &uevents); | |
2505 | spin_unlock_irqrestore(&md->uevent_lock, flags); | |
2506 | ||
ed9e1982 | 2507 | dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); |
7a8c3d3b | 2508 | |
1da177e4 LT |
2509 | atomic_inc(&md->event_nr); |
2510 | wake_up(&md->eventq); | |
2511 | } | |
2512 | ||
c217649b MS |
2513 | /* |
2514 | * Protected by md->suspend_lock obtained by dm_swap_table(). | |
2515 | */ | |
4e90188b | 2516 | static void __set_size(struct mapped_device *md, sector_t size) |
1da177e4 | 2517 | { |
4e90188b | 2518 | set_capacity(md->disk, size); |
1da177e4 | 2519 | |
db8fef4f | 2520 | i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); |
1da177e4 LT |
2521 | } |
2522 | ||
042d2a9b AK |
2523 | /* |
2524 | * Returns old map, which caller must destroy. | |
2525 | */ | |
2526 | static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, | |
2527 | struct queue_limits *limits) | |
1da177e4 | 2528 | { |
042d2a9b | 2529 | struct dm_table *old_map; |
165125e1 | 2530 | struct request_queue *q = md->queue; |
1da177e4 LT |
2531 | sector_t size; |
2532 | ||
2533 | size = dm_table_get_size(t); | |
3ac51e74 DW |
2534 | |
2535 | /* | |
2536 | * Wipe any geometry if the size of the table changed. | |
2537 | */ | |
fd2ed4d2 | 2538 | if (size != dm_get_size(md)) |
3ac51e74 DW |
2539 | memset(&md->geometry, 0, sizeof(md->geometry)); |
2540 | ||
32a926da | 2541 | __set_size(md, size); |
d5816876 | 2542 | |
2ca3310e AK |
2543 | dm_table_event_callback(t, event_callback, md); |
2544 | ||
e6ee8c0b KU |
2545 | /* |
2546 | * The queue hasn't been stopped yet, if the old table type wasn't | |
2547 | * for request-based during suspension. So stop it to prevent | |
2548 | * I/O mapping before resume. | |
2549 | * This must be done before setting the queue restrictions, | |
2550 | * because request-based dm may be run just after the setting. | |
2551 | */ | |
16f12266 | 2552 | if (dm_table_request_based(t)) { |
eca7ee6d | 2553 | dm_stop_queue(q); |
16f12266 MS |
2554 | /* |
2555 | * Leverage the fact that request-based DM targets are | |
2556 | * immutable singletons and establish md->immutable_target | |
2557 | * - used to optimize both dm_request_fn and dm_mq_queue_rq | |
2558 | */ | |
2559 | md->immutable_target = dm_table_get_immutable_target(t); | |
2560 | } | |
e6ee8c0b KU |
2561 | |
2562 | __bind_mempools(md, t); | |
2563 | ||
a12f5d48 | 2564 | old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
83d5e5b0 | 2565 | rcu_assign_pointer(md->map, t); |
36a0456f AK |
2566 | md->immutable_target_type = dm_table_get_immutable_target_type(t); |
2567 | ||
754c5fc7 | 2568 | dm_table_set_restrictions(t, q, limits); |
41abc4e1 HR |
2569 | if (old_map) |
2570 | dm_sync_table(md); | |
1da177e4 | 2571 | |
042d2a9b | 2572 | return old_map; |
1da177e4 LT |
2573 | } |
2574 | ||
a7940155 AK |
2575 | /* |
2576 | * Returns unbound table for the caller to free. | |
2577 | */ | |
2578 | static struct dm_table *__unbind(struct mapped_device *md) | |
1da177e4 | 2579 | { |
a12f5d48 | 2580 | struct dm_table *map = rcu_dereference_protected(md->map, 1); |
1da177e4 LT |
2581 | |
2582 | if (!map) | |
a7940155 | 2583 | return NULL; |
1da177e4 LT |
2584 | |
2585 | dm_table_event_callback(map, NULL, NULL); | |
9cdb8520 | 2586 | RCU_INIT_POINTER(md->map, NULL); |
83d5e5b0 | 2587 | dm_sync_table(md); |
a7940155 AK |
2588 | |
2589 | return map; | |
1da177e4 LT |
2590 | } |
2591 | ||
2592 | /* | |
2593 | * Constructor for a new device. | |
2594 | */ | |
2b06cfff | 2595 | int dm_create(int minor, struct mapped_device **result) |
1da177e4 LT |
2596 | { |
2597 | struct mapped_device *md; | |
2598 | ||
2b06cfff | 2599 | md = alloc_dev(minor); |
1da177e4 LT |
2600 | if (!md) |
2601 | return -ENXIO; | |
2602 | ||
784aae73 MB |
2603 | dm_sysfs_init(md); |
2604 | ||
1da177e4 LT |
2605 | *result = md; |
2606 | return 0; | |
2607 | } | |
2608 | ||
a5664dad MS |
2609 | /* |
2610 | * Functions to manage md->type. | |
2611 | * All are required to hold md->type_lock. | |
2612 | */ | |
2613 | void dm_lock_md_type(struct mapped_device *md) | |
2614 | { | |
2615 | mutex_lock(&md->type_lock); | |
2616 | } | |
2617 | ||
2618 | void dm_unlock_md_type(struct mapped_device *md) | |
2619 | { | |
2620 | mutex_unlock(&md->type_lock); | |
2621 | } | |
2622 | ||
2623 | void dm_set_md_type(struct mapped_device *md, unsigned type) | |
2624 | { | |
00c4fc3b | 2625 | BUG_ON(!mutex_is_locked(&md->type_lock)); |
a5664dad MS |
2626 | md->type = type; |
2627 | } | |
2628 | ||
2629 | unsigned dm_get_md_type(struct mapped_device *md) | |
2630 | { | |
2631 | return md->type; | |
2632 | } | |
2633 | ||
36a0456f AK |
2634 | struct target_type *dm_get_immutable_target_type(struct mapped_device *md) |
2635 | { | |
2636 | return md->immutable_target_type; | |
2637 | } | |
2638 | ||
f84cb8a4 MS |
2639 | /* |
2640 | * The queue_limits are only valid as long as you have a reference | |
2641 | * count on 'md'. | |
2642 | */ | |
2643 | struct queue_limits *dm_get_queue_limits(struct mapped_device *md) | |
2644 | { | |
2645 | BUG_ON(!atomic_read(&md->holders)); | |
2646 | return &md->queue->limits; | |
2647 | } | |
2648 | EXPORT_SYMBOL_GPL(dm_get_queue_limits); | |
2649 | ||
eca7ee6d | 2650 | static void dm_old_init_rq_based_worker_thread(struct mapped_device *md) |
bfebd1cd MS |
2651 | { |
2652 | /* Initialize the request-based DM worker thread */ | |
2653 | init_kthread_worker(&md->kworker); | |
2654 | md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker, | |
2655 | "kdmwork-%s", dm_device_name(md)); | |
2656 | } | |
2657 | ||
4a0b4ddf | 2658 | /* |
eca7ee6d | 2659 | * Fully initialize a .request_fn request-based queue. |
4a0b4ddf | 2660 | */ |
eca7ee6d | 2661 | static int dm_old_init_request_queue(struct mapped_device *md) |
4a0b4ddf MS |
2662 | { |
2663 | struct request_queue *q = NULL; | |
2664 | ||
4a0b4ddf MS |
2665 | /* Fully initialize the queue */ |
2666 | q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL); | |
2667 | if (!q) | |
bfebd1cd | 2668 | return -EINVAL; |
4a0b4ddf | 2669 | |
0ce65797 MS |
2670 | /* disable dm_request_fn's merge heuristic by default */ |
2671 | md->seq_rq_merge_deadline_usecs = 0; | |
2672 | ||
4a0b4ddf | 2673 | md->queue = q; |
eca7ee6d | 2674 | dm_init_normal_md_queue(md); |
4a0b4ddf | 2675 | blk_queue_softirq_done(md->queue, dm_softirq_done); |
eca7ee6d | 2676 | blk_queue_prep_rq(md->queue, dm_old_prep_fn); |
4a0b4ddf | 2677 | |
eca7ee6d | 2678 | dm_old_init_rq_based_worker_thread(md); |
2eb6e1e3 | 2679 | |
4a0b4ddf MS |
2680 | elv_register_queue(md->queue); |
2681 | ||
bfebd1cd MS |
2682 | return 0; |
2683 | } | |
2684 | ||
2685 | static int dm_mq_init_request(void *data, struct request *rq, | |
2686 | unsigned int hctx_idx, unsigned int request_idx, | |
2687 | unsigned int numa_node) | |
2688 | { | |
2689 | struct mapped_device *md = data; | |
2690 | struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); | |
2691 | ||
2692 | /* | |
2693 | * Must initialize md member of tio, otherwise it won't | |
2694 | * be available in dm_mq_queue_rq. | |
2695 | */ | |
2696 | tio->md = md; | |
2697 | ||
591ddcfc MS |
2698 | if (md->init_tio_pdu) { |
2699 | /* target-specific per-io data is immediately after the tio */ | |
2700 | tio->info.ptr = tio + 1; | |
2701 | } | |
2702 | ||
bfebd1cd MS |
2703 | return 0; |
2704 | } | |
2705 | ||
2706 | static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx, | |
2707 | const struct blk_mq_queue_data *bd) | |
2708 | { | |
2709 | struct request *rq = bd->rq; | |
2710 | struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); | |
2711 | struct mapped_device *md = tio->md; | |
16f12266 | 2712 | struct dm_target *ti = md->immutable_target; |
bfebd1cd | 2713 | |
16f12266 MS |
2714 | if (unlikely(!ti)) { |
2715 | int srcu_idx; | |
2716 | struct dm_table *map = dm_get_live_table(md, &srcu_idx); | |
bfebd1cd | 2717 | |
16f12266 | 2718 | ti = dm_table_find_target(map, 0); |
bfebd1cd | 2719 | dm_put_live_table(md, srcu_idx); |
bfebd1cd | 2720 | } |
bfebd1cd MS |
2721 | |
2722 | if (ti->type->busy && ti->type->busy(ti)) | |
2723 | return BLK_MQ_RQ_QUEUE_BUSY; | |
2724 | ||
2725 | dm_start_request(md, rq); | |
2726 | ||
2727 | /* Init tio using md established in .init_request */ | |
2728 | init_tio(tio, rq, md); | |
2729 | ||
02233342 MS |
2730 | /* |
2731 | * Establish tio->ti before queuing work (map_tio_request) | |
2732 | * or making direct call to map_request(). | |
2733 | */ | |
bfebd1cd | 2734 | tio->ti = ti; |
02233342 | 2735 | |
c5248f79 MS |
2736 | /* Direct call is fine since .queue_rq allows allocations */ |
2737 | if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) { | |
2738 | /* Undo dm_start_request() before requeuing */ | |
2739 | rq_end_stats(md, rq); | |
2740 | rq_completed(md, rq_data_dir(rq), false); | |
2741 | return BLK_MQ_RQ_QUEUE_BUSY; | |
02233342 | 2742 | } |
bfebd1cd MS |
2743 | |
2744 | return BLK_MQ_RQ_QUEUE_OK; | |
2745 | } | |
2746 | ||
2747 | static struct blk_mq_ops dm_mq_ops = { | |
2748 | .queue_rq = dm_mq_queue_rq, | |
2749 | .map_queue = blk_mq_map_queue, | |
2750 | .complete = dm_softirq_done, | |
2751 | .init_request = dm_mq_init_request, | |
2752 | }; | |
2753 | ||
591ddcfc MS |
2754 | static int dm_mq_init_request_queue(struct mapped_device *md, |
2755 | struct dm_target *immutable_tgt) | |
bfebd1cd MS |
2756 | { |
2757 | struct request_queue *q; | |
2758 | int err; | |
2759 | ||
c5248f79 MS |
2760 | if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) { |
2761 | DMERR("request-based dm-mq may only be stacked on blk-mq device(s)"); | |
2762 | return -EINVAL; | |
2763 | } | |
2764 | ||
115485e8 | 2765 | md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id); |
1c357a1e MS |
2766 | if (!md->tag_set) |
2767 | return -ENOMEM; | |
2768 | ||
2769 | md->tag_set->ops = &dm_mq_ops; | |
2770 | md->tag_set->queue_depth = dm_get_blk_mq_queue_depth(); | |
115485e8 | 2771 | md->tag_set->numa_node = md->numa_node_id; |
1c357a1e MS |
2772 | md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
2773 | md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues(); | |
2774 | md->tag_set->driver_data = md; | |
2775 | ||
2776 | md->tag_set->cmd_size = sizeof(struct dm_rq_target_io); | |
591ddcfc MS |
2777 | if (immutable_tgt && immutable_tgt->per_io_data_size) { |
2778 | /* any target-specific per-io data is immediately after the tio */ | |
2779 | md->tag_set->cmd_size += immutable_tgt->per_io_data_size; | |
2780 | md->init_tio_pdu = true; | |
2781 | } | |
bfebd1cd | 2782 | |
1c357a1e | 2783 | err = blk_mq_alloc_tag_set(md->tag_set); |
bfebd1cd | 2784 | if (err) |
1c357a1e | 2785 | goto out_kfree_tag_set; |
bfebd1cd | 2786 | |
1c357a1e | 2787 | q = blk_mq_init_allocated_queue(md->tag_set, md->queue); |
bfebd1cd MS |
2788 | if (IS_ERR(q)) { |
2789 | err = PTR_ERR(q); | |
2790 | goto out_tag_set; | |
2791 | } | |
2792 | md->queue = q; | |
2793 | dm_init_md_queue(md); | |
2794 | ||
2795 | /* backfill 'mq' sysfs registration normally done in blk_register_queue */ | |
2796 | blk_mq_register_disk(md->disk); | |
2797 | ||
bfebd1cd MS |
2798 | return 0; |
2799 | ||
2800 | out_tag_set: | |
1c357a1e MS |
2801 | blk_mq_free_tag_set(md->tag_set); |
2802 | out_kfree_tag_set: | |
2803 | kfree(md->tag_set); | |
2804 | ||
bfebd1cd | 2805 | return err; |
4a0b4ddf MS |
2806 | } |
2807 | ||
4e6e36c3 MS |
2808 | static unsigned filter_md_type(unsigned type, struct mapped_device *md) |
2809 | { | |
2810 | if (type == DM_TYPE_BIO_BASED) | |
2811 | return type; | |
2812 | ||
2813 | return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED; | |
2814 | } | |
2815 | ||
4a0b4ddf MS |
2816 | /* |
2817 | * Setup the DM device's queue based on md's type | |
2818 | */ | |
591ddcfc | 2819 | int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) |
4a0b4ddf | 2820 | { |
bfebd1cd | 2821 | int r; |
17e149b8 | 2822 | unsigned md_type = filter_md_type(dm_get_md_type(md), md); |
bfebd1cd MS |
2823 | |
2824 | switch (md_type) { | |
2825 | case DM_TYPE_REQUEST_BASED: | |
eca7ee6d | 2826 | r = dm_old_init_request_queue(md); |
bfebd1cd | 2827 | if (r) { |
eca7ee6d | 2828 | DMERR("Cannot initialize queue for request-based mapped device"); |
bfebd1cd | 2829 | return r; |
ff36ab34 | 2830 | } |
bfebd1cd MS |
2831 | break; |
2832 | case DM_TYPE_MQ_REQUEST_BASED: | |
591ddcfc | 2833 | r = dm_mq_init_request_queue(md, dm_table_get_immutable_target(t)); |
bfebd1cd | 2834 | if (r) { |
eca7ee6d | 2835 | DMERR("Cannot initialize queue for request-based dm-mq mapped device"); |
bfebd1cd MS |
2836 | return r; |
2837 | } | |
2838 | break; | |
2839 | case DM_TYPE_BIO_BASED: | |
eca7ee6d | 2840 | dm_init_normal_md_queue(md); |
ff36ab34 | 2841 | blk_queue_make_request(md->queue, dm_make_request); |
dbba42d8 MP |
2842 | /* |
2843 | * DM handles splitting bios as needed. Free the bio_split bioset | |
2844 | * since it won't be used (saves 1 process per bio-based DM device). | |
2845 | */ | |
2846 | bioset_free(md->queue->bio_split); | |
2847 | md->queue->bio_split = NULL; | |
bfebd1cd | 2848 | break; |
4a0b4ddf MS |
2849 | } |
2850 | ||
2851 | return 0; | |
2852 | } | |
2853 | ||
2bec1f4a | 2854 | struct mapped_device *dm_get_md(dev_t dev) |
1da177e4 LT |
2855 | { |
2856 | struct mapped_device *md; | |
1da177e4 LT |
2857 | unsigned minor = MINOR(dev); |
2858 | ||
2859 | if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) | |
2860 | return NULL; | |
2861 | ||
f32c10b0 | 2862 | spin_lock(&_minor_lock); |
1da177e4 LT |
2863 | |
2864 | md = idr_find(&_minor_idr, minor); | |
2bec1f4a MP |
2865 | if (md) { |
2866 | if ((md == MINOR_ALLOCED || | |
2867 | (MINOR(disk_devt(dm_disk(md))) != minor) || | |
2868 | dm_deleting_md(md) || | |
2869 | test_bit(DMF_FREEING, &md->flags))) { | |
2870 | md = NULL; | |
2871 | goto out; | |
2872 | } | |
2873 | dm_get(md); | |
fba9f90e | 2874 | } |
1da177e4 | 2875 | |
fba9f90e | 2876 | out: |
f32c10b0 | 2877 | spin_unlock(&_minor_lock); |
1da177e4 | 2878 | |
637842cf DT |
2879 | return md; |
2880 | } | |
3cf2e4ba | 2881 | EXPORT_SYMBOL_GPL(dm_get_md); |
d229a958 | 2882 | |
9ade92a9 | 2883 | void *dm_get_mdptr(struct mapped_device *md) |
637842cf | 2884 | { |
9ade92a9 | 2885 | return md->interface_ptr; |
1da177e4 LT |
2886 | } |
2887 | ||
2888 | void dm_set_mdptr(struct mapped_device *md, void *ptr) | |
2889 | { | |
2890 | md->interface_ptr = ptr; | |
2891 | } | |
2892 | ||
2893 | void dm_get(struct mapped_device *md) | |
2894 | { | |
2895 | atomic_inc(&md->holders); | |
3f77316d | 2896 | BUG_ON(test_bit(DMF_FREEING, &md->flags)); |
1da177e4 LT |
2897 | } |
2898 | ||
09ee96b2 MP |
2899 | int dm_hold(struct mapped_device *md) |
2900 | { | |
2901 | spin_lock(&_minor_lock); | |
2902 | if (test_bit(DMF_FREEING, &md->flags)) { | |
2903 | spin_unlock(&_minor_lock); | |
2904 | return -EBUSY; | |
2905 | } | |
2906 | dm_get(md); | |
2907 | spin_unlock(&_minor_lock); | |
2908 | return 0; | |
2909 | } | |
2910 | EXPORT_SYMBOL_GPL(dm_hold); | |
2911 | ||
72d94861 AK |
2912 | const char *dm_device_name(struct mapped_device *md) |
2913 | { | |
2914 | return md->name; | |
2915 | } | |
2916 | EXPORT_SYMBOL_GPL(dm_device_name); | |
2917 | ||
3f77316d | 2918 | static void __dm_destroy(struct mapped_device *md, bool wait) |
1da177e4 | 2919 | { |
1134e5ae | 2920 | struct dm_table *map; |
83d5e5b0 | 2921 | int srcu_idx; |
1da177e4 | 2922 | |
3f77316d | 2923 | might_sleep(); |
fba9f90e | 2924 | |
63a4f065 | 2925 | spin_lock(&_minor_lock); |
3f77316d KU |
2926 | idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); |
2927 | set_bit(DMF_FREEING, &md->flags); | |
2928 | spin_unlock(&_minor_lock); | |
2929 | ||
02233342 | 2930 | if (dm_request_based(md) && md->kworker_task) |
2eb6e1e3 KB |
2931 | flush_kthread_worker(&md->kworker); |
2932 | ||
ab7c7bb6 MP |
2933 | /* |
2934 | * Take suspend_lock so that presuspend and postsuspend methods | |
2935 | * do not race with internal suspend. | |
2936 | */ | |
2937 | mutex_lock(&md->suspend_lock); | |
2a708cff | 2938 | map = dm_get_live_table(md, &srcu_idx); |
3f77316d KU |
2939 | if (!dm_suspended_md(md)) { |
2940 | dm_table_presuspend_targets(map); | |
2941 | dm_table_postsuspend_targets(map); | |
1da177e4 | 2942 | } |
83d5e5b0 MP |
2943 | /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ |
2944 | dm_put_live_table(md, srcu_idx); | |
2a708cff | 2945 | mutex_unlock(&md->suspend_lock); |
83d5e5b0 | 2946 | |
3f77316d KU |
2947 | /* |
2948 | * Rare, but there may be I/O requests still going to complete, | |
2949 | * for example. Wait for all references to disappear. | |
2950 | * No one should increment the reference count of the mapped_device, | |
2951 | * after the mapped_device state becomes DMF_FREEING. | |
2952 | */ | |
2953 | if (wait) | |
2954 | while (atomic_read(&md->holders)) | |
2955 | msleep(1); | |
2956 | else if (atomic_read(&md->holders)) | |
2957 | DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", | |
2958 | dm_device_name(md), atomic_read(&md->holders)); | |
2959 | ||
2960 | dm_sysfs_exit(md); | |
3f77316d KU |
2961 | dm_table_destroy(__unbind(md)); |
2962 | free_dev(md); | |
2963 | } | |
2964 | ||
2965 | void dm_destroy(struct mapped_device *md) | |
2966 | { | |
2967 | __dm_destroy(md, true); | |
2968 | } | |
2969 | ||
2970 | void dm_destroy_immediate(struct mapped_device *md) | |
2971 | { | |
2972 | __dm_destroy(md, false); | |
2973 | } | |
2974 | ||
2975 | void dm_put(struct mapped_device *md) | |
2976 | { | |
2977 | atomic_dec(&md->holders); | |
1da177e4 | 2978 | } |
79eb885c | 2979 | EXPORT_SYMBOL_GPL(dm_put); |
1da177e4 | 2980 | |
401600df | 2981 | static int dm_wait_for_completion(struct mapped_device *md, int interruptible) |
46125c1c MB |
2982 | { |
2983 | int r = 0; | |
b44ebeb0 MP |
2984 | DECLARE_WAITQUEUE(wait, current); |
2985 | ||
b44ebeb0 | 2986 | add_wait_queue(&md->wait, &wait); |
46125c1c MB |
2987 | |
2988 | while (1) { | |
401600df | 2989 | set_current_state(interruptible); |
46125c1c | 2990 | |
b4324fee | 2991 | if (!md_in_flight(md)) |
46125c1c MB |
2992 | break; |
2993 | ||
401600df MP |
2994 | if (interruptible == TASK_INTERRUPTIBLE && |
2995 | signal_pending(current)) { | |
46125c1c MB |
2996 | r = -EINTR; |
2997 | break; | |
2998 | } | |
2999 | ||
3000 | io_schedule(); | |
3001 | } | |
3002 | set_current_state(TASK_RUNNING); | |
3003 | ||
b44ebeb0 MP |
3004 | remove_wait_queue(&md->wait, &wait); |
3005 | ||
46125c1c MB |
3006 | return r; |
3007 | } | |
3008 | ||
1da177e4 LT |
3009 | /* |
3010 | * Process the deferred bios | |
3011 | */ | |
ef208587 | 3012 | static void dm_wq_work(struct work_struct *work) |
1da177e4 | 3013 | { |
ef208587 MP |
3014 | struct mapped_device *md = container_of(work, struct mapped_device, |
3015 | work); | |
6d6f10df | 3016 | struct bio *c; |
83d5e5b0 MP |
3017 | int srcu_idx; |
3018 | struct dm_table *map; | |
1da177e4 | 3019 | |
83d5e5b0 | 3020 | map = dm_get_live_table(md, &srcu_idx); |
ef208587 | 3021 | |
3b00b203 | 3022 | while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { |
df12ee99 AK |
3023 | spin_lock_irq(&md->deferred_lock); |
3024 | c = bio_list_pop(&md->deferred); | |
3025 | spin_unlock_irq(&md->deferred_lock); | |
3026 | ||
6a8736d1 | 3027 | if (!c) |
df12ee99 | 3028 | break; |
022c2611 | 3029 | |
e6ee8c0b KU |
3030 | if (dm_request_based(md)) |
3031 | generic_make_request(c); | |
6a8736d1 | 3032 | else |
83d5e5b0 | 3033 | __split_and_process_bio(md, map, c); |
022c2611 | 3034 | } |
73d410c0 | 3035 | |
83d5e5b0 | 3036 | dm_put_live_table(md, srcu_idx); |
1da177e4 LT |
3037 | } |
3038 | ||
9a1fb464 | 3039 | static void dm_queue_flush(struct mapped_device *md) |
304f3f6a | 3040 | { |
3b00b203 | 3041 | clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
4e857c58 | 3042 | smp_mb__after_atomic(); |
53d5914f | 3043 | queue_work(md->wq, &md->work); |
304f3f6a MB |
3044 | } |
3045 | ||
1da177e4 | 3046 | /* |
042d2a9b | 3047 | * Swap in a new table, returning the old one for the caller to destroy. |
1da177e4 | 3048 | */ |
042d2a9b | 3049 | struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) |
1da177e4 | 3050 | { |
87eb5b21 | 3051 | struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); |
754c5fc7 | 3052 | struct queue_limits limits; |
042d2a9b | 3053 | int r; |
1da177e4 | 3054 | |
e61290a4 | 3055 | mutex_lock(&md->suspend_lock); |
1da177e4 LT |
3056 | |
3057 | /* device must be suspended */ | |
4f186f8b | 3058 | if (!dm_suspended_md(md)) |
93c534ae | 3059 | goto out; |
1da177e4 | 3060 | |
3ae70656 MS |
3061 | /* |
3062 | * If the new table has no data devices, retain the existing limits. | |
3063 | * This helps multipath with queue_if_no_path if all paths disappear, | |
3064 | * then new I/O is queued based on these limits, and then some paths | |
3065 | * reappear. | |
3066 | */ | |
3067 | if (dm_table_has_no_data_devices(table)) { | |
83d5e5b0 | 3068 | live_map = dm_get_live_table_fast(md); |
3ae70656 MS |
3069 | if (live_map) |
3070 | limits = md->queue->limits; | |
83d5e5b0 | 3071 | dm_put_live_table_fast(md); |
3ae70656 MS |
3072 | } |
3073 | ||
87eb5b21 MC |
3074 | if (!live_map) { |
3075 | r = dm_calculate_queue_limits(table, &limits); | |
3076 | if (r) { | |
3077 | map = ERR_PTR(r); | |
3078 | goto out; | |
3079 | } | |
042d2a9b | 3080 | } |
754c5fc7 | 3081 | |
042d2a9b | 3082 | map = __bind(md, table, &limits); |
1da177e4 | 3083 | |
93c534ae | 3084 | out: |
e61290a4 | 3085 | mutex_unlock(&md->suspend_lock); |
042d2a9b | 3086 | return map; |
1da177e4 LT |
3087 | } |
3088 | ||
3089 | /* | |
3090 | * Functions to lock and unlock any filesystem running on the | |
3091 | * device. | |
3092 | */ | |
2ca3310e | 3093 | static int lock_fs(struct mapped_device *md) |
1da177e4 | 3094 | { |
e39e2e95 | 3095 | int r; |
1da177e4 LT |
3096 | |
3097 | WARN_ON(md->frozen_sb); | |
dfbe03f6 | 3098 | |
db8fef4f | 3099 | md->frozen_sb = freeze_bdev(md->bdev); |
dfbe03f6 | 3100 | if (IS_ERR(md->frozen_sb)) { |
cf222b37 | 3101 | r = PTR_ERR(md->frozen_sb); |
e39e2e95 AK |
3102 | md->frozen_sb = NULL; |
3103 | return r; | |
dfbe03f6 AK |
3104 | } |
3105 | ||
aa8d7c2f AK |
3106 | set_bit(DMF_FROZEN, &md->flags); |
3107 | ||
1da177e4 LT |
3108 | return 0; |
3109 | } | |
3110 | ||
2ca3310e | 3111 | static void unlock_fs(struct mapped_device *md) |
1da177e4 | 3112 | { |
aa8d7c2f AK |
3113 | if (!test_bit(DMF_FROZEN, &md->flags)) |
3114 | return; | |
3115 | ||
db8fef4f | 3116 | thaw_bdev(md->bdev, md->frozen_sb); |
1da177e4 | 3117 | md->frozen_sb = NULL; |
aa8d7c2f | 3118 | clear_bit(DMF_FROZEN, &md->flags); |
1da177e4 LT |
3119 | } |
3120 | ||
3121 | /* | |
ffcc3936 MS |
3122 | * If __dm_suspend returns 0, the device is completely quiescent |
3123 | * now. There is no request-processing activity. All new requests | |
3124 | * are being added to md->deferred list. | |
cec47e3d | 3125 | * |
ffcc3936 | 3126 | * Caller must hold md->suspend_lock |
cec47e3d | 3127 | */ |
ffcc3936 MS |
3128 | static int __dm_suspend(struct mapped_device *md, struct dm_table *map, |
3129 | unsigned suspend_flags, int interruptible) | |
1da177e4 | 3130 | { |
ffcc3936 MS |
3131 | bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; |
3132 | bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; | |
3133 | int r; | |
1da177e4 | 3134 | |
2e93ccc1 KU |
3135 | /* |
3136 | * DMF_NOFLUSH_SUSPENDING must be set before presuspend. | |
3137 | * This flag is cleared before dm_suspend returns. | |
3138 | */ | |
3139 | if (noflush) | |
3140 | set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); | |
3141 | ||
d67ee213 MS |
3142 | /* |
3143 | * This gets reverted if there's an error later and the targets | |
3144 | * provide the .presuspend_undo hook. | |
3145 | */ | |
cf222b37 AK |
3146 | dm_table_presuspend_targets(map); |
3147 | ||
32a926da | 3148 | /* |
9f518b27 KU |
3149 | * Flush I/O to the device. |
3150 | * Any I/O submitted after lock_fs() may not be flushed. | |
3151 | * noflush takes precedence over do_lockfs. | |
3152 | * (lock_fs() flushes I/Os and waits for them to complete.) | |
32a926da MP |
3153 | */ |
3154 | if (!noflush && do_lockfs) { | |
3155 | r = lock_fs(md); | |
d67ee213 MS |
3156 | if (r) { |
3157 | dm_table_presuspend_undo_targets(map); | |
ffcc3936 | 3158 | return r; |
d67ee213 | 3159 | } |
aa8d7c2f | 3160 | } |
1da177e4 LT |
3161 | |
3162 | /* | |
3b00b203 MP |
3163 | * Here we must make sure that no processes are submitting requests |
3164 | * to target drivers i.e. no one may be executing | |
3165 | * __split_and_process_bio. This is called from dm_request and | |
3166 | * dm_wq_work. | |
3167 | * | |
3168 | * To get all processes out of __split_and_process_bio in dm_request, | |
3169 | * we take the write lock. To prevent any process from reentering | |
6a8736d1 TH |
3170 | * __split_and_process_bio from dm_request and quiesce the thread |
3171 | * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call | |
3172 | * flush_workqueue(md->wq). | |
1da177e4 | 3173 | */ |
1eb787ec | 3174 | set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
41abc4e1 HR |
3175 | if (map) |
3176 | synchronize_srcu(&md->io_barrier); | |
1da177e4 | 3177 | |
d0bcb878 | 3178 | /* |
29e4013d TH |
3179 | * Stop md->queue before flushing md->wq in case request-based |
3180 | * dm defers requests to md->wq from md->queue. | |
d0bcb878 | 3181 | */ |
2eb6e1e3 | 3182 | if (dm_request_based(md)) { |
eca7ee6d | 3183 | dm_stop_queue(md->queue); |
02233342 MS |
3184 | if (md->kworker_task) |
3185 | flush_kthread_worker(&md->kworker); | |
2eb6e1e3 | 3186 | } |
cec47e3d | 3187 | |
d0bcb878 KU |
3188 | flush_workqueue(md->wq); |
3189 | ||
1da177e4 | 3190 | /* |
3b00b203 MP |
3191 | * At this point no more requests are entering target request routines. |
3192 | * We call dm_wait_for_completion to wait for all existing requests | |
3193 | * to finish. | |
1da177e4 | 3194 | */ |
ffcc3936 | 3195 | r = dm_wait_for_completion(md, interruptible); |
1da177e4 | 3196 | |
6d6f10df | 3197 | if (noflush) |
022c2611 | 3198 | clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
41abc4e1 HR |
3199 | if (map) |
3200 | synchronize_srcu(&md->io_barrier); | |
2e93ccc1 | 3201 | |
1da177e4 | 3202 | /* were we interrupted ? */ |
46125c1c | 3203 | if (r < 0) { |
9a1fb464 | 3204 | dm_queue_flush(md); |
73d410c0 | 3205 | |
cec47e3d | 3206 | if (dm_request_based(md)) |
eca7ee6d | 3207 | dm_start_queue(md->queue); |
cec47e3d | 3208 | |
2ca3310e | 3209 | unlock_fs(md); |
d67ee213 | 3210 | dm_table_presuspend_undo_targets(map); |
ffcc3936 | 3211 | /* pushback list is already flushed, so skip flush */ |
2ca3310e | 3212 | } |
1da177e4 | 3213 | |
ffcc3936 MS |
3214 | return r; |
3215 | } | |
3216 | ||
3217 | /* | |
3218 | * We need to be able to change a mapping table under a mounted | |
3219 | * filesystem. For example we might want to move some data in | |
3220 | * the background. Before the table can be swapped with | |
3221 | * dm_bind_table, dm_suspend must be called to flush any in | |
3222 | * flight bios and ensure that any further io gets deferred. | |
3223 | */ | |
3224 | /* | |
3225 | * Suspend mechanism in request-based dm. | |
3226 | * | |
3227 | * 1. Flush all I/Os by lock_fs() if needed. | |
3228 | * 2. Stop dispatching any I/O by stopping the request_queue. | |
3229 | * 3. Wait for all in-flight I/Os to be completed or requeued. | |
3230 | * | |
3231 | * To abort suspend, start the request_queue. | |
3232 | */ | |
3233 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags) | |
3234 | { | |
3235 | struct dm_table *map = NULL; | |
3236 | int r = 0; | |
3237 | ||
3238 | retry: | |
3239 | mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); | |
3240 | ||
3241 | if (dm_suspended_md(md)) { | |
3242 | r = -EINVAL; | |
3243 | goto out_unlock; | |
3244 | } | |
3245 | ||
3246 | if (dm_suspended_internally_md(md)) { | |
3247 | /* already internally suspended, wait for internal resume */ | |
3248 | mutex_unlock(&md->suspend_lock); | |
3249 | r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); | |
3250 | if (r) | |
3251 | return r; | |
3252 | goto retry; | |
3253 | } | |
3254 | ||
a12f5d48 | 3255 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
ffcc3936 MS |
3256 | |
3257 | r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE); | |
3258 | if (r) | |
3259 | goto out_unlock; | |
3b00b203 | 3260 | |
2ca3310e | 3261 | set_bit(DMF_SUSPENDED, &md->flags); |
b84b0287 | 3262 | |
4d4471cb KU |
3263 | dm_table_postsuspend_targets(map); |
3264 | ||
d287483d | 3265 | out_unlock: |
e61290a4 | 3266 | mutex_unlock(&md->suspend_lock); |
cf222b37 | 3267 | return r; |
1da177e4 LT |
3268 | } |
3269 | ||
ffcc3936 MS |
3270 | static int __dm_resume(struct mapped_device *md, struct dm_table *map) |
3271 | { | |
3272 | if (map) { | |
3273 | int r = dm_table_resume_targets(map); | |
3274 | if (r) | |
3275 | return r; | |
3276 | } | |
3277 | ||
3278 | dm_queue_flush(md); | |
3279 | ||
3280 | /* | |
3281 | * Flushing deferred I/Os must be done after targets are resumed | |
3282 | * so that mapping of targets can work correctly. | |
3283 | * Request-based dm is queueing the deferred I/Os in its request_queue. | |
3284 | */ | |
3285 | if (dm_request_based(md)) | |
eca7ee6d | 3286 | dm_start_queue(md->queue); |
ffcc3936 MS |
3287 | |
3288 | unlock_fs(md); | |
3289 | ||
3290 | return 0; | |
3291 | } | |
3292 | ||
1da177e4 LT |
3293 | int dm_resume(struct mapped_device *md) |
3294 | { | |
cf222b37 | 3295 | int r = -EINVAL; |
cf222b37 | 3296 | struct dm_table *map = NULL; |
1da177e4 | 3297 | |
ffcc3936 MS |
3298 | retry: |
3299 | mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); | |
3300 | ||
4f186f8b | 3301 | if (!dm_suspended_md(md)) |
cf222b37 | 3302 | goto out; |
cf222b37 | 3303 | |
ffcc3936 MS |
3304 | if (dm_suspended_internally_md(md)) { |
3305 | /* already internally suspended, wait for internal resume */ | |
3306 | mutex_unlock(&md->suspend_lock); | |
3307 | r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); | |
3308 | if (r) | |
3309 | return r; | |
3310 | goto retry; | |
3311 | } | |
3312 | ||
a12f5d48 | 3313 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
2ca3310e | 3314 | if (!map || !dm_table_get_size(map)) |
cf222b37 | 3315 | goto out; |
1da177e4 | 3316 | |
ffcc3936 | 3317 | r = __dm_resume(md, map); |
8757b776 MB |
3318 | if (r) |
3319 | goto out; | |
2ca3310e | 3320 | |
2ca3310e AK |
3321 | clear_bit(DMF_SUSPENDED, &md->flags); |
3322 | ||
cf222b37 AK |
3323 | r = 0; |
3324 | out: | |
e61290a4 | 3325 | mutex_unlock(&md->suspend_lock); |
2ca3310e | 3326 | |
cf222b37 | 3327 | return r; |
1da177e4 LT |
3328 | } |
3329 | ||
fd2ed4d2 MP |
3330 | /* |
3331 | * Internal suspend/resume works like userspace-driven suspend. It waits | |
3332 | * until all bios finish and prevents issuing new bios to the target drivers. | |
3333 | * It may be used only from the kernel. | |
fd2ed4d2 MP |
3334 | */ |
3335 | ||
ffcc3936 | 3336 | static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) |
fd2ed4d2 | 3337 | { |
ffcc3936 MS |
3338 | struct dm_table *map = NULL; |
3339 | ||
96b26c8c | 3340 | if (md->internal_suspend_count++) |
ffcc3936 MS |
3341 | return; /* nested internal suspend */ |
3342 | ||
3343 | if (dm_suspended_md(md)) { | |
3344 | set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); | |
3345 | return; /* nest suspend */ | |
3346 | } | |
3347 | ||
a12f5d48 | 3348 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
ffcc3936 MS |
3349 | |
3350 | /* | |
3351 | * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is | |
3352 | * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend | |
3353 | * would require changing .presuspend to return an error -- avoid this | |
3354 | * until there is a need for more elaborate variants of internal suspend. | |
3355 | */ | |
3356 | (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE); | |
3357 | ||
3358 | set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); | |
3359 | ||
3360 | dm_table_postsuspend_targets(map); | |
3361 | } | |
3362 | ||
3363 | static void __dm_internal_resume(struct mapped_device *md) | |
3364 | { | |
96b26c8c MP |
3365 | BUG_ON(!md->internal_suspend_count); |
3366 | ||
3367 | if (--md->internal_suspend_count) | |
ffcc3936 MS |
3368 | return; /* resume from nested internal suspend */ |
3369 | ||
fd2ed4d2 | 3370 | if (dm_suspended_md(md)) |
ffcc3936 MS |
3371 | goto done; /* resume from nested suspend */ |
3372 | ||
3373 | /* | |
3374 | * NOTE: existing callers don't need to call dm_table_resume_targets | |
3375 | * (which may fail -- so best to avoid it for now by passing NULL map) | |
3376 | */ | |
3377 | (void) __dm_resume(md, NULL); | |
3378 | ||
3379 | done: | |
3380 | clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); | |
3381 | smp_mb__after_atomic(); | |
3382 | wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); | |
3383 | } | |
3384 | ||
3385 | void dm_internal_suspend_noflush(struct mapped_device *md) | |
3386 | { | |
3387 | mutex_lock(&md->suspend_lock); | |
3388 | __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); | |
3389 | mutex_unlock(&md->suspend_lock); | |
3390 | } | |
3391 | EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); | |
3392 | ||
3393 | void dm_internal_resume(struct mapped_device *md) | |
3394 | { | |
3395 | mutex_lock(&md->suspend_lock); | |
3396 | __dm_internal_resume(md); | |
3397 | mutex_unlock(&md->suspend_lock); | |
3398 | } | |
3399 | EXPORT_SYMBOL_GPL(dm_internal_resume); | |
3400 | ||
3401 | /* | |
3402 | * Fast variants of internal suspend/resume hold md->suspend_lock, | |
3403 | * which prevents interaction with userspace-driven suspend. | |
3404 | */ | |
3405 | ||
3406 | void dm_internal_suspend_fast(struct mapped_device *md) | |
3407 | { | |
3408 | mutex_lock(&md->suspend_lock); | |
3409 | if (dm_suspended_md(md) || dm_suspended_internally_md(md)) | |
fd2ed4d2 MP |
3410 | return; |
3411 | ||
3412 | set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); | |
3413 | synchronize_srcu(&md->io_barrier); | |
3414 | flush_workqueue(md->wq); | |
3415 | dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); | |
3416 | } | |
b735fede | 3417 | EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); |
fd2ed4d2 | 3418 | |
ffcc3936 | 3419 | void dm_internal_resume_fast(struct mapped_device *md) |
fd2ed4d2 | 3420 | { |
ffcc3936 | 3421 | if (dm_suspended_md(md) || dm_suspended_internally_md(md)) |
fd2ed4d2 MP |
3422 | goto done; |
3423 | ||
3424 | dm_queue_flush(md); | |
3425 | ||
3426 | done: | |
3427 | mutex_unlock(&md->suspend_lock); | |
3428 | } | |
b735fede | 3429 | EXPORT_SYMBOL_GPL(dm_internal_resume_fast); |
fd2ed4d2 | 3430 | |
1da177e4 LT |
3431 | /*----------------------------------------------------------------- |
3432 | * Event notification. | |
3433 | *---------------------------------------------------------------*/ | |
3abf85b5 | 3434 | int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, |
60935eb2 | 3435 | unsigned cookie) |
69267a30 | 3436 | { |
60935eb2 MB |
3437 | char udev_cookie[DM_COOKIE_LENGTH]; |
3438 | char *envp[] = { udev_cookie, NULL }; | |
3439 | ||
3440 | if (!cookie) | |
3abf85b5 | 3441 | return kobject_uevent(&disk_to_dev(md->disk)->kobj, action); |
60935eb2 MB |
3442 | else { |
3443 | snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", | |
3444 | DM_COOKIE_ENV_VAR_NAME, cookie); | |
3abf85b5 PR |
3445 | return kobject_uevent_env(&disk_to_dev(md->disk)->kobj, |
3446 | action, envp); | |
60935eb2 | 3447 | } |
69267a30 AK |
3448 | } |
3449 | ||
7a8c3d3b MA |
3450 | uint32_t dm_next_uevent_seq(struct mapped_device *md) |
3451 | { | |
3452 | return atomic_add_return(1, &md->uevent_seq); | |
3453 | } | |
3454 | ||
1da177e4 LT |
3455 | uint32_t dm_get_event_nr(struct mapped_device *md) |
3456 | { | |
3457 | return atomic_read(&md->event_nr); | |
3458 | } | |
3459 | ||
3460 | int dm_wait_event(struct mapped_device *md, int event_nr) | |
3461 | { | |
3462 | return wait_event_interruptible(md->eventq, | |
3463 | (event_nr != atomic_read(&md->event_nr))); | |
3464 | } | |
3465 | ||
7a8c3d3b MA |
3466 | void dm_uevent_add(struct mapped_device *md, struct list_head *elist) |
3467 | { | |
3468 | unsigned long flags; | |
3469 | ||
3470 | spin_lock_irqsave(&md->uevent_lock, flags); | |
3471 | list_add(elist, &md->uevent_list); | |
3472 | spin_unlock_irqrestore(&md->uevent_lock, flags); | |
3473 | } | |
3474 | ||
1da177e4 LT |
3475 | /* |
3476 | * The gendisk is only valid as long as you have a reference | |
3477 | * count on 'md'. | |
3478 | */ | |
3479 | struct gendisk *dm_disk(struct mapped_device *md) | |
3480 | { | |
3481 | return md->disk; | |
3482 | } | |
65ff5b7d | 3483 | EXPORT_SYMBOL_GPL(dm_disk); |
1da177e4 | 3484 | |
784aae73 MB |
3485 | struct kobject *dm_kobject(struct mapped_device *md) |
3486 | { | |
2995fa78 | 3487 | return &md->kobj_holder.kobj; |
784aae73 MB |
3488 | } |
3489 | ||
784aae73 MB |
3490 | struct mapped_device *dm_get_from_kobject(struct kobject *kobj) |
3491 | { | |
3492 | struct mapped_device *md; | |
3493 | ||
2995fa78 | 3494 | md = container_of(kobj, struct mapped_device, kobj_holder.kobj); |
784aae73 | 3495 | |
4d89b7b4 | 3496 | if (test_bit(DMF_FREEING, &md->flags) || |
432a212c | 3497 | dm_deleting_md(md)) |
4d89b7b4 MB |
3498 | return NULL; |
3499 | ||
784aae73 MB |
3500 | dm_get(md); |
3501 | return md; | |
3502 | } | |
3503 | ||
4f186f8b | 3504 | int dm_suspended_md(struct mapped_device *md) |
1da177e4 LT |
3505 | { |
3506 | return test_bit(DMF_SUSPENDED, &md->flags); | |
3507 | } | |
3508 | ||
ffcc3936 MS |
3509 | int dm_suspended_internally_md(struct mapped_device *md) |
3510 | { | |
3511 | return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); | |
3512 | } | |
3513 | ||
2c140a24 MP |
3514 | int dm_test_deferred_remove_flag(struct mapped_device *md) |
3515 | { | |
3516 | return test_bit(DMF_DEFERRED_REMOVE, &md->flags); | |
3517 | } | |
3518 | ||
64dbce58 KU |
3519 | int dm_suspended(struct dm_target *ti) |
3520 | { | |
ecdb2e25 | 3521 | return dm_suspended_md(dm_table_get_md(ti->table)); |
64dbce58 KU |
3522 | } |
3523 | EXPORT_SYMBOL_GPL(dm_suspended); | |
3524 | ||
2e93ccc1 KU |
3525 | int dm_noflush_suspending(struct dm_target *ti) |
3526 | { | |
ecdb2e25 | 3527 | return __noflush_suspending(dm_table_get_md(ti->table)); |
2e93ccc1 KU |
3528 | } |
3529 | EXPORT_SYMBOL_GPL(dm_noflush_suspending); | |
3530 | ||
78d8e58a | 3531 | struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type, |
30187e1d | 3532 | unsigned integrity, unsigned per_io_data_size) |
e6ee8c0b | 3533 | { |
115485e8 | 3534 | struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); |
78d8e58a MS |
3535 | struct kmem_cache *cachep = NULL; |
3536 | unsigned int pool_size = 0; | |
5f015204 | 3537 | unsigned int front_pad; |
e6ee8c0b KU |
3538 | |
3539 | if (!pools) | |
4e6e36c3 | 3540 | return NULL; |
e6ee8c0b | 3541 | |
78d8e58a | 3542 | type = filter_md_type(type, md); |
17e149b8 | 3543 | |
78d8e58a MS |
3544 | switch (type) { |
3545 | case DM_TYPE_BIO_BASED: | |
3546 | cachep = _io_cache; | |
3547 | pool_size = dm_get_reserved_bio_based_ios(); | |
30187e1d | 3548 | front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone); |
78d8e58a MS |
3549 | break; |
3550 | case DM_TYPE_REQUEST_BASED: | |
3551 | cachep = _rq_tio_cache; | |
3552 | pool_size = dm_get_reserved_rq_based_ios(); | |
3553 | pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache); | |
3554 | if (!pools->rq_pool) | |
3555 | goto out; | |
3556 | /* fall through to setup remaining rq-based pools */ | |
3557 | case DM_TYPE_MQ_REQUEST_BASED: | |
3558 | if (!pool_size) | |
3559 | pool_size = dm_get_reserved_rq_based_ios(); | |
3560 | front_pad = offsetof(struct dm_rq_clone_bio_info, clone); | |
591ddcfc | 3561 | /* per_io_data_size is used for blk-mq pdu at queue allocation */ |
78d8e58a MS |
3562 | break; |
3563 | default: | |
3564 | BUG(); | |
3565 | } | |
3566 | ||
3567 | if (cachep) { | |
3568 | pools->io_pool = mempool_create_slab_pool(pool_size, cachep); | |
3569 | if (!pools->io_pool) | |
3570 | goto out; | |
3571 | } | |
e6ee8c0b | 3572 | |
3d8aab2d | 3573 | pools->bs = bioset_create_nobvec(pool_size, front_pad); |
e6ee8c0b | 3574 | if (!pools->bs) |
5f015204 | 3575 | goto out; |
e6ee8c0b | 3576 | |
a91a2785 | 3577 | if (integrity && bioset_integrity_create(pools->bs, pool_size)) |
5f015204 | 3578 | goto out; |
a91a2785 | 3579 | |
e6ee8c0b | 3580 | return pools; |
5f1b670d | 3581 | |
5f1b670d CH |
3582 | out: |
3583 | dm_free_md_mempools(pools); | |
78d8e58a | 3584 | |
4e6e36c3 | 3585 | return NULL; |
e6ee8c0b KU |
3586 | } |
3587 | ||
3588 | void dm_free_md_mempools(struct dm_md_mempools *pools) | |
3589 | { | |
3590 | if (!pools) | |
3591 | return; | |
3592 | ||
6f65985e JL |
3593 | mempool_destroy(pools->io_pool); |
3594 | mempool_destroy(pools->rq_pool); | |
1ae49ea2 | 3595 | |
e6ee8c0b KU |
3596 | if (pools->bs) |
3597 | bioset_free(pools->bs); | |
3598 | ||
3599 | kfree(pools); | |
3600 | } | |
3601 | ||
71cdb697 | 3602 | static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, |
956a4025 | 3603 | u32 flags) |
71cdb697 CH |
3604 | { |
3605 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3606 | const struct pr_ops *ops; | |
71cdb697 | 3607 | fmode_t mode; |
956a4025 | 3608 | int r; |
71cdb697 | 3609 | |
956a4025 | 3610 | r = dm_grab_bdev_for_ioctl(md, &bdev, &mode); |
71cdb697 CH |
3611 | if (r < 0) |
3612 | return r; | |
3613 | ||
3614 | ops = bdev->bd_disk->fops->pr_ops; | |
3615 | if (ops && ops->pr_register) | |
3616 | r = ops->pr_register(bdev, old_key, new_key, flags); | |
3617 | else | |
3618 | r = -EOPNOTSUPP; | |
3619 | ||
956a4025 | 3620 | bdput(bdev); |
71cdb697 CH |
3621 | return r; |
3622 | } | |
3623 | ||
3624 | static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, | |
956a4025 | 3625 | u32 flags) |
71cdb697 CH |
3626 | { |
3627 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3628 | const struct pr_ops *ops; | |
71cdb697 | 3629 | fmode_t mode; |
956a4025 | 3630 | int r; |
71cdb697 | 3631 | |
956a4025 | 3632 | r = dm_grab_bdev_for_ioctl(md, &bdev, &mode); |
71cdb697 CH |
3633 | if (r < 0) |
3634 | return r; | |
3635 | ||
3636 | ops = bdev->bd_disk->fops->pr_ops; | |
3637 | if (ops && ops->pr_reserve) | |
3638 | r = ops->pr_reserve(bdev, key, type, flags); | |
3639 | else | |
3640 | r = -EOPNOTSUPP; | |
3641 | ||
956a4025 | 3642 | bdput(bdev); |
71cdb697 CH |
3643 | return r; |
3644 | } | |
3645 | ||
3646 | static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) | |
3647 | { | |
3648 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3649 | const struct pr_ops *ops; | |
71cdb697 | 3650 | fmode_t mode; |
956a4025 | 3651 | int r; |
71cdb697 | 3652 | |
956a4025 | 3653 | r = dm_grab_bdev_for_ioctl(md, &bdev, &mode); |
71cdb697 CH |
3654 | if (r < 0) |
3655 | return r; | |
3656 | ||
3657 | ops = bdev->bd_disk->fops->pr_ops; | |
3658 | if (ops && ops->pr_release) | |
3659 | r = ops->pr_release(bdev, key, type); | |
3660 | else | |
3661 | r = -EOPNOTSUPP; | |
3662 | ||
956a4025 | 3663 | bdput(bdev); |
71cdb697 CH |
3664 | return r; |
3665 | } | |
3666 | ||
3667 | static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, | |
956a4025 | 3668 | enum pr_type type, bool abort) |
71cdb697 CH |
3669 | { |
3670 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3671 | const struct pr_ops *ops; | |
71cdb697 | 3672 | fmode_t mode; |
956a4025 | 3673 | int r; |
71cdb697 | 3674 | |
956a4025 | 3675 | r = dm_grab_bdev_for_ioctl(md, &bdev, &mode); |
71cdb697 CH |
3676 | if (r < 0) |
3677 | return r; | |
3678 | ||
3679 | ops = bdev->bd_disk->fops->pr_ops; | |
3680 | if (ops && ops->pr_preempt) | |
3681 | r = ops->pr_preempt(bdev, old_key, new_key, type, abort); | |
3682 | else | |
3683 | r = -EOPNOTSUPP; | |
3684 | ||
956a4025 | 3685 | bdput(bdev); |
71cdb697 CH |
3686 | return r; |
3687 | } | |
3688 | ||
3689 | static int dm_pr_clear(struct block_device *bdev, u64 key) | |
3690 | { | |
3691 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3692 | const struct pr_ops *ops; | |
71cdb697 | 3693 | fmode_t mode; |
956a4025 | 3694 | int r; |
71cdb697 | 3695 | |
956a4025 | 3696 | r = dm_grab_bdev_for_ioctl(md, &bdev, &mode); |
71cdb697 CH |
3697 | if (r < 0) |
3698 | return r; | |
3699 | ||
3700 | ops = bdev->bd_disk->fops->pr_ops; | |
3701 | if (ops && ops->pr_clear) | |
3702 | r = ops->pr_clear(bdev, key); | |
3703 | else | |
3704 | r = -EOPNOTSUPP; | |
3705 | ||
956a4025 | 3706 | bdput(bdev); |
71cdb697 CH |
3707 | return r; |
3708 | } | |
3709 | ||
3710 | static const struct pr_ops dm_pr_ops = { | |
3711 | .pr_register = dm_pr_register, | |
3712 | .pr_reserve = dm_pr_reserve, | |
3713 | .pr_release = dm_pr_release, | |
3714 | .pr_preempt = dm_pr_preempt, | |
3715 | .pr_clear = dm_pr_clear, | |
3716 | }; | |
3717 | ||
83d5cde4 | 3718 | static const struct block_device_operations dm_blk_dops = { |
1da177e4 LT |
3719 | .open = dm_blk_open, |
3720 | .release = dm_blk_close, | |
aa129a22 | 3721 | .ioctl = dm_blk_ioctl, |
3ac51e74 | 3722 | .getgeo = dm_blk_getgeo, |
71cdb697 | 3723 | .pr_ops = &dm_pr_ops, |
1da177e4 LT |
3724 | .owner = THIS_MODULE |
3725 | }; | |
3726 | ||
1da177e4 LT |
3727 | /* |
3728 | * module hooks | |
3729 | */ | |
3730 | module_init(dm_init); | |
3731 | module_exit(dm_exit); | |
3732 | ||
3733 | module_param(major, uint, 0); | |
3734 | MODULE_PARM_DESC(major, "The major number of the device mapper"); | |
f4790826 | 3735 | |
e8603136 MS |
3736 | module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); |
3737 | MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); | |
3738 | ||
f4790826 MS |
3739 | module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR); |
3740 | MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools"); | |
3741 | ||
17e149b8 MS |
3742 | module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR); |
3743 | MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices"); | |
3744 | ||
faad87df MS |
3745 | module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR); |
3746 | MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices"); | |
3747 | ||
3748 | module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR); | |
3749 | MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices"); | |
3750 | ||
115485e8 MS |
3751 | module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); |
3752 | MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); | |
3753 | ||
1da177e4 LT |
3754 | MODULE_DESCRIPTION(DM_NAME " driver"); |
3755 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); | |
3756 | MODULE_LICENSE("GPL"); |