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