blob: 6e15f3565892fce8315cef992e0b2721e8c20088 [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);
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100594 if (r == -ENOTCONN) {
595 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;
606 int srcu_idx, r;
607
608 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
609 if (r < 0)
610 return r;
611
612 if (r > 0) {
613 /*
614 * Target determined this ioctl is being issued against
615 * a logical partition of the parent bdev; so extra
616 * validation is needed.
617 */
618 r = scsi_verify_blk_ioctl(NULL, cmd);
619 if (r)
620 goto out;
621 }
622
623 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
624out:
625 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700626 return r;
627}
628
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100629static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 return mempool_alloc(md->io_pool, GFP_NOIO);
632}
633
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100634static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 mempool_free(io, md->io_pool);
637}
638
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100639static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Mikulas Patockadba14162012-10-12 21:02:15 +0100641 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000644static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
645 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100646{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000647 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100648}
649
650static void free_rq_tio(struct dm_rq_target_io *tio)
651{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000652 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100653}
654
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500655static struct request *alloc_clone_request(struct mapped_device *md,
656 gfp_t gfp_mask)
657{
658 return mempool_alloc(md->rq_pool, gfp_mask);
659}
660
661static void free_clone_request(struct mapped_device *md, struct request *rq)
662{
663 mempool_free(rq, md->rq_pool);
664}
665
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000666static int md_in_flight(struct mapped_device *md)
667{
668 return atomic_read(&md->pending[READ]) +
669 atomic_read(&md->pending[WRITE]);
670}
671
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800672static void start_io_acct(struct dm_io *io)
673{
674 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400675 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900676 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400677 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800678
679 io->start_time = jiffies;
680
Tejun Heo074a7ac2008-08-25 19:56:14 +0900681 cpu = part_stat_lock();
682 part_round_stats(cpu, &dm_disk(md)->part0);
683 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100684 atomic_set(&dm_disk(md)->part0.in_flight[rw],
685 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400686
687 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700688 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400689 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800690}
691
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000692static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800693{
694 struct mapped_device *md = io->md;
695 struct bio *bio = io->bio;
696 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800697 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800698 int rw = bio_data_dir(bio);
699
Gu Zheng18c0b222014-11-24 11:05:26 +0800700 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800701
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400702 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700703 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400704 bio_sectors(bio), true, duration, &io->stats_aux);
705
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100706 /*
707 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200708 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100709 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100710 pending = atomic_dec_return(&md->pending[rw]);
711 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200712 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800713
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000714 /* nudge anyone waiting on suspend queue */
715 if (!pending)
716 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
720 * Add the bio to the list of deferred io.
721 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100722static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200724 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200726 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200728 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200729 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/*
733 * Everyone (including functions in this file), should use this
734 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100735 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100737struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100739 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100741 return srcu_dereference(md->map, &md->io_barrier);
742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100744void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
745{
746 srcu_read_unlock(&md->io_barrier, srcu_idx);
747}
748
749void dm_sync_table(struct mapped_device *md)
750{
751 synchronize_srcu(&md->io_barrier);
752 synchronize_rcu_expedited();
753}
754
755/*
756 * A fast alternative to dm_get_live_table/dm_put_live_table.
757 * The caller must not block between these two functions.
758 */
759static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
760{
761 rcu_read_lock();
762 return rcu_dereference(md->map);
763}
764
765static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
766{
767 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800770/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500771 * Open a table device so we can use it as a map destination.
772 */
773static int open_table_device(struct table_device *td, dev_t dev,
774 struct mapped_device *md)
775{
776 static char *_claim_ptr = "I belong to device-mapper";
777 struct block_device *bdev;
778
779 int r;
780
781 BUG_ON(td->dm_dev.bdev);
782
783 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
784 if (IS_ERR(bdev))
785 return PTR_ERR(bdev);
786
787 r = bd_link_disk_holder(bdev, dm_disk(md));
788 if (r) {
789 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
790 return r;
791 }
792
793 td->dm_dev.bdev = bdev;
794 return 0;
795}
796
797/*
798 * Close a table device that we've been using.
799 */
800static void close_table_device(struct table_device *td, struct mapped_device *md)
801{
802 if (!td->dm_dev.bdev)
803 return;
804
805 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
806 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
807 td->dm_dev.bdev = NULL;
808}
809
810static struct table_device *find_table_device(struct list_head *l, dev_t dev,
811 fmode_t mode) {
812 struct table_device *td;
813
814 list_for_each_entry(td, l, list)
815 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
816 return td;
817
818 return NULL;
819}
820
821int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
822 struct dm_dev **result) {
823 int r;
824 struct table_device *td;
825
826 mutex_lock(&md->table_devices_lock);
827 td = find_table_device(&md->table_devices, dev, mode);
828 if (!td) {
829 td = kmalloc(sizeof(*td), GFP_KERNEL);
830 if (!td) {
831 mutex_unlock(&md->table_devices_lock);
832 return -ENOMEM;
833 }
834
835 td->dm_dev.mode = mode;
836 td->dm_dev.bdev = NULL;
837
838 if ((r = open_table_device(td, dev, md))) {
839 mutex_unlock(&md->table_devices_lock);
840 kfree(td);
841 return r;
842 }
843
844 format_dev_t(td->dm_dev.name, dev);
845
846 atomic_set(&td->count, 0);
847 list_add(&td->list, &md->table_devices);
848 }
849 atomic_inc(&td->count);
850 mutex_unlock(&md->table_devices_lock);
851
852 *result = &td->dm_dev;
853 return 0;
854}
855EXPORT_SYMBOL_GPL(dm_get_table_device);
856
857void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
858{
859 struct table_device *td = container_of(d, struct table_device, dm_dev);
860
861 mutex_lock(&md->table_devices_lock);
862 if (atomic_dec_and_test(&td->count)) {
863 close_table_device(td, md);
864 list_del(&td->list);
865 kfree(td);
866 }
867 mutex_unlock(&md->table_devices_lock);
868}
869EXPORT_SYMBOL(dm_put_table_device);
870
871static void free_table_devices(struct list_head *devices)
872{
873 struct list_head *tmp, *next;
874
875 list_for_each_safe(tmp, next, devices) {
876 struct table_device *td = list_entry(tmp, struct table_device, list);
877
878 DMWARN("dm_destroy: %s still exists with %d references",
879 td->dm_dev.name, atomic_read(&td->count));
880 kfree(td);
881 }
882}
883
884/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800885 * Get the geometry associated with a dm device
886 */
887int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
888{
889 *geo = md->geometry;
890
891 return 0;
892}
893
894/*
895 * Set the geometry of a device.
896 */
897int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
898{
899 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
900
901 if (geo->start > sz) {
902 DMWARN("Start sector is beyond the geometry limits.");
903 return -EINVAL;
904 }
905
906 md->geometry = *geo;
907
908 return 0;
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/*-----------------------------------------------------------------
912 * CRUD START:
913 * A more elegant soln is in the works that uses the queue
914 * merge fn, unfortunately there are a couple of changes to
915 * the block layer that I want to make for this. So in the
916 * interests of getting something for people to use I give
917 * you this clearly demarcated crap.
918 *---------------------------------------------------------------*/
919
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800920static int __noflush_suspending(struct mapped_device *md)
921{
922 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * Decrements the number of outstanding ios that a bio has been
927 * cloned into, completing the original io if necc.
928 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800929static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800931 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000932 int io_error;
933 struct bio *bio;
934 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800935
936 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100937 if (unlikely(error)) {
938 spin_lock_irqsave(&io->endio_lock, flags);
939 if (!(io->error > 0 && __noflush_suspending(md)))
940 io->error = error;
941 spin_unlock_irqrestore(&io->endio_lock, flags);
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800945 if (io->error == DM_ENDIO_REQUEUE) {
946 /*
947 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800948 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100949 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200950 if (__noflush_suspending(md))
951 bio_list_add_head(&md->deferred, io->bio);
952 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800953 /* noflush suspend was interrupted. */
954 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100955 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800956 }
957
Milan Brozb35f8ca2009-03-16 17:44:36 +0000958 io_error = io->error;
959 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200960 end_io_acct(io);
961 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100962
Tejun Heo6a8736d2010-09-08 18:07:00 +0200963 if (io_error == DM_ENDIO_REQUEUE)
964 return;
965
Kent Overstreet4f024f32013-10-11 15:44:27 -0700966 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100967 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200968 * Preflush done for flush with data, reissue
969 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100970 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200971 bio->bi_rw &= ~REQ_FLUSH;
972 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100973 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200974 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700975 trace_block_bio_complete(md->queue, bio, io_error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200976 bio->bi_error = io_error;
977 bio_endio(bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980}
981
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400982static void disable_write_same(struct mapped_device *md)
983{
984 struct queue_limits *limits = dm_get_queue_limits(md);
985
986 /* device doesn't really support WRITE SAME, disable it */
987 limits->max_write_same_sectors = 0;
988}
989
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200990static void clone_endio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200992 int error = bio->bi_error;
zhendong chen5164bec2014-12-17 14:37:04 +0800993 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500994 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000995 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700996 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 dm_endio_fn endio = tio->ti->type->end_io;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001000 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001001 if (r < 0 || r == DM_ENDIO_REQUEUE)
1002 /*
1003 * error and requeue request are handled
1004 * in dec_pending().
1005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001007 else if (r == DM_ENDIO_INCOMPLETE)
1008 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +02001009 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001010 else if (r) {
1011 DMWARN("unimplemented target endio return value: %d", r);
1012 BUG();
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001016 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
1017 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
1018 disable_write_same(md);
1019
Stefan Bader9faf4002006-10-03 01:15:41 -07001020 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001021 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Mike Snitzer78d8e582015-06-26 10:01:13 -04001024/*
1025 * Partial completion handling for request-based dm
1026 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001027static void end_clone_bio(struct bio *clone)
Mike Snitzer78d8e582015-06-26 10:01:13 -04001028{
1029 struct dm_rq_clone_bio_info *info =
1030 container_of(clone, struct dm_rq_clone_bio_info, clone);
1031 struct dm_rq_target_io *tio = info->tio;
1032 struct bio *bio = info->orig;
1033 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001034 int error = clone->bi_error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001035
1036 bio_put(clone);
1037
1038 if (tio->error)
1039 /*
1040 * An error has already been detected on the request.
1041 * Once error occurred, just let clone->end_io() handle
1042 * the remainder.
1043 */
1044 return;
Junichi Nomura50887bd2015-10-06 04:19:54 +00001045 else if (error) {
Mike Snitzer78d8e582015-06-26 10:01:13 -04001046 /*
1047 * Don't notice the error to the upper layer yet.
1048 * The error handling decision is made by the target driver,
1049 * when the request is completed.
1050 */
Junichi Nomura50887bd2015-10-06 04:19:54 +00001051 tio->error = error;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001052 return;
1053 }
1054
1055 /*
1056 * I/O for the bio successfully completed.
1057 * Notice the data completion to the upper layer.
1058 */
1059
1060 /*
1061 * bios are processed from the head of the list.
1062 * So the completing bio should always be rq->bio.
1063 * If it's not, something wrong is happening.
1064 */
1065 if (tio->orig->bio != bio)
1066 DMERR("bio completion is going in the middle of the request");
1067
1068 /*
1069 * Update the original request.
1070 * Do not use blk_end_request() here, because it may complete
1071 * the original request before the clone, and break the ordering.
1072 */
1073 blk_update_request(tio->orig, 0, nr_bytes);
1074}
1075
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001076static struct dm_rq_target_io *tio_from_request(struct request *rq)
1077{
1078 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1079}
1080
Mikulas Patockae262f342015-06-09 17:22:49 -04001081static void rq_end_stats(struct mapped_device *md, struct request *orig)
1082{
1083 if (unlikely(dm_stats_used(&md->stats))) {
1084 struct dm_rq_target_io *tio = tio_from_request(orig);
1085 tio->duration_jiffies = jiffies - tio->duration_jiffies;
1086 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1087 tio->n_sectors, true, tio->duration_jiffies,
1088 &tio->stats_aux);
1089 }
1090}
1091
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001092/*
1093 * Don't touch any member of the md after calling this function because
1094 * the md may be freed in dm_put() at the end of this function.
1095 * Or do dm_get() before calling this function and dm_put() later.
1096 */
Keith Busch466d89a2014-10-17 17:46:37 -06001097static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001098{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001099 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001100
1101 /* nudge anyone waiting on suspend queue */
Mike Snitzer621739b2015-07-08 16:08:24 -04001102 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001103 wake_up(&md->wait);
1104
Jens Axboea8c32a52012-11-06 12:24:26 +01001105 /*
1106 * Run this off this callpath, as drivers could invoke end_io while
1107 * inside their request_fn (and holding the queue lock). Calling
1108 * back into ->request_fn() could deadlock attempting to grab the
1109 * queue lock again.
1110 */
Mike Snitzer9a0e6092015-02-24 11:03:22 -05001111 if (run_queue) {
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001112 if (md->queue->mq_ops)
1113 blk_mq_run_hw_queues(md->queue, true);
Mike Snitzer621739b2015-07-08 16:08:24 -04001114 else
Mike Snitzer9a0e6092015-02-24 11:03:22 -05001115 blk_run_queue_async(md->queue);
1116 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001117
1118 /*
1119 * dm_put() must be at the end of this function. See the comment above
1120 */
1121 dm_put(md);
1122}
1123
Mike Snitzere5d8de32015-05-28 15:12:52 -04001124static void free_rq_clone(struct request *clone)
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001125{
1126 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001127 struct mapped_device *md = tio->md;
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001128
Mike Snitzer78d8e582015-06-26 10:01:13 -04001129 blk_rq_unprep_clone(clone);
1130
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001131 if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1132 /* stacked on blk-mq queue(s) */
Mike Snitzere5863d92014-12-17 21:08:12 -05001133 tio->ti->type->release_clone_rq(clone);
Mike Snitzer02233342015-03-10 23:49:26 -04001134 else if (!md->queue->mq_ops)
1135 /* request_fn queue stacked on request_fn queue(s) */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001136 free_clone_request(md, clone);
Mike Snitzeraa6df8d2015-04-29 10:48:09 -04001137 /*
1138 * NOTE: for the blk-mq queue stacked on request_fn queue(s) case:
1139 * no need to call free_clone_request() because we leverage blk-mq by
1140 * allocating the clone at the end of the blk-mq pdu (see: clone_rq)
1141 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001142
1143 if (!md->queue->mq_ops)
1144 free_rq_tio(tio);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001145}
1146
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001147/*
1148 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001149 * Must be called without clone's queue lock held,
1150 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001151 */
1152static void dm_end_request(struct request *clone, int error)
1153{
1154 int rw = rq_data_dir(clone);
1155 struct dm_rq_target_io *tio = clone->end_io_data;
1156 struct mapped_device *md = tio->md;
1157 struct request *rq = tio->orig;
1158
Tejun Heo29e40132010-09-08 18:07:00 +02001159 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001160 rq->errors = clone->errors;
1161 rq->resid_len = clone->resid_len;
1162
1163 if (rq->sense)
1164 /*
1165 * We are using the sense buffer of the original
1166 * request.
1167 * So setting the length of the sense data is enough.
1168 */
1169 rq->sense_len = clone->sense_len;
1170 }
1171
Mike Snitzere5d8de32015-05-28 15:12:52 -04001172 free_rq_clone(clone);
Mikulas Patockae262f342015-06-09 17:22:49 -04001173 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001174 if (!rq->q->mq_ops)
1175 blk_end_request_all(rq, error);
1176 else
1177 blk_mq_end_request(rq, error);
Tejun Heo29e40132010-09-08 18:07:00 +02001178 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001179}
1180
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001181static void dm_unprep_request(struct request *rq)
1182{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001183 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001184 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001185
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001186 if (!rq->q->mq_ops) {
1187 rq->special = NULL;
1188 rq->cmd_flags &= ~REQ_DONTPREP;
1189 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001190
Mike Snitzere5863d92014-12-17 21:08:12 -05001191 if (clone)
Mike Snitzere5d8de32015-05-28 15:12:52 -04001192 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001193}
1194
1195/*
1196 * Requeue the original request of a clone.
1197 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001198static void old_requeue_request(struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001199{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001200 struct request_queue *q = rq->q;
1201 unsigned long flags;
1202
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001203 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001204 blk_requeue_request(q, rq);
Junichi Nomura4ae99442015-05-26 08:25:54 +00001205 blk_run_queue_async(q);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001206 spin_unlock_irqrestore(q->queue_lock, flags);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001207}
1208
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001209static void dm_requeue_original_request(struct mapped_device *md,
1210 struct request *rq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001211{
1212 int rw = rq_data_dir(rq);
1213
1214 dm_unprep_request(rq);
1215
Mikulas Patockae262f342015-06-09 17:22:49 -04001216 rq_end_stats(md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001217 if (!rq->q->mq_ops)
1218 old_requeue_request(rq);
1219 else {
1220 blk_mq_requeue_request(rq);
1221 blk_mq_kick_requeue_list(rq->q);
1222 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001223
Keith Busch466d89a2014-10-17 17:46:37 -06001224 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001225}
Keith Busch466d89a2014-10-17 17:46:37 -06001226
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001227static void old_stop_queue(struct request_queue *q)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001228{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001229 unsigned long flags;
1230
1231 if (blk_queue_stopped(q))
1232 return;
1233
1234 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001235 blk_stop_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001236 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001237}
1238
1239static void stop_queue(struct request_queue *q)
1240{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001241 if (!q->mq_ops)
1242 old_stop_queue(q);
1243 else
1244 blk_mq_stop_hw_queues(q);
1245}
1246
1247static void old_start_queue(struct request_queue *q)
1248{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001249 unsigned long flags;
1250
1251 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001252 if (blk_queue_stopped(q))
1253 blk_start_queue(q);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001254 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001255}
1256
1257static void start_queue(struct request_queue *q)
1258{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001259 if (!q->mq_ops)
1260 old_start_queue(q);
1261 else
1262 blk_mq_start_stopped_hw_queues(q, true);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001263}
1264
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001265static void dm_done(struct request *clone, int error, bool mapped)
1266{
1267 int r = error;
1268 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001269 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001270
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001271 if (tio->ti) {
1272 rq_end_io = tio->ti->type->rq_end_io;
1273
1274 if (mapped && rq_end_io)
1275 r = rq_end_io(tio->ti, clone, error, &tio->info);
1276 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001277
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001278 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1279 !clone->q->limits.max_write_same_sectors))
1280 disable_write_same(tio->md);
1281
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001282 if (r <= 0)
1283 /* The target wants to complete the I/O */
1284 dm_end_request(clone, r);
1285 else if (r == DM_ENDIO_INCOMPLETE)
1286 /* The target will handle the I/O */
1287 return;
1288 else if (r == DM_ENDIO_REQUEUE)
1289 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001290 dm_requeue_original_request(tio->md, tio->orig);
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001291 else {
1292 DMWARN("unimplemented target endio return value: %d", r);
1293 BUG();
1294 }
1295}
1296
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001297/*
1298 * Request completion handler for request-based dm
1299 */
1300static void dm_softirq_done(struct request *rq)
1301{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001302 bool mapped = true;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001303 struct dm_rq_target_io *tio = tio_from_request(rq);
Keith Busch466d89a2014-10-17 17:46:37 -06001304 struct request *clone = tio->clone;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001305 int rw;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001306
Mike Snitzere5863d92014-12-17 21:08:12 -05001307 if (!clone) {
Mikulas Patockae262f342015-06-09 17:22:49 -04001308 rq_end_stats(tio->md, rq);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001309 rw = rq_data_dir(rq);
1310 if (!rq->q->mq_ops) {
1311 blk_end_request_all(rq, tio->error);
1312 rq_completed(tio->md, rw, false);
1313 free_rq_tio(tio);
1314 } else {
1315 blk_mq_end_request(rq, tio->error);
1316 rq_completed(tio->md, rw, false);
1317 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001318 return;
1319 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001320
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001321 if (rq->cmd_flags & REQ_FAILED)
1322 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001323
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001324 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001325}
1326
1327/*
1328 * Complete the clone and the original request with the error status
1329 * through softirq context.
1330 */
Keith Busch466d89a2014-10-17 17:46:37 -06001331static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001332{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001333 struct dm_rq_target_io *tio = tio_from_request(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001334
1335 tio->error = error;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001336 blk_complete_request(rq);
1337}
1338
1339/*
1340 * Complete the not-mapped clone and the original request with the error status
1341 * through softirq context.
1342 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001343 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001344 */
Keith Busch466d89a2014-10-17 17:46:37 -06001345static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001346{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001347 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001348 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001349}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001350
1351/*
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001352 * Called with the clone's queue lock held (for non-blk-mq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001353 */
1354static void end_clone_request(struct request *clone, int error)
1355{
Keith Busch466d89a2014-10-17 17:46:37 -06001356 struct dm_rq_target_io *tio = clone->end_io_data;
1357
Mike Snitzere5863d92014-12-17 21:08:12 -05001358 if (!clone->q->mq_ops) {
1359 /*
1360 * For just cleaning up the information of the queue in which
1361 * the clone was dispatched.
1362 * The clone is *NOT* freed actually here because it is alloced
1363 * from dm own mempool (REQ_ALLOCED isn't set).
1364 */
1365 __blk_put_request(clone->q, clone);
1366 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001367
1368 /*
1369 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001370 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001371 * - another request may be submitted by the upper level driver
1372 * of the stacking during the completion
1373 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001374 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001375 */
Keith Busch466d89a2014-10-17 17:46:37 -06001376 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001377}
1378
Mike Snitzer56a67df2010-08-12 04:14:10 +01001379/*
1380 * Return maximum size of I/O possible at the supplied sector up to the current
1381 * target boundary.
1382 */
1383static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001385 sector_t target_offset = dm_target_offset(ti, sector);
1386
1387 return ti->len - target_offset;
1388}
1389
1390static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1391{
1392 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001393 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001396 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001398 if (ti->max_io_len) {
1399 offset = dm_target_offset(ti, sector);
1400 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1401 max_len = sector_div(offset, ti->max_io_len);
1402 else
1403 max_len = offset & (ti->max_io_len - 1);
1404 max_len = ti->max_io_len - max_len;
1405
1406 if (len > max_len)
1407 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
1410 return len;
1411}
1412
Mike Snitzer542f9032012-07-27 15:08:00 +01001413int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1414{
1415 if (len > UINT_MAX) {
1416 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1417 (unsigned long long)len, UINT_MAX);
1418 ti->error = "Maximum size of target IO is too large";
1419 return -EINVAL;
1420 }
1421
1422 ti->max_io_len = (uint32_t) len;
1423
1424 return 0;
1425}
1426EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1427
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001428/*
1429 * A target may call dm_accept_partial_bio only from the map routine. It is
1430 * allowed for all bio types except REQ_FLUSH.
1431 *
1432 * dm_accept_partial_bio informs the dm that the target only wants to process
1433 * additional n_sectors sectors of the bio and the rest of the data should be
1434 * sent in a next bio.
1435 *
1436 * A diagram that explains the arithmetics:
1437 * +--------------------+---------------+-------+
1438 * | 1 | 2 | 3 |
1439 * +--------------------+---------------+-------+
1440 *
1441 * <-------------- *tio->len_ptr --------------->
1442 * <------- bi_size ------->
1443 * <-- n_sectors -->
1444 *
1445 * Region 1 was already iterated over with bio_advance or similar function.
1446 * (it may be empty if the target doesn't use bio_advance)
1447 * Region 2 is the remaining bio size that the target wants to process.
1448 * (it may be empty if region 1 is non-empty, although there is no reason
1449 * to make it empty)
1450 * The target requires that region 3 is to be sent in the next bio.
1451 *
1452 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1453 * the partially processed part (the sum of regions 1+2) must be the same for all
1454 * copies of the bio.
1455 */
1456void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1457{
1458 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1459 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1460 BUG_ON(bio->bi_rw & REQ_FLUSH);
1461 BUG_ON(bi_size > *tio->len_ptr);
1462 BUG_ON(n_sectors > bi_size);
1463 *tio->len_ptr -= bi_size - n_sectors;
1464 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1465}
1466EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1467
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001468static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001471 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001472 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001473 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001474 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /*
1479 * Map the clone. If r == 0 we don't need to do
1480 * anything, the target has assumed ownership of
1481 * this io.
1482 */
1483 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001484 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001485 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001486 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001488
Mike Snitzerd07335e2010-11-16 12:52:38 +01001489 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1490 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001493 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1494 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001495 md = tio->io->md;
1496 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001497 free_tio(md, tio);
Mikulas Patockaab378442015-07-01 17:30:36 -04001498 } else if (r != DM_MAPIO_SUBMITTED) {
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001499 DMWARN("unimplemented target map return value: %d", r);
1500 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502}
1503
1504struct clone_info {
1505 struct mapped_device *md;
1506 struct dm_table *map;
1507 struct bio *bio;
1508 struct dm_io *io;
1509 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001510 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511};
1512
Mikulas Patockae0d66092014-03-14 18:40:39 -04001513static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001514{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001515 bio->bi_iter.bi_sector = sector;
1516 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519/*
1520 * Creates a bio that consists of range of complete bvecs.
1521 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001522static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001523 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Mikulas Patockadba14162012-10-12 21:02:15 +01001525 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001527 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001529 if (bio_integrity(bio))
1530 bio_integrity_clone(clone, bio, GFP_NOIO);
1531
1532 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1533 clone->bi_iter.bi_size = to_bytes(len);
1534
1535 if (bio_integrity(bio))
1536 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001539static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001540 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001541 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001542{
Mikulas Patockadba14162012-10-12 21:02:15 +01001543 struct dm_target_io *tio;
1544 struct bio *clone;
1545
Junichi Nomura99778272014-10-03 11:55:16 +00001546 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001547 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001548
1549 tio->io = ci->io;
1550 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001551 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001552
1553 return tio;
1554}
1555
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001556static void __clone_and_map_simple_bio(struct clone_info *ci,
1557 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001558 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001559{
Junichi Nomura99778272014-10-03 11:55:16 +00001560 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001561 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001562
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001563 tio->len_ptr = len;
1564
Junichi Nomura99778272014-10-03 11:55:16 +00001565 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001566 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001567 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001568
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001569 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001570}
1571
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001572static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001573 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001574{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001575 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001576
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001577 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001578 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001579}
1580
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001581static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001582{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001583 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001584 struct dm_target *ti;
1585
Mike Snitzerb372d362010-09-08 18:07:01 +02001586 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001587 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001588 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001589
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001590 return 0;
1591}
1592
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001593static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001594 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001595{
Mikulas Patockadba14162012-10-12 21:02:15 +01001596 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001597 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001598 unsigned target_bio_nr;
1599 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001600
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001601 /*
1602 * Does the target want to receive duplicate copies of the bio?
1603 */
1604 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1605 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001606
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001607 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001608 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001609 tio->len_ptr = len;
1610 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001611 __map_bio(tio);
1612 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001613}
1614
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001615typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001616
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001617static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001618{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001619 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001620}
1621
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001622static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001623{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001624 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001625}
1626
1627typedef bool (*is_split_required_fn)(struct dm_target *ti);
1628
1629static bool is_split_required_for_discard(struct dm_target *ti)
1630{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001631 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001632}
1633
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001634static int __send_changing_extent_only(struct clone_info *ci,
1635 get_num_bios_fn get_num_bios,
1636 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001637{
1638 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001639 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001640 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001641
Mike Snitzera79245b2010-08-12 04:14:24 +01001642 do {
1643 ti = dm_table_find_target(ci->map, ci->sector);
1644 if (!dm_target_is_valid(ti))
1645 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001646
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001647 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001648 * Even though the device advertised support for this type of
1649 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001650 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001651 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001652 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001653 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1654 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001655 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001656
Mike Snitzer23508a92012-12-21 20:23:37 +00001657 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001658 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001659 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001660 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001661
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001662 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001663
1664 ci->sector += len;
1665 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001666
1667 return 0;
1668}
1669
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001670static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001671{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001672 return __send_changing_extent_only(ci, get_num_discard_bios,
1673 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001674}
1675
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001676static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001677{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001678 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001679}
1680
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001681/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001682 * Select the correct strategy for processing a non-flush bio.
1683 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001684static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Mikulas Patockadba14162012-10-12 21:02:15 +01001686 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001687 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001688 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001690 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001691 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001692 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001693 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001694
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001695 ti = dm_table_find_target(ci->map, ci->sector);
1696 if (!dm_target_is_valid(ti))
1697 return -EIO;
1698
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001699 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001700
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001701 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001703 ci->sector += len;
1704 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001710 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001712static void __split_and_process_bio(struct mapped_device *md,
1713 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001716 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001718 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001719 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001720 return;
1721 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001722
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001723 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ci.io = alloc_io(md);
1726 ci.io->error = 0;
1727 atomic_set(&ci.io->io_count, 1);
1728 ci.io->bio = bio;
1729 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001730 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001731 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001733 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001734
Mike Snitzerb372d362010-09-08 18:07:01 +02001735 if (bio->bi_rw & REQ_FLUSH) {
1736 ci.bio = &ci.md->flush_bio;
1737 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001738 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001739 /* dec_pending submits any data associated with flush */
1740 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001741 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001742 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001743 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001744 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001748 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750/*-----------------------------------------------------------------
1751 * CRUD END
1752 *---------------------------------------------------------------*/
1753
1754/*
1755 * The request function that just remaps the bio built up by
1756 * dm_merge_bvec.
1757 */
Jens Axboedece1632015-11-05 10:41:16 -07001758static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Kevin Corry12f03a42006-02-01 03:04:52 -08001760 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001762 int srcu_idx;
1763 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001765 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Gu Zheng18c0b222014-11-24 11:05:26 +08001767 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001768
Tejun Heo6a8736d2010-09-08 18:07:00 +02001769 /* if we're suspended, we have to queue this io for later */
1770 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001771 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Tejun Heo6a8736d2010-09-08 18:07:00 +02001773 if (bio_rw(bio) != READA)
1774 queue_io(md, bio);
1775 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001776 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001777 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001780 __split_and_process_bio(md, map, bio);
1781 dm_put_live_table(md, srcu_idx);
Jens Axboedece1632015-11-05 10:41:16 -07001782 return BLK_QC_T_NONE;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001783}
1784
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001785int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001786{
1787 return blk_queue_stackable(md->queue);
1788}
1789
Keith Busch466d89a2014-10-17 17:46:37 -06001790static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001791{
1792 int r;
1793
Keith Busch466d89a2014-10-17 17:46:37 -06001794 if (blk_queue_io_stat(clone->q))
1795 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001796
Keith Busch466d89a2014-10-17 17:46:37 -06001797 clone->start_time = jiffies;
1798 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001799 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001800 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001801 dm_complete_request(rq, r);
1802}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001803
Mike Snitzer78d8e582015-06-26 10:01:13 -04001804static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1805 void *data)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001806{
Mike Snitzer78d8e582015-06-26 10:01:13 -04001807 struct dm_rq_target_io *tio = data;
1808 struct dm_rq_clone_bio_info *info =
1809 container_of(bio, struct dm_rq_clone_bio_info, clone);
1810
1811 info->orig = bio_orig;
1812 info->tio = tio;
1813 bio->bi_end_io = end_clone_bio;
1814
1815 return 0;
1816}
1817
1818static int setup_clone(struct request *clone, struct request *rq,
1819 struct dm_rq_target_io *tio, gfp_t gfp_mask)
1820{
1821 int r;
1822
1823 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1824 dm_rq_bio_constructor, tio);
1825 if (r)
1826 return r;
1827
1828 clone->cmd = rq->cmd;
1829 clone->cmd_len = rq->cmd_len;
1830 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001831 clone->end_io = end_clone_request;
1832 clone->end_io_data = tio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001833
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001834 tio->clone = clone;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001835
1836 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001837}
1838
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001839static struct request *clone_rq(struct request *rq, struct mapped_device *md,
Keith Busch466d89a2014-10-17 17:46:37 -06001840 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001841{
Mike Snitzer02233342015-03-10 23:49:26 -04001842 /*
1843 * Do not allocate a clone if tio->clone was already set
1844 * (see: dm_mq_queue_rq).
1845 */
1846 bool alloc_clone = !tio->clone;
1847 struct request *clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001848
Mike Snitzer02233342015-03-10 23:49:26 -04001849 if (alloc_clone) {
1850 clone = alloc_clone_request(md, gfp_mask);
1851 if (!clone)
1852 return NULL;
1853 } else
1854 clone = tio->clone;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001855
1856 blk_rq_init(NULL, clone);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001857 if (setup_clone(clone, rq, tio, gfp_mask)) {
1858 /* -ENOMEM */
1859 if (alloc_clone)
1860 free_clone_request(md, clone);
1861 return NULL;
1862 }
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001863
1864 return clone;
1865}
1866
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001867static void map_tio_request(struct kthread_work *work);
1868
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001869static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1870 struct mapped_device *md)
1871{
1872 tio->md = md;
1873 tio->ti = NULL;
1874 tio->clone = NULL;
1875 tio->orig = rq;
1876 tio->error = 0;
1877 memset(&tio->info, 0, sizeof(tio->info));
Mike Snitzer02233342015-03-10 23:49:26 -04001878 if (md->kworker_task)
1879 init_kthread_work(&tio->work, map_tio_request);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001880}
1881
Keith Busch466d89a2014-10-17 17:46:37 -06001882static struct dm_rq_target_io *prep_tio(struct request *rq,
1883 struct mapped_device *md, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001884{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001885 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001886 int srcu_idx;
1887 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001888
1889 tio = alloc_rq_tio(md, gfp_mask);
1890 if (!tio)
1891 return NULL;
1892
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001893 init_tio(tio, rq, md);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001894
Mike Snitzere5863d92014-12-17 21:08:12 -05001895 table = dm_get_live_table(md, &srcu_idx);
1896 if (!dm_table_mq_request_based(table)) {
1897 if (!clone_rq(rq, md, tio, gfp_mask)) {
1898 dm_put_live_table(md, srcu_idx);
1899 free_rq_tio(tio);
1900 return NULL;
1901 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001902 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001903 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001904
Keith Busch466d89a2014-10-17 17:46:37 -06001905 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001906}
1907
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001908/*
1909 * Called with the queue lock held.
1910 */
1911static int dm_prep_fn(struct request_queue *q, struct request *rq)
1912{
1913 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001914 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001915
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001916 if (unlikely(rq->special)) {
1917 DMWARN("Already has something in rq->special.");
1918 return BLKPREP_KILL;
1919 }
1920
Keith Busch466d89a2014-10-17 17:46:37 -06001921 tio = prep_tio(rq, md, GFP_ATOMIC);
1922 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001923 return BLKPREP_DEFER;
1924
Keith Busch466d89a2014-10-17 17:46:37 -06001925 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001926 rq->cmd_flags |= REQ_DONTPREP;
1927
1928 return BLKPREP_OK;
1929}
1930
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001931/*
1932 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05001933 * 0 : the request has been processed
1934 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1935 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001936 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001937static int map_request(struct dm_rq_target_io *tio, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001938 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001939{
Mike Snitzere5863d92014-12-17 21:08:12 -05001940 int r;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001941 struct dm_target *ti = tio->ti;
Mike Snitzere5863d92014-12-17 21:08:12 -05001942 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001943
Mike Snitzere5863d92014-12-17 21:08:12 -05001944 if (tio->clone) {
1945 clone = tio->clone;
1946 r = ti->type->map_rq(ti, clone, &tio->info);
1947 } else {
1948 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1949 if (r < 0) {
1950 /* The target wants to complete the I/O */
1951 dm_kill_unmapped_request(rq, r);
1952 return r;
1953 }
Junichi Nomura3a140752015-05-27 04:22:07 +00001954 if (r != DM_MAPIO_REMAPPED)
1955 return r;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001956 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
1957 /* -ENOMEM */
1958 ti->type->release_clone_rq(clone);
1959 return DM_MAPIO_REQUEUE;
1960 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001961 }
1962
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001963 switch (r) {
1964 case DM_MAPIO_SUBMITTED:
1965 /* The target has taken the I/O to submit by itself later */
1966 break;
1967 case DM_MAPIO_REMAPPED:
1968 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001969 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06001970 blk_rq_pos(rq));
1971 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001972 break;
1973 case DM_MAPIO_REQUEUE:
1974 /* The target wants to requeue the I/O */
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001975 dm_requeue_original_request(md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001976 break;
1977 default:
1978 if (r > 0) {
1979 DMWARN("unimplemented target map return value: %d", r);
1980 BUG();
1981 }
1982
1983 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06001984 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05001985 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001986 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001987
Mike Snitzere5863d92014-12-17 21:08:12 -05001988 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001989}
1990
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001991static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001992{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001993 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05001994 struct request *rq = tio->orig;
1995 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001996
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05001997 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
Mike Snitzer2d76fff2015-04-29 12:07:12 -04001998 dm_requeue_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001999}
2000
Keith Busch466d89a2014-10-17 17:46:37 -06002001static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002002{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002003 if (!orig->q->mq_ops)
2004 blk_start_request(orig);
2005 else
2006 blk_mq_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06002007 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002008
Mike Snitzer0ce65792015-02-26 00:50:28 -05002009 if (md->seq_rq_merge_deadline_usecs) {
2010 md->last_rq_pos = rq_end_sector(orig);
2011 md->last_rq_rw = rq_data_dir(orig);
2012 md->last_rq_start_time = ktime_get();
2013 }
Mike Snitzerde3ec862015-02-24 21:58:21 -05002014
Mikulas Patockae262f342015-06-09 17:22:49 -04002015 if (unlikely(dm_stats_used(&md->stats))) {
2016 struct dm_rq_target_io *tio = tio_from_request(orig);
2017 tio->duration_jiffies = jiffies;
2018 tio->n_sectors = blk_rq_sectors(orig);
2019 dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
2020 tio->n_sectors, false, 0, &tio->stats_aux);
2021 }
2022
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002023 /*
2024 * Hold the md reference here for the in-flight I/O.
2025 * We can't rely on the reference count by device opener,
2026 * because the device may be closed during the request completion
2027 * when all bios are completed.
2028 * See the comment in rq_completed() too.
2029 */
2030 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002031}
2032
Mike Snitzer0ce65792015-02-26 00:50:28 -05002033#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
2034
2035ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
2036{
2037 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
2038}
2039
2040ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
2041 const char *buf, size_t count)
2042{
2043 unsigned deadline;
2044
Mike Snitzer17e149b2015-03-11 15:01:09 -04002045 if (!dm_request_based(md) || md->use_blk_mq)
Mike Snitzer0ce65792015-02-26 00:50:28 -05002046 return count;
2047
2048 if (kstrtouint(buf, 10, &deadline))
2049 return -EINVAL;
2050
2051 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
2052 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
2053
2054 md->seq_rq_merge_deadline_usecs = deadline;
2055
2056 return count;
2057}
2058
2059static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
2060{
2061 ktime_t kt_deadline;
2062
2063 if (!md->seq_rq_merge_deadline_usecs)
2064 return false;
2065
2066 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
2067 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
2068
2069 return !ktime_after(ktime_get(), kt_deadline);
2070}
2071
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002072/*
2073 * q->request_fn for request-based dm.
2074 * Called with the queue lock held.
2075 */
2076static void dm_request_fn(struct request_queue *q)
2077{
2078 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002079 int srcu_idx;
2080 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002081 struct dm_target *ti;
Keith Busch466d89a2014-10-17 17:46:37 -06002082 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002083 struct dm_rq_target_io *tio;
Tejun Heo29e40132010-09-08 18:07:00 +02002084 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002085
2086 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002087 * For suspend, check blk_queue_stopped() and increment
2088 * ->pending within a single queue_lock not to increment the
2089 * number of in-flight I/Os after the queue is stopped in
2090 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002091 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01002092 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002093 rq = blk_peek_request(q);
2094 if (!rq)
Mike Snitzer9d1deb82015-02-24 20:49:18 -05002095 goto out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002096
Tejun Heo29e40132010-09-08 18:07:00 +02002097 /* always use block 0 to find the target for flushes for now */
2098 pos = 0;
2099 if (!(rq->cmd_flags & REQ_FLUSH))
2100 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002101
Tejun Heo29e40132010-09-08 18:07:00 +02002102 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002103 if (!dm_target_is_valid(ti)) {
2104 /*
Keith Busch466d89a2014-10-17 17:46:37 -06002105 * Must perform setup, that rq_completed() requires,
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002106 * before calling dm_kill_unmapped_request
2107 */
2108 DMERR_LIMIT("request attempted access beyond the end of device");
Keith Busch466d89a2014-10-17 17:46:37 -06002109 dm_start_request(md, rq);
2110 dm_kill_unmapped_request(rq, -EIO);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01002111 continue;
2112 }
Tejun Heo29e40132010-09-08 18:07:00 +02002113
Mike Snitzer0ce65792015-02-26 00:50:28 -05002114 if (dm_request_peeked_before_merge_deadline(md) &&
2115 md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
Mike Snitzerde3ec862015-02-24 21:58:21 -05002116 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq))
2117 goto delay_and_out;
2118
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002119 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01002120 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002121
Keith Busch466d89a2014-10-17 17:46:37 -06002122 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002123
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002124 tio = tio_from_request(rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002125 /* Establish tio->ti before queuing work (map_tio_request) */
2126 tio->ti = ti;
2127 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00002128 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002129 }
2130
2131 goto out;
2132
Jens Axboe7eaceac2011-03-10 08:52:07 +01002133delay_and_out:
Mike Snitzerd548b342015-03-05 22:21:10 -05002134 blk_delay_queue(q, HZ / 100);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002135out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002136 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002137}
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139static int dm_any_congested(void *congested_data, int bdi_bits)
2140{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002141 int r = bdi_bits;
2142 struct mapped_device *md = congested_data;
2143 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002145 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002146 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002147 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002148 /*
2149 * Request-based dm cares about only own queue for
2150 * the query about congestion status of request_queue
2151 */
2152 if (dm_request_based(md))
Tejun Heo44522262015-05-22 17:13:26 -04002153 r = md->queue->backing_dev_info.wb.state &
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002154 bdi_bits;
2155 else
2156 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002157 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002158 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002159 }
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return r;
2162}
2163
2164/*-----------------------------------------------------------------
2165 * An IDR is used to keep track of allocated minor numbers.
2166 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002167static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002169 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002171 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
2174/*
2175 * See if the device with a specific minor # is free.
2176 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002177static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002179 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 if (minor >= (1 << MINORBITS))
2182 return -EINVAL;
2183
Tejun Heoc9d76be2013-02-27 17:04:26 -08002184 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002185 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Tejun Heoc9d76be2013-02-27 17:04:26 -08002187 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002189 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002190 idr_preload_end();
2191 if (r < 0)
2192 return r == -ENOSPC ? -EBUSY : r;
2193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002196static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002198 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Tejun Heoc9d76be2013-02-27 17:04:26 -08002200 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002201 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Tejun Heoc9d76be2013-02-27 17:04:26 -08002203 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002205 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002206 idr_preload_end();
2207 if (r < 0)
2208 return r;
2209 *minor = r;
2210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002213static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Mikulas Patocka53d59142009-04-02 19:55:37 +01002215static void dm_wq_work(struct work_struct *work);
2216
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002217static void dm_init_md_queue(struct mapped_device *md)
2218{
2219 /*
2220 * Request-based dm devices cannot be stacked on top of bio-based dm
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002221 * devices. The type of this dm device may not have been decided yet.
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002222 * The type is decided at the first table loading time.
2223 * To prevent problematic device stacking, clear the queue flag
2224 * for request stacking support until then.
2225 *
2226 * This queue is new, so no concurrency on the queue_flags.
2227 */
2228 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
Mikulas Patockaad5f4982015-10-27 19:06:55 -04002229
2230 /*
2231 * Initialize data that will only be used by a non-blk-mq DM queue
2232 * - must do so here (in alloc_dev callchain) before queue is used
2233 */
2234 md->queue->queuedata = md;
2235 md->queue->backing_dev_info.congested_data = md;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002236}
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002237
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002238static void dm_init_old_md_queue(struct mapped_device *md)
2239{
Mike Snitzer17e149b2015-03-11 15:01:09 -04002240 md->use_blk_mq = false;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002241 dm_init_md_queue(md);
2242
2243 /*
2244 * Initialize aspects of queue that aren't relevant for blk-mq
2245 */
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002246 md->queue->backing_dev_info.congested_fn = dm_any_congested;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002247 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002248}
2249
Mike Snitzer0f209722015-04-28 11:50:29 -04002250static void cleanup_mapped_device(struct mapped_device *md)
2251{
Mike Snitzer0f209722015-04-28 11:50:29 -04002252 if (md->wq)
2253 destroy_workqueue(md->wq);
2254 if (md->kworker_task)
2255 kthread_stop(md->kworker_task);
Julia Lawall6f659852015-09-13 14:15:05 +02002256 mempool_destroy(md->io_pool);
2257 mempool_destroy(md->rq_pool);
Mike Snitzer0f209722015-04-28 11:50:29 -04002258 if (md->bs)
2259 bioset_free(md->bs);
2260
Mikulas Patockab06075a2015-07-10 17:21:43 -04002261 cleanup_srcu_struct(&md->io_barrier);
2262
Mike Snitzer0f209722015-04-28 11:50:29 -04002263 if (md->disk) {
2264 spin_lock(&_minor_lock);
2265 md->disk->private_data = NULL;
2266 spin_unlock(&_minor_lock);
Mike Snitzer0f209722015-04-28 11:50:29 -04002267 del_gendisk(md->disk);
2268 put_disk(md->disk);
2269 }
2270
2271 if (md->queue)
2272 blk_cleanup_queue(md->queue);
2273
2274 if (md->bdev) {
2275 bdput(md->bdev);
2276 md->bdev = NULL;
2277 }
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280/*
2281 * Allocate and initialise a blank device with a given minor.
2282 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002283static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002286 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002287 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (!md) {
2290 DMWARN("unable to allocate device, out of memory.");
2291 return NULL;
2292 }
2293
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002294 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002295 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002298 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002299 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002300 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002301 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002303 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002305 r = init_srcu_struct(&md->io_barrier);
2306 if (r < 0)
2307 goto bad_io_barrier;
2308
Mike Snitzer17e149b2015-03-11 15:01:09 -04002309 md->use_blk_mq = use_blk_mq;
Mike Snitzera5664da2010-08-12 04:14:01 +01002310 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002311 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002312 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002313 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002314 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002316 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002318 atomic_set(&md->uevent_seq, 0);
2319 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002320 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002321 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002323 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (!md->queue)
Mike Snitzer0f209722015-04-28 11:50:29 -04002325 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002327 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 md->disk = alloc_disk(1);
2330 if (!md->disk)
Mike Snitzer0f209722015-04-28 11:50:29 -04002331 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002333 atomic_set(&md->pending[0], 0);
2334 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002335 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002336 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002337 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002338 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002339 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 md->disk->major = _major;
2342 md->disk->first_minor = minor;
2343 md->disk->fops = &dm_blk_dops;
2344 md->disk->queue = md->queue;
2345 md->disk->private_data = md;
2346 sprintf(md->disk->disk_name, "dm-%d", minor);
2347 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002348 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Tejun Heo670368a2013-07-30 08:40:21 -04002350 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002351 if (!md->wq)
Mike Snitzer0f209722015-04-28 11:50:29 -04002352 goto bad;
Milan Broz304f3f62008-02-08 02:11:17 +00002353
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002354 md->bdev = bdget_disk(md->disk, 0);
2355 if (!md->bdev)
Mike Snitzer0f209722015-04-28 11:50:29 -04002356 goto bad;
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002357
Tejun Heo6a8736d2010-09-08 18:07:00 +02002358 bio_init(&md->flush_bio);
2359 md->flush_bio.bi_bdev = md->bdev;
2360 md->flush_bio.bi_rw = WRITE_FLUSH;
2361
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002362 dm_stats_init(&md->stats);
2363
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002364 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002365 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002366 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002367 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002368
2369 BUG_ON(old_md != MINOR_ALLOCED);
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return md;
2372
Mike Snitzer0f209722015-04-28 11:50:29 -04002373bad:
2374 cleanup_mapped_device(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002375bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002377bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002378 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002379bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 kfree(md);
2381 return NULL;
2382}
2383
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002384static void unlock_fs(struct mapped_device *md);
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386static void free_dev(struct mapped_device *md)
2387{
Tejun Heof331c022008-09-03 09:01:48 +02002388 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002389
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002390 unlock_fs(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002391
Mike Snitzer0f209722015-04-28 11:50:29 -04002392 cleanup_mapped_device(md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04002393 if (md->use_blk_mq)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002394 blk_mq_free_tag_set(&md->tag_set);
Mike Snitzer0f209722015-04-28 11:50:29 -04002395
2396 free_table_devices(&md->table_devices);
2397 dm_stats_cleanup(&md->stats);
Mike Snitzer63a4f062015-03-23 17:01:43 -04002398 free_minor(minor);
2399
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002400 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 kfree(md);
2402}
2403
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002404static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2405{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002406 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002407
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002408 if (md->bs) {
2409 /* The md already has necessary mempools. */
2410 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002411 /*
2412 * Reload bioset because front_pad may have changed
2413 * because a different table was loaded.
2414 */
2415 bioset_free(md->bs);
2416 md->bs = p->bs;
2417 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002418 }
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002419 /*
2420 * There's no need to reload with request-based dm
2421 * because the size of front_pad doesn't change.
2422 * Note for future: If you are to reload bioset,
2423 * prep-ed requests in the queue may refer
2424 * to bio from the old bioset, so you must walk
2425 * through the queue to unprep.
2426 */
2427 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002428 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002429
Mike Snitzercbc4e3c2015-04-27 16:37:50 -04002430 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2431
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002432 md->io_pool = p->io_pool;
2433 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002434 md->rq_pool = p->rq_pool;
2435 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002436 md->bs = p->bs;
2437 p->bs = NULL;
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002438
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002439out:
Mike Snitzer02233342015-03-10 23:49:26 -04002440 /* mempool bind completed, no longer need any mempools in the table */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002441 dm_table_free_md_mempools(t);
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444/*
2445 * Bind a table to the device.
2446 */
2447static void event_callback(void *context)
2448{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002449 unsigned long flags;
2450 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct mapped_device *md = (struct mapped_device *) context;
2452
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002453 spin_lock_irqsave(&md->uevent_lock, flags);
2454 list_splice_init(&md->uevent_list, &uevents);
2455 spin_unlock_irqrestore(&md->uevent_lock, flags);
2456
Tejun Heoed9e1982008-08-25 19:56:05 +09002457 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 atomic_inc(&md->event_nr);
2460 wake_up(&md->eventq);
2461}
2462
Mike Snitzerc2176492011-01-13 19:53:46 +00002463/*
2464 * Protected by md->suspend_lock obtained by dm_swap_table().
2465 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002466static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002468 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002470 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002473/*
2474 * Returns old map, which caller must destroy.
2475 */
2476static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2477 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002479 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002480 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 sector_t size;
2482
2483 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002484
2485 /*
2486 * Wipe any geometry if the size of the table changed.
2487 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002488 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002489 memset(&md->geometry, 0, sizeof(md->geometry));
2490
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002491 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002493 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002494
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002495 /*
2496 * The queue hasn't been stopped yet, if the old table type wasn't
2497 * for request-based during suspension. So stop it to prevent
2498 * I/O mapping before resume.
2499 * This must be done before setting the queue restrictions,
2500 * because request-based dm may be run just after the setting.
2501 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002502 if (dm_table_request_based(t))
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002503 stop_queue(q);
2504
2505 __bind_mempools(md, t);
2506
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002507 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002508 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002509 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2510
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002511 dm_table_set_restrictions(t, q, limits);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002512 if (old_map)
2513 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002514
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002515 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
Alasdair G Kergona7940152009-12-10 23:52:23 +00002518/*
2519 * Returns unbound table for the caller to free.
2520 */
2521static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002523 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002526 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302529 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002530 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002531
2532 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
2535/*
2536 * Constructor for a new device.
2537 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002538int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
2540 struct mapped_device *md;
2541
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002542 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (!md)
2544 return -ENXIO;
2545
Milan Broz784aae72009-01-06 03:05:12 +00002546 dm_sysfs_init(md);
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 *result = md;
2549 return 0;
2550}
2551
Mike Snitzera5664da2010-08-12 04:14:01 +01002552/*
2553 * Functions to manage md->type.
2554 * All are required to hold md->type_lock.
2555 */
2556void dm_lock_md_type(struct mapped_device *md)
2557{
2558 mutex_lock(&md->type_lock);
2559}
2560
2561void dm_unlock_md_type(struct mapped_device *md)
2562{
2563 mutex_unlock(&md->type_lock);
2564}
2565
2566void dm_set_md_type(struct mapped_device *md, unsigned type)
2567{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002568 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002569 md->type = type;
2570}
2571
2572unsigned dm_get_md_type(struct mapped_device *md)
2573{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002574 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002575 return md->type;
2576}
2577
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002578struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2579{
2580 return md->immutable_target_type;
2581}
2582
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002583/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002584 * The queue_limits are only valid as long as you have a reference
2585 * count on 'md'.
2586 */
2587struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2588{
2589 BUG_ON(!atomic_read(&md->holders));
2590 return &md->queue->limits;
2591}
2592EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2593
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002594static void init_rq_based_worker_thread(struct mapped_device *md)
2595{
2596 /* Initialize the request-based DM worker thread */
2597 init_kthread_worker(&md->kworker);
2598 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2599 "kdmwork-%s", dm_device_name(md));
2600}
2601
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002602/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002603 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2604 */
2605static int dm_init_request_based_queue(struct mapped_device *md)
2606{
2607 struct request_queue *q = NULL;
2608
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002609 /* Fully initialize the queue */
2610 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2611 if (!q)
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002612 return -EINVAL;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002613
Mike Snitzer0ce65792015-02-26 00:50:28 -05002614 /* disable dm_request_fn's merge heuristic by default */
2615 md->seq_rq_merge_deadline_usecs = 0;
2616
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002617 md->queue = q;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002618 dm_init_old_md_queue(md);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002619 blk_queue_softirq_done(md->queue, dm_softirq_done);
2620 blk_queue_prep_rq(md->queue, dm_prep_fn);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002621
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002622 init_rq_based_worker_thread(md);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002623
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002624 elv_register_queue(md->queue);
2625
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002626 return 0;
2627}
2628
2629static int dm_mq_init_request(void *data, struct request *rq,
2630 unsigned int hctx_idx, unsigned int request_idx,
2631 unsigned int numa_node)
2632{
2633 struct mapped_device *md = data;
2634 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2635
2636 /*
2637 * Must initialize md member of tio, otherwise it won't
2638 * be available in dm_mq_queue_rq.
2639 */
2640 tio->md = md;
2641
2642 return 0;
2643}
2644
2645static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2646 const struct blk_mq_queue_data *bd)
2647{
2648 struct request *rq = bd->rq;
2649 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2650 struct mapped_device *md = tio->md;
2651 int srcu_idx;
2652 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2653 struct dm_target *ti;
2654 sector_t pos;
2655
2656 /* always use block 0 to find the target for flushes for now */
2657 pos = 0;
2658 if (!(rq->cmd_flags & REQ_FLUSH))
2659 pos = blk_rq_pos(rq);
2660
2661 ti = dm_table_find_target(map, pos);
2662 if (!dm_target_is_valid(ti)) {
2663 dm_put_live_table(md, srcu_idx);
2664 DMERR_LIMIT("request attempted access beyond the end of device");
2665 /*
2666 * Must perform setup, that rq_completed() requires,
2667 * before returning BLK_MQ_RQ_QUEUE_ERROR
2668 */
2669 dm_start_request(md, rq);
2670 return BLK_MQ_RQ_QUEUE_ERROR;
2671 }
2672 dm_put_live_table(md, srcu_idx);
2673
2674 if (ti->type->busy && ti->type->busy(ti))
2675 return BLK_MQ_RQ_QUEUE_BUSY;
2676
2677 dm_start_request(md, rq);
2678
2679 /* Init tio using md established in .init_request */
2680 init_tio(tio, rq, md);
2681
Mike Snitzer02233342015-03-10 23:49:26 -04002682 /*
2683 * Establish tio->ti before queuing work (map_tio_request)
2684 * or making direct call to map_request().
2685 */
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002686 tio->ti = ti;
Mike Snitzer02233342015-03-10 23:49:26 -04002687
2688 /* Clone the request if underlying devices aren't blk-mq */
2689 if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) {
2690 /* clone request is allocated at the end of the pdu */
2691 tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io);
Mike Snitzer45714fb2015-05-27 15:25:27 -04002692 (void) clone_rq(rq, md, tio, GFP_ATOMIC);
Mike Snitzer02233342015-03-10 23:49:26 -04002693 queue_kthread_work(&md->kworker, &tio->work);
2694 } else {
2695 /* Direct call is fine since .queue_rq allows allocations */
Mike Snitzer45714fb2015-05-27 15:25:27 -04002696 if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
2697 /* Undo dm_start_request() before requeuing */
Mikulas Patockae262f342015-06-09 17:22:49 -04002698 rq_end_stats(md, rq);
Mike Snitzer45714fb2015-05-27 15:25:27 -04002699 rq_completed(md, rq_data_dir(rq), false);
2700 return BLK_MQ_RQ_QUEUE_BUSY;
2701 }
Mike Snitzer02233342015-03-10 23:49:26 -04002702 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002703
2704 return BLK_MQ_RQ_QUEUE_OK;
2705}
2706
2707static struct blk_mq_ops dm_mq_ops = {
2708 .queue_rq = dm_mq_queue_rq,
2709 .map_queue = blk_mq_map_queue,
2710 .complete = dm_softirq_done,
2711 .init_request = dm_mq_init_request,
2712};
2713
2714static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
2715{
Mike Snitzer02233342015-03-10 23:49:26 -04002716 unsigned md_type = dm_get_md_type(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002717 struct request_queue *q;
2718 int err;
2719
2720 memset(&md->tag_set, 0, sizeof(md->tag_set));
2721 md->tag_set.ops = &dm_mq_ops;
2722 md->tag_set.queue_depth = BLKDEV_MAX_RQ;
2723 md->tag_set.numa_node = NUMA_NO_NODE;
2724 md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2725 md->tag_set.nr_hw_queues = 1;
Mike Snitzer02233342015-03-10 23:49:26 -04002726 if (md_type == DM_TYPE_REQUEST_BASED) {
2727 /* make the memory for non-blk-mq clone part of the pdu */
2728 md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request);
2729 } else
2730 md->tag_set.cmd_size = sizeof(struct dm_rq_target_io);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002731 md->tag_set.driver_data = md;
2732
2733 err = blk_mq_alloc_tag_set(&md->tag_set);
2734 if (err)
2735 return err;
2736
2737 q = blk_mq_init_allocated_queue(&md->tag_set, md->queue);
2738 if (IS_ERR(q)) {
2739 err = PTR_ERR(q);
2740 goto out_tag_set;
2741 }
2742 md->queue = q;
2743 dm_init_md_queue(md);
2744
2745 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
2746 blk_mq_register_disk(md->disk);
2747
Mike Snitzer02233342015-03-10 23:49:26 -04002748 if (md_type == DM_TYPE_REQUEST_BASED)
2749 init_rq_based_worker_thread(md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002750
2751 return 0;
2752
2753out_tag_set:
2754 blk_mq_free_tag_set(&md->tag_set);
2755 return err;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002756}
2757
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04002758static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2759{
2760 if (type == DM_TYPE_BIO_BASED)
2761 return type;
2762
2763 return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2764}
2765
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002766/*
2767 * Setup the DM device's queue based on md's type
2768 */
2769int dm_setup_md_queue(struct mapped_device *md)
2770{
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002771 int r;
Mike Snitzer17e149b2015-03-11 15:01:09 -04002772 unsigned md_type = filter_md_type(dm_get_md_type(md), md);
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002773
2774 switch (md_type) {
2775 case DM_TYPE_REQUEST_BASED:
2776 r = dm_init_request_based_queue(md);
2777 if (r) {
Mike Snitzerff36ab32015-02-23 17:56:37 -05002778 DMWARN("Cannot initialize queue for request-based mapped device");
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002779 return r;
Mike Snitzerff36ab32015-02-23 17:56:37 -05002780 }
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002781 break;
2782 case DM_TYPE_MQ_REQUEST_BASED:
2783 r = dm_init_request_based_blk_mq_queue(md);
2784 if (r) {
2785 DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
2786 return r;
2787 }
2788 break;
2789 case DM_TYPE_BIO_BASED:
2790 dm_init_old_md_queue(md);
Mike Snitzerff36ab32015-02-23 17:56:37 -05002791 blk_queue_make_request(md->queue, dm_make_request);
Mikulas Patockadbba42d2015-10-21 16:34:20 -04002792 /*
2793 * DM handles splitting bios as needed. Free the bio_split bioset
2794 * since it won't be used (saves 1 process per bio-based DM device).
2795 */
2796 bioset_free(md->queue->bio_split);
2797 md->queue->bio_split = NULL;
Mike Snitzerbfebd1c2015-03-08 00:51:47 -05002798 break;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002799 }
2800
2801 return 0;
2802}
2803
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002804struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
2806 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 unsigned minor = MINOR(dev);
2808
2809 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2810 return NULL;
2811
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002812 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002815 if (md) {
2816 if ((md == MINOR_ALLOCED ||
2817 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2818 dm_deleting_md(md) ||
2819 test_bit(DMF_FREEING, &md->flags))) {
2820 md = NULL;
2821 goto out;
2822 }
2823 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002826out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002827 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
David Teigland637842c2006-01-06 00:20:00 -08002829 return md;
2830}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002831EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002832
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002833void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002834{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002835 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
2838void dm_set_mdptr(struct mapped_device *md, void *ptr)
2839{
2840 md->interface_ptr = ptr;
2841}
2842
2843void dm_get(struct mapped_device *md)
2844{
2845 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002846 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002849int dm_hold(struct mapped_device *md)
2850{
2851 spin_lock(&_minor_lock);
2852 if (test_bit(DMF_FREEING, &md->flags)) {
2853 spin_unlock(&_minor_lock);
2854 return -EBUSY;
2855 }
2856 dm_get(md);
2857 spin_unlock(&_minor_lock);
2858 return 0;
2859}
2860EXPORT_SYMBOL_GPL(dm_hold);
2861
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002862const char *dm_device_name(struct mapped_device *md)
2863{
2864 return md->name;
2865}
2866EXPORT_SYMBOL_GPL(dm_device_name);
2867
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002868static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002870 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002871 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002873 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002874
Mike Snitzer63a4f062015-03-23 17:01:43 -04002875 spin_lock(&_minor_lock);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002876 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2877 set_bit(DMF_FREEING, &md->flags);
2878 spin_unlock(&_minor_lock);
2879
Mike Snitzer02233342015-03-10 23:49:26 -04002880 if (dm_request_based(md) && md->kworker_task)
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002881 flush_kthread_worker(&md->kworker);
2882
Mikulas Patockaab7c7bb2015-02-27 14:04:27 -05002883 /*
2884 * Take suspend_lock so that presuspend and postsuspend methods
2885 * do not race with internal suspend.
2886 */
2887 mutex_lock(&md->suspend_lock);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002888 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002889 if (!dm_suspended_md(md)) {
2890 dm_table_presuspend_targets(map);
2891 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002893 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2894 dm_put_live_table(md, srcu_idx);
Junichi Nomura2a708cf2015-10-01 08:31:51 +00002895 mutex_unlock(&md->suspend_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002896
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002897 /*
2898 * Rare, but there may be I/O requests still going to complete,
2899 * for example. Wait for all references to disappear.
2900 * No one should increment the reference count of the mapped_device,
2901 * after the mapped_device state becomes DMF_FREEING.
2902 */
2903 if (wait)
2904 while (atomic_read(&md->holders))
2905 msleep(1);
2906 else if (atomic_read(&md->holders))
2907 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2908 dm_device_name(md), atomic_read(&md->holders));
2909
2910 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002911 dm_table_destroy(__unbind(md));
2912 free_dev(md);
2913}
2914
2915void dm_destroy(struct mapped_device *md)
2916{
2917 __dm_destroy(md, true);
2918}
2919
2920void dm_destroy_immediate(struct mapped_device *md)
2921{
2922 __dm_destroy(md, false);
2923}
2924
2925void dm_put(struct mapped_device *md)
2926{
2927 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
Edward Goggin79eb8852007-05-09 02:32:56 -07002929EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Mikulas Patocka401600d2009-04-02 19:55:38 +01002931static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002932{
2933 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002934 DECLARE_WAITQUEUE(wait, current);
2935
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002936 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002937
2938 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002939 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002940
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002941 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002942 break;
2943
Mikulas Patocka401600d2009-04-02 19:55:38 +01002944 if (interruptible == TASK_INTERRUPTIBLE &&
2945 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002946 r = -EINTR;
2947 break;
2948 }
2949
2950 io_schedule();
2951 }
2952 set_current_state(TASK_RUNNING);
2953
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002954 remove_wait_queue(&md->wait, &wait);
2955
Milan Broz46125c12008-02-08 02:10:30 +00002956 return r;
2957}
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959/*
2960 * Process the deferred bios
2961 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002962static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Mikulas Patockaef208582009-04-02 19:55:38 +01002964 struct mapped_device *md = container_of(work, struct mapped_device,
2965 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002966 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002967 int srcu_idx;
2968 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002970 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002971
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002972 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002973 spin_lock_irq(&md->deferred_lock);
2974 c = bio_list_pop(&md->deferred);
2975 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002976
Tejun Heo6a8736d2010-09-08 18:07:00 +02002977 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002978 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002979
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002980 if (dm_request_based(md))
2981 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002982 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002983 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002984 }
Milan Broz73d410c2008-02-08 02:10:25 +00002985
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002986 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002989static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002990{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002991 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002992 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002993 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002994}
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002997 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002999struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Mike Christie87eb5b22013-03-01 22:45:48 +00003001 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003002 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003003 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Daniel Walkere61290a2008-02-08 02:10:08 +00003005 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003008 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Mike Snitzer3ae70652012-09-26 23:45:45 +01003011 /*
3012 * If the new table has no data devices, retain the existing limits.
3013 * This helps multipath with queue_if_no_path if all paths disappear,
3014 * then new I/O is queued based on these limits, and then some paths
3015 * reappear.
3016 */
3017 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003018 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003019 if (live_map)
3020 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01003021 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01003022 }
3023
Mike Christie87eb5b22013-03-01 22:45:48 +00003024 if (!live_map) {
3025 r = dm_calculate_queue_limits(table, &limits);
3026 if (r) {
3027 map = ERR_PTR(r);
3028 goto out;
3029 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003030 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01003031
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003032 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07003034out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003035 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00003036 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
3039/*
3040 * Functions to lock and unlock any filesystem running on the
3041 * device.
3042 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003043static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003045 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003048
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003049 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003050 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003051 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08003052 md->frozen_sb = NULL;
3053 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07003054 }
3055
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003056 set_bit(DMF_FROZEN, &md->flags);
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return 0;
3059}
3060
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003061static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003063 if (!test_bit(DMF_FROZEN, &md->flags))
3064 return;
3065
Mikulas Patockadb8fef42009-06-22 10:12:15 +01003066 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003068 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
3071/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04003072 * If __dm_suspend returns 0, the device is completely quiescent
3073 * now. There is no request-processing activity. All new requests
3074 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003075 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04003076 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003077 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003078static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3079 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003081 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3082 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3083 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003085 /*
3086 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
3087 * This flag is cleared before dm_suspend returns.
3088 */
3089 if (noflush)
3090 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
3091
Mike Snitzerd67ee212014-10-28 20:13:31 -04003092 /*
3093 * This gets reverted if there's an error later and the targets
3094 * provide the .presuspend_undo hook.
3095 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003096 dm_table_presuspend_targets(map);
3097
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003098 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003099 * Flush I/O to the device.
3100 * Any I/O submitted after lock_fs() may not be flushed.
3101 * noflush takes precedence over do_lockfs.
3102 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01003103 */
3104 if (!noflush && do_lockfs) {
3105 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003106 if (r) {
3107 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003108 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04003109 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08003110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003113 * Here we must make sure that no processes are submitting requests
3114 * to target drivers i.e. no one may be executing
3115 * __split_and_process_bio. This is called from dm_request and
3116 * dm_wq_work.
3117 *
3118 * To get all processes out of __split_and_process_bio in dm_request,
3119 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02003120 * __split_and_process_bio from dm_request and quiesce the thread
3121 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
3122 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01003124 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003125 if (map)
3126 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003128 /*
Tejun Heo29e40132010-09-08 18:07:00 +02003129 * Stop md->queue before flushing md->wq in case request-based
3130 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003131 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003132 if (dm_request_based(md)) {
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003133 stop_queue(md->queue);
Mike Snitzer02233342015-03-10 23:49:26 -04003134 if (md->kworker_task)
3135 flush_kthread_worker(&md->kworker);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06003136 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003137
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00003138 flush_workqueue(md->wq);
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003141 * At this point no more requests are entering target request routines.
3142 * We call dm_wait_for_completion to wait for all existing requests
3143 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04003145 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Milan Broz6d6f10d2008-02-08 02:10:22 +00003147 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01003148 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01003149 if (map)
3150 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00003153 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003154 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00003155
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003156 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00003157 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003158
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003159 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04003160 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003161 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003162 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003163
Mike Snitzerffcc3932014-10-28 18:34:52 -04003164 return r;
3165}
3166
3167/*
3168 * We need to be able to change a mapping table under a mounted
3169 * filesystem. For example we might want to move some data in
3170 * the background. Before the table can be swapped with
3171 * dm_bind_table, dm_suspend must be called to flush any in
3172 * flight bios and ensure that any further io gets deferred.
3173 */
3174/*
3175 * Suspend mechanism in request-based dm.
3176 *
3177 * 1. Flush all I/Os by lock_fs() if needed.
3178 * 2. Stop dispatching any I/O by stopping the request_queue.
3179 * 3. Wait for all in-flight I/Os to be completed or requeued.
3180 *
3181 * To abort suspend, start the request_queue.
3182 */
3183int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3184{
3185 struct dm_table *map = NULL;
3186 int r = 0;
3187
3188retry:
3189 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3190
3191 if (dm_suspended_md(md)) {
3192 r = -EINVAL;
3193 goto out_unlock;
3194 }
3195
3196 if (dm_suspended_internally_md(md)) {
3197 /* already internally suspended, wait for internal resume */
3198 mutex_unlock(&md->suspend_lock);
3199 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3200 if (r)
3201 return r;
3202 goto retry;
3203 }
3204
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003205 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003206
3207 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3208 if (r)
3209 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01003210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 set_bit(DMF_SUSPENDED, &md->flags);
3212
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00003213 dm_table_postsuspend_targets(map);
3214
Alasdair G Kergond2874832006-11-08 17:44:43 -08003215out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00003216 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003217 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Mike Snitzerffcc3932014-10-28 18:34:52 -04003220static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003222 if (map) {
3223 int r = dm_table_resume_targets(map);
3224 if (r)
3225 return r;
3226 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003227
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01003228 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003229
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01003230 /*
3231 * Flushing deferred I/Os must be done after targets are resumed
3232 * so that mapping of targets can work correctly.
3233 * Request-based dm is queueing the deferred I/Os in its request_queue.
3234 */
3235 if (dm_request_based(md))
3236 start_queue(md->queue);
3237
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003238 unlock_fs(md);
3239
Mike Snitzerffcc3932014-10-28 18:34:52 -04003240 return 0;
3241}
3242
3243int dm_resume(struct mapped_device *md)
3244{
3245 int r = -EINVAL;
3246 struct dm_table *map = NULL;
3247
3248retry:
3249 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3250
3251 if (!dm_suspended_md(md))
3252 goto out;
3253
3254 if (dm_suspended_internally_md(md)) {
3255 /* already internally suspended, wait for internal resume */
3256 mutex_unlock(&md->suspend_lock);
3257 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3258 if (r)
3259 return r;
3260 goto retry;
3261 }
3262
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003263 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003264 if (!map || !dm_table_get_size(map))
3265 goto out;
3266
3267 r = __dm_resume(md, map);
3268 if (r)
3269 goto out;
3270
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003271 clear_bit(DMF_SUSPENDED, &md->flags);
3272
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003273 r = 0;
3274out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003275 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003276
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003277 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003280/*
3281 * Internal suspend/resume works like userspace-driven suspend. It waits
3282 * until all bios finish and prevents issuing new bios to the target drivers.
3283 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003284 */
3285
Mike Snitzerffcc3932014-10-28 18:34:52 -04003286static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3287{
3288 struct dm_table *map = NULL;
3289
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003290 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003291 return; /* nested internal suspend */
3292
3293 if (dm_suspended_md(md)) {
3294 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3295 return; /* nest suspend */
3296 }
3297
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003298 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003299
3300 /*
3301 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3302 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3303 * would require changing .presuspend to return an error -- avoid this
3304 * until there is a need for more elaborate variants of internal suspend.
3305 */
3306 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3307
3308 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3309
3310 dm_table_postsuspend_targets(map);
3311}
3312
3313static void __dm_internal_resume(struct mapped_device *md)
3314{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003315 BUG_ON(!md->internal_suspend_count);
3316
3317 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003318 return; /* resume from nested internal suspend */
3319
3320 if (dm_suspended_md(md))
3321 goto done; /* resume from nested suspend */
3322
3323 /*
3324 * NOTE: existing callers don't need to call dm_table_resume_targets
3325 * (which may fail -- so best to avoid it for now by passing NULL map)
3326 */
3327 (void) __dm_resume(md, NULL);
3328
3329done:
3330 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3331 smp_mb__after_atomic();
3332 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3333}
3334
3335void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003336{
3337 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003338 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3339 mutex_unlock(&md->suspend_lock);
3340}
3341EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3342
3343void dm_internal_resume(struct mapped_device *md)
3344{
3345 mutex_lock(&md->suspend_lock);
3346 __dm_internal_resume(md);
3347 mutex_unlock(&md->suspend_lock);
3348}
3349EXPORT_SYMBOL_GPL(dm_internal_resume);
3350
3351/*
3352 * Fast variants of internal suspend/resume hold md->suspend_lock,
3353 * which prevents interaction with userspace-driven suspend.
3354 */
3355
3356void dm_internal_suspend_fast(struct mapped_device *md)
3357{
3358 mutex_lock(&md->suspend_lock);
3359 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003360 return;
3361
3362 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3363 synchronize_srcu(&md->io_barrier);
3364 flush_workqueue(md->wq);
3365 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3366}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003367EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003368
Mike Snitzerffcc3932014-10-28 18:34:52 -04003369void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003370{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003371 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003372 goto done;
3373
3374 dm_queue_flush(md);
3375
3376done:
3377 mutex_unlock(&md->suspend_lock);
3378}
Mikulas Patockab735fed2015-02-26 11:40:35 -05003379EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381/*-----------------------------------------------------------------
3382 * Event notification.
3383 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003384int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003385 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003386{
Milan Broz60935eb2009-06-22 10:12:30 +01003387 char udev_cookie[DM_COOKIE_LENGTH];
3388 char *envp[] = { udev_cookie, NULL };
3389
3390 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003391 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003392 else {
3393 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3394 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003395 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3396 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003397 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003398}
3399
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003400uint32_t dm_next_uevent_seq(struct mapped_device *md)
3401{
3402 return atomic_add_return(1, &md->uevent_seq);
3403}
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405uint32_t dm_get_event_nr(struct mapped_device *md)
3406{
3407 return atomic_read(&md->event_nr);
3408}
3409
3410int dm_wait_event(struct mapped_device *md, int event_nr)
3411{
3412 return wait_event_interruptible(md->eventq,
3413 (event_nr != atomic_read(&md->event_nr)));
3414}
3415
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003416void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3417{
3418 unsigned long flags;
3419
3420 spin_lock_irqsave(&md->uevent_lock, flags);
3421 list_add(elist, &md->uevent_list);
3422 spin_unlock_irqrestore(&md->uevent_lock, flags);
3423}
3424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425/*
3426 * The gendisk is only valid as long as you have a reference
3427 * count on 'md'.
3428 */
3429struct gendisk *dm_disk(struct mapped_device *md)
3430{
3431 return md->disk;
3432}
Sami Tolvanen65ff5b72015-03-18 15:52:14 +00003433EXPORT_SYMBOL_GPL(dm_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Milan Broz784aae72009-01-06 03:05:12 +00003435struct kobject *dm_kobject(struct mapped_device *md)
3436{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003437 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003438}
3439
Milan Broz784aae72009-01-06 03:05:12 +00003440struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3441{
3442 struct mapped_device *md;
3443
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003444 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003445
Milan Broz4d89b7b2009-06-22 10:12:11 +01003446 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003447 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003448 return NULL;
3449
Milan Broz784aae72009-01-06 03:05:12 +00003450 dm_get(md);
3451 return md;
3452}
3453
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003454int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
3456 return test_bit(DMF_SUSPENDED, &md->flags);
3457}
3458
Mike Snitzerffcc3932014-10-28 18:34:52 -04003459int dm_suspended_internally_md(struct mapped_device *md)
3460{
3461 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3462}
3463
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003464int dm_test_deferred_remove_flag(struct mapped_device *md)
3465{
3466 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3467}
3468
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003469int dm_suspended(struct dm_target *ti)
3470{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003471 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003472}
3473EXPORT_SYMBOL_GPL(dm_suspended);
3474
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003475int dm_noflush_suspending(struct dm_target *ti)
3476{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003477 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003478}
3479EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3480
Mike Snitzer78d8e582015-06-26 10:01:13 -04003481struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
3482 unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003483{
Mike Snitzer78d8e582015-06-26 10:01:13 -04003484 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3485 struct kmem_cache *cachep = NULL;
3486 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003487 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003488
3489 if (!pools)
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003490 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003491
Mike Snitzer78d8e582015-06-26 10:01:13 -04003492 type = filter_md_type(type, md);
Mike Snitzer17e149b2015-03-11 15:01:09 -04003493
Mike Snitzer78d8e582015-06-26 10:01:13 -04003494 switch (type) {
3495 case DM_TYPE_BIO_BASED:
3496 cachep = _io_cache;
3497 pool_size = dm_get_reserved_bio_based_ios();
3498 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
3499 break;
3500 case DM_TYPE_REQUEST_BASED:
3501 cachep = _rq_tio_cache;
3502 pool_size = dm_get_reserved_rq_based_ios();
3503 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3504 if (!pools->rq_pool)
3505 goto out;
3506 /* fall through to setup remaining rq-based pools */
3507 case DM_TYPE_MQ_REQUEST_BASED:
3508 if (!pool_size)
3509 pool_size = dm_get_reserved_rq_based_ios();
3510 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3511 /* per_bio_data_size is not used. See __bind_mempools(). */
3512 WARN_ON(per_bio_data_size != 0);
3513 break;
3514 default:
3515 BUG();
3516 }
3517
3518 if (cachep) {
3519 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3520 if (!pools->io_pool)
3521 goto out;
3522 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003523
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003524 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003525 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003526 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003527
Martin K. Petersena91a2782011-03-17 11:11:05 +01003528 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003529 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003530
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003531 return pools;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003532
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003533out:
3534 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003535
Mike Snitzer4e6e36c2015-06-26 09:42:57 -04003536 return NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003537}
3538
3539void dm_free_md_mempools(struct dm_md_mempools *pools)
3540{
3541 if (!pools)
3542 return;
3543
Julia Lawall6f659852015-09-13 14:15:05 +02003544 mempool_destroy(pools->io_pool);
3545 mempool_destroy(pools->rq_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003546
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003547 if (pools->bs)
3548 bioset_free(pools->bs);
3549
3550 kfree(pools);
3551}
3552
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003553static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
3554 u32 flags)
3555{
3556 struct mapped_device *md = bdev->bd_disk->private_data;
3557 const struct pr_ops *ops;
3558 struct dm_target *tgt;
3559 fmode_t mode;
3560 int srcu_idx, r;
3561
3562 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3563 if (r < 0)
3564 return r;
3565
3566 ops = bdev->bd_disk->fops->pr_ops;
3567 if (ops && ops->pr_register)
3568 r = ops->pr_register(bdev, old_key, new_key, flags);
3569 else
3570 r = -EOPNOTSUPP;
3571
3572 dm_put_live_table(md, srcu_idx);
3573 return r;
3574}
3575
3576static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
3577 u32 flags)
3578{
3579 struct mapped_device *md = bdev->bd_disk->private_data;
3580 const struct pr_ops *ops;
3581 struct dm_target *tgt;
3582 fmode_t mode;
3583 int srcu_idx, r;
3584
3585 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3586 if (r < 0)
3587 return r;
3588
3589 ops = bdev->bd_disk->fops->pr_ops;
3590 if (ops && ops->pr_reserve)
3591 r = ops->pr_reserve(bdev, key, type, flags);
3592 else
3593 r = -EOPNOTSUPP;
3594
3595 dm_put_live_table(md, srcu_idx);
3596 return r;
3597}
3598
3599static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3600{
3601 struct mapped_device *md = bdev->bd_disk->private_data;
3602 const struct pr_ops *ops;
3603 struct dm_target *tgt;
3604 fmode_t mode;
3605 int srcu_idx, r;
3606
3607 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3608 if (r < 0)
3609 return r;
3610
3611 ops = bdev->bd_disk->fops->pr_ops;
3612 if (ops && ops->pr_release)
3613 r = ops->pr_release(bdev, key, type);
3614 else
3615 r = -EOPNOTSUPP;
3616
3617 dm_put_live_table(md, srcu_idx);
3618 return r;
3619}
3620
3621static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
3622 enum pr_type type, bool abort)
3623{
3624 struct mapped_device *md = bdev->bd_disk->private_data;
3625 const struct pr_ops *ops;
3626 struct dm_target *tgt;
3627 fmode_t mode;
3628 int srcu_idx, r;
3629
3630 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3631 if (r < 0)
3632 return r;
3633
3634 ops = bdev->bd_disk->fops->pr_ops;
3635 if (ops && ops->pr_preempt)
3636 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3637 else
3638 r = -EOPNOTSUPP;
3639
3640 dm_put_live_table(md, srcu_idx);
3641 return r;
3642}
3643
3644static int dm_pr_clear(struct block_device *bdev, u64 key)
3645{
3646 struct mapped_device *md = bdev->bd_disk->private_data;
3647 const struct pr_ops *ops;
3648 struct dm_target *tgt;
3649 fmode_t mode;
3650 int srcu_idx, r;
3651
3652 r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
3653 if (r < 0)
3654 return r;
3655
3656 ops = bdev->bd_disk->fops->pr_ops;
3657 if (ops && ops->pr_clear)
3658 r = ops->pr_clear(bdev, key);
3659 else
3660 r = -EOPNOTSUPP;
3661
3662 dm_put_live_table(md, srcu_idx);
3663 return r;
3664}
3665
3666static const struct pr_ops dm_pr_ops = {
3667 .pr_register = dm_pr_register,
3668 .pr_reserve = dm_pr_reserve,
3669 .pr_release = dm_pr_release,
3670 .pr_preempt = dm_pr_preempt,
3671 .pr_clear = dm_pr_clear,
3672};
3673
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003674static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 .open = dm_blk_open,
3676 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003677 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003678 .getgeo = dm_blk_getgeo,
Christoph Hellwig71cdb692015-10-15 14:10:51 +02003679 .pr_ops = &dm_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 .owner = THIS_MODULE
3681};
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683/*
3684 * module hooks
3685 */
3686module_init(dm_init);
3687module_exit(dm_exit);
3688
3689module_param(major, uint, 0);
3690MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003691
Mike Snitzere8603132013-09-12 18:06:12 -04003692module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3693MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3694
Mike Snitzerf4790822013-09-12 18:06:12 -04003695module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3696MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3697
Mike Snitzer17e149b2015-03-11 15:01:09 -04003698module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
3699MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
3700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701MODULE_DESCRIPTION(DM_NAME " driver");
3702MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3703MODULE_LICENSE("GPL");