blob: 26fedd93702e1c56cdc9d411a31445988972371f [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
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080020#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010021#include <linux/delay.h>
Mike Snitzerffcc3932014-10-28 18:34:52 -040022#include <linux/wait.h>
Keith Busch2eb6e1e2014-10-17 17:46:36 -060023#include <linux/kthread.h>
Mike Snitzer0ce65792015-02-26 00:50:28 -050024#include <linux/ktime.h>
Mike Snitzerde3ec862015-02-24 21:58:21 -050025#include <linux/elevator.h> /* for rq_end_sector() */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -050026#include <linux/blk-mq.h>
Christoph Hellwig71cdb692015-10-15 14:10:51 +020027#include <linux/pr.h>
Li Zefan55782132009-06-09 13:43:05 +080028
29#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Alasdair G Kergon72d94862006-06-26 00:27:35 -070031#define DM_MSG_PREFIX "core"
32
Namhyung Kim71a16732011-10-31 20:18:54 +000033#ifdef CONFIG_PRINTK
34/*
35 * ratelimit state to be used in DMXXX_LIMIT().
36 */
37DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
38 DEFAULT_RATELIMIT_INTERVAL,
39 DEFAULT_RATELIMIT_BURST);
40EXPORT_SYMBOL(dm_ratelimit_state);
41#endif
42
Milan Broz60935eb2009-06-22 10:12:30 +010043/*
44 * Cookies are numeric values sent with CHANGE and REMOVE
45 * uevents while resuming, removing or renaming the device.
46 */
47#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
48#define DM_COOKIE_LENGTH 24
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static const char *_name = DM_NAME;
51
52static unsigned int major = 0;
53static unsigned int _major = 0;
54
Alasdair G Kergond15b7742011-08-02 12:32:01 +010055static DEFINE_IDR(_minor_idr);
56
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070057static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040058
59static void do_deferred_remove(struct work_struct *w);
60
61static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
62
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040063static struct workqueue_struct *deferred_remove_workqueue;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000066 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * One of these is allocated per bio.
68 */
69struct dm_io {
70 struct mapped_device *md;
71 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010073 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080074 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010075 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040076 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
79/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000080 * For request-based dm.
81 * One of these is allocated per request.
82 */
83struct dm_rq_target_io {
84 struct mapped_device *md;
85 struct dm_target *ti;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -050086 struct request *orig, *clone;
Keith Busch2eb6e1e2014-10-17 17:46:36 -060087 struct kthread_work work;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000088 int error;
89 union map_info info;
Mikulas Patockae262f342015-06-09 17:22:49 -040090 struct dm_stats_aux stats_aux;
91 unsigned long duration_jiffies;
92 unsigned n_sectors;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000093};
94
95/*
Kent Overstreet94818742012-09-07 13:44:01 -070096 * For request-based dm - the bio clones we allocate are embedded in these
97 * structs.
98 *
99 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
100 * the bioset is created - this means the bio has to come at the end of the
101 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000102 */
103struct dm_rq_clone_bio_info {
104 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100105 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -0700106 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000107};
108
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700109#define MINOR_ALLOCED ((void *)-1)
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * Bits for the md->flags field.
113 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100114#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800116#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700117#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700118#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800119#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -0700120#define DMF_DEFERRED_REMOVE 6
121#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Milan Broz304f3f62008-02-08 02:11:17 +0000123/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100124 * A dummy definition to make RCU happy.
125 * struct dm_table should never be dereferenced in this file.
126 */
127struct dm_table {
128 int undefined__;
129};
130
131/*
Milan Broz304f3f62008-02-08 02:11:17 +0000132 * Work processed by per-device workqueue.
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100135 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000136 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700138 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100140 /*
141 * The current mapping.
142 * Use dm_get_live_table{_fast} or take suspend_lock for
143 * dereference.
144 */
Pranith Kumar6fa99522014-10-28 15:09:57 -0700145 struct dm_table __rcu *map;
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100146
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500147 struct list_head table_devices;
148 struct mutex table_devices_lock;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long flags;
151
Jens Axboe165125e2007-07-24 09:28:11 +0200152 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100153 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100154 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100155 struct mutex type_lock;
156
Mike Snitzer16f12262016-01-31 17:22:27 -0500157 struct dm_target *immutable_target;
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000158 struct target_type *immutable_target_type;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800161 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 void *interface_ptr;
164
165 /*
166 * A list of ios that arrived while we were suspended.
167 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200168 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100170 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800171 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100172 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200175 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000176 */
177 struct workqueue_struct *wq;
178
179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * io objects are allocated from here.
181 */
182 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500183 mempool_t *rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Stefan Bader9faf4002006-10-03 01:15:41 -0700185 struct bio_set *bs;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * Event handling.
189 */
190 atomic_t event_nr;
191 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100192 atomic_t uevent_seq;
193 struct list_head uevent_list;
194 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /*
197 * freeze/thaw support require holding onto a super block
198 */
199 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100200 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800201
202 /* forced geometry settings */
203 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000204
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500205 /* kobject and completion */
206 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500207
Tejun Heod87f4c12010-09-03 11:56:19 +0200208 /* zero-length flush that will be cloned and submitted to targets */
209 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400210
Mikulas Patocka96b26c82015-01-08 18:52:26 -0500211 /* the number of internal suspends */
212 unsigned internal_suspend_count;
213
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400214 struct dm_stats stats;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600215
216 struct kthread_worker kworker;
217 struct task_struct *kworker_task;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500218
219 /* for request-based merge heuristic in dm_request_fn() */
Mike Snitzer0ce65792015-02-26 00:50:28 -0500220 unsigned seq_rq_merge_deadline_usecs;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500221 int last_rq_rw;
Mike Snitzer0ce65792015-02-26 00:50:28 -0500222 sector_t last_rq_pos;
223 ktime_t last_rq_start_time;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -0500224
225 /* for blk-mq request-based DM support */
Mike Snitzer1c357a12016-02-06 17:01:17 -0500226 struct blk_mq_tag_set *tag_set;
Mike Snitzer17e149b2015-03-11 15:01:09 -0400227 bool use_blk_mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
Mike Snitzer17e149b2015-03-11 15:01:09 -0400230#ifdef CONFIG_DM_MQ_DEFAULT
231static bool use_blk_mq = true;
232#else
233static bool use_blk_mq = false;
234#endif
235
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500236#define DM_MQ_NR_HW_QUEUES 1
237#define DM_MQ_QUEUE_DEPTH 2048
238
239static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
240static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
241
Mike Snitzer17e149b2015-03-11 15:01:09 -0400242bool dm_use_blk_mq(struct mapped_device *md)
243{
244 return md->use_blk_mq;
245}
246
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100247/*
248 * For mempools pre-allocation at the table loading time.
249 */
250struct dm_md_mempools {
251 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500252 mempool_t *rq_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100253 struct bio_set *bs;
254};
255
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500256struct table_device {
257 struct list_head list;
258 atomic_t count;
259 struct dm_dev dm_dev;
260};
261
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400262#define RESERVED_BIO_BASED_IOS 16
263#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400264#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800265static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000266static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500267static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700268
Mike Snitzerf4790822013-09-12 18:06:12 -0400269/*
Mike Snitzere8603132013-09-12 18:06:12 -0400270 * Bio-based DM's mempools' reserved IOs set by the user.
271 */
272static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
273
274/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400275 * Request-based DM's mempools' reserved IOs set by the user.
276 */
277static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
278
Mike Snitzer09c2d532015-02-27 22:25:26 -0500279static unsigned __dm_get_module_param(unsigned *module_param,
Mike Snitzerf4790822013-09-12 18:06:12 -0400280 unsigned def, unsigned max)
281{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500282 unsigned param = ACCESS_ONCE(*module_param);
283 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400284
Mike Snitzer09c2d532015-02-27 22:25:26 -0500285 if (!param)
286 modified_param = def;
287 else if (param > max)
288 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400289
Mike Snitzer09c2d532015-02-27 22:25:26 -0500290 if (modified_param) {
291 (void)cmpxchg(module_param, param, modified_param);
292 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400293 }
294
Mike Snitzer09c2d532015-02-27 22:25:26 -0500295 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400296}
297
Mike Snitzere8603132013-09-12 18:06:12 -0400298unsigned dm_get_reserved_bio_based_ios(void)
299{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500300 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzere8603132013-09-12 18:06:12 -0400301 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
302}
303EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
304
Mike Snitzerf4790822013-09-12 18:06:12 -0400305unsigned dm_get_reserved_rq_based_ios(void)
306{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500307 return __dm_get_module_param(&reserved_rq_based_ios,
Mike Snitzerf4790822013-09-12 18:06:12 -0400308 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
309}
310EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
311
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500312static unsigned dm_get_blk_mq_nr_hw_queues(void)
313{
314 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
315}
316
317static unsigned dm_get_blk_mq_queue_depth(void)
318{
319 return __dm_get_module_param(&dm_mq_queue_depth,
320 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static int __init local_init(void)
324{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100325 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100328 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100330 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000332 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
333 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100334 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000335
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500336 _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
337 __alignof__(struct request), 0, NULL);
338 if (!_rq_cache)
339 goto out_free_rq_tio_cache;
340
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100341 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100342 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500343 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100344
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400345 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
346 if (!deferred_remove_workqueue) {
347 r = -ENOMEM;
348 goto out_uevent_exit;
349 }
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 _major = major;
352 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100353 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400354 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (!_major)
357 _major = r;
358
359 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100360
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400361out_free_workqueue:
362 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100363out_uevent_exit:
364 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500365out_free_rq_cache:
366 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000367out_free_rq_tio_cache:
368 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100369out_free_io_cache:
370 kmem_cache_destroy(_io_cache);
371
372 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static void local_exit(void)
376{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400377 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400378 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400379
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500380 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000381 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700383 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100384 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 _major = 0;
387
388 DMINFO("cleaned up");
389}
390
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000391static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 local_init,
393 dm_target_init,
394 dm_linear_init,
395 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000396 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100397 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400399 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000402static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 local_exit,
404 dm_target_exit,
405 dm_linear_exit,
406 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000407 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100408 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400410 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411};
412
413static int __init dm_init(void)
414{
415 const int count = ARRAY_SIZE(_inits);
416
417 int r, i;
418
419 for (i = 0; i < count; i++) {
420 r = _inits[i]();
421 if (r)
422 goto bad;
423 }
424
425 return 0;
426
427 bad:
428 while (i--)
429 _exits[i]();
430
431 return r;
432}
433
434static void __exit dm_exit(void)
435{
436 int i = ARRAY_SIZE(_exits);
437
438 while (i--)
439 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100440
441 /*
442 * Should be empty by this point.
443 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100444 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/*
448 * Block device functions
449 */
Mike Anderson432a2122009-12-10 23:52:20 +0000450int dm_deleting_md(struct mapped_device *md)
451{
452 return test_bit(DMF_DELETING, &md->flags);
453}
454
Al Virofe5f9f22008-03-02 10:29:31 -0500455static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct mapped_device *md;
458
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700459 spin_lock(&_minor_lock);
460
Al Virofe5f9f22008-03-02 10:29:31 -0500461 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700462 if (!md)
463 goto out;
464
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700465 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000466 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700467 md = NULL;
468 goto out;
469 }
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700472 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700473out:
474 spin_unlock(&_minor_lock);
475
476 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Al Virodb2a1442013-05-05 21:52:57 -0400479static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400481 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200482
Milan Broz4a1aeb92011-01-13 19:59:48 +0000483 spin_lock(&_minor_lock);
484
Mike Snitzer63a4f062015-03-23 17:01:43 -0400485 md = disk->private_data;
486 if (WARN_ON(!md))
487 goto out;
488
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400489 if (atomic_dec_and_test(&md->open_count) &&
490 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400491 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400494out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000495 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700498int dm_open_count(struct mapped_device *md)
499{
500 return atomic_read(&md->open_count);
501}
502
503/*
504 * Guarantees nothing is using the device before it's deleted.
505 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400506int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700507{
508 int r = 0;
509
510 spin_lock(&_minor_lock);
511
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400512 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700513 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400514 if (mark_deferred)
515 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
516 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
517 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700518 else
519 set_bit(DMF_DELETING, &md->flags);
520
521 spin_unlock(&_minor_lock);
522
523 return r;
524}
525
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400526int dm_cancel_deferred_remove(struct mapped_device *md)
527{
528 int r = 0;
529
530 spin_lock(&_minor_lock);
531
532 if (test_bit(DMF_DELETING, &md->flags))
533 r = -EBUSY;
534 else
535 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
536
537 spin_unlock(&_minor_lock);
538
539 return r;
540}
541
542static void do_deferred_remove(struct work_struct *w)
543{
544 dm_deferred_remove();
545}
546
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400547sector_t dm_get_size(struct mapped_device *md)
548{
549 return get_capacity(md->disk);
550}
551
Mike Snitzer9974fa22014-02-28 15:33:43 +0100552struct request_queue *dm_get_md_queue(struct mapped_device *md)
553{
554 return md->queue;
555}
556
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400557struct dm_stats *dm_get_stats(struct mapped_device *md)
558{
559 return &md->stats;
560}
561
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800562static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
563{
564 struct mapped_device *md = bdev->bd_disk->private_data;
565
566 return dm_get_geometry(md, geo);
567}
568
Mike Snitzer956a4022016-02-18 16:13:51 -0500569static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
570 struct block_device **bdev,
571 fmode_t *mode)
Milan Brozaa129a22006-10-03 01:15:15 -0700572{
Mike Snitzer66482022016-02-18 15:44:39 -0500573 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100574 struct dm_table *map;
Mike Snitzer956a4022016-02-18 16:13:51 -0500575 int srcu_idx, r;
Milan Brozaa129a22006-10-03 01:15:15 -0700576
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100577retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200578 r = -ENOTTY;
Mike Snitzer956a4022016-02-18 16:13:51 -0500579 map = dm_get_live_table(md, &srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700580 if (!map || !dm_table_get_size(map))
581 goto out;
582
583 /* We only support devices that have a single target */
584 if (dm_table_get_num_targets(map) != 1)
585 goto out;
586
Mike Snitzer66482022016-02-18 15:44:39 -0500587 tgt = dm_table_get_target(map, 0);
588 if (!tgt->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500589 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700590
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000591 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700592 r = -EAGAIN;
593 goto out;
594 }
595
Mike Snitzer66482022016-02-18 15:44:39 -0500596 r = tgt->type->prepare_ioctl(tgt, bdev, mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200597 if (r < 0)
598 goto out;
599
Mike Snitzer956a4022016-02-18 16:13:51 -0500600 bdgrab(*bdev);
601 dm_put_live_table(md, srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200602 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700603
604out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500605 dm_put_live_table(md, srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000606 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100607 msleep(10);
608 goto retry;
609 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200610 return r;
611}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100612
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200613static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
614 unsigned int cmd, unsigned long arg)
615{
616 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer956a4022016-02-18 16:13:51 -0500617 int r;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200618
Mike Snitzer956a4022016-02-18 16:13:51 -0500619 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200620 if (r < 0)
621 return r;
622
623 if (r > 0) {
624 /*
625 * Target determined this ioctl is being issued against
626 * a logical partition of the parent bdev; so extra
627 * validation is needed.
628 */
629 r = scsi_verify_blk_ioctl(NULL, cmd);
630 if (r)
631 goto out;
632 }
633
Mike Snitzer66482022016-02-18 15:44:39 -0500634 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200635out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500636 bdput(bdev);
Milan Brozaa129a22006-10-03 01:15:15 -0700637 return r;
638}
639
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100640static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 return mempool_alloc(md->io_pool, GFP_NOIO);
643}
644
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100645static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 mempool_free(io, md->io_pool);
648}
649
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100650static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Mikulas Patockadba14162012-10-12 21:02:15 +0100652 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000655static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
656 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100657{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000658 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100659}
660
661static void free_rq_tio(struct dm_rq_target_io *tio)
662{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000663 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100664}
665
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500666static struct request *alloc_clone_request(struct mapped_device *md,
667 gfp_t gfp_mask)
668{
669 return mempool_alloc(md->rq_pool, gfp_mask);
670}
671
672static void free_clone_request(struct mapped_device *md, struct request *rq)
673{
674 mempool_free(rq, md->rq_pool);
675}
676
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000677static int md_in_flight(struct mapped_device *md)
678{
679 return atomic_read(&md->pending[READ]) +
680 atomic_read(&md->pending[WRITE]);
681}
682
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800683static void start_io_acct(struct dm_io *io)
684{
685 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400686 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900687 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400688 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800689
690 io->start_time = jiffies;
691
Tejun Heo074a7ac2008-08-25 19:56:14 +0900692 cpu = part_stat_lock();
693 part_round_stats(cpu, &dm_disk(md)->part0);
694 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100695 atomic_set(&dm_disk(md)->part0.in_flight[rw],
696 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400697
698 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700699 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400700 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800701}
702
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000703static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800704{
705 struct mapped_device *md = io->md;
706 struct bio *bio = io->bio;
707 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800708 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800709 int rw = bio_data_dir(bio);
710
Gu Zheng18c0b222014-11-24 11:05:26 +0800711 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800712
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400713 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700714 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400715 bio_sectors(bio), true, duration, &io->stats_aux);
716
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100717 /*
718 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200719 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100720 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100721 pending = atomic_dec_return(&md->pending[rw]);
722 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200723 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800724
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000725 /* nudge anyone waiting on suspend queue */
726 if (!pending)
727 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/*
731 * Add the bio to the list of deferred io.
732 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100733static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200735 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200737 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200739 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200740 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743/*
744 * Everyone (including functions in this file), should use this
745 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100746 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100748struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100750 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100752 return srcu_dereference(md->map, &md->io_barrier);
753}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100755void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
756{
757 srcu_read_unlock(&md->io_barrier, srcu_idx);
758}
759
760void dm_sync_table(struct mapped_device *md)
761{
762 synchronize_srcu(&md->io_barrier);
763 synchronize_rcu_expedited();
764}
765
766/*
767 * A fast alternative to dm_get_live_table/dm_put_live_table.
768 * The caller must not block between these two functions.
769 */
770static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
771{
772 rcu_read_lock();
773 return rcu_dereference(md->map);
774}
775
776static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
777{
778 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800781/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500782 * Open a table device so we can use it as a map destination.
783 */
784static int open_table_device(struct table_device *td, dev_t dev,
785 struct mapped_device *md)
786{
787 static char *_claim_ptr = "I belong to device-mapper";
788 struct block_device *bdev;
789
790 int r;
791
792 BUG_ON(td->dm_dev.bdev);
793
794 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
795 if (IS_ERR(bdev))
796 return PTR_ERR(bdev);
797
798 r = bd_link_disk_holder(bdev, dm_disk(md));
799 if (r) {
800 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
801 return r;
802 }
803
804 td->dm_dev.bdev = bdev;
805 return 0;
806}
807
808/*
809 * Close a table device that we've been using.
810 */
811static void close_table_device(struct table_device *td, struct mapped_device *md)
812{
813 if (!td->dm_dev.bdev)
814 return;
815
816 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
817 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
818 td->dm_dev.bdev = NULL;
819}
820
821static struct table_device *find_table_device(struct list_head *l, dev_t dev,
822 fmode_t mode) {
823 struct table_device *td;
824
825 list_for_each_entry(td, l, list)
826 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
827 return td;
828
829 return NULL;
830}
831
832int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
833 struct dm_dev **result) {
834 int r;
835 struct table_device *td;
836
837 mutex_lock(&md->table_devices_lock);
838 td = find_table_device(&md->table_devices, dev, mode);
839 if (!td) {
840 td = kmalloc(sizeof(*td), GFP_KERNEL);
841 if (!td) {
842 mutex_unlock(&md->table_devices_lock);
843 return -ENOMEM;
844 }
845
846 td->dm_dev.mode = mode;
847 td->dm_dev.bdev = NULL;
848
849 if ((r = open_table_device(td, dev, md))) {
850 mutex_unlock(&md->table_devices_lock);
851 kfree(td);
852 return r;
853 }
854
855 format_dev_t(td->dm_dev.name, dev);
856
857 atomic_set(&td->count, 0);
858 list_add(&td->list, &md->table_devices);
859 }
860 atomic_inc(&td->count);
861 mutex_unlock(&md->table_devices_lock);
862
863 *result = &td->dm_dev;
864 return 0;
865}
866EXPORT_SYMBOL_GPL(dm_get_table_device);
867
868void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
869{
870 struct table_device *td = container_of(d, struct table_device, dm_dev);
871
872 mutex_lock(&md->table_devices_lock);
873 if (atomic_dec_and_test(&td->count)) {
874 close_table_device(td, md);
875 list_del(&td->list);
876 kfree(td);
877 }
878 mutex_unlock(&md->table_devices_lock);
879}
880EXPORT_SYMBOL(dm_put_table_device);
881
882static void free_table_devices(struct list_head *devices)
883{
884 struct list_head *tmp, *next;
885
886 list_for_each_safe(tmp, next, devices) {
887 struct table_device *td = list_entry(tmp, struct table_device, list);
888
889 DMWARN("dm_destroy: %s still exists with %d references",
890 td->dm_dev.name, atomic_read(&td->count));
891 kfree(td);
892 }
893}
894
895/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800896 * Get the geometry associated with a dm device
897 */
898int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
899{
900 *geo = md->geometry;
901
902 return 0;
903}
904
905/*
906 * Set the geometry of a device.
907 */
908int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
909{
910 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
911
912 if (geo->start > sz) {
913 DMWARN("Start sector is beyond the geometry limits.");
914 return -EINVAL;
915 }
916
917 md->geometry = *geo;
918
919 return 0;
920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/*-----------------------------------------------------------------
923 * CRUD START:
924 * A more elegant soln is in the works that uses the queue
925 * merge fn, unfortunately there are a couple of changes to
926 * the block layer that I want to make for this. So in the
927 * interests of getting something for people to use I give
928 * you this clearly demarcated crap.
929 *---------------------------------------------------------------*/
930
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800931static int __noflush_suspending(struct mapped_device *md)
932{
933 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
937 * Decrements the number of outstanding ios that a bio has been
938 * cloned into, completing the original io if necc.
939 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800940static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800942 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000943 int io_error;
944 struct bio *bio;
945 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800946
947 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100948 if (unlikely(error)) {
949 spin_lock_irqsave(&io->endio_lock, flags);
950 if (!(io->error > 0 && __noflush_suspending(md)))
951 io->error = error;
952 spin_unlock_irqrestore(&io->endio_lock, flags);
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800956 if (io->error == DM_ENDIO_REQUEUE) {
957 /*
958 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800959 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100960 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200961 if (__noflush_suspending(md))
962 bio_list_add_head(&md->deferred, io->bio);
963 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800964 /* noflush suspend was interrupted. */
965 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100966 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800967 }
968
Milan Brozb35f8ca2009-03-16 17:44:36 +0000969 io_error = io->error;
970 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200971 end_io_acct(io);
972 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100973
Tejun Heo6a8736d2010-09-08 18:07:00 +0200974 if (io_error == DM_ENDIO_REQUEUE)
975 return;
976
Kent Overstreet4f024f32013-10-11 15:44:27 -0700977 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100978 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200979 * Preflush done for flush with data, reissue
980 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100981 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200982 bio->bi_rw &= ~REQ_FLUSH;
983 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100984 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200985 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700986 trace_block_bio_complete(md->queue, bio, io_error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200987 bio->bi_error = io_error;
988 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991}
992
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400993static void disable_write_same(struct mapped_device *md)
994{
995 struct queue_limits *limits = dm_get_queue_limits(md);
996
997 /* device doesn't really support WRITE SAME, disable it */
998 limits->max_write_same_sectors = 0;
999}
1000
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001001static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001003 int error = bio->bi_error;
zhendong chen5164bec2014-12-17 14:37:04 +08001004 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -05001005 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001006 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -07001007 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 dm_endio_fn endio = tio->ti->type->end_io;
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001011 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001012 if (r < 0 || r == DM_ENDIO_REQUEUE)
1013 /*
1014 * error and requeue request are handled
1015 * in dec_pending().
1016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001018 else if (r == DM_ENDIO_INCOMPLETE)
1019 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +02001020 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001021 else if (r) {
1022 DMWARN("unimplemented target endio return value: %d", r);
1023 BUG();
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001027 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
1028 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
1029 disable_write_same(md);
1030
Stefan Bader9faf4002006-10-03 01:15:41 -07001031 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001032 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Mike Snitzer78d8e582015-06-26 10:01:13 -04001035/*
1036 * Partial completion handling for request-based dm
1037 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001038static void end_clone_bio(struct bio *clone)
Mike Snitzer78d8e582015-06-26 10:01:13 -04001039{
1040 struct dm_rq_clone_bio_info *info =
1041 container_of(clone, struct dm_rq_clone_bio_info, clone);
1042 struct dm_rq_target_io *tio = info->tio;
1043 struct bio *bio = info->orig;
1044 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001045 int error = clone->bi_error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001046
1047 bio_put(clone);
1048
1049 if (tio->error)
1050 /*
1051 * An error has already been detected on the request.
1052 * Once error occurred, just let clone->end_io() handle
1053 * the remainder.
1054 */
1055 return;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001056 else if (error) {
Mike Snitzer78d8e582015-06-26 10:01:13 -04001057 /*
1058 * Don't notice the error to the upper layer yet.
1059 * The error handling decision is made by the target driver,
1060 * when the request is completed.
1061 */
Junichi Nomura50887bd2015-10-06 04:19:54 +00001062 tio->error = error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001063 return;
1064 }
1065
1066 /*
1067 * I/O for the bio successfully completed.
1068 * Notice the data completion to the upper layer.
1069 */
1070
1071 /*
1072 * bios are processed from the head of the list.
1073 * So the completing bio should always be rq->bio.
1074 * If it's not, something wrong is happening.
1075 */
1076 if (tio->orig->bio != bio)
1077 DMERR("bio completion is going in the middle of the request");
1078
1079 /*
1080 * Update the original request.
1081 * Do not use blk_end_request() here, because it may complete
1082 * the original request before the clone, and break the ordering.
1083 */
1084 blk_update_request(tio->orig, 0, nr_bytes);
1085}
1086
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001087static struct dm_rq_target_io *tio_from_request(struct request *rq)
1088{
1089 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1090}
1091
Mikulas Patockae262f342015-06-09 17:22:49 -04001092static void rq_end_stats(struct mapped_device *md, struct request *orig)
1093{
1094 if (unlikely(dm_stats_used(&md->stats))) {
1095 struct dm_rq_target_io *tio = tio_from_request(orig);
1096 tio->duration_jiffies = jiffies - tio->duration_jiffies;
1097 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1098 tio->n_sectors, true, tio->duration_jiffies,
1099 &tio->stats_aux);
1100 }
1101}
1102
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001103/*
1104 * Don't touch any member of the md after calling this function because
1105 * the md may be freed in dm_put() at the end of this function.
1106 * Or do dm_get() before calling this function and dm_put() later.
1107 */
Keith Busch466d89a2014-10-17 17:46:37 -06001108static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001109{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001110 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001111
1112 /* nudge anyone waiting on suspend queue */
Mike Snitzer621739b2015-07-08 16:08:24 -04001113 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001114 wake_up(&md->wait);
1115
Jens Axboea8c32a52012-11-06 12:24:26 +01001116 /*
1117 * Run this off this callpath, as drivers could invoke end_io while
1118 * inside their request_fn (and holding the queue lock). Calling
1119 * back into ->request_fn() could deadlock attempting to grab the
1120 * queue lock again.
1121 */
Mike Snitzer6acfe682016-02-05 08:49:01 -05001122 if (!md->queue->mq_ops && run_queue)
1123 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001124
1125 /*
1126 * dm_put() must be at the end of this function. See the comment above
1127 */
1128 dm_put(md);
1129}
1130
Mike Snitzere5d8de32015-05-28 15:12:52 -04001131static void free_rq_clone(struct request *clone)
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001132{
1133 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001134 struct mapped_device *md = tio->md;
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001135
Mike Snitzer78d8e582015-06-26 10:01:13 -04001136 blk_rq_unprep_clone(clone);
1137
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001138 if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1139 /* stacked on blk-mq queue(s) */
Mike Snitzere5863d92014-12-17 21:08:12 -05001140 tio->ti->type->release_clone_rq(clone);
Mike Snitzer02233342015-03-10 23:49:26 -04001141 else if (!md->queue->mq_ops)
1142 /* request_fn queue stacked on request_fn queue(s) */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001143 free_clone_request(md, clone);
1144
1145 if (!md->queue->mq_ops)
1146 free_rq_tio(tio);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001147}
1148
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001149/*
1150 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001151 * Must be called without clone's queue lock held,
1152 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001153 */
1154static void dm_end_request(struct request *clone, int error)
1155{
1156 int rw = rq_data_dir(clone);
1157 struct dm_rq_target_io *tio = clone->end_io_data;
1158 struct mapped_device *md = tio->md;
1159 struct request *rq = tio->orig;
1160
Tejun Heo29e40132010-09-08 18:07:00 +02001161 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001162 rq->errors = clone->errors;
1163 rq->resid_len = clone->resid_len;
1164
1165 if (rq->sense)
1166 /*
1167 * We are using the sense buffer of the original
1168 * request.
1169 * So setting the length of the sense data is enough.
1170 */
1171 rq->sense_len = clone->sense_len;
1172 }
1173
Mike Snitzere5d8de32015-05-28 15:12:52 -04001174 free_rq_clone(clone);
Mikulas Patockae262f342015-06-09 17:22:49 -04001175 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001176 if (!rq->q->mq_ops)
1177 blk_end_request_all(rq, error);
1178 else
1179 blk_mq_end_request(rq, error);
Tejun Heo29e40132010-09-08 18:07:00 +02001180 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001181}
1182
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001183static void dm_unprep_request(struct request *rq)
1184{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001185 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001186 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001187
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001188 if (!rq->q->mq_ops) {
1189 rq->special = NULL;
1190 rq->cmd_flags &= ~REQ_DONTPREP;
1191 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001192
Mike Snitzere5863d92014-12-17 21:08:12 -05001193 if (clone)
Mike Snitzere5d8de32015-05-28 15:12:52 -04001194 free_rq_clone(clone);
Mike Snitzer4328daa2016-02-21 19:09:22 -05001195 else if (!tio->md->queue->mq_ops)
1196 free_rq_tio(tio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001197}
1198
1199/*
1200 * Requeue the original request of a clone.
1201 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001202static void old_requeue_request(struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001203{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001204 struct request_queue *q = rq->q;
1205 unsigned long flags;
1206
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001207 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001208 blk_requeue_request(q, rq);
Junichi Nomura4ae99442015-05-26 08:25:54 +00001209 blk_run_queue_async(q);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001210 spin_unlock_irqrestore(q->queue_lock, flags);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001211}
1212
Mike Snitzer818c5f32016-02-20 00:38:47 -05001213static void dm_mq_requeue_request(struct request *rq)
1214{
1215 struct request_queue *q = rq->q;
1216 unsigned long flags;
1217
1218 blk_mq_requeue_request(rq);
1219 spin_lock_irqsave(q->queue_lock, flags);
1220 if (!blk_queue_stopped(q))
1221 blk_mq_kick_requeue_list(q);
1222 spin_unlock_irqrestore(q->queue_lock, flags);
1223}
1224
Mike Snitzer2d76fff12015-04-29 12:07:12 -04001225static void dm_requeue_original_request(struct mapped_device *md,
1226 struct request *rq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001227{
1228 int rw = rq_data_dir(rq);
1229
1230 dm_unprep_request(rq);
1231
Mikulas Patockae262f342015-06-09 17:22:49 -04001232 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001233 if (!rq->q->mq_ops)
1234 old_requeue_request(rq);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001235 else
1236 dm_mq_requeue_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001237
Keith Busch466d89a2014-10-17 17:46:37 -06001238 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001239}
Keith Busch466d89a2014-10-17 17:46:37 -06001240
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001241static void old_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001242{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001243 unsigned long flags;
1244
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001245 spin_lock_irqsave(q->queue_lock, flags);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001246 if (blk_queue_stopped(q)) {
1247 spin_unlock_irqrestore(q->queue_lock, flags);
1248 return;
1249 }
1250
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001251 blk_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001252 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001253}
1254
1255static void stop_queue(struct request_queue *q)
1256{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001257 if (!q->mq_ops)
1258 old_stop_queue(q);
1259 else
1260 blk_mq_stop_hw_queues(q);
1261}
1262
1263static void old_start_queue(struct request_queue *q)
1264{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001265 unsigned long flags;
1266
1267 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001268 if (blk_queue_stopped(q))
1269 blk_start_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001270 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001271}
1272
1273static void start_queue(struct request_queue *q)
1274{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001275 if (!q->mq_ops)
1276 old_start_queue(q);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001277 else {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001278 blk_mq_start_stopped_hw_queues(q, true);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001279 blk_mq_kick_requeue_list(q);
1280 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001281}
1282
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001283static void dm_done(struct request *clone, int error, bool mapped)
1284{
1285 int r = error;
1286 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001287 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001288
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001289 if (tio->ti) {
1290 rq_end_io = tio->ti->type->rq_end_io;
1291
1292 if (mapped && rq_end_io)
1293 r = rq_end_io(tio->ti, clone, error, &tio->info);
1294 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001295
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001296 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1297 !clone->q->limits.max_write_same_sectors))
1298 disable_write_same(tio->md);
1299
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001300 if (r <= 0)
1301 /* The target wants to complete the I/O */
1302 dm_end_request(clone, r);
1303 else if (r == DM_ENDIO_INCOMPLETE)
1304 /* The target will handle the I/O */
1305 return;
1306 else if (r == DM_ENDIO_REQUEUE)
1307 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff12015-04-29 12:07:12 -04001308 dm_requeue_original_request(tio->md, tio->orig);
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001309 else {
1310 DMWARN("unimplemented target endio return value: %d", r);
1311 BUG();
1312 }
1313}
1314
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001315/*
1316 * Request completion handler for request-based dm
1317 */
1318static void dm_softirq_done(struct request *rq)
1319{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001320 bool mapped = true;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001321 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001322 struct request *clone = tio->clone;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001323 int rw;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001324
Mike Snitzere5863d92014-12-17 21:08:12 -05001325 if (!clone) {
Mikulas Patockae262f342015-06-09 17:22:49 -04001326 rq_end_stats(tio->md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001327 rw = rq_data_dir(rq);
1328 if (!rq->q->mq_ops) {
1329 blk_end_request_all(rq, tio->error);
1330 rq_completed(tio->md, rw, false);
1331 free_rq_tio(tio);
1332 } else {
1333 blk_mq_end_request(rq, tio->error);
1334 rq_completed(tio->md, rw, false);
1335 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001336 return;
1337 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001338
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001339 if (rq->cmd_flags & REQ_FAILED)
1340 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001341
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001342 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001343}
1344
1345/*
1346 * Complete the clone and the original request with the error status
1347 * through softirq context.
1348 */
Keith Busch466d89a2014-10-17 17:46:37 -06001349static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001350{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001351 struct dm_rq_target_io *tio = tio_from_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001352
1353 tio->error = error;
Mike Snitzer6acfe682016-02-05 08:49:01 -05001354 if (!rq->q->mq_ops)
1355 blk_complete_request(rq);
1356 else
1357 blk_mq_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001358}
1359
1360/*
1361 * Complete the not-mapped clone and the original request with the error status
1362 * through softirq context.
1363 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001364 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001365 */
Keith Busch466d89a2014-10-17 17:46:37 -06001366static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001367{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001368 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001369 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001370}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001371
1372/*
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001373 * Called with the clone's queue lock held (for non-blk-mq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001374 */
1375static void end_clone_request(struct request *clone, int error)
1376{
Keith Busch466d89a2014-10-17 17:46:37 -06001377 struct dm_rq_target_io *tio = clone->end_io_data;
1378
Mike Snitzere5863d92014-12-17 21:08:12 -05001379 if (!clone->q->mq_ops) {
1380 /*
1381 * For just cleaning up the information of the queue in which
1382 * the clone was dispatched.
1383 * The clone is *NOT* freed actually here because it is alloced
1384 * from dm own mempool (REQ_ALLOCED isn't set).
1385 */
1386 __blk_put_request(clone->q, clone);
1387 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001388
1389 /*
1390 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001391 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001392 * - another request may be submitted by the upper level driver
1393 * of the stacking during the completion
1394 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001395 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001396 */
Keith Busch466d89a2014-10-17 17:46:37 -06001397 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001398}
1399
Mike Snitzer56a67df2010-08-12 04:14:10 +01001400/*
1401 * Return maximum size of I/O possible at the supplied sector up to the current
1402 * target boundary.
1403 */
1404static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001406 sector_t target_offset = dm_target_offset(ti, sector);
1407
1408 return ti->len - target_offset;
1409}
1410
1411static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1412{
1413 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001414 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001417 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001419 if (ti->max_io_len) {
1420 offset = dm_target_offset(ti, sector);
1421 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1422 max_len = sector_div(offset, ti->max_io_len);
1423 else
1424 max_len = offset & (ti->max_io_len - 1);
1425 max_len = ti->max_io_len - max_len;
1426
1427 if (len > max_len)
1428 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 return len;
1432}
1433
Mike Snitzer542f9032012-07-27 15:08:00 +01001434int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1435{
1436 if (len > UINT_MAX) {
1437 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1438 (unsigned long long)len, UINT_MAX);
1439 ti->error = "Maximum size of target IO is too large";
1440 return -EINVAL;
1441 }
1442
1443 ti->max_io_len = (uint32_t) len;
1444
1445 return 0;
1446}
1447EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1448
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001449/*
1450 * A target may call dm_accept_partial_bio only from the map routine. It is
1451 * allowed for all bio types except REQ_FLUSH.
1452 *
1453 * dm_accept_partial_bio informs the dm that the target only wants to process
1454 * additional n_sectors sectors of the bio and the rest of the data should be
1455 * sent in a next bio.
1456 *
1457 * A diagram that explains the arithmetics:
1458 * +--------------------+---------------+-------+
1459 * | 1 | 2 | 3 |
1460 * +--------------------+---------------+-------+
1461 *
1462 * <-------------- *tio->len_ptr --------------->
1463 * <------- bi_size ------->
1464 * <-- n_sectors -->
1465 *
1466 * Region 1 was already iterated over with bio_advance or similar function.
1467 * (it may be empty if the target doesn't use bio_advance)
1468 * Region 2 is the remaining bio size that the target wants to process.
1469 * (it may be empty if region 1 is non-empty, although there is no reason
1470 * to make it empty)
1471 * The target requires that region 3 is to be sent in the next bio.
1472 *
1473 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1474 * the partially processed part (the sum of regions 1+2) must be the same for all
1475 * copies of the bio.
1476 */
1477void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1478{
1479 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1480 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1481 BUG_ON(bio->bi_rw & REQ_FLUSH);
1482 BUG_ON(bi_size > *tio->len_ptr);
1483 BUG_ON(n_sectors > bi_size);
1484 *tio->len_ptr -= bi_size - n_sectors;
1485 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1486}
1487EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1488
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001489static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001492 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001493 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001494 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001495 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /*
1500 * Map the clone. If r == 0 we don't need to do
1501 * anything, the target has assumed ownership of
1502 * this io.
1503 */
1504 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001505 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001506 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001507 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001509
Mike Snitzerd07335e2010-11-16 12:52:38 +01001510 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1511 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001514 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1515 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001516 md = tio->io->md;
1517 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001518 free_tio(md, tio);
Mikulas Patockaab378442015-07-01 17:30:36 -04001519 } else if (r != DM_MAPIO_SUBMITTED) {
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001520 DMWARN("unimplemented target map return value: %d", r);
1521 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523}
1524
1525struct clone_info {
1526 struct mapped_device *md;
1527 struct dm_table *map;
1528 struct bio *bio;
1529 struct dm_io *io;
1530 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001531 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532};
1533
Mikulas Patockae0d66092014-03-14 18:40:39 -04001534static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001535{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001536 bio->bi_iter.bi_sector = sector;
1537 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540/*
1541 * Creates a bio that consists of range of complete bvecs.
1542 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001543static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001544 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Mikulas Patockadba14162012-10-12 21:02:15 +01001546 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001548 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001550 if (bio_integrity(bio))
1551 bio_integrity_clone(clone, bio, GFP_NOIO);
1552
1553 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1554 clone->bi_iter.bi_size = to_bytes(len);
1555
1556 if (bio_integrity(bio))
1557 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001560static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001561 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001562 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001563{
Mikulas Patockadba14162012-10-12 21:02:15 +01001564 struct dm_target_io *tio;
1565 struct bio *clone;
1566
Junichi Nomura99778272014-10-03 11:55:16 +00001567 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001568 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001569
1570 tio->io = ci->io;
1571 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001572 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001573
1574 return tio;
1575}
1576
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001577static void __clone_and_map_simple_bio(struct clone_info *ci,
1578 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001579 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001580{
Junichi Nomura99778272014-10-03 11:55:16 +00001581 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001582 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001583
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001584 tio->len_ptr = len;
1585
Junichi Nomura99778272014-10-03 11:55:16 +00001586 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001587 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001588 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001589
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001590 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001591}
1592
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001593static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001594 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001595{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001596 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001597
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001598 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001599 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001600}
1601
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001602static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001603{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001604 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001605 struct dm_target *ti;
1606
Mike Snitzerb372d362010-09-08 18:07:01 +02001607 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001608 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001609 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001610
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001611 return 0;
1612}
1613
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001614static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001615 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001616{
Mikulas Patockadba14162012-10-12 21:02:15 +01001617 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001618 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001619 unsigned target_bio_nr;
1620 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001621
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001622 /*
1623 * Does the target want to receive duplicate copies of the bio?
1624 */
1625 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1626 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001627
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001628 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001629 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001630 tio->len_ptr = len;
1631 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001632 __map_bio(tio);
1633 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001634}
1635
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001636typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001637
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001638static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001639{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001640 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001641}
1642
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001643static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001644{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001645 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001646}
1647
1648typedef bool (*is_split_required_fn)(struct dm_target *ti);
1649
1650static bool is_split_required_for_discard(struct dm_target *ti)
1651{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001652 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001653}
1654
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001655static int __send_changing_extent_only(struct clone_info *ci,
1656 get_num_bios_fn get_num_bios,
1657 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001658{
1659 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001660 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001661 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001662
Mike Snitzera79245b2010-08-12 04:14:24 +01001663 do {
1664 ti = dm_table_find_target(ci->map, ci->sector);
1665 if (!dm_target_is_valid(ti))
1666 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001667
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001668 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001669 * Even though the device advertised support for this type of
1670 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001671 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001672 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001673 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001674 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1675 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001676 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001677
Mike Snitzer23508a92012-12-21 20:23:37 +00001678 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001679 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001680 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001681 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001682
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001683 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001684
1685 ci->sector += len;
1686 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001687
1688 return 0;
1689}
1690
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001691static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001692{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001693 return __send_changing_extent_only(ci, get_num_discard_bios,
1694 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001695}
1696
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001697static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001698{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001699 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001700}
1701
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001702/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001703 * Select the correct strategy for processing a non-flush bio.
1704 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001705static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Mikulas Patockadba14162012-10-12 21:02:15 +01001707 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001708 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001709 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001711 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001712 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001713 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001714 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001715
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001716 ti = dm_table_find_target(ci->map, ci->sector);
1717 if (!dm_target_is_valid(ti))
1718 return -EIO;
1719
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001720 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001721
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001722 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001724 ci->sector += len;
1725 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001731 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001733static void __split_and_process_bio(struct mapped_device *md,
1734 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001737 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001739 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001740 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001741 return;
1742 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001743
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001744 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 ci.io = alloc_io(md);
1747 ci.io->error = 0;
1748 atomic_set(&ci.io->io_count, 1);
1749 ci.io->bio = bio;
1750 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001751 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001752 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001754 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001755
Mike Snitzerb372d362010-09-08 18:07:01 +02001756 if (bio->bi_rw & REQ_FLUSH) {
1757 ci.bio = &ci.md->flush_bio;
1758 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001759 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001760 /* dec_pending submits any data associated with flush */
1761 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001762 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001763 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001764 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001765 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001769 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771/*-----------------------------------------------------------------
1772 * CRUD END
1773 *---------------------------------------------------------------*/
1774
1775/*
1776 * The request function that just remaps the bio built up by
1777 * dm_merge_bvec.
1778 */
Jens Axboedece1632015-11-05 10:41:16 -07001779static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Kevin Corry12f03a42006-02-01 03:04:52 -08001781 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001783 int srcu_idx;
1784 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001786 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Gu Zheng18c0b222014-11-24 11:05:26 +08001788 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001789
Tejun Heo6a8736d2010-09-08 18:07:00 +02001790 /* if we're suspended, we have to queue this io for later */
1791 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001792 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Tejun Heo6a8736d2010-09-08 18:07:00 +02001794 if (bio_rw(bio) != READA)
1795 queue_io(md, bio);
1796 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001797 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001798 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001801 __split_and_process_bio(md, map, bio);
1802 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001803 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001804}
1805
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001806int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001807{
1808 return blk_queue_stackable(md->queue);
1809}
1810
Keith Busch466d89a2014-10-17 17:46:37 -06001811static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001812{
1813 int r;
1814
Keith Busch466d89a2014-10-17 17:46:37 -06001815 if (blk_queue_io_stat(clone->q))
1816 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001817
Keith Busch466d89a2014-10-17 17:46:37 -06001818 clone->start_time = jiffies;
1819 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001820 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001821 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001822 dm_complete_request(rq, r);
1823}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001824
Mike Snitzer78d8e582015-06-26 10:01:13 -04001825static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1826 void *data)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001827{
Mike Snitzer78d8e582015-06-26 10:01:13 -04001828 struct dm_rq_target_io *tio = data;
1829 struct dm_rq_clone_bio_info *info =
1830 container_of(bio, struct dm_rq_clone_bio_info, clone);
1831
1832 info->orig = bio_orig;
1833 info->tio = tio;
1834 bio->bi_end_io = end_clone_bio;
1835
1836 return 0;
1837}
1838
1839static int setup_clone(struct request *clone, struct request *rq,
1840 struct dm_rq_target_io *tio, gfp_t gfp_mask)
1841{
1842 int r;
1843
1844 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1845 dm_rq_bio_constructor, tio);
1846 if (r)
1847 return r;
1848
1849 clone->cmd = rq->cmd;
1850 clone->cmd_len = rq->cmd_len;
1851 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001852 clone->end_io = end_clone_request;
1853 clone->end_io_data = tio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001854
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001855 tio->clone = clone;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001856
1857 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001858}
1859
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001860static struct request *clone_rq(struct request *rq, struct mapped_device *md,
Keith Busch466d89a2014-10-17 17:46:37 -06001861 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001862{
Mike Snitzer02233342015-03-10 23:49:26 -04001863 /*
Mike Snitzerc5248f72016-02-20 14:02:49 -05001864 * Create clone for use with .request_fn request_queue
Mike Snitzer02233342015-03-10 23:49:26 -04001865 */
Mike Snitzer02233342015-03-10 23:49:26 -04001866 struct request *clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001867
Mike Snitzerc5248f72016-02-20 14:02:49 -05001868 clone = alloc_clone_request(md, gfp_mask);
1869 if (!clone)
1870 return NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001871
1872 blk_rq_init(NULL, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001873 if (setup_clone(clone, rq, tio, gfp_mask)) {
1874 /* -ENOMEM */
Mike Snitzerc5248f72016-02-20 14:02:49 -05001875 free_clone_request(md, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001876 return NULL;
1877 }
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001878
1879 return clone;
1880}
1881
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001882static void map_tio_request(struct kthread_work *work);
1883
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001884static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1885 struct mapped_device *md)
1886{
1887 tio->md = md;
1888 tio->ti = NULL;
1889 tio->clone = NULL;
1890 tio->orig = rq;
1891 tio->error = 0;
1892 memset(&tio->info, 0, sizeof(tio->info));
Mike Snitzer02233342015-03-10 23:49:26 -04001893 if (md->kworker_task)
1894 init_kthread_work(&tio->work, map_tio_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001895}
1896
Keith Busch466d89a2014-10-17 17:46:37 -06001897static struct dm_rq_target_io *prep_tio(struct request *rq,
1898 struct mapped_device *md, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001899{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001900 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001901 int srcu_idx;
1902 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001903
1904 tio = alloc_rq_tio(md, gfp_mask);
1905 if (!tio)
1906 return NULL;
1907
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001908 init_tio(tio, rq, md);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001909
Mike Snitzere5863d92014-12-17 21:08:12 -05001910 table = dm_get_live_table(md, &srcu_idx);
1911 if (!dm_table_mq_request_based(table)) {
1912 if (!clone_rq(rq, md, tio, gfp_mask)) {
1913 dm_put_live_table(md, srcu_idx);
1914 free_rq_tio(tio);
1915 return NULL;
1916 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001917 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001918 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001919
Keith Busch466d89a2014-10-17 17:46:37 -06001920 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001921}
1922
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001923/*
1924 * Called with the queue lock held.
1925 */
1926static int dm_prep_fn(struct request_queue *q, struct request *rq)
1927{
1928 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001929 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001930
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001931 if (unlikely(rq->special)) {
1932 DMWARN("Already has something in rq->special.");
1933 return BLKPREP_KILL;
1934 }
1935
Keith Busch466d89a2014-10-17 17:46:37 -06001936 tio = prep_tio(rq, md, GFP_ATOMIC);
1937 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001938 return BLKPREP_DEFER;
1939
Keith Busch466d89a2014-10-17 17:46:37 -06001940 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001941 rq->cmd_flags |= REQ_DONTPREP;
1942
1943 return BLKPREP_OK;
1944}
1945
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001946/*
1947 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05001948 * 0 : the request has been processed
1949 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1950 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001951 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001952static int map_request(struct dm_rq_target_io *tio, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001953 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001954{
Mike Snitzere5863d92014-12-17 21:08:12 -05001955 int r;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001956 struct dm_target *ti = tio->ti;
Mike Snitzere5863d92014-12-17 21:08:12 -05001957 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001958
Mike Snitzere5863d92014-12-17 21:08:12 -05001959 if (tio->clone) {
1960 clone = tio->clone;
1961 r = ti->type->map_rq(ti, clone, &tio->info);
1962 } else {
1963 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1964 if (r < 0) {
1965 /* The target wants to complete the I/O */
1966 dm_kill_unmapped_request(rq, r);
1967 return r;
1968 }
Junichi Nomura3a140752015-05-27 04:22:07 +00001969 if (r != DM_MAPIO_REMAPPED)
1970 return r;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001971 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
1972 /* -ENOMEM */
1973 ti->type->release_clone_rq(clone);
1974 return DM_MAPIO_REQUEUE;
1975 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001976 }
1977
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001978 switch (r) {
1979 case DM_MAPIO_SUBMITTED:
1980 /* The target has taken the I/O to submit by itself later */
1981 break;
1982 case DM_MAPIO_REMAPPED:
1983 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001984 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06001985 blk_rq_pos(rq));
1986 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001987 break;
1988 case DM_MAPIO_REQUEUE:
1989 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff12015-04-29 12:07:12 -04001990 dm_requeue_original_request(md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001991 break;
1992 default:
1993 if (r > 0) {
1994 DMWARN("unimplemented target map return value: %d", r);
1995 BUG();
1996 }
1997
1998 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06001999 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05002000 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002001 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00002002
Mike Snitzere5863d92014-12-17 21:08:12 -05002003 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002004}
2005
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002006static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002007{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002008 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05002009 struct request *rq = tio->orig;
2010 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002011
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002012 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
Mike Snitzer2d76fff12015-04-29 12:07:12 -04002013 dm_requeue_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002014}
2015
Keith Busch466d89a2014-10-17 17:46:37 -06002016static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002017{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002018 if (!orig->q->mq_ops)
2019 blk_start_request(orig);
2020 else
2021 blk_mq_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06002022 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002023
Mike Snitzer0ce65792015-02-26 00:50:28 -05002024 if (md->seq_rq_merge_deadline_usecs) {
2025 md->last_rq_pos = rq_end_sector(orig);
2026 md->last_rq_rw = rq_data_dir(orig);
2027 md->last_rq_start_time = ktime_get();
2028 }
Mike Snitzerde3ec862015-02-24 21:58:21 -05002029
Mikulas Patockae262f342015-06-09 17:22:49 -04002030 if (unlikely(dm_stats_used(&md->stats))) {
2031 struct dm_rq_target_io *tio = tio_from_request(orig);
2032 tio->duration_jiffies = jiffies;
2033 tio->n_sectors = blk_rq_sectors(orig);
2034 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
2035 tio->n_sectors, false, 0, &tio->stats_aux);
2036 }
2037
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002038 /*
2039 * Hold the md reference here for the in-flight I/O.
2040 * We can't rely on the reference count by device opener,
2041 * because the device may be closed during the request completion
2042 * when all bios are completed.
2043 * See the comment in rq_completed() too.
2044 */
2045 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002046}
2047
Mike Snitzer0ce65792015-02-26 00:50:28 -05002048#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
2049
2050ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
2051{
2052 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
2053}
2054
2055ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
2056 const char *buf, size_t count)
2057{
2058 unsigned deadline;
2059
Mike Snitzer17e149b2015-03-11 15:01:09 -04002060 if (!dm_request_based(md) || md->use_blk_mq)
Mike Snitzer0ce65792015-02-26 00:50:28 -05002061 return count;
2062
2063 if (kstrtouint(buf, 10, &deadline))
2064 return -EINVAL;
2065
2066 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
2067 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
2068
2069 md->seq_rq_merge_deadline_usecs = deadline;
2070
2071 return count;
2072}
2073
2074static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
2075{
2076 ktime_t kt_deadline;
2077
2078 if (!md->seq_rq_merge_deadline_usecs)
2079 return false;
2080
2081 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
2082 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
2083
2084 return !ktime_after(ktime_get(), kt_deadline);
2085}
2086
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002087/*
2088 * q->request_fn for request-based dm.
2089 * Called with the queue lock held.
2090 */
2091static void dm_request_fn(struct request_queue *q)
2092{
2093 struct mapped_device *md = q->queuedata;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002094 struct dm_target *ti = md->immutable_target;
Keith Busch466d89a2014-10-17 17:46:37 -06002095 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002096 struct dm_rq_target_io *tio;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002097 sector_t pos = 0;
2098
2099 if (unlikely(!ti)) {
2100 int srcu_idx;
2101 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2102
2103 ti = dm_table_find_target(map, pos);
2104 dm_put_live_table(md, srcu_idx);
2105 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002106
2107 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002108 * For suspend, check blk_queue_stopped() and increment
2109 * ->pending within a single queue_lock not to increment the
2110 * number of in-flight I/Os after the queue is stopped in
2111 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002112 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002113 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002114 rq = blk_peek_request(q);
2115 if (!rq)
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002116 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002117
Tejun Heo29e40132010-09-08 18:07:00 +02002118 /* always use block 0 to find the target for flushes for now */
2119 pos = 0;
2120 if (!(rq->cmd_flags & REQ_FLUSH))
2121 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002122
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002123 if ((dm_request_peeked_before_merge_deadline(md) &&
2124 md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2125 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
2126 (ti->type->busy && ti->type->busy(ti))) {
2127 blk_delay_queue(q, HZ / 100);
2128 return;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002129 }
Tejun Heo29e40132010-09-08 18:07:00 +02002130
Keith Busch466d89a2014-10-17 17:46:37 -06002131 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002132
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002133 tio = tio_from_request(rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002134 /* Establish tio->ti before queuing work (map_tio_request) */
2135 tio->ti = ti;
2136 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00002137 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002138 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002139}
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141static int dm_any_congested(void *congested_data, int bdi_bits)
2142{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002143 int r = bdi_bits;
2144 struct mapped_device *md = congested_data;
2145 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002147 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05002148 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002149 /*
Mike Snitzere522c032016-02-02 22:35:06 -05002150 * With request-based DM we only need to check the
2151 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002152 */
Mike Snitzere522c032016-02-02 22:35:06 -05002153 r = md->queue->backing_dev_info.wb.state & bdi_bits;
2154 } else {
2155 map = dm_get_live_table_fast(md);
2156 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002157 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05002158 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002159 }
2160 }
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return r;
2163}
2164
2165/*-----------------------------------------------------------------
2166 * An IDR is used to keep track of allocated minor numbers.
2167 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002168static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002170 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002172 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
2175/*
2176 * See if the device with a specific minor # is free.
2177 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002178static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002180 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 if (minor >= (1 << MINORBITS))
2183 return -EINVAL;
2184
Tejun Heoc9d76be2013-02-27 17:04:26 -08002185 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002186 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Tejun Heoc9d76be2013-02-27 17:04:26 -08002188 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002190 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002191 idr_preload_end();
2192 if (r < 0)
2193 return r == -ENOSPC ? -EBUSY : r;
2194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002197static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002199 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Tejun Heoc9d76be2013-02-27 17:04:26 -08002201 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002202 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Tejun Heoc9d76be2013-02-27 17:04:26 -08002204 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002206 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002207 idr_preload_end();
2208 if (r < 0)
2209 return r;
2210 *minor = r;
2211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002214static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Mikulas Patocka53d59142009-04-02 19:55:37 +01002216static void dm_wq_work(struct work_struct *work);
2217
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002218static void dm_init_md_queue(struct mapped_device *md)
2219{
2220 /*
2221 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002222 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002223 * The type is decided at the first table loading time.
2224 * To prevent problematic device stacking, clear the queue flag
2225 * for request stacking support until then.
2226 *
2227 * This queue is new, so no concurrency on the queue_flags.
2228 */
2229 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04002230
2231 /*
2232 * Initialize data that will only be used by a non-blk-mq DM queue
2233 * - must do so here (in alloc_dev callchain) before queue is used
2234 */
2235 md->queue->queuedata = md;
2236 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002237}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002238
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002239static void dm_init_old_md_queue(struct mapped_device *md)
2240{
Mike Snitzer17e149b2015-03-11 15:01:09 -04002241 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002242 dm_init_md_queue(md);
2243
2244 /*
2245 * Initialize aspects of queue that aren't relevant for blk-mq
2246 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002247 md->queue->backing_dev_info.congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002248 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002249}
2250
Mike Snitzer0f209722015-04-28 11:50:29 -04002251static void cleanup_mapped_device(struct mapped_device *md)
2252{
Mike Snitzer0f209722015-04-28 11:50:29 -04002253 if (md->wq)
2254 destroy_workqueue(md->wq);
2255 if (md->kworker_task)
2256 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02002257 mempool_destroy(md->io_pool);
2258 mempool_destroy(md->rq_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04002259 if (md->bs)
2260 bioset_free(md->bs);
2261
Mikulas Patockab06075a2015-07-10 17:21:43 -04002262 cleanup_srcu_struct(&md->io_barrier);
2263
Mike Snitzer0f209722015-04-28 11:50:29 -04002264 if (md->disk) {
2265 spin_lock(&_minor_lock);
2266 md->disk->private_data = NULL;
2267 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04002268 del_gendisk(md->disk);
2269 put_disk(md->disk);
2270 }
2271
2272 if (md->queue)
2273 blk_cleanup_queue(md->queue);
2274
2275 if (md->bdev) {
2276 bdput(md->bdev);
2277 md->bdev = NULL;
2278 }
2279}
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281/*
2282 * Allocate and initialise a blank device with a given minor.
2283 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002284static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002287 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002288 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 if (!md) {
2291 DMWARN("unable to allocate device, out of memory.");
2292 return NULL;
2293 }
2294
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002295 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002296 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002299 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002300 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002301 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002302 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002304 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002306 r = init_srcu_struct(&md->io_barrier);
2307 if (r < 0)
2308 goto bad_io_barrier;
2309
Mike Snitzer17e149b2015-03-11 15:01:09 -04002310 md->use_blk_mq = use_blk_mq;
Mike Snitzera5664da2010-08-12 04:14:01 +01002311 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002312 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002313 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002314 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002315 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002317 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002319 atomic_set(&md->uevent_seq, 0);
2320 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002321 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002322 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002324 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04002326 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002328 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 md->disk = alloc_disk(1);
2331 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04002332 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002334 atomic_set(&md->pending[0], 0);
2335 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002336 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002337 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002338 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002339 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002340 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 md->disk->major = _major;
2343 md->disk->first_minor = minor;
2344 md->disk->fops = &dm_blk_dops;
2345 md->disk->queue = md->queue;
2346 md->disk->private_data = md;
2347 sprintf(md->disk->disk_name, "dm-%d", minor);
2348 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002349 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Tejun Heo670368a2013-07-30 08:40:21 -04002351 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002352 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002353 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002354
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002355 md->bdev = bdget_disk(md->disk, 0);
2356 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002357 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002358
Tejun Heo6a8736d2010-09-08 18:07:00 +02002359 bio_init(&md->flush_bio);
2360 md->flush_bio.bi_bdev = md->bdev;
2361 md->flush_bio.bi_rw = WRITE_FLUSH;
2362
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002363 dm_stats_init(&md->stats);
2364
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002365 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002366 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002367 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002368 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002369
2370 BUG_ON(old_md != MINOR_ALLOCED);
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return md;
2373
Mike Snitzer0f209722015-04-28 11:50:29 -04002374bad:
2375 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002376bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002378bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002379 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002380bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 kfree(md);
2382 return NULL;
2383}
2384
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002385static void unlock_fs(struct mapped_device *md);
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387static void free_dev(struct mapped_device *md)
2388{
Tejun Heof331c022008-09-03 09:01:48 +02002389 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002390
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002391 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002392
Mike Snitzer0f209722015-04-28 11:50:29 -04002393 cleanup_mapped_device(md);
Mike Snitzer1c357a12016-02-06 17:01:17 -05002394 if (md->tag_set) {
2395 blk_mq_free_tag_set(md->tag_set);
2396 kfree(md->tag_set);
2397 }
Mike Snitzer0f209722015-04-28 11:50:29 -04002398
2399 free_table_devices(&md->table_devices);
2400 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002401 free_minor(minor);
2402
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002403 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 kfree(md);
2405}
2406
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002407static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2408{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002409 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002410
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002411 if (md->bs) {
2412 /* The md already has necessary mempools. */
2413 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002414 /*
2415 * Reload bioset because front_pad may have changed
2416 * because a different table was loaded.
2417 */
2418 bioset_free(md->bs);
2419 md->bs = p->bs;
2420 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002421 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002422 /*
2423 * There's no need to reload with request-based dm
2424 * because the size of front_pad doesn't change.
2425 * Note for future: If you are to reload bioset,
2426 * prep-ed requests in the queue may refer
2427 * to bio from the old bioset, so you must walk
2428 * through the queue to unprep.
2429 */
2430 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002431 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002432
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002433 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2434
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002435 md->io_pool = p->io_pool;
2436 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002437 md->rq_pool = p->rq_pool;
2438 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002439 md->bs = p->bs;
2440 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002441
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002442out:
Mike Snitzer02233342015-03-10 23:49:26 -04002443 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002444 dm_table_free_md_mempools(t);
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447/*
2448 * Bind a table to the device.
2449 */
2450static void event_callback(void *context)
2451{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002452 unsigned long flags;
2453 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct mapped_device *md = (struct mapped_device *) context;
2455
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002456 spin_lock_irqsave(&md->uevent_lock, flags);
2457 list_splice_init(&md->uevent_list, &uevents);
2458 spin_unlock_irqrestore(&md->uevent_lock, flags);
2459
Tejun Heoed9e1982008-08-25 19:56:05 +09002460 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 atomic_inc(&md->event_nr);
2463 wake_up(&md->eventq);
2464}
2465
Mike Snitzerc2176492011-01-13 19:53:46 +00002466/*
2467 * Protected by md->suspend_lock obtained by dm_swap_table().
2468 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002469static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002471 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002473 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002476/*
2477 * Returns old map, which caller must destroy.
2478 */
2479static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2480 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002482 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002483 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 sector_t size;
2485
2486 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002487
2488 /*
2489 * Wipe any geometry if the size of the table changed.
2490 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002491 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002492 memset(&md->geometry, 0, sizeof(md->geometry));
2493
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002494 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002496 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002497
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002498 /*
2499 * The queue hasn't been stopped yet, if the old table type wasn't
2500 * for request-based during suspension. So stop it to prevent
2501 * I/O mapping before resume.
2502 * This must be done before setting the queue restrictions,
2503 * because request-based dm may be run just after the setting.
2504 */
Mike Snitzer16f12262016-01-31 17:22:27 -05002505 if (dm_table_request_based(t)) {
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002506 stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05002507 /*
2508 * Leverage the fact that request-based DM targets are
2509 * immutable singletons and establish md->immutable_target
2510 * - used to optimize both dm_request_fn and dm_mq_queue_rq
2511 */
2512 md->immutable_target = dm_table_get_immutable_target(t);
2513 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002514
2515 __bind_mempools(md, t);
2516
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002517 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002518 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002519 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2520
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002521 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002522 if (old_map)
2523 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002524
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002525 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
Alasdair G Kergona7940152009-12-10 23:52:23 +00002528/*
2529 * Returns unbound table for the caller to free.
2530 */
2531static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002533 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002536 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302539 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002540 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002541
2542 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
2545/*
2546 * Constructor for a new device.
2547 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002548int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 struct mapped_device *md;
2551
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002552 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (!md)
2554 return -ENXIO;
2555
Milan Broz784aae72009-01-06 03:05:12 +00002556 dm_sysfs_init(md);
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 *result = md;
2559 return 0;
2560}
2561
Mike Snitzera5664da2010-08-12 04:14:01 +01002562/*
2563 * Functions to manage md->type.
2564 * All are required to hold md->type_lock.
2565 */
2566void dm_lock_md_type(struct mapped_device *md)
2567{
2568 mutex_lock(&md->type_lock);
2569}
2570
2571void dm_unlock_md_type(struct mapped_device *md)
2572{
2573 mutex_unlock(&md->type_lock);
2574}
2575
2576void dm_set_md_type(struct mapped_device *md, unsigned type)
2577{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002578 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002579 md->type = type;
2580}
2581
2582unsigned dm_get_md_type(struct mapped_device *md)
2583{
2584 return md->type;
2585}
2586
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002587struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2588{
2589 return md->immutable_target_type;
2590}
2591
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002592/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002593 * The queue_limits are only valid as long as you have a reference
2594 * count on 'md'.
2595 */
2596struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2597{
2598 BUG_ON(!atomic_read(&md->holders));
2599 return &md->queue->limits;
2600}
2601EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2602
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002603static void init_rq_based_worker_thread(struct mapped_device *md)
2604{
2605 /* Initialize the request-based DM worker thread */
2606 init_kthread_worker(&md->kworker);
2607 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2608 "kdmwork-%s", dm_device_name(md));
2609}
2610
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002611/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002612 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2613 */
2614static int dm_init_request_based_queue(struct mapped_device *md)
2615{
2616 struct request_queue *q = NULL;
2617
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002618 /* Fully initialize the queue */
2619 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2620 if (!q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002621 return -EINVAL;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002622
Mike Snitzer0ce65792015-02-26 00:50:28 -05002623 /* disable dm_request_fn's merge heuristic by default */
2624 md->seq_rq_merge_deadline_usecs = 0;
2625
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002626 md->queue = q;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002627 dm_init_old_md_queue(md);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002628 blk_queue_softirq_done(md->queue, dm_softirq_done);
2629 blk_queue_prep_rq(md->queue, dm_prep_fn);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002630
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002631 init_rq_based_worker_thread(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002632
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002633 elv_register_queue(md->queue);
2634
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002635 return 0;
2636}
2637
2638static int dm_mq_init_request(void *data, struct request *rq,
2639 unsigned int hctx_idx, unsigned int request_idx,
2640 unsigned int numa_node)
2641{
2642 struct mapped_device *md = data;
2643 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2644
2645 /*
2646 * Must initialize md member of tio, otherwise it won't
2647 * be available in dm_mq_queue_rq.
2648 */
2649 tio->md = md;
2650
2651 return 0;
2652}
2653
2654static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2655 const struct blk_mq_queue_data *bd)
2656{
2657 struct request *rq = bd->rq;
2658 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2659 struct mapped_device *md = tio->md;
Mike Snitzer16f12262016-01-31 17:22:27 -05002660 struct dm_target *ti = md->immutable_target;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002661
Mike Snitzer16f12262016-01-31 17:22:27 -05002662 if (unlikely(!ti)) {
2663 int srcu_idx;
2664 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002665
Mike Snitzer16f12262016-01-31 17:22:27 -05002666 ti = dm_table_find_target(map, 0);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002667 dm_put_live_table(md, srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002668 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002669
2670 if (ti->type->busy && ti->type->busy(ti))
2671 return BLK_MQ_RQ_QUEUE_BUSY;
2672
2673 dm_start_request(md, rq);
2674
2675 /* Init tio using md established in .init_request */
2676 init_tio(tio, rq, md);
2677
Mike Snitzer02233342015-03-10 23:49:26 -04002678 /*
2679 * Establish tio->ti before queuing work (map_tio_request)
2680 * or making direct call to map_request().
2681 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002682 tio->ti = ti;
Mike Snitzer02233342015-03-10 23:49:26 -04002683
Mike Snitzerc5248f72016-02-20 14:02:49 -05002684 /* Direct call is fine since .queue_rq allows allocations */
2685 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
2686 /* Undo dm_start_request() before requeuing */
2687 rq_end_stats(md, rq);
2688 rq_completed(md, rq_data_dir(rq), false);
2689 return BLK_MQ_RQ_QUEUE_BUSY;
Mike Snitzer02233342015-03-10 23:49:26 -04002690 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002691
2692 return BLK_MQ_RQ_QUEUE_OK;
2693}
2694
2695static struct blk_mq_ops dm_mq_ops = {
2696 .queue_rq = dm_mq_queue_rq,
2697 .map_queue = blk_mq_map_queue,
2698 .complete = dm_softirq_done,
2699 .init_request = dm_mq_init_request,
2700};
2701
2702static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
2703{
Mike Snitzer02233342015-03-10 23:49:26 -04002704 unsigned md_type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002705 struct request_queue *q;
2706 int err;
2707
Mike Snitzerc5248f72016-02-20 14:02:49 -05002708 if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) {
2709 DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
2710 return -EINVAL;
2711 }
2712
Mike Snitzer1c357a12016-02-06 17:01:17 -05002713 md->tag_set = kzalloc(sizeof(struct blk_mq_tag_set), GFP_KERNEL);
2714 if (!md->tag_set)
2715 return -ENOMEM;
2716
2717 md->tag_set->ops = &dm_mq_ops;
2718 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
2719 md->tag_set->numa_node = NUMA_NO_NODE;
2720 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2721 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
2722 md->tag_set->driver_data = md;
2723
2724 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002725
Mike Snitzer1c357a12016-02-06 17:01:17 -05002726 err = blk_mq_alloc_tag_set(md->tag_set);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002727 if (err)
Mike Snitzer1c357a12016-02-06 17:01:17 -05002728 goto out_kfree_tag_set;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002729
Mike Snitzer1c357a12016-02-06 17:01:17 -05002730 q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002731 if (IS_ERR(q)) {
2732 err = PTR_ERR(q);
2733 goto out_tag_set;
2734 }
2735 md->queue = q;
2736 dm_init_md_queue(md);
2737
2738 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
2739 blk_mq_register_disk(md->disk);
2740
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002741 return 0;
2742
2743out_tag_set:
Mike Snitzer1c357a12016-02-06 17:01:17 -05002744 blk_mq_free_tag_set(md->tag_set);
2745out_kfree_tag_set:
2746 kfree(md->tag_set);
2747
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002748 return err;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002749}
2750
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002751static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2752{
2753 if (type == DM_TYPE_BIO_BASED)
2754 return type;
2755
2756 return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2757}
2758
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002759/*
2760 * Setup the DM device's queue based on md's type
2761 */
2762int dm_setup_md_queue(struct mapped_device *md)
2763{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002764 int r;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002765 unsigned md_type = filter_md_type(dm_get_md_type(md), md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002766
2767 switch (md_type) {
2768 case DM_TYPE_REQUEST_BASED:
2769 r = dm_init_request_based_queue(md);
2770 if (r) {
Mike Snitzerff36ab32015-02-23 17:56:37 -05002771 DMWARN("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002772 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002773 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002774 break;
2775 case DM_TYPE_MQ_REQUEST_BASED:
2776 r = dm_init_request_based_blk_mq_queue(md);
2777 if (r) {
2778 DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
2779 return r;
2780 }
2781 break;
2782 case DM_TYPE_BIO_BASED:
2783 dm_init_old_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002784 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04002785 /*
2786 * DM handles splitting bios as needed. Free the bio_split bioset
2787 * since it won't be used (saves 1 process per bio-based DM device).
2788 */
2789 bioset_free(md->queue->bio_split);
2790 md->queue->bio_split = NULL;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002791 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002792 }
2793
2794 return 0;
2795}
2796
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002797struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
2799 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 unsigned minor = MINOR(dev);
2801
2802 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2803 return NULL;
2804
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002805 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002808 if (md) {
2809 if ((md == MINOR_ALLOCED ||
2810 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2811 dm_deleting_md(md) ||
2812 test_bit(DMF_FREEING, &md->flags))) {
2813 md = NULL;
2814 goto out;
2815 }
2816 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002819out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002820 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
David Teigland637842c2006-01-06 00:20:00 -08002822 return md;
2823}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002824EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002825
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002826void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002827{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002828 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831void dm_set_mdptr(struct mapped_device *md, void *ptr)
2832{
2833 md->interface_ptr = ptr;
2834}
2835
2836void dm_get(struct mapped_device *md)
2837{
2838 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002839 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002842int dm_hold(struct mapped_device *md)
2843{
2844 spin_lock(&_minor_lock);
2845 if (test_bit(DMF_FREEING, &md->flags)) {
2846 spin_unlock(&_minor_lock);
2847 return -EBUSY;
2848 }
2849 dm_get(md);
2850 spin_unlock(&_minor_lock);
2851 return 0;
2852}
2853EXPORT_SYMBOL_GPL(dm_hold);
2854
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002855const char *dm_device_name(struct mapped_device *md)
2856{
2857 return md->name;
2858}
2859EXPORT_SYMBOL_GPL(dm_device_name);
2860
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002861static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002863 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002864 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002866 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002867
Mike Snitzer63a4f062015-03-23 17:01:43 -04002868 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002869 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2870 set_bit(DMF_FREEING, &md->flags);
2871 spin_unlock(&_minor_lock);
2872
Mike Snitzer02233342015-03-10 23:49:26 -04002873 if (dm_request_based(md) && md->kworker_task)
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002874 flush_kthread_worker(&md->kworker);
2875
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002876 /*
2877 * Take suspend_lock so that presuspend and postsuspend methods
2878 * do not race with internal suspend.
2879 */
2880 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002881 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002882 if (!dm_suspended_md(md)) {
2883 dm_table_presuspend_targets(map);
2884 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002886 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2887 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002888 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002889
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002890 /*
2891 * Rare, but there may be I/O requests still going to complete,
2892 * for example. Wait for all references to disappear.
2893 * No one should increment the reference count of the mapped_device,
2894 * after the mapped_device state becomes DMF_FREEING.
2895 */
2896 if (wait)
2897 while (atomic_read(&md->holders))
2898 msleep(1);
2899 else if (atomic_read(&md->holders))
2900 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2901 dm_device_name(md), atomic_read(&md->holders));
2902
2903 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002904 dm_table_destroy(__unbind(md));
2905 free_dev(md);
2906}
2907
2908void dm_destroy(struct mapped_device *md)
2909{
2910 __dm_destroy(md, true);
2911}
2912
2913void dm_destroy_immediate(struct mapped_device *md)
2914{
2915 __dm_destroy(md, false);
2916}
2917
2918void dm_put(struct mapped_device *md)
2919{
2920 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
Edward Goggin79eb8852007-05-09 02:32:56 -07002922EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Mikulas Patocka401600d2009-04-02 19:55:38 +01002924static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002925{
2926 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002927 DECLARE_WAITQUEUE(wait, current);
2928
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002929 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002930
2931 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002932 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002933
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002934 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002935 break;
2936
Mikulas Patocka401600d2009-04-02 19:55:38 +01002937 if (interruptible == TASK_INTERRUPTIBLE &&
2938 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002939 r = -EINTR;
2940 break;
2941 }
2942
2943 io_schedule();
2944 }
2945 set_current_state(TASK_RUNNING);
2946
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002947 remove_wait_queue(&md->wait, &wait);
2948
Milan Broz46125c12008-02-08 02:10:30 +00002949 return r;
2950}
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952/*
2953 * Process the deferred bios
2954 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002955static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Mikulas Patockaef208582009-04-02 19:55:38 +01002957 struct mapped_device *md = container_of(work, struct mapped_device,
2958 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002959 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002960 int srcu_idx;
2961 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002963 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002964
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002965 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002966 spin_lock_irq(&md->deferred_lock);
2967 c = bio_list_pop(&md->deferred);
2968 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002969
Tejun Heo6a8736d2010-09-08 18:07:00 +02002970 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002971 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002972
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002973 if (dm_request_based(md))
2974 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002975 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002976 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002977 }
Milan Broz73d410c2008-02-08 02:10:25 +00002978
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002979 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002982static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002983{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002984 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002985 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002986 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002987}
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002990 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002992struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Mike Christie87eb5b22013-03-01 22:45:48 +00002994 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002995 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002996 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Daniel Walkere61290a2008-02-08 02:10:08 +00002998 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003001 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Mike Snitzer3ae70652012-09-26 23:45:45 +01003004 /*
3005 * If the new table has no data devices, retain the existing limits.
3006 * This helps multipath with queue_if_no_path if all paths disappear,
3007 * then new I/O is queued based on these limits, and then some paths
3008 * reappear.
3009 */
3010 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003011 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003012 if (live_map)
3013 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003014 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003015 }
3016
Mike Christie87eb5b22013-03-01 22:45:48 +00003017 if (!live_map) {
3018 r = dm_calculate_queue_limits(table, &limits);
3019 if (r) {
3020 map = ERR_PTR(r);
3021 goto out;
3022 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003023 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003024
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003025 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003027out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003028 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003029 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
3032/*
3033 * Functions to lock and unlock any filesystem running on the
3034 * device.
3035 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003036static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003038 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003041
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003042 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003043 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003044 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003045 md->frozen_sb = NULL;
3046 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003047 }
3048
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003049 set_bit(DMF_FROZEN, &md->flags);
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return 0;
3052}
3053
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003054static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003056 if (!test_bit(DMF_FROZEN, &md->flags))
3057 return;
3058
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003059 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003061 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
3064/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04003065 * If __dm_suspend returns 0, the device is completely quiescent
3066 * now. There is no request-processing activity. All new requests
3067 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003068 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04003069 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003070 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003071static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3072 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003074 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3075 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3076 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003078 /*
3079 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
3080 * This flag is cleared before dm_suspend returns.
3081 */
3082 if (noflush)
3083 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
3084
Mike Snitzerd67ee212014-10-28 20:13:31 -04003085 /*
3086 * This gets reverted if there's an error later and the targets
3087 * provide the .presuspend_undo hook.
3088 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003089 dm_table_presuspend_targets(map);
3090
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003091 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003092 * Flush I/O to the device.
3093 * Any I/O submitted after lock_fs() may not be flushed.
3094 * noflush takes precedence over do_lockfs.
3095 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003096 */
3097 if (!noflush && do_lockfs) {
3098 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003099 if (r) {
3100 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003101 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04003102 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003106 * Here we must make sure that no processes are submitting requests
3107 * to target drivers i.e. no one may be executing
3108 * __split_and_process_bio. This is called from dm_request and
3109 * dm_wq_work.
3110 *
3111 * To get all processes out of __split_and_process_bio in dm_request,
3112 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02003113 * __split_and_process_bio from dm_request and quiesce the thread
3114 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
3115 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01003117 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003118 if (map)
3119 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003121 /*
Tejun Heo29e40132010-09-08 18:07:00 +02003122 * Stop md->queue before flushing md->wq in case request-based
3123 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003124 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003125 if (dm_request_based(md)) {
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003126 stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04003127 if (md->kworker_task)
3128 flush_kthread_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003129 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003130
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003131 flush_workqueue(md->wq);
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003134 * At this point no more requests are entering target request routines.
3135 * We call dm_wait_for_completion to wait for all existing requests
3136 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003138 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Milan Broz6d6f10d2008-02-08 02:10:22 +00003140 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01003141 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003142 if (map)
3143 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00003146 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003147 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00003148
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003149 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003150 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003151
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003152 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003153 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003154 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003155 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003156
Mike Snitzerffcc3932014-10-28 18:34:52 -04003157 return r;
3158}
3159
3160/*
3161 * We need to be able to change a mapping table under a mounted
3162 * filesystem. For example we might want to move some data in
3163 * the background. Before the table can be swapped with
3164 * dm_bind_table, dm_suspend must be called to flush any in
3165 * flight bios and ensure that any further io gets deferred.
3166 */
3167/*
3168 * Suspend mechanism in request-based dm.
3169 *
3170 * 1. Flush all I/Os by lock_fs() if needed.
3171 * 2. Stop dispatching any I/O by stopping the request_queue.
3172 * 3. Wait for all in-flight I/Os to be completed or requeued.
3173 *
3174 * To abort suspend, start the request_queue.
3175 */
3176int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3177{
3178 struct dm_table *map = NULL;
3179 int r = 0;
3180
3181retry:
3182 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3183
3184 if (dm_suspended_md(md)) {
3185 r = -EINVAL;
3186 goto out_unlock;
3187 }
3188
3189 if (dm_suspended_internally_md(md)) {
3190 /* already internally suspended, wait for internal resume */
3191 mutex_unlock(&md->suspend_lock);
3192 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3193 if (r)
3194 return r;
3195 goto retry;
3196 }
3197
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003198 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003199
3200 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3201 if (r)
3202 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 set_bit(DMF_SUSPENDED, &md->flags);
3205
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00003206 dm_table_postsuspend_targets(map);
3207
Alasdair G Kergond2874832006-11-08 17:44:43 -08003208out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00003209 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003210 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
Mike Snitzerffcc3932014-10-28 18:34:52 -04003213static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003215 if (map) {
3216 int r = dm_table_resume_targets(map);
3217 if (r)
3218 return r;
3219 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003220
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003221 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003222
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003223 /*
3224 * Flushing deferred I/Os must be done after targets are resumed
3225 * so that mapping of targets can work correctly.
3226 * Request-based dm is queueing the deferred I/Os in its request_queue.
3227 */
3228 if (dm_request_based(md))
3229 start_queue(md->queue);
3230
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003231 unlock_fs(md);
3232
Mike Snitzerffcc3932014-10-28 18:34:52 -04003233 return 0;
3234}
3235
3236int dm_resume(struct mapped_device *md)
3237{
3238 int r = -EINVAL;
3239 struct dm_table *map = NULL;
3240
3241retry:
3242 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3243
3244 if (!dm_suspended_md(md))
3245 goto out;
3246
3247 if (dm_suspended_internally_md(md)) {
3248 /* already internally suspended, wait for internal resume */
3249 mutex_unlock(&md->suspend_lock);
3250 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3251 if (r)
3252 return r;
3253 goto retry;
3254 }
3255
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003256 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003257 if (!map || !dm_table_get_size(map))
3258 goto out;
3259
3260 r = __dm_resume(md, map);
3261 if (r)
3262 goto out;
3263
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003264 clear_bit(DMF_SUSPENDED, &md->flags);
3265
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003266 r = 0;
3267out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003268 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003269
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003270 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003273/*
3274 * Internal suspend/resume works like userspace-driven suspend. It waits
3275 * until all bios finish and prevents issuing new bios to the target drivers.
3276 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003277 */
3278
Mike Snitzerffcc3932014-10-28 18:34:52 -04003279static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3280{
3281 struct dm_table *map = NULL;
3282
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003283 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003284 return; /* nested internal suspend */
3285
3286 if (dm_suspended_md(md)) {
3287 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3288 return; /* nest suspend */
3289 }
3290
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003291 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003292
3293 /*
3294 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3295 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3296 * would require changing .presuspend to return an error -- avoid this
3297 * until there is a need for more elaborate variants of internal suspend.
3298 */
3299 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3300
3301 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3302
3303 dm_table_postsuspend_targets(map);
3304}
3305
3306static void __dm_internal_resume(struct mapped_device *md)
3307{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003308 BUG_ON(!md->internal_suspend_count);
3309
3310 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003311 return; /* resume from nested internal suspend */
3312
3313 if (dm_suspended_md(md))
3314 goto done; /* resume from nested suspend */
3315
3316 /*
3317 * NOTE: existing callers don't need to call dm_table_resume_targets
3318 * (which may fail -- so best to avoid it for now by passing NULL map)
3319 */
3320 (void) __dm_resume(md, NULL);
3321
3322done:
3323 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3324 smp_mb__after_atomic();
3325 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3326}
3327
3328void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003329{
3330 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003331 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3332 mutex_unlock(&md->suspend_lock);
3333}
3334EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3335
3336void dm_internal_resume(struct mapped_device *md)
3337{
3338 mutex_lock(&md->suspend_lock);
3339 __dm_internal_resume(md);
3340 mutex_unlock(&md->suspend_lock);
3341}
3342EXPORT_SYMBOL_GPL(dm_internal_resume);
3343
3344/*
3345 * Fast variants of internal suspend/resume hold md->suspend_lock,
3346 * which prevents interaction with userspace-driven suspend.
3347 */
3348
3349void dm_internal_suspend_fast(struct mapped_device *md)
3350{
3351 mutex_lock(&md->suspend_lock);
3352 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003353 return;
3354
3355 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3356 synchronize_srcu(&md->io_barrier);
3357 flush_workqueue(md->wq);
3358 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3359}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003360EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003361
Mike Snitzerffcc3932014-10-28 18:34:52 -04003362void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003363{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003364 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003365 goto done;
3366
3367 dm_queue_flush(md);
3368
3369done:
3370 mutex_unlock(&md->suspend_lock);
3371}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003372EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374/*-----------------------------------------------------------------
3375 * Event notification.
3376 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003377int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003378 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003379{
Milan Broz60935eb2009-06-22 10:12:30 +01003380 char udev_cookie[DM_COOKIE_LENGTH];
3381 char *envp[] = { udev_cookie, NULL };
3382
3383 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003384 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003385 else {
3386 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3387 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003388 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3389 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003390 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003391}
3392
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003393uint32_t dm_next_uevent_seq(struct mapped_device *md)
3394{
3395 return atomic_add_return(1, &md->uevent_seq);
3396}
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398uint32_t dm_get_event_nr(struct mapped_device *md)
3399{
3400 return atomic_read(&md->event_nr);
3401}
3402
3403int dm_wait_event(struct mapped_device *md, int event_nr)
3404{
3405 return wait_event_interruptible(md->eventq,
3406 (event_nr != atomic_read(&md->event_nr)));
3407}
3408
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003409void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3410{
3411 unsigned long flags;
3412
3413 spin_lock_irqsave(&md->uevent_lock, flags);
3414 list_add(elist, &md->uevent_list);
3415 spin_unlock_irqrestore(&md->uevent_lock, flags);
3416}
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418/*
3419 * The gendisk is only valid as long as you have a reference
3420 * count on 'md'.
3421 */
3422struct gendisk *dm_disk(struct mapped_device *md)
3423{
3424 return md->disk;
3425}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00003426EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Milan Broz784aae72009-01-06 03:05:12 +00003428struct kobject *dm_kobject(struct mapped_device *md)
3429{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003430 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003431}
3432
Milan Broz784aae72009-01-06 03:05:12 +00003433struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3434{
3435 struct mapped_device *md;
3436
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003437 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003438
Milan Broz4d89b7b2009-06-22 10:12:11 +01003439 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003440 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003441 return NULL;
3442
Milan Broz784aae72009-01-06 03:05:12 +00003443 dm_get(md);
3444 return md;
3445}
3446
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003447int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
3449 return test_bit(DMF_SUSPENDED, &md->flags);
3450}
3451
Mike Snitzerffcc3932014-10-28 18:34:52 -04003452int dm_suspended_internally_md(struct mapped_device *md)
3453{
3454 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3455}
3456
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003457int dm_test_deferred_remove_flag(struct mapped_device *md)
3458{
3459 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3460}
3461
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003462int dm_suspended(struct dm_target *ti)
3463{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003464 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003465}
3466EXPORT_SYMBOL_GPL(dm_suspended);
3467
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003468int dm_noflush_suspending(struct dm_target *ti)
3469{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003470 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003471}
3472EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3473
Mike Snitzer78d8e582015-06-26 10:01:13 -04003474struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
3475 unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003476{
Mike Snitzer78d8e582015-06-26 10:01:13 -04003477 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3478 struct kmem_cache *cachep = NULL;
3479 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003480 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003481
3482 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003483 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003484
Mike Snitzer78d8e582015-06-26 10:01:13 -04003485 type = filter_md_type(type, md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04003486
Mike Snitzer78d8e582015-06-26 10:01:13 -04003487 switch (type) {
3488 case DM_TYPE_BIO_BASED:
3489 cachep = _io_cache;
3490 pool_size = dm_get_reserved_bio_based_ios();
3491 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
3492 break;
3493 case DM_TYPE_REQUEST_BASED:
3494 cachep = _rq_tio_cache;
3495 pool_size = dm_get_reserved_rq_based_ios();
3496 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3497 if (!pools->rq_pool)
3498 goto out;
3499 /* fall through to setup remaining rq-based pools */
3500 case DM_TYPE_MQ_REQUEST_BASED:
3501 if (!pool_size)
3502 pool_size = dm_get_reserved_rq_based_ios();
3503 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3504 /* per_bio_data_size is not used. See __bind_mempools(). */
3505 WARN_ON(per_bio_data_size != 0);
3506 break;
3507 default:
3508 BUG();
3509 }
3510
3511 if (cachep) {
3512 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3513 if (!pools->io_pool)
3514 goto out;
3515 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003516
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003517 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003518 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003519 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003520
Martin K. Petersena91a2782011-03-17 11:11:05 +01003521 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003522 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003523
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003524 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003525
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003526out:
3527 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003528
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003529 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003530}
3531
3532void dm_free_md_mempools(struct dm_md_mempools *pools)
3533{
3534 if (!pools)
3535 return;
3536
Julia Lawall6f659852015-09-13 14:15:05 +02003537 mempool_destroy(pools->io_pool);
3538 mempool_destroy(pools->rq_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003539
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003540 if (pools->bs)
3541 bioset_free(pools->bs);
3542
3543 kfree(pools);
3544}
3545
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003546static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003547 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003548{
3549 struct mapped_device *md = bdev->bd_disk->private_data;
3550 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003551 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003552 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003553
Mike Snitzer956a4022016-02-18 16:13:51 -05003554 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003555 if (r < 0)
3556 return r;
3557
3558 ops = bdev->bd_disk->fops->pr_ops;
3559 if (ops && ops->pr_register)
3560 r = ops->pr_register(bdev, old_key, new_key, flags);
3561 else
3562 r = -EOPNOTSUPP;
3563
Mike Snitzer956a4022016-02-18 16:13:51 -05003564 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003565 return r;
3566}
3567
3568static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05003569 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003570{
3571 struct mapped_device *md = bdev->bd_disk->private_data;
3572 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003573 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003574 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003575
Mike Snitzer956a4022016-02-18 16:13:51 -05003576 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003577 if (r < 0)
3578 return r;
3579
3580 ops = bdev->bd_disk->fops->pr_ops;
3581 if (ops && ops->pr_reserve)
3582 r = ops->pr_reserve(bdev, key, type, flags);
3583 else
3584 r = -EOPNOTSUPP;
3585
Mike Snitzer956a4022016-02-18 16:13:51 -05003586 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003587 return r;
3588}
3589
3590static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3591{
3592 struct mapped_device *md = bdev->bd_disk->private_data;
3593 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003594 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003595 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003596
Mike Snitzer956a4022016-02-18 16:13:51 -05003597 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003598 if (r < 0)
3599 return r;
3600
3601 ops = bdev->bd_disk->fops->pr_ops;
3602 if (ops && ops->pr_release)
3603 r = ops->pr_release(bdev, key, type);
3604 else
3605 r = -EOPNOTSUPP;
3606
Mike Snitzer956a4022016-02-18 16:13:51 -05003607 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003608 return r;
3609}
3610
3611static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003612 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003613{
3614 struct mapped_device *md = bdev->bd_disk->private_data;
3615 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003616 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003617 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003618
Mike Snitzer956a4022016-02-18 16:13:51 -05003619 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003620 if (r < 0)
3621 return r;
3622
3623 ops = bdev->bd_disk->fops->pr_ops;
3624 if (ops && ops->pr_preempt)
3625 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3626 else
3627 r = -EOPNOTSUPP;
3628
Mike Snitzer956a4022016-02-18 16:13:51 -05003629 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003630 return r;
3631}
3632
3633static int dm_pr_clear(struct block_device *bdev, u64 key)
3634{
3635 struct mapped_device *md = bdev->bd_disk->private_data;
3636 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003637 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003638 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003639
Mike Snitzer956a4022016-02-18 16:13:51 -05003640 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003641 if (r < 0)
3642 return r;
3643
3644 ops = bdev->bd_disk->fops->pr_ops;
3645 if (ops && ops->pr_clear)
3646 r = ops->pr_clear(bdev, key);
3647 else
3648 r = -EOPNOTSUPP;
3649
Mike Snitzer956a4022016-02-18 16:13:51 -05003650 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003651 return r;
3652}
3653
3654static const struct pr_ops dm_pr_ops = {
3655 .pr_register = dm_pr_register,
3656 .pr_reserve = dm_pr_reserve,
3657 .pr_release = dm_pr_release,
3658 .pr_preempt = dm_pr_preempt,
3659 .pr_clear = dm_pr_clear,
3660};
3661
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003662static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 .open = dm_blk_open,
3664 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003665 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003666 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003667 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 .owner = THIS_MODULE
3669};
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671/*
3672 * module hooks
3673 */
3674module_init(dm_init);
3675module_exit(dm_exit);
3676
3677module_param(major, uint, 0);
3678MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003679
Mike Snitzere8603132013-09-12 18:06:12 -04003680module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3681MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3682
Mike Snitzerf4790822013-09-12 18:06:12 -04003683module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3684MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3685
Mike Snitzer17e149b2015-03-11 15:01:09 -04003686module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
3687MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
3688
Mike Snitzerfaad87d2016-01-28 16:52:56 -05003689module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
3690MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
3691
3692module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
3693MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");
3694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695MODULE_DESCRIPTION(DM_NAME " driver");
3696MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3697MODULE_LICENSE("GPL");