blob: e9035959f9049a72ff16c91dc62c4730460cfabd [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 Patocka2a7faeb12013-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 Patocka2a7faeb12013-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);
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001144 /*
1145 * NOTE: for the blk-mq queue stacked on request_fn queue(s) case:
1146 * no need to call free_clone_request() because we leverage blk-mq by
1147 * allocating the clone at the end of the blk-mq pdu (see: clone_rq)
1148 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001149
1150 if (!md->queue->mq_ops)
1151 free_rq_tio(tio);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001152}
1153
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001154/*
1155 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001156 * Must be called without clone's queue lock held,
1157 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001158 */
1159static void dm_end_request(struct request *clone, int error)
1160{
1161 int rw = rq_data_dir(clone);
1162 struct dm_rq_target_io *tio = clone->end_io_data;
1163 struct mapped_device *md = tio->md;
1164 struct request *rq = tio->orig;
1165
Tejun Heo29e40132010-09-08 18:07:00 +02001166 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001167 rq->errors = clone->errors;
1168 rq->resid_len = clone->resid_len;
1169
1170 if (rq->sense)
1171 /*
1172 * We are using the sense buffer of the original
1173 * request.
1174 * So setting the length of the sense data is enough.
1175 */
1176 rq->sense_len = clone->sense_len;
1177 }
1178
Mike Snitzere5d8de32015-05-28 15:12:52 -04001179 free_rq_clone(clone);
Mikulas Patockae262f342015-06-09 17:22:49 -04001180 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001181 if (!rq->q->mq_ops)
1182 blk_end_request_all(rq, error);
1183 else
1184 blk_mq_end_request(rq, error);
Tejun Heo29e40132010-09-08 18:07:00 +02001185 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001186}
1187
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001188static void dm_unprep_request(struct request *rq)
1189{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001190 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001191 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001192
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001193 if (!rq->q->mq_ops) {
1194 rq->special = NULL;
1195 rq->cmd_flags &= ~REQ_DONTPREP;
1196 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001197
Mike Snitzere5863d92014-12-17 21:08:12 -05001198 if (clone)
Mike Snitzere5d8de32015-05-28 15:12:52 -04001199 free_rq_clone(clone);
Mike Snitzer4328daa2016-02-21 19:09:22 -05001200 else if (!tio->md->queue->mq_ops)
1201 free_rq_tio(tio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001202}
1203
1204/*
1205 * Requeue the original request of a clone.
1206 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001207static void old_requeue_request(struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001208{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001209 struct request_queue *q = rq->q;
1210 unsigned long flags;
1211
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001212 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001213 blk_requeue_request(q, rq);
Junichi Nomura4ae99442015-05-26 08:25:54 +00001214 blk_run_queue_async(q);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001215 spin_unlock_irqrestore(q->queue_lock, flags);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001216}
1217
Mike Snitzer2d76fff12015-04-29 12:07:12 -04001218static void dm_requeue_original_request(struct mapped_device *md,
1219 struct request *rq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001220{
1221 int rw = rq_data_dir(rq);
1222
1223 dm_unprep_request(rq);
1224
Mikulas Patockae262f342015-06-09 17:22:49 -04001225 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001226 if (!rq->q->mq_ops)
1227 old_requeue_request(rq);
1228 else {
1229 blk_mq_requeue_request(rq);
1230 blk_mq_kick_requeue_list(rq->q);
1231 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001232
Keith Busch466d89a2014-10-17 17:46:37 -06001233 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001234}
Keith Busch466d89a2014-10-17 17:46:37 -06001235
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001236static void old_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001237{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001238 unsigned long flags;
1239
1240 if (blk_queue_stopped(q))
1241 return;
1242
1243 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001244 blk_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001245 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001246}
1247
1248static void stop_queue(struct request_queue *q)
1249{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001250 if (!q->mq_ops)
1251 old_stop_queue(q);
1252 else
1253 blk_mq_stop_hw_queues(q);
1254}
1255
1256static void old_start_queue(struct request_queue *q)
1257{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001258 unsigned long flags;
1259
1260 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001261 if (blk_queue_stopped(q))
1262 blk_start_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001263 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001264}
1265
1266static void start_queue(struct request_queue *q)
1267{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001268 if (!q->mq_ops)
1269 old_start_queue(q);
1270 else
1271 blk_mq_start_stopped_hw_queues(q, true);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001272}
1273
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001274static void dm_done(struct request *clone, int error, bool mapped)
1275{
1276 int r = error;
1277 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001278 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001279
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001280 if (tio->ti) {
1281 rq_end_io = tio->ti->type->rq_end_io;
1282
1283 if (mapped && rq_end_io)
1284 r = rq_end_io(tio->ti, clone, error, &tio->info);
1285 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001286
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001287 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1288 !clone->q->limits.max_write_same_sectors))
1289 disable_write_same(tio->md);
1290
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001291 if (r <= 0)
1292 /* The target wants to complete the I/O */
1293 dm_end_request(clone, r);
1294 else if (r == DM_ENDIO_INCOMPLETE)
1295 /* The target will handle the I/O */
1296 return;
1297 else if (r == DM_ENDIO_REQUEUE)
1298 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff12015-04-29 12:07:12 -04001299 dm_requeue_original_request(tio->md, tio->orig);
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001300 else {
1301 DMWARN("unimplemented target endio return value: %d", r);
1302 BUG();
1303 }
1304}
1305
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001306/*
1307 * Request completion handler for request-based dm
1308 */
1309static void dm_softirq_done(struct request *rq)
1310{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001311 bool mapped = true;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001312 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001313 struct request *clone = tio->clone;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001314 int rw;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001315
Mike Snitzere5863d92014-12-17 21:08:12 -05001316 if (!clone) {
Mikulas Patockae262f342015-06-09 17:22:49 -04001317 rq_end_stats(tio->md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001318 rw = rq_data_dir(rq);
1319 if (!rq->q->mq_ops) {
1320 blk_end_request_all(rq, tio->error);
1321 rq_completed(tio->md, rw, false);
1322 free_rq_tio(tio);
1323 } else {
1324 blk_mq_end_request(rq, tio->error);
1325 rq_completed(tio->md, rw, false);
1326 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001327 return;
1328 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001329
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001330 if (rq->cmd_flags & REQ_FAILED)
1331 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001332
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001333 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001334}
1335
1336/*
1337 * Complete the clone and the original request with the error status
1338 * through softirq context.
1339 */
Keith Busch466d89a2014-10-17 17:46:37 -06001340static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001341{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001342 struct dm_rq_target_io *tio = tio_from_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001343
1344 tio->error = error;
Mike Snitzer6acfe682016-02-05 08:49:01 -05001345 if (!rq->q->mq_ops)
1346 blk_complete_request(rq);
1347 else
1348 blk_mq_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001349}
1350
1351/*
1352 * Complete the not-mapped clone and the original request with the error status
1353 * through softirq context.
1354 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001355 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001356 */
Keith Busch466d89a2014-10-17 17:46:37 -06001357static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001358{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001359 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001360 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001361}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001362
1363/*
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001364 * Called with the clone's queue lock held (for non-blk-mq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001365 */
1366static void end_clone_request(struct request *clone, int error)
1367{
Keith Busch466d89a2014-10-17 17:46:37 -06001368 struct dm_rq_target_io *tio = clone->end_io_data;
1369
Mike Snitzere5863d92014-12-17 21:08:12 -05001370 if (!clone->q->mq_ops) {
1371 /*
1372 * For just cleaning up the information of the queue in which
1373 * the clone was dispatched.
1374 * The clone is *NOT* freed actually here because it is alloced
1375 * from dm own mempool (REQ_ALLOCED isn't set).
1376 */
1377 __blk_put_request(clone->q, clone);
1378 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001379
1380 /*
1381 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001382 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001383 * - another request may be submitted by the upper level driver
1384 * of the stacking during the completion
1385 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001386 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001387 */
Keith Busch466d89a2014-10-17 17:46:37 -06001388 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001389}
1390
Mike Snitzer56a67df2010-08-12 04:14:10 +01001391/*
1392 * Return maximum size of I/O possible at the supplied sector up to the current
1393 * target boundary.
1394 */
1395static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001397 sector_t target_offset = dm_target_offset(ti, sector);
1398
1399 return ti->len - target_offset;
1400}
1401
1402static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1403{
1404 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001405 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001408 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001410 if (ti->max_io_len) {
1411 offset = dm_target_offset(ti, sector);
1412 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1413 max_len = sector_div(offset, ti->max_io_len);
1414 else
1415 max_len = offset & (ti->max_io_len - 1);
1416 max_len = ti->max_io_len - max_len;
1417
1418 if (len > max_len)
1419 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
1422 return len;
1423}
1424
Mike Snitzer542f9032012-07-27 15:08:00 +01001425int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1426{
1427 if (len > UINT_MAX) {
1428 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1429 (unsigned long long)len, UINT_MAX);
1430 ti->error = "Maximum size of target IO is too large";
1431 return -EINVAL;
1432 }
1433
1434 ti->max_io_len = (uint32_t) len;
1435
1436 return 0;
1437}
1438EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1439
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001440/*
1441 * A target may call dm_accept_partial_bio only from the map routine. It is
1442 * allowed for all bio types except REQ_FLUSH.
1443 *
1444 * dm_accept_partial_bio informs the dm that the target only wants to process
1445 * additional n_sectors sectors of the bio and the rest of the data should be
1446 * sent in a next bio.
1447 *
1448 * A diagram that explains the arithmetics:
1449 * +--------------------+---------------+-------+
1450 * | 1 | 2 | 3 |
1451 * +--------------------+---------------+-------+
1452 *
1453 * <-------------- *tio->len_ptr --------------->
1454 * <------- bi_size ------->
1455 * <-- n_sectors -->
1456 *
1457 * Region 1 was already iterated over with bio_advance or similar function.
1458 * (it may be empty if the target doesn't use bio_advance)
1459 * Region 2 is the remaining bio size that the target wants to process.
1460 * (it may be empty if region 1 is non-empty, although there is no reason
1461 * to make it empty)
1462 * The target requires that region 3 is to be sent in the next bio.
1463 *
1464 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1465 * the partially processed part (the sum of regions 1+2) must be the same for all
1466 * copies of the bio.
1467 */
1468void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1469{
1470 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1471 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1472 BUG_ON(bio->bi_rw & REQ_FLUSH);
1473 BUG_ON(bi_size > *tio->len_ptr);
1474 BUG_ON(n_sectors > bi_size);
1475 *tio->len_ptr -= bi_size - n_sectors;
1476 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1477}
1478EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1479
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001480static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001483 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001484 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001485 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001486 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 /*
1491 * Map the clone. If r == 0 we don't need to do
1492 * anything, the target has assumed ownership of
1493 * this io.
1494 */
1495 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001496 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001497 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001498 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001500
Mike Snitzerd07335e2010-11-16 12:52:38 +01001501 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1502 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001505 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1506 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001507 md = tio->io->md;
1508 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001509 free_tio(md, tio);
Mikulas Patockaab378442015-07-01 17:30:36 -04001510 } else if (r != DM_MAPIO_SUBMITTED) {
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001511 DMWARN("unimplemented target map return value: %d", r);
1512 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514}
1515
1516struct clone_info {
1517 struct mapped_device *md;
1518 struct dm_table *map;
1519 struct bio *bio;
1520 struct dm_io *io;
1521 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001522 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523};
1524
Mikulas Patockae0d66092014-03-14 18:40:39 -04001525static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001526{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001527 bio->bi_iter.bi_sector = sector;
1528 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531/*
1532 * Creates a bio that consists of range of complete bvecs.
1533 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001534static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001535 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Mikulas Patockadba14162012-10-12 21:02:15 +01001537 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001539 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001541 if (bio_integrity(bio))
1542 bio_integrity_clone(clone, bio, GFP_NOIO);
1543
1544 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1545 clone->bi_iter.bi_size = to_bytes(len);
1546
1547 if (bio_integrity(bio))
1548 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001551static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001552 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001553 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001554{
Mikulas Patockadba14162012-10-12 21:02:15 +01001555 struct dm_target_io *tio;
1556 struct bio *clone;
1557
Junichi Nomura99778272014-10-03 11:55:16 +00001558 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001559 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001560
1561 tio->io = ci->io;
1562 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001563 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001564
1565 return tio;
1566}
1567
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001568static void __clone_and_map_simple_bio(struct clone_info *ci,
1569 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001570 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001571{
Junichi Nomura99778272014-10-03 11:55:16 +00001572 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001573 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001574
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001575 tio->len_ptr = len;
1576
Junichi Nomura99778272014-10-03 11:55:16 +00001577 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001578 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001579 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001580
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001581 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001582}
1583
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001584static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001585 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001586{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001587 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001588
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001589 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001590 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001591}
1592
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001593static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001594{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001595 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001596 struct dm_target *ti;
1597
Mike Snitzerb372d362010-09-08 18:07:01 +02001598 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001599 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001600 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001601
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001602 return 0;
1603}
1604
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001605static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001606 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001607{
Mikulas Patockadba14162012-10-12 21:02:15 +01001608 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001609 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001610 unsigned target_bio_nr;
1611 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001612
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001613 /*
1614 * Does the target want to receive duplicate copies of the bio?
1615 */
1616 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1617 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001618
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001619 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001620 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001621 tio->len_ptr = len;
1622 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001623 __map_bio(tio);
1624 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001625}
1626
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001627typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001628
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001629static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001630{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001631 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001632}
1633
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001634static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001635{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001636 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001637}
1638
1639typedef bool (*is_split_required_fn)(struct dm_target *ti);
1640
1641static bool is_split_required_for_discard(struct dm_target *ti)
1642{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001643 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001644}
1645
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001646static int __send_changing_extent_only(struct clone_info *ci,
1647 get_num_bios_fn get_num_bios,
1648 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001649{
1650 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001651 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001652 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001653
Mike Snitzera79245b2010-08-12 04:14:24 +01001654 do {
1655 ti = dm_table_find_target(ci->map, ci->sector);
1656 if (!dm_target_is_valid(ti))
1657 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001658
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001659 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001660 * Even though the device advertised support for this type of
1661 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001662 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001663 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001664 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001665 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1666 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001667 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001668
Mike Snitzer23508a92012-12-21 20:23:37 +00001669 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001670 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001671 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001672 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001673
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001674 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001675
1676 ci->sector += len;
1677 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001678
1679 return 0;
1680}
1681
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001682static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001683{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001684 return __send_changing_extent_only(ci, get_num_discard_bios,
1685 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001686}
1687
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001688static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001689{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001690 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001691}
1692
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001693/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001694 * Select the correct strategy for processing a non-flush bio.
1695 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001696static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Mikulas Patockadba14162012-10-12 21:02:15 +01001698 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001699 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001700 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001702 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001703 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001704 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001705 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001706
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001707 ti = dm_table_find_target(ci->map, ci->sector);
1708 if (!dm_target_is_valid(ti))
1709 return -EIO;
1710
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001711 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001712
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001713 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001715 ci->sector += len;
1716 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
1721/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001722 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001724static void __split_and_process_bio(struct mapped_device *md,
1725 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001728 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001730 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001731 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001732 return;
1733 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001734
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001735 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 ci.io = alloc_io(md);
1738 ci.io->error = 0;
1739 atomic_set(&ci.io->io_count, 1);
1740 ci.io->bio = bio;
1741 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001742 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001743 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001745 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001746
Mike Snitzerb372d362010-09-08 18:07:01 +02001747 if (bio->bi_rw & REQ_FLUSH) {
1748 ci.bio = &ci.md->flush_bio;
1749 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001750 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001751 /* dec_pending submits any data associated with flush */
1752 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001753 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001754 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001755 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001756 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001760 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762/*-----------------------------------------------------------------
1763 * CRUD END
1764 *---------------------------------------------------------------*/
1765
1766/*
1767 * The request function that just remaps the bio built up by
1768 * dm_merge_bvec.
1769 */
Jens Axboedece1632015-11-05 10:41:16 -07001770static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Kevin Corry12f03a42006-02-01 03:04:52 -08001772 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001774 int srcu_idx;
1775 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001777 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Gu Zheng18c0b222014-11-24 11:05:26 +08001779 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001780
Tejun Heo6a8736d2010-09-08 18:07:00 +02001781 /* if we're suspended, we have to queue this io for later */
1782 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001783 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Tejun Heo6a8736d2010-09-08 18:07:00 +02001785 if (bio_rw(bio) != READA)
1786 queue_io(md, bio);
1787 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001788 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001789 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001792 __split_and_process_bio(md, map, bio);
1793 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001794 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001795}
1796
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001797int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001798{
1799 return blk_queue_stackable(md->queue);
1800}
1801
Keith Busch466d89a2014-10-17 17:46:37 -06001802static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001803{
1804 int r;
1805
Keith Busch466d89a2014-10-17 17:46:37 -06001806 if (blk_queue_io_stat(clone->q))
1807 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001808
Keith Busch466d89a2014-10-17 17:46:37 -06001809 clone->start_time = jiffies;
1810 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001811 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001812 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001813 dm_complete_request(rq, r);
1814}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001815
Mike Snitzer78d8e582015-06-26 10:01:13 -04001816static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1817 void *data)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001818{
Mike Snitzer78d8e582015-06-26 10:01:13 -04001819 struct dm_rq_target_io *tio = data;
1820 struct dm_rq_clone_bio_info *info =
1821 container_of(bio, struct dm_rq_clone_bio_info, clone);
1822
1823 info->orig = bio_orig;
1824 info->tio = tio;
1825 bio->bi_end_io = end_clone_bio;
1826
1827 return 0;
1828}
1829
1830static int setup_clone(struct request *clone, struct request *rq,
1831 struct dm_rq_target_io *tio, gfp_t gfp_mask)
1832{
1833 int r;
1834
1835 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1836 dm_rq_bio_constructor, tio);
1837 if (r)
1838 return r;
1839
1840 clone->cmd = rq->cmd;
1841 clone->cmd_len = rq->cmd_len;
1842 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001843 clone->end_io = end_clone_request;
1844 clone->end_io_data = tio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001845
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001846 tio->clone = clone;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001847
1848 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001849}
1850
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001851static struct request *clone_rq(struct request *rq, struct mapped_device *md,
Keith Busch466d89a2014-10-17 17:46:37 -06001852 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001853{
Mike Snitzer02233342015-03-10 23:49:26 -04001854 /*
1855 * Do not allocate a clone if tio->clone was already set
1856 * (see: dm_mq_queue_rq).
1857 */
1858 bool alloc_clone = !tio->clone;
1859 struct request *clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001860
Mike Snitzer02233342015-03-10 23:49:26 -04001861 if (alloc_clone) {
1862 clone = alloc_clone_request(md, gfp_mask);
1863 if (!clone)
1864 return NULL;
1865 } else
1866 clone = tio->clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001867
1868 blk_rq_init(NULL, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001869 if (setup_clone(clone, rq, tio, gfp_mask)) {
1870 /* -ENOMEM */
1871 if (alloc_clone)
1872 free_clone_request(md, clone);
1873 return NULL;
1874 }
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001875
1876 return clone;
1877}
1878
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001879static void map_tio_request(struct kthread_work *work);
1880
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001881static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1882 struct mapped_device *md)
1883{
1884 tio->md = md;
1885 tio->ti = NULL;
1886 tio->clone = NULL;
1887 tio->orig = rq;
1888 tio->error = 0;
1889 memset(&tio->info, 0, sizeof(tio->info));
Mike Snitzer02233342015-03-10 23:49:26 -04001890 if (md->kworker_task)
1891 init_kthread_work(&tio->work, map_tio_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001892}
1893
Keith Busch466d89a2014-10-17 17:46:37 -06001894static struct dm_rq_target_io *prep_tio(struct request *rq,
1895 struct mapped_device *md, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001896{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001897 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001898 int srcu_idx;
1899 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001900
1901 tio = alloc_rq_tio(md, gfp_mask);
1902 if (!tio)
1903 return NULL;
1904
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001905 init_tio(tio, rq, md);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001906
Mike Snitzere5863d92014-12-17 21:08:12 -05001907 table = dm_get_live_table(md, &srcu_idx);
1908 if (!dm_table_mq_request_based(table)) {
1909 if (!clone_rq(rq, md, tio, gfp_mask)) {
1910 dm_put_live_table(md, srcu_idx);
1911 free_rq_tio(tio);
1912 return NULL;
1913 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001914 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001915 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001916
Keith Busch466d89a2014-10-17 17:46:37 -06001917 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001918}
1919
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001920/*
1921 * Called with the queue lock held.
1922 */
1923static int dm_prep_fn(struct request_queue *q, struct request *rq)
1924{
1925 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001926 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001927
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001928 if (unlikely(rq->special)) {
1929 DMWARN("Already has something in rq->special.");
1930 return BLKPREP_KILL;
1931 }
1932
Keith Busch466d89a2014-10-17 17:46:37 -06001933 tio = prep_tio(rq, md, GFP_ATOMIC);
1934 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001935 return BLKPREP_DEFER;
1936
Keith Busch466d89a2014-10-17 17:46:37 -06001937 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001938 rq->cmd_flags |= REQ_DONTPREP;
1939
1940 return BLKPREP_OK;
1941}
1942
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001943/*
1944 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05001945 * 0 : the request has been processed
1946 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1947 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001948 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001949static int map_request(struct dm_rq_target_io *tio, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001950 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001951{
Mike Snitzere5863d92014-12-17 21:08:12 -05001952 int r;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001953 struct dm_target *ti = tio->ti;
Mike Snitzere5863d92014-12-17 21:08:12 -05001954 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001955
Mike Snitzere5863d92014-12-17 21:08:12 -05001956 if (tio->clone) {
1957 clone = tio->clone;
1958 r = ti->type->map_rq(ti, clone, &tio->info);
1959 } else {
1960 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1961 if (r < 0) {
1962 /* The target wants to complete the I/O */
1963 dm_kill_unmapped_request(rq, r);
1964 return r;
1965 }
Junichi Nomura3a140752015-05-27 04:22:07 +00001966 if (r != DM_MAPIO_REMAPPED)
1967 return r;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001968 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
1969 /* -ENOMEM */
1970 ti->type->release_clone_rq(clone);
1971 return DM_MAPIO_REQUEUE;
1972 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001973 }
1974
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001975 switch (r) {
1976 case DM_MAPIO_SUBMITTED:
1977 /* The target has taken the I/O to submit by itself later */
1978 break;
1979 case DM_MAPIO_REMAPPED:
1980 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001981 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06001982 blk_rq_pos(rq));
1983 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001984 break;
1985 case DM_MAPIO_REQUEUE:
1986 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff12015-04-29 12:07:12 -04001987 dm_requeue_original_request(md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001988 break;
1989 default:
1990 if (r > 0) {
1991 DMWARN("unimplemented target map return value: %d", r);
1992 BUG();
1993 }
1994
1995 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06001996 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05001997 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001998 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001999
Mike Snitzere5863d92014-12-17 21:08:12 -05002000 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002001}
2002
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002003static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002004{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002005 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05002006 struct request *rq = tio->orig;
2007 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002008
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002009 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
Mike Snitzer2d76fff12015-04-29 12:07:12 -04002010 dm_requeue_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002011}
2012
Keith Busch466d89a2014-10-17 17:46:37 -06002013static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002014{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002015 if (!orig->q->mq_ops)
2016 blk_start_request(orig);
2017 else
2018 blk_mq_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06002019 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002020
Mike Snitzer0ce65792015-02-26 00:50:28 -05002021 if (md->seq_rq_merge_deadline_usecs) {
2022 md->last_rq_pos = rq_end_sector(orig);
2023 md->last_rq_rw = rq_data_dir(orig);
2024 md->last_rq_start_time = ktime_get();
2025 }
Mike Snitzerde3ec862015-02-24 21:58:21 -05002026
Mikulas Patockae262f342015-06-09 17:22:49 -04002027 if (unlikely(dm_stats_used(&md->stats))) {
2028 struct dm_rq_target_io *tio = tio_from_request(orig);
2029 tio->duration_jiffies = jiffies;
2030 tio->n_sectors = blk_rq_sectors(orig);
2031 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
2032 tio->n_sectors, false, 0, &tio->stats_aux);
2033 }
2034
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002035 /*
2036 * Hold the md reference here for the in-flight I/O.
2037 * We can't rely on the reference count by device opener,
2038 * because the device may be closed during the request completion
2039 * when all bios are completed.
2040 * See the comment in rq_completed() too.
2041 */
2042 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002043}
2044
Mike Snitzer0ce65792015-02-26 00:50:28 -05002045#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
2046
2047ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
2048{
2049 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
2050}
2051
2052ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
2053 const char *buf, size_t count)
2054{
2055 unsigned deadline;
2056
Mike Snitzer17e149b2015-03-11 15:01:09 -04002057 if (!dm_request_based(md) || md->use_blk_mq)
Mike Snitzer0ce65792015-02-26 00:50:28 -05002058 return count;
2059
2060 if (kstrtouint(buf, 10, &deadline))
2061 return -EINVAL;
2062
2063 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
2064 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
2065
2066 md->seq_rq_merge_deadline_usecs = deadline;
2067
2068 return count;
2069}
2070
2071static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
2072{
2073 ktime_t kt_deadline;
2074
2075 if (!md->seq_rq_merge_deadline_usecs)
2076 return false;
2077
2078 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
2079 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
2080
2081 return !ktime_after(ktime_get(), kt_deadline);
2082}
2083
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002084/*
2085 * q->request_fn for request-based dm.
2086 * Called with the queue lock held.
2087 */
2088static void dm_request_fn(struct request_queue *q)
2089{
2090 struct mapped_device *md = q->queuedata;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002091 struct dm_target *ti = md->immutable_target;
Keith Busch466d89a2014-10-17 17:46:37 -06002092 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002093 struct dm_rq_target_io *tio;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002094 sector_t pos = 0;
2095
2096 if (unlikely(!ti)) {
2097 int srcu_idx;
2098 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2099
2100 ti = dm_table_find_target(map, pos);
2101 dm_put_live_table(md, srcu_idx);
2102 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002103
2104 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002105 * For suspend, check blk_queue_stopped() and increment
2106 * ->pending within a single queue_lock not to increment the
2107 * number of in-flight I/Os after the queue is stopped in
2108 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002109 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002110 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002111 rq = blk_peek_request(q);
2112 if (!rq)
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002113 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002114
Tejun Heo29e40132010-09-08 18:07:00 +02002115 /* always use block 0 to find the target for flushes for now */
2116 pos = 0;
2117 if (!(rq->cmd_flags & REQ_FLUSH))
2118 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002119
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002120 if ((dm_request_peeked_before_merge_deadline(md) &&
2121 md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2122 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
2123 (ti->type->busy && ti->type->busy(ti))) {
2124 blk_delay_queue(q, HZ / 100);
2125 return;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002126 }
Tejun Heo29e40132010-09-08 18:07:00 +02002127
Keith Busch466d89a2014-10-17 17:46:37 -06002128 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002129
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002130 tio = tio_from_request(rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002131 /* Establish tio->ti before queuing work (map_tio_request) */
2132 tio->ti = ti;
2133 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00002134 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002135 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002136}
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138static int dm_any_congested(void *congested_data, int bdi_bits)
2139{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002140 int r = bdi_bits;
2141 struct mapped_device *md = congested_data;
2142 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002144 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05002145 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002146 /*
Mike Snitzere522c032016-02-02 22:35:06 -05002147 * With request-based DM we only need to check the
2148 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002149 */
Mike Snitzere522c032016-02-02 22:35:06 -05002150 r = md->queue->backing_dev_info.wb.state & bdi_bits;
2151 } else {
2152 map = dm_get_live_table_fast(md);
2153 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002154 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05002155 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002156 }
2157 }
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return r;
2160}
2161
2162/*-----------------------------------------------------------------
2163 * An IDR is used to keep track of allocated minor numbers.
2164 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002165static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002167 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002169 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
2172/*
2173 * See if the device with a specific minor # is free.
2174 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002175static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002177 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 if (minor >= (1 << MINORBITS))
2180 return -EINVAL;
2181
Tejun Heoc9d76be2013-02-27 17:04:26 -08002182 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002183 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Tejun Heoc9d76be2013-02-27 17:04:26 -08002185 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002187 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002188 idr_preload_end();
2189 if (r < 0)
2190 return r == -ENOSPC ? -EBUSY : r;
2191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002194static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002196 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Tejun Heoc9d76be2013-02-27 17:04:26 -08002198 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002199 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Tejun Heoc9d76be2013-02-27 17:04:26 -08002201 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002203 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002204 idr_preload_end();
2205 if (r < 0)
2206 return r;
2207 *minor = r;
2208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002211static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Mikulas Patocka53d59142009-04-02 19:55:37 +01002213static void dm_wq_work(struct work_struct *work);
2214
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002215static void dm_init_md_queue(struct mapped_device *md)
2216{
2217 /*
2218 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002219 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002220 * The type is decided at the first table loading time.
2221 * To prevent problematic device stacking, clear the queue flag
2222 * for request stacking support until then.
2223 *
2224 * This queue is new, so no concurrency on the queue_flags.
2225 */
2226 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04002227
2228 /*
2229 * Initialize data that will only be used by a non-blk-mq DM queue
2230 * - must do so here (in alloc_dev callchain) before queue is used
2231 */
2232 md->queue->queuedata = md;
2233 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002234}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002235
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002236static void dm_init_old_md_queue(struct mapped_device *md)
2237{
Mike Snitzer17e149b2015-03-11 15:01:09 -04002238 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002239 dm_init_md_queue(md);
2240
2241 /*
2242 * Initialize aspects of queue that aren't relevant for blk-mq
2243 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002244 md->queue->backing_dev_info.congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002245 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002246}
2247
Mike Snitzer0f209722015-04-28 11:50:29 -04002248static void cleanup_mapped_device(struct mapped_device *md)
2249{
Mike Snitzer0f209722015-04-28 11:50:29 -04002250 if (md->wq)
2251 destroy_workqueue(md->wq);
2252 if (md->kworker_task)
2253 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02002254 mempool_destroy(md->io_pool);
2255 mempool_destroy(md->rq_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04002256 if (md->bs)
2257 bioset_free(md->bs);
2258
Mikulas Patockab06075a2015-07-10 17:21:43 -04002259 cleanup_srcu_struct(&md->io_barrier);
2260
Mike Snitzer0f209722015-04-28 11:50:29 -04002261 if (md->disk) {
2262 spin_lock(&_minor_lock);
2263 md->disk->private_data = NULL;
2264 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04002265 del_gendisk(md->disk);
2266 put_disk(md->disk);
2267 }
2268
2269 if (md->queue)
2270 blk_cleanup_queue(md->queue);
2271
2272 if (md->bdev) {
2273 bdput(md->bdev);
2274 md->bdev = NULL;
2275 }
2276}
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278/*
2279 * Allocate and initialise a blank device with a given minor.
2280 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002281static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002284 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002285 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 if (!md) {
2288 DMWARN("unable to allocate device, out of memory.");
2289 return NULL;
2290 }
2291
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002292 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002293 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002296 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002297 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002298 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002299 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002301 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002303 r = init_srcu_struct(&md->io_barrier);
2304 if (r < 0)
2305 goto bad_io_barrier;
2306
Mike Snitzer17e149b2015-03-11 15:01:09 -04002307 md->use_blk_mq = use_blk_mq;
Mike Snitzera5664da2010-08-12 04:14:01 +01002308 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002309 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002310 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002311 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002312 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002314 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002316 atomic_set(&md->uevent_seq, 0);
2317 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002318 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002319 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002321 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04002323 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002325 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 md->disk = alloc_disk(1);
2328 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04002329 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002331 atomic_set(&md->pending[0], 0);
2332 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002333 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002334 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002335 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002336 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002337 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 md->disk->major = _major;
2340 md->disk->first_minor = minor;
2341 md->disk->fops = &dm_blk_dops;
2342 md->disk->queue = md->queue;
2343 md->disk->private_data = md;
2344 sprintf(md->disk->disk_name, "dm-%d", minor);
2345 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002346 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Tejun Heo670368a2013-07-30 08:40:21 -04002348 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002349 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002350 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002351
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002352 md->bdev = bdget_disk(md->disk, 0);
2353 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002354 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002355
Tejun Heo6a8736d2010-09-08 18:07:00 +02002356 bio_init(&md->flush_bio);
2357 md->flush_bio.bi_bdev = md->bdev;
2358 md->flush_bio.bi_rw = WRITE_FLUSH;
2359
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002360 dm_stats_init(&md->stats);
2361
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002362 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002363 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002364 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002365 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002366
2367 BUG_ON(old_md != MINOR_ALLOCED);
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return md;
2370
Mike Snitzer0f209722015-04-28 11:50:29 -04002371bad:
2372 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002373bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002375bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002376 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002377bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 kfree(md);
2379 return NULL;
2380}
2381
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002382static void unlock_fs(struct mapped_device *md);
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384static void free_dev(struct mapped_device *md)
2385{
Tejun Heof331c022008-09-03 09:01:48 +02002386 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002387
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002388 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002389
Mike Snitzer0f209722015-04-28 11:50:29 -04002390 cleanup_mapped_device(md);
Mike Snitzer1c357a12016-02-06 17:01:17 -05002391 if (md->tag_set) {
2392 blk_mq_free_tag_set(md->tag_set);
2393 kfree(md->tag_set);
2394 }
Mike Snitzer0f209722015-04-28 11:50:29 -04002395
2396 free_table_devices(&md->table_devices);
2397 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002398 free_minor(minor);
2399
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002400 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 kfree(md);
2402}
2403
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002404static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2405{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002406 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002407
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002408 if (md->bs) {
2409 /* The md already has necessary mempools. */
2410 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002411 /*
2412 * Reload bioset because front_pad may have changed
2413 * because a different table was loaded.
2414 */
2415 bioset_free(md->bs);
2416 md->bs = p->bs;
2417 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002418 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002419 /*
2420 * There's no need to reload with request-based dm
2421 * because the size of front_pad doesn't change.
2422 * Note for future: If you are to reload bioset,
2423 * prep-ed requests in the queue may refer
2424 * to bio from the old bioset, so you must walk
2425 * through the queue to unprep.
2426 */
2427 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002428 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002429
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002430 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2431
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002432 md->io_pool = p->io_pool;
2433 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002434 md->rq_pool = p->rq_pool;
2435 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002436 md->bs = p->bs;
2437 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002438
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002439out:
Mike Snitzer02233342015-03-10 23:49:26 -04002440 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002441 dm_table_free_md_mempools(t);
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444/*
2445 * Bind a table to the device.
2446 */
2447static void event_callback(void *context)
2448{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002449 unsigned long flags;
2450 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct mapped_device *md = (struct mapped_device *) context;
2452
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002453 spin_lock_irqsave(&md->uevent_lock, flags);
2454 list_splice_init(&md->uevent_list, &uevents);
2455 spin_unlock_irqrestore(&md->uevent_lock, flags);
2456
Tejun Heoed9e1982008-08-25 19:56:05 +09002457 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 atomic_inc(&md->event_nr);
2460 wake_up(&md->eventq);
2461}
2462
Mike Snitzerc2176492011-01-13 19:53:46 +00002463/*
2464 * Protected by md->suspend_lock obtained by dm_swap_table().
2465 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002466static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002468 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002470 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002473/*
2474 * Returns old map, which caller must destroy.
2475 */
2476static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2477 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002479 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002480 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 sector_t size;
2482
2483 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002484
2485 /*
2486 * Wipe any geometry if the size of the table changed.
2487 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002488 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002489 memset(&md->geometry, 0, sizeof(md->geometry));
2490
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002491 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002493 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002494
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002495 /*
2496 * The queue hasn't been stopped yet, if the old table type wasn't
2497 * for request-based during suspension. So stop it to prevent
2498 * I/O mapping before resume.
2499 * This must be done before setting the queue restrictions,
2500 * because request-based dm may be run just after the setting.
2501 */
Mike Snitzer16f12262016-01-31 17:22:27 -05002502 if (dm_table_request_based(t)) {
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002503 stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05002504 /*
2505 * Leverage the fact that request-based DM targets are
2506 * immutable singletons and establish md->immutable_target
2507 * - used to optimize both dm_request_fn and dm_mq_queue_rq
2508 */
2509 md->immutable_target = dm_table_get_immutable_target(t);
2510 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002511
2512 __bind_mempools(md, t);
2513
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002514 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002515 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002516 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2517
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002518 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002519 if (old_map)
2520 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002521
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002522 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Alasdair G Kergona7940152009-12-10 23:52:23 +00002525/*
2526 * Returns unbound table for the caller to free.
2527 */
2528static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002530 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002533 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302536 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002537 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002538
2539 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542/*
2543 * Constructor for a new device.
2544 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002545int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 struct mapped_device *md;
2548
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002549 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (!md)
2551 return -ENXIO;
2552
Milan Broz784aae72009-01-06 03:05:12 +00002553 dm_sysfs_init(md);
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 *result = md;
2556 return 0;
2557}
2558
Mike Snitzera5664da2010-08-12 04:14:01 +01002559/*
2560 * Functions to manage md->type.
2561 * All are required to hold md->type_lock.
2562 */
2563void dm_lock_md_type(struct mapped_device *md)
2564{
2565 mutex_lock(&md->type_lock);
2566}
2567
2568void dm_unlock_md_type(struct mapped_device *md)
2569{
2570 mutex_unlock(&md->type_lock);
2571}
2572
2573void dm_set_md_type(struct mapped_device *md, unsigned type)
2574{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002575 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002576 md->type = type;
2577}
2578
2579unsigned dm_get_md_type(struct mapped_device *md)
2580{
2581 return md->type;
2582}
2583
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002584struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2585{
2586 return md->immutable_target_type;
2587}
2588
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002589/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002590 * The queue_limits are only valid as long as you have a reference
2591 * count on 'md'.
2592 */
2593struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2594{
2595 BUG_ON(!atomic_read(&md->holders));
2596 return &md->queue->limits;
2597}
2598EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2599
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002600static void init_rq_based_worker_thread(struct mapped_device *md)
2601{
2602 /* Initialize the request-based DM worker thread */
2603 init_kthread_worker(&md->kworker);
2604 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2605 "kdmwork-%s", dm_device_name(md));
2606}
2607
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002608/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002609 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2610 */
2611static int dm_init_request_based_queue(struct mapped_device *md)
2612{
2613 struct request_queue *q = NULL;
2614
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002615 /* Fully initialize the queue */
2616 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2617 if (!q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002618 return -EINVAL;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002619
Mike Snitzer0ce65792015-02-26 00:50:28 -05002620 /* disable dm_request_fn's merge heuristic by default */
2621 md->seq_rq_merge_deadline_usecs = 0;
2622
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002623 md->queue = q;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002624 dm_init_old_md_queue(md);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002625 blk_queue_softirq_done(md->queue, dm_softirq_done);
2626 blk_queue_prep_rq(md->queue, dm_prep_fn);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002627
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002628 init_rq_based_worker_thread(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002629
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002630 elv_register_queue(md->queue);
2631
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002632 return 0;
2633}
2634
2635static int dm_mq_init_request(void *data, struct request *rq,
2636 unsigned int hctx_idx, unsigned int request_idx,
2637 unsigned int numa_node)
2638{
2639 struct mapped_device *md = data;
2640 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2641
2642 /*
2643 * Must initialize md member of tio, otherwise it won't
2644 * be available in dm_mq_queue_rq.
2645 */
2646 tio->md = md;
2647
2648 return 0;
2649}
2650
2651static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2652 const struct blk_mq_queue_data *bd)
2653{
2654 struct request *rq = bd->rq;
2655 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2656 struct mapped_device *md = tio->md;
Mike Snitzer16f12262016-01-31 17:22:27 -05002657 struct dm_target *ti = md->immutable_target;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002658
Mike Snitzer16f12262016-01-31 17:22:27 -05002659 if (unlikely(!ti)) {
2660 int srcu_idx;
2661 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002662
Mike Snitzer16f12262016-01-31 17:22:27 -05002663 ti = dm_table_find_target(map, 0);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002664 dm_put_live_table(md, srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002665 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002666
2667 if (ti->type->busy && ti->type->busy(ti))
2668 return BLK_MQ_RQ_QUEUE_BUSY;
2669
2670 dm_start_request(md, rq);
2671
2672 /* Init tio using md established in .init_request */
2673 init_tio(tio, rq, md);
2674
Mike Snitzer02233342015-03-10 23:49:26 -04002675 /*
2676 * Establish tio->ti before queuing work (map_tio_request)
2677 * or making direct call to map_request().
2678 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002679 tio->ti = ti;
Mike Snitzer02233342015-03-10 23:49:26 -04002680
Mike Snitzer16f12262016-01-31 17:22:27 -05002681 /*
2682 * Both the table and md type cannot change after initial table load
2683 */
2684 if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) {
Mike Snitzer02233342015-03-10 23:49:26 -04002685 /* clone request is allocated at the end of the pdu */
2686 tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io);
Mike Snitzer45714fb2015-05-27 15:25:27 -04002687 (void) clone_rq(rq, md, tio, GFP_ATOMIC);
Mike Snitzer02233342015-03-10 23:49:26 -04002688 queue_kthread_work(&md->kworker, &tio->work);
2689 } else {
2690 /* Direct call is fine since .queue_rq allows allocations */
Mike Snitzer45714fb2015-05-27 15:25:27 -04002691 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
2692 /* Undo dm_start_request() before requeuing */
Mikulas Patockae262f342015-06-09 17:22:49 -04002693 rq_end_stats(md, rq);
Mike Snitzer45714fb2015-05-27 15:25:27 -04002694 rq_completed(md, rq_data_dir(rq), false);
2695 return BLK_MQ_RQ_QUEUE_BUSY;
2696 }
Mike Snitzer02233342015-03-10 23:49:26 -04002697 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002698
2699 return BLK_MQ_RQ_QUEUE_OK;
2700}
2701
2702static struct blk_mq_ops dm_mq_ops = {
2703 .queue_rq = dm_mq_queue_rq,
2704 .map_queue = blk_mq_map_queue,
2705 .complete = dm_softirq_done,
2706 .init_request = dm_mq_init_request,
2707};
2708
2709static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
2710{
Mike Snitzer02233342015-03-10 23:49:26 -04002711 unsigned md_type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002712 struct request_queue *q;
2713 int err;
2714
Mike Snitzer1c357a12016-02-06 17:01:17 -05002715 md->tag_set = kzalloc(sizeof(struct blk_mq_tag_set), GFP_KERNEL);
2716 if (!md->tag_set)
2717 return -ENOMEM;
2718
2719 md->tag_set->ops = &dm_mq_ops;
2720 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
2721 md->tag_set->numa_node = NUMA_NO_NODE;
2722 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2723 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
2724 md->tag_set->driver_data = md;
2725
2726 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
Mike Snitzer02233342015-03-10 23:49:26 -04002727 if (md_type == DM_TYPE_REQUEST_BASED) {
Mike Snitzer1c357a12016-02-06 17:01:17 -05002728 /* put the memory for non-blk-mq clone at the end of the pdu */
2729 md->tag_set->cmd_size += sizeof(struct request);
2730 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002731
Mike Snitzer1c357a12016-02-06 17:01:17 -05002732 err = blk_mq_alloc_tag_set(md->tag_set);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002733 if (err)
Mike Snitzer1c357a12016-02-06 17:01:17 -05002734 goto out_kfree_tag_set;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002735
Mike Snitzer1c357a12016-02-06 17:01:17 -05002736 q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002737 if (IS_ERR(q)) {
2738 err = PTR_ERR(q);
2739 goto out_tag_set;
2740 }
2741 md->queue = q;
2742 dm_init_md_queue(md);
2743
2744 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
2745 blk_mq_register_disk(md->disk);
2746
Mike Snitzer02233342015-03-10 23:49:26 -04002747 if (md_type == DM_TYPE_REQUEST_BASED)
2748 init_rq_based_worker_thread(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002749
2750 return 0;
2751
2752out_tag_set:
Mike Snitzer1c357a12016-02-06 17:01:17 -05002753 blk_mq_free_tag_set(md->tag_set);
2754out_kfree_tag_set:
2755 kfree(md->tag_set);
2756
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002757 return err;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002758}
2759
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002760static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2761{
2762 if (type == DM_TYPE_BIO_BASED)
2763 return type;
2764
2765 return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2766}
2767
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002768/*
2769 * Setup the DM device's queue based on md's type
2770 */
2771int dm_setup_md_queue(struct mapped_device *md)
2772{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002773 int r;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002774 unsigned md_type = filter_md_type(dm_get_md_type(md), md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002775
2776 switch (md_type) {
2777 case DM_TYPE_REQUEST_BASED:
2778 r = dm_init_request_based_queue(md);
2779 if (r) {
Mike Snitzerff36ab32015-02-23 17:56:37 -05002780 DMWARN("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002781 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002782 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002783 break;
2784 case DM_TYPE_MQ_REQUEST_BASED:
2785 r = dm_init_request_based_blk_mq_queue(md);
2786 if (r) {
2787 DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
2788 return r;
2789 }
2790 break;
2791 case DM_TYPE_BIO_BASED:
2792 dm_init_old_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002793 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04002794 /*
2795 * DM handles splitting bios as needed. Free the bio_split bioset
2796 * since it won't be used (saves 1 process per bio-based DM device).
2797 */
2798 bioset_free(md->queue->bio_split);
2799 md->queue->bio_split = NULL;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002800 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002801 }
2802
2803 return 0;
2804}
2805
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002806struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
2808 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 unsigned minor = MINOR(dev);
2810
2811 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2812 return NULL;
2813
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002814 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002817 if (md) {
2818 if ((md == MINOR_ALLOCED ||
2819 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2820 dm_deleting_md(md) ||
2821 test_bit(DMF_FREEING, &md->flags))) {
2822 md = NULL;
2823 goto out;
2824 }
2825 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002828out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002829 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
David Teigland637842c2006-01-06 00:20:00 -08002831 return md;
2832}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002833EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002834
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002835void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002836{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002837 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840void dm_set_mdptr(struct mapped_device *md, void *ptr)
2841{
2842 md->interface_ptr = ptr;
2843}
2844
2845void dm_get(struct mapped_device *md)
2846{
2847 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002848 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002851int dm_hold(struct mapped_device *md)
2852{
2853 spin_lock(&_minor_lock);
2854 if (test_bit(DMF_FREEING, &md->flags)) {
2855 spin_unlock(&_minor_lock);
2856 return -EBUSY;
2857 }
2858 dm_get(md);
2859 spin_unlock(&_minor_lock);
2860 return 0;
2861}
2862EXPORT_SYMBOL_GPL(dm_hold);
2863
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002864const char *dm_device_name(struct mapped_device *md)
2865{
2866 return md->name;
2867}
2868EXPORT_SYMBOL_GPL(dm_device_name);
2869
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002870static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002872 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002873 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002875 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002876
Mike Snitzer63a4f062015-03-23 17:01:43 -04002877 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002878 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2879 set_bit(DMF_FREEING, &md->flags);
2880 spin_unlock(&_minor_lock);
2881
Mike Snitzer02233342015-03-10 23:49:26 -04002882 if (dm_request_based(md) && md->kworker_task)
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002883 flush_kthread_worker(&md->kworker);
2884
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002885 /*
2886 * Take suspend_lock so that presuspend and postsuspend methods
2887 * do not race with internal suspend.
2888 */
2889 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002890 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002891 if (!dm_suspended_md(md)) {
2892 dm_table_presuspend_targets(map);
2893 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002895 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2896 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002897 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002898
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002899 /*
2900 * Rare, but there may be I/O requests still going to complete,
2901 * for example. Wait for all references to disappear.
2902 * No one should increment the reference count of the mapped_device,
2903 * after the mapped_device state becomes DMF_FREEING.
2904 */
2905 if (wait)
2906 while (atomic_read(&md->holders))
2907 msleep(1);
2908 else if (atomic_read(&md->holders))
2909 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2910 dm_device_name(md), atomic_read(&md->holders));
2911
2912 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002913 dm_table_destroy(__unbind(md));
2914 free_dev(md);
2915}
2916
2917void dm_destroy(struct mapped_device *md)
2918{
2919 __dm_destroy(md, true);
2920}
2921
2922void dm_destroy_immediate(struct mapped_device *md)
2923{
2924 __dm_destroy(md, false);
2925}
2926
2927void dm_put(struct mapped_device *md)
2928{
2929 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
Edward Goggin79eb8852007-05-09 02:32:56 -07002931EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Mikulas Patocka401600d2009-04-02 19:55:38 +01002933static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002934{
2935 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002936 DECLARE_WAITQUEUE(wait, current);
2937
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002938 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002939
2940 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002941 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002942
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002943 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002944 break;
2945
Mikulas Patocka401600d2009-04-02 19:55:38 +01002946 if (interruptible == TASK_INTERRUPTIBLE &&
2947 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002948 r = -EINTR;
2949 break;
2950 }
2951
2952 io_schedule();
2953 }
2954 set_current_state(TASK_RUNNING);
2955
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002956 remove_wait_queue(&md->wait, &wait);
2957
Milan Broz46125c12008-02-08 02:10:30 +00002958 return r;
2959}
2960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961/*
2962 * Process the deferred bios
2963 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002964static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Mikulas Patockaef208582009-04-02 19:55:38 +01002966 struct mapped_device *md = container_of(work, struct mapped_device,
2967 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002968 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002969 int srcu_idx;
2970 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002972 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002973
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002974 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002975 spin_lock_irq(&md->deferred_lock);
2976 c = bio_list_pop(&md->deferred);
2977 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002978
Tejun Heo6a8736d2010-09-08 18:07:00 +02002979 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002980 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002981
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002982 if (dm_request_based(md))
2983 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002984 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002985 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002986 }
Milan Broz73d410c2008-02-08 02:10:25 +00002987
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002988 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002991static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002992{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002993 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002994 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002995 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002996}
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002999 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003001struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Mike Christie87eb5b22013-03-01 22:45:48 +00003003 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003004 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003005 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Daniel Walkere61290a2008-02-08 02:10:08 +00003007 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003010 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003011 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Mike Snitzer3ae70652012-09-26 23:45:45 +01003013 /*
3014 * If the new table has no data devices, retain the existing limits.
3015 * This helps multipath with queue_if_no_path if all paths disappear,
3016 * then new I/O is queued based on these limits, and then some paths
3017 * reappear.
3018 */
3019 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003020 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003021 if (live_map)
3022 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003023 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003024 }
3025
Mike Christie87eb5b22013-03-01 22:45:48 +00003026 if (!live_map) {
3027 r = dm_calculate_queue_limits(table, &limits);
3028 if (r) {
3029 map = ERR_PTR(r);
3030 goto out;
3031 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003032 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003033
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003034 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003036out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003037 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003038 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
3041/*
3042 * Functions to lock and unlock any filesystem running on the
3043 * device.
3044 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003045static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003047 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003050
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003051 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003052 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003053 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003054 md->frozen_sb = NULL;
3055 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003056 }
3057
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003058 set_bit(DMF_FROZEN, &md->flags);
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return 0;
3061}
3062
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003063static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003065 if (!test_bit(DMF_FROZEN, &md->flags))
3066 return;
3067
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003068 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003070 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
3073/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04003074 * If __dm_suspend returns 0, the device is completely quiescent
3075 * now. There is no request-processing activity. All new requests
3076 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003077 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04003078 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003079 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003080static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3081 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003083 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3084 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3085 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003087 /*
3088 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
3089 * This flag is cleared before dm_suspend returns.
3090 */
3091 if (noflush)
3092 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
3093
Mike Snitzerd67ee212014-10-28 20:13:31 -04003094 /*
3095 * This gets reverted if there's an error later and the targets
3096 * provide the .presuspend_undo hook.
3097 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003098 dm_table_presuspend_targets(map);
3099
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003100 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003101 * Flush I/O to the device.
3102 * Any I/O submitted after lock_fs() may not be flushed.
3103 * noflush takes precedence over do_lockfs.
3104 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003105 */
3106 if (!noflush && do_lockfs) {
3107 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003108 if (r) {
3109 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003110 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04003111 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
3114 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003115 * Here we must make sure that no processes are submitting requests
3116 * to target drivers i.e. no one may be executing
3117 * __split_and_process_bio. This is called from dm_request and
3118 * dm_wq_work.
3119 *
3120 * To get all processes out of __split_and_process_bio in dm_request,
3121 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02003122 * __split_and_process_bio from dm_request and quiesce the thread
3123 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
3124 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01003126 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003127 if (map)
3128 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003130 /*
Tejun Heo29e40132010-09-08 18:07:00 +02003131 * Stop md->queue before flushing md->wq in case request-based
3132 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003133 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003134 if (dm_request_based(md)) {
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003135 stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04003136 if (md->kworker_task)
3137 flush_kthread_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003138 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003139
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003140 flush_workqueue(md->wq);
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003143 * At this point no more requests are entering target request routines.
3144 * We call dm_wait_for_completion to wait for all existing requests
3145 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003147 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Milan Broz6d6f10d2008-02-08 02:10:22 +00003149 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01003150 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003151 if (map)
3152 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00003155 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003156 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00003157
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003158 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003159 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003160
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003161 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003162 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003163 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003164 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003165
Mike Snitzerffcc3932014-10-28 18:34:52 -04003166 return r;
3167}
3168
3169/*
3170 * We need to be able to change a mapping table under a mounted
3171 * filesystem. For example we might want to move some data in
3172 * the background. Before the table can be swapped with
3173 * dm_bind_table, dm_suspend must be called to flush any in
3174 * flight bios and ensure that any further io gets deferred.
3175 */
3176/*
3177 * Suspend mechanism in request-based dm.
3178 *
3179 * 1. Flush all I/Os by lock_fs() if needed.
3180 * 2. Stop dispatching any I/O by stopping the request_queue.
3181 * 3. Wait for all in-flight I/Os to be completed or requeued.
3182 *
3183 * To abort suspend, start the request_queue.
3184 */
3185int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3186{
3187 struct dm_table *map = NULL;
3188 int r = 0;
3189
3190retry:
3191 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3192
3193 if (dm_suspended_md(md)) {
3194 r = -EINVAL;
3195 goto out_unlock;
3196 }
3197
3198 if (dm_suspended_internally_md(md)) {
3199 /* already internally suspended, wait for internal resume */
3200 mutex_unlock(&md->suspend_lock);
3201 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3202 if (r)
3203 return r;
3204 goto retry;
3205 }
3206
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003207 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003208
3209 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3210 if (r)
3211 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 set_bit(DMF_SUSPENDED, &md->flags);
3214
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00003215 dm_table_postsuspend_targets(map);
3216
Alasdair G Kergond2874832006-11-08 17:44:43 -08003217out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00003218 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003219 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Mike Snitzerffcc3932014-10-28 18:34:52 -04003222static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003224 if (map) {
3225 int r = dm_table_resume_targets(map);
3226 if (r)
3227 return r;
3228 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003229
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003230 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003231
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003232 /*
3233 * Flushing deferred I/Os must be done after targets are resumed
3234 * so that mapping of targets can work correctly.
3235 * Request-based dm is queueing the deferred I/Os in its request_queue.
3236 */
3237 if (dm_request_based(md))
3238 start_queue(md->queue);
3239
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003240 unlock_fs(md);
3241
Mike Snitzerffcc3932014-10-28 18:34:52 -04003242 return 0;
3243}
3244
3245int dm_resume(struct mapped_device *md)
3246{
3247 int r = -EINVAL;
3248 struct dm_table *map = NULL;
3249
3250retry:
3251 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3252
3253 if (!dm_suspended_md(md))
3254 goto out;
3255
3256 if (dm_suspended_internally_md(md)) {
3257 /* already internally suspended, wait for internal resume */
3258 mutex_unlock(&md->suspend_lock);
3259 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3260 if (r)
3261 return r;
3262 goto retry;
3263 }
3264
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003265 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003266 if (!map || !dm_table_get_size(map))
3267 goto out;
3268
3269 r = __dm_resume(md, map);
3270 if (r)
3271 goto out;
3272
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003273 clear_bit(DMF_SUSPENDED, &md->flags);
3274
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003275 r = 0;
3276out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003277 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003278
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003279 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003282/*
3283 * Internal suspend/resume works like userspace-driven suspend. It waits
3284 * until all bios finish and prevents issuing new bios to the target drivers.
3285 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003286 */
3287
Mike Snitzerffcc3932014-10-28 18:34:52 -04003288static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3289{
3290 struct dm_table *map = NULL;
3291
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003292 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003293 return; /* nested internal suspend */
3294
3295 if (dm_suspended_md(md)) {
3296 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3297 return; /* nest suspend */
3298 }
3299
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003300 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003301
3302 /*
3303 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3304 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3305 * would require changing .presuspend to return an error -- avoid this
3306 * until there is a need for more elaborate variants of internal suspend.
3307 */
3308 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3309
3310 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3311
3312 dm_table_postsuspend_targets(map);
3313}
3314
3315static void __dm_internal_resume(struct mapped_device *md)
3316{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003317 BUG_ON(!md->internal_suspend_count);
3318
3319 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003320 return; /* resume from nested internal suspend */
3321
3322 if (dm_suspended_md(md))
3323 goto done; /* resume from nested suspend */
3324
3325 /*
3326 * NOTE: existing callers don't need to call dm_table_resume_targets
3327 * (which may fail -- so best to avoid it for now by passing NULL map)
3328 */
3329 (void) __dm_resume(md, NULL);
3330
3331done:
3332 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3333 smp_mb__after_atomic();
3334 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3335}
3336
3337void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003338{
3339 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003340 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3341 mutex_unlock(&md->suspend_lock);
3342}
3343EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3344
3345void dm_internal_resume(struct mapped_device *md)
3346{
3347 mutex_lock(&md->suspend_lock);
3348 __dm_internal_resume(md);
3349 mutex_unlock(&md->suspend_lock);
3350}
3351EXPORT_SYMBOL_GPL(dm_internal_resume);
3352
3353/*
3354 * Fast variants of internal suspend/resume hold md->suspend_lock,
3355 * which prevents interaction with userspace-driven suspend.
3356 */
3357
3358void dm_internal_suspend_fast(struct mapped_device *md)
3359{
3360 mutex_lock(&md->suspend_lock);
3361 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003362 return;
3363
3364 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3365 synchronize_srcu(&md->io_barrier);
3366 flush_workqueue(md->wq);
3367 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3368}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003369EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003370
Mike Snitzerffcc3932014-10-28 18:34:52 -04003371void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003372{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003373 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003374 goto done;
3375
3376 dm_queue_flush(md);
3377
3378done:
3379 mutex_unlock(&md->suspend_lock);
3380}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003381EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003382
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383/*-----------------------------------------------------------------
3384 * Event notification.
3385 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003386int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003387 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003388{
Milan Broz60935eb2009-06-22 10:12:30 +01003389 char udev_cookie[DM_COOKIE_LENGTH];
3390 char *envp[] = { udev_cookie, NULL };
3391
3392 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003393 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003394 else {
3395 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3396 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003397 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3398 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003399 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003400}
3401
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003402uint32_t dm_next_uevent_seq(struct mapped_device *md)
3403{
3404 return atomic_add_return(1, &md->uevent_seq);
3405}
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407uint32_t dm_get_event_nr(struct mapped_device *md)
3408{
3409 return atomic_read(&md->event_nr);
3410}
3411
3412int dm_wait_event(struct mapped_device *md, int event_nr)
3413{
3414 return wait_event_interruptible(md->eventq,
3415 (event_nr != atomic_read(&md->event_nr)));
3416}
3417
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003418void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3419{
3420 unsigned long flags;
3421
3422 spin_lock_irqsave(&md->uevent_lock, flags);
3423 list_add(elist, &md->uevent_list);
3424 spin_unlock_irqrestore(&md->uevent_lock, flags);
3425}
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427/*
3428 * The gendisk is only valid as long as you have a reference
3429 * count on 'md'.
3430 */
3431struct gendisk *dm_disk(struct mapped_device *md)
3432{
3433 return md->disk;
3434}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00003435EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Milan Broz784aae72009-01-06 03:05:12 +00003437struct kobject *dm_kobject(struct mapped_device *md)
3438{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003439 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003440}
3441
Milan Broz784aae72009-01-06 03:05:12 +00003442struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3443{
3444 struct mapped_device *md;
3445
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003446 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003447
Milan Broz4d89b7b2009-06-22 10:12:11 +01003448 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003449 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003450 return NULL;
3451
Milan Broz784aae72009-01-06 03:05:12 +00003452 dm_get(md);
3453 return md;
3454}
3455
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003456int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
3458 return test_bit(DMF_SUSPENDED, &md->flags);
3459}
3460
Mike Snitzerffcc3932014-10-28 18:34:52 -04003461int dm_suspended_internally_md(struct mapped_device *md)
3462{
3463 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3464}
3465
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003466int dm_test_deferred_remove_flag(struct mapped_device *md)
3467{
3468 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3469}
3470
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003471int dm_suspended(struct dm_target *ti)
3472{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003473 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003474}
3475EXPORT_SYMBOL_GPL(dm_suspended);
3476
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003477int dm_noflush_suspending(struct dm_target *ti)
3478{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003479 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003480}
3481EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3482
Mike Snitzer78d8e582015-06-26 10:01:13 -04003483struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
3484 unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003485{
Mike Snitzer78d8e582015-06-26 10:01:13 -04003486 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3487 struct kmem_cache *cachep = NULL;
3488 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003489 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003490
3491 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003492 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003493
Mike Snitzer78d8e582015-06-26 10:01:13 -04003494 type = filter_md_type(type, md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04003495
Mike Snitzer78d8e582015-06-26 10:01:13 -04003496 switch (type) {
3497 case DM_TYPE_BIO_BASED:
3498 cachep = _io_cache;
3499 pool_size = dm_get_reserved_bio_based_ios();
3500 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
3501 break;
3502 case DM_TYPE_REQUEST_BASED:
3503 cachep = _rq_tio_cache;
3504 pool_size = dm_get_reserved_rq_based_ios();
3505 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3506 if (!pools->rq_pool)
3507 goto out;
3508 /* fall through to setup remaining rq-based pools */
3509 case DM_TYPE_MQ_REQUEST_BASED:
3510 if (!pool_size)
3511 pool_size = dm_get_reserved_rq_based_ios();
3512 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3513 /* per_bio_data_size is not used. See __bind_mempools(). */
3514 WARN_ON(per_bio_data_size != 0);
3515 break;
3516 default:
3517 BUG();
3518 }
3519
3520 if (cachep) {
3521 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3522 if (!pools->io_pool)
3523 goto out;
3524 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003525
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003526 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003527 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003528 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003529
Martin K. Petersena91a2782011-03-17 11:11:05 +01003530 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003531 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003532
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003533 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003534
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003535out:
3536 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003537
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003538 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003539}
3540
3541void dm_free_md_mempools(struct dm_md_mempools *pools)
3542{
3543 if (!pools)
3544 return;
3545
Julia Lawall6f659852015-09-13 14:15:05 +02003546 mempool_destroy(pools->io_pool);
3547 mempool_destroy(pools->rq_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003548
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003549 if (pools->bs)
3550 bioset_free(pools->bs);
3551
3552 kfree(pools);
3553}
3554
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003555static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003556 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003557{
3558 struct mapped_device *md = bdev->bd_disk->private_data;
3559 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003560 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003561 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003562
Mike Snitzer956a4022016-02-18 16:13:51 -05003563 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003564 if (r < 0)
3565 return r;
3566
3567 ops = bdev->bd_disk->fops->pr_ops;
3568 if (ops && ops->pr_register)
3569 r = ops->pr_register(bdev, old_key, new_key, flags);
3570 else
3571 r = -EOPNOTSUPP;
3572
Mike Snitzer956a4022016-02-18 16:13:51 -05003573 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003574 return r;
3575}
3576
3577static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05003578 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003579{
3580 struct mapped_device *md = bdev->bd_disk->private_data;
3581 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003582 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003583 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003584
Mike Snitzer956a4022016-02-18 16:13:51 -05003585 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003586 if (r < 0)
3587 return r;
3588
3589 ops = bdev->bd_disk->fops->pr_ops;
3590 if (ops && ops->pr_reserve)
3591 r = ops->pr_reserve(bdev, key, type, flags);
3592 else
3593 r = -EOPNOTSUPP;
3594
Mike Snitzer956a4022016-02-18 16:13:51 -05003595 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003596 return r;
3597}
3598
3599static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3600{
3601 struct mapped_device *md = bdev->bd_disk->private_data;
3602 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003603 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003604 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003605
Mike Snitzer956a4022016-02-18 16:13:51 -05003606 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003607 if (r < 0)
3608 return r;
3609
3610 ops = bdev->bd_disk->fops->pr_ops;
3611 if (ops && ops->pr_release)
3612 r = ops->pr_release(bdev, key, type);
3613 else
3614 r = -EOPNOTSUPP;
3615
Mike Snitzer956a4022016-02-18 16:13:51 -05003616 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003617 return r;
3618}
3619
3620static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003621 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003622{
3623 struct mapped_device *md = bdev->bd_disk->private_data;
3624 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003625 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003626 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003627
Mike Snitzer956a4022016-02-18 16:13:51 -05003628 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003629 if (r < 0)
3630 return r;
3631
3632 ops = bdev->bd_disk->fops->pr_ops;
3633 if (ops && ops->pr_preempt)
3634 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3635 else
3636 r = -EOPNOTSUPP;
3637
Mike Snitzer956a4022016-02-18 16:13:51 -05003638 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003639 return r;
3640}
3641
3642static int dm_pr_clear(struct block_device *bdev, u64 key)
3643{
3644 struct mapped_device *md = bdev->bd_disk->private_data;
3645 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003646 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003647 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003648
Mike Snitzer956a4022016-02-18 16:13:51 -05003649 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003650 if (r < 0)
3651 return r;
3652
3653 ops = bdev->bd_disk->fops->pr_ops;
3654 if (ops && ops->pr_clear)
3655 r = ops->pr_clear(bdev, key);
3656 else
3657 r = -EOPNOTSUPP;
3658
Mike Snitzer956a4022016-02-18 16:13:51 -05003659 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003660 return r;
3661}
3662
3663static const struct pr_ops dm_pr_ops = {
3664 .pr_register = dm_pr_register,
3665 .pr_reserve = dm_pr_reserve,
3666 .pr_release = dm_pr_release,
3667 .pr_preempt = dm_pr_preempt,
3668 .pr_clear = dm_pr_clear,
3669};
3670
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003671static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 .open = dm_blk_open,
3673 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003674 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003675 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003676 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 .owner = THIS_MODULE
3678};
3679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680/*
3681 * module hooks
3682 */
3683module_init(dm_init);
3684module_exit(dm_exit);
3685
3686module_param(major, uint, 0);
3687MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003688
Mike Snitzere8603132013-09-12 18:06:12 -04003689module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3690MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3691
Mike Snitzerf4790822013-09-12 18:06:12 -04003692module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3693MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3694
Mike Snitzer17e149b2015-03-11 15:01:09 -04003695module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
3696MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
3697
Mike Snitzerfaad87d2016-01-28 16:52:56 -05003698module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
3699MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
3700
3701module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
3702MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704MODULE_DESCRIPTION(DM_NAME " driver");
3705MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3706MODULE_LICENSE("GPL");