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