blob: aba7ed9abb3ab774dfcac7d710a50290888cf230 [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
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100130 /*
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -0500131 * The current mapping (struct dm_table *).
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100132 * Use dm_get_live_table{_fast} or take suspend_lock for
133 * dereference.
134 */
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -0500135 void __rcu *map;
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100136
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500137 struct list_head table_devices;
138 struct mutex table_devices_lock;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned long flags;
141
Jens Axboe165125e2007-07-24 09:28:11 +0200142 struct request_queue *queue;
Mike Snitzer115485e2016-02-22 12:16:21 -0500143 int numa_node_id;
144
Mike Snitzera5664da2010-08-12 04:14:01 +0100145 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100146 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100147 struct mutex type_lock;
148
Mike Snitzer032482f2016-02-22 15:27:50 -0500149 atomic_t holders;
150 atomic_t open_count;
151
Mike Snitzer16f12262016-01-31 17:22:27 -0500152 struct dm_target *immutable_target;
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000153 struct target_type *immutable_target_type;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800156 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 void *interface_ptr;
159
160 /*
161 * A list of ios that arrived while we were suspended.
162 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200163 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100165 struct work_struct work;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100166 spinlock_t deferred_lock;
Mike Snitzer032482f2016-02-22 15:27:50 -0500167 struct bio_list deferred;
168
169 /*
170 * Event handling.
171 */
172 wait_queue_head_t eventq;
173 atomic_t event_nr;
174 atomic_t uevent_seq;
175 struct list_head uevent_list;
176 spinlock_t uevent_lock; /* Protect access to uevent_list */
177
178 /* the number of internal suspends */
179 unsigned internal_suspend_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200182 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000183 */
184 struct workqueue_struct *wq;
185
186 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * io objects are allocated from here.
188 */
189 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500190 mempool_t *rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Stefan Bader9faf4002006-10-03 01:15:41 -0700192 struct bio_set *bs;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * freeze/thaw support require holding onto a super block
196 */
197 struct super_block *frozen_sb;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800198
199 /* forced geometry settings */
200 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000201
Mike Snitzer032482f2016-02-22 15:27:50 -0500202 struct block_device *bdev;
203
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500204 /* kobject and completion */
205 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500206
Tejun Heod87f4c12010-09-03 11:56:19 +0200207 /* zero-length flush that will be cloned and submitted to targets */
208 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400209
210 struct dm_stats stats;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600211
212 struct kthread_worker kworker;
213 struct task_struct *kworker_task;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500214
215 /* for request-based merge heuristic in dm_request_fn() */
Mike Snitzer0ce65792015-02-26 00:50:28 -0500216 unsigned seq_rq_merge_deadline_usecs;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500217 int last_rq_rw;
Mike Snitzer0ce65792015-02-26 00:50:28 -0500218 sector_t last_rq_pos;
219 ktime_t last_rq_start_time;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -0500220
221 /* for blk-mq request-based DM support */
Mike Snitzer1c357a12016-02-06 17:01:17 -0500222 struct blk_mq_tag_set *tag_set;
Mike Snitzer591ddcf2016-01-31 12:05:42 -0500223 bool use_blk_mq:1;
224 bool init_tio_pdu:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
Mike Snitzer17e149b2015-03-11 15:01:09 -0400227#ifdef CONFIG_DM_MQ_DEFAULT
228static bool use_blk_mq = true;
229#else
230static bool use_blk_mq = false;
231#endif
232
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500233#define DM_MQ_NR_HW_QUEUES 1
234#define DM_MQ_QUEUE_DEPTH 2048
Mike Snitzer115485e2016-02-22 12:16:21 -0500235#define DM_NUMA_NODE NUMA_NO_NODE
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500236
237static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
238static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
Mike Snitzer115485e2016-02-22 12:16:21 -0500239static int dm_numa_node = DM_NUMA_NODE;
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500240
Mike Snitzer17e149b2015-03-11 15:01:09 -0400241bool dm_use_blk_mq(struct mapped_device *md)
242{
243 return md->use_blk_mq;
244}
Mike Snitzer591ddcf2016-01-31 12:05:42 -0500245EXPORT_SYMBOL_GPL(dm_use_blk_mq);
Mike Snitzer17e149b2015-03-11 15:01:09 -0400246
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100247/*
248 * For mempools pre-allocation at the table loading time.
249 */
250struct dm_md_mempools {
251 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500252 mempool_t *rq_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100253 struct bio_set *bs;
254};
255
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500256struct table_device {
257 struct list_head list;
258 atomic_t count;
259 struct dm_dev dm_dev;
260};
261
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400262#define RESERVED_BIO_BASED_IOS 16
263#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400264#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800265static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000266static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500267static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700268
Mike Snitzerf4790822013-09-12 18:06:12 -0400269/*
Mike Snitzere8603132013-09-12 18:06:12 -0400270 * Bio-based DM's mempools' reserved IOs set by the user.
271 */
272static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
273
274/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400275 * Request-based DM's mempools' reserved IOs set by the user.
276 */
277static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
278
Mike Snitzer115485e2016-02-22 12:16:21 -0500279static int __dm_get_module_param_int(int *module_param, int min, int max)
280{
281 int param = ACCESS_ONCE(*module_param);
282 int modified_param = 0;
283 bool modified = true;
284
285 if (param < min)
286 modified_param = min;
287 else if (param > max)
288 modified_param = max;
289 else
290 modified = false;
291
292 if (modified) {
293 (void)cmpxchg(module_param, param, modified_param);
294 param = modified_param;
295 }
296
297 return param;
298}
299
Mike Snitzer09c2d532015-02-27 22:25:26 -0500300static unsigned __dm_get_module_param(unsigned *module_param,
Mike Snitzerf4790822013-09-12 18:06:12 -0400301 unsigned def, unsigned max)
302{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500303 unsigned param = ACCESS_ONCE(*module_param);
304 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400305
Mike Snitzer09c2d532015-02-27 22:25:26 -0500306 if (!param)
307 modified_param = def;
308 else if (param > max)
309 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400310
Mike Snitzer09c2d532015-02-27 22:25:26 -0500311 if (modified_param) {
312 (void)cmpxchg(module_param, param, modified_param);
313 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400314 }
315
Mike Snitzer09c2d532015-02-27 22:25:26 -0500316 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400317}
318
Mike Snitzere8603132013-09-12 18:06:12 -0400319unsigned dm_get_reserved_bio_based_ios(void)
320{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500321 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzere8603132013-09-12 18:06:12 -0400322 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
323}
324EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
325
Mike Snitzerf4790822013-09-12 18:06:12 -0400326unsigned dm_get_reserved_rq_based_ios(void)
327{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500328 return __dm_get_module_param(&reserved_rq_based_ios,
Mike Snitzerf4790822013-09-12 18:06:12 -0400329 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
330}
331EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
332
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500333static unsigned dm_get_blk_mq_nr_hw_queues(void)
334{
335 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
336}
337
338static unsigned dm_get_blk_mq_queue_depth(void)
339{
340 return __dm_get_module_param(&dm_mq_queue_depth,
341 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
342}
343
Mike Snitzer115485e2016-02-22 12:16:21 -0500344static unsigned dm_get_numa_node(void)
345{
346 return __dm_get_module_param_int(&dm_numa_node,
347 DM_NUMA_NODE, num_online_nodes() - 1);
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static int __init local_init(void)
351{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100352 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100355 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100357 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000359 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
360 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100361 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000362
Mike Snitzereca7ee62016-02-20 13:45:38 -0500363 _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500364 __alignof__(struct request), 0, NULL);
365 if (!_rq_cache)
366 goto out_free_rq_tio_cache;
367
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100368 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100369 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500370 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100371
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400372 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
373 if (!deferred_remove_workqueue) {
374 r = -ENOMEM;
375 goto out_uevent_exit;
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 _major = major;
379 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100380 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400381 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (!_major)
384 _major = r;
385
386 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100387
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400388out_free_workqueue:
389 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100390out_uevent_exit:
391 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500392out_free_rq_cache:
393 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000394out_free_rq_tio_cache:
395 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100396out_free_io_cache:
397 kmem_cache_destroy(_io_cache);
398
399 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static void local_exit(void)
403{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400404 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400405 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400406
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500407 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000408 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700410 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100411 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 _major = 0;
414
415 DMINFO("cleaned up");
416}
417
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000418static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 local_init,
420 dm_target_init,
421 dm_linear_init,
422 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000423 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100424 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400426 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000429static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 local_exit,
431 dm_target_exit,
432 dm_linear_exit,
433 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000434 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100435 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400437 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438};
439
440static int __init dm_init(void)
441{
442 const int count = ARRAY_SIZE(_inits);
443
444 int r, i;
445
446 for (i = 0; i < count; i++) {
447 r = _inits[i]();
448 if (r)
449 goto bad;
450 }
451
452 return 0;
453
454 bad:
455 while (i--)
456 _exits[i]();
457
458 return r;
459}
460
461static void __exit dm_exit(void)
462{
463 int i = ARRAY_SIZE(_exits);
464
465 while (i--)
466 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100467
468 /*
469 * Should be empty by this point.
470 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100471 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474/*
475 * Block device functions
476 */
Mike Anderson432a2122009-12-10 23:52:20 +0000477int dm_deleting_md(struct mapped_device *md)
478{
479 return test_bit(DMF_DELETING, &md->flags);
480}
481
Al Virofe5f9f22008-03-02 10:29:31 -0500482static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct mapped_device *md;
485
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700486 spin_lock(&_minor_lock);
487
Al Virofe5f9f22008-03-02 10:29:31 -0500488 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700489 if (!md)
490 goto out;
491
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700492 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000493 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700494 md = NULL;
495 goto out;
496 }
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700499 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700500out:
501 spin_unlock(&_minor_lock);
502
503 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Al Virodb2a1442013-05-05 21:52:57 -0400506static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400508 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200509
Milan Broz4a1aeb92011-01-13 19:59:48 +0000510 spin_lock(&_minor_lock);
511
Mike Snitzer63a4f062015-03-23 17:01:43 -0400512 md = disk->private_data;
513 if (WARN_ON(!md))
514 goto out;
515
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400516 if (atomic_dec_and_test(&md->open_count) &&
517 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400518 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400521out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000522 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700525int dm_open_count(struct mapped_device *md)
526{
527 return atomic_read(&md->open_count);
528}
529
530/*
531 * Guarantees nothing is using the device before it's deleted.
532 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400533int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700534{
535 int r = 0;
536
537 spin_lock(&_minor_lock);
538
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400539 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700540 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400541 if (mark_deferred)
542 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
543 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
544 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700545 else
546 set_bit(DMF_DELETING, &md->flags);
547
548 spin_unlock(&_minor_lock);
549
550 return r;
551}
552
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400553int dm_cancel_deferred_remove(struct mapped_device *md)
554{
555 int r = 0;
556
557 spin_lock(&_minor_lock);
558
559 if (test_bit(DMF_DELETING, &md->flags))
560 r = -EBUSY;
561 else
562 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
563
564 spin_unlock(&_minor_lock);
565
566 return r;
567}
568
569static void do_deferred_remove(struct work_struct *w)
570{
571 dm_deferred_remove();
572}
573
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400574sector_t dm_get_size(struct mapped_device *md)
575{
576 return get_capacity(md->disk);
577}
578
Mike Snitzer9974fa22014-02-28 15:33:43 +0100579struct request_queue *dm_get_md_queue(struct mapped_device *md)
580{
581 return md->queue;
582}
583
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400584struct dm_stats *dm_get_stats(struct mapped_device *md)
585{
586 return &md->stats;
587}
588
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800589static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
590{
591 struct mapped_device *md = bdev->bd_disk->private_data;
592
593 return dm_get_geometry(md, geo);
594}
595
Mike Snitzer956a4022016-02-18 16:13:51 -0500596static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
597 struct block_device **bdev,
598 fmode_t *mode)
Milan Brozaa129a22006-10-03 01:15:15 -0700599{
Mike Snitzer66482022016-02-18 15:44:39 -0500600 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100601 struct dm_table *map;
Mike Snitzer956a4022016-02-18 16:13:51 -0500602 int srcu_idx, r;
Milan Brozaa129a22006-10-03 01:15:15 -0700603
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100604retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200605 r = -ENOTTY;
Mike Snitzer956a4022016-02-18 16:13:51 -0500606 map = dm_get_live_table(md, &srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700607 if (!map || !dm_table_get_size(map))
608 goto out;
609
610 /* We only support devices that have a single target */
611 if (dm_table_get_num_targets(map) != 1)
612 goto out;
613
Mike Snitzer66482022016-02-18 15:44:39 -0500614 tgt = dm_table_get_target(map, 0);
615 if (!tgt->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500616 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700617
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000618 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700619 r = -EAGAIN;
620 goto out;
621 }
622
Mike Snitzer66482022016-02-18 15:44:39 -0500623 r = tgt->type->prepare_ioctl(tgt, bdev, mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200624 if (r < 0)
625 goto out;
626
Mike Snitzer956a4022016-02-18 16:13:51 -0500627 bdgrab(*bdev);
628 dm_put_live_table(md, srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200629 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700630
631out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500632 dm_put_live_table(md, srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000633 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100634 msleep(10);
635 goto retry;
636 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200637 return r;
638}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100639
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200640static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
641 unsigned int cmd, unsigned long arg)
642{
643 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer956a4022016-02-18 16:13:51 -0500644 int r;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200645
Mike Snitzer956a4022016-02-18 16:13:51 -0500646 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200647 if (r < 0)
648 return r;
649
650 if (r > 0) {
651 /*
652 * Target determined this ioctl is being issued against
653 * a logical partition of the parent bdev; so extra
654 * validation is needed.
655 */
656 r = scsi_verify_blk_ioctl(NULL, cmd);
657 if (r)
658 goto out;
659 }
660
Mike Snitzer66482022016-02-18 15:44:39 -0500661 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200662out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500663 bdput(bdev);
Milan Brozaa129a22006-10-03 01:15:15 -0700664 return r;
665}
666
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100667static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 return mempool_alloc(md->io_pool, GFP_NOIO);
670}
671
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100672static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 mempool_free(io, md->io_pool);
675}
676
Mike Snitzercfae7522016-04-11 12:05:38 -0400677static void free_tio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Mikulas Patockadba14162012-10-12 21:02:15 +0100679 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Mike Snitzereca7ee62016-02-20 13:45:38 -0500682static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md,
683 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100684{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000685 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100686}
687
Mike Snitzereca7ee62016-02-20 13:45:38 -0500688static void free_old_rq_tio(struct dm_rq_target_io *tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100689{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000690 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100691}
692
Mike Snitzereca7ee62016-02-20 13:45:38 -0500693static struct request *alloc_old_clone_request(struct mapped_device *md,
694 gfp_t gfp_mask)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500695{
696 return mempool_alloc(md->rq_pool, gfp_mask);
697}
698
Mike Snitzereca7ee62016-02-20 13:45:38 -0500699static void free_old_clone_request(struct mapped_device *md, struct request *rq)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500700{
701 mempool_free(rq, md->rq_pool);
702}
703
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000704static int md_in_flight(struct mapped_device *md)
705{
706 return atomic_read(&md->pending[READ]) +
707 atomic_read(&md->pending[WRITE]);
708}
709
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800710static void start_io_acct(struct dm_io *io)
711{
712 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400713 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900714 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400715 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800716
717 io->start_time = jiffies;
718
Tejun Heo074a7ac2008-08-25 19:56:14 +0900719 cpu = part_stat_lock();
720 part_round_stats(cpu, &dm_disk(md)->part0);
721 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100722 atomic_set(&dm_disk(md)->part0.in_flight[rw],
723 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400724
725 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500726 dm_stats_account_io(&md->stats, bio_data_dir(bio),
727 bio->bi_iter.bi_sector, bio_sectors(bio),
728 false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800729}
730
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000731static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800732{
733 struct mapped_device *md = io->md;
734 struct bio *bio = io->bio;
735 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800736 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800737 int rw = bio_data_dir(bio);
738
Gu Zheng18c0b222014-11-24 11:05:26 +0800739 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800740
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400741 if (unlikely(dm_stats_used(&md->stats)))
Mike Christie528ec5a2016-06-05 14:32:03 -0500742 dm_stats_account_io(&md->stats, bio_data_dir(bio),
743 bio->bi_iter.bi_sector, bio_sectors(bio),
744 true, duration, &io->stats_aux);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400745
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100746 /*
747 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200748 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100749 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100750 pending = atomic_dec_return(&md->pending[rw]);
751 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200752 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800753
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000754 /* nudge anyone waiting on suspend queue */
755 if (!pending)
756 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
760 * Add the bio to the list of deferred io.
761 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100762static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200764 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200766 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200768 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200769 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772/*
773 * Everyone (including functions in this file), should use this
774 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100775 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100777struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100779 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100781 return srcu_dereference(md->map, &md->io_barrier);
782}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100784void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
785{
786 srcu_read_unlock(&md->io_barrier, srcu_idx);
787}
788
789void dm_sync_table(struct mapped_device *md)
790{
791 synchronize_srcu(&md->io_barrier);
792 synchronize_rcu_expedited();
793}
794
795/*
796 * A fast alternative to dm_get_live_table/dm_put_live_table.
797 * The caller must not block between these two functions.
798 */
799static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
800{
801 rcu_read_lock();
802 return rcu_dereference(md->map);
803}
804
805static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
806{
807 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800810/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500811 * Open a table device so we can use it as a map destination.
812 */
813static int open_table_device(struct table_device *td, dev_t dev,
814 struct mapped_device *md)
815{
816 static char *_claim_ptr = "I belong to device-mapper";
817 struct block_device *bdev;
818
819 int r;
820
821 BUG_ON(td->dm_dev.bdev);
822
823 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
824 if (IS_ERR(bdev))
825 return PTR_ERR(bdev);
826
827 r = bd_link_disk_holder(bdev, dm_disk(md));
828 if (r) {
829 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
830 return r;
831 }
832
833 td->dm_dev.bdev = bdev;
834 return 0;
835}
836
837/*
838 * Close a table device that we've been using.
839 */
840static void close_table_device(struct table_device *td, struct mapped_device *md)
841{
842 if (!td->dm_dev.bdev)
843 return;
844
845 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
846 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
847 td->dm_dev.bdev = NULL;
848}
849
850static struct table_device *find_table_device(struct list_head *l, dev_t dev,
851 fmode_t mode) {
852 struct table_device *td;
853
854 list_for_each_entry(td, l, list)
855 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
856 return td;
857
858 return NULL;
859}
860
861int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
862 struct dm_dev **result) {
863 int r;
864 struct table_device *td;
865
866 mutex_lock(&md->table_devices_lock);
867 td = find_table_device(&md->table_devices, dev, mode);
868 if (!td) {
Mike Snitzer115485e2016-02-22 12:16:21 -0500869 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500870 if (!td) {
871 mutex_unlock(&md->table_devices_lock);
872 return -ENOMEM;
873 }
874
875 td->dm_dev.mode = mode;
876 td->dm_dev.bdev = NULL;
877
878 if ((r = open_table_device(td, dev, md))) {
879 mutex_unlock(&md->table_devices_lock);
880 kfree(td);
881 return r;
882 }
883
884 format_dev_t(td->dm_dev.name, dev);
885
886 atomic_set(&td->count, 0);
887 list_add(&td->list, &md->table_devices);
888 }
889 atomic_inc(&td->count);
890 mutex_unlock(&md->table_devices_lock);
891
892 *result = &td->dm_dev;
893 return 0;
894}
895EXPORT_SYMBOL_GPL(dm_get_table_device);
896
897void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
898{
899 struct table_device *td = container_of(d, struct table_device, dm_dev);
900
901 mutex_lock(&md->table_devices_lock);
902 if (atomic_dec_and_test(&td->count)) {
903 close_table_device(td, md);
904 list_del(&td->list);
905 kfree(td);
906 }
907 mutex_unlock(&md->table_devices_lock);
908}
909EXPORT_SYMBOL(dm_put_table_device);
910
911static void free_table_devices(struct list_head *devices)
912{
913 struct list_head *tmp, *next;
914
915 list_for_each_safe(tmp, next, devices) {
916 struct table_device *td = list_entry(tmp, struct table_device, list);
917
918 DMWARN("dm_destroy: %s still exists with %d references",
919 td->dm_dev.name, atomic_read(&td->count));
920 kfree(td);
921 }
922}
923
924/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800925 * Get the geometry associated with a dm device
926 */
927int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
928{
929 *geo = md->geometry;
930
931 return 0;
932}
933
934/*
935 * Set the geometry of a device.
936 */
937int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
938{
939 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
940
941 if (geo->start > sz) {
942 DMWARN("Start sector is beyond the geometry limits.");
943 return -EINVAL;
944 }
945
946 md->geometry = *geo;
947
948 return 0;
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*-----------------------------------------------------------------
952 * CRUD START:
953 * A more elegant soln is in the works that uses the queue
954 * merge fn, unfortunately there are a couple of changes to
955 * the block layer that I want to make for this. So in the
956 * interests of getting something for people to use I give
957 * you this clearly demarcated crap.
958 *---------------------------------------------------------------*/
959
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800960static int __noflush_suspending(struct mapped_device *md)
961{
962 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/*
966 * Decrements the number of outstanding ios that a bio has been
967 * cloned into, completing the original io if necc.
968 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800969static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800971 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000972 int io_error;
973 struct bio *bio;
974 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800975
976 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100977 if (unlikely(error)) {
978 spin_lock_irqsave(&io->endio_lock, flags);
979 if (!(io->error > 0 && __noflush_suspending(md)))
980 io->error = error;
981 spin_unlock_irqrestore(&io->endio_lock, flags);
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800985 if (io->error == DM_ENDIO_REQUEUE) {
986 /*
987 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800988 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100989 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200990 if (__noflush_suspending(md))
991 bio_list_add_head(&md->deferred, io->bio);
992 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800993 /* noflush suspend was interrupted. */
994 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100995 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800996 }
997
Milan Brozb35f8ca2009-03-16 17:44:36 +0000998 io_error = io->error;
999 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +02001000 end_io_acct(io);
1001 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +01001002
Tejun Heo6a8736d2010-09-08 18:07:00 +02001003 if (io_error == DM_ENDIO_REQUEUE)
1004 return;
1005
Mike Christie28a8f0d2016-06-05 14:32:25 -05001006 if ((bio->bi_rw & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001007 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +02001008 * Preflush done for flush with data, reissue
Mike Christie28a8f0d2016-06-05 14:32:25 -05001009 * without REQ_PREFLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001010 */
Mike Christie28a8f0d2016-06-05 14:32:25 -05001011 bio->bi_rw &= ~REQ_PREFLUSH;
Tejun Heo6a8736d2010-09-08 18:07:00 +02001012 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001013 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +02001014 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07001015 trace_block_bio_complete(md->queue, bio, io_error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001016 bio->bi_error = io_error;
1017 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020}
1021
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001022static void disable_write_same(struct mapped_device *md)
1023{
1024 struct queue_limits *limits = dm_get_queue_limits(md);
1025
1026 /* device doesn't really support WRITE SAME, disable it */
1027 limits->max_write_same_sectors = 0;
1028}
1029
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001030static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001032 int error = bio->bi_error;
zhendong chen5164bec2014-12-17 14:37:04 +08001033 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -05001034 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001035 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -07001036 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 dm_endio_fn endio = tio->ti->type->end_io;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001040 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001041 if (r < 0 || r == DM_ENDIO_REQUEUE)
1042 /*
1043 * error and requeue request are handled
1044 * in dec_pending().
1045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001047 else if (r == DM_ENDIO_INCOMPLETE)
1048 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +02001049 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001050 else if (r) {
1051 DMWARN("unimplemented target endio return value: %d", r);
1052 BUG();
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Mike Christiee6047142016-06-05 14:32:04 -05001056 if (unlikely(r == -EREMOTEIO && (bio_op(bio) == REQ_OP_WRITE_SAME) &&
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001057 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
1058 disable_write_same(md);
1059
Mike Snitzercfae7522016-04-11 12:05:38 -04001060 free_tio(tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001061 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Mike Snitzer78d8e582015-06-26 10:01:13 -04001064/*
1065 * Partial completion handling for request-based dm
1066 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001067static void end_clone_bio(struct bio *clone)
Mike Snitzer78d8e582015-06-26 10:01:13 -04001068{
1069 struct dm_rq_clone_bio_info *info =
1070 container_of(clone, struct dm_rq_clone_bio_info, clone);
1071 struct dm_rq_target_io *tio = info->tio;
1072 struct bio *bio = info->orig;
1073 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001074 int error = clone->bi_error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001075
1076 bio_put(clone);
1077
1078 if (tio->error)
1079 /*
1080 * An error has already been detected on the request.
1081 * Once error occurred, just let clone->end_io() handle
1082 * the remainder.
1083 */
1084 return;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001085 else if (error) {
Mike Snitzer78d8e582015-06-26 10:01:13 -04001086 /*
1087 * Don't notice the error to the upper layer yet.
1088 * The error handling decision is made by the target driver,
1089 * when the request is completed.
1090 */
Junichi Nomura50887bd2015-10-06 04:19:54 +00001091 tio->error = error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001092 return;
1093 }
1094
1095 /*
1096 * I/O for the bio successfully completed.
1097 * Notice the data completion to the upper layer.
1098 */
1099
1100 /*
1101 * bios are processed from the head of the list.
1102 * So the completing bio should always be rq->bio.
1103 * If it's not, something wrong is happening.
1104 */
1105 if (tio->orig->bio != bio)
1106 DMERR("bio completion is going in the middle of the request");
1107
1108 /*
1109 * Update the original request.
1110 * Do not use blk_end_request() here, because it may complete
1111 * the original request before the clone, and break the ordering.
1112 */
1113 blk_update_request(tio->orig, 0, nr_bytes);
1114}
1115
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001116static struct dm_rq_target_io *tio_from_request(struct request *rq)
1117{
1118 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1119}
1120
Mikulas Patockae262f342015-06-09 17:22:49 -04001121static void rq_end_stats(struct mapped_device *md, struct request *orig)
1122{
1123 if (unlikely(dm_stats_used(&md->stats))) {
1124 struct dm_rq_target_io *tio = tio_from_request(orig);
1125 tio->duration_jiffies = jiffies - tio->duration_jiffies;
Mike Christie528ec5a2016-06-05 14:32:03 -05001126 dm_stats_account_io(&md->stats, rq_data_dir(orig),
1127 blk_rq_pos(orig), tio->n_sectors, true,
1128 tio->duration_jiffies, &tio->stats_aux);
Mikulas Patockae262f342015-06-09 17:22:49 -04001129 }
1130}
1131
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001132/*
1133 * Don't touch any member of the md after calling this function because
1134 * the md may be freed in dm_put() at the end of this function.
1135 * Or do dm_get() before calling this function and dm_put() later.
1136 */
Keith Busch466d89a2014-10-17 17:46:37 -06001137static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001138{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001139 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001140
1141 /* nudge anyone waiting on suspend queue */
Mike Snitzer621739b2015-07-08 16:08:24 -04001142 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001143 wake_up(&md->wait);
1144
Jens Axboea8c32a52012-11-06 12:24:26 +01001145 /*
1146 * Run this off this callpath, as drivers could invoke end_io while
1147 * inside their request_fn (and holding the queue lock). Calling
1148 * back into ->request_fn() could deadlock attempting to grab the
1149 * queue lock again.
1150 */
Mike Snitzer6acfe682016-02-05 08:49:01 -05001151 if (!md->queue->mq_ops && run_queue)
1152 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001153
1154 /*
1155 * dm_put() must be at the end of this function. See the comment above
1156 */
1157 dm_put(md);
1158}
1159
Mike Snitzere5d8de32015-05-28 15:12:52 -04001160static void free_rq_clone(struct request *clone)
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001161{
1162 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001163 struct mapped_device *md = tio->md;
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001164
Mike Snitzer78d8e582015-06-26 10:01:13 -04001165 blk_rq_unprep_clone(clone);
1166
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001167 if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1168 /* stacked on blk-mq queue(s) */
Mike Snitzere5863d92014-12-17 21:08:12 -05001169 tio->ti->type->release_clone_rq(clone);
Mike Snitzer02233342015-03-10 23:49:26 -04001170 else if (!md->queue->mq_ops)
1171 /* request_fn queue stacked on request_fn queue(s) */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001172 free_old_clone_request(md, clone);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001173
1174 if (!md->queue->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001175 free_old_rq_tio(tio);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001176}
1177
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001178/*
1179 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001180 * Must be called without clone's queue lock held,
1181 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001182 */
1183static void dm_end_request(struct request *clone, int error)
1184{
1185 int rw = rq_data_dir(clone);
1186 struct dm_rq_target_io *tio = clone->end_io_data;
1187 struct mapped_device *md = tio->md;
1188 struct request *rq = tio->orig;
1189
Tejun Heo29e40132010-09-08 18:07:00 +02001190 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001191 rq->errors = clone->errors;
1192 rq->resid_len = clone->resid_len;
1193
1194 if (rq->sense)
1195 /*
1196 * We are using the sense buffer of the original
1197 * request.
1198 * So setting the length of the sense data is enough.
1199 */
1200 rq->sense_len = clone->sense_len;
1201 }
1202
Mike Snitzere5d8de32015-05-28 15:12:52 -04001203 free_rq_clone(clone);
Mikulas Patockae262f342015-06-09 17:22:49 -04001204 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001205 if (!rq->q->mq_ops)
1206 blk_end_request_all(rq, error);
1207 else
1208 blk_mq_end_request(rq, error);
Tejun Heo29e40132010-09-08 18:07:00 +02001209 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001210}
1211
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001212static void dm_unprep_request(struct request *rq)
1213{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001214 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001215 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001216
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001217 if (!rq->q->mq_ops) {
1218 rq->special = NULL;
1219 rq->cmd_flags &= ~REQ_DONTPREP;
1220 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001221
Mike Snitzere5863d92014-12-17 21:08:12 -05001222 if (clone)
Mike Snitzere5d8de32015-05-28 15:12:52 -04001223 free_rq_clone(clone);
Mike Snitzer4328daa2016-02-21 19:09:22 -05001224 else if (!tio->md->queue->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001225 free_old_rq_tio(tio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001226}
1227
1228/*
1229 * Requeue the original request of a clone.
1230 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001231static void dm_old_requeue_request(struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001232{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001233 struct request_queue *q = rq->q;
1234 unsigned long flags;
1235
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001236 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001237 blk_requeue_request(q, rq);
Junichi Nomura4ae99442015-05-26 08:25:54 +00001238 blk_run_queue_async(q);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001239 spin_unlock_irqrestore(q->queue_lock, flags);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001240}
1241
Mike Snitzer818c5f32016-02-20 00:38:47 -05001242static void dm_mq_requeue_request(struct request *rq)
1243{
1244 struct request_queue *q = rq->q;
1245 unsigned long flags;
1246
1247 blk_mq_requeue_request(rq);
1248 spin_lock_irqsave(q->queue_lock, flags);
1249 if (!blk_queue_stopped(q))
1250 blk_mq_kick_requeue_list(q);
1251 spin_unlock_irqrestore(q->queue_lock, flags);
1252}
1253
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001254static void dm_requeue_original_request(struct mapped_device *md,
1255 struct request *rq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001256{
1257 int rw = rq_data_dir(rq);
1258
Bryn M. Reeves98dbc9c2016-03-14 17:04:34 -04001259 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001260 dm_unprep_request(rq);
1261
1262 if (!rq->q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001263 dm_old_requeue_request(rq);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001264 else
1265 dm_mq_requeue_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001266
Keith Busch466d89a2014-10-17 17:46:37 -06001267 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001268}
Keith Busch466d89a2014-10-17 17:46:37 -06001269
Mike Snitzereca7ee62016-02-20 13:45:38 -05001270static void dm_old_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001271{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001272 unsigned long flags;
1273
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001274 spin_lock_irqsave(q->queue_lock, flags);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001275 if (blk_queue_stopped(q)) {
1276 spin_unlock_irqrestore(q->queue_lock, flags);
1277 return;
1278 }
1279
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001280 blk_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001281 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001282}
1283
Mike Snitzereca7ee62016-02-20 13:45:38 -05001284static void dm_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001285{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001286 if (!q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001287 dm_old_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001288 else
1289 blk_mq_stop_hw_queues(q);
1290}
1291
Mike Snitzereca7ee62016-02-20 13:45:38 -05001292static void dm_old_start_queue(struct request_queue *q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001293{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001294 unsigned long flags;
1295
1296 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001297 if (blk_queue_stopped(q))
1298 blk_start_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001299 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001300}
1301
Mike Snitzereca7ee62016-02-20 13:45:38 -05001302static void dm_start_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001303{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001304 if (!q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001305 dm_old_start_queue(q);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001306 else {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001307 blk_mq_start_stopped_hw_queues(q, true);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001308 blk_mq_kick_requeue_list(q);
1309 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001310}
1311
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001312static void dm_done(struct request *clone, int error, bool mapped)
1313{
1314 int r = error;
1315 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001316 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001317
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001318 if (tio->ti) {
1319 rq_end_io = tio->ti->type->rq_end_io;
1320
1321 if (mapped && rq_end_io)
1322 r = rq_end_io(tio->ti, clone, error, &tio->info);
1323 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001324
Mike Christiec2df40d2016-06-05 14:32:17 -05001325 if (unlikely(r == -EREMOTEIO && (req_op(clone) == REQ_OP_WRITE_SAME) &&
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001326 !clone->q->limits.max_write_same_sectors))
1327 disable_write_same(tio->md);
1328
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001329 if (r <= 0)
1330 /* The target wants to complete the I/O */
1331 dm_end_request(clone, r);
1332 else if (r == DM_ENDIO_INCOMPLETE)
1333 /* The target will handle the I/O */
1334 return;
1335 else if (r == DM_ENDIO_REQUEUE)
1336 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001337 dm_requeue_original_request(tio->md, tio->orig);
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001338 else {
1339 DMWARN("unimplemented target endio return value: %d", r);
1340 BUG();
1341 }
1342}
1343
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001344/*
1345 * Request completion handler for request-based dm
1346 */
1347static void dm_softirq_done(struct request *rq)
1348{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001349 bool mapped = true;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001350 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001351 struct request *clone = tio->clone;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001352 int rw;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001353
Mike Snitzere5863d92014-12-17 21:08:12 -05001354 if (!clone) {
Mikulas Patockae262f342015-06-09 17:22:49 -04001355 rq_end_stats(tio->md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001356 rw = rq_data_dir(rq);
1357 if (!rq->q->mq_ops) {
1358 blk_end_request_all(rq, tio->error);
1359 rq_completed(tio->md, rw, false);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001360 free_old_rq_tio(tio);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001361 } else {
1362 blk_mq_end_request(rq, tio->error);
1363 rq_completed(tio->md, rw, false);
1364 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001365 return;
1366 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001367
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001368 if (rq->cmd_flags & REQ_FAILED)
1369 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001370
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001371 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001372}
1373
1374/*
1375 * Complete the clone and the original request with the error status
1376 * through softirq context.
1377 */
Keith Busch466d89a2014-10-17 17:46:37 -06001378static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001379{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001380 struct dm_rq_target_io *tio = tio_from_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001381
1382 tio->error = error;
Mike Snitzer6acfe682016-02-05 08:49:01 -05001383 if (!rq->q->mq_ops)
1384 blk_complete_request(rq);
1385 else
1386 blk_mq_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001387}
1388
1389/*
1390 * Complete the not-mapped clone and the original request with the error status
1391 * through softirq context.
1392 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001393 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001394 */
Keith Busch466d89a2014-10-17 17:46:37 -06001395static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001396{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001397 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001398 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001399}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001400
1401/*
Mike Snitzereca7ee62016-02-20 13:45:38 -05001402 * Called with the clone's queue lock held (in the case of .request_fn)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001403 */
1404static void end_clone_request(struct request *clone, int error)
1405{
Keith Busch466d89a2014-10-17 17:46:37 -06001406 struct dm_rq_target_io *tio = clone->end_io_data;
1407
Mike Snitzere5863d92014-12-17 21:08:12 -05001408 if (!clone->q->mq_ops) {
1409 /*
1410 * For just cleaning up the information of the queue in which
1411 * the clone was dispatched.
1412 * The clone is *NOT* freed actually here because it is alloced
1413 * from dm own mempool (REQ_ALLOCED isn't set).
1414 */
1415 __blk_put_request(clone->q, clone);
1416 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001417
1418 /*
1419 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001420 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001421 * - another request may be submitted by the upper level driver
1422 * of the stacking during the completion
1423 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001424 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001425 */
Keith Busch466d89a2014-10-17 17:46:37 -06001426 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001427}
1428
Mike Snitzer56a67df2010-08-12 04:14:10 +01001429/*
1430 * Return maximum size of I/O possible at the supplied sector up to the current
1431 * target boundary.
1432 */
1433static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001435 sector_t target_offset = dm_target_offset(ti, sector);
1436
1437 return ti->len - target_offset;
1438}
1439
1440static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1441{
1442 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001443 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001446 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001448 if (ti->max_io_len) {
1449 offset = dm_target_offset(ti, sector);
1450 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1451 max_len = sector_div(offset, ti->max_io_len);
1452 else
1453 max_len = offset & (ti->max_io_len - 1);
1454 max_len = ti->max_io_len - max_len;
1455
1456 if (len > max_len)
1457 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
1460 return len;
1461}
1462
Mike Snitzer542f9032012-07-27 15:08:00 +01001463int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1464{
1465 if (len > UINT_MAX) {
1466 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1467 (unsigned long long)len, UINT_MAX);
1468 ti->error = "Maximum size of target IO is too large";
1469 return -EINVAL;
1470 }
1471
1472 ti->max_io_len = (uint32_t) len;
1473
1474 return 0;
1475}
1476EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1477
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001478/*
1479 * A target may call dm_accept_partial_bio only from the map routine. It is
Mike Christie28a8f0d2016-06-05 14:32:25 -05001480 * allowed for all bio types except REQ_PREFLUSH.
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001481 *
1482 * dm_accept_partial_bio informs the dm that the target only wants to process
1483 * additional n_sectors sectors of the bio and the rest of the data should be
1484 * sent in a next bio.
1485 *
1486 * A diagram that explains the arithmetics:
1487 * +--------------------+---------------+-------+
1488 * | 1 | 2 | 3 |
1489 * +--------------------+---------------+-------+
1490 *
1491 * <-------------- *tio->len_ptr --------------->
1492 * <------- bi_size ------->
1493 * <-- n_sectors -->
1494 *
1495 * Region 1 was already iterated over with bio_advance or similar function.
1496 * (it may be empty if the target doesn't use bio_advance)
1497 * Region 2 is the remaining bio size that the target wants to process.
1498 * (it may be empty if region 1 is non-empty, although there is no reason
1499 * to make it empty)
1500 * The target requires that region 3 is to be sent in the next bio.
1501 *
1502 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1503 * the partially processed part (the sum of regions 1+2) must be the same for all
1504 * copies of the bio.
1505 */
1506void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1507{
1508 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1509 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Mike Christie28a8f0d2016-06-05 14:32:25 -05001510 BUG_ON(bio->bi_rw & REQ_PREFLUSH);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001511 BUG_ON(bi_size > *tio->len_ptr);
1512 BUG_ON(n_sectors > bi_size);
1513 *tio->len_ptr -= bi_size - n_sectors;
1514 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1515}
1516EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1517
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001518static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001521 sector_t sector;
Mikulas Patockadba14162012-10-12 21:02:15 +01001522 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001523 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /*
1528 * Map the clone. If r == 0 we don't need to do
1529 * anything, the target has assumed ownership of
1530 * this io.
1531 */
1532 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001533 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001534 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001535 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001537
Mike Snitzerd07335e2010-11-16 12:52:38 +01001538 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1539 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001542 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1543 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001544 dec_pending(tio->io, r);
Mike Snitzercfae7522016-04-11 12:05:38 -04001545 free_tio(tio);
Mikulas Patockaab378442015-07-01 17:30:36 -04001546 } else if (r != DM_MAPIO_SUBMITTED) {
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001547 DMWARN("unimplemented target map return value: %d", r);
1548 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550}
1551
1552struct clone_info {
1553 struct mapped_device *md;
1554 struct dm_table *map;
1555 struct bio *bio;
1556 struct dm_io *io;
1557 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001558 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559};
1560
Mikulas Patockae0d66092014-03-14 18:40:39 -04001561static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001562{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001563 bio->bi_iter.bi_sector = sector;
1564 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
1568 * Creates a bio that consists of range of complete bvecs.
1569 */
Mike Snitzerc80914e2016-03-02 12:33:03 -05001570static int clone_bio(struct dm_target_io *tio, struct bio *bio,
1571 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Mikulas Patockadba14162012-10-12 21:02:15 +01001573 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001575 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Mike Snitzerc80914e2016-03-02 12:33:03 -05001577 if (bio_integrity(bio)) {
1578 int r = bio_integrity_clone(clone, bio, GFP_NOIO);
1579 if (r < 0)
1580 return r;
1581 }
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001582
1583 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1584 clone->bi_iter.bi_size = to_bytes(len);
1585
1586 if (bio_integrity(bio))
1587 bio_integrity_trim(clone, 0, len);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001588
1589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001592static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001593 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001594 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001595{
Mikulas Patockadba14162012-10-12 21:02:15 +01001596 struct dm_target_io *tio;
1597 struct bio *clone;
1598
Junichi Nomura99778272014-10-03 11:55:16 +00001599 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001600 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001601
1602 tio->io = ci->io;
1603 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001604 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001605
1606 return tio;
1607}
1608
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001609static void __clone_and_map_simple_bio(struct clone_info *ci,
1610 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001611 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001612{
Junichi Nomura99778272014-10-03 11:55:16 +00001613 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001614 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001615
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001616 tio->len_ptr = len;
1617
Junichi Nomura99778272014-10-03 11:55:16 +00001618 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001619 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001620 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001621
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001622 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001623}
1624
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001625static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001626 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001627{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001628 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001629
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001630 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001631 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001632}
1633
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001634static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001635{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001636 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001637 struct dm_target *ti;
1638
Mike Snitzerb372d362010-09-08 18:07:01 +02001639 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001640 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001641 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001642
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001643 return 0;
1644}
1645
Mike Snitzerc80914e2016-03-02 12:33:03 -05001646static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001647 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001648{
Mikulas Patockadba14162012-10-12 21:02:15 +01001649 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001650 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001651 unsigned target_bio_nr;
1652 unsigned num_target_bios = 1;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001653 int r = 0;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001654
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001655 /*
1656 * Does the target want to receive duplicate copies of the bio?
1657 */
1658 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1659 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001660
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001661 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001662 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001663 tio->len_ptr = len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001664 r = clone_bio(tio, bio, sector, *len);
Mikulas Patocka072623d2016-04-09 12:48:18 -04001665 if (r < 0) {
Mike Snitzercfae7522016-04-11 12:05:38 -04001666 free_tio(tio);
Mike Snitzerc80914e2016-03-02 12:33:03 -05001667 break;
Mikulas Patocka072623d2016-04-09 12:48:18 -04001668 }
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001669 __map_bio(tio);
1670 }
Mike Snitzerc80914e2016-03-02 12:33:03 -05001671
1672 return r;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001673}
1674
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001675typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001676
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001677static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001678{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001679 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001680}
1681
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001682static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001683{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001684 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001685}
1686
1687typedef bool (*is_split_required_fn)(struct dm_target *ti);
1688
1689static bool is_split_required_for_discard(struct dm_target *ti)
1690{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001691 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001692}
1693
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001694static int __send_changing_extent_only(struct clone_info *ci,
1695 get_num_bios_fn get_num_bios,
1696 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001697{
1698 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001699 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001700 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001701
Mike Snitzera79245b2010-08-12 04:14:24 +01001702 do {
1703 ti = dm_table_find_target(ci->map, ci->sector);
1704 if (!dm_target_is_valid(ti))
1705 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001706
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001707 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001708 * Even though the device advertised support for this type of
1709 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001710 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001711 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001712 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001713 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1714 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001715 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001716
Mike Snitzer23508a92012-12-21 20:23:37 +00001717 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001718 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001719 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001720 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001721
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001722 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001723
1724 ci->sector += len;
1725 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001726
1727 return 0;
1728}
1729
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001730static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001731{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001732 return __send_changing_extent_only(ci, get_num_discard_bios,
1733 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001734}
1735
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001736static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001737{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001738 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001739}
1740
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001741/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001742 * Select the correct strategy for processing a non-flush bio.
1743 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001744static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Mikulas Patockadba14162012-10-12 21:02:15 +01001746 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001747 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001748 unsigned len;
Mike Snitzerc80914e2016-03-02 12:33:03 -05001749 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Mike Christiee6047142016-06-05 14:32:04 -05001751 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001752 return __send_discard(ci);
Mike Christiee6047142016-06-05 14:32:04 -05001753 else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001754 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001755
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001756 ti = dm_table_find_target(ci->map, ci->sector);
1757 if (!dm_target_is_valid(ti))
1758 return -EIO;
1759
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001760 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001761
Mike Snitzerc80914e2016-03-02 12:33:03 -05001762 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1763 if (r < 0)
1764 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001766 ci->sector += len;
1767 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001773 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001775static void __split_and_process_bio(struct mapped_device *md,
1776 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001779 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001781 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001782 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001783 return;
1784 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001785
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001786 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 ci.io = alloc_io(md);
1789 ci.io->error = 0;
1790 atomic_set(&ci.io->io_count, 1);
1791 ci.io->bio = bio;
1792 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001793 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001794 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001796 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001797
Mike Christie28a8f0d2016-06-05 14:32:25 -05001798 if (bio->bi_rw & REQ_PREFLUSH) {
Mike Snitzerb372d362010-09-08 18:07:01 +02001799 ci.bio = &ci.md->flush_bio;
1800 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001801 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001802 /* dec_pending submits any data associated with flush */
1803 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001804 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001805 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001806 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001807 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001811 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813/*-----------------------------------------------------------------
1814 * CRUD END
1815 *---------------------------------------------------------------*/
1816
1817/*
1818 * The request function that just remaps the bio built up by
1819 * dm_merge_bvec.
1820 */
Jens Axboedece1632015-11-05 10:41:16 -07001821static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Kevin Corry12f03a42006-02-01 03:04:52 -08001823 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001825 int srcu_idx;
1826 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001828 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Gu Zheng18c0b222014-11-24 11:05:26 +08001830 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001831
Tejun Heo6a8736d2010-09-08 18:07:00 +02001832 /* if we're suspended, we have to queue this io for later */
1833 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001834 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Tejun Heo6a8736d2010-09-08 18:07:00 +02001836 if (bio_rw(bio) != READA)
1837 queue_io(md, bio);
1838 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001839 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001840 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001843 __split_and_process_bio(md, map, bio);
1844 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001845 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001846}
1847
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001848int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001849{
1850 return blk_queue_stackable(md->queue);
1851}
1852
Keith Busch466d89a2014-10-17 17:46:37 -06001853static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001854{
1855 int r;
1856
Keith Busch466d89a2014-10-17 17:46:37 -06001857 if (blk_queue_io_stat(clone->q))
1858 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001859
Keith Busch466d89a2014-10-17 17:46:37 -06001860 clone->start_time = jiffies;
1861 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001862 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001863 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001864 dm_complete_request(rq, r);
1865}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001866
Mike Snitzer78d8e582015-06-26 10:01:13 -04001867static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1868 void *data)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001869{
Mike Snitzer78d8e582015-06-26 10:01:13 -04001870 struct dm_rq_target_io *tio = data;
1871 struct dm_rq_clone_bio_info *info =
1872 container_of(bio, struct dm_rq_clone_bio_info, clone);
1873
1874 info->orig = bio_orig;
1875 info->tio = tio;
1876 bio->bi_end_io = end_clone_bio;
1877
1878 return 0;
1879}
1880
1881static int setup_clone(struct request *clone, struct request *rq,
1882 struct dm_rq_target_io *tio, gfp_t gfp_mask)
1883{
1884 int r;
1885
1886 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1887 dm_rq_bio_constructor, tio);
1888 if (r)
1889 return r;
1890
1891 clone->cmd = rq->cmd;
1892 clone->cmd_len = rq->cmd_len;
1893 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001894 clone->end_io = end_clone_request;
1895 clone->end_io_data = tio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001896
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001897 tio->clone = clone;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001898
1899 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001900}
1901
Mike Snitzereca7ee62016-02-20 13:45:38 -05001902static struct request *clone_old_rq(struct request *rq, struct mapped_device *md,
1903 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001904{
Mike Snitzer02233342015-03-10 23:49:26 -04001905 /*
Mike Snitzerc5248f72016-02-20 14:02:49 -05001906 * Create clone for use with .request_fn request_queue
Mike Snitzer02233342015-03-10 23:49:26 -04001907 */
Mike Snitzer02233342015-03-10 23:49:26 -04001908 struct request *clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001909
Mike Snitzereca7ee62016-02-20 13:45:38 -05001910 clone = alloc_old_clone_request(md, gfp_mask);
Mike Snitzerc5248f72016-02-20 14:02:49 -05001911 if (!clone)
1912 return NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001913
1914 blk_rq_init(NULL, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001915 if (setup_clone(clone, rq, tio, gfp_mask)) {
1916 /* -ENOMEM */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001917 free_old_clone_request(md, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001918 return NULL;
1919 }
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001920
1921 return clone;
1922}
1923
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001924static void map_tio_request(struct kthread_work *work);
1925
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001926static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1927 struct mapped_device *md)
1928{
1929 tio->md = md;
1930 tio->ti = NULL;
1931 tio->clone = NULL;
1932 tio->orig = rq;
1933 tio->error = 0;
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001934 /*
1935 * Avoid initializing info for blk-mq; it passes
1936 * target-specific data through info.ptr
1937 * (see: dm_mq_init_request)
1938 */
1939 if (!md->init_tio_pdu)
1940 memset(&tio->info, 0, sizeof(tio->info));
Mike Snitzer02233342015-03-10 23:49:26 -04001941 if (md->kworker_task)
1942 init_kthread_work(&tio->work, map_tio_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001943}
1944
Mike Snitzereca7ee62016-02-20 13:45:38 -05001945static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq,
1946 struct mapped_device *md,
1947 gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001948{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001949 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001950 int srcu_idx;
1951 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001952
Mike Snitzereca7ee62016-02-20 13:45:38 -05001953 tio = alloc_old_rq_tio(md, gfp_mask);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001954 if (!tio)
1955 return NULL;
1956
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001957 init_tio(tio, rq, md);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001958
Mike Snitzere5863d92014-12-17 21:08:12 -05001959 table = dm_get_live_table(md, &srcu_idx);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001960 /*
1961 * Must clone a request if this .request_fn DM device
1962 * is stacked on .request_fn device(s).
1963 */
Mike Snitzere5863d92014-12-17 21:08:12 -05001964 if (!dm_table_mq_request_based(table)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001965 if (!clone_old_rq(rq, md, tio, gfp_mask)) {
Mike Snitzere5863d92014-12-17 21:08:12 -05001966 dm_put_live_table(md, srcu_idx);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001967 free_old_rq_tio(tio);
Mike Snitzere5863d92014-12-17 21:08:12 -05001968 return NULL;
1969 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001970 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001971 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001972
Keith Busch466d89a2014-10-17 17:46:37 -06001973 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001974}
1975
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001976/*
1977 * Called with the queue lock held.
1978 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001979static int dm_old_prep_fn(struct request_queue *q, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001980{
1981 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001982 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001983
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001984 if (unlikely(rq->special)) {
1985 DMWARN("Already has something in rq->special.");
1986 return BLKPREP_KILL;
1987 }
1988
Mike Snitzereca7ee62016-02-20 13:45:38 -05001989 tio = dm_old_prep_tio(rq, md, GFP_ATOMIC);
Keith Busch466d89a2014-10-17 17:46:37 -06001990 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001991 return BLKPREP_DEFER;
1992
Keith Busch466d89a2014-10-17 17:46:37 -06001993 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001994 rq->cmd_flags |= REQ_DONTPREP;
1995
1996 return BLKPREP_OK;
1997}
1998
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001999/*
2000 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05002001 * 0 : the request has been processed
2002 * DM_MAPIO_REQUEUE : the original request needs to be requeued
2003 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00002004 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002005static int map_request(struct dm_rq_target_io *tio, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00002006 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002007{
Mike Snitzere5863d92014-12-17 21:08:12 -05002008 int r;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002009 struct dm_target *ti = tio->ti;
Mike Snitzere5863d92014-12-17 21:08:12 -05002010 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002011
Mike Snitzere5863d92014-12-17 21:08:12 -05002012 if (tio->clone) {
2013 clone = tio->clone;
2014 r = ti->type->map_rq(ti, clone, &tio->info);
2015 } else {
2016 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
2017 if (r < 0) {
2018 /* The target wants to complete the I/O */
2019 dm_kill_unmapped_request(rq, r);
2020 return r;
2021 }
Junichi Nomura3a140752015-05-27 04:22:07 +00002022 if (r != DM_MAPIO_REMAPPED)
2023 return r;
Mike Snitzer78d8e582015-06-26 10:01:13 -04002024 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
2025 /* -ENOMEM */
2026 ti->type->release_clone_rq(clone);
2027 return DM_MAPIO_REQUEUE;
2028 }
Mike Snitzere5863d92014-12-17 21:08:12 -05002029 }
2030
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002031 switch (r) {
2032 case DM_MAPIO_SUBMITTED:
2033 /* The target has taken the I/O to submit by itself later */
2034 break;
2035 case DM_MAPIO_REMAPPED:
2036 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00002037 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06002038 blk_rq_pos(rq));
2039 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002040 break;
2041 case DM_MAPIO_REQUEUE:
2042 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04002043 dm_requeue_original_request(md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002044 break;
2045 default:
2046 if (r > 0) {
2047 DMWARN("unimplemented target map return value: %d", r);
2048 BUG();
2049 }
2050
2051 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06002052 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05002053 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002054 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00002055
Mike Snitzere5863d92014-12-17 21:08:12 -05002056 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002057}
2058
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002059static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002060{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002061 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05002062 struct request *rq = tio->orig;
2063 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002064
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002065 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
Mike Snitzer2d76fff2015-04-29 12:07:12 -04002066 dm_requeue_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002067}
2068
Keith Busch466d89a2014-10-17 17:46:37 -06002069static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002070{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002071 if (!orig->q->mq_ops)
2072 blk_start_request(orig);
2073 else
2074 blk_mq_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06002075 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002076
Mike Snitzer0ce65792015-02-26 00:50:28 -05002077 if (md->seq_rq_merge_deadline_usecs) {
2078 md->last_rq_pos = rq_end_sector(orig);
2079 md->last_rq_rw = rq_data_dir(orig);
2080 md->last_rq_start_time = ktime_get();
2081 }
Mike Snitzerde3ec862015-02-24 21:58:21 -05002082
Mikulas Patockae262f342015-06-09 17:22:49 -04002083 if (unlikely(dm_stats_used(&md->stats))) {
2084 struct dm_rq_target_io *tio = tio_from_request(orig);
2085 tio->duration_jiffies = jiffies;
2086 tio->n_sectors = blk_rq_sectors(orig);
Mike Christie528ec5a2016-06-05 14:32:03 -05002087 dm_stats_account_io(&md->stats, rq_data_dir(orig),
2088 blk_rq_pos(orig), tio->n_sectors, false, 0,
2089 &tio->stats_aux);
Mikulas Patockae262f342015-06-09 17:22:49 -04002090 }
2091
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002092 /*
2093 * Hold the md reference here for the in-flight I/O.
2094 * We can't rely on the reference count by device opener,
2095 * because the device may be closed during the request completion
2096 * when all bios are completed.
2097 * See the comment in rq_completed() too.
2098 */
2099 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002100}
2101
Mike Snitzer0ce65792015-02-26 00:50:28 -05002102#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
2103
2104ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
2105{
2106 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
2107}
2108
2109ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
2110 const char *buf, size_t count)
2111{
2112 unsigned deadline;
2113
Mike Snitzer17e149b2015-03-11 15:01:09 -04002114 if (!dm_request_based(md) || md->use_blk_mq)
Mike Snitzer0ce65792015-02-26 00:50:28 -05002115 return count;
2116
2117 if (kstrtouint(buf, 10, &deadline))
2118 return -EINVAL;
2119
2120 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
2121 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
2122
2123 md->seq_rq_merge_deadline_usecs = deadline;
2124
2125 return count;
2126}
2127
2128static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
2129{
2130 ktime_t kt_deadline;
2131
2132 if (!md->seq_rq_merge_deadline_usecs)
2133 return false;
2134
2135 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
2136 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
2137
2138 return !ktime_after(ktime_get(), kt_deadline);
2139}
2140
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002141/*
2142 * q->request_fn for request-based dm.
2143 * Called with the queue lock held.
2144 */
2145static void dm_request_fn(struct request_queue *q)
2146{
2147 struct mapped_device *md = q->queuedata;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002148 struct dm_target *ti = md->immutable_target;
Keith Busch466d89a2014-10-17 17:46:37 -06002149 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002150 struct dm_rq_target_io *tio;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002151 sector_t pos = 0;
2152
2153 if (unlikely(!ti)) {
2154 int srcu_idx;
2155 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2156
2157 ti = dm_table_find_target(map, pos);
2158 dm_put_live_table(md, srcu_idx);
2159 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002160
2161 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002162 * For suspend, check blk_queue_stopped() and increment
2163 * ->pending within a single queue_lock not to increment the
2164 * number of in-flight I/Os after the queue is stopped in
2165 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002166 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002167 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002168 rq = blk_peek_request(q);
2169 if (!rq)
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002170 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002171
Tejun Heo29e40132010-09-08 18:07:00 +02002172 /* always use block 0 to find the target for flushes for now */
2173 pos = 0;
Mike Christie3a5e02c2016-06-05 14:32:23 -05002174 if (req_op(rq) != REQ_OP_FLUSH)
Tejun Heo29e40132010-09-08 18:07:00 +02002175 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002176
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002177 if ((dm_request_peeked_before_merge_deadline(md) &&
2178 md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2179 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
2180 (ti->type->busy && ti->type->busy(ti))) {
2181 blk_delay_queue(q, HZ / 100);
2182 return;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002183 }
Tejun Heo29e40132010-09-08 18:07:00 +02002184
Keith Busch466d89a2014-10-17 17:46:37 -06002185 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002186
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002187 tio = tio_from_request(rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002188 /* Establish tio->ti before queuing work (map_tio_request) */
2189 tio->ti = ti;
2190 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00002191 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002192 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195static int dm_any_congested(void *congested_data, int bdi_bits)
2196{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002197 int r = bdi_bits;
2198 struct mapped_device *md = congested_data;
2199 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002201 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05002202 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002203 /*
Mike Snitzere522c032016-02-02 22:35:06 -05002204 * With request-based DM we only need to check the
2205 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002206 */
Mike Snitzere522c032016-02-02 22:35:06 -05002207 r = md->queue->backing_dev_info.wb.state & bdi_bits;
2208 } else {
2209 map = dm_get_live_table_fast(md);
2210 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002211 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05002212 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002213 }
2214 }
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return r;
2217}
2218
2219/*-----------------------------------------------------------------
2220 * An IDR is used to keep track of allocated minor numbers.
2221 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002222static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002224 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002226 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229/*
2230 * See if the device with a specific minor # is free.
2231 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002232static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002234 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 if (minor >= (1 << MINORBITS))
2237 return -EINVAL;
2238
Tejun Heoc9d76be2013-02-27 17:04:26 -08002239 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002240 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Tejun Heoc9d76be2013-02-27 17:04:26 -08002242 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002244 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002245 idr_preload_end();
2246 if (r < 0)
2247 return r == -ENOSPC ? -EBUSY : r;
2248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002251static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002253 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Tejun Heoc9d76be2013-02-27 17:04:26 -08002255 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002256 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Tejun Heoc9d76be2013-02-27 17:04:26 -08002258 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002260 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002261 idr_preload_end();
2262 if (r < 0)
2263 return r;
2264 *minor = r;
2265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002268static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Mikulas Patocka53d59142009-04-02 19:55:37 +01002270static void dm_wq_work(struct work_struct *work);
2271
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002272static void dm_init_md_queue(struct mapped_device *md)
2273{
2274 /*
2275 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002276 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002277 * The type is decided at the first table loading time.
2278 * To prevent problematic device stacking, clear the queue flag
2279 * for request stacking support until then.
2280 *
2281 * This queue is new, so no concurrency on the queue_flags.
2282 */
2283 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04002284
2285 /*
2286 * Initialize data that will only be used by a non-blk-mq DM queue
2287 * - must do so here (in alloc_dev callchain) before queue is used
2288 */
2289 md->queue->queuedata = md;
2290 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002291}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002292
Mike Snitzereca7ee62016-02-20 13:45:38 -05002293static void dm_init_normal_md_queue(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002294{
Mike Snitzer17e149b2015-03-11 15:01:09 -04002295 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002296 dm_init_md_queue(md);
2297
2298 /*
2299 * Initialize aspects of queue that aren't relevant for blk-mq
2300 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002301 md->queue->backing_dev_info.congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002302 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002303}
2304
Mike Snitzer0f209722015-04-28 11:50:29 -04002305static void cleanup_mapped_device(struct mapped_device *md)
2306{
Mike Snitzer0f209722015-04-28 11:50:29 -04002307 if (md->wq)
2308 destroy_workqueue(md->wq);
2309 if (md->kworker_task)
2310 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02002311 mempool_destroy(md->io_pool);
2312 mempool_destroy(md->rq_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04002313 if (md->bs)
2314 bioset_free(md->bs);
2315
Mikulas Patockab06075a2015-07-10 17:21:43 -04002316 cleanup_srcu_struct(&md->io_barrier);
2317
Mike Snitzer0f209722015-04-28 11:50:29 -04002318 if (md->disk) {
2319 spin_lock(&_minor_lock);
2320 md->disk->private_data = NULL;
2321 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04002322 del_gendisk(md->disk);
2323 put_disk(md->disk);
2324 }
2325
2326 if (md->queue)
2327 blk_cleanup_queue(md->queue);
2328
2329 if (md->bdev) {
2330 bdput(md->bdev);
2331 md->bdev = NULL;
2332 }
2333}
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335/*
2336 * Allocate and initialise a blank device with a given minor.
2337 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002338static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Mike Snitzer115485e2016-02-22 12:16:21 -05002340 int r, numa_node_id = dm_get_numa_node();
2341 struct mapped_device *md;
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002342 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Mike Snitzer115485e2016-02-22 12:16:21 -05002344 md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (!md) {
2346 DMWARN("unable to allocate device, out of memory.");
2347 return NULL;
2348 }
2349
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002350 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002351 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002354 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002355 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002356 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002357 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002359 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002361 r = init_srcu_struct(&md->io_barrier);
2362 if (r < 0)
2363 goto bad_io_barrier;
2364
Mike Snitzer115485e2016-02-22 12:16:21 -05002365 md->numa_node_id = numa_node_id;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002366 md->use_blk_mq = use_blk_mq;
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002367 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01002368 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002369 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002370 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002371 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002372 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002374 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002376 atomic_set(&md->uevent_seq, 0);
2377 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002378 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002379 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Mike Snitzer115485e2016-02-22 12:16:21 -05002381 md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04002383 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002385 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002386
Mike Snitzer115485e2016-02-22 12:16:21 -05002387 md->disk = alloc_disk_node(1, numa_node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04002389 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002391 atomic_set(&md->pending[0], 0);
2392 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002393 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002394 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002395 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002396 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002397 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 md->disk->major = _major;
2400 md->disk->first_minor = minor;
2401 md->disk->fops = &dm_blk_dops;
2402 md->disk->queue = md->queue;
2403 md->disk->private_data = md;
2404 sprintf(md->disk->disk_name, "dm-%d", minor);
2405 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002406 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Tejun Heo670368a2013-07-30 08:40:21 -04002408 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002409 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002410 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002411
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002412 md->bdev = bdget_disk(md->disk, 0);
2413 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002414 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002415
Tejun Heo6a8736d2010-09-08 18:07:00 +02002416 bio_init(&md->flush_bio);
2417 md->flush_bio.bi_bdev = md->bdev;
Mike Christiee6047142016-06-05 14:32:04 -05002418 bio_set_op_attrs(&md->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002419
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002420 dm_stats_init(&md->stats);
2421
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002422 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002423 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002424 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002425 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002426
2427 BUG_ON(old_md != MINOR_ALLOCED);
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return md;
2430
Mike Snitzer0f209722015-04-28 11:50:29 -04002431bad:
2432 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002433bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002435bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002436 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002437bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 kfree(md);
2439 return NULL;
2440}
2441
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002442static void unlock_fs(struct mapped_device *md);
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444static void free_dev(struct mapped_device *md)
2445{
Tejun Heof331c022008-09-03 09:01:48 +02002446 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002447
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002448 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002449
Mike Snitzer0f209722015-04-28 11:50:29 -04002450 cleanup_mapped_device(md);
Mike Snitzer1c357a12016-02-06 17:01:17 -05002451 if (md->tag_set) {
2452 blk_mq_free_tag_set(md->tag_set);
2453 kfree(md->tag_set);
2454 }
Mike Snitzer0f209722015-04-28 11:50:29 -04002455
2456 free_table_devices(&md->table_devices);
2457 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002458 free_minor(minor);
2459
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002460 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 kfree(md);
2462}
2463
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002464static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2465{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002466 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002467
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002468 if (md->bs) {
2469 /* The md already has necessary mempools. */
2470 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002471 /*
2472 * Reload bioset because front_pad may have changed
2473 * because a different table was loaded.
2474 */
2475 bioset_free(md->bs);
2476 md->bs = p->bs;
2477 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002478 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002479 /*
2480 * There's no need to reload with request-based dm
2481 * because the size of front_pad doesn't change.
2482 * Note for future: If you are to reload bioset,
2483 * prep-ed requests in the queue may refer
2484 * to bio from the old bioset, so you must walk
2485 * through the queue to unprep.
2486 */
2487 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002488 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002489
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002490 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2491
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002492 md->io_pool = p->io_pool;
2493 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002494 md->rq_pool = p->rq_pool;
2495 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002496 md->bs = p->bs;
2497 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002498
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002499out:
Mike Snitzer02233342015-03-10 23:49:26 -04002500 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002501 dm_table_free_md_mempools(t);
2502}
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504/*
2505 * Bind a table to the device.
2506 */
2507static void event_callback(void *context)
2508{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002509 unsigned long flags;
2510 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct mapped_device *md = (struct mapped_device *) context;
2512
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002513 spin_lock_irqsave(&md->uevent_lock, flags);
2514 list_splice_init(&md->uevent_list, &uevents);
2515 spin_unlock_irqrestore(&md->uevent_lock, flags);
2516
Tejun Heoed9e1982008-08-25 19:56:05 +09002517 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 atomic_inc(&md->event_nr);
2520 wake_up(&md->eventq);
2521}
2522
Mike Snitzerc2176492011-01-13 19:53:46 +00002523/*
2524 * Protected by md->suspend_lock obtained by dm_swap_table().
2525 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002526static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002528 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002530 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002533/*
2534 * Returns old map, which caller must destroy.
2535 */
2536static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2537 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002539 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002540 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 sector_t size;
2542
2543 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002544
2545 /*
2546 * Wipe any geometry if the size of the table changed.
2547 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002548 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002549 memset(&md->geometry, 0, sizeof(md->geometry));
2550
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002551 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002553 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002554
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002555 /*
2556 * The queue hasn't been stopped yet, if the old table type wasn't
2557 * for request-based during suspension. So stop it to prevent
2558 * I/O mapping before resume.
2559 * This must be done before setting the queue restrictions,
2560 * because request-based dm may be run just after the setting.
2561 */
Mike Snitzer16f12262016-01-31 17:22:27 -05002562 if (dm_table_request_based(t)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002563 dm_stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05002564 /*
2565 * Leverage the fact that request-based DM targets are
2566 * immutable singletons and establish md->immutable_target
2567 * - used to optimize both dm_request_fn and dm_mq_queue_rq
2568 */
2569 md->immutable_target = dm_table_get_immutable_target(t);
2570 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002571
2572 __bind_mempools(md, t);
2573
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002574 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzer1d3aa6f2016-02-22 14:14:24 -05002575 rcu_assign_pointer(md->map, (void *)t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002576 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2577
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002578 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002579 if (old_map)
2580 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002581
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002582 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Alasdair G Kergona7940152009-12-10 23:52:23 +00002585/*
2586 * Returns unbound table for the caller to free.
2587 */
2588static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002590 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002593 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302596 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002597 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002598
2599 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
2602/*
2603 * Constructor for a new device.
2604 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002605int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 struct mapped_device *md;
2608
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002609 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (!md)
2611 return -ENXIO;
2612
Milan Broz784aae72009-01-06 03:05:12 +00002613 dm_sysfs_init(md);
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 *result = md;
2616 return 0;
2617}
2618
Mike Snitzera5664da2010-08-12 04:14:01 +01002619/*
2620 * Functions to manage md->type.
2621 * All are required to hold md->type_lock.
2622 */
2623void dm_lock_md_type(struct mapped_device *md)
2624{
2625 mutex_lock(&md->type_lock);
2626}
2627
2628void dm_unlock_md_type(struct mapped_device *md)
2629{
2630 mutex_unlock(&md->type_lock);
2631}
2632
2633void dm_set_md_type(struct mapped_device *md, unsigned type)
2634{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002635 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002636 md->type = type;
2637}
2638
2639unsigned dm_get_md_type(struct mapped_device *md)
2640{
2641 return md->type;
2642}
2643
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002644struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2645{
2646 return md->immutable_target_type;
2647}
2648
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002649/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002650 * The queue_limits are only valid as long as you have a reference
2651 * count on 'md'.
2652 */
2653struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2654{
2655 BUG_ON(!atomic_read(&md->holders));
2656 return &md->queue->limits;
2657}
2658EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2659
Mike Snitzereca7ee62016-02-20 13:45:38 -05002660static void dm_old_init_rq_based_worker_thread(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002661{
2662 /* Initialize the request-based DM worker thread */
2663 init_kthread_worker(&md->kworker);
2664 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2665 "kdmwork-%s", dm_device_name(md));
2666}
2667
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002668/*
Mike Snitzereca7ee62016-02-20 13:45:38 -05002669 * Fully initialize a .request_fn request-based queue.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002670 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05002671static int dm_old_init_request_queue(struct mapped_device *md)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002672{
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002673 /* Fully initialize the queue */
Bob Liue233d802016-02-24 16:15:38 +08002674 if (!blk_init_allocated_queue(md->queue, dm_request_fn, NULL))
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002675 return -EINVAL;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002676
Mike Snitzer0ce65792015-02-26 00:50:28 -05002677 /* disable dm_request_fn's merge heuristic by default */
2678 md->seq_rq_merge_deadline_usecs = 0;
2679
Mike Snitzereca7ee62016-02-20 13:45:38 -05002680 dm_init_normal_md_queue(md);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002681 blk_queue_softirq_done(md->queue, dm_softirq_done);
Mike Snitzereca7ee62016-02-20 13:45:38 -05002682 blk_queue_prep_rq(md->queue, dm_old_prep_fn);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002683
Mike Snitzereca7ee62016-02-20 13:45:38 -05002684 dm_old_init_rq_based_worker_thread(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002685
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002686 elv_register_queue(md->queue);
2687
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002688 return 0;
2689}
2690
2691static int dm_mq_init_request(void *data, struct request *rq,
2692 unsigned int hctx_idx, unsigned int request_idx,
2693 unsigned int numa_node)
2694{
2695 struct mapped_device *md = data;
2696 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2697
2698 /*
2699 * Must initialize md member of tio, otherwise it won't
2700 * be available in dm_mq_queue_rq.
2701 */
2702 tio->md = md;
2703
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002704 if (md->init_tio_pdu) {
2705 /* target-specific per-io data is immediately after the tio */
2706 tio->info.ptr = tio + 1;
2707 }
2708
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002709 return 0;
2710}
2711
2712static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2713 const struct blk_mq_queue_data *bd)
2714{
2715 struct request *rq = bd->rq;
2716 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2717 struct mapped_device *md = tio->md;
Mike Snitzer16f12262016-01-31 17:22:27 -05002718 struct dm_target *ti = md->immutable_target;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002719
Mike Snitzer16f12262016-01-31 17:22:27 -05002720 if (unlikely(!ti)) {
2721 int srcu_idx;
2722 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002723
Mike Snitzer16f12262016-01-31 17:22:27 -05002724 ti = dm_table_find_target(map, 0);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002725 dm_put_live_table(md, srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002726 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002727
2728 if (ti->type->busy && ti->type->busy(ti))
2729 return BLK_MQ_RQ_QUEUE_BUSY;
2730
2731 dm_start_request(md, rq);
2732
2733 /* Init tio using md established in .init_request */
2734 init_tio(tio, rq, md);
2735
Mike Snitzer02233342015-03-10 23:49:26 -04002736 /*
2737 * Establish tio->ti before queuing work (map_tio_request)
2738 * or making direct call to map_request().
2739 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002740 tio->ti = ti;
Mike Snitzer02233342015-03-10 23:49:26 -04002741
Mike Snitzerc5248f72016-02-20 14:02:49 -05002742 /* Direct call is fine since .queue_rq allows allocations */
2743 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
2744 /* Undo dm_start_request() before requeuing */
2745 rq_end_stats(md, rq);
2746 rq_completed(md, rq_data_dir(rq), false);
2747 return BLK_MQ_RQ_QUEUE_BUSY;
Mike Snitzer02233342015-03-10 23:49:26 -04002748 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002749
2750 return BLK_MQ_RQ_QUEUE_OK;
2751}
2752
2753static struct blk_mq_ops dm_mq_ops = {
2754 .queue_rq = dm_mq_queue_rq,
2755 .map_queue = blk_mq_map_queue,
2756 .complete = dm_softirq_done,
2757 .init_request = dm_mq_init_request,
2758};
2759
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002760static int dm_mq_init_request_queue(struct mapped_device *md,
2761 struct dm_target *immutable_tgt)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002762{
2763 struct request_queue *q;
2764 int err;
2765
Mike Snitzerc5248f72016-02-20 14:02:49 -05002766 if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) {
2767 DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
2768 return -EINVAL;
2769 }
2770
Mike Snitzer115485e2016-02-22 12:16:21 -05002771 md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
Mike Snitzer1c357a12016-02-06 17:01:17 -05002772 if (!md->tag_set)
2773 return -ENOMEM;
2774
2775 md->tag_set->ops = &dm_mq_ops;
2776 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
Mike Snitzer115485e2016-02-22 12:16:21 -05002777 md->tag_set->numa_node = md->numa_node_id;
Mike Snitzer1c357a12016-02-06 17:01:17 -05002778 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2779 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
2780 md->tag_set->driver_data = md;
2781
2782 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002783 if (immutable_tgt && immutable_tgt->per_io_data_size) {
2784 /* any target-specific per-io data is immediately after the tio */
2785 md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
2786 md->init_tio_pdu = true;
2787 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002788
Mike Snitzer1c357a12016-02-06 17:01:17 -05002789 err = blk_mq_alloc_tag_set(md->tag_set);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002790 if (err)
Mike Snitzer1c357a12016-02-06 17:01:17 -05002791 goto out_kfree_tag_set;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002792
Mike Snitzer1c357a12016-02-06 17:01:17 -05002793 q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002794 if (IS_ERR(q)) {
2795 err = PTR_ERR(q);
2796 goto out_tag_set;
2797 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002798 dm_init_md_queue(md);
2799
2800 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
2801 blk_mq_register_disk(md->disk);
2802
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002803 return 0;
2804
2805out_tag_set:
Mike Snitzer1c357a12016-02-06 17:01:17 -05002806 blk_mq_free_tag_set(md->tag_set);
2807out_kfree_tag_set:
2808 kfree(md->tag_set);
2809
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002810 return err;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002811}
2812
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002813static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2814{
2815 if (type == DM_TYPE_BIO_BASED)
2816 return type;
2817
2818 return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2819}
2820
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002821/*
2822 * Setup the DM device's queue based on md's type
2823 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002824int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002825{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002826 int r;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002827 unsigned md_type = filter_md_type(dm_get_md_type(md), md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002828
2829 switch (md_type) {
2830 case DM_TYPE_REQUEST_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05002831 r = dm_old_init_request_queue(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002832 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002833 DMERR("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002834 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002835 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002836 break;
2837 case DM_TYPE_MQ_REQUEST_BASED:
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002838 r = dm_mq_init_request_queue(md, dm_table_get_immutable_target(t));
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002839 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002840 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002841 return r;
2842 }
2843 break;
2844 case DM_TYPE_BIO_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05002845 dm_init_normal_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002846 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04002847 /*
2848 * DM handles splitting bios as needed. Free the bio_split bioset
2849 * since it won't be used (saves 1 process per bio-based DM device).
2850 */
2851 bioset_free(md->queue->bio_split);
2852 md->queue->bio_split = NULL;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002853 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002854 }
2855
2856 return 0;
2857}
2858
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002859struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 unsigned minor = MINOR(dev);
2863
2864 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2865 return NULL;
2866
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002867 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002870 if (md) {
2871 if ((md == MINOR_ALLOCED ||
2872 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2873 dm_deleting_md(md) ||
2874 test_bit(DMF_FREEING, &md->flags))) {
2875 md = NULL;
2876 goto out;
2877 }
2878 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002881out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002882 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
David Teigland637842c2006-01-06 00:20:00 -08002884 return md;
2885}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002886EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002887
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002888void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002889{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002890 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
2893void dm_set_mdptr(struct mapped_device *md, void *ptr)
2894{
2895 md->interface_ptr = ptr;
2896}
2897
2898void dm_get(struct mapped_device *md)
2899{
2900 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002901 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002904int dm_hold(struct mapped_device *md)
2905{
2906 spin_lock(&_minor_lock);
2907 if (test_bit(DMF_FREEING, &md->flags)) {
2908 spin_unlock(&_minor_lock);
2909 return -EBUSY;
2910 }
2911 dm_get(md);
2912 spin_unlock(&_minor_lock);
2913 return 0;
2914}
2915EXPORT_SYMBOL_GPL(dm_hold);
2916
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002917const char *dm_device_name(struct mapped_device *md)
2918{
2919 return md->name;
2920}
2921EXPORT_SYMBOL_GPL(dm_device_name);
2922
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002923static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002925 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002926 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002928 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002929
Mike Snitzer63a4f062015-03-23 17:01:43 -04002930 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002931 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2932 set_bit(DMF_FREEING, &md->flags);
2933 spin_unlock(&_minor_lock);
2934
Mike Snitzer02233342015-03-10 23:49:26 -04002935 if (dm_request_based(md) && md->kworker_task)
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002936 flush_kthread_worker(&md->kworker);
2937
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002938 /*
2939 * Take suspend_lock so that presuspend and postsuspend methods
2940 * do not race with internal suspend.
2941 */
2942 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002943 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002944 if (!dm_suspended_md(md)) {
2945 dm_table_presuspend_targets(map);
2946 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002948 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2949 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002950 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002951
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002952 /*
2953 * Rare, but there may be I/O requests still going to complete,
2954 * for example. Wait for all references to disappear.
2955 * No one should increment the reference count of the mapped_device,
2956 * after the mapped_device state becomes DMF_FREEING.
2957 */
2958 if (wait)
2959 while (atomic_read(&md->holders))
2960 msleep(1);
2961 else if (atomic_read(&md->holders))
2962 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2963 dm_device_name(md), atomic_read(&md->holders));
2964
2965 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002966 dm_table_destroy(__unbind(md));
2967 free_dev(md);
2968}
2969
2970void dm_destroy(struct mapped_device *md)
2971{
2972 __dm_destroy(md, true);
2973}
2974
2975void dm_destroy_immediate(struct mapped_device *md)
2976{
2977 __dm_destroy(md, false);
2978}
2979
2980void dm_put(struct mapped_device *md)
2981{
2982 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
Edward Goggin79eb8852007-05-09 02:32:56 -07002984EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Mikulas Patocka401600d2009-04-02 19:55:38 +01002986static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002987{
2988 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002989 DECLARE_WAITQUEUE(wait, current);
2990
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002991 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002992
2993 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002994 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002995
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002996 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002997 break;
2998
Mikulas Patocka401600d2009-04-02 19:55:38 +01002999 if (interruptible == TASK_INTERRUPTIBLE &&
3000 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00003001 r = -EINTR;
3002 break;
3003 }
3004
3005 io_schedule();
3006 }
3007 set_current_state(TASK_RUNNING);
3008
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01003009 remove_wait_queue(&md->wait, &wait);
3010
Milan Broz46125c12008-02-08 02:10:30 +00003011 return r;
3012}
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014/*
3015 * Process the deferred bios
3016 */
Mikulas Patockaef208582009-04-02 19:55:38 +01003017static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Mikulas Patockaef208582009-04-02 19:55:38 +01003019 struct mapped_device *md = container_of(work, struct mapped_device,
3020 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00003021 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003022 int srcu_idx;
3023 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003025 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01003026
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003027 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01003028 spin_lock_irq(&md->deferred_lock);
3029 c = bio_list_pop(&md->deferred);
3030 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01003031
Tejun Heo6a8736d2010-09-08 18:07:00 +02003032 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01003033 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01003034
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003035 if (dm_request_based(md))
3036 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02003037 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003038 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01003039 }
Milan Broz73d410c2008-02-08 02:10:25 +00003040
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003041 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003044static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00003045{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003046 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003047 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01003048 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00003049}
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003052 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003054struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Mike Christie87eb5b22013-03-01 22:45:48 +00003056 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003057 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003058 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Daniel Walkere61290a2008-02-08 02:10:08 +00003060 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003063 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003064 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Mike Snitzer3ae70652012-09-26 23:45:45 +01003066 /*
3067 * If the new table has no data devices, retain the existing limits.
3068 * This helps multipath with queue_if_no_path if all paths disappear,
3069 * then new I/O is queued based on these limits, and then some paths
3070 * reappear.
3071 */
3072 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003073 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003074 if (live_map)
3075 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003076 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003077 }
3078
Mike Christie87eb5b22013-03-01 22:45:48 +00003079 if (!live_map) {
3080 r = dm_calculate_queue_limits(table, &limits);
3081 if (r) {
3082 map = ERR_PTR(r);
3083 goto out;
3084 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003085 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003086
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003087 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003089out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003090 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003091 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
3094/*
3095 * Functions to lock and unlock any filesystem running on the
3096 * device.
3097 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003098static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003100 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003103
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003104 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003105 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003106 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003107 md->frozen_sb = NULL;
3108 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003109 }
3110
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003111 set_bit(DMF_FROZEN, &md->flags);
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return 0;
3114}
3115
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003116static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003118 if (!test_bit(DMF_FROZEN, &md->flags))
3119 return;
3120
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003121 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003123 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
3126/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04003127 * If __dm_suspend returns 0, the device is completely quiescent
3128 * now. There is no request-processing activity. All new requests
3129 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003130 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04003131 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003132 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003133static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3134 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003136 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3137 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3138 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003140 /*
3141 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
3142 * This flag is cleared before dm_suspend returns.
3143 */
3144 if (noflush)
3145 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
3146
Mike Snitzerd67ee212014-10-28 20:13:31 -04003147 /*
3148 * This gets reverted if there's an error later and the targets
3149 * provide the .presuspend_undo hook.
3150 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003151 dm_table_presuspend_targets(map);
3152
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003153 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003154 * Flush I/O to the device.
3155 * Any I/O submitted after lock_fs() may not be flushed.
3156 * noflush takes precedence over do_lockfs.
3157 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003158 */
3159 if (!noflush && do_lockfs) {
3160 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003161 if (r) {
3162 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003163 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04003164 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
3167 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003168 * Here we must make sure that no processes are submitting requests
3169 * to target drivers i.e. no one may be executing
3170 * __split_and_process_bio. This is called from dm_request and
3171 * dm_wq_work.
3172 *
3173 * To get all processes out of __split_and_process_bio in dm_request,
3174 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02003175 * __split_and_process_bio from dm_request and quiesce the thread
3176 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
3177 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01003179 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003180 if (map)
3181 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003183 /*
Tejun Heo29e40132010-09-08 18:07:00 +02003184 * Stop md->queue before flushing md->wq in case request-based
3185 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003186 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003187 if (dm_request_based(md)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05003188 dm_stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04003189 if (md->kworker_task)
3190 flush_kthread_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003191 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003192
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003193 flush_workqueue(md->wq);
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003196 * At this point no more requests are entering target request routines.
3197 * We call dm_wait_for_completion to wait for all existing requests
3198 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003200 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Milan Broz6d6f10d2008-02-08 02:10:22 +00003202 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01003203 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003204 if (map)
3205 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00003208 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003209 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00003210
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003211 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05003212 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003213
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003214 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003215 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003216 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003217 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003218
Mike Snitzerffcc3932014-10-28 18:34:52 -04003219 return r;
3220}
3221
3222/*
3223 * We need to be able to change a mapping table under a mounted
3224 * filesystem. For example we might want to move some data in
3225 * the background. Before the table can be swapped with
3226 * dm_bind_table, dm_suspend must be called to flush any in
3227 * flight bios and ensure that any further io gets deferred.
3228 */
3229/*
3230 * Suspend mechanism in request-based dm.
3231 *
3232 * 1. Flush all I/Os by lock_fs() if needed.
3233 * 2. Stop dispatching any I/O by stopping the request_queue.
3234 * 3. Wait for all in-flight I/Os to be completed or requeued.
3235 *
3236 * To abort suspend, start the request_queue.
3237 */
3238int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3239{
3240 struct dm_table *map = NULL;
3241 int r = 0;
3242
3243retry:
3244 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3245
3246 if (dm_suspended_md(md)) {
3247 r = -EINVAL;
3248 goto out_unlock;
3249 }
3250
3251 if (dm_suspended_internally_md(md)) {
3252 /* already internally suspended, wait for internal resume */
3253 mutex_unlock(&md->suspend_lock);
3254 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3255 if (r)
3256 return r;
3257 goto retry;
3258 }
3259
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003260 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003261
3262 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3263 if (r)
3264 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 set_bit(DMF_SUSPENDED, &md->flags);
3267
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00003268 dm_table_postsuspend_targets(map);
3269
Alasdair G Kergond2874832006-11-08 17:44:43 -08003270out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00003271 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003272 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Mike Snitzerffcc3932014-10-28 18:34:52 -04003275static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003277 if (map) {
3278 int r = dm_table_resume_targets(map);
3279 if (r)
3280 return r;
3281 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003282
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003283 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003284
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003285 /*
3286 * Flushing deferred I/Os must be done after targets are resumed
3287 * so that mapping of targets can work correctly.
3288 * Request-based dm is queueing the deferred I/Os in its request_queue.
3289 */
3290 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05003291 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003292
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003293 unlock_fs(md);
3294
Mike Snitzerffcc3932014-10-28 18:34:52 -04003295 return 0;
3296}
3297
3298int dm_resume(struct mapped_device *md)
3299{
3300 int r = -EINVAL;
3301 struct dm_table *map = NULL;
3302
3303retry:
3304 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3305
3306 if (!dm_suspended_md(md))
3307 goto out;
3308
3309 if (dm_suspended_internally_md(md)) {
3310 /* already internally suspended, wait for internal resume */
3311 mutex_unlock(&md->suspend_lock);
3312 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3313 if (r)
3314 return r;
3315 goto retry;
3316 }
3317
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003318 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003319 if (!map || !dm_table_get_size(map))
3320 goto out;
3321
3322 r = __dm_resume(md, map);
3323 if (r)
3324 goto out;
3325
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003326 clear_bit(DMF_SUSPENDED, &md->flags);
3327
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003328 r = 0;
3329out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003330 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003331
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003332 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003335/*
3336 * Internal suspend/resume works like userspace-driven suspend. It waits
3337 * until all bios finish and prevents issuing new bios to the target drivers.
3338 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003339 */
3340
Mike Snitzerffcc3932014-10-28 18:34:52 -04003341static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3342{
3343 struct dm_table *map = NULL;
3344
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003345 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003346 return; /* nested internal suspend */
3347
3348 if (dm_suspended_md(md)) {
3349 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3350 return; /* nest suspend */
3351 }
3352
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003353 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003354
3355 /*
3356 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3357 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3358 * would require changing .presuspend to return an error -- avoid this
3359 * until there is a need for more elaborate variants of internal suspend.
3360 */
3361 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3362
3363 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3364
3365 dm_table_postsuspend_targets(map);
3366}
3367
3368static void __dm_internal_resume(struct mapped_device *md)
3369{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003370 BUG_ON(!md->internal_suspend_count);
3371
3372 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003373 return; /* resume from nested internal suspend */
3374
3375 if (dm_suspended_md(md))
3376 goto done; /* resume from nested suspend */
3377
3378 /*
3379 * NOTE: existing callers don't need to call dm_table_resume_targets
3380 * (which may fail -- so best to avoid it for now by passing NULL map)
3381 */
3382 (void) __dm_resume(md, NULL);
3383
3384done:
3385 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3386 smp_mb__after_atomic();
3387 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3388}
3389
3390void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003391{
3392 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003393 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3394 mutex_unlock(&md->suspend_lock);
3395}
3396EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3397
3398void dm_internal_resume(struct mapped_device *md)
3399{
3400 mutex_lock(&md->suspend_lock);
3401 __dm_internal_resume(md);
3402 mutex_unlock(&md->suspend_lock);
3403}
3404EXPORT_SYMBOL_GPL(dm_internal_resume);
3405
3406/*
3407 * Fast variants of internal suspend/resume hold md->suspend_lock,
3408 * which prevents interaction with userspace-driven suspend.
3409 */
3410
3411void dm_internal_suspend_fast(struct mapped_device *md)
3412{
3413 mutex_lock(&md->suspend_lock);
3414 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003415 return;
3416
3417 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3418 synchronize_srcu(&md->io_barrier);
3419 flush_workqueue(md->wq);
3420 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3421}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003422EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003423
Mike Snitzerffcc3932014-10-28 18:34:52 -04003424void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003425{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003426 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003427 goto done;
3428
3429 dm_queue_flush(md);
3430
3431done:
3432 mutex_unlock(&md->suspend_lock);
3433}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003434EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436/*-----------------------------------------------------------------
3437 * Event notification.
3438 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003439int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003440 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003441{
Milan Broz60935eb2009-06-22 10:12:30 +01003442 char udev_cookie[DM_COOKIE_LENGTH];
3443 char *envp[] = { udev_cookie, NULL };
3444
3445 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003446 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003447 else {
3448 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3449 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003450 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3451 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003452 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003453}
3454
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003455uint32_t dm_next_uevent_seq(struct mapped_device *md)
3456{
3457 return atomic_add_return(1, &md->uevent_seq);
3458}
3459
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460uint32_t dm_get_event_nr(struct mapped_device *md)
3461{
3462 return atomic_read(&md->event_nr);
3463}
3464
3465int dm_wait_event(struct mapped_device *md, int event_nr)
3466{
3467 return wait_event_interruptible(md->eventq,
3468 (event_nr != atomic_read(&md->event_nr)));
3469}
3470
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003471void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3472{
3473 unsigned long flags;
3474
3475 spin_lock_irqsave(&md->uevent_lock, flags);
3476 list_add(elist, &md->uevent_list);
3477 spin_unlock_irqrestore(&md->uevent_lock, flags);
3478}
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480/*
3481 * The gendisk is only valid as long as you have a reference
3482 * count on 'md'.
3483 */
3484struct gendisk *dm_disk(struct mapped_device *md)
3485{
3486 return md->disk;
3487}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00003488EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Milan Broz784aae72009-01-06 03:05:12 +00003490struct kobject *dm_kobject(struct mapped_device *md)
3491{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003492 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003493}
3494
Milan Broz784aae72009-01-06 03:05:12 +00003495struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3496{
3497 struct mapped_device *md;
3498
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003499 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003500
Milan Broz4d89b7b2009-06-22 10:12:11 +01003501 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003502 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003503 return NULL;
3504
Milan Broz784aae72009-01-06 03:05:12 +00003505 dm_get(md);
3506 return md;
3507}
3508
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003509int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510{
3511 return test_bit(DMF_SUSPENDED, &md->flags);
3512}
3513
Mike Snitzerffcc3932014-10-28 18:34:52 -04003514int dm_suspended_internally_md(struct mapped_device *md)
3515{
3516 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3517}
3518
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003519int dm_test_deferred_remove_flag(struct mapped_device *md)
3520{
3521 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3522}
3523
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003524int dm_suspended(struct dm_target *ti)
3525{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003526 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003527}
3528EXPORT_SYMBOL_GPL(dm_suspended);
3529
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003530int dm_noflush_suspending(struct dm_target *ti)
3531{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003532 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003533}
3534EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3535
Mike Snitzer78d8e582015-06-26 10:01:13 -04003536struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
Mike Snitzer30187e12016-01-31 13:28:26 -05003537 unsigned integrity, unsigned per_io_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003538{
Mike Snitzer115485e2016-02-22 12:16:21 -05003539 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
Mike Snitzer78d8e582015-06-26 10:01:13 -04003540 struct kmem_cache *cachep = NULL;
3541 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003542 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003543
3544 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003545 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003546
Mike Snitzer78d8e582015-06-26 10:01:13 -04003547 type = filter_md_type(type, md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04003548
Mike Snitzer78d8e582015-06-26 10:01:13 -04003549 switch (type) {
3550 case DM_TYPE_BIO_BASED:
3551 cachep = _io_cache;
3552 pool_size = dm_get_reserved_bio_based_ios();
Mike Snitzer30187e12016-01-31 13:28:26 -05003553 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 -04003554 break;
3555 case DM_TYPE_REQUEST_BASED:
3556 cachep = _rq_tio_cache;
3557 pool_size = dm_get_reserved_rq_based_ios();
3558 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3559 if (!pools->rq_pool)
3560 goto out;
3561 /* fall through to setup remaining rq-based pools */
3562 case DM_TYPE_MQ_REQUEST_BASED:
3563 if (!pool_size)
3564 pool_size = dm_get_reserved_rq_based_ios();
3565 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05003566 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04003567 break;
3568 default:
3569 BUG();
3570 }
3571
3572 if (cachep) {
3573 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3574 if (!pools->io_pool)
3575 goto out;
3576 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003577
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003578 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003579 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003580 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003581
Martin K. Petersena91a2782011-03-17 11:11:05 +01003582 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003583 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003584
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003585 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003586
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003587out:
3588 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003589
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003590 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003591}
3592
3593void dm_free_md_mempools(struct dm_md_mempools *pools)
3594{
3595 if (!pools)
3596 return;
3597
Julia Lawall6f659852015-09-13 14:15:05 +02003598 mempool_destroy(pools->io_pool);
3599 mempool_destroy(pools->rq_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003600
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003601 if (pools->bs)
3602 bioset_free(pools->bs);
3603
3604 kfree(pools);
3605}
3606
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003607static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003608 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003609{
3610 struct mapped_device *md = bdev->bd_disk->private_data;
3611 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003612 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003613 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003614
Mike Snitzer956a4022016-02-18 16:13:51 -05003615 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003616 if (r < 0)
3617 return r;
3618
3619 ops = bdev->bd_disk->fops->pr_ops;
3620 if (ops && ops->pr_register)
3621 r = ops->pr_register(bdev, old_key, new_key, flags);
3622 else
3623 r = -EOPNOTSUPP;
3624
Mike Snitzer956a4022016-02-18 16:13:51 -05003625 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003626 return r;
3627}
3628
3629static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05003630 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003631{
3632 struct mapped_device *md = bdev->bd_disk->private_data;
3633 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003634 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003635 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003636
Mike Snitzer956a4022016-02-18 16:13:51 -05003637 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003638 if (r < 0)
3639 return r;
3640
3641 ops = bdev->bd_disk->fops->pr_ops;
3642 if (ops && ops->pr_reserve)
3643 r = ops->pr_reserve(bdev, key, type, flags);
3644 else
3645 r = -EOPNOTSUPP;
3646
Mike Snitzer956a4022016-02-18 16:13:51 -05003647 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003648 return r;
3649}
3650
3651static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3652{
3653 struct mapped_device *md = bdev->bd_disk->private_data;
3654 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003655 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003656 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003657
Mike Snitzer956a4022016-02-18 16:13:51 -05003658 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003659 if (r < 0)
3660 return r;
3661
3662 ops = bdev->bd_disk->fops->pr_ops;
3663 if (ops && ops->pr_release)
3664 r = ops->pr_release(bdev, key, type);
3665 else
3666 r = -EOPNOTSUPP;
3667
Mike Snitzer956a4022016-02-18 16:13:51 -05003668 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003669 return r;
3670}
3671
3672static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003673 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003674{
3675 struct mapped_device *md = bdev->bd_disk->private_data;
3676 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003677 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003678 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003679
Mike Snitzer956a4022016-02-18 16:13:51 -05003680 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003681 if (r < 0)
3682 return r;
3683
3684 ops = bdev->bd_disk->fops->pr_ops;
3685 if (ops && ops->pr_preempt)
3686 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3687 else
3688 r = -EOPNOTSUPP;
3689
Mike Snitzer956a4022016-02-18 16:13:51 -05003690 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003691 return r;
3692}
3693
3694static int dm_pr_clear(struct block_device *bdev, u64 key)
3695{
3696 struct mapped_device *md = bdev->bd_disk->private_data;
3697 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003698 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003699 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003700
Mike Snitzer956a4022016-02-18 16:13:51 -05003701 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003702 if (r < 0)
3703 return r;
3704
3705 ops = bdev->bd_disk->fops->pr_ops;
3706 if (ops && ops->pr_clear)
3707 r = ops->pr_clear(bdev, key);
3708 else
3709 r = -EOPNOTSUPP;
3710
Mike Snitzer956a4022016-02-18 16:13:51 -05003711 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003712 return r;
3713}
3714
3715static const struct pr_ops dm_pr_ops = {
3716 .pr_register = dm_pr_register,
3717 .pr_reserve = dm_pr_reserve,
3718 .pr_release = dm_pr_release,
3719 .pr_preempt = dm_pr_preempt,
3720 .pr_clear = dm_pr_clear,
3721};
3722
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003723static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 .open = dm_blk_open,
3725 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003726 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003727 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003728 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 .owner = THIS_MODULE
3730};
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732/*
3733 * module hooks
3734 */
3735module_init(dm_init);
3736module_exit(dm_exit);
3737
3738module_param(major, uint, 0);
3739MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003740
Mike Snitzere8603132013-09-12 18:06:12 -04003741module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3742MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3743
Mike Snitzerf4790822013-09-12 18:06:12 -04003744module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3745MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3746
Mike Snitzer17e149b2015-03-11 15:01:09 -04003747module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
3748MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
3749
Mike Snitzerfaad87d2016-01-28 16:52:56 -05003750module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
3751MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
3752
3753module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
3754MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");
3755
Mike Snitzer115485e2016-02-22 12:16:21 -05003756module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3757MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759MODULE_DESCRIPTION(DM_NAME " driver");
3760MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3761MODULE_LICENSE("GPL");