blob: 4cc7e2599664c54ab040d763095b517eef21b551 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
Mike Snitzer4cc96132016-05-12 16:28:10 -04008#include "dm-core.h"
9#include "dm-rq.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +010010#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <linux/init.h>
13#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080014#include <linux/mutex.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/blkpg.h>
17#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mempool.h>
Dan Williamsf26c5712017-04-12 12:35:44 -070019#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/idr.h>
Dan Williams7e026c82017-05-29 12:57:56 -070022#include <linux/uio.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080023#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010024#include <linux/delay.h>
Mike Snitzerffcc3932014-10-28 18:34:52 -040025#include <linux/wait.h>
Christoph Hellwig71cdb692015-10-15 14:10:51 +020026#include <linux/pr.h>
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +030027#include <linux/refcount.h>
Christoph Hellwigc6a564ff2020-03-25 16:48:42 +010028#include <linux/part_stat.h>
Satya Tangiralaa892c8d2020-05-14 00:37:18 +000029#include <linux/blk-crypto.h>
Li Zefan55782132009-06-09 13:43:05 +080030
Alasdair G Kergon72d94862006-06-26 00:27:35 -070031#define DM_MSG_PREFIX "core"
32
Milan Broz60935eb2009-06-22 10:12:30 +010033/*
34 * Cookies are numeric values sent with CHANGE and REMOVE
35 * uevents while resuming, removing or renaming the device.
36 */
37#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
38#define DM_COOKIE_LENGTH 24
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static const char *_name = DM_NAME;
41
42static unsigned int major = 0;
43static unsigned int _major = 0;
44
Alasdair G Kergond15b7742011-08-02 12:32:01 +010045static DEFINE_IDR(_minor_idr);
46
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070047static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040048
49static void do_deferred_remove(struct work_struct *w);
50
51static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
52
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040053static struct workqueue_struct *deferred_remove_workqueue;
54
Mikulas Patocka93e64422017-01-16 16:05:59 -050055atomic_t dm_global_event_nr = ATOMIC_INIT(0);
56DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
57
Mikulas Patocka62e08242017-09-20 07:29:49 -040058void dm_issue_global_event(void)
59{
60 atomic_inc(&dm_global_event_nr);
61 wake_up(&dm_global_eventq);
62}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
Mike Snitzer64f52b02017-12-11 23:17:47 -050065 * One of these is allocated (on-stack) per original bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
Mike Snitzer64f52b02017-12-11 23:17:47 -050067struct clone_info {
Mike Snitzer64f52b02017-12-11 23:17:47 -050068 struct dm_table *map;
69 struct bio *bio;
70 struct dm_io *io;
71 sector_t sector;
72 unsigned sector_count;
73};
74
75/*
76 * One of these is allocated per clone bio.
77 */
78#define DM_TIO_MAGIC 7282014
79struct dm_target_io {
80 unsigned magic;
81 struct dm_io *io;
82 struct dm_target *ti;
83 unsigned target_bio_nr;
84 unsigned *len_ptr;
85 bool inside_dm_io;
86 struct bio clone;
87};
88
89/*
90 * One of these is allocated per original bio.
91 * It contains the first clone used for that original.
92 */
93#define DM_IO_MAGIC 5191977
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct dm_io {
Mike Snitzer64f52b02017-12-11 23:17:47 -050095 unsigned magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct mapped_device *md;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020097 blk_status_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 atomic_t io_count;
Mike Snitzer745dc572017-12-11 20:51:50 -050099 struct bio *orig_bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800100 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100101 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400102 struct dm_stats_aux stats_aux;
Mike Snitzer64f52b02017-12-11 23:17:47 -0500103 /* last member of dm_target_io is 'struct bio' */
104 struct dm_target_io tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Mike Snitzer64f52b02017-12-11 23:17:47 -0500107void *dm_per_bio_data(struct bio *bio, size_t data_size)
108{
109 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
110 if (!tio->inside_dm_io)
111 return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
112 return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size;
113}
114EXPORT_SYMBOL_GPL(dm_per_bio_data);
115
116struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
117{
118 struct dm_io *io = (struct dm_io *)((char *)data + data_size);
119 if (io->magic == DM_IO_MAGIC)
120 return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone));
121 BUG_ON(io->magic != DM_TIO_MAGIC);
122 return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone));
123}
124EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
125
126unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
127{
128 return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
129}
130EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
131
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700132#define MINOR_ALLOCED ((void *)-1)
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * Bits for the md->flags field.
136 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100137#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800139#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700140#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700141#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800142#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -0700143#define DMF_DEFERRED_REMOVE 6
144#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Mike Snitzer115485e2016-02-22 12:16:21 -0500146#define DM_NUMA_NODE NUMA_NO_NODE
Mike Snitzer115485e2016-02-22 12:16:21 -0500147static int dm_numa_node = DM_NUMA_NODE;
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500148
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100149/*
150 * For mempools pre-allocation at the table loading time.
151 */
152struct dm_md_mempools {
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400153 struct bio_set bs;
154 struct bio_set io_bs;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100155};
156
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500157struct table_device {
158 struct list_head list;
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300159 refcount_t count;
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500160 struct dm_dev dm_dev;
161};
162
Mike Snitzerf4790822013-09-12 18:06:12 -0400163/*
Mike Snitzere8603132013-09-12 18:06:12 -0400164 * Bio-based DM's mempools' reserved IOs set by the user.
165 */
Mike Snitzer4cc96132016-05-12 16:28:10 -0400166#define RESERVED_BIO_BASED_IOS 16
Mike Snitzere8603132013-09-12 18:06:12 -0400167static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
168
Mike Snitzer115485e2016-02-22 12:16:21 -0500169static int __dm_get_module_param_int(int *module_param, int min, int max)
170{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700171 int param = READ_ONCE(*module_param);
Mike Snitzer115485e2016-02-22 12:16:21 -0500172 int modified_param = 0;
173 bool modified = true;
174
175 if (param < min)
176 modified_param = min;
177 else if (param > max)
178 modified_param = max;
179 else
180 modified = false;
181
182 if (modified) {
183 (void)cmpxchg(module_param, param, modified_param);
184 param = modified_param;
185 }
186
187 return param;
188}
189
Mike Snitzer4cc96132016-05-12 16:28:10 -0400190unsigned __dm_get_module_param(unsigned *module_param,
191 unsigned def, unsigned max)
Mike Snitzerf4790822013-09-12 18:06:12 -0400192{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700193 unsigned param = READ_ONCE(*module_param);
Mike Snitzer09c2d532015-02-27 22:25:26 -0500194 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400195
Mike Snitzer09c2d532015-02-27 22:25:26 -0500196 if (!param)
197 modified_param = def;
198 else if (param > max)
199 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400200
Mike Snitzer09c2d532015-02-27 22:25:26 -0500201 if (modified_param) {
202 (void)cmpxchg(module_param, param, modified_param);
203 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400204 }
205
Mike Snitzer09c2d532015-02-27 22:25:26 -0500206 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400207}
208
Mike Snitzere8603132013-09-12 18:06:12 -0400209unsigned dm_get_reserved_bio_based_ios(void)
210{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500211 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzer4cc96132016-05-12 16:28:10 -0400212 RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
Mike Snitzere8603132013-09-12 18:06:12 -0400213}
214EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
215
Mike Snitzer115485e2016-02-22 12:16:21 -0500216static unsigned dm_get_numa_node(void)
217{
218 return __dm_get_module_param_int(&dm_numa_node,
219 DM_NUMA_NODE, num_online_nodes() - 1);
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static int __init local_init(void)
223{
Mike Snitzere689fba2019-02-20 15:37:44 -0500224 int r;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500225
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100226 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100227 if (r)
Mike Snitzere689fba2019-02-20 15:37:44 -0500228 return r;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100229
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400230 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
231 if (!deferred_remove_workqueue) {
232 r = -ENOMEM;
233 goto out_uevent_exit;
234 }
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 _major = major;
237 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100238 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400239 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!_major)
242 _major = r;
243
244 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100245
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400246out_free_workqueue:
247 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100248out_uevent_exit:
249 dm_uevent_exit();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100250
251 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254static void local_exit(void)
255{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400256 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400257 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400258
Akinobu Mita00d59402007-07-17 04:03:46 -0700259 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100260 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 _major = 0;
263
264 DMINFO("cleaned up");
265}
266
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000267static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 local_init,
269 dm_target_init,
270 dm_linear_init,
271 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000272 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100273 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400275 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000278static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 local_exit,
280 dm_target_exit,
281 dm_linear_exit,
282 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000283 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100284 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400286 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
289static int __init dm_init(void)
290{
291 const int count = ARRAY_SIZE(_inits);
292
293 int r, i;
294
295 for (i = 0; i < count; i++) {
296 r = _inits[i]();
297 if (r)
298 goto bad;
299 }
300
301 return 0;
302
303 bad:
304 while (i--)
305 _exits[i]();
306
307 return r;
308}
309
310static void __exit dm_exit(void)
311{
312 int i = ARRAY_SIZE(_exits);
313
314 while (i--)
315 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100316
317 /*
318 * Should be empty by this point.
319 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100320 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/*
324 * Block device functions
325 */
Mike Anderson432a2122009-12-10 23:52:20 +0000326int dm_deleting_md(struct mapped_device *md)
327{
328 return test_bit(DMF_DELETING, &md->flags);
329}
330
Al Virofe5f9f22008-03-02 10:29:31 -0500331static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct mapped_device *md;
334
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700335 spin_lock(&_minor_lock);
336
Al Virofe5f9f22008-03-02 10:29:31 -0500337 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700338 if (!md)
339 goto out;
340
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700341 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000342 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700343 md = NULL;
344 goto out;
345 }
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700348 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700349out:
350 spin_unlock(&_minor_lock);
351
352 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Al Virodb2a1442013-05-05 21:52:57 -0400355static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400357 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200358
Milan Broz4a1aeb92011-01-13 19:59:48 +0000359 spin_lock(&_minor_lock);
360
Mike Snitzer63a4f062015-03-23 17:01:43 -0400361 md = disk->private_data;
362 if (WARN_ON(!md))
363 goto out;
364
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400365 if (atomic_dec_and_test(&md->open_count) &&
366 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400367 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400370out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000371 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700374int dm_open_count(struct mapped_device *md)
375{
376 return atomic_read(&md->open_count);
377}
378
379/*
380 * Guarantees nothing is using the device before it's deleted.
381 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400382int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700383{
384 int r = 0;
385
386 spin_lock(&_minor_lock);
387
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400388 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700389 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400390 if (mark_deferred)
391 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
392 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
393 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700394 else
395 set_bit(DMF_DELETING, &md->flags);
396
397 spin_unlock(&_minor_lock);
398
399 return r;
400}
401
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400402int dm_cancel_deferred_remove(struct mapped_device *md)
403{
404 int r = 0;
405
406 spin_lock(&_minor_lock);
407
408 if (test_bit(DMF_DELETING, &md->flags))
409 r = -EBUSY;
410 else
411 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
412
413 spin_unlock(&_minor_lock);
414
415 return r;
416}
417
418static void do_deferred_remove(struct work_struct *w)
419{
420 dm_deferred_remove();
421}
422
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400423sector_t dm_get_size(struct mapped_device *md)
424{
425 return get_capacity(md->disk);
426}
427
Mike Snitzer9974fa22014-02-28 15:33:43 +0100428struct request_queue *dm_get_md_queue(struct mapped_device *md)
429{
430 return md->queue;
431}
432
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400433struct dm_stats *dm_get_stats(struct mapped_device *md)
434{
435 return &md->stats;
436}
437
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800438static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
439{
440 struct mapped_device *md = bdev->bd_disk->private_data;
441
442 return dm_get_geometry(md, geo);
443}
444
Christoph Hellwige76239a2018-10-12 19:08:49 +0900445#ifdef CONFIG_BLK_DEV_ZONED
Christoph Hellwigd4100352019-11-11 11:39:30 +0900446int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx, void *data)
447{
448 struct dm_report_zones_args *args = data;
449 sector_t sector_diff = args->tgt->begin - args->start;
450
451 /*
452 * Ignore zones beyond the target range.
453 */
454 if (zone->start >= args->start + args->tgt->len)
455 return 0;
456
457 /*
458 * Remap the start sector and write pointer position of the zone
459 * to match its position in the target range.
460 */
461 zone->start += sector_diff;
462 if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
463 if (zone->cond == BLK_ZONE_COND_FULL)
464 zone->wp = zone->start + zone->len;
465 else if (zone->cond == BLK_ZONE_COND_EMPTY)
466 zone->wp = zone->start;
467 else
468 zone->wp += sector_diff;
469 }
470
471 args->next_sector = zone->start + zone->len;
472 return args->orig_cb(zone, args->zone_idx++, args->orig_data);
473}
474EXPORT_SYMBOL_GPL(dm_report_zones_cb);
475
476static int dm_blk_report_zones(struct gendisk *disk, sector_t sector,
477 unsigned int nr_zones, report_zones_cb cb, void *data)
478{
Christoph Hellwige76239a2018-10-12 19:08:49 +0900479 struct mapped_device *md = disk->private_data;
Christoph Hellwige76239a2018-10-12 19:08:49 +0900480 struct dm_table *map;
481 int srcu_idx, ret;
Christoph Hellwigd4100352019-11-11 11:39:30 +0900482 struct dm_report_zones_args args = {
483 .next_sector = sector,
484 .orig_data = data,
485 .orig_cb = cb,
486 };
Christoph Hellwige76239a2018-10-12 19:08:49 +0900487
488 if (dm_suspended_md(md))
489 return -EAGAIN;
490
491 map = dm_get_live_table(md, &srcu_idx);
492 if (!map)
493 return -EIO;
494
Christoph Hellwigd4100352019-11-11 11:39:30 +0900495 do {
496 struct dm_target *tgt;
Christoph Hellwige76239a2018-10-12 19:08:49 +0900497
Christoph Hellwigd4100352019-11-11 11:39:30 +0900498 tgt = dm_table_find_target(map, args.next_sector);
499 if (WARN_ON_ONCE(!tgt->type->report_zones)) {
500 ret = -EIO;
501 goto out;
502 }
Christoph Hellwige76239a2018-10-12 19:08:49 +0900503
Christoph Hellwigd4100352019-11-11 11:39:30 +0900504 args.tgt = tgt;
505 ret = tgt->type->report_zones(tgt, &args, nr_zones);
506 if (ret < 0)
507 goto out;
508 } while (args.zone_idx < nr_zones &&
509 args.next_sector < get_capacity(disk));
Christoph Hellwige76239a2018-10-12 19:08:49 +0900510
Christoph Hellwigd4100352019-11-11 11:39:30 +0900511 ret = args.zone_idx;
Christoph Hellwige76239a2018-10-12 19:08:49 +0900512out:
513 dm_put_live_table(md, srcu_idx);
514 return ret;
Christoph Hellwige76239a2018-10-12 19:08:49 +0900515}
Christoph Hellwigd4100352019-11-11 11:39:30 +0900516#else
517#define dm_blk_report_zones NULL
518#endif /* CONFIG_BLK_DEV_ZONED */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900519
Mike Snitzer971888c2018-04-03 15:05:12 -0400520static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -0400521 struct block_device **bdev)
Mike Snitzer971888c2018-04-03 15:05:12 -0400522 __acquires(md->io_barrier)
Milan Brozaa129a22006-10-03 01:15:15 -0700523{
Mike Snitzer66482022016-02-18 15:44:39 -0500524 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100525 struct dm_table *map;
Mike Snitzer971888c2018-04-03 15:05:12 -0400526 int r;
Milan Brozaa129a22006-10-03 01:15:15 -0700527
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100528retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200529 r = -ENOTTY;
Mike Snitzer971888c2018-04-03 15:05:12 -0400530 map = dm_get_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700531 if (!map || !dm_table_get_size(map))
Mike Snitzer971888c2018-04-03 15:05:12 -0400532 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700533
534 /* We only support devices that have a single target */
535 if (dm_table_get_num_targets(map) != 1)
Mike Snitzer971888c2018-04-03 15:05:12 -0400536 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700537
Mike Snitzer66482022016-02-18 15:44:39 -0500538 tgt = dm_table_get_target(map, 0);
539 if (!tgt->type->prepare_ioctl)
Mike Snitzer971888c2018-04-03 15:05:12 -0400540 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700541
Mike Snitzer971888c2018-04-03 15:05:12 -0400542 if (dm_suspended_md(md))
543 return -EAGAIN;
Milan Brozaa129a22006-10-03 01:15:15 -0700544
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -0400545 r = tgt->type->prepare_ioctl(tgt, bdev);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000546 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Mike Snitzer971888c2018-04-03 15:05:12 -0400547 dm_put_live_table(md, *srcu_idx);
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100548 msleep(10);
549 goto retry;
550 }
Mike Snitzer971888c2018-04-03 15:05:12 -0400551
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200552 return r;
553}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100554
Mike Snitzer971888c2018-04-03 15:05:12 -0400555static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
556 __releases(md->io_barrier)
557{
558 dm_put_live_table(md, srcu_idx);
559}
560
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200561static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
562 unsigned int cmd, unsigned long arg)
563{
564 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer971888c2018-04-03 15:05:12 -0400565 int r, srcu_idx;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200566
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -0400567 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200568 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -0400569 goto out;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200570
571 if (r > 0) {
572 /*
Christoph Hellwige980f622017-02-04 10:45:03 +0100573 * Target determined this ioctl is being issued against a
574 * subset of the parent bdev; require extra privileges.
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200575 */
Christoph Hellwige980f622017-02-04 10:45:03 +0100576 if (!capable(CAP_SYS_RAWIO)) {
577 DMWARN_LIMIT(
578 "%s: sending ioctl %x to DM device without required privilege.",
579 current->comm, cmd);
580 r = -ENOIOCTLCMD;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200581 goto out;
Christoph Hellwige980f622017-02-04 10:45:03 +0100582 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200583 }
584
Mike Snitzer66482022016-02-18 15:44:39 -0500585 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200586out:
Mike Snitzer971888c2018-04-03 15:05:12 -0400587 dm_unprepare_ioctl(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700588 return r;
589}
590
Mike Snitzer978e51b2017-12-09 15:16:42 -0500591static void start_io_acct(struct dm_io *io);
592
593static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Mike Snitzer64f52b02017-12-11 23:17:47 -0500595 struct dm_io *io;
596 struct dm_target_io *tio;
597 struct bio *clone;
598
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400599 clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs);
Mike Snitzer64f52b02017-12-11 23:17:47 -0500600 if (!clone)
601 return NULL;
602
603 tio = container_of(clone, struct dm_target_io, clone);
604 tio->inside_dm_io = true;
605 tio->io = NULL;
606
607 io = container_of(tio, struct dm_io, tio);
608 io->magic = DM_IO_MAGIC;
Mike Snitzer978e51b2017-12-09 15:16:42 -0500609 io->status = 0;
610 atomic_set(&io->io_count, 1);
611 io->orig_bio = bio;
612 io->md = md;
613 spin_lock_init(&io->endio_lock);
614
615 start_io_acct(io);
Mike Snitzer64f52b02017-12-11 23:17:47 -0500616
617 return io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100620static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Mike Snitzer64f52b02017-12-11 23:17:47 -0500622 bio_put(&io->tio.clone);
623}
624
625static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
626 unsigned target_bio_nr, gfp_t gfp_mask)
627{
628 struct dm_target_io *tio;
629
630 if (!ci->io->tio.io) {
631 /* the dm_target_io embedded in ci->io is available */
632 tio = &ci->io->tio;
633 } else {
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400634 struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs);
Mike Snitzer64f52b02017-12-11 23:17:47 -0500635 if (!clone)
636 return NULL;
637
638 tio = container_of(clone, struct dm_target_io, clone);
639 tio->inside_dm_io = false;
640 }
641
642 tio->magic = DM_TIO_MAGIC;
643 tio->io = ci->io;
644 tio->ti = ti;
645 tio->target_bio_nr = target_bio_nr;
646
647 return tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Mike Snitzercfae7522016-04-11 12:05:38 -0400650static void free_tio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Mike Snitzer64f52b02017-12-11 23:17:47 -0500652 if (tio->inside_dm_io)
653 return;
Mikulas Patockadba14162012-10-12 21:02:15 +0100654 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Gabriel Krisman Bertazi087615bf2020-04-30 16:48:29 -0400657u64 dm_start_time_ns_from_clone(struct bio *bio)
658{
659 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
660 struct dm_io *io = tio->io;
661
662 return jiffies_to_nsecs(io->start_time);
663}
664EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone);
665
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800666static void start_io_acct(struct dm_io *io)
667{
668 struct mapped_device *md = io->md;
Mike Snitzer745dc572017-12-11 20:51:50 -0500669 struct bio *bio = io->orig_bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800670
Christoph Hellwig86240d52020-05-27 07:24:09 +0200671 io->start_time = bio_start_io_acct(bio);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400672 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500673 dm_stats_account_io(&md->stats, bio_data_dir(bio),
674 bio->bi_iter.bi_sector, bio_sectors(bio),
675 false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800676}
677
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000678static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800679{
680 struct mapped_device *md = io->md;
Mike Snitzer745dc572017-12-11 20:51:50 -0500681 struct bio *bio = io->orig_bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800682 unsigned long duration = jiffies - io->start_time;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800683
Christoph Hellwig86240d52020-05-27 07:24:09 +0200684 bio_end_io_acct(bio, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800685
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400686 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500687 dm_stats_account_io(&md->stats, bio_data_dir(bio),
688 bio->bi_iter.bi_sector, bio_sectors(bio),
689 true, duration, &io->stats_aux);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400690
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000691 /* nudge anyone waiting on suspend queue */
Mikulas Patocka645efa82019-02-05 05:09:00 -0500692 if (unlikely(wq_has_sleeper(&md->wait)))
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000693 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/*
697 * Add the bio to the list of deferred io.
698 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100699static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200701 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200703 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200705 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200706 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709/*
710 * Everyone (including functions in this file), should use this
711 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100712 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100714struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100716 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100718 return srcu_dereference(md->map, &md->io_barrier);
719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100721void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
722{
723 srcu_read_unlock(&md->io_barrier, srcu_idx);
724}
725
726void dm_sync_table(struct mapped_device *md)
727{
728 synchronize_srcu(&md->io_barrier);
729 synchronize_rcu_expedited();
730}
731
732/*
733 * A fast alternative to dm_get_live_table/dm_put_live_table.
734 * The caller must not block between these two functions.
735 */
736static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
737{
738 rcu_read_lock();
739 return rcu_dereference(md->map);
740}
741
742static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
743{
744 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Mike Snitzer971888c2018-04-03 15:05:12 -0400747static char *_dm_claim_ptr = "I belong to device-mapper";
748
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800749/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500750 * Open a table device so we can use it as a map destination.
751 */
752static int open_table_device(struct table_device *td, dev_t dev,
753 struct mapped_device *md)
754{
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500755 struct block_device *bdev;
756
757 int r;
758
759 BUG_ON(td->dm_dev.bdev);
760
Mike Snitzer519049a2018-02-22 13:31:20 -0500761 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500762 if (IS_ERR(bdev))
763 return PTR_ERR(bdev);
764
765 r = bd_link_disk_holder(bdev, dm_disk(md));
766 if (r) {
767 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
768 return r;
769 }
770
771 td->dm_dev.bdev = bdev;
Dan Williams817bf402017-04-12 13:37:44 -0700772 td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500773 return 0;
774}
775
776/*
777 * Close a table device that we've been using.
778 */
779static void close_table_device(struct table_device *td, struct mapped_device *md)
780{
781 if (!td->dm_dev.bdev)
782 return;
783
784 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
785 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
Dan Williams817bf402017-04-12 13:37:44 -0700786 put_dax(td->dm_dev.dax_dev);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500787 td->dm_dev.bdev = NULL;
Dan Williams817bf402017-04-12 13:37:44 -0700788 td->dm_dev.dax_dev = NULL;
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500789}
790
791static struct table_device *find_table_device(struct list_head *l, dev_t dev,
Sheetal Singala8454fca2019-05-10 23:18:37 +0530792 fmode_t mode)
793{
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500794 struct table_device *td;
795
796 list_for_each_entry(td, l, list)
797 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
798 return td;
799
800 return NULL;
801}
802
803int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
Sheetal Singala8454fca2019-05-10 23:18:37 +0530804 struct dm_dev **result)
805{
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500806 int r;
807 struct table_device *td;
808
809 mutex_lock(&md->table_devices_lock);
810 td = find_table_device(&md->table_devices, dev, mode);
811 if (!td) {
Mike Snitzer115485e2016-02-22 12:16:21 -0500812 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500813 if (!td) {
814 mutex_unlock(&md->table_devices_lock);
815 return -ENOMEM;
816 }
817
818 td->dm_dev.mode = mode;
819 td->dm_dev.bdev = NULL;
820
821 if ((r = open_table_device(td, dev, md))) {
822 mutex_unlock(&md->table_devices_lock);
823 kfree(td);
824 return r;
825 }
826
827 format_dev_t(td->dm_dev.name, dev);
828
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300829 refcount_set(&td->count, 1);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500830 list_add(&td->list, &md->table_devices);
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300831 } else {
832 refcount_inc(&td->count);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500833 }
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500834 mutex_unlock(&md->table_devices_lock);
835
836 *result = &td->dm_dev;
837 return 0;
838}
839EXPORT_SYMBOL_GPL(dm_get_table_device);
840
841void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
842{
843 struct table_device *td = container_of(d, struct table_device, dm_dev);
844
845 mutex_lock(&md->table_devices_lock);
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300846 if (refcount_dec_and_test(&td->count)) {
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500847 close_table_device(td, md);
848 list_del(&td->list);
849 kfree(td);
850 }
851 mutex_unlock(&md->table_devices_lock);
852}
853EXPORT_SYMBOL(dm_put_table_device);
854
855static void free_table_devices(struct list_head *devices)
856{
857 struct list_head *tmp, *next;
858
859 list_for_each_safe(tmp, next, devices) {
860 struct table_device *td = list_entry(tmp, struct table_device, list);
861
862 DMWARN("dm_destroy: %s still exists with %d references",
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300863 td->dm_dev.name, refcount_read(&td->count));
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500864 kfree(td);
865 }
866}
867
868/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800869 * Get the geometry associated with a dm device
870 */
871int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
872{
873 *geo = md->geometry;
874
875 return 0;
876}
877
878/*
879 * Set the geometry of a device.
880 */
881int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
882{
883 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
884
885 if (geo->start > sz) {
886 DMWARN("Start sector is beyond the geometry limits.");
887 return -EINVAL;
888 }
889
890 md->geometry = *geo;
891
892 return 0;
893}
894
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800895static int __noflush_suspending(struct mapped_device *md)
896{
897 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/*
901 * Decrements the number of outstanding ios that a bio has been
902 * cloned into, completing the original io if necc.
903 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200904static void dec_pending(struct dm_io *io, blk_status_t error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800906 unsigned long flags;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200907 blk_status_t io_error;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000908 struct bio *bio;
909 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800910
911 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100912 if (unlikely(error)) {
913 spin_lock_irqsave(&io->endio_lock, flags);
Mike Snitzer745dc572017-12-11 20:51:50 -0500914 if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200915 io->status = error;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100916 spin_unlock_irqrestore(&io->endio_lock, flags);
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 if (atomic_dec_and_test(&io->io_count)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200920 if (io->status == BLK_STS_DM_REQUEUE) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800921 /*
922 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800923 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100924 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200925 if (__noflush_suspending(md))
Mike Snitzer745dc572017-12-11 20:51:50 -0500926 /* NOTE early return due to BLK_STS_DM_REQUEUE below */
927 bio_list_add_head(&md->deferred, io->orig_bio);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200928 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800929 /* noflush suspend was interrupted. */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200930 io->status = BLK_STS_IOERR;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100931 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800932 }
933
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200934 io_error = io->status;
Mike Snitzer745dc572017-12-11 20:51:50 -0500935 bio = io->orig_bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200936 end_io_acct(io);
937 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100938
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200939 if (io_error == BLK_STS_DM_REQUEUE)
Tejun Heo6a8736d2010-09-08 18:07:00 +0200940 return;
941
Jens Axboe1eff9d32016-08-05 15:35:16 -0600942 if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100943 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200944 * Preflush done for flush with data, reissue
Mike Christie28a8f0d2016-06-05 14:32:25 -0500945 * without REQ_PREFLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100946 */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600947 bio->bi_opf &= ~REQ_PREFLUSH;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200948 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100949 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200950 /* done with normal IO or empty flush */
NeilBrown8dd601f2018-02-15 20:00:15 +1100951 if (io_error)
952 bio->bi_status = io_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200953 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956}
957
Mike Snitzerbcb44432019-04-03 12:23:11 -0400958void disable_discard(struct mapped_device *md)
959{
960 struct queue_limits *limits = dm_get_queue_limits(md);
961
962 /* device doesn't really support DISCARD, disable it */
963 limits->max_discard_sectors = 0;
964 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue);
965}
966
Mike Snitzer4cc96132016-05-12 16:28:10 -0400967void disable_write_same(struct mapped_device *md)
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400968{
969 struct queue_limits *limits = dm_get_queue_limits(md);
970
971 /* device doesn't really support WRITE SAME, disable it */
972 limits->max_write_same_sectors = 0;
973}
974
Christoph Hellwigac62d622017-04-05 19:21:05 +0200975void disable_write_zeroes(struct mapped_device *md)
976{
977 struct queue_limits *limits = dm_get_queue_limits(md);
978
979 /* device doesn't really support WRITE ZEROES, disable it */
980 limits->max_write_zeroes_sectors = 0;
981}
982
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200983static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200985 blk_status_t error = bio->bi_status;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500986 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000987 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700988 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 dm_endio_fn endio = tio->ti->type->end_io;
Johannes Thumshirn415c79e2020-06-19 15:59:04 +0900990 struct bio *orig_bio = io->orig_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Mike Snitzer978e51b2017-12-09 15:16:42 -0500992 if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) {
Mike Snitzerbcb44432019-04-03 12:23:11 -0400993 if (bio_op(bio) == REQ_OP_DISCARD &&
994 !bio->bi_disk->queue->limits.max_discard_sectors)
995 disable_discard(md);
996 else if (bio_op(bio) == REQ_OP_WRITE_SAME &&
997 !bio->bi_disk->queue->limits.max_write_same_sectors)
Christoph Hellwigac62d622017-04-05 19:21:05 +0200998 disable_write_same(md);
Mike Snitzerbcb44432019-04-03 12:23:11 -0400999 else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
1000 !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
Christoph Hellwigac62d622017-04-05 19:21:05 +02001001 disable_write_zeroes(md);
1002 }
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001003
Johannes Thumshirn415c79e2020-06-19 15:59:04 +09001004 /*
1005 * For zone-append bios get offset in zone of the written
1006 * sector and add that to the original bio sector pos.
1007 */
1008 if (bio_op(orig_bio) == REQ_OP_ZONE_APPEND) {
1009 sector_t written_sector = bio->bi_iter.bi_sector;
1010 struct request_queue *q = orig_bio->bi_disk->queue;
1011 u64 mask = (u64)blk_queue_zone_sectors(q) - 1;
1012
1013 orig_bio->bi_iter.bi_sector += written_sector & mask;
1014 }
1015
Christoph Hellwig1be56902017-06-03 09:38:03 +02001016 if (endio) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001017 int r = endio(tio->ti, bio, &error);
Christoph Hellwig1be56902017-06-03 09:38:03 +02001018 switch (r) {
1019 case DM_ENDIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001020 error = BLK_STS_DM_REQUEUE;
Christoph Hellwig1be56902017-06-03 09:38:03 +02001021 /*FALLTHRU*/
1022 case DM_ENDIO_DONE:
1023 break;
1024 case DM_ENDIO_INCOMPLETE:
1025 /* The target will handle the io */
1026 return;
1027 default:
1028 DMWARN("unimplemented target endio return value: %d", r);
1029 BUG();
1030 }
1031 }
1032
Mike Snitzercfae7522016-04-11 12:05:38 -04001033 free_tio(tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001034 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Mike Snitzer78d8e582015-06-26 10:01:13 -04001037/*
Mike Snitzer56a67df2010-08-12 04:14:10 +01001038 * Return maximum size of I/O possible at the supplied sector up to the current
1039 * target boundary.
1040 */
1041static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001043 sector_t target_offset = dm_target_offset(ti, sector);
1044
1045 return ti->len - target_offset;
1046}
1047
1048static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1049{
1050 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001051 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001054 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001056 if (ti->max_io_len) {
1057 offset = dm_target_offset(ti, sector);
1058 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1059 max_len = sector_div(offset, ti->max_io_len);
1060 else
1061 max_len = offset & (ti->max_io_len - 1);
1062 max_len = ti->max_io_len - max_len;
1063
1064 if (len > max_len)
1065 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
1068 return len;
1069}
1070
Mike Snitzer542f9032012-07-27 15:08:00 +01001071int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1072{
1073 if (len > UINT_MAX) {
1074 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1075 (unsigned long long)len, UINT_MAX);
1076 ti->error = "Maximum size of target IO is too large";
1077 return -EINVAL;
1078 }
1079
Mikulas Patocka75ae1932019-03-21 16:46:12 -04001080 ti->max_io_len = (uint32_t) len;
Mike Snitzer542f9032012-07-27 15:08:00 +01001081
1082 return 0;
1083}
1084EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1085
Dan Williamsf26c5712017-04-12 12:35:44 -07001086static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
Mike Snitzer3d97c822018-04-30 16:06:28 -04001087 sector_t sector, int *srcu_idx)
1088 __acquires(md->io_barrier)
Toshi Kani545ed202016-06-22 17:54:53 -06001089{
Toshi Kani545ed202016-06-22 17:54:53 -06001090 struct dm_table *map;
1091 struct dm_target *ti;
Toshi Kani545ed202016-06-22 17:54:53 -06001092
Dan Williamsf26c5712017-04-12 12:35:44 -07001093 map = dm_get_live_table(md, srcu_idx);
Toshi Kani545ed202016-06-22 17:54:53 -06001094 if (!map)
Dan Williamsf26c5712017-04-12 12:35:44 -07001095 return NULL;
Toshi Kani545ed202016-06-22 17:54:53 -06001096
1097 ti = dm_table_find_target(map, sector);
Mikulas Patocka123d87d2019-08-23 09:55:26 -04001098 if (!ti)
Dan Williamsf26c5712017-04-12 12:35:44 -07001099 return NULL;
1100
1101 return ti;
1102}
1103
1104static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
Mike Snitzer3d97c822018-04-30 16:06:28 -04001105 long nr_pages, void **kaddr, pfn_t *pfn)
Dan Williamsf26c5712017-04-12 12:35:44 -07001106{
1107 struct mapped_device *md = dax_get_private(dax_dev);
1108 sector_t sector = pgoff * PAGE_SECTORS;
1109 struct dm_target *ti;
1110 long len, ret = -EIO;
1111 int srcu_idx;
1112
1113 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1114
1115 if (!ti)
Toshi Kani545ed202016-06-22 17:54:53 -06001116 goto out;
Dan Williamsf26c5712017-04-12 12:35:44 -07001117 if (!ti->type->direct_access)
1118 goto out;
1119 len = max_io_len(sector, ti) / PAGE_SECTORS;
1120 if (len < 1)
1121 goto out;
1122 nr_pages = min(len, nr_pages);
Ross Zwislerdbc62652018-06-26 16:30:41 -06001123 ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
Dan Williams817bf402017-04-12 13:37:44 -07001124
Dan Williamsf26c5712017-04-12 12:35:44 -07001125 out:
Toshi Kani545ed202016-06-22 17:54:53 -06001126 dm_put_live_table(md, srcu_idx);
Dan Williamsf26c5712017-04-12 12:35:44 -07001127
1128 return ret;
Toshi Kani545ed202016-06-22 17:54:53 -06001129}
1130
Dan Williams7bf7eac2019-05-16 13:26:29 -07001131static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev,
1132 int blocksize, sector_t start, sector_t len)
1133{
1134 struct mapped_device *md = dax_get_private(dax_dev);
1135 struct dm_table *map;
1136 int srcu_idx;
1137 bool ret;
1138
1139 map = dm_get_live_table(md, &srcu_idx);
1140 if (!map)
1141 return false;
1142
Pankaj Gupta2e9ee092019-07-05 19:33:25 +05301143 ret = dm_table_supports_dax(map, device_supports_dax, &blocksize);
Dan Williams7bf7eac2019-05-16 13:26:29 -07001144
1145 dm_put_live_table(md, srcu_idx);
1146
1147 return ret;
1148}
1149
Dan Williams7e026c82017-05-29 12:57:56 -07001150static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
Mike Snitzer3d97c822018-04-30 16:06:28 -04001151 void *addr, size_t bytes, struct iov_iter *i)
Dan Williams7e026c82017-05-29 12:57:56 -07001152{
1153 struct mapped_device *md = dax_get_private(dax_dev);
1154 sector_t sector = pgoff * PAGE_SECTORS;
1155 struct dm_target *ti;
1156 long ret = 0;
1157 int srcu_idx;
1158
1159 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1160
1161 if (!ti)
1162 goto out;
1163 if (!ti->type->dax_copy_from_iter) {
1164 ret = copy_from_iter(addr, bytes, i);
1165 goto out;
1166 }
1167 ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
1168 out:
1169 dm_put_live_table(md, srcu_idx);
1170
1171 return ret;
1172}
1173
Dan Williamsb3a9a0c2018-05-02 06:46:33 -07001174static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
1175 void *addr, size_t bytes, struct iov_iter *i)
1176{
1177 struct mapped_device *md = dax_get_private(dax_dev);
1178 sector_t sector = pgoff * PAGE_SECTORS;
1179 struct dm_target *ti;
1180 long ret = 0;
1181 int srcu_idx;
1182
1183 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1184
1185 if (!ti)
1186 goto out;
1187 if (!ti->type->dax_copy_to_iter) {
1188 ret = copy_to_iter(addr, bytes, i);
1189 goto out;
1190 }
1191 ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i);
1192 out:
1193 dm_put_live_table(md, srcu_idx);
1194
1195 return ret;
1196}
1197
Vivek Goyalcdf6cdc2020-02-28 11:34:54 -05001198static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
1199 size_t nr_pages)
1200{
1201 struct mapped_device *md = dax_get_private(dax_dev);
1202 sector_t sector = pgoff * PAGE_SECTORS;
1203 struct dm_target *ti;
1204 int ret = -EIO;
1205 int srcu_idx;
1206
1207 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1208
1209 if (!ti)
1210 goto out;
1211 if (WARN_ON(!ti->type->dax_zero_page_range)) {
1212 /*
1213 * ->zero_page_range() is mandatory dax operation. If we are
1214 * here, something is wrong.
1215 */
1216 dm_put_live_table(md, srcu_idx);
1217 goto out;
1218 }
1219 ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages);
1220
1221 out:
1222 dm_put_live_table(md, srcu_idx);
1223
1224 return ret;
1225}
1226
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001227/*
1228 * A target may call dm_accept_partial_bio only from the map routine. It is
Ajay Joshi2e2d6f72019-10-27 23:05:48 +09001229 * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_RESET,
1230 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE and REQ_OP_ZONE_FINISH.
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001231 *
1232 * dm_accept_partial_bio informs the dm that the target only wants to process
1233 * additional n_sectors sectors of the bio and the rest of the data should be
1234 * sent in a next bio.
1235 *
1236 * A diagram that explains the arithmetics:
1237 * +--------------------+---------------+-------+
1238 * | 1 | 2 | 3 |
1239 * +--------------------+---------------+-------+
1240 *
1241 * <-------------- *tio->len_ptr --------------->
1242 * <------- bi_size ------->
1243 * <-- n_sectors -->
1244 *
1245 * Region 1 was already iterated over with bio_advance or similar function.
1246 * (it may be empty if the target doesn't use bio_advance)
1247 * Region 2 is the remaining bio size that the target wants to process.
1248 * (it may be empty if region 1 is non-empty, although there is no reason
1249 * to make it empty)
1250 * The target requires that region 3 is to be sent in the next bio.
1251 *
1252 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1253 * the partially processed part (the sum of regions 1+2) must be the same for all
1254 * copies of the bio.
1255 */
1256void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1257{
1258 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1259 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Jens Axboe1eff9d32016-08-05 15:35:16 -06001260 BUG_ON(bio->bi_opf & REQ_PREFLUSH);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001261 BUG_ON(bi_size > *tio->len_ptr);
1262 BUG_ON(n_sectors > bi_size);
1263 *tio->len_ptr -= bi_size - n_sectors;
1264 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1265}
1266EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1267
Mike Snitzer978e51b2017-12-09 15:16:42 -05001268static blk_qc_t __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001271 sector_t sector;
Mikulas Patockadba14162012-10-12 21:02:15 +01001272 struct bio *clone = &tio->clone;
Mike Snitzer64f52b02017-12-11 23:17:47 -05001273 struct dm_io *io = tio->io;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001274 struct mapped_device *md = io->md;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001275 struct dm_target *ti = tio->ti;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001276 blk_qc_t ret = BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /*
1281 * Map the clone. If r == 0 we don't need to do
1282 * anything, the target has assumed ownership of
1283 * this io.
1284 */
Mike Snitzer64f52b02017-12-11 23:17:47 -05001285 atomic_inc(&io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001286 sector = clone->bi_iter.bi_sector;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001287
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001288 r = ti->type->map(ti, clone);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001289 switch (r) {
1290 case DM_MAPIO_SUBMITTED:
1291 break;
1292 case DM_MAPIO_REMAPPED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /* the bio has been remapped so dispatch it */
Christoph Hellwig74d46992017-08-23 19:10:32 +02001294 trace_block_bio_remap(clone->bi_disk->queue, clone,
Mike Snitzer64f52b02017-12-11 23:17:47 -05001295 bio_dev(io->orig_bio), sector);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001296 if (md->type == DM_TYPE_NVME_BIO_BASED)
1297 ret = direct_make_request(clone);
1298 else
1299 ret = generic_make_request(clone);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001300 break;
1301 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001302 free_tio(tio);
Mike Snitzer64f52b02017-12-11 23:17:47 -05001303 dec_pending(io, BLK_STS_IOERR);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001304 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +02001305 case DM_MAPIO_REQUEUE:
Mike Snitzercfae7522016-04-11 12:05:38 -04001306 free_tio(tio);
Mike Snitzer64f52b02017-12-11 23:17:47 -05001307 dec_pending(io, BLK_STS_DM_REQUEUE);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001308 break;
1309 default:
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001310 DMWARN("unimplemented target map return value: %d", r);
1311 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Mike Snitzer978e51b2017-12-09 15:16:42 -05001314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Mikulas Patockae0d66092014-03-14 18:40:39 -04001317static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001318{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001319 bio->bi_iter.bi_sector = sector;
1320 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323/*
1324 * Creates a bio that consists of range of complete bvecs.
1325 */
Mike Snitzerc80914e2016-03-02 12:33:03 -05001326static int clone_bio(struct dm_target_io *tio, struct bio *bio,
1327 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Mikulas Patockadba14162012-10-12 21:02:15 +01001329 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001331 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00001333 bio_crypt_clone(clone, bio, GFP_NOIO);
1334
Mike Snitzer57c36512019-01-16 18:53:26 -05001335 if (bio_integrity(bio)) {
Mikulas Patockae2460f22017-04-18 16:51:48 -04001336 int r;
1337
1338 if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1339 !dm_target_passes_integrity(tio->ti->type))) {
1340 DMWARN("%s: the target %s doesn't support integrity data.",
1341 dm_device_name(tio->io->md),
1342 tio->ti->type->name);
1343 return -EIO;
1344 }
1345
1346 r = bio_integrity_clone(clone, bio, GFP_NOIO);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001347 if (r < 0)
1348 return r;
1349 }
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001350
Mike Snitzerfa8db492019-02-05 17:07:58 -05001351 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1352 clone->bi_iter.bi_size = to_bytes(len);
1353
1354 if (bio_integrity(bio))
1355 bio_integrity_trim(clone);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001356
1357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Mike Snitzer318716d2017-11-22 14:56:12 -05001360static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1361 struct dm_target *ti, unsigned num_bios)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001362{
Mikulas Patockadba14162012-10-12 21:02:15 +01001363 struct dm_target_io *tio;
Mike Snitzer318716d2017-11-22 14:56:12 -05001364 int try;
Mikulas Patockadba14162012-10-12 21:02:15 +01001365
Mike Snitzer318716d2017-11-22 14:56:12 -05001366 if (!num_bios)
1367 return;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001368
Mike Snitzer318716d2017-11-22 14:56:12 -05001369 if (num_bios == 1) {
1370 tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1371 bio_list_add(blist, &tio->clone);
1372 return;
1373 }
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001374
Mike Snitzer318716d2017-11-22 14:56:12 -05001375 for (try = 0; try < 2; try++) {
1376 int bio_nr;
1377 struct bio *bio;
1378
1379 if (try)
Mike Snitzerbc02cdb2017-12-14 16:30:42 -05001380 mutex_lock(&ci->io->md->table_devices_lock);
Mike Snitzer318716d2017-11-22 14:56:12 -05001381 for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1382 tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1383 if (!tio)
1384 break;
1385
1386 bio_list_add(blist, &tio->clone);
1387 }
1388 if (try)
Mike Snitzerbc02cdb2017-12-14 16:30:42 -05001389 mutex_unlock(&ci->io->md->table_devices_lock);
Mike Snitzer318716d2017-11-22 14:56:12 -05001390 if (bio_nr == num_bios)
1391 return;
1392
1393 while ((bio = bio_list_pop(blist))) {
1394 tio = container_of(bio, struct dm_target_io, clone);
1395 free_tio(tio);
1396 }
1397 }
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001398}
1399
Mike Snitzer978e51b2017-12-09 15:16:42 -05001400static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
1401 struct dm_target_io *tio, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001402{
Mikulas Patockadba14162012-10-12 21:02:15 +01001403 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001404
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001405 tio->len_ptr = len;
1406
Junichi Nomura99778272014-10-03 11:55:16 +00001407 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001408 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001409 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001410
Mike Snitzer978e51b2017-12-09 15:16:42 -05001411 return __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001412}
1413
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001414static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001415 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001416{
Mike Snitzer318716d2017-11-22 14:56:12 -05001417 struct bio_list blist = BIO_EMPTY_LIST;
1418 struct bio *bio;
1419 struct dm_target_io *tio;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001420
Mike Snitzer318716d2017-11-22 14:56:12 -05001421 alloc_multiple_bios(&blist, ci, ti, num_bios);
1422
1423 while ((bio = bio_list_pop(&blist))) {
1424 tio = container_of(bio, struct dm_target_io, clone);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001425 (void) __clone_and_map_simple_bio(ci, tio, len);
Mike Snitzer318716d2017-11-22 14:56:12 -05001426 }
Mike Snitzer06a426c2010-08-12 04:14:09 +01001427}
1428
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001429static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001430{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001431 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001432 struct dm_target *ti;
1433
Dennis Zhou892ad712018-12-05 12:10:30 -05001434 /*
Jens Axboedbe3ece2018-12-19 09:13:34 -07001435 * Empty flush uses a statically initialized bio, as the base for
1436 * cloning. However, blkg association requires that a bdev is
1437 * associated with a gendisk, which doesn't happen until the bdev is
1438 * opened. So, blkg association is done at issue time of the flush
1439 * rather than when the device is created in alloc_dev().
Dennis Zhou892ad712018-12-05 12:10:30 -05001440 */
1441 bio_set_dev(ci->bio, ci->io->md->bdev);
1442
Mike Snitzerb372d362010-09-08 18:07:01 +02001443 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001444 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001445 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001446 return 0;
1447}
1448
Mike Snitzerc80914e2016-03-02 12:33:03 -05001449static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
NeilBrownf31c21e2017-11-22 14:25:18 +11001450 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001451{
Mikulas Patockadba14162012-10-12 21:02:15 +01001452 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001453 struct dm_target_io *tio;
NeilBrownf31c21e2017-11-22 14:25:18 +11001454 int r;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001455
Mike Snitzer318716d2017-11-22 14:56:12 -05001456 tio = alloc_tio(ci, ti, 0, GFP_NOIO);
NeilBrownf31c21e2017-11-22 14:25:18 +11001457 tio->len_ptr = len;
1458 r = clone_bio(tio, bio, sector, *len);
1459 if (r < 0) {
1460 free_tio(tio);
1461 return r;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001462 }
Mike Snitzer978e51b2017-12-09 15:16:42 -05001463 (void) __map_bio(tio);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001464
NeilBrownf31c21e2017-11-22 14:25:18 +11001465 return 0;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001466}
1467
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001468typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001469
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001470static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001471{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001472 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001473}
1474
Denis Semakin00716542018-03-13 13:23:45 +04001475static unsigned get_num_secure_erase_bios(struct dm_target *ti)
1476{
1477 return ti->num_secure_erase_bios;
1478}
1479
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001480static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001481{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001482 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001483}
1484
Christoph Hellwigac62d622017-04-05 19:21:05 +02001485static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1486{
1487 return ti->num_write_zeroes_bios;
1488}
1489
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001490static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
Mike Snitzer61697a62019-01-18 14:19:26 -05001491 unsigned num_bios)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001492{
Michael Lass51b86f92019-05-21 21:58:07 +02001493 unsigned len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001494
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001495 /*
1496 * Even though the device advertised support for this type of
1497 * request, that does not mean every target supports it, and
1498 * reconfiguration might also have changed that since the
1499 * check was performed.
1500 */
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001501 if (!num_bios)
1502 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001503
Michael Lass51b86f92019-05-21 21:58:07 +02001504 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1505
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001506 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001507
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001508 ci->sector += len;
1509 ci->sector_count -= len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001510
1511 return 0;
1512}
1513
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001514static int __send_discard(struct clone_info *ci, struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001515{
Mike Snitzer61697a62019-01-18 14:19:26 -05001516 return __send_changing_extent_only(ci, ti, get_num_discard_bios(ti));
Mike Snitzer23508a92012-12-21 20:23:37 +00001517}
1518
Denis Semakin00716542018-03-13 13:23:45 +04001519static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
1520{
Mike Snitzer61697a62019-01-18 14:19:26 -05001521 return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios(ti));
Denis Semakin00716542018-03-13 13:23:45 +04001522}
1523
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001524static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001525{
Mike Snitzer61697a62019-01-18 14:19:26 -05001526 return __send_changing_extent_only(ci, ti, get_num_write_same_bios(ti));
Mike Snitzer23508a92012-12-21 20:23:37 +00001527}
1528
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001529static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
Christoph Hellwigac62d622017-04-05 19:21:05 +02001530{
Mike Snitzer61697a62019-01-18 14:19:26 -05001531 return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios(ti));
Christoph Hellwigac62d622017-04-05 19:21:05 +02001532}
1533
Mike Snitzer568c73a2019-01-18 14:10:37 -05001534static bool is_abnormal_io(struct bio *bio)
1535{
1536 bool r = false;
1537
1538 switch (bio_op(bio)) {
1539 case REQ_OP_DISCARD:
1540 case REQ_OP_SECURE_ERASE:
1541 case REQ_OP_WRITE_SAME:
1542 case REQ_OP_WRITE_ZEROES:
1543 r = true;
1544 break;
1545 }
1546
1547 return r;
1548}
1549
Mike Snitzer0519c712018-03-26 11:49:16 -04001550static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
1551 int *result)
1552{
1553 struct bio *bio = ci->bio;
1554
1555 if (bio_op(bio) == REQ_OP_DISCARD)
1556 *result = __send_discard(ci, ti);
Denis Semakin00716542018-03-13 13:23:45 +04001557 else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
1558 *result = __send_secure_erase(ci, ti);
Mike Snitzer0519c712018-03-26 11:49:16 -04001559 else if (bio_op(bio) == REQ_OP_WRITE_SAME)
1560 *result = __send_write_same(ci, ti);
1561 else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
1562 *result = __send_write_zeroes(ci, ti);
1563 else
1564 return false;
1565
1566 return true;
1567}
1568
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001569/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001570 * Select the correct strategy for processing a non-flush bio.
1571 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001572static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001574 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001575 unsigned len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001576 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001578 ti = dm_table_find_target(ci->map, ci->sector);
Mikulas Patocka123d87d2019-08-23 09:55:26 -04001579 if (!ti)
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001580 return -EIO;
1581
Mike Snitzer568c73a2019-01-18 14:10:37 -05001582 if (__process_abnormal_io(ci, ti, &r))
Mike Snitzer0519c712018-03-26 11:49:16 -04001583 return r;
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001584
Christoph Hellwige76239a2018-10-12 19:08:49 +09001585 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001586
Mike Snitzerc80914e2016-03-02 12:33:03 -05001587 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1588 if (r < 0)
1589 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001591 ci->sector += len;
1592 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
Mike Snitzer978e51b2017-12-09 15:16:42 -05001597static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1598 struct dm_table *map, struct bio *bio)
1599{
1600 ci->map = map;
1601 ci->io = alloc_io(md, bio);
1602 ci->sector = bio->bi_iter.bi_sector;
1603}
1604
Mike Snitzera1e1cb72019-01-17 10:48:01 -05001605#define __dm_part_stat_sub(part, field, subnd) \
1606 (part_stat_get(part, field) -= (subnd))
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001609 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
Mike Snitzer978e51b2017-12-09 15:16:42 -05001611static blk_qc_t __split_and_process_bio(struct mapped_device *md,
1612 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 struct clone_info ci;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001615 blk_qc_t ret = BLK_QC_T_NONE;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001616 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Mike Snitzer978e51b2017-12-09 15:16:42 -05001618 init_clone_info(&ci, md, map, bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001619
Jens Axboe1eff9d32016-08-05 15:35:16 -06001620 if (bio->bi_opf & REQ_PREFLUSH) {
Jens Axboedbe3ece2018-12-19 09:13:34 -07001621 struct bio flush_bio;
1622
1623 /*
1624 * Use an on-stack bio for this, it's safe since we don't
1625 * need to reference it after submit. It's just used as
1626 * the basis for the clone(s).
1627 */
1628 bio_init(&flush_bio, NULL, 0);
1629 flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1630 ci.bio = &flush_bio;
Mike Snitzerb372d362010-09-08 18:07:01 +02001631 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001632 error = __send_empty_flush(&ci);
Christoph Hellwig382761d2020-06-27 09:31:46 +02001633 bio_uninit(ci.bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001634 /* dec_pending submits any data associated with flush */
Ajay Joshi2e2d6f72019-10-27 23:05:48 +09001635 } else if (op_is_zone_mgmt(bio_op(bio))) {
Damien Le Moala4aa5e52017-05-08 16:40:46 -07001636 ci.bio = bio;
1637 ci.sector_count = 0;
1638 error = __split_and_process_non_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001639 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001640 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001641 ci.sector_count = bio_sectors(bio);
NeilBrown18a25da2017-09-06 09:43:28 +10001642 while (ci.sector_count && !error) {
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001643 error = __split_and_process_non_flush(&ci);
NeilBrown18a25da2017-09-06 09:43:28 +10001644 if (current->bio_list && ci.sector_count && !error) {
1645 /*
1646 * Remainder must be passed to generic_make_request()
1647 * so that it gets handled *after* bios already submitted
1648 * have been completely processed.
1649 * We take a clone of the original to store in
Mike Snitzer745dc572017-12-11 20:51:50 -05001650 * ci.io->orig_bio to be used by end_io_acct() and
NeilBrown18a25da2017-09-06 09:43:28 +10001651 * for dec_pending to use for completion handling.
NeilBrown18a25da2017-09-06 09:43:28 +10001652 */
Mike Snitzerf21c6012018-06-15 09:35:33 -04001653 struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count,
1654 GFP_NOIO, &md->queue->bio_split);
Mike Snitzer745dc572017-12-11 20:51:50 -05001655 ci.io->orig_bio = b;
Mike Snitzera1e1cb72019-01-17 10:48:01 -05001656
1657 /*
1658 * Adjust IO stats for each split, otherwise upon queue
1659 * reentry there will be redundant IO accounting.
1660 * NOTE: this is a stop-gap fix, a proper fix involves
1661 * significant refactoring of DM core's bio splitting
1662 * (by eliminating DM's splitting and just using bio_split)
1663 */
1664 part_stat_lock();
1665 __dm_part_stat_sub(&dm_disk(md)->part0,
1666 sectors[op_stat_group(bio_op(bio))], ci.sector_count);
1667 part_stat_unlock();
1668
NeilBrown18a25da2017-09-06 09:43:28 +10001669 bio_chain(b, bio);
Mike Snitzer075c18c32019-01-18 01:21:11 -05001670 trace_block_split(md->queue, b, bio->bi_iter.bi_sector);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001671 ret = generic_make_request(bio);
NeilBrown18a25da2017-09-06 09:43:28 +10001672 break;
1673 }
1674 }
Tejun Heod87f4c12010-09-03 11:56:19 +02001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* drop the extra reference count */
Bart Van Assche54385bf2017-08-09 11:32:10 -07001678 dec_pending(ci.io, errno_to_blk_status(error));
Mike Snitzer978e51b2017-12-09 15:16:42 -05001679 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682/*
Mike Snitzer978e51b2017-12-09 15:16:42 -05001683 * Optimized variant of __split_and_process_bio that leverages the
1684 * fact that targets that use it do _not_ have a need to split bios.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 */
Mike Snitzer568c73a2019-01-18 14:10:37 -05001686static blk_qc_t __process_bio(struct mapped_device *md, struct dm_table *map,
1687 struct bio *bio, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Mike Snitzer978e51b2017-12-09 15:16:42 -05001689 struct clone_info ci;
1690 blk_qc_t ret = BLK_QC_T_NONE;
1691 int error = 0;
1692
Mike Snitzer978e51b2017-12-09 15:16:42 -05001693 init_clone_info(&ci, md, map, bio);
1694
1695 if (bio->bi_opf & REQ_PREFLUSH) {
Jens Axboedbe3ece2018-12-19 09:13:34 -07001696 struct bio flush_bio;
1697
1698 /*
1699 * Use an on-stack bio for this, it's safe since we don't
1700 * need to reference it after submit. It's just used as
1701 * the basis for the clone(s).
1702 */
1703 bio_init(&flush_bio, NULL, 0);
1704 flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1705 ci.bio = &flush_bio;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001706 ci.sector_count = 0;
1707 error = __send_empty_flush(&ci);
Christoph Hellwig382761d2020-06-27 09:31:46 +02001708 bio_uninit(ci.bio);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001709 /* dec_pending submits any data associated with flush */
1710 } else {
Mike Snitzer978e51b2017-12-09 15:16:42 -05001711 struct dm_target_io *tio;
1712
Mike Snitzer978e51b2017-12-09 15:16:42 -05001713 ci.bio = bio;
1714 ci.sector_count = bio_sectors(bio);
Mike Snitzer568c73a2019-01-18 14:10:37 -05001715 if (__process_abnormal_io(&ci, ti, &error))
Mike Snitzer0519c712018-03-26 11:49:16 -04001716 goto out;
1717
1718 tio = alloc_tio(&ci, ti, 0, GFP_NOIO);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001719 ret = __clone_and_map_simple_bio(&ci, tio, NULL);
1720 }
1721out:
1722 /* drop the extra reference count */
1723 dec_pending(ci.io, errno_to_blk_status(error));
1724 return ret;
1725}
1726
Mike Snitzer568c73a2019-01-18 14:10:37 -05001727static void dm_queue_split(struct mapped_device *md, struct dm_target *ti, struct bio **bio)
1728{
1729 unsigned len, sector_count;
1730
1731 sector_count = bio_sectors(*bio);
1732 len = min_t(sector_t, max_io_len((*bio)->bi_iter.bi_sector, ti), sector_count);
1733
1734 if (sector_count > len) {
1735 struct bio *split = bio_split(*bio, len, GFP_NOIO, &md->queue->bio_split);
1736
1737 bio_chain(split, *bio);
1738 trace_block_split(md->queue, split, (*bio)->bi_iter.bi_sector);
1739 generic_make_request(*bio);
1740 *bio = split;
1741 }
1742}
1743
Mike Snitzer6548c7c2019-01-17 14:33:01 -05001744static blk_qc_t dm_process_bio(struct mapped_device *md,
1745 struct dm_table *map, struct bio *bio)
1746{
Mike Snitzer568c73a2019-01-18 14:10:37 -05001747 blk_qc_t ret = BLK_QC_T_NONE;
1748 struct dm_target *ti = md->immutable_target;
1749
1750 if (unlikely(!map)) {
1751 bio_io_error(bio);
1752 return ret;
1753 }
1754
1755 if (!ti) {
1756 ti = dm_table_find_target(map, bio->bi_iter.bi_sector);
Mikulas Patocka123d87d2019-08-23 09:55:26 -04001757 if (unlikely(!ti)) {
Mike Snitzer568c73a2019-01-18 14:10:37 -05001758 bio_io_error(bio);
1759 return ret;
1760 }
1761 }
1762
1763 /*
1764 * If in ->make_request_fn we need to use blk_queue_split(), otherwise
1765 * queue_limits for abnormal requests (e.g. discard, writesame, etc)
1766 * won't be imposed.
1767 */
1768 if (current->bio_list) {
Mike Snitzer120c9252020-04-02 19:36:26 -04001769 if (is_abnormal_io(bio))
1770 blk_queue_split(md->queue, &bio);
1771 else
Mike Snitzer568c73a2019-01-18 14:10:37 -05001772 dm_queue_split(md, ti, &bio);
1773 }
1774
Mike Snitzer6548c7c2019-01-17 14:33:01 -05001775 if (dm_get_md_type(md) == DM_TYPE_NVME_BIO_BASED)
Mike Snitzer568c73a2019-01-18 14:10:37 -05001776 return __process_bio(md, map, bio, ti);
Mike Snitzer6548c7c2019-01-17 14:33:01 -05001777 else
1778 return __split_and_process_bio(md, map, bio);
1779}
1780
Mikulas Patocka24113d42018-11-06 22:34:59 +01001781static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct mapped_device *md = q->queuedata;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001784 blk_qc_t ret = BLK_QC_T_NONE;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001785 int srcu_idx;
1786 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Christoph Hellwigac7c5672020-05-16 20:28:01 +02001788 if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) {
1789 /*
1790 * We are called with a live reference on q_usage_counter, but
1791 * that one will be released as soon as we return. Grab an
1792 * extra one as blk_mq_make_request expects to be able to
1793 * consume a reference (which lives until the request is freed
1794 * in case a request is allocated).
1795 */
1796 percpu_ref_get(&q->q_usage_counter);
Christoph Hellwig8cf79612020-04-25 09:53:36 +02001797 return blk_mq_make_request(q, bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +02001798 }
Christoph Hellwig8cf79612020-04-25 09:53:36 +02001799
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001800 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Tejun Heo6a8736d2010-09-08 18:07:00 +02001802 /* if we're suspended, we have to queue this io for later */
1803 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001804 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Jens Axboe1eff9d32016-08-05 15:35:16 -06001806 if (!(bio->bi_opf & REQ_RAHEAD))
Tejun Heo6a8736d2010-09-08 18:07:00 +02001807 queue_io(md, bio);
1808 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001809 bio_io_error(bio);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001810 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Mike Snitzer6548c7c2019-01-17 14:33:01 -05001813 ret = dm_process_bio(md, map, bio);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001814
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001815 dm_put_live_table(md, srcu_idx);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001816 return ret;
1817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819static int dm_any_congested(void *congested_data, int bdi_bits)
1820{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001821 int r = bdi_bits;
1822 struct mapped_device *md = congested_data;
1823 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001825 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05001826 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001827 /*
Mike Snitzere522c032016-02-02 22:35:06 -05001828 * With request-based DM we only need to check the
1829 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001830 */
Hou Tao974f51e2020-03-03 16:45:01 +08001831 struct backing_dev_info *bdi = md->queue->backing_dev_info;
1832 r = bdi->wb.congested->state & bdi_bits;
Mike Snitzere522c032016-02-02 22:35:06 -05001833 } else {
1834 map = dm_get_live_table_fast(md);
1835 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001836 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05001837 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001838 }
1839 }
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return r;
1842}
1843
1844/*-----------------------------------------------------------------
1845 * An IDR is used to keep track of allocated minor numbers.
1846 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001847static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001849 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001851 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
1854/*
1855 * See if the device with a specific minor # is free.
1856 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001857static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001859 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 if (minor >= (1 << MINORBITS))
1862 return -EINVAL;
1863
Tejun Heoc9d76be2013-02-27 17:04:26 -08001864 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001865 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Tejun Heoc9d76be2013-02-27 17:04:26 -08001867 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001869 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001870 idr_preload_end();
1871 if (r < 0)
1872 return r == -ENOSPC ? -EBUSY : r;
1873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001876static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001878 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Tejun Heoc9d76be2013-02-27 17:04:26 -08001880 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001881 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Tejun Heoc9d76be2013-02-27 17:04:26 -08001883 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001885 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001886 idr_preload_end();
1887 if (r < 0)
1888 return r;
1889 *minor = r;
1890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001893static const struct block_device_operations dm_blk_dops;
Dan Williamsf26c5712017-04-12 12:35:44 -07001894static const struct dax_operations dm_dax_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Mikulas Patocka53d59142009-04-02 19:55:37 +01001896static void dm_wq_work(struct work_struct *work);
1897
Mike Snitzer0f209722015-04-28 11:50:29 -04001898static void cleanup_mapped_device(struct mapped_device *md)
1899{
Mike Snitzer0f209722015-04-28 11:50:29 -04001900 if (md->wq)
1901 destroy_workqueue(md->wq);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001902 bioset_exit(&md->bs);
1903 bioset_exit(&md->io_bs);
Mike Snitzer0f209722015-04-28 11:50:29 -04001904
Dan Williamsf26c5712017-04-12 12:35:44 -07001905 if (md->dax_dev) {
1906 kill_dax(md->dax_dev);
1907 put_dax(md->dax_dev);
1908 md->dax_dev = NULL;
1909 }
1910
Mike Snitzer0f209722015-04-28 11:50:29 -04001911 if (md->disk) {
1912 spin_lock(&_minor_lock);
1913 md->disk->private_data = NULL;
1914 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04001915 del_gendisk(md->disk);
1916 put_disk(md->disk);
1917 }
1918
1919 if (md->queue)
1920 blk_cleanup_queue(md->queue);
1921
Tahsin Erdogand09960b2016-10-10 05:35:19 -07001922 cleanup_srcu_struct(&md->io_barrier);
1923
Mike Snitzer0f209722015-04-28 11:50:29 -04001924 if (md->bdev) {
1925 bdput(md->bdev);
1926 md->bdev = NULL;
1927 }
Mike Snitzer4cc96132016-05-12 16:28:10 -04001928
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05001929 mutex_destroy(&md->suspend_lock);
1930 mutex_destroy(&md->type_lock);
1931 mutex_destroy(&md->table_devices_lock);
1932
Mike Snitzer4cc96132016-05-12 16:28:10 -04001933 dm_mq_cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/*
1937 * Allocate and initialise a blank device with a given minor.
1938 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001939static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Mike Snitzer115485e2016-02-22 12:16:21 -05001941 int r, numa_node_id = dm_get_numa_node();
1942 struct mapped_device *md;
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001943 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Mikulas Patocka856eb092017-10-31 19:33:02 -04001945 md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (!md) {
1947 DMWARN("unable to allocate device, out of memory.");
1948 return NULL;
1949 }
1950
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001951 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001952 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001955 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001956 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001957 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001958 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001960 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001962 r = init_srcu_struct(&md->io_barrier);
1963 if (r < 0)
1964 goto bad_io_barrier;
1965
Mike Snitzer115485e2016-02-22 12:16:21 -05001966 md->numa_node_id = numa_node_id;
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001967 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01001968 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001969 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001970 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001971 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001972 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001974 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001976 atomic_set(&md->uevent_seq, 0);
1977 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001978 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001979 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Mike Snitzer47ace7e2020-01-27 14:07:23 -05001981 /*
1982 * default to bio-based required ->make_request_fn until DM
1983 * table is loaded and md->type established. If request-based
1984 * table is loaded: blk-mq will override accordingly.
1985 */
Christoph Hellwig3d745ea2020-03-27 09:30:11 +01001986 md->queue = blk_alloc_queue(dm_make_request, numa_node_id);
1987 if (!md->queue)
1988 goto bad;
1989 md->queue->queuedata = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Mike Snitzerc12c9a32018-01-12 09:32:21 -05001991 md->disk = alloc_disk_node(1, md->numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04001993 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001995 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001996 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001997 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05001998 init_completion(&md->kobj_holder.completion);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 md->disk->major = _major;
2001 md->disk->first_minor = minor;
2002 md->disk->fops = &dm_blk_dops;
2003 md->disk->queue = md->queue;
2004 md->disk->private_data = md;
2005 sprintf(md->disk->disk_name, "dm-%d", minor);
Dan Williamsf26c5712017-04-12 12:35:44 -07002006
Dan Williams976431b2018-03-29 17:22:13 -07002007 if (IS_ENABLED(CONFIG_DAX_DRIVER)) {
Pankaj Guptafefc1d92019-07-05 19:33:24 +05302008 md->dax_dev = alloc_dax(md, md->disk->disk_name,
2009 &dm_dax_ops, 0);
Vivek Goyal4e4ced92020-04-01 12:11:25 -04002010 if (IS_ERR(md->dax_dev))
Dan Williams976431b2018-03-29 17:22:13 -07002011 goto bad;
2012 }
Dan Williamsf26c5712017-04-12 12:35:44 -07002013
Mike Snitzerc100ec42018-01-08 20:03:04 -05002014 add_disk_no_queue_reg(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002015 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Tejun Heo670368a2013-07-30 08:40:21 -04002017 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002018 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002019 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002020
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002021 md->bdev = bdget_disk(md->disk, 0);
2022 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002023 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002024
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002025 dm_stats_init(&md->stats);
2026
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002027 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002028 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002029 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002030 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002031
2032 BUG_ON(old_md != MINOR_ALLOCED);
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return md;
2035
Mike Snitzer0f209722015-04-28 11:50:29 -04002036bad:
2037 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002038bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002040bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002041 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002042bad_module_get:
Mikulas Patocka856eb092017-10-31 19:33:02 -04002043 kvfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return NULL;
2045}
2046
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002047static void unlock_fs(struct mapped_device *md);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049static void free_dev(struct mapped_device *md)
2050{
Tejun Heof331c022008-09-03 09:01:48 +02002051 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002052
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002053 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002054
Mike Snitzer0f209722015-04-28 11:50:29 -04002055 cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04002056
2057 free_table_devices(&md->table_devices);
2058 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002059 free_minor(minor);
2060
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002061 module_put(THIS_MODULE);
Mikulas Patocka856eb092017-10-31 19:33:02 -04002062 kvfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002065static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002066{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002067 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002068 int ret = 0;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002069
Mike Snitzer0776aa02017-12-08 14:40:52 -05002070 if (dm_table_bio_based(t)) {
Mike Snitzer64f52b02017-12-11 23:17:47 -05002071 /*
2072 * The md may already have mempools that need changing.
2073 * If so, reload bioset because front_pad may have changed
2074 * because a different table was loaded.
2075 */
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002076 bioset_exit(&md->bs);
2077 bioset_exit(&md->io_bs);
Mike Snitzer0776aa02017-12-08 14:40:52 -05002078
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002079 } else if (bioset_initialized(&md->bs)) {
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002080 /*
2081 * There's no need to reload with request-based dm
2082 * because the size of front_pad doesn't change.
2083 * Note for future: If you are to reload bioset,
2084 * prep-ed requests in the queue may refer
2085 * to bio from the old bioset, so you must walk
2086 * through the queue to unprep.
2087 */
2088 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002089 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002090
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002091 BUG_ON(!p ||
2092 bioset_initialized(&md->bs) ||
2093 bioset_initialized(&md->io_bs));
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002094
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002095 ret = bioset_init_from_src(&md->bs, &p->bs);
2096 if (ret)
2097 goto out;
2098 ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
2099 if (ret)
2100 bioset_exit(&md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002101out:
Mike Snitzer02233342015-03-10 23:49:26 -04002102 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002103 dm_table_free_md_mempools(t);
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002104 return ret;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002105}
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107/*
2108 * Bind a table to the device.
2109 */
2110static void event_callback(void *context)
2111{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002112 unsigned long flags;
2113 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct mapped_device *md = (struct mapped_device *) context;
2115
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002116 spin_lock_irqsave(&md->uevent_lock, flags);
2117 list_splice_init(&md->uevent_list, &uevents);
2118 spin_unlock_irqrestore(&md->uevent_lock, flags);
2119
Tejun Heoed9e1982008-08-25 19:56:05 +09002120 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 atomic_inc(&md->event_nr);
2123 wake_up(&md->eventq);
Mikulas Patocka62e08242017-09-20 07:29:49 -04002124 dm_issue_global_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
Mike Snitzerc2176492011-01-13 19:53:46 +00002127/*
2128 * Protected by md->suspend_lock obtained by dm_swap_table().
2129 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002130static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Bart Van Assche1ea06542017-04-27 10:11:21 -07002132 lockdep_assert_held(&md->suspend_lock);
2133
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002134 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002136 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002139/*
2140 * Returns old map, which caller must destroy.
2141 */
2142static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2143 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002145 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002146 struct request_queue *q = md->queue;
Mike Snitzer978e51b2017-12-09 15:16:42 -05002147 bool request_based = dm_table_request_based(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 sector_t size;
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002149 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07002151 lockdep_assert_held(&md->suspend_lock);
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002154
2155 /*
2156 * Wipe any geometry if the size of the table changed.
2157 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002158 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002159 memset(&md->geometry, 0, sizeof(md->geometry));
2160
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002161 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002163 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002164
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002165 /*
2166 * The queue hasn't been stopped yet, if the old table type wasn't
2167 * for request-based during suspension. So stop it to prevent
2168 * I/O mapping before resume.
2169 * This must be done before setting the queue restrictions,
2170 * because request-based dm may be run just after the setting.
2171 */
Mike Snitzer978e51b2017-12-09 15:16:42 -05002172 if (request_based)
Mike Snitzereca7ee62016-02-20 13:45:38 -05002173 dm_stop_queue(q);
Mike Snitzer978e51b2017-12-09 15:16:42 -05002174
2175 if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) {
Mike Snitzer16f12262016-01-31 17:22:27 -05002176 /*
Mike Snitzer978e51b2017-12-09 15:16:42 -05002177 * Leverage the fact that request-based DM targets and
2178 * NVMe bio based targets are immutable singletons
2179 * - used to optimize both dm_request_fn and dm_mq_queue_rq;
2180 * and __process_bio.
Mike Snitzer16f12262016-01-31 17:22:27 -05002181 */
2182 md->immutable_target = dm_table_get_immutable_target(t);
2183 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002184
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002185 ret = __bind_mempools(md, t);
2186 if (ret) {
2187 old_map = ERR_PTR(ret);
2188 goto out;
2189 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002190
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002191 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -05002192 rcu_assign_pointer(md->map, (void *)t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002193 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2194
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002195 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002196 if (old_map)
2197 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002198
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002199out:
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002200 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Alasdair G Kergona7940152009-12-10 23:52:23 +00002203/*
2204 * Returns unbound table for the caller to free.
2205 */
2206static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002208 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002211 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302214 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002215 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002216
2217 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
2220/*
2221 * Constructor for a new device.
2222 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002223int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Mike Snitzerc12c9a32018-01-12 09:32:21 -05002225 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 struct mapped_device *md;
2227
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002228 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (!md)
2230 return -ENXIO;
2231
Mike Snitzerc12c9a32018-01-12 09:32:21 -05002232 r = dm_sysfs_init(md);
2233 if (r) {
2234 free_dev(md);
2235 return r;
2236 }
Milan Broz784aae72009-01-06 03:05:12 +00002237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 *result = md;
2239 return 0;
2240}
2241
Mike Snitzera5664da2010-08-12 04:14:01 +01002242/*
2243 * Functions to manage md->type.
2244 * All are required to hold md->type_lock.
2245 */
2246void dm_lock_md_type(struct mapped_device *md)
2247{
2248 mutex_lock(&md->type_lock);
2249}
2250
2251void dm_unlock_md_type(struct mapped_device *md)
2252{
2253 mutex_unlock(&md->type_lock);
2254}
2255
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002256void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
Mike Snitzera5664da2010-08-12 04:14:01 +01002257{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002258 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002259 md->type = type;
2260}
2261
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002262enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
Mike Snitzera5664da2010-08-12 04:14:01 +01002263{
2264 return md->type;
2265}
2266
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002267struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2268{
2269 return md->immutable_target_type;
2270}
2271
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002272/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002273 * The queue_limits are only valid as long as you have a reference
2274 * count on 'md'.
2275 */
2276struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2277{
2278 BUG_ON(!atomic_read(&md->holders));
2279 return &md->queue->limits;
2280}
2281EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2282
Hou Tao974f51e2020-03-03 16:45:01 +08002283static void dm_init_congested_fn(struct mapped_device *md)
2284{
2285 md->queue->backing_dev_info->congested_data = md;
2286 md->queue->backing_dev_info->congested_fn = dm_any_congested;
2287}
2288
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002289/*
2290 * Setup the DM device's queue based on md's type
2291 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002292int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002293{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002294 int r;
Mike Snitzerc100ec42018-01-08 20:03:04 -05002295 struct queue_limits limits;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002296 enum dm_queue_mode type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002297
Toshi Kani545ed202016-06-22 17:54:53 -06002298 switch (type) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002299 case DM_TYPE_REQUEST_BASED:
Mike Snitzere83068a2016-05-24 21:16:51 -04002300 r = dm_mq_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002301 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002302 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002303 return r;
2304 }
Hou Tao974f51e2020-03-03 16:45:01 +08002305 dm_init_congested_fn(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002306 break;
2307 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002308 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzer978e51b2017-12-09 15:16:42 -05002309 case DM_TYPE_NVME_BIO_BASED:
Hou Tao974f51e2020-03-03 16:45:01 +08002310 dm_init_congested_fn(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002311 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002312 case DM_TYPE_NONE:
2313 WARN_ON_ONCE(true);
2314 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002315 }
2316
Mike Snitzerc100ec42018-01-08 20:03:04 -05002317 r = dm_calculate_queue_limits(t, &limits);
2318 if (r) {
2319 DMERR("Cannot calculate initial queue limits");
2320 return r;
2321 }
2322 dm_table_set_restrictions(t, md->queue, &limits);
2323 blk_register_queue(md->disk);
2324
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002325 return 0;
2326}
2327
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002328struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 unsigned minor = MINOR(dev);
2332
2333 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2334 return NULL;
2335
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002336 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 md = idr_find(&_minor_idr, minor);
Mike Snitzer49de5762017-11-06 16:40:10 -05002339 if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
2340 test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2341 md = NULL;
2342 goto out;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002343 }
Mike Snitzer49de5762017-11-06 16:40:10 -05002344 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002345out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002346 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
David Teigland637842c2006-01-06 00:20:00 -08002348 return md;
2349}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002350EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002351
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002352void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002353{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002354 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355}
2356
2357void dm_set_mdptr(struct mapped_device *md, void *ptr)
2358{
2359 md->interface_ptr = ptr;
2360}
2361
2362void dm_get(struct mapped_device *md)
2363{
2364 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002365 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002368int dm_hold(struct mapped_device *md)
2369{
2370 spin_lock(&_minor_lock);
2371 if (test_bit(DMF_FREEING, &md->flags)) {
2372 spin_unlock(&_minor_lock);
2373 return -EBUSY;
2374 }
2375 dm_get(md);
2376 spin_unlock(&_minor_lock);
2377 return 0;
2378}
2379EXPORT_SYMBOL_GPL(dm_hold);
2380
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002381const char *dm_device_name(struct mapped_device *md)
2382{
2383 return md->name;
2384}
2385EXPORT_SYMBOL_GPL(dm_device_name);
2386
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002387static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002389 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002390 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002392 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002393
Mike Snitzer63a4f062015-03-23 17:01:43 -04002394 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002395 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2396 set_bit(DMF_FREEING, &md->flags);
2397 spin_unlock(&_minor_lock);
2398
Mike Snitzerc12c9a32018-01-12 09:32:21 -05002399 blk_set_queue_dying(md->queue);
Bart Van Assche3b785fb2016-08-31 15:17:49 -07002400
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002401 /*
2402 * Take suspend_lock so that presuspend and postsuspend methods
2403 * do not race with internal suspend.
2404 */
2405 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002406 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002407 if (!dm_suspended_md(md)) {
2408 dm_table_presuspend_targets(map);
Mikulas Patockaadc0daa2020-02-24 10:20:28 +01002409 set_bit(DMF_SUSPENDED, &md->flags);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002410 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002412 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2413 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002414 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002415
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002416 /*
2417 * Rare, but there may be I/O requests still going to complete,
2418 * for example. Wait for all references to disappear.
2419 * No one should increment the reference count of the mapped_device,
2420 * after the mapped_device state becomes DMF_FREEING.
2421 */
2422 if (wait)
2423 while (atomic_read(&md->holders))
2424 msleep(1);
2425 else if (atomic_read(&md->holders))
2426 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2427 dm_device_name(md), atomic_read(&md->holders));
2428
2429 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002430 dm_table_destroy(__unbind(md));
2431 free_dev(md);
2432}
2433
2434void dm_destroy(struct mapped_device *md)
2435{
2436 __dm_destroy(md, true);
2437}
2438
2439void dm_destroy_immediate(struct mapped_device *md)
2440{
2441 __dm_destroy(md, false);
2442}
2443
2444void dm_put(struct mapped_device *md)
2445{
2446 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
Edward Goggin79eb8852007-05-09 02:32:56 -07002448EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Ming Lei85067742020-06-24 16:00:58 -04002450static bool md_in_flight_bios(struct mapped_device *md)
2451{
2452 int cpu;
2453 struct hd_struct *part = &dm_disk(md)->part0;
2454 long sum = 0;
2455
2456 for_each_possible_cpu(cpu) {
2457 sum += part_stat_local_read_cpu(part, in_flight[0], cpu);
2458 sum += part_stat_local_read_cpu(part, in_flight[1], cpu);
2459 }
2460
2461 return sum != 0;
2462}
2463
2464static int dm_wait_for_bios_completion(struct mapped_device *md, long task_state)
Milan Broz46125c12008-02-08 02:10:30 +00002465{
2466 int r = 0;
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002467 DEFINE_WAIT(wait);
Milan Broz46125c12008-02-08 02:10:30 +00002468
Ming Lei85067742020-06-24 16:00:58 -04002469 while (true) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002470 prepare_to_wait(&md->wait, &wait, task_state);
Milan Broz46125c12008-02-08 02:10:30 +00002471
Ming Lei85067742020-06-24 16:00:58 -04002472 if (!md_in_flight_bios(md))
Milan Broz46125c12008-02-08 02:10:30 +00002473 break;
2474
Bart Van Asschee3fabdf2016-08-31 15:16:22 -07002475 if (signal_pending_state(task_state, current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002476 r = -EINTR;
2477 break;
2478 }
2479
2480 io_schedule();
2481 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002482 finish_wait(&md->wait, &wait);
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002483
Milan Broz46125c12008-02-08 02:10:30 +00002484 return r;
2485}
2486
Ming Lei85067742020-06-24 16:00:58 -04002487static int dm_wait_for_completion(struct mapped_device *md, long task_state)
2488{
2489 int r = 0;
2490
2491 if (!queue_is_mq(md->queue))
2492 return dm_wait_for_bios_completion(md, task_state);
2493
2494 while (true) {
2495 if (!blk_mq_queue_inflight(md->queue))
2496 break;
2497
2498 if (signal_pending_state(task_state, current)) {
2499 r = -EINTR;
2500 break;
2501 }
2502
2503 msleep(5);
2504 }
2505
2506 return r;
2507}
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509/*
2510 * Process the deferred bios
2511 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002512static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Mikulas Patockaef208582009-04-02 19:55:38 +01002514 struct mapped_device *md = container_of(work, struct mapped_device,
2515 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002516 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002517 int srcu_idx;
2518 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002520 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002521
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002522 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002523 spin_lock_irq(&md->deferred_lock);
2524 c = bio_list_pop(&md->deferred);
2525 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002526
Tejun Heo6a8736d2010-09-08 18:07:00 +02002527 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002528 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002529
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002530 if (dm_request_based(md))
Mike Snitzer6548c7c2019-01-17 14:33:01 -05002531 (void) generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002532 else
Mike Snitzer6548c7c2019-01-17 14:33:01 -05002533 (void) dm_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002534 }
Milan Broz73d410c2008-02-08 02:10:25 +00002535
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002536 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002539static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002540{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002541 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002542 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002543 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002547 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002549struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Mike Christie87eb5b22013-03-01 22:45:48 +00002551 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002552 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002553 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Daniel Walkere61290a2008-02-08 02:10:08 +00002555 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002558 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Mike Snitzer3ae70652012-09-26 23:45:45 +01002561 /*
2562 * If the new table has no data devices, retain the existing limits.
2563 * This helps multipath with queue_if_no_path if all paths disappear,
2564 * then new I/O is queued based on these limits, and then some paths
2565 * reappear.
2566 */
2567 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002568 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002569 if (live_map)
2570 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002571 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002572 }
2573
Mike Christie87eb5b22013-03-01 22:45:48 +00002574 if (!live_map) {
2575 r = dm_calculate_queue_limits(table, &limits);
2576 if (r) {
2577 map = ERR_PTR(r);
2578 goto out;
2579 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002580 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002581
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002582 map = __bind(md, table, &limits);
Mikulas Patocka62e08242017-09-20 07:29:49 -04002583 dm_issue_global_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002585out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002586 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002587 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590/*
2591 * Functions to lock and unlock any filesystem running on the
2592 * device.
2593 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002594static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002596 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002599
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002600 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002601 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002602 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002603 md->frozen_sb = NULL;
2604 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002605 }
2606
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002607 set_bit(DMF_FROZEN, &md->flags);
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return 0;
2610}
2611
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002612static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002614 if (!test_bit(DMF_FROZEN, &md->flags))
2615 return;
2616
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002617 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002619 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
2622/*
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002623 * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2624 * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2625 * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2626 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04002627 * If __dm_suspend returns 0, the device is completely quiescent
2628 * now. There is no request-processing activity. All new requests
2629 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002630 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04002631static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002632 unsigned suspend_flags, long task_state,
Mike Snitzereaf9a732016-08-02 13:07:20 -04002633 int dmf_suspended_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002635 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2636 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2637 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07002639 lockdep_assert_held(&md->suspend_lock);
2640
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002641 /*
2642 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2643 * This flag is cleared before dm_suspend returns.
2644 */
2645 if (noflush)
2646 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Bart Van Assche86331f32017-04-27 10:11:26 -07002647 else
Mike Snitzerac75b09f2020-05-14 12:55:39 -04002648 DMDEBUG("%s: suspending with flush", dm_device_name(md));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002649
Mike Snitzerd67ee212014-10-28 20:13:31 -04002650 /*
2651 * This gets reverted if there's an error later and the targets
2652 * provide the .presuspend_undo hook.
2653 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002654 dm_table_presuspend_targets(map);
2655
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002656 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002657 * Flush I/O to the device.
2658 * Any I/O submitted after lock_fs() may not be flushed.
2659 * noflush takes precedence over do_lockfs.
2660 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002661 */
2662 if (!noflush && do_lockfs) {
2663 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002664 if (r) {
2665 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002666 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04002667 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002671 * Here we must make sure that no processes are submitting requests
2672 * to target drivers i.e. no one may be executing
2673 * __split_and_process_bio. This is called from dm_request and
2674 * dm_wq_work.
2675 *
2676 * To get all processes out of __split_and_process_bio in dm_request,
2677 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002678 * __split_and_process_bio from dm_request and quiesce the thread
2679 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2680 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002682 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002683 if (map)
2684 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002686 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002687 * Stop md->queue before flushing md->wq in case request-based
2688 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002689 */
Jens Axboe6a23e052018-10-10 20:49:26 -06002690 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002691 dm_stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002692
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002693 flush_workqueue(md->wq);
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002696 * At this point no more requests are entering target request routines.
2697 * We call dm_wait_for_completion to wait for all existing requests
2698 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 */
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002700 r = dm_wait_for_completion(md, task_state);
Mike Snitzereaf9a732016-08-02 13:07:20 -04002701 if (!r)
2702 set_bit(dmf_suspended_flag, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Milan Broz6d6f10d2008-02-08 02:10:22 +00002704 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002705 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002706 if (map)
2707 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002710 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002711 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002712
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002713 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002714 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002715
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002716 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002717 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002718 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002719 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002720
Mike Snitzerffcc3932014-10-28 18:34:52 -04002721 return r;
2722}
2723
2724/*
2725 * We need to be able to change a mapping table under a mounted
2726 * filesystem. For example we might want to move some data in
2727 * the background. Before the table can be swapped with
2728 * dm_bind_table, dm_suspend must be called to flush any in
2729 * flight bios and ensure that any further io gets deferred.
2730 */
2731/*
2732 * Suspend mechanism in request-based dm.
2733 *
2734 * 1. Flush all I/Os by lock_fs() if needed.
2735 * 2. Stop dispatching any I/O by stopping the request_queue.
2736 * 3. Wait for all in-flight I/Os to be completed or requeued.
2737 *
2738 * To abort suspend, start the request_queue.
2739 */
2740int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2741{
2742 struct dm_table *map = NULL;
2743 int r = 0;
2744
2745retry:
2746 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2747
2748 if (dm_suspended_md(md)) {
2749 r = -EINVAL;
2750 goto out_unlock;
2751 }
2752
2753 if (dm_suspended_internally_md(md)) {
2754 /* already internally suspended, wait for internal resume */
2755 mutex_unlock(&md->suspend_lock);
2756 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2757 if (r)
2758 return r;
2759 goto retry;
2760 }
2761
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002762 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002763
Mike Snitzereaf9a732016-08-02 13:07:20 -04002764 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002765 if (r)
2766 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002767
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002768 dm_table_postsuspend_targets(map);
2769
Alasdair G Kergond2874832006-11-08 17:44:43 -08002770out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002771 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002772 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
Mike Snitzerffcc3932014-10-28 18:34:52 -04002775static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002777 if (map) {
2778 int r = dm_table_resume_targets(map);
2779 if (r)
2780 return r;
2781 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002782
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002783 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002784
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002785 /*
2786 * Flushing deferred I/Os must be done after targets are resumed
2787 * so that mapping of targets can work correctly.
2788 * Request-based dm is queueing the deferred I/Os in its request_queue.
2789 */
2790 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002791 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002792
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002793 unlock_fs(md);
2794
Mike Snitzerffcc3932014-10-28 18:34:52 -04002795 return 0;
2796}
2797
2798int dm_resume(struct mapped_device *md)
2799{
Minfei Huang8dc23652016-09-06 16:00:29 +08002800 int r;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002801 struct dm_table *map = NULL;
2802
2803retry:
Minfei Huang8dc23652016-09-06 16:00:29 +08002804 r = -EINVAL;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002805 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2806
2807 if (!dm_suspended_md(md))
2808 goto out;
2809
2810 if (dm_suspended_internally_md(md)) {
2811 /* already internally suspended, wait for internal resume */
2812 mutex_unlock(&md->suspend_lock);
2813 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2814 if (r)
2815 return r;
2816 goto retry;
2817 }
2818
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002819 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002820 if (!map || !dm_table_get_size(map))
2821 goto out;
2822
2823 r = __dm_resume(md, map);
2824 if (r)
2825 goto out;
2826
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002827 clear_bit(DMF_SUSPENDED, &md->flags);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002828out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002829 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002830
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002831 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002834/*
2835 * Internal suspend/resume works like userspace-driven suspend. It waits
2836 * until all bios finish and prevents issuing new bios to the target drivers.
2837 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002838 */
2839
Mike Snitzerffcc3932014-10-28 18:34:52 -04002840static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2841{
2842 struct dm_table *map = NULL;
2843
Bart Van Assche1ea06542017-04-27 10:11:21 -07002844 lockdep_assert_held(&md->suspend_lock);
2845
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002846 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002847 return; /* nested internal suspend */
2848
2849 if (dm_suspended_md(md)) {
2850 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2851 return; /* nest suspend */
2852 }
2853
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002854 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002855
2856 /*
2857 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2858 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
2859 * would require changing .presuspend to return an error -- avoid this
2860 * until there is a need for more elaborate variants of internal suspend.
2861 */
Mike Snitzereaf9a732016-08-02 13:07:20 -04002862 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2863 DMF_SUSPENDED_INTERNALLY);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002864
2865 dm_table_postsuspend_targets(map);
2866}
2867
2868static void __dm_internal_resume(struct mapped_device *md)
2869{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002870 BUG_ON(!md->internal_suspend_count);
2871
2872 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002873 return; /* resume from nested internal suspend */
2874
2875 if (dm_suspended_md(md))
2876 goto done; /* resume from nested suspend */
2877
2878 /*
2879 * NOTE: existing callers don't need to call dm_table_resume_targets
2880 * (which may fail -- so best to avoid it for now by passing NULL map)
2881 */
2882 (void) __dm_resume(md, NULL);
2883
2884done:
2885 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2886 smp_mb__after_atomic();
2887 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2888}
2889
2890void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002891{
2892 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002893 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2894 mutex_unlock(&md->suspend_lock);
2895}
2896EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2897
2898void dm_internal_resume(struct mapped_device *md)
2899{
2900 mutex_lock(&md->suspend_lock);
2901 __dm_internal_resume(md);
2902 mutex_unlock(&md->suspend_lock);
2903}
2904EXPORT_SYMBOL_GPL(dm_internal_resume);
2905
2906/*
2907 * Fast variants of internal suspend/resume hold md->suspend_lock,
2908 * which prevents interaction with userspace-driven suspend.
2909 */
2910
2911void dm_internal_suspend_fast(struct mapped_device *md)
2912{
2913 mutex_lock(&md->suspend_lock);
2914 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002915 return;
2916
2917 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2918 synchronize_srcu(&md->io_barrier);
2919 flush_workqueue(md->wq);
2920 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2921}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002922EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002923
Mike Snitzerffcc3932014-10-28 18:34:52 -04002924void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002925{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002926 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002927 goto done;
2928
2929 dm_queue_flush(md);
2930
2931done:
2932 mutex_unlock(&md->suspend_lock);
2933}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002934EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936/*-----------------------------------------------------------------
2937 * Event notification.
2938 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002939int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002940 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002941{
Milan Broz60935eb2009-06-22 10:12:30 +01002942 char udev_cookie[DM_COOKIE_LENGTH];
2943 char *envp[] = { udev_cookie, NULL };
2944
2945 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002946 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002947 else {
2948 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2949 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002950 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2951 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002952 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002953}
2954
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002955uint32_t dm_next_uevent_seq(struct mapped_device *md)
2956{
2957 return atomic_add_return(1, &md->uevent_seq);
2958}
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960uint32_t dm_get_event_nr(struct mapped_device *md)
2961{
2962 return atomic_read(&md->event_nr);
2963}
2964
2965int dm_wait_event(struct mapped_device *md, int event_nr)
2966{
2967 return wait_event_interruptible(md->eventq,
2968 (event_nr != atomic_read(&md->event_nr)));
2969}
2970
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002971void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2972{
2973 unsigned long flags;
2974
2975 spin_lock_irqsave(&md->uevent_lock, flags);
2976 list_add(elist, &md->uevent_list);
2977 spin_unlock_irqrestore(&md->uevent_lock, flags);
2978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980/*
2981 * The gendisk is only valid as long as you have a reference
2982 * count on 'md'.
2983 */
2984struct gendisk *dm_disk(struct mapped_device *md)
2985{
2986 return md->disk;
2987}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00002988EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Milan Broz784aae72009-01-06 03:05:12 +00002990struct kobject *dm_kobject(struct mapped_device *md)
2991{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002992 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00002993}
2994
Milan Broz784aae72009-01-06 03:05:12 +00002995struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2996{
2997 struct mapped_device *md;
2998
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002999 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003000
Hou Taob9a41d22017-11-01 15:42:36 +08003001 spin_lock(&_minor_lock);
3002 if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
3003 md = NULL;
3004 goto out;
3005 }
Milan Broz784aae72009-01-06 03:05:12 +00003006 dm_get(md);
Hou Taob9a41d22017-11-01 15:42:36 +08003007out:
3008 spin_unlock(&_minor_lock);
3009
Milan Broz784aae72009-01-06 03:05:12 +00003010 return md;
3011}
3012
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003013int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 return test_bit(DMF_SUSPENDED, &md->flags);
3016}
3017
Mike Snitzerffcc3932014-10-28 18:34:52 -04003018int dm_suspended_internally_md(struct mapped_device *md)
3019{
3020 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3021}
3022
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003023int dm_test_deferred_remove_flag(struct mapped_device *md)
3024{
3025 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3026}
3027
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003028int dm_suspended(struct dm_target *ti)
3029{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003030 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003031}
3032EXPORT_SYMBOL_GPL(dm_suspended);
3033
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003034int dm_noflush_suspending(struct dm_target *ti)
3035{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003036 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003037}
3038EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3039
Bart Van Assche7e0d5742017-04-27 10:11:23 -07003040struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
Mike Snitzer0776aa02017-12-08 14:40:52 -05003041 unsigned integrity, unsigned per_io_data_size,
3042 unsigned min_pool_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003043{
Mike Snitzer115485e2016-02-22 12:16:21 -05003044 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
Mike Snitzer78d8e582015-06-26 10:01:13 -04003045 unsigned int pool_size = 0;
Mike Snitzer64f52b02017-12-11 23:17:47 -05003046 unsigned int front_pad, io_front_pad;
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003047 int ret;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003048
3049 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003050 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003051
Mike Snitzer78d8e582015-06-26 10:01:13 -04003052 switch (type) {
3053 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06003054 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzer22c11852017-12-04 21:07:37 -05003055 case DM_TYPE_NVME_BIO_BASED:
Mike Snitzer0776aa02017-12-08 14:40:52 -05003056 pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
Mike Snitzer30187e12016-01-31 13:28:26 -05003057 front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
Mike Snitzer64f52b02017-12-11 23:17:47 -05003058 io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003059 ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
3060 if (ret)
Mike Snitzer64f52b02017-12-11 23:17:47 -05003061 goto out;
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003062 if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
Christoph Hellwigeb8db832017-01-22 18:32:46 +01003063 goto out;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003064 break;
3065 case DM_TYPE_REQUEST_BASED:
Mike Snitzer0776aa02017-12-08 14:40:52 -05003066 pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
Mike Snitzer78d8e582015-06-26 10:01:13 -04003067 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05003068 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04003069 break;
3070 default:
3071 BUG();
3072 }
3073
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003074 ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
3075 if (ret)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003076 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003077
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003078 if (integrity && bioset_integrity_create(&pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003079 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003080
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003081 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003082
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003083out:
3084 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003085
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003086 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003087}
3088
3089void dm_free_md_mempools(struct dm_md_mempools *pools)
3090{
3091 if (!pools)
3092 return;
3093
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003094 bioset_exit(&pools->bs);
3095 bioset_exit(&pools->io_bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003096
3097 kfree(pools);
3098}
3099
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09003100struct dm_pr {
3101 u64 old_key;
3102 u64 new_key;
3103 u32 flags;
3104 bool fail_early;
3105};
3106
3107static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
3108 void *data)
3109{
3110 struct mapped_device *md = bdev->bd_disk->private_data;
3111 struct dm_table *table;
3112 struct dm_target *ti;
3113 int ret = -ENOTTY, srcu_idx;
3114
3115 table = dm_get_live_table(md, &srcu_idx);
3116 if (!table || !dm_table_get_size(table))
3117 goto out;
3118
3119 /* We only support devices that have a single target */
3120 if (dm_table_get_num_targets(table) != 1)
3121 goto out;
3122 ti = dm_table_get_target(table, 0);
3123
3124 ret = -EINVAL;
3125 if (!ti->type->iterate_devices)
3126 goto out;
3127
3128 ret = ti->type->iterate_devices(ti, fn, data);
3129out:
3130 dm_put_live_table(md, srcu_idx);
3131 return ret;
3132}
3133
3134/*
3135 * For register / unregister we need to manually call out to every path.
3136 */
3137static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
3138 sector_t start, sector_t len, void *data)
3139{
3140 struct dm_pr *pr = data;
3141 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
3142
3143 if (!ops || !ops->pr_register)
3144 return -EOPNOTSUPP;
3145 return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
3146}
3147
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003148static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003149 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003150{
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09003151 struct dm_pr pr = {
3152 .old_key = old_key,
3153 .new_key = new_key,
3154 .flags = flags,
3155 .fail_early = true,
3156 };
3157 int ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003158
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09003159 ret = dm_call_pr(bdev, __dm_pr_register, &pr);
3160 if (ret && new_key) {
3161 /* unregister all paths if we failed to register any path */
3162 pr.old_key = new_key;
3163 pr.new_key = 0;
3164 pr.flags = 0;
3165 pr.fail_early = false;
3166 dm_call_pr(bdev, __dm_pr_register, &pr);
3167 }
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003168
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09003169 return ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003170}
3171
3172static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05003173 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003174{
3175 struct mapped_device *md = bdev->bd_disk->private_data;
3176 const struct pr_ops *ops;
Mike Snitzer971888c2018-04-03 15:05:12 -04003177 int r, srcu_idx;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003178
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04003179 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003180 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -04003181 goto out;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003182
3183 ops = bdev->bd_disk->fops->pr_ops;
3184 if (ops && ops->pr_reserve)
3185 r = ops->pr_reserve(bdev, key, type, flags);
3186 else
3187 r = -EOPNOTSUPP;
Mike Snitzer971888c2018-04-03 15:05:12 -04003188out:
3189 dm_unprepare_ioctl(md, srcu_idx);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003190 return r;
3191}
3192
3193static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3194{
3195 struct mapped_device *md = bdev->bd_disk->private_data;
3196 const struct pr_ops *ops;
Mike Snitzer971888c2018-04-03 15:05:12 -04003197 int r, srcu_idx;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003198
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04003199 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003200 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -04003201 goto out;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003202
3203 ops = bdev->bd_disk->fops->pr_ops;
3204 if (ops && ops->pr_release)
3205 r = ops->pr_release(bdev, key, type);
3206 else
3207 r = -EOPNOTSUPP;
Mike Snitzer971888c2018-04-03 15:05:12 -04003208out:
3209 dm_unprepare_ioctl(md, srcu_idx);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003210 return r;
3211}
3212
3213static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003214 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003215{
3216 struct mapped_device *md = bdev->bd_disk->private_data;
3217 const struct pr_ops *ops;
Mike Snitzer971888c2018-04-03 15:05:12 -04003218 int r, srcu_idx;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003219
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04003220 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003221 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -04003222 goto out;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003223
3224 ops = bdev->bd_disk->fops->pr_ops;
3225 if (ops && ops->pr_preempt)
3226 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3227 else
3228 r = -EOPNOTSUPP;
Mike Snitzer971888c2018-04-03 15:05:12 -04003229out:
3230 dm_unprepare_ioctl(md, srcu_idx);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003231 return r;
3232}
3233
3234static int dm_pr_clear(struct block_device *bdev, u64 key)
3235{
3236 struct mapped_device *md = bdev->bd_disk->private_data;
3237 const struct pr_ops *ops;
Mike Snitzer971888c2018-04-03 15:05:12 -04003238 int r, srcu_idx;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003239
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04003240 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003241 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -04003242 goto out;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003243
3244 ops = bdev->bd_disk->fops->pr_ops;
3245 if (ops && ops->pr_clear)
3246 r = ops->pr_clear(bdev, key);
3247 else
3248 r = -EOPNOTSUPP;
Mike Snitzer971888c2018-04-03 15:05:12 -04003249out:
3250 dm_unprepare_ioctl(md, srcu_idx);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003251 return r;
3252}
3253
3254static const struct pr_ops dm_pr_ops = {
3255 .pr_register = dm_pr_register,
3256 .pr_reserve = dm_pr_reserve,
3257 .pr_release = dm_pr_release,
3258 .pr_preempt = dm_pr_preempt,
3259 .pr_clear = dm_pr_clear,
3260};
3261
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003262static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 .open = dm_blk_open,
3264 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003265 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003266 .getgeo = dm_blk_getgeo,
Christoph Hellwige76239a2018-10-12 19:08:49 +09003267 .report_zones = dm_blk_report_zones,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003268 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 .owner = THIS_MODULE
3270};
3271
Dan Williamsf26c5712017-04-12 12:35:44 -07003272static const struct dax_operations dm_dax_ops = {
3273 .direct_access = dm_dax_direct_access,
Dan Williams7bf7eac2019-05-16 13:26:29 -07003274 .dax_supported = dm_dax_supported,
Dan Williams7e026c82017-05-29 12:57:56 -07003275 .copy_from_iter = dm_dax_copy_from_iter,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -07003276 .copy_to_iter = dm_dax_copy_to_iter,
Vivek Goyalcdf6cdc2020-02-28 11:34:54 -05003277 .zero_page_range = dm_dax_zero_page_range,
Dan Williamsf26c5712017-04-12 12:35:44 -07003278};
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280/*
3281 * module hooks
3282 */
3283module_init(dm_init);
3284module_exit(dm_exit);
3285
3286module_param(major, uint, 0);
3287MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003288
Mike Snitzere8603132013-09-12 18:06:12 -04003289module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3290MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3291
Mike Snitzer115485e2016-02-22 12:16:21 -05003292module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3293MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295MODULE_DESCRIPTION(DM_NAME " driver");
3296MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3297MODULE_LICENSE("GPL");