blob: ff22aa2a07b509c626429c48ef063d2ab43baa9f [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>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080022#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010023#include <linux/delay.h>
Mike Snitzerffcc3932014-10-28 18:34:52 -040024#include <linux/wait.h>
Christoph Hellwig71cdb692015-10-15 14:10:51 +020025#include <linux/pr.h>
Li Zefan55782132009-06-09 13:43:05 +080026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "core"
28
Namhyung Kim71a16732011-10-31 20:18:54 +000029#ifdef CONFIG_PRINTK
30/*
31 * ratelimit state to be used in DMXXX_LIMIT().
32 */
33DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
34 DEFAULT_RATELIMIT_INTERVAL,
35 DEFAULT_RATELIMIT_BURST);
36EXPORT_SYMBOL(dm_ratelimit_state);
37#endif
38
Milan Broz60935eb2009-06-22 10:12:30 +010039/*
40 * Cookies are numeric values sent with CHANGE and REMOVE
41 * uevents while resuming, removing or renaming the device.
42 */
43#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
44#define DM_COOKIE_LENGTH 24
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static const char *_name = DM_NAME;
47
48static unsigned int major = 0;
49static unsigned int _major = 0;
50
Alasdair G Kergond15b7742011-08-02 12:32:01 +010051static DEFINE_IDR(_minor_idr);
52
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070053static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040054
55static void do_deferred_remove(struct work_struct *w);
56
57static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
58
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040059static struct workqueue_struct *deferred_remove_workqueue;
60
Mikulas Patocka93e64422017-01-16 16:05:59 -050061atomic_t dm_global_event_nr = ATOMIC_INIT(0);
62DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * One of these is allocated per bio.
66 */
67struct dm_io {
68 struct mapped_device *md;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020069 blk_status_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010071 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080072 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010073 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040074 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070077#define MINOR_ALLOCED ((void *)-1)
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * Bits for the md->flags field.
81 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +010082#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080084#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -070085#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070086#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080087#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -070088#define DMF_DEFERRED_REMOVE 6
89#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Mike Snitzer115485e2016-02-22 12:16:21 -050091#define DM_NUMA_NODE NUMA_NO_NODE
Mike Snitzer115485e2016-02-22 12:16:21 -050092static int dm_numa_node = DM_NUMA_NODE;
Mike Snitzerfaad87d2016-01-28 16:52:56 -050093
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +010094/*
95 * For mempools pre-allocation at the table loading time.
96 */
97struct dm_md_mempools {
98 mempool_t *io_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +010099 struct bio_set *bs;
100};
101
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500102struct table_device {
103 struct list_head list;
104 atomic_t count;
105 struct dm_dev dm_dev;
106};
107
Christoph Lametere18b8902006-12-06 20:33:20 -0800108static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000109static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500110static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700111
Mike Snitzerf4790822013-09-12 18:06:12 -0400112/*
Mike Snitzere8603132013-09-12 18:06:12 -0400113 * Bio-based DM's mempools' reserved IOs set by the user.
114 */
Mike Snitzer4cc96132016-05-12 16:28:10 -0400115#define RESERVED_BIO_BASED_IOS 16
Mike Snitzere8603132013-09-12 18:06:12 -0400116static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
117
Mike Snitzer115485e2016-02-22 12:16:21 -0500118static int __dm_get_module_param_int(int *module_param, int min, int max)
119{
120 int param = ACCESS_ONCE(*module_param);
121 int modified_param = 0;
122 bool modified = true;
123
124 if (param < min)
125 modified_param = min;
126 else if (param > max)
127 modified_param = max;
128 else
129 modified = false;
130
131 if (modified) {
132 (void)cmpxchg(module_param, param, modified_param);
133 param = modified_param;
134 }
135
136 return param;
137}
138
Mike Snitzer4cc96132016-05-12 16:28:10 -0400139unsigned __dm_get_module_param(unsigned *module_param,
140 unsigned def, unsigned max)
Mike Snitzerf4790822013-09-12 18:06:12 -0400141{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500142 unsigned param = ACCESS_ONCE(*module_param);
143 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400144
Mike Snitzer09c2d532015-02-27 22:25:26 -0500145 if (!param)
146 modified_param = def;
147 else if (param > max)
148 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400149
Mike Snitzer09c2d532015-02-27 22:25:26 -0500150 if (modified_param) {
151 (void)cmpxchg(module_param, param, modified_param);
152 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400153 }
154
Mike Snitzer09c2d532015-02-27 22:25:26 -0500155 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400156}
157
Mike Snitzere8603132013-09-12 18:06:12 -0400158unsigned dm_get_reserved_bio_based_ios(void)
159{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500160 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzer4cc96132016-05-12 16:28:10 -0400161 RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
Mike Snitzere8603132013-09-12 18:06:12 -0400162}
163EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
164
Mike Snitzer115485e2016-02-22 12:16:21 -0500165static unsigned dm_get_numa_node(void)
166{
167 return __dm_get_module_param_int(&dm_numa_node,
168 DM_NUMA_NODE, num_online_nodes() - 1);
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static int __init local_init(void)
172{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100173 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100176 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100178 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000180 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
181 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100182 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000183
Mike Snitzereca7ee62016-02-20 13:45:38 -0500184 _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500185 __alignof__(struct request), 0, NULL);
186 if (!_rq_cache)
187 goto out_free_rq_tio_cache;
188
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100189 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100190 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500191 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100192
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400193 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
194 if (!deferred_remove_workqueue) {
195 r = -ENOMEM;
196 goto out_uevent_exit;
197 }
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 _major = major;
200 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100201 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400202 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (!_major)
205 _major = r;
206
207 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100208
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400209out_free_workqueue:
210 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100211out_uevent_exit:
212 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500213out_free_rq_cache:
214 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000215out_free_rq_tio_cache:
216 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100217out_free_io_cache:
218 kmem_cache_destroy(_io_cache);
219
220 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static void local_exit(void)
224{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400225 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400226 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400227
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500228 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000229 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700231 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100232 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 _major = 0;
235
236 DMINFO("cleaned up");
237}
238
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000239static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 local_init,
241 dm_target_init,
242 dm_linear_init,
243 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000244 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100245 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400247 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000250static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 local_exit,
252 dm_target_exit,
253 dm_linear_exit,
254 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000255 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100256 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400258 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
261static int __init dm_init(void)
262{
263 const int count = ARRAY_SIZE(_inits);
264
265 int r, i;
266
267 for (i = 0; i < count; i++) {
268 r = _inits[i]();
269 if (r)
270 goto bad;
271 }
272
273 return 0;
274
275 bad:
276 while (i--)
277 _exits[i]();
278
279 return r;
280}
281
282static void __exit dm_exit(void)
283{
284 int i = ARRAY_SIZE(_exits);
285
286 while (i--)
287 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100288
289 /*
290 * Should be empty by this point.
291 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100292 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/*
296 * Block device functions
297 */
Mike Anderson432a2122009-12-10 23:52:20 +0000298int dm_deleting_md(struct mapped_device *md)
299{
300 return test_bit(DMF_DELETING, &md->flags);
301}
302
Al Virofe5f9f22008-03-02 10:29:31 -0500303static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct mapped_device *md;
306
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700307 spin_lock(&_minor_lock);
308
Al Virofe5f9f22008-03-02 10:29:31 -0500309 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700310 if (!md)
311 goto out;
312
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700313 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000314 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700315 md = NULL;
316 goto out;
317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700320 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700321out:
322 spin_unlock(&_minor_lock);
323
324 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Al Virodb2a1442013-05-05 21:52:57 -0400327static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400329 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200330
Milan Broz4a1aeb92011-01-13 19:59:48 +0000331 spin_lock(&_minor_lock);
332
Mike Snitzer63a4f062015-03-23 17:01:43 -0400333 md = disk->private_data;
334 if (WARN_ON(!md))
335 goto out;
336
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400337 if (atomic_dec_and_test(&md->open_count) &&
338 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400339 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400342out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000343 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700346int dm_open_count(struct mapped_device *md)
347{
348 return atomic_read(&md->open_count);
349}
350
351/*
352 * Guarantees nothing is using the device before it's deleted.
353 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400354int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700355{
356 int r = 0;
357
358 spin_lock(&_minor_lock);
359
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400360 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700361 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400362 if (mark_deferred)
363 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
364 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
365 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700366 else
367 set_bit(DMF_DELETING, &md->flags);
368
369 spin_unlock(&_minor_lock);
370
371 return r;
372}
373
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400374int dm_cancel_deferred_remove(struct mapped_device *md)
375{
376 int r = 0;
377
378 spin_lock(&_minor_lock);
379
380 if (test_bit(DMF_DELETING, &md->flags))
381 r = -EBUSY;
382 else
383 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
384
385 spin_unlock(&_minor_lock);
386
387 return r;
388}
389
390static void do_deferred_remove(struct work_struct *w)
391{
392 dm_deferred_remove();
393}
394
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400395sector_t dm_get_size(struct mapped_device *md)
396{
397 return get_capacity(md->disk);
398}
399
Mike Snitzer9974fa22014-02-28 15:33:43 +0100400struct request_queue *dm_get_md_queue(struct mapped_device *md)
401{
402 return md->queue;
403}
404
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400405struct dm_stats *dm_get_stats(struct mapped_device *md)
406{
407 return &md->stats;
408}
409
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800410static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
411{
412 struct mapped_device *md = bdev->bd_disk->private_data;
413
414 return dm_get_geometry(md, geo);
415}
416
Mike Snitzer956a4022016-02-18 16:13:51 -0500417static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
418 struct block_device **bdev,
419 fmode_t *mode)
Milan Brozaa129a22006-10-03 01:15:15 -0700420{
Mike Snitzer66482022016-02-18 15:44:39 -0500421 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100422 struct dm_table *map;
Mike Snitzer956a4022016-02-18 16:13:51 -0500423 int srcu_idx, r;
Milan Brozaa129a22006-10-03 01:15:15 -0700424
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100425retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200426 r = -ENOTTY;
Mike Snitzer956a4022016-02-18 16:13:51 -0500427 map = dm_get_live_table(md, &srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700428 if (!map || !dm_table_get_size(map))
429 goto out;
430
431 /* We only support devices that have a single target */
432 if (dm_table_get_num_targets(map) != 1)
433 goto out;
434
Mike Snitzer66482022016-02-18 15:44:39 -0500435 tgt = dm_table_get_target(map, 0);
436 if (!tgt->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500437 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700438
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000439 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700440 r = -EAGAIN;
441 goto out;
442 }
443
Mike Snitzer66482022016-02-18 15:44:39 -0500444 r = tgt->type->prepare_ioctl(tgt, bdev, mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200445 if (r < 0)
446 goto out;
447
Mike Snitzer956a4022016-02-18 16:13:51 -0500448 bdgrab(*bdev);
449 dm_put_live_table(md, srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200450 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700451
452out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500453 dm_put_live_table(md, srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000454 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100455 msleep(10);
456 goto retry;
457 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200458 return r;
459}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100460
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200461static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
462 unsigned int cmd, unsigned long arg)
463{
464 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer956a4022016-02-18 16:13:51 -0500465 int r;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200466
Mike Snitzer956a4022016-02-18 16:13:51 -0500467 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200468 if (r < 0)
469 return r;
470
471 if (r > 0) {
472 /*
Christoph Hellwige980f622017-02-04 10:45:03 +0100473 * Target determined this ioctl is being issued against a
474 * subset of the parent bdev; require extra privileges.
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200475 */
Christoph Hellwige980f622017-02-04 10:45:03 +0100476 if (!capable(CAP_SYS_RAWIO)) {
477 DMWARN_LIMIT(
478 "%s: sending ioctl %x to DM device without required privilege.",
479 current->comm, cmd);
480 r = -ENOIOCTLCMD;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200481 goto out;
Christoph Hellwige980f622017-02-04 10:45:03 +0100482 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200483 }
484
Mike Snitzer66482022016-02-18 15:44:39 -0500485 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200486out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500487 bdput(bdev);
Milan Brozaa129a22006-10-03 01:15:15 -0700488 return r;
489}
490
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100491static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 return mempool_alloc(md->io_pool, GFP_NOIO);
494}
495
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100496static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 mempool_free(io, md->io_pool);
499}
500
Mike Snitzercfae7522016-04-11 12:05:38 -0400501static void free_tio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Mikulas Patockadba14162012-10-12 21:02:15 +0100503 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Mike Snitzer4cc96132016-05-12 16:28:10 -0400506int md_in_flight(struct mapped_device *md)
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000507{
508 return atomic_read(&md->pending[READ]) +
509 atomic_read(&md->pending[WRITE]);
510}
511
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800512static void start_io_acct(struct dm_io *io)
513{
514 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400515 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900516 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400517 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800518
519 io->start_time = jiffies;
520
Tejun Heo074a7ac2008-08-25 19:56:14 +0900521 cpu = part_stat_lock();
522 part_round_stats(cpu, &dm_disk(md)->part0);
523 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100524 atomic_set(&dm_disk(md)->part0.in_flight[rw],
525 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400526
527 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500528 dm_stats_account_io(&md->stats, bio_data_dir(bio),
529 bio->bi_iter.bi_sector, bio_sectors(bio),
530 false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800531}
532
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000533static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800534{
535 struct mapped_device *md = io->md;
536 struct bio *bio = io->bio;
537 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800538 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800539 int rw = bio_data_dir(bio);
540
Gu Zheng18c0b222014-11-24 11:05:26 +0800541 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800542
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400543 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500544 dm_stats_account_io(&md->stats, bio_data_dir(bio),
545 bio->bi_iter.bi_sector, bio_sectors(bio),
546 true, duration, &io->stats_aux);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400547
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100548 /*
549 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200550 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100551 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100552 pending = atomic_dec_return(&md->pending[rw]);
553 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200554 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800555
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000556 /* nudge anyone waiting on suspend queue */
557 if (!pending)
558 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/*
562 * Add the bio to the list of deferred io.
563 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100564static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200566 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200568 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200570 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200571 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/*
575 * Everyone (including functions in this file), should use this
576 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100577 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100579struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100581 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100583 return srcu_dereference(md->map, &md->io_barrier);
584}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100586void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
587{
588 srcu_read_unlock(&md->io_barrier, srcu_idx);
589}
590
591void dm_sync_table(struct mapped_device *md)
592{
593 synchronize_srcu(&md->io_barrier);
594 synchronize_rcu_expedited();
595}
596
597/*
598 * A fast alternative to dm_get_live_table/dm_put_live_table.
599 * The caller must not block between these two functions.
600 */
601static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
602{
603 rcu_read_lock();
604 return rcu_dereference(md->map);
605}
606
607static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
608{
609 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800612/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500613 * Open a table device so we can use it as a map destination.
614 */
615static int open_table_device(struct table_device *td, dev_t dev,
616 struct mapped_device *md)
617{
618 static char *_claim_ptr = "I belong to device-mapper";
619 struct block_device *bdev;
620
621 int r;
622
623 BUG_ON(td->dm_dev.bdev);
624
625 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
626 if (IS_ERR(bdev))
627 return PTR_ERR(bdev);
628
629 r = bd_link_disk_holder(bdev, dm_disk(md));
630 if (r) {
631 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
632 return r;
633 }
634
635 td->dm_dev.bdev = bdev;
Dan Williams817bf402017-04-12 13:37:44 -0700636 td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500637 return 0;
638}
639
640/*
641 * Close a table device that we've been using.
642 */
643static void close_table_device(struct table_device *td, struct mapped_device *md)
644{
645 if (!td->dm_dev.bdev)
646 return;
647
648 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
649 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
Dan Williams817bf402017-04-12 13:37:44 -0700650 put_dax(td->dm_dev.dax_dev);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500651 td->dm_dev.bdev = NULL;
Dan Williams817bf402017-04-12 13:37:44 -0700652 td->dm_dev.dax_dev = NULL;
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500653}
654
655static struct table_device *find_table_device(struct list_head *l, dev_t dev,
656 fmode_t mode) {
657 struct table_device *td;
658
659 list_for_each_entry(td, l, list)
660 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
661 return td;
662
663 return NULL;
664}
665
666int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
667 struct dm_dev **result) {
668 int r;
669 struct table_device *td;
670
671 mutex_lock(&md->table_devices_lock);
672 td = find_table_device(&md->table_devices, dev, mode);
673 if (!td) {
Mike Snitzer115485e2016-02-22 12:16:21 -0500674 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500675 if (!td) {
676 mutex_unlock(&md->table_devices_lock);
677 return -ENOMEM;
678 }
679
680 td->dm_dev.mode = mode;
681 td->dm_dev.bdev = NULL;
682
683 if ((r = open_table_device(td, dev, md))) {
684 mutex_unlock(&md->table_devices_lock);
685 kfree(td);
686 return r;
687 }
688
689 format_dev_t(td->dm_dev.name, dev);
690
691 atomic_set(&td->count, 0);
692 list_add(&td->list, &md->table_devices);
693 }
694 atomic_inc(&td->count);
695 mutex_unlock(&md->table_devices_lock);
696
697 *result = &td->dm_dev;
698 return 0;
699}
700EXPORT_SYMBOL_GPL(dm_get_table_device);
701
702void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
703{
704 struct table_device *td = container_of(d, struct table_device, dm_dev);
705
706 mutex_lock(&md->table_devices_lock);
707 if (atomic_dec_and_test(&td->count)) {
708 close_table_device(td, md);
709 list_del(&td->list);
710 kfree(td);
711 }
712 mutex_unlock(&md->table_devices_lock);
713}
714EXPORT_SYMBOL(dm_put_table_device);
715
716static void free_table_devices(struct list_head *devices)
717{
718 struct list_head *tmp, *next;
719
720 list_for_each_safe(tmp, next, devices) {
721 struct table_device *td = list_entry(tmp, struct table_device, list);
722
723 DMWARN("dm_destroy: %s still exists with %d references",
724 td->dm_dev.name, atomic_read(&td->count));
725 kfree(td);
726 }
727}
728
729/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800730 * Get the geometry associated with a dm device
731 */
732int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
733{
734 *geo = md->geometry;
735
736 return 0;
737}
738
739/*
740 * Set the geometry of a device.
741 */
742int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
743{
744 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
745
746 if (geo->start > sz) {
747 DMWARN("Start sector is beyond the geometry limits.");
748 return -EINVAL;
749 }
750
751 md->geometry = *geo;
752
753 return 0;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*-----------------------------------------------------------------
757 * CRUD START:
758 * A more elegant soln is in the works that uses the queue
759 * merge fn, unfortunately there are a couple of changes to
760 * the block layer that I want to make for this. So in the
761 * interests of getting something for people to use I give
762 * you this clearly demarcated crap.
763 *---------------------------------------------------------------*/
764
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800765static int __noflush_suspending(struct mapped_device *md)
766{
767 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770/*
771 * Decrements the number of outstanding ios that a bio has been
772 * cloned into, completing the original io if necc.
773 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200774static void dec_pending(struct dm_io *io, blk_status_t error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800776 unsigned long flags;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200777 blk_status_t io_error;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000778 struct bio *bio;
779 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800780
781 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100782 if (unlikely(error)) {
783 spin_lock_irqsave(&io->endio_lock, flags);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200784 if (!(io->status == BLK_STS_DM_REQUEUE &&
785 __noflush_suspending(md)))
786 io->status = error;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100787 spin_unlock_irqrestore(&io->endio_lock, flags);
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (atomic_dec_and_test(&io->io_count)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200791 if (io->status == BLK_STS_DM_REQUEUE) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800792 /*
793 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800794 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100795 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200796 if (__noflush_suspending(md))
797 bio_list_add_head(&md->deferred, io->bio);
798 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800799 /* noflush suspend was interrupted. */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200800 io->status = BLK_STS_IOERR;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100801 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800802 }
803
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200804 io_error = io->status;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000805 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200806 end_io_acct(io);
807 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100808
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200809 if (io_error == BLK_STS_DM_REQUEUE)
Tejun Heo6a8736d2010-09-08 18:07:00 +0200810 return;
811
Jens Axboe1eff9d32016-08-05 15:35:16 -0600812 if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100813 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200814 * Preflush done for flush with data, reissue
Mike Christie28a8f0d2016-06-05 14:32:25 -0500815 * without REQ_PREFLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100816 */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600817 bio->bi_opf &= ~REQ_PREFLUSH;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200818 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100819 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200820 /* done with normal IO or empty flush */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200821 bio->bi_status = io_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200822 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825}
826
Mike Snitzer4cc96132016-05-12 16:28:10 -0400827void disable_write_same(struct mapped_device *md)
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400828{
829 struct queue_limits *limits = dm_get_queue_limits(md);
830
831 /* device doesn't really support WRITE SAME, disable it */
832 limits->max_write_same_sectors = 0;
833}
834
Christoph Hellwigac62d622017-04-05 19:21:05 +0200835void disable_write_zeroes(struct mapped_device *md)
836{
837 struct queue_limits *limits = dm_get_queue_limits(md);
838
839 /* device doesn't really support WRITE ZEROES, disable it */
840 limits->max_write_zeroes_sectors = 0;
841}
842
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200843static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200845 blk_status_t error = bio->bi_status;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500846 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000847 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700848 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 dm_endio_fn endio = tio->ti->type->end_io;
850
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200851 if (unlikely(error == BLK_STS_TARGET)) {
Christoph Hellwigac62d622017-04-05 19:21:05 +0200852 if (bio_op(bio) == REQ_OP_WRITE_SAME &&
853 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors)
854 disable_write_same(md);
855 if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
856 !bdev_get_queue(bio->bi_bdev)->limits.max_write_zeroes_sectors)
857 disable_write_zeroes(md);
858 }
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400859
Christoph Hellwig1be56902017-06-03 09:38:03 +0200860 if (endio) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200861 int r = endio(tio->ti, bio, &error);
Christoph Hellwig1be56902017-06-03 09:38:03 +0200862 switch (r) {
863 case DM_ENDIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200864 error = BLK_STS_DM_REQUEUE;
Christoph Hellwig1be56902017-06-03 09:38:03 +0200865 /*FALLTHRU*/
866 case DM_ENDIO_DONE:
867 break;
868 case DM_ENDIO_INCOMPLETE:
869 /* The target will handle the io */
870 return;
871 default:
872 DMWARN("unimplemented target endio return value: %d", r);
873 BUG();
874 }
875 }
876
Mike Snitzercfae7522016-04-11 12:05:38 -0400877 free_tio(tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000878 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Mike Snitzer78d8e582015-06-26 10:01:13 -0400881/*
Mike Snitzer56a67df2010-08-12 04:14:10 +0100882 * Return maximum size of I/O possible at the supplied sector up to the current
883 * target boundary.
884 */
885static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100887 sector_t target_offset = dm_target_offset(ti, sector);
888
889 return ti->len - target_offset;
890}
891
892static sector_t max_io_len(sector_t sector, struct dm_target *ti)
893{
894 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +0100895 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /*
Mike Snitzer542f9032012-07-27 15:08:00 +0100898 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100900 if (ti->max_io_len) {
901 offset = dm_target_offset(ti, sector);
902 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
903 max_len = sector_div(offset, ti->max_io_len);
904 else
905 max_len = offset & (ti->max_io_len - 1);
906 max_len = ti->max_io_len - max_len;
907
908 if (len > max_len)
909 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
912 return len;
913}
914
Mike Snitzer542f9032012-07-27 15:08:00 +0100915int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
916{
917 if (len > UINT_MAX) {
918 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
919 (unsigned long long)len, UINT_MAX);
920 ti->error = "Maximum size of target IO is too large";
921 return -EINVAL;
922 }
923
924 ti->max_io_len = (uint32_t) len;
925
926 return 0;
927}
928EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
929
Dan Williamsf26c5712017-04-12 12:35:44 -0700930static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
931 sector_t sector, int *srcu_idx)
Toshi Kani545ed202016-06-22 17:54:53 -0600932{
Toshi Kani545ed202016-06-22 17:54:53 -0600933 struct dm_table *map;
934 struct dm_target *ti;
Toshi Kani545ed202016-06-22 17:54:53 -0600935
Dan Williamsf26c5712017-04-12 12:35:44 -0700936 map = dm_get_live_table(md, srcu_idx);
Toshi Kani545ed202016-06-22 17:54:53 -0600937 if (!map)
Dan Williamsf26c5712017-04-12 12:35:44 -0700938 return NULL;
Toshi Kani545ed202016-06-22 17:54:53 -0600939
940 ti = dm_table_find_target(map, sector);
941 if (!dm_target_is_valid(ti))
Dan Williamsf26c5712017-04-12 12:35:44 -0700942 return NULL;
943
944 return ti;
945}
946
947static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
948 long nr_pages, void **kaddr, pfn_t *pfn)
949{
950 struct mapped_device *md = dax_get_private(dax_dev);
951 sector_t sector = pgoff * PAGE_SECTORS;
952 struct dm_target *ti;
953 long len, ret = -EIO;
954 int srcu_idx;
955
956 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
957
958 if (!ti)
Toshi Kani545ed202016-06-22 17:54:53 -0600959 goto out;
Dan Williamsf26c5712017-04-12 12:35:44 -0700960 if (!ti->type->direct_access)
961 goto out;
962 len = max_io_len(sector, ti) / PAGE_SECTORS;
963 if (len < 1)
964 goto out;
965 nr_pages = min(len, nr_pages);
Toshi Kani545ed202016-06-22 17:54:53 -0600966 if (ti->type->direct_access)
Dan Williams817bf402017-04-12 13:37:44 -0700967 ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
968
Dan Williamsf26c5712017-04-12 12:35:44 -0700969 out:
Toshi Kani545ed202016-06-22 17:54:53 -0600970 dm_put_live_table(md, srcu_idx);
Dan Williamsf26c5712017-04-12 12:35:44 -0700971
972 return ret;
Toshi Kani545ed202016-06-22 17:54:53 -0600973}
974
Mikulas Patocka1dd40c32014-03-14 18:41:24 -0400975/*
976 * A target may call dm_accept_partial_bio only from the map routine. It is
Mike Christie28a8f0d2016-06-05 14:32:25 -0500977 * allowed for all bio types except REQ_PREFLUSH.
Mikulas Patocka1dd40c32014-03-14 18:41:24 -0400978 *
979 * dm_accept_partial_bio informs the dm that the target only wants to process
980 * additional n_sectors sectors of the bio and the rest of the data should be
981 * sent in a next bio.
982 *
983 * A diagram that explains the arithmetics:
984 * +--------------------+---------------+-------+
985 * | 1 | 2 | 3 |
986 * +--------------------+---------------+-------+
987 *
988 * <-------------- *tio->len_ptr --------------->
989 * <------- bi_size ------->
990 * <-- n_sectors -->
991 *
992 * Region 1 was already iterated over with bio_advance or similar function.
993 * (it may be empty if the target doesn't use bio_advance)
994 * Region 2 is the remaining bio size that the target wants to process.
995 * (it may be empty if region 1 is non-empty, although there is no reason
996 * to make it empty)
997 * The target requires that region 3 is to be sent in the next bio.
998 *
999 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1000 * the partially processed part (the sum of regions 1+2) must be the same for all
1001 * copies of the bio.
1002 */
1003void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1004{
1005 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1006 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Jens Axboe1eff9d32016-08-05 15:35:16 -06001007 BUG_ON(bio->bi_opf & REQ_PREFLUSH);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001008 BUG_ON(bi_size > *tio->len_ptr);
1009 BUG_ON(n_sectors > bi_size);
1010 *tio->len_ptr -= bi_size - n_sectors;
1011 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1012}
1013EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1014
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001015/*
1016 * Flush current->bio_list when the target map method blocks.
1017 * This fixes deadlocks in snapshot and possibly in other targets.
1018 */
1019struct dm_offload {
1020 struct blk_plug plug;
1021 struct blk_plug_cb cb;
1022};
1023
1024static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
1025{
1026 struct dm_offload *o = container_of(cb, struct dm_offload, cb);
1027 struct bio_list list;
1028 struct bio *bio;
NeilBrownf5fe1b52017-03-10 17:00:47 +11001029 int i;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001030
1031 INIT_LIST_HEAD(&o->cb.list);
1032
1033 if (unlikely(!current->bio_list))
1034 return;
1035
NeilBrownf5fe1b52017-03-10 17:00:47 +11001036 for (i = 0; i < 2; i++) {
1037 list = current->bio_list[i];
1038 bio_list_init(&current->bio_list[i]);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001039
NeilBrownf5fe1b52017-03-10 17:00:47 +11001040 while ((bio = bio_list_pop(&list))) {
1041 struct bio_set *bs = bio->bi_pool;
NeilBrown47e0fb42017-06-18 14:38:57 +10001042 if (unlikely(!bs) || bs == fs_bio_set ||
1043 !bs->rescue_workqueue) {
NeilBrownf5fe1b52017-03-10 17:00:47 +11001044 bio_list_add(&current->bio_list[i], bio);
1045 continue;
1046 }
1047
1048 spin_lock(&bs->rescue_lock);
1049 bio_list_add(&bs->rescue_list, bio);
1050 queue_work(bs->rescue_workqueue, &bs->rescue_work);
1051 spin_unlock(&bs->rescue_lock);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001052 }
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001053 }
1054}
1055
1056static void dm_offload_start(struct dm_offload *o)
1057{
1058 blk_start_plug(&o->plug);
1059 o->cb.callback = flush_current_bio_list;
1060 list_add(&o->cb.list, &current->plug->cb_list);
1061}
1062
1063static void dm_offload_end(struct dm_offload *o)
1064{
1065 list_del(&o->cb.list);
1066 blk_finish_plug(&o->plug);
1067}
1068
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001069static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001072 sector_t sector;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001073 struct dm_offload o;
Mikulas Patockadba14162012-10-12 21:02:15 +01001074 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001075 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /*
1080 * Map the clone. If r == 0 we don't need to do
1081 * anything, the target has assumed ownership of
1082 * this io.
1083 */
1084 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001085 sector = clone->bi_iter.bi_sector;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001086
1087 dm_offload_start(&o);
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001088 r = ti->type->map(ti, clone);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001089 dm_offload_end(&o);
1090
Christoph Hellwig846785e2017-06-03 09:38:02 +02001091 switch (r) {
1092 case DM_MAPIO_SUBMITTED:
1093 break;
1094 case DM_MAPIO_REMAPPED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /* the bio has been remapped so dispatch it */
Mike Snitzerd07335e2010-11-16 12:52:38 +01001096 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1097 tio->io->bio->bi_bdev->bd_dev, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 generic_make_request(clone);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001099 break;
1100 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001101 dec_pending(tio->io, BLK_STS_IOERR);
1102 free_tio(tio);
1103 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +02001104 case DM_MAPIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001105 dec_pending(tio->io, BLK_STS_DM_REQUEUE);
Mike Snitzercfae7522016-04-11 12:05:38 -04001106 free_tio(tio);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001107 break;
1108 default:
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001109 DMWARN("unimplemented target map return value: %d", r);
1110 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112}
1113
1114struct clone_info {
1115 struct mapped_device *md;
1116 struct dm_table *map;
1117 struct bio *bio;
1118 struct dm_io *io;
1119 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001120 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121};
1122
Mikulas Patockae0d66092014-03-14 18:40:39 -04001123static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001124{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001125 bio->bi_iter.bi_sector = sector;
1126 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129/*
1130 * Creates a bio that consists of range of complete bvecs.
1131 */
Mike Snitzerc80914e2016-03-02 12:33:03 -05001132static int clone_bio(struct dm_target_io *tio, struct bio *bio,
1133 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Mikulas Patockadba14162012-10-12 21:02:15 +01001135 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001137 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Mikulas Patockae2460f22017-04-18 16:51:48 -04001139 if (unlikely(bio_integrity(bio) != NULL)) {
1140 int r;
1141
1142 if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1143 !dm_target_passes_integrity(tio->ti->type))) {
1144 DMWARN("%s: the target %s doesn't support integrity data.",
1145 dm_device_name(tio->io->md),
1146 tio->ti->type->name);
1147 return -EIO;
1148 }
1149
1150 r = bio_integrity_clone(clone, bio, GFP_NOIO);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001151 if (r < 0)
1152 return r;
1153 }
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001154
1155 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1156 clone->bi_iter.bi_size = to_bytes(len);
1157
Mikulas Patockae2460f22017-04-18 16:51:48 -04001158 if (unlikely(bio_integrity(bio) != NULL))
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001159 bio_integrity_trim(clone, 0, len);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001160
1161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001164static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001165 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001166 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001167{
Mikulas Patockadba14162012-10-12 21:02:15 +01001168 struct dm_target_io *tio;
1169 struct bio *clone;
1170
Junichi Nomura99778272014-10-03 11:55:16 +00001171 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001172 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001173
1174 tio->io = ci->io;
1175 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001176 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001177
1178 return tio;
1179}
1180
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001181static void __clone_and_map_simple_bio(struct clone_info *ci,
1182 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001183 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001184{
Junichi Nomura99778272014-10-03 11:55:16 +00001185 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001186 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001187
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001188 tio->len_ptr = len;
1189
Junichi Nomura99778272014-10-03 11:55:16 +00001190 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001191 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001192 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001193
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001194 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001195}
1196
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001197static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001198 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001199{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001200 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001201
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001202 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001203 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001204}
1205
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001206static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001207{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001208 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001209 struct dm_target *ti;
1210
Mike Snitzerb372d362010-09-08 18:07:01 +02001211 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001212 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001213 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001214
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001215 return 0;
1216}
1217
Mike Snitzerc80914e2016-03-02 12:33:03 -05001218static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001219 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001220{
Mikulas Patockadba14162012-10-12 21:02:15 +01001221 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001222 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001223 unsigned target_bio_nr;
1224 unsigned num_target_bios = 1;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001225 int r = 0;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001226
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001227 /*
1228 * Does the target want to receive duplicate copies of the bio?
1229 */
1230 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1231 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001232
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001233 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001234 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001235 tio->len_ptr = len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001236 r = clone_bio(tio, bio, sector, *len);
Mikulas Patocka072623d2016-04-09 12:48:18 -04001237 if (r < 0) {
Mike Snitzercfae7522016-04-11 12:05:38 -04001238 free_tio(tio);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001239 break;
Mikulas Patocka072623d2016-04-09 12:48:18 -04001240 }
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001241 __map_bio(tio);
1242 }
Mike Snitzerc80914e2016-03-02 12:33:03 -05001243
1244 return r;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001245}
1246
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001247typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001248
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001249static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001250{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001251 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001252}
1253
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001254static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001255{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001256 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001257}
1258
Christoph Hellwigac62d622017-04-05 19:21:05 +02001259static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1260{
1261 return ti->num_write_zeroes_bios;
1262}
1263
Mike Snitzer23508a92012-12-21 20:23:37 +00001264typedef bool (*is_split_required_fn)(struct dm_target *ti);
1265
1266static bool is_split_required_for_discard(struct dm_target *ti)
1267{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001268 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001269}
1270
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001271static int __send_changing_extent_only(struct clone_info *ci,
1272 get_num_bios_fn get_num_bios,
1273 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001274{
1275 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001276 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001277 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001278
Mike Snitzera79245b2010-08-12 04:14:24 +01001279 do {
1280 ti = dm_table_find_target(ci->map, ci->sector);
1281 if (!dm_target_is_valid(ti))
1282 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001283
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001284 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001285 * Even though the device advertised support for this type of
1286 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001287 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001288 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001289 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001290 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1291 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001292 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001293
Mike Snitzer23508a92012-12-21 20:23:37 +00001294 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001295 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001296 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001297 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001298
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001299 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001300
1301 ci->sector += len;
1302 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001303
1304 return 0;
1305}
1306
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001307static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001308{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001309 return __send_changing_extent_only(ci, get_num_discard_bios,
1310 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001311}
1312
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001313static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001314{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001315 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001316}
1317
Christoph Hellwigac62d622017-04-05 19:21:05 +02001318static int __send_write_zeroes(struct clone_info *ci)
1319{
1320 return __send_changing_extent_only(ci, get_num_write_zeroes_bios, NULL);
1321}
1322
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001323/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001324 * Select the correct strategy for processing a non-flush bio.
1325 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001326static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Mikulas Patockadba14162012-10-12 21:02:15 +01001328 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001329 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001330 unsigned len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001331 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Mike Christiee6047142016-06-05 14:32:04 -05001333 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001334 return __send_discard(ci);
Mike Christiee6047142016-06-05 14:32:04 -05001335 else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001336 return __send_write_same(ci);
Christoph Hellwigac62d622017-04-05 19:21:05 +02001337 else if (unlikely(bio_op(bio) == REQ_OP_WRITE_ZEROES))
1338 return __send_write_zeroes(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001339
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001340 ti = dm_table_find_target(ci->map, ci->sector);
1341 if (!dm_target_is_valid(ti))
1342 return -EIO;
1343
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001344 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001345
Mike Snitzerc80914e2016-03-02 12:33:03 -05001346 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1347 if (r < 0)
1348 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001350 ci->sector += len;
1351 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001357 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001359static void __split_and_process_bio(struct mapped_device *md,
1360 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001363 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001365 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001366 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001367 return;
1368 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001369
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001370 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 ci.io = alloc_io(md);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001373 ci.io->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 atomic_set(&ci.io->io_count, 1);
1375 ci.io->bio = bio;
1376 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001377 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001378 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001380 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001381
Jens Axboe1eff9d32016-08-05 15:35:16 -06001382 if (bio->bi_opf & REQ_PREFLUSH) {
Mike Snitzerb372d362010-09-08 18:07:01 +02001383 ci.bio = &ci.md->flush_bio;
1384 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001385 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001386 /* dec_pending submits any data associated with flush */
1387 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001388 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001389 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001390 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001391 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001395 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397/*-----------------------------------------------------------------
1398 * CRUD END
1399 *---------------------------------------------------------------*/
1400
1401/*
1402 * The request function that just remaps the bio built up by
1403 * dm_merge_bvec.
1404 */
Jens Axboedece1632015-11-05 10:41:16 -07001405static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Kevin Corry12f03a42006-02-01 03:04:52 -08001407 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001409 int srcu_idx;
1410 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001412 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Gu Zheng18c0b222014-11-24 11:05:26 +08001414 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001415
Tejun Heo6a8736d2010-09-08 18:07:00 +02001416 /* if we're suspended, we have to queue this io for later */
1417 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001418 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Jens Axboe1eff9d32016-08-05 15:35:16 -06001420 if (!(bio->bi_opf & REQ_RAHEAD))
Tejun Heo6a8736d2010-09-08 18:07:00 +02001421 queue_io(md, bio);
1422 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001423 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001424 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001427 __split_and_process_bio(md, map, bio);
1428 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001429 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432static int dm_any_congested(void *congested_data, int bdi_bits)
1433{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001434 int r = bdi_bits;
1435 struct mapped_device *md = congested_data;
1436 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001438 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05001439 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001440 /*
Mike Snitzere522c032016-02-02 22:35:06 -05001441 * With request-based DM we only need to check the
1442 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001443 */
Jan Karadc3b17c2017-02-02 15:56:50 +01001444 r = md->queue->backing_dev_info->wb.state & bdi_bits;
Mike Snitzere522c032016-02-02 22:35:06 -05001445 } else {
1446 map = dm_get_live_table_fast(md);
1447 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001448 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05001449 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001450 }
1451 }
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return r;
1454}
1455
1456/*-----------------------------------------------------------------
1457 * An IDR is used to keep track of allocated minor numbers.
1458 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001459static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001461 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001463 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466/*
1467 * See if the device with a specific minor # is free.
1468 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001469static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001471 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (minor >= (1 << MINORBITS))
1474 return -EINVAL;
1475
Tejun Heoc9d76be2013-02-27 17:04:26 -08001476 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001477 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Tejun Heoc9d76be2013-02-27 17:04:26 -08001479 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001481 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001482 idr_preload_end();
1483 if (r < 0)
1484 return r == -ENOSPC ? -EBUSY : r;
1485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001488static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001490 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Tejun Heoc9d76be2013-02-27 17:04:26 -08001492 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001493 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Tejun Heoc9d76be2013-02-27 17:04:26 -08001495 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001497 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001498 idr_preload_end();
1499 if (r < 0)
1500 return r;
1501 *minor = r;
1502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001505static const struct block_device_operations dm_blk_dops;
Dan Williamsf26c5712017-04-12 12:35:44 -07001506static const struct dax_operations dm_dax_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Mikulas Patocka53d59142009-04-02 19:55:37 +01001508static void dm_wq_work(struct work_struct *work);
1509
Mike Snitzer4cc96132016-05-12 16:28:10 -04001510void dm_init_md_queue(struct mapped_device *md)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001511{
1512 /*
1513 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001514 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001515 * The type is decided at the first table loading time.
1516 * To prevent problematic device stacking, clear the queue flag
1517 * for request stacking support until then.
1518 *
1519 * This queue is new, so no concurrency on the queue_flags.
1520 */
1521 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04001522
1523 /*
1524 * Initialize data that will only be used by a non-blk-mq DM queue
1525 * - must do so here (in alloc_dev callchain) before queue is used
1526 */
1527 md->queue->queuedata = md;
Jan Karadc3b17c2017-02-02 15:56:50 +01001528 md->queue->backing_dev_info->congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001529}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001530
Mike Snitzer4cc96132016-05-12 16:28:10 -04001531void dm_init_normal_md_queue(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001532{
Mike Snitzer17e149b2015-03-11 15:01:09 -04001533 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001534 dm_init_md_queue(md);
1535
1536 /*
1537 * Initialize aspects of queue that aren't relevant for blk-mq
1538 */
Jan Karadc3b17c2017-02-02 15:56:50 +01001539 md->queue->backing_dev_info->congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001540 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001541}
1542
Mike Snitzer0f209722015-04-28 11:50:29 -04001543static void cleanup_mapped_device(struct mapped_device *md)
1544{
Mike Snitzer0f209722015-04-28 11:50:29 -04001545 if (md->wq)
1546 destroy_workqueue(md->wq);
1547 if (md->kworker_task)
1548 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02001549 mempool_destroy(md->io_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04001550 if (md->bs)
1551 bioset_free(md->bs);
1552
Dan Williamsf26c5712017-04-12 12:35:44 -07001553 if (md->dax_dev) {
1554 kill_dax(md->dax_dev);
1555 put_dax(md->dax_dev);
1556 md->dax_dev = NULL;
1557 }
1558
Mike Snitzer0f209722015-04-28 11:50:29 -04001559 if (md->disk) {
1560 spin_lock(&_minor_lock);
1561 md->disk->private_data = NULL;
1562 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04001563 del_gendisk(md->disk);
1564 put_disk(md->disk);
1565 }
1566
1567 if (md->queue)
1568 blk_cleanup_queue(md->queue);
1569
Tahsin Erdogand09960b2016-10-10 05:35:19 -07001570 cleanup_srcu_struct(&md->io_barrier);
1571
Mike Snitzer0f209722015-04-28 11:50:29 -04001572 if (md->bdev) {
1573 bdput(md->bdev);
1574 md->bdev = NULL;
1575 }
Mike Snitzer4cc96132016-05-12 16:28:10 -04001576
1577 dm_mq_cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580/*
1581 * Allocate and initialise a blank device with a given minor.
1582 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001583static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Mike Snitzer115485e2016-02-22 12:16:21 -05001585 int r, numa_node_id = dm_get_numa_node();
Dan Williamsf26c5712017-04-12 12:35:44 -07001586 struct dax_device *dax_dev;
Mike Snitzer115485e2016-02-22 12:16:21 -05001587 struct mapped_device *md;
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001588 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Mike Snitzer115485e2016-02-22 12:16:21 -05001590 md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (!md) {
1592 DMWARN("unable to allocate device, out of memory.");
1593 return NULL;
1594 }
1595
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001596 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001597 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001600 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001601 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001602 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001603 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001605 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001607 r = init_srcu_struct(&md->io_barrier);
1608 if (r < 0)
1609 goto bad_io_barrier;
1610
Mike Snitzer115485e2016-02-22 12:16:21 -05001611 md->numa_node_id = numa_node_id;
Mike Snitzer4cc96132016-05-12 16:28:10 -04001612 md->use_blk_mq = dm_use_blk_mq_default();
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001613 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01001614 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001615 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001616 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001617 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001618 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001620 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001622 atomic_set(&md->uevent_seq, 0);
1623 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001624 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001625 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Mike Snitzer115485e2016-02-22 12:16:21 -05001627 md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04001629 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001631 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001632
Mike Snitzer115485e2016-02-22 12:16:21 -05001633 md->disk = alloc_disk_node(1, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04001635 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001637 atomic_set(&md->pending[0], 0);
1638 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001639 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001640 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001641 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05001642 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001643 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 md->disk->major = _major;
1646 md->disk->first_minor = minor;
1647 md->disk->fops = &dm_blk_dops;
1648 md->disk->queue = md->queue;
1649 md->disk->private_data = md;
1650 sprintf(md->disk->disk_name, "dm-%d", minor);
Dan Williamsf26c5712017-04-12 12:35:44 -07001651
1652 dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1653 if (!dax_dev)
1654 goto bad;
1655 md->dax_dev = dax_dev;
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001658 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Tejun Heo670368a2013-07-30 08:40:21 -04001660 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001661 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04001662 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00001663
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001664 md->bdev = bdget_disk(md->disk, 0);
1665 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04001666 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001667
Ming Lei3a83f462016-11-22 08:57:21 -07001668 bio_init(&md->flush_bio, NULL, 0);
Tejun Heo6a8736d2010-09-08 18:07:00 +02001669 md->flush_bio.bi_bdev = md->bdev;
Jan Karaff0361b2017-05-31 09:44:32 +02001670 md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
Tejun Heo6a8736d2010-09-08 18:07:00 +02001671
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001672 dm_stats_init(&md->stats);
1673
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001674 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001675 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001676 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001677 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001678
1679 BUG_ON(old_md != MINOR_ALLOCED);
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return md;
1682
Mike Snitzer0f209722015-04-28 11:50:29 -04001683bad:
1684 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001685bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001687bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001688 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001689bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 kfree(md);
1691 return NULL;
1692}
1693
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001694static void unlock_fs(struct mapped_device *md);
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static void free_dev(struct mapped_device *md)
1697{
Tejun Heof331c022008-09-03 09:01:48 +02001698 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001699
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001700 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001701
Mike Snitzer0f209722015-04-28 11:50:29 -04001702 cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001703
1704 free_table_devices(&md->table_devices);
1705 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04001706 free_minor(minor);
1707
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001708 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 kfree(md);
1710}
1711
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001712static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1713{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001714 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001715
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001716 if (md->bs) {
1717 /* The md already has necessary mempools. */
Toshi Kani545ed202016-06-22 17:54:53 -06001718 if (dm_table_bio_based(t)) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001719 /*
1720 * Reload bioset because front_pad may have changed
1721 * because a different table was loaded.
1722 */
1723 bioset_free(md->bs);
1724 md->bs = p->bs;
1725 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001726 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001727 /*
1728 * There's no need to reload with request-based dm
1729 * because the size of front_pad doesn't change.
1730 * Note for future: If you are to reload bioset,
1731 * prep-ed requests in the queue may refer
1732 * to bio from the old bioset, so you must walk
1733 * through the queue to unprep.
1734 */
1735 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001736 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001737
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001738 BUG_ON(!p || md->io_pool || md->bs);
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04001739
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001740 md->io_pool = p->io_pool;
1741 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001742 md->bs = p->bs;
1743 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001744
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001745out:
Mike Snitzer02233342015-03-10 23:49:26 -04001746 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001747 dm_table_free_md_mempools(t);
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750/*
1751 * Bind a table to the device.
1752 */
1753static void event_callback(void *context)
1754{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001755 unsigned long flags;
1756 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct mapped_device *md = (struct mapped_device *) context;
1758
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001759 spin_lock_irqsave(&md->uevent_lock, flags);
1760 list_splice_init(&md->uevent_list, &uevents);
1761 spin_unlock_irqrestore(&md->uevent_lock, flags);
1762
Tejun Heoed9e1982008-08-25 19:56:05 +09001763 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 atomic_inc(&md->event_nr);
Mikulas Patocka93e64422017-01-16 16:05:59 -05001766 atomic_inc(&dm_global_event_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 wake_up(&md->eventq);
Mikulas Patocka93e64422017-01-16 16:05:59 -05001768 wake_up(&dm_global_eventq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
Mike Snitzerc2176492011-01-13 19:53:46 +00001771/*
1772 * Protected by md->suspend_lock obtained by dm_swap_table().
1773 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001774static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Bart Van Assche1ea06542017-04-27 10:11:21 -07001776 lockdep_assert_held(&md->suspend_lock);
1777
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001778 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001780 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001783/*
1784 * Returns old map, which caller must destroy.
1785 */
1786static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1787 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001789 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02001790 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 sector_t size;
1792
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07001793 lockdep_assert_held(&md->suspend_lock);
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001796
1797 /*
1798 * Wipe any geometry if the size of the table changed.
1799 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001800 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001801 memset(&md->geometry, 0, sizeof(md->geometry));
1802
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001803 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001805 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001806
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001807 /*
1808 * The queue hasn't been stopped yet, if the old table type wasn't
1809 * for request-based during suspension. So stop it to prevent
1810 * I/O mapping before resume.
1811 * This must be done before setting the queue restrictions,
1812 * because request-based dm may be run just after the setting.
1813 */
Mike Snitzer16f12262016-01-31 17:22:27 -05001814 if (dm_table_request_based(t)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001815 dm_stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05001816 /*
1817 * Leverage the fact that request-based DM targets are
1818 * immutable singletons and establish md->immutable_target
1819 * - used to optimize both dm_request_fn and dm_mq_queue_rq
1820 */
1821 md->immutable_target = dm_table_get_immutable_target(t);
1822 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001823
1824 __bind_mempools(md, t);
1825
Eric Dumazeta12f5d42014-11-23 09:34:29 -08001826 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -05001827 rcu_assign_pointer(md->map, (void *)t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001828 md->immutable_target_type = dm_table_get_immutable_target_type(t);
1829
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001830 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01001831 if (old_map)
1832 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001833
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001834 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Alasdair G Kergona7940152009-12-10 23:52:23 +00001837/*
1838 * Returns unbound table for the caller to free.
1839 */
1840static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08001842 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00001845 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05301848 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001849 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00001850
1851 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
1854/*
1855 * Constructor for a new device.
1856 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001857int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 struct mapped_device *md;
1860
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001861 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (!md)
1863 return -ENXIO;
1864
Milan Broz784aae72009-01-06 03:05:12 +00001865 dm_sysfs_init(md);
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 *result = md;
1868 return 0;
1869}
1870
Mike Snitzera5664da2010-08-12 04:14:01 +01001871/*
1872 * Functions to manage md->type.
1873 * All are required to hold md->type_lock.
1874 */
1875void dm_lock_md_type(struct mapped_device *md)
1876{
1877 mutex_lock(&md->type_lock);
1878}
1879
1880void dm_unlock_md_type(struct mapped_device *md)
1881{
1882 mutex_unlock(&md->type_lock);
1883}
1884
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001885void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
Mike Snitzera5664da2010-08-12 04:14:01 +01001886{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04001887 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01001888 md->type = type;
1889}
1890
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001891enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
Mike Snitzera5664da2010-08-12 04:14:01 +01001892{
1893 return md->type;
1894}
1895
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001896struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
1897{
1898 return md->immutable_target_type;
1899}
1900
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001901/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04001902 * The queue_limits are only valid as long as you have a reference
1903 * count on 'md'.
1904 */
1905struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
1906{
1907 BUG_ON(!atomic_read(&md->holders));
1908 return &md->queue->limits;
1909}
1910EXPORT_SYMBOL_GPL(dm_get_queue_limits);
1911
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001912/*
1913 * Setup the DM device's queue based on md's type
1914 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001915int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001916{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001917 int r;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001918 enum dm_queue_mode type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001919
Toshi Kani545ed202016-06-22 17:54:53 -06001920 switch (type) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001921 case DM_TYPE_REQUEST_BASED:
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001922 r = dm_old_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001923 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001924 DMERR("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001925 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05001926 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001927 break;
1928 case DM_TYPE_MQ_REQUEST_BASED:
Mike Snitzere83068a2016-05-24 21:16:51 -04001929 r = dm_mq_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001930 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001931 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001932 return r;
1933 }
1934 break;
1935 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06001936 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05001937 dm_init_normal_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05001938 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04001939 /*
1940 * DM handles splitting bios as needed. Free the bio_split bioset
1941 * since it won't be used (saves 1 process per bio-based DM device).
1942 */
1943 bioset_free(md->queue->bio_split);
1944 md->queue->bio_split = NULL;
Toshi Kani545ed202016-06-22 17:54:53 -06001945
1946 if (type == DM_TYPE_DAX_BIO_BASED)
1947 queue_flag_set_unlocked(QUEUE_FLAG_DAX, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001948 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001949 case DM_TYPE_NONE:
1950 WARN_ON_ONCE(true);
1951 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001952 }
1953
1954 return 0;
1955}
1956
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05001957struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 unsigned minor = MINOR(dev);
1961
1962 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1963 return NULL;
1964
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001965 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05001968 if (md) {
1969 if ((md == MINOR_ALLOCED ||
1970 (MINOR(disk_devt(dm_disk(md))) != minor) ||
1971 dm_deleting_md(md) ||
1972 test_bit(DMF_FREEING, &md->flags))) {
1973 md = NULL;
1974 goto out;
1975 }
1976 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001979out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001980 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
David Teigland637842c2006-01-06 00:20:00 -08001982 return md;
1983}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00001984EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08001985
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001986void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001987{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001988 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991void dm_set_mdptr(struct mapped_device *md, void *ptr)
1992{
1993 md->interface_ptr = ptr;
1994}
1995
1996void dm_get(struct mapped_device *md)
1997{
1998 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001999 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002002int dm_hold(struct mapped_device *md)
2003{
2004 spin_lock(&_minor_lock);
2005 if (test_bit(DMF_FREEING, &md->flags)) {
2006 spin_unlock(&_minor_lock);
2007 return -EBUSY;
2008 }
2009 dm_get(md);
2010 spin_unlock(&_minor_lock);
2011 return 0;
2012}
2013EXPORT_SYMBOL_GPL(dm_hold);
2014
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002015const char *dm_device_name(struct mapped_device *md)
2016{
2017 return md->name;
2018}
2019EXPORT_SYMBOL_GPL(dm_device_name);
2020
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002021static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Bart Van Assche3b785fb2016-08-31 15:17:49 -07002023 struct request_queue *q = dm_get_md_queue(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002024 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002025 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002027 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002028
Mike Snitzer63a4f062015-03-23 17:01:43 -04002029 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002030 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2031 set_bit(DMF_FREEING, &md->flags);
2032 spin_unlock(&_minor_lock);
2033
Bart Van Assche2e91c362016-11-18 14:26:47 -08002034 blk_set_queue_dying(q);
Bart Van Assche3b785fb2016-08-31 15:17:49 -07002035
Mike Snitzer02233342015-03-10 23:49:26 -04002036 if (dm_request_based(md) && md->kworker_task)
Petr Mladek39891442016-10-11 13:55:20 -07002037 kthread_flush_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002038
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002039 /*
2040 * Take suspend_lock so that presuspend and postsuspend methods
2041 * do not race with internal suspend.
2042 */
2043 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002044 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002045 if (!dm_suspended_md(md)) {
2046 dm_table_presuspend_targets(map);
2047 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002049 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2050 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002051 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002052
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002053 /*
2054 * Rare, but there may be I/O requests still going to complete,
2055 * for example. Wait for all references to disappear.
2056 * No one should increment the reference count of the mapped_device,
2057 * after the mapped_device state becomes DMF_FREEING.
2058 */
2059 if (wait)
2060 while (atomic_read(&md->holders))
2061 msleep(1);
2062 else if (atomic_read(&md->holders))
2063 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2064 dm_device_name(md), atomic_read(&md->holders));
2065
2066 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002067 dm_table_destroy(__unbind(md));
2068 free_dev(md);
2069}
2070
2071void dm_destroy(struct mapped_device *md)
2072{
2073 __dm_destroy(md, true);
2074}
2075
2076void dm_destroy_immediate(struct mapped_device *md)
2077{
2078 __dm_destroy(md, false);
2079}
2080
2081void dm_put(struct mapped_device *md)
2082{
2083 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
Edward Goggin79eb8852007-05-09 02:32:56 -07002085EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002087static int dm_wait_for_completion(struct mapped_device *md, long task_state)
Milan Broz46125c12008-02-08 02:10:30 +00002088{
2089 int r = 0;
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002090 DEFINE_WAIT(wait);
Milan Broz46125c12008-02-08 02:10:30 +00002091
2092 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002093 prepare_to_wait(&md->wait, &wait, task_state);
Milan Broz46125c12008-02-08 02:10:30 +00002094
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002095 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002096 break;
2097
Bart Van Asschee3fabdf2016-08-31 15:16:22 -07002098 if (signal_pending_state(task_state, current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002099 r = -EINTR;
2100 break;
2101 }
2102
2103 io_schedule();
2104 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002105 finish_wait(&md->wait, &wait);
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002106
Milan Broz46125c12008-02-08 02:10:30 +00002107 return r;
2108}
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110/*
2111 * Process the deferred bios
2112 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002113static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Mikulas Patockaef208582009-04-02 19:55:38 +01002115 struct mapped_device *md = container_of(work, struct mapped_device,
2116 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002117 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002118 int srcu_idx;
2119 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002121 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002122
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002123 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002124 spin_lock_irq(&md->deferred_lock);
2125 c = bio_list_pop(&md->deferred);
2126 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002127
Tejun Heo6a8736d2010-09-08 18:07:00 +02002128 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002129 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002130
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002131 if (dm_request_based(md))
2132 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002133 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002134 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002135 }
Milan Broz73d410c2008-02-08 02:10:25 +00002136
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002137 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002140static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002141{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002142 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002143 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002144 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002145}
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002148 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002150struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Mike Christie87eb5b22013-03-01 22:45:48 +00002152 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002153 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002154 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Daniel Walkere61290a2008-02-08 02:10:08 +00002156 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002159 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002160 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Mike Snitzer3ae70652012-09-26 23:45:45 +01002162 /*
2163 * If the new table has no data devices, retain the existing limits.
2164 * This helps multipath with queue_if_no_path if all paths disappear,
2165 * then new I/O is queued based on these limits, and then some paths
2166 * reappear.
2167 */
2168 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002169 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002170 if (live_map)
2171 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002172 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002173 }
2174
Mike Christie87eb5b22013-03-01 22:45:48 +00002175 if (!live_map) {
2176 r = dm_calculate_queue_limits(table, &limits);
2177 if (r) {
2178 map = ERR_PTR(r);
2179 goto out;
2180 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002181 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002182
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002183 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002185out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002186 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002187 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
2190/*
2191 * Functions to lock and unlock any filesystem running on the
2192 * device.
2193 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002194static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002196 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002199
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002200 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002201 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002202 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002203 md->frozen_sb = NULL;
2204 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002205 }
2206
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002207 set_bit(DMF_FROZEN, &md->flags);
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return 0;
2210}
2211
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002212static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002214 if (!test_bit(DMF_FROZEN, &md->flags))
2215 return;
2216
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002217 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002219 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222/*
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002223 * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2224 * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2225 * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2226 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04002227 * If __dm_suspend returns 0, the device is completely quiescent
2228 * now. There is no request-processing activity. All new requests
2229 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002230 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04002231static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002232 unsigned suspend_flags, long task_state,
Mike Snitzereaf9a732016-08-02 13:07:20 -04002233 int dmf_suspended_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002235 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2236 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2237 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07002239 lockdep_assert_held(&md->suspend_lock);
2240
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002241 /*
2242 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2243 * This flag is cleared before dm_suspend returns.
2244 */
2245 if (noflush)
2246 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Bart Van Assche86331f32017-04-27 10:11:26 -07002247 else
2248 pr_debug("%s: suspending with flush\n", dm_device_name(md));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002249
Mike Snitzerd67ee212014-10-28 20:13:31 -04002250 /*
2251 * This gets reverted if there's an error later and the targets
2252 * provide the .presuspend_undo hook.
2253 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002254 dm_table_presuspend_targets(map);
2255
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002256 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002257 * Flush I/O to the device.
2258 * Any I/O submitted after lock_fs() may not be flushed.
2259 * noflush takes precedence over do_lockfs.
2260 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002261 */
2262 if (!noflush && do_lockfs) {
2263 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002264 if (r) {
2265 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002266 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04002267 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002271 * Here we must make sure that no processes are submitting requests
2272 * to target drivers i.e. no one may be executing
2273 * __split_and_process_bio. This is called from dm_request and
2274 * dm_wq_work.
2275 *
2276 * To get all processes out of __split_and_process_bio in dm_request,
2277 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002278 * __split_and_process_bio from dm_request and quiesce the thread
2279 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2280 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002282 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002283 if (map)
2284 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002286 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002287 * Stop md->queue before flushing md->wq in case request-based
2288 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002289 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002290 if (dm_request_based(md)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002291 dm_stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04002292 if (md->kworker_task)
Petr Mladek39891442016-10-11 13:55:20 -07002293 kthread_flush_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002294 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002295
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002296 flush_workqueue(md->wq);
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002299 * At this point no more requests are entering target request routines.
2300 * We call dm_wait_for_completion to wait for all existing requests
2301 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 */
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002303 r = dm_wait_for_completion(md, task_state);
Mike Snitzereaf9a732016-08-02 13:07:20 -04002304 if (!r)
2305 set_bit(dmf_suspended_flag, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Milan Broz6d6f10d2008-02-08 02:10:22 +00002307 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002308 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002309 if (map)
2310 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002313 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002314 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002315
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002316 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002317 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002318
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002319 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002320 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002321 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002322 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002323
Mike Snitzerffcc3932014-10-28 18:34:52 -04002324 return r;
2325}
2326
2327/*
2328 * We need to be able to change a mapping table under a mounted
2329 * filesystem. For example we might want to move some data in
2330 * the background. Before the table can be swapped with
2331 * dm_bind_table, dm_suspend must be called to flush any in
2332 * flight bios and ensure that any further io gets deferred.
2333 */
2334/*
2335 * Suspend mechanism in request-based dm.
2336 *
2337 * 1. Flush all I/Os by lock_fs() if needed.
2338 * 2. Stop dispatching any I/O by stopping the request_queue.
2339 * 3. Wait for all in-flight I/Os to be completed or requeued.
2340 *
2341 * To abort suspend, start the request_queue.
2342 */
2343int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2344{
2345 struct dm_table *map = NULL;
2346 int r = 0;
2347
2348retry:
2349 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2350
2351 if (dm_suspended_md(md)) {
2352 r = -EINVAL;
2353 goto out_unlock;
2354 }
2355
2356 if (dm_suspended_internally_md(md)) {
2357 /* already internally suspended, wait for internal resume */
2358 mutex_unlock(&md->suspend_lock);
2359 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2360 if (r)
2361 return r;
2362 goto retry;
2363 }
2364
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002365 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002366
Mike Snitzereaf9a732016-08-02 13:07:20 -04002367 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002368 if (r)
2369 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002370
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002371 dm_table_postsuspend_targets(map);
2372
Alasdair G Kergond2874832006-11-08 17:44:43 -08002373out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002374 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002375 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Mike Snitzerffcc3932014-10-28 18:34:52 -04002378static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002380 if (map) {
2381 int r = dm_table_resume_targets(map);
2382 if (r)
2383 return r;
2384 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002385
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002386 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002387
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002388 /*
2389 * Flushing deferred I/Os must be done after targets are resumed
2390 * so that mapping of targets can work correctly.
2391 * Request-based dm is queueing the deferred I/Os in its request_queue.
2392 */
2393 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002394 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002395
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002396 unlock_fs(md);
2397
Mike Snitzerffcc3932014-10-28 18:34:52 -04002398 return 0;
2399}
2400
2401int dm_resume(struct mapped_device *md)
2402{
Minfei Huang8dc23652016-09-06 16:00:29 +08002403 int r;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002404 struct dm_table *map = NULL;
2405
2406retry:
Minfei Huang8dc23652016-09-06 16:00:29 +08002407 r = -EINVAL;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002408 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2409
2410 if (!dm_suspended_md(md))
2411 goto out;
2412
2413 if (dm_suspended_internally_md(md)) {
2414 /* already internally suspended, wait for internal resume */
2415 mutex_unlock(&md->suspend_lock);
2416 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2417 if (r)
2418 return r;
2419 goto retry;
2420 }
2421
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002422 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002423 if (!map || !dm_table_get_size(map))
2424 goto out;
2425
2426 r = __dm_resume(md, map);
2427 if (r)
2428 goto out;
2429
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002430 clear_bit(DMF_SUSPENDED, &md->flags);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002431out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002432 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002433
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002434 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002437/*
2438 * Internal suspend/resume works like userspace-driven suspend. It waits
2439 * until all bios finish and prevents issuing new bios to the target drivers.
2440 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002441 */
2442
Mike Snitzerffcc3932014-10-28 18:34:52 -04002443static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2444{
2445 struct dm_table *map = NULL;
2446
Bart Van Assche1ea06542017-04-27 10:11:21 -07002447 lockdep_assert_held(&md->suspend_lock);
2448
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002449 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002450 return; /* nested internal suspend */
2451
2452 if (dm_suspended_md(md)) {
2453 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2454 return; /* nest suspend */
2455 }
2456
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002457 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002458
2459 /*
2460 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2461 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
2462 * would require changing .presuspend to return an error -- avoid this
2463 * until there is a need for more elaborate variants of internal suspend.
2464 */
Mike Snitzereaf9a732016-08-02 13:07:20 -04002465 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2466 DMF_SUSPENDED_INTERNALLY);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002467
2468 dm_table_postsuspend_targets(map);
2469}
2470
2471static void __dm_internal_resume(struct mapped_device *md)
2472{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002473 BUG_ON(!md->internal_suspend_count);
2474
2475 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002476 return; /* resume from nested internal suspend */
2477
2478 if (dm_suspended_md(md))
2479 goto done; /* resume from nested suspend */
2480
2481 /*
2482 * NOTE: existing callers don't need to call dm_table_resume_targets
2483 * (which may fail -- so best to avoid it for now by passing NULL map)
2484 */
2485 (void) __dm_resume(md, NULL);
2486
2487done:
2488 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2489 smp_mb__after_atomic();
2490 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2491}
2492
2493void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002494{
2495 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002496 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2497 mutex_unlock(&md->suspend_lock);
2498}
2499EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2500
2501void dm_internal_resume(struct mapped_device *md)
2502{
2503 mutex_lock(&md->suspend_lock);
2504 __dm_internal_resume(md);
2505 mutex_unlock(&md->suspend_lock);
2506}
2507EXPORT_SYMBOL_GPL(dm_internal_resume);
2508
2509/*
2510 * Fast variants of internal suspend/resume hold md->suspend_lock,
2511 * which prevents interaction with userspace-driven suspend.
2512 */
2513
2514void dm_internal_suspend_fast(struct mapped_device *md)
2515{
2516 mutex_lock(&md->suspend_lock);
2517 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002518 return;
2519
2520 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2521 synchronize_srcu(&md->io_barrier);
2522 flush_workqueue(md->wq);
2523 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2524}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002525EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002526
Mike Snitzerffcc3932014-10-28 18:34:52 -04002527void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002528{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002529 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002530 goto done;
2531
2532 dm_queue_flush(md);
2533
2534done:
2535 mutex_unlock(&md->suspend_lock);
2536}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002537EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539/*-----------------------------------------------------------------
2540 * Event notification.
2541 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002542int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002543 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002544{
Milan Broz60935eb2009-06-22 10:12:30 +01002545 char udev_cookie[DM_COOKIE_LENGTH];
2546 char *envp[] = { udev_cookie, NULL };
2547
2548 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002549 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002550 else {
2551 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2552 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002553 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2554 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002555 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002556}
2557
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002558uint32_t dm_next_uevent_seq(struct mapped_device *md)
2559{
2560 return atomic_add_return(1, &md->uevent_seq);
2561}
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563uint32_t dm_get_event_nr(struct mapped_device *md)
2564{
2565 return atomic_read(&md->event_nr);
2566}
2567
2568int dm_wait_event(struct mapped_device *md, int event_nr)
2569{
2570 return wait_event_interruptible(md->eventq,
2571 (event_nr != atomic_read(&md->event_nr)));
2572}
2573
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002574void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2575{
2576 unsigned long flags;
2577
2578 spin_lock_irqsave(&md->uevent_lock, flags);
2579 list_add(elist, &md->uevent_list);
2580 spin_unlock_irqrestore(&md->uevent_lock, flags);
2581}
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583/*
2584 * The gendisk is only valid as long as you have a reference
2585 * count on 'md'.
2586 */
2587struct gendisk *dm_disk(struct mapped_device *md)
2588{
2589 return md->disk;
2590}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00002591EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Milan Broz784aae72009-01-06 03:05:12 +00002593struct kobject *dm_kobject(struct mapped_device *md)
2594{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002595 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00002596}
2597
Milan Broz784aae72009-01-06 03:05:12 +00002598struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2599{
2600 struct mapped_device *md;
2601
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002602 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00002603
Milan Broz4d89b7b2009-06-22 10:12:11 +01002604 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002605 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002606 return NULL;
2607
Milan Broz784aae72009-01-06 03:05:12 +00002608 dm_get(md);
2609 return md;
2610}
2611
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002612int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
2614 return test_bit(DMF_SUSPENDED, &md->flags);
2615}
2616
Mike Snitzerffcc3932014-10-28 18:34:52 -04002617int dm_suspended_internally_md(struct mapped_device *md)
2618{
2619 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2620}
2621
Mikulas Patocka2c140a22013-11-01 18:27:41 -04002622int dm_test_deferred_remove_flag(struct mapped_device *md)
2623{
2624 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2625}
2626
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002627int dm_suspended(struct dm_target *ti)
2628{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002629 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002630}
2631EXPORT_SYMBOL_GPL(dm_suspended);
2632
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002633int dm_noflush_suspending(struct dm_target *ti)
2634{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002635 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002636}
2637EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2638
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002639struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
Mike Snitzer30187e12016-01-31 13:28:26 -05002640 unsigned integrity, unsigned per_io_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002641{
Mike Snitzer115485e2016-02-22 12:16:21 -05002642 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
Mike Snitzer78d8e582015-06-26 10:01:13 -04002643 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002644 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002645
2646 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002647 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002648
Mike Snitzer78d8e582015-06-26 10:01:13 -04002649 switch (type) {
2650 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002651 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzer78d8e582015-06-26 10:01:13 -04002652 pool_size = dm_get_reserved_bio_based_ios();
Mike Snitzer30187e12016-01-31 13:28:26 -05002653 front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
Christoph Hellwigeb8db832017-01-22 18:32:46 +01002654
2655 pools->io_pool = mempool_create_slab_pool(pool_size, _io_cache);
2656 if (!pools->io_pool)
2657 goto out;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002658 break;
2659 case DM_TYPE_REQUEST_BASED:
Mike Snitzer78d8e582015-06-26 10:01:13 -04002660 case DM_TYPE_MQ_REQUEST_BASED:
Christoph Hellwigeb8db832017-01-22 18:32:46 +01002661 pool_size = dm_get_reserved_rq_based_ios();
Mike Snitzer78d8e582015-06-26 10:01:13 -04002662 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002663 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04002664 break;
2665 default:
2666 BUG();
2667 }
2668
NeilBrown47e0fb42017-06-18 14:38:57 +10002669 pools->bs = bioset_create(pool_size, front_pad, BIOSET_NEED_RESCUER);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002670 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002671 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002672
Martin K. Petersena91a2782011-03-17 11:11:05 +01002673 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002674 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002675
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002676 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002677
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002678out:
2679 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002680
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002681 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002682}
2683
2684void dm_free_md_mempools(struct dm_md_mempools *pools)
2685{
2686 if (!pools)
2687 return;
2688
Julia Lawall6f659852015-09-13 14:15:05 +02002689 mempool_destroy(pools->io_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002690
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002691 if (pools->bs)
2692 bioset_free(pools->bs);
2693
2694 kfree(pools);
2695}
2696
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002697struct dm_pr {
2698 u64 old_key;
2699 u64 new_key;
2700 u32 flags;
2701 bool fail_early;
2702};
2703
2704static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
2705 void *data)
2706{
2707 struct mapped_device *md = bdev->bd_disk->private_data;
2708 struct dm_table *table;
2709 struct dm_target *ti;
2710 int ret = -ENOTTY, srcu_idx;
2711
2712 table = dm_get_live_table(md, &srcu_idx);
2713 if (!table || !dm_table_get_size(table))
2714 goto out;
2715
2716 /* We only support devices that have a single target */
2717 if (dm_table_get_num_targets(table) != 1)
2718 goto out;
2719 ti = dm_table_get_target(table, 0);
2720
2721 ret = -EINVAL;
2722 if (!ti->type->iterate_devices)
2723 goto out;
2724
2725 ret = ti->type->iterate_devices(ti, fn, data);
2726out:
2727 dm_put_live_table(md, srcu_idx);
2728 return ret;
2729}
2730
2731/*
2732 * For register / unregister we need to manually call out to every path.
2733 */
2734static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
2735 sector_t start, sector_t len, void *data)
2736{
2737 struct dm_pr *pr = data;
2738 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
2739
2740 if (!ops || !ops->pr_register)
2741 return -EOPNOTSUPP;
2742 return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
2743}
2744
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002745static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05002746 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002747{
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002748 struct dm_pr pr = {
2749 .old_key = old_key,
2750 .new_key = new_key,
2751 .flags = flags,
2752 .fail_early = true,
2753 };
2754 int ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002755
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002756 ret = dm_call_pr(bdev, __dm_pr_register, &pr);
2757 if (ret && new_key) {
2758 /* unregister all paths if we failed to register any path */
2759 pr.old_key = new_key;
2760 pr.new_key = 0;
2761 pr.flags = 0;
2762 pr.fail_early = false;
2763 dm_call_pr(bdev, __dm_pr_register, &pr);
2764 }
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002765
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002766 return ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002767}
2768
2769static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05002770 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002771{
2772 struct mapped_device *md = bdev->bd_disk->private_data;
2773 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002774 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002775 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002776
Mike Snitzer956a4022016-02-18 16:13:51 -05002777 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002778 if (r < 0)
2779 return r;
2780
2781 ops = bdev->bd_disk->fops->pr_ops;
2782 if (ops && ops->pr_reserve)
2783 r = ops->pr_reserve(bdev, key, type, flags);
2784 else
2785 r = -EOPNOTSUPP;
2786
Mike Snitzer956a4022016-02-18 16:13:51 -05002787 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002788 return r;
2789}
2790
2791static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
2792{
2793 struct mapped_device *md = bdev->bd_disk->private_data;
2794 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002795 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002796 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002797
Mike Snitzer956a4022016-02-18 16:13:51 -05002798 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002799 if (r < 0)
2800 return r;
2801
2802 ops = bdev->bd_disk->fops->pr_ops;
2803 if (ops && ops->pr_release)
2804 r = ops->pr_release(bdev, key, type);
2805 else
2806 r = -EOPNOTSUPP;
2807
Mike Snitzer956a4022016-02-18 16:13:51 -05002808 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002809 return r;
2810}
2811
2812static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05002813 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002814{
2815 struct mapped_device *md = bdev->bd_disk->private_data;
2816 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002817 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002818 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002819
Mike Snitzer956a4022016-02-18 16:13:51 -05002820 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002821 if (r < 0)
2822 return r;
2823
2824 ops = bdev->bd_disk->fops->pr_ops;
2825 if (ops && ops->pr_preempt)
2826 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
2827 else
2828 r = -EOPNOTSUPP;
2829
Mike Snitzer956a4022016-02-18 16:13:51 -05002830 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002831 return r;
2832}
2833
2834static int dm_pr_clear(struct block_device *bdev, u64 key)
2835{
2836 struct mapped_device *md = bdev->bd_disk->private_data;
2837 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002838 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002839 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002840
Mike Snitzer956a4022016-02-18 16:13:51 -05002841 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002842 if (r < 0)
2843 return r;
2844
2845 ops = bdev->bd_disk->fops->pr_ops;
2846 if (ops && ops->pr_clear)
2847 r = ops->pr_clear(bdev, key);
2848 else
2849 r = -EOPNOTSUPP;
2850
Mike Snitzer956a4022016-02-18 16:13:51 -05002851 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002852 return r;
2853}
2854
2855static const struct pr_ops dm_pr_ops = {
2856 .pr_register = dm_pr_register,
2857 .pr_reserve = dm_pr_reserve,
2858 .pr_release = dm_pr_release,
2859 .pr_preempt = dm_pr_preempt,
2860 .pr_clear = dm_pr_clear,
2861};
2862
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002863static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 .open = dm_blk_open,
2865 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002866 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002867 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002868 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 .owner = THIS_MODULE
2870};
2871
Dan Williamsf26c5712017-04-12 12:35:44 -07002872static const struct dax_operations dm_dax_ops = {
2873 .direct_access = dm_dax_direct_access,
2874};
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876/*
2877 * module hooks
2878 */
2879module_init(dm_init);
2880module_exit(dm_exit);
2881
2882module_param(major, uint, 0);
2883MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04002884
Mike Snitzere8603132013-09-12 18:06:12 -04002885module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
2886MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
2887
Mike Snitzer115485e2016-02-22 12:16:21 -05002888module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
2889MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891MODULE_DESCRIPTION(DM_NAME " driver");
2892MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2893MODULE_LICENSE("GPL");