]>
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 | ||
4cc96131 MS |
8 | #include "dm-core.h" |
9 | #include "dm-rq.h" | |
51e5b2bd | 10 | #include "dm-uevent.h" |
1da177e4 LT |
11 | |
12 | #include <linux/init.h> | |
13 | #include <linux/module.h> | |
48c9c27b | 14 | #include <linux/mutex.h> |
6958c1c6 | 15 | #include <linux/sched/mm.h> |
174cd4b1 | 16 | #include <linux/sched/signal.h> |
1da177e4 LT |
17 | #include <linux/blkpg.h> |
18 | #include <linux/bio.h> | |
1da177e4 | 19 | #include <linux/mempool.h> |
f26c5719 | 20 | #include <linux/dax.h> |
1da177e4 LT |
21 | #include <linux/slab.h> |
22 | #include <linux/idr.h> | |
7e026c8c | 23 | #include <linux/uio.h> |
3ac51e74 | 24 | #include <linux/hdreg.h> |
3f77316d | 25 | #include <linux/delay.h> |
ffcc3936 | 26 | #include <linux/wait.h> |
71cdb697 | 27 | #include <linux/pr.h> |
b0b4d7c6 | 28 | #include <linux/refcount.h> |
c6a564ff | 29 | #include <linux/part_stat.h> |
a892c8d5 | 30 | #include <linux/blk-crypto.h> |
55782138 | 31 | |
72d94861 AK |
32 | #define DM_MSG_PREFIX "core" |
33 | ||
60935eb2 MB |
34 | /* |
35 | * Cookies are numeric values sent with CHANGE and REMOVE | |
36 | * uevents while resuming, removing or renaming the device. | |
37 | */ | |
38 | #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" | |
39 | #define DM_COOKIE_LENGTH 24 | |
40 | ||
1da177e4 LT |
41 | static const char *_name = DM_NAME; |
42 | ||
43 | static unsigned int major = 0; | |
44 | static unsigned int _major = 0; | |
45 | ||
d15b774c AK |
46 | static DEFINE_IDR(_minor_idr); |
47 | ||
f32c10b0 | 48 | static DEFINE_SPINLOCK(_minor_lock); |
2c140a24 MP |
49 | |
50 | static void do_deferred_remove(struct work_struct *w); | |
51 | ||
52 | static DECLARE_WORK(deferred_remove_work, do_deferred_remove); | |
53 | ||
acfe0ad7 MP |
54 | static struct workqueue_struct *deferred_remove_workqueue; |
55 | ||
93e6442c MP |
56 | atomic_t dm_global_event_nr = ATOMIC_INIT(0); |
57 | DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq); | |
58 | ||
62e08243 MP |
59 | void dm_issue_global_event(void) |
60 | { | |
61 | atomic_inc(&dm_global_event_nr); | |
62 | wake_up(&dm_global_eventq); | |
63 | } | |
64 | ||
1da177e4 | 65 | /* |
64f52b0e | 66 | * One of these is allocated (on-stack) per original bio. |
1da177e4 | 67 | */ |
64f52b0e | 68 | struct clone_info { |
64f52b0e MS |
69 | struct dm_table *map; |
70 | struct bio *bio; | |
71 | struct dm_io *io; | |
72 | sector_t sector; | |
73 | unsigned sector_count; | |
74 | }; | |
75 | ||
76 | /* | |
77 | * One of these is allocated per clone bio. | |
78 | */ | |
79 | #define DM_TIO_MAGIC 7282014 | |
80 | struct dm_target_io { | |
81 | unsigned magic; | |
82 | struct dm_io *io; | |
83 | struct dm_target *ti; | |
84 | unsigned target_bio_nr; | |
85 | unsigned *len_ptr; | |
86 | bool inside_dm_io; | |
87 | struct bio clone; | |
88 | }; | |
89 | ||
1da177e4 | 90 | /* |
745dc570 | 91 | * One of these is allocated per original bio. |
64f52b0e | 92 | * It contains the first clone used for that original. |
1da177e4 | 93 | */ |
64f52b0e | 94 | #define DM_IO_MAGIC 5191977 |
1da177e4 | 95 | struct dm_io { |
64f52b0e | 96 | unsigned magic; |
1da177e4 | 97 | struct mapped_device *md; |
4e4cbee9 | 98 | blk_status_t status; |
1da177e4 | 99 | atomic_t io_count; |
745dc570 | 100 | struct bio *orig_bio; |
3eaf840e | 101 | unsigned long start_time; |
f88fb981 | 102 | spinlock_t endio_lock; |
fd2ed4d2 | 103 | struct dm_stats_aux stats_aux; |
64f52b0e MS |
104 | /* last member of dm_target_io is 'struct bio' */ |
105 | struct dm_target_io tio; | |
1da177e4 LT |
106 | }; |
107 | ||
64f52b0e MS |
108 | void *dm_per_bio_data(struct bio *bio, size_t data_size) |
109 | { | |
110 | struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); | |
111 | if (!tio->inside_dm_io) | |
112 | return (char *)bio - offsetof(struct dm_target_io, clone) - data_size; | |
113 | return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size; | |
114 | } | |
115 | EXPORT_SYMBOL_GPL(dm_per_bio_data); | |
116 | ||
117 | struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) | |
118 | { | |
119 | struct dm_io *io = (struct dm_io *)((char *)data + data_size); | |
120 | if (io->magic == DM_IO_MAGIC) | |
121 | return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone)); | |
122 | BUG_ON(io->magic != DM_TIO_MAGIC); | |
123 | return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone)); | |
124 | } | |
125 | EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data); | |
126 | ||
127 | unsigned dm_bio_get_target_bio_nr(const struct bio *bio) | |
128 | { | |
129 | return container_of(bio, struct dm_target_io, clone)->target_bio_nr; | |
130 | } | |
131 | EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr); | |
132 | ||
ba61fdd1 JM |
133 | #define MINOR_ALLOCED ((void *)-1) |
134 | ||
1da177e4 LT |
135 | /* |
136 | * Bits for the md->flags field. | |
137 | */ | |
1eb787ec | 138 | #define DMF_BLOCK_IO_FOR_SUSPEND 0 |
1da177e4 | 139 | #define DMF_SUSPENDED 1 |
aa8d7c2f | 140 | #define DMF_FROZEN 2 |
fba9f90e | 141 | #define DMF_FREEING 3 |
5c6bd75d | 142 | #define DMF_DELETING 4 |
2e93ccc1 | 143 | #define DMF_NOFLUSH_SUSPENDING 5 |
8ae12666 KO |
144 | #define DMF_DEFERRED_REMOVE 6 |
145 | #define DMF_SUSPENDED_INTERNALLY 7 | |
5df96f2b | 146 | #define DMF_POST_SUSPENDING 8 |
1da177e4 | 147 | |
115485e8 | 148 | #define DM_NUMA_NODE NUMA_NO_NODE |
115485e8 | 149 | static int dm_numa_node = DM_NUMA_NODE; |
faad87df | 150 | |
bffdde6f MP |
151 | #define DEFAULT_SWAP_BIOS (8 * 1048576 / PAGE_SIZE) |
152 | static int swap_bios = DEFAULT_SWAP_BIOS; | |
153 | static int get_swap_bios(void) | |
154 | { | |
155 | int latch = READ_ONCE(swap_bios); | |
156 | if (unlikely(latch <= 0)) | |
157 | latch = DEFAULT_SWAP_BIOS; | |
158 | return latch; | |
159 | } | |
160 | ||
e6ee8c0b KU |
161 | /* |
162 | * For mempools pre-allocation at the table loading time. | |
163 | */ | |
164 | struct dm_md_mempools { | |
6f1c819c KO |
165 | struct bio_set bs; |
166 | struct bio_set io_bs; | |
e6ee8c0b KU |
167 | }; |
168 | ||
86f1152b BM |
169 | struct table_device { |
170 | struct list_head list; | |
b0b4d7c6 | 171 | refcount_t count; |
86f1152b BM |
172 | struct dm_dev dm_dev; |
173 | }; | |
174 | ||
e8603136 MS |
175 | /* |
176 | * Bio-based DM's mempools' reserved IOs set by the user. | |
177 | */ | |
4cc96131 | 178 | #define RESERVED_BIO_BASED_IOS 16 |
e8603136 MS |
179 | static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; |
180 | ||
115485e8 MS |
181 | static int __dm_get_module_param_int(int *module_param, int min, int max) |
182 | { | |
6aa7de05 | 183 | int param = READ_ONCE(*module_param); |
115485e8 MS |
184 | int modified_param = 0; |
185 | bool modified = true; | |
186 | ||
187 | if (param < min) | |
188 | modified_param = min; | |
189 | else if (param > max) | |
190 | modified_param = max; | |
191 | else | |
192 | modified = false; | |
193 | ||
194 | if (modified) { | |
195 | (void)cmpxchg(module_param, param, modified_param); | |
196 | param = modified_param; | |
197 | } | |
198 | ||
199 | return param; | |
200 | } | |
201 | ||
4cc96131 MS |
202 | unsigned __dm_get_module_param(unsigned *module_param, |
203 | unsigned def, unsigned max) | |
f4790826 | 204 | { |
6aa7de05 | 205 | unsigned param = READ_ONCE(*module_param); |
09c2d531 | 206 | unsigned modified_param = 0; |
f4790826 | 207 | |
09c2d531 MS |
208 | if (!param) |
209 | modified_param = def; | |
210 | else if (param > max) | |
211 | modified_param = max; | |
f4790826 | 212 | |
09c2d531 MS |
213 | if (modified_param) { |
214 | (void)cmpxchg(module_param, param, modified_param); | |
215 | param = modified_param; | |
f4790826 MS |
216 | } |
217 | ||
09c2d531 | 218 | return param; |
f4790826 MS |
219 | } |
220 | ||
e8603136 MS |
221 | unsigned dm_get_reserved_bio_based_ios(void) |
222 | { | |
09c2d531 | 223 | return __dm_get_module_param(&reserved_bio_based_ios, |
4cc96131 | 224 | RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS); |
e8603136 MS |
225 | } |
226 | EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); | |
227 | ||
115485e8 MS |
228 | static unsigned dm_get_numa_node(void) |
229 | { | |
230 | return __dm_get_module_param_int(&dm_numa_node, | |
231 | DM_NUMA_NODE, num_online_nodes() - 1); | |
232 | } | |
233 | ||
1da177e4 LT |
234 | static int __init local_init(void) |
235 | { | |
e689fbab | 236 | int r; |
1ae49ea2 | 237 | |
51e5b2bd | 238 | r = dm_uevent_init(); |
51157b4a | 239 | if (r) |
e689fbab | 240 | return r; |
51e5b2bd | 241 | |
acfe0ad7 MP |
242 | deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); |
243 | if (!deferred_remove_workqueue) { | |
244 | r = -ENOMEM; | |
245 | goto out_uevent_exit; | |
246 | } | |
247 | ||
1da177e4 LT |
248 | _major = major; |
249 | r = register_blkdev(_major, _name); | |
51157b4a | 250 | if (r < 0) |
acfe0ad7 | 251 | goto out_free_workqueue; |
1da177e4 LT |
252 | |
253 | if (!_major) | |
254 | _major = r; | |
255 | ||
256 | return 0; | |
51157b4a | 257 | |
acfe0ad7 MP |
258 | out_free_workqueue: |
259 | destroy_workqueue(deferred_remove_workqueue); | |
51157b4a KU |
260 | out_uevent_exit: |
261 | dm_uevent_exit(); | |
51157b4a KU |
262 | |
263 | return r; | |
1da177e4 LT |
264 | } |
265 | ||
266 | static void local_exit(void) | |
267 | { | |
2c140a24 | 268 | flush_scheduled_work(); |
acfe0ad7 | 269 | destroy_workqueue(deferred_remove_workqueue); |
2c140a24 | 270 | |
00d59405 | 271 | unregister_blkdev(_major, _name); |
51e5b2bd | 272 | dm_uevent_exit(); |
1da177e4 LT |
273 | |
274 | _major = 0; | |
275 | ||
276 | DMINFO("cleaned up"); | |
277 | } | |
278 | ||
b9249e55 | 279 | static int (*_inits[])(void) __initdata = { |
1da177e4 LT |
280 | local_init, |
281 | dm_target_init, | |
282 | dm_linear_init, | |
283 | dm_stripe_init, | |
952b3557 | 284 | dm_io_init, |
945fa4d2 | 285 | dm_kcopyd_init, |
1da177e4 | 286 | dm_interface_init, |
fd2ed4d2 | 287 | dm_statistics_init, |
1da177e4 LT |
288 | }; |
289 | ||
b9249e55 | 290 | static void (*_exits[])(void) = { |
1da177e4 LT |
291 | local_exit, |
292 | dm_target_exit, | |
293 | dm_linear_exit, | |
294 | dm_stripe_exit, | |
952b3557 | 295 | dm_io_exit, |
945fa4d2 | 296 | dm_kcopyd_exit, |
1da177e4 | 297 | dm_interface_exit, |
fd2ed4d2 | 298 | dm_statistics_exit, |
1da177e4 LT |
299 | }; |
300 | ||
301 | static int __init dm_init(void) | |
302 | { | |
303 | const int count = ARRAY_SIZE(_inits); | |
304 | ||
305 | int r, i; | |
306 | ||
307 | for (i = 0; i < count; i++) { | |
308 | r = _inits[i](); | |
309 | if (r) | |
310 | goto bad; | |
311 | } | |
312 | ||
313 | return 0; | |
314 | ||
315 | bad: | |
316 | while (i--) | |
317 | _exits[i](); | |
318 | ||
319 | return r; | |
320 | } | |
321 | ||
322 | static void __exit dm_exit(void) | |
323 | { | |
324 | int i = ARRAY_SIZE(_exits); | |
325 | ||
326 | while (i--) | |
327 | _exits[i](); | |
d15b774c AK |
328 | |
329 | /* | |
330 | * Should be empty by this point. | |
331 | */ | |
d15b774c | 332 | idr_destroy(&_minor_idr); |
1da177e4 LT |
333 | } |
334 | ||
335 | /* | |
336 | * Block device functions | |
337 | */ | |
432a212c MA |
338 | int dm_deleting_md(struct mapped_device *md) |
339 | { | |
340 | return test_bit(DMF_DELETING, &md->flags); | |
341 | } | |
342 | ||
fe5f9f2c | 343 | static int dm_blk_open(struct block_device *bdev, fmode_t mode) |
1da177e4 LT |
344 | { |
345 | struct mapped_device *md; | |
346 | ||
fba9f90e JM |
347 | spin_lock(&_minor_lock); |
348 | ||
fe5f9f2c | 349 | md = bdev->bd_disk->private_data; |
fba9f90e JM |
350 | if (!md) |
351 | goto out; | |
352 | ||
5c6bd75d | 353 | if (test_bit(DMF_FREEING, &md->flags) || |
432a212c | 354 | dm_deleting_md(md)) { |
fba9f90e JM |
355 | md = NULL; |
356 | goto out; | |
357 | } | |
358 | ||
1da177e4 | 359 | dm_get(md); |
5c6bd75d | 360 | atomic_inc(&md->open_count); |
fba9f90e JM |
361 | out: |
362 | spin_unlock(&_minor_lock); | |
363 | ||
364 | return md ? 0 : -ENXIO; | |
1da177e4 LT |
365 | } |
366 | ||
db2a144b | 367 | static void dm_blk_close(struct gendisk *disk, fmode_t mode) |
1da177e4 | 368 | { |
63a4f065 | 369 | struct mapped_device *md; |
6e9624b8 | 370 | |
4a1aeb98 MB |
371 | spin_lock(&_minor_lock); |
372 | ||
63a4f065 MS |
373 | md = disk->private_data; |
374 | if (WARN_ON(!md)) | |
375 | goto out; | |
376 | ||
2c140a24 MP |
377 | if (atomic_dec_and_test(&md->open_count) && |
378 | (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) | |
acfe0ad7 | 379 | queue_work(deferred_remove_workqueue, &deferred_remove_work); |
2c140a24 | 380 | |
1da177e4 | 381 | dm_put(md); |
63a4f065 | 382 | out: |
4a1aeb98 | 383 | spin_unlock(&_minor_lock); |
1da177e4 LT |
384 | } |
385 | ||
5c6bd75d AK |
386 | int dm_open_count(struct mapped_device *md) |
387 | { | |
388 | return atomic_read(&md->open_count); | |
389 | } | |
390 | ||
391 | /* | |
392 | * Guarantees nothing is using the device before it's deleted. | |
393 | */ | |
2c140a24 | 394 | int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) |
5c6bd75d AK |
395 | { |
396 | int r = 0; | |
397 | ||
398 | spin_lock(&_minor_lock); | |
399 | ||
2c140a24 | 400 | if (dm_open_count(md)) { |
5c6bd75d | 401 | r = -EBUSY; |
2c140a24 MP |
402 | if (mark_deferred) |
403 | set_bit(DMF_DEFERRED_REMOVE, &md->flags); | |
404 | } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) | |
405 | r = -EEXIST; | |
5c6bd75d AK |
406 | else |
407 | set_bit(DMF_DELETING, &md->flags); | |
408 | ||
409 | spin_unlock(&_minor_lock); | |
410 | ||
411 | return r; | |
412 | } | |
413 | ||
2c140a24 MP |
414 | int dm_cancel_deferred_remove(struct mapped_device *md) |
415 | { | |
416 | int r = 0; | |
417 | ||
418 | spin_lock(&_minor_lock); | |
419 | ||
420 | if (test_bit(DMF_DELETING, &md->flags)) | |
421 | r = -EBUSY; | |
422 | else | |
423 | clear_bit(DMF_DEFERRED_REMOVE, &md->flags); | |
424 | ||
425 | spin_unlock(&_minor_lock); | |
426 | ||
427 | return r; | |
428 | } | |
429 | ||
430 | static void do_deferred_remove(struct work_struct *w) | |
431 | { | |
432 | dm_deferred_remove(); | |
433 | } | |
434 | ||
3ac51e74 DW |
435 | static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
436 | { | |
437 | struct mapped_device *md = bdev->bd_disk->private_data; | |
438 | ||
439 | return dm_get_geometry(md, geo); | |
440 | } | |
441 | ||
d4100351 CH |
442 | #ifdef CONFIG_BLK_DEV_ZONED |
443 | int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx, void *data) | |
444 | { | |
445 | struct dm_report_zones_args *args = data; | |
446 | sector_t sector_diff = args->tgt->begin - args->start; | |
447 | ||
448 | /* | |
449 | * Ignore zones beyond the target range. | |
450 | */ | |
451 | if (zone->start >= args->start + args->tgt->len) | |
452 | return 0; | |
453 | ||
454 | /* | |
455 | * Remap the start sector and write pointer position of the zone | |
456 | * to match its position in the target range. | |
457 | */ | |
458 | zone->start += sector_diff; | |
459 | if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) { | |
460 | if (zone->cond == BLK_ZONE_COND_FULL) | |
461 | zone->wp = zone->start + zone->len; | |
462 | else if (zone->cond == BLK_ZONE_COND_EMPTY) | |
463 | zone->wp = zone->start; | |
464 | else | |
465 | zone->wp += sector_diff; | |
466 | } | |
467 | ||
468 | args->next_sector = zone->start + zone->len; | |
469 | return args->orig_cb(zone, args->zone_idx++, args->orig_data); | |
470 | } | |
471 | EXPORT_SYMBOL_GPL(dm_report_zones_cb); | |
472 | ||
e76239a3 | 473 | static int dm_blk_report_zones(struct gendisk *disk, sector_t sector, |
d4100351 | 474 | unsigned int nr_zones, report_zones_cb cb, void *data) |
e76239a3 | 475 | { |
e76239a3 | 476 | struct mapped_device *md = disk->private_data; |
e76239a3 CH |
477 | struct dm_table *map; |
478 | int srcu_idx, ret; | |
d4100351 CH |
479 | struct dm_report_zones_args args = { |
480 | .next_sector = sector, | |
481 | .orig_data = data, | |
482 | .orig_cb = cb, | |
483 | }; | |
e76239a3 CH |
484 | |
485 | if (dm_suspended_md(md)) | |
486 | return -EAGAIN; | |
487 | ||
488 | map = dm_get_live_table(md, &srcu_idx); | |
89478335 SS |
489 | if (!map) { |
490 | ret = -EIO; | |
491 | goto out; | |
492 | } | |
e76239a3 | 493 | |
d4100351 CH |
494 | do { |
495 | struct dm_target *tgt; | |
e76239a3 | 496 | |
d4100351 CH |
497 | tgt = dm_table_find_target(map, args.next_sector); |
498 | if (WARN_ON_ONCE(!tgt->type->report_zones)) { | |
499 | ret = -EIO; | |
500 | goto out; | |
501 | } | |
e76239a3 | 502 | |
d4100351 | 503 | args.tgt = tgt; |
a9cb9f41 JT |
504 | ret = tgt->type->report_zones(tgt, &args, |
505 | nr_zones - args.zone_idx); | |
d4100351 CH |
506 | if (ret < 0) |
507 | goto out; | |
508 | } while (args.zone_idx < nr_zones && | |
509 | args.next_sector < get_capacity(disk)); | |
e76239a3 | 510 | |
d4100351 | 511 | ret = args.zone_idx; |
e76239a3 CH |
512 | out: |
513 | dm_put_live_table(md, srcu_idx); | |
514 | return ret; | |
e76239a3 | 515 | } |
d4100351 CH |
516 | #else |
517 | #define dm_blk_report_zones NULL | |
518 | #endif /* CONFIG_BLK_DEV_ZONED */ | |
e76239a3 | 519 | |
971888c4 | 520 | static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx, |
5bd5e8d8 | 521 | struct block_device **bdev) |
aa129a22 | 522 | { |
66482026 | 523 | struct dm_target *tgt; |
6c182cd8 | 524 | struct dm_table *map; |
971888c4 | 525 | int r; |
aa129a22 | 526 | |
6c182cd8 | 527 | retry: |
e56f81e0 | 528 | r = -ENOTTY; |
971888c4 | 529 | map = dm_get_live_table(md, srcu_idx); |
aa129a22 | 530 | if (!map || !dm_table_get_size(map)) |
971888c4 | 531 | return r; |
aa129a22 MB |
532 | |
533 | /* We only support devices that have a single target */ | |
534 | if (dm_table_get_num_targets(map) != 1) | |
971888c4 | 535 | return r; |
aa129a22 | 536 | |
66482026 MS |
537 | tgt = dm_table_get_target(map, 0); |
538 | if (!tgt->type->prepare_ioctl) | |
971888c4 | 539 | return r; |
519049af | 540 | |
971888c4 MS |
541 | if (dm_suspended_md(md)) |
542 | return -EAGAIN; | |
aa129a22 | 543 | |
5bd5e8d8 | 544 | r = tgt->type->prepare_ioctl(tgt, bdev); |
5bbbfdf6 | 545 | if (r == -ENOTCONN && !fatal_signal_pending(current)) { |
971888c4 | 546 | dm_put_live_table(md, *srcu_idx); |
6c182cd8 HR |
547 | msleep(10); |
548 | goto retry; | |
549 | } | |
971888c4 | 550 | |
e56f81e0 CH |
551 | return r; |
552 | } | |
553 | ||
971888c4 | 554 | static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx) |
971888c4 MS |
555 | { |
556 | dm_put_live_table(md, srcu_idx); | |
557 | } | |
558 | ||
e56f81e0 CH |
559 | static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, |
560 | unsigned int cmd, unsigned long arg) | |
561 | { | |
562 | struct mapped_device *md = bdev->bd_disk->private_data; | |
971888c4 | 563 | int r, srcu_idx; |
e56f81e0 | 564 | |
5bd5e8d8 | 565 | r = dm_prepare_ioctl(md, &srcu_idx, &bdev); |
e56f81e0 | 566 | if (r < 0) |
971888c4 | 567 | goto out; |
6c182cd8 | 568 | |
e56f81e0 CH |
569 | if (r > 0) { |
570 | /* | |
e980f623 CH |
571 | * Target determined this ioctl is being issued against a |
572 | * subset of the parent bdev; require extra privileges. | |
e56f81e0 | 573 | */ |
e980f623 | 574 | if (!capable(CAP_SYS_RAWIO)) { |
0378c625 | 575 | DMDEBUG_LIMIT( |
e980f623 CH |
576 | "%s: sending ioctl %x to DM device without required privilege.", |
577 | current->comm, cmd); | |
578 | r = -ENOIOCTLCMD; | |
e56f81e0 | 579 | goto out; |
e980f623 | 580 | } |
e56f81e0 | 581 | } |
6c182cd8 | 582 | |
a7cb3d2f CH |
583 | if (!bdev->bd_disk->fops->ioctl) |
584 | r = -ENOTTY; | |
585 | else | |
586 | r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); | |
e56f81e0 | 587 | out: |
971888c4 | 588 | dm_unprepare_ioctl(md, srcu_idx); |
aa129a22 MB |
589 | return r; |
590 | } | |
591 | ||
7465d7ac MS |
592 | u64 dm_start_time_ns_from_clone(struct bio *bio) |
593 | { | |
594 | struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); | |
595 | struct dm_io *io = tio->io; | |
596 | ||
597 | return jiffies_to_nsecs(io->start_time); | |
598 | } | |
599 | EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone); | |
600 | ||
601 | static void start_io_acct(struct dm_io *io) | |
602 | { | |
603 | struct mapped_device *md = io->md; | |
604 | struct bio *bio = io->orig_bio; | |
605 | ||
606 | io->start_time = bio_start_io_acct(bio); | |
607 | if (unlikely(dm_stats_used(&md->stats))) | |
608 | dm_stats_account_io(&md->stats, bio_data_dir(bio), | |
609 | bio->bi_iter.bi_sector, bio_sectors(bio), | |
610 | false, 0, &io->stats_aux); | |
611 | } | |
612 | ||
613 | static void end_io_acct(struct dm_io *io) | |
614 | { | |
615 | struct mapped_device *md = io->md; | |
616 | struct bio *bio = io->orig_bio; | |
617 | unsigned long duration = jiffies - io->start_time; | |
618 | ||
619 | bio_end_io_acct(bio, io->start_time); | |
620 | ||
621 | if (unlikely(dm_stats_used(&md->stats))) | |
622 | dm_stats_account_io(&md->stats, bio_data_dir(bio), | |
623 | bio->bi_iter.bi_sector, bio_sectors(bio), | |
624 | true, duration, &io->stats_aux); | |
625 | ||
626 | /* nudge anyone waiting on suspend queue */ | |
627 | if (unlikely(wq_has_sleeper(&md->wait))) | |
628 | wake_up(&md->wait); | |
629 | } | |
978e51ba MS |
630 | |
631 | static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) | |
1da177e4 | 632 | { |
64f52b0e MS |
633 | struct dm_io *io; |
634 | struct dm_target_io *tio; | |
635 | struct bio *clone; | |
636 | ||
6f1c819c | 637 | clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs); |
64f52b0e MS |
638 | if (!clone) |
639 | return NULL; | |
640 | ||
641 | tio = container_of(clone, struct dm_target_io, clone); | |
642 | tio->inside_dm_io = true; | |
643 | tio->io = NULL; | |
644 | ||
645 | io = container_of(tio, struct dm_io, tio); | |
646 | io->magic = DM_IO_MAGIC; | |
978e51ba MS |
647 | io->status = 0; |
648 | atomic_set(&io->io_count, 1); | |
649 | io->orig_bio = bio; | |
650 | io->md = md; | |
651 | spin_lock_init(&io->endio_lock); | |
652 | ||
653 | start_io_acct(io); | |
64f52b0e MS |
654 | |
655 | return io; | |
1da177e4 LT |
656 | } |
657 | ||
028867ac | 658 | static void free_io(struct mapped_device *md, struct dm_io *io) |
1da177e4 | 659 | { |
64f52b0e MS |
660 | bio_put(&io->tio.clone); |
661 | } | |
662 | ||
663 | static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti, | |
664 | unsigned target_bio_nr, gfp_t gfp_mask) | |
665 | { | |
666 | struct dm_target_io *tio; | |
667 | ||
668 | if (!ci->io->tio.io) { | |
669 | /* the dm_target_io embedded in ci->io is available */ | |
670 | tio = &ci->io->tio; | |
671 | } else { | |
6f1c819c | 672 | struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs); |
64f52b0e MS |
673 | if (!clone) |
674 | return NULL; | |
675 | ||
676 | tio = container_of(clone, struct dm_target_io, clone); | |
677 | tio->inside_dm_io = false; | |
678 | } | |
679 | ||
680 | tio->magic = DM_TIO_MAGIC; | |
681 | tio->io = ci->io; | |
682 | tio->ti = ti; | |
683 | tio->target_bio_nr = target_bio_nr; | |
684 | ||
685 | return tio; | |
1da177e4 LT |
686 | } |
687 | ||
cfae7529 | 688 | static void free_tio(struct dm_target_io *tio) |
1da177e4 | 689 | { |
64f52b0e MS |
690 | if (tio->inside_dm_io) |
691 | return; | |
dba14160 | 692 | bio_put(&tio->clone); |
1da177e4 LT |
693 | } |
694 | ||
695 | /* | |
696 | * Add the bio to the list of deferred io. | |
697 | */ | |
92c63902 | 698 | static void queue_io(struct mapped_device *md, struct bio *bio) |
1da177e4 | 699 | { |
05447420 | 700 | unsigned long flags; |
1da177e4 | 701 | |
05447420 | 702 | spin_lock_irqsave(&md->deferred_lock, flags); |
1da177e4 | 703 | bio_list_add(&md->deferred, bio); |
05447420 | 704 | spin_unlock_irqrestore(&md->deferred_lock, flags); |
6a8736d1 | 705 | queue_work(md->wq, &md->work); |
1da177e4 LT |
706 | } |
707 | ||
708 | /* | |
709 | * Everyone (including functions in this file), should use this | |
710 | * function to access the md->map field, and make sure they call | |
83d5e5b0 | 711 | * dm_put_live_table() when finished. |
1da177e4 | 712 | */ |
83d5e5b0 | 713 | struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) |
1da177e4 | 714 | { |
83d5e5b0 MP |
715 | *srcu_idx = srcu_read_lock(&md->io_barrier); |
716 | ||
717 | return srcu_dereference(md->map, &md->io_barrier); | |
718 | } | |
1da177e4 | 719 | |
83d5e5b0 MP |
720 | void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) |
721 | { | |
722 | srcu_read_unlock(&md->io_barrier, srcu_idx); | |
723 | } | |
724 | ||
725 | void dm_sync_table(struct mapped_device *md) | |
726 | { | |
727 | synchronize_srcu(&md->io_barrier); | |
728 | synchronize_rcu_expedited(); | |
729 | } | |
730 | ||
731 | /* | |
732 | * A fast alternative to dm_get_live_table/dm_put_live_table. | |
733 | * The caller must not block between these two functions. | |
734 | */ | |
735 | static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) | |
736 | { | |
737 | rcu_read_lock(); | |
738 | return rcu_dereference(md->map); | |
739 | } | |
1da177e4 | 740 | |
83d5e5b0 MP |
741 | static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) |
742 | { | |
743 | rcu_read_unlock(); | |
1da177e4 LT |
744 | } |
745 | ||
971888c4 MS |
746 | static char *_dm_claim_ptr = "I belong to device-mapper"; |
747 | ||
86f1152b BM |
748 | /* |
749 | * Open a table device so we can use it as a map destination. | |
750 | */ | |
751 | static int open_table_device(struct table_device *td, dev_t dev, | |
752 | struct mapped_device *md) | |
753 | { | |
86f1152b BM |
754 | struct block_device *bdev; |
755 | ||
756 | int r; | |
757 | ||
758 | BUG_ON(td->dm_dev.bdev); | |
759 | ||
519049af | 760 | bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); |
86f1152b BM |
761 | if (IS_ERR(bdev)) |
762 | return PTR_ERR(bdev); | |
763 | ||
764 | r = bd_link_disk_holder(bdev, dm_disk(md)); | |
765 | if (r) { | |
766 | blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); | |
767 | return r; | |
768 | } | |
769 | ||
770 | td->dm_dev.bdev = bdev; | |
817bf402 | 771 | td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); |
86f1152b BM |
772 | return 0; |
773 | } | |
774 | ||
775 | /* | |
776 | * Close a table device that we've been using. | |
777 | */ | |
778 | static void close_table_device(struct table_device *td, struct mapped_device *md) | |
779 | { | |
780 | if (!td->dm_dev.bdev) | |
781 | return; | |
782 | ||
783 | bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); | |
784 | blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); | |
817bf402 | 785 | put_dax(td->dm_dev.dax_dev); |
86f1152b | 786 | td->dm_dev.bdev = NULL; |
817bf402 | 787 | td->dm_dev.dax_dev = NULL; |
86f1152b BM |
788 | } |
789 | ||
790 | static struct table_device *find_table_device(struct list_head *l, dev_t dev, | |
8454fca4 SS |
791 | fmode_t mode) |
792 | { | |
86f1152b BM |
793 | struct table_device *td; |
794 | ||
795 | list_for_each_entry(td, l, list) | |
796 | if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) | |
797 | return td; | |
798 | ||
799 | return NULL; | |
800 | } | |
801 | ||
802 | int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, | |
8454fca4 SS |
803 | struct dm_dev **result) |
804 | { | |
86f1152b BM |
805 | int r; |
806 | struct table_device *td; | |
807 | ||
808 | mutex_lock(&md->table_devices_lock); | |
809 | td = find_table_device(&md->table_devices, dev, mode); | |
810 | if (!td) { | |
115485e8 | 811 | td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); |
86f1152b BM |
812 | if (!td) { |
813 | mutex_unlock(&md->table_devices_lock); | |
814 | return -ENOMEM; | |
815 | } | |
816 | ||
817 | td->dm_dev.mode = mode; | |
818 | td->dm_dev.bdev = NULL; | |
819 | ||
820 | if ((r = open_table_device(td, dev, md))) { | |
821 | mutex_unlock(&md->table_devices_lock); | |
822 | kfree(td); | |
823 | return r; | |
824 | } | |
825 | ||
826 | format_dev_t(td->dm_dev.name, dev); | |
827 | ||
b0b4d7c6 | 828 | refcount_set(&td->count, 1); |
86f1152b | 829 | list_add(&td->list, &md->table_devices); |
b0b4d7c6 ER |
830 | } else { |
831 | refcount_inc(&td->count); | |
86f1152b | 832 | } |
86f1152b BM |
833 | mutex_unlock(&md->table_devices_lock); |
834 | ||
835 | *result = &td->dm_dev; | |
836 | return 0; | |
837 | } | |
838 | EXPORT_SYMBOL_GPL(dm_get_table_device); | |
839 | ||
840 | void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) | |
841 | { | |
842 | struct table_device *td = container_of(d, struct table_device, dm_dev); | |
843 | ||
844 | mutex_lock(&md->table_devices_lock); | |
b0b4d7c6 | 845 | if (refcount_dec_and_test(&td->count)) { |
86f1152b BM |
846 | close_table_device(td, md); |
847 | list_del(&td->list); | |
848 | kfree(td); | |
849 | } | |
850 | mutex_unlock(&md->table_devices_lock); | |
851 | } | |
852 | EXPORT_SYMBOL(dm_put_table_device); | |
853 | ||
854 | static void free_table_devices(struct list_head *devices) | |
855 | { | |
856 | struct list_head *tmp, *next; | |
857 | ||
858 | list_for_each_safe(tmp, next, devices) { | |
859 | struct table_device *td = list_entry(tmp, struct table_device, list); | |
860 | ||
861 | DMWARN("dm_destroy: %s still exists with %d references", | |
b0b4d7c6 | 862 | td->dm_dev.name, refcount_read(&td->count)); |
86f1152b BM |
863 | kfree(td); |
864 | } | |
865 | } | |
866 | ||
3ac51e74 DW |
867 | /* |
868 | * Get the geometry associated with a dm device | |
869 | */ | |
870 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) | |
871 | { | |
872 | *geo = md->geometry; | |
873 | ||
874 | return 0; | |
875 | } | |
876 | ||
877 | /* | |
878 | * Set the geometry of a device. | |
879 | */ | |
880 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) | |
881 | { | |
882 | sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; | |
883 | ||
884 | if (geo->start > sz) { | |
885 | DMWARN("Start sector is beyond the geometry limits."); | |
886 | return -EINVAL; | |
887 | } | |
888 | ||
889 | md->geometry = *geo; | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
2e93ccc1 KU |
894 | static int __noflush_suspending(struct mapped_device *md) |
895 | { | |
896 | return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); | |
897 | } | |
898 | ||
1da177e4 LT |
899 | /* |
900 | * Decrements the number of outstanding ios that a bio has been | |
901 | * cloned into, completing the original io if necc. | |
902 | */ | |
4e4cbee9 | 903 | static void dec_pending(struct dm_io *io, blk_status_t error) |
1da177e4 | 904 | { |
2e93ccc1 | 905 | unsigned long flags; |
4e4cbee9 | 906 | blk_status_t io_error; |
b35f8caa MB |
907 | struct bio *bio; |
908 | struct mapped_device *md = io->md; | |
2e93ccc1 KU |
909 | |
910 | /* Push-back supersedes any I/O errors */ | |
f88fb981 KU |
911 | if (unlikely(error)) { |
912 | spin_lock_irqsave(&io->endio_lock, flags); | |
745dc570 | 913 | if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md))) |
4e4cbee9 | 914 | io->status = error; |
f88fb981 KU |
915 | spin_unlock_irqrestore(&io->endio_lock, flags); |
916 | } | |
1da177e4 LT |
917 | |
918 | if (atomic_dec_and_test(&io->io_count)) { | |
4e4cbee9 | 919 | if (io->status == BLK_STS_DM_REQUEUE) { |
2e93ccc1 KU |
920 | /* |
921 | * Target requested pushing back the I/O. | |
2e93ccc1 | 922 | */ |
022c2611 | 923 | spin_lock_irqsave(&md->deferred_lock, flags); |
6a8736d1 | 924 | if (__noflush_suspending(md)) |
745dc570 MS |
925 | /* NOTE early return due to BLK_STS_DM_REQUEUE below */ |
926 | bio_list_add_head(&md->deferred, io->orig_bio); | |
6a8736d1 | 927 | else |
2e93ccc1 | 928 | /* noflush suspend was interrupted. */ |
4e4cbee9 | 929 | io->status = BLK_STS_IOERR; |
022c2611 | 930 | spin_unlock_irqrestore(&md->deferred_lock, flags); |
2e93ccc1 KU |
931 | } |
932 | ||
4e4cbee9 | 933 | io_error = io->status; |
745dc570 | 934 | bio = io->orig_bio; |
6a8736d1 TH |
935 | end_io_acct(io); |
936 | free_io(md, io); | |
937 | ||
4e4cbee9 | 938 | if (io_error == BLK_STS_DM_REQUEUE) |
6a8736d1 | 939 | return; |
2e93ccc1 | 940 | |
1eff9d32 | 941 | if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) { |
af7e466a | 942 | /* |
6a8736d1 | 943 | * Preflush done for flush with data, reissue |
28a8f0d3 | 944 | * without REQ_PREFLUSH. |
af7e466a | 945 | */ |
1eff9d32 | 946 | bio->bi_opf &= ~REQ_PREFLUSH; |
6a8736d1 | 947 | queue_io(md, bio); |
af7e466a | 948 | } else { |
b372d360 | 949 | /* done with normal IO or empty flush */ |
8dd601fa N |
950 | if (io_error) |
951 | bio->bi_status = io_error; | |
4246a0b6 | 952 | bio_endio(bio); |
b35f8caa | 953 | } |
1da177e4 LT |
954 | } |
955 | } | |
956 | ||
bcb44433 MS |
957 | void disable_discard(struct mapped_device *md) |
958 | { | |
959 | struct queue_limits *limits = dm_get_queue_limits(md); | |
960 | ||
961 | /* device doesn't really support DISCARD, disable it */ | |
962 | limits->max_discard_sectors = 0; | |
963 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue); | |
964 | } | |
965 | ||
4cc96131 | 966 | void disable_write_same(struct mapped_device *md) |
7eee4ae2 MS |
967 | { |
968 | struct queue_limits *limits = dm_get_queue_limits(md); | |
969 | ||
970 | /* device doesn't really support WRITE SAME, disable it */ | |
971 | limits->max_write_same_sectors = 0; | |
972 | } | |
973 | ||
ac62d620 CH |
974 | void disable_write_zeroes(struct mapped_device *md) |
975 | { | |
976 | struct queue_limits *limits = dm_get_queue_limits(md); | |
977 | ||
978 | /* device doesn't really support WRITE ZEROES, disable it */ | |
979 | limits->max_write_zeroes_sectors = 0; | |
980 | } | |
981 | ||
bffdde6f MP |
982 | static bool swap_bios_limit(struct dm_target *ti, struct bio *bio) |
983 | { | |
984 | return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios); | |
985 | } | |
986 | ||
4246a0b6 | 987 | static void clone_endio(struct bio *bio) |
1da177e4 | 988 | { |
4e4cbee9 | 989 | blk_status_t error = bio->bi_status; |
bfc6d41c | 990 | struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); |
b35f8caa | 991 | struct dm_io *io = tio->io; |
9faf400f | 992 | struct mapped_device *md = tio->io->md; |
1da177e4 | 993 | dm_endio_fn endio = tio->ti->type->end_io; |
415c79e1 | 994 | struct bio *orig_bio = io->orig_bio; |
1da177e4 | 995 | |
9c37de29 | 996 | if (unlikely(error == BLK_STS_TARGET)) { |
bcb44433 MS |
997 | if (bio_op(bio) == REQ_OP_DISCARD && |
998 | !bio->bi_disk->queue->limits.max_discard_sectors) | |
999 | disable_discard(md); | |
1000 | else if (bio_op(bio) == REQ_OP_WRITE_SAME && | |
1001 | !bio->bi_disk->queue->limits.max_write_same_sectors) | |
ac62d620 | 1002 | disable_write_same(md); |
bcb44433 MS |
1003 | else if (bio_op(bio) == REQ_OP_WRITE_ZEROES && |
1004 | !bio->bi_disk->queue->limits.max_write_zeroes_sectors) | |
ac62d620 CH |
1005 | disable_write_zeroes(md); |
1006 | } | |
7eee4ae2 | 1007 | |
415c79e1 JT |
1008 | /* |
1009 | * For zone-append bios get offset in zone of the written | |
1010 | * sector and add that to the original bio sector pos. | |
1011 | */ | |
1012 | if (bio_op(orig_bio) == REQ_OP_ZONE_APPEND) { | |
1013 | sector_t written_sector = bio->bi_iter.bi_sector; | |
1014 | struct request_queue *q = orig_bio->bi_disk->queue; | |
1015 | u64 mask = (u64)blk_queue_zone_sectors(q) - 1; | |
1016 | ||
1017 | orig_bio->bi_iter.bi_sector += written_sector & mask; | |
1018 | } | |
1019 | ||
1be56909 | 1020 | if (endio) { |
4e4cbee9 | 1021 | int r = endio(tio->ti, bio, &error); |
1be56909 CH |
1022 | switch (r) { |
1023 | case DM_ENDIO_REQUEUE: | |
4e4cbee9 | 1024 | error = BLK_STS_DM_REQUEUE; |
df561f66 | 1025 | fallthrough; |
1be56909 CH |
1026 | case DM_ENDIO_DONE: |
1027 | break; | |
1028 | case DM_ENDIO_INCOMPLETE: | |
1029 | /* The target will handle the io */ | |
1030 | return; | |
1031 | default: | |
1032 | DMWARN("unimplemented target endio return value: %d", r); | |
1033 | BUG(); | |
1034 | } | |
1035 | } | |
1036 | ||
bffdde6f MP |
1037 | if (unlikely(swap_bios_limit(tio->ti, bio))) { |
1038 | struct mapped_device *md = io->md; | |
1039 | up(&md->swap_bios_semaphore); | |
1040 | } | |
1041 | ||
cfae7529 | 1042 | free_tio(tio); |
b35f8caa | 1043 | dec_pending(io, error); |
1da177e4 LT |
1044 | } |
1045 | ||
56a67df7 MS |
1046 | /* |
1047 | * Return maximum size of I/O possible at the supplied sector up to the current | |
1048 | * target boundary. | |
1049 | */ | |
3720281d MS |
1050 | static inline sector_t max_io_len_target_boundary(struct dm_target *ti, |
1051 | sector_t target_offset) | |
56a67df7 | 1052 | { |
56a67df7 MS |
1053 | return ti->len - target_offset; |
1054 | } | |
1055 | ||
3720281d | 1056 | static sector_t max_io_len(struct dm_target *ti, sector_t sector) |
1da177e4 | 1057 | { |
3720281d MS |
1058 | sector_t target_offset = dm_target_offset(ti, sector); |
1059 | sector_t len = max_io_len_target_boundary(ti, target_offset); | |
5091cdec | 1060 | sector_t max_len; |
1da177e4 LT |
1061 | |
1062 | /* | |
3ee16db3 MS |
1063 | * Does the target need to split IO even further? |
1064 | * - varied (per target) IO splitting is a tenet of DM; this | |
1065 | * explains why stacked chunk_sectors based splitting via | |
1066 | * blk_max_size_offset() isn't possible here. So pass in | |
1067 | * ti->max_io_len to override stacked chunk_sectors. | |
1da177e4 | 1068 | */ |
3ee16db3 MS |
1069 | if (ti->max_io_len) { |
1070 | max_len = blk_max_size_offset(ti->table->md->queue, | |
1071 | target_offset, ti->max_io_len); | |
1072 | if (len > max_len) | |
1073 | len = max_len; | |
1074 | } | |
1da177e4 LT |
1075 | |
1076 | return len; | |
1077 | } | |
1078 | ||
542f9038 MS |
1079 | int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) |
1080 | { | |
1081 | if (len > UINT_MAX) { | |
1082 | DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", | |
1083 | (unsigned long long)len, UINT_MAX); | |
1084 | ti->error = "Maximum size of target IO is too large"; | |
1085 | return -EINVAL; | |
1086 | } | |
1087 | ||
75ae1936 | 1088 | ti->max_io_len = (uint32_t) len; |
542f9038 MS |
1089 | |
1090 | return 0; | |
1091 | } | |
1092 | EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); | |
1093 | ||
f26c5719 | 1094 | static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, |
3d97c829 MS |
1095 | sector_t sector, int *srcu_idx) |
1096 | __acquires(md->io_barrier) | |
545ed20e | 1097 | { |
545ed20e TK |
1098 | struct dm_table *map; |
1099 | struct dm_target *ti; | |
545ed20e | 1100 | |
f26c5719 | 1101 | map = dm_get_live_table(md, srcu_idx); |
545ed20e | 1102 | if (!map) |
f26c5719 | 1103 | return NULL; |
545ed20e TK |
1104 | |
1105 | ti = dm_table_find_target(map, sector); | |
123d87d5 | 1106 | if (!ti) |
f26c5719 | 1107 | return NULL; |
545ed20e | 1108 | |
f26c5719 DW |
1109 | return ti; |
1110 | } | |
545ed20e | 1111 | |
f26c5719 | 1112 | static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, |
3d97c829 | 1113 | long nr_pages, void **kaddr, pfn_t *pfn) |
f26c5719 DW |
1114 | { |
1115 | struct mapped_device *md = dax_get_private(dax_dev); | |
1116 | sector_t sector = pgoff * PAGE_SECTORS; | |
1117 | struct dm_target *ti; | |
1118 | long len, ret = -EIO; | |
1119 | int srcu_idx; | |
545ed20e | 1120 | |
f26c5719 | 1121 | ti = dm_dax_get_live_target(md, sector, &srcu_idx); |
545ed20e | 1122 | |
f26c5719 DW |
1123 | if (!ti) |
1124 | goto out; | |
1125 | if (!ti->type->direct_access) | |
1126 | goto out; | |
3720281d | 1127 | len = max_io_len(ti, sector) / PAGE_SECTORS; |
f26c5719 DW |
1128 | if (len < 1) |
1129 | goto out; | |
1130 | nr_pages = min(len, nr_pages); | |
dbc62659 | 1131 | ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); |
817bf402 | 1132 | |
f26c5719 | 1133 | out: |
545ed20e | 1134 | dm_put_live_table(md, srcu_idx); |
f26c5719 DW |
1135 | |
1136 | return ret; | |
545ed20e TK |
1137 | } |
1138 | ||
7bf7eac8 DW |
1139 | static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev, |
1140 | int blocksize, sector_t start, sector_t len) | |
1141 | { | |
1142 | struct mapped_device *md = dax_get_private(dax_dev); | |
1143 | struct dm_table *map; | |
02186d88 | 1144 | bool ret = false; |
7bf7eac8 | 1145 | int srcu_idx; |
7bf7eac8 DW |
1146 | |
1147 | map = dm_get_live_table(md, &srcu_idx); | |
1148 | if (!map) | |
02186d88 | 1149 | goto out; |
7bf7eac8 | 1150 | |
ae7be0c2 | 1151 | ret = dm_table_supports_dax(map, device_not_dax_capable, &blocksize); |
7bf7eac8 | 1152 | |
02186d88 | 1153 | out: |
7bf7eac8 DW |
1154 | dm_put_live_table(md, srcu_idx); |
1155 | ||
1156 | return ret; | |
1157 | } | |
1158 | ||
7e026c8c | 1159 | static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, |
3d97c829 | 1160 | void *addr, size_t bytes, struct iov_iter *i) |
7e026c8c DW |
1161 | { |
1162 | struct mapped_device *md = dax_get_private(dax_dev); | |
1163 | sector_t sector = pgoff * PAGE_SECTORS; | |
1164 | struct dm_target *ti; | |
1165 | long ret = 0; | |
1166 | int srcu_idx; | |
1167 | ||
1168 | ti = dm_dax_get_live_target(md, sector, &srcu_idx); | |
1169 | ||
1170 | if (!ti) | |
1171 | goto out; | |
1172 | if (!ti->type->dax_copy_from_iter) { | |
1173 | ret = copy_from_iter(addr, bytes, i); | |
1174 | goto out; | |
1175 | } | |
1176 | ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i); | |
1177 | out: | |
1178 | dm_put_live_table(md, srcu_idx); | |
1179 | ||
1180 | return ret; | |
1181 | } | |
1182 | ||
b3a9a0c3 DW |
1183 | static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, |
1184 | void *addr, size_t bytes, struct iov_iter *i) | |
1185 | { | |
1186 | struct mapped_device *md = dax_get_private(dax_dev); | |
1187 | sector_t sector = pgoff * PAGE_SECTORS; | |
1188 | struct dm_target *ti; | |
1189 | long ret = 0; | |
1190 | int srcu_idx; | |
1191 | ||
1192 | ti = dm_dax_get_live_target(md, sector, &srcu_idx); | |
1193 | ||
1194 | if (!ti) | |
1195 | goto out; | |
1196 | if (!ti->type->dax_copy_to_iter) { | |
1197 | ret = copy_to_iter(addr, bytes, i); | |
1198 | goto out; | |
1199 | } | |
1200 | ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i); | |
1201 | out: | |
1202 | dm_put_live_table(md, srcu_idx); | |
1203 | ||
1204 | return ret; | |
1205 | } | |
1206 | ||
cdf6cdcd VG |
1207 | static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, |
1208 | size_t nr_pages) | |
1209 | { | |
1210 | struct mapped_device *md = dax_get_private(dax_dev); | |
1211 | sector_t sector = pgoff * PAGE_SECTORS; | |
1212 | struct dm_target *ti; | |
1213 | int ret = -EIO; | |
1214 | int srcu_idx; | |
1215 | ||
1216 | ti = dm_dax_get_live_target(md, sector, &srcu_idx); | |
1217 | ||
1218 | if (!ti) | |
1219 | goto out; | |
1220 | if (WARN_ON(!ti->type->dax_zero_page_range)) { | |
1221 | /* | |
1222 | * ->zero_page_range() is mandatory dax operation. If we are | |
1223 | * here, something is wrong. | |
1224 | */ | |
cdf6cdcd VG |
1225 | goto out; |
1226 | } | |
1227 | ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); | |
cdf6cdcd VG |
1228 | out: |
1229 | dm_put_live_table(md, srcu_idx); | |
1230 | ||
1231 | return ret; | |
1232 | } | |
1233 | ||
1dd40c3e MP |
1234 | /* |
1235 | * A target may call dm_accept_partial_bio only from the map routine. It is | |
2e2d6f7e AJ |
1236 | * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_RESET, |
1237 | * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE and REQ_OP_ZONE_FINISH. | |
1dd40c3e MP |
1238 | * |
1239 | * dm_accept_partial_bio informs the dm that the target only wants to process | |
1240 | * additional n_sectors sectors of the bio and the rest of the data should be | |
1241 | * sent in a next bio. | |
1242 | * | |
1243 | * A diagram that explains the arithmetics: | |
1244 | * +--------------------+---------------+-------+ | |
1245 | * | 1 | 2 | 3 | | |
1246 | * +--------------------+---------------+-------+ | |
1247 | * | |
1248 | * <-------------- *tio->len_ptr ---------------> | |
1249 | * <------- bi_size -------> | |
1250 | * <-- n_sectors --> | |
1251 | * | |
1252 | * Region 1 was already iterated over with bio_advance or similar function. | |
1253 | * (it may be empty if the target doesn't use bio_advance) | |
1254 | * Region 2 is the remaining bio size that the target wants to process. | |
1255 | * (it may be empty if region 1 is non-empty, although there is no reason | |
1256 | * to make it empty) | |
1257 | * The target requires that region 3 is to be sent in the next bio. | |
1258 | * | |
1259 | * If the target wants to receive multiple copies of the bio (via num_*bios, etc), | |
1260 | * the partially processed part (the sum of regions 1+2) must be the same for all | |
1261 | * copies of the bio. | |
1262 | */ | |
1263 | void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) | |
1264 | { | |
1265 | struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); | |
1266 | unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; | |
1eff9d32 | 1267 | BUG_ON(bio->bi_opf & REQ_PREFLUSH); |
1dd40c3e MP |
1268 | BUG_ON(bi_size > *tio->len_ptr); |
1269 | BUG_ON(n_sectors > bi_size); | |
1270 | *tio->len_ptr -= bi_size - n_sectors; | |
1271 | bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; | |
1272 | } | |
1273 | EXPORT_SYMBOL_GPL(dm_accept_partial_bio); | |
1274 | ||
bffdde6f MP |
1275 | static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch) |
1276 | { | |
1277 | mutex_lock(&md->swap_bios_lock); | |
1278 | while (latch < md->swap_bios) { | |
1279 | cond_resched(); | |
1280 | down(&md->swap_bios_semaphore); | |
1281 | md->swap_bios--; | |
1282 | } | |
1283 | while (latch > md->swap_bios) { | |
1284 | cond_resched(); | |
1285 | up(&md->swap_bios_semaphore); | |
1286 | md->swap_bios++; | |
1287 | } | |
1288 | mutex_unlock(&md->swap_bios_lock); | |
1289 | } | |
1290 | ||
978e51ba | 1291 | static blk_qc_t __map_bio(struct dm_target_io *tio) |
1da177e4 LT |
1292 | { |
1293 | int r; | |
2056a782 | 1294 | sector_t sector; |
dba14160 | 1295 | struct bio *clone = &tio->clone; |
64f52b0e | 1296 | struct dm_io *io = tio->io; |
bd2a49b8 | 1297 | struct dm_target *ti = tio->ti; |
978e51ba | 1298 | blk_qc_t ret = BLK_QC_T_NONE; |
1da177e4 | 1299 | |
1da177e4 | 1300 | clone->bi_end_io = clone_endio; |
1da177e4 LT |
1301 | |
1302 | /* | |
1303 | * Map the clone. If r == 0 we don't need to do | |
1304 | * anything, the target has assumed ownership of | |
1305 | * this io. | |
1306 | */ | |
64f52b0e | 1307 | atomic_inc(&io->io_count); |
4f024f37 | 1308 | sector = clone->bi_iter.bi_sector; |
d67a5f4b | 1309 | |
bffdde6f MP |
1310 | if (unlikely(swap_bios_limit(ti, clone))) { |
1311 | struct mapped_device *md = io->md; | |
1312 | int latch = get_swap_bios(); | |
1313 | if (unlikely(latch != md->swap_bios)) | |
1314 | __set_swap_bios_limit(md, latch); | |
1315 | down(&md->swap_bios_semaphore); | |
1316 | } | |
1317 | ||
7de3ee57 | 1318 | r = ti->type->map(ti, clone); |
846785e6 CH |
1319 | switch (r) { |
1320 | case DM_MAPIO_SUBMITTED: | |
1321 | break; | |
1322 | case DM_MAPIO_REMAPPED: | |
1da177e4 | 1323 | /* the bio has been remapped so dispatch it */ |
1c02fca6 | 1324 | trace_block_bio_remap(clone, bio_dev(io->orig_bio), sector); |
5a6c35f9 | 1325 | ret = submit_bio_noacct(clone); |
846785e6 CH |
1326 | break; |
1327 | case DM_MAPIO_KILL: | |
bffdde6f MP |
1328 | if (unlikely(swap_bios_limit(ti, clone))) { |
1329 | struct mapped_device *md = io->md; | |
1330 | up(&md->swap_bios_semaphore); | |
1331 | } | |
4e4cbee9 | 1332 | free_tio(tio); |
64f52b0e | 1333 | dec_pending(io, BLK_STS_IOERR); |
4e4cbee9 | 1334 | break; |
846785e6 | 1335 | case DM_MAPIO_REQUEUE: |
bffdde6f MP |
1336 | if (unlikely(swap_bios_limit(ti, clone))) { |
1337 | struct mapped_device *md = io->md; | |
1338 | up(&md->swap_bios_semaphore); | |
1339 | } | |
cfae7529 | 1340 | free_tio(tio); |
64f52b0e | 1341 | dec_pending(io, BLK_STS_DM_REQUEUE); |
846785e6 CH |
1342 | break; |
1343 | default: | |
45cbcd79 KU |
1344 | DMWARN("unimplemented target map return value: %d", r); |
1345 | BUG(); | |
1da177e4 | 1346 | } |
1da177e4 | 1347 | |
978e51ba | 1348 | return ret; |
1da177e4 | 1349 | } |
1da177e4 | 1350 | |
e0d6609a | 1351 | static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) |
bd2a49b8 | 1352 | { |
4f024f37 KO |
1353 | bio->bi_iter.bi_sector = sector; |
1354 | bio->bi_iter.bi_size = to_bytes(len); | |
1da177e4 LT |
1355 | } |
1356 | ||
1357 | /* | |
1358 | * Creates a bio that consists of range of complete bvecs. | |
1359 | */ | |
c80914e8 MS |
1360 | static int clone_bio(struct dm_target_io *tio, struct bio *bio, |
1361 | sector_t sector, unsigned len) | |
1da177e4 | 1362 | { |
dba14160 | 1363 | struct bio *clone = &tio->clone; |
07560151 | 1364 | int r; |
1da177e4 | 1365 | |
1c3b13e6 KO |
1366 | __bio_clone_fast(clone, bio); |
1367 | ||
07560151 EB |
1368 | r = bio_crypt_clone(clone, bio, GFP_NOIO); |
1369 | if (r < 0) | |
1370 | return r; | |
a892c8d5 | 1371 | |
57c36519 | 1372 | if (bio_integrity(bio)) { |
e2460f2a MP |
1373 | if (unlikely(!dm_target_has_integrity(tio->ti->type) && |
1374 | !dm_target_passes_integrity(tio->ti->type))) { | |
1375 | DMWARN("%s: the target %s doesn't support integrity data.", | |
1376 | dm_device_name(tio->io->md), | |
1377 | tio->ti->type->name); | |
1378 | return -EIO; | |
1379 | } | |
1380 | ||
1381 | r = bio_integrity_clone(clone, bio, GFP_NOIO); | |
c80914e8 MS |
1382 | if (r < 0) |
1383 | return r; | |
1384 | } | |
bd2a49b8 | 1385 | |
fa8db494 MS |
1386 | bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); |
1387 | clone->bi_iter.bi_size = to_bytes(len); | |
1388 | ||
1389 | if (bio_integrity(bio)) | |
1390 | bio_integrity_trim(clone); | |
c80914e8 MS |
1391 | |
1392 | return 0; | |
1da177e4 LT |
1393 | } |
1394 | ||
318716dd MS |
1395 | static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, |
1396 | struct dm_target *ti, unsigned num_bios) | |
f9ab94ce | 1397 | { |
dba14160 | 1398 | struct dm_target_io *tio; |
318716dd | 1399 | int try; |
dba14160 | 1400 | |
318716dd MS |
1401 | if (!num_bios) |
1402 | return; | |
f9ab94ce | 1403 | |
318716dd MS |
1404 | if (num_bios == 1) { |
1405 | tio = alloc_tio(ci, ti, 0, GFP_NOIO); | |
1406 | bio_list_add(blist, &tio->clone); | |
1407 | return; | |
1408 | } | |
9015df24 | 1409 | |
318716dd MS |
1410 | for (try = 0; try < 2; try++) { |
1411 | int bio_nr; | |
1412 | struct bio *bio; | |
1413 | ||
1414 | if (try) | |
bc02cdbe | 1415 | mutex_lock(&ci->io->md->table_devices_lock); |
318716dd MS |
1416 | for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { |
1417 | tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT); | |
1418 | if (!tio) | |
1419 | break; | |
1420 | ||
1421 | bio_list_add(blist, &tio->clone); | |
1422 | } | |
1423 | if (try) | |
bc02cdbe | 1424 | mutex_unlock(&ci->io->md->table_devices_lock); |
318716dd MS |
1425 | if (bio_nr == num_bios) |
1426 | return; | |
1427 | ||
1428 | while ((bio = bio_list_pop(blist))) { | |
1429 | tio = container_of(bio, struct dm_target_io, clone); | |
1430 | free_tio(tio); | |
1431 | } | |
1432 | } | |
9015df24 AK |
1433 | } |
1434 | ||
978e51ba MS |
1435 | static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci, |
1436 | struct dm_target_io *tio, unsigned *len) | |
9015df24 | 1437 | { |
dba14160 | 1438 | struct bio *clone = &tio->clone; |
9015df24 | 1439 | |
1dd40c3e MP |
1440 | tio->len_ptr = len; |
1441 | ||
99778273 | 1442 | __bio_clone_fast(clone, ci->bio); |
bd2a49b8 | 1443 | if (len) |
1dd40c3e | 1444 | bio_setup_sector(clone, ci->sector, *len); |
f9ab94ce | 1445 | |
978e51ba | 1446 | return __map_bio(tio); |
f9ab94ce MP |
1447 | } |
1448 | ||
14fe594d | 1449 | static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, |
1dd40c3e | 1450 | unsigned num_bios, unsigned *len) |
06a426ce | 1451 | { |
318716dd MS |
1452 | struct bio_list blist = BIO_EMPTY_LIST; |
1453 | struct bio *bio; | |
1454 | struct dm_target_io *tio; | |
1455 | ||
1456 | alloc_multiple_bios(&blist, ci, ti, num_bios); | |
06a426ce | 1457 | |
318716dd MS |
1458 | while ((bio = bio_list_pop(&blist))) { |
1459 | tio = container_of(bio, struct dm_target_io, clone); | |
978e51ba | 1460 | (void) __clone_and_map_simple_bio(ci, tio, len); |
318716dd | 1461 | } |
06a426ce MS |
1462 | } |
1463 | ||
14fe594d | 1464 | static int __send_empty_flush(struct clone_info *ci) |
f9ab94ce | 1465 | { |
06a426ce | 1466 | unsigned target_nr = 0; |
f9ab94ce | 1467 | struct dm_target *ti; |
828678b8 MS |
1468 | struct bio flush_bio; |
1469 | ||
1470 | /* | |
1471 | * Use an on-stack bio for this, it's safe since we don't | |
1472 | * need to reference it after submit. It's just used as | |
1473 | * the basis for the clone(s). | |
1474 | */ | |
1475 | bio_init(&flush_bio, NULL, 0); | |
1476 | flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC; | |
47d95102 CH |
1477 | flush_bio.bi_disk = ci->io->md->disk; |
1478 | bio_associate_blkg(&flush_bio); | |
1479 | ||
828678b8 MS |
1480 | ci->bio = &flush_bio; |
1481 | ci->sector_count = 0; | |
f9ab94ce | 1482 | |
b372d360 | 1483 | BUG_ON(bio_has_data(ci->bio)); |
f9ab94ce | 1484 | while ((ti = dm_table_get_target(ci->map, target_nr++))) |
1dd40c3e | 1485 | __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); |
828678b8 MS |
1486 | |
1487 | bio_uninit(ci->bio); | |
f9ab94ce MP |
1488 | return 0; |
1489 | } | |
1490 | ||
c80914e8 | 1491 | static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, |
f31c21e4 | 1492 | sector_t sector, unsigned *len) |
5ae89a87 | 1493 | { |
dba14160 | 1494 | struct bio *bio = ci->bio; |
5ae89a87 | 1495 | struct dm_target_io *tio; |
f31c21e4 | 1496 | int r; |
5ae89a87 | 1497 | |
318716dd | 1498 | tio = alloc_tio(ci, ti, 0, GFP_NOIO); |
f31c21e4 N |
1499 | tio->len_ptr = len; |
1500 | r = clone_bio(tio, bio, sector, *len); | |
1501 | if (r < 0) { | |
1502 | free_tio(tio); | |
1503 | return r; | |
b0d8ed4d | 1504 | } |
978e51ba | 1505 | (void) __map_bio(tio); |
c80914e8 | 1506 | |
f31c21e4 | 1507 | return 0; |
5ae89a87 MS |
1508 | } |
1509 | ||
3d7f4562 | 1510 | static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, |
61697a6a | 1511 | unsigned num_bios) |
ba1cbad9 | 1512 | { |
51b86f9a | 1513 | unsigned len; |
ba1cbad9 | 1514 | |
3d7f4562 MS |
1515 | /* |
1516 | * Even though the device advertised support for this type of | |
1517 | * request, that does not mean every target supports it, and | |
1518 | * reconfiguration might also have changed that since the | |
1519 | * check was performed. | |
1520 | */ | |
3d7f4562 MS |
1521 | if (!num_bios) |
1522 | return -EOPNOTSUPP; | |
ba1cbad9 | 1523 | |
3720281d MS |
1524 | len = min_t(sector_t, ci->sector_count, |
1525 | max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector))); | |
51b86f9a | 1526 | |
3d7f4562 | 1527 | __send_duplicate_bios(ci, ti, num_bios, &len); |
e262f347 | 1528 | |
3d7f4562 MS |
1529 | ci->sector += len; |
1530 | ci->sector_count -= len; | |
5ae89a87 MS |
1531 | |
1532 | return 0; | |
ba1cbad9 MS |
1533 | } |
1534 | ||
568c73a3 MS |
1535 | static bool is_abnormal_io(struct bio *bio) |
1536 | { | |
1537 | bool r = false; | |
1538 | ||
1539 | switch (bio_op(bio)) { | |
1540 | case REQ_OP_DISCARD: | |
1541 | case REQ_OP_SECURE_ERASE: | |
1542 | case REQ_OP_WRITE_SAME: | |
1543 | case REQ_OP_WRITE_ZEROES: | |
1544 | r = true; | |
1545 | break; | |
1546 | } | |
1547 | ||
1548 | return r; | |
1549 | } | |
1550 | ||
0519c71e MS |
1551 | static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, |
1552 | int *result) | |
1553 | { | |
1554 | struct bio *bio = ci->bio; | |
9679b5a7 | 1555 | unsigned num_bios = 0; |
0519c71e | 1556 | |
9679b5a7 MS |
1557 | switch (bio_op(bio)) { |
1558 | case REQ_OP_DISCARD: | |
1559 | num_bios = ti->num_discard_bios; | |
1560 | break; | |
1561 | case REQ_OP_SECURE_ERASE: | |
1562 | num_bios = ti->num_secure_erase_bios; | |
1563 | break; | |
1564 | case REQ_OP_WRITE_SAME: | |
1565 | num_bios = ti->num_write_same_bios; | |
1566 | break; | |
1567 | case REQ_OP_WRITE_ZEROES: | |
1568 | num_bios = ti->num_write_zeroes_bios; | |
1569 | break; | |
1570 | default: | |
0519c71e | 1571 | return false; |
9679b5a7 | 1572 | } |
0519c71e | 1573 | |
9679b5a7 | 1574 | *result = __send_changing_extent_only(ci, ti, num_bios); |
0519c71e MS |
1575 | return true; |
1576 | } | |
1577 | ||
e4c93811 AK |
1578 | /* |
1579 | * Select the correct strategy for processing a non-flush bio. | |
1580 | */ | |
14fe594d | 1581 | static int __split_and_process_non_flush(struct clone_info *ci) |
0ce65797 | 1582 | { |
512875bd | 1583 | struct dm_target *ti; |
1c3b13e6 | 1584 | unsigned len; |
c80914e8 | 1585 | int r; |
0ce65797 | 1586 | |
512875bd | 1587 | ti = dm_table_find_target(ci->map, ci->sector); |
123d87d5 | 1588 | if (!ti) |
512875bd JN |
1589 | return -EIO; |
1590 | ||
568c73a3 | 1591 | if (__process_abnormal_io(ci, ti, &r)) |
0519c71e | 1592 | return r; |
3d7f4562 | 1593 | |
3720281d | 1594 | len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count); |
0ce65797 | 1595 | |
c80914e8 MS |
1596 | r = __clone_and_map_data_bio(ci, ti, ci->sector, &len); |
1597 | if (r < 0) | |
1598 | return r; | |
0ce65797 | 1599 | |
1c3b13e6 KO |
1600 | ci->sector += len; |
1601 | ci->sector_count -= len; | |
0ce65797 | 1602 | |
1c3b13e6 | 1603 | return 0; |
0ce65797 MS |
1604 | } |
1605 | ||
978e51ba MS |
1606 | static void init_clone_info(struct clone_info *ci, struct mapped_device *md, |
1607 | struct dm_table *map, struct bio *bio) | |
1608 | { | |
1609 | ci->map = map; | |
1610 | ci->io = alloc_io(md, bio); | |
1611 | ci->sector = bio->bi_iter.bi_sector; | |
1612 | } | |
1613 | ||
a1e1cb72 MS |
1614 | #define __dm_part_stat_sub(part, field, subnd) \ |
1615 | (part_stat_get(part, field) -= (subnd)) | |
1616 | ||
1da177e4 | 1617 | /* |
14fe594d | 1618 | * Entry point to split a bio into clones and submit them to the targets. |
1da177e4 | 1619 | */ |
978e51ba MS |
1620 | static blk_qc_t __split_and_process_bio(struct mapped_device *md, |
1621 | struct dm_table *map, struct bio *bio) | |
0ce65797 | 1622 | { |
1da177e4 | 1623 | struct clone_info ci; |
978e51ba | 1624 | blk_qc_t ret = BLK_QC_T_NONE; |
512875bd | 1625 | int error = 0; |
1da177e4 | 1626 | |
978e51ba | 1627 | init_clone_info(&ci, md, map, bio); |
0ce65797 | 1628 | |
1eff9d32 | 1629 | if (bio->bi_opf & REQ_PREFLUSH) { |
14fe594d | 1630 | error = __send_empty_flush(&ci); |
b372d360 | 1631 | /* dec_pending submits any data associated with flush */ |
2e2d6f7e | 1632 | } else if (op_is_zone_mgmt(bio_op(bio))) { |
a4aa5e56 DLM |
1633 | ci.bio = bio; |
1634 | ci.sector_count = 0; | |
1635 | error = __split_and_process_non_flush(&ci); | |
b372d360 | 1636 | } else { |
6a8736d1 | 1637 | ci.bio = bio; |
d87f4c14 | 1638 | ci.sector_count = bio_sectors(bio); |
18a25da8 | 1639 | while (ci.sector_count && !error) { |
14fe594d | 1640 | error = __split_and_process_non_flush(&ci); |
985eabdc | 1641 | if (ci.sector_count && !error) { |
18a25da8 | 1642 | /* |
ed00aabd | 1643 | * Remainder must be passed to submit_bio_noacct() |
18a25da8 N |
1644 | * so that it gets handled *after* bios already submitted |
1645 | * have been completely processed. | |
1646 | * We take a clone of the original to store in | |
745dc570 | 1647 | * ci.io->orig_bio to be used by end_io_acct() and |
18a25da8 | 1648 | * for dec_pending to use for completion handling. |
18a25da8 | 1649 | */ |
f21c601a MS |
1650 | struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count, |
1651 | GFP_NOIO, &md->queue->bio_split); | |
745dc570 | 1652 | ci.io->orig_bio = b; |
a1e1cb72 MS |
1653 | |
1654 | /* | |
1655 | * Adjust IO stats for each split, otherwise upon queue | |
1656 | * reentry there will be redundant IO accounting. | |
1657 | * NOTE: this is a stop-gap fix, a proper fix involves | |
1658 | * significant refactoring of DM core's bio splitting | |
1659 | * (by eliminating DM's splitting and just using bio_split) | |
1660 | */ | |
1661 | part_stat_lock(); | |
8446fe92 | 1662 | __dm_part_stat_sub(dm_disk(md)->part0, |
a1e1cb72 MS |
1663 | sectors[op_stat_group(bio_op(bio))], ci.sector_count); |
1664 | part_stat_unlock(); | |
1665 | ||
18a25da8 | 1666 | bio_chain(b, bio); |
eb6f7f7c | 1667 | trace_block_split(b, bio->bi_iter.bi_sector); |
ed00aabd | 1668 | ret = submit_bio_noacct(bio); |
18a25da8 N |
1669 | break; |
1670 | } | |
1671 | } | |
d87f4c14 | 1672 | } |
0ce65797 | 1673 | |
1da177e4 | 1674 | /* drop the extra reference count */ |
54385bf7 | 1675 | dec_pending(ci.io, errno_to_blk_status(error)); |
978e51ba | 1676 | return ret; |
0ce65797 MS |
1677 | } |
1678 | ||
c62b37d9 | 1679 | static blk_qc_t dm_submit_bio(struct bio *bio) |
cec47e3d | 1680 | { |
c4a59c4e | 1681 | struct mapped_device *md = bio->bi_disk->private_data; |
978e51ba | 1682 | blk_qc_t ret = BLK_QC_T_NONE; |
83d5e5b0 MP |
1683 | int srcu_idx; |
1684 | struct dm_table *map; | |
cec47e3d | 1685 | |
83d5e5b0 | 1686 | map = dm_get_live_table(md, &srcu_idx); |
b2abdb1b MS |
1687 | if (unlikely(!map)) { |
1688 | DMERR_LIMIT("%s: mapping table unavailable, erroring io", | |
1689 | dm_device_name(md)); | |
1690 | bio_io_error(bio); | |
1691 | goto out; | |
1692 | } | |
29e4013d | 1693 | |
b2abdb1b | 1694 | /* If suspended, queue this IO for later */ |
6a8736d1 | 1695 | if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { |
6abc4946 KK |
1696 | if (bio->bi_opf & REQ_NOWAIT) |
1697 | bio_wouldblock_error(bio); | |
b2abdb1b | 1698 | else if (bio->bi_opf & REQ_RAHEAD) |
54d9a1b4 | 1699 | bio_io_error(bio); |
b2abdb1b MS |
1700 | else |
1701 | queue_io(md, bio); | |
1702 | goto out; | |
cec47e3d | 1703 | } |
1da177e4 | 1704 | |
b2abdb1b MS |
1705 | /* |
1706 | * Use blk_queue_split() for abnormal IO (e.g. discard, writesame, etc) | |
1707 | * otherwise associated queue_limits won't be imposed. | |
1708 | */ | |
1709 | if (is_abnormal_io(bio)) | |
1710 | blk_queue_split(&bio); | |
978e51ba | 1711 | |
9c37de29 | 1712 | ret = __split_and_process_bio(md, map, bio); |
b2abdb1b | 1713 | out: |
83d5e5b0 | 1714 | dm_put_live_table(md, srcu_idx); |
978e51ba MS |
1715 | return ret; |
1716 | } | |
1717 | ||
1da177e4 LT |
1718 | /*----------------------------------------------------------------- |
1719 | * An IDR is used to keep track of allocated minor numbers. | |
1720 | *---------------------------------------------------------------*/ | |
2b06cfff | 1721 | static void free_minor(int minor) |
1da177e4 | 1722 | { |
f32c10b0 | 1723 | spin_lock(&_minor_lock); |
1da177e4 | 1724 | idr_remove(&_minor_idr, minor); |
f32c10b0 | 1725 | spin_unlock(&_minor_lock); |
1da177e4 LT |
1726 | } |
1727 | ||
1728 | /* | |
1729 | * See if the device with a specific minor # is free. | |
1730 | */ | |
cf13ab8e | 1731 | static int specific_minor(int minor) |
1da177e4 | 1732 | { |
c9d76be6 | 1733 | int r; |
1da177e4 LT |
1734 | |
1735 | if (minor >= (1 << MINORBITS)) | |
1736 | return -EINVAL; | |
1737 | ||
c9d76be6 | 1738 | idr_preload(GFP_KERNEL); |
f32c10b0 | 1739 | spin_lock(&_minor_lock); |
1da177e4 | 1740 | |
c9d76be6 | 1741 | r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); |
1da177e4 | 1742 | |
f32c10b0 | 1743 | spin_unlock(&_minor_lock); |
c9d76be6 TH |
1744 | idr_preload_end(); |
1745 | if (r < 0) | |
1746 | return r == -ENOSPC ? -EBUSY : r; | |
1747 | return 0; | |
1da177e4 LT |
1748 | } |
1749 | ||
cf13ab8e | 1750 | static int next_free_minor(int *minor) |
1da177e4 | 1751 | { |
c9d76be6 | 1752 | int r; |
62f75c2f | 1753 | |
c9d76be6 | 1754 | idr_preload(GFP_KERNEL); |
f32c10b0 | 1755 | spin_lock(&_minor_lock); |
1da177e4 | 1756 | |
c9d76be6 | 1757 | r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); |
1da177e4 | 1758 | |
f32c10b0 | 1759 | spin_unlock(&_minor_lock); |
c9d76be6 TH |
1760 | idr_preload_end(); |
1761 | if (r < 0) | |
1762 | return r; | |
1763 | *minor = r; | |
1764 | return 0; | |
1da177e4 LT |
1765 | } |
1766 | ||
83d5cde4 | 1767 | static const struct block_device_operations dm_blk_dops; |
681cc5e8 | 1768 | static const struct block_device_operations dm_rq_blk_dops; |
f26c5719 | 1769 | static const struct dax_operations dm_dax_ops; |
1da177e4 | 1770 | |
53d5914f MP |
1771 | static void dm_wq_work(struct work_struct *work); |
1772 | ||
0f20972f MS |
1773 | static void cleanup_mapped_device(struct mapped_device *md) |
1774 | { | |
0f20972f MS |
1775 | if (md->wq) |
1776 | destroy_workqueue(md->wq); | |
6f1c819c KO |
1777 | bioset_exit(&md->bs); |
1778 | bioset_exit(&md->io_bs); | |
0f20972f | 1779 | |
f26c5719 DW |
1780 | if (md->dax_dev) { |
1781 | kill_dax(md->dax_dev); | |
1782 | put_dax(md->dax_dev); | |
1783 | md->dax_dev = NULL; | |
1784 | } | |
1785 | ||
0f20972f MS |
1786 | if (md->disk) { |
1787 | spin_lock(&_minor_lock); | |
1788 | md->disk->private_data = NULL; | |
1789 | spin_unlock(&_minor_lock); | |
0f20972f MS |
1790 | del_gendisk(md->disk); |
1791 | put_disk(md->disk); | |
1792 | } | |
1793 | ||
1794 | if (md->queue) | |
1795 | blk_cleanup_queue(md->queue); | |
1796 | ||
d09960b0 TE |
1797 | cleanup_srcu_struct(&md->io_barrier); |
1798 | ||
d5ffebdd MS |
1799 | mutex_destroy(&md->suspend_lock); |
1800 | mutex_destroy(&md->type_lock); | |
1801 | mutex_destroy(&md->table_devices_lock); | |
bffdde6f | 1802 | mutex_destroy(&md->swap_bios_lock); |
d5ffebdd | 1803 | |
4cc96131 | 1804 | dm_mq_cleanup_mapped_device(md); |
0f20972f MS |
1805 | } |
1806 | ||
1da177e4 LT |
1807 | /* |
1808 | * Allocate and initialise a blank device with a given minor. | |
1809 | */ | |
2b06cfff | 1810 | static struct mapped_device *alloc_dev(int minor) |
1da177e4 | 1811 | { |
115485e8 MS |
1812 | int r, numa_node_id = dm_get_numa_node(); |
1813 | struct mapped_device *md; | |
ba61fdd1 | 1814 | void *old_md; |
1da177e4 | 1815 | |
856eb091 | 1816 | md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); |
1da177e4 LT |
1817 | if (!md) { |
1818 | DMWARN("unable to allocate device, out of memory."); | |
1819 | return NULL; | |
1820 | } | |
1821 | ||
10da4f79 | 1822 | if (!try_module_get(THIS_MODULE)) |
6ed7ade8 | 1823 | goto bad_module_get; |
10da4f79 | 1824 | |
1da177e4 | 1825 | /* get a minor number for the dev */ |
2b06cfff | 1826 | if (minor == DM_ANY_MINOR) |
cf13ab8e | 1827 | r = next_free_minor(&minor); |
2b06cfff | 1828 | else |
cf13ab8e | 1829 | r = specific_minor(minor); |
1da177e4 | 1830 | if (r < 0) |
6ed7ade8 | 1831 | goto bad_minor; |
1da177e4 | 1832 | |
83d5e5b0 MP |
1833 | r = init_srcu_struct(&md->io_barrier); |
1834 | if (r < 0) | |
1835 | goto bad_io_barrier; | |
1836 | ||
115485e8 | 1837 | md->numa_node_id = numa_node_id; |
591ddcfc | 1838 | md->init_tio_pdu = false; |
a5664dad | 1839 | md->type = DM_TYPE_NONE; |
e61290a4 | 1840 | mutex_init(&md->suspend_lock); |
a5664dad | 1841 | mutex_init(&md->type_lock); |
86f1152b | 1842 | mutex_init(&md->table_devices_lock); |
022c2611 | 1843 | spin_lock_init(&md->deferred_lock); |
1da177e4 | 1844 | atomic_set(&md->holders, 1); |
5c6bd75d | 1845 | atomic_set(&md->open_count, 0); |
1da177e4 | 1846 | atomic_set(&md->event_nr, 0); |
7a8c3d3b MA |
1847 | atomic_set(&md->uevent_seq, 0); |
1848 | INIT_LIST_HEAD(&md->uevent_list); | |
86f1152b | 1849 | INIT_LIST_HEAD(&md->table_devices); |
7a8c3d3b | 1850 | spin_lock_init(&md->uevent_lock); |
1da177e4 | 1851 | |
47ace7e0 | 1852 | /* |
c62b37d9 CH |
1853 | * default to bio-based until DM table is loaded and md->type |
1854 | * established. If request-based table is loaded: blk-mq will | |
1855 | * override accordingly. | |
47ace7e0 | 1856 | */ |
c62b37d9 | 1857 | md->queue = blk_alloc_queue(numa_node_id); |
3d745ea5 CH |
1858 | if (!md->queue) |
1859 | goto bad; | |
1da177e4 | 1860 | |
c12c9a3c | 1861 | md->disk = alloc_disk_node(1, md->numa_node_id); |
1da177e4 | 1862 | if (!md->disk) |
0f20972f | 1863 | goto bad; |
1da177e4 | 1864 | |
f0b04115 | 1865 | init_waitqueue_head(&md->wait); |
53d5914f | 1866 | INIT_WORK(&md->work, dm_wq_work); |
f0b04115 | 1867 | init_waitqueue_head(&md->eventq); |
2995fa78 | 1868 | init_completion(&md->kobj_holder.completion); |
f0b04115 | 1869 | |
bffdde6f MP |
1870 | md->swap_bios = get_swap_bios(); |
1871 | sema_init(&md->swap_bios_semaphore, md->swap_bios); | |
1872 | mutex_init(&md->swap_bios_lock); | |
1873 | ||
1da177e4 LT |
1874 | md->disk->major = _major; |
1875 | md->disk->first_minor = minor; | |
1876 | md->disk->fops = &dm_blk_dops; | |
1877 | md->disk->queue = md->queue; | |
1878 | md->disk->private_data = md; | |
1879 | sprintf(md->disk->disk_name, "dm-%d", minor); | |
f26c5719 | 1880 | |
976431b0 | 1881 | if (IS_ENABLED(CONFIG_DAX_DRIVER)) { |
fefc1d97 PG |
1882 | md->dax_dev = alloc_dax(md, md->disk->disk_name, |
1883 | &dm_dax_ops, 0); | |
4e4ced93 | 1884 | if (IS_ERR(md->dax_dev)) |
976431b0 DW |
1885 | goto bad; |
1886 | } | |
f26c5719 | 1887 | |
c100ec49 | 1888 | add_disk_no_queue_reg(md->disk); |
7e51f257 | 1889 | format_dev_t(md->name, MKDEV(_major, minor)); |
1da177e4 | 1890 | |
670368a8 | 1891 | md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); |
304f3f6a | 1892 | if (!md->wq) |
0f20972f | 1893 | goto bad; |
304f3f6a | 1894 | |
fd2ed4d2 MP |
1895 | dm_stats_init(&md->stats); |
1896 | ||
ba61fdd1 | 1897 | /* Populate the mapping, nobody knows we exist yet */ |
f32c10b0 | 1898 | spin_lock(&_minor_lock); |
ba61fdd1 | 1899 | old_md = idr_replace(&_minor_idr, md, minor); |
f32c10b0 | 1900 | spin_unlock(&_minor_lock); |
ba61fdd1 JM |
1901 | |
1902 | BUG_ON(old_md != MINOR_ALLOCED); | |
1903 | ||
1da177e4 LT |
1904 | return md; |
1905 | ||
0f20972f MS |
1906 | bad: |
1907 | cleanup_mapped_device(md); | |
83d5e5b0 | 1908 | bad_io_barrier: |
1da177e4 | 1909 | free_minor(minor); |
6ed7ade8 | 1910 | bad_minor: |
10da4f79 | 1911 | module_put(THIS_MODULE); |
6ed7ade8 | 1912 | bad_module_get: |
856eb091 | 1913 | kvfree(md); |
1da177e4 LT |
1914 | return NULL; |
1915 | } | |
1916 | ||
ae9da83f JN |
1917 | static void unlock_fs(struct mapped_device *md); |
1918 | ||
1da177e4 LT |
1919 | static void free_dev(struct mapped_device *md) |
1920 | { | |
f331c029 | 1921 | int minor = MINOR(disk_devt(md->disk)); |
63d94e48 | 1922 | |
32a926da | 1923 | unlock_fs(md); |
2eb6e1e3 | 1924 | |
0f20972f | 1925 | cleanup_mapped_device(md); |
63a4f065 | 1926 | |
86f1152b | 1927 | free_table_devices(&md->table_devices); |
63a4f065 | 1928 | dm_stats_cleanup(&md->stats); |
63a4f065 MS |
1929 | free_minor(minor); |
1930 | ||
10da4f79 | 1931 | module_put(THIS_MODULE); |
856eb091 | 1932 | kvfree(md); |
1da177e4 LT |
1933 | } |
1934 | ||
2a2a4c51 | 1935 | static int __bind_mempools(struct mapped_device *md, struct dm_table *t) |
e6ee8c0b | 1936 | { |
c0820cf5 | 1937 | struct dm_md_mempools *p = dm_table_get_md_mempools(t); |
2a2a4c51 | 1938 | int ret = 0; |
e6ee8c0b | 1939 | |
0776aa0e | 1940 | if (dm_table_bio_based(t)) { |
64f52b0e MS |
1941 | /* |
1942 | * The md may already have mempools that need changing. | |
1943 | * If so, reload bioset because front_pad may have changed | |
1944 | * because a different table was loaded. | |
1945 | */ | |
6f1c819c KO |
1946 | bioset_exit(&md->bs); |
1947 | bioset_exit(&md->io_bs); | |
0776aa0e | 1948 | |
6f1c819c | 1949 | } else if (bioset_initialized(&md->bs)) { |
4e6e36c3 MS |
1950 | /* |
1951 | * There's no need to reload with request-based dm | |
1952 | * because the size of front_pad doesn't change. | |
1953 | * Note for future: If you are to reload bioset, | |
1954 | * prep-ed requests in the queue may refer | |
1955 | * to bio from the old bioset, so you must walk | |
1956 | * through the queue to unprep. | |
1957 | */ | |
1958 | goto out; | |
c0820cf5 | 1959 | } |
e6ee8c0b | 1960 | |
6f1c819c KO |
1961 | BUG_ON(!p || |
1962 | bioset_initialized(&md->bs) || | |
1963 | bioset_initialized(&md->io_bs)); | |
cbc4e3c1 | 1964 | |
2a2a4c51 JA |
1965 | ret = bioset_init_from_src(&md->bs, &p->bs); |
1966 | if (ret) | |
1967 | goto out; | |
1968 | ret = bioset_init_from_src(&md->io_bs, &p->io_bs); | |
1969 | if (ret) | |
1970 | bioset_exit(&md->bs); | |
e6ee8c0b | 1971 | out: |
02233342 | 1972 | /* mempool bind completed, no longer need any mempools in the table */ |
e6ee8c0b | 1973 | dm_table_free_md_mempools(t); |
2a2a4c51 | 1974 | return ret; |
e6ee8c0b KU |
1975 | } |
1976 | ||
1da177e4 LT |
1977 | /* |
1978 | * Bind a table to the device. | |
1979 | */ | |
1980 | static void event_callback(void *context) | |
1981 | { | |
7a8c3d3b MA |
1982 | unsigned long flags; |
1983 | LIST_HEAD(uevents); | |
1da177e4 LT |
1984 | struct mapped_device *md = (struct mapped_device *) context; |
1985 | ||
7a8c3d3b MA |
1986 | spin_lock_irqsave(&md->uevent_lock, flags); |
1987 | list_splice_init(&md->uevent_list, &uevents); | |
1988 | spin_unlock_irqrestore(&md->uevent_lock, flags); | |
1989 | ||
ed9e1982 | 1990 | dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); |
7a8c3d3b | 1991 | |
1da177e4 LT |
1992 | atomic_inc(&md->event_nr); |
1993 | wake_up(&md->eventq); | |
62e08243 | 1994 | dm_issue_global_event(); |
1da177e4 LT |
1995 | } |
1996 | ||
042d2a9b AK |
1997 | /* |
1998 | * Returns old map, which caller must destroy. | |
1999 | */ | |
2000 | static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, | |
2001 | struct queue_limits *limits) | |
1da177e4 | 2002 | { |
042d2a9b | 2003 | struct dm_table *old_map; |
165125e1 | 2004 | struct request_queue *q = md->queue; |
978e51ba | 2005 | bool request_based = dm_table_request_based(t); |
1da177e4 | 2006 | sector_t size; |
2a2a4c51 | 2007 | int ret; |
1da177e4 | 2008 | |
5a8f1f80 BVA |
2009 | lockdep_assert_held(&md->suspend_lock); |
2010 | ||
1da177e4 | 2011 | size = dm_table_get_size(t); |
3ac51e74 DW |
2012 | |
2013 | /* | |
2014 | * Wipe any geometry if the size of the table changed. | |
2015 | */ | |
fd2ed4d2 | 2016 | if (size != dm_get_size(md)) |
3ac51e74 DW |
2017 | memset(&md->geometry, 0, sizeof(md->geometry)); |
2018 | ||
c1103d73 MP |
2019 | if (!get_capacity(md->disk)) |
2020 | set_capacity(md->disk, size); | |
2021 | else | |
2022 | set_capacity_and_notify(md->disk, size); | |
d5816876 | 2023 | |
2ca3310e AK |
2024 | dm_table_event_callback(t, event_callback, md); |
2025 | ||
e6ee8c0b KU |
2026 | /* |
2027 | * The queue hasn't been stopped yet, if the old table type wasn't | |
2028 | * for request-based during suspension. So stop it to prevent | |
2029 | * I/O mapping before resume. | |
2030 | * This must be done before setting the queue restrictions, | |
2031 | * because request-based dm may be run just after the setting. | |
2032 | */ | |
978e51ba | 2033 | if (request_based) |
eca7ee6d | 2034 | dm_stop_queue(q); |
978e51ba | 2035 | |
9c37de29 | 2036 | if (request_based) { |
16f12266 | 2037 | /* |
9c37de29 MS |
2038 | * Leverage the fact that request-based DM targets are |
2039 | * immutable singletons - used to optimize dm_mq_queue_rq. | |
16f12266 MS |
2040 | */ |
2041 | md->immutable_target = dm_table_get_immutable_target(t); | |
2042 | } | |
e6ee8c0b | 2043 | |
2a2a4c51 JA |
2044 | ret = __bind_mempools(md, t); |
2045 | if (ret) { | |
2046 | old_map = ERR_PTR(ret); | |
2047 | goto out; | |
2048 | } | |
e6ee8c0b | 2049 | |
a12f5d48 | 2050 | old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
1d3aa6f6 | 2051 | rcu_assign_pointer(md->map, (void *)t); |
36a0456f AK |
2052 | md->immutable_target_type = dm_table_get_immutable_target_type(t); |
2053 | ||
754c5fc7 | 2054 | dm_table_set_restrictions(t, q, limits); |
41abc4e1 HR |
2055 | if (old_map) |
2056 | dm_sync_table(md); | |
1da177e4 | 2057 | |
2a2a4c51 | 2058 | out: |
042d2a9b | 2059 | return old_map; |
1da177e4 LT |
2060 | } |
2061 | ||
a7940155 AK |
2062 | /* |
2063 | * Returns unbound table for the caller to free. | |
2064 | */ | |
2065 | static struct dm_table *__unbind(struct mapped_device *md) | |
1da177e4 | 2066 | { |
a12f5d48 | 2067 | struct dm_table *map = rcu_dereference_protected(md->map, 1); |
1da177e4 LT |
2068 | |
2069 | if (!map) | |
a7940155 | 2070 | return NULL; |
1da177e4 LT |
2071 | |
2072 | dm_table_event_callback(map, NULL, NULL); | |
9cdb8520 | 2073 | RCU_INIT_POINTER(md->map, NULL); |
83d5e5b0 | 2074 | dm_sync_table(md); |
a7940155 AK |
2075 | |
2076 | return map; | |
1da177e4 LT |
2077 | } |
2078 | ||
2079 | /* | |
2080 | * Constructor for a new device. | |
2081 | */ | |
2b06cfff | 2082 | int dm_create(int minor, struct mapped_device **result) |
1da177e4 | 2083 | { |
c12c9a3c | 2084 | int r; |
1da177e4 LT |
2085 | struct mapped_device *md; |
2086 | ||
2b06cfff | 2087 | md = alloc_dev(minor); |
1da177e4 LT |
2088 | if (!md) |
2089 | return -ENXIO; | |
2090 | ||
c12c9a3c MS |
2091 | r = dm_sysfs_init(md); |
2092 | if (r) { | |
2093 | free_dev(md); | |
2094 | return r; | |
2095 | } | |
784aae73 | 2096 | |
1da177e4 LT |
2097 | *result = md; |
2098 | return 0; | |
2099 | } | |
2100 | ||
a5664dad MS |
2101 | /* |
2102 | * Functions to manage md->type. | |
2103 | * All are required to hold md->type_lock. | |
2104 | */ | |
2105 | void dm_lock_md_type(struct mapped_device *md) | |
2106 | { | |
2107 | mutex_lock(&md->type_lock); | |
2108 | } | |
2109 | ||
2110 | void dm_unlock_md_type(struct mapped_device *md) | |
2111 | { | |
2112 | mutex_unlock(&md->type_lock); | |
2113 | } | |
2114 | ||
7e0d574f | 2115 | void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) |
a5664dad | 2116 | { |
00c4fc3b | 2117 | BUG_ON(!mutex_is_locked(&md->type_lock)); |
a5664dad MS |
2118 | md->type = type; |
2119 | } | |
2120 | ||
7e0d574f | 2121 | enum dm_queue_mode dm_get_md_type(struct mapped_device *md) |
a5664dad MS |
2122 | { |
2123 | return md->type; | |
2124 | } | |
2125 | ||
36a0456f AK |
2126 | struct target_type *dm_get_immutable_target_type(struct mapped_device *md) |
2127 | { | |
2128 | return md->immutable_target_type; | |
2129 | } | |
2130 | ||
f84cb8a4 MS |
2131 | /* |
2132 | * The queue_limits are only valid as long as you have a reference | |
2133 | * count on 'md'. | |
2134 | */ | |
2135 | struct queue_limits *dm_get_queue_limits(struct mapped_device *md) | |
2136 | { | |
2137 | BUG_ON(!atomic_read(&md->holders)); | |
2138 | return &md->queue->limits; | |
2139 | } | |
2140 | EXPORT_SYMBOL_GPL(dm_get_queue_limits); | |
2141 | ||
4a0b4ddf MS |
2142 | /* |
2143 | * Setup the DM device's queue based on md's type | |
2144 | */ | |
591ddcfc | 2145 | int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) |
4a0b4ddf | 2146 | { |
bfebd1cd | 2147 | int r; |
c100ec49 | 2148 | struct queue_limits limits; |
7e0d574f | 2149 | enum dm_queue_mode type = dm_get_md_type(md); |
bfebd1cd | 2150 | |
545ed20e | 2151 | switch (type) { |
bfebd1cd | 2152 | case DM_TYPE_REQUEST_BASED: |
681cc5e8 | 2153 | md->disk->fops = &dm_rq_blk_dops; |
e83068a5 | 2154 | r = dm_mq_init_request_queue(md, t); |
bfebd1cd | 2155 | if (r) { |
681cc5e8 | 2156 | DMERR("Cannot initialize queue for request-based dm mapped device"); |
bfebd1cd MS |
2157 | return r; |
2158 | } | |
2159 | break; | |
2160 | case DM_TYPE_BIO_BASED: | |
545ed20e | 2161 | case DM_TYPE_DAX_BIO_BASED: |
bfebd1cd | 2162 | break; |
7e0d574f BVA |
2163 | case DM_TYPE_NONE: |
2164 | WARN_ON_ONCE(true); | |
2165 | break; | |
4a0b4ddf MS |
2166 | } |
2167 | ||
c100ec49 MS |
2168 | r = dm_calculate_queue_limits(t, &limits); |
2169 | if (r) { | |
2170 | DMERR("Cannot calculate initial queue limits"); | |
2171 | return r; | |
2172 | } | |
2173 | dm_table_set_restrictions(t, md->queue, &limits); | |
2174 | blk_register_queue(md->disk); | |
2175 | ||
4a0b4ddf MS |
2176 | return 0; |
2177 | } | |
2178 | ||
2bec1f4a | 2179 | struct mapped_device *dm_get_md(dev_t dev) |
1da177e4 LT |
2180 | { |
2181 | struct mapped_device *md; | |
1da177e4 LT |
2182 | unsigned minor = MINOR(dev); |
2183 | ||
2184 | if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) | |
2185 | return NULL; | |
2186 | ||
f32c10b0 | 2187 | spin_lock(&_minor_lock); |
1da177e4 LT |
2188 | |
2189 | md = idr_find(&_minor_idr, minor); | |
49de5769 MS |
2190 | if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || |
2191 | test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { | |
2192 | md = NULL; | |
2193 | goto out; | |
fba9f90e | 2194 | } |
49de5769 | 2195 | dm_get(md); |
fba9f90e | 2196 | out: |
f32c10b0 | 2197 | spin_unlock(&_minor_lock); |
1da177e4 | 2198 | |
637842cf DT |
2199 | return md; |
2200 | } | |
3cf2e4ba | 2201 | EXPORT_SYMBOL_GPL(dm_get_md); |
d229a958 | 2202 | |
9ade92a9 | 2203 | void *dm_get_mdptr(struct mapped_device *md) |
637842cf | 2204 | { |
9ade92a9 | 2205 | return md->interface_ptr; |
1da177e4 LT |
2206 | } |
2207 | ||
2208 | void dm_set_mdptr(struct mapped_device *md, void *ptr) | |
2209 | { | |
2210 | md->interface_ptr = ptr; | |
2211 | } | |
2212 | ||
2213 | void dm_get(struct mapped_device *md) | |
2214 | { | |
2215 | atomic_inc(&md->holders); | |
3f77316d | 2216 | BUG_ON(test_bit(DMF_FREEING, &md->flags)); |
1da177e4 LT |
2217 | } |
2218 | ||
09ee96b2 MP |
2219 | int dm_hold(struct mapped_device *md) |
2220 | { | |
2221 | spin_lock(&_minor_lock); | |
2222 | if (test_bit(DMF_FREEING, &md->flags)) { | |
2223 | spin_unlock(&_minor_lock); | |
2224 | return -EBUSY; | |
2225 | } | |
2226 | dm_get(md); | |
2227 | spin_unlock(&_minor_lock); | |
2228 | return 0; | |
2229 | } | |
2230 | EXPORT_SYMBOL_GPL(dm_hold); | |
2231 | ||
72d94861 AK |
2232 | const char *dm_device_name(struct mapped_device *md) |
2233 | { | |
2234 | return md->name; | |
2235 | } | |
2236 | EXPORT_SYMBOL_GPL(dm_device_name); | |
2237 | ||
3f77316d | 2238 | static void __dm_destroy(struct mapped_device *md, bool wait) |
1da177e4 | 2239 | { |
1134e5ae | 2240 | struct dm_table *map; |
83d5e5b0 | 2241 | int srcu_idx; |
1da177e4 | 2242 | |
3f77316d | 2243 | might_sleep(); |
fba9f90e | 2244 | |
63a4f065 | 2245 | spin_lock(&_minor_lock); |
3f77316d KU |
2246 | idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); |
2247 | set_bit(DMF_FREEING, &md->flags); | |
2248 | spin_unlock(&_minor_lock); | |
3b785fbc | 2249 | |
c12c9a3c | 2250 | blk_set_queue_dying(md->queue); |
3f77316d | 2251 | |
ab7c7bb6 MP |
2252 | /* |
2253 | * Take suspend_lock so that presuspend and postsuspend methods | |
2254 | * do not race with internal suspend. | |
2255 | */ | |
2256 | mutex_lock(&md->suspend_lock); | |
2a708cff | 2257 | map = dm_get_live_table(md, &srcu_idx); |
3f77316d KU |
2258 | if (!dm_suspended_md(md)) { |
2259 | dm_table_presuspend_targets(map); | |
adc0daad | 2260 | set_bit(DMF_SUSPENDED, &md->flags); |
5df96f2b | 2261 | set_bit(DMF_POST_SUSPENDING, &md->flags); |
3f77316d | 2262 | dm_table_postsuspend_targets(map); |
1da177e4 | 2263 | } |
83d5e5b0 MP |
2264 | /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ |
2265 | dm_put_live_table(md, srcu_idx); | |
2a708cff | 2266 | mutex_unlock(&md->suspend_lock); |
83d5e5b0 | 2267 | |
3f77316d KU |
2268 | /* |
2269 | * Rare, but there may be I/O requests still going to complete, | |
2270 | * for example. Wait for all references to disappear. | |
2271 | * No one should increment the reference count of the mapped_device, | |
2272 | * after the mapped_device state becomes DMF_FREEING. | |
2273 | */ | |
2274 | if (wait) | |
2275 | while (atomic_read(&md->holders)) | |
2276 | msleep(1); | |
2277 | else if (atomic_read(&md->holders)) | |
2278 | DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", | |
2279 | dm_device_name(md), atomic_read(&md->holders)); | |
2280 | ||
2281 | dm_sysfs_exit(md); | |
3f77316d KU |
2282 | dm_table_destroy(__unbind(md)); |
2283 | free_dev(md); | |
2284 | } | |
2285 | ||
2286 | void dm_destroy(struct mapped_device *md) | |
2287 | { | |
2288 | __dm_destroy(md, true); | |
2289 | } | |
2290 | ||
2291 | void dm_destroy_immediate(struct mapped_device *md) | |
2292 | { | |
2293 | __dm_destroy(md, false); | |
2294 | } | |
2295 | ||
2296 | void dm_put(struct mapped_device *md) | |
2297 | { | |
2298 | atomic_dec(&md->holders); | |
1da177e4 | 2299 | } |
79eb885c | 2300 | EXPORT_SYMBOL_GPL(dm_put); |
1da177e4 | 2301 | |
85067747 ML |
2302 | static bool md_in_flight_bios(struct mapped_device *md) |
2303 | { | |
2304 | int cpu; | |
8446fe92 | 2305 | struct block_device *part = dm_disk(md)->part0; |
85067747 ML |
2306 | long sum = 0; |
2307 | ||
2308 | for_each_possible_cpu(cpu) { | |
2309 | sum += part_stat_local_read_cpu(part, in_flight[0], cpu); | |
2310 | sum += part_stat_local_read_cpu(part, in_flight[1], cpu); | |
2311 | } | |
2312 | ||
2313 | return sum != 0; | |
2314 | } | |
2315 | ||
2316 | static int dm_wait_for_bios_completion(struct mapped_device *md, long task_state) | |
46125c1c MB |
2317 | { |
2318 | int r = 0; | |
9f4c3f87 | 2319 | DEFINE_WAIT(wait); |
46125c1c | 2320 | |
85067747 | 2321 | while (true) { |
9f4c3f87 | 2322 | prepare_to_wait(&md->wait, &wait, task_state); |
46125c1c | 2323 | |
85067747 | 2324 | if (!md_in_flight_bios(md)) |
46125c1c MB |
2325 | break; |
2326 | ||
e3fabdfd | 2327 | if (signal_pending_state(task_state, current)) { |
46125c1c MB |
2328 | r = -EINTR; |
2329 | break; | |
2330 | } | |
2331 | ||
2332 | io_schedule(); | |
2333 | } | |
9f4c3f87 | 2334 | finish_wait(&md->wait, &wait); |
b44ebeb0 | 2335 | |
46125c1c MB |
2336 | return r; |
2337 | } | |
2338 | ||
85067747 ML |
2339 | static int dm_wait_for_completion(struct mapped_device *md, long task_state) |
2340 | { | |
2341 | int r = 0; | |
2342 | ||
2343 | if (!queue_is_mq(md->queue)) | |
2344 | return dm_wait_for_bios_completion(md, task_state); | |
2345 | ||
2346 | while (true) { | |
2347 | if (!blk_mq_queue_inflight(md->queue)) | |
2348 | break; | |
2349 | ||
2350 | if (signal_pending_state(task_state, current)) { | |
2351 | r = -EINTR; | |
2352 | break; | |
2353 | } | |
2354 | ||
2355 | msleep(5); | |
2356 | } | |
2357 | ||
2358 | return r; | |
2359 | } | |
2360 | ||
1da177e4 LT |
2361 | /* |
2362 | * Process the deferred bios | |
2363 | */ | |
ef208587 | 2364 | static void dm_wq_work(struct work_struct *work) |
1da177e4 | 2365 | { |
0c2915b8 MS |
2366 | struct mapped_device *md = container_of(work, struct mapped_device, work); |
2367 | struct bio *bio; | |
ef208587 | 2368 | |
3b00b203 | 2369 | while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { |
df12ee99 | 2370 | spin_lock_irq(&md->deferred_lock); |
0c2915b8 | 2371 | bio = bio_list_pop(&md->deferred); |
df12ee99 AK |
2372 | spin_unlock_irq(&md->deferred_lock); |
2373 | ||
0c2915b8 | 2374 | if (!bio) |
df12ee99 | 2375 | break; |
022c2611 | 2376 | |
0c2915b8 | 2377 | submit_bio_noacct(bio); |
022c2611 | 2378 | } |
1da177e4 LT |
2379 | } |
2380 | ||
9a1fb464 | 2381 | static void dm_queue_flush(struct mapped_device *md) |
304f3f6a | 2382 | { |
3b00b203 | 2383 | clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
4e857c58 | 2384 | smp_mb__after_atomic(); |
53d5914f | 2385 | queue_work(md->wq, &md->work); |
304f3f6a MB |
2386 | } |
2387 | ||
1da177e4 | 2388 | /* |
042d2a9b | 2389 | * Swap in a new table, returning the old one for the caller to destroy. |
1da177e4 | 2390 | */ |
042d2a9b | 2391 | struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) |
1da177e4 | 2392 | { |
87eb5b21 | 2393 | struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); |
754c5fc7 | 2394 | struct queue_limits limits; |
042d2a9b | 2395 | int r; |
1da177e4 | 2396 | |
e61290a4 | 2397 | mutex_lock(&md->suspend_lock); |
1da177e4 LT |
2398 | |
2399 | /* device must be suspended */ | |
4f186f8b | 2400 | if (!dm_suspended_md(md)) |
93c534ae | 2401 | goto out; |
1da177e4 | 2402 | |
3ae70656 MS |
2403 | /* |
2404 | * If the new table has no data devices, retain the existing limits. | |
2405 | * This helps multipath with queue_if_no_path if all paths disappear, | |
2406 | * then new I/O is queued based on these limits, and then some paths | |
2407 | * reappear. | |
2408 | */ | |
2409 | if (dm_table_has_no_data_devices(table)) { | |
83d5e5b0 | 2410 | live_map = dm_get_live_table_fast(md); |
3ae70656 MS |
2411 | if (live_map) |
2412 | limits = md->queue->limits; | |
83d5e5b0 | 2413 | dm_put_live_table_fast(md); |
3ae70656 MS |
2414 | } |
2415 | ||
87eb5b21 MC |
2416 | if (!live_map) { |
2417 | r = dm_calculate_queue_limits(table, &limits); | |
2418 | if (r) { | |
2419 | map = ERR_PTR(r); | |
2420 | goto out; | |
2421 | } | |
042d2a9b | 2422 | } |
754c5fc7 | 2423 | |
042d2a9b | 2424 | map = __bind(md, table, &limits); |
62e08243 | 2425 | dm_issue_global_event(); |
1da177e4 | 2426 | |
93c534ae | 2427 | out: |
e61290a4 | 2428 | mutex_unlock(&md->suspend_lock); |
042d2a9b | 2429 | return map; |
1da177e4 LT |
2430 | } |
2431 | ||
2432 | /* | |
2433 | * Functions to lock and unlock any filesystem running on the | |
2434 | * device. | |
2435 | */ | |
2ca3310e | 2436 | static int lock_fs(struct mapped_device *md) |
1da177e4 | 2437 | { |
e39e2e95 | 2438 | int r; |
1da177e4 | 2439 | |
040f04bd | 2440 | WARN_ON(test_bit(DMF_FROZEN, &md->flags)); |
aa8d7c2f | 2441 | |
977115c0 | 2442 | r = freeze_bdev(md->disk->part0); |
040f04bd CH |
2443 | if (!r) |
2444 | set_bit(DMF_FROZEN, &md->flags); | |
2445 | return r; | |
1da177e4 LT |
2446 | } |
2447 | ||
2ca3310e | 2448 | static void unlock_fs(struct mapped_device *md) |
1da177e4 | 2449 | { |
aa8d7c2f AK |
2450 | if (!test_bit(DMF_FROZEN, &md->flags)) |
2451 | return; | |
977115c0 | 2452 | thaw_bdev(md->disk->part0); |
aa8d7c2f | 2453 | clear_bit(DMF_FROZEN, &md->flags); |
1da177e4 LT |
2454 | } |
2455 | ||
2456 | /* | |
b48633f8 BVA |
2457 | * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG |
2458 | * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE | |
2459 | * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY | |
2460 | * | |
ffcc3936 MS |
2461 | * If __dm_suspend returns 0, the device is completely quiescent |
2462 | * now. There is no request-processing activity. All new requests | |
2463 | * are being added to md->deferred list. | |
cec47e3d | 2464 | */ |
ffcc3936 | 2465 | static int __dm_suspend(struct mapped_device *md, struct dm_table *map, |
b48633f8 | 2466 | unsigned suspend_flags, long task_state, |
eaf9a736 | 2467 | int dmf_suspended_flag) |
1da177e4 | 2468 | { |
ffcc3936 MS |
2469 | bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; |
2470 | bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; | |
2471 | int r; | |
1da177e4 | 2472 | |
5a8f1f80 BVA |
2473 | lockdep_assert_held(&md->suspend_lock); |
2474 | ||
2e93ccc1 KU |
2475 | /* |
2476 | * DMF_NOFLUSH_SUSPENDING must be set before presuspend. | |
2477 | * This flag is cleared before dm_suspend returns. | |
2478 | */ | |
2479 | if (noflush) | |
2480 | set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); | |
86331f39 | 2481 | else |
ac75b09f | 2482 | DMDEBUG("%s: suspending with flush", dm_device_name(md)); |
2e93ccc1 | 2483 | |
d67ee213 MS |
2484 | /* |
2485 | * This gets reverted if there's an error later and the targets | |
2486 | * provide the .presuspend_undo hook. | |
2487 | */ | |
cf222b37 AK |
2488 | dm_table_presuspend_targets(map); |
2489 | ||
32a926da | 2490 | /* |
9f518b27 KU |
2491 | * Flush I/O to the device. |
2492 | * Any I/O submitted after lock_fs() may not be flushed. | |
2493 | * noflush takes precedence over do_lockfs. | |
2494 | * (lock_fs() flushes I/Os and waits for them to complete.) | |
32a926da MP |
2495 | */ |
2496 | if (!noflush && do_lockfs) { | |
2497 | r = lock_fs(md); | |
d67ee213 MS |
2498 | if (r) { |
2499 | dm_table_presuspend_undo_targets(map); | |
ffcc3936 | 2500 | return r; |
d67ee213 | 2501 | } |
aa8d7c2f | 2502 | } |
1da177e4 LT |
2503 | |
2504 | /* | |
3b00b203 MP |
2505 | * Here we must make sure that no processes are submitting requests |
2506 | * to target drivers i.e. no one may be executing | |
0cede372 | 2507 | * __split_and_process_bio from dm_submit_bio. |
3b00b203 | 2508 | * |
0cede372 | 2509 | * To get all processes out of __split_and_process_bio in dm_submit_bio, |
3b00b203 | 2510 | * we take the write lock. To prevent any process from reentering |
0cede372 MS |
2511 | * __split_and_process_bio from dm_submit_bio and quiesce the thread |
2512 | * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call | |
6a8736d1 | 2513 | * flush_workqueue(md->wq). |
1da177e4 | 2514 | */ |
1eb787ec | 2515 | set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
41abc4e1 HR |
2516 | if (map) |
2517 | synchronize_srcu(&md->io_barrier); | |
1da177e4 | 2518 | |
d0bcb878 | 2519 | /* |
29e4013d TH |
2520 | * Stop md->queue before flushing md->wq in case request-based |
2521 | * dm defers requests to md->wq from md->queue. | |
d0bcb878 | 2522 | */ |
6a23e05c | 2523 | if (dm_request_based(md)) |
eca7ee6d | 2524 | dm_stop_queue(md->queue); |
cec47e3d | 2525 | |
d0bcb878 KU |
2526 | flush_workqueue(md->wq); |
2527 | ||
1da177e4 | 2528 | /* |
3b00b203 MP |
2529 | * At this point no more requests are entering target request routines. |
2530 | * We call dm_wait_for_completion to wait for all existing requests | |
2531 | * to finish. | |
1da177e4 | 2532 | */ |
b48633f8 | 2533 | r = dm_wait_for_completion(md, task_state); |
eaf9a736 MS |
2534 | if (!r) |
2535 | set_bit(dmf_suspended_flag, &md->flags); | |
1da177e4 | 2536 | |
6d6f10df | 2537 | if (noflush) |
022c2611 | 2538 | clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
41abc4e1 HR |
2539 | if (map) |
2540 | synchronize_srcu(&md->io_barrier); | |
2e93ccc1 | 2541 | |
1da177e4 | 2542 | /* were we interrupted ? */ |
46125c1c | 2543 | if (r < 0) { |
9a1fb464 | 2544 | dm_queue_flush(md); |
73d410c0 | 2545 | |
cec47e3d | 2546 | if (dm_request_based(md)) |
eca7ee6d | 2547 | dm_start_queue(md->queue); |
cec47e3d | 2548 | |
2ca3310e | 2549 | unlock_fs(md); |
d67ee213 | 2550 | dm_table_presuspend_undo_targets(map); |
ffcc3936 | 2551 | /* pushback list is already flushed, so skip flush */ |
2ca3310e | 2552 | } |
1da177e4 | 2553 | |
ffcc3936 MS |
2554 | return r; |
2555 | } | |
2556 | ||
2557 | /* | |
2558 | * We need to be able to change a mapping table under a mounted | |
2559 | * filesystem. For example we might want to move some data in | |
2560 | * the background. Before the table can be swapped with | |
2561 | * dm_bind_table, dm_suspend must be called to flush any in | |
2562 | * flight bios and ensure that any further io gets deferred. | |
2563 | */ | |
2564 | /* | |
2565 | * Suspend mechanism in request-based dm. | |
2566 | * | |
2567 | * 1. Flush all I/Os by lock_fs() if needed. | |
2568 | * 2. Stop dispatching any I/O by stopping the request_queue. | |
2569 | * 3. Wait for all in-flight I/Os to be completed or requeued. | |
2570 | * | |
2571 | * To abort suspend, start the request_queue. | |
2572 | */ | |
2573 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags) | |
2574 | { | |
2575 | struct dm_table *map = NULL; | |
2576 | int r = 0; | |
2577 | ||
2578 | retry: | |
2579 | mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); | |
2580 | ||
2581 | if (dm_suspended_md(md)) { | |
2582 | r = -EINVAL; | |
2583 | goto out_unlock; | |
2584 | } | |
2585 | ||
2586 | if (dm_suspended_internally_md(md)) { | |
2587 | /* already internally suspended, wait for internal resume */ | |
2588 | mutex_unlock(&md->suspend_lock); | |
2589 | r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); | |
2590 | if (r) | |
2591 | return r; | |
2592 | goto retry; | |
2593 | } | |
2594 | ||
a12f5d48 | 2595 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
ffcc3936 | 2596 | |
eaf9a736 | 2597 | r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); |
ffcc3936 MS |
2598 | if (r) |
2599 | goto out_unlock; | |
3b00b203 | 2600 | |
5df96f2b | 2601 | set_bit(DMF_POST_SUSPENDING, &md->flags); |
4d4471cb | 2602 | dm_table_postsuspend_targets(map); |
5df96f2b | 2603 | clear_bit(DMF_POST_SUSPENDING, &md->flags); |
4d4471cb | 2604 | |
d287483d | 2605 | out_unlock: |
e61290a4 | 2606 | mutex_unlock(&md->suspend_lock); |
cf222b37 | 2607 | return r; |
1da177e4 LT |
2608 | } |
2609 | ||
ffcc3936 MS |
2610 | static int __dm_resume(struct mapped_device *md, struct dm_table *map) |
2611 | { | |
2612 | if (map) { | |
2613 | int r = dm_table_resume_targets(map); | |
2614 | if (r) | |
2615 | return r; | |
2616 | } | |
2617 | ||
2618 | dm_queue_flush(md); | |
2619 | ||
2620 | /* | |
2621 | * Flushing deferred I/Os must be done after targets are resumed | |
2622 | * so that mapping of targets can work correctly. | |
2623 | * Request-based dm is queueing the deferred I/Os in its request_queue. | |
2624 | */ | |
2625 | if (dm_request_based(md)) | |
eca7ee6d | 2626 | dm_start_queue(md->queue); |
ffcc3936 MS |
2627 | |
2628 | unlock_fs(md); | |
2629 | ||
2630 | return 0; | |
2631 | } | |
2632 | ||
1da177e4 LT |
2633 | int dm_resume(struct mapped_device *md) |
2634 | { | |
8dc23658 | 2635 | int r; |
cf222b37 | 2636 | struct dm_table *map = NULL; |
1da177e4 | 2637 | |
ffcc3936 | 2638 | retry: |
8dc23658 | 2639 | r = -EINVAL; |
ffcc3936 MS |
2640 | mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); |
2641 | ||
4f186f8b | 2642 | if (!dm_suspended_md(md)) |
cf222b37 | 2643 | goto out; |
cf222b37 | 2644 | |
ffcc3936 MS |
2645 | if (dm_suspended_internally_md(md)) { |
2646 | /* already internally suspended, wait for internal resume */ | |
2647 | mutex_unlock(&md->suspend_lock); | |
2648 | r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); | |
2649 | if (r) | |
2650 | return r; | |
2651 | goto retry; | |
2652 | } | |
2653 | ||
a12f5d48 | 2654 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
2ca3310e | 2655 | if (!map || !dm_table_get_size(map)) |
cf222b37 | 2656 | goto out; |
1da177e4 | 2657 | |
ffcc3936 | 2658 | r = __dm_resume(md, map); |
8757b776 MB |
2659 | if (r) |
2660 | goto out; | |
2ca3310e | 2661 | |
2ca3310e | 2662 | clear_bit(DMF_SUSPENDED, &md->flags); |
cf222b37 | 2663 | out: |
e61290a4 | 2664 | mutex_unlock(&md->suspend_lock); |
2ca3310e | 2665 | |
cf222b37 | 2666 | return r; |
1da177e4 LT |
2667 | } |
2668 | ||
fd2ed4d2 MP |
2669 | /* |
2670 | * Internal suspend/resume works like userspace-driven suspend. It waits | |
2671 | * until all bios finish and prevents issuing new bios to the target drivers. | |
2672 | * It may be used only from the kernel. | |
fd2ed4d2 MP |
2673 | */ |
2674 | ||
ffcc3936 | 2675 | static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) |
fd2ed4d2 | 2676 | { |
ffcc3936 MS |
2677 | struct dm_table *map = NULL; |
2678 | ||
1ea0654e BVA |
2679 | lockdep_assert_held(&md->suspend_lock); |
2680 | ||
96b26c8c | 2681 | if (md->internal_suspend_count++) |
ffcc3936 MS |
2682 | return; /* nested internal suspend */ |
2683 | ||
2684 | if (dm_suspended_md(md)) { | |
2685 | set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); | |
2686 | return; /* nest suspend */ | |
2687 | } | |
2688 | ||
a12f5d48 | 2689 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
ffcc3936 MS |
2690 | |
2691 | /* | |
2692 | * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is | |
2693 | * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend | |
2694 | * would require changing .presuspend to return an error -- avoid this | |
2695 | * until there is a need for more elaborate variants of internal suspend. | |
2696 | */ | |
eaf9a736 MS |
2697 | (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, |
2698 | DMF_SUSPENDED_INTERNALLY); | |
ffcc3936 | 2699 | |
5df96f2b | 2700 | set_bit(DMF_POST_SUSPENDING, &md->flags); |
ffcc3936 | 2701 | dm_table_postsuspend_targets(map); |
5df96f2b | 2702 | clear_bit(DMF_POST_SUSPENDING, &md->flags); |
ffcc3936 MS |
2703 | } |
2704 | ||
2705 | static void __dm_internal_resume(struct mapped_device *md) | |
2706 | { | |
96b26c8c MP |
2707 | BUG_ON(!md->internal_suspend_count); |
2708 | ||
2709 | if (--md->internal_suspend_count) | |
ffcc3936 MS |
2710 | return; /* resume from nested internal suspend */ |
2711 | ||
fd2ed4d2 | 2712 | if (dm_suspended_md(md)) |
ffcc3936 MS |
2713 | goto done; /* resume from nested suspend */ |
2714 | ||
2715 | /* | |
2716 | * NOTE: existing callers don't need to call dm_table_resume_targets | |
2717 | * (which may fail -- so best to avoid it for now by passing NULL map) | |
2718 | */ | |
2719 | (void) __dm_resume(md, NULL); | |
2720 | ||
2721 | done: | |
2722 | clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); | |
2723 | smp_mb__after_atomic(); | |
2724 | wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); | |
2725 | } | |
2726 | ||
2727 | void dm_internal_suspend_noflush(struct mapped_device *md) | |
2728 | { | |
2729 | mutex_lock(&md->suspend_lock); | |
2730 | __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); | |
2731 | mutex_unlock(&md->suspend_lock); | |
2732 | } | |
2733 | EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); | |
2734 | ||
2735 | void dm_internal_resume(struct mapped_device *md) | |
2736 | { | |
2737 | mutex_lock(&md->suspend_lock); | |
2738 | __dm_internal_resume(md); | |
2739 | mutex_unlock(&md->suspend_lock); | |
2740 | } | |
2741 | EXPORT_SYMBOL_GPL(dm_internal_resume); | |
2742 | ||
2743 | /* | |
2744 | * Fast variants of internal suspend/resume hold md->suspend_lock, | |
2745 | * which prevents interaction with userspace-driven suspend. | |
2746 | */ | |
2747 | ||
2748 | void dm_internal_suspend_fast(struct mapped_device *md) | |
2749 | { | |
2750 | mutex_lock(&md->suspend_lock); | |
2751 | if (dm_suspended_md(md) || dm_suspended_internally_md(md)) | |
fd2ed4d2 MP |
2752 | return; |
2753 | ||
2754 | set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); | |
2755 | synchronize_srcu(&md->io_barrier); | |
2756 | flush_workqueue(md->wq); | |
2757 | dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); | |
2758 | } | |
b735fede | 2759 | EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); |
fd2ed4d2 | 2760 | |
ffcc3936 | 2761 | void dm_internal_resume_fast(struct mapped_device *md) |
fd2ed4d2 | 2762 | { |
ffcc3936 | 2763 | if (dm_suspended_md(md) || dm_suspended_internally_md(md)) |
fd2ed4d2 MP |
2764 | goto done; |
2765 | ||
2766 | dm_queue_flush(md); | |
2767 | ||
2768 | done: | |
2769 | mutex_unlock(&md->suspend_lock); | |
2770 | } | |
b735fede | 2771 | EXPORT_SYMBOL_GPL(dm_internal_resume_fast); |
fd2ed4d2 | 2772 | |
1da177e4 LT |
2773 | /*----------------------------------------------------------------- |
2774 | * Event notification. | |
2775 | *---------------------------------------------------------------*/ | |
3abf85b5 | 2776 | int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, |
60935eb2 | 2777 | unsigned cookie) |
69267a30 | 2778 | { |
6958c1c6 MP |
2779 | int r; |
2780 | unsigned noio_flag; | |
60935eb2 MB |
2781 | char udev_cookie[DM_COOKIE_LENGTH]; |
2782 | char *envp[] = { udev_cookie, NULL }; | |
2783 | ||
6958c1c6 MP |
2784 | noio_flag = memalloc_noio_save(); |
2785 | ||
60935eb2 | 2786 | if (!cookie) |
6958c1c6 | 2787 | r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action); |
60935eb2 MB |
2788 | else { |
2789 | snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", | |
2790 | DM_COOKIE_ENV_VAR_NAME, cookie); | |
6958c1c6 MP |
2791 | r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, |
2792 | action, envp); | |
60935eb2 | 2793 | } |
6958c1c6 MP |
2794 | |
2795 | memalloc_noio_restore(noio_flag); | |
2796 | ||
2797 | return r; | |
69267a30 AK |
2798 | } |
2799 | ||
7a8c3d3b MA |
2800 | uint32_t dm_next_uevent_seq(struct mapped_device *md) |
2801 | { | |
2802 | return atomic_add_return(1, &md->uevent_seq); | |
2803 | } | |
2804 | ||
1da177e4 LT |
2805 | uint32_t dm_get_event_nr(struct mapped_device *md) |
2806 | { | |
2807 | return atomic_read(&md->event_nr); | |
2808 | } | |
2809 | ||
2810 | int dm_wait_event(struct mapped_device *md, int event_nr) | |
2811 | { | |
2812 | return wait_event_interruptible(md->eventq, | |
2813 | (event_nr != atomic_read(&md->event_nr))); | |
2814 | } | |
2815 | ||
7a8c3d3b MA |
2816 | void dm_uevent_add(struct mapped_device *md, struct list_head *elist) |
2817 | { | |
2818 | unsigned long flags; | |
2819 | ||
2820 | spin_lock_irqsave(&md->uevent_lock, flags); | |
2821 | list_add(elist, &md->uevent_list); | |
2822 | spin_unlock_irqrestore(&md->uevent_lock, flags); | |
2823 | } | |
2824 | ||
1da177e4 LT |
2825 | /* |
2826 | * The gendisk is only valid as long as you have a reference | |
2827 | * count on 'md'. | |
2828 | */ | |
2829 | struct gendisk *dm_disk(struct mapped_device *md) | |
2830 | { | |
2831 | return md->disk; | |
2832 | } | |
65ff5b7d | 2833 | EXPORT_SYMBOL_GPL(dm_disk); |
1da177e4 | 2834 | |
784aae73 MB |
2835 | struct kobject *dm_kobject(struct mapped_device *md) |
2836 | { | |
2995fa78 | 2837 | return &md->kobj_holder.kobj; |
784aae73 MB |
2838 | } |
2839 | ||
784aae73 MB |
2840 | struct mapped_device *dm_get_from_kobject(struct kobject *kobj) |
2841 | { | |
2842 | struct mapped_device *md; | |
2843 | ||
2995fa78 | 2844 | md = container_of(kobj, struct mapped_device, kobj_holder.kobj); |
784aae73 | 2845 | |
b9a41d21 HT |
2846 | spin_lock(&_minor_lock); |
2847 | if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { | |
2848 | md = NULL; | |
2849 | goto out; | |
2850 | } | |
784aae73 | 2851 | dm_get(md); |
b9a41d21 HT |
2852 | out: |
2853 | spin_unlock(&_minor_lock); | |
2854 | ||
784aae73 MB |
2855 | return md; |
2856 | } | |
2857 | ||
4f186f8b | 2858 | int dm_suspended_md(struct mapped_device *md) |
1da177e4 LT |
2859 | { |
2860 | return test_bit(DMF_SUSPENDED, &md->flags); | |
2861 | } | |
2862 | ||
5df96f2b MP |
2863 | static int dm_post_suspending_md(struct mapped_device *md) |
2864 | { | |
2865 | return test_bit(DMF_POST_SUSPENDING, &md->flags); | |
2866 | } | |
2867 | ||
ffcc3936 MS |
2868 | int dm_suspended_internally_md(struct mapped_device *md) |
2869 | { | |
2870 | return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); | |
2871 | } | |
2872 | ||
2c140a24 MP |
2873 | int dm_test_deferred_remove_flag(struct mapped_device *md) |
2874 | { | |
2875 | return test_bit(DMF_DEFERRED_REMOVE, &md->flags); | |
2876 | } | |
2877 | ||
64dbce58 KU |
2878 | int dm_suspended(struct dm_target *ti) |
2879 | { | |
33bd6f06 | 2880 | return dm_suspended_md(ti->table->md); |
64dbce58 KU |
2881 | } |
2882 | EXPORT_SYMBOL_GPL(dm_suspended); | |
2883 | ||
5df96f2b MP |
2884 | int dm_post_suspending(struct dm_target *ti) |
2885 | { | |
33bd6f06 | 2886 | return dm_post_suspending_md(ti->table->md); |
5df96f2b MP |
2887 | } |
2888 | EXPORT_SYMBOL_GPL(dm_post_suspending); | |
2889 | ||
2e93ccc1 KU |
2890 | int dm_noflush_suspending(struct dm_target *ti) |
2891 | { | |
33bd6f06 | 2892 | return __noflush_suspending(ti->table->md); |
2e93ccc1 KU |
2893 | } |
2894 | EXPORT_SYMBOL_GPL(dm_noflush_suspending); | |
2895 | ||
7e0d574f | 2896 | struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, |
0776aa0e MS |
2897 | unsigned integrity, unsigned per_io_data_size, |
2898 | unsigned min_pool_size) | |
e6ee8c0b | 2899 | { |
115485e8 | 2900 | struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); |
78d8e58a | 2901 | unsigned int pool_size = 0; |
64f52b0e | 2902 | unsigned int front_pad, io_front_pad; |
6f1c819c | 2903 | int ret; |
e6ee8c0b KU |
2904 | |
2905 | if (!pools) | |
4e6e36c3 | 2906 | return NULL; |
e6ee8c0b | 2907 | |
78d8e58a MS |
2908 | switch (type) { |
2909 | case DM_TYPE_BIO_BASED: | |
545ed20e | 2910 | case DM_TYPE_DAX_BIO_BASED: |
0776aa0e | 2911 | pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); |
30187e1d | 2912 | front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone); |
64f52b0e | 2913 | io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio); |
6f1c819c KO |
2914 | ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0); |
2915 | if (ret) | |
64f52b0e | 2916 | goto out; |
6f1c819c | 2917 | if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) |
eb8db831 | 2918 | goto out; |
78d8e58a MS |
2919 | break; |
2920 | case DM_TYPE_REQUEST_BASED: | |
0776aa0e | 2921 | pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); |
78d8e58a | 2922 | front_pad = offsetof(struct dm_rq_clone_bio_info, clone); |
591ddcfc | 2923 | /* per_io_data_size is used for blk-mq pdu at queue allocation */ |
78d8e58a MS |
2924 | break; |
2925 | default: | |
2926 | BUG(); | |
2927 | } | |
2928 | ||
6f1c819c KO |
2929 | ret = bioset_init(&pools->bs, pool_size, front_pad, 0); |
2930 | if (ret) | |
5f015204 | 2931 | goto out; |
e6ee8c0b | 2932 | |
6f1c819c | 2933 | if (integrity && bioset_integrity_create(&pools->bs, pool_size)) |
5f015204 | 2934 | goto out; |
a91a2785 | 2935 | |
e6ee8c0b | 2936 | return pools; |
5f1b670d | 2937 | |
5f1b670d CH |
2938 | out: |
2939 | dm_free_md_mempools(pools); | |
78d8e58a | 2940 | |
4e6e36c3 | 2941 | return NULL; |
e6ee8c0b KU |
2942 | } |
2943 | ||
2944 | void dm_free_md_mempools(struct dm_md_mempools *pools) | |
2945 | { | |
2946 | if (!pools) | |
2947 | return; | |
2948 | ||
6f1c819c KO |
2949 | bioset_exit(&pools->bs); |
2950 | bioset_exit(&pools->io_bs); | |
e6ee8c0b KU |
2951 | |
2952 | kfree(pools); | |
2953 | } | |
2954 | ||
9c72bad1 CH |
2955 | struct dm_pr { |
2956 | u64 old_key; | |
2957 | u64 new_key; | |
2958 | u32 flags; | |
2959 | bool fail_early; | |
2960 | }; | |
2961 | ||
2962 | static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, | |
2963 | void *data) | |
71cdb697 CH |
2964 | { |
2965 | struct mapped_device *md = bdev->bd_disk->private_data; | |
9c72bad1 CH |
2966 | struct dm_table *table; |
2967 | struct dm_target *ti; | |
2968 | int ret = -ENOTTY, srcu_idx; | |
71cdb697 | 2969 | |
9c72bad1 CH |
2970 | table = dm_get_live_table(md, &srcu_idx); |
2971 | if (!table || !dm_table_get_size(table)) | |
2972 | goto out; | |
71cdb697 | 2973 | |
9c72bad1 CH |
2974 | /* We only support devices that have a single target */ |
2975 | if (dm_table_get_num_targets(table) != 1) | |
2976 | goto out; | |
2977 | ti = dm_table_get_target(table, 0); | |
71cdb697 | 2978 | |
9c72bad1 CH |
2979 | ret = -EINVAL; |
2980 | if (!ti->type->iterate_devices) | |
2981 | goto out; | |
2982 | ||
2983 | ret = ti->type->iterate_devices(ti, fn, data); | |
2984 | out: | |
2985 | dm_put_live_table(md, srcu_idx); | |
2986 | return ret; | |
2987 | } | |
2988 | ||
2989 | /* | |
2990 | * For register / unregister we need to manually call out to every path. | |
2991 | */ | |
2992 | static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, | |
2993 | sector_t start, sector_t len, void *data) | |
2994 | { | |
2995 | struct dm_pr *pr = data; | |
2996 | const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; | |
2997 | ||
2998 | if (!ops || !ops->pr_register) | |
2999 | return -EOPNOTSUPP; | |
3000 | return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); | |
3001 | } | |
3002 | ||
3003 | static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, | |
3004 | u32 flags) | |
3005 | { | |
3006 | struct dm_pr pr = { | |
3007 | .old_key = old_key, | |
3008 | .new_key = new_key, | |
3009 | .flags = flags, | |
3010 | .fail_early = true, | |
3011 | }; | |
3012 | int ret; | |
3013 | ||
3014 | ret = dm_call_pr(bdev, __dm_pr_register, &pr); | |
3015 | if (ret && new_key) { | |
3016 | /* unregister all paths if we failed to register any path */ | |
3017 | pr.old_key = new_key; | |
3018 | pr.new_key = 0; | |
3019 | pr.flags = 0; | |
3020 | pr.fail_early = false; | |
3021 | dm_call_pr(bdev, __dm_pr_register, &pr); | |
3022 | } | |
3023 | ||
3024 | return ret; | |
71cdb697 CH |
3025 | } |
3026 | ||
3027 | static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, | |
956a4025 | 3028 | u32 flags) |
71cdb697 CH |
3029 | { |
3030 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3031 | const struct pr_ops *ops; | |
971888c4 | 3032 | int r, srcu_idx; |
71cdb697 | 3033 | |
5bd5e8d8 | 3034 | r = dm_prepare_ioctl(md, &srcu_idx, &bdev); |
71cdb697 | 3035 | if (r < 0) |
971888c4 | 3036 | goto out; |
71cdb697 CH |
3037 | |
3038 | ops = bdev->bd_disk->fops->pr_ops; | |
3039 | if (ops && ops->pr_reserve) | |
3040 | r = ops->pr_reserve(bdev, key, type, flags); | |
3041 | else | |
3042 | r = -EOPNOTSUPP; | |
971888c4 MS |
3043 | out: |
3044 | dm_unprepare_ioctl(md, srcu_idx); | |
71cdb697 CH |
3045 | return r; |
3046 | } | |
3047 | ||
3048 | static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) | |
3049 | { | |
3050 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3051 | const struct pr_ops *ops; | |
971888c4 | 3052 | int r, srcu_idx; |
71cdb697 | 3053 | |
5bd5e8d8 | 3054 | r = dm_prepare_ioctl(md, &srcu_idx, &bdev); |
71cdb697 | 3055 | if (r < 0) |
971888c4 | 3056 | goto out; |
71cdb697 CH |
3057 | |
3058 | ops = bdev->bd_disk->fops->pr_ops; | |
3059 | if (ops && ops->pr_release) | |
3060 | r = ops->pr_release(bdev, key, type); | |
3061 | else | |
3062 | r = -EOPNOTSUPP; | |
971888c4 MS |
3063 | out: |
3064 | dm_unprepare_ioctl(md, srcu_idx); | |
71cdb697 CH |
3065 | return r; |
3066 | } | |
3067 | ||
3068 | static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, | |
956a4025 | 3069 | enum pr_type type, bool abort) |
71cdb697 CH |
3070 | { |
3071 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3072 | const struct pr_ops *ops; | |
971888c4 | 3073 | int r, srcu_idx; |
71cdb697 | 3074 | |
5bd5e8d8 | 3075 | r = dm_prepare_ioctl(md, &srcu_idx, &bdev); |
71cdb697 | 3076 | if (r < 0) |
971888c4 | 3077 | goto out; |
71cdb697 CH |
3078 | |
3079 | ops = bdev->bd_disk->fops->pr_ops; | |
3080 | if (ops && ops->pr_preempt) | |
3081 | r = ops->pr_preempt(bdev, old_key, new_key, type, abort); | |
3082 | else | |
3083 | r = -EOPNOTSUPP; | |
971888c4 MS |
3084 | out: |
3085 | dm_unprepare_ioctl(md, srcu_idx); | |
71cdb697 CH |
3086 | return r; |
3087 | } | |
3088 | ||
3089 | static int dm_pr_clear(struct block_device *bdev, u64 key) | |
3090 | { | |
3091 | struct mapped_device *md = bdev->bd_disk->private_data; | |
3092 | const struct pr_ops *ops; | |
971888c4 | 3093 | int r, srcu_idx; |
71cdb697 | 3094 | |
5bd5e8d8 | 3095 | r = dm_prepare_ioctl(md, &srcu_idx, &bdev); |
71cdb697 | 3096 | if (r < 0) |
971888c4 | 3097 | goto out; |
71cdb697 CH |
3098 | |
3099 | ops = bdev->bd_disk->fops->pr_ops; | |
3100 | if (ops && ops->pr_clear) | |
3101 | r = ops->pr_clear(bdev, key); | |
3102 | else | |
3103 | r = -EOPNOTSUPP; | |
971888c4 MS |
3104 | out: |
3105 | dm_unprepare_ioctl(md, srcu_idx); | |
71cdb697 CH |
3106 | return r; |
3107 | } | |
3108 | ||
3109 | static const struct pr_ops dm_pr_ops = { | |
3110 | .pr_register = dm_pr_register, | |
3111 | .pr_reserve = dm_pr_reserve, | |
3112 | .pr_release = dm_pr_release, | |
3113 | .pr_preempt = dm_pr_preempt, | |
3114 | .pr_clear = dm_pr_clear, | |
3115 | }; | |
3116 | ||
83d5cde4 | 3117 | static const struct block_device_operations dm_blk_dops = { |
c62b37d9 | 3118 | .submit_bio = dm_submit_bio, |
1da177e4 LT |
3119 | .open = dm_blk_open, |
3120 | .release = dm_blk_close, | |
aa129a22 | 3121 | .ioctl = dm_blk_ioctl, |
3ac51e74 | 3122 | .getgeo = dm_blk_getgeo, |
e76239a3 | 3123 | .report_zones = dm_blk_report_zones, |
71cdb697 | 3124 | .pr_ops = &dm_pr_ops, |
1da177e4 LT |
3125 | .owner = THIS_MODULE |
3126 | }; | |
3127 | ||
681cc5e8 MS |
3128 | static const struct block_device_operations dm_rq_blk_dops = { |
3129 | .open = dm_blk_open, | |
3130 | .release = dm_blk_close, | |
3131 | .ioctl = dm_blk_ioctl, | |
3132 | .getgeo = dm_blk_getgeo, | |
3133 | .pr_ops = &dm_pr_ops, | |
3134 | .owner = THIS_MODULE | |
3135 | }; | |
3136 | ||
f26c5719 DW |
3137 | static const struct dax_operations dm_dax_ops = { |
3138 | .direct_access = dm_dax_direct_access, | |
7bf7eac8 | 3139 | .dax_supported = dm_dax_supported, |
7e026c8c | 3140 | .copy_from_iter = dm_dax_copy_from_iter, |
b3a9a0c3 | 3141 | .copy_to_iter = dm_dax_copy_to_iter, |
cdf6cdcd | 3142 | .zero_page_range = dm_dax_zero_page_range, |
f26c5719 DW |
3143 | }; |
3144 | ||
1da177e4 LT |
3145 | /* |
3146 | * module hooks | |
3147 | */ | |
3148 | module_init(dm_init); | |
3149 | module_exit(dm_exit); | |
3150 | ||
3151 | module_param(major, uint, 0); | |
3152 | MODULE_PARM_DESC(major, "The major number of the device mapper"); | |
f4790826 | 3153 | |
e8603136 MS |
3154 | module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); |
3155 | MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); | |
3156 | ||
115485e8 MS |
3157 | module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); |
3158 | MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); | |
3159 | ||
bffdde6f MP |
3160 | module_param(swap_bios, int, S_IRUGO | S_IWUSR); |
3161 | MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs"); | |
3162 | ||
1da177e4 LT |
3163 | MODULE_DESCRIPTION(DM_NAME " driver"); |
3164 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); | |
3165 | MODULE_LICENSE("GPL"); |