blob: 8bb1ebb6ca7b5ea5836824d6bf281232143cec48 [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>
Li Zefan55782132009-06-09 13:43:05 +080027
28#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Alasdair G Kergon72d94862006-06-26 00:27:35 -070030#define DM_MSG_PREFIX "core"
31
Namhyung Kim71a16732011-10-31 20:18:54 +000032#ifdef CONFIG_PRINTK
33/*
34 * ratelimit state to be used in DMXXX_LIMIT().
35 */
36DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
37 DEFAULT_RATELIMIT_INTERVAL,
38 DEFAULT_RATELIMIT_BURST);
39EXPORT_SYMBOL(dm_ratelimit_state);
40#endif
41
Milan Broz60935eb2009-06-22 10:12:30 +010042/*
43 * Cookies are numeric values sent with CHANGE and REMOVE
44 * uevents while resuming, removing or renaming the device.
45 */
46#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
47#define DM_COOKIE_LENGTH 24
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static const char *_name = DM_NAME;
50
51static unsigned int major = 0;
52static unsigned int _major = 0;
53
Alasdair G Kergond15b7742011-08-02 12:32:01 +010054static DEFINE_IDR(_minor_idr);
55
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070056static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040057
58static void do_deferred_remove(struct work_struct *w);
59
60static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
61
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040062static struct workqueue_struct *deferred_remove_workqueue;
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000065 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * One of these is allocated per bio.
67 */
68struct dm_io {
69 struct mapped_device *md;
70 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010072 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080073 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010074 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040075 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
78/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000079 * For request-based dm.
80 * One of these is allocated per request.
81 */
82struct dm_rq_target_io {
83 struct mapped_device *md;
84 struct dm_target *ti;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -050085 struct request *orig, *clone;
Keith Busch2eb6e1e2014-10-17 17:46:36 -060086 struct kthread_work work;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000087 int error;
88 union map_info info;
Mikulas Patockae262f342015-06-09 17:22:49 -040089 struct dm_stats_aux stats_aux;
90 unsigned long duration_jiffies;
91 unsigned n_sectors;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000092};
93
94/*
Kent Overstreet94818742012-09-07 13:44:01 -070095 * For request-based dm - the bio clones we allocate are embedded in these
96 * structs.
97 *
98 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
99 * the bioset is created - this means the bio has to come at the end of the
100 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000101 */
102struct dm_rq_clone_bio_info {
103 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100104 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -0700105 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000106};
107
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100108union map_info *dm_get_rq_mapinfo(struct request *rq)
109{
110 if (rq && rq->end_io_data)
111 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
112 return NULL;
113}
114EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
115
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700116#define MINOR_ALLOCED ((void *)-1)
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * Bits for the md->flags field.
120 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100121#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800123#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700124#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700125#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800126#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -0700127#define DMF_DEFERRED_REMOVE 6
128#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Milan Broz304f3f62008-02-08 02:11:17 +0000130/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100131 * A dummy definition to make RCU happy.
132 * struct dm_table should never be dereferenced in this file.
133 */
134struct dm_table {
135 int undefined__;
136};
137
138/*
Milan Broz304f3f62008-02-08 02:11:17 +0000139 * Work processed by per-device workqueue.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100142 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000143 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700145 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100147 /*
148 * The current mapping.
149 * Use dm_get_live_table{_fast} or take suspend_lock for
150 * dereference.
151 */
Pranith Kumar6fa99522014-10-28 15:09:57 -0700152 struct dm_table __rcu *map;
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100153
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500154 struct list_head table_devices;
155 struct mutex table_devices_lock;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 unsigned long flags;
158
Jens Axboe165125e2007-07-24 09:28:11 +0200159 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100160 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100161 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100162 struct mutex type_lock;
163
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000164 struct target_type *immutable_target_type;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800167 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 void *interface_ptr;
170
171 /*
172 * A list of ios that arrived while we were suspended.
173 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200174 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100176 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800177 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100178 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200181 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000182 */
183 struct workqueue_struct *wq;
184
185 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * io objects are allocated from here.
187 */
188 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500189 mempool_t *rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Stefan Bader9faf4002006-10-03 01:15:41 -0700191 struct bio_set *bs;
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * Event handling.
195 */
196 atomic_t event_nr;
197 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100198 atomic_t uevent_seq;
199 struct list_head uevent_list;
200 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /*
203 * freeze/thaw support require holding onto a super block
204 */
205 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100206 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800207
208 /* forced geometry settings */
209 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000210
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500211 /* kobject and completion */
212 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500213
Tejun Heod87f4c12010-09-03 11:56:19 +0200214 /* zero-length flush that will be cloned and submitted to targets */
215 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400216
Mikulas Patocka96b26c82015-01-08 18:52:26 -0500217 /* the number of internal suspends */
218 unsigned internal_suspend_count;
219
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400220 struct dm_stats stats;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600221
222 struct kthread_worker kworker;
223 struct task_struct *kworker_task;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500224
225 /* for request-based merge heuristic in dm_request_fn() */
Mike Snitzer0ce65792015-02-26 00:50:28 -0500226 unsigned seq_rq_merge_deadline_usecs;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500227 int last_rq_rw;
Mike Snitzer0ce65792015-02-26 00:50:28 -0500228 sector_t last_rq_pos;
229 ktime_t last_rq_start_time;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -0500230
231 /* for blk-mq request-based DM support */
232 struct blk_mq_tag_set tag_set;
Mike Snitzer17e149b2015-03-11 15:01:09 -0400233 bool use_blk_mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
Mike Snitzer17e149b2015-03-11 15:01:09 -0400236#ifdef CONFIG_DM_MQ_DEFAULT
237static bool use_blk_mq = true;
238#else
239static bool use_blk_mq = false;
240#endif
241
242bool dm_use_blk_mq(struct mapped_device *md)
243{
244 return md->use_blk_mq;
245}
246
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100247/*
248 * For mempools pre-allocation at the table loading time.
249 */
250struct dm_md_mempools {
251 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500252 mempool_t *rq_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100253 struct bio_set *bs;
254};
255
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500256struct table_device {
257 struct list_head list;
258 atomic_t count;
259 struct dm_dev dm_dev;
260};
261
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400262#define RESERVED_BIO_BASED_IOS 16
263#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400264#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800265static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000266static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500267static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700268
Mike Snitzerf4790822013-09-12 18:06:12 -0400269/*
Mike Snitzere8603132013-09-12 18:06:12 -0400270 * Bio-based DM's mempools' reserved IOs set by the user.
271 */
272static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
273
274/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400275 * Request-based DM's mempools' reserved IOs set by the user.
276 */
277static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
278
Mike Snitzer09c2d532015-02-27 22:25:26 -0500279static unsigned __dm_get_module_param(unsigned *module_param,
Mike Snitzerf4790822013-09-12 18:06:12 -0400280 unsigned def, unsigned max)
281{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500282 unsigned param = ACCESS_ONCE(*module_param);
283 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400284
Mike Snitzer09c2d532015-02-27 22:25:26 -0500285 if (!param)
286 modified_param = def;
287 else if (param > max)
288 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400289
Mike Snitzer09c2d532015-02-27 22:25:26 -0500290 if (modified_param) {
291 (void)cmpxchg(module_param, param, modified_param);
292 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400293 }
294
Mike Snitzer09c2d532015-02-27 22:25:26 -0500295 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400296}
297
Mike Snitzere8603132013-09-12 18:06:12 -0400298unsigned dm_get_reserved_bio_based_ios(void)
299{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500300 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzere8603132013-09-12 18:06:12 -0400301 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
302}
303EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
304
Mike Snitzerf4790822013-09-12 18:06:12 -0400305unsigned dm_get_reserved_rq_based_ios(void)
306{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500307 return __dm_get_module_param(&reserved_rq_based_ios,
Mike Snitzerf4790822013-09-12 18:06:12 -0400308 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
309}
310EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static int __init local_init(void)
313{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100314 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100317 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100319 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000321 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
322 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100323 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000324
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500325 _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
326 __alignof__(struct request), 0, NULL);
327 if (!_rq_cache)
328 goto out_free_rq_tio_cache;
329
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100330 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100331 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500332 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100333
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400334 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
335 if (!deferred_remove_workqueue) {
336 r = -ENOMEM;
337 goto out_uevent_exit;
338 }
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 _major = major;
341 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100342 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400343 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 if (!_major)
346 _major = r;
347
348 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100349
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400350out_free_workqueue:
351 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100352out_uevent_exit:
353 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500354out_free_rq_cache:
355 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000356out_free_rq_tio_cache:
357 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100358out_free_io_cache:
359 kmem_cache_destroy(_io_cache);
360
361 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364static void local_exit(void)
365{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400366 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400367 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400368
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500369 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000370 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700372 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100373 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 _major = 0;
376
377 DMINFO("cleaned up");
378}
379
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000380static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 local_init,
382 dm_target_init,
383 dm_linear_init,
384 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000385 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100386 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400388 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389};
390
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000391static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 local_exit,
393 dm_target_exit,
394 dm_linear_exit,
395 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000396 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100397 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400399 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
402static int __init dm_init(void)
403{
404 const int count = ARRAY_SIZE(_inits);
405
406 int r, i;
407
408 for (i = 0; i < count; i++) {
409 r = _inits[i]();
410 if (r)
411 goto bad;
412 }
413
414 return 0;
415
416 bad:
417 while (i--)
418 _exits[i]();
419
420 return r;
421}
422
423static void __exit dm_exit(void)
424{
425 int i = ARRAY_SIZE(_exits);
426
427 while (i--)
428 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100429
430 /*
431 * Should be empty by this point.
432 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100433 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * Block device functions
438 */
Mike Anderson432a2122009-12-10 23:52:20 +0000439int dm_deleting_md(struct mapped_device *md)
440{
441 return test_bit(DMF_DELETING, &md->flags);
442}
443
Al Virofe5f9f22008-03-02 10:29:31 -0500444static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct mapped_device *md;
447
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700448 spin_lock(&_minor_lock);
449
Al Virofe5f9f22008-03-02 10:29:31 -0500450 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700451 if (!md)
452 goto out;
453
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700454 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000455 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700456 md = NULL;
457 goto out;
458 }
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700461 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700462out:
463 spin_unlock(&_minor_lock);
464
465 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Al Virodb2a1442013-05-05 21:52:57 -0400468static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400470 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200471
Milan Broz4a1aeb92011-01-13 19:59:48 +0000472 spin_lock(&_minor_lock);
473
Mike Snitzer63a4f062015-03-23 17:01:43 -0400474 md = disk->private_data;
475 if (WARN_ON(!md))
476 goto out;
477
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400478 if (atomic_dec_and_test(&md->open_count) &&
479 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400480 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400483out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000484 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700487int dm_open_count(struct mapped_device *md)
488{
489 return atomic_read(&md->open_count);
490}
491
492/*
493 * Guarantees nothing is using the device before it's deleted.
494 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400495int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700496{
497 int r = 0;
498
499 spin_lock(&_minor_lock);
500
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400501 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700502 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400503 if (mark_deferred)
504 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
505 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
506 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700507 else
508 set_bit(DMF_DELETING, &md->flags);
509
510 spin_unlock(&_minor_lock);
511
512 return r;
513}
514
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400515int dm_cancel_deferred_remove(struct mapped_device *md)
516{
517 int r = 0;
518
519 spin_lock(&_minor_lock);
520
521 if (test_bit(DMF_DELETING, &md->flags))
522 r = -EBUSY;
523 else
524 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
525
526 spin_unlock(&_minor_lock);
527
528 return r;
529}
530
531static void do_deferred_remove(struct work_struct *w)
532{
533 dm_deferred_remove();
534}
535
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400536sector_t dm_get_size(struct mapped_device *md)
537{
538 return get_capacity(md->disk);
539}
540
Mike Snitzer9974fa22014-02-28 15:33:43 +0100541struct request_queue *dm_get_md_queue(struct mapped_device *md)
542{
543 return md->queue;
544}
545
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400546struct dm_stats *dm_get_stats(struct mapped_device *md)
547{
548 return &md->stats;
549}
550
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800551static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
552{
553 struct mapped_device *md = bdev->bd_disk->private_data;
554
555 return dm_get_geometry(md, geo);
556}
557
Al Virofe5f9f22008-03-02 10:29:31 -0500558static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700559 unsigned int cmd, unsigned long arg)
560{
Al Virofe5f9f22008-03-02 10:29:31 -0500561 struct mapped_device *md = bdev->bd_disk->private_data;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100562 int srcu_idx;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100563 struct dm_table *map;
Milan Brozaa129a22006-10-03 01:15:15 -0700564 struct dm_target *tgt;
565 int r = -ENOTTY;
566
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100567retry:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100568 map = dm_get_live_table(md, &srcu_idx);
569
Milan Brozaa129a22006-10-03 01:15:15 -0700570 if (!map || !dm_table_get_size(map))
571 goto out;
572
573 /* We only support devices that have a single target */
574 if (dm_table_get_num_targets(map) != 1)
575 goto out;
576
577 tgt = dm_table_get_target(map, 0);
Mike Snitzer4d341d82014-11-16 14:21:47 -0500578 if (!tgt->type->ioctl)
579 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700580
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000581 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700582 r = -EAGAIN;
583 goto out;
584 }
585
Mike Snitzer4d341d82014-11-16 14:21:47 -0500586 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700587
588out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100589 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700590
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100591 if (r == -ENOTCONN) {
592 msleep(10);
593 goto retry;
594 }
595
Milan Brozaa129a22006-10-03 01:15:15 -0700596 return r;
597}
598
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100599static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 return mempool_alloc(md->io_pool, GFP_NOIO);
602}
603
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100604static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 mempool_free(io, md->io_pool);
607}
608
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100609static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Mikulas Patockadba14162012-10-12 21:02:15 +0100611 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000614static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
615 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100616{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000617 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100618}
619
620static void free_rq_tio(struct dm_rq_target_io *tio)
621{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000622 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100623}
624
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500625static struct request *alloc_clone_request(struct mapped_device *md,
626 gfp_t gfp_mask)
627{
628 return mempool_alloc(md->rq_pool, gfp_mask);
629}
630
631static void free_clone_request(struct mapped_device *md, struct request *rq)
632{
633 mempool_free(rq, md->rq_pool);
634}
635
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000636static int md_in_flight(struct mapped_device *md)
637{
638 return atomic_read(&md->pending[READ]) +
639 atomic_read(&md->pending[WRITE]);
640}
641
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800642static void start_io_acct(struct dm_io *io)
643{
644 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400645 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900646 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400647 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800648
649 io->start_time = jiffies;
650
Tejun Heo074a7ac2008-08-25 19:56:14 +0900651 cpu = part_stat_lock();
652 part_round_stats(cpu, &dm_disk(md)->part0);
653 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100654 atomic_set(&dm_disk(md)->part0.in_flight[rw],
655 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400656
657 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700658 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400659 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800660}
661
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000662static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800663{
664 struct mapped_device *md = io->md;
665 struct bio *bio = io->bio;
666 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800667 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800668 int rw = bio_data_dir(bio);
669
Gu Zheng18c0b222014-11-24 11:05:26 +0800670 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800671
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400672 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700673 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400674 bio_sectors(bio), true, duration, &io->stats_aux);
675
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100676 /*
677 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200678 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100679 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100680 pending = atomic_dec_return(&md->pending[rw]);
681 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200682 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800683
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000684 /* nudge anyone waiting on suspend queue */
685 if (!pending)
686 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
690 * Add the bio to the list of deferred io.
691 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100692static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200694 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200696 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200698 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200699 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*
703 * Everyone (including functions in this file), should use this
704 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100705 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100707struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100709 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100711 return srcu_dereference(md->map, &md->io_barrier);
712}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100714void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
715{
716 srcu_read_unlock(&md->io_barrier, srcu_idx);
717}
718
719void dm_sync_table(struct mapped_device *md)
720{
721 synchronize_srcu(&md->io_barrier);
722 synchronize_rcu_expedited();
723}
724
725/*
726 * A fast alternative to dm_get_live_table/dm_put_live_table.
727 * The caller must not block between these two functions.
728 */
729static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
730{
731 rcu_read_lock();
732 return rcu_dereference(md->map);
733}
734
735static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
736{
737 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800740/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500741 * Open a table device so we can use it as a map destination.
742 */
743static int open_table_device(struct table_device *td, dev_t dev,
744 struct mapped_device *md)
745{
746 static char *_claim_ptr = "I belong to device-mapper";
747 struct block_device *bdev;
748
749 int r;
750
751 BUG_ON(td->dm_dev.bdev);
752
753 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
754 if (IS_ERR(bdev))
755 return PTR_ERR(bdev);
756
757 r = bd_link_disk_holder(bdev, dm_disk(md));
758 if (r) {
759 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
760 return r;
761 }
762
763 td->dm_dev.bdev = bdev;
764 return 0;
765}
766
767/*
768 * Close a table device that we've been using.
769 */
770static void close_table_device(struct table_device *td, struct mapped_device *md)
771{
772 if (!td->dm_dev.bdev)
773 return;
774
775 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
776 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
777 td->dm_dev.bdev = NULL;
778}
779
780static struct table_device *find_table_device(struct list_head *l, dev_t dev,
781 fmode_t mode) {
782 struct table_device *td;
783
784 list_for_each_entry(td, l, list)
785 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
786 return td;
787
788 return NULL;
789}
790
791int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
792 struct dm_dev **result) {
793 int r;
794 struct table_device *td;
795
796 mutex_lock(&md->table_devices_lock);
797 td = find_table_device(&md->table_devices, dev, mode);
798 if (!td) {
799 td = kmalloc(sizeof(*td), GFP_KERNEL);
800 if (!td) {
801 mutex_unlock(&md->table_devices_lock);
802 return -ENOMEM;
803 }
804
805 td->dm_dev.mode = mode;
806 td->dm_dev.bdev = NULL;
807
808 if ((r = open_table_device(td, dev, md))) {
809 mutex_unlock(&md->table_devices_lock);
810 kfree(td);
811 return r;
812 }
813
814 format_dev_t(td->dm_dev.name, dev);
815
816 atomic_set(&td->count, 0);
817 list_add(&td->list, &md->table_devices);
818 }
819 atomic_inc(&td->count);
820 mutex_unlock(&md->table_devices_lock);
821
822 *result = &td->dm_dev;
823 return 0;
824}
825EXPORT_SYMBOL_GPL(dm_get_table_device);
826
827void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
828{
829 struct table_device *td = container_of(d, struct table_device, dm_dev);
830
831 mutex_lock(&md->table_devices_lock);
832 if (atomic_dec_and_test(&td->count)) {
833 close_table_device(td, md);
834 list_del(&td->list);
835 kfree(td);
836 }
837 mutex_unlock(&md->table_devices_lock);
838}
839EXPORT_SYMBOL(dm_put_table_device);
840
841static void free_table_devices(struct list_head *devices)
842{
843 struct list_head *tmp, *next;
844
845 list_for_each_safe(tmp, next, devices) {
846 struct table_device *td = list_entry(tmp, struct table_device, list);
847
848 DMWARN("dm_destroy: %s still exists with %d references",
849 td->dm_dev.name, atomic_read(&td->count));
850 kfree(td);
851 }
852}
853
854/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800855 * Get the geometry associated with a dm device
856 */
857int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
858{
859 *geo = md->geometry;
860
861 return 0;
862}
863
864/*
865 * Set the geometry of a device.
866 */
867int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
868{
869 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
870
871 if (geo->start > sz) {
872 DMWARN("Start sector is beyond the geometry limits.");
873 return -EINVAL;
874 }
875
876 md->geometry = *geo;
877
878 return 0;
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/*-----------------------------------------------------------------
882 * CRUD START:
883 * A more elegant soln is in the works that uses the queue
884 * merge fn, unfortunately there are a couple of changes to
885 * the block layer that I want to make for this. So in the
886 * interests of getting something for people to use I give
887 * you this clearly demarcated crap.
888 *---------------------------------------------------------------*/
889
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800890static int __noflush_suspending(struct mapped_device *md)
891{
892 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/*
896 * Decrements the number of outstanding ios that a bio has been
897 * cloned into, completing the original io if necc.
898 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800899static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800901 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000902 int io_error;
903 struct bio *bio;
904 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800905
906 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100907 if (unlikely(error)) {
908 spin_lock_irqsave(&io->endio_lock, flags);
909 if (!(io->error > 0 && __noflush_suspending(md)))
910 io->error = error;
911 spin_unlock_irqrestore(&io->endio_lock, flags);
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800915 if (io->error == DM_ENDIO_REQUEUE) {
916 /*
917 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800918 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100919 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200920 if (__noflush_suspending(md))
921 bio_list_add_head(&md->deferred, io->bio);
922 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800923 /* noflush suspend was interrupted. */
924 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100925 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800926 }
927
Milan Brozb35f8ca2009-03-16 17:44:36 +0000928 io_error = io->error;
929 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200930 end_io_acct(io);
931 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100932
Tejun Heo6a8736d2010-09-08 18:07:00 +0200933 if (io_error == DM_ENDIO_REQUEUE)
934 return;
935
Kent Overstreet4f024f32013-10-11 15:44:27 -0700936 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100937 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200938 * Preflush done for flush with data, reissue
939 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100940 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200941 bio->bi_rw &= ~REQ_FLUSH;
942 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100943 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200944 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700945 trace_block_bio_complete(md->queue, bio, io_error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200946 bio->bi_error = io_error;
947 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950}
951
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400952static void disable_write_same(struct mapped_device *md)
953{
954 struct queue_limits *limits = dm_get_queue_limits(md);
955
956 /* device doesn't really support WRITE SAME, disable it */
957 limits->max_write_same_sectors = 0;
958}
959
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200960static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200962 int error = bio->bi_error;
zhendong chen5164bec2014-12-17 14:37:04 +0800963 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500964 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000965 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700966 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 dm_endio_fn endio = tio->ti->type->end_io;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000970 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800971 if (r < 0 || r == DM_ENDIO_REQUEUE)
972 /*
973 * error and requeue request are handled
974 * in dec_pending().
975 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800977 else if (r == DM_ENDIO_INCOMPLETE)
978 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200979 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800980 else if (r) {
981 DMWARN("unimplemented target endio return value: %d", r);
982 BUG();
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400986 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
987 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
988 disable_write_same(md);
989
Stefan Bader9faf4002006-10-03 01:15:41 -0700990 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000991 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Mike Snitzer78d8e582015-06-26 10:01:13 -0400994/*
995 * Partial completion handling for request-based dm
996 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200997static void end_clone_bio(struct bio *clone)
Mike Snitzer78d8e582015-06-26 10:01:13 -0400998{
999 struct dm_rq_clone_bio_info *info =
1000 container_of(clone, struct dm_rq_clone_bio_info, clone);
1001 struct dm_rq_target_io *tio = info->tio;
1002 struct bio *bio = info->orig;
1003 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
1004
1005 bio_put(clone);
1006
1007 if (tio->error)
1008 /*
1009 * An error has already been detected on the request.
1010 * Once error occurred, just let clone->end_io() handle
1011 * the remainder.
1012 */
1013 return;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001014 else if (bio->bi_error) {
Mike Snitzer78d8e582015-06-26 10:01:13 -04001015 /*
1016 * Don't notice the error to the upper layer yet.
1017 * The error handling decision is made by the target driver,
1018 * when the request is completed.
1019 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001020 tio->error = bio->bi_error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001021 return;
1022 }
1023
1024 /*
1025 * I/O for the bio successfully completed.
1026 * Notice the data completion to the upper layer.
1027 */
1028
1029 /*
1030 * bios are processed from the head of the list.
1031 * So the completing bio should always be rq->bio.
1032 * If it's not, something wrong is happening.
1033 */
1034 if (tio->orig->bio != bio)
1035 DMERR("bio completion is going in the middle of the request");
1036
1037 /*
1038 * Update the original request.
1039 * Do not use blk_end_request() here, because it may complete
1040 * the original request before the clone, and break the ordering.
1041 */
1042 blk_update_request(tio->orig, 0, nr_bytes);
1043}
1044
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001045static struct dm_rq_target_io *tio_from_request(struct request *rq)
1046{
1047 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1048}
1049
Mikulas Patockae262f342015-06-09 17:22:49 -04001050static void rq_end_stats(struct mapped_device *md, struct request *orig)
1051{
1052 if (unlikely(dm_stats_used(&md->stats))) {
1053 struct dm_rq_target_io *tio = tio_from_request(orig);
1054 tio->duration_jiffies = jiffies - tio->duration_jiffies;
1055 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1056 tio->n_sectors, true, tio->duration_jiffies,
1057 &tio->stats_aux);
1058 }
1059}
1060
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001061/*
1062 * Don't touch any member of the md after calling this function because
1063 * the md may be freed in dm_put() at the end of this function.
1064 * Or do dm_get() before calling this function and dm_put() later.
1065 */
Keith Busch466d89a2014-10-17 17:46:37 -06001066static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001067{
Mike Snitzer9a0e6092015-02-24 11:03:22 -05001068 int nr_requests_pending;
1069
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001070 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001071
1072 /* nudge anyone waiting on suspend queue */
Mike Snitzer9a0e6092015-02-24 11:03:22 -05001073 nr_requests_pending = md_in_flight(md);
1074 if (!nr_requests_pending)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001075 wake_up(&md->wait);
1076
Jens Axboea8c32a52012-11-06 12:24:26 +01001077 /*
1078 * Run this off this callpath, as drivers could invoke end_io while
1079 * inside their request_fn (and holding the queue lock). Calling
1080 * back into ->request_fn() could deadlock attempting to grab the
1081 * queue lock again.
1082 */
Mike Snitzer9a0e6092015-02-24 11:03:22 -05001083 if (run_queue) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001084 if (md->queue->mq_ops)
1085 blk_mq_run_hw_queues(md->queue, true);
1086 else if (!nr_requests_pending ||
1087 (nr_requests_pending >= md->queue->nr_congestion_on))
Mike Snitzer9a0e6092015-02-24 11:03:22 -05001088 blk_run_queue_async(md->queue);
1089 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001090
1091 /*
1092 * dm_put() must be at the end of this function. See the comment above
1093 */
1094 dm_put(md);
1095}
1096
Mike Snitzere5d8de32015-05-28 15:12:52 -04001097static void free_rq_clone(struct request *clone)
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001098{
1099 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001100 struct mapped_device *md = tio->md;
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001101
Mike Snitzer78d8e582015-06-26 10:01:13 -04001102 blk_rq_unprep_clone(clone);
1103
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001104 if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1105 /* stacked on blk-mq queue(s) */
Mike Snitzere5863d92014-12-17 21:08:12 -05001106 tio->ti->type->release_clone_rq(clone);
Mike Snitzer02233342015-03-10 23:49:26 -04001107 else if (!md->queue->mq_ops)
1108 /* request_fn queue stacked on request_fn queue(s) */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001109 free_clone_request(md, clone);
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001110 /*
1111 * NOTE: for the blk-mq queue stacked on request_fn queue(s) case:
1112 * no need to call free_clone_request() because we leverage blk-mq by
1113 * allocating the clone at the end of the blk-mq pdu (see: clone_rq)
1114 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001115
1116 if (!md->queue->mq_ops)
1117 free_rq_tio(tio);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001118}
1119
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001120/*
1121 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001122 * Must be called without clone's queue lock held,
1123 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001124 */
1125static void dm_end_request(struct request *clone, int error)
1126{
1127 int rw = rq_data_dir(clone);
1128 struct dm_rq_target_io *tio = clone->end_io_data;
1129 struct mapped_device *md = tio->md;
1130 struct request *rq = tio->orig;
1131
Tejun Heo29e40132010-09-08 18:07:00 +02001132 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001133 rq->errors = clone->errors;
1134 rq->resid_len = clone->resid_len;
1135
1136 if (rq->sense)
1137 /*
1138 * We are using the sense buffer of the original
1139 * request.
1140 * So setting the length of the sense data is enough.
1141 */
1142 rq->sense_len = clone->sense_len;
1143 }
1144
Mike Snitzere5d8de32015-05-28 15:12:52 -04001145 free_rq_clone(clone);
Mikulas Patockae262f342015-06-09 17:22:49 -04001146 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001147 if (!rq->q->mq_ops)
1148 blk_end_request_all(rq, error);
1149 else
1150 blk_mq_end_request(rq, error);
Tejun Heo29e40132010-09-08 18:07:00 +02001151 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001152}
1153
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001154static void dm_unprep_request(struct request *rq)
1155{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001156 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001157 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001158
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001159 if (!rq->q->mq_ops) {
1160 rq->special = NULL;
1161 rq->cmd_flags &= ~REQ_DONTPREP;
1162 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001163
Mike Snitzere5863d92014-12-17 21:08:12 -05001164 if (clone)
Mike Snitzere5d8de32015-05-28 15:12:52 -04001165 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001166}
1167
1168/*
1169 * Requeue the original request of a clone.
1170 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001171static void old_requeue_request(struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001172{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001173 struct request_queue *q = rq->q;
1174 unsigned long flags;
1175
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001176 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001177 blk_requeue_request(q, rq);
Junichi Nomura4ae99442015-05-26 08:25:54 +00001178 blk_run_queue_async(q);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001179 spin_unlock_irqrestore(q->queue_lock, flags);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001180}
1181
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001182static void dm_requeue_original_request(struct mapped_device *md,
1183 struct request *rq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001184{
1185 int rw = rq_data_dir(rq);
1186
1187 dm_unprep_request(rq);
1188
Mikulas Patockae262f342015-06-09 17:22:49 -04001189 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001190 if (!rq->q->mq_ops)
1191 old_requeue_request(rq);
1192 else {
1193 blk_mq_requeue_request(rq);
1194 blk_mq_kick_requeue_list(rq->q);
1195 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001196
Keith Busch466d89a2014-10-17 17:46:37 -06001197 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001198}
Keith Busch466d89a2014-10-17 17:46:37 -06001199
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001200static void old_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001201{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001202 unsigned long flags;
1203
1204 if (blk_queue_stopped(q))
1205 return;
1206
1207 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001208 blk_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001209 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001210}
1211
1212static void stop_queue(struct request_queue *q)
1213{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001214 if (!q->mq_ops)
1215 old_stop_queue(q);
1216 else
1217 blk_mq_stop_hw_queues(q);
1218}
1219
1220static void old_start_queue(struct request_queue *q)
1221{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001222 unsigned long flags;
1223
1224 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001225 if (blk_queue_stopped(q))
1226 blk_start_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001227 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001228}
1229
1230static void start_queue(struct request_queue *q)
1231{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001232 if (!q->mq_ops)
1233 old_start_queue(q);
1234 else
1235 blk_mq_start_stopped_hw_queues(q, true);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001236}
1237
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001238static void dm_done(struct request *clone, int error, bool mapped)
1239{
1240 int r = error;
1241 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001242 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001243
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001244 if (tio->ti) {
1245 rq_end_io = tio->ti->type->rq_end_io;
1246
1247 if (mapped && rq_end_io)
1248 r = rq_end_io(tio->ti, clone, error, &tio->info);
1249 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001250
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001251 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1252 !clone->q->limits.max_write_same_sectors))
1253 disable_write_same(tio->md);
1254
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001255 if (r <= 0)
1256 /* The target wants to complete the I/O */
1257 dm_end_request(clone, r);
1258 else if (r == DM_ENDIO_INCOMPLETE)
1259 /* The target will handle the I/O */
1260 return;
1261 else if (r == DM_ENDIO_REQUEUE)
1262 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001263 dm_requeue_original_request(tio->md, tio->orig);
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001264 else {
1265 DMWARN("unimplemented target endio return value: %d", r);
1266 BUG();
1267 }
1268}
1269
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001270/*
1271 * Request completion handler for request-based dm
1272 */
1273static void dm_softirq_done(struct request *rq)
1274{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001275 bool mapped = true;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001276 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001277 struct request *clone = tio->clone;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001278 int rw;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001279
Mike Snitzere5863d92014-12-17 21:08:12 -05001280 if (!clone) {
Mikulas Patockae262f342015-06-09 17:22:49 -04001281 rq_end_stats(tio->md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001282 rw = rq_data_dir(rq);
1283 if (!rq->q->mq_ops) {
1284 blk_end_request_all(rq, tio->error);
1285 rq_completed(tio->md, rw, false);
1286 free_rq_tio(tio);
1287 } else {
1288 blk_mq_end_request(rq, tio->error);
1289 rq_completed(tio->md, rw, false);
1290 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001291 return;
1292 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001293
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001294 if (rq->cmd_flags & REQ_FAILED)
1295 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001296
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001297 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001298}
1299
1300/*
1301 * Complete the clone and the original request with the error status
1302 * through softirq context.
1303 */
Keith Busch466d89a2014-10-17 17:46:37 -06001304static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001305{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001306 struct dm_rq_target_io *tio = tio_from_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001307
1308 tio->error = error;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001309 blk_complete_request(rq);
1310}
1311
1312/*
1313 * Complete the not-mapped clone and the original request with the error status
1314 * through softirq context.
1315 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001316 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001317 */
Keith Busch466d89a2014-10-17 17:46:37 -06001318static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001319{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001320 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001321 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001322}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001323
1324/*
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001325 * Called with the clone's queue lock held (for non-blk-mq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001326 */
1327static void end_clone_request(struct request *clone, int error)
1328{
Keith Busch466d89a2014-10-17 17:46:37 -06001329 struct dm_rq_target_io *tio = clone->end_io_data;
1330
Mike Snitzere5863d92014-12-17 21:08:12 -05001331 if (!clone->q->mq_ops) {
1332 /*
1333 * For just cleaning up the information of the queue in which
1334 * the clone was dispatched.
1335 * The clone is *NOT* freed actually here because it is alloced
1336 * from dm own mempool (REQ_ALLOCED isn't set).
1337 */
1338 __blk_put_request(clone->q, clone);
1339 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001340
1341 /*
1342 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001343 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001344 * - another request may be submitted by the upper level driver
1345 * of the stacking during the completion
1346 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001347 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001348 */
Keith Busch466d89a2014-10-17 17:46:37 -06001349 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001350}
1351
Mike Snitzer56a67df2010-08-12 04:14:10 +01001352/*
1353 * Return maximum size of I/O possible at the supplied sector up to the current
1354 * target boundary.
1355 */
1356static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001358 sector_t target_offset = dm_target_offset(ti, sector);
1359
1360 return ti->len - target_offset;
1361}
1362
1363static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1364{
1365 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001366 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001369 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001371 if (ti->max_io_len) {
1372 offset = dm_target_offset(ti, sector);
1373 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1374 max_len = sector_div(offset, ti->max_io_len);
1375 else
1376 max_len = offset & (ti->max_io_len - 1);
1377 max_len = ti->max_io_len - max_len;
1378
1379 if (len > max_len)
1380 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
1383 return len;
1384}
1385
Mike Snitzer542f9032012-07-27 15:08:00 +01001386int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1387{
1388 if (len > UINT_MAX) {
1389 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1390 (unsigned long long)len, UINT_MAX);
1391 ti->error = "Maximum size of target IO is too large";
1392 return -EINVAL;
1393 }
1394
1395 ti->max_io_len = (uint32_t) len;
1396
1397 return 0;
1398}
1399EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1400
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001401/*
1402 * A target may call dm_accept_partial_bio only from the map routine. It is
1403 * allowed for all bio types except REQ_FLUSH.
1404 *
1405 * dm_accept_partial_bio informs the dm that the target only wants to process
1406 * additional n_sectors sectors of the bio and the rest of the data should be
1407 * sent in a next bio.
1408 *
1409 * A diagram that explains the arithmetics:
1410 * +--------------------+---------------+-------+
1411 * | 1 | 2 | 3 |
1412 * +--------------------+---------------+-------+
1413 *
1414 * <-------------- *tio->len_ptr --------------->
1415 * <------- bi_size ------->
1416 * <-- n_sectors -->
1417 *
1418 * Region 1 was already iterated over with bio_advance or similar function.
1419 * (it may be empty if the target doesn't use bio_advance)
1420 * Region 2 is the remaining bio size that the target wants to process.
1421 * (it may be empty if region 1 is non-empty, although there is no reason
1422 * to make it empty)
1423 * The target requires that region 3 is to be sent in the next bio.
1424 *
1425 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1426 * the partially processed part (the sum of regions 1+2) must be the same for all
1427 * copies of the bio.
1428 */
1429void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1430{
1431 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1432 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1433 BUG_ON(bio->bi_rw & REQ_FLUSH);
1434 BUG_ON(bi_size > *tio->len_ptr);
1435 BUG_ON(n_sectors > bi_size);
1436 *tio->len_ptr -= bi_size - n_sectors;
1437 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1438}
1439EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1440
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001441static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001444 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001445 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001446 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001447 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 /*
1452 * Map the clone. If r == 0 we don't need to do
1453 * anything, the target has assumed ownership of
1454 * this io.
1455 */
1456 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001457 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001458 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001459 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001461
Mike Snitzerd07335e2010-11-16 12:52:38 +01001462 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1463 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001466 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1467 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001468 md = tio->io->md;
1469 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001470 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001471 } else if (r) {
1472 DMWARN("unimplemented target map return value: %d", r);
1473 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475}
1476
1477struct clone_info {
1478 struct mapped_device *md;
1479 struct dm_table *map;
1480 struct bio *bio;
1481 struct dm_io *io;
1482 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001483 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484};
1485
Mikulas Patockae0d66092014-03-14 18:40:39 -04001486static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001487{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001488 bio->bi_iter.bi_sector = sector;
1489 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492/*
1493 * Creates a bio that consists of range of complete bvecs.
1494 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001495static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001496 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Mikulas Patockadba14162012-10-12 21:02:15 +01001498 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001500 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001502 if (bio_integrity(bio))
1503 bio_integrity_clone(clone, bio, GFP_NOIO);
1504
1505 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1506 clone->bi_iter.bi_size = to_bytes(len);
1507
1508 if (bio_integrity(bio))
1509 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001512static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001513 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001514 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001515{
Mikulas Patockadba14162012-10-12 21:02:15 +01001516 struct dm_target_io *tio;
1517 struct bio *clone;
1518
Junichi Nomura99778272014-10-03 11:55:16 +00001519 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001520 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001521
1522 tio->io = ci->io;
1523 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001524 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001525
1526 return tio;
1527}
1528
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001529static void __clone_and_map_simple_bio(struct clone_info *ci,
1530 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001531 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001532{
Junichi Nomura99778272014-10-03 11:55:16 +00001533 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001534 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001535
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001536 tio->len_ptr = len;
1537
Junichi Nomura99778272014-10-03 11:55:16 +00001538 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001539 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001540 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001541
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001542 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001543}
1544
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001545static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001546 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001547{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001548 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001549
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001550 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001551 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001552}
1553
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001554static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001555{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001556 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001557 struct dm_target *ti;
1558
Mike Snitzerb372d362010-09-08 18:07:01 +02001559 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001560 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001561 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001562
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001563 return 0;
1564}
1565
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001566static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001567 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001568{
Mikulas Patockadba14162012-10-12 21:02:15 +01001569 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001570 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001571 unsigned target_bio_nr;
1572 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001573
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001574 /*
1575 * Does the target want to receive duplicate copies of the bio?
1576 */
1577 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1578 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001579
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001580 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001581 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001582 tio->len_ptr = len;
1583 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001584 __map_bio(tio);
1585 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001586}
1587
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001588typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001589
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001590static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001591{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001592 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001593}
1594
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001595static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001596{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001597 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001598}
1599
1600typedef bool (*is_split_required_fn)(struct dm_target *ti);
1601
1602static bool is_split_required_for_discard(struct dm_target *ti)
1603{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001604 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001605}
1606
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001607static int __send_changing_extent_only(struct clone_info *ci,
1608 get_num_bios_fn get_num_bios,
1609 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001610{
1611 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001612 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001613 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001614
Mike Snitzera79245b2010-08-12 04:14:24 +01001615 do {
1616 ti = dm_table_find_target(ci->map, ci->sector);
1617 if (!dm_target_is_valid(ti))
1618 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001619
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001620 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001621 * Even though the device advertised support for this type of
1622 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001623 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001624 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001625 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001626 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1627 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001628 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001629
Mike Snitzer23508a92012-12-21 20:23:37 +00001630 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001631 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001632 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001633 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001634
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001635 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001636
1637 ci->sector += len;
1638 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001639
1640 return 0;
1641}
1642
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001643static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001644{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001645 return __send_changing_extent_only(ci, get_num_discard_bios,
1646 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001647}
1648
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001649static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001650{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001651 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001652}
1653
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001654/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001655 * Select the correct strategy for processing a non-flush bio.
1656 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001657static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Mikulas Patockadba14162012-10-12 21:02:15 +01001659 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001660 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001661 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001663 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001664 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001665 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001666 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001667
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001668 ti = dm_table_find_target(ci->map, ci->sector);
1669 if (!dm_target_is_valid(ti))
1670 return -EIO;
1671
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001672 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001673
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001674 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001676 ci->sector += len;
1677 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001683 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001685static void __split_and_process_bio(struct mapped_device *md,
1686 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001689 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001691 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001692 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001693 return;
1694 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001695
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001696 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 ci.io = alloc_io(md);
1699 ci.io->error = 0;
1700 atomic_set(&ci.io->io_count, 1);
1701 ci.io->bio = bio;
1702 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001703 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001704 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001706 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001707
Mike Snitzerb372d362010-09-08 18:07:01 +02001708 if (bio->bi_rw & REQ_FLUSH) {
1709 ci.bio = &ci.md->flush_bio;
1710 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001711 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001712 /* dec_pending submits any data associated with flush */
1713 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001714 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001715 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001716 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001717 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001721 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723/*-----------------------------------------------------------------
1724 * CRUD END
1725 *---------------------------------------------------------------*/
1726
1727/*
1728 * The request function that just remaps the bio built up by
1729 * dm_merge_bvec.
1730 */
Mike Snitzerff36ab32015-02-23 17:56:37 -05001731static void dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Kevin Corry12f03a42006-02-01 03:04:52 -08001733 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001735 int srcu_idx;
1736 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001738 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Kent Overstreet54efd502015-04-23 22:37:18 -07001740 blk_queue_split(q, &bio, q->bio_split);
1741
Gu Zheng18c0b222014-11-24 11:05:26 +08001742 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001743
Tejun Heo6a8736d2010-09-08 18:07:00 +02001744 /* if we're suspended, we have to queue this io for later */
1745 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001746 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Tejun Heo6a8736d2010-09-08 18:07:00 +02001748 if (bio_rw(bio) != READA)
1749 queue_io(md, bio);
1750 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001751 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001752 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001755 __split_and_process_bio(md, map, bio);
1756 dm_put_live_table(md, srcu_idx);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001757 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001758}
1759
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001760int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001761{
1762 return blk_queue_stackable(md->queue);
1763}
1764
Keith Busch466d89a2014-10-17 17:46:37 -06001765static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001766{
1767 int r;
1768
Keith Busch466d89a2014-10-17 17:46:37 -06001769 if (blk_queue_io_stat(clone->q))
1770 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001771
Keith Busch466d89a2014-10-17 17:46:37 -06001772 clone->start_time = jiffies;
1773 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001774 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001775 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001776 dm_complete_request(rq, r);
1777}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001778
Mike Snitzer78d8e582015-06-26 10:01:13 -04001779static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1780 void *data)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001781{
Mike Snitzer78d8e582015-06-26 10:01:13 -04001782 struct dm_rq_target_io *tio = data;
1783 struct dm_rq_clone_bio_info *info =
1784 container_of(bio, struct dm_rq_clone_bio_info, clone);
1785
1786 info->orig = bio_orig;
1787 info->tio = tio;
1788 bio->bi_end_io = end_clone_bio;
1789
1790 return 0;
1791}
1792
1793static int setup_clone(struct request *clone, struct request *rq,
1794 struct dm_rq_target_io *tio, gfp_t gfp_mask)
1795{
1796 int r;
1797
1798 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1799 dm_rq_bio_constructor, tio);
1800 if (r)
1801 return r;
1802
1803 clone->cmd = rq->cmd;
1804 clone->cmd_len = rq->cmd_len;
1805 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001806 clone->end_io = end_clone_request;
1807 clone->end_io_data = tio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001808
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001809 tio->clone = clone;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001810
1811 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001812}
1813
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001814static struct request *clone_rq(struct request *rq, struct mapped_device *md,
Keith Busch466d89a2014-10-17 17:46:37 -06001815 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001816{
Mike Snitzer02233342015-03-10 23:49:26 -04001817 /*
1818 * Do not allocate a clone if tio->clone was already set
1819 * (see: dm_mq_queue_rq).
1820 */
1821 bool alloc_clone = !tio->clone;
1822 struct request *clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001823
Mike Snitzer02233342015-03-10 23:49:26 -04001824 if (alloc_clone) {
1825 clone = alloc_clone_request(md, gfp_mask);
1826 if (!clone)
1827 return NULL;
1828 } else
1829 clone = tio->clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001830
1831 blk_rq_init(NULL, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001832 if (setup_clone(clone, rq, tio, gfp_mask)) {
1833 /* -ENOMEM */
1834 if (alloc_clone)
1835 free_clone_request(md, clone);
1836 return NULL;
1837 }
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001838
1839 return clone;
1840}
1841
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001842static void map_tio_request(struct kthread_work *work);
1843
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001844static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1845 struct mapped_device *md)
1846{
1847 tio->md = md;
1848 tio->ti = NULL;
1849 tio->clone = NULL;
1850 tio->orig = rq;
1851 tio->error = 0;
1852 memset(&tio->info, 0, sizeof(tio->info));
Mike Snitzer02233342015-03-10 23:49:26 -04001853 if (md->kworker_task)
1854 init_kthread_work(&tio->work, map_tio_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001855}
1856
Keith Busch466d89a2014-10-17 17:46:37 -06001857static struct dm_rq_target_io *prep_tio(struct request *rq,
1858 struct mapped_device *md, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001859{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001860 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001861 int srcu_idx;
1862 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001863
1864 tio = alloc_rq_tio(md, gfp_mask);
1865 if (!tio)
1866 return NULL;
1867
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001868 init_tio(tio, rq, md);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001869
Mike Snitzere5863d92014-12-17 21:08:12 -05001870 table = dm_get_live_table(md, &srcu_idx);
1871 if (!dm_table_mq_request_based(table)) {
1872 if (!clone_rq(rq, md, tio, gfp_mask)) {
1873 dm_put_live_table(md, srcu_idx);
1874 free_rq_tio(tio);
1875 return NULL;
1876 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001877 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001878 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001879
Keith Busch466d89a2014-10-17 17:46:37 -06001880 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001881}
1882
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001883/*
1884 * Called with the queue lock held.
1885 */
1886static int dm_prep_fn(struct request_queue *q, struct request *rq)
1887{
1888 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001889 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001890
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001891 if (unlikely(rq->special)) {
1892 DMWARN("Already has something in rq->special.");
1893 return BLKPREP_KILL;
1894 }
1895
Keith Busch466d89a2014-10-17 17:46:37 -06001896 tio = prep_tio(rq, md, GFP_ATOMIC);
1897 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001898 return BLKPREP_DEFER;
1899
Keith Busch466d89a2014-10-17 17:46:37 -06001900 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001901 rq->cmd_flags |= REQ_DONTPREP;
1902
1903 return BLKPREP_OK;
1904}
1905
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001906/*
1907 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05001908 * 0 : the request has been processed
1909 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1910 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001911 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001912static int map_request(struct dm_rq_target_io *tio, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001913 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001914{
Mike Snitzere5863d92014-12-17 21:08:12 -05001915 int r;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001916 struct dm_target *ti = tio->ti;
Mike Snitzere5863d92014-12-17 21:08:12 -05001917 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001918
Mike Snitzere5863d92014-12-17 21:08:12 -05001919 if (tio->clone) {
1920 clone = tio->clone;
1921 r = ti->type->map_rq(ti, clone, &tio->info);
1922 } else {
1923 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1924 if (r < 0) {
1925 /* The target wants to complete the I/O */
1926 dm_kill_unmapped_request(rq, r);
1927 return r;
1928 }
Junichi Nomura3a140752015-05-27 04:22:07 +00001929 if (r != DM_MAPIO_REMAPPED)
1930 return r;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001931 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
1932 /* -ENOMEM */
1933 ti->type->release_clone_rq(clone);
1934 return DM_MAPIO_REQUEUE;
1935 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001936 }
1937
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001938 switch (r) {
1939 case DM_MAPIO_SUBMITTED:
1940 /* The target has taken the I/O to submit by itself later */
1941 break;
1942 case DM_MAPIO_REMAPPED:
1943 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001944 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06001945 blk_rq_pos(rq));
1946 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001947 break;
1948 case DM_MAPIO_REQUEUE:
1949 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001950 dm_requeue_original_request(md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001951 break;
1952 default:
1953 if (r > 0) {
1954 DMWARN("unimplemented target map return value: %d", r);
1955 BUG();
1956 }
1957
1958 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06001959 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05001960 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001961 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001962
Mike Snitzere5863d92014-12-17 21:08:12 -05001963 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001964}
1965
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001966static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001967{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001968 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05001969 struct request *rq = tio->orig;
1970 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001971
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001972 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001973 dm_requeue_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001974}
1975
Keith Busch466d89a2014-10-17 17:46:37 -06001976static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001977{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001978 if (!orig->q->mq_ops)
1979 blk_start_request(orig);
1980 else
1981 blk_mq_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06001982 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001983
Mike Snitzer0ce65792015-02-26 00:50:28 -05001984 if (md->seq_rq_merge_deadline_usecs) {
1985 md->last_rq_pos = rq_end_sector(orig);
1986 md->last_rq_rw = rq_data_dir(orig);
1987 md->last_rq_start_time = ktime_get();
1988 }
Mike Snitzerde3ec862015-02-24 21:58:21 -05001989
Mikulas Patockae262f342015-06-09 17:22:49 -04001990 if (unlikely(dm_stats_used(&md->stats))) {
1991 struct dm_rq_target_io *tio = tio_from_request(orig);
1992 tio->duration_jiffies = jiffies;
1993 tio->n_sectors = blk_rq_sectors(orig);
1994 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1995 tio->n_sectors, false, 0, &tio->stats_aux);
1996 }
1997
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001998 /*
1999 * Hold the md reference here for the in-flight I/O.
2000 * We can't rely on the reference count by device opener,
2001 * because the device may be closed during the request completion
2002 * when all bios are completed.
2003 * See the comment in rq_completed() too.
2004 */
2005 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002006}
2007
Mike Snitzer0ce65792015-02-26 00:50:28 -05002008#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
2009
2010ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
2011{
2012 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
2013}
2014
2015ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
2016 const char *buf, size_t count)
2017{
2018 unsigned deadline;
2019
Mike Snitzer17e149b2015-03-11 15:01:09 -04002020 if (!dm_request_based(md) || md->use_blk_mq)
Mike Snitzer0ce65792015-02-26 00:50:28 -05002021 return count;
2022
2023 if (kstrtouint(buf, 10, &deadline))
2024 return -EINVAL;
2025
2026 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
2027 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
2028
2029 md->seq_rq_merge_deadline_usecs = deadline;
2030
2031 return count;
2032}
2033
2034static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
2035{
2036 ktime_t kt_deadline;
2037
2038 if (!md->seq_rq_merge_deadline_usecs)
2039 return false;
2040
2041 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
2042 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
2043
2044 return !ktime_after(ktime_get(), kt_deadline);
2045}
2046
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002047/*
2048 * q->request_fn for request-based dm.
2049 * Called with the queue lock held.
2050 */
2051static void dm_request_fn(struct request_queue *q)
2052{
2053 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002054 int srcu_idx;
2055 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002056 struct dm_target *ti;
Keith Busch466d89a2014-10-17 17:46:37 -06002057 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002058 struct dm_rq_target_io *tio;
Tejun Heo29e40132010-09-08 18:07:00 +02002059 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002060
2061 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002062 * For suspend, check blk_queue_stopped() and increment
2063 * ->pending within a single queue_lock not to increment the
2064 * number of in-flight I/Os after the queue is stopped in
2065 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002066 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002067 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002068 rq = blk_peek_request(q);
2069 if (!rq)
Mike Snitzer9d1deb82015-02-24 20:49:18 -05002070 goto out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002071
Tejun Heo29e40132010-09-08 18:07:00 +02002072 /* always use block 0 to find the target for flushes for now */
2073 pos = 0;
2074 if (!(rq->cmd_flags & REQ_FLUSH))
2075 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002076
Tejun Heo29e40132010-09-08 18:07:00 +02002077 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002078 if (!dm_target_is_valid(ti)) {
2079 /*
Keith Busch466d89a2014-10-17 17:46:37 -06002080 * Must perform setup, that rq_completed() requires,
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002081 * before calling dm_kill_unmapped_request
2082 */
2083 DMERR_LIMIT("request attempted access beyond the end of device");
Keith Busch466d89a2014-10-17 17:46:37 -06002084 dm_start_request(md, rq);
2085 dm_kill_unmapped_request(rq, -EIO);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002086 continue;
2087 }
Tejun Heo29e40132010-09-08 18:07:00 +02002088
Mike Snitzer0ce65792015-02-26 00:50:28 -05002089 if (dm_request_peeked_before_merge_deadline(md) &&
2090 md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
Mike Snitzerde3ec862015-02-24 21:58:21 -05002091 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq))
2092 goto delay_and_out;
2093
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002094 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01002095 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002096
Keith Busch466d89a2014-10-17 17:46:37 -06002097 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002098
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002099 tio = tio_from_request(rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002100 /* Establish tio->ti before queuing work (map_tio_request) */
2101 tio->ti = ti;
2102 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00002103 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002104 }
2105
2106 goto out;
2107
Jens Axboe7eaceac2011-03-10 08:52:07 +01002108delay_and_out:
Mike Snitzerd548b342015-03-05 22:21:10 -05002109 blk_delay_queue(q, HZ / 100);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002110out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002111 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114static int dm_any_congested(void *congested_data, int bdi_bits)
2115{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002116 int r = bdi_bits;
2117 struct mapped_device *md = congested_data;
2118 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002120 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002121 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002122 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002123 /*
2124 * Request-based dm cares about only own queue for
2125 * the query about congestion status of request_queue
2126 */
2127 if (dm_request_based(md))
Tejun Heo44522262015-05-22 17:13:26 -04002128 r = md->queue->backing_dev_info.wb.state &
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002129 bdi_bits;
2130 else
2131 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002132 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002133 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002134 }
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return r;
2137}
2138
2139/*-----------------------------------------------------------------
2140 * An IDR is used to keep track of allocated minor numbers.
2141 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002142static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002144 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002146 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149/*
2150 * See if the device with a specific minor # is free.
2151 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002152static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002154 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 if (minor >= (1 << MINORBITS))
2157 return -EINVAL;
2158
Tejun Heoc9d76be2013-02-27 17:04:26 -08002159 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002160 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Tejun Heoc9d76be2013-02-27 17:04:26 -08002162 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002164 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002165 idr_preload_end();
2166 if (r < 0)
2167 return r == -ENOSPC ? -EBUSY : r;
2168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002171static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002173 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Tejun Heoc9d76be2013-02-27 17:04:26 -08002175 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002176 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Tejun Heoc9d76be2013-02-27 17:04:26 -08002178 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002180 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002181 idr_preload_end();
2182 if (r < 0)
2183 return r;
2184 *minor = r;
2185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002188static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Mikulas Patocka53d59142009-04-02 19:55:37 +01002190static void dm_wq_work(struct work_struct *work);
2191
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002192static void dm_init_md_queue(struct mapped_device *md)
2193{
2194 /*
2195 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002196 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002197 * The type is decided at the first table loading time.
2198 * To prevent problematic device stacking, clear the queue flag
2199 * for request stacking support until then.
2200 *
2201 * This queue is new, so no concurrency on the queue_flags.
2202 */
2203 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002204}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002205
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002206static void dm_init_old_md_queue(struct mapped_device *md)
2207{
Mike Snitzer17e149b2015-03-11 15:01:09 -04002208 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002209 dm_init_md_queue(md);
2210
2211 /*
2212 * Initialize aspects of queue that aren't relevant for blk-mq
2213 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002214 md->queue->queuedata = md;
2215 md->queue->backing_dev_info.congested_fn = dm_any_congested;
2216 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002217
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002218 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002219}
2220
Mike Snitzer0f209722015-04-28 11:50:29 -04002221static void cleanup_mapped_device(struct mapped_device *md)
2222{
2223 cleanup_srcu_struct(&md->io_barrier);
2224
2225 if (md->wq)
2226 destroy_workqueue(md->wq);
2227 if (md->kworker_task)
2228 kthread_stop(md->kworker_task);
2229 if (md->io_pool)
2230 mempool_destroy(md->io_pool);
2231 if (md->rq_pool)
2232 mempool_destroy(md->rq_pool);
2233 if (md->bs)
2234 bioset_free(md->bs);
2235
2236 if (md->disk) {
2237 spin_lock(&_minor_lock);
2238 md->disk->private_data = NULL;
2239 spin_unlock(&_minor_lock);
2240 if (blk_get_integrity(md->disk))
2241 blk_integrity_unregister(md->disk);
2242 del_gendisk(md->disk);
2243 put_disk(md->disk);
2244 }
2245
2246 if (md->queue)
2247 blk_cleanup_queue(md->queue);
2248
2249 if (md->bdev) {
2250 bdput(md->bdev);
2251 md->bdev = NULL;
2252 }
2253}
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255/*
2256 * Allocate and initialise a blank device with a given minor.
2257 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002258static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002261 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002262 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 if (!md) {
2265 DMWARN("unable to allocate device, out of memory.");
2266 return NULL;
2267 }
2268
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002269 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002270 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002273 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002274 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002275 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002276 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002278 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002280 r = init_srcu_struct(&md->io_barrier);
2281 if (r < 0)
2282 goto bad_io_barrier;
2283
Mike Snitzer17e149b2015-03-11 15:01:09 -04002284 md->use_blk_mq = use_blk_mq;
Mike Snitzera5664da2010-08-12 04:14:01 +01002285 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002286 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002287 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002288 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002289 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002291 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002293 atomic_set(&md->uevent_seq, 0);
2294 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002295 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002296 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002298 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04002300 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002302 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 md->disk = alloc_disk(1);
2305 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04002306 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002308 atomic_set(&md->pending[0], 0);
2309 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002310 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002311 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002312 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002313 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002314 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 md->disk->major = _major;
2317 md->disk->first_minor = minor;
2318 md->disk->fops = &dm_blk_dops;
2319 md->disk->queue = md->queue;
2320 md->disk->private_data = md;
2321 sprintf(md->disk->disk_name, "dm-%d", minor);
2322 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002323 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Tejun Heo670368a2013-07-30 08:40:21 -04002325 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002326 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002327 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002328
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002329 md->bdev = bdget_disk(md->disk, 0);
2330 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002331 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002332
Tejun Heo6a8736d2010-09-08 18:07:00 +02002333 bio_init(&md->flush_bio);
2334 md->flush_bio.bi_bdev = md->bdev;
2335 md->flush_bio.bi_rw = WRITE_FLUSH;
2336
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002337 dm_stats_init(&md->stats);
2338
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002339 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002340 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002341 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002342 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002343
2344 BUG_ON(old_md != MINOR_ALLOCED);
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return md;
2347
Mike Snitzer0f209722015-04-28 11:50:29 -04002348bad:
2349 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002350bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002352bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002353 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002354bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 kfree(md);
2356 return NULL;
2357}
2358
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002359static void unlock_fs(struct mapped_device *md);
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361static void free_dev(struct mapped_device *md)
2362{
Tejun Heof331c022008-09-03 09:01:48 +02002363 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002364
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002365 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002366
Mike Snitzer0f209722015-04-28 11:50:29 -04002367 cleanup_mapped_device(md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04002368 if (md->use_blk_mq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002369 blk_mq_free_tag_set(&md->tag_set);
Mike Snitzer0f209722015-04-28 11:50:29 -04002370
2371 free_table_devices(&md->table_devices);
2372 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002373 free_minor(minor);
2374
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002375 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 kfree(md);
2377}
2378
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002379static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2380{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002381 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002382
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002383 if (md->bs) {
2384 /* The md already has necessary mempools. */
2385 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002386 /*
2387 * Reload bioset because front_pad may have changed
2388 * because a different table was loaded.
2389 */
2390 bioset_free(md->bs);
2391 md->bs = p->bs;
2392 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002393 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002394 /*
2395 * There's no need to reload with request-based dm
2396 * because the size of front_pad doesn't change.
2397 * Note for future: If you are to reload bioset,
2398 * prep-ed requests in the queue may refer
2399 * to bio from the old bioset, so you must walk
2400 * through the queue to unprep.
2401 */
2402 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002403 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002404
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002405 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2406
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002407 md->io_pool = p->io_pool;
2408 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002409 md->rq_pool = p->rq_pool;
2410 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002411 md->bs = p->bs;
2412 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002413
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002414out:
Mike Snitzer02233342015-03-10 23:49:26 -04002415 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002416 dm_table_free_md_mempools(t);
2417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419/*
2420 * Bind a table to the device.
2421 */
2422static void event_callback(void *context)
2423{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002424 unsigned long flags;
2425 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct mapped_device *md = (struct mapped_device *) context;
2427
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002428 spin_lock_irqsave(&md->uevent_lock, flags);
2429 list_splice_init(&md->uevent_list, &uevents);
2430 spin_unlock_irqrestore(&md->uevent_lock, flags);
2431
Tejun Heoed9e1982008-08-25 19:56:05 +09002432 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 atomic_inc(&md->event_nr);
2435 wake_up(&md->eventq);
2436}
2437
Mike Snitzerc2176492011-01-13 19:53:46 +00002438/*
2439 * Protected by md->suspend_lock obtained by dm_swap_table().
2440 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002441static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002443 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002445 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002448/*
2449 * Returns old map, which caller must destroy.
2450 */
2451static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2452 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002454 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002455 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 sector_t size;
2457
2458 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002459
2460 /*
2461 * Wipe any geometry if the size of the table changed.
2462 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002463 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002464 memset(&md->geometry, 0, sizeof(md->geometry));
2465
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002466 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002468 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002469
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002470 /*
2471 * The queue hasn't been stopped yet, if the old table type wasn't
2472 * for request-based during suspension. So stop it to prevent
2473 * I/O mapping before resume.
2474 * This must be done before setting the queue restrictions,
2475 * because request-based dm may be run just after the setting.
2476 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002477 if (dm_table_request_based(t))
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002478 stop_queue(q);
2479
2480 __bind_mempools(md, t);
2481
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002482 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002483 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002484 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2485
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002486 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002487 if (old_map)
2488 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002489
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002490 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
Alasdair G Kergona7940152009-12-10 23:52:23 +00002493/*
2494 * Returns unbound table for the caller to free.
2495 */
2496static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002498 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002501 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302504 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002505 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002506
2507 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510/*
2511 * Constructor for a new device.
2512 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002513int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 struct mapped_device *md;
2516
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002517 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (!md)
2519 return -ENXIO;
2520
Milan Broz784aae72009-01-06 03:05:12 +00002521 dm_sysfs_init(md);
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 *result = md;
2524 return 0;
2525}
2526
Mike Snitzera5664da2010-08-12 04:14:01 +01002527/*
2528 * Functions to manage md->type.
2529 * All are required to hold md->type_lock.
2530 */
2531void dm_lock_md_type(struct mapped_device *md)
2532{
2533 mutex_lock(&md->type_lock);
2534}
2535
2536void dm_unlock_md_type(struct mapped_device *md)
2537{
2538 mutex_unlock(&md->type_lock);
2539}
2540
2541void dm_set_md_type(struct mapped_device *md, unsigned type)
2542{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002543 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002544 md->type = type;
2545}
2546
2547unsigned dm_get_md_type(struct mapped_device *md)
2548{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002549 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002550 return md->type;
2551}
2552
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002553struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2554{
2555 return md->immutable_target_type;
2556}
2557
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002558/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002559 * The queue_limits are only valid as long as you have a reference
2560 * count on 'md'.
2561 */
2562struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2563{
2564 BUG_ON(!atomic_read(&md->holders));
2565 return &md->queue->limits;
2566}
2567EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2568
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002569static void init_rq_based_worker_thread(struct mapped_device *md)
2570{
2571 /* Initialize the request-based DM worker thread */
2572 init_kthread_worker(&md->kworker);
2573 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2574 "kdmwork-%s", dm_device_name(md));
2575}
2576
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002577/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002578 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2579 */
2580static int dm_init_request_based_queue(struct mapped_device *md)
2581{
2582 struct request_queue *q = NULL;
2583
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002584 /* Fully initialize the queue */
2585 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2586 if (!q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002587 return -EINVAL;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002588
Mike Snitzer0ce65792015-02-26 00:50:28 -05002589 /* disable dm_request_fn's merge heuristic by default */
2590 md->seq_rq_merge_deadline_usecs = 0;
2591
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002592 md->queue = q;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002593 dm_init_old_md_queue(md);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002594 blk_queue_softirq_done(md->queue, dm_softirq_done);
2595 blk_queue_prep_rq(md->queue, dm_prep_fn);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002596
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002597 init_rq_based_worker_thread(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002598
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002599 elv_register_queue(md->queue);
2600
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002601 return 0;
2602}
2603
2604static int dm_mq_init_request(void *data, struct request *rq,
2605 unsigned int hctx_idx, unsigned int request_idx,
2606 unsigned int numa_node)
2607{
2608 struct mapped_device *md = data;
2609 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2610
2611 /*
2612 * Must initialize md member of tio, otherwise it won't
2613 * be available in dm_mq_queue_rq.
2614 */
2615 tio->md = md;
2616
2617 return 0;
2618}
2619
2620static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2621 const struct blk_mq_queue_data *bd)
2622{
2623 struct request *rq = bd->rq;
2624 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2625 struct mapped_device *md = tio->md;
2626 int srcu_idx;
2627 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2628 struct dm_target *ti;
2629 sector_t pos;
2630
2631 /* always use block 0 to find the target for flushes for now */
2632 pos = 0;
2633 if (!(rq->cmd_flags & REQ_FLUSH))
2634 pos = blk_rq_pos(rq);
2635
2636 ti = dm_table_find_target(map, pos);
2637 if (!dm_target_is_valid(ti)) {
2638 dm_put_live_table(md, srcu_idx);
2639 DMERR_LIMIT("request attempted access beyond the end of device");
2640 /*
2641 * Must perform setup, that rq_completed() requires,
2642 * before returning BLK_MQ_RQ_QUEUE_ERROR
2643 */
2644 dm_start_request(md, rq);
2645 return BLK_MQ_RQ_QUEUE_ERROR;
2646 }
2647 dm_put_live_table(md, srcu_idx);
2648
2649 if (ti->type->busy && ti->type->busy(ti))
2650 return BLK_MQ_RQ_QUEUE_BUSY;
2651
2652 dm_start_request(md, rq);
2653
2654 /* Init tio using md established in .init_request */
2655 init_tio(tio, rq, md);
2656
Mike Snitzer02233342015-03-10 23:49:26 -04002657 /*
2658 * Establish tio->ti before queuing work (map_tio_request)
2659 * or making direct call to map_request().
2660 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002661 tio->ti = ti;
Mike Snitzer02233342015-03-10 23:49:26 -04002662
2663 /* Clone the request if underlying devices aren't blk-mq */
2664 if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) {
2665 /* clone request is allocated at the end of the pdu */
2666 tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io);
Mike Snitzer45714fb2015-05-27 15:25:27 -04002667 (void) clone_rq(rq, md, tio, GFP_ATOMIC);
Mike Snitzer02233342015-03-10 23:49:26 -04002668 queue_kthread_work(&md->kworker, &tio->work);
2669 } else {
2670 /* Direct call is fine since .queue_rq allows allocations */
Mike Snitzer45714fb2015-05-27 15:25:27 -04002671 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
2672 /* Undo dm_start_request() before requeuing */
Mikulas Patockae262f342015-06-09 17:22:49 -04002673 rq_end_stats(md, rq);
Mike Snitzer45714fb2015-05-27 15:25:27 -04002674 rq_completed(md, rq_data_dir(rq), false);
2675 return BLK_MQ_RQ_QUEUE_BUSY;
2676 }
Mike Snitzer02233342015-03-10 23:49:26 -04002677 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002678
2679 return BLK_MQ_RQ_QUEUE_OK;
2680}
2681
2682static struct blk_mq_ops dm_mq_ops = {
2683 .queue_rq = dm_mq_queue_rq,
2684 .map_queue = blk_mq_map_queue,
2685 .complete = dm_softirq_done,
2686 .init_request = dm_mq_init_request,
2687};
2688
2689static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
2690{
Mike Snitzer02233342015-03-10 23:49:26 -04002691 unsigned md_type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002692 struct request_queue *q;
2693 int err;
2694
2695 memset(&md->tag_set, 0, sizeof(md->tag_set));
2696 md->tag_set.ops = &dm_mq_ops;
2697 md->tag_set.queue_depth = BLKDEV_MAX_RQ;
2698 md->tag_set.numa_node = NUMA_NO_NODE;
2699 md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2700 md->tag_set.nr_hw_queues = 1;
Mike Snitzer02233342015-03-10 23:49:26 -04002701 if (md_type == DM_TYPE_REQUEST_BASED) {
2702 /* make the memory for non-blk-mq clone part of the pdu */
2703 md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request);
2704 } else
2705 md->tag_set.cmd_size = sizeof(struct dm_rq_target_io);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002706 md->tag_set.driver_data = md;
2707
2708 err = blk_mq_alloc_tag_set(&md->tag_set);
2709 if (err)
2710 return err;
2711
2712 q = blk_mq_init_allocated_queue(&md->tag_set, md->queue);
2713 if (IS_ERR(q)) {
2714 err = PTR_ERR(q);
2715 goto out_tag_set;
2716 }
2717 md->queue = q;
2718 dm_init_md_queue(md);
2719
2720 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
2721 blk_mq_register_disk(md->disk);
2722
Mike Snitzer02233342015-03-10 23:49:26 -04002723 if (md_type == DM_TYPE_REQUEST_BASED)
2724 init_rq_based_worker_thread(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002725
2726 return 0;
2727
2728out_tag_set:
2729 blk_mq_free_tag_set(&md->tag_set);
2730 return err;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002731}
2732
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002733static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2734{
2735 if (type == DM_TYPE_BIO_BASED)
2736 return type;
2737
2738 return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2739}
2740
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002741/*
2742 * Setup the DM device's queue based on md's type
2743 */
2744int dm_setup_md_queue(struct mapped_device *md)
2745{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002746 int r;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002747 unsigned md_type = filter_md_type(dm_get_md_type(md), md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002748
2749 switch (md_type) {
2750 case DM_TYPE_REQUEST_BASED:
2751 r = dm_init_request_based_queue(md);
2752 if (r) {
Mike Snitzerff36ab32015-02-23 17:56:37 -05002753 DMWARN("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002754 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002755 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002756 break;
2757 case DM_TYPE_MQ_REQUEST_BASED:
2758 r = dm_init_request_based_blk_mq_queue(md);
2759 if (r) {
2760 DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
2761 return r;
2762 }
2763 break;
2764 case DM_TYPE_BIO_BASED:
2765 dm_init_old_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002766 blk_queue_make_request(md->queue, dm_make_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002767 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002768 }
2769
2770 return 0;
2771}
2772
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002773struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 unsigned minor = MINOR(dev);
2777
2778 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2779 return NULL;
2780
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002781 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002784 if (md) {
2785 if ((md == MINOR_ALLOCED ||
2786 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2787 dm_deleting_md(md) ||
2788 test_bit(DMF_FREEING, &md->flags))) {
2789 md = NULL;
2790 goto out;
2791 }
2792 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002795out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002796 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
David Teigland637842c2006-01-06 00:20:00 -08002798 return md;
2799}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002800EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002801
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002802void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002803{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002804 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
2806
2807void dm_set_mdptr(struct mapped_device *md, void *ptr)
2808{
2809 md->interface_ptr = ptr;
2810}
2811
2812void dm_get(struct mapped_device *md)
2813{
2814 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002815 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002818int dm_hold(struct mapped_device *md)
2819{
2820 spin_lock(&_minor_lock);
2821 if (test_bit(DMF_FREEING, &md->flags)) {
2822 spin_unlock(&_minor_lock);
2823 return -EBUSY;
2824 }
2825 dm_get(md);
2826 spin_unlock(&_minor_lock);
2827 return 0;
2828}
2829EXPORT_SYMBOL_GPL(dm_hold);
2830
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002831const char *dm_device_name(struct mapped_device *md)
2832{
2833 return md->name;
2834}
2835EXPORT_SYMBOL_GPL(dm_device_name);
2836
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002837static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002839 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002840 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002842 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002843
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002844 map = dm_get_live_table(md, &srcu_idx);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002845
2846 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002847 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2848 set_bit(DMF_FREEING, &md->flags);
2849 spin_unlock(&_minor_lock);
2850
Mike Snitzer02233342015-03-10 23:49:26 -04002851 if (dm_request_based(md) && md->kworker_task)
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002852 flush_kthread_worker(&md->kworker);
2853
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002854 /*
2855 * Take suspend_lock so that presuspend and postsuspend methods
2856 * do not race with internal suspend.
2857 */
2858 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002859 if (!dm_suspended_md(md)) {
2860 dm_table_presuspend_targets(map);
2861 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002863 mutex_unlock(&md->suspend_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002864
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002865 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2866 dm_put_live_table(md, srcu_idx);
2867
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002868 /*
2869 * Rare, but there may be I/O requests still going to complete,
2870 * for example. Wait for all references to disappear.
2871 * No one should increment the reference count of the mapped_device,
2872 * after the mapped_device state becomes DMF_FREEING.
2873 */
2874 if (wait)
2875 while (atomic_read(&md->holders))
2876 msleep(1);
2877 else if (atomic_read(&md->holders))
2878 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2879 dm_device_name(md), atomic_read(&md->holders));
2880
2881 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002882 dm_table_destroy(__unbind(md));
2883 free_dev(md);
2884}
2885
2886void dm_destroy(struct mapped_device *md)
2887{
2888 __dm_destroy(md, true);
2889}
2890
2891void dm_destroy_immediate(struct mapped_device *md)
2892{
2893 __dm_destroy(md, false);
2894}
2895
2896void dm_put(struct mapped_device *md)
2897{
2898 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
Edward Goggin79eb8852007-05-09 02:32:56 -07002900EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Mikulas Patocka401600d2009-04-02 19:55:38 +01002902static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002903{
2904 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002905 DECLARE_WAITQUEUE(wait, current);
2906
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002907 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002908
2909 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002910 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002911
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002912 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002913 break;
2914
Mikulas Patocka401600d2009-04-02 19:55:38 +01002915 if (interruptible == TASK_INTERRUPTIBLE &&
2916 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002917 r = -EINTR;
2918 break;
2919 }
2920
2921 io_schedule();
2922 }
2923 set_current_state(TASK_RUNNING);
2924
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002925 remove_wait_queue(&md->wait, &wait);
2926
Milan Broz46125c12008-02-08 02:10:30 +00002927 return r;
2928}
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930/*
2931 * Process the deferred bios
2932 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002933static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Mikulas Patockaef208582009-04-02 19:55:38 +01002935 struct mapped_device *md = container_of(work, struct mapped_device,
2936 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002937 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002938 int srcu_idx;
2939 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002941 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002942
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002943 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002944 spin_lock_irq(&md->deferred_lock);
2945 c = bio_list_pop(&md->deferred);
2946 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002947
Tejun Heo6a8736d2010-09-08 18:07:00 +02002948 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002949 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002950
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002951 if (dm_request_based(md))
2952 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002953 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002954 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002955 }
Milan Broz73d410c2008-02-08 02:10:25 +00002956
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002957 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}
2959
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002960static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002961{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002962 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002963 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002964 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002965}
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002968 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002970struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Mike Christie87eb5b22013-03-01 22:45:48 +00002972 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002973 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002974 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Daniel Walkere61290a2008-02-08 02:10:08 +00002976 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002979 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Mike Snitzer3ae70652012-09-26 23:45:45 +01002982 /*
2983 * If the new table has no data devices, retain the existing limits.
2984 * This helps multipath with queue_if_no_path if all paths disappear,
2985 * then new I/O is queued based on these limits, and then some paths
2986 * reappear.
2987 */
2988 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002989 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002990 if (live_map)
2991 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002992 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002993 }
2994
Mike Christie87eb5b22013-03-01 22:45:48 +00002995 if (!live_map) {
2996 r = dm_calculate_queue_limits(table, &limits);
2997 if (r) {
2998 map = ERR_PTR(r);
2999 goto out;
3000 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003001 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003002
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003003 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003005out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003006 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003007 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
3010/*
3011 * Functions to lock and unlock any filesystem running on the
3012 * device.
3013 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003014static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003016 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003019
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003020 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003021 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003022 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003023 md->frozen_sb = NULL;
3024 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003025 }
3026
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003027 set_bit(DMF_FROZEN, &md->flags);
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return 0;
3030}
3031
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003032static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003034 if (!test_bit(DMF_FROZEN, &md->flags))
3035 return;
3036
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003037 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003039 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04003043 * If __dm_suspend returns 0, the device is completely quiescent
3044 * now. There is no request-processing activity. All new requests
3045 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003046 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04003047 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003048 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003049static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3050 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003052 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3053 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3054 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003056 /*
3057 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
3058 * This flag is cleared before dm_suspend returns.
3059 */
3060 if (noflush)
3061 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
3062
Mike Snitzerd67ee212014-10-28 20:13:31 -04003063 /*
3064 * This gets reverted if there's an error later and the targets
3065 * provide the .presuspend_undo hook.
3066 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003067 dm_table_presuspend_targets(map);
3068
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003069 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003070 * Flush I/O to the device.
3071 * Any I/O submitted after lock_fs() may not be flushed.
3072 * noflush takes precedence over do_lockfs.
3073 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003074 */
3075 if (!noflush && do_lockfs) {
3076 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003077 if (r) {
3078 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003079 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04003080 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
3083 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003084 * Here we must make sure that no processes are submitting requests
3085 * to target drivers i.e. no one may be executing
3086 * __split_and_process_bio. This is called from dm_request and
3087 * dm_wq_work.
3088 *
3089 * To get all processes out of __split_and_process_bio in dm_request,
3090 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02003091 * __split_and_process_bio from dm_request and quiesce the thread
3092 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
3093 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01003095 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003096 if (map)
3097 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003099 /*
Tejun Heo29e40132010-09-08 18:07:00 +02003100 * Stop md->queue before flushing md->wq in case request-based
3101 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003102 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003103 if (dm_request_based(md)) {
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003104 stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04003105 if (md->kworker_task)
3106 flush_kthread_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003107 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003108
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003109 flush_workqueue(md->wq);
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003112 * At this point no more requests are entering target request routines.
3113 * We call dm_wait_for_completion to wait for all existing requests
3114 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003116 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Milan Broz6d6f10d2008-02-08 02:10:22 +00003118 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01003119 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003120 if (map)
3121 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00003124 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003125 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00003126
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003127 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003128 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003129
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003130 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003131 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003132 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003133 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003134
Mike Snitzerffcc3932014-10-28 18:34:52 -04003135 return r;
3136}
3137
3138/*
3139 * We need to be able to change a mapping table under a mounted
3140 * filesystem. For example we might want to move some data in
3141 * the background. Before the table can be swapped with
3142 * dm_bind_table, dm_suspend must be called to flush any in
3143 * flight bios and ensure that any further io gets deferred.
3144 */
3145/*
3146 * Suspend mechanism in request-based dm.
3147 *
3148 * 1. Flush all I/Os by lock_fs() if needed.
3149 * 2. Stop dispatching any I/O by stopping the request_queue.
3150 * 3. Wait for all in-flight I/Os to be completed or requeued.
3151 *
3152 * To abort suspend, start the request_queue.
3153 */
3154int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3155{
3156 struct dm_table *map = NULL;
3157 int r = 0;
3158
3159retry:
3160 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3161
3162 if (dm_suspended_md(md)) {
3163 r = -EINVAL;
3164 goto out_unlock;
3165 }
3166
3167 if (dm_suspended_internally_md(md)) {
3168 /* already internally suspended, wait for internal resume */
3169 mutex_unlock(&md->suspend_lock);
3170 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3171 if (r)
3172 return r;
3173 goto retry;
3174 }
3175
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003176 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003177
3178 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3179 if (r)
3180 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 set_bit(DMF_SUSPENDED, &md->flags);
3183
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00003184 dm_table_postsuspend_targets(map);
3185
Alasdair G Kergond2874832006-11-08 17:44:43 -08003186out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00003187 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003188 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
Mike Snitzerffcc3932014-10-28 18:34:52 -04003191static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003193 if (map) {
3194 int r = dm_table_resume_targets(map);
3195 if (r)
3196 return r;
3197 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003198
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003199 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003200
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003201 /*
3202 * Flushing deferred I/Os must be done after targets are resumed
3203 * so that mapping of targets can work correctly.
3204 * Request-based dm is queueing the deferred I/Os in its request_queue.
3205 */
3206 if (dm_request_based(md))
3207 start_queue(md->queue);
3208
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003209 unlock_fs(md);
3210
Mike Snitzerffcc3932014-10-28 18:34:52 -04003211 return 0;
3212}
3213
3214int dm_resume(struct mapped_device *md)
3215{
3216 int r = -EINVAL;
3217 struct dm_table *map = NULL;
3218
3219retry:
3220 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3221
3222 if (!dm_suspended_md(md))
3223 goto out;
3224
3225 if (dm_suspended_internally_md(md)) {
3226 /* already internally suspended, wait for internal resume */
3227 mutex_unlock(&md->suspend_lock);
3228 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3229 if (r)
3230 return r;
3231 goto retry;
3232 }
3233
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003234 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003235 if (!map || !dm_table_get_size(map))
3236 goto out;
3237
3238 r = __dm_resume(md, map);
3239 if (r)
3240 goto out;
3241
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003242 clear_bit(DMF_SUSPENDED, &md->flags);
3243
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003244 r = 0;
3245out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003246 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003247
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003248 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003251/*
3252 * Internal suspend/resume works like userspace-driven suspend. It waits
3253 * until all bios finish and prevents issuing new bios to the target drivers.
3254 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003255 */
3256
Mike Snitzerffcc3932014-10-28 18:34:52 -04003257static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3258{
3259 struct dm_table *map = NULL;
3260
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003261 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003262 return; /* nested internal suspend */
3263
3264 if (dm_suspended_md(md)) {
3265 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3266 return; /* nest suspend */
3267 }
3268
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003269 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003270
3271 /*
3272 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3273 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3274 * would require changing .presuspend to return an error -- avoid this
3275 * until there is a need for more elaborate variants of internal suspend.
3276 */
3277 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3278
3279 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3280
3281 dm_table_postsuspend_targets(map);
3282}
3283
3284static void __dm_internal_resume(struct mapped_device *md)
3285{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003286 BUG_ON(!md->internal_suspend_count);
3287
3288 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003289 return; /* resume from nested internal suspend */
3290
3291 if (dm_suspended_md(md))
3292 goto done; /* resume from nested suspend */
3293
3294 /*
3295 * NOTE: existing callers don't need to call dm_table_resume_targets
3296 * (which may fail -- so best to avoid it for now by passing NULL map)
3297 */
3298 (void) __dm_resume(md, NULL);
3299
3300done:
3301 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3302 smp_mb__after_atomic();
3303 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3304}
3305
3306void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003307{
3308 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003309 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3310 mutex_unlock(&md->suspend_lock);
3311}
3312EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3313
3314void dm_internal_resume(struct mapped_device *md)
3315{
3316 mutex_lock(&md->suspend_lock);
3317 __dm_internal_resume(md);
3318 mutex_unlock(&md->suspend_lock);
3319}
3320EXPORT_SYMBOL_GPL(dm_internal_resume);
3321
3322/*
3323 * Fast variants of internal suspend/resume hold md->suspend_lock,
3324 * which prevents interaction with userspace-driven suspend.
3325 */
3326
3327void dm_internal_suspend_fast(struct mapped_device *md)
3328{
3329 mutex_lock(&md->suspend_lock);
3330 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003331 return;
3332
3333 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3334 synchronize_srcu(&md->io_barrier);
3335 flush_workqueue(md->wq);
3336 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3337}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003338EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003339
Mike Snitzerffcc3932014-10-28 18:34:52 -04003340void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003341{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003342 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003343 goto done;
3344
3345 dm_queue_flush(md);
3346
3347done:
3348 mutex_unlock(&md->suspend_lock);
3349}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003350EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352/*-----------------------------------------------------------------
3353 * Event notification.
3354 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003355int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003356 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003357{
Milan Broz60935eb2009-06-22 10:12:30 +01003358 char udev_cookie[DM_COOKIE_LENGTH];
3359 char *envp[] = { udev_cookie, NULL };
3360
3361 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003362 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003363 else {
3364 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3365 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003366 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3367 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003368 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003369}
3370
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003371uint32_t dm_next_uevent_seq(struct mapped_device *md)
3372{
3373 return atomic_add_return(1, &md->uevent_seq);
3374}
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376uint32_t dm_get_event_nr(struct mapped_device *md)
3377{
3378 return atomic_read(&md->event_nr);
3379}
3380
3381int dm_wait_event(struct mapped_device *md, int event_nr)
3382{
3383 return wait_event_interruptible(md->eventq,
3384 (event_nr != atomic_read(&md->event_nr)));
3385}
3386
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003387void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3388{
3389 unsigned long flags;
3390
3391 spin_lock_irqsave(&md->uevent_lock, flags);
3392 list_add(elist, &md->uevent_list);
3393 spin_unlock_irqrestore(&md->uevent_lock, flags);
3394}
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396/*
3397 * The gendisk is only valid as long as you have a reference
3398 * count on 'md'.
3399 */
3400struct gendisk *dm_disk(struct mapped_device *md)
3401{
3402 return md->disk;
3403}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00003404EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Milan Broz784aae72009-01-06 03:05:12 +00003406struct kobject *dm_kobject(struct mapped_device *md)
3407{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003408 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003409}
3410
Milan Broz784aae72009-01-06 03:05:12 +00003411struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3412{
3413 struct mapped_device *md;
3414
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003415 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003416
Milan Broz4d89b7b2009-06-22 10:12:11 +01003417 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003418 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003419 return NULL;
3420
Milan Broz784aae72009-01-06 03:05:12 +00003421 dm_get(md);
3422 return md;
3423}
3424
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003425int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
3427 return test_bit(DMF_SUSPENDED, &md->flags);
3428}
3429
Mike Snitzerffcc3932014-10-28 18:34:52 -04003430int dm_suspended_internally_md(struct mapped_device *md)
3431{
3432 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3433}
3434
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003435int dm_test_deferred_remove_flag(struct mapped_device *md)
3436{
3437 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3438}
3439
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003440int dm_suspended(struct dm_target *ti)
3441{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003442 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003443}
3444EXPORT_SYMBOL_GPL(dm_suspended);
3445
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003446int dm_noflush_suspending(struct dm_target *ti)
3447{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003448 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003449}
3450EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3451
Mike Snitzer78d8e582015-06-26 10:01:13 -04003452struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
3453 unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003454{
Mike Snitzer78d8e582015-06-26 10:01:13 -04003455 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3456 struct kmem_cache *cachep = NULL;
3457 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003458 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003459
3460 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003461 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003462
Mike Snitzer78d8e582015-06-26 10:01:13 -04003463 type = filter_md_type(type, md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04003464
Mike Snitzer78d8e582015-06-26 10:01:13 -04003465 switch (type) {
3466 case DM_TYPE_BIO_BASED:
3467 cachep = _io_cache;
3468 pool_size = dm_get_reserved_bio_based_ios();
3469 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
3470 break;
3471 case DM_TYPE_REQUEST_BASED:
3472 cachep = _rq_tio_cache;
3473 pool_size = dm_get_reserved_rq_based_ios();
3474 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3475 if (!pools->rq_pool)
3476 goto out;
3477 /* fall through to setup remaining rq-based pools */
3478 case DM_TYPE_MQ_REQUEST_BASED:
3479 if (!pool_size)
3480 pool_size = dm_get_reserved_rq_based_ios();
3481 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3482 /* per_bio_data_size is not used. See __bind_mempools(). */
3483 WARN_ON(per_bio_data_size != 0);
3484 break;
3485 default:
3486 BUG();
3487 }
3488
3489 if (cachep) {
3490 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3491 if (!pools->io_pool)
3492 goto out;
3493 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003494
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003495 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003496 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003497 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003498
Martin K. Petersena91a2782011-03-17 11:11:05 +01003499 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003500 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003501
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003502 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003503
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003504out:
3505 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003506
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003507 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003508}
3509
3510void dm_free_md_mempools(struct dm_md_mempools *pools)
3511{
3512 if (!pools)
3513 return;
3514
3515 if (pools->io_pool)
3516 mempool_destroy(pools->io_pool);
3517
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003518 if (pools->rq_pool)
3519 mempool_destroy(pools->rq_pool);
3520
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003521 if (pools->bs)
3522 bioset_free(pools->bs);
3523
3524 kfree(pools);
3525}
3526
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003527static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 .open = dm_blk_open,
3529 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003530 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003531 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 .owner = THIS_MODULE
3533};
3534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535/*
3536 * module hooks
3537 */
3538module_init(dm_init);
3539module_exit(dm_exit);
3540
3541module_param(major, uint, 0);
3542MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003543
Mike Snitzere8603132013-09-12 18:06:12 -04003544module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3545MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3546
Mike Snitzerf4790822013-09-12 18:06:12 -04003547module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3548MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3549
Mike Snitzer17e149b2015-03-11 15:01:09 -04003550module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
3551MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553MODULE_DESCRIPTION(DM_NAME " driver");
3554MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3555MODULE_LICENSE("GPL");