blob: aac716143eb18d19bff9c94511cb2f2fc0b3bbdc [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/blkpg.h>
16#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080020#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010021#include <linux/delay.h>
Mike Snitzerffcc3932014-10-28 18:34:52 -040022#include <linux/wait.h>
Christoph Hellwig71cdb692015-10-15 14:10:51 +020023#include <linux/pr.h>
Li Zefan55782132009-06-09 13:43:05 +080024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Namhyung Kim71a16732011-10-31 20:18:54 +000027#ifdef CONFIG_PRINTK
28/*
29 * ratelimit state to be used in DMXXX_LIMIT().
30 */
31DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
32 DEFAULT_RATELIMIT_INTERVAL,
33 DEFAULT_RATELIMIT_BURST);
34EXPORT_SYMBOL(dm_ratelimit_state);
35#endif
36
Milan Broz60935eb2009-06-22 10:12:30 +010037/*
38 * Cookies are numeric values sent with CHANGE and REMOVE
39 * uevents while resuming, removing or renaming the device.
40 */
41#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42#define DM_COOKIE_LENGTH 24
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static const char *_name = DM_NAME;
45
46static unsigned int major = 0;
47static unsigned int _major = 0;
48
Alasdair G Kergond15b7742011-08-02 12:32:01 +010049static DEFINE_IDR(_minor_idr);
50
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070051static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040052
53static void do_deferred_remove(struct work_struct *w);
54
55static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
56
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040057static struct workqueue_struct *deferred_remove_workqueue;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * One of these is allocated per bio.
61 */
62struct dm_io {
63 struct mapped_device *md;
64 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010066 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080067 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010068 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040069 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
AnilKumar Chimata1d078802017-04-12 15:58:33 -070072union map_info *dm_get_rq_mapinfo(struct request *rq)
73{
74 if (rq && rq->end_io_data)
75 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
76 return NULL;
77}
78
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070079#define MINOR_ALLOCED ((void *)-1)
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Bits for the md->flags field.
83 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +010084#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080086#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -070087#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070088#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080089#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -070090#define DMF_DEFERRED_REMOVE 6
91#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Mike Snitzer115485e2016-02-22 12:16:21 -050093#define DM_NUMA_NODE NUMA_NO_NODE
Mike Snitzer115485e2016-02-22 12:16:21 -050094static int dm_numa_node = DM_NUMA_NODE;
Mike Snitzerfaad87d2016-01-28 16:52:56 -050095
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +010096/*
97 * For mempools pre-allocation at the table loading time.
98 */
99struct dm_md_mempools {
100 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500101 mempool_t *rq_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100102 struct bio_set *bs;
103};
104
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500105struct table_device {
106 struct list_head list;
107 atomic_t count;
108 struct dm_dev dm_dev;
109};
110
Christoph Lametere18b8902006-12-06 20:33:20 -0800111static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000112static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500113static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700114
Mike Snitzerf4790822013-09-12 18:06:12 -0400115/*
Mike Snitzere8603132013-09-12 18:06:12 -0400116 * Bio-based DM's mempools' reserved IOs set by the user.
117 */
Mike Snitzer4cc96132016-05-12 16:28:10 -0400118#define RESERVED_BIO_BASED_IOS 16
Mike Snitzere8603132013-09-12 18:06:12 -0400119static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
120
Mike Snitzer115485e2016-02-22 12:16:21 -0500121static int __dm_get_module_param_int(int *module_param, int min, int max)
122{
123 int param = ACCESS_ONCE(*module_param);
124 int modified_param = 0;
125 bool modified = true;
126
127 if (param < min)
128 modified_param = min;
129 else if (param > max)
130 modified_param = max;
131 else
132 modified = false;
133
134 if (modified) {
135 (void)cmpxchg(module_param, param, modified_param);
136 param = modified_param;
137 }
138
139 return param;
140}
141
Mike Snitzer4cc96132016-05-12 16:28:10 -0400142unsigned __dm_get_module_param(unsigned *module_param,
143 unsigned def, unsigned max)
Mike Snitzerf4790822013-09-12 18:06:12 -0400144{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500145 unsigned param = ACCESS_ONCE(*module_param);
146 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400147
Mike Snitzer09c2d532015-02-27 22:25:26 -0500148 if (!param)
149 modified_param = def;
150 else if (param > max)
151 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400152
Mike Snitzer09c2d532015-02-27 22:25:26 -0500153 if (modified_param) {
154 (void)cmpxchg(module_param, param, modified_param);
155 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400156 }
157
Mike Snitzer09c2d532015-02-27 22:25:26 -0500158 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400159}
160
Mike Snitzere8603132013-09-12 18:06:12 -0400161unsigned dm_get_reserved_bio_based_ios(void)
162{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500163 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzer4cc96132016-05-12 16:28:10 -0400164 RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
Mike Snitzere8603132013-09-12 18:06:12 -0400165}
166EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
167
Mike Snitzer115485e2016-02-22 12:16:21 -0500168static unsigned dm_get_numa_node(void)
169{
170 return __dm_get_module_param_int(&dm_numa_node,
171 DM_NUMA_NODE, num_online_nodes() - 1);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int __init local_init(void)
175{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100176 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100179 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100181 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000183 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
184 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100185 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000186
Mike Snitzereca7ee62016-02-20 13:45:38 -0500187 _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500188 __alignof__(struct request), 0, NULL);
189 if (!_rq_cache)
190 goto out_free_rq_tio_cache;
191
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100192 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100193 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500194 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100195
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400196 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
197 if (!deferred_remove_workqueue) {
198 r = -ENOMEM;
199 goto out_uevent_exit;
200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 _major = major;
203 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100204 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400205 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 if (!_major)
208 _major = r;
209
210 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100211
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400212out_free_workqueue:
213 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100214out_uevent_exit:
215 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500216out_free_rq_cache:
217 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000218out_free_rq_tio_cache:
219 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100220out_free_io_cache:
221 kmem_cache_destroy(_io_cache);
222
223 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226static void local_exit(void)
227{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400228 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400229 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400230
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500231 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000232 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700234 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100235 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 _major = 0;
238
239 DMINFO("cleaned up");
240}
241
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000242static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 local_init,
244 dm_target_init,
245 dm_linear_init,
246 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000247 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100248 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400250 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000253static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 local_exit,
255 dm_target_exit,
256 dm_linear_exit,
257 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000258 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100259 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400261 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
264static int __init dm_init(void)
265{
266 const int count = ARRAY_SIZE(_inits);
267
268 int r, i;
269
270 for (i = 0; i < count; i++) {
271 r = _inits[i]();
272 if (r)
273 goto bad;
274 }
275
276 return 0;
277
278 bad:
279 while (i--)
280 _exits[i]();
281
282 return r;
283}
284
285static void __exit dm_exit(void)
286{
287 int i = ARRAY_SIZE(_exits);
288
289 while (i--)
290 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100291
292 /*
293 * Should be empty by this point.
294 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100295 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/*
299 * Block device functions
300 */
Mike Anderson432a2122009-12-10 23:52:20 +0000301int dm_deleting_md(struct mapped_device *md)
302{
303 return test_bit(DMF_DELETING, &md->flags);
304}
305
Al Virofe5f9f22008-03-02 10:29:31 -0500306static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct mapped_device *md;
309
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700310 spin_lock(&_minor_lock);
311
Al Virofe5f9f22008-03-02 10:29:31 -0500312 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700313 if (!md)
314 goto out;
315
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700316 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000317 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700318 md = NULL;
319 goto out;
320 }
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700323 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700324out:
325 spin_unlock(&_minor_lock);
326
327 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Al Virodb2a1442013-05-05 21:52:57 -0400330static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400332 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200333
Milan Broz4a1aeb92011-01-13 19:59:48 +0000334 spin_lock(&_minor_lock);
335
Mike Snitzer63a4f062015-03-23 17:01:43 -0400336 md = disk->private_data;
337 if (WARN_ON(!md))
338 goto out;
339
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400340 if (atomic_dec_and_test(&md->open_count) &&
341 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400342 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400345out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000346 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700349int dm_open_count(struct mapped_device *md)
350{
351 return atomic_read(&md->open_count);
352}
353
354/*
355 * Guarantees nothing is using the device before it's deleted.
356 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400357int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700358{
359 int r = 0;
360
361 spin_lock(&_minor_lock);
362
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400363 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700364 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400365 if (mark_deferred)
366 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
367 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
368 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700369 else
370 set_bit(DMF_DELETING, &md->flags);
371
372 spin_unlock(&_minor_lock);
373
374 return r;
375}
376
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400377int dm_cancel_deferred_remove(struct mapped_device *md)
378{
379 int r = 0;
380
381 spin_lock(&_minor_lock);
382
383 if (test_bit(DMF_DELETING, &md->flags))
384 r = -EBUSY;
385 else
386 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
387
388 spin_unlock(&_minor_lock);
389
390 return r;
391}
392
393static void do_deferred_remove(struct work_struct *w)
394{
395 dm_deferred_remove();
396}
397
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400398sector_t dm_get_size(struct mapped_device *md)
399{
400 return get_capacity(md->disk);
401}
402
Mike Snitzer9974fa22014-02-28 15:33:43 +0100403struct request_queue *dm_get_md_queue(struct mapped_device *md)
404{
405 return md->queue;
406}
407
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400408struct dm_stats *dm_get_stats(struct mapped_device *md)
409{
410 return &md->stats;
411}
412
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800413static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
414{
415 struct mapped_device *md = bdev->bd_disk->private_data;
416
417 return dm_get_geometry(md, geo);
418}
419
Mike Snitzer956a4022016-02-18 16:13:51 -0500420static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
421 struct block_device **bdev,
422 fmode_t *mode)
Milan Brozaa129a22006-10-03 01:15:15 -0700423{
Mike Snitzer66482022016-02-18 15:44:39 -0500424 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100425 struct dm_table *map;
Mike Snitzer956a4022016-02-18 16:13:51 -0500426 int srcu_idx, r;
Milan Brozaa129a22006-10-03 01:15:15 -0700427
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100428retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200429 r = -ENOTTY;
Mike Snitzer956a4022016-02-18 16:13:51 -0500430 map = dm_get_live_table(md, &srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700431 if (!map || !dm_table_get_size(map))
432 goto out;
433
434 /* We only support devices that have a single target */
435 if (dm_table_get_num_targets(map) != 1)
436 goto out;
437
Mike Snitzer66482022016-02-18 15:44:39 -0500438 tgt = dm_table_get_target(map, 0);
439 if (!tgt->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500440 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700441
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000442 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700443 r = -EAGAIN;
444 goto out;
445 }
446
Mike Snitzer66482022016-02-18 15:44:39 -0500447 r = tgt->type->prepare_ioctl(tgt, bdev, mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200448 if (r < 0)
449 goto out;
450
Mike Snitzer956a4022016-02-18 16:13:51 -0500451 bdgrab(*bdev);
452 dm_put_live_table(md, srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200453 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700454
455out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500456 dm_put_live_table(md, srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000457 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100458 msleep(10);
459 goto retry;
460 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200461 return r;
462}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100463
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200464static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
465 unsigned int cmd, unsigned long arg)
466{
467 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer956a4022016-02-18 16:13:51 -0500468 int r;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200469
Mike Snitzer956a4022016-02-18 16:13:51 -0500470 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200471 if (r < 0)
472 return r;
473
474 if (r > 0) {
475 /*
476 * Target determined this ioctl is being issued against
477 * a logical partition of the parent bdev; so extra
478 * validation is needed.
479 */
480 r = scsi_verify_blk_ioctl(NULL, cmd);
481 if (r)
482 goto out;
483 }
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;
636 return 0;
637}
638
639/*
640 * Close a table device that we've been using.
641 */
642static void close_table_device(struct table_device *td, struct mapped_device *md)
643{
644 if (!td->dm_dev.bdev)
645 return;
646
647 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
648 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
649 td->dm_dev.bdev = NULL;
650}
651
652static struct table_device *find_table_device(struct list_head *l, dev_t dev,
653 fmode_t mode) {
654 struct table_device *td;
655
656 list_for_each_entry(td, l, list)
657 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
658 return td;
659
660 return NULL;
661}
662
663int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
664 struct dm_dev **result) {
665 int r;
666 struct table_device *td;
667
668 mutex_lock(&md->table_devices_lock);
669 td = find_table_device(&md->table_devices, dev, mode);
670 if (!td) {
Mike Snitzer115485e2016-02-22 12:16:21 -0500671 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500672 if (!td) {
673 mutex_unlock(&md->table_devices_lock);
674 return -ENOMEM;
675 }
676
677 td->dm_dev.mode = mode;
678 td->dm_dev.bdev = NULL;
679
680 if ((r = open_table_device(td, dev, md))) {
681 mutex_unlock(&md->table_devices_lock);
682 kfree(td);
683 return r;
684 }
685
686 format_dev_t(td->dm_dev.name, dev);
687
688 atomic_set(&td->count, 0);
689 list_add(&td->list, &md->table_devices);
690 }
691 atomic_inc(&td->count);
692 mutex_unlock(&md->table_devices_lock);
693
694 *result = &td->dm_dev;
695 return 0;
696}
697EXPORT_SYMBOL_GPL(dm_get_table_device);
698
699void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
700{
701 struct table_device *td = container_of(d, struct table_device, dm_dev);
702
703 mutex_lock(&md->table_devices_lock);
704 if (atomic_dec_and_test(&td->count)) {
705 close_table_device(td, md);
706 list_del(&td->list);
707 kfree(td);
708 }
709 mutex_unlock(&md->table_devices_lock);
710}
711EXPORT_SYMBOL(dm_put_table_device);
712
713static void free_table_devices(struct list_head *devices)
714{
715 struct list_head *tmp, *next;
716
717 list_for_each_safe(tmp, next, devices) {
718 struct table_device *td = list_entry(tmp, struct table_device, list);
719
720 DMWARN("dm_destroy: %s still exists with %d references",
721 td->dm_dev.name, atomic_read(&td->count));
722 kfree(td);
723 }
724}
725
726/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800727 * Get the geometry associated with a dm device
728 */
729int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
730{
731 *geo = md->geometry;
732
733 return 0;
734}
735
736/*
737 * Set the geometry of a device.
738 */
739int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
740{
741 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
742
743 if (geo->start > sz) {
744 DMWARN("Start sector is beyond the geometry limits.");
745 return -EINVAL;
746 }
747
748 md->geometry = *geo;
749
750 return 0;
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*-----------------------------------------------------------------
754 * CRUD START:
755 * A more elegant soln is in the works that uses the queue
756 * merge fn, unfortunately there are a couple of changes to
757 * the block layer that I want to make for this. So in the
758 * interests of getting something for people to use I give
759 * you this clearly demarcated crap.
760 *---------------------------------------------------------------*/
761
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800762static int __noflush_suspending(struct mapped_device *md)
763{
764 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
768 * Decrements the number of outstanding ios that a bio has been
769 * cloned into, completing the original io if necc.
770 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800771static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800773 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000774 int io_error;
775 struct bio *bio;
776 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800777
778 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100779 if (unlikely(error)) {
780 spin_lock_irqsave(&io->endio_lock, flags);
781 if (!(io->error > 0 && __noflush_suspending(md)))
782 io->error = error;
783 spin_unlock_irqrestore(&io->endio_lock, flags);
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800787 if (io->error == DM_ENDIO_REQUEUE) {
788 /*
789 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800790 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100791 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200792 if (__noflush_suspending(md))
793 bio_list_add_head(&md->deferred, io->bio);
794 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800795 /* noflush suspend was interrupted. */
796 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100797 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800798 }
799
Milan Brozb35f8ca2009-03-16 17:44:36 +0000800 io_error = io->error;
801 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200802 end_io_acct(io);
803 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100804
Tejun Heo6a8736d2010-09-08 18:07:00 +0200805 if (io_error == DM_ENDIO_REQUEUE)
806 return;
807
Jens Axboe1eff9d32016-08-05 15:35:16 -0600808 if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100809 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200810 * Preflush done for flush with data, reissue
Mike Christie28a8f0d2016-06-05 14:32:25 -0500811 * without REQ_PREFLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100812 */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600813 bio->bi_opf &= ~REQ_PREFLUSH;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200814 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100815 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200816 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700817 trace_block_bio_complete(md->queue, bio, io_error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200818 bio->bi_error = io_error;
819 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822}
823
Mike Snitzer4cc96132016-05-12 16:28:10 -0400824void disable_write_same(struct mapped_device *md)
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400825{
826 struct queue_limits *limits = dm_get_queue_limits(md);
827
828 /* device doesn't really support WRITE SAME, disable it */
829 limits->max_write_same_sectors = 0;
830}
831
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200832static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200834 int error = bio->bi_error;
zhendong chen5164bec2014-12-17 14:37:04 +0800835 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500836 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000837 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700838 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 dm_endio_fn endio = tio->ti->type->end_io;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000842 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800843 if (r < 0 || r == DM_ENDIO_REQUEUE)
844 /*
845 * error and requeue request are handled
846 * in dec_pending().
847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800849 else if (r == DM_ENDIO_INCOMPLETE)
850 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200851 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800852 else if (r) {
853 DMWARN("unimplemented target endio return value: %d", r);
854 BUG();
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
Mike Christiee6047142016-06-05 14:32:04 -0500858 if (unlikely(r == -EREMOTEIO && (bio_op(bio) == REQ_OP_WRITE_SAME) &&
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400859 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
860 disable_write_same(md);
861
Mike Snitzercfae7522016-04-11 12:05:38 -0400862 free_tio(tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000863 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Mike Snitzer78d8e582015-06-26 10:01:13 -0400866/*
Mike Snitzer56a67df2010-08-12 04:14:10 +0100867 * Return maximum size of I/O possible at the supplied sector up to the current
868 * target boundary.
869 */
870static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100872 sector_t target_offset = dm_target_offset(ti, sector);
873
874 return ti->len - target_offset;
875}
876
877static sector_t max_io_len(sector_t sector, struct dm_target *ti)
878{
879 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +0100880 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /*
Mike Snitzer542f9032012-07-27 15:08:00 +0100883 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100885 if (ti->max_io_len) {
886 offset = dm_target_offset(ti, sector);
887 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
888 max_len = sector_div(offset, ti->max_io_len);
889 else
890 max_len = offset & (ti->max_io_len - 1);
891 max_len = ti->max_io_len - max_len;
892
893 if (len > max_len)
894 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
897 return len;
898}
899
Mike Snitzer542f9032012-07-27 15:08:00 +0100900int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
901{
902 if (len > UINT_MAX) {
903 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
904 (unsigned long long)len, UINT_MAX);
905 ti->error = "Maximum size of target IO is too large";
906 return -EINVAL;
907 }
908
909 ti->max_io_len = (uint32_t) len;
910
911 return 0;
912}
913EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
914
Toshi Kani545ed202016-06-22 17:54:53 -0600915static long dm_blk_direct_access(struct block_device *bdev, sector_t sector,
Linus Torvaldsf0c98eb2016-07-28 17:22:07 -0700916 void **kaddr, pfn_t *pfn, long size)
Toshi Kani545ed202016-06-22 17:54:53 -0600917{
918 struct mapped_device *md = bdev->bd_disk->private_data;
919 struct dm_table *map;
920 struct dm_target *ti;
921 int srcu_idx;
922 long len, ret = -EIO;
923
924 map = dm_get_live_table(md, &srcu_idx);
925 if (!map)
926 goto out;
927
928 ti = dm_table_find_target(map, sector);
929 if (!dm_target_is_valid(ti))
930 goto out;
931
932 len = max_io_len(sector, ti) << SECTOR_SHIFT;
933 size = min(len, size);
934
935 if (ti->type->direct_access)
936 ret = ti->type->direct_access(ti, sector, kaddr, pfn, size);
937out:
938 dm_put_live_table(md, srcu_idx);
939 return min(ret, size);
940}
941
Mikulas Patocka1dd40c32014-03-14 18:41:24 -0400942/*
943 * A target may call dm_accept_partial_bio only from the map routine. It is
Mike Christie28a8f0d2016-06-05 14:32:25 -0500944 * allowed for all bio types except REQ_PREFLUSH.
Mikulas Patocka1dd40c32014-03-14 18:41:24 -0400945 *
946 * dm_accept_partial_bio informs the dm that the target only wants to process
947 * additional n_sectors sectors of the bio and the rest of the data should be
948 * sent in a next bio.
949 *
950 * A diagram that explains the arithmetics:
951 * +--------------------+---------------+-------+
952 * | 1 | 2 | 3 |
953 * +--------------------+---------------+-------+
954 *
955 * <-------------- *tio->len_ptr --------------->
956 * <------- bi_size ------->
957 * <-- n_sectors -->
958 *
959 * Region 1 was already iterated over with bio_advance or similar function.
960 * (it may be empty if the target doesn't use bio_advance)
961 * Region 2 is the remaining bio size that the target wants to process.
962 * (it may be empty if region 1 is non-empty, although there is no reason
963 * to make it empty)
964 * The target requires that region 3 is to be sent in the next bio.
965 *
966 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
967 * the partially processed part (the sum of regions 1+2) must be the same for all
968 * copies of the bio.
969 */
970void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
971{
972 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
973 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600974 BUG_ON(bio->bi_opf & REQ_PREFLUSH);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -0400975 BUG_ON(bi_size > *tio->len_ptr);
976 BUG_ON(n_sectors > bi_size);
977 *tio->len_ptr -= bi_size - n_sectors;
978 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
979}
980EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
981
Mikulas Patocka21582cd2017-02-15 11:26:10 -0500982/*
983 * Flush current->bio_list when the target map method blocks.
984 * This fixes deadlocks in snapshot and possibly in other targets.
985 */
986struct dm_offload {
987 struct blk_plug plug;
988 struct blk_plug_cb cb;
989};
990
991static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
992{
993 struct dm_offload *o = container_of(cb, struct dm_offload, cb);
994 struct bio_list list;
995 struct bio *bio;
NeilBrown5959cde2017-03-10 17:00:47 +1100996 int i;
Mikulas Patocka21582cd2017-02-15 11:26:10 -0500997
998 INIT_LIST_HEAD(&o->cb.list);
999
1000 if (unlikely(!current->bio_list))
1001 return;
1002
NeilBrown5959cde2017-03-10 17:00:47 +11001003 for (i = 0; i < 2; i++) {
1004 list = current->bio_list[i];
1005 bio_list_init(&current->bio_list[i]);
Mikulas Patocka21582cd2017-02-15 11:26:10 -05001006
NeilBrown5959cde2017-03-10 17:00:47 +11001007 while ((bio = bio_list_pop(&list))) {
1008 struct bio_set *bs = bio->bi_pool;
1009 if (unlikely(!bs) || bs == fs_bio_set) {
1010 bio_list_add(&current->bio_list[i], bio);
1011 continue;
1012 }
1013
1014 spin_lock(&bs->rescue_lock);
1015 bio_list_add(&bs->rescue_list, bio);
1016 queue_work(bs->rescue_workqueue, &bs->rescue_work);
1017 spin_unlock(&bs->rescue_lock);
Mikulas Patocka21582cd2017-02-15 11:26:10 -05001018 }
Mikulas Patocka21582cd2017-02-15 11:26:10 -05001019 }
1020}
1021
1022static void dm_offload_start(struct dm_offload *o)
1023{
1024 blk_start_plug(&o->plug);
1025 o->cb.callback = flush_current_bio_list;
1026 list_add(&o->cb.list, &current->plug->cb_list);
1027}
1028
1029static void dm_offload_end(struct dm_offload *o)
1030{
1031 list_del(&o->cb.list);
1032 blk_finish_plug(&o->plug);
1033}
1034
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001035static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001038 sector_t sector;
Mikulas Patocka21582cd2017-02-15 11:26:10 -05001039 struct dm_offload o;
Mikulas Patockadba14162012-10-12 21:02:15 +01001040 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001041 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /*
1046 * Map the clone. If r == 0 we don't need to do
1047 * anything, the target has assumed ownership of
1048 * this io.
1049 */
1050 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001051 sector = clone->bi_iter.bi_sector;
Mikulas Patocka21582cd2017-02-15 11:26:10 -05001052
1053 dm_offload_start(&o);
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001054 r = ti->type->map(ti, clone);
Mikulas Patocka21582cd2017-02-15 11:26:10 -05001055 dm_offload_end(&o);
1056
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001057 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001059
Mike Snitzerd07335e2010-11-16 12:52:38 +01001060 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1061 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001064 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1065 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001066 dec_pending(tio->io, r);
Mike Snitzercfae7522016-04-11 12:05:38 -04001067 free_tio(tio);
Mikulas Patockaab378442015-07-01 17:30:36 -04001068 } else if (r != DM_MAPIO_SUBMITTED) {
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001069 DMWARN("unimplemented target map return value: %d", r);
1070 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072}
1073
1074struct clone_info {
1075 struct mapped_device *md;
1076 struct dm_table *map;
1077 struct bio *bio;
1078 struct dm_io *io;
1079 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001080 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081};
1082
Mikulas Patockae0d66092014-03-14 18:40:39 -04001083static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001084{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001085 bio->bi_iter.bi_sector = sector;
1086 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/*
1090 * Creates a bio that consists of range of complete bvecs.
1091 */
Mike Snitzerc80914e2016-03-02 12:33:03 -05001092static int clone_bio(struct dm_target_io *tio, struct bio *bio,
1093 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Mikulas Patockadba14162012-10-12 21:02:15 +01001095 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001097 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Mike Snitzerc80914e2016-03-02 12:33:03 -05001099 if (bio_integrity(bio)) {
1100 int r = bio_integrity_clone(clone, bio, GFP_NOIO);
1101 if (r < 0)
1102 return r;
1103 }
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001104
1105 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1106 clone->bi_iter.bi_size = to_bytes(len);
1107
1108 if (bio_integrity(bio))
1109 bio_integrity_trim(clone, 0, len);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001110
1111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001114static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001115 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001116 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001117{
Mikulas Patockadba14162012-10-12 21:02:15 +01001118 struct dm_target_io *tio;
1119 struct bio *clone;
1120
Junichi Nomura99778272014-10-03 11:55:16 +00001121 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001122 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001123
1124 tio->io = ci->io;
1125 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001126 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001127
1128 return tio;
1129}
1130
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001131static void __clone_and_map_simple_bio(struct clone_info *ci,
1132 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001133 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001134{
Junichi Nomura99778272014-10-03 11:55:16 +00001135 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001136 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001137
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001138 tio->len_ptr = len;
1139
Junichi Nomura99778272014-10-03 11:55:16 +00001140 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001141 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001142 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001143
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001144 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001145}
1146
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001147static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001148 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001149{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001150 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001151
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001152 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001153 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001154}
1155
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001156static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001157{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001158 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001159 struct dm_target *ti;
1160
Mike Snitzerb372d362010-09-08 18:07:01 +02001161 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001162 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001163 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001164
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001165 return 0;
1166}
1167
Mike Snitzerc80914e2016-03-02 12:33:03 -05001168static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001169 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001170{
Mikulas Patockadba14162012-10-12 21:02:15 +01001171 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001172 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001173 unsigned target_bio_nr;
1174 unsigned num_target_bios = 1;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001175 int r = 0;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001176
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001177 /*
1178 * Does the target want to receive duplicate copies of the bio?
1179 */
1180 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1181 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001182
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001183 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001184 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001185 tio->len_ptr = len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001186 r = clone_bio(tio, bio, sector, *len);
Mikulas Patocka072623d2016-04-09 12:48:18 -04001187 if (r < 0) {
Mike Snitzercfae7522016-04-11 12:05:38 -04001188 free_tio(tio);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001189 break;
Mikulas Patocka072623d2016-04-09 12:48:18 -04001190 }
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001191 __map_bio(tio);
1192 }
Mike Snitzerc80914e2016-03-02 12:33:03 -05001193
1194 return r;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001195}
1196
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001197typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001198
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001199static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001200{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001201 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001202}
1203
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001204static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001205{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001206 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001207}
1208
1209typedef bool (*is_split_required_fn)(struct dm_target *ti);
1210
1211static bool is_split_required_for_discard(struct dm_target *ti)
1212{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001213 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001214}
1215
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001216static int __send_changing_extent_only(struct clone_info *ci,
1217 get_num_bios_fn get_num_bios,
1218 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001219{
1220 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001221 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001222 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001223
Mike Snitzera79245b2010-08-12 04:14:24 +01001224 do {
1225 ti = dm_table_find_target(ci->map, ci->sector);
1226 if (!dm_target_is_valid(ti))
1227 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001228
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001229 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001230 * Even though the device advertised support for this type of
1231 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001232 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001233 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001234 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001235 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1236 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001237 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001238
Mike Snitzer23508a92012-12-21 20:23:37 +00001239 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001240 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001241 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001242 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001243
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001244 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001245
1246 ci->sector += len;
1247 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001248
1249 return 0;
1250}
1251
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001252static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001253{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001254 return __send_changing_extent_only(ci, get_num_discard_bios,
1255 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001256}
1257
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001258static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001259{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001260 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001261}
1262
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001263/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001264 * Select the correct strategy for processing a non-flush bio.
1265 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001266static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Mikulas Patockadba14162012-10-12 21:02:15 +01001268 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001269 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001270 unsigned len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001271 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Mike Christiee6047142016-06-05 14:32:04 -05001273 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001274 return __send_discard(ci);
Mike Christiee6047142016-06-05 14:32:04 -05001275 else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001276 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001277
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001278 ti = dm_table_find_target(ci->map, ci->sector);
1279 if (!dm_target_is_valid(ti))
1280 return -EIO;
1281
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001282 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001283
Mike Snitzerc80914e2016-03-02 12:33:03 -05001284 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1285 if (r < 0)
1286 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001288 ci->sector += len;
1289 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001295 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001297static void __split_and_process_bio(struct mapped_device *md,
1298 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001301 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001303 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001304 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001305 return;
1306 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001307
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001308 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 ci.io = alloc_io(md);
1311 ci.io->error = 0;
1312 atomic_set(&ci.io->io_count, 1);
1313 ci.io->bio = bio;
1314 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001315 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001316 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001318 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001319
Jens Axboe1eff9d32016-08-05 15:35:16 -06001320 if (bio->bi_opf & REQ_PREFLUSH) {
Mike Snitzerb372d362010-09-08 18:07:01 +02001321 ci.bio = &ci.md->flush_bio;
1322 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001323 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001324 /* dec_pending submits any data associated with flush */
1325 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001326 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001327 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001328 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001329 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001333 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335/*-----------------------------------------------------------------
1336 * CRUD END
1337 *---------------------------------------------------------------*/
1338
1339/*
1340 * The request function that just remaps the bio built up by
1341 * dm_merge_bvec.
1342 */
Jens Axboedece1632015-11-05 10:41:16 -07001343static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Kevin Corry12f03a42006-02-01 03:04:52 -08001345 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001347 int srcu_idx;
1348 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001350 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Gu Zheng18c0b222014-11-24 11:05:26 +08001352 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001353
Tejun Heo6a8736d2010-09-08 18:07:00 +02001354 /* if we're suspended, we have to queue this io for later */
1355 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001356 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Jens Axboe1eff9d32016-08-05 15:35:16 -06001358 if (!(bio->bi_opf & REQ_RAHEAD))
Tejun Heo6a8736d2010-09-08 18:07:00 +02001359 queue_io(md, bio);
1360 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001361 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001362 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001365 __split_and_process_bio(md, map, bio);
1366 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001367 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370static int dm_any_congested(void *congested_data, int bdi_bits)
1371{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001372 int r = bdi_bits;
1373 struct mapped_device *md = congested_data;
1374 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001376 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05001377 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001378 /*
Mike Snitzere522c032016-02-02 22:35:06 -05001379 * With request-based DM we only need to check the
1380 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001381 */
Mike Snitzere522c032016-02-02 22:35:06 -05001382 r = md->queue->backing_dev_info.wb.state & bdi_bits;
1383 } else {
1384 map = dm_get_live_table_fast(md);
1385 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001386 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05001387 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001388 }
1389 }
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return r;
1392}
1393
1394/*-----------------------------------------------------------------
1395 * An IDR is used to keep track of allocated minor numbers.
1396 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001397static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001399 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001401 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
1404/*
1405 * See if the device with a specific minor # is free.
1406 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001407static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001409 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (minor >= (1 << MINORBITS))
1412 return -EINVAL;
1413
Tejun Heoc9d76be2013-02-27 17:04:26 -08001414 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001415 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Tejun Heoc9d76be2013-02-27 17:04:26 -08001417 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001419 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001420 idr_preload_end();
1421 if (r < 0)
1422 return r == -ENOSPC ? -EBUSY : r;
1423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001426static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001428 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Tejun Heoc9d76be2013-02-27 17:04:26 -08001430 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001431 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Tejun Heoc9d76be2013-02-27 17:04:26 -08001433 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001435 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001436 idr_preload_end();
1437 if (r < 0)
1438 return r;
1439 *minor = r;
1440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001443static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Mikulas Patocka53d59142009-04-02 19:55:37 +01001445static void dm_wq_work(struct work_struct *work);
1446
Mike Snitzer4cc96132016-05-12 16:28:10 -04001447void dm_init_md_queue(struct mapped_device *md)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001448{
1449 /*
1450 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001451 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001452 * The type is decided at the first table loading time.
1453 * To prevent problematic device stacking, clear the queue flag
1454 * for request stacking support until then.
1455 *
1456 * This queue is new, so no concurrency on the queue_flags.
1457 */
1458 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04001459
1460 /*
1461 * Initialize data that will only be used by a non-blk-mq DM queue
1462 * - must do so here (in alloc_dev callchain) before queue is used
1463 */
1464 md->queue->queuedata = md;
1465 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001466}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001467
Mike Snitzer4cc96132016-05-12 16:28:10 -04001468void dm_init_normal_md_queue(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001469{
Mike Snitzer17e149b2015-03-11 15:01:09 -04001470 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001471 dm_init_md_queue(md);
1472
1473 /*
1474 * Initialize aspects of queue that aren't relevant for blk-mq
1475 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001476 md->queue->backing_dev_info.congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001477 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001478}
1479
Mike Snitzer0f209722015-04-28 11:50:29 -04001480static void cleanup_mapped_device(struct mapped_device *md)
1481{
Mike Snitzer0f209722015-04-28 11:50:29 -04001482 if (md->wq)
1483 destroy_workqueue(md->wq);
1484 if (md->kworker_task)
1485 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02001486 mempool_destroy(md->io_pool);
1487 mempool_destroy(md->rq_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04001488 if (md->bs)
1489 bioset_free(md->bs);
1490
1491 if (md->disk) {
1492 spin_lock(&_minor_lock);
1493 md->disk->private_data = NULL;
1494 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04001495 del_gendisk(md->disk);
1496 put_disk(md->disk);
1497 }
1498
1499 if (md->queue)
1500 blk_cleanup_queue(md->queue);
1501
Tahsin Erdogand09960b2016-10-10 05:35:19 -07001502 cleanup_srcu_struct(&md->io_barrier);
1503
Mike Snitzer0f209722015-04-28 11:50:29 -04001504 if (md->bdev) {
1505 bdput(md->bdev);
1506 md->bdev = NULL;
1507 }
Mike Snitzer4cc96132016-05-12 16:28:10 -04001508
1509 dm_mq_cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512/*
1513 * Allocate and initialise a blank device with a given minor.
1514 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001515static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Mike Snitzer115485e2016-02-22 12:16:21 -05001517 int r, numa_node_id = dm_get_numa_node();
1518 struct mapped_device *md;
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001519 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Mike Snitzer115485e2016-02-22 12:16:21 -05001521 md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (!md) {
1523 DMWARN("unable to allocate device, out of memory.");
1524 return NULL;
1525 }
1526
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001527 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001528 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001531 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001532 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001533 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001534 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001536 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001538 r = init_srcu_struct(&md->io_barrier);
1539 if (r < 0)
1540 goto bad_io_barrier;
1541
Mike Snitzer115485e2016-02-22 12:16:21 -05001542 md->numa_node_id = numa_node_id;
Mike Snitzer4cc96132016-05-12 16:28:10 -04001543 md->use_blk_mq = dm_use_blk_mq_default();
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001544 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01001545 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001546 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001547 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001548 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001549 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001551 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001553 atomic_set(&md->uevent_seq, 0);
1554 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001555 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001556 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Mike Snitzer115485e2016-02-22 12:16:21 -05001558 md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04001560 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001562 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001563
Mike Snitzer115485e2016-02-22 12:16:21 -05001564 md->disk = alloc_disk_node(1, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04001566 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001568 atomic_set(&md->pending[0], 0);
1569 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001570 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001571 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001572 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05001573 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001574 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 md->disk->major = _major;
1577 md->disk->first_minor = minor;
1578 md->disk->fops = &dm_blk_dops;
1579 md->disk->queue = md->queue;
1580 md->disk->private_data = md;
1581 sprintf(md->disk->disk_name, "dm-%d", minor);
1582 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001583 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Tejun Heo670368a2013-07-30 08:40:21 -04001585 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001586 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04001587 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00001588
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001589 md->bdev = bdget_disk(md->disk, 0);
1590 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04001591 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001592
Tejun Heo6a8736d2010-09-08 18:07:00 +02001593 bio_init(&md->flush_bio);
1594 md->flush_bio.bi_bdev = md->bdev;
Mike Christiee6047142016-06-05 14:32:04 -05001595 bio_set_op_attrs(&md->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
Tejun Heo6a8736d2010-09-08 18:07:00 +02001596
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001597 dm_stats_init(&md->stats);
1598
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001599 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001600 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001601 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001602 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001603
1604 BUG_ON(old_md != MINOR_ALLOCED);
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return md;
1607
Mike Snitzer0f209722015-04-28 11:50:29 -04001608bad:
1609 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001610bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001612bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001613 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001614bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 kfree(md);
1616 return NULL;
1617}
1618
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001619static void unlock_fs(struct mapped_device *md);
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621static void free_dev(struct mapped_device *md)
1622{
Tejun Heof331c022008-09-03 09:01:48 +02001623 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001624
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001625 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001626
Mike Snitzer0f209722015-04-28 11:50:29 -04001627 cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001628
1629 free_table_devices(&md->table_devices);
1630 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04001631 free_minor(minor);
1632
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001633 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 kfree(md);
1635}
1636
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001637static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1638{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001639 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001640
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001641 if (md->bs) {
1642 /* The md already has necessary mempools. */
Toshi Kani545ed202016-06-22 17:54:53 -06001643 if (dm_table_bio_based(t)) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001644 /*
1645 * Reload bioset because front_pad may have changed
1646 * because a different table was loaded.
1647 */
1648 bioset_free(md->bs);
1649 md->bs = p->bs;
1650 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001651 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001652 /*
1653 * There's no need to reload with request-based dm
1654 * because the size of front_pad doesn't change.
1655 * Note for future: If you are to reload bioset,
1656 * prep-ed requests in the queue may refer
1657 * to bio from the old bioset, so you must walk
1658 * through the queue to unprep.
1659 */
1660 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001661 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001662
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04001663 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
1664
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001665 md->io_pool = p->io_pool;
1666 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001667 md->rq_pool = p->rq_pool;
1668 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001669 md->bs = p->bs;
1670 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001671
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001672out:
Mike Snitzer02233342015-03-10 23:49:26 -04001673 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001674 dm_table_free_md_mempools(t);
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677/*
1678 * Bind a table to the device.
1679 */
1680static void event_callback(void *context)
1681{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001682 unsigned long flags;
1683 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct mapped_device *md = (struct mapped_device *) context;
1685
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001686 spin_lock_irqsave(&md->uevent_lock, flags);
1687 list_splice_init(&md->uevent_list, &uevents);
1688 spin_unlock_irqrestore(&md->uevent_lock, flags);
1689
Tejun Heoed9e1982008-08-25 19:56:05 +09001690 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 atomic_inc(&md->event_nr);
1693 wake_up(&md->eventq);
1694}
1695
Mike Snitzerc2176492011-01-13 19:53:46 +00001696/*
1697 * Protected by md->suspend_lock obtained by dm_swap_table().
1698 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001699static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001701 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001703 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001706/*
1707 * Returns old map, which caller must destroy.
1708 */
1709static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1710 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001712 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02001713 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 sector_t size;
1715
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07001716 lockdep_assert_held(&md->suspend_lock);
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001719
1720 /*
1721 * Wipe any geometry if the size of the table changed.
1722 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001723 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001724 memset(&md->geometry, 0, sizeof(md->geometry));
1725
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001726 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001728 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001729
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001730 /*
1731 * The queue hasn't been stopped yet, if the old table type wasn't
1732 * for request-based during suspension. So stop it to prevent
1733 * I/O mapping before resume.
1734 * This must be done before setting the queue restrictions,
1735 * because request-based dm may be run just after the setting.
1736 */
Mike Snitzer16f12262016-01-31 17:22:27 -05001737 if (dm_table_request_based(t)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001738 dm_stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05001739 /*
1740 * Leverage the fact that request-based DM targets are
1741 * immutable singletons and establish md->immutable_target
1742 * - used to optimize both dm_request_fn and dm_mq_queue_rq
1743 */
1744 md->immutable_target = dm_table_get_immutable_target(t);
1745 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001746
1747 __bind_mempools(md, t);
1748
Eric Dumazeta12f5d42014-11-23 09:34:29 -08001749 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -05001750 rcu_assign_pointer(md->map, (void *)t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001751 md->immutable_target_type = dm_table_get_immutable_target_type(t);
1752
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001753 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01001754 if (old_map)
1755 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001756
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001757 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Alasdair G Kergona7940152009-12-10 23:52:23 +00001760/*
1761 * Returns unbound table for the caller to free.
1762 */
1763static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08001765 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00001768 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05301771 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001772 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00001773
1774 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777/*
1778 * Constructor for a new device.
1779 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001780int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 struct mapped_device *md;
1783
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001784 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (!md)
1786 return -ENXIO;
1787
Milan Broz784aae72009-01-06 03:05:12 +00001788 dm_sysfs_init(md);
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 *result = md;
1791 return 0;
1792}
1793
Mike Snitzera5664da2010-08-12 04:14:01 +01001794/*
1795 * Functions to manage md->type.
1796 * All are required to hold md->type_lock.
1797 */
1798void dm_lock_md_type(struct mapped_device *md)
1799{
1800 mutex_lock(&md->type_lock);
1801}
1802
1803void dm_unlock_md_type(struct mapped_device *md)
1804{
1805 mutex_unlock(&md->type_lock);
1806}
1807
1808void dm_set_md_type(struct mapped_device *md, unsigned type)
1809{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04001810 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01001811 md->type = type;
1812}
1813
1814unsigned dm_get_md_type(struct mapped_device *md)
1815{
1816 return md->type;
1817}
1818
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001819struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
1820{
1821 return md->immutable_target_type;
1822}
1823
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001824/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04001825 * The queue_limits are only valid as long as you have a reference
1826 * count on 'md'.
1827 */
1828struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
1829{
1830 BUG_ON(!atomic_read(&md->holders));
1831 return &md->queue->limits;
1832}
1833EXPORT_SYMBOL_GPL(dm_get_queue_limits);
1834
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001835/*
1836 * Setup the DM device's queue based on md's type
1837 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001838int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001839{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001840 int r;
Toshi Kani545ed202016-06-22 17:54:53 -06001841 unsigned type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001842
Toshi Kani545ed202016-06-22 17:54:53 -06001843 switch (type) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001844 case DM_TYPE_REQUEST_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05001845 r = dm_old_init_request_queue(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001846 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001847 DMERR("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001848 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05001849 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001850 break;
1851 case DM_TYPE_MQ_REQUEST_BASED:
Mike Snitzere83068a2016-05-24 21:16:51 -04001852 r = dm_mq_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001853 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001854 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001855 return r;
1856 }
1857 break;
1858 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06001859 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05001860 dm_init_normal_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05001861 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04001862 /*
1863 * DM handles splitting bios as needed. Free the bio_split bioset
1864 * since it won't be used (saves 1 process per bio-based DM device).
1865 */
1866 bioset_free(md->queue->bio_split);
1867 md->queue->bio_split = NULL;
Toshi Kani545ed202016-06-22 17:54:53 -06001868
1869 if (type == DM_TYPE_DAX_BIO_BASED)
1870 queue_flag_set_unlocked(QUEUE_FLAG_DAX, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001871 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001872 }
1873
1874 return 0;
1875}
1876
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05001877struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 unsigned minor = MINOR(dev);
1881
1882 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1883 return NULL;
1884
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001885 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05001888 if (md) {
1889 if ((md == MINOR_ALLOCED ||
1890 (MINOR(disk_devt(dm_disk(md))) != minor) ||
1891 dm_deleting_md(md) ||
1892 test_bit(DMF_FREEING, &md->flags))) {
1893 md = NULL;
1894 goto out;
1895 }
1896 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001899out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001900 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
David Teigland637842c2006-01-06 00:20:00 -08001902 return md;
1903}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00001904EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08001905
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001906void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001907{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001908 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911void dm_set_mdptr(struct mapped_device *md, void *ptr)
1912{
1913 md->interface_ptr = ptr;
1914}
1915
1916void dm_get(struct mapped_device *md)
1917{
1918 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001919 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001922int dm_hold(struct mapped_device *md)
1923{
1924 spin_lock(&_minor_lock);
1925 if (test_bit(DMF_FREEING, &md->flags)) {
1926 spin_unlock(&_minor_lock);
1927 return -EBUSY;
1928 }
1929 dm_get(md);
1930 spin_unlock(&_minor_lock);
1931 return 0;
1932}
1933EXPORT_SYMBOL_GPL(dm_hold);
1934
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001935const char *dm_device_name(struct mapped_device *md)
1936{
1937 return md->name;
1938}
1939EXPORT_SYMBOL_GPL(dm_device_name);
1940
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001941static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Bart Van Assche3b785fb2016-08-31 15:17:49 -07001943 struct request_queue *q = dm_get_md_queue(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001944 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001945 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001947 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001948
Mike Snitzer63a4f062015-03-23 17:01:43 -04001949 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001950 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
1951 set_bit(DMF_FREEING, &md->flags);
1952 spin_unlock(&_minor_lock);
1953
Bart Van Assche3b785fb2016-08-31 15:17:49 -07001954 spin_lock_irq(q->queue_lock);
1955 queue_flag_set(QUEUE_FLAG_DYING, q);
1956 spin_unlock_irq(q->queue_lock);
1957
Mike Snitzer02233342015-03-10 23:49:26 -04001958 if (dm_request_based(md) && md->kworker_task)
Petr Mladek39891442016-10-11 13:55:20 -07001959 kthread_flush_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001960
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05001961 /*
1962 * Take suspend_lock so that presuspend and postsuspend methods
1963 * do not race with internal suspend.
1964 */
1965 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00001966 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001967 if (!dm_suspended_md(md)) {
1968 dm_table_presuspend_targets(map);
1969 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001971 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
1972 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00001973 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001974
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001975 /*
1976 * Rare, but there may be I/O requests still going to complete,
1977 * for example. Wait for all references to disappear.
1978 * No one should increment the reference count of the mapped_device,
1979 * after the mapped_device state becomes DMF_FREEING.
1980 */
1981 if (wait)
1982 while (atomic_read(&md->holders))
1983 msleep(1);
1984 else if (atomic_read(&md->holders))
1985 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
1986 dm_device_name(md), atomic_read(&md->holders));
1987
1988 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001989 dm_table_destroy(__unbind(md));
1990 free_dev(md);
1991}
1992
1993void dm_destroy(struct mapped_device *md)
1994{
1995 __dm_destroy(md, true);
1996}
1997
1998void dm_destroy_immediate(struct mapped_device *md)
1999{
2000 __dm_destroy(md, false);
2001}
2002
2003void dm_put(struct mapped_device *md)
2004{
2005 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
Edward Goggin79eb8852007-05-09 02:32:56 -07002007EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002009static int dm_wait_for_completion(struct mapped_device *md, long task_state)
Milan Broz46125c12008-02-08 02:10:30 +00002010{
2011 int r = 0;
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002012 DEFINE_WAIT(wait);
Milan Broz46125c12008-02-08 02:10:30 +00002013
2014 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002015 prepare_to_wait(&md->wait, &wait, task_state);
Milan Broz46125c12008-02-08 02:10:30 +00002016
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002017 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002018 break;
2019
Bart Van Asschee3fabdf2016-08-31 15:16:22 -07002020 if (signal_pending_state(task_state, current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002021 r = -EINTR;
2022 break;
2023 }
2024
2025 io_schedule();
2026 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002027 finish_wait(&md->wait, &wait);
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002028
Milan Broz46125c12008-02-08 02:10:30 +00002029 return r;
2030}
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032/*
2033 * Process the deferred bios
2034 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002035static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Mikulas Patockaef208582009-04-02 19:55:38 +01002037 struct mapped_device *md = container_of(work, struct mapped_device,
2038 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002039 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002040 int srcu_idx;
2041 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002043 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002044
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002045 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002046 spin_lock_irq(&md->deferred_lock);
2047 c = bio_list_pop(&md->deferred);
2048 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002049
Tejun Heo6a8736d2010-09-08 18:07:00 +02002050 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002051 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002052
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002053 if (dm_request_based(md))
2054 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002055 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002056 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002057 }
Milan Broz73d410c2008-02-08 02:10:25 +00002058
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002059 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002062static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002063{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002064 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002065 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002066 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002067}
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002070 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002072struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Mike Christie87eb5b22013-03-01 22:45:48 +00002074 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002075 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002076 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Daniel Walkere61290a2008-02-08 02:10:08 +00002078 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002081 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002082 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Mike Snitzer3ae70652012-09-26 23:45:45 +01002084 /*
2085 * If the new table has no data devices, retain the existing limits.
2086 * This helps multipath with queue_if_no_path if all paths disappear,
2087 * then new I/O is queued based on these limits, and then some paths
2088 * reappear.
2089 */
2090 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002091 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002092 if (live_map)
2093 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002094 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002095 }
2096
Mike Christie87eb5b22013-03-01 22:45:48 +00002097 if (!live_map) {
2098 r = dm_calculate_queue_limits(table, &limits);
2099 if (r) {
2100 map = ERR_PTR(r);
2101 goto out;
2102 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002103 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002104
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002105 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002107out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002108 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002109 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112/*
2113 * Functions to lock and unlock any filesystem running on the
2114 * device.
2115 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002116static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002118 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002121
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002122 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002123 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002124 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002125 md->frozen_sb = NULL;
2126 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002127 }
2128
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002129 set_bit(DMF_FROZEN, &md->flags);
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return 0;
2132}
2133
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002134static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002136 if (!test_bit(DMF_FROZEN, &md->flags))
2137 return;
2138
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002139 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002141 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
2144/*
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002145 * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2146 * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2147 * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2148 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04002149 * If __dm_suspend returns 0, the device is completely quiescent
2150 * now. There is no request-processing activity. All new requests
2151 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002152 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04002153 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002154 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04002155static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002156 unsigned suspend_flags, long task_state,
Mike Snitzereaf9a732016-08-02 13:07:20 -04002157 int dmf_suspended_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002159 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2160 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2161 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07002163 lockdep_assert_held(&md->suspend_lock);
2164
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002165 /*
2166 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2167 * This flag is cleared before dm_suspend returns.
2168 */
2169 if (noflush)
2170 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2171
Mike Snitzerd67ee212014-10-28 20:13:31 -04002172 /*
2173 * This gets reverted if there's an error later and the targets
2174 * provide the .presuspend_undo hook.
2175 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002176 dm_table_presuspend_targets(map);
2177
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002178 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002179 * Flush I/O to the device.
2180 * Any I/O submitted after lock_fs() may not be flushed.
2181 * noflush takes precedence over do_lockfs.
2182 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002183 */
2184 if (!noflush && do_lockfs) {
2185 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002186 if (r) {
2187 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002188 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04002189 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002193 * Here we must make sure that no processes are submitting requests
2194 * to target drivers i.e. no one may be executing
2195 * __split_and_process_bio. This is called from dm_request and
2196 * dm_wq_work.
2197 *
2198 * To get all processes out of __split_and_process_bio in dm_request,
2199 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002200 * __split_and_process_bio from dm_request and quiesce the thread
2201 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2202 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002204 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002205 if (map)
2206 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002208 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002209 * Stop md->queue before flushing md->wq in case request-based
2210 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002211 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002212 if (dm_request_based(md)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002213 dm_stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04002214 if (md->kworker_task)
Petr Mladek39891442016-10-11 13:55:20 -07002215 kthread_flush_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002216 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002217
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002218 flush_workqueue(md->wq);
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002221 * At this point no more requests are entering target request routines.
2222 * We call dm_wait_for_completion to wait for all existing requests
2223 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 */
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002225 r = dm_wait_for_completion(md, task_state);
Mike Snitzereaf9a732016-08-02 13:07:20 -04002226 if (!r)
2227 set_bit(dmf_suspended_flag, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Milan Broz6d6f10d2008-02-08 02:10:22 +00002229 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002230 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002231 if (map)
2232 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002235 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002236 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002237
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002238 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002239 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002240
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002241 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002242 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002243 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002244 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002245
Mike Snitzerffcc3932014-10-28 18:34:52 -04002246 return r;
2247}
2248
2249/*
2250 * We need to be able to change a mapping table under a mounted
2251 * filesystem. For example we might want to move some data in
2252 * the background. Before the table can be swapped with
2253 * dm_bind_table, dm_suspend must be called to flush any in
2254 * flight bios and ensure that any further io gets deferred.
2255 */
2256/*
2257 * Suspend mechanism in request-based dm.
2258 *
2259 * 1. Flush all I/Os by lock_fs() if needed.
2260 * 2. Stop dispatching any I/O by stopping the request_queue.
2261 * 3. Wait for all in-flight I/Os to be completed or requeued.
2262 *
2263 * To abort suspend, start the request_queue.
2264 */
2265int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2266{
2267 struct dm_table *map = NULL;
2268 int r = 0;
2269
2270retry:
2271 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2272
2273 if (dm_suspended_md(md)) {
2274 r = -EINVAL;
2275 goto out_unlock;
2276 }
2277
2278 if (dm_suspended_internally_md(md)) {
2279 /* already internally suspended, wait for internal resume */
2280 mutex_unlock(&md->suspend_lock);
2281 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2282 if (r)
2283 return r;
2284 goto retry;
2285 }
2286
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002287 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002288
Mike Snitzereaf9a732016-08-02 13:07:20 -04002289 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002290 if (r)
2291 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002292
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002293 dm_table_postsuspend_targets(map);
2294
Alasdair G Kergond2874832006-11-08 17:44:43 -08002295out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002296 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002297 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
Mike Snitzerffcc3932014-10-28 18:34:52 -04002300static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002302 if (map) {
2303 int r = dm_table_resume_targets(map);
2304 if (r)
2305 return r;
2306 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002307
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002308 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002309
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002310 /*
2311 * Flushing deferred I/Os must be done after targets are resumed
2312 * so that mapping of targets can work correctly.
2313 * Request-based dm is queueing the deferred I/Os in its request_queue.
2314 */
2315 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002316 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002317
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002318 unlock_fs(md);
2319
Mike Snitzerffcc3932014-10-28 18:34:52 -04002320 return 0;
2321}
2322
2323int dm_resume(struct mapped_device *md)
2324{
Minfei Huang8dc23652016-09-06 16:00:29 +08002325 int r;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002326 struct dm_table *map = NULL;
2327
2328retry:
Minfei Huang8dc23652016-09-06 16:00:29 +08002329 r = -EINVAL;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002330 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2331
2332 if (!dm_suspended_md(md))
2333 goto out;
2334
2335 if (dm_suspended_internally_md(md)) {
2336 /* already internally suspended, wait for internal resume */
2337 mutex_unlock(&md->suspend_lock);
2338 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2339 if (r)
2340 return r;
2341 goto retry;
2342 }
2343
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002344 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002345 if (!map || !dm_table_get_size(map))
2346 goto out;
2347
2348 r = __dm_resume(md, map);
2349 if (r)
2350 goto out;
2351
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002352 clear_bit(DMF_SUSPENDED, &md->flags);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002353out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002354 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002355
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002356 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002359/*
2360 * Internal suspend/resume works like userspace-driven suspend. It waits
2361 * until all bios finish and prevents issuing new bios to the target drivers.
2362 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002363 */
2364
Mike Snitzerffcc3932014-10-28 18:34:52 -04002365static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2366{
2367 struct dm_table *map = NULL;
2368
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002369 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002370 return; /* nested internal suspend */
2371
2372 if (dm_suspended_md(md)) {
2373 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2374 return; /* nest suspend */
2375 }
2376
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002377 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002378
2379 /*
2380 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2381 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
2382 * would require changing .presuspend to return an error -- avoid this
2383 * until there is a need for more elaborate variants of internal suspend.
2384 */
Mike Snitzereaf9a732016-08-02 13:07:20 -04002385 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2386 DMF_SUSPENDED_INTERNALLY);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002387
2388 dm_table_postsuspend_targets(map);
2389}
2390
2391static void __dm_internal_resume(struct mapped_device *md)
2392{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002393 BUG_ON(!md->internal_suspend_count);
2394
2395 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002396 return; /* resume from nested internal suspend */
2397
2398 if (dm_suspended_md(md))
2399 goto done; /* resume from nested suspend */
2400
2401 /*
2402 * NOTE: existing callers don't need to call dm_table_resume_targets
2403 * (which may fail -- so best to avoid it for now by passing NULL map)
2404 */
2405 (void) __dm_resume(md, NULL);
2406
2407done:
2408 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2409 smp_mb__after_atomic();
2410 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2411}
2412
2413void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002414{
2415 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002416 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2417 mutex_unlock(&md->suspend_lock);
2418}
2419EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2420
2421void dm_internal_resume(struct mapped_device *md)
2422{
2423 mutex_lock(&md->suspend_lock);
2424 __dm_internal_resume(md);
2425 mutex_unlock(&md->suspend_lock);
2426}
2427EXPORT_SYMBOL_GPL(dm_internal_resume);
2428
2429/*
2430 * Fast variants of internal suspend/resume hold md->suspend_lock,
2431 * which prevents interaction with userspace-driven suspend.
2432 */
2433
2434void dm_internal_suspend_fast(struct mapped_device *md)
2435{
2436 mutex_lock(&md->suspend_lock);
2437 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002438 return;
2439
2440 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2441 synchronize_srcu(&md->io_barrier);
2442 flush_workqueue(md->wq);
2443 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2444}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002445EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002446
Mike Snitzerffcc3932014-10-28 18:34:52 -04002447void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002448{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002449 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002450 goto done;
2451
2452 dm_queue_flush(md);
2453
2454done:
2455 mutex_unlock(&md->suspend_lock);
2456}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002457EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459/*-----------------------------------------------------------------
2460 * Event notification.
2461 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002462int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002463 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002464{
Milan Broz60935eb2009-06-22 10:12:30 +01002465 char udev_cookie[DM_COOKIE_LENGTH];
2466 char *envp[] = { udev_cookie, NULL };
2467
2468 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002469 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002470 else {
2471 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2472 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002473 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2474 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002475 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002476}
2477
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002478uint32_t dm_next_uevent_seq(struct mapped_device *md)
2479{
2480 return atomic_add_return(1, &md->uevent_seq);
2481}
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483uint32_t dm_get_event_nr(struct mapped_device *md)
2484{
2485 return atomic_read(&md->event_nr);
2486}
2487
2488int dm_wait_event(struct mapped_device *md, int event_nr)
2489{
2490 return wait_event_interruptible(md->eventq,
2491 (event_nr != atomic_read(&md->event_nr)));
2492}
2493
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002494void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2495{
2496 unsigned long flags;
2497
2498 spin_lock_irqsave(&md->uevent_lock, flags);
2499 list_add(elist, &md->uevent_list);
2500 spin_unlock_irqrestore(&md->uevent_lock, flags);
2501}
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503/*
2504 * The gendisk is only valid as long as you have a reference
2505 * count on 'md'.
2506 */
2507struct gendisk *dm_disk(struct mapped_device *md)
2508{
2509 return md->disk;
2510}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00002511EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Milan Broz784aae72009-01-06 03:05:12 +00002513struct kobject *dm_kobject(struct mapped_device *md)
2514{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002515 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00002516}
2517
Milan Broz784aae72009-01-06 03:05:12 +00002518struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2519{
2520 struct mapped_device *md;
2521
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002522 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00002523
Milan Broz4d89b7b2009-06-22 10:12:11 +01002524 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002525 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002526 return NULL;
2527
Milan Broz784aae72009-01-06 03:05:12 +00002528 dm_get(md);
2529 return md;
2530}
2531
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002532int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 return test_bit(DMF_SUSPENDED, &md->flags);
2535}
2536
Mike Snitzerffcc3932014-10-28 18:34:52 -04002537int dm_suspended_internally_md(struct mapped_device *md)
2538{
2539 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2540}
2541
Mikulas Patocka2c140a22013-11-01 18:27:41 -04002542int dm_test_deferred_remove_flag(struct mapped_device *md)
2543{
2544 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2545}
2546
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002547int dm_suspended(struct dm_target *ti)
2548{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002549 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002550}
2551EXPORT_SYMBOL_GPL(dm_suspended);
2552
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002553int dm_noflush_suspending(struct dm_target *ti)
2554{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002555 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002556}
2557EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2558
Mike Snitzer78d8e582015-06-26 10:01:13 -04002559struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
Mike Snitzer30187e12016-01-31 13:28:26 -05002560 unsigned integrity, unsigned per_io_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002561{
Mike Snitzer115485e2016-02-22 12:16:21 -05002562 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
Mike Snitzer78d8e582015-06-26 10:01:13 -04002563 struct kmem_cache *cachep = NULL;
2564 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002565 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002566
2567 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002568 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002569
Mike Snitzer78d8e582015-06-26 10:01:13 -04002570 switch (type) {
2571 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002572 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzer78d8e582015-06-26 10:01:13 -04002573 cachep = _io_cache;
2574 pool_size = dm_get_reserved_bio_based_ios();
Mike Snitzer30187e12016-01-31 13:28:26 -05002575 front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04002576 break;
2577 case DM_TYPE_REQUEST_BASED:
2578 cachep = _rq_tio_cache;
2579 pool_size = dm_get_reserved_rq_based_ios();
2580 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
2581 if (!pools->rq_pool)
2582 goto out;
2583 /* fall through to setup remaining rq-based pools */
2584 case DM_TYPE_MQ_REQUEST_BASED:
2585 if (!pool_size)
2586 pool_size = dm_get_reserved_rq_based_ios();
2587 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002588 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04002589 break;
2590 default:
2591 BUG();
2592 }
2593
2594 if (cachep) {
2595 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
2596 if (!pools->io_pool)
2597 goto out;
2598 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002599
Junichi Nomura3d8aab22014-10-03 11:55:26 +00002600 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002601 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002602 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002603
Martin K. Petersena91a2782011-03-17 11:11:05 +01002604 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002605 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002606
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002607 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002608
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002609out:
2610 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002611
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002612 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002613}
2614
2615void dm_free_md_mempools(struct dm_md_mempools *pools)
2616{
2617 if (!pools)
2618 return;
2619
Julia Lawall6f659852015-09-13 14:15:05 +02002620 mempool_destroy(pools->io_pool);
2621 mempool_destroy(pools->rq_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002622
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002623 if (pools->bs)
2624 bioset_free(pools->bs);
2625
2626 kfree(pools);
2627}
2628
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002629struct dm_pr {
2630 u64 old_key;
2631 u64 new_key;
2632 u32 flags;
2633 bool fail_early;
2634};
2635
2636static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
2637 void *data)
2638{
2639 struct mapped_device *md = bdev->bd_disk->private_data;
2640 struct dm_table *table;
2641 struct dm_target *ti;
2642 int ret = -ENOTTY, srcu_idx;
2643
2644 table = dm_get_live_table(md, &srcu_idx);
2645 if (!table || !dm_table_get_size(table))
2646 goto out;
2647
2648 /* We only support devices that have a single target */
2649 if (dm_table_get_num_targets(table) != 1)
2650 goto out;
2651 ti = dm_table_get_target(table, 0);
2652
2653 ret = -EINVAL;
2654 if (!ti->type->iterate_devices)
2655 goto out;
2656
2657 ret = ti->type->iterate_devices(ti, fn, data);
2658out:
2659 dm_put_live_table(md, srcu_idx);
2660 return ret;
2661}
2662
2663/*
2664 * For register / unregister we need to manually call out to every path.
2665 */
2666static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
2667 sector_t start, sector_t len, void *data)
2668{
2669 struct dm_pr *pr = data;
2670 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
2671
2672 if (!ops || !ops->pr_register)
2673 return -EOPNOTSUPP;
2674 return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
2675}
2676
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002677static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05002678 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002679{
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002680 struct dm_pr pr = {
2681 .old_key = old_key,
2682 .new_key = new_key,
2683 .flags = flags,
2684 .fail_early = true,
2685 };
2686 int ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002687
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002688 ret = dm_call_pr(bdev, __dm_pr_register, &pr);
2689 if (ret && new_key) {
2690 /* unregister all paths if we failed to register any path */
2691 pr.old_key = new_key;
2692 pr.new_key = 0;
2693 pr.flags = 0;
2694 pr.fail_early = false;
2695 dm_call_pr(bdev, __dm_pr_register, &pr);
2696 }
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002697
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002698 return ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002699}
2700
2701static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05002702 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002703{
2704 struct mapped_device *md = bdev->bd_disk->private_data;
2705 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002706 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002707 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002708
Mike Snitzer956a4022016-02-18 16:13:51 -05002709 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002710 if (r < 0)
2711 return r;
2712
2713 ops = bdev->bd_disk->fops->pr_ops;
2714 if (ops && ops->pr_reserve)
2715 r = ops->pr_reserve(bdev, key, type, flags);
2716 else
2717 r = -EOPNOTSUPP;
2718
Mike Snitzer956a4022016-02-18 16:13:51 -05002719 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002720 return r;
2721}
2722
2723static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
2724{
2725 struct mapped_device *md = bdev->bd_disk->private_data;
2726 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002727 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002728 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002729
Mike Snitzer956a4022016-02-18 16:13:51 -05002730 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002731 if (r < 0)
2732 return r;
2733
2734 ops = bdev->bd_disk->fops->pr_ops;
2735 if (ops && ops->pr_release)
2736 r = ops->pr_release(bdev, key, type);
2737 else
2738 r = -EOPNOTSUPP;
2739
Mike Snitzer956a4022016-02-18 16:13:51 -05002740 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002741 return r;
2742}
2743
2744static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05002745 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002746{
2747 struct mapped_device *md = bdev->bd_disk->private_data;
2748 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002749 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002750 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002751
Mike Snitzer956a4022016-02-18 16:13:51 -05002752 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002753 if (r < 0)
2754 return r;
2755
2756 ops = bdev->bd_disk->fops->pr_ops;
2757 if (ops && ops->pr_preempt)
2758 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
2759 else
2760 r = -EOPNOTSUPP;
2761
Mike Snitzer956a4022016-02-18 16:13:51 -05002762 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002763 return r;
2764}
2765
2766static int dm_pr_clear(struct block_device *bdev, u64 key)
2767{
2768 struct mapped_device *md = bdev->bd_disk->private_data;
2769 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002770 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002771 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002772
Mike Snitzer956a4022016-02-18 16:13:51 -05002773 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002774 if (r < 0)
2775 return r;
2776
2777 ops = bdev->bd_disk->fops->pr_ops;
2778 if (ops && ops->pr_clear)
2779 r = ops->pr_clear(bdev, key);
2780 else
2781 r = -EOPNOTSUPP;
2782
Mike Snitzer956a4022016-02-18 16:13:51 -05002783 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002784 return r;
2785}
2786
2787static const struct pr_ops dm_pr_ops = {
2788 .pr_register = dm_pr_register,
2789 .pr_reserve = dm_pr_reserve,
2790 .pr_release = dm_pr_release,
2791 .pr_preempt = dm_pr_preempt,
2792 .pr_clear = dm_pr_clear,
2793};
2794
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002795static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 .open = dm_blk_open,
2797 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002798 .ioctl = dm_blk_ioctl,
Toshi Kani545ed202016-06-22 17:54:53 -06002799 .direct_access = dm_blk_direct_access,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002800 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002801 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 .owner = THIS_MODULE
2803};
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805/*
2806 * module hooks
2807 */
2808module_init(dm_init);
2809module_exit(dm_exit);
2810
2811module_param(major, uint, 0);
2812MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04002813
Mike Snitzere8603132013-09-12 18:06:12 -04002814module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
2815MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
2816
Mike Snitzer115485e2016-02-22 12:16:21 -05002817module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
2818MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820MODULE_DESCRIPTION(DM_NAME " driver");
2821MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2822MODULE_LICENSE("GPL");