blob: 76f4cfdd6b419bf9b25bf6fbea4218d1a7971a40 [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>
Li Zefan55782132009-06-09 13:43:05 +080028
Alasdair G Kergon72d94862006-06-26 00:27:35 -070029#define DM_MSG_PREFIX "core"
30
Milan Broz60935eb2009-06-22 10:12:30 +010031/*
32 * Cookies are numeric values sent with CHANGE and REMOVE
33 * uevents while resuming, removing or renaming the device.
34 */
35#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
36#define DM_COOKIE_LENGTH 24
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static const char *_name = DM_NAME;
39
40static unsigned int major = 0;
41static unsigned int _major = 0;
42
Alasdair G Kergond15b7742011-08-02 12:32:01 +010043static DEFINE_IDR(_minor_idr);
44
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070045static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040046
47static void do_deferred_remove(struct work_struct *w);
48
49static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
50
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040051static struct workqueue_struct *deferred_remove_workqueue;
52
Mikulas Patocka93e64422017-01-16 16:05:59 -050053atomic_t dm_global_event_nr = ATOMIC_INIT(0);
54DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
55
Mikulas Patocka62e08242017-09-20 07:29:49 -040056void dm_issue_global_event(void)
57{
58 atomic_inc(&dm_global_event_nr);
59 wake_up(&dm_global_eventq);
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Mike Snitzer64f52b02017-12-11 23:17:47 -050063 * One of these is allocated (on-stack) per original bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Mike Snitzer64f52b02017-12-11 23:17:47 -050065struct clone_info {
Mike Snitzer64f52b02017-12-11 23:17:47 -050066 struct dm_table *map;
67 struct bio *bio;
68 struct dm_io *io;
69 sector_t sector;
70 unsigned sector_count;
71};
72
73/*
74 * One of these is allocated per clone bio.
75 */
76#define DM_TIO_MAGIC 7282014
77struct dm_target_io {
78 unsigned magic;
79 struct dm_io *io;
80 struct dm_target *ti;
81 unsigned target_bio_nr;
82 unsigned *len_ptr;
83 bool inside_dm_io;
84 struct bio clone;
85};
86
87/*
88 * One of these is allocated per original bio.
89 * It contains the first clone used for that original.
90 */
91#define DM_IO_MAGIC 5191977
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct dm_io {
Mike Snitzer64f52b02017-12-11 23:17:47 -050093 unsigned magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct mapped_device *md;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020095 blk_status_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 atomic_t io_count;
Mike Snitzer745dc572017-12-11 20:51:50 -050097 struct bio *orig_bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080098 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010099 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400100 struct dm_stats_aux stats_aux;
Mike Snitzer64f52b02017-12-11 23:17:47 -0500101 /* last member of dm_target_io is 'struct bio' */
102 struct dm_target_io tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
Mike Snitzer64f52b02017-12-11 23:17:47 -0500105void *dm_per_bio_data(struct bio *bio, size_t data_size)
106{
107 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
108 if (!tio->inside_dm_io)
109 return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
110 return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size;
111}
112EXPORT_SYMBOL_GPL(dm_per_bio_data);
113
114struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
115{
116 struct dm_io *io = (struct dm_io *)((char *)data + data_size);
117 if (io->magic == DM_IO_MAGIC)
118 return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone));
119 BUG_ON(io->magic != DM_TIO_MAGIC);
120 return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone));
121}
122EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
123
124unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
125{
126 return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
127}
128EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
129
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700130#define MINOR_ALLOCED ((void *)-1)
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * Bits for the md->flags field.
134 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100135#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800137#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700138#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700139#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800140#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -0700141#define DMF_DEFERRED_REMOVE 6
142#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Mike Snitzer115485e2016-02-22 12:16:21 -0500144#define DM_NUMA_NODE NUMA_NO_NODE
Mike Snitzer115485e2016-02-22 12:16:21 -0500145static int dm_numa_node = DM_NUMA_NODE;
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500146
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100147/*
148 * For mempools pre-allocation at the table loading time.
149 */
150struct dm_md_mempools {
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400151 struct bio_set bs;
152 struct bio_set io_bs;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100153};
154
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500155struct table_device {
156 struct list_head list;
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300157 refcount_t count;
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500158 struct dm_dev dm_dev;
159};
160
Mike Snitzerf4790822013-09-12 18:06:12 -0400161/*
Mike Snitzere8603132013-09-12 18:06:12 -0400162 * Bio-based DM's mempools' reserved IOs set by the user.
163 */
Mike Snitzer4cc96132016-05-12 16:28:10 -0400164#define RESERVED_BIO_BASED_IOS 16
Mike Snitzere8603132013-09-12 18:06:12 -0400165static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
166
Mike Snitzer115485e2016-02-22 12:16:21 -0500167static int __dm_get_module_param_int(int *module_param, int min, int max)
168{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700169 int param = READ_ONCE(*module_param);
Mike Snitzer115485e2016-02-22 12:16:21 -0500170 int modified_param = 0;
171 bool modified = true;
172
173 if (param < min)
174 modified_param = min;
175 else if (param > max)
176 modified_param = max;
177 else
178 modified = false;
179
180 if (modified) {
181 (void)cmpxchg(module_param, param, modified_param);
182 param = modified_param;
183 }
184
185 return param;
186}
187
Mike Snitzer4cc96132016-05-12 16:28:10 -0400188unsigned __dm_get_module_param(unsigned *module_param,
189 unsigned def, unsigned max)
Mike Snitzerf4790822013-09-12 18:06:12 -0400190{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700191 unsigned param = READ_ONCE(*module_param);
Mike Snitzer09c2d532015-02-27 22:25:26 -0500192 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400193
Mike Snitzer09c2d532015-02-27 22:25:26 -0500194 if (!param)
195 modified_param = def;
196 else if (param > max)
197 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400198
Mike Snitzer09c2d532015-02-27 22:25:26 -0500199 if (modified_param) {
200 (void)cmpxchg(module_param, param, modified_param);
201 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400202 }
203
Mike Snitzer09c2d532015-02-27 22:25:26 -0500204 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400205}
206
Mike Snitzere8603132013-09-12 18:06:12 -0400207unsigned dm_get_reserved_bio_based_ios(void)
208{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500209 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzer4cc96132016-05-12 16:28:10 -0400210 RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
Mike Snitzere8603132013-09-12 18:06:12 -0400211}
212EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
213
Mike Snitzer115485e2016-02-22 12:16:21 -0500214static unsigned dm_get_numa_node(void)
215{
216 return __dm_get_module_param_int(&dm_numa_node,
217 DM_NUMA_NODE, num_online_nodes() - 1);
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static int __init local_init(void)
221{
Mike Snitzere689fba2019-02-20 15:37:44 -0500222 int r;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500223
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100224 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100225 if (r)
Mike Snitzere689fba2019-02-20 15:37:44 -0500226 return r;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100227
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400228 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
229 if (!deferred_remove_workqueue) {
230 r = -ENOMEM;
231 goto out_uevent_exit;
232 }
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 _major = major;
235 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100236 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400237 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (!_major)
240 _major = r;
241
242 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100243
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400244out_free_workqueue:
245 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100246out_uevent_exit:
247 dm_uevent_exit();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100248
249 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static void local_exit(void)
253{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400254 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400255 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400256
Akinobu Mita00d59402007-07-17 04:03:46 -0700257 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100258 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 _major = 0;
261
262 DMINFO("cleaned up");
263}
264
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000265static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 local_init,
267 dm_target_init,
268 dm_linear_init,
269 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000270 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100271 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400273 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274};
275
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000276static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 local_exit,
278 dm_target_exit,
279 dm_linear_exit,
280 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000281 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100282 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400284 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
287static int __init dm_init(void)
288{
289 const int count = ARRAY_SIZE(_inits);
290
291 int r, i;
292
293 for (i = 0; i < count; i++) {
294 r = _inits[i]();
295 if (r)
296 goto bad;
297 }
298
299 return 0;
300
301 bad:
302 while (i--)
303 _exits[i]();
304
305 return r;
306}
307
308static void __exit dm_exit(void)
309{
310 int i = ARRAY_SIZE(_exits);
311
312 while (i--)
313 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100314
315 /*
316 * Should be empty by this point.
317 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100318 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/*
322 * Block device functions
323 */
Mike Anderson432a2122009-12-10 23:52:20 +0000324int dm_deleting_md(struct mapped_device *md)
325{
326 return test_bit(DMF_DELETING, &md->flags);
327}
328
Al Virofe5f9f22008-03-02 10:29:31 -0500329static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct mapped_device *md;
332
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700333 spin_lock(&_minor_lock);
334
Al Virofe5f9f22008-03-02 10:29:31 -0500335 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700336 if (!md)
337 goto out;
338
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700339 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000340 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700341 md = NULL;
342 goto out;
343 }
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700346 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700347out:
348 spin_unlock(&_minor_lock);
349
350 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Al Virodb2a1442013-05-05 21:52:57 -0400353static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400355 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200356
Milan Broz4a1aeb92011-01-13 19:59:48 +0000357 spin_lock(&_minor_lock);
358
Mike Snitzer63a4f062015-03-23 17:01:43 -0400359 md = disk->private_data;
360 if (WARN_ON(!md))
361 goto out;
362
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400363 if (atomic_dec_and_test(&md->open_count) &&
364 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400365 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400368out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000369 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700372int dm_open_count(struct mapped_device *md)
373{
374 return atomic_read(&md->open_count);
375}
376
377/*
378 * Guarantees nothing is using the device before it's deleted.
379 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400380int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700381{
382 int r = 0;
383
384 spin_lock(&_minor_lock);
385
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400386 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700387 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400388 if (mark_deferred)
389 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
390 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
391 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700392 else
393 set_bit(DMF_DELETING, &md->flags);
394
395 spin_unlock(&_minor_lock);
396
397 return r;
398}
399
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400400int dm_cancel_deferred_remove(struct mapped_device *md)
401{
402 int r = 0;
403
404 spin_lock(&_minor_lock);
405
406 if (test_bit(DMF_DELETING, &md->flags))
407 r = -EBUSY;
408 else
409 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
410
411 spin_unlock(&_minor_lock);
412
413 return r;
414}
415
416static void do_deferred_remove(struct work_struct *w)
417{
418 dm_deferred_remove();
419}
420
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400421sector_t dm_get_size(struct mapped_device *md)
422{
423 return get_capacity(md->disk);
424}
425
Mike Snitzer9974fa22014-02-28 15:33:43 +0100426struct request_queue *dm_get_md_queue(struct mapped_device *md)
427{
428 return md->queue;
429}
430
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400431struct dm_stats *dm_get_stats(struct mapped_device *md)
432{
433 return &md->stats;
434}
435
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800436static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
437{
438 struct mapped_device *md = bdev->bd_disk->private_data;
439
440 return dm_get_geometry(md, geo);
441}
442
Christoph Hellwige76239a2018-10-12 19:08:49 +0900443static int dm_blk_report_zones(struct gendisk *disk, sector_t sector,
Damien Le Moalbd976e52019-07-01 14:09:16 +0900444 struct blk_zone *zones, unsigned int *nr_zones)
Christoph Hellwige76239a2018-10-12 19:08:49 +0900445{
446#ifdef CONFIG_BLK_DEV_ZONED
447 struct mapped_device *md = disk->private_data;
448 struct dm_target *tgt;
449 struct dm_table *map;
450 int srcu_idx, ret;
451
452 if (dm_suspended_md(md))
453 return -EAGAIN;
454
455 map = dm_get_live_table(md, &srcu_idx);
456 if (!map)
457 return -EIO;
458
459 tgt = dm_table_find_target(map, sector);
Mikulas Patocka123d87d2019-08-23 09:55:26 -0400460 if (!tgt) {
Christoph Hellwige76239a2018-10-12 19:08:49 +0900461 ret = -EIO;
462 goto out;
463 }
464
465 /*
466 * If we are executing this, we already know that the block device
467 * is a zoned device and so each target should have support for that
468 * type of drive. A missing report_zones method means that the target
469 * driver has a problem.
470 */
471 if (WARN_ON(!tgt->type->report_zones)) {
472 ret = -EIO;
473 goto out;
474 }
475
Damien Le Moalbd976e52019-07-01 14:09:16 +0900476 ret = tgt->type->report_zones(tgt, sector, zones, nr_zones);
Christoph Hellwige76239a2018-10-12 19:08:49 +0900477
478out:
479 dm_put_live_table(md, srcu_idx);
480 return ret;
481#else
482 return -ENOTSUPP;
483#endif
484}
485
Mike Snitzer971888c2018-04-03 15:05:12 -0400486static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -0400487 struct block_device **bdev)
Mike Snitzer971888c2018-04-03 15:05:12 -0400488 __acquires(md->io_barrier)
Milan Brozaa129a22006-10-03 01:15:15 -0700489{
Mike Snitzer66482022016-02-18 15:44:39 -0500490 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100491 struct dm_table *map;
Mike Snitzer971888c2018-04-03 15:05:12 -0400492 int r;
Milan Brozaa129a22006-10-03 01:15:15 -0700493
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100494retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200495 r = -ENOTTY;
Mike Snitzer971888c2018-04-03 15:05:12 -0400496 map = dm_get_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700497 if (!map || !dm_table_get_size(map))
Mike Snitzer971888c2018-04-03 15:05:12 -0400498 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700499
500 /* We only support devices that have a single target */
501 if (dm_table_get_num_targets(map) != 1)
Mike Snitzer971888c2018-04-03 15:05:12 -0400502 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700503
Mike Snitzer66482022016-02-18 15:44:39 -0500504 tgt = dm_table_get_target(map, 0);
505 if (!tgt->type->prepare_ioctl)
Mike Snitzer971888c2018-04-03 15:05:12 -0400506 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700507
Mike Snitzer971888c2018-04-03 15:05:12 -0400508 if (dm_suspended_md(md))
509 return -EAGAIN;
Milan Brozaa129a22006-10-03 01:15:15 -0700510
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -0400511 r = tgt->type->prepare_ioctl(tgt, bdev);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000512 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Mike Snitzer971888c2018-04-03 15:05:12 -0400513 dm_put_live_table(md, *srcu_idx);
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100514 msleep(10);
515 goto retry;
516 }
Mike Snitzer971888c2018-04-03 15:05:12 -0400517
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200518 return r;
519}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100520
Mike Snitzer971888c2018-04-03 15:05:12 -0400521static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
522 __releases(md->io_barrier)
523{
524 dm_put_live_table(md, srcu_idx);
525}
526
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200527static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
528 unsigned int cmd, unsigned long arg)
529{
530 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer971888c2018-04-03 15:05:12 -0400531 int r, srcu_idx;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200532
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -0400533 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200534 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -0400535 goto out;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200536
537 if (r > 0) {
538 /*
Christoph Hellwige980f622017-02-04 10:45:03 +0100539 * Target determined this ioctl is being issued against a
540 * subset of the parent bdev; require extra privileges.
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200541 */
Christoph Hellwige980f622017-02-04 10:45:03 +0100542 if (!capable(CAP_SYS_RAWIO)) {
543 DMWARN_LIMIT(
544 "%s: sending ioctl %x to DM device without required privilege.",
545 current->comm, cmd);
546 r = -ENOIOCTLCMD;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200547 goto out;
Christoph Hellwige980f622017-02-04 10:45:03 +0100548 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200549 }
550
Mike Snitzer66482022016-02-18 15:44:39 -0500551 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200552out:
Mike Snitzer971888c2018-04-03 15:05:12 -0400553 dm_unprepare_ioctl(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700554 return r;
555}
556
Mike Snitzer978e51b2017-12-09 15:16:42 -0500557static void start_io_acct(struct dm_io *io);
558
559static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Mike Snitzer64f52b02017-12-11 23:17:47 -0500561 struct dm_io *io;
562 struct dm_target_io *tio;
563 struct bio *clone;
564
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400565 clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs);
Mike Snitzer64f52b02017-12-11 23:17:47 -0500566 if (!clone)
567 return NULL;
568
569 tio = container_of(clone, struct dm_target_io, clone);
570 tio->inside_dm_io = true;
571 tio->io = NULL;
572
573 io = container_of(tio, struct dm_io, tio);
574 io->magic = DM_IO_MAGIC;
Mike Snitzer978e51b2017-12-09 15:16:42 -0500575 io->status = 0;
576 atomic_set(&io->io_count, 1);
577 io->orig_bio = bio;
578 io->md = md;
579 spin_lock_init(&io->endio_lock);
580
581 start_io_acct(io);
Mike Snitzer64f52b02017-12-11 23:17:47 -0500582
583 return io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100586static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Mike Snitzer64f52b02017-12-11 23:17:47 -0500588 bio_put(&io->tio.clone);
589}
590
591static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
592 unsigned target_bio_nr, gfp_t gfp_mask)
593{
594 struct dm_target_io *tio;
595
596 if (!ci->io->tio.io) {
597 /* the dm_target_io embedded in ci->io is available */
598 tio = &ci->io->tio;
599 } else {
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400600 struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs);
Mike Snitzer64f52b02017-12-11 23:17:47 -0500601 if (!clone)
602 return NULL;
603
604 tio = container_of(clone, struct dm_target_io, clone);
605 tio->inside_dm_io = false;
606 }
607
608 tio->magic = DM_TIO_MAGIC;
609 tio->io = ci->io;
610 tio->ti = ti;
611 tio->target_bio_nr = target_bio_nr;
612
613 return tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Mike Snitzercfae7522016-04-11 12:05:38 -0400616static void free_tio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Mike Snitzer64f52b02017-12-11 23:17:47 -0500618 if (tio->inside_dm_io)
619 return;
Mikulas Patockadba14162012-10-12 21:02:15 +0100620 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Mike Snitzerc4576ae2018-12-11 09:10:26 -0500623static bool md_in_flight_bios(struct mapped_device *md)
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000624{
Mikulas Patocka6f757232018-12-06 11:41:22 -0500625 int cpu;
626 struct hd_struct *part = &dm_disk(md)->part0;
Jens Axboeb7934ba2018-12-10 15:45:53 -0700627 long sum = 0;
Mikulas Patocka6f757232018-12-06 11:41:22 -0500628
629 for_each_possible_cpu(cpu) {
Jens Axboeb7934ba2018-12-10 15:45:53 -0700630 sum += part_stat_local_read_cpu(part, in_flight[0], cpu);
631 sum += part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka6f757232018-12-06 11:41:22 -0500632 }
633
Jens Axboeb7934ba2018-12-10 15:45:53 -0700634 return sum != 0;
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000635}
636
Mike Snitzerc4576ae2018-12-11 09:10:26 -0500637static bool md_in_flight(struct mapped_device *md)
638{
639 if (queue_is_mq(md->queue))
Jens Axboe3c94d832018-12-17 21:11:17 -0700640 return blk_mq_queue_inflight(md->queue);
Mike Snitzerc4576ae2018-12-11 09:10:26 -0500641 else
642 return md_in_flight_bios(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100643}
644
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800645static void start_io_acct(struct dm_io *io)
646{
647 struct mapped_device *md = io->md;
Mike Snitzer745dc572017-12-11 20:51:50 -0500648 struct bio *bio = io->orig_bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800649
650 io->start_time = jiffies;
651
Michael Callahanddcf35d2018-07-18 04:47:39 -0700652 generic_start_io_acct(md->queue, bio_op(bio), bio_sectors(bio),
653 &dm_disk(md)->part0);
Mike Snitzerf3986372017-12-17 11:56:48 -0500654
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400655 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500656 dm_stats_account_io(&md->stats, bio_data_dir(bio),
657 bio->bi_iter.bi_sector, bio_sectors(bio),
658 false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800659}
660
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000661static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800662{
663 struct mapped_device *md = io->md;
Mike Snitzer745dc572017-12-11 20:51:50 -0500664 struct bio *bio = io->orig_bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800665 unsigned long duration = jiffies - io->start_time;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800666
Michael Callahanddcf35d2018-07-18 04:47:39 -0700667 generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0,
668 io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800669
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400670 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500671 dm_stats_account_io(&md->stats, bio_data_dir(bio),
672 bio->bi_iter.bi_sector, bio_sectors(bio),
673 true, duration, &io->stats_aux);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400674
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000675 /* nudge anyone waiting on suspend queue */
Mikulas Patocka645efa82019-02-05 05:09:00 -0500676 if (unlikely(wq_has_sleeper(&md->wait)))
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000677 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
681 * Add the bio to the list of deferred io.
682 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100683static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200685 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200687 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200689 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200690 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693/*
694 * Everyone (including functions in this file), should use this
695 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100696 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100698struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100700 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100702 return srcu_dereference(md->map, &md->io_barrier);
703}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100705void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
706{
707 srcu_read_unlock(&md->io_barrier, srcu_idx);
708}
709
710void dm_sync_table(struct mapped_device *md)
711{
712 synchronize_srcu(&md->io_barrier);
713 synchronize_rcu_expedited();
714}
715
716/*
717 * A fast alternative to dm_get_live_table/dm_put_live_table.
718 * The caller must not block between these two functions.
719 */
720static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
721{
722 rcu_read_lock();
723 return rcu_dereference(md->map);
724}
725
726static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
727{
728 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Mike Snitzer971888c2018-04-03 15:05:12 -0400731static char *_dm_claim_ptr = "I belong to device-mapper";
732
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800733/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500734 * Open a table device so we can use it as a map destination.
735 */
736static int open_table_device(struct table_device *td, dev_t dev,
737 struct mapped_device *md)
738{
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500739 struct block_device *bdev;
740
741 int r;
742
743 BUG_ON(td->dm_dev.bdev);
744
Mike Snitzer519049a2018-02-22 13:31:20 -0500745 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500746 if (IS_ERR(bdev))
747 return PTR_ERR(bdev);
748
749 r = bd_link_disk_holder(bdev, dm_disk(md));
750 if (r) {
751 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
752 return r;
753 }
754
755 td->dm_dev.bdev = bdev;
Dan Williams817bf402017-04-12 13:37:44 -0700756 td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500757 return 0;
758}
759
760/*
761 * Close a table device that we've been using.
762 */
763static void close_table_device(struct table_device *td, struct mapped_device *md)
764{
765 if (!td->dm_dev.bdev)
766 return;
767
768 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
769 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
Dan Williams817bf402017-04-12 13:37:44 -0700770 put_dax(td->dm_dev.dax_dev);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500771 td->dm_dev.bdev = NULL;
Dan Williams817bf402017-04-12 13:37:44 -0700772 td->dm_dev.dax_dev = NULL;
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500773}
774
775static struct table_device *find_table_device(struct list_head *l, dev_t dev,
Sheetal Singala8454fca2019-05-10 23:18:37 +0530776 fmode_t mode)
777{
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500778 struct table_device *td;
779
780 list_for_each_entry(td, l, list)
781 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
782 return td;
783
784 return NULL;
785}
786
787int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
Sheetal Singala8454fca2019-05-10 23:18:37 +0530788 struct dm_dev **result)
789{
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500790 int r;
791 struct table_device *td;
792
793 mutex_lock(&md->table_devices_lock);
794 td = find_table_device(&md->table_devices, dev, mode);
795 if (!td) {
Mike Snitzer115485e2016-02-22 12:16:21 -0500796 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500797 if (!td) {
798 mutex_unlock(&md->table_devices_lock);
799 return -ENOMEM;
800 }
801
802 td->dm_dev.mode = mode;
803 td->dm_dev.bdev = NULL;
804
805 if ((r = open_table_device(td, dev, md))) {
806 mutex_unlock(&md->table_devices_lock);
807 kfree(td);
808 return r;
809 }
810
811 format_dev_t(td->dm_dev.name, dev);
812
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300813 refcount_set(&td->count, 1);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500814 list_add(&td->list, &md->table_devices);
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300815 } else {
816 refcount_inc(&td->count);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500817 }
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500818 mutex_unlock(&md->table_devices_lock);
819
820 *result = &td->dm_dev;
821 return 0;
822}
823EXPORT_SYMBOL_GPL(dm_get_table_device);
824
825void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
826{
827 struct table_device *td = container_of(d, struct table_device, dm_dev);
828
829 mutex_lock(&md->table_devices_lock);
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300830 if (refcount_dec_and_test(&td->count)) {
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500831 close_table_device(td, md);
832 list_del(&td->list);
833 kfree(td);
834 }
835 mutex_unlock(&md->table_devices_lock);
836}
837EXPORT_SYMBOL(dm_put_table_device);
838
839static void free_table_devices(struct list_head *devices)
840{
841 struct list_head *tmp, *next;
842
843 list_for_each_safe(tmp, next, devices) {
844 struct table_device *td = list_entry(tmp, struct table_device, list);
845
846 DMWARN("dm_destroy: %s still exists with %d references",
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300847 td->dm_dev.name, refcount_read(&td->count));
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500848 kfree(td);
849 }
850}
851
852/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800853 * Get the geometry associated with a dm device
854 */
855int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
856{
857 *geo = md->geometry;
858
859 return 0;
860}
861
862/*
863 * Set the geometry of a device.
864 */
865int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
866{
867 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
868
869 if (geo->start > sz) {
870 DMWARN("Start sector is beyond the geometry limits.");
871 return -EINVAL;
872 }
873
874 md->geometry = *geo;
875
876 return 0;
877}
878
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800879static int __noflush_suspending(struct mapped_device *md)
880{
881 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/*
885 * Decrements the number of outstanding ios that a bio has been
886 * cloned into, completing the original io if necc.
887 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200888static void dec_pending(struct dm_io *io, blk_status_t error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800890 unsigned long flags;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200891 blk_status_t io_error;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000892 struct bio *bio;
893 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800894
895 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100896 if (unlikely(error)) {
897 spin_lock_irqsave(&io->endio_lock, flags);
Mike Snitzer745dc572017-12-11 20:51:50 -0500898 if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200899 io->status = error;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100900 spin_unlock_irqrestore(&io->endio_lock, flags);
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (atomic_dec_and_test(&io->io_count)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200904 if (io->status == BLK_STS_DM_REQUEUE) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800905 /*
906 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800907 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100908 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200909 if (__noflush_suspending(md))
Mike Snitzer745dc572017-12-11 20:51:50 -0500910 /* NOTE early return due to BLK_STS_DM_REQUEUE below */
911 bio_list_add_head(&md->deferred, io->orig_bio);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200912 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800913 /* noflush suspend was interrupted. */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200914 io->status = BLK_STS_IOERR;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100915 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800916 }
917
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200918 io_error = io->status;
Mike Snitzer745dc572017-12-11 20:51:50 -0500919 bio = io->orig_bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200920 end_io_acct(io);
921 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100922
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200923 if (io_error == BLK_STS_DM_REQUEUE)
Tejun Heo6a8736d2010-09-08 18:07:00 +0200924 return;
925
Jens Axboe1eff9d32016-08-05 15:35:16 -0600926 if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100927 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200928 * Preflush done for flush with data, reissue
Mike Christie28a8f0d2016-06-05 14:32:25 -0500929 * without REQ_PREFLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100930 */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600931 bio->bi_opf &= ~REQ_PREFLUSH;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200932 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100933 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200934 /* done with normal IO or empty flush */
NeilBrown8dd601f2018-02-15 20:00:15 +1100935 if (io_error)
936 bio->bi_status = io_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200937 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940}
941
Mike Snitzerbcb44432019-04-03 12:23:11 -0400942void disable_discard(struct mapped_device *md)
943{
944 struct queue_limits *limits = dm_get_queue_limits(md);
945
946 /* device doesn't really support DISCARD, disable it */
947 limits->max_discard_sectors = 0;
948 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue);
949}
950
Mike Snitzer4cc96132016-05-12 16:28:10 -0400951void disable_write_same(struct mapped_device *md)
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400952{
953 struct queue_limits *limits = dm_get_queue_limits(md);
954
955 /* device doesn't really support WRITE SAME, disable it */
956 limits->max_write_same_sectors = 0;
957}
958
Christoph Hellwigac62d622017-04-05 19:21:05 +0200959void disable_write_zeroes(struct mapped_device *md)
960{
961 struct queue_limits *limits = dm_get_queue_limits(md);
962
963 /* device doesn't really support WRITE ZEROES, disable it */
964 limits->max_write_zeroes_sectors = 0;
965}
966
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200967static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200969 blk_status_t error = bio->bi_status;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500970 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000971 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700972 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dm_endio_fn endio = tio->ti->type->end_io;
974
Mike Snitzer978e51b2017-12-09 15:16:42 -0500975 if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) {
Mike Snitzerbcb44432019-04-03 12:23:11 -0400976 if (bio_op(bio) == REQ_OP_DISCARD &&
977 !bio->bi_disk->queue->limits.max_discard_sectors)
978 disable_discard(md);
979 else if (bio_op(bio) == REQ_OP_WRITE_SAME &&
980 !bio->bi_disk->queue->limits.max_write_same_sectors)
Christoph Hellwigac62d622017-04-05 19:21:05 +0200981 disable_write_same(md);
Mike Snitzerbcb44432019-04-03 12:23:11 -0400982 else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
983 !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
Christoph Hellwigac62d622017-04-05 19:21:05 +0200984 disable_write_zeroes(md);
985 }
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400986
Christoph Hellwig1be56902017-06-03 09:38:03 +0200987 if (endio) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200988 int r = endio(tio->ti, bio, &error);
Christoph Hellwig1be56902017-06-03 09:38:03 +0200989 switch (r) {
990 case DM_ENDIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200991 error = BLK_STS_DM_REQUEUE;
Christoph Hellwig1be56902017-06-03 09:38:03 +0200992 /*FALLTHRU*/
993 case DM_ENDIO_DONE:
994 break;
995 case DM_ENDIO_INCOMPLETE:
996 /* The target will handle the io */
997 return;
998 default:
999 DMWARN("unimplemented target endio return value: %d", r);
1000 BUG();
1001 }
1002 }
1003
Mike Snitzercfae7522016-04-11 12:05:38 -04001004 free_tio(tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001005 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Mike Snitzer78d8e582015-06-26 10:01:13 -04001008/*
Mike Snitzer56a67df2010-08-12 04:14:10 +01001009 * Return maximum size of I/O possible at the supplied sector up to the current
1010 * target boundary.
1011 */
1012static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001014 sector_t target_offset = dm_target_offset(ti, sector);
1015
1016 return ti->len - target_offset;
1017}
1018
1019static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1020{
1021 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001022 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001025 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001027 if (ti->max_io_len) {
1028 offset = dm_target_offset(ti, sector);
1029 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1030 max_len = sector_div(offset, ti->max_io_len);
1031 else
1032 max_len = offset & (ti->max_io_len - 1);
1033 max_len = ti->max_io_len - max_len;
1034
1035 if (len > max_len)
1036 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 return len;
1040}
1041
Mike Snitzer542f9032012-07-27 15:08:00 +01001042int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1043{
1044 if (len > UINT_MAX) {
1045 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1046 (unsigned long long)len, UINT_MAX);
1047 ti->error = "Maximum size of target IO is too large";
1048 return -EINVAL;
1049 }
1050
Mikulas Patocka75ae1932019-03-21 16:46:12 -04001051 ti->max_io_len = (uint32_t) len;
Mike Snitzer542f9032012-07-27 15:08:00 +01001052
1053 return 0;
1054}
1055EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1056
Dan Williamsf26c5712017-04-12 12:35:44 -07001057static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
Mike Snitzer3d97c822018-04-30 16:06:28 -04001058 sector_t sector, int *srcu_idx)
1059 __acquires(md->io_barrier)
Toshi Kani545ed202016-06-22 17:54:53 -06001060{
Toshi Kani545ed202016-06-22 17:54:53 -06001061 struct dm_table *map;
1062 struct dm_target *ti;
Toshi Kani545ed202016-06-22 17:54:53 -06001063
Dan Williamsf26c5712017-04-12 12:35:44 -07001064 map = dm_get_live_table(md, srcu_idx);
Toshi Kani545ed202016-06-22 17:54:53 -06001065 if (!map)
Dan Williamsf26c5712017-04-12 12:35:44 -07001066 return NULL;
Toshi Kani545ed202016-06-22 17:54:53 -06001067
1068 ti = dm_table_find_target(map, sector);
Mikulas Patocka123d87d2019-08-23 09:55:26 -04001069 if (!ti)
Dan Williamsf26c5712017-04-12 12:35:44 -07001070 return NULL;
1071
1072 return ti;
1073}
1074
1075static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
Mike Snitzer3d97c822018-04-30 16:06:28 -04001076 long nr_pages, void **kaddr, pfn_t *pfn)
Dan Williamsf26c5712017-04-12 12:35:44 -07001077{
1078 struct mapped_device *md = dax_get_private(dax_dev);
1079 sector_t sector = pgoff * PAGE_SECTORS;
1080 struct dm_target *ti;
1081 long len, ret = -EIO;
1082 int srcu_idx;
1083
1084 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1085
1086 if (!ti)
Toshi Kani545ed202016-06-22 17:54:53 -06001087 goto out;
Dan Williamsf26c5712017-04-12 12:35:44 -07001088 if (!ti->type->direct_access)
1089 goto out;
1090 len = max_io_len(sector, ti) / PAGE_SECTORS;
1091 if (len < 1)
1092 goto out;
1093 nr_pages = min(len, nr_pages);
Ross Zwislerdbc62652018-06-26 16:30:41 -06001094 ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
Dan Williams817bf402017-04-12 13:37:44 -07001095
Dan Williamsf26c5712017-04-12 12:35:44 -07001096 out:
Toshi Kani545ed202016-06-22 17:54:53 -06001097 dm_put_live_table(md, srcu_idx);
Dan Williamsf26c5712017-04-12 12:35:44 -07001098
1099 return ret;
Toshi Kani545ed202016-06-22 17:54:53 -06001100}
1101
Dan Williams7bf7eac2019-05-16 13:26:29 -07001102static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev,
1103 int blocksize, sector_t start, sector_t len)
1104{
1105 struct mapped_device *md = dax_get_private(dax_dev);
1106 struct dm_table *map;
1107 int srcu_idx;
1108 bool ret;
1109
1110 map = dm_get_live_table(md, &srcu_idx);
1111 if (!map)
1112 return false;
1113
Pankaj Gupta2e9ee092019-07-05 19:33:25 +05301114 ret = dm_table_supports_dax(map, device_supports_dax, &blocksize);
Dan Williams7bf7eac2019-05-16 13:26:29 -07001115
1116 dm_put_live_table(md, srcu_idx);
1117
1118 return ret;
1119}
1120
Dan Williams7e026c82017-05-29 12:57:56 -07001121static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
Mike Snitzer3d97c822018-04-30 16:06:28 -04001122 void *addr, size_t bytes, struct iov_iter *i)
Dan Williams7e026c82017-05-29 12:57:56 -07001123{
1124 struct mapped_device *md = dax_get_private(dax_dev);
1125 sector_t sector = pgoff * PAGE_SECTORS;
1126 struct dm_target *ti;
1127 long ret = 0;
1128 int srcu_idx;
1129
1130 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1131
1132 if (!ti)
1133 goto out;
1134 if (!ti->type->dax_copy_from_iter) {
1135 ret = copy_from_iter(addr, bytes, i);
1136 goto out;
1137 }
1138 ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
1139 out:
1140 dm_put_live_table(md, srcu_idx);
1141
1142 return ret;
1143}
1144
Dan Williamsb3a9a0c2018-05-02 06:46:33 -07001145static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
1146 void *addr, size_t bytes, struct iov_iter *i)
1147{
1148 struct mapped_device *md = dax_get_private(dax_dev);
1149 sector_t sector = pgoff * PAGE_SECTORS;
1150 struct dm_target *ti;
1151 long ret = 0;
1152 int srcu_idx;
1153
1154 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1155
1156 if (!ti)
1157 goto out;
1158 if (!ti->type->dax_copy_to_iter) {
1159 ret = copy_to_iter(addr, bytes, i);
1160 goto out;
1161 }
1162 ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i);
1163 out:
1164 dm_put_live_table(md, srcu_idx);
1165
1166 return ret;
1167}
1168
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001169/*
1170 * A target may call dm_accept_partial_bio only from the map routine. It is
Ajay Joshi2e2d6f72019-10-27 23:05:48 +09001171 * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_RESET,
1172 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE and REQ_OP_ZONE_FINISH.
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001173 *
1174 * dm_accept_partial_bio informs the dm that the target only wants to process
1175 * additional n_sectors sectors of the bio and the rest of the data should be
1176 * sent in a next bio.
1177 *
1178 * A diagram that explains the arithmetics:
1179 * +--------------------+---------------+-------+
1180 * | 1 | 2 | 3 |
1181 * +--------------------+---------------+-------+
1182 *
1183 * <-------------- *tio->len_ptr --------------->
1184 * <------- bi_size ------->
1185 * <-- n_sectors -->
1186 *
1187 * Region 1 was already iterated over with bio_advance or similar function.
1188 * (it may be empty if the target doesn't use bio_advance)
1189 * Region 2 is the remaining bio size that the target wants to process.
1190 * (it may be empty if region 1 is non-empty, although there is no reason
1191 * to make it empty)
1192 * The target requires that region 3 is to be sent in the next bio.
1193 *
1194 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1195 * the partially processed part (the sum of regions 1+2) must be the same for all
1196 * copies of the bio.
1197 */
1198void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1199{
1200 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1201 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Jens Axboe1eff9d32016-08-05 15:35:16 -06001202 BUG_ON(bio->bi_opf & REQ_PREFLUSH);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001203 BUG_ON(bi_size > *tio->len_ptr);
1204 BUG_ON(n_sectors > bi_size);
1205 *tio->len_ptr -= bi_size - n_sectors;
1206 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1207}
1208EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1209
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001210/*
Christoph Hellwige76239a2018-10-12 19:08:49 +09001211 * The zone descriptors obtained with a zone report indicate
1212 * zone positions within the underlying device of the target. The zone
1213 * descriptors must be remapped to match their position within the dm device.
Damien Le Moal10999302017-05-08 16:40:48 -07001214 */
Christoph Hellwige76239a2018-10-12 19:08:49 +09001215void dm_remap_zone_report(struct dm_target *ti, sector_t start,
1216 struct blk_zone *zones, unsigned int *nr_zones)
Damien Le Moal10999302017-05-08 16:40:48 -07001217{
1218#ifdef CONFIG_BLK_DEV_ZONED
Damien Le Moal10999302017-05-08 16:40:48 -07001219 struct blk_zone *zone;
Christoph Hellwige76239a2018-10-12 19:08:49 +09001220 unsigned int nrz = *nr_zones;
1221 int i;
Damien Le Moal10999302017-05-08 16:40:48 -07001222
1223 /*
Christoph Hellwige76239a2018-10-12 19:08:49 +09001224 * Remap the start sector and write pointer position of the zones in
1225 * the array. Since we may have obtained from the target underlying
1226 * device more zones that the target size, also adjust the number
1227 * of zones.
Damien Le Moal9864cd5dc52018-10-09 14:24:31 +09001228 */
Christoph Hellwige76239a2018-10-12 19:08:49 +09001229 for (i = 0; i < nrz; i++) {
1230 zone = zones + i;
1231 if (zone->start >= start + ti->len) {
1232 memset(zone, 0, sizeof(struct blk_zone) * (nrz - i));
1233 break;
Damien Le Moal10999302017-05-08 16:40:48 -07001234 }
1235
Christoph Hellwige76239a2018-10-12 19:08:49 +09001236 zone->start = zone->start + ti->begin - start;
1237 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
1238 continue;
Damien Le Moal10999302017-05-08 16:40:48 -07001239
Christoph Hellwige76239a2018-10-12 19:08:49 +09001240 if (zone->cond == BLK_ZONE_COND_FULL)
1241 zone->wp = zone->start + zone->len;
1242 else if (zone->cond == BLK_ZONE_COND_EMPTY)
1243 zone->wp = zone->start;
1244 else
1245 zone->wp = zone->wp + ti->begin - start;
Damien Le Moal10999302017-05-08 16:40:48 -07001246 }
1247
Christoph Hellwige76239a2018-10-12 19:08:49 +09001248 *nr_zones = i;
Damien Le Moal10999302017-05-08 16:40:48 -07001249#else /* !CONFIG_BLK_DEV_ZONED */
Christoph Hellwige76239a2018-10-12 19:08:49 +09001250 *nr_zones = 0;
Damien Le Moal10999302017-05-08 16:40:48 -07001251#endif
1252}
1253EXPORT_SYMBOL_GPL(dm_remap_zone_report);
1254
Mike Snitzer978e51b2017-12-09 15:16:42 -05001255static blk_qc_t __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001258 sector_t sector;
Mikulas Patockadba14162012-10-12 21:02:15 +01001259 struct bio *clone = &tio->clone;
Mike Snitzer64f52b02017-12-11 23:17:47 -05001260 struct dm_io *io = tio->io;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001261 struct mapped_device *md = io->md;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001262 struct dm_target *ti = tio->ti;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001263 blk_qc_t ret = BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
1268 * Map the clone. If r == 0 we don't need to do
1269 * anything, the target has assumed ownership of
1270 * this io.
1271 */
Mike Snitzer64f52b02017-12-11 23:17:47 -05001272 atomic_inc(&io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001273 sector = clone->bi_iter.bi_sector;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001274
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001275 r = ti->type->map(ti, clone);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001276 switch (r) {
1277 case DM_MAPIO_SUBMITTED:
1278 break;
1279 case DM_MAPIO_REMAPPED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* the bio has been remapped so dispatch it */
Christoph Hellwig74d46992017-08-23 19:10:32 +02001281 trace_block_bio_remap(clone->bi_disk->queue, clone,
Mike Snitzer64f52b02017-12-11 23:17:47 -05001282 bio_dev(io->orig_bio), sector);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001283 if (md->type == DM_TYPE_NVME_BIO_BASED)
1284 ret = direct_make_request(clone);
1285 else
1286 ret = generic_make_request(clone);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001287 break;
1288 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001289 free_tio(tio);
Mike Snitzer64f52b02017-12-11 23:17:47 -05001290 dec_pending(io, BLK_STS_IOERR);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001291 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +02001292 case DM_MAPIO_REQUEUE:
Mike Snitzercfae7522016-04-11 12:05:38 -04001293 free_tio(tio);
Mike Snitzer64f52b02017-12-11 23:17:47 -05001294 dec_pending(io, BLK_STS_DM_REQUEUE);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001295 break;
1296 default:
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001297 DMWARN("unimplemented target map return value: %d", r);
1298 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Mike Snitzer978e51b2017-12-09 15:16:42 -05001301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Mikulas Patockae0d66092014-03-14 18:40:39 -04001304static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001305{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001306 bio->bi_iter.bi_sector = sector;
1307 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
1310/*
1311 * Creates a bio that consists of range of complete bvecs.
1312 */
Mike Snitzerc80914e2016-03-02 12:33:03 -05001313static int clone_bio(struct dm_target_io *tio, struct bio *bio,
1314 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Mikulas Patockadba14162012-10-12 21:02:15 +01001316 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001318 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Mike Snitzer57c36512019-01-16 18:53:26 -05001320 if (bio_integrity(bio)) {
Mikulas Patockae2460f22017-04-18 16:51:48 -04001321 int r;
1322
1323 if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1324 !dm_target_passes_integrity(tio->ti->type))) {
1325 DMWARN("%s: the target %s doesn't support integrity data.",
1326 dm_device_name(tio->io->md),
1327 tio->ti->type->name);
1328 return -EIO;
1329 }
1330
1331 r = bio_integrity_clone(clone, bio, GFP_NOIO);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001332 if (r < 0)
1333 return r;
1334 }
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001335
Mike Snitzerfa8db492019-02-05 17:07:58 -05001336 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1337 clone->bi_iter.bi_size = to_bytes(len);
1338
1339 if (bio_integrity(bio))
1340 bio_integrity_trim(clone);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001341
1342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Mike Snitzer318716d2017-11-22 14:56:12 -05001345static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1346 struct dm_target *ti, unsigned num_bios)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001347{
Mikulas Patockadba14162012-10-12 21:02:15 +01001348 struct dm_target_io *tio;
Mike Snitzer318716d2017-11-22 14:56:12 -05001349 int try;
Mikulas Patockadba14162012-10-12 21:02:15 +01001350
Mike Snitzer318716d2017-11-22 14:56:12 -05001351 if (!num_bios)
1352 return;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001353
Mike Snitzer318716d2017-11-22 14:56:12 -05001354 if (num_bios == 1) {
1355 tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1356 bio_list_add(blist, &tio->clone);
1357 return;
1358 }
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001359
Mike Snitzer318716d2017-11-22 14:56:12 -05001360 for (try = 0; try < 2; try++) {
1361 int bio_nr;
1362 struct bio *bio;
1363
1364 if (try)
Mike Snitzerbc02cdb2017-12-14 16:30:42 -05001365 mutex_lock(&ci->io->md->table_devices_lock);
Mike Snitzer318716d2017-11-22 14:56:12 -05001366 for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1367 tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1368 if (!tio)
1369 break;
1370
1371 bio_list_add(blist, &tio->clone);
1372 }
1373 if (try)
Mike Snitzerbc02cdb2017-12-14 16:30:42 -05001374 mutex_unlock(&ci->io->md->table_devices_lock);
Mike Snitzer318716d2017-11-22 14:56:12 -05001375 if (bio_nr == num_bios)
1376 return;
1377
1378 while ((bio = bio_list_pop(blist))) {
1379 tio = container_of(bio, struct dm_target_io, clone);
1380 free_tio(tio);
1381 }
1382 }
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001383}
1384
Mike Snitzer978e51b2017-12-09 15:16:42 -05001385static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
1386 struct dm_target_io *tio, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001387{
Mikulas Patockadba14162012-10-12 21:02:15 +01001388 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001389
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001390 tio->len_ptr = len;
1391
Junichi Nomura99778272014-10-03 11:55:16 +00001392 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001393 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001394 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001395
Mike Snitzer978e51b2017-12-09 15:16:42 -05001396 return __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001397}
1398
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001399static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001400 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001401{
Mike Snitzer318716d2017-11-22 14:56:12 -05001402 struct bio_list blist = BIO_EMPTY_LIST;
1403 struct bio *bio;
1404 struct dm_target_io *tio;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001405
Mike Snitzer318716d2017-11-22 14:56:12 -05001406 alloc_multiple_bios(&blist, ci, ti, num_bios);
1407
1408 while ((bio = bio_list_pop(&blist))) {
1409 tio = container_of(bio, struct dm_target_io, clone);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001410 (void) __clone_and_map_simple_bio(ci, tio, len);
Mike Snitzer318716d2017-11-22 14:56:12 -05001411 }
Mike Snitzer06a426c2010-08-12 04:14:09 +01001412}
1413
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001414static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001415{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001416 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001417 struct dm_target *ti;
1418
Dennis Zhou892ad712018-12-05 12:10:30 -05001419 /*
Jens Axboedbe3ece2018-12-19 09:13:34 -07001420 * Empty flush uses a statically initialized bio, as the base for
1421 * cloning. However, blkg association requires that a bdev is
1422 * associated with a gendisk, which doesn't happen until the bdev is
1423 * opened. So, blkg association is done at issue time of the flush
1424 * rather than when the device is created in alloc_dev().
Dennis Zhou892ad712018-12-05 12:10:30 -05001425 */
1426 bio_set_dev(ci->bio, ci->io->md->bdev);
1427
Mike Snitzerb372d362010-09-08 18:07:01 +02001428 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001429 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001430 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001431
Dennis Zhou892ad712018-12-05 12:10:30 -05001432 bio_disassociate_blkg(ci->bio);
1433
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001434 return 0;
1435}
1436
Mike Snitzerc80914e2016-03-02 12:33:03 -05001437static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
NeilBrownf31c21e2017-11-22 14:25:18 +11001438 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001439{
Mikulas Patockadba14162012-10-12 21:02:15 +01001440 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001441 struct dm_target_io *tio;
NeilBrownf31c21e2017-11-22 14:25:18 +11001442 int r;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001443
Mike Snitzer318716d2017-11-22 14:56:12 -05001444 tio = alloc_tio(ci, ti, 0, GFP_NOIO);
NeilBrownf31c21e2017-11-22 14:25:18 +11001445 tio->len_ptr = len;
1446 r = clone_bio(tio, bio, sector, *len);
1447 if (r < 0) {
1448 free_tio(tio);
1449 return r;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001450 }
Mike Snitzer978e51b2017-12-09 15:16:42 -05001451 (void) __map_bio(tio);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001452
NeilBrownf31c21e2017-11-22 14:25:18 +11001453 return 0;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001454}
1455
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001456typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001457
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001458static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001459{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001460 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001461}
1462
Denis Semakin00716542018-03-13 13:23:45 +04001463static unsigned get_num_secure_erase_bios(struct dm_target *ti)
1464{
1465 return ti->num_secure_erase_bios;
1466}
1467
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001468static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001469{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001470 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001471}
1472
Christoph Hellwigac62d622017-04-05 19:21:05 +02001473static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1474{
1475 return ti->num_write_zeroes_bios;
1476}
1477
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001478static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
Mike Snitzer61697a62019-01-18 14:19:26 -05001479 unsigned num_bios)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001480{
Michael Lass51b86f92019-05-21 21:58:07 +02001481 unsigned len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001482
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001483 /*
1484 * Even though the device advertised support for this type of
1485 * request, that does not mean every target supports it, and
1486 * reconfiguration might also have changed that since the
1487 * check was performed.
1488 */
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001489 if (!num_bios)
1490 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001491
Michael Lass51b86f92019-05-21 21:58:07 +02001492 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1493
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001494 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001495
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001496 ci->sector += len;
1497 ci->sector_count -= len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001498
1499 return 0;
1500}
1501
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001502static int __send_discard(struct clone_info *ci, struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001503{
Mike Snitzer61697a62019-01-18 14:19:26 -05001504 return __send_changing_extent_only(ci, ti, get_num_discard_bios(ti));
Mike Snitzer23508a92012-12-21 20:23:37 +00001505}
1506
Denis Semakin00716542018-03-13 13:23:45 +04001507static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
1508{
Mike Snitzer61697a62019-01-18 14:19:26 -05001509 return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios(ti));
Denis Semakin00716542018-03-13 13:23:45 +04001510}
1511
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001512static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001513{
Mike Snitzer61697a62019-01-18 14:19:26 -05001514 return __send_changing_extent_only(ci, ti, get_num_write_same_bios(ti));
Mike Snitzer23508a92012-12-21 20:23:37 +00001515}
1516
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001517static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
Christoph Hellwigac62d622017-04-05 19:21:05 +02001518{
Mike Snitzer61697a62019-01-18 14:19:26 -05001519 return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios(ti));
Christoph Hellwigac62d622017-04-05 19:21:05 +02001520}
1521
Mike Snitzer568c73a2019-01-18 14:10:37 -05001522static bool is_abnormal_io(struct bio *bio)
1523{
1524 bool r = false;
1525
1526 switch (bio_op(bio)) {
1527 case REQ_OP_DISCARD:
1528 case REQ_OP_SECURE_ERASE:
1529 case REQ_OP_WRITE_SAME:
1530 case REQ_OP_WRITE_ZEROES:
1531 r = true;
1532 break;
1533 }
1534
1535 return r;
1536}
1537
Mike Snitzer0519c712018-03-26 11:49:16 -04001538static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
1539 int *result)
1540{
1541 struct bio *bio = ci->bio;
1542
1543 if (bio_op(bio) == REQ_OP_DISCARD)
1544 *result = __send_discard(ci, ti);
Denis Semakin00716542018-03-13 13:23:45 +04001545 else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
1546 *result = __send_secure_erase(ci, ti);
Mike Snitzer0519c712018-03-26 11:49:16 -04001547 else if (bio_op(bio) == REQ_OP_WRITE_SAME)
1548 *result = __send_write_same(ci, ti);
1549 else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
1550 *result = __send_write_zeroes(ci, ti);
1551 else
1552 return false;
1553
1554 return true;
1555}
1556
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001557/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001558 * Select the correct strategy for processing a non-flush bio.
1559 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001560static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001562 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001563 unsigned len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001564 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001566 ti = dm_table_find_target(ci->map, ci->sector);
Mikulas Patocka123d87d2019-08-23 09:55:26 -04001567 if (!ti)
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001568 return -EIO;
1569
Mike Snitzer568c73a2019-01-18 14:10:37 -05001570 if (__process_abnormal_io(ci, ti, &r))
Mike Snitzer0519c712018-03-26 11:49:16 -04001571 return r;
Mike Snitzer3d7f4562017-12-08 15:02:11 -05001572
Christoph Hellwige76239a2018-10-12 19:08:49 +09001573 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001574
Mike Snitzerc80914e2016-03-02 12:33:03 -05001575 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1576 if (r < 0)
1577 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001579 ci->sector += len;
1580 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
Mike Snitzer978e51b2017-12-09 15:16:42 -05001585static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1586 struct dm_table *map, struct bio *bio)
1587{
1588 ci->map = map;
1589 ci->io = alloc_io(md, bio);
1590 ci->sector = bio->bi_iter.bi_sector;
1591}
1592
Mike Snitzera1e1cb72019-01-17 10:48:01 -05001593#define __dm_part_stat_sub(part, field, subnd) \
1594 (part_stat_get(part, field) -= (subnd))
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001597 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Mike Snitzer978e51b2017-12-09 15:16:42 -05001599static blk_qc_t __split_and_process_bio(struct mapped_device *md,
1600 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct clone_info ci;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001603 blk_qc_t ret = BLK_QC_T_NONE;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001604 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Mike Snitzer978e51b2017-12-09 15:16:42 -05001606 init_clone_info(&ci, md, map, bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001607
Jens Axboe1eff9d32016-08-05 15:35:16 -06001608 if (bio->bi_opf & REQ_PREFLUSH) {
Jens Axboedbe3ece2018-12-19 09:13:34 -07001609 struct bio flush_bio;
1610
1611 /*
1612 * Use an on-stack bio for this, it's safe since we don't
1613 * need to reference it after submit. It's just used as
1614 * the basis for the clone(s).
1615 */
1616 bio_init(&flush_bio, NULL, 0);
1617 flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1618 ci.bio = &flush_bio;
Mike Snitzerb372d362010-09-08 18:07:01 +02001619 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001620 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001621 /* dec_pending submits any data associated with flush */
Ajay Joshi2e2d6f72019-10-27 23:05:48 +09001622 } else if (op_is_zone_mgmt(bio_op(bio))) {
Damien Le Moala4aa5e52017-05-08 16:40:46 -07001623 ci.bio = bio;
1624 ci.sector_count = 0;
1625 error = __split_and_process_non_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001626 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001627 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001628 ci.sector_count = bio_sectors(bio);
NeilBrown18a25da2017-09-06 09:43:28 +10001629 while (ci.sector_count && !error) {
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001630 error = __split_and_process_non_flush(&ci);
NeilBrown18a25da2017-09-06 09:43:28 +10001631 if (current->bio_list && ci.sector_count && !error) {
1632 /*
1633 * Remainder must be passed to generic_make_request()
1634 * so that it gets handled *after* bios already submitted
1635 * have been completely processed.
1636 * We take a clone of the original to store in
Mike Snitzer745dc572017-12-11 20:51:50 -05001637 * ci.io->orig_bio to be used by end_io_acct() and
NeilBrown18a25da2017-09-06 09:43:28 +10001638 * for dec_pending to use for completion handling.
NeilBrown18a25da2017-09-06 09:43:28 +10001639 */
Mike Snitzerf21c6012018-06-15 09:35:33 -04001640 struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count,
1641 GFP_NOIO, &md->queue->bio_split);
Mike Snitzer745dc572017-12-11 20:51:50 -05001642 ci.io->orig_bio = b;
Mike Snitzera1e1cb72019-01-17 10:48:01 -05001643
1644 /*
1645 * Adjust IO stats for each split, otherwise upon queue
1646 * reentry there will be redundant IO accounting.
1647 * NOTE: this is a stop-gap fix, a proper fix involves
1648 * significant refactoring of DM core's bio splitting
1649 * (by eliminating DM's splitting and just using bio_split)
1650 */
1651 part_stat_lock();
1652 __dm_part_stat_sub(&dm_disk(md)->part0,
1653 sectors[op_stat_group(bio_op(bio))], ci.sector_count);
1654 part_stat_unlock();
1655
NeilBrown18a25da2017-09-06 09:43:28 +10001656 bio_chain(b, bio);
Mike Snitzer075c18c32019-01-18 01:21:11 -05001657 trace_block_split(md->queue, b, bio->bi_iter.bi_sector);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001658 ret = generic_make_request(bio);
NeilBrown18a25da2017-09-06 09:43:28 +10001659 break;
1660 }
1661 }
Tejun Heod87f4c12010-09-03 11:56:19 +02001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /* drop the extra reference count */
Bart Van Assche54385bf2017-08-09 11:32:10 -07001665 dec_pending(ci.io, errno_to_blk_status(error));
Mike Snitzer978e51b2017-12-09 15:16:42 -05001666 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669/*
Mike Snitzer978e51b2017-12-09 15:16:42 -05001670 * Optimized variant of __split_and_process_bio that leverages the
1671 * fact that targets that use it do _not_ have a need to split bios.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
Mike Snitzer568c73a2019-01-18 14:10:37 -05001673static blk_qc_t __process_bio(struct mapped_device *md, struct dm_table *map,
1674 struct bio *bio, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Mike Snitzer978e51b2017-12-09 15:16:42 -05001676 struct clone_info ci;
1677 blk_qc_t ret = BLK_QC_T_NONE;
1678 int error = 0;
1679
Mike Snitzer978e51b2017-12-09 15:16:42 -05001680 init_clone_info(&ci, md, map, bio);
1681
1682 if (bio->bi_opf & REQ_PREFLUSH) {
Jens Axboedbe3ece2018-12-19 09:13:34 -07001683 struct bio flush_bio;
1684
1685 /*
1686 * Use an on-stack bio for this, it's safe since we don't
1687 * need to reference it after submit. It's just used as
1688 * the basis for the clone(s).
1689 */
1690 bio_init(&flush_bio, NULL, 0);
1691 flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1692 ci.bio = &flush_bio;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001693 ci.sector_count = 0;
1694 error = __send_empty_flush(&ci);
1695 /* dec_pending submits any data associated with flush */
1696 } else {
Mike Snitzer978e51b2017-12-09 15:16:42 -05001697 struct dm_target_io *tio;
1698
Mike Snitzer978e51b2017-12-09 15:16:42 -05001699 ci.bio = bio;
1700 ci.sector_count = bio_sectors(bio);
Mike Snitzer568c73a2019-01-18 14:10:37 -05001701 if (__process_abnormal_io(&ci, ti, &error))
Mike Snitzer0519c712018-03-26 11:49:16 -04001702 goto out;
1703
1704 tio = alloc_tio(&ci, ti, 0, GFP_NOIO);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001705 ret = __clone_and_map_simple_bio(&ci, tio, NULL);
1706 }
1707out:
1708 /* drop the extra reference count */
1709 dec_pending(ci.io, errno_to_blk_status(error));
1710 return ret;
1711}
1712
Mike Snitzer568c73a2019-01-18 14:10:37 -05001713static void dm_queue_split(struct mapped_device *md, struct dm_target *ti, struct bio **bio)
1714{
1715 unsigned len, sector_count;
1716
1717 sector_count = bio_sectors(*bio);
1718 len = min_t(sector_t, max_io_len((*bio)->bi_iter.bi_sector, ti), sector_count);
1719
1720 if (sector_count > len) {
1721 struct bio *split = bio_split(*bio, len, GFP_NOIO, &md->queue->bio_split);
1722
1723 bio_chain(split, *bio);
1724 trace_block_split(md->queue, split, (*bio)->bi_iter.bi_sector);
1725 generic_make_request(*bio);
1726 *bio = split;
1727 }
1728}
1729
Mike Snitzer6548c7c2019-01-17 14:33:01 -05001730static blk_qc_t dm_process_bio(struct mapped_device *md,
1731 struct dm_table *map, struct bio *bio)
1732{
Mike Snitzer568c73a2019-01-18 14:10:37 -05001733 blk_qc_t ret = BLK_QC_T_NONE;
1734 struct dm_target *ti = md->immutable_target;
1735
1736 if (unlikely(!map)) {
1737 bio_io_error(bio);
1738 return ret;
1739 }
1740
1741 if (!ti) {
1742 ti = dm_table_find_target(map, bio->bi_iter.bi_sector);
Mikulas Patocka123d87d2019-08-23 09:55:26 -04001743 if (unlikely(!ti)) {
Mike Snitzer568c73a2019-01-18 14:10:37 -05001744 bio_io_error(bio);
1745 return ret;
1746 }
1747 }
1748
1749 /*
1750 * If in ->make_request_fn we need to use blk_queue_split(), otherwise
1751 * queue_limits for abnormal requests (e.g. discard, writesame, etc)
1752 * won't be imposed.
1753 */
1754 if (current->bio_list) {
Mike Snitzereffd58c2019-02-22 09:52:02 -05001755 blk_queue_split(md->queue, &bio);
1756 if (!is_abnormal_io(bio))
Mike Snitzer568c73a2019-01-18 14:10:37 -05001757 dm_queue_split(md, ti, &bio);
1758 }
1759
Mike Snitzer6548c7c2019-01-17 14:33:01 -05001760 if (dm_get_md_type(md) == DM_TYPE_NVME_BIO_BASED)
Mike Snitzer568c73a2019-01-18 14:10:37 -05001761 return __process_bio(md, map, bio, ti);
Mike Snitzer6548c7c2019-01-17 14:33:01 -05001762 else
1763 return __split_and_process_bio(md, map, bio);
1764}
1765
Mikulas Patocka24113d42018-11-06 22:34:59 +01001766static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct mapped_device *md = q->queuedata;
Mike Snitzer978e51b2017-12-09 15:16:42 -05001769 blk_qc_t ret = BLK_QC_T_NONE;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001770 int srcu_idx;
1771 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001773 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Tejun Heo6a8736d2010-09-08 18:07:00 +02001775 /* if we're suspended, we have to queue this io for later */
1776 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001777 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Jens Axboe1eff9d32016-08-05 15:35:16 -06001779 if (!(bio->bi_opf & REQ_RAHEAD))
Tejun Heo6a8736d2010-09-08 18:07:00 +02001780 queue_io(md, bio);
1781 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001782 bio_io_error(bio);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001783 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785
Mike Snitzer6548c7c2019-01-17 14:33:01 -05001786 ret = dm_process_bio(md, map, bio);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001787
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001788 dm_put_live_table(md, srcu_idx);
Mike Snitzer978e51b2017-12-09 15:16:42 -05001789 return ret;
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792static int dm_any_congested(void *congested_data, int bdi_bits)
1793{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001794 int r = bdi_bits;
1795 struct mapped_device *md = congested_data;
1796 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001798 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05001799 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001800 /*
Mike Snitzere522c032016-02-02 22:35:06 -05001801 * With request-based DM we only need to check the
1802 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001803 */
Jan Karadc3b17c2017-02-02 15:56:50 +01001804 r = md->queue->backing_dev_info->wb.state & bdi_bits;
Mike Snitzere522c032016-02-02 22:35:06 -05001805 } else {
1806 map = dm_get_live_table_fast(md);
1807 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001808 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05001809 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001810 }
1811 }
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return r;
1814}
1815
1816/*-----------------------------------------------------------------
1817 * An IDR is used to keep track of allocated minor numbers.
1818 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001819static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001821 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001823 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
1826/*
1827 * See if the device with a specific minor # is free.
1828 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001829static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001831 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (minor >= (1 << MINORBITS))
1834 return -EINVAL;
1835
Tejun Heoc9d76be2013-02-27 17:04:26 -08001836 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001837 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Tejun Heoc9d76be2013-02-27 17:04:26 -08001839 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001841 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001842 idr_preload_end();
1843 if (r < 0)
1844 return r == -ENOSPC ? -EBUSY : r;
1845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001848static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001850 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Tejun Heoc9d76be2013-02-27 17:04:26 -08001852 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001853 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Tejun Heoc9d76be2013-02-27 17:04:26 -08001855 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001857 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001858 idr_preload_end();
1859 if (r < 0)
1860 return r;
1861 *minor = r;
1862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001865static const struct block_device_operations dm_blk_dops;
Dan Williamsf26c5712017-04-12 12:35:44 -07001866static const struct dax_operations dm_dax_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Mikulas Patocka53d59142009-04-02 19:55:37 +01001868static void dm_wq_work(struct work_struct *work);
1869
Mike Snitzerc12c9a32018-01-12 09:32:21 -05001870static void dm_init_normal_md_queue(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001871{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001872 /*
1873 * Initialize aspects of queue that aren't relevant for blk-mq
1874 */
Jan Karadc3b17c2017-02-02 15:56:50 +01001875 md->queue->backing_dev_info->congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001876}
1877
Mike Snitzer0f209722015-04-28 11:50:29 -04001878static void cleanup_mapped_device(struct mapped_device *md)
1879{
Mike Snitzer0f209722015-04-28 11:50:29 -04001880 if (md->wq)
1881 destroy_workqueue(md->wq);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001882 bioset_exit(&md->bs);
1883 bioset_exit(&md->io_bs);
Mike Snitzer0f209722015-04-28 11:50:29 -04001884
Dan Williamsf26c5712017-04-12 12:35:44 -07001885 if (md->dax_dev) {
1886 kill_dax(md->dax_dev);
1887 put_dax(md->dax_dev);
1888 md->dax_dev = NULL;
1889 }
1890
Mike Snitzer0f209722015-04-28 11:50:29 -04001891 if (md->disk) {
1892 spin_lock(&_minor_lock);
1893 md->disk->private_data = NULL;
1894 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04001895 del_gendisk(md->disk);
1896 put_disk(md->disk);
1897 }
1898
1899 if (md->queue)
1900 blk_cleanup_queue(md->queue);
1901
Tahsin Erdogand09960b2016-10-10 05:35:19 -07001902 cleanup_srcu_struct(&md->io_barrier);
1903
Mike Snitzer0f209722015-04-28 11:50:29 -04001904 if (md->bdev) {
1905 bdput(md->bdev);
1906 md->bdev = NULL;
1907 }
Mike Snitzer4cc96132016-05-12 16:28:10 -04001908
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05001909 mutex_destroy(&md->suspend_lock);
1910 mutex_destroy(&md->type_lock);
1911 mutex_destroy(&md->table_devices_lock);
1912
Mike Snitzer4cc96132016-05-12 16:28:10 -04001913 dm_mq_cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916/*
1917 * Allocate and initialise a blank device with a given minor.
1918 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001919static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Mike Snitzer115485e2016-02-22 12:16:21 -05001921 int r, numa_node_id = dm_get_numa_node();
1922 struct mapped_device *md;
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001923 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Mikulas Patocka856eb092017-10-31 19:33:02 -04001925 md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (!md) {
1927 DMWARN("unable to allocate device, out of memory.");
1928 return NULL;
1929 }
1930
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001931 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001932 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001935 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001936 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001937 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001938 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001940 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001942 r = init_srcu_struct(&md->io_barrier);
1943 if (r < 0)
1944 goto bad_io_barrier;
1945
Mike Snitzer115485e2016-02-22 12:16:21 -05001946 md->numa_node_id = numa_node_id;
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001947 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01001948 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001949 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001950 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001951 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001952 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001954 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001956 atomic_set(&md->uevent_seq, 0);
1957 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001958 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001959 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Christoph Hellwig6d469642018-11-14 17:02:18 +01001961 md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04001963 goto bad;
Mike Snitzerc12c9a32018-01-12 09:32:21 -05001964 md->queue->queuedata = md;
1965 md->queue->backing_dev_info->congested_data = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Mike Snitzerc12c9a32018-01-12 09:32:21 -05001967 md->disk = alloc_disk_node(1, md->numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04001969 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001971 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001972 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001973 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05001974 init_completion(&md->kobj_holder.completion);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 md->disk->major = _major;
1977 md->disk->first_minor = minor;
1978 md->disk->fops = &dm_blk_dops;
1979 md->disk->queue = md->queue;
1980 md->disk->private_data = md;
1981 sprintf(md->disk->disk_name, "dm-%d", minor);
Dan Williamsf26c5712017-04-12 12:35:44 -07001982
Dan Williams976431b2018-03-29 17:22:13 -07001983 if (IS_ENABLED(CONFIG_DAX_DRIVER)) {
Pankaj Guptafefc1d92019-07-05 19:33:24 +05301984 md->dax_dev = alloc_dax(md, md->disk->disk_name,
1985 &dm_dax_ops, 0);
Peng Wang514cf4f2019-04-18 21:59:19 +08001986 if (!md->dax_dev)
Dan Williams976431b2018-03-29 17:22:13 -07001987 goto bad;
1988 }
Dan Williamsf26c5712017-04-12 12:35:44 -07001989
Mike Snitzerc100ec42018-01-08 20:03:04 -05001990 add_disk_no_queue_reg(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001991 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Tejun Heo670368a2013-07-30 08:40:21 -04001993 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001994 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04001995 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00001996
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001997 md->bdev = bdget_disk(md->disk, 0);
1998 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04001999 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002000
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002001 dm_stats_init(&md->stats);
2002
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002003 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002004 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002005 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002006 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002007
2008 BUG_ON(old_md != MINOR_ALLOCED);
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return md;
2011
Mike Snitzer0f209722015-04-28 11:50:29 -04002012bad:
2013 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002014bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002016bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002017 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002018bad_module_get:
Mikulas Patocka856eb092017-10-31 19:33:02 -04002019 kvfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return NULL;
2021}
2022
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002023static void unlock_fs(struct mapped_device *md);
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025static void free_dev(struct mapped_device *md)
2026{
Tejun Heof331c022008-09-03 09:01:48 +02002027 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002028
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002029 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002030
Mike Snitzer0f209722015-04-28 11:50:29 -04002031 cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04002032
2033 free_table_devices(&md->table_devices);
2034 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002035 free_minor(minor);
2036
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002037 module_put(THIS_MODULE);
Mikulas Patocka856eb092017-10-31 19:33:02 -04002038 kvfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002041static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002042{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002043 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002044 int ret = 0;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002045
Mike Snitzer0776aa02017-12-08 14:40:52 -05002046 if (dm_table_bio_based(t)) {
Mike Snitzer64f52b02017-12-11 23:17:47 -05002047 /*
2048 * The md may already have mempools that need changing.
2049 * If so, reload bioset because front_pad may have changed
2050 * because a different table was loaded.
2051 */
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002052 bioset_exit(&md->bs);
2053 bioset_exit(&md->io_bs);
Mike Snitzer0776aa02017-12-08 14:40:52 -05002054
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002055 } else if (bioset_initialized(&md->bs)) {
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002056 /*
2057 * There's no need to reload with request-based dm
2058 * because the size of front_pad doesn't change.
2059 * Note for future: If you are to reload bioset,
2060 * prep-ed requests in the queue may refer
2061 * to bio from the old bioset, so you must walk
2062 * through the queue to unprep.
2063 */
2064 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002065 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002066
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002067 BUG_ON(!p ||
2068 bioset_initialized(&md->bs) ||
2069 bioset_initialized(&md->io_bs));
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002070
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002071 ret = bioset_init_from_src(&md->bs, &p->bs);
2072 if (ret)
2073 goto out;
2074 ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
2075 if (ret)
2076 bioset_exit(&md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002077out:
Mike Snitzer02233342015-03-10 23:49:26 -04002078 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002079 dm_table_free_md_mempools(t);
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002080 return ret;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083/*
2084 * Bind a table to the device.
2085 */
2086static void event_callback(void *context)
2087{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002088 unsigned long flags;
2089 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct mapped_device *md = (struct mapped_device *) context;
2091
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002092 spin_lock_irqsave(&md->uevent_lock, flags);
2093 list_splice_init(&md->uevent_list, &uevents);
2094 spin_unlock_irqrestore(&md->uevent_lock, flags);
2095
Tejun Heoed9e1982008-08-25 19:56:05 +09002096 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 atomic_inc(&md->event_nr);
2099 wake_up(&md->eventq);
Mikulas Patocka62e08242017-09-20 07:29:49 -04002100 dm_issue_global_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Mike Snitzerc2176492011-01-13 19:53:46 +00002103/*
2104 * Protected by md->suspend_lock obtained by dm_swap_table().
2105 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002106static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Bart Van Assche1ea06542017-04-27 10:11:21 -07002108 lockdep_assert_held(&md->suspend_lock);
2109
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002110 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002112 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002115/*
2116 * Returns old map, which caller must destroy.
2117 */
2118static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2119 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002121 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002122 struct request_queue *q = md->queue;
Mike Snitzer978e51b2017-12-09 15:16:42 -05002123 bool request_based = dm_table_request_based(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 sector_t size;
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002125 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07002127 lockdep_assert_held(&md->suspend_lock);
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002130
2131 /*
2132 * Wipe any geometry if the size of the table changed.
2133 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002134 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002135 memset(&md->geometry, 0, sizeof(md->geometry));
2136
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002137 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002139 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002140
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002141 /*
2142 * The queue hasn't been stopped yet, if the old table type wasn't
2143 * for request-based during suspension. So stop it to prevent
2144 * I/O mapping before resume.
2145 * This must be done before setting the queue restrictions,
2146 * because request-based dm may be run just after the setting.
2147 */
Mike Snitzer978e51b2017-12-09 15:16:42 -05002148 if (request_based)
Mike Snitzereca7ee62016-02-20 13:45:38 -05002149 dm_stop_queue(q);
Mike Snitzer978e51b2017-12-09 15:16:42 -05002150
2151 if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) {
Mike Snitzer16f12262016-01-31 17:22:27 -05002152 /*
Mike Snitzer978e51b2017-12-09 15:16:42 -05002153 * Leverage the fact that request-based DM targets and
2154 * NVMe bio based targets are immutable singletons
2155 * - used to optimize both dm_request_fn and dm_mq_queue_rq;
2156 * and __process_bio.
Mike Snitzer16f12262016-01-31 17:22:27 -05002157 */
2158 md->immutable_target = dm_table_get_immutable_target(t);
2159 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002160
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002161 ret = __bind_mempools(md, t);
2162 if (ret) {
2163 old_map = ERR_PTR(ret);
2164 goto out;
2165 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002166
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002167 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -05002168 rcu_assign_pointer(md->map, (void *)t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002169 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2170
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002171 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002172 if (old_map)
2173 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002174
Jens Axboe2a2a4c52018-06-07 14:42:06 -06002175out:
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002176 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Alasdair G Kergona7940152009-12-10 23:52:23 +00002179/*
2180 * Returns unbound table for the caller to free.
2181 */
2182static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002184 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002187 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302190 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002191 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002192
2193 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
2196/*
2197 * Constructor for a new device.
2198 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002199int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Mike Snitzerc12c9a32018-01-12 09:32:21 -05002201 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct mapped_device *md;
2203
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002204 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (!md)
2206 return -ENXIO;
2207
Mike Snitzerc12c9a32018-01-12 09:32:21 -05002208 r = dm_sysfs_init(md);
2209 if (r) {
2210 free_dev(md);
2211 return r;
2212 }
Milan Broz784aae72009-01-06 03:05:12 +00002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 *result = md;
2215 return 0;
2216}
2217
Mike Snitzera5664da2010-08-12 04:14:01 +01002218/*
2219 * Functions to manage md->type.
2220 * All are required to hold md->type_lock.
2221 */
2222void dm_lock_md_type(struct mapped_device *md)
2223{
2224 mutex_lock(&md->type_lock);
2225}
2226
2227void dm_unlock_md_type(struct mapped_device *md)
2228{
2229 mutex_unlock(&md->type_lock);
2230}
2231
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002232void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
Mike Snitzera5664da2010-08-12 04:14:01 +01002233{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002234 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002235 md->type = type;
2236}
2237
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002238enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
Mike Snitzera5664da2010-08-12 04:14:01 +01002239{
2240 return md->type;
2241}
2242
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002243struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2244{
2245 return md->immutable_target_type;
2246}
2247
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002248/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002249 * The queue_limits are only valid as long as you have a reference
2250 * count on 'md'.
2251 */
2252struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2253{
2254 BUG_ON(!atomic_read(&md->holders));
2255 return &md->queue->limits;
2256}
2257EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2258
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002259/*
2260 * Setup the DM device's queue based on md's type
2261 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002262int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002263{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002264 int r;
Mike Snitzerc100ec42018-01-08 20:03:04 -05002265 struct queue_limits limits;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002266 enum dm_queue_mode type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002267
Toshi Kani545ed202016-06-22 17:54:53 -06002268 switch (type) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002269 case DM_TYPE_REQUEST_BASED:
Mike Snitzere83068a2016-05-24 21:16:51 -04002270 r = dm_mq_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002271 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002272 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002273 return r;
2274 }
2275 break;
2276 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002277 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzer978e51b2017-12-09 15:16:42 -05002278 case DM_TYPE_NVME_BIO_BASED:
2279 dm_init_normal_md_queue(md);
Mikulas Patocka24113d42018-11-06 22:34:59 +01002280 blk_queue_make_request(md->queue, dm_make_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002281 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002282 case DM_TYPE_NONE:
2283 WARN_ON_ONCE(true);
2284 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002285 }
2286
Mike Snitzerc100ec42018-01-08 20:03:04 -05002287 r = dm_calculate_queue_limits(t, &limits);
2288 if (r) {
2289 DMERR("Cannot calculate initial queue limits");
2290 return r;
2291 }
2292 dm_table_set_restrictions(t, md->queue, &limits);
2293 blk_register_queue(md->disk);
2294
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002295 return 0;
2296}
2297
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002298struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 unsigned minor = MINOR(dev);
2302
2303 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2304 return NULL;
2305
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002306 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 md = idr_find(&_minor_idr, minor);
Mike Snitzer49de5762017-11-06 16:40:10 -05002309 if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
2310 test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2311 md = NULL;
2312 goto out;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002313 }
Mike Snitzer49de5762017-11-06 16:40:10 -05002314 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002315out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002316 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
David Teigland637842c2006-01-06 00:20:00 -08002318 return md;
2319}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002320EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002321
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002322void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002323{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002324 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
2327void dm_set_mdptr(struct mapped_device *md, void *ptr)
2328{
2329 md->interface_ptr = ptr;
2330}
2331
2332void dm_get(struct mapped_device *md)
2333{
2334 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002335 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336}
2337
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002338int dm_hold(struct mapped_device *md)
2339{
2340 spin_lock(&_minor_lock);
2341 if (test_bit(DMF_FREEING, &md->flags)) {
2342 spin_unlock(&_minor_lock);
2343 return -EBUSY;
2344 }
2345 dm_get(md);
2346 spin_unlock(&_minor_lock);
2347 return 0;
2348}
2349EXPORT_SYMBOL_GPL(dm_hold);
2350
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002351const char *dm_device_name(struct mapped_device *md)
2352{
2353 return md->name;
2354}
2355EXPORT_SYMBOL_GPL(dm_device_name);
2356
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002357static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002359 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002360 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002362 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002363
Mike Snitzer63a4f062015-03-23 17:01:43 -04002364 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002365 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2366 set_bit(DMF_FREEING, &md->flags);
2367 spin_unlock(&_minor_lock);
2368
Mike Snitzerc12c9a32018-01-12 09:32:21 -05002369 blk_set_queue_dying(md->queue);
Bart Van Assche3b785fb2016-08-31 15:17:49 -07002370
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002371 /*
2372 * Take suspend_lock so that presuspend and postsuspend methods
2373 * do not race with internal suspend.
2374 */
2375 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002376 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002377 if (!dm_suspended_md(md)) {
2378 dm_table_presuspend_targets(map);
2379 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002381 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2382 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002383 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002384
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002385 /*
2386 * Rare, but there may be I/O requests still going to complete,
2387 * for example. Wait for all references to disappear.
2388 * No one should increment the reference count of the mapped_device,
2389 * after the mapped_device state becomes DMF_FREEING.
2390 */
2391 if (wait)
2392 while (atomic_read(&md->holders))
2393 msleep(1);
2394 else if (atomic_read(&md->holders))
2395 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2396 dm_device_name(md), atomic_read(&md->holders));
2397
2398 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002399 dm_table_destroy(__unbind(md));
2400 free_dev(md);
2401}
2402
2403void dm_destroy(struct mapped_device *md)
2404{
2405 __dm_destroy(md, true);
2406}
2407
2408void dm_destroy_immediate(struct mapped_device *md)
2409{
2410 __dm_destroy(md, false);
2411}
2412
2413void dm_put(struct mapped_device *md)
2414{
2415 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
Edward Goggin79eb8852007-05-09 02:32:56 -07002417EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002419static int dm_wait_for_completion(struct mapped_device *md, long task_state)
Milan Broz46125c12008-02-08 02:10:30 +00002420{
2421 int r = 0;
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002422 DEFINE_WAIT(wait);
Milan Broz46125c12008-02-08 02:10:30 +00002423
2424 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002425 prepare_to_wait(&md->wait, &wait, task_state);
Milan Broz46125c12008-02-08 02:10:30 +00002426
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002427 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002428 break;
2429
Bart Van Asschee3fabdf2016-08-31 15:16:22 -07002430 if (signal_pending_state(task_state, current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002431 r = -EINTR;
2432 break;
2433 }
2434
2435 io_schedule();
2436 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002437 finish_wait(&md->wait, &wait);
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002438
Milan Broz46125c12008-02-08 02:10:30 +00002439 return r;
2440}
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442/*
2443 * Process the deferred bios
2444 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002445static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Mikulas Patockaef208582009-04-02 19:55:38 +01002447 struct mapped_device *md = container_of(work, struct mapped_device,
2448 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002449 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002450 int srcu_idx;
2451 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002453 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002454
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002455 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002456 spin_lock_irq(&md->deferred_lock);
2457 c = bio_list_pop(&md->deferred);
2458 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002459
Tejun Heo6a8736d2010-09-08 18:07:00 +02002460 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002461 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002462
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002463 if (dm_request_based(md))
Mike Snitzer6548c7c2019-01-17 14:33:01 -05002464 (void) generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002465 else
Mike Snitzer6548c7c2019-01-17 14:33:01 -05002466 (void) dm_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002467 }
Milan Broz73d410c2008-02-08 02:10:25 +00002468
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002469 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002472static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002473{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002474 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002475 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002476 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002477}
2478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002480 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002482struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
Mike Christie87eb5b22013-03-01 22:45:48 +00002484 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002485 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002486 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Daniel Walkere61290a2008-02-08 02:10:08 +00002488 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002491 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Mike Snitzer3ae70652012-09-26 23:45:45 +01002494 /*
2495 * If the new table has no data devices, retain the existing limits.
2496 * This helps multipath with queue_if_no_path if all paths disappear,
2497 * then new I/O is queued based on these limits, and then some paths
2498 * reappear.
2499 */
2500 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002501 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002502 if (live_map)
2503 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002504 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002505 }
2506
Mike Christie87eb5b22013-03-01 22:45:48 +00002507 if (!live_map) {
2508 r = dm_calculate_queue_limits(table, &limits);
2509 if (r) {
2510 map = ERR_PTR(r);
2511 goto out;
2512 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002513 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002514
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002515 map = __bind(md, table, &limits);
Mikulas Patocka62e08242017-09-20 07:29:49 -04002516 dm_issue_global_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002518out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002519 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002520 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523/*
2524 * Functions to lock and unlock any filesystem running on the
2525 * device.
2526 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002527static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002529 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002532
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002533 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002534 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002535 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002536 md->frozen_sb = NULL;
2537 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002538 }
2539
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002540 set_bit(DMF_FROZEN, &md->flags);
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return 0;
2543}
2544
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002545static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002547 if (!test_bit(DMF_FROZEN, &md->flags))
2548 return;
2549
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002550 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002552 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
2555/*
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002556 * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2557 * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2558 * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2559 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04002560 * If __dm_suspend returns 0, the device is completely quiescent
2561 * now. There is no request-processing activity. All new requests
2562 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002563 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04002564static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002565 unsigned suspend_flags, long task_state,
Mike Snitzereaf9a732016-08-02 13:07:20 -04002566 int dmf_suspended_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002568 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2569 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2570 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07002572 lockdep_assert_held(&md->suspend_lock);
2573
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002574 /*
2575 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2576 * This flag is cleared before dm_suspend returns.
2577 */
2578 if (noflush)
2579 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Bart Van Assche86331f32017-04-27 10:11:26 -07002580 else
2581 pr_debug("%s: suspending with flush\n", dm_device_name(md));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002582
Mike Snitzerd67ee212014-10-28 20:13:31 -04002583 /*
2584 * This gets reverted if there's an error later and the targets
2585 * provide the .presuspend_undo hook.
2586 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002587 dm_table_presuspend_targets(map);
2588
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002589 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002590 * Flush I/O to the device.
2591 * Any I/O submitted after lock_fs() may not be flushed.
2592 * noflush takes precedence over do_lockfs.
2593 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002594 */
2595 if (!noflush && do_lockfs) {
2596 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002597 if (r) {
2598 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002599 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04002600 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002604 * Here we must make sure that no processes are submitting requests
2605 * to target drivers i.e. no one may be executing
2606 * __split_and_process_bio. This is called from dm_request and
2607 * dm_wq_work.
2608 *
2609 * To get all processes out of __split_and_process_bio in dm_request,
2610 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002611 * __split_and_process_bio from dm_request and quiesce the thread
2612 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2613 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002615 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002616 if (map)
2617 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002619 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002620 * Stop md->queue before flushing md->wq in case request-based
2621 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002622 */
Jens Axboe6a23e052018-10-10 20:49:26 -06002623 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002624 dm_stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002625
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002626 flush_workqueue(md->wq);
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002629 * At this point no more requests are entering target request routines.
2630 * We call dm_wait_for_completion to wait for all existing requests
2631 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 */
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002633 r = dm_wait_for_completion(md, task_state);
Mike Snitzereaf9a732016-08-02 13:07:20 -04002634 if (!r)
2635 set_bit(dmf_suspended_flag, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Milan Broz6d6f10d2008-02-08 02:10:22 +00002637 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002638 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002639 if (map)
2640 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002643 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002644 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002645
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002646 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002647 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002648
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002649 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002650 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002651 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002652 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002653
Mike Snitzerffcc3932014-10-28 18:34:52 -04002654 return r;
2655}
2656
2657/*
2658 * We need to be able to change a mapping table under a mounted
2659 * filesystem. For example we might want to move some data in
2660 * the background. Before the table can be swapped with
2661 * dm_bind_table, dm_suspend must be called to flush any in
2662 * flight bios and ensure that any further io gets deferred.
2663 */
2664/*
2665 * Suspend mechanism in request-based dm.
2666 *
2667 * 1. Flush all I/Os by lock_fs() if needed.
2668 * 2. Stop dispatching any I/O by stopping the request_queue.
2669 * 3. Wait for all in-flight I/Os to be completed or requeued.
2670 *
2671 * To abort suspend, start the request_queue.
2672 */
2673int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2674{
2675 struct dm_table *map = NULL;
2676 int r = 0;
2677
2678retry:
2679 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2680
2681 if (dm_suspended_md(md)) {
2682 r = -EINVAL;
2683 goto out_unlock;
2684 }
2685
2686 if (dm_suspended_internally_md(md)) {
2687 /* already internally suspended, wait for internal resume */
2688 mutex_unlock(&md->suspend_lock);
2689 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2690 if (r)
2691 return r;
2692 goto retry;
2693 }
2694
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002695 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002696
Mike Snitzereaf9a732016-08-02 13:07:20 -04002697 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002698 if (r)
2699 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002700
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002701 dm_table_postsuspend_targets(map);
2702
Alasdair G Kergond2874832006-11-08 17:44:43 -08002703out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002704 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002705 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Mike Snitzerffcc3932014-10-28 18:34:52 -04002708static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002710 if (map) {
2711 int r = dm_table_resume_targets(map);
2712 if (r)
2713 return r;
2714 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002715
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002716 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002717
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002718 /*
2719 * Flushing deferred I/Os must be done after targets are resumed
2720 * so that mapping of targets can work correctly.
2721 * Request-based dm is queueing the deferred I/Os in its request_queue.
2722 */
2723 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002724 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002725
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002726 unlock_fs(md);
2727
Mike Snitzerffcc3932014-10-28 18:34:52 -04002728 return 0;
2729}
2730
2731int dm_resume(struct mapped_device *md)
2732{
Minfei Huang8dc23652016-09-06 16:00:29 +08002733 int r;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002734 struct dm_table *map = NULL;
2735
2736retry:
Minfei Huang8dc23652016-09-06 16:00:29 +08002737 r = -EINVAL;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002738 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2739
2740 if (!dm_suspended_md(md))
2741 goto out;
2742
2743 if (dm_suspended_internally_md(md)) {
2744 /* already internally suspended, wait for internal resume */
2745 mutex_unlock(&md->suspend_lock);
2746 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2747 if (r)
2748 return r;
2749 goto retry;
2750 }
2751
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002752 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002753 if (!map || !dm_table_get_size(map))
2754 goto out;
2755
2756 r = __dm_resume(md, map);
2757 if (r)
2758 goto out;
2759
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002760 clear_bit(DMF_SUSPENDED, &md->flags);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002761out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002762 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002763
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002764 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002767/*
2768 * Internal suspend/resume works like userspace-driven suspend. It waits
2769 * until all bios finish and prevents issuing new bios to the target drivers.
2770 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002771 */
2772
Mike Snitzerffcc3932014-10-28 18:34:52 -04002773static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2774{
2775 struct dm_table *map = NULL;
2776
Bart Van Assche1ea06542017-04-27 10:11:21 -07002777 lockdep_assert_held(&md->suspend_lock);
2778
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002779 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002780 return; /* nested internal suspend */
2781
2782 if (dm_suspended_md(md)) {
2783 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2784 return; /* nest suspend */
2785 }
2786
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002787 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002788
2789 /*
2790 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2791 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
2792 * would require changing .presuspend to return an error -- avoid this
2793 * until there is a need for more elaborate variants of internal suspend.
2794 */
Mike Snitzereaf9a732016-08-02 13:07:20 -04002795 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2796 DMF_SUSPENDED_INTERNALLY);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002797
2798 dm_table_postsuspend_targets(map);
2799}
2800
2801static void __dm_internal_resume(struct mapped_device *md)
2802{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002803 BUG_ON(!md->internal_suspend_count);
2804
2805 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002806 return; /* resume from nested internal suspend */
2807
2808 if (dm_suspended_md(md))
2809 goto done; /* resume from nested suspend */
2810
2811 /*
2812 * NOTE: existing callers don't need to call dm_table_resume_targets
2813 * (which may fail -- so best to avoid it for now by passing NULL map)
2814 */
2815 (void) __dm_resume(md, NULL);
2816
2817done:
2818 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2819 smp_mb__after_atomic();
2820 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2821}
2822
2823void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002824{
2825 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002826 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2827 mutex_unlock(&md->suspend_lock);
2828}
2829EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2830
2831void dm_internal_resume(struct mapped_device *md)
2832{
2833 mutex_lock(&md->suspend_lock);
2834 __dm_internal_resume(md);
2835 mutex_unlock(&md->suspend_lock);
2836}
2837EXPORT_SYMBOL_GPL(dm_internal_resume);
2838
2839/*
2840 * Fast variants of internal suspend/resume hold md->suspend_lock,
2841 * which prevents interaction with userspace-driven suspend.
2842 */
2843
2844void dm_internal_suspend_fast(struct mapped_device *md)
2845{
2846 mutex_lock(&md->suspend_lock);
2847 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002848 return;
2849
2850 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2851 synchronize_srcu(&md->io_barrier);
2852 flush_workqueue(md->wq);
2853 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2854}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002855EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002856
Mike Snitzerffcc3932014-10-28 18:34:52 -04002857void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002858{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002859 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002860 goto done;
2861
2862 dm_queue_flush(md);
2863
2864done:
2865 mutex_unlock(&md->suspend_lock);
2866}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002867EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869/*-----------------------------------------------------------------
2870 * Event notification.
2871 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002872int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002873 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002874{
Milan Broz60935eb2009-06-22 10:12:30 +01002875 char udev_cookie[DM_COOKIE_LENGTH];
2876 char *envp[] = { udev_cookie, NULL };
2877
2878 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002879 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002880 else {
2881 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2882 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002883 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2884 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002885 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002886}
2887
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002888uint32_t dm_next_uevent_seq(struct mapped_device *md)
2889{
2890 return atomic_add_return(1, &md->uevent_seq);
2891}
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893uint32_t dm_get_event_nr(struct mapped_device *md)
2894{
2895 return atomic_read(&md->event_nr);
2896}
2897
2898int dm_wait_event(struct mapped_device *md, int event_nr)
2899{
2900 return wait_event_interruptible(md->eventq,
2901 (event_nr != atomic_read(&md->event_nr)));
2902}
2903
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002904void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2905{
2906 unsigned long flags;
2907
2908 spin_lock_irqsave(&md->uevent_lock, flags);
2909 list_add(elist, &md->uevent_list);
2910 spin_unlock_irqrestore(&md->uevent_lock, flags);
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913/*
2914 * The gendisk is only valid as long as you have a reference
2915 * count on 'md'.
2916 */
2917struct gendisk *dm_disk(struct mapped_device *md)
2918{
2919 return md->disk;
2920}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00002921EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Milan Broz784aae72009-01-06 03:05:12 +00002923struct kobject *dm_kobject(struct mapped_device *md)
2924{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002925 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00002926}
2927
Milan Broz784aae72009-01-06 03:05:12 +00002928struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2929{
2930 struct mapped_device *md;
2931
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002932 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00002933
Hou Taob9a41d22017-11-01 15:42:36 +08002934 spin_lock(&_minor_lock);
2935 if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2936 md = NULL;
2937 goto out;
2938 }
Milan Broz784aae72009-01-06 03:05:12 +00002939 dm_get(md);
Hou Taob9a41d22017-11-01 15:42:36 +08002940out:
2941 spin_unlock(&_minor_lock);
2942
Milan Broz784aae72009-01-06 03:05:12 +00002943 return md;
2944}
2945
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002946int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 return test_bit(DMF_SUSPENDED, &md->flags);
2949}
2950
Mike Snitzerffcc3932014-10-28 18:34:52 -04002951int dm_suspended_internally_md(struct mapped_device *md)
2952{
2953 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2954}
2955
Mikulas Patocka2c140a22013-11-01 18:27:41 -04002956int dm_test_deferred_remove_flag(struct mapped_device *md)
2957{
2958 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2959}
2960
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002961int dm_suspended(struct dm_target *ti)
2962{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002963 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002964}
2965EXPORT_SYMBOL_GPL(dm_suspended);
2966
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002967int dm_noflush_suspending(struct dm_target *ti)
2968{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002969 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002970}
2971EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2972
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002973struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
Mike Snitzer0776aa02017-12-08 14:40:52 -05002974 unsigned integrity, unsigned per_io_data_size,
2975 unsigned min_pool_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002976{
Mike Snitzer115485e2016-02-22 12:16:21 -05002977 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
Mike Snitzer78d8e582015-06-26 10:01:13 -04002978 unsigned int pool_size = 0;
Mike Snitzer64f52b02017-12-11 23:17:47 -05002979 unsigned int front_pad, io_front_pad;
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002980 int ret;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002981
2982 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002983 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002984
Mike Snitzer78d8e582015-06-26 10:01:13 -04002985 switch (type) {
2986 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002987 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzer22c11852017-12-04 21:07:37 -05002988 case DM_TYPE_NVME_BIO_BASED:
Mike Snitzer0776aa02017-12-08 14:40:52 -05002989 pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
Mike Snitzer30187e12016-01-31 13:28:26 -05002990 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 -05002991 io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002992 ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
2993 if (ret)
Mike Snitzer64f52b02017-12-11 23:17:47 -05002994 goto out;
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002995 if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
Christoph Hellwigeb8db832017-01-22 18:32:46 +01002996 goto out;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002997 break;
2998 case DM_TYPE_REQUEST_BASED:
Mike Snitzer0776aa02017-12-08 14:40:52 -05002999 pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
Mike Snitzer78d8e582015-06-26 10:01:13 -04003000 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05003001 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04003002 break;
3003 default:
3004 BUG();
3005 }
3006
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003007 ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
3008 if (ret)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003009 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003010
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003011 if (integrity && bioset_integrity_create(&pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003012 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003013
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003014 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003015
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003016out:
3017 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003018
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003019 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003020}
3021
3022void dm_free_md_mempools(struct dm_md_mempools *pools)
3023{
3024 if (!pools)
3025 return;
3026
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003027 bioset_exit(&pools->bs);
3028 bioset_exit(&pools->io_bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003029
3030 kfree(pools);
3031}
3032
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09003033struct dm_pr {
3034 u64 old_key;
3035 u64 new_key;
3036 u32 flags;
3037 bool fail_early;
3038};
3039
3040static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
3041 void *data)
3042{
3043 struct mapped_device *md = bdev->bd_disk->private_data;
3044 struct dm_table *table;
3045 struct dm_target *ti;
3046 int ret = -ENOTTY, srcu_idx;
3047
3048 table = dm_get_live_table(md, &srcu_idx);
3049 if (!table || !dm_table_get_size(table))
3050 goto out;
3051
3052 /* We only support devices that have a single target */
3053 if (dm_table_get_num_targets(table) != 1)
3054 goto out;
3055 ti = dm_table_get_target(table, 0);
3056
3057 ret = -EINVAL;
3058 if (!ti->type->iterate_devices)
3059 goto out;
3060
3061 ret = ti->type->iterate_devices(ti, fn, data);
3062out:
3063 dm_put_live_table(md, srcu_idx);
3064 return ret;
3065}
3066
3067/*
3068 * For register / unregister we need to manually call out to every path.
3069 */
3070static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
3071 sector_t start, sector_t len, void *data)
3072{
3073 struct dm_pr *pr = data;
3074 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
3075
3076 if (!ops || !ops->pr_register)
3077 return -EOPNOTSUPP;
3078 return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
3079}
3080
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003081static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003082 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003083{
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09003084 struct dm_pr pr = {
3085 .old_key = old_key,
3086 .new_key = new_key,
3087 .flags = flags,
3088 .fail_early = true,
3089 };
3090 int ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003091
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09003092 ret = dm_call_pr(bdev, __dm_pr_register, &pr);
3093 if (ret && new_key) {
3094 /* unregister all paths if we failed to register any path */
3095 pr.old_key = new_key;
3096 pr.new_key = 0;
3097 pr.flags = 0;
3098 pr.fail_early = false;
3099 dm_call_pr(bdev, __dm_pr_register, &pr);
3100 }
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003101
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09003102 return ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003103}
3104
3105static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05003106 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003107{
3108 struct mapped_device *md = bdev->bd_disk->private_data;
3109 const struct pr_ops *ops;
Mike Snitzer971888c2018-04-03 15:05:12 -04003110 int r, srcu_idx;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003111
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04003112 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003113 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -04003114 goto out;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003115
3116 ops = bdev->bd_disk->fops->pr_ops;
3117 if (ops && ops->pr_reserve)
3118 r = ops->pr_reserve(bdev, key, type, flags);
3119 else
3120 r = -EOPNOTSUPP;
Mike Snitzer971888c2018-04-03 15:05:12 -04003121out:
3122 dm_unprepare_ioctl(md, srcu_idx);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003123 return r;
3124}
3125
3126static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3127{
3128 struct mapped_device *md = bdev->bd_disk->private_data;
3129 const struct pr_ops *ops;
Mike Snitzer971888c2018-04-03 15:05:12 -04003130 int r, srcu_idx;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003131
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04003132 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003133 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -04003134 goto out;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003135
3136 ops = bdev->bd_disk->fops->pr_ops;
3137 if (ops && ops->pr_release)
3138 r = ops->pr_release(bdev, key, type);
3139 else
3140 r = -EOPNOTSUPP;
Mike Snitzer971888c2018-04-03 15:05:12 -04003141out:
3142 dm_unprepare_ioctl(md, srcu_idx);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003143 return r;
3144}
3145
3146static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003147 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003148{
3149 struct mapped_device *md = bdev->bd_disk->private_data;
3150 const struct pr_ops *ops;
Mike Snitzer971888c2018-04-03 15:05:12 -04003151 int r, srcu_idx;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003152
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04003153 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003154 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -04003155 goto out;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003156
3157 ops = bdev->bd_disk->fops->pr_ops;
3158 if (ops && ops->pr_preempt)
3159 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3160 else
3161 r = -EOPNOTSUPP;
Mike Snitzer971888c2018-04-03 15:05:12 -04003162out:
3163 dm_unprepare_ioctl(md, srcu_idx);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003164 return r;
3165}
3166
3167static int dm_pr_clear(struct block_device *bdev, u64 key)
3168{
3169 struct mapped_device *md = bdev->bd_disk->private_data;
3170 const struct pr_ops *ops;
Mike Snitzer971888c2018-04-03 15:05:12 -04003171 int r, srcu_idx;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003172
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04003173 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003174 if (r < 0)
Mike Snitzer971888c2018-04-03 15:05:12 -04003175 goto out;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003176
3177 ops = bdev->bd_disk->fops->pr_ops;
3178 if (ops && ops->pr_clear)
3179 r = ops->pr_clear(bdev, key);
3180 else
3181 r = -EOPNOTSUPP;
Mike Snitzer971888c2018-04-03 15:05:12 -04003182out:
3183 dm_unprepare_ioctl(md, srcu_idx);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003184 return r;
3185}
3186
3187static const struct pr_ops dm_pr_ops = {
3188 .pr_register = dm_pr_register,
3189 .pr_reserve = dm_pr_reserve,
3190 .pr_release = dm_pr_release,
3191 .pr_preempt = dm_pr_preempt,
3192 .pr_clear = dm_pr_clear,
3193};
3194
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003195static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 .open = dm_blk_open,
3197 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003198 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003199 .getgeo = dm_blk_getgeo,
Christoph Hellwige76239a2018-10-12 19:08:49 +09003200 .report_zones = dm_blk_report_zones,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003201 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 .owner = THIS_MODULE
3203};
3204
Dan Williamsf26c5712017-04-12 12:35:44 -07003205static const struct dax_operations dm_dax_ops = {
3206 .direct_access = dm_dax_direct_access,
Dan Williams7bf7eac2019-05-16 13:26:29 -07003207 .dax_supported = dm_dax_supported,
Dan Williams7e026c82017-05-29 12:57:56 -07003208 .copy_from_iter = dm_dax_copy_from_iter,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -07003209 .copy_to_iter = dm_dax_copy_to_iter,
Dan Williamsf26c5712017-04-12 12:35:44 -07003210};
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212/*
3213 * module hooks
3214 */
3215module_init(dm_init);
3216module_exit(dm_exit);
3217
3218module_param(major, uint, 0);
3219MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003220
Mike Snitzere8603132013-09-12 18:06:12 -04003221module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3222MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3223
Mike Snitzer115485e2016-02-22 12:16:21 -05003224module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3225MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227MODULE_DESCRIPTION(DM_NAME " driver");
3228MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3229MODULE_LICENSE("GPL");