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