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