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