blob: 92c2fee413b6cd8cd329e398bc59dc04107f3e88 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080020#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010021#include <linux/delay.h>
Mike Snitzerffcc3932014-10-28 18:34:52 -040022#include <linux/wait.h>
Keith Busch2eb6e1e2014-10-17 17:46:36 -060023#include <linux/kthread.h>
Mike Snitzer0ce65792015-02-26 00:50:28 -050024#include <linux/ktime.h>
Mike Snitzerde3ec862015-02-24 21:58:21 -050025#include <linux/elevator.h> /* for rq_end_sector() */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -050026#include <linux/blk-mq.h>
Christoph Hellwig71cdb692015-10-15 14:10:51 +020027#include <linux/pr.h>
Li Zefan55782132009-06-09 13:43:05 +080028
29#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Alasdair G Kergon72d94862006-06-26 00:27:35 -070031#define DM_MSG_PREFIX "core"
32
Namhyung Kim71a16732011-10-31 20:18:54 +000033#ifdef CONFIG_PRINTK
34/*
35 * ratelimit state to be used in DMXXX_LIMIT().
36 */
37DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
38 DEFAULT_RATELIMIT_INTERVAL,
39 DEFAULT_RATELIMIT_BURST);
40EXPORT_SYMBOL(dm_ratelimit_state);
41#endif
42
Milan Broz60935eb2009-06-22 10:12:30 +010043/*
44 * Cookies are numeric values sent with CHANGE and REMOVE
45 * uevents while resuming, removing or renaming the device.
46 */
47#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
48#define DM_COOKIE_LENGTH 24
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static const char *_name = DM_NAME;
51
52static unsigned int major = 0;
53static unsigned int _major = 0;
54
Alasdair G Kergond15b7742011-08-02 12:32:01 +010055static DEFINE_IDR(_minor_idr);
56
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070057static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040058
59static void do_deferred_remove(struct work_struct *w);
60
61static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
62
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040063static struct workqueue_struct *deferred_remove_workqueue;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000066 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * One of these is allocated per bio.
68 */
69struct dm_io {
70 struct mapped_device *md;
71 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010073 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080074 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010075 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040076 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
79/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000080 * For request-based dm.
81 * One of these is allocated per request.
82 */
83struct dm_rq_target_io {
84 struct mapped_device *md;
85 struct dm_target *ti;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -050086 struct request *orig, *clone;
Keith Busch2eb6e1e2014-10-17 17:46:36 -060087 struct kthread_work work;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000088 int error;
89 union map_info info;
Mikulas Patockae262f342015-06-09 17:22:49 -040090 struct dm_stats_aux stats_aux;
91 unsigned long duration_jiffies;
92 unsigned n_sectors;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000093};
94
95/*
Kent Overstreet94818742012-09-07 13:44:01 -070096 * For request-based dm - the bio clones we allocate are embedded in these
97 * structs.
98 *
99 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
100 * the bioset is created - this means the bio has to come at the end of the
101 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000102 */
103struct dm_rq_clone_bio_info {
104 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100105 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -0700106 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000107};
108
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700109#define MINOR_ALLOCED ((void *)-1)
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * Bits for the md->flags field.
113 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100114#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800116#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700117#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700118#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800119#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -0700120#define DMF_DEFERRED_REMOVE 6
121#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Milan Broz304f3f62008-02-08 02:11:17 +0000123/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100124 * A dummy definition to make RCU happy.
125 * struct dm_table should never be dereferenced in this file.
126 */
127struct dm_table {
128 int undefined__;
129};
130
131/*
Milan Broz304f3f62008-02-08 02:11:17 +0000132 * Work processed by per-device workqueue.
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100135 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000136 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700138 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100140 /*
141 * The current mapping.
142 * Use dm_get_live_table{_fast} or take suspend_lock for
143 * dereference.
144 */
Pranith Kumar6fa99522014-10-28 15:09:57 -0700145 struct dm_table __rcu *map;
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100146
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500147 struct list_head table_devices;
148 struct mutex table_devices_lock;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long flags;
151
Jens Axboe165125e2007-07-24 09:28:11 +0200152 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100153 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100154 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100155 struct mutex type_lock;
156
Mike Snitzer16f12262016-01-31 17:22:27 -0500157 struct dm_target *immutable_target;
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000158 struct target_type *immutable_target_type;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800161 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 void *interface_ptr;
164
165 /*
166 * A list of ios that arrived while we were suspended.
167 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200168 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100170 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800171 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100172 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200175 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000176 */
177 struct workqueue_struct *wq;
178
179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * io objects are allocated from here.
181 */
182 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500183 mempool_t *rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Stefan Bader9faf4002006-10-03 01:15:41 -0700185 struct bio_set *bs;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * Event handling.
189 */
190 atomic_t event_nr;
191 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100192 atomic_t uevent_seq;
193 struct list_head uevent_list;
194 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /*
197 * freeze/thaw support require holding onto a super block
198 */
199 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100200 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800201
202 /* forced geometry settings */
203 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000204
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500205 /* kobject and completion */
206 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500207
Tejun Heod87f4c12010-09-03 11:56:19 +0200208 /* zero-length flush that will be cloned and submitted to targets */
209 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400210
Mikulas Patocka96b26c82015-01-08 18:52:26 -0500211 /* the number of internal suspends */
212 unsigned internal_suspend_count;
213
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400214 struct dm_stats stats;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600215
216 struct kthread_worker kworker;
217 struct task_struct *kworker_task;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500218
219 /* for request-based merge heuristic in dm_request_fn() */
Mike Snitzer0ce65792015-02-26 00:50:28 -0500220 unsigned seq_rq_merge_deadline_usecs;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500221 int last_rq_rw;
Mike Snitzer0ce65792015-02-26 00:50:28 -0500222 sector_t last_rq_pos;
223 ktime_t last_rq_start_time;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -0500224
225 /* for blk-mq request-based DM support */
Mike Snitzer1c357a12016-02-06 17:01:17 -0500226 struct blk_mq_tag_set *tag_set;
Mike Snitzer591ddcf2016-01-31 12:05:42 -0500227 bool use_blk_mq:1;
228 bool init_tio_pdu:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Mike Snitzer17e149b2015-03-11 15:01:09 -0400231#ifdef CONFIG_DM_MQ_DEFAULT
232static bool use_blk_mq = true;
233#else
234static bool use_blk_mq = false;
235#endif
236
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500237#define DM_MQ_NR_HW_QUEUES 1
238#define DM_MQ_QUEUE_DEPTH 2048
239
240static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
241static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
242
Mike Snitzer17e149b2015-03-11 15:01:09 -0400243bool dm_use_blk_mq(struct mapped_device *md)
244{
245 return md->use_blk_mq;
246}
Mike Snitzer591ddcf2016-01-31 12:05:42 -0500247EXPORT_SYMBOL_GPL(dm_use_blk_mq);
Mike Snitzer17e149b2015-03-11 15:01:09 -0400248
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100249/*
250 * For mempools pre-allocation at the table loading time.
251 */
252struct dm_md_mempools {
253 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500254 mempool_t *rq_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100255 struct bio_set *bs;
256};
257
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500258struct table_device {
259 struct list_head list;
260 atomic_t count;
261 struct dm_dev dm_dev;
262};
263
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400264#define RESERVED_BIO_BASED_IOS 16
265#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400266#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800267static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000268static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500269static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700270
Mike Snitzerf4790822013-09-12 18:06:12 -0400271/*
Mike Snitzere8603132013-09-12 18:06:12 -0400272 * Bio-based DM's mempools' reserved IOs set by the user.
273 */
274static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
275
276/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400277 * Request-based DM's mempools' reserved IOs set by the user.
278 */
279static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
280
Mike Snitzer09c2d532015-02-27 22:25:26 -0500281static unsigned __dm_get_module_param(unsigned *module_param,
Mike Snitzerf4790822013-09-12 18:06:12 -0400282 unsigned def, unsigned max)
283{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500284 unsigned param = ACCESS_ONCE(*module_param);
285 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400286
Mike Snitzer09c2d532015-02-27 22:25:26 -0500287 if (!param)
288 modified_param = def;
289 else if (param > max)
290 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400291
Mike Snitzer09c2d532015-02-27 22:25:26 -0500292 if (modified_param) {
293 (void)cmpxchg(module_param, param, modified_param);
294 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400295 }
296
Mike Snitzer09c2d532015-02-27 22:25:26 -0500297 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400298}
299
Mike Snitzere8603132013-09-12 18:06:12 -0400300unsigned dm_get_reserved_bio_based_ios(void)
301{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500302 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzere8603132013-09-12 18:06:12 -0400303 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
304}
305EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
306
Mike Snitzerf4790822013-09-12 18:06:12 -0400307unsigned dm_get_reserved_rq_based_ios(void)
308{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500309 return __dm_get_module_param(&reserved_rq_based_ios,
Mike Snitzerf4790822013-09-12 18:06:12 -0400310 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
311}
312EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
313
Mike Snitzerfaad87d2016-01-28 16:52:56 -0500314static unsigned dm_get_blk_mq_nr_hw_queues(void)
315{
316 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
317}
318
319static unsigned dm_get_blk_mq_queue_depth(void)
320{
321 return __dm_get_module_param(&dm_mq_queue_depth,
322 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static int __init local_init(void)
326{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100327 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100330 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100332 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000334 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
335 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100336 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000337
Mike Snitzereca7ee62016-02-20 13:45:38 -0500338 _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500339 __alignof__(struct request), 0, NULL);
340 if (!_rq_cache)
341 goto out_free_rq_tio_cache;
342
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100343 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100344 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500345 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100346
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400347 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
348 if (!deferred_remove_workqueue) {
349 r = -ENOMEM;
350 goto out_uevent_exit;
351 }
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 _major = major;
354 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100355 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400356 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (!_major)
359 _major = r;
360
361 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100362
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400363out_free_workqueue:
364 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100365out_uevent_exit:
366 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500367out_free_rq_cache:
368 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000369out_free_rq_tio_cache:
370 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100371out_free_io_cache:
372 kmem_cache_destroy(_io_cache);
373
374 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377static void local_exit(void)
378{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400379 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400380 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400381
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500382 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000383 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700385 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100386 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 _major = 0;
389
390 DMINFO("cleaned up");
391}
392
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000393static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 local_init,
395 dm_target_init,
396 dm_linear_init,
397 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000398 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100399 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400401 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};
403
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000404static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 local_exit,
406 dm_target_exit,
407 dm_linear_exit,
408 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000409 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100410 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400412 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413};
414
415static int __init dm_init(void)
416{
417 const int count = ARRAY_SIZE(_inits);
418
419 int r, i;
420
421 for (i = 0; i < count; i++) {
422 r = _inits[i]();
423 if (r)
424 goto bad;
425 }
426
427 return 0;
428
429 bad:
430 while (i--)
431 _exits[i]();
432
433 return r;
434}
435
436static void __exit dm_exit(void)
437{
438 int i = ARRAY_SIZE(_exits);
439
440 while (i--)
441 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100442
443 /*
444 * Should be empty by this point.
445 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100446 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/*
450 * Block device functions
451 */
Mike Anderson432a2122009-12-10 23:52:20 +0000452int dm_deleting_md(struct mapped_device *md)
453{
454 return test_bit(DMF_DELETING, &md->flags);
455}
456
Al Virofe5f9f22008-03-02 10:29:31 -0500457static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct mapped_device *md;
460
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700461 spin_lock(&_minor_lock);
462
Al Virofe5f9f22008-03-02 10:29:31 -0500463 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700464 if (!md)
465 goto out;
466
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700467 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000468 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700469 md = NULL;
470 goto out;
471 }
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700474 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700475out:
476 spin_unlock(&_minor_lock);
477
478 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Al Virodb2a1442013-05-05 21:52:57 -0400481static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400483 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200484
Milan Broz4a1aeb92011-01-13 19:59:48 +0000485 spin_lock(&_minor_lock);
486
Mike Snitzer63a4f062015-03-23 17:01:43 -0400487 md = disk->private_data;
488 if (WARN_ON(!md))
489 goto out;
490
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400491 if (atomic_dec_and_test(&md->open_count) &&
492 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400493 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400496out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000497 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700500int dm_open_count(struct mapped_device *md)
501{
502 return atomic_read(&md->open_count);
503}
504
505/*
506 * Guarantees nothing is using the device before it's deleted.
507 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400508int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700509{
510 int r = 0;
511
512 spin_lock(&_minor_lock);
513
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400514 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700515 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400516 if (mark_deferred)
517 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
518 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
519 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700520 else
521 set_bit(DMF_DELETING, &md->flags);
522
523 spin_unlock(&_minor_lock);
524
525 return r;
526}
527
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400528int dm_cancel_deferred_remove(struct mapped_device *md)
529{
530 int r = 0;
531
532 spin_lock(&_minor_lock);
533
534 if (test_bit(DMF_DELETING, &md->flags))
535 r = -EBUSY;
536 else
537 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
538
539 spin_unlock(&_minor_lock);
540
541 return r;
542}
543
544static void do_deferred_remove(struct work_struct *w)
545{
546 dm_deferred_remove();
547}
548
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400549sector_t dm_get_size(struct mapped_device *md)
550{
551 return get_capacity(md->disk);
552}
553
Mike Snitzer9974fa22014-02-28 15:33:43 +0100554struct request_queue *dm_get_md_queue(struct mapped_device *md)
555{
556 return md->queue;
557}
558
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400559struct dm_stats *dm_get_stats(struct mapped_device *md)
560{
561 return &md->stats;
562}
563
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800564static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
565{
566 struct mapped_device *md = bdev->bd_disk->private_data;
567
568 return dm_get_geometry(md, geo);
569}
570
Mike Snitzer956a4022016-02-18 16:13:51 -0500571static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
572 struct block_device **bdev,
573 fmode_t *mode)
Milan Brozaa129a22006-10-03 01:15:15 -0700574{
Mike Snitzer66482022016-02-18 15:44:39 -0500575 struct dm_target *tgt;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100576 struct dm_table *map;
Mike Snitzer956a4022016-02-18 16:13:51 -0500577 int srcu_idx, r;
Milan Brozaa129a22006-10-03 01:15:15 -0700578
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100579retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200580 r = -ENOTTY;
Mike Snitzer956a4022016-02-18 16:13:51 -0500581 map = dm_get_live_table(md, &srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700582 if (!map || !dm_table_get_size(map))
583 goto out;
584
585 /* We only support devices that have a single target */
586 if (dm_table_get_num_targets(map) != 1)
587 goto out;
588
Mike Snitzer66482022016-02-18 15:44:39 -0500589 tgt = dm_table_get_target(map, 0);
590 if (!tgt->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500591 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700592
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000593 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700594 r = -EAGAIN;
595 goto out;
596 }
597
Mike Snitzer66482022016-02-18 15:44:39 -0500598 r = tgt->type->prepare_ioctl(tgt, bdev, mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200599 if (r < 0)
600 goto out;
601
Mike Snitzer956a4022016-02-18 16:13:51 -0500602 bdgrab(*bdev);
603 dm_put_live_table(md, srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200604 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700605
606out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500607 dm_put_live_table(md, srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000608 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100609 msleep(10);
610 goto retry;
611 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200612 return r;
613}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100614
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200615static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
616 unsigned int cmd, unsigned long arg)
617{
618 struct mapped_device *md = bdev->bd_disk->private_data;
Mike Snitzer956a4022016-02-18 16:13:51 -0500619 int r;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200620
Mike Snitzer956a4022016-02-18 16:13:51 -0500621 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200622 if (r < 0)
623 return r;
624
625 if (r > 0) {
626 /*
627 * Target determined this ioctl is being issued against
628 * a logical partition of the parent bdev; so extra
629 * validation is needed.
630 */
631 r = scsi_verify_blk_ioctl(NULL, cmd);
632 if (r)
633 goto out;
634 }
635
Mike Snitzer66482022016-02-18 15:44:39 -0500636 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200637out:
Mike Snitzer956a4022016-02-18 16:13:51 -0500638 bdput(bdev);
Milan Brozaa129a22006-10-03 01:15:15 -0700639 return r;
640}
641
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100642static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 return mempool_alloc(md->io_pool, GFP_NOIO);
645}
646
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100647static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 mempool_free(io, md->io_pool);
650}
651
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100652static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Mikulas Patockadba14162012-10-12 21:02:15 +0100654 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Mike Snitzereca7ee62016-02-20 13:45:38 -0500657static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md,
658 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100659{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000660 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100661}
662
Mike Snitzereca7ee62016-02-20 13:45:38 -0500663static void free_old_rq_tio(struct dm_rq_target_io *tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100664{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000665 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100666}
667
Mike Snitzereca7ee62016-02-20 13:45:38 -0500668static struct request *alloc_old_clone_request(struct mapped_device *md,
669 gfp_t gfp_mask)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500670{
671 return mempool_alloc(md->rq_pool, gfp_mask);
672}
673
Mike Snitzereca7ee62016-02-20 13:45:38 -0500674static void free_old_clone_request(struct mapped_device *md, struct request *rq)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500675{
676 mempool_free(rq, md->rq_pool);
677}
678
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000679static int md_in_flight(struct mapped_device *md)
680{
681 return atomic_read(&md->pending[READ]) +
682 atomic_read(&md->pending[WRITE]);
683}
684
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800685static void start_io_acct(struct dm_io *io)
686{
687 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400688 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900689 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400690 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800691
692 io->start_time = jiffies;
693
Tejun Heo074a7ac2008-08-25 19:56:14 +0900694 cpu = part_stat_lock();
695 part_round_stats(cpu, &dm_disk(md)->part0);
696 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100697 atomic_set(&dm_disk(md)->part0.in_flight[rw],
698 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400699
700 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700701 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400702 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800703}
704
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000705static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800706{
707 struct mapped_device *md = io->md;
708 struct bio *bio = io->bio;
709 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800710 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800711 int rw = bio_data_dir(bio);
712
Gu Zheng18c0b222014-11-24 11:05:26 +0800713 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800714
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400715 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700716 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400717 bio_sectors(bio), true, duration, &io->stats_aux);
718
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100719 /*
720 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200721 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100722 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100723 pending = atomic_dec_return(&md->pending[rw]);
724 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200725 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800726
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000727 /* nudge anyone waiting on suspend queue */
728 if (!pending)
729 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*
733 * Add the bio to the list of deferred io.
734 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100735static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200737 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200739 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200741 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200742 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745/*
746 * Everyone (including functions in this file), should use this
747 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100748 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100750struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100752 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100754 return srcu_dereference(md->map, &md->io_barrier);
755}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100757void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
758{
759 srcu_read_unlock(&md->io_barrier, srcu_idx);
760}
761
762void dm_sync_table(struct mapped_device *md)
763{
764 synchronize_srcu(&md->io_barrier);
765 synchronize_rcu_expedited();
766}
767
768/*
769 * A fast alternative to dm_get_live_table/dm_put_live_table.
770 * The caller must not block between these two functions.
771 */
772static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
773{
774 rcu_read_lock();
775 return rcu_dereference(md->map);
776}
777
778static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
779{
780 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800783/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500784 * Open a table device so we can use it as a map destination.
785 */
786static int open_table_device(struct table_device *td, dev_t dev,
787 struct mapped_device *md)
788{
789 static char *_claim_ptr = "I belong to device-mapper";
790 struct block_device *bdev;
791
792 int r;
793
794 BUG_ON(td->dm_dev.bdev);
795
796 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
797 if (IS_ERR(bdev))
798 return PTR_ERR(bdev);
799
800 r = bd_link_disk_holder(bdev, dm_disk(md));
801 if (r) {
802 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
803 return r;
804 }
805
806 td->dm_dev.bdev = bdev;
807 return 0;
808}
809
810/*
811 * Close a table device that we've been using.
812 */
813static void close_table_device(struct table_device *td, struct mapped_device *md)
814{
815 if (!td->dm_dev.bdev)
816 return;
817
818 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
819 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
820 td->dm_dev.bdev = NULL;
821}
822
823static struct table_device *find_table_device(struct list_head *l, dev_t dev,
824 fmode_t mode) {
825 struct table_device *td;
826
827 list_for_each_entry(td, l, list)
828 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
829 return td;
830
831 return NULL;
832}
833
834int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
835 struct dm_dev **result) {
836 int r;
837 struct table_device *td;
838
839 mutex_lock(&md->table_devices_lock);
840 td = find_table_device(&md->table_devices, dev, mode);
841 if (!td) {
842 td = kmalloc(sizeof(*td), GFP_KERNEL);
843 if (!td) {
844 mutex_unlock(&md->table_devices_lock);
845 return -ENOMEM;
846 }
847
848 td->dm_dev.mode = mode;
849 td->dm_dev.bdev = NULL;
850
851 if ((r = open_table_device(td, dev, md))) {
852 mutex_unlock(&md->table_devices_lock);
853 kfree(td);
854 return r;
855 }
856
857 format_dev_t(td->dm_dev.name, dev);
858
859 atomic_set(&td->count, 0);
860 list_add(&td->list, &md->table_devices);
861 }
862 atomic_inc(&td->count);
863 mutex_unlock(&md->table_devices_lock);
864
865 *result = &td->dm_dev;
866 return 0;
867}
868EXPORT_SYMBOL_GPL(dm_get_table_device);
869
870void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
871{
872 struct table_device *td = container_of(d, struct table_device, dm_dev);
873
874 mutex_lock(&md->table_devices_lock);
875 if (atomic_dec_and_test(&td->count)) {
876 close_table_device(td, md);
877 list_del(&td->list);
878 kfree(td);
879 }
880 mutex_unlock(&md->table_devices_lock);
881}
882EXPORT_SYMBOL(dm_put_table_device);
883
884static void free_table_devices(struct list_head *devices)
885{
886 struct list_head *tmp, *next;
887
888 list_for_each_safe(tmp, next, devices) {
889 struct table_device *td = list_entry(tmp, struct table_device, list);
890
891 DMWARN("dm_destroy: %s still exists with %d references",
892 td->dm_dev.name, atomic_read(&td->count));
893 kfree(td);
894 }
895}
896
897/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800898 * Get the geometry associated with a dm device
899 */
900int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
901{
902 *geo = md->geometry;
903
904 return 0;
905}
906
907/*
908 * Set the geometry of a device.
909 */
910int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
911{
912 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
913
914 if (geo->start > sz) {
915 DMWARN("Start sector is beyond the geometry limits.");
916 return -EINVAL;
917 }
918
919 md->geometry = *geo;
920
921 return 0;
922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/*-----------------------------------------------------------------
925 * CRUD START:
926 * A more elegant soln is in the works that uses the queue
927 * merge fn, unfortunately there are a couple of changes to
928 * the block layer that I want to make for this. So in the
929 * interests of getting something for people to use I give
930 * you this clearly demarcated crap.
931 *---------------------------------------------------------------*/
932
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800933static int __noflush_suspending(struct mapped_device *md)
934{
935 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*
939 * Decrements the number of outstanding ios that a bio has been
940 * cloned into, completing the original io if necc.
941 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800942static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800944 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000945 int io_error;
946 struct bio *bio;
947 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800948
949 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100950 if (unlikely(error)) {
951 spin_lock_irqsave(&io->endio_lock, flags);
952 if (!(io->error > 0 && __noflush_suspending(md)))
953 io->error = error;
954 spin_unlock_irqrestore(&io->endio_lock, flags);
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800958 if (io->error == DM_ENDIO_REQUEUE) {
959 /*
960 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800961 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100962 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200963 if (__noflush_suspending(md))
964 bio_list_add_head(&md->deferred, io->bio);
965 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800966 /* noflush suspend was interrupted. */
967 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100968 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800969 }
970
Milan Brozb35f8ca2009-03-16 17:44:36 +0000971 io_error = io->error;
972 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200973 end_io_acct(io);
974 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100975
Tejun Heo6a8736d2010-09-08 18:07:00 +0200976 if (io_error == DM_ENDIO_REQUEUE)
977 return;
978
Kent Overstreet4f024f32013-10-11 15:44:27 -0700979 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100980 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200981 * Preflush done for flush with data, reissue
982 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100983 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200984 bio->bi_rw &= ~REQ_FLUSH;
985 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100986 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200987 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700988 trace_block_bio_complete(md->queue, bio, io_error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200989 bio->bi_error = io_error;
990 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993}
994
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400995static void disable_write_same(struct mapped_device *md)
996{
997 struct queue_limits *limits = dm_get_queue_limits(md);
998
999 /* device doesn't really support WRITE SAME, disable it */
1000 limits->max_write_same_sectors = 0;
1001}
1002
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001003static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001005 int error = bio->bi_error;
zhendong chen5164bec2014-12-17 14:37:04 +08001006 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -05001007 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001008 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -07001009 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 dm_endio_fn endio = tio->ti->type->end_io;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001013 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001014 if (r < 0 || r == DM_ENDIO_REQUEUE)
1015 /*
1016 * error and requeue request are handled
1017 * in dec_pending().
1018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001020 else if (r == DM_ENDIO_INCOMPLETE)
1021 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +02001022 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001023 else if (r) {
1024 DMWARN("unimplemented target endio return value: %d", r);
1025 BUG();
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001029 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
1030 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
1031 disable_write_same(md);
1032
Stefan Bader9faf4002006-10-03 01:15:41 -07001033 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001034 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Mike Snitzer78d8e582015-06-26 10:01:13 -04001037/*
1038 * Partial completion handling for request-based dm
1039 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001040static void end_clone_bio(struct bio *clone)
Mike Snitzer78d8e582015-06-26 10:01:13 -04001041{
1042 struct dm_rq_clone_bio_info *info =
1043 container_of(clone, struct dm_rq_clone_bio_info, clone);
1044 struct dm_rq_target_io *tio = info->tio;
1045 struct bio *bio = info->orig;
1046 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001047 int error = clone->bi_error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001048
1049 bio_put(clone);
1050
1051 if (tio->error)
1052 /*
1053 * An error has already been detected on the request.
1054 * Once error occurred, just let clone->end_io() handle
1055 * the remainder.
1056 */
1057 return;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001058 else if (error) {
Mike Snitzer78d8e582015-06-26 10:01:13 -04001059 /*
1060 * Don't notice the error to the upper layer yet.
1061 * The error handling decision is made by the target driver,
1062 * when the request is completed.
1063 */
Junichi Nomura50887bd2015-10-06 04:19:54 +00001064 tio->error = error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001065 return;
1066 }
1067
1068 /*
1069 * I/O for the bio successfully completed.
1070 * Notice the data completion to the upper layer.
1071 */
1072
1073 /*
1074 * bios are processed from the head of the list.
1075 * So the completing bio should always be rq->bio.
1076 * If it's not, something wrong is happening.
1077 */
1078 if (tio->orig->bio != bio)
1079 DMERR("bio completion is going in the middle of the request");
1080
1081 /*
1082 * Update the original request.
1083 * Do not use blk_end_request() here, because it may complete
1084 * the original request before the clone, and break the ordering.
1085 */
1086 blk_update_request(tio->orig, 0, nr_bytes);
1087}
1088
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001089static struct dm_rq_target_io *tio_from_request(struct request *rq)
1090{
1091 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1092}
1093
Mikulas Patockae262f342015-06-09 17:22:49 -04001094static void rq_end_stats(struct mapped_device *md, struct request *orig)
1095{
1096 if (unlikely(dm_stats_used(&md->stats))) {
1097 struct dm_rq_target_io *tio = tio_from_request(orig);
1098 tio->duration_jiffies = jiffies - tio->duration_jiffies;
1099 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1100 tio->n_sectors, true, tio->duration_jiffies,
1101 &tio->stats_aux);
1102 }
1103}
1104
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001105/*
1106 * Don't touch any member of the md after calling this function because
1107 * the md may be freed in dm_put() at the end of this function.
1108 * Or do dm_get() before calling this function and dm_put() later.
1109 */
Keith Busch466d89a2014-10-17 17:46:37 -06001110static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001111{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001112 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001113
1114 /* nudge anyone waiting on suspend queue */
Mike Snitzer621739b2015-07-08 16:08:24 -04001115 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001116 wake_up(&md->wait);
1117
Jens Axboea8c32a52012-11-06 12:24:26 +01001118 /*
1119 * Run this off this callpath, as drivers could invoke end_io while
1120 * inside their request_fn (and holding the queue lock). Calling
1121 * back into ->request_fn() could deadlock attempting to grab the
1122 * queue lock again.
1123 */
Mike Snitzer6acfe682016-02-05 08:49:01 -05001124 if (!md->queue->mq_ops && run_queue)
1125 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001126
1127 /*
1128 * dm_put() must be at the end of this function. See the comment above
1129 */
1130 dm_put(md);
1131}
1132
Mike Snitzere5d8de32015-05-28 15:12:52 -04001133static void free_rq_clone(struct request *clone)
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001134{
1135 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001136 struct mapped_device *md = tio->md;
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001137
Mike Snitzer78d8e582015-06-26 10:01:13 -04001138 blk_rq_unprep_clone(clone);
1139
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001140 if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1141 /* stacked on blk-mq queue(s) */
Mike Snitzere5863d92014-12-17 21:08:12 -05001142 tio->ti->type->release_clone_rq(clone);
Mike Snitzer02233342015-03-10 23:49:26 -04001143 else if (!md->queue->mq_ops)
1144 /* request_fn queue stacked on request_fn queue(s) */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001145 free_old_clone_request(md, clone);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001146
1147 if (!md->queue->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001148 free_old_rq_tio(tio);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001149}
1150
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001151/*
1152 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001153 * Must be called without clone's queue lock held,
1154 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001155 */
1156static void dm_end_request(struct request *clone, int error)
1157{
1158 int rw = rq_data_dir(clone);
1159 struct dm_rq_target_io *tio = clone->end_io_data;
1160 struct mapped_device *md = tio->md;
1161 struct request *rq = tio->orig;
1162
Tejun Heo29e40132010-09-08 18:07:00 +02001163 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001164 rq->errors = clone->errors;
1165 rq->resid_len = clone->resid_len;
1166
1167 if (rq->sense)
1168 /*
1169 * We are using the sense buffer of the original
1170 * request.
1171 * So setting the length of the sense data is enough.
1172 */
1173 rq->sense_len = clone->sense_len;
1174 }
1175
Mike Snitzere5d8de32015-05-28 15:12:52 -04001176 free_rq_clone(clone);
Mikulas Patockae262f342015-06-09 17:22:49 -04001177 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001178 if (!rq->q->mq_ops)
1179 blk_end_request_all(rq, error);
1180 else
1181 blk_mq_end_request(rq, error);
Tejun Heo29e40132010-09-08 18:07:00 +02001182 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001183}
1184
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001185static void dm_unprep_request(struct request *rq)
1186{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001187 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001188 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001189
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001190 if (!rq->q->mq_ops) {
1191 rq->special = NULL;
1192 rq->cmd_flags &= ~REQ_DONTPREP;
1193 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001194
Mike Snitzere5863d92014-12-17 21:08:12 -05001195 if (clone)
Mike Snitzere5d8de32015-05-28 15:12:52 -04001196 free_rq_clone(clone);
Mike Snitzer4328daa2016-02-21 19:09:22 -05001197 else if (!tio->md->queue->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001198 free_old_rq_tio(tio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001199}
1200
1201/*
1202 * Requeue the original request of a clone.
1203 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001204static void dm_old_requeue_request(struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001205{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001206 struct request_queue *q = rq->q;
1207 unsigned long flags;
1208
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001209 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001210 blk_requeue_request(q, rq);
Junichi Nomura4ae99442015-05-26 08:25:54 +00001211 blk_run_queue_async(q);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001212 spin_unlock_irqrestore(q->queue_lock, flags);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001213}
1214
Mike Snitzer818c5f32016-02-20 00:38:47 -05001215static void dm_mq_requeue_request(struct request *rq)
1216{
1217 struct request_queue *q = rq->q;
1218 unsigned long flags;
1219
1220 blk_mq_requeue_request(rq);
1221 spin_lock_irqsave(q->queue_lock, flags);
1222 if (!blk_queue_stopped(q))
1223 blk_mq_kick_requeue_list(q);
1224 spin_unlock_irqrestore(q->queue_lock, flags);
1225}
1226
Mike Snitzer2d76fff12015-04-29 12:07:12 -04001227static void dm_requeue_original_request(struct mapped_device *md,
1228 struct request *rq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001229{
1230 int rw = rq_data_dir(rq);
1231
1232 dm_unprep_request(rq);
1233
Mikulas Patockae262f342015-06-09 17:22:49 -04001234 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001235 if (!rq->q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001236 dm_old_requeue_request(rq);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001237 else
1238 dm_mq_requeue_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001239
Keith Busch466d89a2014-10-17 17:46:37 -06001240 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001241}
Keith Busch466d89a2014-10-17 17:46:37 -06001242
Mike Snitzereca7ee62016-02-20 13:45:38 -05001243static void dm_old_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001244{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001245 unsigned long flags;
1246
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001247 spin_lock_irqsave(q->queue_lock, flags);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001248 if (blk_queue_stopped(q)) {
1249 spin_unlock_irqrestore(q->queue_lock, flags);
1250 return;
1251 }
1252
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001253 blk_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001254 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001255}
1256
Mike Snitzereca7ee62016-02-20 13:45:38 -05001257static void dm_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001258{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001259 if (!q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001260 dm_old_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001261 else
1262 blk_mq_stop_hw_queues(q);
1263}
1264
Mike Snitzereca7ee62016-02-20 13:45:38 -05001265static void dm_old_start_queue(struct request_queue *q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001266{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001267 unsigned long flags;
1268
1269 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001270 if (blk_queue_stopped(q))
1271 blk_start_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001272 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001273}
1274
Mike Snitzereca7ee62016-02-20 13:45:38 -05001275static void dm_start_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001276{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001277 if (!q->mq_ops)
Mike Snitzereca7ee62016-02-20 13:45:38 -05001278 dm_old_start_queue(q);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001279 else {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001280 blk_mq_start_stopped_hw_queues(q, true);
Mike Snitzer818c5f32016-02-20 00:38:47 -05001281 blk_mq_kick_requeue_list(q);
1282 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001283}
1284
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001285static void dm_done(struct request *clone, int error, bool mapped)
1286{
1287 int r = error;
1288 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001289 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001290
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001291 if (tio->ti) {
1292 rq_end_io = tio->ti->type->rq_end_io;
1293
1294 if (mapped && rq_end_io)
1295 r = rq_end_io(tio->ti, clone, error, &tio->info);
1296 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001297
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001298 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1299 !clone->q->limits.max_write_same_sectors))
1300 disable_write_same(tio->md);
1301
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001302 if (r <= 0)
1303 /* The target wants to complete the I/O */
1304 dm_end_request(clone, r);
1305 else if (r == DM_ENDIO_INCOMPLETE)
1306 /* The target will handle the I/O */
1307 return;
1308 else if (r == DM_ENDIO_REQUEUE)
1309 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff12015-04-29 12:07:12 -04001310 dm_requeue_original_request(tio->md, tio->orig);
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001311 else {
1312 DMWARN("unimplemented target endio return value: %d", r);
1313 BUG();
1314 }
1315}
1316
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001317/*
1318 * Request completion handler for request-based dm
1319 */
1320static void dm_softirq_done(struct request *rq)
1321{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001322 bool mapped = true;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001323 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001324 struct request *clone = tio->clone;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001325 int rw;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001326
Mike Snitzere5863d92014-12-17 21:08:12 -05001327 if (!clone) {
Mikulas Patockae262f342015-06-09 17:22:49 -04001328 rq_end_stats(tio->md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001329 rw = rq_data_dir(rq);
1330 if (!rq->q->mq_ops) {
1331 blk_end_request_all(rq, tio->error);
1332 rq_completed(tio->md, rw, false);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001333 free_old_rq_tio(tio);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001334 } else {
1335 blk_mq_end_request(rq, tio->error);
1336 rq_completed(tio->md, rw, false);
1337 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001338 return;
1339 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001340
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001341 if (rq->cmd_flags & REQ_FAILED)
1342 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001343
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001344 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001345}
1346
1347/*
1348 * Complete the clone and the original request with the error status
1349 * through softirq context.
1350 */
Keith Busch466d89a2014-10-17 17:46:37 -06001351static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001352{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001353 struct dm_rq_target_io *tio = tio_from_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001354
1355 tio->error = error;
Mike Snitzer6acfe682016-02-05 08:49:01 -05001356 if (!rq->q->mq_ops)
1357 blk_complete_request(rq);
1358 else
1359 blk_mq_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001360}
1361
1362/*
1363 * Complete the not-mapped clone and the original request with the error status
1364 * through softirq context.
1365 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001366 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001367 */
Keith Busch466d89a2014-10-17 17:46:37 -06001368static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001369{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001370 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001371 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001372}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001373
1374/*
Mike Snitzereca7ee62016-02-20 13:45:38 -05001375 * Called with the clone's queue lock held (in the case of .request_fn)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001376 */
1377static void end_clone_request(struct request *clone, int error)
1378{
Keith Busch466d89a2014-10-17 17:46:37 -06001379 struct dm_rq_target_io *tio = clone->end_io_data;
1380
Mike Snitzere5863d92014-12-17 21:08:12 -05001381 if (!clone->q->mq_ops) {
1382 /*
1383 * For just cleaning up the information of the queue in which
1384 * the clone was dispatched.
1385 * The clone is *NOT* freed actually here because it is alloced
1386 * from dm own mempool (REQ_ALLOCED isn't set).
1387 */
1388 __blk_put_request(clone->q, clone);
1389 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001390
1391 /*
1392 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001393 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001394 * - another request may be submitted by the upper level driver
1395 * of the stacking during the completion
1396 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001397 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001398 */
Keith Busch466d89a2014-10-17 17:46:37 -06001399 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001400}
1401
Mike Snitzer56a67df2010-08-12 04:14:10 +01001402/*
1403 * Return maximum size of I/O possible at the supplied sector up to the current
1404 * target boundary.
1405 */
1406static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001408 sector_t target_offset = dm_target_offset(ti, sector);
1409
1410 return ti->len - target_offset;
1411}
1412
1413static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1414{
1415 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001416 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001419 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001421 if (ti->max_io_len) {
1422 offset = dm_target_offset(ti, sector);
1423 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1424 max_len = sector_div(offset, ti->max_io_len);
1425 else
1426 max_len = offset & (ti->max_io_len - 1);
1427 max_len = ti->max_io_len - max_len;
1428
1429 if (len > max_len)
1430 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
1433 return len;
1434}
1435
Mike Snitzer542f9032012-07-27 15:08:00 +01001436int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1437{
1438 if (len > UINT_MAX) {
1439 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1440 (unsigned long long)len, UINT_MAX);
1441 ti->error = "Maximum size of target IO is too large";
1442 return -EINVAL;
1443 }
1444
1445 ti->max_io_len = (uint32_t) len;
1446
1447 return 0;
1448}
1449EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1450
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001451/*
1452 * A target may call dm_accept_partial_bio only from the map routine. It is
1453 * allowed for all bio types except REQ_FLUSH.
1454 *
1455 * dm_accept_partial_bio informs the dm that the target only wants to process
1456 * additional n_sectors sectors of the bio and the rest of the data should be
1457 * sent in a next bio.
1458 *
1459 * A diagram that explains the arithmetics:
1460 * +--------------------+---------------+-------+
1461 * | 1 | 2 | 3 |
1462 * +--------------------+---------------+-------+
1463 *
1464 * <-------------- *tio->len_ptr --------------->
1465 * <------- bi_size ------->
1466 * <-- n_sectors -->
1467 *
1468 * Region 1 was already iterated over with bio_advance or similar function.
1469 * (it may be empty if the target doesn't use bio_advance)
1470 * Region 2 is the remaining bio size that the target wants to process.
1471 * (it may be empty if region 1 is non-empty, although there is no reason
1472 * to make it empty)
1473 * The target requires that region 3 is to be sent in the next bio.
1474 *
1475 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1476 * the partially processed part (the sum of regions 1+2) must be the same for all
1477 * copies of the bio.
1478 */
1479void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1480{
1481 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1482 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1483 BUG_ON(bio->bi_rw & REQ_FLUSH);
1484 BUG_ON(bi_size > *tio->len_ptr);
1485 BUG_ON(n_sectors > bi_size);
1486 *tio->len_ptr -= bi_size - n_sectors;
1487 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1488}
1489EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1490
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001491static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001494 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001495 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001496 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001497 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /*
1502 * Map the clone. If r == 0 we don't need to do
1503 * anything, the target has assumed ownership of
1504 * this io.
1505 */
1506 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001507 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001508 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001509 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001511
Mike Snitzerd07335e2010-11-16 12:52:38 +01001512 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1513 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001516 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1517 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001518 md = tio->io->md;
1519 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001520 free_tio(md, tio);
Mikulas Patockaab378442015-07-01 17:30:36 -04001521 } else if (r != DM_MAPIO_SUBMITTED) {
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001522 DMWARN("unimplemented target map return value: %d", r);
1523 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525}
1526
1527struct clone_info {
1528 struct mapped_device *md;
1529 struct dm_table *map;
1530 struct bio *bio;
1531 struct dm_io *io;
1532 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001533 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534};
1535
Mikulas Patockae0d66092014-03-14 18:40:39 -04001536static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001537{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001538 bio->bi_iter.bi_sector = sector;
1539 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542/*
1543 * Creates a bio that consists of range of complete bvecs.
1544 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001545static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001546 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Mikulas Patockadba14162012-10-12 21:02:15 +01001548 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001550 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001552 if (bio_integrity(bio))
1553 bio_integrity_clone(clone, bio, GFP_NOIO);
1554
1555 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1556 clone->bi_iter.bi_size = to_bytes(len);
1557
1558 if (bio_integrity(bio))
1559 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001562static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001563 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001564 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001565{
Mikulas Patockadba14162012-10-12 21:02:15 +01001566 struct dm_target_io *tio;
1567 struct bio *clone;
1568
Junichi Nomura99778272014-10-03 11:55:16 +00001569 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001570 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001571
1572 tio->io = ci->io;
1573 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001574 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001575
1576 return tio;
1577}
1578
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001579static void __clone_and_map_simple_bio(struct clone_info *ci,
1580 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001581 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001582{
Junichi Nomura99778272014-10-03 11:55:16 +00001583 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001584 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001585
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001586 tio->len_ptr = len;
1587
Junichi Nomura99778272014-10-03 11:55:16 +00001588 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001589 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001590 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001591
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001592 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001593}
1594
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001595static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001596 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001597{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001598 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001599
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001600 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001601 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001602}
1603
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001604static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001605{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001606 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001607 struct dm_target *ti;
1608
Mike Snitzerb372d362010-09-08 18:07:01 +02001609 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001610 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001611 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001612
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001613 return 0;
1614}
1615
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001616static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001617 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001618{
Mikulas Patockadba14162012-10-12 21:02:15 +01001619 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001620 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001621 unsigned target_bio_nr;
1622 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001623
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001624 /*
1625 * Does the target want to receive duplicate copies of the bio?
1626 */
1627 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1628 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001629
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001630 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001631 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001632 tio->len_ptr = len;
1633 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001634 __map_bio(tio);
1635 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001636}
1637
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001638typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001639
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001640static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001641{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001642 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001643}
1644
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001645static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001646{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001647 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001648}
1649
1650typedef bool (*is_split_required_fn)(struct dm_target *ti);
1651
1652static bool is_split_required_for_discard(struct dm_target *ti)
1653{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001654 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001655}
1656
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001657static int __send_changing_extent_only(struct clone_info *ci,
1658 get_num_bios_fn get_num_bios,
1659 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001660{
1661 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001662 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001663 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001664
Mike Snitzera79245b2010-08-12 04:14:24 +01001665 do {
1666 ti = dm_table_find_target(ci->map, ci->sector);
1667 if (!dm_target_is_valid(ti))
1668 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001669
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001670 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001671 * Even though the device advertised support for this type of
1672 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001673 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001674 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001675 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001676 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1677 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001678 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001679
Mike Snitzer23508a92012-12-21 20:23:37 +00001680 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001681 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001682 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001683 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001684
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001685 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001686
1687 ci->sector += len;
1688 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001689
1690 return 0;
1691}
1692
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001693static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001694{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001695 return __send_changing_extent_only(ci, get_num_discard_bios,
1696 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001697}
1698
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001699static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001700{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001701 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001702}
1703
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001704/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001705 * Select the correct strategy for processing a non-flush bio.
1706 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001707static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Mikulas Patockadba14162012-10-12 21:02:15 +01001709 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001710 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001711 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001713 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001714 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001715 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001716 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001717
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001718 ti = dm_table_find_target(ci->map, ci->sector);
1719 if (!dm_target_is_valid(ti))
1720 return -EIO;
1721
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001722 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001723
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001724 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001726 ci->sector += len;
1727 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001733 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001735static void __split_and_process_bio(struct mapped_device *md,
1736 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001739 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001741 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001742 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001743 return;
1744 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001745
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001746 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 ci.io = alloc_io(md);
1749 ci.io->error = 0;
1750 atomic_set(&ci.io->io_count, 1);
1751 ci.io->bio = bio;
1752 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001753 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001754 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001756 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001757
Mike Snitzerb372d362010-09-08 18:07:01 +02001758 if (bio->bi_rw & REQ_FLUSH) {
1759 ci.bio = &ci.md->flush_bio;
1760 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001761 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001762 /* dec_pending submits any data associated with flush */
1763 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001764 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001765 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001766 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001767 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001771 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773/*-----------------------------------------------------------------
1774 * CRUD END
1775 *---------------------------------------------------------------*/
1776
1777/*
1778 * The request function that just remaps the bio built up by
1779 * dm_merge_bvec.
1780 */
Jens Axboedece1632015-11-05 10:41:16 -07001781static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Kevin Corry12f03a42006-02-01 03:04:52 -08001783 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001785 int srcu_idx;
1786 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001788 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Gu Zheng18c0b222014-11-24 11:05:26 +08001790 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001791
Tejun Heo6a8736d2010-09-08 18:07:00 +02001792 /* if we're suspended, we have to queue this io for later */
1793 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001794 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Tejun Heo6a8736d2010-09-08 18:07:00 +02001796 if (bio_rw(bio) != READA)
1797 queue_io(md, bio);
1798 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001799 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001800 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001803 __split_and_process_bio(md, map, bio);
1804 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001805 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001806}
1807
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001808int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001809{
1810 return blk_queue_stackable(md->queue);
1811}
1812
Keith Busch466d89a2014-10-17 17:46:37 -06001813static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001814{
1815 int r;
1816
Keith Busch466d89a2014-10-17 17:46:37 -06001817 if (blk_queue_io_stat(clone->q))
1818 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001819
Keith Busch466d89a2014-10-17 17:46:37 -06001820 clone->start_time = jiffies;
1821 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001822 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001823 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001824 dm_complete_request(rq, r);
1825}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001826
Mike Snitzer78d8e582015-06-26 10:01:13 -04001827static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1828 void *data)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001829{
Mike Snitzer78d8e582015-06-26 10:01:13 -04001830 struct dm_rq_target_io *tio = data;
1831 struct dm_rq_clone_bio_info *info =
1832 container_of(bio, struct dm_rq_clone_bio_info, clone);
1833
1834 info->orig = bio_orig;
1835 info->tio = tio;
1836 bio->bi_end_io = end_clone_bio;
1837
1838 return 0;
1839}
1840
1841static int setup_clone(struct request *clone, struct request *rq,
1842 struct dm_rq_target_io *tio, gfp_t gfp_mask)
1843{
1844 int r;
1845
1846 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1847 dm_rq_bio_constructor, tio);
1848 if (r)
1849 return r;
1850
1851 clone->cmd = rq->cmd;
1852 clone->cmd_len = rq->cmd_len;
1853 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001854 clone->end_io = end_clone_request;
1855 clone->end_io_data = tio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001856
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001857 tio->clone = clone;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001858
1859 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001860}
1861
Mike Snitzereca7ee62016-02-20 13:45:38 -05001862static struct request *clone_old_rq(struct request *rq, struct mapped_device *md,
1863 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001864{
Mike Snitzer02233342015-03-10 23:49:26 -04001865 /*
Mike Snitzerc5248f72016-02-20 14:02:49 -05001866 * Create clone for use with .request_fn request_queue
Mike Snitzer02233342015-03-10 23:49:26 -04001867 */
Mike Snitzer02233342015-03-10 23:49:26 -04001868 struct request *clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001869
Mike Snitzereca7ee62016-02-20 13:45:38 -05001870 clone = alloc_old_clone_request(md, gfp_mask);
Mike Snitzerc5248f72016-02-20 14:02:49 -05001871 if (!clone)
1872 return NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001873
1874 blk_rq_init(NULL, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001875 if (setup_clone(clone, rq, tio, gfp_mask)) {
1876 /* -ENOMEM */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001877 free_old_clone_request(md, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001878 return NULL;
1879 }
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001880
1881 return clone;
1882}
1883
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001884static void map_tio_request(struct kthread_work *work);
1885
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001886static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1887 struct mapped_device *md)
1888{
1889 tio->md = md;
1890 tio->ti = NULL;
1891 tio->clone = NULL;
1892 tio->orig = rq;
1893 tio->error = 0;
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001894 /*
1895 * Avoid initializing info for blk-mq; it passes
1896 * target-specific data through info.ptr
1897 * (see: dm_mq_init_request)
1898 */
1899 if (!md->init_tio_pdu)
1900 memset(&tio->info, 0, sizeof(tio->info));
Mike Snitzer02233342015-03-10 23:49:26 -04001901 if (md->kworker_task)
1902 init_kthread_work(&tio->work, map_tio_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001903}
1904
Mike Snitzereca7ee62016-02-20 13:45:38 -05001905static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq,
1906 struct mapped_device *md,
1907 gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001908{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001909 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001910 int srcu_idx;
1911 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001912
Mike Snitzereca7ee62016-02-20 13:45:38 -05001913 tio = alloc_old_rq_tio(md, gfp_mask);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001914 if (!tio)
1915 return NULL;
1916
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001917 init_tio(tio, rq, md);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001918
Mike Snitzere5863d92014-12-17 21:08:12 -05001919 table = dm_get_live_table(md, &srcu_idx);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001920 /*
1921 * Must clone a request if this .request_fn DM device
1922 * is stacked on .request_fn device(s).
1923 */
Mike Snitzere5863d92014-12-17 21:08:12 -05001924 if (!dm_table_mq_request_based(table)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05001925 if (!clone_old_rq(rq, md, tio, gfp_mask)) {
Mike Snitzere5863d92014-12-17 21:08:12 -05001926 dm_put_live_table(md, srcu_idx);
Mike Snitzereca7ee62016-02-20 13:45:38 -05001927 free_old_rq_tio(tio);
Mike Snitzere5863d92014-12-17 21:08:12 -05001928 return NULL;
1929 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001930 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001931 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001932
Keith Busch466d89a2014-10-17 17:46:37 -06001933 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001934}
1935
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001936/*
1937 * Called with the queue lock held.
1938 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05001939static int dm_old_prep_fn(struct request_queue *q, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001940{
1941 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001942 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001943
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001944 if (unlikely(rq->special)) {
1945 DMWARN("Already has something in rq->special.");
1946 return BLKPREP_KILL;
1947 }
1948
Mike Snitzereca7ee62016-02-20 13:45:38 -05001949 tio = dm_old_prep_tio(rq, md, GFP_ATOMIC);
Keith Busch466d89a2014-10-17 17:46:37 -06001950 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001951 return BLKPREP_DEFER;
1952
Keith Busch466d89a2014-10-17 17:46:37 -06001953 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001954 rq->cmd_flags |= REQ_DONTPREP;
1955
1956 return BLKPREP_OK;
1957}
1958
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001959/*
1960 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05001961 * 0 : the request has been processed
1962 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1963 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001964 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001965static int map_request(struct dm_rq_target_io *tio, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001966 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001967{
Mike Snitzere5863d92014-12-17 21:08:12 -05001968 int r;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001969 struct dm_target *ti = tio->ti;
Mike Snitzere5863d92014-12-17 21:08:12 -05001970 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001971
Mike Snitzere5863d92014-12-17 21:08:12 -05001972 if (tio->clone) {
1973 clone = tio->clone;
1974 r = ti->type->map_rq(ti, clone, &tio->info);
1975 } else {
1976 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1977 if (r < 0) {
1978 /* The target wants to complete the I/O */
1979 dm_kill_unmapped_request(rq, r);
1980 return r;
1981 }
Junichi Nomura3a140752015-05-27 04:22:07 +00001982 if (r != DM_MAPIO_REMAPPED)
1983 return r;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001984 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
1985 /* -ENOMEM */
1986 ti->type->release_clone_rq(clone);
1987 return DM_MAPIO_REQUEUE;
1988 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001989 }
1990
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001991 switch (r) {
1992 case DM_MAPIO_SUBMITTED:
1993 /* The target has taken the I/O to submit by itself later */
1994 break;
1995 case DM_MAPIO_REMAPPED:
1996 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001997 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06001998 blk_rq_pos(rq));
1999 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002000 break;
2001 case DM_MAPIO_REQUEUE:
2002 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff12015-04-29 12:07:12 -04002003 dm_requeue_original_request(md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002004 break;
2005 default:
2006 if (r > 0) {
2007 DMWARN("unimplemented target map return value: %d", r);
2008 BUG();
2009 }
2010
2011 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06002012 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05002013 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002014 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00002015
Mike Snitzere5863d92014-12-17 21:08:12 -05002016 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002017}
2018
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002019static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002020{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002021 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05002022 struct request *rq = tio->orig;
2023 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002024
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002025 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
Mike Snitzer2d76fff12015-04-29 12:07:12 -04002026 dm_requeue_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002027}
2028
Keith Busch466d89a2014-10-17 17:46:37 -06002029static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002030{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002031 if (!orig->q->mq_ops)
2032 blk_start_request(orig);
2033 else
2034 blk_mq_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06002035 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002036
Mike Snitzer0ce65792015-02-26 00:50:28 -05002037 if (md->seq_rq_merge_deadline_usecs) {
2038 md->last_rq_pos = rq_end_sector(orig);
2039 md->last_rq_rw = rq_data_dir(orig);
2040 md->last_rq_start_time = ktime_get();
2041 }
Mike Snitzerde3ec862015-02-24 21:58:21 -05002042
Mikulas Patockae262f342015-06-09 17:22:49 -04002043 if (unlikely(dm_stats_used(&md->stats))) {
2044 struct dm_rq_target_io *tio = tio_from_request(orig);
2045 tio->duration_jiffies = jiffies;
2046 tio->n_sectors = blk_rq_sectors(orig);
2047 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
2048 tio->n_sectors, false, 0, &tio->stats_aux);
2049 }
2050
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002051 /*
2052 * Hold the md reference here for the in-flight I/O.
2053 * We can't rely on the reference count by device opener,
2054 * because the device may be closed during the request completion
2055 * when all bios are completed.
2056 * See the comment in rq_completed() too.
2057 */
2058 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002059}
2060
Mike Snitzer0ce65792015-02-26 00:50:28 -05002061#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
2062
2063ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
2064{
2065 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
2066}
2067
2068ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
2069 const char *buf, size_t count)
2070{
2071 unsigned deadline;
2072
Mike Snitzer17e149b2015-03-11 15:01:09 -04002073 if (!dm_request_based(md) || md->use_blk_mq)
Mike Snitzer0ce65792015-02-26 00:50:28 -05002074 return count;
2075
2076 if (kstrtouint(buf, 10, &deadline))
2077 return -EINVAL;
2078
2079 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
2080 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
2081
2082 md->seq_rq_merge_deadline_usecs = deadline;
2083
2084 return count;
2085}
2086
2087static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
2088{
2089 ktime_t kt_deadline;
2090
2091 if (!md->seq_rq_merge_deadline_usecs)
2092 return false;
2093
2094 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
2095 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
2096
2097 return !ktime_after(ktime_get(), kt_deadline);
2098}
2099
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002100/*
2101 * q->request_fn for request-based dm.
2102 * Called with the queue lock held.
2103 */
2104static void dm_request_fn(struct request_queue *q)
2105{
2106 struct mapped_device *md = q->queuedata;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002107 struct dm_target *ti = md->immutable_target;
Keith Busch466d89a2014-10-17 17:46:37 -06002108 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002109 struct dm_rq_target_io *tio;
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002110 sector_t pos = 0;
2111
2112 if (unlikely(!ti)) {
2113 int srcu_idx;
2114 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2115
2116 ti = dm_table_find_target(map, pos);
2117 dm_put_live_table(md, srcu_idx);
2118 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002119
2120 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002121 * For suspend, check blk_queue_stopped() and increment
2122 * ->pending within a single queue_lock not to increment the
2123 * number of in-flight I/Os after the queue is stopped in
2124 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002125 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002126 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002127 rq = blk_peek_request(q);
2128 if (!rq)
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002129 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002130
Tejun Heo29e40132010-09-08 18:07:00 +02002131 /* always use block 0 to find the target for flushes for now */
2132 pos = 0;
2133 if (!(rq->cmd_flags & REQ_FLUSH))
2134 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002135
Mike Snitzerc91852ff2016-01-31 17:33:32 -05002136 if ((dm_request_peeked_before_merge_deadline(md) &&
2137 md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2138 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
2139 (ti->type->busy && ti->type->busy(ti))) {
2140 blk_delay_queue(q, HZ / 100);
2141 return;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002142 }
Tejun Heo29e40132010-09-08 18:07:00 +02002143
Keith Busch466d89a2014-10-17 17:46:37 -06002144 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002145
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002146 tio = tio_from_request(rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002147 /* Establish tio->ti before queuing work (map_tio_request) */
2148 tio->ti = ti;
2149 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00002150 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002151 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002152}
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154static int dm_any_congested(void *congested_data, int bdi_bits)
2155{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002156 int r = bdi_bits;
2157 struct mapped_device *md = congested_data;
2158 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002160 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mike Snitzere522c032016-02-02 22:35:06 -05002161 if (dm_request_based(md)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002162 /*
Mike Snitzere522c032016-02-02 22:35:06 -05002163 * With request-based DM we only need to check the
2164 * top-level queue for congestion.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002165 */
Mike Snitzere522c032016-02-02 22:35:06 -05002166 r = md->queue->backing_dev_info.wb.state & bdi_bits;
2167 } else {
2168 map = dm_get_live_table_fast(md);
2169 if (map)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002170 r = dm_table_any_congested(map, bdi_bits);
Mike Snitzere522c032016-02-02 22:35:06 -05002171 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002172 }
2173 }
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return r;
2176}
2177
2178/*-----------------------------------------------------------------
2179 * An IDR is used to keep track of allocated minor numbers.
2180 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002181static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002183 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002185 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
2188/*
2189 * See if the device with a specific minor # is free.
2190 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002191static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002193 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 if (minor >= (1 << MINORBITS))
2196 return -EINVAL;
2197
Tejun Heoc9d76be2013-02-27 17:04:26 -08002198 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002199 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Tejun Heoc9d76be2013-02-27 17:04:26 -08002201 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002203 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002204 idr_preload_end();
2205 if (r < 0)
2206 return r == -ENOSPC ? -EBUSY : r;
2207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002210static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002212 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Tejun Heoc9d76be2013-02-27 17:04:26 -08002214 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002215 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Tejun Heoc9d76be2013-02-27 17:04:26 -08002217 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002219 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002220 idr_preload_end();
2221 if (r < 0)
2222 return r;
2223 *minor = r;
2224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002227static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Mikulas Patocka53d59142009-04-02 19:55:37 +01002229static void dm_wq_work(struct work_struct *work);
2230
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002231static void dm_init_md_queue(struct mapped_device *md)
2232{
2233 /*
2234 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002235 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002236 * The type is decided at the first table loading time.
2237 * To prevent problematic device stacking, clear the queue flag
2238 * for request stacking support until then.
2239 *
2240 * This queue is new, so no concurrency on the queue_flags.
2241 */
2242 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04002243
2244 /*
2245 * Initialize data that will only be used by a non-blk-mq DM queue
2246 * - must do so here (in alloc_dev callchain) before queue is used
2247 */
2248 md->queue->queuedata = md;
2249 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002250}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002251
Mike Snitzereca7ee62016-02-20 13:45:38 -05002252static void dm_init_normal_md_queue(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002253{
Mike Snitzer17e149b2015-03-11 15:01:09 -04002254 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002255 dm_init_md_queue(md);
2256
2257 /*
2258 * Initialize aspects of queue that aren't relevant for blk-mq
2259 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002260 md->queue->backing_dev_info.congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002261 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002262}
2263
Mike Snitzer0f209722015-04-28 11:50:29 -04002264static void cleanup_mapped_device(struct mapped_device *md)
2265{
Mike Snitzer0f209722015-04-28 11:50:29 -04002266 if (md->wq)
2267 destroy_workqueue(md->wq);
2268 if (md->kworker_task)
2269 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02002270 mempool_destroy(md->io_pool);
2271 mempool_destroy(md->rq_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04002272 if (md->bs)
2273 bioset_free(md->bs);
2274
Mikulas Patockab06075a2015-07-10 17:21:43 -04002275 cleanup_srcu_struct(&md->io_barrier);
2276
Mike Snitzer0f209722015-04-28 11:50:29 -04002277 if (md->disk) {
2278 spin_lock(&_minor_lock);
2279 md->disk->private_data = NULL;
2280 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04002281 del_gendisk(md->disk);
2282 put_disk(md->disk);
2283 }
2284
2285 if (md->queue)
2286 blk_cleanup_queue(md->queue);
2287
2288 if (md->bdev) {
2289 bdput(md->bdev);
2290 md->bdev = NULL;
2291 }
2292}
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294/*
2295 * Allocate and initialise a blank device with a given minor.
2296 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002297static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002300 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002301 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (!md) {
2304 DMWARN("unable to allocate device, out of memory.");
2305 return NULL;
2306 }
2307
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002308 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002309 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002312 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002313 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002314 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002315 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002317 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002319 r = init_srcu_struct(&md->io_barrier);
2320 if (r < 0)
2321 goto bad_io_barrier;
2322
Mike Snitzer17e149b2015-03-11 15:01:09 -04002323 md->use_blk_mq = use_blk_mq;
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002324 md->init_tio_pdu = false;
Mike Snitzera5664da2010-08-12 04:14:01 +01002325 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002326 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002327 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002328 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002329 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002331 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002333 atomic_set(&md->uevent_seq, 0);
2334 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002335 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002336 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002338 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04002340 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002342 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 md->disk = alloc_disk(1);
2345 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04002346 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002348 atomic_set(&md->pending[0], 0);
2349 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002350 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002351 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002352 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002353 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002354 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 md->disk->major = _major;
2357 md->disk->first_minor = minor;
2358 md->disk->fops = &dm_blk_dops;
2359 md->disk->queue = md->queue;
2360 md->disk->private_data = md;
2361 sprintf(md->disk->disk_name, "dm-%d", minor);
2362 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002363 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Tejun Heo670368a2013-07-30 08:40:21 -04002365 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002366 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002367 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002368
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002369 md->bdev = bdget_disk(md->disk, 0);
2370 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002371 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002372
Tejun Heo6a8736d2010-09-08 18:07:00 +02002373 bio_init(&md->flush_bio);
2374 md->flush_bio.bi_bdev = md->bdev;
2375 md->flush_bio.bi_rw = WRITE_FLUSH;
2376
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002377 dm_stats_init(&md->stats);
2378
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002379 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002380 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002381 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002382 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002383
2384 BUG_ON(old_md != MINOR_ALLOCED);
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return md;
2387
Mike Snitzer0f209722015-04-28 11:50:29 -04002388bad:
2389 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002390bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002392bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002393 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002394bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 kfree(md);
2396 return NULL;
2397}
2398
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002399static void unlock_fs(struct mapped_device *md);
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401static void free_dev(struct mapped_device *md)
2402{
Tejun Heof331c022008-09-03 09:01:48 +02002403 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002404
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002405 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002406
Mike Snitzer0f209722015-04-28 11:50:29 -04002407 cleanup_mapped_device(md);
Mike Snitzer1c357a12016-02-06 17:01:17 -05002408 if (md->tag_set) {
2409 blk_mq_free_tag_set(md->tag_set);
2410 kfree(md->tag_set);
2411 }
Mike Snitzer0f209722015-04-28 11:50:29 -04002412
2413 free_table_devices(&md->table_devices);
2414 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002415 free_minor(minor);
2416
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002417 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 kfree(md);
2419}
2420
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002421static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2422{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002423 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002424
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002425 if (md->bs) {
2426 /* The md already has necessary mempools. */
2427 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002428 /*
2429 * Reload bioset because front_pad may have changed
2430 * because a different table was loaded.
2431 */
2432 bioset_free(md->bs);
2433 md->bs = p->bs;
2434 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002435 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002436 /*
2437 * There's no need to reload with request-based dm
2438 * because the size of front_pad doesn't change.
2439 * Note for future: If you are to reload bioset,
2440 * prep-ed requests in the queue may refer
2441 * to bio from the old bioset, so you must walk
2442 * through the queue to unprep.
2443 */
2444 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002445 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002446
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002447 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2448
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002449 md->io_pool = p->io_pool;
2450 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002451 md->rq_pool = p->rq_pool;
2452 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002453 md->bs = p->bs;
2454 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002455
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002456out:
Mike Snitzer02233342015-03-10 23:49:26 -04002457 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002458 dm_table_free_md_mempools(t);
2459}
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461/*
2462 * Bind a table to the device.
2463 */
2464static void event_callback(void *context)
2465{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002466 unsigned long flags;
2467 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 struct mapped_device *md = (struct mapped_device *) context;
2469
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002470 spin_lock_irqsave(&md->uevent_lock, flags);
2471 list_splice_init(&md->uevent_list, &uevents);
2472 spin_unlock_irqrestore(&md->uevent_lock, flags);
2473
Tejun Heoed9e1982008-08-25 19:56:05 +09002474 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 atomic_inc(&md->event_nr);
2477 wake_up(&md->eventq);
2478}
2479
Mike Snitzerc2176492011-01-13 19:53:46 +00002480/*
2481 * Protected by md->suspend_lock obtained by dm_swap_table().
2482 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002483static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002485 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002487 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002490/*
2491 * Returns old map, which caller must destroy.
2492 */
2493static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2494 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002496 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002497 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 sector_t size;
2499
2500 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002501
2502 /*
2503 * Wipe any geometry if the size of the table changed.
2504 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002505 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002506 memset(&md->geometry, 0, sizeof(md->geometry));
2507
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002508 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002510 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002511
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002512 /*
2513 * The queue hasn't been stopped yet, if the old table type wasn't
2514 * for request-based during suspension. So stop it to prevent
2515 * I/O mapping before resume.
2516 * This must be done before setting the queue restrictions,
2517 * because request-based dm may be run just after the setting.
2518 */
Mike Snitzer16f12262016-01-31 17:22:27 -05002519 if (dm_table_request_based(t)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002520 dm_stop_queue(q);
Mike Snitzer16f12262016-01-31 17:22:27 -05002521 /*
2522 * Leverage the fact that request-based DM targets are
2523 * immutable singletons and establish md->immutable_target
2524 * - used to optimize both dm_request_fn and dm_mq_queue_rq
2525 */
2526 md->immutable_target = dm_table_get_immutable_target(t);
2527 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002528
2529 __bind_mempools(md, t);
2530
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002531 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002532 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002533 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2534
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002535 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002536 if (old_map)
2537 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002538
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002539 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
Alasdair G Kergona7940152009-12-10 23:52:23 +00002542/*
2543 * Returns unbound table for the caller to free.
2544 */
2545static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002547 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002550 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302553 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002554 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002555
2556 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
2559/*
2560 * Constructor for a new device.
2561 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002562int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 struct mapped_device *md;
2565
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002566 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (!md)
2568 return -ENXIO;
2569
Milan Broz784aae72009-01-06 03:05:12 +00002570 dm_sysfs_init(md);
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 *result = md;
2573 return 0;
2574}
2575
Mike Snitzera5664da2010-08-12 04:14:01 +01002576/*
2577 * Functions to manage md->type.
2578 * All are required to hold md->type_lock.
2579 */
2580void dm_lock_md_type(struct mapped_device *md)
2581{
2582 mutex_lock(&md->type_lock);
2583}
2584
2585void dm_unlock_md_type(struct mapped_device *md)
2586{
2587 mutex_unlock(&md->type_lock);
2588}
2589
2590void dm_set_md_type(struct mapped_device *md, unsigned type)
2591{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002592 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002593 md->type = type;
2594}
2595
2596unsigned dm_get_md_type(struct mapped_device *md)
2597{
2598 return md->type;
2599}
2600
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002601struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2602{
2603 return md->immutable_target_type;
2604}
2605
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002606/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002607 * The queue_limits are only valid as long as you have a reference
2608 * count on 'md'.
2609 */
2610struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2611{
2612 BUG_ON(!atomic_read(&md->holders));
2613 return &md->queue->limits;
2614}
2615EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2616
Mike Snitzereca7ee62016-02-20 13:45:38 -05002617static void dm_old_init_rq_based_worker_thread(struct mapped_device *md)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002618{
2619 /* Initialize the request-based DM worker thread */
2620 init_kthread_worker(&md->kworker);
2621 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2622 "kdmwork-%s", dm_device_name(md));
2623}
2624
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002625/*
Mike Snitzereca7ee62016-02-20 13:45:38 -05002626 * Fully initialize a .request_fn request-based queue.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002627 */
Mike Snitzereca7ee62016-02-20 13:45:38 -05002628static int dm_old_init_request_queue(struct mapped_device *md)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002629{
2630 struct request_queue *q = NULL;
2631
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002632 /* Fully initialize the queue */
2633 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2634 if (!q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002635 return -EINVAL;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002636
Mike Snitzer0ce65792015-02-26 00:50:28 -05002637 /* disable dm_request_fn's merge heuristic by default */
2638 md->seq_rq_merge_deadline_usecs = 0;
2639
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002640 md->queue = q;
Mike Snitzereca7ee62016-02-20 13:45:38 -05002641 dm_init_normal_md_queue(md);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002642 blk_queue_softirq_done(md->queue, dm_softirq_done);
Mike Snitzereca7ee62016-02-20 13:45:38 -05002643 blk_queue_prep_rq(md->queue, dm_old_prep_fn);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002644
Mike Snitzereca7ee62016-02-20 13:45:38 -05002645 dm_old_init_rq_based_worker_thread(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002646
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002647 elv_register_queue(md->queue);
2648
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002649 return 0;
2650}
2651
2652static int dm_mq_init_request(void *data, struct request *rq,
2653 unsigned int hctx_idx, unsigned int request_idx,
2654 unsigned int numa_node)
2655{
2656 struct mapped_device *md = data;
2657 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2658
2659 /*
2660 * Must initialize md member of tio, otherwise it won't
2661 * be available in dm_mq_queue_rq.
2662 */
2663 tio->md = md;
2664
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002665 if (md->init_tio_pdu) {
2666 /* target-specific per-io data is immediately after the tio */
2667 tio->info.ptr = tio + 1;
2668 }
2669
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002670 return 0;
2671}
2672
2673static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2674 const struct blk_mq_queue_data *bd)
2675{
2676 struct request *rq = bd->rq;
2677 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2678 struct mapped_device *md = tio->md;
Mike Snitzer16f12262016-01-31 17:22:27 -05002679 struct dm_target *ti = md->immutable_target;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002680
Mike Snitzer16f12262016-01-31 17:22:27 -05002681 if (unlikely(!ti)) {
2682 int srcu_idx;
2683 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002684
Mike Snitzer16f12262016-01-31 17:22:27 -05002685 ti = dm_table_find_target(map, 0);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002686 dm_put_live_table(md, srcu_idx);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002687 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002688
2689 if (ti->type->busy && ti->type->busy(ti))
2690 return BLK_MQ_RQ_QUEUE_BUSY;
2691
2692 dm_start_request(md, rq);
2693
2694 /* Init tio using md established in .init_request */
2695 init_tio(tio, rq, md);
2696
Mike Snitzer02233342015-03-10 23:49:26 -04002697 /*
2698 * Establish tio->ti before queuing work (map_tio_request)
2699 * or making direct call to map_request().
2700 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002701 tio->ti = ti;
Mike Snitzer02233342015-03-10 23:49:26 -04002702
Mike Snitzerc5248f72016-02-20 14:02:49 -05002703 /* Direct call is fine since .queue_rq allows allocations */
2704 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
2705 /* Undo dm_start_request() before requeuing */
2706 rq_end_stats(md, rq);
2707 rq_completed(md, rq_data_dir(rq), false);
2708 return BLK_MQ_RQ_QUEUE_BUSY;
Mike Snitzer02233342015-03-10 23:49:26 -04002709 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002710
2711 return BLK_MQ_RQ_QUEUE_OK;
2712}
2713
2714static struct blk_mq_ops dm_mq_ops = {
2715 .queue_rq = dm_mq_queue_rq,
2716 .map_queue = blk_mq_map_queue,
2717 .complete = dm_softirq_done,
2718 .init_request = dm_mq_init_request,
2719};
2720
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002721static int dm_mq_init_request_queue(struct mapped_device *md,
2722 struct dm_target *immutable_tgt)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002723{
2724 struct request_queue *q;
2725 int err;
2726
Mike Snitzerc5248f72016-02-20 14:02:49 -05002727 if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) {
2728 DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
2729 return -EINVAL;
2730 }
2731
Mike Snitzer1c357a12016-02-06 17:01:17 -05002732 md->tag_set = kzalloc(sizeof(struct blk_mq_tag_set), GFP_KERNEL);
2733 if (!md->tag_set)
2734 return -ENOMEM;
2735
2736 md->tag_set->ops = &dm_mq_ops;
2737 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
2738 md->tag_set->numa_node = NUMA_NO_NODE;
2739 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2740 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
2741 md->tag_set->driver_data = md;
2742
2743 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002744 if (immutable_tgt && immutable_tgt->per_io_data_size) {
2745 /* any target-specific per-io data is immediately after the tio */
2746 md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
2747 md->init_tio_pdu = true;
2748 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002749
Mike Snitzer1c357a12016-02-06 17:01:17 -05002750 err = blk_mq_alloc_tag_set(md->tag_set);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002751 if (err)
Mike Snitzer1c357a12016-02-06 17:01:17 -05002752 goto out_kfree_tag_set;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002753
Mike Snitzer1c357a12016-02-06 17:01:17 -05002754 q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002755 if (IS_ERR(q)) {
2756 err = PTR_ERR(q);
2757 goto out_tag_set;
2758 }
2759 md->queue = q;
2760 dm_init_md_queue(md);
2761
2762 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
2763 blk_mq_register_disk(md->disk);
2764
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002765 return 0;
2766
2767out_tag_set:
Mike Snitzer1c357a12016-02-06 17:01:17 -05002768 blk_mq_free_tag_set(md->tag_set);
2769out_kfree_tag_set:
2770 kfree(md->tag_set);
2771
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002772 return err;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002773}
2774
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002775static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2776{
2777 if (type == DM_TYPE_BIO_BASED)
2778 return type;
2779
2780 return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2781}
2782
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002783/*
2784 * Setup the DM device's queue based on md's type
2785 */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002786int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002787{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002788 int r;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002789 unsigned md_type = filter_md_type(dm_get_md_type(md), md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002790
2791 switch (md_type) {
2792 case DM_TYPE_REQUEST_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05002793 r = dm_old_init_request_queue(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002794 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002795 DMERR("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002796 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002797 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002798 break;
2799 case DM_TYPE_MQ_REQUEST_BASED:
Mike Snitzer591ddcf2016-01-31 12:05:42 -05002800 r = dm_mq_init_request_queue(md, dm_table_get_immutable_target(t));
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002801 if (r) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05002802 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002803 return r;
2804 }
2805 break;
2806 case DM_TYPE_BIO_BASED:
Mike Snitzereca7ee62016-02-20 13:45:38 -05002807 dm_init_normal_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002808 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04002809 /*
2810 * DM handles splitting bios as needed. Free the bio_split bioset
2811 * since it won't be used (saves 1 process per bio-based DM device).
2812 */
2813 bioset_free(md->queue->bio_split);
2814 md->queue->bio_split = NULL;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002815 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002816 }
2817
2818 return 0;
2819}
2820
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002821struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
2823 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 unsigned minor = MINOR(dev);
2825
2826 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2827 return NULL;
2828
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002829 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002832 if (md) {
2833 if ((md == MINOR_ALLOCED ||
2834 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2835 dm_deleting_md(md) ||
2836 test_bit(DMF_FREEING, &md->flags))) {
2837 md = NULL;
2838 goto out;
2839 }
2840 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002843out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002844 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
David Teigland637842c2006-01-06 00:20:00 -08002846 return md;
2847}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002848EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002849
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002850void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002851{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002852 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
2855void dm_set_mdptr(struct mapped_device *md, void *ptr)
2856{
2857 md->interface_ptr = ptr;
2858}
2859
2860void dm_get(struct mapped_device *md)
2861{
2862 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002863 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864}
2865
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002866int dm_hold(struct mapped_device *md)
2867{
2868 spin_lock(&_minor_lock);
2869 if (test_bit(DMF_FREEING, &md->flags)) {
2870 spin_unlock(&_minor_lock);
2871 return -EBUSY;
2872 }
2873 dm_get(md);
2874 spin_unlock(&_minor_lock);
2875 return 0;
2876}
2877EXPORT_SYMBOL_GPL(dm_hold);
2878
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002879const char *dm_device_name(struct mapped_device *md)
2880{
2881 return md->name;
2882}
2883EXPORT_SYMBOL_GPL(dm_device_name);
2884
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002885static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002887 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002888 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002890 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002891
Mike Snitzer63a4f062015-03-23 17:01:43 -04002892 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002893 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2894 set_bit(DMF_FREEING, &md->flags);
2895 spin_unlock(&_minor_lock);
2896
Mike Snitzer02233342015-03-10 23:49:26 -04002897 if (dm_request_based(md) && md->kworker_task)
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002898 flush_kthread_worker(&md->kworker);
2899
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002900 /*
2901 * Take suspend_lock so that presuspend and postsuspend methods
2902 * do not race with internal suspend.
2903 */
2904 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002905 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002906 if (!dm_suspended_md(md)) {
2907 dm_table_presuspend_targets(map);
2908 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002910 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2911 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002912 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002913
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002914 /*
2915 * Rare, but there may be I/O requests still going to complete,
2916 * for example. Wait for all references to disappear.
2917 * No one should increment the reference count of the mapped_device,
2918 * after the mapped_device state becomes DMF_FREEING.
2919 */
2920 if (wait)
2921 while (atomic_read(&md->holders))
2922 msleep(1);
2923 else if (atomic_read(&md->holders))
2924 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2925 dm_device_name(md), atomic_read(&md->holders));
2926
2927 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002928 dm_table_destroy(__unbind(md));
2929 free_dev(md);
2930}
2931
2932void dm_destroy(struct mapped_device *md)
2933{
2934 __dm_destroy(md, true);
2935}
2936
2937void dm_destroy_immediate(struct mapped_device *md)
2938{
2939 __dm_destroy(md, false);
2940}
2941
2942void dm_put(struct mapped_device *md)
2943{
2944 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
Edward Goggin79eb8852007-05-09 02:32:56 -07002946EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Mikulas Patocka401600d2009-04-02 19:55:38 +01002948static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002949{
2950 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002951 DECLARE_WAITQUEUE(wait, current);
2952
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002953 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002954
2955 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002956 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002957
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002958 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002959 break;
2960
Mikulas Patocka401600d2009-04-02 19:55:38 +01002961 if (interruptible == TASK_INTERRUPTIBLE &&
2962 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002963 r = -EINTR;
2964 break;
2965 }
2966
2967 io_schedule();
2968 }
2969 set_current_state(TASK_RUNNING);
2970
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002971 remove_wait_queue(&md->wait, &wait);
2972
Milan Broz46125c12008-02-08 02:10:30 +00002973 return r;
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976/*
2977 * Process the deferred bios
2978 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002979static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
Mikulas Patockaef208582009-04-02 19:55:38 +01002981 struct mapped_device *md = container_of(work, struct mapped_device,
2982 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002983 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002984 int srcu_idx;
2985 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002987 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002988
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002989 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002990 spin_lock_irq(&md->deferred_lock);
2991 c = bio_list_pop(&md->deferred);
2992 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002993
Tejun Heo6a8736d2010-09-08 18:07:00 +02002994 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002995 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002996
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002997 if (dm_request_based(md))
2998 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002999 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003000 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01003001 }
Milan Broz73d410c2008-02-08 02:10:25 +00003002
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003003 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003006static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00003007{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003008 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003009 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01003010 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00003011}
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003014 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003016struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Mike Christie87eb5b22013-03-01 22:45:48 +00003018 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003019 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003020 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Daniel Walkere61290a2008-02-08 02:10:08 +00003022 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003025 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003026 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Mike Snitzer3ae70652012-09-26 23:45:45 +01003028 /*
3029 * If the new table has no data devices, retain the existing limits.
3030 * This helps multipath with queue_if_no_path if all paths disappear,
3031 * then new I/O is queued based on these limits, and then some paths
3032 * reappear.
3033 */
3034 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003035 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003036 if (live_map)
3037 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003038 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003039 }
3040
Mike Christie87eb5b22013-03-01 22:45:48 +00003041 if (!live_map) {
3042 r = dm_calculate_queue_limits(table, &limits);
3043 if (r) {
3044 map = ERR_PTR(r);
3045 goto out;
3046 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003047 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003048
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003049 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003051out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003052 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003053 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
3056/*
3057 * Functions to lock and unlock any filesystem running on the
3058 * device.
3059 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003060static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003062 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003065
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003066 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003067 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003068 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003069 md->frozen_sb = NULL;
3070 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003071 }
3072
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003073 set_bit(DMF_FROZEN, &md->flags);
3074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return 0;
3076}
3077
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003078static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003080 if (!test_bit(DMF_FROZEN, &md->flags))
3081 return;
3082
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003083 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003085 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
3088/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04003089 * If __dm_suspend returns 0, the device is completely quiescent
3090 * now. There is no request-processing activity. All new requests
3091 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003092 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04003093 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003094 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003095static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3096 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003098 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3099 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3100 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003102 /*
3103 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
3104 * This flag is cleared before dm_suspend returns.
3105 */
3106 if (noflush)
3107 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
3108
Mike Snitzerd67ee212014-10-28 20:13:31 -04003109 /*
3110 * This gets reverted if there's an error later and the targets
3111 * provide the .presuspend_undo hook.
3112 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003113 dm_table_presuspend_targets(map);
3114
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003115 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003116 * Flush I/O to the device.
3117 * Any I/O submitted after lock_fs() may not be flushed.
3118 * noflush takes precedence over do_lockfs.
3119 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003120 */
3121 if (!noflush && do_lockfs) {
3122 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003123 if (r) {
3124 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003125 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04003126 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003130 * Here we must make sure that no processes are submitting requests
3131 * to target drivers i.e. no one may be executing
3132 * __split_and_process_bio. This is called from dm_request and
3133 * dm_wq_work.
3134 *
3135 * To get all processes out of __split_and_process_bio in dm_request,
3136 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02003137 * __split_and_process_bio from dm_request and quiesce the thread
3138 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
3139 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01003141 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003142 if (map)
3143 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003145 /*
Tejun Heo29e40132010-09-08 18:07:00 +02003146 * Stop md->queue before flushing md->wq in case request-based
3147 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003148 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003149 if (dm_request_based(md)) {
Mike Snitzereca7ee62016-02-20 13:45:38 -05003150 dm_stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04003151 if (md->kworker_task)
3152 flush_kthread_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003153 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003154
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003155 flush_workqueue(md->wq);
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003158 * At this point no more requests are entering target request routines.
3159 * We call dm_wait_for_completion to wait for all existing requests
3160 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003162 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Milan Broz6d6f10d2008-02-08 02:10:22 +00003164 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01003165 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003166 if (map)
3167 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00003170 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003171 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00003172
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003173 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05003174 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003175
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003176 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003177 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003178 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003179 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003180
Mike Snitzerffcc3932014-10-28 18:34:52 -04003181 return r;
3182}
3183
3184/*
3185 * We need to be able to change a mapping table under a mounted
3186 * filesystem. For example we might want to move some data in
3187 * the background. Before the table can be swapped with
3188 * dm_bind_table, dm_suspend must be called to flush any in
3189 * flight bios and ensure that any further io gets deferred.
3190 */
3191/*
3192 * Suspend mechanism in request-based dm.
3193 *
3194 * 1. Flush all I/Os by lock_fs() if needed.
3195 * 2. Stop dispatching any I/O by stopping the request_queue.
3196 * 3. Wait for all in-flight I/Os to be completed or requeued.
3197 *
3198 * To abort suspend, start the request_queue.
3199 */
3200int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3201{
3202 struct dm_table *map = NULL;
3203 int r = 0;
3204
3205retry:
3206 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3207
3208 if (dm_suspended_md(md)) {
3209 r = -EINVAL;
3210 goto out_unlock;
3211 }
3212
3213 if (dm_suspended_internally_md(md)) {
3214 /* already internally suspended, wait for internal resume */
3215 mutex_unlock(&md->suspend_lock);
3216 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3217 if (r)
3218 return r;
3219 goto retry;
3220 }
3221
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003222 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003223
3224 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3225 if (r)
3226 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 set_bit(DMF_SUSPENDED, &md->flags);
3229
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00003230 dm_table_postsuspend_targets(map);
3231
Alasdair G Kergond2874832006-11-08 17:44:43 -08003232out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00003233 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003234 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Mike Snitzerffcc3932014-10-28 18:34:52 -04003237static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003239 if (map) {
3240 int r = dm_table_resume_targets(map);
3241 if (r)
3242 return r;
3243 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003244
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003245 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003246
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003247 /*
3248 * Flushing deferred I/Os must be done after targets are resumed
3249 * so that mapping of targets can work correctly.
3250 * Request-based dm is queueing the deferred I/Os in its request_queue.
3251 */
3252 if (dm_request_based(md))
Mike Snitzereca7ee62016-02-20 13:45:38 -05003253 dm_start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003254
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003255 unlock_fs(md);
3256
Mike Snitzerffcc3932014-10-28 18:34:52 -04003257 return 0;
3258}
3259
3260int dm_resume(struct mapped_device *md)
3261{
3262 int r = -EINVAL;
3263 struct dm_table *map = NULL;
3264
3265retry:
3266 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3267
3268 if (!dm_suspended_md(md))
3269 goto out;
3270
3271 if (dm_suspended_internally_md(md)) {
3272 /* already internally suspended, wait for internal resume */
3273 mutex_unlock(&md->suspend_lock);
3274 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3275 if (r)
3276 return r;
3277 goto retry;
3278 }
3279
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003280 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003281 if (!map || !dm_table_get_size(map))
3282 goto out;
3283
3284 r = __dm_resume(md, map);
3285 if (r)
3286 goto out;
3287
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003288 clear_bit(DMF_SUSPENDED, &md->flags);
3289
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003290 r = 0;
3291out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003292 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003293
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003294 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003297/*
3298 * Internal suspend/resume works like userspace-driven suspend. It waits
3299 * until all bios finish and prevents issuing new bios to the target drivers.
3300 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003301 */
3302
Mike Snitzerffcc3932014-10-28 18:34:52 -04003303static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3304{
3305 struct dm_table *map = NULL;
3306
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003307 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003308 return; /* nested internal suspend */
3309
3310 if (dm_suspended_md(md)) {
3311 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3312 return; /* nest suspend */
3313 }
3314
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003315 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003316
3317 /*
3318 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3319 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3320 * would require changing .presuspend to return an error -- avoid this
3321 * until there is a need for more elaborate variants of internal suspend.
3322 */
3323 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3324
3325 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3326
3327 dm_table_postsuspend_targets(map);
3328}
3329
3330static void __dm_internal_resume(struct mapped_device *md)
3331{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003332 BUG_ON(!md->internal_suspend_count);
3333
3334 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003335 return; /* resume from nested internal suspend */
3336
3337 if (dm_suspended_md(md))
3338 goto done; /* resume from nested suspend */
3339
3340 /*
3341 * NOTE: existing callers don't need to call dm_table_resume_targets
3342 * (which may fail -- so best to avoid it for now by passing NULL map)
3343 */
3344 (void) __dm_resume(md, NULL);
3345
3346done:
3347 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3348 smp_mb__after_atomic();
3349 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3350}
3351
3352void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003353{
3354 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003355 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3356 mutex_unlock(&md->suspend_lock);
3357}
3358EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3359
3360void dm_internal_resume(struct mapped_device *md)
3361{
3362 mutex_lock(&md->suspend_lock);
3363 __dm_internal_resume(md);
3364 mutex_unlock(&md->suspend_lock);
3365}
3366EXPORT_SYMBOL_GPL(dm_internal_resume);
3367
3368/*
3369 * Fast variants of internal suspend/resume hold md->suspend_lock,
3370 * which prevents interaction with userspace-driven suspend.
3371 */
3372
3373void dm_internal_suspend_fast(struct mapped_device *md)
3374{
3375 mutex_lock(&md->suspend_lock);
3376 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003377 return;
3378
3379 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3380 synchronize_srcu(&md->io_barrier);
3381 flush_workqueue(md->wq);
3382 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3383}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003384EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003385
Mike Snitzerffcc3932014-10-28 18:34:52 -04003386void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003387{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003388 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003389 goto done;
3390
3391 dm_queue_flush(md);
3392
3393done:
3394 mutex_unlock(&md->suspend_lock);
3395}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003396EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398/*-----------------------------------------------------------------
3399 * Event notification.
3400 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003401int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003402 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003403{
Milan Broz60935eb2009-06-22 10:12:30 +01003404 char udev_cookie[DM_COOKIE_LENGTH];
3405 char *envp[] = { udev_cookie, NULL };
3406
3407 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003408 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003409 else {
3410 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3411 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003412 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3413 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003414 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003415}
3416
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003417uint32_t dm_next_uevent_seq(struct mapped_device *md)
3418{
3419 return atomic_add_return(1, &md->uevent_seq);
3420}
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422uint32_t dm_get_event_nr(struct mapped_device *md)
3423{
3424 return atomic_read(&md->event_nr);
3425}
3426
3427int dm_wait_event(struct mapped_device *md, int event_nr)
3428{
3429 return wait_event_interruptible(md->eventq,
3430 (event_nr != atomic_read(&md->event_nr)));
3431}
3432
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003433void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3434{
3435 unsigned long flags;
3436
3437 spin_lock_irqsave(&md->uevent_lock, flags);
3438 list_add(elist, &md->uevent_list);
3439 spin_unlock_irqrestore(&md->uevent_lock, flags);
3440}
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442/*
3443 * The gendisk is only valid as long as you have a reference
3444 * count on 'md'.
3445 */
3446struct gendisk *dm_disk(struct mapped_device *md)
3447{
3448 return md->disk;
3449}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00003450EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Milan Broz784aae72009-01-06 03:05:12 +00003452struct kobject *dm_kobject(struct mapped_device *md)
3453{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003454 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003455}
3456
Milan Broz784aae72009-01-06 03:05:12 +00003457struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3458{
3459 struct mapped_device *md;
3460
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003461 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003462
Milan Broz4d89b7b2009-06-22 10:12:11 +01003463 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003464 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003465 return NULL;
3466
Milan Broz784aae72009-01-06 03:05:12 +00003467 dm_get(md);
3468 return md;
3469}
3470
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003471int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
3473 return test_bit(DMF_SUSPENDED, &md->flags);
3474}
3475
Mike Snitzerffcc3932014-10-28 18:34:52 -04003476int dm_suspended_internally_md(struct mapped_device *md)
3477{
3478 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3479}
3480
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003481int dm_test_deferred_remove_flag(struct mapped_device *md)
3482{
3483 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3484}
3485
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003486int dm_suspended(struct dm_target *ti)
3487{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003488 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003489}
3490EXPORT_SYMBOL_GPL(dm_suspended);
3491
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003492int dm_noflush_suspending(struct dm_target *ti)
3493{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003494 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003495}
3496EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3497
Mike Snitzer78d8e582015-06-26 10:01:13 -04003498struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
Mike Snitzer30187e12016-01-31 13:28:26 -05003499 unsigned integrity, unsigned per_io_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003500{
Mike Snitzer78d8e582015-06-26 10:01:13 -04003501 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3502 struct kmem_cache *cachep = NULL;
3503 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003504 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003505
3506 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003507 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003508
Mike Snitzer78d8e582015-06-26 10:01:13 -04003509 type = filter_md_type(type, md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04003510
Mike Snitzer78d8e582015-06-26 10:01:13 -04003511 switch (type) {
3512 case DM_TYPE_BIO_BASED:
3513 cachep = _io_cache;
3514 pool_size = dm_get_reserved_bio_based_ios();
Mike Snitzer30187e12016-01-31 13:28:26 -05003515 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 -04003516 break;
3517 case DM_TYPE_REQUEST_BASED:
3518 cachep = _rq_tio_cache;
3519 pool_size = dm_get_reserved_rq_based_ios();
3520 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3521 if (!pools->rq_pool)
3522 goto out;
3523 /* fall through to setup remaining rq-based pools */
3524 case DM_TYPE_MQ_REQUEST_BASED:
3525 if (!pool_size)
3526 pool_size = dm_get_reserved_rq_based_ios();
3527 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
Mike Snitzer591ddcf2016-01-31 12:05:42 -05003528 /* per_io_data_size is used for blk-mq pdu at queue allocation */
Mike Snitzer78d8e582015-06-26 10:01:13 -04003529 break;
3530 default:
3531 BUG();
3532 }
3533
3534 if (cachep) {
3535 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3536 if (!pools->io_pool)
3537 goto out;
3538 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003539
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003540 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003541 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003542 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003543
Martin K. Petersena91a2782011-03-17 11:11:05 +01003544 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003545 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003546
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003547 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003548
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003549out:
3550 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003551
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003552 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003553}
3554
3555void dm_free_md_mempools(struct dm_md_mempools *pools)
3556{
3557 if (!pools)
3558 return;
3559
Julia Lawall6f659852015-09-13 14:15:05 +02003560 mempool_destroy(pools->io_pool);
3561 mempool_destroy(pools->rq_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003562
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003563 if (pools->bs)
3564 bioset_free(pools->bs);
3565
3566 kfree(pools);
3567}
3568
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003569static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003570 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003571{
3572 struct mapped_device *md = bdev->bd_disk->private_data;
3573 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003574 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003575 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003576
Mike Snitzer956a4022016-02-18 16:13:51 -05003577 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003578 if (r < 0)
3579 return r;
3580
3581 ops = bdev->bd_disk->fops->pr_ops;
3582 if (ops && ops->pr_register)
3583 r = ops->pr_register(bdev, old_key, new_key, flags);
3584 else
3585 r = -EOPNOTSUPP;
3586
Mike Snitzer956a4022016-02-18 16:13:51 -05003587 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003588 return r;
3589}
3590
3591static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
Mike Snitzer956a4022016-02-18 16:13:51 -05003592 u32 flags)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003593{
3594 struct mapped_device *md = bdev->bd_disk->private_data;
3595 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003596 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003597 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003598
Mike Snitzer956a4022016-02-18 16:13:51 -05003599 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003600 if (r < 0)
3601 return r;
3602
3603 ops = bdev->bd_disk->fops->pr_ops;
3604 if (ops && ops->pr_reserve)
3605 r = ops->pr_reserve(bdev, key, type, flags);
3606 else
3607 r = -EOPNOTSUPP;
3608
Mike Snitzer956a4022016-02-18 16:13:51 -05003609 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003610 return r;
3611}
3612
3613static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3614{
3615 struct mapped_device *md = bdev->bd_disk->private_data;
3616 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003617 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003618 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003619
Mike Snitzer956a4022016-02-18 16:13:51 -05003620 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003621 if (r < 0)
3622 return r;
3623
3624 ops = bdev->bd_disk->fops->pr_ops;
3625 if (ops && ops->pr_release)
3626 r = ops->pr_release(bdev, key, type);
3627 else
3628 r = -EOPNOTSUPP;
3629
Mike Snitzer956a4022016-02-18 16:13:51 -05003630 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003631 return r;
3632}
3633
3634static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
Mike Snitzer956a4022016-02-18 16:13:51 -05003635 enum pr_type type, bool abort)
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003636{
3637 struct mapped_device *md = bdev->bd_disk->private_data;
3638 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003639 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003640 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003641
Mike Snitzer956a4022016-02-18 16:13:51 -05003642 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003643 if (r < 0)
3644 return r;
3645
3646 ops = bdev->bd_disk->fops->pr_ops;
3647 if (ops && ops->pr_preempt)
3648 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3649 else
3650 r = -EOPNOTSUPP;
3651
Mike Snitzer956a4022016-02-18 16:13:51 -05003652 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003653 return r;
3654}
3655
3656static int dm_pr_clear(struct block_device *bdev, u64 key)
3657{
3658 struct mapped_device *md = bdev->bd_disk->private_data;
3659 const struct pr_ops *ops;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003660 fmode_t mode;
Mike Snitzer956a4022016-02-18 16:13:51 -05003661 int r;
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003662
Mike Snitzer956a4022016-02-18 16:13:51 -05003663 r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003664 if (r < 0)
3665 return r;
3666
3667 ops = bdev->bd_disk->fops->pr_ops;
3668 if (ops && ops->pr_clear)
3669 r = ops->pr_clear(bdev, key);
3670 else
3671 r = -EOPNOTSUPP;
3672
Mike Snitzer956a4022016-02-18 16:13:51 -05003673 bdput(bdev);
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003674 return r;
3675}
3676
3677static const struct pr_ops dm_pr_ops = {
3678 .pr_register = dm_pr_register,
3679 .pr_reserve = dm_pr_reserve,
3680 .pr_release = dm_pr_release,
3681 .pr_preempt = dm_pr_preempt,
3682 .pr_clear = dm_pr_clear,
3683};
3684
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003685static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 .open = dm_blk_open,
3687 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003688 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003689 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003690 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 .owner = THIS_MODULE
3692};
3693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694/*
3695 * module hooks
3696 */
3697module_init(dm_init);
3698module_exit(dm_exit);
3699
3700module_param(major, uint, 0);
3701MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003702
Mike Snitzere8603132013-09-12 18:06:12 -04003703module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3704MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3705
Mike Snitzerf4790822013-09-12 18:06:12 -04003706module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3707MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3708
Mike Snitzer17e149b2015-03-11 15:01:09 -04003709module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
3710MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
3711
Mike Snitzerfaad87d2016-01-28 16:52:56 -05003712module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
3713MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
3714
3715module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
3716MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718MODULE_DESCRIPTION(DM_NAME " driver");
3719MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3720MODULE_LICENSE("GPL");