blob: 79b8f072e76a9b942236c6d06630cc63f532025c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
Mike Snitzer4cc96132016-05-12 16:28:10 -04008#include "dm-core.h"
9#include "dm-rq.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +010010#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <linux/init.h>
13#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080014#include <linux/mutex.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/blkpg.h>
17#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mempool.h>
Dan Williamsf26c5712017-04-12 12:35:44 -070019#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/idr.h>
Dan Williams7e026c82017-05-29 12:57:56 -070022#include <linux/uio.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080023#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010024#include <linux/delay.h>
Mike Snitzerffcc3932014-10-28 18:34:52 -040025#include <linux/wait.h>
Christoph Hellwig71cdb692015-10-15 14:10:51 +020026#include <linux/pr.h>
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +030027#include <linux/refcount.h>
Li Zefan55782132009-06-09 13:43:05 +080028
Alasdair G Kergon72d94862006-06-26 00:27:35 -070029#define DM_MSG_PREFIX "core"
30
Milan Broz60935eb2009-06-22 10:12:30 +010031/*
32 * Cookies are numeric values sent with CHANGE and REMOVE
33 * uevents while resuming, removing or renaming the device.
34 */
35#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
36#define DM_COOKIE_LENGTH 24
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static const char *_name = DM_NAME;
39
40static unsigned int major = 0;
41static unsigned int _major = 0;
42
Alasdair G Kergond15b7742011-08-02 12:32:01 +010043static DEFINE_IDR(_minor_idr);
44
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070045static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040046
47static void do_deferred_remove(struct work_struct *w);
48
49static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
50
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040051static struct workqueue_struct *deferred_remove_workqueue;
52
Mikulas Patocka93e64422017-01-16 16:05:59 -050053atomic_t dm_global_event_nr = ATOMIC_INIT(0);
54DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
55
Mikulas Patocka62e08242017-09-20 07:29:49 -040056void dm_issue_global_event(void)
57{
58 atomic_inc(&dm_global_event_nr);
59 wake_up(&dm_global_eventq);
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * One of these is allocated per bio.
64 */
65struct dm_io {
66 struct mapped_device *md;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020067 blk_status_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010069 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080070 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010071 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040072 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070075#define MINOR_ALLOCED ((void *)-1)
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * Bits for the md->flags field.
79 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +010080#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080082#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -070083#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070084#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080085#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -070086#define DMF_DEFERRED_REMOVE 6
87#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Mike Snitzer115485e2016-02-22 12:16:21 -050089#define DM_NUMA_NODE NUMA_NO_NODE
Mike Snitzer115485e2016-02-22 12:16:21 -050090static int dm_numa_node = DM_NUMA_NODE;
Mike Snitzerfaad87d2016-01-28 16:52:56 -050091
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +010092/*
93 * For mempools pre-allocation at the table loading time.
94 */
95struct dm_md_mempools {
96 mempool_t *io_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +010097 struct bio_set *bs;
98};
99
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500100struct table_device {
101 struct list_head list;
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300102 refcount_t count;
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500103 struct dm_dev dm_dev;
104};
105
Christoph Lametere18b8902006-12-06 20:33:20 -0800106static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000107static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500108static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700109
Mike Snitzerf4790822013-09-12 18:06:12 -0400110/*
Mike Snitzere8603132013-09-12 18:06:12 -0400111 * Bio-based DM's mempools' reserved IOs set by the user.
112 */
Mike Snitzer4cc96132016-05-12 16:28:10 -0400113#define RESERVED_BIO_BASED_IOS 16
Mike Snitzere8603132013-09-12 18:06:12 -0400114static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
115
Mike Snitzer115485e2016-02-22 12:16:21 -0500116static int __dm_get_module_param_int(int *module_param, int min, int max)
117{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700118 int param = READ_ONCE(*module_param);
Mike Snitzer115485e2016-02-22 12:16:21 -0500119 int modified_param = 0;
120 bool modified = true;
121
122 if (param < min)
123 modified_param = min;
124 else if (param > max)
125 modified_param = max;
126 else
127 modified = false;
128
129 if (modified) {
130 (void)cmpxchg(module_param, param, modified_param);
131 param = modified_param;
132 }
133
134 return param;
135}
136
Mike Snitzer4cc96132016-05-12 16:28:10 -0400137unsigned __dm_get_module_param(unsigned *module_param,
138 unsigned def, unsigned max)
Mike Snitzerf4790822013-09-12 18:06:12 -0400139{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700140 unsigned param = READ_ONCE(*module_param);
Mike Snitzer09c2d532015-02-27 22:25:26 -0500141 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400142
Mike Snitzer09c2d532015-02-27 22:25:26 -0500143 if (!param)
144 modified_param = def;
145 else if (param > max)
146 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400147
Mike Snitzer09c2d532015-02-27 22:25:26 -0500148 if (modified_param) {
149 (void)cmpxchg(module_param, param, modified_param);
150 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400151 }
152
Mike Snitzer09c2d532015-02-27 22:25:26 -0500153 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400154}
155
Mike Snitzere8603132013-09-12 18:06:12 -0400156unsigned dm_get_reserved_bio_based_ios(void)
157{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500158 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzer4cc96132016-05-12 16:28:10 -0400159 RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
Mike Snitzere8603132013-09-12 18:06:12 -0400160}
161EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
162
Mike Snitzer115485e2016-02-22 12:16:21 -0500163static unsigned dm_get_numa_node(void)
164{
165 return __dm_get_module_param_int(&dm_numa_node,
166 DM_NUMA_NODE, num_online_nodes() - 1);
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int __init local_init(void)
170{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100171 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100174 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100176 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000178 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
179 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100180 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000181
Mike Snitzereca7ee62016-02-20 13:45:38 -0500182 _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500183 __alignof__(struct request), 0, NULL);
184 if (!_rq_cache)
185 goto out_free_rq_tio_cache;
186
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100187 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100188 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500189 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100190
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400191 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
192 if (!deferred_remove_workqueue) {
193 r = -ENOMEM;
194 goto out_uevent_exit;
195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 _major = major;
198 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100199 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400200 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (!_major)
203 _major = r;
204
205 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100206
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400207out_free_workqueue:
208 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100209out_uevent_exit:
210 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500211out_free_rq_cache:
212 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000213out_free_rq_tio_cache:
214 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100215out_free_io_cache:
216 kmem_cache_destroy(_io_cache);
217
218 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static void local_exit(void)
222{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400223 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400224 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400225
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500226 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000227 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700229 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100230 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 _major = 0;
233
234 DMINFO("cleaned up");
235}
236
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000237static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 local_init,
239 dm_target_init,
240 dm_linear_init,
241 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000242 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100243 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400245 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000248static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 local_exit,
250 dm_target_exit,
251 dm_linear_exit,
252 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000253 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100254 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400256 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257};
258
259static int __init dm_init(void)
260{
261 const int count = ARRAY_SIZE(_inits);
262
263 int r, i;
264
265 for (i = 0; i < count; i++) {
266 r = _inits[i]();
267 if (r)
268 goto bad;
269 }
270
271 return 0;
272
273 bad:
274 while (i--)
275 _exits[i]();
276
277 return r;
278}
279
280static void __exit dm_exit(void)
281{
282 int i = ARRAY_SIZE(_exits);
283
284 while (i--)
285 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100286
287 /*
288 * Should be empty by this point.
289 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100290 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
294 * Block device functions
295 */
Mike Anderson432a2122009-12-10 23:52:20 +0000296int dm_deleting_md(struct mapped_device *md)
297{
298 return test_bit(DMF_DELETING, &md->flags);
299}
300
Al Virofe5f9f22008-03-02 10:29:31 -0500301static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct mapped_device *md;
304
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700305 spin_lock(&_minor_lock);
306
Al Virofe5f9f22008-03-02 10:29:31 -0500307 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700308 if (!md)
309 goto out;
310
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700311 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000312 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700313 md = NULL;
314 goto out;
315 }
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700318 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700319out:
320 spin_unlock(&_minor_lock);
321
322 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Al Virodb2a1442013-05-05 21:52:57 -0400325static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400327 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200328
Milan Broz4a1aeb92011-01-13 19:59:48 +0000329 spin_lock(&_minor_lock);
330
Mike Snitzer63a4f062015-03-23 17:01:43 -0400331 md = disk->private_data;
332 if (WARN_ON(!md))
333 goto out;
334
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400335 if (atomic_dec_and_test(&md->open_count) &&
336 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400337 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400340out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000341 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700344int dm_open_count(struct mapped_device *md)
345{
346 return atomic_read(&md->open_count);
347}
348
349/*
350 * Guarantees nothing is using the device before it's deleted.
351 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400352int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700353{
354 int r = 0;
355
356 spin_lock(&_minor_lock);
357
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400358 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700359 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400360 if (mark_deferred)
361 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
362 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
363 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700364 else
365 set_bit(DMF_DELETING, &md->flags);
366
367 spin_unlock(&_minor_lock);
368
369 return r;
370}
371
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400372int dm_cancel_deferred_remove(struct mapped_device *md)
373{
374 int r = 0;
375
376 spin_lock(&_minor_lock);
377
378 if (test_bit(DMF_DELETING, &md->flags))
379 r = -EBUSY;
380 else
381 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
382
383 spin_unlock(&_minor_lock);
384
385 return r;
386}
387
388static void do_deferred_remove(struct work_struct *w)
389{
390 dm_deferred_remove();
391}
392
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400393sector_t dm_get_size(struct mapped_device *md)
394{
395 return get_capacity(md->disk);
396}
397
Mike Snitzer9974fa22014-02-28 15:33:43 +0100398struct request_queue *dm_get_md_queue(struct mapped_device *md)
399{
400 return md->queue;
401}
402
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400403struct dm_stats *dm_get_stats(struct mapped_device *md)
404{
405 return &md->stats;
406}
407
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800408static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
409{
410 struct mapped_device *md = bdev->bd_disk->private_data;
411
412 return dm_get_geometry(md, geo);
413}
414
Mike Snitzer956a4022016-02-18 16:13:51 -0500415static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
416 struct block_device **bdev,
417 fmode_t *mode)
Milan Brozaa129a22006-10-03 01:15:15 -0700418{
Mike Snitzer66482022016-02-18 15:44:39 -0500419 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100420 struct dm_table *map;
Mike Snitzer956a4022016-02-18 16:13:51 -0500421 int srcu_idx, r;
Milan Brozaa129a22006-10-03 01:15:15 -0700422
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100423retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200424 r = -ENOTTY;
Mike Snitzer956a4022016-02-18 16:13:51 -0500425 map = dm_get_live_table(md, &srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700426 if (!map || !dm_table_get_size(map))
427 goto out;
428
429 /* We only support devices that have a single target */
430 if (dm_table_get_num_targets(map) != 1)
431 goto out;
432
Mike Snitzer66482022016-02-18 15:44:39 -0500433 tgt = dm_table_get_target(map, 0);
434 if (!tgt->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500435 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700436
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000437 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700438 r = -EAGAIN;
439 goto out;
440 }
441
Mike Snitzer66482022016-02-18 15:44:39 -0500442 r = tgt->type->prepare_ioctl(tgt, bdev, mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200443 if (r < 0)
444 goto out;
445
Mike Snitzer956a4022016-02-18 16:13:51 -0500446 bdgrab(*bdev);
447 dm_put_live_table(md, srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200448 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700449
450out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500451 dm_put_live_table(md, srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000452 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100453 msleep(10);
454 goto retry;
455 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200456 return r;
457}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100458
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200459static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
460 unsigned int cmd, unsigned long arg)
461{
462 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer956a4022016-02-18 16:13:51 -0500463 int r;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200464
Mike Snitzer956a4022016-02-18 16:13:51 -0500465 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200466 if (r < 0)
467 return r;
468
469 if (r > 0) {
470 /*
Christoph Hellwige980f622017-02-04 10:45:03 +0100471 * Target determined this ioctl is being issued against a
472 * subset of the parent bdev; require extra privileges.
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200473 */
Christoph Hellwige980f622017-02-04 10:45:03 +0100474 if (!capable(CAP_SYS_RAWIO)) {
475 DMWARN_LIMIT(
476 "%s: sending ioctl %x to DM device without required privilege.",
477 current->comm, cmd);
478 r = -ENOIOCTLCMD;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200479 goto out;
Christoph Hellwige980f622017-02-04 10:45:03 +0100480 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200481 }
482
Mike Snitzer66482022016-02-18 15:44:39 -0500483 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200484out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500485 bdput(bdev);
Milan Brozaa129a22006-10-03 01:15:15 -0700486 return r;
487}
488
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100489static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 return mempool_alloc(md->io_pool, GFP_NOIO);
492}
493
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100494static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 mempool_free(io, md->io_pool);
497}
498
Mike Snitzercfae7522016-04-11 12:05:38 -0400499static void free_tio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Mikulas Patockadba14162012-10-12 21:02:15 +0100501 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Mike Snitzer4cc96132016-05-12 16:28:10 -0400504int md_in_flight(struct mapped_device *md)
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000505{
506 return atomic_read(&md->pending[READ]) +
507 atomic_read(&md->pending[WRITE]);
508}
509
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800510static void start_io_acct(struct dm_io *io)
511{
512 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400513 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900514 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400515 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800516
517 io->start_time = jiffies;
518
Tejun Heo074a7ac2008-08-25 19:56:14 +0900519 cpu = part_stat_lock();
Jens Axboed62e26b2017-06-30 21:55:08 -0600520 part_round_stats(md->queue, cpu, &dm_disk(md)->part0);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900521 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100522 atomic_set(&dm_disk(md)->part0.in_flight[rw],
523 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400524
525 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500526 dm_stats_account_io(&md->stats, bio_data_dir(bio),
527 bio->bi_iter.bi_sector, bio_sectors(bio),
528 false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800529}
530
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000531static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800532{
533 struct mapped_device *md = io->md;
534 struct bio *bio = io->bio;
535 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800536 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800537 int rw = bio_data_dir(bio);
538
Jens Axboed62e26b2017-06-30 21:55:08 -0600539 generic_end_io_acct(md->queue, rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800540
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400541 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500542 dm_stats_account_io(&md->stats, bio_data_dir(bio),
543 bio->bi_iter.bi_sector, bio_sectors(bio),
544 true, duration, &io->stats_aux);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400545
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100546 /*
547 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200548 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100549 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100550 pending = atomic_dec_return(&md->pending[rw]);
551 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200552 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800553
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000554 /* nudge anyone waiting on suspend queue */
555 if (!pending)
556 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/*
560 * Add the bio to the list of deferred io.
561 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100562static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200564 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200566 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200568 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200569 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572/*
573 * Everyone (including functions in this file), should use this
574 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100575 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100577struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100579 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100581 return srcu_dereference(md->map, &md->io_barrier);
582}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100584void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
585{
586 srcu_read_unlock(&md->io_barrier, srcu_idx);
587}
588
589void dm_sync_table(struct mapped_device *md)
590{
591 synchronize_srcu(&md->io_barrier);
592 synchronize_rcu_expedited();
593}
594
595/*
596 * A fast alternative to dm_get_live_table/dm_put_live_table.
597 * The caller must not block between these two functions.
598 */
599static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
600{
601 rcu_read_lock();
602 return rcu_dereference(md->map);
603}
604
605static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
606{
607 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800610/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500611 * Open a table device so we can use it as a map destination.
612 */
613static int open_table_device(struct table_device *td, dev_t dev,
614 struct mapped_device *md)
615{
616 static char *_claim_ptr = "I belong to device-mapper";
617 struct block_device *bdev;
618
619 int r;
620
621 BUG_ON(td->dm_dev.bdev);
622
623 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
624 if (IS_ERR(bdev))
625 return PTR_ERR(bdev);
626
627 r = bd_link_disk_holder(bdev, dm_disk(md));
628 if (r) {
629 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
630 return r;
631 }
632
633 td->dm_dev.bdev = bdev;
Dan Williams817bf402017-04-12 13:37:44 -0700634 td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500635 return 0;
636}
637
638/*
639 * Close a table device that we've been using.
640 */
641static void close_table_device(struct table_device *td, struct mapped_device *md)
642{
643 if (!td->dm_dev.bdev)
644 return;
645
646 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
647 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
Dan Williams817bf402017-04-12 13:37:44 -0700648 put_dax(td->dm_dev.dax_dev);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500649 td->dm_dev.bdev = NULL;
Dan Williams817bf402017-04-12 13:37:44 -0700650 td->dm_dev.dax_dev = NULL;
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500651}
652
653static struct table_device *find_table_device(struct list_head *l, dev_t dev,
654 fmode_t mode) {
655 struct table_device *td;
656
657 list_for_each_entry(td, l, list)
658 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
659 return td;
660
661 return NULL;
662}
663
664int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
665 struct dm_dev **result) {
666 int r;
667 struct table_device *td;
668
669 mutex_lock(&md->table_devices_lock);
670 td = find_table_device(&md->table_devices, dev, mode);
671 if (!td) {
Mike Snitzer115485e2016-02-22 12:16:21 -0500672 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500673 if (!td) {
674 mutex_unlock(&md->table_devices_lock);
675 return -ENOMEM;
676 }
677
678 td->dm_dev.mode = mode;
679 td->dm_dev.bdev = NULL;
680
681 if ((r = open_table_device(td, dev, md))) {
682 mutex_unlock(&md->table_devices_lock);
683 kfree(td);
684 return r;
685 }
686
687 format_dev_t(td->dm_dev.name, dev);
688
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300689 refcount_set(&td->count, 1);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500690 list_add(&td->list, &md->table_devices);
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300691 } else {
692 refcount_inc(&td->count);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500693 }
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500694 mutex_unlock(&md->table_devices_lock);
695
696 *result = &td->dm_dev;
697 return 0;
698}
699EXPORT_SYMBOL_GPL(dm_get_table_device);
700
701void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
702{
703 struct table_device *td = container_of(d, struct table_device, dm_dev);
704
705 mutex_lock(&md->table_devices_lock);
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300706 if (refcount_dec_and_test(&td->count)) {
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500707 close_table_device(td, md);
708 list_del(&td->list);
709 kfree(td);
710 }
711 mutex_unlock(&md->table_devices_lock);
712}
713EXPORT_SYMBOL(dm_put_table_device);
714
715static void free_table_devices(struct list_head *devices)
716{
717 struct list_head *tmp, *next;
718
719 list_for_each_safe(tmp, next, devices) {
720 struct table_device *td = list_entry(tmp, struct table_device, list);
721
722 DMWARN("dm_destroy: %s still exists with %d references",
Elena Reshetovab0b4d7c2017-10-20 10:37:39 +0300723 td->dm_dev.name, refcount_read(&td->count));
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500724 kfree(td);
725 }
726}
727
728/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800729 * Get the geometry associated with a dm device
730 */
731int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
732{
733 *geo = md->geometry;
734
735 return 0;
736}
737
738/*
739 * Set the geometry of a device.
740 */
741int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
742{
743 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
744
745 if (geo->start > sz) {
746 DMWARN("Start sector is beyond the geometry limits.");
747 return -EINVAL;
748 }
749
750 md->geometry = *geo;
751
752 return 0;
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*-----------------------------------------------------------------
756 * CRUD START:
757 * A more elegant soln is in the works that uses the queue
758 * merge fn, unfortunately there are a couple of changes to
759 * the block layer that I want to make for this. So in the
760 * interests of getting something for people to use I give
761 * you this clearly demarcated crap.
762 *---------------------------------------------------------------*/
763
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800764static int __noflush_suspending(struct mapped_device *md)
765{
766 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * Decrements the number of outstanding ios that a bio has been
771 * cloned into, completing the original io if necc.
772 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200773static void dec_pending(struct dm_io *io, blk_status_t error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800775 unsigned long flags;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200776 blk_status_t io_error;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000777 struct bio *bio;
778 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800779
780 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100781 if (unlikely(error)) {
782 spin_lock_irqsave(&io->endio_lock, flags);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200783 if (!(io->status == BLK_STS_DM_REQUEUE &&
784 __noflush_suspending(md)))
785 io->status = error;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100786 spin_unlock_irqrestore(&io->endio_lock, flags);
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (atomic_dec_and_test(&io->io_count)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200790 if (io->status == BLK_STS_DM_REQUEUE) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800791 /*
792 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800793 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100794 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200795 if (__noflush_suspending(md))
796 bio_list_add_head(&md->deferred, io->bio);
797 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800798 /* noflush suspend was interrupted. */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200799 io->status = BLK_STS_IOERR;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100800 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800801 }
802
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200803 io_error = io->status;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000804 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200805 end_io_acct(io);
806 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100807
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200808 if (io_error == BLK_STS_DM_REQUEUE)
Tejun Heo6a8736d2010-09-08 18:07:00 +0200809 return;
810
Jens Axboe1eff9d32016-08-05 15:35:16 -0600811 if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100812 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200813 * Preflush done for flush with data, reissue
Mike Christie28a8f0d2016-06-05 14:32:25 -0500814 * without REQ_PREFLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100815 */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600816 bio->bi_opf &= ~REQ_PREFLUSH;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200817 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100818 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200819 /* done with normal IO or empty flush */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200820 bio->bi_status = io_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200821 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824}
825
Mike Snitzer4cc96132016-05-12 16:28:10 -0400826void disable_write_same(struct mapped_device *md)
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400827{
828 struct queue_limits *limits = dm_get_queue_limits(md);
829
830 /* device doesn't really support WRITE SAME, disable it */
831 limits->max_write_same_sectors = 0;
832}
833
Christoph Hellwigac62d622017-04-05 19:21:05 +0200834void disable_write_zeroes(struct mapped_device *md)
835{
836 struct queue_limits *limits = dm_get_queue_limits(md);
837
838 /* device doesn't really support WRITE ZEROES, disable it */
839 limits->max_write_zeroes_sectors = 0;
840}
841
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200842static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200844 blk_status_t error = bio->bi_status;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500845 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000846 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700847 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 dm_endio_fn endio = tio->ti->type->end_io;
849
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200850 if (unlikely(error == BLK_STS_TARGET)) {
Christoph Hellwigac62d622017-04-05 19:21:05 +0200851 if (bio_op(bio) == REQ_OP_WRITE_SAME &&
Christoph Hellwig74d46992017-08-23 19:10:32 +0200852 !bio->bi_disk->queue->limits.max_write_same_sectors)
Christoph Hellwigac62d622017-04-05 19:21:05 +0200853 disable_write_same(md);
854 if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
Christoph Hellwig74d46992017-08-23 19:10:32 +0200855 !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
Christoph Hellwigac62d622017-04-05 19:21:05 +0200856 disable_write_zeroes(md);
857 }
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400858
Christoph Hellwig1be56902017-06-03 09:38:03 +0200859 if (endio) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200860 int r = endio(tio->ti, bio, &error);
Christoph Hellwig1be56902017-06-03 09:38:03 +0200861 switch (r) {
862 case DM_ENDIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200863 error = BLK_STS_DM_REQUEUE;
Christoph Hellwig1be56902017-06-03 09:38:03 +0200864 /*FALLTHRU*/
865 case DM_ENDIO_DONE:
866 break;
867 case DM_ENDIO_INCOMPLETE:
868 /* The target will handle the io */
869 return;
870 default:
871 DMWARN("unimplemented target endio return value: %d", r);
872 BUG();
873 }
874 }
875
Mike Snitzercfae7522016-04-11 12:05:38 -0400876 free_tio(tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000877 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Mike Snitzer78d8e582015-06-26 10:01:13 -0400880/*
Mike Snitzer56a67df2010-08-12 04:14:10 +0100881 * Return maximum size of I/O possible at the supplied sector up to the current
882 * target boundary.
883 */
884static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100886 sector_t target_offset = dm_target_offset(ti, sector);
887
888 return ti->len - target_offset;
889}
890
891static sector_t max_io_len(sector_t sector, struct dm_target *ti)
892{
893 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +0100894 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /*
Mike Snitzer542f9032012-07-27 15:08:00 +0100897 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100899 if (ti->max_io_len) {
900 offset = dm_target_offset(ti, sector);
901 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
902 max_len = sector_div(offset, ti->max_io_len);
903 else
904 max_len = offset & (ti->max_io_len - 1);
905 max_len = ti->max_io_len - max_len;
906
907 if (len > max_len)
908 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
911 return len;
912}
913
Mike Snitzer542f9032012-07-27 15:08:00 +0100914int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
915{
916 if (len > UINT_MAX) {
917 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
918 (unsigned long long)len, UINT_MAX);
919 ti->error = "Maximum size of target IO is too large";
920 return -EINVAL;
921 }
922
923 ti->max_io_len = (uint32_t) len;
924
925 return 0;
926}
927EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
928
Dan Williamsf26c5712017-04-12 12:35:44 -0700929static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
930 sector_t sector, int *srcu_idx)
Toshi Kani545ed202016-06-22 17:54:53 -0600931{
Toshi Kani545ed202016-06-22 17:54:53 -0600932 struct dm_table *map;
933 struct dm_target *ti;
Toshi Kani545ed202016-06-22 17:54:53 -0600934
Dan Williamsf26c5712017-04-12 12:35:44 -0700935 map = dm_get_live_table(md, srcu_idx);
Toshi Kani545ed202016-06-22 17:54:53 -0600936 if (!map)
Dan Williamsf26c5712017-04-12 12:35:44 -0700937 return NULL;
Toshi Kani545ed202016-06-22 17:54:53 -0600938
939 ti = dm_table_find_target(map, sector);
940 if (!dm_target_is_valid(ti))
Dan Williamsf26c5712017-04-12 12:35:44 -0700941 return NULL;
942
943 return ti;
944}
945
946static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
947 long nr_pages, void **kaddr, pfn_t *pfn)
948{
949 struct mapped_device *md = dax_get_private(dax_dev);
950 sector_t sector = pgoff * PAGE_SECTORS;
951 struct dm_target *ti;
952 long len, ret = -EIO;
953 int srcu_idx;
954
955 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
956
957 if (!ti)
Toshi Kani545ed202016-06-22 17:54:53 -0600958 goto out;
Dan Williamsf26c5712017-04-12 12:35:44 -0700959 if (!ti->type->direct_access)
960 goto out;
961 len = max_io_len(sector, ti) / PAGE_SECTORS;
962 if (len < 1)
963 goto out;
964 nr_pages = min(len, nr_pages);
Toshi Kani545ed202016-06-22 17:54:53 -0600965 if (ti->type->direct_access)
Dan Williams817bf402017-04-12 13:37:44 -0700966 ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
967
Dan Williamsf26c5712017-04-12 12:35:44 -0700968 out:
Toshi Kani545ed202016-06-22 17:54:53 -0600969 dm_put_live_table(md, srcu_idx);
Dan Williamsf26c5712017-04-12 12:35:44 -0700970
971 return ret;
Toshi Kani545ed202016-06-22 17:54:53 -0600972}
973
Dan Williams7e026c82017-05-29 12:57:56 -0700974static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
975 void *addr, size_t bytes, struct iov_iter *i)
976{
977 struct mapped_device *md = dax_get_private(dax_dev);
978 sector_t sector = pgoff * PAGE_SECTORS;
979 struct dm_target *ti;
980 long ret = 0;
981 int srcu_idx;
982
983 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
984
985 if (!ti)
986 goto out;
987 if (!ti->type->dax_copy_from_iter) {
988 ret = copy_from_iter(addr, bytes, i);
989 goto out;
990 }
991 ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
992 out:
993 dm_put_live_table(md, srcu_idx);
994
995 return ret;
996}
997
Mikulas Patocka1dd40c32014-03-14 18:41:24 -0400998/*
999 * A target may call dm_accept_partial_bio only from the map routine. It is
NeilBrownc06b3e52017-11-21 08:44:35 -05001000 * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET.
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001001 *
1002 * dm_accept_partial_bio informs the dm that the target only wants to process
1003 * additional n_sectors sectors of the bio and the rest of the data should be
1004 * sent in a next bio.
1005 *
1006 * A diagram that explains the arithmetics:
1007 * +--------------------+---------------+-------+
1008 * | 1 | 2 | 3 |
1009 * +--------------------+---------------+-------+
1010 *
1011 * <-------------- *tio->len_ptr --------------->
1012 * <------- bi_size ------->
1013 * <-- n_sectors -->
1014 *
1015 * Region 1 was already iterated over with bio_advance or similar function.
1016 * (it may be empty if the target doesn't use bio_advance)
1017 * Region 2 is the remaining bio size that the target wants to process.
1018 * (it may be empty if region 1 is non-empty, although there is no reason
1019 * to make it empty)
1020 * The target requires that region 3 is to be sent in the next bio.
1021 *
1022 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1023 * the partially processed part (the sum of regions 1+2) must be the same for all
1024 * copies of the bio.
1025 */
1026void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1027{
1028 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1029 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Jens Axboe1eff9d32016-08-05 15:35:16 -06001030 BUG_ON(bio->bi_opf & REQ_PREFLUSH);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001031 BUG_ON(bi_size > *tio->len_ptr);
1032 BUG_ON(n_sectors > bi_size);
1033 *tio->len_ptr -= bi_size - n_sectors;
1034 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1035}
1036EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1037
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001038/*
Damien Le Moal10999302017-05-08 16:40:48 -07001039 * The zone descriptors obtained with a zone report indicate
1040 * zone positions within the target device. The zone descriptors
1041 * must be remapped to match their position within the dm device.
1042 * A target may call dm_remap_zone_report after completion of a
1043 * REQ_OP_ZONE_REPORT bio to remap the zone descriptors obtained
1044 * from the target device mapping to the dm device.
1045 */
1046void dm_remap_zone_report(struct dm_target *ti, struct bio *bio, sector_t start)
1047{
1048#ifdef CONFIG_BLK_DEV_ZONED
1049 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1050 struct bio *report_bio = tio->io->bio;
1051 struct blk_zone_report_hdr *hdr = NULL;
1052 struct blk_zone *zone;
1053 unsigned int nr_rep = 0;
1054 unsigned int ofst;
1055 struct bio_vec bvec;
1056 struct bvec_iter iter;
1057 void *addr;
1058
1059 if (bio->bi_status)
1060 return;
1061
1062 /*
1063 * Remap the start sector of the reported zones. For sequential zones,
1064 * also remap the write pointer position.
1065 */
1066 bio_for_each_segment(bvec, report_bio, iter) {
1067 addr = kmap_atomic(bvec.bv_page);
1068
1069 /* Remember the report header in the first page */
1070 if (!hdr) {
1071 hdr = addr;
1072 ofst = sizeof(struct blk_zone_report_hdr);
1073 } else
1074 ofst = 0;
1075
1076 /* Set zones start sector */
1077 while (hdr->nr_zones && ofst < bvec.bv_len) {
1078 zone = addr + ofst;
1079 if (zone->start >= start + ti->len) {
1080 hdr->nr_zones = 0;
1081 break;
1082 }
1083 zone->start = zone->start + ti->begin - start;
1084 if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
1085 if (zone->cond == BLK_ZONE_COND_FULL)
1086 zone->wp = zone->start + zone->len;
1087 else if (zone->cond == BLK_ZONE_COND_EMPTY)
1088 zone->wp = zone->start;
1089 else
1090 zone->wp = zone->wp + ti->begin - start;
1091 }
1092 ofst += sizeof(struct blk_zone);
1093 hdr->nr_zones--;
1094 nr_rep++;
1095 }
1096
1097 if (addr != hdr)
1098 kunmap_atomic(addr);
1099
1100 if (!hdr->nr_zones)
1101 break;
1102 }
1103
1104 if (hdr) {
1105 hdr->nr_zones = nr_rep;
1106 kunmap_atomic(hdr);
1107 }
1108
1109 bio_advance(report_bio, report_bio->bi_iter.bi_size);
1110
1111#else /* !CONFIG_BLK_DEV_ZONED */
1112 bio->bi_status = BLK_STS_NOTSUPP;
1113#endif
1114}
1115EXPORT_SYMBOL_GPL(dm_remap_zone_report);
1116
1117/*
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001118 * Flush current->bio_list when the target map method blocks.
1119 * This fixes deadlocks in snapshot and possibly in other targets.
1120 */
1121struct dm_offload {
1122 struct blk_plug plug;
1123 struct blk_plug_cb cb;
1124};
1125
1126static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
1127{
1128 struct dm_offload *o = container_of(cb, struct dm_offload, cb);
1129 struct bio_list list;
1130 struct bio *bio;
NeilBrownf5fe1b52017-03-10 17:00:47 +11001131 int i;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001132
1133 INIT_LIST_HEAD(&o->cb.list);
1134
1135 if (unlikely(!current->bio_list))
1136 return;
1137
NeilBrownf5fe1b52017-03-10 17:00:47 +11001138 for (i = 0; i < 2; i++) {
1139 list = current->bio_list[i];
1140 bio_list_init(&current->bio_list[i]);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001141
NeilBrownf5fe1b52017-03-10 17:00:47 +11001142 while ((bio = bio_list_pop(&list))) {
1143 struct bio_set *bs = bio->bi_pool;
NeilBrown47e0fb42017-06-18 14:38:57 +10001144 if (unlikely(!bs) || bs == fs_bio_set ||
1145 !bs->rescue_workqueue) {
NeilBrownf5fe1b52017-03-10 17:00:47 +11001146 bio_list_add(&current->bio_list[i], bio);
1147 continue;
1148 }
1149
1150 spin_lock(&bs->rescue_lock);
1151 bio_list_add(&bs->rescue_list, bio);
1152 queue_work(bs->rescue_workqueue, &bs->rescue_work);
1153 spin_unlock(&bs->rescue_lock);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001154 }
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001155 }
1156}
1157
1158static void dm_offload_start(struct dm_offload *o)
1159{
1160 blk_start_plug(&o->plug);
1161 o->cb.callback = flush_current_bio_list;
1162 list_add(&o->cb.list, &current->plug->cb_list);
1163}
1164
1165static void dm_offload_end(struct dm_offload *o)
1166{
1167 list_del(&o->cb.list);
1168 blk_finish_plug(&o->plug);
1169}
1170
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001171static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001174 sector_t sector;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001175 struct dm_offload o;
Mikulas Patockadba14162012-10-12 21:02:15 +01001176 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001177 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /*
1182 * Map the clone. If r == 0 we don't need to do
1183 * anything, the target has assumed ownership of
1184 * this io.
1185 */
1186 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001187 sector = clone->bi_iter.bi_sector;
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001188
1189 dm_offload_start(&o);
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001190 r = ti->type->map(ti, clone);
Mikulas Patockad67a5f42017-02-15 11:26:10 -05001191 dm_offload_end(&o);
1192
Christoph Hellwig846785e2017-06-03 09:38:02 +02001193 switch (r) {
1194 case DM_MAPIO_SUBMITTED:
1195 break;
1196 case DM_MAPIO_REMAPPED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* the bio has been remapped so dispatch it */
Christoph Hellwig74d46992017-08-23 19:10:32 +02001198 trace_block_bio_remap(clone->bi_disk->queue, clone,
1199 bio_dev(tio->io->bio), sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 generic_make_request(clone);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001201 break;
1202 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001203 dec_pending(tio->io, BLK_STS_IOERR);
1204 free_tio(tio);
1205 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +02001206 case DM_MAPIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001207 dec_pending(tio->io, BLK_STS_DM_REQUEUE);
Mike Snitzercfae7522016-04-11 12:05:38 -04001208 free_tio(tio);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001209 break;
1210 default:
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001211 DMWARN("unimplemented target map return value: %d", r);
1212 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
1215
1216struct clone_info {
1217 struct mapped_device *md;
1218 struct dm_table *map;
1219 struct bio *bio;
1220 struct dm_io *io;
1221 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001222 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223};
1224
Mikulas Patockae0d66092014-03-14 18:40:39 -04001225static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001226{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001227 bio->bi_iter.bi_sector = sector;
1228 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231/*
1232 * Creates a bio that consists of range of complete bvecs.
1233 */
Mike Snitzerc80914e2016-03-02 12:33:03 -05001234static int clone_bio(struct dm_target_io *tio, struct bio *bio,
1235 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Mikulas Patockadba14162012-10-12 21:02:15 +01001237 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001239 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Mikulas Patockae2460f22017-04-18 16:51:48 -04001241 if (unlikely(bio_integrity(bio) != NULL)) {
1242 int r;
1243
1244 if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1245 !dm_target_passes_integrity(tio->ti->type))) {
1246 DMWARN("%s: the target %s doesn't support integrity data.",
1247 dm_device_name(tio->io->md),
1248 tio->ti->type->name);
1249 return -EIO;
1250 }
1251
1252 r = bio_integrity_clone(clone, bio, GFP_NOIO);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001253 if (r < 0)
1254 return r;
1255 }
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001256
Damien Le Moal264c8692017-05-08 16:40:47 -07001257 if (bio_op(bio) != REQ_OP_ZONE_REPORT)
1258 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001259 clone->bi_iter.bi_size = to_bytes(len);
1260
Mikulas Patockae2460f22017-04-18 16:51:48 -04001261 if (unlikely(bio_integrity(bio) != NULL))
Dmitry Monakhovfbd08e72017-06-29 11:31:10 -07001262 bio_integrity_trim(clone);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001263
1264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Mike Snitzer318716d2017-11-22 14:56:12 -05001267static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
1268 unsigned target_bio_nr, gfp_t gfp_mask)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001269{
Mikulas Patockadba14162012-10-12 21:02:15 +01001270 struct dm_target_io *tio;
1271 struct bio *clone;
1272
Mike Snitzer318716d2017-11-22 14:56:12 -05001273 clone = bio_alloc_bioset(gfp_mask, 0, ci->md->bs);
1274 if (!clone)
1275 return NULL;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001276
Mike Snitzer318716d2017-11-22 14:56:12 -05001277 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001278 tio->io = ci->io;
1279 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001280 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001281
1282 return tio;
1283}
1284
Mike Snitzer318716d2017-11-22 14:56:12 -05001285static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1286 struct dm_target *ti, unsigned num_bios)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001287{
Mike Snitzer318716d2017-11-22 14:56:12 -05001288 struct dm_target_io *tio;
1289 int try;
1290
1291 if (!num_bios)
1292 return;
1293
1294 if (num_bios == 1) {
1295 tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1296 bio_list_add(blist, &tio->clone);
1297 return;
1298 }
1299
1300 for (try = 0; try < 2; try++) {
1301 int bio_nr;
1302 struct bio *bio;
1303
1304 if (try)
1305 mutex_lock(&ci->md->table_devices_lock);
1306 for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1307 tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1308 if (!tio)
1309 break;
1310
1311 bio_list_add(blist, &tio->clone);
1312 }
1313 if (try)
1314 mutex_unlock(&ci->md->table_devices_lock);
1315 if (bio_nr == num_bios)
1316 return;
1317
1318 while ((bio = bio_list_pop(blist))) {
1319 tio = container_of(bio, struct dm_target_io, clone);
1320 free_tio(tio);
1321 }
1322 }
1323}
1324
1325static void __clone_and_map_simple_bio(struct clone_info *ci,
1326 struct dm_target_io *tio, unsigned *len)
1327{
Mikulas Patockadba14162012-10-12 21:02:15 +01001328 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001329
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001330 tio->len_ptr = len;
1331
Junichi Nomura99778272014-10-03 11:55:16 +00001332 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001333 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001334 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001335
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001336 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001337}
1338
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001339static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001340 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001341{
Mike Snitzer318716d2017-11-22 14:56:12 -05001342 struct bio_list blist = BIO_EMPTY_LIST;
1343 struct bio *bio;
1344 struct dm_target_io *tio;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001345
Mike Snitzer318716d2017-11-22 14:56:12 -05001346 alloc_multiple_bios(&blist, ci, ti, num_bios);
1347
1348 while ((bio = bio_list_pop(&blist))) {
1349 tio = container_of(bio, struct dm_target_io, clone);
1350 __clone_and_map_simple_bio(ci, tio, len);
1351 }
Mike Snitzer06a426c2010-08-12 04:14:09 +01001352}
1353
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001354static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001355{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001356 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001357 struct dm_target *ti;
1358
Mike Snitzerb372d362010-09-08 18:07:01 +02001359 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001360 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001361 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001362
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001363 return 0;
1364}
1365
Mike Snitzerc80914e2016-03-02 12:33:03 -05001366static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
NeilBrownf31c21e2017-11-22 14:25:18 +11001367 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001368{
Mikulas Patockadba14162012-10-12 21:02:15 +01001369 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001370 struct dm_target_io *tio;
NeilBrownf31c21e2017-11-22 14:25:18 +11001371 int r;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001372
Mike Snitzer318716d2017-11-22 14:56:12 -05001373 tio = alloc_tio(ci, ti, 0, GFP_NOIO);
NeilBrownf31c21e2017-11-22 14:25:18 +11001374 tio->len_ptr = len;
1375 r = clone_bio(tio, bio, sector, *len);
1376 if (r < 0) {
1377 free_tio(tio);
1378 return r;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001379 }
NeilBrownf31c21e2017-11-22 14:25:18 +11001380 __map_bio(tio);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001381
NeilBrownf31c21e2017-11-22 14:25:18 +11001382 return 0;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001383}
1384
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001385typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001386
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001387static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001388{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001389 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001390}
1391
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001392static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001393{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001394 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001395}
1396
Christoph Hellwigac62d622017-04-05 19:21:05 +02001397static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1398{
1399 return ti->num_write_zeroes_bios;
1400}
1401
Mike Snitzer23508a92012-12-21 20:23:37 +00001402typedef bool (*is_split_required_fn)(struct dm_target *ti);
1403
1404static bool is_split_required_for_discard(struct dm_target *ti)
1405{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001406 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001407}
1408
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001409static int __send_changing_extent_only(struct clone_info *ci,
1410 get_num_bios_fn get_num_bios,
1411 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001412{
1413 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001414 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001415 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001416
Mike Snitzera79245b2010-08-12 04:14:24 +01001417 do {
1418 ti = dm_table_find_target(ci->map, ci->sector);
1419 if (!dm_target_is_valid(ti))
1420 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001421
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001422 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001423 * Even though the device advertised support for this type of
1424 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001425 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001426 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001427 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001428 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1429 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001430 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001431
Mike Snitzer23508a92012-12-21 20:23:37 +00001432 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001433 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001434 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001435 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001436
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001437 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001438
1439 ci->sector += len;
1440 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001441
1442 return 0;
1443}
1444
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001445static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001446{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001447 return __send_changing_extent_only(ci, get_num_discard_bios,
1448 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001449}
1450
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001451static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001452{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001453 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001454}
1455
Christoph Hellwigac62d622017-04-05 19:21:05 +02001456static int __send_write_zeroes(struct clone_info *ci)
1457{
1458 return __send_changing_extent_only(ci, get_num_write_zeroes_bios, NULL);
1459}
1460
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001461/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001462 * Select the correct strategy for processing a non-flush bio.
1463 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001464static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Mikulas Patockadba14162012-10-12 21:02:15 +01001466 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001467 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001468 unsigned len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001469 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Mike Christiee6047142016-06-05 14:32:04 -05001471 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001472 return __send_discard(ci);
Mike Christiee6047142016-06-05 14:32:04 -05001473 else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001474 return __send_write_same(ci);
Christoph Hellwigac62d622017-04-05 19:21:05 +02001475 else if (unlikely(bio_op(bio) == REQ_OP_WRITE_ZEROES))
1476 return __send_write_zeroes(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001477
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001478 ti = dm_table_find_target(ci->map, ci->sector);
1479 if (!dm_target_is_valid(ti))
1480 return -EIO;
1481
Damien Le Moal264c8692017-05-08 16:40:47 -07001482 if (bio_op(bio) == REQ_OP_ZONE_REPORT)
1483 len = ci->sector_count;
1484 else
1485 len = min_t(sector_t, max_io_len(ci->sector, ti),
1486 ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001487
Mike Snitzerc80914e2016-03-02 12:33:03 -05001488 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1489 if (r < 0)
1490 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001492 ci->sector += len;
1493 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001499 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001501static void __split_and_process_bio(struct mapped_device *md,
1502 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001505 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001507 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001508 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001509 return;
1510 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001511
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001512 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 ci.io = alloc_io(md);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001515 ci.io->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 atomic_set(&ci.io->io_count, 1);
1517 ci.io->bio = bio;
1518 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001519 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001520 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001522 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001523
Jens Axboe1eff9d32016-08-05 15:35:16 -06001524 if (bio->bi_opf & REQ_PREFLUSH) {
Mike Snitzerb372d362010-09-08 18:07:01 +02001525 ci.bio = &ci.md->flush_bio;
1526 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001527 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001528 /* dec_pending submits any data associated with flush */
Damien Le Moala4aa5e52017-05-08 16:40:46 -07001529 } else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1530 ci.bio = bio;
1531 ci.sector_count = 0;
1532 error = __split_and_process_non_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001533 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001534 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001535 ci.sector_count = bio_sectors(bio);
NeilBrown18a25da2017-09-06 09:43:28 +10001536 while (ci.sector_count && !error) {
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001537 error = __split_and_process_non_flush(&ci);
NeilBrown18a25da2017-09-06 09:43:28 +10001538 if (current->bio_list && ci.sector_count && !error) {
1539 /*
1540 * Remainder must be passed to generic_make_request()
1541 * so that it gets handled *after* bios already submitted
1542 * have been completely processed.
1543 * We take a clone of the original to store in
1544 * ci.io->bio to be used by end_io_acct() and
1545 * for dec_pending to use for completion handling.
1546 * As this path is not used for REQ_OP_ZONE_REPORT,
1547 * the usage of io->bio in dm_remap_zone_report()
1548 * won't be affected by this reassignment.
1549 */
1550 struct bio *b = bio_clone_bioset(bio, GFP_NOIO,
1551 md->queue->bio_split);
1552 ci.io->bio = b;
1553 bio_advance(bio, (bio_sectors(bio) - ci.sector_count) << 9);
1554 bio_chain(b, bio);
1555 generic_make_request(bio);
1556 break;
1557 }
1558 }
Tejun Heod87f4c12010-09-03 11:56:19 +02001559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /* drop the extra reference count */
Bart Van Assche54385bf2017-08-09 11:32:10 -07001562 dec_pending(ci.io, errno_to_blk_status(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564/*-----------------------------------------------------------------
1565 * CRUD END
1566 *---------------------------------------------------------------*/
1567
1568/*
NeilBrown18a25da2017-09-06 09:43:28 +10001569 * The request function that remaps the bio to one target and
1570 * splits off any remainder.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 */
Jens Axboedece1632015-11-05 10:41:16 -07001572static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Kevin Corry12f03a42006-02-01 03:04:52 -08001574 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001576 int srcu_idx;
1577 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001579 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Jens Axboed62e26b2017-06-30 21:55:08 -06001581 generic_start_io_acct(q, rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001582
Tejun Heo6a8736d2010-09-08 18:07:00 +02001583 /* if we're suspended, we have to queue this io for later */
1584 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001585 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Jens Axboe1eff9d32016-08-05 15:35:16 -06001587 if (!(bio->bi_opf & REQ_RAHEAD))
Tejun Heo6a8736d2010-09-08 18:07:00 +02001588 queue_io(md, bio);
1589 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001590 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001591 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001594 __split_and_process_bio(md, map, bio);
1595 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001596 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static int dm_any_congested(void *congested_data, int bdi_bits)
1600{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001601 int r = bdi_bits;
1602 struct mapped_device *md = congested_data;
1603 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001605 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05001606 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001607 /*
Mike Snitzere522c032016-02-02 22:35:06 -05001608 * With request-based DM we only need to check the
1609 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001610 */
Jan Karadc3b17c2017-02-02 15:56:50 +01001611 r = md->queue->backing_dev_info->wb.state & bdi_bits;
Mike Snitzere522c032016-02-02 22:35:06 -05001612 } else {
1613 map = dm_get_live_table_fast(md);
1614 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001615 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05001616 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001617 }
1618 }
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return r;
1621}
1622
1623/*-----------------------------------------------------------------
1624 * An IDR is used to keep track of allocated minor numbers.
1625 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001626static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001628 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001630 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633/*
1634 * See if the device with a specific minor # is free.
1635 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001636static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001638 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 if (minor >= (1 << MINORBITS))
1641 return -EINVAL;
1642
Tejun Heoc9d76be2013-02-27 17:04:26 -08001643 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001644 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Tejun Heoc9d76be2013-02-27 17:04:26 -08001646 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001648 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001649 idr_preload_end();
1650 if (r < 0)
1651 return r == -ENOSPC ? -EBUSY : r;
1652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001655static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001657 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Tejun Heoc9d76be2013-02-27 17:04:26 -08001659 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001660 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Tejun Heoc9d76be2013-02-27 17:04:26 -08001662 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001664 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001665 idr_preload_end();
1666 if (r < 0)
1667 return r;
1668 *minor = r;
1669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001672static const struct block_device_operations dm_blk_dops;
Dan Williamsf26c5712017-04-12 12:35:44 -07001673static const struct dax_operations dm_dax_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Mikulas Patocka53d59142009-04-02 19:55:37 +01001675static void dm_wq_work(struct work_struct *work);
1676
Mike Snitzer4cc96132016-05-12 16:28:10 -04001677void dm_init_md_queue(struct mapped_device *md)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001678{
1679 /*
Mikulas Patockaad5f4982015-10-27 19:06:55 -04001680 * Initialize data that will only be used by a non-blk-mq DM queue
1681 * - must do so here (in alloc_dev callchain) before queue is used
1682 */
1683 md->queue->queuedata = md;
Jan Karadc3b17c2017-02-02 15:56:50 +01001684 md->queue->backing_dev_info->congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001685}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001686
Mike Snitzer4cc96132016-05-12 16:28:10 -04001687void dm_init_normal_md_queue(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001688{
Mike Snitzer17e149b2015-03-11 15:01:09 -04001689 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001690 dm_init_md_queue(md);
1691
1692 /*
1693 * Initialize aspects of queue that aren't relevant for blk-mq
1694 */
Jan Karadc3b17c2017-02-02 15:56:50 +01001695 md->queue->backing_dev_info->congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001696}
1697
Mike Snitzer0f209722015-04-28 11:50:29 -04001698static void cleanup_mapped_device(struct mapped_device *md)
1699{
Mike Snitzer0f209722015-04-28 11:50:29 -04001700 if (md->wq)
1701 destroy_workqueue(md->wq);
1702 if (md->kworker_task)
1703 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02001704 mempool_destroy(md->io_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04001705 if (md->bs)
1706 bioset_free(md->bs);
1707
Dan Williamsf26c5712017-04-12 12:35:44 -07001708 if (md->dax_dev) {
1709 kill_dax(md->dax_dev);
1710 put_dax(md->dax_dev);
1711 md->dax_dev = NULL;
1712 }
1713
Mike Snitzer0f209722015-04-28 11:50:29 -04001714 if (md->disk) {
1715 spin_lock(&_minor_lock);
1716 md->disk->private_data = NULL;
1717 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04001718 del_gendisk(md->disk);
1719 put_disk(md->disk);
1720 }
1721
1722 if (md->queue)
1723 blk_cleanup_queue(md->queue);
1724
Tahsin Erdogand09960b2016-10-10 05:35:19 -07001725 cleanup_srcu_struct(&md->io_barrier);
1726
Mike Snitzer0f209722015-04-28 11:50:29 -04001727 if (md->bdev) {
1728 bdput(md->bdev);
1729 md->bdev = NULL;
1730 }
Mike Snitzer4cc96132016-05-12 16:28:10 -04001731
1732 dm_mq_cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735/*
1736 * Allocate and initialise a blank device with a given minor.
1737 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001738static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Mike Snitzer115485e2016-02-22 12:16:21 -05001740 int r, numa_node_id = dm_get_numa_node();
Dan Williamsf26c5712017-04-12 12:35:44 -07001741 struct dax_device *dax_dev;
Mike Snitzer115485e2016-02-22 12:16:21 -05001742 struct mapped_device *md;
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001743 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Mikulas Patocka856eb092017-10-31 19:33:02 -04001745 md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (!md) {
1747 DMWARN("unable to allocate device, out of memory.");
1748 return NULL;
1749 }
1750
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001751 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001752 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001755 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001756 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001757 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001758 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001760 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001762 r = init_srcu_struct(&md->io_barrier);
1763 if (r < 0)
1764 goto bad_io_barrier;
1765
Mike Snitzer115485e2016-02-22 12:16:21 -05001766 md->numa_node_id = numa_node_id;
Mike Snitzer4cc96132016-05-12 16:28:10 -04001767 md->use_blk_mq = dm_use_blk_mq_default();
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001768 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01001769 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001770 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001771 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001772 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001773 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001775 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001777 atomic_set(&md->uevent_seq, 0);
1778 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001779 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001780 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Mike Snitzer115485e2016-02-22 12:16:21 -05001782 md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04001784 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001786 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001787
Mike Snitzer115485e2016-02-22 12:16:21 -05001788 md->disk = alloc_disk_node(1, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04001790 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001792 atomic_set(&md->pending[0], 0);
1793 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001794 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001795 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001796 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05001797 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001798 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 md->disk->major = _major;
1801 md->disk->first_minor = minor;
1802 md->disk->fops = &dm_blk_dops;
1803 md->disk->queue = md->queue;
1804 md->disk->private_data = md;
1805 sprintf(md->disk->disk_name, "dm-%d", minor);
Dan Williamsf26c5712017-04-12 12:35:44 -07001806
1807 dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1808 if (!dax_dev)
1809 goto bad;
1810 md->dax_dev = dax_dev;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001813 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Tejun Heo670368a2013-07-30 08:40:21 -04001815 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001816 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04001817 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00001818
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001819 md->bdev = bdget_disk(md->disk, 0);
1820 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04001821 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001822
Ming Lei3a83f462016-11-22 08:57:21 -07001823 bio_init(&md->flush_bio, NULL, 0);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001824 bio_set_dev(&md->flush_bio, md->bdev);
Jan Karaff0361b2017-05-31 09:44:32 +02001825 md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
Tejun Heo6a8736d2010-09-08 18:07:00 +02001826
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001827 dm_stats_init(&md->stats);
1828
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001829 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001830 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001831 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001832 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001833
1834 BUG_ON(old_md != MINOR_ALLOCED);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return md;
1837
Mike Snitzer0f209722015-04-28 11:50:29 -04001838bad:
1839 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001840bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001842bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001843 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001844bad_module_get:
Mikulas Patocka856eb092017-10-31 19:33:02 -04001845 kvfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return NULL;
1847}
1848
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001849static void unlock_fs(struct mapped_device *md);
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851static void free_dev(struct mapped_device *md)
1852{
Tejun Heof331c022008-09-03 09:01:48 +02001853 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001854
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001855 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001856
Mike Snitzer0f209722015-04-28 11:50:29 -04001857 cleanup_mapped_device(md);
Mike Snitzer0f209722015-04-28 11:50:29 -04001858
1859 free_table_devices(&md->table_devices);
1860 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04001861 free_minor(minor);
1862
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001863 module_put(THIS_MODULE);
Mikulas Patocka856eb092017-10-31 19:33:02 -04001864 kvfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001867static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1868{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001869 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001870
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001871 if (md->bs) {
1872 /* The md already has necessary mempools. */
Toshi Kani545ed202016-06-22 17:54:53 -06001873 if (dm_table_bio_based(t)) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001874 /*
1875 * Reload bioset because front_pad may have changed
1876 * because a different table was loaded.
1877 */
1878 bioset_free(md->bs);
1879 md->bs = p->bs;
1880 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001881 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001882 /*
1883 * There's no need to reload with request-based dm
1884 * because the size of front_pad doesn't change.
1885 * Note for future: If you are to reload bioset,
1886 * prep-ed requests in the queue may refer
1887 * to bio from the old bioset, so you must walk
1888 * through the queue to unprep.
1889 */
1890 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001891 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001892
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001893 BUG_ON(!p || md->io_pool || md->bs);
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04001894
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001895 md->io_pool = p->io_pool;
1896 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001897 md->bs = p->bs;
1898 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04001899
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001900out:
Mike Snitzer02233342015-03-10 23:49:26 -04001901 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001902 dm_table_free_md_mempools(t);
1903}
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905/*
1906 * Bind a table to the device.
1907 */
1908static void event_callback(void *context)
1909{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001910 unsigned long flags;
1911 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct mapped_device *md = (struct mapped_device *) context;
1913
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001914 spin_lock_irqsave(&md->uevent_lock, flags);
1915 list_splice_init(&md->uevent_list, &uevents);
1916 spin_unlock_irqrestore(&md->uevent_lock, flags);
1917
Tejun Heoed9e1982008-08-25 19:56:05 +09001918 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 atomic_inc(&md->event_nr);
1921 wake_up(&md->eventq);
Mikulas Patocka62e08242017-09-20 07:29:49 -04001922 dm_issue_global_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Mike Snitzerc2176492011-01-13 19:53:46 +00001925/*
1926 * Protected by md->suspend_lock obtained by dm_swap_table().
1927 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001928static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Bart Van Assche1ea06542017-04-27 10:11:21 -07001930 lockdep_assert_held(&md->suspend_lock);
1931
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001932 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001934 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001937/*
1938 * Returns old map, which caller must destroy.
1939 */
1940static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1941 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001943 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02001944 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 sector_t size;
1946
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07001947 lockdep_assert_held(&md->suspend_lock);
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001950
1951 /*
1952 * Wipe any geometry if the size of the table changed.
1953 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001954 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001955 memset(&md->geometry, 0, sizeof(md->geometry));
1956
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001957 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001959 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001960
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001961 /*
1962 * The queue hasn't been stopped yet, if the old table type wasn't
1963 * for request-based during suspension. So stop it to prevent
1964 * I/O mapping before resume.
1965 * This must be done before setting the queue restrictions,
1966 * because request-based dm may be run just after the setting.
1967 */
Mike Snitzer16f12262016-01-31 17:22:27 -05001968 if (dm_table_request_based(t)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001969 dm_stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05001970 /*
1971 * Leverage the fact that request-based DM targets are
1972 * immutable singletons and establish md->immutable_target
1973 * - used to optimize both dm_request_fn and dm_mq_queue_rq
1974 */
1975 md->immutable_target = dm_table_get_immutable_target(t);
1976 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001977
1978 __bind_mempools(md, t);
1979
Eric Dumazeta12f5d42014-11-23 09:34:29 -08001980 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -05001981 rcu_assign_pointer(md->map, (void *)t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001982 md->immutable_target_type = dm_table_get_immutable_target_type(t);
1983
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001984 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01001985 if (old_map)
1986 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001987
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001988 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Alasdair G Kergona7940152009-12-10 23:52:23 +00001991/*
1992 * Returns unbound table for the caller to free.
1993 */
1994static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08001996 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00001999 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302002 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002003 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002004
2005 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
2008/*
2009 * Constructor for a new device.
2010 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002011int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 struct mapped_device *md;
2014
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002015 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (!md)
2017 return -ENXIO;
2018
Milan Broz784aae72009-01-06 03:05:12 +00002019 dm_sysfs_init(md);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 *result = md;
2022 return 0;
2023}
2024
Mike Snitzera5664da2010-08-12 04:14:01 +01002025/*
2026 * Functions to manage md->type.
2027 * All are required to hold md->type_lock.
2028 */
2029void dm_lock_md_type(struct mapped_device *md)
2030{
2031 mutex_lock(&md->type_lock);
2032}
2033
2034void dm_unlock_md_type(struct mapped_device *md)
2035{
2036 mutex_unlock(&md->type_lock);
2037}
2038
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002039void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
Mike Snitzera5664da2010-08-12 04:14:01 +01002040{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002041 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002042 md->type = type;
2043}
2044
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002045enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
Mike Snitzera5664da2010-08-12 04:14:01 +01002046{
2047 return md->type;
2048}
2049
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002050struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2051{
2052 return md->immutable_target_type;
2053}
2054
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002055/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002056 * The queue_limits are only valid as long as you have a reference
2057 * count on 'md'.
2058 */
2059struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2060{
2061 BUG_ON(!atomic_read(&md->holders));
2062 return &md->queue->limits;
2063}
2064EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2065
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002066/*
2067 * Setup the DM device's queue based on md's type
2068 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002069int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002070{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002071 int r;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002072 enum dm_queue_mode type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002073
Toshi Kani545ed202016-06-22 17:54:53 -06002074 switch (type) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002075 case DM_TYPE_REQUEST_BASED:
Christoph Hellwigeb8db832017-01-22 18:32:46 +01002076 r = dm_old_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002077 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002078 DMERR("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002079 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002080 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002081 break;
2082 case DM_TYPE_MQ_REQUEST_BASED:
Mike Snitzere83068a2016-05-24 21:16:51 -04002083 r = dm_mq_init_request_queue(md, t);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002084 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002085 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002086 return r;
2087 }
2088 break;
2089 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002090 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05002091 dm_init_normal_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002092 blk_queue_make_request(md->queue, dm_make_request);
Toshi Kani545ed202016-06-22 17:54:53 -06002093
2094 if (type == DM_TYPE_DAX_BIO_BASED)
2095 queue_flag_set_unlocked(QUEUE_FLAG_DAX, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002096 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002097 case DM_TYPE_NONE:
2098 WARN_ON_ONCE(true);
2099 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002100 }
2101
2102 return 0;
2103}
2104
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002105struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 unsigned minor = MINOR(dev);
2109
2110 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2111 return NULL;
2112
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002113 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 md = idr_find(&_minor_idr, minor);
Mike Snitzer49de5762017-11-06 16:40:10 -05002116 if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
2117 test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2118 md = NULL;
2119 goto out;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002120 }
Mike Snitzer49de5762017-11-06 16:40:10 -05002121 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002122out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002123 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
David Teigland637842c2006-01-06 00:20:00 -08002125 return md;
2126}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002127EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002128
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002129void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002130{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002131 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
2134void dm_set_mdptr(struct mapped_device *md, void *ptr)
2135{
2136 md->interface_ptr = ptr;
2137}
2138
2139void dm_get(struct mapped_device *md)
2140{
2141 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002142 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002145int dm_hold(struct mapped_device *md)
2146{
2147 spin_lock(&_minor_lock);
2148 if (test_bit(DMF_FREEING, &md->flags)) {
2149 spin_unlock(&_minor_lock);
2150 return -EBUSY;
2151 }
2152 dm_get(md);
2153 spin_unlock(&_minor_lock);
2154 return 0;
2155}
2156EXPORT_SYMBOL_GPL(dm_hold);
2157
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002158const char *dm_device_name(struct mapped_device *md)
2159{
2160 return md->name;
2161}
2162EXPORT_SYMBOL_GPL(dm_device_name);
2163
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002164static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Bart Van Assche3b785fb2016-08-31 15:17:49 -07002166 struct request_queue *q = dm_get_md_queue(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002167 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002168 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002170 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002171
Mike Snitzer63a4f062015-03-23 17:01:43 -04002172 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002173 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2174 set_bit(DMF_FREEING, &md->flags);
2175 spin_unlock(&_minor_lock);
2176
Bart Van Assche2e91c362016-11-18 14:26:47 -08002177 blk_set_queue_dying(q);
Bart Van Assche3b785fb2016-08-31 15:17:49 -07002178
Mike Snitzer02233342015-03-10 23:49:26 -04002179 if (dm_request_based(md) && md->kworker_task)
Petr Mladek39891442016-10-11 13:55:20 -07002180 kthread_flush_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002181
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002182 /*
2183 * Take suspend_lock so that presuspend and postsuspend methods
2184 * do not race with internal suspend.
2185 */
2186 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002187 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002188 if (!dm_suspended_md(md)) {
2189 dm_table_presuspend_targets(map);
2190 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002192 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2193 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002194 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002195
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002196 /*
2197 * Rare, but there may be I/O requests still going to complete,
2198 * for example. Wait for all references to disappear.
2199 * No one should increment the reference count of the mapped_device,
2200 * after the mapped_device state becomes DMF_FREEING.
2201 */
2202 if (wait)
2203 while (atomic_read(&md->holders))
2204 msleep(1);
2205 else if (atomic_read(&md->holders))
2206 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2207 dm_device_name(md), atomic_read(&md->holders));
2208
2209 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002210 dm_table_destroy(__unbind(md));
2211 free_dev(md);
2212}
2213
2214void dm_destroy(struct mapped_device *md)
2215{
2216 __dm_destroy(md, true);
2217}
2218
2219void dm_destroy_immediate(struct mapped_device *md)
2220{
2221 __dm_destroy(md, false);
2222}
2223
2224void dm_put(struct mapped_device *md)
2225{
2226 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
Edward Goggin79eb8852007-05-09 02:32:56 -07002228EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002230static int dm_wait_for_completion(struct mapped_device *md, long task_state)
Milan Broz46125c12008-02-08 02:10:30 +00002231{
2232 int r = 0;
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002233 DEFINE_WAIT(wait);
Milan Broz46125c12008-02-08 02:10:30 +00002234
2235 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002236 prepare_to_wait(&md->wait, &wait, task_state);
Milan Broz46125c12008-02-08 02:10:30 +00002237
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002238 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002239 break;
2240
Bart Van Asschee3fabdf2016-08-31 15:16:22 -07002241 if (signal_pending_state(task_state, current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002242 r = -EINTR;
2243 break;
2244 }
2245
2246 io_schedule();
2247 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07002248 finish_wait(&md->wait, &wait);
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002249
Milan Broz46125c12008-02-08 02:10:30 +00002250 return r;
2251}
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253/*
2254 * Process the deferred bios
2255 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002256static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Mikulas Patockaef208582009-04-02 19:55:38 +01002258 struct mapped_device *md = container_of(work, struct mapped_device,
2259 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002260 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002261 int srcu_idx;
2262 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002264 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002265
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002266 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002267 spin_lock_irq(&md->deferred_lock);
2268 c = bio_list_pop(&md->deferred);
2269 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002270
Tejun Heo6a8736d2010-09-08 18:07:00 +02002271 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002272 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002273
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002274 if (dm_request_based(md))
2275 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002276 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002277 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002278 }
Milan Broz73d410c2008-02-08 02:10:25 +00002279
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002280 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002283static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002284{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002285 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002286 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002287 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002288}
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002291 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002293struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Mike Christie87eb5b22013-03-01 22:45:48 +00002295 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002296 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002297 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Daniel Walkere61290a2008-02-08 02:10:08 +00002299 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002302 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Mike Snitzer3ae70652012-09-26 23:45:45 +01002305 /*
2306 * If the new table has no data devices, retain the existing limits.
2307 * This helps multipath with queue_if_no_path if all paths disappear,
2308 * then new I/O is queued based on these limits, and then some paths
2309 * reappear.
2310 */
2311 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002312 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002313 if (live_map)
2314 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002315 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002316 }
2317
Mike Christie87eb5b22013-03-01 22:45:48 +00002318 if (!live_map) {
2319 r = dm_calculate_queue_limits(table, &limits);
2320 if (r) {
2321 map = ERR_PTR(r);
2322 goto out;
2323 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002324 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002325
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002326 map = __bind(md, table, &limits);
Mikulas Patocka62e08242017-09-20 07:29:49 -04002327 dm_issue_global_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002329out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002330 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002331 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334/*
2335 * Functions to lock and unlock any filesystem running on the
2336 * device.
2337 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002338static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002340 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002343
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002344 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002345 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002346 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002347 md->frozen_sb = NULL;
2348 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002349 }
2350
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002351 set_bit(DMF_FROZEN, &md->flags);
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return 0;
2354}
2355
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002356static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002358 if (!test_bit(DMF_FROZEN, &md->flags))
2359 return;
2360
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002361 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002363 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
2366/*
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002367 * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2368 * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2369 * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2370 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04002371 * If __dm_suspend returns 0, the device is completely quiescent
2372 * now. There is no request-processing activity. All new requests
2373 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002374 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04002375static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002376 unsigned suspend_flags, long task_state,
Mike Snitzereaf9a732016-08-02 13:07:20 -04002377 int dmf_suspended_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002379 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2380 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2381 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Bart Van Assche5a8f1f82016-08-31 15:17:04 -07002383 lockdep_assert_held(&md->suspend_lock);
2384
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002385 /*
2386 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2387 * This flag is cleared before dm_suspend returns.
2388 */
2389 if (noflush)
2390 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Bart Van Assche86331f32017-04-27 10:11:26 -07002391 else
2392 pr_debug("%s: suspending with flush\n", dm_device_name(md));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002393
Mike Snitzerd67ee212014-10-28 20:13:31 -04002394 /*
2395 * This gets reverted if there's an error later and the targets
2396 * provide the .presuspend_undo hook.
2397 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002398 dm_table_presuspend_targets(map);
2399
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002400 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002401 * Flush I/O to the device.
2402 * Any I/O submitted after lock_fs() may not be flushed.
2403 * noflush takes precedence over do_lockfs.
2404 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002405 */
2406 if (!noflush && do_lockfs) {
2407 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002408 if (r) {
2409 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002410 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04002411 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002415 * Here we must make sure that no processes are submitting requests
2416 * to target drivers i.e. no one may be executing
2417 * __split_and_process_bio. This is called from dm_request and
2418 * dm_wq_work.
2419 *
2420 * To get all processes out of __split_and_process_bio in dm_request,
2421 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002422 * __split_and_process_bio from dm_request and quiesce the thread
2423 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2424 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002426 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002427 if (map)
2428 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002430 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002431 * Stop md->queue before flushing md->wq in case request-based
2432 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002433 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002434 if (dm_request_based(md)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002435 dm_stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04002436 if (md->kworker_task)
Petr Mladek39891442016-10-11 13:55:20 -07002437 kthread_flush_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002438 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002439
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002440 flush_workqueue(md->wq);
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002443 * At this point no more requests are entering target request routines.
2444 * We call dm_wait_for_completion to wait for all existing requests
2445 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 */
Bart Van Asscheb48633f2016-08-31 15:16:02 -07002447 r = dm_wait_for_completion(md, task_state);
Mike Snitzereaf9a732016-08-02 13:07:20 -04002448 if (!r)
2449 set_bit(dmf_suspended_flag, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Milan Broz6d6f10d2008-02-08 02:10:22 +00002451 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002452 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002453 if (map)
2454 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002457 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002458 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002459
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002460 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002461 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002462
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002463 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002464 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002465 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002466 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002467
Mike Snitzerffcc3932014-10-28 18:34:52 -04002468 return r;
2469}
2470
2471/*
2472 * We need to be able to change a mapping table under a mounted
2473 * filesystem. For example we might want to move some data in
2474 * the background. Before the table can be swapped with
2475 * dm_bind_table, dm_suspend must be called to flush any in
2476 * flight bios and ensure that any further io gets deferred.
2477 */
2478/*
2479 * Suspend mechanism in request-based dm.
2480 *
2481 * 1. Flush all I/Os by lock_fs() if needed.
2482 * 2. Stop dispatching any I/O by stopping the request_queue.
2483 * 3. Wait for all in-flight I/Os to be completed or requeued.
2484 *
2485 * To abort suspend, start the request_queue.
2486 */
2487int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2488{
2489 struct dm_table *map = NULL;
2490 int r = 0;
2491
2492retry:
2493 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2494
2495 if (dm_suspended_md(md)) {
2496 r = -EINVAL;
2497 goto out_unlock;
2498 }
2499
2500 if (dm_suspended_internally_md(md)) {
2501 /* already internally suspended, wait for internal resume */
2502 mutex_unlock(&md->suspend_lock);
2503 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2504 if (r)
2505 return r;
2506 goto retry;
2507 }
2508
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002509 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002510
Mike Snitzereaf9a732016-08-02 13:07:20 -04002511 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002512 if (r)
2513 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002514
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002515 dm_table_postsuspend_targets(map);
2516
Alasdair G Kergond2874832006-11-08 17:44:43 -08002517out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002518 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002519 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Mike Snitzerffcc3932014-10-28 18:34:52 -04002522static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002524 if (map) {
2525 int r = dm_table_resume_targets(map);
2526 if (r)
2527 return r;
2528 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002529
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002530 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002531
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002532 /*
2533 * Flushing deferred I/Os must be done after targets are resumed
2534 * so that mapping of targets can work correctly.
2535 * Request-based dm is queueing the deferred I/Os in its request_queue.
2536 */
2537 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05002538 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002539
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002540 unlock_fs(md);
2541
Mike Snitzerffcc3932014-10-28 18:34:52 -04002542 return 0;
2543}
2544
2545int dm_resume(struct mapped_device *md)
2546{
Minfei Huang8dc23652016-09-06 16:00:29 +08002547 int r;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002548 struct dm_table *map = NULL;
2549
2550retry:
Minfei Huang8dc23652016-09-06 16:00:29 +08002551 r = -EINVAL;
Mike Snitzerffcc3932014-10-28 18:34:52 -04002552 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2553
2554 if (!dm_suspended_md(md))
2555 goto out;
2556
2557 if (dm_suspended_internally_md(md)) {
2558 /* already internally suspended, wait for internal resume */
2559 mutex_unlock(&md->suspend_lock);
2560 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2561 if (r)
2562 return r;
2563 goto retry;
2564 }
2565
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002566 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002567 if (!map || !dm_table_get_size(map))
2568 goto out;
2569
2570 r = __dm_resume(md, map);
2571 if (r)
2572 goto out;
2573
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002574 clear_bit(DMF_SUSPENDED, &md->flags);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002575out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002576 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002577
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002578 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002581/*
2582 * Internal suspend/resume works like userspace-driven suspend. It waits
2583 * until all bios finish and prevents issuing new bios to the target drivers.
2584 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002585 */
2586
Mike Snitzerffcc3932014-10-28 18:34:52 -04002587static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2588{
2589 struct dm_table *map = NULL;
2590
Bart Van Assche1ea06542017-04-27 10:11:21 -07002591 lockdep_assert_held(&md->suspend_lock);
2592
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002593 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002594 return; /* nested internal suspend */
2595
2596 if (dm_suspended_md(md)) {
2597 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2598 return; /* nest suspend */
2599 }
2600
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002601 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002602
2603 /*
2604 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2605 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
2606 * would require changing .presuspend to return an error -- avoid this
2607 * until there is a need for more elaborate variants of internal suspend.
2608 */
Mike Snitzereaf9a732016-08-02 13:07:20 -04002609 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2610 DMF_SUSPENDED_INTERNALLY);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002611
2612 dm_table_postsuspend_targets(map);
2613}
2614
2615static void __dm_internal_resume(struct mapped_device *md)
2616{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05002617 BUG_ON(!md->internal_suspend_count);
2618
2619 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04002620 return; /* resume from nested internal suspend */
2621
2622 if (dm_suspended_md(md))
2623 goto done; /* resume from nested suspend */
2624
2625 /*
2626 * NOTE: existing callers don't need to call dm_table_resume_targets
2627 * (which may fail -- so best to avoid it for now by passing NULL map)
2628 */
2629 (void) __dm_resume(md, NULL);
2630
2631done:
2632 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2633 smp_mb__after_atomic();
2634 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2635}
2636
2637void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002638{
2639 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002640 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2641 mutex_unlock(&md->suspend_lock);
2642}
2643EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2644
2645void dm_internal_resume(struct mapped_device *md)
2646{
2647 mutex_lock(&md->suspend_lock);
2648 __dm_internal_resume(md);
2649 mutex_unlock(&md->suspend_lock);
2650}
2651EXPORT_SYMBOL_GPL(dm_internal_resume);
2652
2653/*
2654 * Fast variants of internal suspend/resume hold md->suspend_lock,
2655 * which prevents interaction with userspace-driven suspend.
2656 */
2657
2658void dm_internal_suspend_fast(struct mapped_device *md)
2659{
2660 mutex_lock(&md->suspend_lock);
2661 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002662 return;
2663
2664 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2665 synchronize_srcu(&md->io_barrier);
2666 flush_workqueue(md->wq);
2667 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2668}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002669EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002670
Mike Snitzerffcc3932014-10-28 18:34:52 -04002671void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002672{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002673 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002674 goto done;
2675
2676 dm_queue_flush(md);
2677
2678done:
2679 mutex_unlock(&md->suspend_lock);
2680}
Mikulas Patockab735fed2015-02-26 11:40:35 -05002681EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*-----------------------------------------------------------------
2684 * Event notification.
2685 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002686int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002687 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002688{
Milan Broz60935eb2009-06-22 10:12:30 +01002689 char udev_cookie[DM_COOKIE_LENGTH];
2690 char *envp[] = { udev_cookie, NULL };
2691
2692 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002693 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002694 else {
2695 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2696 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002697 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2698 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002699 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002700}
2701
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002702uint32_t dm_next_uevent_seq(struct mapped_device *md)
2703{
2704 return atomic_add_return(1, &md->uevent_seq);
2705}
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707uint32_t dm_get_event_nr(struct mapped_device *md)
2708{
2709 return atomic_read(&md->event_nr);
2710}
2711
2712int dm_wait_event(struct mapped_device *md, int event_nr)
2713{
2714 return wait_event_interruptible(md->eventq,
2715 (event_nr != atomic_read(&md->event_nr)));
2716}
2717
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002718void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2719{
2720 unsigned long flags;
2721
2722 spin_lock_irqsave(&md->uevent_lock, flags);
2723 list_add(elist, &md->uevent_list);
2724 spin_unlock_irqrestore(&md->uevent_lock, flags);
2725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727/*
2728 * The gendisk is only valid as long as you have a reference
2729 * count on 'md'.
2730 */
2731struct gendisk *dm_disk(struct mapped_device *md)
2732{
2733 return md->disk;
2734}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00002735EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Milan Broz784aae72009-01-06 03:05:12 +00002737struct kobject *dm_kobject(struct mapped_device *md)
2738{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002739 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00002740}
2741
Milan Broz784aae72009-01-06 03:05:12 +00002742struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2743{
2744 struct mapped_device *md;
2745
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002746 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00002747
Hou Taob9a41d22017-11-01 15:42:36 +08002748 spin_lock(&_minor_lock);
2749 if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2750 md = NULL;
2751 goto out;
2752 }
Milan Broz784aae72009-01-06 03:05:12 +00002753 dm_get(md);
Hou Taob9a41d22017-11-01 15:42:36 +08002754out:
2755 spin_unlock(&_minor_lock);
2756
Milan Broz784aae72009-01-06 03:05:12 +00002757 return md;
2758}
2759
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002760int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 return test_bit(DMF_SUSPENDED, &md->flags);
2763}
2764
Mike Snitzerffcc3932014-10-28 18:34:52 -04002765int dm_suspended_internally_md(struct mapped_device *md)
2766{
2767 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2768}
2769
Mikulas Patocka2c140a22013-11-01 18:27:41 -04002770int dm_test_deferred_remove_flag(struct mapped_device *md)
2771{
2772 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2773}
2774
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002775int dm_suspended(struct dm_target *ti)
2776{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002777 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002778}
2779EXPORT_SYMBOL_GPL(dm_suspended);
2780
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002781int dm_noflush_suspending(struct dm_target *ti)
2782{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002783 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002784}
2785EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2786
Bart Van Assche7e0d5742017-04-27 10:11:23 -07002787struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
Mike Snitzer30187e12016-01-31 13:28:26 -05002788 unsigned integrity, unsigned per_io_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002789{
Mike Snitzer115485e2016-02-22 12:16:21 -05002790 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
Mike Snitzer78d8e582015-06-26 10:01:13 -04002791 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002792 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002793
2794 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002795 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002796
Mike Snitzer78d8e582015-06-26 10:01:13 -04002797 switch (type) {
2798 case DM_TYPE_BIO_BASED:
Toshi Kani545ed202016-06-22 17:54:53 -06002799 case DM_TYPE_DAX_BIO_BASED:
Mike Snitzer78d8e582015-06-26 10:01:13 -04002800 pool_size = dm_get_reserved_bio_based_ios();
Mike Snitzer30187e12016-01-31 13:28:26 -05002801 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 +01002802
2803 pools->io_pool = mempool_create_slab_pool(pool_size, _io_cache);
2804 if (!pools->io_pool)
2805 goto out;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002806 break;
2807 case DM_TYPE_REQUEST_BASED:
Mike Snitzer78d8e582015-06-26 10:01:13 -04002808 case DM_TYPE_MQ_REQUEST_BASED:
Christoph Hellwigeb8db832017-01-22 18:32:46 +01002809 pool_size = dm_get_reserved_rq_based_ios();
Mike Snitzer78d8e582015-06-26 10:01:13 -04002810 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002811 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04002812 break;
2813 default:
2814 BUG();
2815 }
2816
NeilBrown47e0fb42017-06-18 14:38:57 +10002817 pools->bs = bioset_create(pool_size, front_pad, BIOSET_NEED_RESCUER);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002818 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002819 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002820
Martin K. Petersena91a2782011-03-17 11:11:05 +01002821 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002822 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002823
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002824 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002825
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002826out:
2827 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002828
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002829 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002830}
2831
2832void dm_free_md_mempools(struct dm_md_mempools *pools)
2833{
2834 if (!pools)
2835 return;
2836
Julia Lawall6f659852015-09-13 14:15:05 +02002837 mempool_destroy(pools->io_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002838
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002839 if (pools->bs)
2840 bioset_free(pools->bs);
2841
2842 kfree(pools);
2843}
2844
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002845struct dm_pr {
2846 u64 old_key;
2847 u64 new_key;
2848 u32 flags;
2849 bool fail_early;
2850};
2851
2852static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
2853 void *data)
2854{
2855 struct mapped_device *md = bdev->bd_disk->private_data;
2856 struct dm_table *table;
2857 struct dm_target *ti;
2858 int ret = -ENOTTY, srcu_idx;
2859
2860 table = dm_get_live_table(md, &srcu_idx);
2861 if (!table || !dm_table_get_size(table))
2862 goto out;
2863
2864 /* We only support devices that have a single target */
2865 if (dm_table_get_num_targets(table) != 1)
2866 goto out;
2867 ti = dm_table_get_target(table, 0);
2868
2869 ret = -EINVAL;
2870 if (!ti->type->iterate_devices)
2871 goto out;
2872
2873 ret = ti->type->iterate_devices(ti, fn, data);
2874out:
2875 dm_put_live_table(md, srcu_idx);
2876 return ret;
2877}
2878
2879/*
2880 * For register / unregister we need to manually call out to every path.
2881 */
2882static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
2883 sector_t start, sector_t len, void *data)
2884{
2885 struct dm_pr *pr = data;
2886 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
2887
2888 if (!ops || !ops->pr_register)
2889 return -EOPNOTSUPP;
2890 return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
2891}
2892
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002893static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05002894 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002895{
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002896 struct dm_pr pr = {
2897 .old_key = old_key,
2898 .new_key = new_key,
2899 .flags = flags,
2900 .fail_early = true,
2901 };
2902 int ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002903
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002904 ret = dm_call_pr(bdev, __dm_pr_register, &pr);
2905 if (ret && new_key) {
2906 /* unregister all paths if we failed to register any path */
2907 pr.old_key = new_key;
2908 pr.new_key = 0;
2909 pr.flags = 0;
2910 pr.fail_early = false;
2911 dm_call_pr(bdev, __dm_pr_register, &pr);
2912 }
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002913
Christoph Hellwig9c72bad2016-07-08 21:23:51 +09002914 return ret;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002915}
2916
2917static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05002918 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002919{
2920 struct mapped_device *md = bdev->bd_disk->private_data;
2921 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002922 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002923 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002924
Mike Snitzer956a4022016-02-18 16:13:51 -05002925 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002926 if (r < 0)
2927 return r;
2928
2929 ops = bdev->bd_disk->fops->pr_ops;
2930 if (ops && ops->pr_reserve)
2931 r = ops->pr_reserve(bdev, key, type, flags);
2932 else
2933 r = -EOPNOTSUPP;
2934
Mike Snitzer956a4022016-02-18 16:13:51 -05002935 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002936 return r;
2937}
2938
2939static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
2940{
2941 struct mapped_device *md = bdev->bd_disk->private_data;
2942 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002943 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002944 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002945
Mike Snitzer956a4022016-02-18 16:13:51 -05002946 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002947 if (r < 0)
2948 return r;
2949
2950 ops = bdev->bd_disk->fops->pr_ops;
2951 if (ops && ops->pr_release)
2952 r = ops->pr_release(bdev, key, type);
2953 else
2954 r = -EOPNOTSUPP;
2955
Mike Snitzer956a4022016-02-18 16:13:51 -05002956 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002957 return r;
2958}
2959
2960static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05002961 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002962{
2963 struct mapped_device *md = bdev->bd_disk->private_data;
2964 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002965 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002966 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002967
Mike Snitzer956a4022016-02-18 16:13:51 -05002968 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002969 if (r < 0)
2970 return r;
2971
2972 ops = bdev->bd_disk->fops->pr_ops;
2973 if (ops && ops->pr_preempt)
2974 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
2975 else
2976 r = -EOPNOTSUPP;
2977
Mike Snitzer956a4022016-02-18 16:13:51 -05002978 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002979 return r;
2980}
2981
2982static int dm_pr_clear(struct block_device *bdev, u64 key)
2983{
2984 struct mapped_device *md = bdev->bd_disk->private_data;
2985 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002986 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05002987 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002988
Mike Snitzer956a4022016-02-18 16:13:51 -05002989 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02002990 if (r < 0)
2991 return r;
2992
2993 ops = bdev->bd_disk->fops->pr_ops;
2994 if (ops && ops->pr_clear)
2995 r = ops->pr_clear(bdev, key);
2996 else
2997 r = -EOPNOTSUPP;
2998
Mike Snitzer956a4022016-02-18 16:13:51 -05002999 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003000 return r;
3001}
3002
3003static const struct pr_ops dm_pr_ops = {
3004 .pr_register = dm_pr_register,
3005 .pr_reserve = dm_pr_reserve,
3006 .pr_release = dm_pr_release,
3007 .pr_preempt = dm_pr_preempt,
3008 .pr_clear = dm_pr_clear,
3009};
3010
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003011static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 .open = dm_blk_open,
3013 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003014 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003015 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003016 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 .owner = THIS_MODULE
3018};
3019
Dan Williamsf26c5712017-04-12 12:35:44 -07003020static const struct dax_operations dm_dax_ops = {
3021 .direct_access = dm_dax_direct_access,
Dan Williams7e026c82017-05-29 12:57:56 -07003022 .copy_from_iter = dm_dax_copy_from_iter,
Dan Williamsf26c5712017-04-12 12:35:44 -07003023};
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025/*
3026 * module hooks
3027 */
3028module_init(dm_init);
3029module_exit(dm_exit);
3030
3031module_param(major, uint, 0);
3032MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003033
Mike Snitzere8603132013-09-12 18:06:12 -04003034module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3035MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3036
Mike Snitzer115485e2016-02-22 12:16:21 -05003037module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3038MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040MODULE_DESCRIPTION(DM_NAME " driver");
3041MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3042MODULE_LICENSE("GPL");