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