blob: 2edbcc2d7d3f6274b689447859470d2773ea4e3b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
Mike Snitzer4cc96132016-05-12 16:28:10 -04008#include "dm-core.h"
9#include "dm-rq.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +010010#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <linux/init.h>
13#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080014#include <linux/mutex.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/blkpg.h>
17#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mempool.h>
Dan Williamsf26c5712017-04-12 12:35:44 -070019#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/idr.h>
Dan Williams7e026c82017-05-29 12:57:56 -070022#include <linux/uio.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080023#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010024#include <linux/delay.h>
Mike Snitzerffcc3932014-10-28 18:34:52 -040025#include <linux/wait.h>
Christoph Hellwig71cdb692015-10-15 14:10:51 +020026#include <linux/pr.h>
Li Zefan55782132009-06-09 13:43:05 +080027
Alasdair G Kergon72d94862006-06-26 00:27:35 -070028#define DM_MSG_PREFIX "core"
29
Namhyung Kim71a16732011-10-31 20:18:54 +000030#ifdef CONFIG_PRINTK
31/*
32 * ratelimit state to be used in DMXXX_LIMIT().
33 */
34DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
35 DEFAULT_RATELIMIT_INTERVAL,
36 DEFAULT_RATELIMIT_BURST);
37EXPORT_SYMBOL(dm_ratelimit_state);
38#endif
39
Milan Broz60935eb2009-06-22 10:12:30 +010040/*
41 * Cookies are numeric values sent with CHANGE and REMOVE
42 * uevents while resuming, removing or renaming the device.
43 */
44#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
45#define DM_COOKIE_LENGTH 24
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static const char *_name = DM_NAME;
48
49static unsigned int major = 0;
50static unsigned int _major = 0;
51
Alasdair G Kergond15b7742011-08-02 12:32:01 +010052static DEFINE_IDR(_minor_idr);
53
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070054static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040055
56static void do_deferred_remove(struct work_struct *w);
57
58static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
59
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040060static struct workqueue_struct *deferred_remove_workqueue;
61
Mikulas Patocka93e64422017-01-16 16:05:59 -050062atomic_t dm_global_event_nr = ATOMIC_INIT(0);
63DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
66 * One of these is allocated per bio.
67 */
68struct dm_io {
69 struct mapped_device *md;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020070 blk_status_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010072 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080073 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010074 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040075 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070078#define MINOR_ALLOCED ((void *)-1)
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * Bits for the md->flags field.
82 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +010083#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080085#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -070086#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070087#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080088#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -070089#define DMF_DEFERRED_REMOVE 6
90#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Mike Snitzer115485e2016-02-22 12:16:21 -050092#define DM_NUMA_NODE NUMA_NO_NODE
Mike Snitzer115485e2016-02-22 12:16:21 -050093static int dm_numa_node = DM_NUMA_NODE;
Mike Snitzerfaad87d2016-01-28 16:52:56 -050094
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +010095/*
96 * For mempools pre-allocation at the table loading time.
97 */
98struct dm_md_mempools {
99 mempool_t *io_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100100 struct bio_set *bs;
101};
102
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500103struct table_device {
104 struct list_head list;
105 atomic_t count;
106 struct dm_dev dm_dev;
107};
108
Christoph Lametere18b8902006-12-06 20:33:20 -0800109static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000110static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500111static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700112
Mike Snitzerf4790822013-09-12 18:06:12 -0400113/*
Mike Snitzere8603132013-09-12 18:06:12 -0400114 * Bio-based DM's mempools' reserved IOs set by the user.
115 */
Mike Snitzer4cc96132016-05-12 16:28:10 -0400116#define RESERVED_BIO_BASED_IOS 16
Mike Snitzere8603132013-09-12 18:06:12 -0400117static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
118
Mike Snitzer115485e2016-02-22 12:16:21 -0500119static int __dm_get_module_param_int(int *module_param, int min, int max)
120{
121 int param = ACCESS_ONCE(*module_param);
122 int modified_param = 0;
123 bool modified = true;
124
125 if (param < min)
126 modified_param = min;
127 else if (param > max)
128 modified_param = max;
129 else
130 modified = false;
131
132 if (modified) {
133 (void)cmpxchg(module_param, param, modified_param);
134 param = modified_param;
135 }
136
137 return param;
138}
139
Mike Snitzer4cc96132016-05-12 16:28:10 -0400140unsigned __dm_get_module_param(unsigned *module_param,
141 unsigned def, unsigned max)
Mike Snitzerf4790822013-09-12 18:06:12 -0400142{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500143 unsigned param = ACCESS_ONCE(*module_param);
144 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400145
Mike Snitzer09c2d532015-02-27 22:25:26 -0500146 if (!param)
147 modified_param = def;
148 else if (param > max)
149 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400150
Mike Snitzer09c2d532015-02-27 22:25:26 -0500151 if (modified_param) {
152 (void)cmpxchg(module_param, param, modified_param);
153 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400154 }
155
Mike Snitzer09c2d532015-02-27 22:25:26 -0500156 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400157}
158
Mike Snitzere8603132013-09-12 18:06:12 -0400159unsigned dm_get_reserved_bio_based_ios(void)
160{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500161 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzer4cc96132016-05-12 16:28:10 -0400162 RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
Mike Snitzere8603132013-09-12 18:06:12 -0400163}
164EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
165
Mike Snitzer115485e2016-02-22 12:16:21 -0500166static unsigned dm_get_numa_node(void)
167{
168 return __dm_get_module_param_int(&dm_numa_node,
169 DM_NUMA_NODE, num_online_nodes() - 1);
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int __init local_init(void)
173{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100174 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100177 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100179 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000181 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
182 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100183 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000184
Mike Snitzereca7ee62016-02-20 13:45:38 -0500185 _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500186 __alignof__(struct request), 0, NULL);
187 if (!_rq_cache)
188 goto out_free_rq_tio_cache;
189
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100190 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100191 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500192 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100193
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400194 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
195 if (!deferred_remove_workqueue) {
196 r = -ENOMEM;
197 goto out_uevent_exit;
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 _major = major;
201 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100202 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400203 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (!_major)
206 _major = r;
207
208 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100209
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400210out_free_workqueue:
211 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100212out_uevent_exit:
213 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500214out_free_rq_cache:
215 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000216out_free_rq_tio_cache:
217 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100218out_free_io_cache:
219 kmem_cache_destroy(_io_cache);
220
221 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224static void local_exit(void)
225{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400226 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400227 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400228
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500229 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000230 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700232 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100233 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 _major = 0;
236
237 DMINFO("cleaned up");
238}
239
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000240static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 local_init,
242 dm_target_init,
243 dm_linear_init,
244 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000245 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100246 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400248 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000251static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 local_exit,
253 dm_target_exit,
254 dm_linear_exit,
255 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000256 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100257 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400259 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
262static int __init dm_init(void)
263{
264 const int count = ARRAY_SIZE(_inits);
265
266 int r, i;
267
268 for (i = 0; i < count; i++) {
269 r = _inits[i]();
270 if (r)
271 goto bad;
272 }
273
274 return 0;
275
276 bad:
277 while (i--)
278 _exits[i]();
279
280 return r;
281}
282
283static void __exit dm_exit(void)
284{
285 int i = ARRAY_SIZE(_exits);
286
287 while (i--)
288 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100289
290 /*
291 * Should be empty by this point.
292 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100293 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/*
297 * Block device functions
298 */
Mike Anderson432a2122009-12-10 23:52:20 +0000299int dm_deleting_md(struct mapped_device *md)
300{
301 return test_bit(DMF_DELETING, &md->flags);
302}
303
Al Virofe5f9f22008-03-02 10:29:31 -0500304static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 struct mapped_device *md;
307
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700308 spin_lock(&_minor_lock);
309
Al Virofe5f9f22008-03-02 10:29:31 -0500310 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700311 if (!md)
312 goto out;
313
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700314 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000315 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700316 md = NULL;
317 goto out;
318 }
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700321 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700322out:
323 spin_unlock(&_minor_lock);
324
325 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Al Virodb2a1442013-05-05 21:52:57 -0400328static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400330 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200331
Milan Broz4a1aeb92011-01-13 19:59:48 +0000332 spin_lock(&_minor_lock);
333
Mike Snitzer63a4f062015-03-23 17:01:43 -0400334 md = disk->private_data;
335 if (WARN_ON(!md))
336 goto out;
337
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400338 if (atomic_dec_and_test(&md->open_count) &&
339 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400340 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400343out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000344 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700347int dm_open_count(struct mapped_device *md)
348{
349 return atomic_read(&md->open_count);
350}
351
352/*
353 * Guarantees nothing is using the device before it's deleted.
354 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400355int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700356{
357 int r = 0;
358
359 spin_lock(&_minor_lock);
360
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400361 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700362 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400363 if (mark_deferred)
364 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
365 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
366 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700367 else
368 set_bit(DMF_DELETING, &md->flags);
369
370 spin_unlock(&_minor_lock);
371
372 return r;
373}
374
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400375int dm_cancel_deferred_remove(struct mapped_device *md)
376{
377 int r = 0;
378
379 spin_lock(&_minor_lock);
380
381 if (test_bit(DMF_DELETING, &md->flags))
382 r = -EBUSY;
383 else
384 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
385
386 spin_unlock(&_minor_lock);
387
388 return r;
389}
390
391static void do_deferred_remove(struct work_struct *w)
392{
393 dm_deferred_remove();
394}
395
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400396sector_t dm_get_size(struct mapped_device *md)
397{
398 return get_capacity(md->disk);
399}
400
Mike Snitzer9974fa22014-02-28 15:33:43 +0100401struct request_queue *dm_get_md_queue(struct mapped_device *md)
402{
403 return md->queue;
404}
405
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400406struct dm_stats *dm_get_stats(struct mapped_device *md)
407{
408 return &md->stats;
409}
410
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800411static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
412{
413 struct mapped_device *md = bdev->bd_disk->private_data;
414
415 return dm_get_geometry(md, geo);
416}
417
Mike Snitzer956a4022016-02-18 16:13:51 -0500418static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
419 struct block_device **bdev,
420 fmode_t *mode)
Milan Brozaa129a22006-10-03 01:15:15 -0700421{
Mike Snitzer66482022016-02-18 15:44:39 -0500422 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100423 struct dm_table *map;
Mike Snitzer956a4022016-02-18 16:13:51 -0500424 int srcu_idx, r;
Milan Brozaa129a22006-10-03 01:15:15 -0700425
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100426retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200427 r = -ENOTTY;
Mike Snitzer956a4022016-02-18 16:13:51 -0500428 map = dm_get_live_table(md, &srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700429 if (!map || !dm_table_get_size(map))
430 goto out;
431
432 /* We only support devices that have a single target */
433 if (dm_table_get_num_targets(map) != 1)
434 goto out;
435
Mike Snitzer66482022016-02-18 15:44:39 -0500436 tgt = dm_table_get_target(map, 0);
437 if (!tgt->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500438 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700439
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000440 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700441 r = -EAGAIN;
442 goto out;
443 }
444
Mike Snitzer66482022016-02-18 15:44:39 -0500445 r = tgt->type->prepare_ioctl(tgt, bdev, mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200446 if (r < 0)
447 goto out;
448
Mike Snitzer956a4022016-02-18 16:13:51 -0500449 bdgrab(*bdev);
450 dm_put_live_table(md, srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200451 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700452
453out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500454 dm_put_live_table(md, srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000455 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100456 msleep(10);
457 goto retry;
458 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200459 return r;
460}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100461
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200462static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
463 unsigned int cmd, unsigned long arg)
464{
465 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer956a4022016-02-18 16:13:51 -0500466 int r;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200467
Mike Snitzer956a4022016-02-18 16:13:51 -0500468 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200469 if (r < 0)
470 return r;
471
472 if (r > 0) {
473 /*
Christoph Hellwige980f622017-02-04 10:45:03 +0100474 * Target determined this ioctl is being issued against a
475 * subset of the parent bdev; require extra privileges.
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200476 */
Christoph Hellwige980f622017-02-04 10:45:03 +0100477 if (!capable(CAP_SYS_RAWIO)) {
478 DMWARN_LIMIT(
479 "%s: sending ioctl %x to DM device without required privilege.",
480 current->comm, cmd);
481 r = -ENOIOCTLCMD;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200482 goto out;
Christoph Hellwige980f622017-02-04 10:45:03 +0100483 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200484 }
485
Mike Snitzer66482022016-02-18 15:44:39 -0500486 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200487out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500488 bdput(bdev);
Milan Brozaa129a22006-10-03 01:15:15 -0700489 return r;
490}
491
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100492static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 return mempool_alloc(md->io_pool, GFP_NOIO);
495}
496
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100497static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 mempool_free(io, md->io_pool);
500}
501
Mike Snitzercfae7522016-04-11 12:05:38 -0400502static void free_tio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Mikulas Patockadba14162012-10-12 21:02:15 +0100504 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Mike Snitzer4cc96132016-05-12 16:28:10 -0400507int md_in_flight(struct mapped_device *md)
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000508{
509 return atomic_read(&md->pending[READ]) +
510 atomic_read(&md->pending[WRITE]);
511}
512
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800513static void start_io_acct(struct dm_io *io)
514{
515 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400516 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900517 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400518 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800519
520 io->start_time = jiffies;
521
Tejun Heo074a7ac2008-08-25 19:56:14 +0900522 cpu = part_stat_lock();
523 part_round_stats(cpu, &dm_disk(md)->part0);
524 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100525 atomic_set(&dm_disk(md)->part0.in_flight[rw],
526 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400527
528 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500529 dm_stats_account_io(&md->stats, bio_data_dir(bio),
530 bio->bi_iter.bi_sector, bio_sectors(bio),
531 false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800532}
533
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000534static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800535{
536 struct mapped_device *md = io->md;
537 struct bio *bio = io->bio;
538 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800539 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800540 int rw = bio_data_dir(bio);
541
Gu Zheng18c0b222014-11-24 11:05:26 +0800542 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800543
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400544 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500545 dm_stats_account_io(&md->stats, bio_data_dir(bio),
546 bio->bi_iter.bi_sector, bio_sectors(bio),
547 true, duration, &io->stats_aux);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400548
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100549 /*
550 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200551 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100552 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100553 pending = atomic_dec_return(&md->pending[rw]);
554 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200555 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800556
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000557 /* nudge anyone waiting on suspend queue */
558 if (!pending)
559 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*
563 * Add the bio to the list of deferred io.
564 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100565static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200567 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200569 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200571 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200572 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575/*
576 * Everyone (including functions in this file), should use this
577 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100578 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100580struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100582 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100584 return srcu_dereference(md->map, &md->io_barrier);
585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100587void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
588{
589 srcu_read_unlock(&md->io_barrier, srcu_idx);
590}
591
592void dm_sync_table(struct mapped_device *md)
593{
594 synchronize_srcu(&md->io_barrier);
595 synchronize_rcu_expedited();
596}
597
598/*
599 * A fast alternative to dm_get_live_table/dm_put_live_table.
600 * The caller must not block between these two functions.
601 */
602static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
603{
604 rcu_read_lock();
605 return rcu_dereference(md->map);
606}
607
608static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
609{
610 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800613/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500614 * Open a table device so we can use it as a map destination.
615 */
616static int open_table_device(struct table_device *td, dev_t dev,
617 struct mapped_device *md)
618{
619 static char *_claim_ptr = "I belong to device-mapper";
620 struct block_device *bdev;
621
622 int r;
623
624 BUG_ON(td->dm_dev.bdev);
625
626 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
627 if (IS_ERR(bdev))
628 return PTR_ERR(bdev);
629
630 r = bd_link_disk_holder(bdev, dm_disk(md));
631 if (r) {
632 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
633 return r;
634 }
635
636 td->dm_dev.bdev = bdev;
Dan Williams817bf402017-04-12 13:37:44 -0700637 td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500638 return 0;
639}
640
641/*
642 * Close a table device that we've been using.
643 */
644static void close_table_device(struct table_device *td, struct mapped_device *md)
645{
646 if (!td->dm_dev.bdev)
647 return;
648
649 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
650 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
Dan Williams817bf402017-04-12 13:37:44 -0700651 put_dax(td->dm_dev.dax_dev);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500652 td->dm_dev.bdev = NULL;
Dan Williams817bf402017-04-12 13:37:44 -0700653 td->dm_dev.dax_dev = NULL;
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500654}
655
656static struct table_device *find_table_device(struct list_head *l, dev_t dev,
657 fmode_t mode) {
658 struct table_device *td;
659
660 list_for_each_entry(td, l, list)
661 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
662 return td;
663
664 return NULL;
665}
666
667int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
668 struct dm_dev **result) {
669 int r;
670 struct table_device *td;
671
672 mutex_lock(&md->table_devices_lock);
673 td = find_table_device(&md->table_devices, dev, mode);
674 if (!td) {
Mike Snitzer115485e2016-02-22 12:16:21 -0500675 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500676 if (!td) {
677 mutex_unlock(&md->table_devices_lock);
678 return -ENOMEM;
679 }
680
681 td->dm_dev.mode = mode;
682 td->dm_dev.bdev = NULL;
683
684 if ((r = open_table_device(td, dev, md))) {
685 mutex_unlock(&md->table_devices_lock);
686 kfree(td);
687 return r;
688 }
689
690 format_dev_t(td->dm_dev.name, dev);
691
692 atomic_set(&td->count, 0);
693 list_add(&td->list, &md->table_devices);
694 }
695 atomic_inc(&td->count);
696 mutex_unlock(&md->table_devices_lock);
697
698 *result = &td->dm_dev;
699 return 0;
700}
701EXPORT_SYMBOL_GPL(dm_get_table_device);
702
703void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
704{
705 struct table_device *td = container_of(d, struct table_device, dm_dev);
706
707 mutex_lock(&md->table_devices_lock);
708 if (atomic_dec_and_test(&td->count)) {
709 close_table_device(td, md);
710 list_del(&td->list);
711 kfree(td);
712 }
713 mutex_unlock(&md->table_devices_lock);
714}
715EXPORT_SYMBOL(dm_put_table_device);
716
717static void free_table_devices(struct list_head *devices)
718{
719 struct list_head *tmp, *next;
720
721 list_for_each_safe(tmp, next, devices) {
722 struct table_device *td = list_entry(tmp, struct table_device, list);
723
724 DMWARN("dm_destroy: %s still exists with %d references",
725 td->dm_dev.name, atomic_read(&td->count));
726 kfree(td);
727 }
728}
729
730/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800731 * Get the geometry associated with a dm device
732 */
733int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
734{
735 *geo = md->geometry;
736
737 return 0;
738}
739
740/*
741 * Set the geometry of a device.
742 */
743int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
744{
745 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
746
747 if (geo->start > sz) {
748 DMWARN("Start sector is beyond the geometry limits.");
749 return -EINVAL;
750 }
751
752 md->geometry = *geo;
753
754 return 0;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*-----------------------------------------------------------------
758 * CRUD START:
759 * A more elegant soln is in the works that uses the queue
760 * merge fn, unfortunately there are a couple of changes to
761 * the block layer that I want to make for this. So in the
762 * interests of getting something for people to use I give
763 * you this clearly demarcated crap.
764 *---------------------------------------------------------------*/
765
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800766static int __noflush_suspending(struct mapped_device *md)
767{
768 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/*
772 * Decrements the number of outstanding ios that a bio has been
773 * cloned into, completing the original io if necc.
774 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200775static void dec_pending(struct dm_io *io, blk_status_t error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800777 unsigned long flags;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200778 blk_status_t io_error;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000779 struct bio *bio;
780 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800781
782 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100783 if (unlikely(error)) {
784 spin_lock_irqsave(&io->endio_lock, flags);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200785 if (!(io->status == BLK_STS_DM_REQUEUE &&
786 __noflush_suspending(md)))
787 io->status = error;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100788 spin_unlock_irqrestore(&io->endio_lock, flags);
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (atomic_dec_and_test(&io->io_count)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200792 if (io->status == BLK_STS_DM_REQUEUE) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800793 /*
794 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800795 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100796 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200797 if (__noflush_suspending(md))
798 bio_list_add_head(&md->deferred, io->bio);
799 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800800 /* noflush suspend was interrupted. */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200801 io->status = BLK_STS_IOERR;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100802 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800803 }
804
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200805 io_error = io->status;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000806 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200807 end_io_acct(io);
808 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100809
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200810 if (io_error == BLK_STS_DM_REQUEUE)
Tejun Heo6a8736d2010-09-08 18:07:00 +0200811 return;
812
Jens Axboe1eff9d32016-08-05 15:35:16 -0600813 if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100814 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200815 * Preflush done for flush with data, reissue
Mike Christie28a8f0d2016-06-05 14:32:25 -0500816 * without REQ_PREFLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100817 */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600818 bio->bi_opf &= ~REQ_PREFLUSH;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200819 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100820 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200821 /* done with normal IO or empty flush */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200822 bio->bi_status = io_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200823 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826}
827
Mike Snitzer4cc96132016-05-12 16:28:10 -0400828void disable_write_same(struct mapped_device *md)
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400829{
830 struct queue_limits *limits = dm_get_queue_limits(md);
831
832 /* device doesn't really support WRITE SAME, disable it */
833 limits->max_write_same_sectors = 0;
834}
835
Christoph Hellwigac62d622017-04-05 19:21:05 +0200836void disable_write_zeroes(struct mapped_device *md)
837{
838 struct queue_limits *limits = dm_get_queue_limits(md);
839
840 /* device doesn't really support WRITE ZEROES, disable it */
841 limits->max_write_zeroes_sectors = 0;
842}
843
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200844static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200846 blk_status_t error = bio->bi_status;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500847 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000848 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700849 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 dm_endio_fn endio = tio->ti->type->end_io;
851
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200852 if (unlikely(error == BLK_STS_TARGET)) {
Christoph Hellwigac62d622017-04-05 19:21:05 +0200853 if (bio_op(bio) == REQ_OP_WRITE_SAME &&
854 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors)
855 disable_write_same(md);
856 if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
857 !bdev_get_queue(bio->bi_bdev)->limits.max_write_zeroes_sectors)
858 disable_write_zeroes(md);
859 }
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400860
Christoph Hellwig1be56902017-06-03 09:38:03 +0200861 if (endio) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200862 int r = endio(tio->ti, bio, &error);
Christoph Hellwig1be56902017-06-03 09:38:03 +0200863 switch (r) {
864 case DM_ENDIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200865 error = BLK_STS_DM_REQUEUE;
Christoph Hellwig1be56902017-06-03 09:38:03 +0200866 /*FALLTHRU*/
867 case DM_ENDIO_DONE:
868 break;
869 case DM_ENDIO_INCOMPLETE:
870 /* The target will handle the io */
871 return;
872 default:
873 DMWARN("unimplemented target endio return value: %d", r);
874 BUG();
875 }
876 }
877
Mike Snitzercfae7522016-04-11 12:05:38 -0400878 free_tio(tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000879 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Mike Snitzer78d8e582015-06-26 10:01:13 -0400882/*
Mike Snitzer56a67df2010-08-12 04:14:10 +0100883 * Return maximum size of I/O possible at the supplied sector up to the current
884 * target boundary.
885 */
886static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100888 sector_t target_offset = dm_target_offset(ti, sector);
889
890 return ti->len - target_offset;
891}
892
893static sector_t max_io_len(sector_t sector, struct dm_target *ti)
894{
895 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +0100896 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /*
Mike Snitzer542f9032012-07-27 15:08:00 +0100899 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100901 if (ti->max_io_len) {
902 offset = dm_target_offset(ti, sector);
903 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
904 max_len = sector_div(offset, ti->max_io_len);
905 else
906 max_len = offset & (ti->max_io_len - 1);
907 max_len = ti->max_io_len - max_len;
908
909 if (len > max_len)
910 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
913 return len;
914}
915
Mike Snitzer542f9032012-07-27 15:08:00 +0100916int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
917{
918 if (len > UINT_MAX) {
919 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
920 (unsigned long long)len, UINT_MAX);
921 ti->error = "Maximum size of target IO is too large";
922 return -EINVAL;
923 }
924
925 ti->max_io_len = (uint32_t) len;
926
927 return 0;
928}
929EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
930
Dan Williamsf26c5712017-04-12 12:35:44 -0700931static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
932 sector_t sector, int *srcu_idx)
Toshi Kani545ed202016-06-22 17:54:53 -0600933{
Toshi Kani545ed202016-06-22 17:54:53 -0600934 struct dm_table *map;
935 struct dm_target *ti;
Toshi Kani545ed202016-06-22 17:54:53 -0600936
Dan Williamsf26c5712017-04-12 12:35:44 -0700937 map = dm_get_live_table(md, srcu_idx);
Toshi Kani545ed202016-06-22 17:54:53 -0600938 if (!map)
Dan Williamsf26c5712017-04-12 12:35:44 -0700939 return NULL;
Toshi Kani545ed202016-06-22 17:54:53 -0600940
941 ti = dm_table_find_target(map, sector);
942 if (!dm_target_is_valid(ti))
Dan Williamsf26c5712017-04-12 12:35:44 -0700943 return NULL;
944
945 return ti;
946}
947
948static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
949 long nr_pages, void **kaddr, pfn_t *pfn)
950{
951 struct mapped_device *md = dax_get_private(dax_dev);
952 sector_t sector = pgoff * PAGE_SECTORS;
953 struct dm_target *ti;
954 long len, ret = -EIO;
955 int srcu_idx;
956
957 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
958
959 if (!ti)
Toshi Kani545ed202016-06-22 17:54:53 -0600960 goto out;
Dan Williamsf26c5712017-04-12 12:35:44 -0700961 if (!ti->type->direct_access)
962 goto out;
963 len = max_io_len(sector, ti) / PAGE_SECTORS;
964 if (len < 1)
965 goto out;
966 nr_pages = min(len, nr_pages);
Toshi Kani545ed202016-06-22 17:54:53 -0600967 if (ti->type->direct_access)
Dan Williams817bf402017-04-12 13:37:44 -0700968 ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
969
Dan Williamsf26c5712017-04-12 12:35:44 -0700970 out:
Toshi Kani545ed202016-06-22 17:54:53 -0600971 dm_put_live_table(md, srcu_idx);
Dan Williamsf26c5712017-04-12 12:35:44 -0700972
973 return ret;
Toshi Kani545ed202016-06-22 17:54:53 -0600974}
975
Dan Williams7e026c82017-05-29 12:57:56 -0700976static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
977 void *addr, size_t bytes, struct iov_iter *i)
978{
979 struct mapped_device *md = dax_get_private(dax_dev);
980 sector_t sector = pgoff * PAGE_SECTORS;
981 struct dm_target *ti;
982 long ret = 0;
983 int srcu_idx;
984
985 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
986
987 if (!ti)
988 goto out;
989 if (!ti->type->dax_copy_from_iter) {
990 ret = copy_from_iter(addr, bytes, i);
991 goto out;
992 }
993 ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
994 out:
995 dm_put_live_table(md, srcu_idx);
996
997 return ret;
998}
999
Dan Williamsabebfbe2017-05-29 13:02:52 -07001000static void dm_dax_flush(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
1001 size_t size)
1002{
1003 struct mapped_device *md = dax_get_private(dax_dev);
1004 sector_t sector = pgoff * PAGE_SECTORS;
1005 struct dm_target *ti;
1006 int srcu_idx;
1007
1008 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1009
1010 if (!ti)
1011 goto out;
1012 if (ti->type->dax_flush)
1013 ti->type->dax_flush(ti, pgoff, addr, size);
1014 out:
1015 dm_put_live_table(md, srcu_idx);
1016}
1017
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001018/*
1019 * A target may call dm_accept_partial_bio only from the map routine. It is
Mike Christie28a8f0d2016-06-05 14:32:25 -05001020 * allowed for all bio types except REQ_PREFLUSH.
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001021 *
1022 * dm_accept_partial_bio informs the dm that the target only wants to process
1023 * additional n_sectors sectors of the bio and the rest of the data should be
1024 * sent in a next bio.
1025 *
1026 * A diagram that explains the arithmetics:
1027 * +--------------------+---------------+-------+
1028 * | 1 | 2 | 3 |
1029 * +--------------------+---------------+-------+
1030 *
1031 * <-------------- *tio->len_ptr --------------->
1032 * <------- bi_size ------->
1033 * <-- n_sectors -->
1034 *
1035 * Region 1 was already iterated over with bio_advance or similar function.
1036 * (it may be empty if the target doesn't use bio_advance)
1037 * Region 2 is the remaining bio size that the target wants to process.
1038 * (it may be empty if region 1 is non-empty, although there is no reason
1039 * to make it empty)
1040 * The target requires that region 3 is to be sent in the next bio.
1041 *
1042 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1043 * the partially processed part (the sum of regions 1+2) must be the same for all
1044 * copies of the bio.
1045 */
1046void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1047{
1048 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1049 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Jens Axboe1eff9d32016-08-05 15:35:16 -06001050 BUG_ON(bio->bi_opf & REQ_PREFLUSH);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001051 BUG_ON(bi_size > *tio->len_ptr);
1052 BUG_ON(n_sectors > bi_size);
1053 *tio->len_ptr -= bi_size - n_sectors;
1054 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1055}
1056EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1057
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001058/*
Damien Le Moal10999302017-05-08 16:40:48 -07001059 * The zone descriptors obtained with a zone report indicate
1060 * zone positions within the target device. The zone descriptors
1061 * must be remapped to match their position within the dm device.
1062 * A target may call dm_remap_zone_report after completion of a
1063 * REQ_OP_ZONE_REPORT bio to remap the zone descriptors obtained
1064 * from the target device mapping to the dm device.
1065 */
1066void dm_remap_zone_report(struct dm_target *ti, struct bio *bio, sector_t start)
1067{
1068#ifdef CONFIG_BLK_DEV_ZONED
1069 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1070 struct bio *report_bio = tio->io->bio;
1071 struct blk_zone_report_hdr *hdr = NULL;
1072 struct blk_zone *zone;
1073 unsigned int nr_rep = 0;
1074 unsigned int ofst;
1075 struct bio_vec bvec;
1076 struct bvec_iter iter;
1077 void *addr;
1078
1079 if (bio->bi_status)
1080 return;
1081
1082 /*
1083 * Remap the start sector of the reported zones. For sequential zones,
1084 * also remap the write pointer position.
1085 */
1086 bio_for_each_segment(bvec, report_bio, iter) {
1087 addr = kmap_atomic(bvec.bv_page);
1088
1089 /* Remember the report header in the first page */
1090 if (!hdr) {
1091 hdr = addr;
1092 ofst = sizeof(struct blk_zone_report_hdr);
1093 } else
1094 ofst = 0;
1095
1096 /* Set zones start sector */
1097 while (hdr->nr_zones && ofst < bvec.bv_len) {
1098 zone = addr + ofst;
1099 if (zone->start >= start + ti->len) {
1100 hdr->nr_zones = 0;
1101 break;
1102 }
1103 zone->start = zone->start + ti->begin - start;
1104 if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
1105 if (zone->cond == BLK_ZONE_COND_FULL)
1106 zone->wp = zone->start + zone->len;
1107 else if (zone->cond == BLK_ZONE_COND_EMPTY)
1108 zone->wp = zone->start;
1109 else
1110 zone->wp = zone->wp + ti->begin - start;
1111 }
1112 ofst += sizeof(struct blk_zone);
1113 hdr->nr_zones--;
1114 nr_rep++;
1115 }
1116
1117 if (addr != hdr)
1118 kunmap_atomic(addr);
1119
1120 if (!hdr->nr_zones)
1121 break;
1122 }
1123
1124 if (hdr) {
1125 hdr->nr_zones = nr_rep;
1126 kunmap_atomic(hdr);
1127 }
1128
1129 bio_advance(report_bio, report_bio->bi_iter.bi_size);
1130
1131#else /* !CONFIG_BLK_DEV_ZONED */
1132 bio->bi_status = BLK_STS_NOTSUPP;
1133#endif
1134}
1135EXPORT_SYMBOL_GPL(dm_remap_zone_report);
1136
1137/*
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001138 * Flush current->bio_list when the target map method blocks.
1139 * This fixes deadlocks in snapshot and possibly in other targets.
1140 */
1141struct dm_offload {
1142 struct blk_plug plug;
1143 struct blk_plug_cb cb;
1144};
1145
1146static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
1147{
1148 struct dm_offload *o = container_of(cb, struct dm_offload, cb);
1149 struct bio_list list;
1150 struct bio *bio;
NeilBrownf5fe1b52017-03-10 17:00:47 +11001151 int i;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001152
1153 INIT_LIST_HEAD(&o->cb.list);
1154
1155 if (unlikely(!current->bio_list))
1156 return;
1157
NeilBrownf5fe1b52017-03-10 17:00:47 +11001158 for (i = 0; i < 2; i++) {
1159 list = current->bio_list[i];
1160 bio_list_init(&current->bio_list[i]);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001161
NeilBrownf5fe1b52017-03-10 17:00:47 +11001162 while ((bio = bio_list_pop(&list))) {
1163 struct bio_set *bs = bio->bi_pool;
NeilBrown47e0fb42017-06-18 14:38:57 +10001164 if (unlikely(!bs) || bs == fs_bio_set ||
1165 !bs->rescue_workqueue) {
NeilBrownf5fe1b52017-03-10 17:00:47 +11001166 bio_list_add(&current->bio_list[i], bio);
1167 continue;
1168 }
1169
1170 spin_lock(&bs->rescue_lock);
1171 bio_list_add(&bs->rescue_list, bio);
1172 queue_work(bs->rescue_workqueue, &bs->rescue_work);
1173 spin_unlock(&bs->rescue_lock);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001174 }
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001175 }
1176}
1177
1178static void dm_offload_start(struct dm_offload *o)
1179{
1180 blk_start_plug(&o->plug);
1181 o->cb.callback = flush_current_bio_list;
1182 list_add(&o->cb.list, &current->plug->cb_list);
1183}
1184
1185static void dm_offload_end(struct dm_offload *o)
1186{
1187 list_del(&o->cb.list);
1188 blk_finish_plug(&o->plug);
1189}
1190
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001191static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001194 sector_t sector;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001195 struct dm_offload o;
Mikulas Patockadba14162012-10-12 21:02:15 +01001196 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001197 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /*
1202 * Map the clone. If r == 0 we don't need to do
1203 * anything, the target has assumed ownership of
1204 * this io.
1205 */
1206 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001207 sector = clone->bi_iter.bi_sector;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001208
1209 dm_offload_start(&o);
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001210 r = ti->type->map(ti, clone);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001211 dm_offload_end(&o);
1212
Christoph Hellwig846785e2017-06-03 09:38:02 +02001213 switch (r) {
1214 case DM_MAPIO_SUBMITTED:
1215 break;
1216 case DM_MAPIO_REMAPPED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* the bio has been remapped so dispatch it */
Mike Snitzerd07335e2010-11-16 12:52:38 +01001218 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1219 tio->io->bio->bi_bdev->bd_dev, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 generic_make_request(clone);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001221 break;
1222 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001223 dec_pending(tio->io, BLK_STS_IOERR);
1224 free_tio(tio);
1225 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +02001226 case DM_MAPIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001227 dec_pending(tio->io, BLK_STS_DM_REQUEUE);
Mike Snitzercfae7522016-04-11 12:05:38 -04001228 free_tio(tio);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001229 break;
1230 default:
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001231 DMWARN("unimplemented target map return value: %d", r);
1232 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234}
1235
1236struct clone_info {
1237 struct mapped_device *md;
1238 struct dm_table *map;
1239 struct bio *bio;
1240 struct dm_io *io;
1241 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001242 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243};
1244
Mikulas Patockae0d66092014-03-14 18:40:39 -04001245static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001246{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001247 bio->bi_iter.bi_sector = sector;
1248 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*
1252 * Creates a bio that consists of range of complete bvecs.
1253 */
Mike Snitzerc80914e2016-03-02 12:33:03 -05001254static int clone_bio(struct dm_target_io *tio, struct bio *bio,
1255 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Mikulas Patockadba14162012-10-12 21:02:15 +01001257 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001259 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Mikulas Patockae2460f22017-04-18 16:51:48 -04001261 if (unlikely(bio_integrity(bio) != NULL)) {
1262 int r;
1263
1264 if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1265 !dm_target_passes_integrity(tio->ti->type))) {
1266 DMWARN("%s: the target %s doesn't support integrity data.",
1267 dm_device_name(tio->io->md),
1268 tio->ti->type->name);
1269 return -EIO;
1270 }
1271
1272 r = bio_integrity_clone(clone, bio, GFP_NOIO);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001273 if (r < 0)
1274 return r;
1275 }
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001276
Damien Le Moal264c8692017-05-08 16:40:47 -07001277 if (bio_op(bio) != REQ_OP_ZONE_REPORT)
1278 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001279 clone->bi_iter.bi_size = to_bytes(len);
1280
Mikulas Patockae2460f22017-04-18 16:51:48 -04001281 if (unlikely(bio_integrity(bio) != NULL))
Dmitry Monakhovfbd08e72017-06-29 11:31:10 -07001282 bio_integrity_trim(clone);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001283
1284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001287static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001288 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001289 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001290{
Mikulas Patockadba14162012-10-12 21:02:15 +01001291 struct dm_target_io *tio;
1292 struct bio *clone;
1293
Junichi Nomura99778272014-10-03 11:55:16 +00001294 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001295 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001296
1297 tio->io = ci->io;
1298 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001299 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001300
1301 return tio;
1302}
1303
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001304static void __clone_and_map_simple_bio(struct clone_info *ci,
1305 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001306 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001307{
Junichi Nomura99778272014-10-03 11:55:16 +00001308 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001309 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001310
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001311 tio->len_ptr = len;
1312
Junichi Nomura99778272014-10-03 11:55:16 +00001313 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001314 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001315 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001316
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001317 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001318}
1319
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001320static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001321 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001322{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001323 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001324
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001325 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001326 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001327}
1328
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001329static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001330{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001331 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001332 struct dm_target *ti;
1333
Mike Snitzerb372d362010-09-08 18:07:01 +02001334 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001335 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001336 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001337
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001338 return 0;
1339}
1340
Mike Snitzerc80914e2016-03-02 12:33:03 -05001341static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001342 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001343{
Mikulas Patockadba14162012-10-12 21:02:15 +01001344 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001345 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001346 unsigned target_bio_nr;
1347 unsigned num_target_bios = 1;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001348 int r = 0;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001349
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001350 /*
1351 * Does the target want to receive duplicate copies of the bio?
1352 */
1353 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1354 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001355
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001356 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001357 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001358 tio->len_ptr = len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001359 r = clone_bio(tio, bio, sector, *len);
Mikulas Patocka072623d2016-04-09 12:48:18 -04001360 if (r < 0) {
Mike Snitzercfae7522016-04-11 12:05:38 -04001361 free_tio(tio);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001362 break;
Mikulas Patocka072623d2016-04-09 12:48:18 -04001363 }
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001364 __map_bio(tio);
1365 }
Mike Snitzerc80914e2016-03-02 12:33:03 -05001366
1367 return r;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001368}
1369
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001370typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001371
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001372static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001373{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001374 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001375}
1376
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001377static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001378{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001379 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001380}
1381
Christoph Hellwigac62d622017-04-05 19:21:05 +02001382static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1383{
1384 return ti->num_write_zeroes_bios;
1385}
1386
Mike Snitzer23508a92012-12-21 20:23:37 +00001387typedef bool (*is_split_required_fn)(struct dm_target *ti);
1388
1389static bool is_split_required_for_discard(struct dm_target *ti)
1390{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001391 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001392}
1393
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001394static int __send_changing_extent_only(struct clone_info *ci,
1395 get_num_bios_fn get_num_bios,
1396 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001397{
1398 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001399 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001400 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001401
Mike Snitzera79245b2010-08-12 04:14:24 +01001402 do {
1403 ti = dm_table_find_target(ci->map, ci->sector);
1404 if (!dm_target_is_valid(ti))
1405 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001406
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001407 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001408 * Even though the device advertised support for this type of
1409 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001410 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001411 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001412 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001413 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1414 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001415 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001416
Mike Snitzer23508a92012-12-21 20:23:37 +00001417 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001418 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001419 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001420 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001421
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001422 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001423
1424 ci->sector += len;
1425 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001426
1427 return 0;
1428}
1429
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001430static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001431{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001432 return __send_changing_extent_only(ci, get_num_discard_bios,
1433 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001434}
1435
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001436static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001437{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001438 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001439}
1440
Christoph Hellwigac62d622017-04-05 19:21:05 +02001441static int __send_write_zeroes(struct clone_info *ci)
1442{
1443 return __send_changing_extent_only(ci, get_num_write_zeroes_bios, NULL);
1444}
1445
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001446/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001447 * Select the correct strategy for processing a non-flush bio.
1448 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001449static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Mikulas Patockadba14162012-10-12 21:02:15 +01001451 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001452 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001453 unsigned len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001454 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Mike Christiee6047142016-06-05 14:32:04 -05001456 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001457 return __send_discard(ci);
Mike Christiee6047142016-06-05 14:32:04 -05001458 else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001459 return __send_write_same(ci);
Christoph Hellwigac62d622017-04-05 19:21:05 +02001460 else if (unlikely(bio_op(bio) == REQ_OP_WRITE_ZEROES))
1461 return __send_write_zeroes(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001462
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001463 ti = dm_table_find_target(ci->map, ci->sector);
1464 if (!dm_target_is_valid(ti))
1465 return -EIO;
1466
Damien Le Moal264c8692017-05-08 16:40:47 -07001467 if (bio_op(bio) == REQ_OP_ZONE_REPORT)
1468 len = ci->sector_count;
1469 else
1470 len = min_t(sector_t, max_io_len(ci->sector, ti),
1471 ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001472
Mike Snitzerc80914e2016-03-02 12:33:03 -05001473 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1474 if (r < 0)
1475 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001477 ci->sector += len;
1478 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001484 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001486static void __split_and_process_bio(struct mapped_device *md,
1487 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001490 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001492 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001493 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001494 return;
1495 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001496
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001497 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 ci.io = alloc_io(md);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001500 ci.io->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 atomic_set(&ci.io->io_count, 1);
1502 ci.io->bio = bio;
1503 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001504 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001505 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001507 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001508
Jens Axboe1eff9d32016-08-05 15:35:16 -06001509 if (bio->bi_opf & REQ_PREFLUSH) {
Mike Snitzerb372d362010-09-08 18:07:01 +02001510 ci.bio = &ci.md->flush_bio;
1511 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001512 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001513 /* dec_pending submits any data associated with flush */
Damien Le Moala4aa5e52017-05-08 16:40:46 -07001514 } else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1515 ci.bio = bio;
1516 ci.sector_count = 0;
1517 error = __split_and_process_non_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001518 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001519 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001520 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001521 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001522 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001526 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528/*-----------------------------------------------------------------
1529 * CRUD END
1530 *---------------------------------------------------------------*/
1531
1532/*
1533 * The request function that just remaps the bio built up by
1534 * dm_merge_bvec.
1535 */
Jens Axboedece1632015-11-05 10:41:16 -07001536static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Kevin Corry12f03a42006-02-01 03:04:52 -08001538 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001540 int srcu_idx;
1541 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001543 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Gu Zheng18c0b222014-11-24 11:05:26 +08001545 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001546
Tejun Heo6a8736d2010-09-08 18:07:00 +02001547 /* if we're suspended, we have to queue this io for later */
1548 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001549 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Jens Axboe1eff9d32016-08-05 15:35:16 -06001551 if (!(bio->bi_opf & REQ_RAHEAD))
Tejun Heo6a8736d2010-09-08 18:07:00 +02001552 queue_io(md, bio);
1553 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001554 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001555 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001558 __split_and_process_bio(md, map, bio);
1559 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001560 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563static int dm_any_congested(void *congested_data, int bdi_bits)
1564{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001565 int r = bdi_bits;
1566 struct mapped_device *md = congested_data;
1567 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001569 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05001570 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001571 /*
Mike Snitzere522c032016-02-02 22:35:06 -05001572 * With request-based DM we only need to check the
1573 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001574 */
Jan Karadc3b17c2017-02-02 15:56:50 +01001575 r = md->queue->backing_dev_info->wb.state & bdi_bits;
Mike Snitzere522c032016-02-02 22:35:06 -05001576 } else {
1577 map = dm_get_live_table_fast(md);
1578 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001579 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05001580 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001581 }
1582 }
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return r;
1585}
1586
1587/*-----------------------------------------------------------------
1588 * An IDR is used to keep track of allocated minor numbers.
1589 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001590static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001592 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001594 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597/*
1598 * See if the device with a specific minor # is free.
1599 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001600static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001602 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 if (minor >= (1 << MINORBITS))
1605 return -EINVAL;
1606
Tejun Heoc9d76be2013-02-27 17:04:26 -08001607 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001608 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Tejun Heoc9d76be2013-02-27 17:04:26 -08001610 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001612 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001613 idr_preload_end();
1614 if (r < 0)
1615 return r == -ENOSPC ? -EBUSY : r;
1616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001619static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001621 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Tejun Heoc9d76be2013-02-27 17:04:26 -08001623 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001624 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Tejun Heoc9d76be2013-02-27 17:04:26 -08001626 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001628 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001629 idr_preload_end();
1630 if (r < 0)
1631 return r;
1632 *minor = r;
1633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001636static const struct block_device_operations dm_blk_dops;
Dan Williamsf26c5712017-04-12 12:35:44 -07001637static const struct dax_operations dm_dax_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Mikulas Patocka53d59142009-04-02 19:55:37 +01001639static void dm_wq_work(struct work_struct *work);
1640
Mike Snitzer4cc96132016-05-12 16:28:10 -04001641void dm_init_md_queue(struct mapped_device *md)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001642{
1643 /*
1644 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001645 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001646 * The type is decided at the first table loading time.
1647 * To prevent problematic device stacking, clear the queue flag
1648 * for request stacking support until then.
1649 *
1650 * This queue is new, so no concurrency on the queue_flags.
1651 */
1652 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04001653
1654 /*
1655 * Initialize data that will only be used by a non-blk-mq DM queue
1656 * - must do so here (in alloc_dev callchain) before queue is used
1657 */
1658 md->queue->queuedata = md;
Jan Karadc3b17c2017-02-02 15:56:50 +01001659 md->queue->backing_dev_info->congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001660}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001661
Mike Snitzer4cc96132016-05-12 16:28:10 -04001662void dm_init_normal_md_queue(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001663{
Mike Snitzer17e149b2015-03-11 15:01:09 -04001664 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001665 dm_init_md_queue(md);
1666
1667 /*
1668 * Initialize aspects of queue that aren't relevant for blk-mq
1669 */
Jan Karadc3b17c2017-02-02 15:56:50 +01001670 md->queue->backing_dev_info->congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001671}
1672
Mike Snitzer0f209722015-04-28 11:50:29 -04001673static void cleanup_mapped_device(struct mapped_device *md)
1674{
Mike Snitzer0f209722015-04-28 11:50:29 -04001675 if (md->wq)
1676 destroy_workqueue(md->wq);
1677 if (md->kworker_task)
1678 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02001679 mempool_destroy(md->io_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04001680 if (md->bs)
1681 bioset_free(md->bs);
1682
Dan Williamsf26c5712017-04-12 12:35:44 -07001683 if (md->dax_dev) {
1684 kill_dax(md->dax_dev);
1685 put_dax(md->dax_dev);
1686 md->dax_dev = NULL;
1687 }
1688
Mike Snitzer0f209722015-04-28 11:50:29 -04001689 if (md->disk) {
1690 spin_lock(&_minor_lock);
1691 md->disk->private_data = NULL;
1692 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04001693 del_gendisk(md->disk);
1694 put_disk(md->disk);
1695 }
1696
1697 if (md->queue)
1698 blk_cleanup_queue(md->queue);
1699
Tahsin Erdogand09960b2016-10-10 05:35:19 -07001700 cleanup_srcu_struct(&md->io_barrier);
1701
Mike Snitzer0f209722015-04-28 11:50:29 -04001702 if (md->bdev) {
1703 bdput(md->bdev);
1704 md->bdev = NULL;
1705 }
Mike Snitzer4cc96132016-05-12 16:28:10 -04001706
1707 dm_mq_cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001708}
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710/*
1711 * Allocate and initialise a blank device with a given minor.
1712 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001713static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Mike Snitzer115485e2016-02-22 12:16:21 -05001715 int r, numa_node_id = dm_get_numa_node();
Dan Williamsf26c5712017-04-12 12:35:44 -07001716 struct dax_device *dax_dev;
Mike Snitzer115485e2016-02-22 12:16:21 -05001717 struct mapped_device *md;
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001718 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Mike Snitzer115485e2016-02-22 12:16:21 -05001720 md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (!md) {
1722 DMWARN("unable to allocate device, out of memory.");
1723 return NULL;
1724 }
1725
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001726 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001727 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001730 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001731 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001732 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001733 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001735 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001737 r = init_srcu_struct(&md->io_barrier);
1738 if (r < 0)
1739 goto bad_io_barrier;
1740
Mike Snitzer115485e2016-02-22 12:16:21 -05001741 md->numa_node_id = numa_node_id;
Mike Snitzer4cc96132016-05-12 16:28:10 -04001742 md->use_blk_mq = dm_use_blk_mq_default();
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001743 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01001744 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001745 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001746 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001747 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001748 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001750 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001752 atomic_set(&md->uevent_seq, 0);
1753 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001754 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001755 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Mike Snitzer115485e2016-02-22 12:16:21 -05001757 md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04001759 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001761 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001762
Mike Snitzer115485e2016-02-22 12:16:21 -05001763 md->disk = alloc_disk_node(1, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04001765 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001767 atomic_set(&md->pending[0], 0);
1768 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001769 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001770 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001771 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05001772 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001773 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 md->disk->major = _major;
1776 md->disk->first_minor = minor;
1777 md->disk->fops = &dm_blk_dops;
1778 md->disk->queue = md->queue;
1779 md->disk->private_data = md;
1780 sprintf(md->disk->disk_name, "dm-%d", minor);
Dan Williamsf26c5712017-04-12 12:35:44 -07001781
1782 dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1783 if (!dax_dev)
1784 goto bad;
1785 md->dax_dev = dax_dev;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001788 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Tejun Heo670368a2013-07-30 08:40:21 -04001790 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001791 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04001792 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00001793
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001794 md->bdev = bdget_disk(md->disk, 0);
1795 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04001796 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001797
Ming Lei3a83f462016-11-22 08:57:21 -07001798 bio_init(&md->flush_bio, NULL, 0);
Tejun Heo6a8736d2010-09-08 18:07:00 +02001799 md->flush_bio.bi_bdev = md->bdev;
Jan Karaff0361b2017-05-31 09:44:32 +02001800 md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
Tejun Heo6a8736d2010-09-08 18:07:00 +02001801
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001802 dm_stats_init(&md->stats);
1803
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001804 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001805 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001806 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001807 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001808
1809 BUG_ON(old_md != MINOR_ALLOCED);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return md;
1812
Mike Snitzer0f209722015-04-28 11:50:29 -04001813bad:
1814 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001815bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001817bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001818 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001819bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 kfree(md);
1821 return NULL;
1822}
1823
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001824static void unlock_fs(struct mapped_device *md);
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826static void free_dev(struct mapped_device *md)
1827{
Tejun Heof331c022008-09-03 09:01:48 +02001828 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001829
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001830 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001831
Mike Snitzer0f209722015-04-28 11:50:29 -04001832 cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001833
1834 free_table_devices(&md->table_devices);
1835 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04001836 free_minor(minor);
1837
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001838 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 kfree(md);
1840}
1841
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001842static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1843{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001844 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001845
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001846 if (md->bs) {
1847 /* The md already has necessary mempools. */
Toshi Kani545ed202016-06-22 17:54:53 -06001848 if (dm_table_bio_based(t)) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001849 /*
1850 * Reload bioset because front_pad may have changed
1851 * because a different table was loaded.
1852 */
1853 bioset_free(md->bs);
1854 md->bs = p->bs;
1855 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001856 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001857 /*
1858 * There's no need to reload with request-based dm
1859 * because the size of front_pad doesn't change.
1860 * Note for future: If you are to reload bioset,
1861 * prep-ed requests in the queue may refer
1862 * to bio from the old bioset, so you must walk
1863 * through the queue to unprep.
1864 */
1865 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001866 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001867
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001868 BUG_ON(!p || md->io_pool || md->bs);
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04001869
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001870 md->io_pool = p->io_pool;
1871 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001872 md->bs = p->bs;
1873 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001874
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001875out:
Mike Snitzer02233342015-03-10 23:49:26 -04001876 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001877 dm_table_free_md_mempools(t);
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880/*
1881 * Bind a table to the device.
1882 */
1883static void event_callback(void *context)
1884{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001885 unsigned long flags;
1886 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 struct mapped_device *md = (struct mapped_device *) context;
1888
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001889 spin_lock_irqsave(&md->uevent_lock, flags);
1890 list_splice_init(&md->uevent_list, &uevents);
1891 spin_unlock_irqrestore(&md->uevent_lock, flags);
1892
Tejun Heoed9e1982008-08-25 19:56:05 +09001893 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 atomic_inc(&md->event_nr);
Mikulas Patocka93e64422017-01-16 16:05:59 -05001896 atomic_inc(&dm_global_event_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 wake_up(&md->eventq);
Mikulas Patocka93e64422017-01-16 16:05:59 -05001898 wake_up(&dm_global_eventq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Mike Snitzerc2176492011-01-13 19:53:46 +00001901/*
1902 * Protected by md->suspend_lock obtained by dm_swap_table().
1903 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001904static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Bart Van Assche1ea06542017-04-27 10:11:21 -07001906 lockdep_assert_held(&md->suspend_lock);
1907
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001908 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001910 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001913/*
1914 * Returns old map, which caller must destroy.
1915 */
1916static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1917 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001919 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02001920 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 sector_t size;
1922
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07001923 lockdep_assert_held(&md->suspend_lock);
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001926
1927 /*
1928 * Wipe any geometry if the size of the table changed.
1929 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001930 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001931 memset(&md->geometry, 0, sizeof(md->geometry));
1932
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001933 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001935 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001936
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001937 /*
1938 * The queue hasn't been stopped yet, if the old table type wasn't
1939 * for request-based during suspension. So stop it to prevent
1940 * I/O mapping before resume.
1941 * This must be done before setting the queue restrictions,
1942 * because request-based dm may be run just after the setting.
1943 */
Mike Snitzer16f12262016-01-31 17:22:27 -05001944 if (dm_table_request_based(t)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001945 dm_stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05001946 /*
1947 * Leverage the fact that request-based DM targets are
1948 * immutable singletons and establish md->immutable_target
1949 * - used to optimize both dm_request_fn and dm_mq_queue_rq
1950 */
1951 md->immutable_target = dm_table_get_immutable_target(t);
1952 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001953
1954 __bind_mempools(md, t);
1955
Eric Dumazeta12f5d42014-11-23 09:34:29 -08001956 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -05001957 rcu_assign_pointer(md->map, (void *)t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001958 md->immutable_target_type = dm_table_get_immutable_target_type(t);
1959
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001960 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01001961 if (old_map)
1962 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001963
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001964 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Alasdair G Kergona7940152009-12-10 23:52:23 +00001967/*
1968 * Returns unbound table for the caller to free.
1969 */
1970static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08001972 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00001975 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05301978 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001979 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00001980
1981 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984/*
1985 * Constructor for a new device.
1986 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001987int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct mapped_device *md;
1990
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001991 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (!md)
1993 return -ENXIO;
1994
Milan Broz784aae72009-01-06 03:05:12 +00001995 dm_sysfs_init(md);
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 *result = md;
1998 return 0;
1999}
2000
Mike Snitzera5664da2010-08-12 04:14:01 +01002001/*
2002 * Functions to manage md->type.
2003 * All are required to hold md->type_lock.
2004 */
2005void dm_lock_md_type(struct mapped_device *md)
2006{
2007 mutex_lock(&md->type_lock);
2008}
2009
2010void dm_unlock_md_type(struct mapped_device *md)
2011{
2012 mutex_unlock(&md->type_lock);
2013}
2014
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002015void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
Mike Snitzera5664da2010-08-12 04:14:01 +01002016{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002017 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002018 md->type = type;
2019}
2020
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002021enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
Mike Snitzera5664da2010-08-12 04:14:01 +01002022{
2023 return md->type;
2024}
2025
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002026struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2027{
2028 return md->immutable_target_type;
2029}
2030
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002031/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002032 * The queue_limits are only valid as long as you have a reference
2033 * count on 'md'.
2034 */
2035struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2036{
2037 BUG_ON(!atomic_read(&md->holders));
2038 return &md->queue->limits;
2039}
2040EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2041
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002042/*
2043 * Setup the DM device's queue based on md's type
2044 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002045int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002046{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002047 int r;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002048 enum dm_queue_mode type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002049
Toshi Kani545ed202016-06-22 17:54:53 -06002050 switch (type) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002051 case DM_TYPE_REQUEST_BASED:
Christoph Hellwigeb8db832017-01-22 18:32:46 +01002052 r = dm_old_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002053 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002054 DMERR("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002055 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002056 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002057 break;
2058 case DM_TYPE_MQ_REQUEST_BASED:
Mike Snitzere83068a2016-05-24 21:16:51 -04002059 r = dm_mq_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002060 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002061 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002062 return r;
2063 }
2064 break;
2065 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002066 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05002067 dm_init_normal_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002068 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04002069 /*
2070 * DM handles splitting bios as needed. Free the bio_split bioset
2071 * since it won't be used (saves 1 process per bio-based DM device).
2072 */
2073 bioset_free(md->queue->bio_split);
2074 md->queue->bio_split = NULL;
Toshi Kani545ed202016-06-22 17:54:53 -06002075
2076 if (type == DM_TYPE_DAX_BIO_BASED)
2077 queue_flag_set_unlocked(QUEUE_FLAG_DAX, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002078 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002079 case DM_TYPE_NONE:
2080 WARN_ON_ONCE(true);
2081 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002082 }
2083
2084 return 0;
2085}
2086
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002087struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 unsigned minor = MINOR(dev);
2091
2092 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2093 return NULL;
2094
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002095 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002098 if (md) {
2099 if ((md == MINOR_ALLOCED ||
2100 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2101 dm_deleting_md(md) ||
2102 test_bit(DMF_FREEING, &md->flags))) {
2103 md = NULL;
2104 goto out;
2105 }
2106 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002109out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002110 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
David Teigland637842c2006-01-06 00:20:00 -08002112 return md;
2113}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002114EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002115
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002116void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002117{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002118 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121void dm_set_mdptr(struct mapped_device *md, void *ptr)
2122{
2123 md->interface_ptr = ptr;
2124}
2125
2126void dm_get(struct mapped_device *md)
2127{
2128 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002129 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002132int dm_hold(struct mapped_device *md)
2133{
2134 spin_lock(&_minor_lock);
2135 if (test_bit(DMF_FREEING, &md->flags)) {
2136 spin_unlock(&_minor_lock);
2137 return -EBUSY;
2138 }
2139 dm_get(md);
2140 spin_unlock(&_minor_lock);
2141 return 0;
2142}
2143EXPORT_SYMBOL_GPL(dm_hold);
2144
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002145const char *dm_device_name(struct mapped_device *md)
2146{
2147 return md->name;
2148}
2149EXPORT_SYMBOL_GPL(dm_device_name);
2150
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002151static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Bart Van Assche3b785fb2016-08-31 15:17:49 -07002153 struct request_queue *q = dm_get_md_queue(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002154 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002155 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002157 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002158
Mike Snitzer63a4f062015-03-23 17:01:43 -04002159 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002160 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2161 set_bit(DMF_FREEING, &md->flags);
2162 spin_unlock(&_minor_lock);
2163
Bart Van Assche2e91c362016-11-18 14:26:47 -08002164 blk_set_queue_dying(q);
Bart Van Assche3b785fb2016-08-31 15:17:49 -07002165
Mike Snitzer02233342015-03-10 23:49:26 -04002166 if (dm_request_based(md) && md->kworker_task)
Petr Mladek39891442016-10-11 13:55:20 -07002167 kthread_flush_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002168
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002169 /*
2170 * Take suspend_lock so that presuspend and postsuspend methods
2171 * do not race with internal suspend.
2172 */
2173 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002174 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002175 if (!dm_suspended_md(md)) {
2176 dm_table_presuspend_targets(map);
2177 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002179 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2180 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002181 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002182
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002183 /*
2184 * Rare, but there may be I/O requests still going to complete,
2185 * for example. Wait for all references to disappear.
2186 * No one should increment the reference count of the mapped_device,
2187 * after the mapped_device state becomes DMF_FREEING.
2188 */
2189 if (wait)
2190 while (atomic_read(&md->holders))
2191 msleep(1);
2192 else if (atomic_read(&md->holders))
2193 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2194 dm_device_name(md), atomic_read(&md->holders));
2195
2196 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002197 dm_table_destroy(__unbind(md));
2198 free_dev(md);
2199}
2200
2201void dm_destroy(struct mapped_device *md)
2202{
2203 __dm_destroy(md, true);
2204}
2205
2206void dm_destroy_immediate(struct mapped_device *md)
2207{
2208 __dm_destroy(md, false);
2209}
2210
2211void dm_put(struct mapped_device *md)
2212{
2213 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
Edward Goggin79eb8852007-05-09 02:32:56 -07002215EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002217static int dm_wait_for_completion(struct mapped_device *md, long task_state)
Milan Broz46125c12008-02-08 02:10:30 +00002218{
2219 int r = 0;
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002220 DEFINE_WAIT(wait);
Milan Broz46125c12008-02-08 02:10:30 +00002221
2222 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002223 prepare_to_wait(&md->wait, &wait, task_state);
Milan Broz46125c12008-02-08 02:10:30 +00002224
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002225 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002226 break;
2227
Bart Van Asschee3fabdf2016-08-31 15:16:22 -07002228 if (signal_pending_state(task_state, current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002229 r = -EINTR;
2230 break;
2231 }
2232
2233 io_schedule();
2234 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002235 finish_wait(&md->wait, &wait);
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002236
Milan Broz46125c12008-02-08 02:10:30 +00002237 return r;
2238}
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240/*
2241 * Process the deferred bios
2242 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002243static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Mikulas Patockaef208582009-04-02 19:55:38 +01002245 struct mapped_device *md = container_of(work, struct mapped_device,
2246 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002247 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002248 int srcu_idx;
2249 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002251 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002252
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002253 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002254 spin_lock_irq(&md->deferred_lock);
2255 c = bio_list_pop(&md->deferred);
2256 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002257
Tejun Heo6a8736d2010-09-08 18:07:00 +02002258 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002259 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002260
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002261 if (dm_request_based(md))
2262 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002263 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002264 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002265 }
Milan Broz73d410c2008-02-08 02:10:25 +00002266
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002267 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002270static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002271{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002272 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002273 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002274 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002275}
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002278 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002280struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Mike Christie87eb5b22013-03-01 22:45:48 +00002282 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002283 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002284 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Daniel Walkere61290a2008-02-08 02:10:08 +00002286 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002289 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Mike Snitzer3ae70652012-09-26 23:45:45 +01002292 /*
2293 * If the new table has no data devices, retain the existing limits.
2294 * This helps multipath with queue_if_no_path if all paths disappear,
2295 * then new I/O is queued based on these limits, and then some paths
2296 * reappear.
2297 */
2298 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002299 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002300 if (live_map)
2301 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002302 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002303 }
2304
Mike Christie87eb5b22013-03-01 22:45:48 +00002305 if (!live_map) {
2306 r = dm_calculate_queue_limits(table, &limits);
2307 if (r) {
2308 map = ERR_PTR(r);
2309 goto out;
2310 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002311 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002312
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002313 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002315out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002316 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002317 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
2320/*
2321 * Functions to lock and unlock any filesystem running on the
2322 * device.
2323 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002324static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002326 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002329
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002330 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002331 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002332 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002333 md->frozen_sb = NULL;
2334 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002335 }
2336
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002337 set_bit(DMF_FROZEN, &md->flags);
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return 0;
2340}
2341
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002342static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002344 if (!test_bit(DMF_FROZEN, &md->flags))
2345 return;
2346
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002347 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002349 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
2352/*
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002353 * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2354 * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2355 * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2356 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04002357 * If __dm_suspend returns 0, the device is completely quiescent
2358 * now. There is no request-processing activity. All new requests
2359 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002360 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04002361static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002362 unsigned suspend_flags, long task_state,
Mike Snitzereaf9a732016-08-02 13:07:20 -04002363 int dmf_suspended_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002365 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2366 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2367 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07002369 lockdep_assert_held(&md->suspend_lock);
2370
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002371 /*
2372 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2373 * This flag is cleared before dm_suspend returns.
2374 */
2375 if (noflush)
2376 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Bart Van Assche86331f32017-04-27 10:11:26 -07002377 else
2378 pr_debug("%s: suspending with flush\n", dm_device_name(md));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002379
Mike Snitzerd67ee212014-10-28 20:13:31 -04002380 /*
2381 * This gets reverted if there's an error later and the targets
2382 * provide the .presuspend_undo hook.
2383 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002384 dm_table_presuspend_targets(map);
2385
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002386 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002387 * Flush I/O to the device.
2388 * Any I/O submitted after lock_fs() may not be flushed.
2389 * noflush takes precedence over do_lockfs.
2390 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002391 */
2392 if (!noflush && do_lockfs) {
2393 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002394 if (r) {
2395 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002396 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04002397 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002401 * Here we must make sure that no processes are submitting requests
2402 * to target drivers i.e. no one may be executing
2403 * __split_and_process_bio. This is called from dm_request and
2404 * dm_wq_work.
2405 *
2406 * To get all processes out of __split_and_process_bio in dm_request,
2407 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002408 * __split_and_process_bio from dm_request and quiesce the thread
2409 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2410 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002412 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002413 if (map)
2414 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002416 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002417 * Stop md->queue before flushing md->wq in case request-based
2418 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002419 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002420 if (dm_request_based(md)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002421 dm_stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04002422 if (md->kworker_task)
Petr Mladek39891442016-10-11 13:55:20 -07002423 kthread_flush_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002424 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002425
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002426 flush_workqueue(md->wq);
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002429 * At this point no more requests are entering target request routines.
2430 * We call dm_wait_for_completion to wait for all existing requests
2431 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 */
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002433 r = dm_wait_for_completion(md, task_state);
Mike Snitzereaf9a732016-08-02 13:07:20 -04002434 if (!r)
2435 set_bit(dmf_suspended_flag, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Milan Broz6d6f10d2008-02-08 02:10:22 +00002437 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002438 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002439 if (map)
2440 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002443 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002444 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002445
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002446 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002447 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002448
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002449 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002450 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002451 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002452 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002453
Mike Snitzerffcc3932014-10-28 18:34:52 -04002454 return r;
2455}
2456
2457/*
2458 * We need to be able to change a mapping table under a mounted
2459 * filesystem. For example we might want to move some data in
2460 * the background. Before the table can be swapped with
2461 * dm_bind_table, dm_suspend must be called to flush any in
2462 * flight bios and ensure that any further io gets deferred.
2463 */
2464/*
2465 * Suspend mechanism in request-based dm.
2466 *
2467 * 1. Flush all I/Os by lock_fs() if needed.
2468 * 2. Stop dispatching any I/O by stopping the request_queue.
2469 * 3. Wait for all in-flight I/Os to be completed or requeued.
2470 *
2471 * To abort suspend, start the request_queue.
2472 */
2473int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2474{
2475 struct dm_table *map = NULL;
2476 int r = 0;
2477
2478retry:
2479 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2480
2481 if (dm_suspended_md(md)) {
2482 r = -EINVAL;
2483 goto out_unlock;
2484 }
2485
2486 if (dm_suspended_internally_md(md)) {
2487 /* already internally suspended, wait for internal resume */
2488 mutex_unlock(&md->suspend_lock);
2489 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2490 if (r)
2491 return r;
2492 goto retry;
2493 }
2494
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002495 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002496
Mike Snitzereaf9a732016-08-02 13:07:20 -04002497 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002498 if (r)
2499 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002500
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002501 dm_table_postsuspend_targets(map);
2502
Alasdair G Kergond2874832006-11-08 17:44:43 -08002503out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002504 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002505 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Mike Snitzerffcc3932014-10-28 18:34:52 -04002508static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002510 if (map) {
2511 int r = dm_table_resume_targets(map);
2512 if (r)
2513 return r;
2514 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002515
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002516 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002517
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002518 /*
2519 * Flushing deferred I/Os must be done after targets are resumed
2520 * so that mapping of targets can work correctly.
2521 * Request-based dm is queueing the deferred I/Os in its request_queue.
2522 */
2523 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002524 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002525
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002526 unlock_fs(md);
2527
Mike Snitzerffcc3932014-10-28 18:34:52 -04002528 return 0;
2529}
2530
2531int dm_resume(struct mapped_device *md)
2532{
Minfei Huang8dc23652016-09-06 16:00:29 +08002533 int r;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002534 struct dm_table *map = NULL;
2535
2536retry:
Minfei Huang8dc23652016-09-06 16:00:29 +08002537 r = -EINVAL;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002538 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2539
2540 if (!dm_suspended_md(md))
2541 goto out;
2542
2543 if (dm_suspended_internally_md(md)) {
2544 /* already internally suspended, wait for internal resume */
2545 mutex_unlock(&md->suspend_lock);
2546 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2547 if (r)
2548 return r;
2549 goto retry;
2550 }
2551
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002552 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002553 if (!map || !dm_table_get_size(map))
2554 goto out;
2555
2556 r = __dm_resume(md, map);
2557 if (r)
2558 goto out;
2559
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002560 clear_bit(DMF_SUSPENDED, &md->flags);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002561out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002562 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002563
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002564 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002567/*
2568 * Internal suspend/resume works like userspace-driven suspend. It waits
2569 * until all bios finish and prevents issuing new bios to the target drivers.
2570 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002571 */
2572
Mike Snitzerffcc3932014-10-28 18:34:52 -04002573static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2574{
2575 struct dm_table *map = NULL;
2576
Bart Van Assche1ea06542017-04-27 10:11:21 -07002577 lockdep_assert_held(&md->suspend_lock);
2578
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002579 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002580 return; /* nested internal suspend */
2581
2582 if (dm_suspended_md(md)) {
2583 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2584 return; /* nest suspend */
2585 }
2586
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002587 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002588
2589 /*
2590 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2591 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
2592 * would require changing .presuspend to return an error -- avoid this
2593 * until there is a need for more elaborate variants of internal suspend.
2594 */
Mike Snitzereaf9a732016-08-02 13:07:20 -04002595 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2596 DMF_SUSPENDED_INTERNALLY);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002597
2598 dm_table_postsuspend_targets(map);
2599}
2600
2601static void __dm_internal_resume(struct mapped_device *md)
2602{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002603 BUG_ON(!md->internal_suspend_count);
2604
2605 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002606 return; /* resume from nested internal suspend */
2607
2608 if (dm_suspended_md(md))
2609 goto done; /* resume from nested suspend */
2610
2611 /*
2612 * NOTE: existing callers don't need to call dm_table_resume_targets
2613 * (which may fail -- so best to avoid it for now by passing NULL map)
2614 */
2615 (void) __dm_resume(md, NULL);
2616
2617done:
2618 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2619 smp_mb__after_atomic();
2620 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2621}
2622
2623void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002624{
2625 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002626 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2627 mutex_unlock(&md->suspend_lock);
2628}
2629EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2630
2631void dm_internal_resume(struct mapped_device *md)
2632{
2633 mutex_lock(&md->suspend_lock);
2634 __dm_internal_resume(md);
2635 mutex_unlock(&md->suspend_lock);
2636}
2637EXPORT_SYMBOL_GPL(dm_internal_resume);
2638
2639/*
2640 * Fast variants of internal suspend/resume hold md->suspend_lock,
2641 * which prevents interaction with userspace-driven suspend.
2642 */
2643
2644void dm_internal_suspend_fast(struct mapped_device *md)
2645{
2646 mutex_lock(&md->suspend_lock);
2647 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002648 return;
2649
2650 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2651 synchronize_srcu(&md->io_barrier);
2652 flush_workqueue(md->wq);
2653 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2654}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002655EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002656
Mike Snitzerffcc3932014-10-28 18:34:52 -04002657void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002658{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002659 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002660 goto done;
2661
2662 dm_queue_flush(md);
2663
2664done:
2665 mutex_unlock(&md->suspend_lock);
2666}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002667EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669/*-----------------------------------------------------------------
2670 * Event notification.
2671 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002672int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002673 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002674{
Milan Broz60935eb2009-06-22 10:12:30 +01002675 char udev_cookie[DM_COOKIE_LENGTH];
2676 char *envp[] = { udev_cookie, NULL };
2677
2678 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002679 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002680 else {
2681 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2682 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002683 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2684 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002685 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002686}
2687
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002688uint32_t dm_next_uevent_seq(struct mapped_device *md)
2689{
2690 return atomic_add_return(1, &md->uevent_seq);
2691}
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693uint32_t dm_get_event_nr(struct mapped_device *md)
2694{
2695 return atomic_read(&md->event_nr);
2696}
2697
2698int dm_wait_event(struct mapped_device *md, int event_nr)
2699{
2700 return wait_event_interruptible(md->eventq,
2701 (event_nr != atomic_read(&md->event_nr)));
2702}
2703
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002704void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2705{
2706 unsigned long flags;
2707
2708 spin_lock_irqsave(&md->uevent_lock, flags);
2709 list_add(elist, &md->uevent_list);
2710 spin_unlock_irqrestore(&md->uevent_lock, flags);
2711}
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713/*
2714 * The gendisk is only valid as long as you have a reference
2715 * count on 'md'.
2716 */
2717struct gendisk *dm_disk(struct mapped_device *md)
2718{
2719 return md->disk;
2720}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00002721EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Milan Broz784aae72009-01-06 03:05:12 +00002723struct kobject *dm_kobject(struct mapped_device *md)
2724{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002725 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00002726}
2727
Milan Broz784aae72009-01-06 03:05:12 +00002728struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2729{
2730 struct mapped_device *md;
2731
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002732 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00002733
Milan Broz4d89b7b2009-06-22 10:12:11 +01002734 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002735 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002736 return NULL;
2737
Milan Broz784aae72009-01-06 03:05:12 +00002738 dm_get(md);
2739 return md;
2740}
2741
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002742int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 return test_bit(DMF_SUSPENDED, &md->flags);
2745}
2746
Mike Snitzerffcc3932014-10-28 18:34:52 -04002747int dm_suspended_internally_md(struct mapped_device *md)
2748{
2749 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2750}
2751
Mikulas Patocka2c140a22013-11-01 18:27:41 -04002752int dm_test_deferred_remove_flag(struct mapped_device *md)
2753{
2754 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2755}
2756
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002757int dm_suspended(struct dm_target *ti)
2758{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002759 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002760}
2761EXPORT_SYMBOL_GPL(dm_suspended);
2762
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002763int dm_noflush_suspending(struct dm_target *ti)
2764{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002765 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002766}
2767EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2768
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002769struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
Mike Snitzer30187e12016-01-31 13:28:26 -05002770 unsigned integrity, unsigned per_io_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002771{
Mike Snitzer115485e2016-02-22 12:16:21 -05002772 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
Mike Snitzer78d8e582015-06-26 10:01:13 -04002773 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002774 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002775
2776 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002777 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002778
Mike Snitzer78d8e582015-06-26 10:01:13 -04002779 switch (type) {
2780 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002781 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzer78d8e582015-06-26 10:01:13 -04002782 pool_size = dm_get_reserved_bio_based_ios();
Mike Snitzer30187e12016-01-31 13:28:26 -05002783 front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
Christoph Hellwigeb8db832017-01-22 18:32:46 +01002784
2785 pools->io_pool = mempool_create_slab_pool(pool_size, _io_cache);
2786 if (!pools->io_pool)
2787 goto out;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002788 break;
2789 case DM_TYPE_REQUEST_BASED:
Mike Snitzer78d8e582015-06-26 10:01:13 -04002790 case DM_TYPE_MQ_REQUEST_BASED:
Christoph Hellwigeb8db832017-01-22 18:32:46 +01002791 pool_size = dm_get_reserved_rq_based_ios();
Mike Snitzer78d8e582015-06-26 10:01:13 -04002792 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002793 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04002794 break;
2795 default:
2796 BUG();
2797 }
2798
NeilBrown47e0fb42017-06-18 14:38:57 +10002799 pools->bs = bioset_create(pool_size, front_pad, BIOSET_NEED_RESCUER);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002800 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002801 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002802
Martin K. Petersena91a2782011-03-17 11:11:05 +01002803 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002804 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002805
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002806 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002807
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002808out:
2809 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002810
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002811 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002812}
2813
2814void dm_free_md_mempools(struct dm_md_mempools *pools)
2815{
2816 if (!pools)
2817 return;
2818
Julia Lawall6f659852015-09-13 14:15:05 +02002819 mempool_destroy(pools->io_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002820
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002821 if (pools->bs)
2822 bioset_free(pools->bs);
2823
2824 kfree(pools);
2825}
2826
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002827struct dm_pr {
2828 u64 old_key;
2829 u64 new_key;
2830 u32 flags;
2831 bool fail_early;
2832};
2833
2834static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
2835 void *data)
2836{
2837 struct mapped_device *md = bdev->bd_disk->private_data;
2838 struct dm_table *table;
2839 struct dm_target *ti;
2840 int ret = -ENOTTY, srcu_idx;
2841
2842 table = dm_get_live_table(md, &srcu_idx);
2843 if (!table || !dm_table_get_size(table))
2844 goto out;
2845
2846 /* We only support devices that have a single target */
2847 if (dm_table_get_num_targets(table) != 1)
2848 goto out;
2849 ti = dm_table_get_target(table, 0);
2850
2851 ret = -EINVAL;
2852 if (!ti->type->iterate_devices)
2853 goto out;
2854
2855 ret = ti->type->iterate_devices(ti, fn, data);
2856out:
2857 dm_put_live_table(md, srcu_idx);
2858 return ret;
2859}
2860
2861/*
2862 * For register / unregister we need to manually call out to every path.
2863 */
2864static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
2865 sector_t start, sector_t len, void *data)
2866{
2867 struct dm_pr *pr = data;
2868 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
2869
2870 if (!ops || !ops->pr_register)
2871 return -EOPNOTSUPP;
2872 return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
2873}
2874
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002875static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05002876 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002877{
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002878 struct dm_pr pr = {
2879 .old_key = old_key,
2880 .new_key = new_key,
2881 .flags = flags,
2882 .fail_early = true,
2883 };
2884 int ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002885
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002886 ret = dm_call_pr(bdev, __dm_pr_register, &pr);
2887 if (ret && new_key) {
2888 /* unregister all paths if we failed to register any path */
2889 pr.old_key = new_key;
2890 pr.new_key = 0;
2891 pr.flags = 0;
2892 pr.fail_early = false;
2893 dm_call_pr(bdev, __dm_pr_register, &pr);
2894 }
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002895
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002896 return ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002897}
2898
2899static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05002900 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002901{
2902 struct mapped_device *md = bdev->bd_disk->private_data;
2903 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002904 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002905 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002906
Mike Snitzer956a4022016-02-18 16:13:51 -05002907 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002908 if (r < 0)
2909 return r;
2910
2911 ops = bdev->bd_disk->fops->pr_ops;
2912 if (ops && ops->pr_reserve)
2913 r = ops->pr_reserve(bdev, key, type, flags);
2914 else
2915 r = -EOPNOTSUPP;
2916
Mike Snitzer956a4022016-02-18 16:13:51 -05002917 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002918 return r;
2919}
2920
2921static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
2922{
2923 struct mapped_device *md = bdev->bd_disk->private_data;
2924 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002925 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002926 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002927
Mike Snitzer956a4022016-02-18 16:13:51 -05002928 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002929 if (r < 0)
2930 return r;
2931
2932 ops = bdev->bd_disk->fops->pr_ops;
2933 if (ops && ops->pr_release)
2934 r = ops->pr_release(bdev, key, type);
2935 else
2936 r = -EOPNOTSUPP;
2937
Mike Snitzer956a4022016-02-18 16:13:51 -05002938 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002939 return r;
2940}
2941
2942static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05002943 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002944{
2945 struct mapped_device *md = bdev->bd_disk->private_data;
2946 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002947 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002948 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002949
Mike Snitzer956a4022016-02-18 16:13:51 -05002950 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002951 if (r < 0)
2952 return r;
2953
2954 ops = bdev->bd_disk->fops->pr_ops;
2955 if (ops && ops->pr_preempt)
2956 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
2957 else
2958 r = -EOPNOTSUPP;
2959
Mike Snitzer956a4022016-02-18 16:13:51 -05002960 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002961 return r;
2962}
2963
2964static int dm_pr_clear(struct block_device *bdev, u64 key)
2965{
2966 struct mapped_device *md = bdev->bd_disk->private_data;
2967 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002968 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002969 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002970
Mike Snitzer956a4022016-02-18 16:13:51 -05002971 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002972 if (r < 0)
2973 return r;
2974
2975 ops = bdev->bd_disk->fops->pr_ops;
2976 if (ops && ops->pr_clear)
2977 r = ops->pr_clear(bdev, key);
2978 else
2979 r = -EOPNOTSUPP;
2980
Mike Snitzer956a4022016-02-18 16:13:51 -05002981 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002982 return r;
2983}
2984
2985static const struct pr_ops dm_pr_ops = {
2986 .pr_register = dm_pr_register,
2987 .pr_reserve = dm_pr_reserve,
2988 .pr_release = dm_pr_release,
2989 .pr_preempt = dm_pr_preempt,
2990 .pr_clear = dm_pr_clear,
2991};
2992
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002993static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .open = dm_blk_open,
2995 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002996 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002997 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002998 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 .owner = THIS_MODULE
3000};
3001
Dan Williamsf26c5712017-04-12 12:35:44 -07003002static const struct dax_operations dm_dax_ops = {
3003 .direct_access = dm_dax_direct_access,
Dan Williams7e026c82017-05-29 12:57:56 -07003004 .copy_from_iter = dm_dax_copy_from_iter,
Dan Williamsabebfbe2017-05-29 13:02:52 -07003005 .flush = dm_dax_flush,
Dan Williamsf26c5712017-04-12 12:35:44 -07003006};
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008/*
3009 * module hooks
3010 */
3011module_init(dm_init);
3012module_exit(dm_exit);
3013
3014module_param(major, uint, 0);
3015MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003016
Mike Snitzere8603132013-09-12 18:06:12 -04003017module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3018MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3019
Mike Snitzer115485e2016-02-22 12:16:21 -05003020module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3021MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023MODULE_DESCRIPTION(DM_NAME " driver");
3024MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3025MODULE_LICENSE("GPL");