blob: 3cae8547420a10f454bdcbfcd8f1a19ad2db2945 [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/*
124 * Work processed by per-device workqueue.
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100127 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000128 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700130 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100132 /*
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -0500133 * The current mapping (struct dm_table *).
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100134 * Use dm_get_live_table{_fast} or take suspend_lock for
135 * dereference.
136 */
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -0500137 void __rcu *map;
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100138
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500139 struct list_head table_devices;
140 struct mutex table_devices_lock;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned long flags;
143
Jens Axboe165125e2007-07-24 09:28:11 +0200144 struct request_queue *queue;
Mike Snitzer115485e2016-02-22 12:16:21 -0500145 int numa_node_id;
146
Mike Snitzera5664da2010-08-12 04:14:01 +0100147 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100148 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100149 struct mutex type_lock;
150
Mike Snitzer16f12262016-01-31 17:22:27 -0500151 struct dm_target *immutable_target;
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000152 struct target_type *immutable_target_type;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800155 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 void *interface_ptr;
158
159 /*
160 * A list of ios that arrived while we were suspended.
161 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200162 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100164 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800165 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100166 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200169 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000170 */
171 struct workqueue_struct *wq;
172
173 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * io objects are allocated from here.
175 */
176 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500177 mempool_t *rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Stefan Bader9faf4002006-10-03 01:15:41 -0700179 struct bio_set *bs;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * Event handling.
183 */
184 atomic_t event_nr;
185 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100186 atomic_t uevent_seq;
187 struct list_head uevent_list;
188 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /*
191 * freeze/thaw support require holding onto a super block
192 */
193 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100194 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800195
196 /* forced geometry settings */
197 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000198
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500199 /* kobject and completion */
200 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500201
Tejun Heod87f4c12010-09-03 11:56:19 +0200202 /* zero-length flush that will be cloned and submitted to targets */
203 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400204
Mikulas Patocka96b26c82015-01-08 18:52:26 -0500205 /* the number of internal suspends */
206 unsigned internal_suspend_count;
207
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400208 struct dm_stats stats;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600209
210 struct kthread_worker kworker;
211 struct task_struct *kworker_task;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500212
213 /* for request-based merge heuristic in dm_request_fn() */
Mike Snitzer0ce65792015-02-26 00:50:28 -0500214 unsigned seq_rq_merge_deadline_usecs;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500215 int last_rq_rw;
Mike Snitzer0ce65792015-02-26 00:50:28 -0500216 sector_t last_rq_pos;
217 ktime_t last_rq_start_time;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -0500218
219 /* for blk-mq request-based DM support */
Mike Snitzer1c357a12016-02-06 17:01:17 -0500220 struct blk_mq_tag_set *tag_set;
Mike Snitzer591ddcf2016-01-31 12:05:42 -0500221 bool use_blk_mq:1;
222 bool init_tio_pdu:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
Mike Snitzer17e149b2015-03-11 15:01:09 -0400225#ifdef CONFIG_DM_MQ_DEFAULT
226static bool use_blk_mq = true;
227#else
228static bool use_blk_mq = false;
229#endif
230
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500231#define DM_MQ_NR_HW_QUEUES 1
232#define DM_MQ_QUEUE_DEPTH 2048
Mike Snitzer115485e2016-02-22 12:16:21 -0500233#define DM_NUMA_NODE NUMA_NO_NODE
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500234
235static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
236static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
Mike Snitzer115485e2016-02-22 12:16:21 -0500237static int dm_numa_node = DM_NUMA_NODE;
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500238
Mike Snitzer17e149b2015-03-11 15:01:09 -0400239bool dm_use_blk_mq(struct mapped_device *md)
240{
241 return md->use_blk_mq;
242}
Mike Snitzer591ddcf2016-01-31 12:05:42 -0500243EXPORT_SYMBOL_GPL(dm_use_blk_mq);
Mike Snitzer17e149b2015-03-11 15:01:09 -0400244
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100245/*
246 * For mempools pre-allocation at the table loading time.
247 */
248struct dm_md_mempools {
249 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500250 mempool_t *rq_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100251 struct bio_set *bs;
252};
253
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500254struct table_device {
255 struct list_head list;
256 atomic_t count;
257 struct dm_dev dm_dev;
258};
259
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400260#define RESERVED_BIO_BASED_IOS 16
261#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400262#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800263static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000264static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500265static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700266
Mike Snitzerf4790822013-09-12 18:06:12 -0400267/*
Mike Snitzere8603132013-09-12 18:06:12 -0400268 * Bio-based DM's mempools' reserved IOs set by the user.
269 */
270static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
271
272/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400273 * Request-based DM's mempools' reserved IOs set by the user.
274 */
275static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
276
Mike Snitzer115485e2016-02-22 12:16:21 -0500277static int __dm_get_module_param_int(int *module_param, int min, int max)
278{
279 int param = ACCESS_ONCE(*module_param);
280 int modified_param = 0;
281 bool modified = true;
282
283 if (param < min)
284 modified_param = min;
285 else if (param > max)
286 modified_param = max;
287 else
288 modified = false;
289
290 if (modified) {
291 (void)cmpxchg(module_param, param, modified_param);
292 param = modified_param;
293 }
294
295 return param;
296}
297
Mike Snitzer09c2d532015-02-27 22:25:26 -0500298static unsigned __dm_get_module_param(unsigned *module_param,
Mike Snitzerf4790822013-09-12 18:06:12 -0400299 unsigned def, unsigned max)
300{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500301 unsigned param = ACCESS_ONCE(*module_param);
302 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400303
Mike Snitzer09c2d532015-02-27 22:25:26 -0500304 if (!param)
305 modified_param = def;
306 else if (param > max)
307 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400308
Mike Snitzer09c2d532015-02-27 22:25:26 -0500309 if (modified_param) {
310 (void)cmpxchg(module_param, param, modified_param);
311 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400312 }
313
Mike Snitzer09c2d532015-02-27 22:25:26 -0500314 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400315}
316
Mike Snitzere8603132013-09-12 18:06:12 -0400317unsigned dm_get_reserved_bio_based_ios(void)
318{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500319 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzere8603132013-09-12 18:06:12 -0400320 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
321}
322EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
323
Mike Snitzerf4790822013-09-12 18:06:12 -0400324unsigned dm_get_reserved_rq_based_ios(void)
325{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500326 return __dm_get_module_param(&reserved_rq_based_ios,
Mike Snitzerf4790822013-09-12 18:06:12 -0400327 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
328}
329EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
330
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500331static unsigned dm_get_blk_mq_nr_hw_queues(void)
332{
333 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
334}
335
336static unsigned dm_get_blk_mq_queue_depth(void)
337{
338 return __dm_get_module_param(&dm_mq_queue_depth,
339 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
340}
341
Mike Snitzer115485e2016-02-22 12:16:21 -0500342static unsigned dm_get_numa_node(void)
343{
344 return __dm_get_module_param_int(&dm_numa_node,
345 DM_NUMA_NODE, num_online_nodes() - 1);
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static int __init local_init(void)
349{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100350 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100353 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100355 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000357 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
358 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100359 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000360
Mike Snitzereca7ee62016-02-20 13:45:38 -0500361 _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500362 __alignof__(struct request), 0, NULL);
363 if (!_rq_cache)
364 goto out_free_rq_tio_cache;
365
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100366 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100367 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500368 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100369
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400370 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
371 if (!deferred_remove_workqueue) {
372 r = -ENOMEM;
373 goto out_uevent_exit;
374 }
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 _major = major;
377 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100378 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400379 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (!_major)
382 _major = r;
383
384 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100385
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400386out_free_workqueue:
387 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100388out_uevent_exit:
389 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500390out_free_rq_cache:
391 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000392out_free_rq_tio_cache:
393 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100394out_free_io_cache:
395 kmem_cache_destroy(_io_cache);
396
397 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static void local_exit(void)
401{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400402 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400403 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400404
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500405 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000406 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700408 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100409 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 _major = 0;
412
413 DMINFO("cleaned up");
414}
415
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000416static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 local_init,
418 dm_target_init,
419 dm_linear_init,
420 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000421 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100422 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400424 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
426
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000427static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 local_exit,
429 dm_target_exit,
430 dm_linear_exit,
431 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000432 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100433 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400435 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
437
438static int __init dm_init(void)
439{
440 const int count = ARRAY_SIZE(_inits);
441
442 int r, i;
443
444 for (i = 0; i < count; i++) {
445 r = _inits[i]();
446 if (r)
447 goto bad;
448 }
449
450 return 0;
451
452 bad:
453 while (i--)
454 _exits[i]();
455
456 return r;
457}
458
459static void __exit dm_exit(void)
460{
461 int i = ARRAY_SIZE(_exits);
462
463 while (i--)
464 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100465
466 /*
467 * Should be empty by this point.
468 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100469 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472/*
473 * Block device functions
474 */
Mike Anderson432a2122009-12-10 23:52:20 +0000475int dm_deleting_md(struct mapped_device *md)
476{
477 return test_bit(DMF_DELETING, &md->flags);
478}
479
Al Virofe5f9f22008-03-02 10:29:31 -0500480static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct mapped_device *md;
483
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700484 spin_lock(&_minor_lock);
485
Al Virofe5f9f22008-03-02 10:29:31 -0500486 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700487 if (!md)
488 goto out;
489
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700490 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000491 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700492 md = NULL;
493 goto out;
494 }
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700497 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700498out:
499 spin_unlock(&_minor_lock);
500
501 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Al Virodb2a1442013-05-05 21:52:57 -0400504static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400506 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200507
Milan Broz4a1aeb92011-01-13 19:59:48 +0000508 spin_lock(&_minor_lock);
509
Mike Snitzer63a4f062015-03-23 17:01:43 -0400510 md = disk->private_data;
511 if (WARN_ON(!md))
512 goto out;
513
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400514 if (atomic_dec_and_test(&md->open_count) &&
515 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400516 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400519out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000520 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700523int dm_open_count(struct mapped_device *md)
524{
525 return atomic_read(&md->open_count);
526}
527
528/*
529 * Guarantees nothing is using the device before it's deleted.
530 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400531int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700532{
533 int r = 0;
534
535 spin_lock(&_minor_lock);
536
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400537 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700538 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400539 if (mark_deferred)
540 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
541 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
542 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700543 else
544 set_bit(DMF_DELETING, &md->flags);
545
546 spin_unlock(&_minor_lock);
547
548 return r;
549}
550
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400551int dm_cancel_deferred_remove(struct mapped_device *md)
552{
553 int r = 0;
554
555 spin_lock(&_minor_lock);
556
557 if (test_bit(DMF_DELETING, &md->flags))
558 r = -EBUSY;
559 else
560 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
561
562 spin_unlock(&_minor_lock);
563
564 return r;
565}
566
567static void do_deferred_remove(struct work_struct *w)
568{
569 dm_deferred_remove();
570}
571
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400572sector_t dm_get_size(struct mapped_device *md)
573{
574 return get_capacity(md->disk);
575}
576
Mike Snitzer9974fa22014-02-28 15:33:43 +0100577struct request_queue *dm_get_md_queue(struct mapped_device *md)
578{
579 return md->queue;
580}
581
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400582struct dm_stats *dm_get_stats(struct mapped_device *md)
583{
584 return &md->stats;
585}
586
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800587static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
588{
589 struct mapped_device *md = bdev->bd_disk->private_data;
590
591 return dm_get_geometry(md, geo);
592}
593
Mike Snitzer956a4022016-02-18 16:13:51 -0500594static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
595 struct block_device **bdev,
596 fmode_t *mode)
Milan Brozaa129a22006-10-03 01:15:15 -0700597{
Mike Snitzer66482022016-02-18 15:44:39 -0500598 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100599 struct dm_table *map;
Mike Snitzer956a4022016-02-18 16:13:51 -0500600 int srcu_idx, r;
Milan Brozaa129a22006-10-03 01:15:15 -0700601
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100602retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200603 r = -ENOTTY;
Mike Snitzer956a4022016-02-18 16:13:51 -0500604 map = dm_get_live_table(md, &srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700605 if (!map || !dm_table_get_size(map))
606 goto out;
607
608 /* We only support devices that have a single target */
609 if (dm_table_get_num_targets(map) != 1)
610 goto out;
611
Mike Snitzer66482022016-02-18 15:44:39 -0500612 tgt = dm_table_get_target(map, 0);
613 if (!tgt->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500614 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700615
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000616 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700617 r = -EAGAIN;
618 goto out;
619 }
620
Mike Snitzer66482022016-02-18 15:44:39 -0500621 r = tgt->type->prepare_ioctl(tgt, bdev, mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200622 if (r < 0)
623 goto out;
624
Mike Snitzer956a4022016-02-18 16:13:51 -0500625 bdgrab(*bdev);
626 dm_put_live_table(md, srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200627 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700628
629out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500630 dm_put_live_table(md, srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000631 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100632 msleep(10);
633 goto retry;
634 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200635 return r;
636}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100637
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200638static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
639 unsigned int cmd, unsigned long arg)
640{
641 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer956a4022016-02-18 16:13:51 -0500642 int r;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200643
Mike Snitzer956a4022016-02-18 16:13:51 -0500644 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200645 if (r < 0)
646 return r;
647
648 if (r > 0) {
649 /*
650 * Target determined this ioctl is being issued against
651 * a logical partition of the parent bdev; so extra
652 * validation is needed.
653 */
654 r = scsi_verify_blk_ioctl(NULL, cmd);
655 if (r)
656 goto out;
657 }
658
Mike Snitzer66482022016-02-18 15:44:39 -0500659 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200660out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500661 bdput(bdev);
Milan Brozaa129a22006-10-03 01:15:15 -0700662 return r;
663}
664
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100665static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 return mempool_alloc(md->io_pool, GFP_NOIO);
668}
669
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100670static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 mempool_free(io, md->io_pool);
673}
674
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100675static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Mikulas Patockadba14162012-10-12 21:02:15 +0100677 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Mike Snitzereca7ee62016-02-20 13:45:38 -0500680static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md,
681 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100682{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000683 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100684}
685
Mike Snitzereca7ee62016-02-20 13:45:38 -0500686static void free_old_rq_tio(struct dm_rq_target_io *tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100687{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000688 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100689}
690
Mike Snitzereca7ee62016-02-20 13:45:38 -0500691static struct request *alloc_old_clone_request(struct mapped_device *md,
692 gfp_t gfp_mask)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500693{
694 return mempool_alloc(md->rq_pool, gfp_mask);
695}
696
Mike Snitzereca7ee62016-02-20 13:45:38 -0500697static void free_old_clone_request(struct mapped_device *md, struct request *rq)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500698{
699 mempool_free(rq, md->rq_pool);
700}
701
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000702static int md_in_flight(struct mapped_device *md)
703{
704 return atomic_read(&md->pending[READ]) +
705 atomic_read(&md->pending[WRITE]);
706}
707
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800708static void start_io_acct(struct dm_io *io)
709{
710 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400711 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900712 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400713 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800714
715 io->start_time = jiffies;
716
Tejun Heo074a7ac2008-08-25 19:56:14 +0900717 cpu = part_stat_lock();
718 part_round_stats(cpu, &dm_disk(md)->part0);
719 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100720 atomic_set(&dm_disk(md)->part0.in_flight[rw],
721 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400722
723 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700724 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400725 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800726}
727
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000728static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800729{
730 struct mapped_device *md = io->md;
731 struct bio *bio = io->bio;
732 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800733 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800734 int rw = bio_data_dir(bio);
735
Gu Zheng18c0b222014-11-24 11:05:26 +0800736 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800737
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400738 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700739 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400740 bio_sectors(bio), true, duration, &io->stats_aux);
741
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100742 /*
743 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200744 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100745 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100746 pending = atomic_dec_return(&md->pending[rw]);
747 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200748 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800749
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000750 /* nudge anyone waiting on suspend queue */
751 if (!pending)
752 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*
756 * Add the bio to the list of deferred io.
757 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100758static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200760 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200762 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200764 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200765 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768/*
769 * Everyone (including functions in this file), should use this
770 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100771 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100773struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100775 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100777 return srcu_dereference(md->map, &md->io_barrier);
778}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100780void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
781{
782 srcu_read_unlock(&md->io_barrier, srcu_idx);
783}
784
785void dm_sync_table(struct mapped_device *md)
786{
787 synchronize_srcu(&md->io_barrier);
788 synchronize_rcu_expedited();
789}
790
791/*
792 * A fast alternative to dm_get_live_table/dm_put_live_table.
793 * The caller must not block between these two functions.
794 */
795static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
796{
797 rcu_read_lock();
798 return rcu_dereference(md->map);
799}
800
801static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
802{
803 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800806/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500807 * Open a table device so we can use it as a map destination.
808 */
809static int open_table_device(struct table_device *td, dev_t dev,
810 struct mapped_device *md)
811{
812 static char *_claim_ptr = "I belong to device-mapper";
813 struct block_device *bdev;
814
815 int r;
816
817 BUG_ON(td->dm_dev.bdev);
818
819 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
820 if (IS_ERR(bdev))
821 return PTR_ERR(bdev);
822
823 r = bd_link_disk_holder(bdev, dm_disk(md));
824 if (r) {
825 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
826 return r;
827 }
828
829 td->dm_dev.bdev = bdev;
830 return 0;
831}
832
833/*
834 * Close a table device that we've been using.
835 */
836static void close_table_device(struct table_device *td, struct mapped_device *md)
837{
838 if (!td->dm_dev.bdev)
839 return;
840
841 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
842 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
843 td->dm_dev.bdev = NULL;
844}
845
846static struct table_device *find_table_device(struct list_head *l, dev_t dev,
847 fmode_t mode) {
848 struct table_device *td;
849
850 list_for_each_entry(td, l, list)
851 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
852 return td;
853
854 return NULL;
855}
856
857int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
858 struct dm_dev **result) {
859 int r;
860 struct table_device *td;
861
862 mutex_lock(&md->table_devices_lock);
863 td = find_table_device(&md->table_devices, dev, mode);
864 if (!td) {
Mike Snitzer115485e2016-02-22 12:16:21 -0500865 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500866 if (!td) {
867 mutex_unlock(&md->table_devices_lock);
868 return -ENOMEM;
869 }
870
871 td->dm_dev.mode = mode;
872 td->dm_dev.bdev = NULL;
873
874 if ((r = open_table_device(td, dev, md))) {
875 mutex_unlock(&md->table_devices_lock);
876 kfree(td);
877 return r;
878 }
879
880 format_dev_t(td->dm_dev.name, dev);
881
882 atomic_set(&td->count, 0);
883 list_add(&td->list, &md->table_devices);
884 }
885 atomic_inc(&td->count);
886 mutex_unlock(&md->table_devices_lock);
887
888 *result = &td->dm_dev;
889 return 0;
890}
891EXPORT_SYMBOL_GPL(dm_get_table_device);
892
893void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
894{
895 struct table_device *td = container_of(d, struct table_device, dm_dev);
896
897 mutex_lock(&md->table_devices_lock);
898 if (atomic_dec_and_test(&td->count)) {
899 close_table_device(td, md);
900 list_del(&td->list);
901 kfree(td);
902 }
903 mutex_unlock(&md->table_devices_lock);
904}
905EXPORT_SYMBOL(dm_put_table_device);
906
907static void free_table_devices(struct list_head *devices)
908{
909 struct list_head *tmp, *next;
910
911 list_for_each_safe(tmp, next, devices) {
912 struct table_device *td = list_entry(tmp, struct table_device, list);
913
914 DMWARN("dm_destroy: %s still exists with %d references",
915 td->dm_dev.name, atomic_read(&td->count));
916 kfree(td);
917 }
918}
919
920/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800921 * Get the geometry associated with a dm device
922 */
923int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
924{
925 *geo = md->geometry;
926
927 return 0;
928}
929
930/*
931 * Set the geometry of a device.
932 */
933int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
934{
935 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
936
937 if (geo->start > sz) {
938 DMWARN("Start sector is beyond the geometry limits.");
939 return -EINVAL;
940 }
941
942 md->geometry = *geo;
943
944 return 0;
945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*-----------------------------------------------------------------
948 * CRUD START:
949 * A more elegant soln is in the works that uses the queue
950 * merge fn, unfortunately there are a couple of changes to
951 * the block layer that I want to make for this. So in the
952 * interests of getting something for people to use I give
953 * you this clearly demarcated crap.
954 *---------------------------------------------------------------*/
955
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800956static int __noflush_suspending(struct mapped_device *md)
957{
958 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/*
962 * Decrements the number of outstanding ios that a bio has been
963 * cloned into, completing the original io if necc.
964 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800965static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800967 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000968 int io_error;
969 struct bio *bio;
970 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800971
972 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100973 if (unlikely(error)) {
974 spin_lock_irqsave(&io->endio_lock, flags);
975 if (!(io->error > 0 && __noflush_suspending(md)))
976 io->error = error;
977 spin_unlock_irqrestore(&io->endio_lock, flags);
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800981 if (io->error == DM_ENDIO_REQUEUE) {
982 /*
983 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800984 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100985 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200986 if (__noflush_suspending(md))
987 bio_list_add_head(&md->deferred, io->bio);
988 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800989 /* noflush suspend was interrupted. */
990 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100991 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800992 }
993
Milan Brozb35f8ca2009-03-16 17:44:36 +0000994 io_error = io->error;
995 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200996 end_io_acct(io);
997 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100998
Tejun Heo6a8736d2010-09-08 18:07:00 +0200999 if (io_error == DM_ENDIO_REQUEUE)
1000 return;
1001
Kent Overstreet4f024f32013-10-11 15:44:27 -07001002 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001003 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +02001004 * Preflush done for flush with data, reissue
1005 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001006 */
Tejun Heo6a8736d2010-09-08 18:07:00 +02001007 bio->bi_rw &= ~REQ_FLUSH;
1008 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001009 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +02001010 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07001011 trace_block_bio_complete(md->queue, bio, io_error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001012 bio->bi_error = io_error;
1013 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016}
1017
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001018static void disable_write_same(struct mapped_device *md)
1019{
1020 struct queue_limits *limits = dm_get_queue_limits(md);
1021
1022 /* device doesn't really support WRITE SAME, disable it */
1023 limits->max_write_same_sectors = 0;
1024}
1025
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001026static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001028 int error = bio->bi_error;
zhendong chen5164bec2014-12-17 14:37:04 +08001029 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -05001030 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001031 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -07001032 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 dm_endio_fn endio = tio->ti->type->end_io;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001036 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001037 if (r < 0 || r == DM_ENDIO_REQUEUE)
1038 /*
1039 * error and requeue request are handled
1040 * in dec_pending().
1041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001043 else if (r == DM_ENDIO_INCOMPLETE)
1044 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +02001045 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001046 else if (r) {
1047 DMWARN("unimplemented target endio return value: %d", r);
1048 BUG();
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001052 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
1053 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
1054 disable_write_same(md);
1055
Stefan Bader9faf4002006-10-03 01:15:41 -07001056 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001057 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Mike Snitzer78d8e582015-06-26 10:01:13 -04001060/*
1061 * Partial completion handling for request-based dm
1062 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001063static void end_clone_bio(struct bio *clone)
Mike Snitzer78d8e582015-06-26 10:01:13 -04001064{
1065 struct dm_rq_clone_bio_info *info =
1066 container_of(clone, struct dm_rq_clone_bio_info, clone);
1067 struct dm_rq_target_io *tio = info->tio;
1068 struct bio *bio = info->orig;
1069 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001070 int error = clone->bi_error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001071
1072 bio_put(clone);
1073
1074 if (tio->error)
1075 /*
1076 * An error has already been detected on the request.
1077 * Once error occurred, just let clone->end_io() handle
1078 * the remainder.
1079 */
1080 return;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001081 else if (error) {
Mike Snitzer78d8e582015-06-26 10:01:13 -04001082 /*
1083 * Don't notice the error to the upper layer yet.
1084 * The error handling decision is made by the target driver,
1085 * when the request is completed.
1086 */
Junichi Nomura50887bd2015-10-06 04:19:54 +00001087 tio->error = error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001088 return;
1089 }
1090
1091 /*
1092 * I/O for the bio successfully completed.
1093 * Notice the data completion to the upper layer.
1094 */
1095
1096 /*
1097 * bios are processed from the head of the list.
1098 * So the completing bio should always be rq->bio.
1099 * If it's not, something wrong is happening.
1100 */
1101 if (tio->orig->bio != bio)
1102 DMERR("bio completion is going in the middle of the request");
1103
1104 /*
1105 * Update the original request.
1106 * Do not use blk_end_request() here, because it may complete
1107 * the original request before the clone, and break the ordering.
1108 */
1109 blk_update_request(tio->orig, 0, nr_bytes);
1110}
1111
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001112static struct dm_rq_target_io *tio_from_request(struct request *rq)
1113{
1114 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1115}
1116
Mikulas Patockae262f342015-06-09 17:22:49 -04001117static void rq_end_stats(struct mapped_device *md, struct request *orig)
1118{
1119 if (unlikely(dm_stats_used(&md->stats))) {
1120 struct dm_rq_target_io *tio = tio_from_request(orig);
1121 tio->duration_jiffies = jiffies - tio->duration_jiffies;
1122 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1123 tio->n_sectors, true, tio->duration_jiffies,
1124 &tio->stats_aux);
1125 }
1126}
1127
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001128/*
1129 * Don't touch any member of the md after calling this function because
1130 * the md may be freed in dm_put() at the end of this function.
1131 * Or do dm_get() before calling this function and dm_put() later.
1132 */
Keith Busch466d89a2014-10-17 17:46:37 -06001133static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001134{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001135 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001136
1137 /* nudge anyone waiting on suspend queue */
Mike Snitzer621739b2015-07-08 16:08:24 -04001138 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001139 wake_up(&md->wait);
1140
Jens Axboea8c32a52012-11-06 12:24:26 +01001141 /*
1142 * Run this off this callpath, as drivers could invoke end_io while
1143 * inside their request_fn (and holding the queue lock). Calling
1144 * back into ->request_fn() could deadlock attempting to grab the
1145 * queue lock again.
1146 */
Mike Snitzer6acfe682016-02-05 08:49:01 -05001147 if (!md->queue->mq_ops && run_queue)
1148 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001149
1150 /*
1151 * dm_put() must be at the end of this function. See the comment above
1152 */
1153 dm_put(md);
1154}
1155
Mike Snitzere5d8de32015-05-28 15:12:52 -04001156static void free_rq_clone(struct request *clone)
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001157{
1158 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001159 struct mapped_device *md = tio->md;
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001160
Mike Snitzer78d8e582015-06-26 10:01:13 -04001161 blk_rq_unprep_clone(clone);
1162
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001163 if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1164 /* stacked on blk-mq queue(s) */
Mike Snitzere5863d92014-12-17 21:08:12 -05001165 tio->ti->type->release_clone_rq(clone);
Mike Snitzer02233342015-03-10 23:49:26 -04001166 else if (!md->queue->mq_ops)
1167 /* request_fn queue stacked on request_fn queue(s) */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001168 free_old_clone_request(md, clone);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001169
1170 if (!md->queue->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001171 free_old_rq_tio(tio);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001172}
1173
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001174/*
1175 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001176 * Must be called without clone's queue lock held,
1177 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001178 */
1179static void dm_end_request(struct request *clone, int error)
1180{
1181 int rw = rq_data_dir(clone);
1182 struct dm_rq_target_io *tio = clone->end_io_data;
1183 struct mapped_device *md = tio->md;
1184 struct request *rq = tio->orig;
1185
Tejun Heo29e40132010-09-08 18:07:00 +02001186 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001187 rq->errors = clone->errors;
1188 rq->resid_len = clone->resid_len;
1189
1190 if (rq->sense)
1191 /*
1192 * We are using the sense buffer of the original
1193 * request.
1194 * So setting the length of the sense data is enough.
1195 */
1196 rq->sense_len = clone->sense_len;
1197 }
1198
Mike Snitzere5d8de32015-05-28 15:12:52 -04001199 free_rq_clone(clone);
Mikulas Patockae262f342015-06-09 17:22:49 -04001200 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001201 if (!rq->q->mq_ops)
1202 blk_end_request_all(rq, error);
1203 else
1204 blk_mq_end_request(rq, error);
Tejun Heo29e40132010-09-08 18:07:00 +02001205 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001206}
1207
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001208static void dm_unprep_request(struct request *rq)
1209{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001210 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001211 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001212
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001213 if (!rq->q->mq_ops) {
1214 rq->special = NULL;
1215 rq->cmd_flags &= ~REQ_DONTPREP;
1216 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001217
Mike Snitzere5863d92014-12-17 21:08:12 -05001218 if (clone)
Mike Snitzere5d8de32015-05-28 15:12:52 -04001219 free_rq_clone(clone);
Mike Snitzer4328daa2016-02-21 19:09:22 -05001220 else if (!tio->md->queue->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001221 free_old_rq_tio(tio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001222}
1223
1224/*
1225 * Requeue the original request of a clone.
1226 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001227static void dm_old_requeue_request(struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001228{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001229 struct request_queue *q = rq->q;
1230 unsigned long flags;
1231
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001232 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001233 blk_requeue_request(q, rq);
Junichi Nomura4ae99442015-05-26 08:25:54 +00001234 blk_run_queue_async(q);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001235 spin_unlock_irqrestore(q->queue_lock, flags);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001236}
1237
Mike Snitzer818c5f32016-02-20 00:38:47 -05001238static void dm_mq_requeue_request(struct request *rq)
1239{
1240 struct request_queue *q = rq->q;
1241 unsigned long flags;
1242
1243 blk_mq_requeue_request(rq);
1244 spin_lock_irqsave(q->queue_lock, flags);
1245 if (!blk_queue_stopped(q))
1246 blk_mq_kick_requeue_list(q);
1247 spin_unlock_irqrestore(q->queue_lock, flags);
1248}
1249
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001250static void dm_requeue_original_request(struct mapped_device *md,
1251 struct request *rq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001252{
1253 int rw = rq_data_dir(rq);
1254
1255 dm_unprep_request(rq);
1256
Mikulas Patockae262f342015-06-09 17:22:49 -04001257 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001258 if (!rq->q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001259 dm_old_requeue_request(rq);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001260 else
1261 dm_mq_requeue_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001262
Keith Busch466d89a2014-10-17 17:46:37 -06001263 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001264}
Keith Busch466d89a2014-10-17 17:46:37 -06001265
Mike Snitzereca7ee62016-02-20 13:45:38 -05001266static void dm_old_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001267{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001268 unsigned long flags;
1269
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001270 spin_lock_irqsave(q->queue_lock, flags);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001271 if (blk_queue_stopped(q)) {
1272 spin_unlock_irqrestore(q->queue_lock, flags);
1273 return;
1274 }
1275
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001276 blk_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001277 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001278}
1279
Mike Snitzereca7ee62016-02-20 13:45:38 -05001280static void dm_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001281{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001282 if (!q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001283 dm_old_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001284 else
1285 blk_mq_stop_hw_queues(q);
1286}
1287
Mike Snitzereca7ee62016-02-20 13:45:38 -05001288static void dm_old_start_queue(struct request_queue *q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001289{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001290 unsigned long flags;
1291
1292 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001293 if (blk_queue_stopped(q))
1294 blk_start_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001295 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001296}
1297
Mike Snitzereca7ee62016-02-20 13:45:38 -05001298static void dm_start_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001299{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001300 if (!q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001301 dm_old_start_queue(q);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001302 else {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001303 blk_mq_start_stopped_hw_queues(q, true);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001304 blk_mq_kick_requeue_list(q);
1305 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001306}
1307
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001308static void dm_done(struct request *clone, int error, bool mapped)
1309{
1310 int r = error;
1311 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001312 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001313
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001314 if (tio->ti) {
1315 rq_end_io = tio->ti->type->rq_end_io;
1316
1317 if (mapped && rq_end_io)
1318 r = rq_end_io(tio->ti, clone, error, &tio->info);
1319 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001320
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001321 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1322 !clone->q->limits.max_write_same_sectors))
1323 disable_write_same(tio->md);
1324
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001325 if (r <= 0)
1326 /* The target wants to complete the I/O */
1327 dm_end_request(clone, r);
1328 else if (r == DM_ENDIO_INCOMPLETE)
1329 /* The target will handle the I/O */
1330 return;
1331 else if (r == DM_ENDIO_REQUEUE)
1332 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001333 dm_requeue_original_request(tio->md, tio->orig);
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001334 else {
1335 DMWARN("unimplemented target endio return value: %d", r);
1336 BUG();
1337 }
1338}
1339
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001340/*
1341 * Request completion handler for request-based dm
1342 */
1343static void dm_softirq_done(struct request *rq)
1344{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001345 bool mapped = true;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001346 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001347 struct request *clone = tio->clone;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001348 int rw;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001349
Mike Snitzere5863d92014-12-17 21:08:12 -05001350 if (!clone) {
Mikulas Patockae262f342015-06-09 17:22:49 -04001351 rq_end_stats(tio->md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001352 rw = rq_data_dir(rq);
1353 if (!rq->q->mq_ops) {
1354 blk_end_request_all(rq, tio->error);
1355 rq_completed(tio->md, rw, false);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001356 free_old_rq_tio(tio);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001357 } else {
1358 blk_mq_end_request(rq, tio->error);
1359 rq_completed(tio->md, rw, false);
1360 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001361 return;
1362 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001363
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001364 if (rq->cmd_flags & REQ_FAILED)
1365 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001366
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001367 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001368}
1369
1370/*
1371 * Complete the clone and the original request with the error status
1372 * through softirq context.
1373 */
Keith Busch466d89a2014-10-17 17:46:37 -06001374static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001375{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001376 struct dm_rq_target_io *tio = tio_from_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001377
1378 tio->error = error;
Mike Snitzer6acfe682016-02-05 08:49:01 -05001379 if (!rq->q->mq_ops)
1380 blk_complete_request(rq);
1381 else
1382 blk_mq_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001383}
1384
1385/*
1386 * Complete the not-mapped clone and the original request with the error status
1387 * through softirq context.
1388 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001389 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001390 */
Keith Busch466d89a2014-10-17 17:46:37 -06001391static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001392{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001393 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001394 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001395}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001396
1397/*
Mike Snitzereca7ee62016-02-20 13:45:38 -05001398 * Called with the clone's queue lock held (in the case of .request_fn)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001399 */
1400static void end_clone_request(struct request *clone, int error)
1401{
Keith Busch466d89a2014-10-17 17:46:37 -06001402 struct dm_rq_target_io *tio = clone->end_io_data;
1403
Mike Snitzere5863d92014-12-17 21:08:12 -05001404 if (!clone->q->mq_ops) {
1405 /*
1406 * For just cleaning up the information of the queue in which
1407 * the clone was dispatched.
1408 * The clone is *NOT* freed actually here because it is alloced
1409 * from dm own mempool (REQ_ALLOCED isn't set).
1410 */
1411 __blk_put_request(clone->q, clone);
1412 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001413
1414 /*
1415 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001416 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001417 * - another request may be submitted by the upper level driver
1418 * of the stacking during the completion
1419 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001420 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001421 */
Keith Busch466d89a2014-10-17 17:46:37 -06001422 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001423}
1424
Mike Snitzer56a67df2010-08-12 04:14:10 +01001425/*
1426 * Return maximum size of I/O possible at the supplied sector up to the current
1427 * target boundary.
1428 */
1429static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001431 sector_t target_offset = dm_target_offset(ti, sector);
1432
1433 return ti->len - target_offset;
1434}
1435
1436static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1437{
1438 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001439 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001442 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001444 if (ti->max_io_len) {
1445 offset = dm_target_offset(ti, sector);
1446 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1447 max_len = sector_div(offset, ti->max_io_len);
1448 else
1449 max_len = offset & (ti->max_io_len - 1);
1450 max_len = ti->max_io_len - max_len;
1451
1452 if (len > max_len)
1453 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455
1456 return len;
1457}
1458
Mike Snitzer542f9032012-07-27 15:08:00 +01001459int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1460{
1461 if (len > UINT_MAX) {
1462 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1463 (unsigned long long)len, UINT_MAX);
1464 ti->error = "Maximum size of target IO is too large";
1465 return -EINVAL;
1466 }
1467
1468 ti->max_io_len = (uint32_t) len;
1469
1470 return 0;
1471}
1472EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1473
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001474/*
1475 * A target may call dm_accept_partial_bio only from the map routine. It is
1476 * allowed for all bio types except REQ_FLUSH.
1477 *
1478 * dm_accept_partial_bio informs the dm that the target only wants to process
1479 * additional n_sectors sectors of the bio and the rest of the data should be
1480 * sent in a next bio.
1481 *
1482 * A diagram that explains the arithmetics:
1483 * +--------------------+---------------+-------+
1484 * | 1 | 2 | 3 |
1485 * +--------------------+---------------+-------+
1486 *
1487 * <-------------- *tio->len_ptr --------------->
1488 * <------- bi_size ------->
1489 * <-- n_sectors -->
1490 *
1491 * Region 1 was already iterated over with bio_advance or similar function.
1492 * (it may be empty if the target doesn't use bio_advance)
1493 * Region 2 is the remaining bio size that the target wants to process.
1494 * (it may be empty if region 1 is non-empty, although there is no reason
1495 * to make it empty)
1496 * The target requires that region 3 is to be sent in the next bio.
1497 *
1498 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1499 * the partially processed part (the sum of regions 1+2) must be the same for all
1500 * copies of the bio.
1501 */
1502void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1503{
1504 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1505 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1506 BUG_ON(bio->bi_rw & REQ_FLUSH);
1507 BUG_ON(bi_size > *tio->len_ptr);
1508 BUG_ON(n_sectors > bi_size);
1509 *tio->len_ptr -= bi_size - n_sectors;
1510 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1511}
1512EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1513
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001514static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001517 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001518 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001519 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001520 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /*
1525 * Map the clone. If r == 0 we don't need to do
1526 * anything, the target has assumed ownership of
1527 * this io.
1528 */
1529 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001530 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001531 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001532 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001534
Mike Snitzerd07335e2010-11-16 12:52:38 +01001535 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1536 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001539 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1540 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001541 md = tio->io->md;
1542 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001543 free_tio(md, tio);
Mikulas Patockaab378442015-07-01 17:30:36 -04001544 } else if (r != DM_MAPIO_SUBMITTED) {
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001545 DMWARN("unimplemented target map return value: %d", r);
1546 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548}
1549
1550struct clone_info {
1551 struct mapped_device *md;
1552 struct dm_table *map;
1553 struct bio *bio;
1554 struct dm_io *io;
1555 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001556 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557};
1558
Mikulas Patockae0d66092014-03-14 18:40:39 -04001559static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001560{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001561 bio->bi_iter.bi_sector = sector;
1562 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565/*
1566 * Creates a bio that consists of range of complete bvecs.
1567 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001568static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001569 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Mikulas Patockadba14162012-10-12 21:02:15 +01001571 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001573 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001575 if (bio_integrity(bio))
1576 bio_integrity_clone(clone, bio, GFP_NOIO);
1577
1578 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1579 clone->bi_iter.bi_size = to_bytes(len);
1580
1581 if (bio_integrity(bio))
1582 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001585static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001586 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001587 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001588{
Mikulas Patockadba14162012-10-12 21:02:15 +01001589 struct dm_target_io *tio;
1590 struct bio *clone;
1591
Junichi Nomura99778272014-10-03 11:55:16 +00001592 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001593 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001594
1595 tio->io = ci->io;
1596 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001597 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001598
1599 return tio;
1600}
1601
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001602static void __clone_and_map_simple_bio(struct clone_info *ci,
1603 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001604 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001605{
Junichi Nomura99778272014-10-03 11:55:16 +00001606 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001607 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001608
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001609 tio->len_ptr = len;
1610
Junichi Nomura99778272014-10-03 11:55:16 +00001611 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001612 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001613 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001614
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001615 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001616}
1617
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001618static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001619 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001620{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001621 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001622
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001623 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001624 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001625}
1626
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001627static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001628{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001629 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001630 struct dm_target *ti;
1631
Mike Snitzerb372d362010-09-08 18:07:01 +02001632 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001633 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001634 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001635
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001636 return 0;
1637}
1638
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001639static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001640 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001641{
Mikulas Patockadba14162012-10-12 21:02:15 +01001642 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001643 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001644 unsigned target_bio_nr;
1645 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001646
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001647 /*
1648 * Does the target want to receive duplicate copies of the bio?
1649 */
1650 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1651 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001652
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001653 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001654 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001655 tio->len_ptr = len;
1656 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001657 __map_bio(tio);
1658 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001659}
1660
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001661typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001662
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001663static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001664{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001665 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001666}
1667
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001668static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001669{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001670 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001671}
1672
1673typedef bool (*is_split_required_fn)(struct dm_target *ti);
1674
1675static bool is_split_required_for_discard(struct dm_target *ti)
1676{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001677 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001678}
1679
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001680static int __send_changing_extent_only(struct clone_info *ci,
1681 get_num_bios_fn get_num_bios,
1682 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001683{
1684 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001685 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001686 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001687
Mike Snitzera79245b2010-08-12 04:14:24 +01001688 do {
1689 ti = dm_table_find_target(ci->map, ci->sector);
1690 if (!dm_target_is_valid(ti))
1691 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001692
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001693 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001694 * Even though the device advertised support for this type of
1695 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001696 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001697 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001698 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001699 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1700 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001701 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001702
Mike Snitzer23508a92012-12-21 20:23:37 +00001703 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001704 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001705 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001706 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001707
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001708 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001709
1710 ci->sector += len;
1711 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001712
1713 return 0;
1714}
1715
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001716static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001717{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001718 return __send_changing_extent_only(ci, get_num_discard_bios,
1719 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001720}
1721
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001722static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001723{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001724 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001725}
1726
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001727/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001728 * Select the correct strategy for processing a non-flush bio.
1729 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001730static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Mikulas Patockadba14162012-10-12 21:02:15 +01001732 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001733 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001734 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001736 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001737 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001738 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001739 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001740
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001741 ti = dm_table_find_target(ci->map, ci->sector);
1742 if (!dm_target_is_valid(ti))
1743 return -EIO;
1744
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001745 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001746
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001747 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001749 ci->sector += len;
1750 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
1755/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001756 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001758static void __split_and_process_bio(struct mapped_device *md,
1759 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001762 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001764 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001765 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001766 return;
1767 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001768
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001769 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 ci.io = alloc_io(md);
1772 ci.io->error = 0;
1773 atomic_set(&ci.io->io_count, 1);
1774 ci.io->bio = bio;
1775 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001776 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001777 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001779 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001780
Mike Snitzerb372d362010-09-08 18:07:01 +02001781 if (bio->bi_rw & REQ_FLUSH) {
1782 ci.bio = &ci.md->flush_bio;
1783 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001784 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001785 /* dec_pending submits any data associated with flush */
1786 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001787 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001788 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001789 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001790 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001794 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796/*-----------------------------------------------------------------
1797 * CRUD END
1798 *---------------------------------------------------------------*/
1799
1800/*
1801 * The request function that just remaps the bio built up by
1802 * dm_merge_bvec.
1803 */
Jens Axboedece1632015-11-05 10:41:16 -07001804static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Kevin Corry12f03a42006-02-01 03:04:52 -08001806 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001808 int srcu_idx;
1809 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001811 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Gu Zheng18c0b222014-11-24 11:05:26 +08001813 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001814
Tejun Heo6a8736d2010-09-08 18:07:00 +02001815 /* if we're suspended, we have to queue this io for later */
1816 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001817 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Tejun Heo6a8736d2010-09-08 18:07:00 +02001819 if (bio_rw(bio) != READA)
1820 queue_io(md, bio);
1821 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001822 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001823 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001826 __split_and_process_bio(md, map, bio);
1827 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001828 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001829}
1830
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001831int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001832{
1833 return blk_queue_stackable(md->queue);
1834}
1835
Keith Busch466d89a2014-10-17 17:46:37 -06001836static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001837{
1838 int r;
1839
Keith Busch466d89a2014-10-17 17:46:37 -06001840 if (blk_queue_io_stat(clone->q))
1841 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001842
Keith Busch466d89a2014-10-17 17:46:37 -06001843 clone->start_time = jiffies;
1844 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001845 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001846 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001847 dm_complete_request(rq, r);
1848}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001849
Mike Snitzer78d8e582015-06-26 10:01:13 -04001850static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1851 void *data)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001852{
Mike Snitzer78d8e582015-06-26 10:01:13 -04001853 struct dm_rq_target_io *tio = data;
1854 struct dm_rq_clone_bio_info *info =
1855 container_of(bio, struct dm_rq_clone_bio_info, clone);
1856
1857 info->orig = bio_orig;
1858 info->tio = tio;
1859 bio->bi_end_io = end_clone_bio;
1860
1861 return 0;
1862}
1863
1864static int setup_clone(struct request *clone, struct request *rq,
1865 struct dm_rq_target_io *tio, gfp_t gfp_mask)
1866{
1867 int r;
1868
1869 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1870 dm_rq_bio_constructor, tio);
1871 if (r)
1872 return r;
1873
1874 clone->cmd = rq->cmd;
1875 clone->cmd_len = rq->cmd_len;
1876 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001877 clone->end_io = end_clone_request;
1878 clone->end_io_data = tio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001879
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001880 tio->clone = clone;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001881
1882 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001883}
1884
Mike Snitzereca7ee62016-02-20 13:45:38 -05001885static struct request *clone_old_rq(struct request *rq, struct mapped_device *md,
1886 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001887{
Mike Snitzer02233342015-03-10 23:49:26 -04001888 /*
Mike Snitzerc5248f72016-02-20 14:02:49 -05001889 * Create clone for use with .request_fn request_queue
Mike Snitzer02233342015-03-10 23:49:26 -04001890 */
Mike Snitzer02233342015-03-10 23:49:26 -04001891 struct request *clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001892
Mike Snitzereca7ee62016-02-20 13:45:38 -05001893 clone = alloc_old_clone_request(md, gfp_mask);
Mike Snitzerc5248f72016-02-20 14:02:49 -05001894 if (!clone)
1895 return NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001896
1897 blk_rq_init(NULL, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001898 if (setup_clone(clone, rq, tio, gfp_mask)) {
1899 /* -ENOMEM */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001900 free_old_clone_request(md, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001901 return NULL;
1902 }
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001903
1904 return clone;
1905}
1906
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001907static void map_tio_request(struct kthread_work *work);
1908
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001909static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1910 struct mapped_device *md)
1911{
1912 tio->md = md;
1913 tio->ti = NULL;
1914 tio->clone = NULL;
1915 tio->orig = rq;
1916 tio->error = 0;
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001917 /*
1918 * Avoid initializing info for blk-mq; it passes
1919 * target-specific data through info.ptr
1920 * (see: dm_mq_init_request)
1921 */
1922 if (!md->init_tio_pdu)
1923 memset(&tio->info, 0, sizeof(tio->info));
Mike Snitzer02233342015-03-10 23:49:26 -04001924 if (md->kworker_task)
1925 init_kthread_work(&tio->work, map_tio_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001926}
1927
Mike Snitzereca7ee62016-02-20 13:45:38 -05001928static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq,
1929 struct mapped_device *md,
1930 gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001931{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001932 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001933 int srcu_idx;
1934 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001935
Mike Snitzereca7ee62016-02-20 13:45:38 -05001936 tio = alloc_old_rq_tio(md, gfp_mask);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001937 if (!tio)
1938 return NULL;
1939
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001940 init_tio(tio, rq, md);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001941
Mike Snitzere5863d92014-12-17 21:08:12 -05001942 table = dm_get_live_table(md, &srcu_idx);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001943 /*
1944 * Must clone a request if this .request_fn DM device
1945 * is stacked on .request_fn device(s).
1946 */
Mike Snitzere5863d92014-12-17 21:08:12 -05001947 if (!dm_table_mq_request_based(table)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001948 if (!clone_old_rq(rq, md, tio, gfp_mask)) {
Mike Snitzere5863d92014-12-17 21:08:12 -05001949 dm_put_live_table(md, srcu_idx);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001950 free_old_rq_tio(tio);
Mike Snitzere5863d92014-12-17 21:08:12 -05001951 return NULL;
1952 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001953 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001954 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001955
Keith Busch466d89a2014-10-17 17:46:37 -06001956 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001957}
1958
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001959/*
1960 * Called with the queue lock held.
1961 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001962static int dm_old_prep_fn(struct request_queue *q, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001963{
1964 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001965 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001966
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001967 if (unlikely(rq->special)) {
1968 DMWARN("Already has something in rq->special.");
1969 return BLKPREP_KILL;
1970 }
1971
Mike Snitzereca7ee62016-02-20 13:45:38 -05001972 tio = dm_old_prep_tio(rq, md, GFP_ATOMIC);
Keith Busch466d89a2014-10-17 17:46:37 -06001973 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001974 return BLKPREP_DEFER;
1975
Keith Busch466d89a2014-10-17 17:46:37 -06001976 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001977 rq->cmd_flags |= REQ_DONTPREP;
1978
1979 return BLKPREP_OK;
1980}
1981
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001982/*
1983 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05001984 * 0 : the request has been processed
1985 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1986 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001987 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001988static int map_request(struct dm_rq_target_io *tio, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001989 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001990{
Mike Snitzere5863d92014-12-17 21:08:12 -05001991 int r;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001992 struct dm_target *ti = tio->ti;
Mike Snitzere5863d92014-12-17 21:08:12 -05001993 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001994
Mike Snitzere5863d92014-12-17 21:08:12 -05001995 if (tio->clone) {
1996 clone = tio->clone;
1997 r = ti->type->map_rq(ti, clone, &tio->info);
1998 } else {
1999 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
2000 if (r < 0) {
2001 /* The target wants to complete the I/O */
2002 dm_kill_unmapped_request(rq, r);
2003 return r;
2004 }
Junichi Nomura3a140752015-05-27 04:22:07 +00002005 if (r != DM_MAPIO_REMAPPED)
2006 return r;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002007 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
2008 /* -ENOMEM */
2009 ti->type->release_clone_rq(clone);
2010 return DM_MAPIO_REQUEUE;
2011 }
Mike Snitzere5863d92014-12-17 21:08:12 -05002012 }
2013
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002014 switch (r) {
2015 case DM_MAPIO_SUBMITTED:
2016 /* The target has taken the I/O to submit by itself later */
2017 break;
2018 case DM_MAPIO_REMAPPED:
2019 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00002020 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06002021 blk_rq_pos(rq));
2022 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002023 break;
2024 case DM_MAPIO_REQUEUE:
2025 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04002026 dm_requeue_original_request(md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002027 break;
2028 default:
2029 if (r > 0) {
2030 DMWARN("unimplemented target map return value: %d", r);
2031 BUG();
2032 }
2033
2034 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06002035 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05002036 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002037 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00002038
Mike Snitzere5863d92014-12-17 21:08:12 -05002039 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002040}
2041
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002042static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002043{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002044 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05002045 struct request *rq = tio->orig;
2046 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002047
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002048 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
Mike Snitzer2d76fff2015-04-29 12:07:12 -04002049 dm_requeue_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002050}
2051
Keith Busch466d89a2014-10-17 17:46:37 -06002052static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002053{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002054 if (!orig->q->mq_ops)
2055 blk_start_request(orig);
2056 else
2057 blk_mq_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06002058 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002059
Mike Snitzer0ce65792015-02-26 00:50:28 -05002060 if (md->seq_rq_merge_deadline_usecs) {
2061 md->last_rq_pos = rq_end_sector(orig);
2062 md->last_rq_rw = rq_data_dir(orig);
2063 md->last_rq_start_time = ktime_get();
2064 }
Mike Snitzerde3ec862015-02-24 21:58:21 -05002065
Mikulas Patockae262f342015-06-09 17:22:49 -04002066 if (unlikely(dm_stats_used(&md->stats))) {
2067 struct dm_rq_target_io *tio = tio_from_request(orig);
2068 tio->duration_jiffies = jiffies;
2069 tio->n_sectors = blk_rq_sectors(orig);
2070 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
2071 tio->n_sectors, false, 0, &tio->stats_aux);
2072 }
2073
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002074 /*
2075 * Hold the md reference here for the in-flight I/O.
2076 * We can't rely on the reference count by device opener,
2077 * because the device may be closed during the request completion
2078 * when all bios are completed.
2079 * See the comment in rq_completed() too.
2080 */
2081 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002082}
2083
Mike Snitzer0ce65792015-02-26 00:50:28 -05002084#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
2085
2086ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
2087{
2088 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
2089}
2090
2091ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
2092 const char *buf, size_t count)
2093{
2094 unsigned deadline;
2095
Mike Snitzer17e149b2015-03-11 15:01:09 -04002096 if (!dm_request_based(md) || md->use_blk_mq)
Mike Snitzer0ce65792015-02-26 00:50:28 -05002097 return count;
2098
2099 if (kstrtouint(buf, 10, &deadline))
2100 return -EINVAL;
2101
2102 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
2103 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
2104
2105 md->seq_rq_merge_deadline_usecs = deadline;
2106
2107 return count;
2108}
2109
2110static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
2111{
2112 ktime_t kt_deadline;
2113
2114 if (!md->seq_rq_merge_deadline_usecs)
2115 return false;
2116
2117 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
2118 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
2119
2120 return !ktime_after(ktime_get(), kt_deadline);
2121}
2122
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002123/*
2124 * q->request_fn for request-based dm.
2125 * Called with the queue lock held.
2126 */
2127static void dm_request_fn(struct request_queue *q)
2128{
2129 struct mapped_device *md = q->queuedata;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002130 struct dm_target *ti = md->immutable_target;
Keith Busch466d89a2014-10-17 17:46:37 -06002131 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002132 struct dm_rq_target_io *tio;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002133 sector_t pos = 0;
2134
2135 if (unlikely(!ti)) {
2136 int srcu_idx;
2137 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2138
2139 ti = dm_table_find_target(map, pos);
2140 dm_put_live_table(md, srcu_idx);
2141 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002142
2143 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002144 * For suspend, check blk_queue_stopped() and increment
2145 * ->pending within a single queue_lock not to increment the
2146 * number of in-flight I/Os after the queue is stopped in
2147 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002148 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002149 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002150 rq = blk_peek_request(q);
2151 if (!rq)
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002152 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002153
Tejun Heo29e40132010-09-08 18:07:00 +02002154 /* always use block 0 to find the target for flushes for now */
2155 pos = 0;
2156 if (!(rq->cmd_flags & REQ_FLUSH))
2157 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002158
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002159 if ((dm_request_peeked_before_merge_deadline(md) &&
2160 md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2161 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
2162 (ti->type->busy && ti->type->busy(ti))) {
2163 blk_delay_queue(q, HZ / 100);
2164 return;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002165 }
Tejun Heo29e40132010-09-08 18:07:00 +02002166
Keith Busch466d89a2014-10-17 17:46:37 -06002167 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002168
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002169 tio = tio_from_request(rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002170 /* Establish tio->ti before queuing work (map_tio_request) */
2171 tio->ti = ti;
2172 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00002173 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002174 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002175}
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177static int dm_any_congested(void *congested_data, int bdi_bits)
2178{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002179 int r = bdi_bits;
2180 struct mapped_device *md = congested_data;
2181 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002183 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05002184 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002185 /*
Mike Snitzere522c032016-02-02 22:35:06 -05002186 * With request-based DM we only need to check the
2187 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002188 */
Mike Snitzere522c032016-02-02 22:35:06 -05002189 r = md->queue->backing_dev_info.wb.state & bdi_bits;
2190 } else {
2191 map = dm_get_live_table_fast(md);
2192 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002193 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05002194 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002195 }
2196 }
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return r;
2199}
2200
2201/*-----------------------------------------------------------------
2202 * An IDR is used to keep track of allocated minor numbers.
2203 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002204static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002206 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002208 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
2211/*
2212 * See if the device with a specific minor # is free.
2213 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002214static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002216 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 if (minor >= (1 << MINORBITS))
2219 return -EINVAL;
2220
Tejun Heoc9d76be2013-02-27 17:04:26 -08002221 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002222 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Tejun Heoc9d76be2013-02-27 17:04:26 -08002224 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002226 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002227 idr_preload_end();
2228 if (r < 0)
2229 return r == -ENOSPC ? -EBUSY : r;
2230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002233static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002235 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Tejun Heoc9d76be2013-02-27 17:04:26 -08002237 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002238 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Tejun Heoc9d76be2013-02-27 17:04:26 -08002240 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002242 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002243 idr_preload_end();
2244 if (r < 0)
2245 return r;
2246 *minor = r;
2247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002250static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Mikulas Patocka53d59142009-04-02 19:55:37 +01002252static void dm_wq_work(struct work_struct *work);
2253
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002254static void dm_init_md_queue(struct mapped_device *md)
2255{
2256 /*
2257 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002258 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002259 * The type is decided at the first table loading time.
2260 * To prevent problematic device stacking, clear the queue flag
2261 * for request stacking support until then.
2262 *
2263 * This queue is new, so no concurrency on the queue_flags.
2264 */
2265 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04002266
2267 /*
2268 * Initialize data that will only be used by a non-blk-mq DM queue
2269 * - must do so here (in alloc_dev callchain) before queue is used
2270 */
2271 md->queue->queuedata = md;
2272 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002273}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002274
Mike Snitzereca7ee62016-02-20 13:45:38 -05002275static void dm_init_normal_md_queue(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002276{
Mike Snitzer17e149b2015-03-11 15:01:09 -04002277 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002278 dm_init_md_queue(md);
2279
2280 /*
2281 * Initialize aspects of queue that aren't relevant for blk-mq
2282 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002283 md->queue->backing_dev_info.congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002284 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002285}
2286
Mike Snitzer0f209722015-04-28 11:50:29 -04002287static void cleanup_mapped_device(struct mapped_device *md)
2288{
Mike Snitzer0f209722015-04-28 11:50:29 -04002289 if (md->wq)
2290 destroy_workqueue(md->wq);
2291 if (md->kworker_task)
2292 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02002293 mempool_destroy(md->io_pool);
2294 mempool_destroy(md->rq_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04002295 if (md->bs)
2296 bioset_free(md->bs);
2297
Mikulas Patockab06075a2015-07-10 17:21:43 -04002298 cleanup_srcu_struct(&md->io_barrier);
2299
Mike Snitzer0f209722015-04-28 11:50:29 -04002300 if (md->disk) {
2301 spin_lock(&_minor_lock);
2302 md->disk->private_data = NULL;
2303 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04002304 del_gendisk(md->disk);
2305 put_disk(md->disk);
2306 }
2307
2308 if (md->queue)
2309 blk_cleanup_queue(md->queue);
2310
2311 if (md->bdev) {
2312 bdput(md->bdev);
2313 md->bdev = NULL;
2314 }
2315}
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/*
2318 * Allocate and initialise a blank device with a given minor.
2319 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002320static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Mike Snitzer115485e2016-02-22 12:16:21 -05002322 int r, numa_node_id = dm_get_numa_node();
2323 struct mapped_device *md;
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002324 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Mike Snitzer115485e2016-02-22 12:16:21 -05002326 md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (!md) {
2328 DMWARN("unable to allocate device, out of memory.");
2329 return NULL;
2330 }
2331
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002332 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002333 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002336 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002337 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002338 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002339 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002341 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002343 r = init_srcu_struct(&md->io_barrier);
2344 if (r < 0)
2345 goto bad_io_barrier;
2346
Mike Snitzer115485e2016-02-22 12:16:21 -05002347 md->numa_node_id = numa_node_id;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002348 md->use_blk_mq = use_blk_mq;
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002349 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01002350 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002351 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002352 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002353 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002354 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002356 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002358 atomic_set(&md->uevent_seq, 0);
2359 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002360 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002361 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Mike Snitzer115485e2016-02-22 12:16:21 -05002363 md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04002365 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002367 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002368
Mike Snitzer115485e2016-02-22 12:16:21 -05002369 md->disk = alloc_disk_node(1, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04002371 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002373 atomic_set(&md->pending[0], 0);
2374 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002375 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002376 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002377 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002378 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002379 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 md->disk->major = _major;
2382 md->disk->first_minor = minor;
2383 md->disk->fops = &dm_blk_dops;
2384 md->disk->queue = md->queue;
2385 md->disk->private_data = md;
2386 sprintf(md->disk->disk_name, "dm-%d", minor);
2387 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002388 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Tejun Heo670368a2013-07-30 08:40:21 -04002390 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002391 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002392 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002393
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002394 md->bdev = bdget_disk(md->disk, 0);
2395 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002396 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002397
Tejun Heo6a8736d2010-09-08 18:07:00 +02002398 bio_init(&md->flush_bio);
2399 md->flush_bio.bi_bdev = md->bdev;
2400 md->flush_bio.bi_rw = WRITE_FLUSH;
2401
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002402 dm_stats_init(&md->stats);
2403
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002404 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002405 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002406 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002407 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002408
2409 BUG_ON(old_md != MINOR_ALLOCED);
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return md;
2412
Mike Snitzer0f209722015-04-28 11:50:29 -04002413bad:
2414 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002415bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002417bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002418 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002419bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 kfree(md);
2421 return NULL;
2422}
2423
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002424static void unlock_fs(struct mapped_device *md);
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426static void free_dev(struct mapped_device *md)
2427{
Tejun Heof331c022008-09-03 09:01:48 +02002428 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002429
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002430 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002431
Mike Snitzer0f209722015-04-28 11:50:29 -04002432 cleanup_mapped_device(md);
Mike Snitzer1c357a12016-02-06 17:01:17 -05002433 if (md->tag_set) {
2434 blk_mq_free_tag_set(md->tag_set);
2435 kfree(md->tag_set);
2436 }
Mike Snitzer0f209722015-04-28 11:50:29 -04002437
2438 free_table_devices(&md->table_devices);
2439 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002440 free_minor(minor);
2441
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002442 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 kfree(md);
2444}
2445
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002446static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2447{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002448 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002449
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002450 if (md->bs) {
2451 /* The md already has necessary mempools. */
2452 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002453 /*
2454 * Reload bioset because front_pad may have changed
2455 * because a different table was loaded.
2456 */
2457 bioset_free(md->bs);
2458 md->bs = p->bs;
2459 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002460 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002461 /*
2462 * There's no need to reload with request-based dm
2463 * because the size of front_pad doesn't change.
2464 * Note for future: If you are to reload bioset,
2465 * prep-ed requests in the queue may refer
2466 * to bio from the old bioset, so you must walk
2467 * through the queue to unprep.
2468 */
2469 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002470 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002471
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002472 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2473
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002474 md->io_pool = p->io_pool;
2475 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002476 md->rq_pool = p->rq_pool;
2477 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002478 md->bs = p->bs;
2479 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002480
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002481out:
Mike Snitzer02233342015-03-10 23:49:26 -04002482 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002483 dm_table_free_md_mempools(t);
2484}
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486/*
2487 * Bind a table to the device.
2488 */
2489static void event_callback(void *context)
2490{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002491 unsigned long flags;
2492 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 struct mapped_device *md = (struct mapped_device *) context;
2494
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002495 spin_lock_irqsave(&md->uevent_lock, flags);
2496 list_splice_init(&md->uevent_list, &uevents);
2497 spin_unlock_irqrestore(&md->uevent_lock, flags);
2498
Tejun Heoed9e1982008-08-25 19:56:05 +09002499 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 atomic_inc(&md->event_nr);
2502 wake_up(&md->eventq);
2503}
2504
Mike Snitzerc2176492011-01-13 19:53:46 +00002505/*
2506 * Protected by md->suspend_lock obtained by dm_swap_table().
2507 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002508static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002510 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002512 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002515/*
2516 * Returns old map, which caller must destroy.
2517 */
2518static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2519 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002521 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002522 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 sector_t size;
2524
2525 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002526
2527 /*
2528 * Wipe any geometry if the size of the table changed.
2529 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002530 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002531 memset(&md->geometry, 0, sizeof(md->geometry));
2532
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002533 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002535 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002536
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002537 /*
2538 * The queue hasn't been stopped yet, if the old table type wasn't
2539 * for request-based during suspension. So stop it to prevent
2540 * I/O mapping before resume.
2541 * This must be done before setting the queue restrictions,
2542 * because request-based dm may be run just after the setting.
2543 */
Mike Snitzer16f12262016-01-31 17:22:27 -05002544 if (dm_table_request_based(t)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002545 dm_stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05002546 /*
2547 * Leverage the fact that request-based DM targets are
2548 * immutable singletons and establish md->immutable_target
2549 * - used to optimize both dm_request_fn and dm_mq_queue_rq
2550 */
2551 md->immutable_target = dm_table_get_immutable_target(t);
2552 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002553
2554 __bind_mempools(md, t);
2555
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002556 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -05002557 rcu_assign_pointer(md->map, (void *)t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002558 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2559
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002560 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002561 if (old_map)
2562 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002563
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002564 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Alasdair G Kergona7940152009-12-10 23:52:23 +00002567/*
2568 * Returns unbound table for the caller to free.
2569 */
2570static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002572 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002575 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302578 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002579 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002580
2581 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
2584/*
2585 * Constructor for a new device.
2586 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002587int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 struct mapped_device *md;
2590
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002591 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (!md)
2593 return -ENXIO;
2594
Milan Broz784aae72009-01-06 03:05:12 +00002595 dm_sysfs_init(md);
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 *result = md;
2598 return 0;
2599}
2600
Mike Snitzera5664da2010-08-12 04:14:01 +01002601/*
2602 * Functions to manage md->type.
2603 * All are required to hold md->type_lock.
2604 */
2605void dm_lock_md_type(struct mapped_device *md)
2606{
2607 mutex_lock(&md->type_lock);
2608}
2609
2610void dm_unlock_md_type(struct mapped_device *md)
2611{
2612 mutex_unlock(&md->type_lock);
2613}
2614
2615void dm_set_md_type(struct mapped_device *md, unsigned type)
2616{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002617 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002618 md->type = type;
2619}
2620
2621unsigned dm_get_md_type(struct mapped_device *md)
2622{
2623 return md->type;
2624}
2625
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002626struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2627{
2628 return md->immutable_target_type;
2629}
2630
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002631/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002632 * The queue_limits are only valid as long as you have a reference
2633 * count on 'md'.
2634 */
2635struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2636{
2637 BUG_ON(!atomic_read(&md->holders));
2638 return &md->queue->limits;
2639}
2640EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2641
Mike Snitzereca7ee62016-02-20 13:45:38 -05002642static void dm_old_init_rq_based_worker_thread(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002643{
2644 /* Initialize the request-based DM worker thread */
2645 init_kthread_worker(&md->kworker);
2646 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2647 "kdmwork-%s", dm_device_name(md));
2648}
2649
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002650/*
Mike Snitzereca7ee62016-02-20 13:45:38 -05002651 * Fully initialize a .request_fn request-based queue.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002652 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05002653static int dm_old_init_request_queue(struct mapped_device *md)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002654{
2655 struct request_queue *q = NULL;
2656
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002657 /* Fully initialize the queue */
2658 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2659 if (!q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002660 return -EINVAL;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002661
Mike Snitzer0ce65792015-02-26 00:50:28 -05002662 /* disable dm_request_fn's merge heuristic by default */
2663 md->seq_rq_merge_deadline_usecs = 0;
2664
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002665 md->queue = q;
Mike Snitzereca7ee62016-02-20 13:45:38 -05002666 dm_init_normal_md_queue(md);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002667 blk_queue_softirq_done(md->queue, dm_softirq_done);
Mike Snitzereca7ee62016-02-20 13:45:38 -05002668 blk_queue_prep_rq(md->queue, dm_old_prep_fn);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002669
Mike Snitzereca7ee62016-02-20 13:45:38 -05002670 dm_old_init_rq_based_worker_thread(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002671
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002672 elv_register_queue(md->queue);
2673
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002674 return 0;
2675}
2676
2677static int dm_mq_init_request(void *data, struct request *rq,
2678 unsigned int hctx_idx, unsigned int request_idx,
2679 unsigned int numa_node)
2680{
2681 struct mapped_device *md = data;
2682 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2683
2684 /*
2685 * Must initialize md member of tio, otherwise it won't
2686 * be available in dm_mq_queue_rq.
2687 */
2688 tio->md = md;
2689
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002690 if (md->init_tio_pdu) {
2691 /* target-specific per-io data is immediately after the tio */
2692 tio->info.ptr = tio + 1;
2693 }
2694
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002695 return 0;
2696}
2697
2698static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2699 const struct blk_mq_queue_data *bd)
2700{
2701 struct request *rq = bd->rq;
2702 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2703 struct mapped_device *md = tio->md;
Mike Snitzer16f12262016-01-31 17:22:27 -05002704 struct dm_target *ti = md->immutable_target;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002705
Mike Snitzer16f12262016-01-31 17:22:27 -05002706 if (unlikely(!ti)) {
2707 int srcu_idx;
2708 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002709
Mike Snitzer16f12262016-01-31 17:22:27 -05002710 ti = dm_table_find_target(map, 0);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002711 dm_put_live_table(md, srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002712 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002713
2714 if (ti->type->busy && ti->type->busy(ti))
2715 return BLK_MQ_RQ_QUEUE_BUSY;
2716
2717 dm_start_request(md, rq);
2718
2719 /* Init tio using md established in .init_request */
2720 init_tio(tio, rq, md);
2721
Mike Snitzer02233342015-03-10 23:49:26 -04002722 /*
2723 * Establish tio->ti before queuing work (map_tio_request)
2724 * or making direct call to map_request().
2725 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002726 tio->ti = ti;
Mike Snitzer02233342015-03-10 23:49:26 -04002727
Mike Snitzerc5248f72016-02-20 14:02:49 -05002728 /* Direct call is fine since .queue_rq allows allocations */
2729 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
2730 /* Undo dm_start_request() before requeuing */
2731 rq_end_stats(md, rq);
2732 rq_completed(md, rq_data_dir(rq), false);
2733 return BLK_MQ_RQ_QUEUE_BUSY;
Mike Snitzer02233342015-03-10 23:49:26 -04002734 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002735
2736 return BLK_MQ_RQ_QUEUE_OK;
2737}
2738
2739static struct blk_mq_ops dm_mq_ops = {
2740 .queue_rq = dm_mq_queue_rq,
2741 .map_queue = blk_mq_map_queue,
2742 .complete = dm_softirq_done,
2743 .init_request = dm_mq_init_request,
2744};
2745
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002746static int dm_mq_init_request_queue(struct mapped_device *md,
2747 struct dm_target *immutable_tgt)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002748{
2749 struct request_queue *q;
2750 int err;
2751
Mike Snitzerc5248f72016-02-20 14:02:49 -05002752 if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) {
2753 DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
2754 return -EINVAL;
2755 }
2756
Mike Snitzer115485e2016-02-22 12:16:21 -05002757 md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
Mike Snitzer1c357a12016-02-06 17:01:17 -05002758 if (!md->tag_set)
2759 return -ENOMEM;
2760
2761 md->tag_set->ops = &dm_mq_ops;
2762 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
Mike Snitzer115485e2016-02-22 12:16:21 -05002763 md->tag_set->numa_node = md->numa_node_id;
Mike Snitzer1c357a12016-02-06 17:01:17 -05002764 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2765 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
2766 md->tag_set->driver_data = md;
2767
2768 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002769 if (immutable_tgt && immutable_tgt->per_io_data_size) {
2770 /* any target-specific per-io data is immediately after the tio */
2771 md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
2772 md->init_tio_pdu = true;
2773 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002774
Mike Snitzer1c357a12016-02-06 17:01:17 -05002775 err = blk_mq_alloc_tag_set(md->tag_set);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002776 if (err)
Mike Snitzer1c357a12016-02-06 17:01:17 -05002777 goto out_kfree_tag_set;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002778
Mike Snitzer1c357a12016-02-06 17:01:17 -05002779 q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002780 if (IS_ERR(q)) {
2781 err = PTR_ERR(q);
2782 goto out_tag_set;
2783 }
2784 md->queue = q;
2785 dm_init_md_queue(md);
2786
2787 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
2788 blk_mq_register_disk(md->disk);
2789
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002790 return 0;
2791
2792out_tag_set:
Mike Snitzer1c357a12016-02-06 17:01:17 -05002793 blk_mq_free_tag_set(md->tag_set);
2794out_kfree_tag_set:
2795 kfree(md->tag_set);
2796
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002797 return err;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002798}
2799
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002800static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2801{
2802 if (type == DM_TYPE_BIO_BASED)
2803 return type;
2804
2805 return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2806}
2807
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002808/*
2809 * Setup the DM device's queue based on md's type
2810 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002811int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002812{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002813 int r;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002814 unsigned md_type = filter_md_type(dm_get_md_type(md), md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002815
2816 switch (md_type) {
2817 case DM_TYPE_REQUEST_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05002818 r = dm_old_init_request_queue(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002819 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002820 DMERR("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002821 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002822 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002823 break;
2824 case DM_TYPE_MQ_REQUEST_BASED:
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002825 r = dm_mq_init_request_queue(md, dm_table_get_immutable_target(t));
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002826 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002827 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002828 return r;
2829 }
2830 break;
2831 case DM_TYPE_BIO_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05002832 dm_init_normal_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002833 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04002834 /*
2835 * DM handles splitting bios as needed. Free the bio_split bioset
2836 * since it won't be used (saves 1 process per bio-based DM device).
2837 */
2838 bioset_free(md->queue->bio_split);
2839 md->queue->bio_split = NULL;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002840 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002841 }
2842
2843 return 0;
2844}
2845
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002846struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 unsigned minor = MINOR(dev);
2850
2851 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2852 return NULL;
2853
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002854 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002857 if (md) {
2858 if ((md == MINOR_ALLOCED ||
2859 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2860 dm_deleting_md(md) ||
2861 test_bit(DMF_FREEING, &md->flags))) {
2862 md = NULL;
2863 goto out;
2864 }
2865 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002868out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002869 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
David Teigland637842c2006-01-06 00:20:00 -08002871 return md;
2872}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002873EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002874
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002875void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002876{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002877 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
2880void dm_set_mdptr(struct mapped_device *md, void *ptr)
2881{
2882 md->interface_ptr = ptr;
2883}
2884
2885void dm_get(struct mapped_device *md)
2886{
2887 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002888 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002891int dm_hold(struct mapped_device *md)
2892{
2893 spin_lock(&_minor_lock);
2894 if (test_bit(DMF_FREEING, &md->flags)) {
2895 spin_unlock(&_minor_lock);
2896 return -EBUSY;
2897 }
2898 dm_get(md);
2899 spin_unlock(&_minor_lock);
2900 return 0;
2901}
2902EXPORT_SYMBOL_GPL(dm_hold);
2903
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002904const char *dm_device_name(struct mapped_device *md)
2905{
2906 return md->name;
2907}
2908EXPORT_SYMBOL_GPL(dm_device_name);
2909
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002910static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002912 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002913 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002915 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002916
Mike Snitzer63a4f062015-03-23 17:01:43 -04002917 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002918 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2919 set_bit(DMF_FREEING, &md->flags);
2920 spin_unlock(&_minor_lock);
2921
Mike Snitzer02233342015-03-10 23:49:26 -04002922 if (dm_request_based(md) && md->kworker_task)
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002923 flush_kthread_worker(&md->kworker);
2924
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002925 /*
2926 * Take suspend_lock so that presuspend and postsuspend methods
2927 * do not race with internal suspend.
2928 */
2929 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002930 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002931 if (!dm_suspended_md(md)) {
2932 dm_table_presuspend_targets(map);
2933 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002935 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2936 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002937 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002938
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002939 /*
2940 * Rare, but there may be I/O requests still going to complete,
2941 * for example. Wait for all references to disappear.
2942 * No one should increment the reference count of the mapped_device,
2943 * after the mapped_device state becomes DMF_FREEING.
2944 */
2945 if (wait)
2946 while (atomic_read(&md->holders))
2947 msleep(1);
2948 else if (atomic_read(&md->holders))
2949 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2950 dm_device_name(md), atomic_read(&md->holders));
2951
2952 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002953 dm_table_destroy(__unbind(md));
2954 free_dev(md);
2955}
2956
2957void dm_destroy(struct mapped_device *md)
2958{
2959 __dm_destroy(md, true);
2960}
2961
2962void dm_destroy_immediate(struct mapped_device *md)
2963{
2964 __dm_destroy(md, false);
2965}
2966
2967void dm_put(struct mapped_device *md)
2968{
2969 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
Edward Goggin79eb8852007-05-09 02:32:56 -07002971EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Mikulas Patocka401600d2009-04-02 19:55:38 +01002973static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002974{
2975 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002976 DECLARE_WAITQUEUE(wait, current);
2977
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002978 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002979
2980 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002981 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002982
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002983 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002984 break;
2985
Mikulas Patocka401600d2009-04-02 19:55:38 +01002986 if (interruptible == TASK_INTERRUPTIBLE &&
2987 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002988 r = -EINTR;
2989 break;
2990 }
2991
2992 io_schedule();
2993 }
2994 set_current_state(TASK_RUNNING);
2995
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002996 remove_wait_queue(&md->wait, &wait);
2997
Milan Broz46125c12008-02-08 02:10:30 +00002998 return r;
2999}
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001/*
3002 * Process the deferred bios
3003 */
Mikulas Patockaef208582009-04-02 19:55:38 +01003004static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Mikulas Patockaef208582009-04-02 19:55:38 +01003006 struct mapped_device *md = container_of(work, struct mapped_device,
3007 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00003008 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003009 int srcu_idx;
3010 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003012 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01003013
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003014 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01003015 spin_lock_irq(&md->deferred_lock);
3016 c = bio_list_pop(&md->deferred);
3017 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01003018
Tejun Heo6a8736d2010-09-08 18:07:00 +02003019 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01003020 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01003021
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003022 if (dm_request_based(md))
3023 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02003024 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003025 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01003026 }
Milan Broz73d410c2008-02-08 02:10:25 +00003027
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003028 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003031static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00003032{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003033 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003034 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01003035 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00003036}
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003039 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003041struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Mike Christie87eb5b22013-03-01 22:45:48 +00003043 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003044 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003045 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Daniel Walkere61290a2008-02-08 02:10:08 +00003047 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003050 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Mike Snitzer3ae70652012-09-26 23:45:45 +01003053 /*
3054 * If the new table has no data devices, retain the existing limits.
3055 * This helps multipath with queue_if_no_path if all paths disappear,
3056 * then new I/O is queued based on these limits, and then some paths
3057 * reappear.
3058 */
3059 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003060 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003061 if (live_map)
3062 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003063 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003064 }
3065
Mike Christie87eb5b22013-03-01 22:45:48 +00003066 if (!live_map) {
3067 r = dm_calculate_queue_limits(table, &limits);
3068 if (r) {
3069 map = ERR_PTR(r);
3070 goto out;
3071 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003072 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003073
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003074 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003076out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003077 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003078 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
3081/*
3082 * Functions to lock and unlock any filesystem running on the
3083 * device.
3084 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003085static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003087 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003090
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003091 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003092 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003093 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003094 md->frozen_sb = NULL;
3095 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003096 }
3097
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003098 set_bit(DMF_FROZEN, &md->flags);
3099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return 0;
3101}
3102
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003103static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003105 if (!test_bit(DMF_FROZEN, &md->flags))
3106 return;
3107
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003108 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003110 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
3113/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04003114 * If __dm_suspend returns 0, the device is completely quiescent
3115 * now. There is no request-processing activity. All new requests
3116 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003117 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04003118 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003119 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003120static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3121 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003123 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3124 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3125 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003127 /*
3128 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
3129 * This flag is cleared before dm_suspend returns.
3130 */
3131 if (noflush)
3132 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
3133
Mike Snitzerd67ee212014-10-28 20:13:31 -04003134 /*
3135 * This gets reverted if there's an error later and the targets
3136 * provide the .presuspend_undo hook.
3137 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003138 dm_table_presuspend_targets(map);
3139
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003140 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003141 * Flush I/O to the device.
3142 * Any I/O submitted after lock_fs() may not be flushed.
3143 * noflush takes precedence over do_lockfs.
3144 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003145 */
3146 if (!noflush && do_lockfs) {
3147 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003148 if (r) {
3149 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003150 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04003151 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003155 * Here we must make sure that no processes are submitting requests
3156 * to target drivers i.e. no one may be executing
3157 * __split_and_process_bio. This is called from dm_request and
3158 * dm_wq_work.
3159 *
3160 * To get all processes out of __split_and_process_bio in dm_request,
3161 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02003162 * __split_and_process_bio from dm_request and quiesce the thread
3163 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
3164 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01003166 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003167 if (map)
3168 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003170 /*
Tejun Heo29e40132010-09-08 18:07:00 +02003171 * Stop md->queue before flushing md->wq in case request-based
3172 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003173 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003174 if (dm_request_based(md)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05003175 dm_stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04003176 if (md->kworker_task)
3177 flush_kthread_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003178 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003179
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003180 flush_workqueue(md->wq);
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003183 * At this point no more requests are entering target request routines.
3184 * We call dm_wait_for_completion to wait for all existing requests
3185 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003187 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Milan Broz6d6f10d2008-02-08 02:10:22 +00003189 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01003190 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003191 if (map)
3192 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00003195 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003196 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00003197
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003198 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05003199 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003200
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003201 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003202 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003203 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003204 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003205
Mike Snitzerffcc3932014-10-28 18:34:52 -04003206 return r;
3207}
3208
3209/*
3210 * We need to be able to change a mapping table under a mounted
3211 * filesystem. For example we might want to move some data in
3212 * the background. Before the table can be swapped with
3213 * dm_bind_table, dm_suspend must be called to flush any in
3214 * flight bios and ensure that any further io gets deferred.
3215 */
3216/*
3217 * Suspend mechanism in request-based dm.
3218 *
3219 * 1. Flush all I/Os by lock_fs() if needed.
3220 * 2. Stop dispatching any I/O by stopping the request_queue.
3221 * 3. Wait for all in-flight I/Os to be completed or requeued.
3222 *
3223 * To abort suspend, start the request_queue.
3224 */
3225int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3226{
3227 struct dm_table *map = NULL;
3228 int r = 0;
3229
3230retry:
3231 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3232
3233 if (dm_suspended_md(md)) {
3234 r = -EINVAL;
3235 goto out_unlock;
3236 }
3237
3238 if (dm_suspended_internally_md(md)) {
3239 /* already internally suspended, wait for internal resume */
3240 mutex_unlock(&md->suspend_lock);
3241 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3242 if (r)
3243 return r;
3244 goto retry;
3245 }
3246
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003247 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003248
3249 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3250 if (r)
3251 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 set_bit(DMF_SUSPENDED, &md->flags);
3254
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00003255 dm_table_postsuspend_targets(map);
3256
Alasdair G Kergond2874832006-11-08 17:44:43 -08003257out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00003258 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003259 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Mike Snitzerffcc3932014-10-28 18:34:52 -04003262static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003264 if (map) {
3265 int r = dm_table_resume_targets(map);
3266 if (r)
3267 return r;
3268 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003269
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003270 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003271
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003272 /*
3273 * Flushing deferred I/Os must be done after targets are resumed
3274 * so that mapping of targets can work correctly.
3275 * Request-based dm is queueing the deferred I/Os in its request_queue.
3276 */
3277 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05003278 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003279
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003280 unlock_fs(md);
3281
Mike Snitzerffcc3932014-10-28 18:34:52 -04003282 return 0;
3283}
3284
3285int dm_resume(struct mapped_device *md)
3286{
3287 int r = -EINVAL;
3288 struct dm_table *map = NULL;
3289
3290retry:
3291 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3292
3293 if (!dm_suspended_md(md))
3294 goto out;
3295
3296 if (dm_suspended_internally_md(md)) {
3297 /* already internally suspended, wait for internal resume */
3298 mutex_unlock(&md->suspend_lock);
3299 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3300 if (r)
3301 return r;
3302 goto retry;
3303 }
3304
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003305 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003306 if (!map || !dm_table_get_size(map))
3307 goto out;
3308
3309 r = __dm_resume(md, map);
3310 if (r)
3311 goto out;
3312
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003313 clear_bit(DMF_SUSPENDED, &md->flags);
3314
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003315 r = 0;
3316out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003317 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003318
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003319 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003322/*
3323 * Internal suspend/resume works like userspace-driven suspend. It waits
3324 * until all bios finish and prevents issuing new bios to the target drivers.
3325 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003326 */
3327
Mike Snitzerffcc3932014-10-28 18:34:52 -04003328static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3329{
3330 struct dm_table *map = NULL;
3331
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003332 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003333 return; /* nested internal suspend */
3334
3335 if (dm_suspended_md(md)) {
3336 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3337 return; /* nest suspend */
3338 }
3339
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003340 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003341
3342 /*
3343 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3344 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3345 * would require changing .presuspend to return an error -- avoid this
3346 * until there is a need for more elaborate variants of internal suspend.
3347 */
3348 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3349
3350 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3351
3352 dm_table_postsuspend_targets(map);
3353}
3354
3355static void __dm_internal_resume(struct mapped_device *md)
3356{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003357 BUG_ON(!md->internal_suspend_count);
3358
3359 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003360 return; /* resume from nested internal suspend */
3361
3362 if (dm_suspended_md(md))
3363 goto done; /* resume from nested suspend */
3364
3365 /*
3366 * NOTE: existing callers don't need to call dm_table_resume_targets
3367 * (which may fail -- so best to avoid it for now by passing NULL map)
3368 */
3369 (void) __dm_resume(md, NULL);
3370
3371done:
3372 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3373 smp_mb__after_atomic();
3374 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3375}
3376
3377void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003378{
3379 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003380 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3381 mutex_unlock(&md->suspend_lock);
3382}
3383EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3384
3385void dm_internal_resume(struct mapped_device *md)
3386{
3387 mutex_lock(&md->suspend_lock);
3388 __dm_internal_resume(md);
3389 mutex_unlock(&md->suspend_lock);
3390}
3391EXPORT_SYMBOL_GPL(dm_internal_resume);
3392
3393/*
3394 * Fast variants of internal suspend/resume hold md->suspend_lock,
3395 * which prevents interaction with userspace-driven suspend.
3396 */
3397
3398void dm_internal_suspend_fast(struct mapped_device *md)
3399{
3400 mutex_lock(&md->suspend_lock);
3401 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003402 return;
3403
3404 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3405 synchronize_srcu(&md->io_barrier);
3406 flush_workqueue(md->wq);
3407 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3408}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003409EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003410
Mike Snitzerffcc3932014-10-28 18:34:52 -04003411void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003412{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003413 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003414 goto done;
3415
3416 dm_queue_flush(md);
3417
3418done:
3419 mutex_unlock(&md->suspend_lock);
3420}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003421EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423/*-----------------------------------------------------------------
3424 * Event notification.
3425 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003426int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003427 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003428{
Milan Broz60935eb2009-06-22 10:12:30 +01003429 char udev_cookie[DM_COOKIE_LENGTH];
3430 char *envp[] = { udev_cookie, NULL };
3431
3432 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003433 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003434 else {
3435 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3436 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003437 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3438 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003439 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003440}
3441
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003442uint32_t dm_next_uevent_seq(struct mapped_device *md)
3443{
3444 return atomic_add_return(1, &md->uevent_seq);
3445}
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447uint32_t dm_get_event_nr(struct mapped_device *md)
3448{
3449 return atomic_read(&md->event_nr);
3450}
3451
3452int dm_wait_event(struct mapped_device *md, int event_nr)
3453{
3454 return wait_event_interruptible(md->eventq,
3455 (event_nr != atomic_read(&md->event_nr)));
3456}
3457
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003458void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3459{
3460 unsigned long flags;
3461
3462 spin_lock_irqsave(&md->uevent_lock, flags);
3463 list_add(elist, &md->uevent_list);
3464 spin_unlock_irqrestore(&md->uevent_lock, flags);
3465}
3466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467/*
3468 * The gendisk is only valid as long as you have a reference
3469 * count on 'md'.
3470 */
3471struct gendisk *dm_disk(struct mapped_device *md)
3472{
3473 return md->disk;
3474}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00003475EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Milan Broz784aae72009-01-06 03:05:12 +00003477struct kobject *dm_kobject(struct mapped_device *md)
3478{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003479 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003480}
3481
Milan Broz784aae72009-01-06 03:05:12 +00003482struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3483{
3484 struct mapped_device *md;
3485
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003486 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003487
Milan Broz4d89b7b2009-06-22 10:12:11 +01003488 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003489 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003490 return NULL;
3491
Milan Broz784aae72009-01-06 03:05:12 +00003492 dm_get(md);
3493 return md;
3494}
3495
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003496int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
3498 return test_bit(DMF_SUSPENDED, &md->flags);
3499}
3500
Mike Snitzerffcc3932014-10-28 18:34:52 -04003501int dm_suspended_internally_md(struct mapped_device *md)
3502{
3503 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3504}
3505
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003506int dm_test_deferred_remove_flag(struct mapped_device *md)
3507{
3508 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3509}
3510
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003511int dm_suspended(struct dm_target *ti)
3512{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003513 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003514}
3515EXPORT_SYMBOL_GPL(dm_suspended);
3516
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003517int dm_noflush_suspending(struct dm_target *ti)
3518{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003519 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003520}
3521EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3522
Mike Snitzer78d8e582015-06-26 10:01:13 -04003523struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
Mike Snitzer30187e12016-01-31 13:28:26 -05003524 unsigned integrity, unsigned per_io_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003525{
Mike Snitzer115485e2016-02-22 12:16:21 -05003526 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
Mike Snitzer78d8e582015-06-26 10:01:13 -04003527 struct kmem_cache *cachep = NULL;
3528 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003529 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003530
3531 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003532 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003533
Mike Snitzer78d8e582015-06-26 10:01:13 -04003534 type = filter_md_type(type, md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04003535
Mike Snitzer78d8e582015-06-26 10:01:13 -04003536 switch (type) {
3537 case DM_TYPE_BIO_BASED:
3538 cachep = _io_cache;
3539 pool_size = dm_get_reserved_bio_based_ios();
Mike Snitzer30187e12016-01-31 13:28:26 -05003540 front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04003541 break;
3542 case DM_TYPE_REQUEST_BASED:
3543 cachep = _rq_tio_cache;
3544 pool_size = dm_get_reserved_rq_based_ios();
3545 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3546 if (!pools->rq_pool)
3547 goto out;
3548 /* fall through to setup remaining rq-based pools */
3549 case DM_TYPE_MQ_REQUEST_BASED:
3550 if (!pool_size)
3551 pool_size = dm_get_reserved_rq_based_ios();
3552 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05003553 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04003554 break;
3555 default:
3556 BUG();
3557 }
3558
3559 if (cachep) {
3560 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3561 if (!pools->io_pool)
3562 goto out;
3563 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003564
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003565 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003566 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003567 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003568
Martin K. Petersena91a2782011-03-17 11:11:05 +01003569 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003570 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003571
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003572 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003573
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003574out:
3575 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003576
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003577 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003578}
3579
3580void dm_free_md_mempools(struct dm_md_mempools *pools)
3581{
3582 if (!pools)
3583 return;
3584
Julia Lawall6f659852015-09-13 14:15:05 +02003585 mempool_destroy(pools->io_pool);
3586 mempool_destroy(pools->rq_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003587
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003588 if (pools->bs)
3589 bioset_free(pools->bs);
3590
3591 kfree(pools);
3592}
3593
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003594static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003595 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003596{
3597 struct mapped_device *md = bdev->bd_disk->private_data;
3598 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003599 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003600 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003601
Mike Snitzer956a4022016-02-18 16:13:51 -05003602 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003603 if (r < 0)
3604 return r;
3605
3606 ops = bdev->bd_disk->fops->pr_ops;
3607 if (ops && ops->pr_register)
3608 r = ops->pr_register(bdev, old_key, new_key, flags);
3609 else
3610 r = -EOPNOTSUPP;
3611
Mike Snitzer956a4022016-02-18 16:13:51 -05003612 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003613 return r;
3614}
3615
3616static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05003617 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003618{
3619 struct mapped_device *md = bdev->bd_disk->private_data;
3620 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003621 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003622 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003623
Mike Snitzer956a4022016-02-18 16:13:51 -05003624 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003625 if (r < 0)
3626 return r;
3627
3628 ops = bdev->bd_disk->fops->pr_ops;
3629 if (ops && ops->pr_reserve)
3630 r = ops->pr_reserve(bdev, key, type, flags);
3631 else
3632 r = -EOPNOTSUPP;
3633
Mike Snitzer956a4022016-02-18 16:13:51 -05003634 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003635 return r;
3636}
3637
3638static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3639{
3640 struct mapped_device *md = bdev->bd_disk->private_data;
3641 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003642 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003643 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003644
Mike Snitzer956a4022016-02-18 16:13:51 -05003645 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003646 if (r < 0)
3647 return r;
3648
3649 ops = bdev->bd_disk->fops->pr_ops;
3650 if (ops && ops->pr_release)
3651 r = ops->pr_release(bdev, key, type);
3652 else
3653 r = -EOPNOTSUPP;
3654
Mike Snitzer956a4022016-02-18 16:13:51 -05003655 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003656 return r;
3657}
3658
3659static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003660 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003661{
3662 struct mapped_device *md = bdev->bd_disk->private_data;
3663 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003664 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003665 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003666
Mike Snitzer956a4022016-02-18 16:13:51 -05003667 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003668 if (r < 0)
3669 return r;
3670
3671 ops = bdev->bd_disk->fops->pr_ops;
3672 if (ops && ops->pr_preempt)
3673 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3674 else
3675 r = -EOPNOTSUPP;
3676
Mike Snitzer956a4022016-02-18 16:13:51 -05003677 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003678 return r;
3679}
3680
3681static int dm_pr_clear(struct block_device *bdev, u64 key)
3682{
3683 struct mapped_device *md = bdev->bd_disk->private_data;
3684 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003685 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003686 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003687
Mike Snitzer956a4022016-02-18 16:13:51 -05003688 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003689 if (r < 0)
3690 return r;
3691
3692 ops = bdev->bd_disk->fops->pr_ops;
3693 if (ops && ops->pr_clear)
3694 r = ops->pr_clear(bdev, key);
3695 else
3696 r = -EOPNOTSUPP;
3697
Mike Snitzer956a4022016-02-18 16:13:51 -05003698 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003699 return r;
3700}
3701
3702static const struct pr_ops dm_pr_ops = {
3703 .pr_register = dm_pr_register,
3704 .pr_reserve = dm_pr_reserve,
3705 .pr_release = dm_pr_release,
3706 .pr_preempt = dm_pr_preempt,
3707 .pr_clear = dm_pr_clear,
3708};
3709
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003710static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 .open = dm_blk_open,
3712 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003713 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003714 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003715 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 .owner = THIS_MODULE
3717};
3718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719/*
3720 * module hooks
3721 */
3722module_init(dm_init);
3723module_exit(dm_exit);
3724
3725module_param(major, uint, 0);
3726MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003727
Mike Snitzere8603132013-09-12 18:06:12 -04003728module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3729MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3730
Mike Snitzerf4790822013-09-12 18:06:12 -04003731module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3732MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3733
Mike Snitzer17e149b2015-03-11 15:01:09 -04003734module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
3735MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
3736
Mike Snitzerfaad87d2016-01-28 16:52:56 -05003737module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
3738MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
3739
3740module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
3741MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");
3742
Mike Snitzer115485e2016-02-22 12:16:21 -05003743module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3744MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746MODULE_DESCRIPTION(DM_NAME " driver");
3747MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3748MODULE_LICENSE("GPL");