blob: dd834927bc66e87bf4ec433bc96889068d3bbb82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080020#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010021#include <linux/delay.h>
Mike Snitzerffcc3932014-10-28 18:34:52 -040022#include <linux/wait.h>
Keith Busch2eb6e1e2014-10-17 17:46:36 -060023#include <linux/kthread.h>
Mike Snitzer0ce65792015-02-26 00:50:28 -050024#include <linux/ktime.h>
Mike Snitzerde3ec862015-02-24 21:58:21 -050025#include <linux/elevator.h> /* for rq_end_sector() */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -050026#include <linux/blk-mq.h>
Christoph Hellwig71cdb692015-10-15 14:10:51 +020027#include <linux/pr.h>
Li Zefan55782132009-06-09 13:43:05 +080028
29#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Alasdair G Kergon72d94862006-06-26 00:27:35 -070031#define DM_MSG_PREFIX "core"
32
Namhyung Kim71a16732011-10-31 20:18:54 +000033#ifdef CONFIG_PRINTK
34/*
35 * ratelimit state to be used in DMXXX_LIMIT().
36 */
37DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
38 DEFAULT_RATELIMIT_INTERVAL,
39 DEFAULT_RATELIMIT_BURST);
40EXPORT_SYMBOL(dm_ratelimit_state);
41#endif
42
Milan Broz60935eb2009-06-22 10:12:30 +010043/*
44 * Cookies are numeric values sent with CHANGE and REMOVE
45 * uevents while resuming, removing or renaming the device.
46 */
47#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
48#define DM_COOKIE_LENGTH 24
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static const char *_name = DM_NAME;
51
52static unsigned int major = 0;
53static unsigned int _major = 0;
54
Alasdair G Kergond15b7742011-08-02 12:32:01 +010055static DEFINE_IDR(_minor_idr);
56
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070057static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040058
59static void do_deferred_remove(struct work_struct *w);
60
61static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
62
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040063static struct workqueue_struct *deferred_remove_workqueue;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000066 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * One of these is allocated per bio.
68 */
69struct dm_io {
70 struct mapped_device *md;
71 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010073 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080074 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010075 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040076 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
79/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000080 * For request-based dm.
81 * One of these is allocated per request.
82 */
83struct dm_rq_target_io {
84 struct mapped_device *md;
85 struct dm_target *ti;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -050086 struct request *orig, *clone;
Keith Busch2eb6e1e2014-10-17 17:46:36 -060087 struct kthread_work work;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000088 int error;
89 union map_info info;
Mikulas Patockae262f342015-06-09 17:22:49 -040090 struct dm_stats_aux stats_aux;
91 unsigned long duration_jiffies;
92 unsigned n_sectors;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000093};
94
95/*
Kent Overstreet94818742012-09-07 13:44:01 -070096 * For request-based dm - the bio clones we allocate are embedded in these
97 * structs.
98 *
99 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
100 * the bioset is created - this means the bio has to come at the end of the
101 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000102 */
103struct dm_rq_clone_bio_info {
104 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100105 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -0700106 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000107};
108
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100109union map_info *dm_get_rq_mapinfo(struct request *rq)
110{
111 if (rq && rq->end_io_data)
112 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
113 return NULL;
114}
115EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
116
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700117#define MINOR_ALLOCED ((void *)-1)
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * Bits for the md->flags field.
121 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100122#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800124#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700125#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700126#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800127#define DMF_NOFLUSH_SUSPENDING 5
Kent Overstreet8ae12662015-04-27 23:48:34 -0700128#define DMF_DEFERRED_REMOVE 6
129#define DMF_SUSPENDED_INTERNALLY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Milan Broz304f3f62008-02-08 02:11:17 +0000131/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100132 * A dummy definition to make RCU happy.
133 * struct dm_table should never be dereferenced in this file.
134 */
135struct dm_table {
136 int undefined__;
137};
138
139/*
Milan Broz304f3f62008-02-08 02:11:17 +0000140 * Work processed by per-device workqueue.
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100143 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000144 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700146 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100148 /*
149 * The current mapping.
150 * Use dm_get_live_table{_fast} or take suspend_lock for
151 * dereference.
152 */
Pranith Kumar6fa99522014-10-28 15:09:57 -0700153 struct dm_table __rcu *map;
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100154
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500155 struct list_head table_devices;
156 struct mutex table_devices_lock;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned long flags;
159
Jens Axboe165125e2007-07-24 09:28:11 +0200160 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100161 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100162 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100163 struct mutex type_lock;
164
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000165 struct target_type *immutable_target_type;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800168 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 void *interface_ptr;
171
172 /*
173 * A list of ios that arrived while we were suspended.
174 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200175 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100177 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800178 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100179 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200182 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000183 */
184 struct workqueue_struct *wq;
185
186 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * io objects are allocated from here.
188 */
189 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500190 mempool_t *rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Stefan Bader9faf4002006-10-03 01:15:41 -0700192 struct bio_set *bs;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /*
195 * Event handling.
196 */
197 atomic_t event_nr;
198 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100199 atomic_t uevent_seq;
200 struct list_head uevent_list;
201 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /*
204 * freeze/thaw support require holding onto a super block
205 */
206 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100207 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800208
209 /* forced geometry settings */
210 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000211
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500212 /* kobject and completion */
213 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500214
Tejun Heod87f4c12010-09-03 11:56:19 +0200215 /* zero-length flush that will be cloned and submitted to targets */
216 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400217
Mikulas Patocka96b26c82015-01-08 18:52:26 -0500218 /* the number of internal suspends */
219 unsigned internal_suspend_count;
220
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400221 struct dm_stats stats;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600222
223 struct kthread_worker kworker;
224 struct task_struct *kworker_task;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500225
226 /* for request-based merge heuristic in dm_request_fn() */
Mike Snitzer0ce65792015-02-26 00:50:28 -0500227 unsigned seq_rq_merge_deadline_usecs;
Mike Snitzerde3ec862015-02-24 21:58:21 -0500228 int last_rq_rw;
Mike Snitzer0ce65792015-02-26 00:50:28 -0500229 sector_t last_rq_pos;
230 ktime_t last_rq_start_time;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -0500231
232 /* for blk-mq request-based DM support */
233 struct blk_mq_tag_set tag_set;
Mike Snitzer17e149b2015-03-11 15:01:09 -0400234 bool use_blk_mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
Mike Snitzer17e149b2015-03-11 15:01:09 -0400237#ifdef CONFIG_DM_MQ_DEFAULT
238static bool use_blk_mq = true;
239#else
240static bool use_blk_mq = false;
241#endif
242
243bool dm_use_blk_mq(struct mapped_device *md)
244{
245 return md->use_blk_mq;
246}
247
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100248/*
249 * For mempools pre-allocation at the table loading time.
250 */
251struct dm_md_mempools {
252 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500253 mempool_t *rq_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100254 struct bio_set *bs;
255};
256
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500257struct table_device {
258 struct list_head list;
259 atomic_t count;
260 struct dm_dev dm_dev;
261};
262
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400263#define RESERVED_BIO_BASED_IOS 16
264#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400265#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800266static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000267static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500268static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700269
Mike Snitzerf4790822013-09-12 18:06:12 -0400270/*
Mike Snitzere8603132013-09-12 18:06:12 -0400271 * Bio-based DM's mempools' reserved IOs set by the user.
272 */
273static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
274
275/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400276 * Request-based DM's mempools' reserved IOs set by the user.
277 */
278static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
279
Mike Snitzer09c2d532015-02-27 22:25:26 -0500280static unsigned __dm_get_module_param(unsigned *module_param,
Mike Snitzerf4790822013-09-12 18:06:12 -0400281 unsigned def, unsigned max)
282{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500283 unsigned param = ACCESS_ONCE(*module_param);
284 unsigned modified_param = 0;
Mike Snitzerf4790822013-09-12 18:06:12 -0400285
Mike Snitzer09c2d532015-02-27 22:25:26 -0500286 if (!param)
287 modified_param = def;
288 else if (param > max)
289 modified_param = max;
Mike Snitzerf4790822013-09-12 18:06:12 -0400290
Mike Snitzer09c2d532015-02-27 22:25:26 -0500291 if (modified_param) {
292 (void)cmpxchg(module_param, param, modified_param);
293 param = modified_param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400294 }
295
Mike Snitzer09c2d532015-02-27 22:25:26 -0500296 return param;
Mike Snitzerf4790822013-09-12 18:06:12 -0400297}
298
Mike Snitzere8603132013-09-12 18:06:12 -0400299unsigned dm_get_reserved_bio_based_ios(void)
300{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500301 return __dm_get_module_param(&reserved_bio_based_ios,
Mike Snitzere8603132013-09-12 18:06:12 -0400302 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
303}
304EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
305
Mike Snitzerf4790822013-09-12 18:06:12 -0400306unsigned dm_get_reserved_rq_based_ios(void)
307{
Mike Snitzer09c2d532015-02-27 22:25:26 -0500308 return __dm_get_module_param(&reserved_rq_based_ios,
Mike Snitzerf4790822013-09-12 18:06:12 -0400309 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
310}
311EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int __init local_init(void)
314{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100315 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100318 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100320 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000322 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
323 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100324 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000325
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500326 _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
327 __alignof__(struct request), 0, NULL);
328 if (!_rq_cache)
329 goto out_free_rq_tio_cache;
330
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100331 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100332 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500333 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100334
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400335 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
336 if (!deferred_remove_workqueue) {
337 r = -ENOMEM;
338 goto out_uevent_exit;
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 _major = major;
342 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100343 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400344 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (!_major)
347 _major = r;
348
349 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100350
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400351out_free_workqueue:
352 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100353out_uevent_exit:
354 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500355out_free_rq_cache:
356 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000357out_free_rq_tio_cache:
358 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100359out_free_io_cache:
360 kmem_cache_destroy(_io_cache);
361
362 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static void local_exit(void)
366{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400367 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400368 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400369
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500370 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000371 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700373 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100374 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 _major = 0;
377
378 DMINFO("cleaned up");
379}
380
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000381static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 local_init,
383 dm_target_init,
384 dm_linear_init,
385 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000386 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100387 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400389 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
391
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000392static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 local_exit,
394 dm_target_exit,
395 dm_linear_exit,
396 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000397 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100398 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400400 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
403static int __init dm_init(void)
404{
405 const int count = ARRAY_SIZE(_inits);
406
407 int r, i;
408
409 for (i = 0; i < count; i++) {
410 r = _inits[i]();
411 if (r)
412 goto bad;
413 }
414
415 return 0;
416
417 bad:
418 while (i--)
419 _exits[i]();
420
421 return r;
422}
423
424static void __exit dm_exit(void)
425{
426 int i = ARRAY_SIZE(_exits);
427
428 while (i--)
429 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100430
431 /*
432 * Should be empty by this point.
433 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100434 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437/*
438 * Block device functions
439 */
Mike Anderson432a2122009-12-10 23:52:20 +0000440int dm_deleting_md(struct mapped_device *md)
441{
442 return test_bit(DMF_DELETING, &md->flags);
443}
444
Al Virofe5f9f22008-03-02 10:29:31 -0500445static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct mapped_device *md;
448
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700449 spin_lock(&_minor_lock);
450
Al Virofe5f9f22008-03-02 10:29:31 -0500451 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700452 if (!md)
453 goto out;
454
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700455 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000456 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700457 md = NULL;
458 goto out;
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700462 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700463out:
464 spin_unlock(&_minor_lock);
465
466 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Al Virodb2a1442013-05-05 21:52:57 -0400469static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Mike Snitzer63a4f062015-03-23 17:01:43 -0400471 struct mapped_device *md;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200472
Milan Broz4a1aeb92011-01-13 19:59:48 +0000473 spin_lock(&_minor_lock);
474
Mike Snitzer63a4f062015-03-23 17:01:43 -0400475 md = disk->private_data;
476 if (WARN_ON(!md))
477 goto out;
478
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400479 if (atomic_dec_and_test(&md->open_count) &&
480 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400481 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 dm_put(md);
Mike Snitzer63a4f062015-03-23 17:01:43 -0400484out:
Milan Broz4a1aeb92011-01-13 19:59:48 +0000485 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700488int dm_open_count(struct mapped_device *md)
489{
490 return atomic_read(&md->open_count);
491}
492
493/*
494 * Guarantees nothing is using the device before it's deleted.
495 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400496int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700497{
498 int r = 0;
499
500 spin_lock(&_minor_lock);
501
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400502 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700503 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400504 if (mark_deferred)
505 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
506 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
507 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700508 else
509 set_bit(DMF_DELETING, &md->flags);
510
511 spin_unlock(&_minor_lock);
512
513 return r;
514}
515
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400516int dm_cancel_deferred_remove(struct mapped_device *md)
517{
518 int r = 0;
519
520 spin_lock(&_minor_lock);
521
522 if (test_bit(DMF_DELETING, &md->flags))
523 r = -EBUSY;
524 else
525 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
526
527 spin_unlock(&_minor_lock);
528
529 return r;
530}
531
532static void do_deferred_remove(struct work_struct *w)
533{
534 dm_deferred_remove();
535}
536
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400537sector_t dm_get_size(struct mapped_device *md)
538{
539 return get_capacity(md->disk);
540}
541
Mike Snitzer9974fa22014-02-28 15:33:43 +0100542struct request_queue *dm_get_md_queue(struct mapped_device *md)
543{
544 return md->queue;
545}
546
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400547struct dm_stats *dm_get_stats(struct mapped_device *md)
548{
549 return &md->stats;
550}
551
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800552static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
553{
554 struct mapped_device *md = bdev->bd_disk->private_data;
555
556 return dm_get_geometry(md, geo);
557}
558
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200559static int dm_get_live_table_for_ioctl(struct mapped_device *md,
560 struct dm_target **tgt, struct block_device **bdev,
561 fmode_t *mode, int *srcu_idx)
Milan Brozaa129a22006-10-03 01:15:15 -0700562{
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100563 struct dm_table *map;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200564 int r;
Milan Brozaa129a22006-10-03 01:15:15 -0700565
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100566retry:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200567 r = -ENOTTY;
568 map = dm_get_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700569 if (!map || !dm_table_get_size(map))
570 goto out;
571
572 /* We only support devices that have a single target */
573 if (dm_table_get_num_targets(map) != 1)
574 goto out;
575
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200576 *tgt = dm_table_get_target(map, 0);
577
578 if (!(*tgt)->type->prepare_ioctl)
Mike Snitzer4d341d82014-11-16 14:21:47 -0500579 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
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200586 r = (*tgt)->type->prepare_ioctl(*tgt, bdev, mode);
587 if (r < 0)
588 goto out;
589
590 return r;
Milan Brozaa129a22006-10-03 01:15:15 -0700591
592out:
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200593 dm_put_live_table(md, *srcu_idx);
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +0000594 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100595 msleep(10);
596 goto retry;
597 }
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200598 return r;
599}
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100600
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200601static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
602 unsigned int cmd, unsigned long arg)
603{
604 struct mapped_device *md = bdev->bd_disk->private_data;
605 struct dm_target *tgt;
Mike Snitzer647a20d2015-11-17 14:11:34 -0500606 struct block_device *tgt_bdev = NULL;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200607 int srcu_idx, r;
608
Mike Snitzer647a20d2015-11-17 14:11:34 -0500609 r = dm_get_live_table_for_ioctl(md, &tgt, &tgt_bdev, &mode, &srcu_idx);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200610 if (r < 0)
611 return r;
612
613 if (r > 0) {
614 /*
615 * Target determined this ioctl is being issued against
616 * a logical partition of the parent bdev; so extra
617 * validation is needed.
618 */
619 r = scsi_verify_blk_ioctl(NULL, cmd);
620 if (r)
621 goto out;
622 }
623
Mike Snitzer647a20d2015-11-17 14:11:34 -0500624 r = __blkdev_driver_ioctl(tgt_bdev, mode, cmd, arg);
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200625out:
626 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700627 return r;
628}
629
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100630static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 return mempool_alloc(md->io_pool, GFP_NOIO);
633}
634
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100635static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 mempool_free(io, md->io_pool);
638}
639
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100640static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Mikulas Patockadba14162012-10-12 21:02:15 +0100642 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000645static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
646 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100647{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000648 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100649}
650
651static void free_rq_tio(struct dm_rq_target_io *tio)
652{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000653 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100654}
655
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500656static struct request *alloc_clone_request(struct mapped_device *md,
657 gfp_t gfp_mask)
658{
659 return mempool_alloc(md->rq_pool, gfp_mask);
660}
661
662static void free_clone_request(struct mapped_device *md, struct request *rq)
663{
664 mempool_free(rq, md->rq_pool);
665}
666
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000667static int md_in_flight(struct mapped_device *md)
668{
669 return atomic_read(&md->pending[READ]) +
670 atomic_read(&md->pending[WRITE]);
671}
672
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800673static void start_io_acct(struct dm_io *io)
674{
675 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400676 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900677 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400678 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800679
680 io->start_time = jiffies;
681
Tejun Heo074a7ac2008-08-25 19:56:14 +0900682 cpu = part_stat_lock();
683 part_round_stats(cpu, &dm_disk(md)->part0);
684 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100685 atomic_set(&dm_disk(md)->part0.in_flight[rw],
686 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400687
688 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700689 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400690 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800691}
692
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000693static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800694{
695 struct mapped_device *md = io->md;
696 struct bio *bio = io->bio;
697 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800698 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800699 int rw = bio_data_dir(bio);
700
Gu Zheng18c0b222014-11-24 11:05:26 +0800701 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800702
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400703 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700704 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400705 bio_sectors(bio), true, duration, &io->stats_aux);
706
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100707 /*
708 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200709 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100710 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100711 pending = atomic_dec_return(&md->pending[rw]);
712 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200713 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800714
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000715 /* nudge anyone waiting on suspend queue */
716 if (!pending)
717 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * Add the bio to the list of deferred io.
722 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100723static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200725 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200727 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200729 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200730 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733/*
734 * Everyone (including functions in this file), should use this
735 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100736 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100738struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100740 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100742 return srcu_dereference(md->map, &md->io_barrier);
743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100745void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
746{
747 srcu_read_unlock(&md->io_barrier, srcu_idx);
748}
749
750void dm_sync_table(struct mapped_device *md)
751{
752 synchronize_srcu(&md->io_barrier);
753 synchronize_rcu_expedited();
754}
755
756/*
757 * A fast alternative to dm_get_live_table/dm_put_live_table.
758 * The caller must not block between these two functions.
759 */
760static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
761{
762 rcu_read_lock();
763 return rcu_dereference(md->map);
764}
765
766static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
767{
768 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800771/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500772 * Open a table device so we can use it as a map destination.
773 */
774static int open_table_device(struct table_device *td, dev_t dev,
775 struct mapped_device *md)
776{
777 static char *_claim_ptr = "I belong to device-mapper";
778 struct block_device *bdev;
779
780 int r;
781
782 BUG_ON(td->dm_dev.bdev);
783
784 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
785 if (IS_ERR(bdev))
786 return PTR_ERR(bdev);
787
788 r = bd_link_disk_holder(bdev, dm_disk(md));
789 if (r) {
790 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
791 return r;
792 }
793
794 td->dm_dev.bdev = bdev;
795 return 0;
796}
797
798/*
799 * Close a table device that we've been using.
800 */
801static void close_table_device(struct table_device *td, struct mapped_device *md)
802{
803 if (!td->dm_dev.bdev)
804 return;
805
806 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
807 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
808 td->dm_dev.bdev = NULL;
809}
810
811static struct table_device *find_table_device(struct list_head *l, dev_t dev,
812 fmode_t mode) {
813 struct table_device *td;
814
815 list_for_each_entry(td, l, list)
816 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
817 return td;
818
819 return NULL;
820}
821
822int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
823 struct dm_dev **result) {
824 int r;
825 struct table_device *td;
826
827 mutex_lock(&md->table_devices_lock);
828 td = find_table_device(&md->table_devices, dev, mode);
829 if (!td) {
830 td = kmalloc(sizeof(*td), GFP_KERNEL);
831 if (!td) {
832 mutex_unlock(&md->table_devices_lock);
833 return -ENOMEM;
834 }
835
836 td->dm_dev.mode = mode;
837 td->dm_dev.bdev = NULL;
838
839 if ((r = open_table_device(td, dev, md))) {
840 mutex_unlock(&md->table_devices_lock);
841 kfree(td);
842 return r;
843 }
844
845 format_dev_t(td->dm_dev.name, dev);
846
847 atomic_set(&td->count, 0);
848 list_add(&td->list, &md->table_devices);
849 }
850 atomic_inc(&td->count);
851 mutex_unlock(&md->table_devices_lock);
852
853 *result = &td->dm_dev;
854 return 0;
855}
856EXPORT_SYMBOL_GPL(dm_get_table_device);
857
858void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
859{
860 struct table_device *td = container_of(d, struct table_device, dm_dev);
861
862 mutex_lock(&md->table_devices_lock);
863 if (atomic_dec_and_test(&td->count)) {
864 close_table_device(td, md);
865 list_del(&td->list);
866 kfree(td);
867 }
868 mutex_unlock(&md->table_devices_lock);
869}
870EXPORT_SYMBOL(dm_put_table_device);
871
872static void free_table_devices(struct list_head *devices)
873{
874 struct list_head *tmp, *next;
875
876 list_for_each_safe(tmp, next, devices) {
877 struct table_device *td = list_entry(tmp, struct table_device, list);
878
879 DMWARN("dm_destroy: %s still exists with %d references",
880 td->dm_dev.name, atomic_read(&td->count));
881 kfree(td);
882 }
883}
884
885/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800886 * Get the geometry associated with a dm device
887 */
888int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
889{
890 *geo = md->geometry;
891
892 return 0;
893}
894
895/*
896 * Set the geometry of a device.
897 */
898int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
899{
900 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
901
902 if (geo->start > sz) {
903 DMWARN("Start sector is beyond the geometry limits.");
904 return -EINVAL;
905 }
906
907 md->geometry = *geo;
908
909 return 0;
910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912/*-----------------------------------------------------------------
913 * CRUD START:
914 * A more elegant soln is in the works that uses the queue
915 * merge fn, unfortunately there are a couple of changes to
916 * the block layer that I want to make for this. So in the
917 * interests of getting something for people to use I give
918 * you this clearly demarcated crap.
919 *---------------------------------------------------------------*/
920
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800921static int __noflush_suspending(struct mapped_device *md)
922{
923 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/*
927 * Decrements the number of outstanding ios that a bio has been
928 * cloned into, completing the original io if necc.
929 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800930static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800932 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000933 int io_error;
934 struct bio *bio;
935 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800936
937 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100938 if (unlikely(error)) {
939 spin_lock_irqsave(&io->endio_lock, flags);
940 if (!(io->error > 0 && __noflush_suspending(md)))
941 io->error = error;
942 spin_unlock_irqrestore(&io->endio_lock, flags);
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800946 if (io->error == DM_ENDIO_REQUEUE) {
947 /*
948 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800949 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100950 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200951 if (__noflush_suspending(md))
952 bio_list_add_head(&md->deferred, io->bio);
953 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800954 /* noflush suspend was interrupted. */
955 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100956 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800957 }
958
Milan Brozb35f8ca2009-03-16 17:44:36 +0000959 io_error = io->error;
960 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200961 end_io_acct(io);
962 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100963
Tejun Heo6a8736d2010-09-08 18:07:00 +0200964 if (io_error == DM_ENDIO_REQUEUE)
965 return;
966
Kent Overstreet4f024f32013-10-11 15:44:27 -0700967 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100968 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200969 * Preflush done for flush with data, reissue
970 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100971 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200972 bio->bi_rw &= ~REQ_FLUSH;
973 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100974 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200975 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700976 trace_block_bio_complete(md->queue, bio, io_error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200977 bio->bi_error = io_error;
978 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981}
982
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400983static void disable_write_same(struct mapped_device *md)
984{
985 struct queue_limits *limits = dm_get_queue_limits(md);
986
987 /* device doesn't really support WRITE SAME, disable it */
988 limits->max_write_same_sectors = 0;
989}
990
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200991static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200993 int error = bio->bi_error;
zhendong chen5164bec2014-12-17 14:37:04 +0800994 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500995 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000996 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700997 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 dm_endio_fn endio = tio->ti->type->end_io;
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001001 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001002 if (r < 0 || r == DM_ENDIO_REQUEUE)
1003 /*
1004 * error and requeue request are handled
1005 * in dec_pending().
1006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001008 else if (r == DM_ENDIO_INCOMPLETE)
1009 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +02001010 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001011 else if (r) {
1012 DMWARN("unimplemented target endio return value: %d", r);
1013 BUG();
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001017 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
1018 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
1019 disable_write_same(md);
1020
Stefan Bader9faf4002006-10-03 01:15:41 -07001021 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001022 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Mike Snitzer78d8e582015-06-26 10:01:13 -04001025/*
1026 * Partial completion handling for request-based dm
1027 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001028static void end_clone_bio(struct bio *clone)
Mike Snitzer78d8e582015-06-26 10:01:13 -04001029{
1030 struct dm_rq_clone_bio_info *info =
1031 container_of(clone, struct dm_rq_clone_bio_info, clone);
1032 struct dm_rq_target_io *tio = info->tio;
1033 struct bio *bio = info->orig;
1034 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001035 int error = clone->bi_error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001036
1037 bio_put(clone);
1038
1039 if (tio->error)
1040 /*
1041 * An error has already been detected on the request.
1042 * Once error occurred, just let clone->end_io() handle
1043 * the remainder.
1044 */
1045 return;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001046 else if (error) {
Mike Snitzer78d8e582015-06-26 10:01:13 -04001047 /*
1048 * Don't notice the error to the upper layer yet.
1049 * The error handling decision is made by the target driver,
1050 * when the request is completed.
1051 */
Junichi Nomura50887bd2015-10-06 04:19:54 +00001052 tio->error = error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001053 return;
1054 }
1055
1056 /*
1057 * I/O for the bio successfully completed.
1058 * Notice the data completion to the upper layer.
1059 */
1060
1061 /*
1062 * bios are processed from the head of the list.
1063 * So the completing bio should always be rq->bio.
1064 * If it's not, something wrong is happening.
1065 */
1066 if (tio->orig->bio != bio)
1067 DMERR("bio completion is going in the middle of the request");
1068
1069 /*
1070 * Update the original request.
1071 * Do not use blk_end_request() here, because it may complete
1072 * the original request before the clone, and break the ordering.
1073 */
1074 blk_update_request(tio->orig, 0, nr_bytes);
1075}
1076
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001077static struct dm_rq_target_io *tio_from_request(struct request *rq)
1078{
1079 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1080}
1081
Mikulas Patockae262f342015-06-09 17:22:49 -04001082static void rq_end_stats(struct mapped_device *md, struct request *orig)
1083{
1084 if (unlikely(dm_stats_used(&md->stats))) {
1085 struct dm_rq_target_io *tio = tio_from_request(orig);
1086 tio->duration_jiffies = jiffies - tio->duration_jiffies;
1087 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1088 tio->n_sectors, true, tio->duration_jiffies,
1089 &tio->stats_aux);
1090 }
1091}
1092
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001093/*
1094 * Don't touch any member of the md after calling this function because
1095 * the md may be freed in dm_put() at the end of this function.
1096 * Or do dm_get() before calling this function and dm_put() later.
1097 */
Keith Busch466d89a2014-10-17 17:46:37 -06001098static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001099{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001100 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001101
1102 /* nudge anyone waiting on suspend queue */
Mike Snitzer621739b2015-07-08 16:08:24 -04001103 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001104 wake_up(&md->wait);
1105
Jens Axboea8c32a52012-11-06 12:24:26 +01001106 /*
1107 * Run this off this callpath, as drivers could invoke end_io while
1108 * inside their request_fn (and holding the queue lock). Calling
1109 * back into ->request_fn() could deadlock attempting to grab the
1110 * queue lock again.
1111 */
Mike Snitzer9a0e6092015-02-24 11:03:22 -05001112 if (run_queue) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001113 if (md->queue->mq_ops)
1114 blk_mq_run_hw_queues(md->queue, true);
Mike Snitzer621739b2015-07-08 16:08:24 -04001115 else
Mike Snitzer9a0e6092015-02-24 11:03:22 -05001116 blk_run_queue_async(md->queue);
1117 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001118
1119 /*
1120 * dm_put() must be at the end of this function. See the comment above
1121 */
1122 dm_put(md);
1123}
1124
Mike Snitzere5d8de32015-05-28 15:12:52 -04001125static void free_rq_clone(struct request *clone)
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001126{
1127 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001128 struct mapped_device *md = tio->md;
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001129
Mike Snitzer78d8e582015-06-26 10:01:13 -04001130 blk_rq_unprep_clone(clone);
1131
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001132 if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1133 /* stacked on blk-mq queue(s) */
Mike Snitzere5863d92014-12-17 21:08:12 -05001134 tio->ti->type->release_clone_rq(clone);
Mike Snitzer02233342015-03-10 23:49:26 -04001135 else if (!md->queue->mq_ops)
1136 /* request_fn queue stacked on request_fn queue(s) */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001137 free_clone_request(md, clone);
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001138 /*
1139 * NOTE: for the blk-mq queue stacked on request_fn queue(s) case:
1140 * no need to call free_clone_request() because we leverage blk-mq by
1141 * allocating the clone at the end of the blk-mq pdu (see: clone_rq)
1142 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001143
1144 if (!md->queue->mq_ops)
1145 free_rq_tio(tio);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001146}
1147
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001148/*
1149 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001150 * Must be called without clone's queue lock held,
1151 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001152 */
1153static void dm_end_request(struct request *clone, int error)
1154{
1155 int rw = rq_data_dir(clone);
1156 struct dm_rq_target_io *tio = clone->end_io_data;
1157 struct mapped_device *md = tio->md;
1158 struct request *rq = tio->orig;
1159
Tejun Heo29e40132010-09-08 18:07:00 +02001160 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001161 rq->errors = clone->errors;
1162 rq->resid_len = clone->resid_len;
1163
1164 if (rq->sense)
1165 /*
1166 * We are using the sense buffer of the original
1167 * request.
1168 * So setting the length of the sense data is enough.
1169 */
1170 rq->sense_len = clone->sense_len;
1171 }
1172
Mike Snitzere5d8de32015-05-28 15:12:52 -04001173 free_rq_clone(clone);
Mikulas Patockae262f342015-06-09 17:22:49 -04001174 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001175 if (!rq->q->mq_ops)
1176 blk_end_request_all(rq, error);
1177 else
1178 blk_mq_end_request(rq, error);
Tejun Heo29e40132010-09-08 18:07:00 +02001179 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001180}
1181
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001182static void dm_unprep_request(struct request *rq)
1183{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001184 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001185 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001186
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001187 if (!rq->q->mq_ops) {
1188 rq->special = NULL;
1189 rq->cmd_flags &= ~REQ_DONTPREP;
1190 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001191
Mike Snitzere5863d92014-12-17 21:08:12 -05001192 if (clone)
Mike Snitzere5d8de32015-05-28 15:12:52 -04001193 free_rq_clone(clone);
Mike Snitzer4328daa2016-02-21 19:09:22 -05001194 else if (!tio->md->queue->mq_ops)
1195 free_rq_tio(tio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001196}
1197
1198/*
1199 * Requeue the original request of a clone.
1200 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001201static void old_requeue_request(struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001202{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001203 struct request_queue *q = rq->q;
1204 unsigned long flags;
1205
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001206 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001207 blk_requeue_request(q, rq);
Junichi Nomura4ae99442015-05-26 08:25:54 +00001208 blk_run_queue_async(q);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001209 spin_unlock_irqrestore(q->queue_lock, flags);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001210}
1211
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001212static void dm_requeue_original_request(struct mapped_device *md,
1213 struct request *rq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001214{
1215 int rw = rq_data_dir(rq);
1216
1217 dm_unprep_request(rq);
1218
Mikulas Patockae262f342015-06-09 17:22:49 -04001219 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001220 if (!rq->q->mq_ops)
1221 old_requeue_request(rq);
1222 else {
1223 blk_mq_requeue_request(rq);
1224 blk_mq_kick_requeue_list(rq->q);
1225 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001226
Keith Busch466d89a2014-10-17 17:46:37 -06001227 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001228}
Keith Busch466d89a2014-10-17 17:46:37 -06001229
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001230static void old_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001231{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001232 unsigned long flags;
1233
1234 if (blk_queue_stopped(q))
1235 return;
1236
1237 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001238 blk_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001239 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001240}
1241
1242static void stop_queue(struct request_queue *q)
1243{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001244 if (!q->mq_ops)
1245 old_stop_queue(q);
1246 else
1247 blk_mq_stop_hw_queues(q);
1248}
1249
1250static void old_start_queue(struct request_queue *q)
1251{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001252 unsigned long flags;
1253
1254 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001255 if (blk_queue_stopped(q))
1256 blk_start_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001257 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001258}
1259
1260static void start_queue(struct request_queue *q)
1261{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001262 if (!q->mq_ops)
1263 old_start_queue(q);
1264 else
1265 blk_mq_start_stopped_hw_queues(q, true);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001266}
1267
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001268static void dm_done(struct request *clone, int error, bool mapped)
1269{
1270 int r = error;
1271 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001272 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001273
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001274 if (tio->ti) {
1275 rq_end_io = tio->ti->type->rq_end_io;
1276
1277 if (mapped && rq_end_io)
1278 r = rq_end_io(tio->ti, clone, error, &tio->info);
1279 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001280
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001281 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1282 !clone->q->limits.max_write_same_sectors))
1283 disable_write_same(tio->md);
1284
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001285 if (r <= 0)
1286 /* The target wants to complete the I/O */
1287 dm_end_request(clone, r);
1288 else if (r == DM_ENDIO_INCOMPLETE)
1289 /* The target will handle the I/O */
1290 return;
1291 else if (r == DM_ENDIO_REQUEUE)
1292 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001293 dm_requeue_original_request(tio->md, tio->orig);
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001294 else {
1295 DMWARN("unimplemented target endio return value: %d", r);
1296 BUG();
1297 }
1298}
1299
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001300/*
1301 * Request completion handler for request-based dm
1302 */
1303static void dm_softirq_done(struct request *rq)
1304{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001305 bool mapped = true;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001306 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001307 struct request *clone = tio->clone;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001308 int rw;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001309
Mike Snitzere5863d92014-12-17 21:08:12 -05001310 if (!clone) {
Mikulas Patockae262f342015-06-09 17:22:49 -04001311 rq_end_stats(tio->md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001312 rw = rq_data_dir(rq);
1313 if (!rq->q->mq_ops) {
1314 blk_end_request_all(rq, tio->error);
1315 rq_completed(tio->md, rw, false);
1316 free_rq_tio(tio);
1317 } else {
1318 blk_mq_end_request(rq, tio->error);
1319 rq_completed(tio->md, rw, false);
1320 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001321 return;
1322 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001323
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001324 if (rq->cmd_flags & REQ_FAILED)
1325 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001326
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001327 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001328}
1329
1330/*
1331 * Complete the clone and the original request with the error status
1332 * through softirq context.
1333 */
Keith Busch466d89a2014-10-17 17:46:37 -06001334static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001335{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001336 struct dm_rq_target_io *tio = tio_from_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001337
1338 tio->error = error;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001339 blk_complete_request(rq);
1340}
1341
1342/*
1343 * Complete the not-mapped clone and the original request with the error status
1344 * through softirq context.
1345 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001346 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001347 */
Keith Busch466d89a2014-10-17 17:46:37 -06001348static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001349{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001350 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001351 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001352}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001353
1354/*
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001355 * Called with the clone's queue lock held (for non-blk-mq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001356 */
1357static void end_clone_request(struct request *clone, int error)
1358{
Keith Busch466d89a2014-10-17 17:46:37 -06001359 struct dm_rq_target_io *tio = clone->end_io_data;
1360
Mike Snitzere5863d92014-12-17 21:08:12 -05001361 if (!clone->q->mq_ops) {
1362 /*
1363 * For just cleaning up the information of the queue in which
1364 * the clone was dispatched.
1365 * The clone is *NOT* freed actually here because it is alloced
1366 * from dm own mempool (REQ_ALLOCED isn't set).
1367 */
1368 __blk_put_request(clone->q, clone);
1369 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001370
1371 /*
1372 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001373 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001374 * - another request may be submitted by the upper level driver
1375 * of the stacking during the completion
1376 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001377 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001378 */
Keith Busch466d89a2014-10-17 17:46:37 -06001379 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001380}
1381
Mike Snitzer56a67df2010-08-12 04:14:10 +01001382/*
1383 * Return maximum size of I/O possible at the supplied sector up to the current
1384 * target boundary.
1385 */
1386static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001388 sector_t target_offset = dm_target_offset(ti, sector);
1389
1390 return ti->len - target_offset;
1391}
1392
1393static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1394{
1395 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001396 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001399 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001401 if (ti->max_io_len) {
1402 offset = dm_target_offset(ti, sector);
1403 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1404 max_len = sector_div(offset, ti->max_io_len);
1405 else
1406 max_len = offset & (ti->max_io_len - 1);
1407 max_len = ti->max_io_len - max_len;
1408
1409 if (len > max_len)
1410 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
1413 return len;
1414}
1415
Mike Snitzer542f9032012-07-27 15:08:00 +01001416int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1417{
1418 if (len > UINT_MAX) {
1419 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1420 (unsigned long long)len, UINT_MAX);
1421 ti->error = "Maximum size of target IO is too large";
1422 return -EINVAL;
1423 }
1424
1425 ti->max_io_len = (uint32_t) len;
1426
1427 return 0;
1428}
1429EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1430
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001431/*
1432 * A target may call dm_accept_partial_bio only from the map routine. It is
1433 * allowed for all bio types except REQ_FLUSH.
1434 *
1435 * dm_accept_partial_bio informs the dm that the target only wants to process
1436 * additional n_sectors sectors of the bio and the rest of the data should be
1437 * sent in a next bio.
1438 *
1439 * A diagram that explains the arithmetics:
1440 * +--------------------+---------------+-------+
1441 * | 1 | 2 | 3 |
1442 * +--------------------+---------------+-------+
1443 *
1444 * <-------------- *tio->len_ptr --------------->
1445 * <------- bi_size ------->
1446 * <-- n_sectors -->
1447 *
1448 * Region 1 was already iterated over with bio_advance or similar function.
1449 * (it may be empty if the target doesn't use bio_advance)
1450 * Region 2 is the remaining bio size that the target wants to process.
1451 * (it may be empty if region 1 is non-empty, although there is no reason
1452 * to make it empty)
1453 * The target requires that region 3 is to be sent in the next bio.
1454 *
1455 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1456 * the partially processed part (the sum of regions 1+2) must be the same for all
1457 * copies of the bio.
1458 */
1459void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1460{
1461 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1462 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1463 BUG_ON(bio->bi_rw & REQ_FLUSH);
1464 BUG_ON(bi_size > *tio->len_ptr);
1465 BUG_ON(n_sectors > bi_size);
1466 *tio->len_ptr -= bi_size - n_sectors;
1467 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1468}
1469EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1470
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001471static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001474 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001475 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001476 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001477 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 /*
1482 * Map the clone. If r == 0 we don't need to do
1483 * anything, the target has assumed ownership of
1484 * this io.
1485 */
1486 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001487 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001488 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001489 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001491
Mike Snitzerd07335e2010-11-16 12:52:38 +01001492 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1493 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001496 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1497 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001498 md = tio->io->md;
1499 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001500 free_tio(md, tio);
Mikulas Patockaab378442015-07-01 17:30:36 -04001501 } else if (r != DM_MAPIO_SUBMITTED) {
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001502 DMWARN("unimplemented target map return value: %d", r);
1503 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505}
1506
1507struct clone_info {
1508 struct mapped_device *md;
1509 struct dm_table *map;
1510 struct bio *bio;
1511 struct dm_io *io;
1512 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001513 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514};
1515
Mikulas Patockae0d66092014-03-14 18:40:39 -04001516static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001517{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001518 bio->bi_iter.bi_sector = sector;
1519 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522/*
1523 * Creates a bio that consists of range of complete bvecs.
1524 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001525static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001526 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Mikulas Patockadba14162012-10-12 21:02:15 +01001528 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001530 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001532 if (bio_integrity(bio))
1533 bio_integrity_clone(clone, bio, GFP_NOIO);
1534
1535 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1536 clone->bi_iter.bi_size = to_bytes(len);
1537
1538 if (bio_integrity(bio))
1539 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001542static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001543 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001544 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001545{
Mikulas Patockadba14162012-10-12 21:02:15 +01001546 struct dm_target_io *tio;
1547 struct bio *clone;
1548
Junichi Nomura99778272014-10-03 11:55:16 +00001549 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001550 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001551
1552 tio->io = ci->io;
1553 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001554 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001555
1556 return tio;
1557}
1558
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001559static void __clone_and_map_simple_bio(struct clone_info *ci,
1560 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001561 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001562{
Junichi Nomura99778272014-10-03 11:55:16 +00001563 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001564 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001565
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001566 tio->len_ptr = len;
1567
Junichi Nomura99778272014-10-03 11:55:16 +00001568 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001569 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001570 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001571
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001572 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001573}
1574
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001575static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001576 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001577{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001578 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001579
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001580 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001581 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001582}
1583
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001584static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001585{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001586 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001587 struct dm_target *ti;
1588
Mike Snitzerb372d362010-09-08 18:07:01 +02001589 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001590 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001591 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001592
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001593 return 0;
1594}
1595
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001596static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001597 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001598{
Mikulas Patockadba14162012-10-12 21:02:15 +01001599 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001600 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001601 unsigned target_bio_nr;
1602 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001603
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001604 /*
1605 * Does the target want to receive duplicate copies of the bio?
1606 */
1607 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1608 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001609
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001610 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001611 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001612 tio->len_ptr = len;
1613 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001614 __map_bio(tio);
1615 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001616}
1617
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001618typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001619
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001620static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001621{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001622 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001623}
1624
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001625static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001626{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001627 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001628}
1629
1630typedef bool (*is_split_required_fn)(struct dm_target *ti);
1631
1632static bool is_split_required_for_discard(struct dm_target *ti)
1633{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001634 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001635}
1636
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001637static int __send_changing_extent_only(struct clone_info *ci,
1638 get_num_bios_fn get_num_bios,
1639 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001640{
1641 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001642 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001643 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001644
Mike Snitzera79245b2010-08-12 04:14:24 +01001645 do {
1646 ti = dm_table_find_target(ci->map, ci->sector);
1647 if (!dm_target_is_valid(ti))
1648 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001649
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001650 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001651 * Even though the device advertised support for this type of
1652 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001653 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001654 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001655 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001656 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1657 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001658 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001659
Mike Snitzer23508a92012-12-21 20:23:37 +00001660 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001661 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001662 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001663 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001664
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001665 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001666
1667 ci->sector += len;
1668 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001669
1670 return 0;
1671}
1672
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001673static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001674{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001675 return __send_changing_extent_only(ci, get_num_discard_bios,
1676 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001677}
1678
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001679static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001680{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001681 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001682}
1683
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001684/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001685 * Select the correct strategy for processing a non-flush bio.
1686 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001687static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Mikulas Patockadba14162012-10-12 21:02:15 +01001689 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001690 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001691 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001693 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001694 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001695 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001696 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001697
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001698 ti = dm_table_find_target(ci->map, ci->sector);
1699 if (!dm_target_is_valid(ti))
1700 return -EIO;
1701
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001702 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001703
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001704 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001706 ci->sector += len;
1707 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001713 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001715static void __split_and_process_bio(struct mapped_device *md,
1716 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001719 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001721 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001722 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001723 return;
1724 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001725
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001726 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 ci.io = alloc_io(md);
1729 ci.io->error = 0;
1730 atomic_set(&ci.io->io_count, 1);
1731 ci.io->bio = bio;
1732 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001733 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001734 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001736 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001737
Mike Snitzerb372d362010-09-08 18:07:01 +02001738 if (bio->bi_rw & REQ_FLUSH) {
1739 ci.bio = &ci.md->flush_bio;
1740 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001741 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001742 /* dec_pending submits any data associated with flush */
1743 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001744 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001745 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001746 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001747 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001751 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753/*-----------------------------------------------------------------
1754 * CRUD END
1755 *---------------------------------------------------------------*/
1756
1757/*
1758 * The request function that just remaps the bio built up by
1759 * dm_merge_bvec.
1760 */
Jens Axboedece1632015-11-05 10:41:16 -07001761static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Kevin Corry12f03a42006-02-01 03:04:52 -08001763 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001765 int srcu_idx;
1766 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001768 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Gu Zheng18c0b222014-11-24 11:05:26 +08001770 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001771
Tejun Heo6a8736d2010-09-08 18:07:00 +02001772 /* if we're suspended, we have to queue this io for later */
1773 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001774 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Tejun Heo6a8736d2010-09-08 18:07:00 +02001776 if (bio_rw(bio) != READA)
1777 queue_io(md, bio);
1778 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001779 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001780 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001783 __split_and_process_bio(md, map, bio);
1784 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001785 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001786}
1787
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001788int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001789{
1790 return blk_queue_stackable(md->queue);
1791}
1792
Keith Busch466d89a2014-10-17 17:46:37 -06001793static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001794{
1795 int r;
1796
Keith Busch466d89a2014-10-17 17:46:37 -06001797 if (blk_queue_io_stat(clone->q))
1798 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001799
Keith Busch466d89a2014-10-17 17:46:37 -06001800 clone->start_time = jiffies;
1801 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001802 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001803 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001804 dm_complete_request(rq, r);
1805}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001806
Mike Snitzer78d8e582015-06-26 10:01:13 -04001807static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1808 void *data)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001809{
Mike Snitzer78d8e582015-06-26 10:01:13 -04001810 struct dm_rq_target_io *tio = data;
1811 struct dm_rq_clone_bio_info *info =
1812 container_of(bio, struct dm_rq_clone_bio_info, clone);
1813
1814 info->orig = bio_orig;
1815 info->tio = tio;
1816 bio->bi_end_io = end_clone_bio;
1817
1818 return 0;
1819}
1820
1821static int setup_clone(struct request *clone, struct request *rq,
1822 struct dm_rq_target_io *tio, gfp_t gfp_mask)
1823{
1824 int r;
1825
1826 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1827 dm_rq_bio_constructor, tio);
1828 if (r)
1829 return r;
1830
1831 clone->cmd = rq->cmd;
1832 clone->cmd_len = rq->cmd_len;
1833 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001834 clone->end_io = end_clone_request;
1835 clone->end_io_data = tio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001836
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001837 tio->clone = clone;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001838
1839 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001840}
1841
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001842static struct request *clone_rq(struct request *rq, struct mapped_device *md,
Keith Busch466d89a2014-10-17 17:46:37 -06001843 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001844{
Mike Snitzer02233342015-03-10 23:49:26 -04001845 /*
1846 * Do not allocate a clone if tio->clone was already set
1847 * (see: dm_mq_queue_rq).
1848 */
1849 bool alloc_clone = !tio->clone;
1850 struct request *clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001851
Mike Snitzer02233342015-03-10 23:49:26 -04001852 if (alloc_clone) {
1853 clone = alloc_clone_request(md, gfp_mask);
1854 if (!clone)
1855 return NULL;
1856 } else
1857 clone = tio->clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001858
1859 blk_rq_init(NULL, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001860 if (setup_clone(clone, rq, tio, gfp_mask)) {
1861 /* -ENOMEM */
1862 if (alloc_clone)
1863 free_clone_request(md, clone);
1864 return NULL;
1865 }
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001866
1867 return clone;
1868}
1869
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001870static void map_tio_request(struct kthread_work *work);
1871
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001872static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1873 struct mapped_device *md)
1874{
1875 tio->md = md;
1876 tio->ti = NULL;
1877 tio->clone = NULL;
1878 tio->orig = rq;
1879 tio->error = 0;
1880 memset(&tio->info, 0, sizeof(tio->info));
Mike Snitzer02233342015-03-10 23:49:26 -04001881 if (md->kworker_task)
1882 init_kthread_work(&tio->work, map_tio_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001883}
1884
Keith Busch466d89a2014-10-17 17:46:37 -06001885static struct dm_rq_target_io *prep_tio(struct request *rq,
1886 struct mapped_device *md, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001887{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001888 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001889 int srcu_idx;
1890 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001891
1892 tio = alloc_rq_tio(md, gfp_mask);
1893 if (!tio)
1894 return NULL;
1895
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001896 init_tio(tio, rq, md);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001897
Mike Snitzere5863d92014-12-17 21:08:12 -05001898 table = dm_get_live_table(md, &srcu_idx);
1899 if (!dm_table_mq_request_based(table)) {
1900 if (!clone_rq(rq, md, tio, gfp_mask)) {
1901 dm_put_live_table(md, srcu_idx);
1902 free_rq_tio(tio);
1903 return NULL;
1904 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001905 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001906 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001907
Keith Busch466d89a2014-10-17 17:46:37 -06001908 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001909}
1910
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001911/*
1912 * Called with the queue lock held.
1913 */
1914static int dm_prep_fn(struct request_queue *q, struct request *rq)
1915{
1916 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001917 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001918
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001919 if (unlikely(rq->special)) {
1920 DMWARN("Already has something in rq->special.");
1921 return BLKPREP_KILL;
1922 }
1923
Keith Busch466d89a2014-10-17 17:46:37 -06001924 tio = prep_tio(rq, md, GFP_ATOMIC);
1925 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001926 return BLKPREP_DEFER;
1927
Keith Busch466d89a2014-10-17 17:46:37 -06001928 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001929 rq->cmd_flags |= REQ_DONTPREP;
1930
1931 return BLKPREP_OK;
1932}
1933
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001934/*
1935 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05001936 * 0 : the request has been processed
1937 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1938 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001939 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001940static int map_request(struct dm_rq_target_io *tio, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001941 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001942{
Mike Snitzere5863d92014-12-17 21:08:12 -05001943 int r;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001944 struct dm_target *ti = tio->ti;
Mike Snitzere5863d92014-12-17 21:08:12 -05001945 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001946
Mike Snitzere5863d92014-12-17 21:08:12 -05001947 if (tio->clone) {
1948 clone = tio->clone;
1949 r = ti->type->map_rq(ti, clone, &tio->info);
1950 } else {
1951 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1952 if (r < 0) {
1953 /* The target wants to complete the I/O */
1954 dm_kill_unmapped_request(rq, r);
1955 return r;
1956 }
Junichi Nomura3a140752015-05-27 04:22:07 +00001957 if (r != DM_MAPIO_REMAPPED)
1958 return r;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001959 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
1960 /* -ENOMEM */
1961 ti->type->release_clone_rq(clone);
1962 return DM_MAPIO_REQUEUE;
1963 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001964 }
1965
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001966 switch (r) {
1967 case DM_MAPIO_SUBMITTED:
1968 /* The target has taken the I/O to submit by itself later */
1969 break;
1970 case DM_MAPIO_REMAPPED:
1971 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001972 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06001973 blk_rq_pos(rq));
1974 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001975 break;
1976 case DM_MAPIO_REQUEUE:
1977 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001978 dm_requeue_original_request(md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001979 break;
1980 default:
1981 if (r > 0) {
1982 DMWARN("unimplemented target map return value: %d", r);
1983 BUG();
1984 }
1985
1986 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06001987 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05001988 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001989 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001990
Mike Snitzere5863d92014-12-17 21:08:12 -05001991 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001992}
1993
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001994static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001995{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001996 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05001997 struct request *rq = tio->orig;
1998 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001999
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002000 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
Mike Snitzer2d76fff2015-04-29 12:07:12 -04002001 dm_requeue_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002002}
2003
Keith Busch466d89a2014-10-17 17:46:37 -06002004static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002005{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002006 if (!orig->q->mq_ops)
2007 blk_start_request(orig);
2008 else
2009 blk_mq_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06002010 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002011
Mike Snitzer0ce65792015-02-26 00:50:28 -05002012 if (md->seq_rq_merge_deadline_usecs) {
2013 md->last_rq_pos = rq_end_sector(orig);
2014 md->last_rq_rw = rq_data_dir(orig);
2015 md->last_rq_start_time = ktime_get();
2016 }
Mike Snitzerde3ec862015-02-24 21:58:21 -05002017
Mikulas Patockae262f342015-06-09 17:22:49 -04002018 if (unlikely(dm_stats_used(&md->stats))) {
2019 struct dm_rq_target_io *tio = tio_from_request(orig);
2020 tio->duration_jiffies = jiffies;
2021 tio->n_sectors = blk_rq_sectors(orig);
2022 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
2023 tio->n_sectors, false, 0, &tio->stats_aux);
2024 }
2025
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002026 /*
2027 * Hold the md reference here for the in-flight I/O.
2028 * We can't rely on the reference count by device opener,
2029 * because the device may be closed during the request completion
2030 * when all bios are completed.
2031 * See the comment in rq_completed() too.
2032 */
2033 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002034}
2035
Mike Snitzer0ce65792015-02-26 00:50:28 -05002036#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
2037
2038ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
2039{
2040 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
2041}
2042
2043ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
2044 const char *buf, size_t count)
2045{
2046 unsigned deadline;
2047
Mike Snitzer17e149b2015-03-11 15:01:09 -04002048 if (!dm_request_based(md) || md->use_blk_mq)
Mike Snitzer0ce65792015-02-26 00:50:28 -05002049 return count;
2050
2051 if (kstrtouint(buf, 10, &deadline))
2052 return -EINVAL;
2053
2054 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
2055 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
2056
2057 md->seq_rq_merge_deadline_usecs = deadline;
2058
2059 return count;
2060}
2061
2062static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
2063{
2064 ktime_t kt_deadline;
2065
2066 if (!md->seq_rq_merge_deadline_usecs)
2067 return false;
2068
2069 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
2070 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
2071
2072 return !ktime_after(ktime_get(), kt_deadline);
2073}
2074
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002075/*
2076 * q->request_fn for request-based dm.
2077 * Called with the queue lock held.
2078 */
2079static void dm_request_fn(struct request_queue *q)
2080{
2081 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002082 int srcu_idx;
2083 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002084 struct dm_target *ti;
Keith Busch466d89a2014-10-17 17:46:37 -06002085 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002086 struct dm_rq_target_io *tio;
Tejun Heo29e40132010-09-08 18:07:00 +02002087 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002088
2089 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002090 * For suspend, check blk_queue_stopped() and increment
2091 * ->pending within a single queue_lock not to increment the
2092 * number of in-flight I/Os after the queue is stopped in
2093 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002094 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002095 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002096 rq = blk_peek_request(q);
2097 if (!rq)
Mike Snitzer9d1deb82015-02-24 20:49:18 -05002098 goto out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002099
Tejun Heo29e40132010-09-08 18:07:00 +02002100 /* always use block 0 to find the target for flushes for now */
2101 pos = 0;
2102 if (!(rq->cmd_flags & REQ_FLUSH))
2103 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002104
Tejun Heo29e40132010-09-08 18:07:00 +02002105 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002106 if (!dm_target_is_valid(ti)) {
2107 /*
Keith Busch466d89a2014-10-17 17:46:37 -06002108 * Must perform setup, that rq_completed() requires,
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002109 * before calling dm_kill_unmapped_request
2110 */
2111 DMERR_LIMIT("request attempted access beyond the end of device");
Keith Busch466d89a2014-10-17 17:46:37 -06002112 dm_start_request(md, rq);
2113 dm_kill_unmapped_request(rq, -EIO);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002114 continue;
2115 }
Tejun Heo29e40132010-09-08 18:07:00 +02002116
Mike Snitzer0ce65792015-02-26 00:50:28 -05002117 if (dm_request_peeked_before_merge_deadline(md) &&
2118 md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
Mike Snitzerde3ec862015-02-24 21:58:21 -05002119 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq))
2120 goto delay_and_out;
2121
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002122 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01002123 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002124
Keith Busch466d89a2014-10-17 17:46:37 -06002125 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002126
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002127 tio = tio_from_request(rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002128 /* Establish tio->ti before queuing work (map_tio_request) */
2129 tio->ti = ti;
2130 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00002131 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002132 }
2133
2134 goto out;
2135
Jens Axboe7eaceac2011-03-10 08:52:07 +01002136delay_and_out:
Mike Snitzerd548b342015-03-05 22:21:10 -05002137 blk_delay_queue(q, HZ / 100);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002138out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002139 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002140}
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142static int dm_any_congested(void *congested_data, int bdi_bits)
2143{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002144 int r = bdi_bits;
2145 struct mapped_device *md = congested_data;
2146 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002148 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002149 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002150 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002151 /*
2152 * Request-based dm cares about only own queue for
2153 * the query about congestion status of request_queue
2154 */
2155 if (dm_request_based(md))
Tejun Heo44522262015-05-22 17:13:26 -04002156 r = md->queue->backing_dev_info.wb.state &
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002157 bdi_bits;
2158 else
2159 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002160 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002161 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002162 }
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return r;
2165}
2166
2167/*-----------------------------------------------------------------
2168 * An IDR is used to keep track of allocated minor numbers.
2169 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002170static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002172 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002174 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177/*
2178 * See if the device with a specific minor # is free.
2179 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002180static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002182 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 if (minor >= (1 << MINORBITS))
2185 return -EINVAL;
2186
Tejun Heoc9d76be2013-02-27 17:04:26 -08002187 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002188 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Tejun Heoc9d76be2013-02-27 17:04:26 -08002190 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002192 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002193 idr_preload_end();
2194 if (r < 0)
2195 return r == -ENOSPC ? -EBUSY : r;
2196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002199static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002201 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Tejun Heoc9d76be2013-02-27 17:04:26 -08002203 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002204 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Tejun Heoc9d76be2013-02-27 17:04:26 -08002206 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002208 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002209 idr_preload_end();
2210 if (r < 0)
2211 return r;
2212 *minor = r;
2213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002216static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Mikulas Patocka53d59142009-04-02 19:55:37 +01002218static void dm_wq_work(struct work_struct *work);
2219
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002220static void dm_init_md_queue(struct mapped_device *md)
2221{
2222 /*
2223 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002224 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002225 * The type is decided at the first table loading time.
2226 * To prevent problematic device stacking, clear the queue flag
2227 * for request stacking support until then.
2228 *
2229 * This queue is new, so no concurrency on the queue_flags.
2230 */
2231 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04002232
2233 /*
2234 * Initialize data that will only be used by a non-blk-mq DM queue
2235 * - must do so here (in alloc_dev callchain) before queue is used
2236 */
2237 md->queue->queuedata = md;
2238 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002239}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002240
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002241static void dm_init_old_md_queue(struct mapped_device *md)
2242{
Mike Snitzer17e149b2015-03-11 15:01:09 -04002243 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002244 dm_init_md_queue(md);
2245
2246 /*
2247 * Initialize aspects of queue that aren't relevant for blk-mq
2248 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002249 md->queue->backing_dev_info.congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002250 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002251}
2252
Mike Snitzer0f209722015-04-28 11:50:29 -04002253static void cleanup_mapped_device(struct mapped_device *md)
2254{
Mike Snitzer0f209722015-04-28 11:50:29 -04002255 if (md->wq)
2256 destroy_workqueue(md->wq);
2257 if (md->kworker_task)
2258 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02002259 mempool_destroy(md->io_pool);
2260 mempool_destroy(md->rq_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04002261 if (md->bs)
2262 bioset_free(md->bs);
2263
Mikulas Patockab06075a2015-07-10 17:21:43 -04002264 cleanup_srcu_struct(&md->io_barrier);
2265
Mike Snitzer0f209722015-04-28 11:50:29 -04002266 if (md->disk) {
2267 spin_lock(&_minor_lock);
2268 md->disk->private_data = NULL;
2269 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04002270 del_gendisk(md->disk);
2271 put_disk(md->disk);
2272 }
2273
2274 if (md->queue)
2275 blk_cleanup_queue(md->queue);
2276
2277 if (md->bdev) {
2278 bdput(md->bdev);
2279 md->bdev = NULL;
2280 }
2281}
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283/*
2284 * Allocate and initialise a blank device with a given minor.
2285 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002286static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002289 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002290 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 if (!md) {
2293 DMWARN("unable to allocate device, out of memory.");
2294 return NULL;
2295 }
2296
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002297 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002298 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002301 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002302 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002303 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002304 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002306 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002308 r = init_srcu_struct(&md->io_barrier);
2309 if (r < 0)
2310 goto bad_io_barrier;
2311
Mike Snitzer17e149b2015-03-11 15:01:09 -04002312 md->use_blk_mq = use_blk_mq;
Mike Snitzera5664da2010-08-12 04:14:01 +01002313 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002314 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002315 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002316 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002317 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002319 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002321 atomic_set(&md->uevent_seq, 0);
2322 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002323 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002324 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002326 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04002328 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002330 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 md->disk = alloc_disk(1);
2333 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04002334 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002336 atomic_set(&md->pending[0], 0);
2337 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002338 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002339 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002340 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002341 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002342 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 md->disk->major = _major;
2345 md->disk->first_minor = minor;
2346 md->disk->fops = &dm_blk_dops;
2347 md->disk->queue = md->queue;
2348 md->disk->private_data = md;
2349 sprintf(md->disk->disk_name, "dm-%d", minor);
2350 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002351 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Tejun Heo670368a2013-07-30 08:40:21 -04002353 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002354 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002355 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002356
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002357 md->bdev = bdget_disk(md->disk, 0);
2358 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002359 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002360
Tejun Heo6a8736d2010-09-08 18:07:00 +02002361 bio_init(&md->flush_bio);
2362 md->flush_bio.bi_bdev = md->bdev;
2363 md->flush_bio.bi_rw = WRITE_FLUSH;
2364
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002365 dm_stats_init(&md->stats);
2366
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002367 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002368 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002369 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002370 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002371
2372 BUG_ON(old_md != MINOR_ALLOCED);
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return md;
2375
Mike Snitzer0f209722015-04-28 11:50:29 -04002376bad:
2377 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002378bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002380bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002381 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002382bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 kfree(md);
2384 return NULL;
2385}
2386
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002387static void unlock_fs(struct mapped_device *md);
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389static void free_dev(struct mapped_device *md)
2390{
Tejun Heof331c022008-09-03 09:01:48 +02002391 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002392
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002393 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002394
Mike Snitzer0f209722015-04-28 11:50:29 -04002395 cleanup_mapped_device(md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04002396 if (md->use_blk_mq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002397 blk_mq_free_tag_set(&md->tag_set);
Mike Snitzer0f209722015-04-28 11:50:29 -04002398
2399 free_table_devices(&md->table_devices);
2400 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002401 free_minor(minor);
2402
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002403 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 kfree(md);
2405}
2406
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002407static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2408{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002409 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002410
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002411 if (md->bs) {
2412 /* The md already has necessary mempools. */
2413 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002414 /*
2415 * Reload bioset because front_pad may have changed
2416 * because a different table was loaded.
2417 */
2418 bioset_free(md->bs);
2419 md->bs = p->bs;
2420 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002421 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002422 /*
2423 * There's no need to reload with request-based dm
2424 * because the size of front_pad doesn't change.
2425 * Note for future: If you are to reload bioset,
2426 * prep-ed requests in the queue may refer
2427 * to bio from the old bioset, so you must walk
2428 * through the queue to unprep.
2429 */
2430 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002431 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002432
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002433 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2434
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002435 md->io_pool = p->io_pool;
2436 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002437 md->rq_pool = p->rq_pool;
2438 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002439 md->bs = p->bs;
2440 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002441
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002442out:
Mike Snitzer02233342015-03-10 23:49:26 -04002443 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002444 dm_table_free_md_mempools(t);
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447/*
2448 * Bind a table to the device.
2449 */
2450static void event_callback(void *context)
2451{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002452 unsigned long flags;
2453 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct mapped_device *md = (struct mapped_device *) context;
2455
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002456 spin_lock_irqsave(&md->uevent_lock, flags);
2457 list_splice_init(&md->uevent_list, &uevents);
2458 spin_unlock_irqrestore(&md->uevent_lock, flags);
2459
Tejun Heoed9e1982008-08-25 19:56:05 +09002460 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 atomic_inc(&md->event_nr);
2463 wake_up(&md->eventq);
2464}
2465
Mike Snitzerc2176492011-01-13 19:53:46 +00002466/*
2467 * Protected by md->suspend_lock obtained by dm_swap_table().
2468 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002469static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002471 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002473 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002476/*
2477 * Returns old map, which caller must destroy.
2478 */
2479static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2480 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002482 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002483 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 sector_t size;
2485
2486 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002487
2488 /*
2489 * Wipe any geometry if the size of the table changed.
2490 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002491 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002492 memset(&md->geometry, 0, sizeof(md->geometry));
2493
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002494 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002496 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002497
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002498 /*
2499 * The queue hasn't been stopped yet, if the old table type wasn't
2500 * for request-based during suspension. So stop it to prevent
2501 * I/O mapping before resume.
2502 * This must be done before setting the queue restrictions,
2503 * because request-based dm may be run just after the setting.
2504 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002505 if (dm_table_request_based(t))
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002506 stop_queue(q);
2507
2508 __bind_mempools(md, t);
2509
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002510 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002511 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002512 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2513
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002514 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002515 if (old_map)
2516 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002517
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002518 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
Alasdair G Kergona7940152009-12-10 23:52:23 +00002521/*
2522 * Returns unbound table for the caller to free.
2523 */
2524static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002526 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002529 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302532 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002533 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002534
2535 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538/*
2539 * Constructor for a new device.
2540 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002541int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 struct mapped_device *md;
2544
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002545 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (!md)
2547 return -ENXIO;
2548
Milan Broz784aae72009-01-06 03:05:12 +00002549 dm_sysfs_init(md);
2550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 *result = md;
2552 return 0;
2553}
2554
Mike Snitzera5664da2010-08-12 04:14:01 +01002555/*
2556 * Functions to manage md->type.
2557 * All are required to hold md->type_lock.
2558 */
2559void dm_lock_md_type(struct mapped_device *md)
2560{
2561 mutex_lock(&md->type_lock);
2562}
2563
2564void dm_unlock_md_type(struct mapped_device *md)
2565{
2566 mutex_unlock(&md->type_lock);
2567}
2568
2569void dm_set_md_type(struct mapped_device *md, unsigned type)
2570{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002571 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002572 md->type = type;
2573}
2574
2575unsigned dm_get_md_type(struct mapped_device *md)
2576{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002577 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002578 return md->type;
2579}
2580
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002581struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2582{
2583 return md->immutable_target_type;
2584}
2585
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002586/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002587 * The queue_limits are only valid as long as you have a reference
2588 * count on 'md'.
2589 */
2590struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2591{
2592 BUG_ON(!atomic_read(&md->holders));
2593 return &md->queue->limits;
2594}
2595EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2596
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002597static void init_rq_based_worker_thread(struct mapped_device *md)
2598{
2599 /* Initialize the request-based DM worker thread */
2600 init_kthread_worker(&md->kworker);
2601 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2602 "kdmwork-%s", dm_device_name(md));
2603}
2604
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002605/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002606 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2607 */
2608static int dm_init_request_based_queue(struct mapped_device *md)
2609{
2610 struct request_queue *q = NULL;
2611
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002612 /* Fully initialize the queue */
2613 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2614 if (!q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002615 return -EINVAL;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002616
Mike Snitzer0ce65792015-02-26 00:50:28 -05002617 /* disable dm_request_fn's merge heuristic by default */
2618 md->seq_rq_merge_deadline_usecs = 0;
2619
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002620 md->queue = q;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002621 dm_init_old_md_queue(md);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002622 blk_queue_softirq_done(md->queue, dm_softirq_done);
2623 blk_queue_prep_rq(md->queue, dm_prep_fn);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002624
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002625 init_rq_based_worker_thread(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002626
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002627 elv_register_queue(md->queue);
2628
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002629 return 0;
2630}
2631
2632static int dm_mq_init_request(void *data, struct request *rq,
2633 unsigned int hctx_idx, unsigned int request_idx,
2634 unsigned int numa_node)
2635{
2636 struct mapped_device *md = data;
2637 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2638
2639 /*
2640 * Must initialize md member of tio, otherwise it won't
2641 * be available in dm_mq_queue_rq.
2642 */
2643 tio->md = md;
2644
2645 return 0;
2646}
2647
2648static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2649 const struct blk_mq_queue_data *bd)
2650{
2651 struct request *rq = bd->rq;
2652 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2653 struct mapped_device *md = tio->md;
2654 int srcu_idx;
2655 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2656 struct dm_target *ti;
2657 sector_t pos;
2658
2659 /* always use block 0 to find the target for flushes for now */
2660 pos = 0;
2661 if (!(rq->cmd_flags & REQ_FLUSH))
2662 pos = blk_rq_pos(rq);
2663
2664 ti = dm_table_find_target(map, pos);
2665 if (!dm_target_is_valid(ti)) {
2666 dm_put_live_table(md, srcu_idx);
2667 DMERR_LIMIT("request attempted access beyond the end of device");
2668 /*
2669 * Must perform setup, that rq_completed() requires,
2670 * before returning BLK_MQ_RQ_QUEUE_ERROR
2671 */
2672 dm_start_request(md, rq);
2673 return BLK_MQ_RQ_QUEUE_ERROR;
2674 }
2675 dm_put_live_table(md, srcu_idx);
2676
2677 if (ti->type->busy && ti->type->busy(ti))
2678 return BLK_MQ_RQ_QUEUE_BUSY;
2679
2680 dm_start_request(md, rq);
2681
2682 /* Init tio using md established in .init_request */
2683 init_tio(tio, rq, md);
2684
Mike Snitzer02233342015-03-10 23:49:26 -04002685 /*
2686 * Establish tio->ti before queuing work (map_tio_request)
2687 * or making direct call to map_request().
2688 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002689 tio->ti = ti;
Mike Snitzer02233342015-03-10 23:49:26 -04002690
2691 /* Clone the request if underlying devices aren't blk-mq */
2692 if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) {
2693 /* clone request is allocated at the end of the pdu */
2694 tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io);
Mike Snitzer45714fb2015-05-27 15:25:27 -04002695 (void) clone_rq(rq, md, tio, GFP_ATOMIC);
Mike Snitzer02233342015-03-10 23:49:26 -04002696 queue_kthread_work(&md->kworker, &tio->work);
2697 } else {
2698 /* Direct call is fine since .queue_rq allows allocations */
Mike Snitzer45714fb2015-05-27 15:25:27 -04002699 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
2700 /* Undo dm_start_request() before requeuing */
Mikulas Patockae262f342015-06-09 17:22:49 -04002701 rq_end_stats(md, rq);
Mike Snitzer45714fb2015-05-27 15:25:27 -04002702 rq_completed(md, rq_data_dir(rq), false);
2703 return BLK_MQ_RQ_QUEUE_BUSY;
2704 }
Mike Snitzer02233342015-03-10 23:49:26 -04002705 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002706
2707 return BLK_MQ_RQ_QUEUE_OK;
2708}
2709
2710static struct blk_mq_ops dm_mq_ops = {
2711 .queue_rq = dm_mq_queue_rq,
2712 .map_queue = blk_mq_map_queue,
2713 .complete = dm_softirq_done,
2714 .init_request = dm_mq_init_request,
2715};
2716
2717static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
2718{
Mike Snitzer02233342015-03-10 23:49:26 -04002719 unsigned md_type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002720 struct request_queue *q;
2721 int err;
2722
2723 memset(&md->tag_set, 0, sizeof(md->tag_set));
2724 md->tag_set.ops = &dm_mq_ops;
2725 md->tag_set.queue_depth = BLKDEV_MAX_RQ;
2726 md->tag_set.numa_node = NUMA_NO_NODE;
2727 md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2728 md->tag_set.nr_hw_queues = 1;
Mike Snitzer02233342015-03-10 23:49:26 -04002729 if (md_type == DM_TYPE_REQUEST_BASED) {
2730 /* make the memory for non-blk-mq clone part of the pdu */
2731 md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request);
2732 } else
2733 md->tag_set.cmd_size = sizeof(struct dm_rq_target_io);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002734 md->tag_set.driver_data = md;
2735
2736 err = blk_mq_alloc_tag_set(&md->tag_set);
2737 if (err)
2738 return err;
2739
2740 q = blk_mq_init_allocated_queue(&md->tag_set, md->queue);
2741 if (IS_ERR(q)) {
2742 err = PTR_ERR(q);
2743 goto out_tag_set;
2744 }
2745 md->queue = q;
2746 dm_init_md_queue(md);
2747
2748 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
2749 blk_mq_register_disk(md->disk);
2750
Mike Snitzer02233342015-03-10 23:49:26 -04002751 if (md_type == DM_TYPE_REQUEST_BASED)
2752 init_rq_based_worker_thread(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002753
2754 return 0;
2755
2756out_tag_set:
2757 blk_mq_free_tag_set(&md->tag_set);
2758 return err;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002759}
2760
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002761static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2762{
2763 if (type == DM_TYPE_BIO_BASED)
2764 return type;
2765
2766 return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2767}
2768
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002769/*
2770 * Setup the DM device's queue based on md's type
2771 */
2772int dm_setup_md_queue(struct mapped_device *md)
2773{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002774 int r;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002775 unsigned md_type = filter_md_type(dm_get_md_type(md), md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002776
2777 switch (md_type) {
2778 case DM_TYPE_REQUEST_BASED:
2779 r = dm_init_request_based_queue(md);
2780 if (r) {
Mike Snitzerff36ab32015-02-23 17:56:37 -05002781 DMWARN("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002782 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002783 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002784 break;
2785 case DM_TYPE_MQ_REQUEST_BASED:
2786 r = dm_init_request_based_blk_mq_queue(md);
2787 if (r) {
2788 DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
2789 return r;
2790 }
2791 break;
2792 case DM_TYPE_BIO_BASED:
2793 dm_init_old_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002794 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04002795 /*
2796 * DM handles splitting bios as needed. Free the bio_split bioset
2797 * since it won't be used (saves 1 process per bio-based DM device).
2798 */
2799 bioset_free(md->queue->bio_split);
2800 md->queue->bio_split = NULL;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002801 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002802 }
2803
2804 return 0;
2805}
2806
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002807struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
2809 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 unsigned minor = MINOR(dev);
2811
2812 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2813 return NULL;
2814
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002815 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002818 if (md) {
2819 if ((md == MINOR_ALLOCED ||
2820 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2821 dm_deleting_md(md) ||
2822 test_bit(DMF_FREEING, &md->flags))) {
2823 md = NULL;
2824 goto out;
2825 }
2826 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002829out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002830 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
David Teigland637842c2006-01-06 00:20:00 -08002832 return md;
2833}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002834EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002835
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002836void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002837{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002838 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}
2840
2841void dm_set_mdptr(struct mapped_device *md, void *ptr)
2842{
2843 md->interface_ptr = ptr;
2844}
2845
2846void dm_get(struct mapped_device *md)
2847{
2848 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002849 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002852int dm_hold(struct mapped_device *md)
2853{
2854 spin_lock(&_minor_lock);
2855 if (test_bit(DMF_FREEING, &md->flags)) {
2856 spin_unlock(&_minor_lock);
2857 return -EBUSY;
2858 }
2859 dm_get(md);
2860 spin_unlock(&_minor_lock);
2861 return 0;
2862}
2863EXPORT_SYMBOL_GPL(dm_hold);
2864
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002865const char *dm_device_name(struct mapped_device *md)
2866{
2867 return md->name;
2868}
2869EXPORT_SYMBOL_GPL(dm_device_name);
2870
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002871static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002873 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002874 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002876 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002877
Mike Snitzer63a4f062015-03-23 17:01:43 -04002878 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002879 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2880 set_bit(DMF_FREEING, &md->flags);
2881 spin_unlock(&_minor_lock);
2882
Mike Snitzer02233342015-03-10 23:49:26 -04002883 if (dm_request_based(md) && md->kworker_task)
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002884 flush_kthread_worker(&md->kworker);
2885
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002886 /*
2887 * Take suspend_lock so that presuspend and postsuspend methods
2888 * do not race with internal suspend.
2889 */
2890 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002891 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002892 if (!dm_suspended_md(md)) {
2893 dm_table_presuspend_targets(map);
2894 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002896 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2897 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002898 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002899
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002900 /*
2901 * Rare, but there may be I/O requests still going to complete,
2902 * for example. Wait for all references to disappear.
2903 * No one should increment the reference count of the mapped_device,
2904 * after the mapped_device state becomes DMF_FREEING.
2905 */
2906 if (wait)
2907 while (atomic_read(&md->holders))
2908 msleep(1);
2909 else if (atomic_read(&md->holders))
2910 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2911 dm_device_name(md), atomic_read(&md->holders));
2912
2913 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002914 dm_table_destroy(__unbind(md));
2915 free_dev(md);
2916}
2917
2918void dm_destroy(struct mapped_device *md)
2919{
2920 __dm_destroy(md, true);
2921}
2922
2923void dm_destroy_immediate(struct mapped_device *md)
2924{
2925 __dm_destroy(md, false);
2926}
2927
2928void dm_put(struct mapped_device *md)
2929{
2930 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
Edward Goggin79eb8852007-05-09 02:32:56 -07002932EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Mikulas Patocka401600d2009-04-02 19:55:38 +01002934static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002935{
2936 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002937 DECLARE_WAITQUEUE(wait, current);
2938
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002939 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002940
2941 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002942 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002943
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002944 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002945 break;
2946
Mikulas Patocka401600d2009-04-02 19:55:38 +01002947 if (interruptible == TASK_INTERRUPTIBLE &&
2948 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002949 r = -EINTR;
2950 break;
2951 }
2952
2953 io_schedule();
2954 }
2955 set_current_state(TASK_RUNNING);
2956
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002957 remove_wait_queue(&md->wait, &wait);
2958
Milan Broz46125c12008-02-08 02:10:30 +00002959 return r;
2960}
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962/*
2963 * Process the deferred bios
2964 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002965static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
Mikulas Patockaef208582009-04-02 19:55:38 +01002967 struct mapped_device *md = container_of(work, struct mapped_device,
2968 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002969 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002970 int srcu_idx;
2971 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002973 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002974
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002975 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002976 spin_lock_irq(&md->deferred_lock);
2977 c = bio_list_pop(&md->deferred);
2978 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002979
Tejun Heo6a8736d2010-09-08 18:07:00 +02002980 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002981 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002982
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002983 if (dm_request_based(md))
2984 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002985 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002986 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002987 }
Milan Broz73d410c2008-02-08 02:10:25 +00002988
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002989 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002992static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002993{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002994 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002995 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002996 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002997}
2998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003000 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003002struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Mike Christie87eb5b22013-03-01 22:45:48 +00003004 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003005 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003006 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Daniel Walkere61290a2008-02-08 02:10:08 +00003008 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003011 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Mike Snitzer3ae70652012-09-26 23:45:45 +01003014 /*
3015 * If the new table has no data devices, retain the existing limits.
3016 * This helps multipath with queue_if_no_path if all paths disappear,
3017 * then new I/O is queued based on these limits, and then some paths
3018 * reappear.
3019 */
3020 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003021 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003022 if (live_map)
3023 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003024 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003025 }
3026
Mike Christie87eb5b22013-03-01 22:45:48 +00003027 if (!live_map) {
3028 r = dm_calculate_queue_limits(table, &limits);
3029 if (r) {
3030 map = ERR_PTR(r);
3031 goto out;
3032 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003033 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003034
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003035 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003037out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003038 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003039 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042/*
3043 * Functions to lock and unlock any filesystem running on the
3044 * device.
3045 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003046static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003048 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
3050 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003051
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003052 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003053 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003054 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003055 md->frozen_sb = NULL;
3056 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003057 }
3058
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003059 set_bit(DMF_FROZEN, &md->flags);
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return 0;
3062}
3063
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003064static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003066 if (!test_bit(DMF_FROZEN, &md->flags))
3067 return;
3068
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003069 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003071 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
3074/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04003075 * If __dm_suspend returns 0, the device is completely quiescent
3076 * now. There is no request-processing activity. All new requests
3077 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003078 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04003079 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003080 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003081static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3082 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003084 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3085 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3086 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003088 /*
3089 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
3090 * This flag is cleared before dm_suspend returns.
3091 */
3092 if (noflush)
3093 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
3094
Mike Snitzerd67ee212014-10-28 20:13:31 -04003095 /*
3096 * This gets reverted if there's an error later and the targets
3097 * provide the .presuspend_undo hook.
3098 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003099 dm_table_presuspend_targets(map);
3100
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003101 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003102 * Flush I/O to the device.
3103 * Any I/O submitted after lock_fs() may not be flushed.
3104 * noflush takes precedence over do_lockfs.
3105 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003106 */
3107 if (!noflush && do_lockfs) {
3108 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003109 if (r) {
3110 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003111 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04003112 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003116 * Here we must make sure that no processes are submitting requests
3117 * to target drivers i.e. no one may be executing
3118 * __split_and_process_bio. This is called from dm_request and
3119 * dm_wq_work.
3120 *
3121 * To get all processes out of __split_and_process_bio in dm_request,
3122 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02003123 * __split_and_process_bio from dm_request and quiesce the thread
3124 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
3125 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01003127 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003128 if (map)
3129 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003131 /*
Tejun Heo29e40132010-09-08 18:07:00 +02003132 * Stop md->queue before flushing md->wq in case request-based
3133 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003134 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003135 if (dm_request_based(md)) {
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003136 stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04003137 if (md->kworker_task)
3138 flush_kthread_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003139 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003140
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003141 flush_workqueue(md->wq);
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003144 * At this point no more requests are entering target request routines.
3145 * We call dm_wait_for_completion to wait for all existing requests
3146 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003148 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Milan Broz6d6f10d2008-02-08 02:10:22 +00003150 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01003151 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003152 if (map)
3153 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00003156 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003157 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00003158
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003159 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003160 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003161
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003162 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003163 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003164 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003165 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003166
Mike Snitzerffcc3932014-10-28 18:34:52 -04003167 return r;
3168}
3169
3170/*
3171 * We need to be able to change a mapping table under a mounted
3172 * filesystem. For example we might want to move some data in
3173 * the background. Before the table can be swapped with
3174 * dm_bind_table, dm_suspend must be called to flush any in
3175 * flight bios and ensure that any further io gets deferred.
3176 */
3177/*
3178 * Suspend mechanism in request-based dm.
3179 *
3180 * 1. Flush all I/Os by lock_fs() if needed.
3181 * 2. Stop dispatching any I/O by stopping the request_queue.
3182 * 3. Wait for all in-flight I/Os to be completed or requeued.
3183 *
3184 * To abort suspend, start the request_queue.
3185 */
3186int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3187{
3188 struct dm_table *map = NULL;
3189 int r = 0;
3190
3191retry:
3192 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3193
3194 if (dm_suspended_md(md)) {
3195 r = -EINVAL;
3196 goto out_unlock;
3197 }
3198
3199 if (dm_suspended_internally_md(md)) {
3200 /* already internally suspended, wait for internal resume */
3201 mutex_unlock(&md->suspend_lock);
3202 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3203 if (r)
3204 return r;
3205 goto retry;
3206 }
3207
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003208 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003209
3210 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3211 if (r)
3212 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 set_bit(DMF_SUSPENDED, &md->flags);
3215
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00003216 dm_table_postsuspend_targets(map);
3217
Alasdair G Kergond2874832006-11-08 17:44:43 -08003218out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00003219 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003220 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Mike Snitzerffcc3932014-10-28 18:34:52 -04003223static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003225 if (map) {
3226 int r = dm_table_resume_targets(map);
3227 if (r)
3228 return r;
3229 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003230
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003231 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003232
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003233 /*
3234 * Flushing deferred I/Os must be done after targets are resumed
3235 * so that mapping of targets can work correctly.
3236 * Request-based dm is queueing the deferred I/Os in its request_queue.
3237 */
3238 if (dm_request_based(md))
3239 start_queue(md->queue);
3240
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003241 unlock_fs(md);
3242
Mike Snitzerffcc3932014-10-28 18:34:52 -04003243 return 0;
3244}
3245
3246int dm_resume(struct mapped_device *md)
3247{
3248 int r = -EINVAL;
3249 struct dm_table *map = NULL;
3250
3251retry:
3252 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3253
3254 if (!dm_suspended_md(md))
3255 goto out;
3256
3257 if (dm_suspended_internally_md(md)) {
3258 /* already internally suspended, wait for internal resume */
3259 mutex_unlock(&md->suspend_lock);
3260 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3261 if (r)
3262 return r;
3263 goto retry;
3264 }
3265
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003266 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003267 if (!map || !dm_table_get_size(map))
3268 goto out;
3269
3270 r = __dm_resume(md, map);
3271 if (r)
3272 goto out;
3273
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003274 clear_bit(DMF_SUSPENDED, &md->flags);
3275
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003276 r = 0;
3277out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003278 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003279
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003280 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003283/*
3284 * Internal suspend/resume works like userspace-driven suspend. It waits
3285 * until all bios finish and prevents issuing new bios to the target drivers.
3286 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003287 */
3288
Mike Snitzerffcc3932014-10-28 18:34:52 -04003289static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3290{
3291 struct dm_table *map = NULL;
3292
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003293 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003294 return; /* nested internal suspend */
3295
3296 if (dm_suspended_md(md)) {
3297 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3298 return; /* nest suspend */
3299 }
3300
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003301 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003302
3303 /*
3304 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3305 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3306 * would require changing .presuspend to return an error -- avoid this
3307 * until there is a need for more elaborate variants of internal suspend.
3308 */
3309 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3310
3311 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3312
3313 dm_table_postsuspend_targets(map);
3314}
3315
3316static void __dm_internal_resume(struct mapped_device *md)
3317{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003318 BUG_ON(!md->internal_suspend_count);
3319
3320 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003321 return; /* resume from nested internal suspend */
3322
3323 if (dm_suspended_md(md))
3324 goto done; /* resume from nested suspend */
3325
3326 /*
3327 * NOTE: existing callers don't need to call dm_table_resume_targets
3328 * (which may fail -- so best to avoid it for now by passing NULL map)
3329 */
3330 (void) __dm_resume(md, NULL);
3331
3332done:
3333 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3334 smp_mb__after_atomic();
3335 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3336}
3337
3338void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003339{
3340 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003341 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3342 mutex_unlock(&md->suspend_lock);
3343}
3344EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3345
3346void dm_internal_resume(struct mapped_device *md)
3347{
3348 mutex_lock(&md->suspend_lock);
3349 __dm_internal_resume(md);
3350 mutex_unlock(&md->suspend_lock);
3351}
3352EXPORT_SYMBOL_GPL(dm_internal_resume);
3353
3354/*
3355 * Fast variants of internal suspend/resume hold md->suspend_lock,
3356 * which prevents interaction with userspace-driven suspend.
3357 */
3358
3359void dm_internal_suspend_fast(struct mapped_device *md)
3360{
3361 mutex_lock(&md->suspend_lock);
3362 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003363 return;
3364
3365 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3366 synchronize_srcu(&md->io_barrier);
3367 flush_workqueue(md->wq);
3368 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3369}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003370EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003371
Mike Snitzerffcc3932014-10-28 18:34:52 -04003372void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003373{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003374 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003375 goto done;
3376
3377 dm_queue_flush(md);
3378
3379done:
3380 mutex_unlock(&md->suspend_lock);
3381}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003382EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384/*-----------------------------------------------------------------
3385 * Event notification.
3386 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003387int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003388 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003389{
Milan Broz60935eb2009-06-22 10:12:30 +01003390 char udev_cookie[DM_COOKIE_LENGTH];
3391 char *envp[] = { udev_cookie, NULL };
3392
3393 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003394 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003395 else {
3396 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3397 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003398 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3399 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003400 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003401}
3402
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003403uint32_t dm_next_uevent_seq(struct mapped_device *md)
3404{
3405 return atomic_add_return(1, &md->uevent_seq);
3406}
3407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408uint32_t dm_get_event_nr(struct mapped_device *md)
3409{
3410 return atomic_read(&md->event_nr);
3411}
3412
3413int dm_wait_event(struct mapped_device *md, int event_nr)
3414{
3415 return wait_event_interruptible(md->eventq,
3416 (event_nr != atomic_read(&md->event_nr)));
3417}
3418
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003419void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3420{
3421 unsigned long flags;
3422
3423 spin_lock_irqsave(&md->uevent_lock, flags);
3424 list_add(elist, &md->uevent_list);
3425 spin_unlock_irqrestore(&md->uevent_lock, flags);
3426}
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428/*
3429 * The gendisk is only valid as long as you have a reference
3430 * count on 'md'.
3431 */
3432struct gendisk *dm_disk(struct mapped_device *md)
3433{
3434 return md->disk;
3435}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00003436EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Milan Broz784aae72009-01-06 03:05:12 +00003438struct kobject *dm_kobject(struct mapped_device *md)
3439{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003440 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003441}
3442
Milan Broz784aae72009-01-06 03:05:12 +00003443struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3444{
3445 struct mapped_device *md;
3446
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003447 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003448
Milan Broz4d89b7b2009-06-22 10:12:11 +01003449 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003450 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003451 return NULL;
3452
Milan Broz784aae72009-01-06 03:05:12 +00003453 dm_get(md);
3454 return md;
3455}
3456
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003457int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 return test_bit(DMF_SUSPENDED, &md->flags);
3460}
3461
Mike Snitzerffcc3932014-10-28 18:34:52 -04003462int dm_suspended_internally_md(struct mapped_device *md)
3463{
3464 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3465}
3466
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003467int dm_test_deferred_remove_flag(struct mapped_device *md)
3468{
3469 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3470}
3471
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003472int dm_suspended(struct dm_target *ti)
3473{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003474 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003475}
3476EXPORT_SYMBOL_GPL(dm_suspended);
3477
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003478int dm_noflush_suspending(struct dm_target *ti)
3479{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003480 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003481}
3482EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3483
Mike Snitzer78d8e582015-06-26 10:01:13 -04003484struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
3485 unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003486{
Mike Snitzer78d8e582015-06-26 10:01:13 -04003487 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3488 struct kmem_cache *cachep = NULL;
3489 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003490 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003491
3492 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003493 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003494
Mike Snitzer78d8e582015-06-26 10:01:13 -04003495 type = filter_md_type(type, md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04003496
Mike Snitzer78d8e582015-06-26 10:01:13 -04003497 switch (type) {
3498 case DM_TYPE_BIO_BASED:
3499 cachep = _io_cache;
3500 pool_size = dm_get_reserved_bio_based_ios();
3501 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
3502 break;
3503 case DM_TYPE_REQUEST_BASED:
3504 cachep = _rq_tio_cache;
3505 pool_size = dm_get_reserved_rq_based_ios();
3506 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3507 if (!pools->rq_pool)
3508 goto out;
3509 /* fall through to setup remaining rq-based pools */
3510 case DM_TYPE_MQ_REQUEST_BASED:
3511 if (!pool_size)
3512 pool_size = dm_get_reserved_rq_based_ios();
3513 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3514 /* per_bio_data_size is not used. See __bind_mempools(). */
3515 WARN_ON(per_bio_data_size != 0);
3516 break;
3517 default:
3518 BUG();
3519 }
3520
3521 if (cachep) {
3522 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3523 if (!pools->io_pool)
3524 goto out;
3525 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003526
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003527 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003528 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003529 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003530
Martin K. Petersena91a2782011-03-17 11:11:05 +01003531 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003532 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003533
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003534 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003535
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003536out:
3537 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003538
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003539 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003540}
3541
3542void dm_free_md_mempools(struct dm_md_mempools *pools)
3543{
3544 if (!pools)
3545 return;
3546
Julia Lawall6f659852015-09-13 14:15:05 +02003547 mempool_destroy(pools->io_pool);
3548 mempool_destroy(pools->rq_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003549
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003550 if (pools->bs)
3551 bioset_free(pools->bs);
3552
3553 kfree(pools);
3554}
3555
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003556static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
3557 u32 flags)
3558{
3559 struct mapped_device *md = bdev->bd_disk->private_data;
3560 const struct pr_ops *ops;
3561 struct dm_target *tgt;
3562 fmode_t mode;
3563 int srcu_idx, r;
3564
3565 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3566 if (r < 0)
3567 return r;
3568
3569 ops = bdev->bd_disk->fops->pr_ops;
3570 if (ops && ops->pr_register)
3571 r = ops->pr_register(bdev, old_key, new_key, flags);
3572 else
3573 r = -EOPNOTSUPP;
3574
3575 dm_put_live_table(md, srcu_idx);
3576 return r;
3577}
3578
3579static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
3580 u32 flags)
3581{
3582 struct mapped_device *md = bdev->bd_disk->private_data;
3583 const struct pr_ops *ops;
3584 struct dm_target *tgt;
3585 fmode_t mode;
3586 int srcu_idx, r;
3587
3588 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3589 if (r < 0)
3590 return r;
3591
3592 ops = bdev->bd_disk->fops->pr_ops;
3593 if (ops && ops->pr_reserve)
3594 r = ops->pr_reserve(bdev, key, type, flags);
3595 else
3596 r = -EOPNOTSUPP;
3597
3598 dm_put_live_table(md, srcu_idx);
3599 return r;
3600}
3601
3602static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3603{
3604 struct mapped_device *md = bdev->bd_disk->private_data;
3605 const struct pr_ops *ops;
3606 struct dm_target *tgt;
3607 fmode_t mode;
3608 int srcu_idx, r;
3609
3610 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3611 if (r < 0)
3612 return r;
3613
3614 ops = bdev->bd_disk->fops->pr_ops;
3615 if (ops && ops->pr_release)
3616 r = ops->pr_release(bdev, key, type);
3617 else
3618 r = -EOPNOTSUPP;
3619
3620 dm_put_live_table(md, srcu_idx);
3621 return r;
3622}
3623
3624static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
3625 enum pr_type type, bool abort)
3626{
3627 struct mapped_device *md = bdev->bd_disk->private_data;
3628 const struct pr_ops *ops;
3629 struct dm_target *tgt;
3630 fmode_t mode;
3631 int srcu_idx, r;
3632
3633 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3634 if (r < 0)
3635 return r;
3636
3637 ops = bdev->bd_disk->fops->pr_ops;
3638 if (ops && ops->pr_preempt)
3639 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3640 else
3641 r = -EOPNOTSUPP;
3642
3643 dm_put_live_table(md, srcu_idx);
3644 return r;
3645}
3646
3647static int dm_pr_clear(struct block_device *bdev, u64 key)
3648{
3649 struct mapped_device *md = bdev->bd_disk->private_data;
3650 const struct pr_ops *ops;
3651 struct dm_target *tgt;
3652 fmode_t mode;
3653 int srcu_idx, r;
3654
3655 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3656 if (r < 0)
3657 return r;
3658
3659 ops = bdev->bd_disk->fops->pr_ops;
3660 if (ops && ops->pr_clear)
3661 r = ops->pr_clear(bdev, key);
3662 else
3663 r = -EOPNOTSUPP;
3664
3665 dm_put_live_table(md, srcu_idx);
3666 return r;
3667}
3668
3669static const struct pr_ops dm_pr_ops = {
3670 .pr_register = dm_pr_register,
3671 .pr_reserve = dm_pr_reserve,
3672 .pr_release = dm_pr_release,
3673 .pr_preempt = dm_pr_preempt,
3674 .pr_clear = dm_pr_clear,
3675};
3676
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003677static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 .open = dm_blk_open,
3679 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003680 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003681 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003682 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 .owner = THIS_MODULE
3684};
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686/*
3687 * module hooks
3688 */
3689module_init(dm_init);
3690module_exit(dm_exit);
3691
3692module_param(major, uint, 0);
3693MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003694
Mike Snitzere8603132013-09-12 18:06:12 -04003695module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3696MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3697
Mike Snitzerf4790822013-09-12 18:06:12 -04003698module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3699MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3700
Mike Snitzer17e149b2015-03-11 15:01:09 -04003701module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
3702MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704MODULE_DESCRIPTION(DM_NAME " driver");
3705MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3706MODULE_LICENSE("GPL");