blob: 73f28802dc7abc3cb46dc38c8ef6fb5bb521e66b [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>
Li Zefan55782132009-06-09 13:43:05 +080024
25#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "core"
28
Namhyung Kim71a16732011-10-31 20:18:54 +000029#ifdef CONFIG_PRINTK
30/*
31 * ratelimit state to be used in DMXXX_LIMIT().
32 */
33DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
34 DEFAULT_RATELIMIT_INTERVAL,
35 DEFAULT_RATELIMIT_BURST);
36EXPORT_SYMBOL(dm_ratelimit_state);
37#endif
38
Milan Broz60935eb2009-06-22 10:12:30 +010039/*
40 * Cookies are numeric values sent with CHANGE and REMOVE
41 * uevents while resuming, removing or renaming the device.
42 */
43#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
44#define DM_COOKIE_LENGTH 24
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static const char *_name = DM_NAME;
47
48static unsigned int major = 0;
49static unsigned int _major = 0;
50
Alasdair G Kergond15b7742011-08-02 12:32:01 +010051static DEFINE_IDR(_minor_idr);
52
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070053static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040054
55static void do_deferred_remove(struct work_struct *w);
56
57static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
58
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040059static struct workqueue_struct *deferred_remove_workqueue;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000062 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * One of these is allocated per bio.
64 */
65struct dm_io {
66 struct mapped_device *md;
67 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010069 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080070 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010071 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040072 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000076 * For request-based dm.
77 * One of these is allocated per request.
78 */
79struct dm_rq_target_io {
80 struct mapped_device *md;
81 struct dm_target *ti;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -050082 struct request *orig, *clone;
Keith Busch2eb6e1e2014-10-17 17:46:36 -060083 struct kthread_work work;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000084 int error;
85 union map_info info;
86};
87
88/*
Kent Overstreet94818742012-09-07 13:44:01 -070089 * For request-based dm - the bio clones we allocate are embedded in these
90 * structs.
91 *
92 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
93 * the bioset is created - this means the bio has to come at the end of the
94 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000095 */
96struct dm_rq_clone_bio_info {
97 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010098 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -070099 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000100};
101
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100102union map_info *dm_get_rq_mapinfo(struct request *rq)
103{
104 if (rq && rq->end_io_data)
105 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
106 return NULL;
107}
108EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
109
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700110#define MINOR_ALLOCED ((void *)-1)
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * Bits for the md->flags field.
114 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100115#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800117#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700118#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700119#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800120#define DMF_NOFLUSH_SUSPENDING 5
Mikulas Patockad5b9dd02011-08-02 12:32:04 +0100121#define DMF_MERGE_IS_OPTIONAL 6
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400122#define DMF_DEFERRED_REMOVE 7
Mike Snitzerffcc3932014-10-28 18:34:52 -0400123#define DMF_SUSPENDED_INTERNALLY 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Milan Broz304f3f62008-02-08 02:11:17 +0000125/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100126 * A dummy definition to make RCU happy.
127 * struct dm_table should never be dereferenced in this file.
128 */
129struct dm_table {
130 int undefined__;
131};
132
133/*
Milan Broz304f3f62008-02-08 02:11:17 +0000134 * Work processed by per-device workqueue.
135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100137 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000138 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700140 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100142 /*
143 * The current mapping.
144 * Use dm_get_live_table{_fast} or take suspend_lock for
145 * dereference.
146 */
Pranith Kumar6fa99522014-10-28 15:09:57 -0700147 struct dm_table __rcu *map;
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100148
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500149 struct list_head table_devices;
150 struct mutex table_devices_lock;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unsigned long flags;
153
Jens Axboe165125e2007-07-24 09:28:11 +0200154 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100155 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100156 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100157 struct mutex type_lock;
158
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000159 struct target_type *immutable_target_type;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800162 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 void *interface_ptr;
165
166 /*
167 * A list of ios that arrived while we were suspended.
168 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200169 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100171 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800172 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100173 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200176 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000177 */
178 struct workqueue_struct *wq;
179
180 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * io objects are allocated from here.
182 */
183 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500184 mempool_t *rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Stefan Bader9faf4002006-10-03 01:15:41 -0700186 struct bio_set *bs;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /*
189 * Event handling.
190 */
191 atomic_t event_nr;
192 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100193 atomic_t uevent_seq;
194 struct list_head uevent_list;
195 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /*
198 * freeze/thaw support require holding onto a super block
199 */
200 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100201 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800202
203 /* forced geometry settings */
204 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000205
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500206 /* kobject and completion */
207 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500208
Tejun Heod87f4c12010-09-03 11:56:19 +0200209 /* zero-length flush that will be cloned and submitted to targets */
210 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400211
Mikulas Patocka96b26c82015-01-08 18:52:26 -0500212 /* the number of internal suspends */
213 unsigned internal_suspend_count;
214
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400215 struct dm_stats stats;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600216
217 struct kthread_worker kworker;
218 struct task_struct *kworker_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100221/*
222 * For mempools pre-allocation at the table loading time.
223 */
224struct dm_md_mempools {
225 mempool_t *io_pool;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500226 mempool_t *rq_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100227 struct bio_set *bs;
228};
229
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500230struct table_device {
231 struct list_head list;
232 atomic_t count;
233 struct dm_dev dm_dev;
234};
235
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400236#define RESERVED_BIO_BASED_IOS 16
237#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400238#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800239static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000240static struct kmem_cache *_rq_tio_cache;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500241static struct kmem_cache *_rq_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700242
Mike Snitzerf4790822013-09-12 18:06:12 -0400243/*
Mike Snitzere8603132013-09-12 18:06:12 -0400244 * Bio-based DM's mempools' reserved IOs set by the user.
245 */
246static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
247
248/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400249 * Request-based DM's mempools' reserved IOs set by the user.
250 */
251static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
252
253static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
254 unsigned def, unsigned max)
255{
256 unsigned ios = ACCESS_ONCE(*reserved_ios);
257 unsigned modified_ios = 0;
258
259 if (!ios)
260 modified_ios = def;
261 else if (ios > max)
262 modified_ios = max;
263
264 if (modified_ios) {
265 (void)cmpxchg(reserved_ios, ios, modified_ios);
266 ios = modified_ios;
267 }
268
269 return ios;
270}
271
Mike Snitzere8603132013-09-12 18:06:12 -0400272unsigned dm_get_reserved_bio_based_ios(void)
273{
274 return __dm_get_reserved_ios(&reserved_bio_based_ios,
275 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
276}
277EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
278
Mike Snitzerf4790822013-09-12 18:06:12 -0400279unsigned dm_get_reserved_rq_based_ios(void)
280{
281 return __dm_get_reserved_ios(&reserved_rq_based_ios,
282 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
283}
284EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int __init local_init(void)
287{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100288 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100291 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100293 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000295 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
296 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100297 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000298
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500299 _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
300 __alignof__(struct request), 0, NULL);
301 if (!_rq_cache)
302 goto out_free_rq_tio_cache;
303
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100304 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100305 if (r)
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500306 goto out_free_rq_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100307
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400308 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
309 if (!deferred_remove_workqueue) {
310 r = -ENOMEM;
311 goto out_uevent_exit;
312 }
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 _major = major;
315 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100316 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400317 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (!_major)
320 _major = r;
321
322 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100323
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400324out_free_workqueue:
325 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100326out_uevent_exit:
327 dm_uevent_exit();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500328out_free_rq_cache:
329 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000330out_free_rq_tio_cache:
331 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100332out_free_io_cache:
333 kmem_cache_destroy(_io_cache);
334
335 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338static void local_exit(void)
339{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400340 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400341 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400342
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500343 kmem_cache_destroy(_rq_cache);
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000344 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700346 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100347 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 _major = 0;
350
351 DMINFO("cleaned up");
352}
353
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000354static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 local_init,
356 dm_target_init,
357 dm_linear_init,
358 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000359 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100360 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400362 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363};
364
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000365static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 local_exit,
367 dm_target_exit,
368 dm_linear_exit,
369 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000370 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100371 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400373 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374};
375
376static int __init dm_init(void)
377{
378 const int count = ARRAY_SIZE(_inits);
379
380 int r, i;
381
382 for (i = 0; i < count; i++) {
383 r = _inits[i]();
384 if (r)
385 goto bad;
386 }
387
388 return 0;
389
390 bad:
391 while (i--)
392 _exits[i]();
393
394 return r;
395}
396
397static void __exit dm_exit(void)
398{
399 int i = ARRAY_SIZE(_exits);
400
401 while (i--)
402 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100403
404 /*
405 * Should be empty by this point.
406 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100407 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410/*
411 * Block device functions
412 */
Mike Anderson432a2122009-12-10 23:52:20 +0000413int dm_deleting_md(struct mapped_device *md)
414{
415 return test_bit(DMF_DELETING, &md->flags);
416}
417
Al Virofe5f9f22008-03-02 10:29:31 -0500418static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct mapped_device *md;
421
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700422 spin_lock(&_minor_lock);
423
Al Virofe5f9f22008-03-02 10:29:31 -0500424 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700425 if (!md)
426 goto out;
427
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700428 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000429 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700430 md = NULL;
431 goto out;
432 }
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700435 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700436
437out:
438 spin_unlock(&_minor_lock);
439
440 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Al Virodb2a1442013-05-05 21:52:57 -0400443static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Al Virofe5f9f22008-03-02 10:29:31 -0500445 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200446
Milan Broz4a1aeb92011-01-13 19:59:48 +0000447 spin_lock(&_minor_lock);
448
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400449 if (atomic_dec_and_test(&md->open_count) &&
450 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400451 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000454
455 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700458int dm_open_count(struct mapped_device *md)
459{
460 return atomic_read(&md->open_count);
461}
462
463/*
464 * Guarantees nothing is using the device before it's deleted.
465 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400466int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700467{
468 int r = 0;
469
470 spin_lock(&_minor_lock);
471
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400472 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700473 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400474 if (mark_deferred)
475 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
476 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
477 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700478 else
479 set_bit(DMF_DELETING, &md->flags);
480
481 spin_unlock(&_minor_lock);
482
483 return r;
484}
485
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400486int dm_cancel_deferred_remove(struct mapped_device *md)
487{
488 int r = 0;
489
490 spin_lock(&_minor_lock);
491
492 if (test_bit(DMF_DELETING, &md->flags))
493 r = -EBUSY;
494 else
495 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
496
497 spin_unlock(&_minor_lock);
498
499 return r;
500}
501
502static void do_deferred_remove(struct work_struct *w)
503{
504 dm_deferred_remove();
505}
506
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400507sector_t dm_get_size(struct mapped_device *md)
508{
509 return get_capacity(md->disk);
510}
511
Mike Snitzer9974fa22014-02-28 15:33:43 +0100512struct request_queue *dm_get_md_queue(struct mapped_device *md)
513{
514 return md->queue;
515}
516
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400517struct dm_stats *dm_get_stats(struct mapped_device *md)
518{
519 return &md->stats;
520}
521
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800522static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
523{
524 struct mapped_device *md = bdev->bd_disk->private_data;
525
526 return dm_get_geometry(md, geo);
527}
528
Al Virofe5f9f22008-03-02 10:29:31 -0500529static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700530 unsigned int cmd, unsigned long arg)
531{
Al Virofe5f9f22008-03-02 10:29:31 -0500532 struct mapped_device *md = bdev->bd_disk->private_data;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100533 int srcu_idx;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100534 struct dm_table *map;
Milan Brozaa129a22006-10-03 01:15:15 -0700535 struct dm_target *tgt;
536 int r = -ENOTTY;
537
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100538retry:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100539 map = dm_get_live_table(md, &srcu_idx);
540
Milan Brozaa129a22006-10-03 01:15:15 -0700541 if (!map || !dm_table_get_size(map))
542 goto out;
543
544 /* We only support devices that have a single target */
545 if (dm_table_get_num_targets(map) != 1)
546 goto out;
547
548 tgt = dm_table_get_target(map, 0);
Mike Snitzer4d341d82014-11-16 14:21:47 -0500549 if (!tgt->type->ioctl)
550 goto out;
Milan Brozaa129a22006-10-03 01:15:15 -0700551
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000552 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700553 r = -EAGAIN;
554 goto out;
555 }
556
Mike Snitzer4d341d82014-11-16 14:21:47 -0500557 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700558
559out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100560 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700561
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100562 if (r == -ENOTCONN) {
563 msleep(10);
564 goto retry;
565 }
566
Milan Brozaa129a22006-10-03 01:15:15 -0700567 return r;
568}
569
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100570static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 return mempool_alloc(md->io_pool, GFP_NOIO);
573}
574
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100575static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 mempool_free(io, md->io_pool);
578}
579
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100580static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Mikulas Patockadba14162012-10-12 21:02:15 +0100582 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000585static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
586 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100587{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000588 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100589}
590
591static void free_rq_tio(struct dm_rq_target_io *tio)
592{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000593 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100594}
595
Mike Snitzer1ae49ea2014-12-05 17:11:05 -0500596static struct request *alloc_clone_request(struct mapped_device *md,
597 gfp_t gfp_mask)
598{
599 return mempool_alloc(md->rq_pool, gfp_mask);
600}
601
602static void free_clone_request(struct mapped_device *md, struct request *rq)
603{
604 mempool_free(rq, md->rq_pool);
605}
606
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000607static int md_in_flight(struct mapped_device *md)
608{
609 return atomic_read(&md->pending[READ]) +
610 atomic_read(&md->pending[WRITE]);
611}
612
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800613static void start_io_acct(struct dm_io *io)
614{
615 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400616 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900617 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400618 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800619
620 io->start_time = jiffies;
621
Tejun Heo074a7ac2008-08-25 19:56:14 +0900622 cpu = part_stat_lock();
623 part_round_stats(cpu, &dm_disk(md)->part0);
624 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100625 atomic_set(&dm_disk(md)->part0.in_flight[rw],
626 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400627
628 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700629 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400630 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800631}
632
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000633static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800634{
635 struct mapped_device *md = io->md;
636 struct bio *bio = io->bio;
637 unsigned long duration = jiffies - io->start_time;
Gu Zheng18c0b222014-11-24 11:05:26 +0800638 int pending;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800639 int rw = bio_data_dir(bio);
640
Gu Zheng18c0b222014-11-24 11:05:26 +0800641 generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800642
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400643 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700644 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400645 bio_sectors(bio), true, duration, &io->stats_aux);
646
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100647 /*
648 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200649 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100650 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100651 pending = atomic_dec_return(&md->pending[rw]);
652 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200653 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800654
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000655 /* nudge anyone waiting on suspend queue */
656 if (!pending)
657 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/*
661 * Add the bio to the list of deferred io.
662 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100663static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200665 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200667 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200669 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200670 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/*
674 * Everyone (including functions in this file), should use this
675 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100676 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100678struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100680 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100682 return srcu_dereference(md->map, &md->io_barrier);
683}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100685void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
686{
687 srcu_read_unlock(&md->io_barrier, srcu_idx);
688}
689
690void dm_sync_table(struct mapped_device *md)
691{
692 synchronize_srcu(&md->io_barrier);
693 synchronize_rcu_expedited();
694}
695
696/*
697 * A fast alternative to dm_get_live_table/dm_put_live_table.
698 * The caller must not block between these two functions.
699 */
700static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
701{
702 rcu_read_lock();
703 return rcu_dereference(md->map);
704}
705
706static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
707{
708 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800711/*
Benjamin Marzinski86f11522014-08-13 13:53:43 -0500712 * Open a table device so we can use it as a map destination.
713 */
714static int open_table_device(struct table_device *td, dev_t dev,
715 struct mapped_device *md)
716{
717 static char *_claim_ptr = "I belong to device-mapper";
718 struct block_device *bdev;
719
720 int r;
721
722 BUG_ON(td->dm_dev.bdev);
723
724 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
725 if (IS_ERR(bdev))
726 return PTR_ERR(bdev);
727
728 r = bd_link_disk_holder(bdev, dm_disk(md));
729 if (r) {
730 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
731 return r;
732 }
733
734 td->dm_dev.bdev = bdev;
735 return 0;
736}
737
738/*
739 * Close a table device that we've been using.
740 */
741static void close_table_device(struct table_device *td, struct mapped_device *md)
742{
743 if (!td->dm_dev.bdev)
744 return;
745
746 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
747 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
748 td->dm_dev.bdev = NULL;
749}
750
751static struct table_device *find_table_device(struct list_head *l, dev_t dev,
752 fmode_t mode) {
753 struct table_device *td;
754
755 list_for_each_entry(td, l, list)
756 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
757 return td;
758
759 return NULL;
760}
761
762int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
763 struct dm_dev **result) {
764 int r;
765 struct table_device *td;
766
767 mutex_lock(&md->table_devices_lock);
768 td = find_table_device(&md->table_devices, dev, mode);
769 if (!td) {
770 td = kmalloc(sizeof(*td), GFP_KERNEL);
771 if (!td) {
772 mutex_unlock(&md->table_devices_lock);
773 return -ENOMEM;
774 }
775
776 td->dm_dev.mode = mode;
777 td->dm_dev.bdev = NULL;
778
779 if ((r = open_table_device(td, dev, md))) {
780 mutex_unlock(&md->table_devices_lock);
781 kfree(td);
782 return r;
783 }
784
785 format_dev_t(td->dm_dev.name, dev);
786
787 atomic_set(&td->count, 0);
788 list_add(&td->list, &md->table_devices);
789 }
790 atomic_inc(&td->count);
791 mutex_unlock(&md->table_devices_lock);
792
793 *result = &td->dm_dev;
794 return 0;
795}
796EXPORT_SYMBOL_GPL(dm_get_table_device);
797
798void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
799{
800 struct table_device *td = container_of(d, struct table_device, dm_dev);
801
802 mutex_lock(&md->table_devices_lock);
803 if (atomic_dec_and_test(&td->count)) {
804 close_table_device(td, md);
805 list_del(&td->list);
806 kfree(td);
807 }
808 mutex_unlock(&md->table_devices_lock);
809}
810EXPORT_SYMBOL(dm_put_table_device);
811
812static void free_table_devices(struct list_head *devices)
813{
814 struct list_head *tmp, *next;
815
816 list_for_each_safe(tmp, next, devices) {
817 struct table_device *td = list_entry(tmp, struct table_device, list);
818
819 DMWARN("dm_destroy: %s still exists with %d references",
820 td->dm_dev.name, atomic_read(&td->count));
821 kfree(td);
822 }
823}
824
825/*
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800826 * Get the geometry associated with a dm device
827 */
828int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
829{
830 *geo = md->geometry;
831
832 return 0;
833}
834
835/*
836 * Set the geometry of a device.
837 */
838int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
839{
840 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
841
842 if (geo->start > sz) {
843 DMWARN("Start sector is beyond the geometry limits.");
844 return -EINVAL;
845 }
846
847 md->geometry = *geo;
848
849 return 0;
850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/*-----------------------------------------------------------------
853 * CRUD START:
854 * A more elegant soln is in the works that uses the queue
855 * merge fn, unfortunately there are a couple of changes to
856 * the block layer that I want to make for this. So in the
857 * interests of getting something for people to use I give
858 * you this clearly demarcated crap.
859 *---------------------------------------------------------------*/
860
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800861static int __noflush_suspending(struct mapped_device *md)
862{
863 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * Decrements the number of outstanding ios that a bio has been
868 * cloned into, completing the original io if necc.
869 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800870static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800872 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000873 int io_error;
874 struct bio *bio;
875 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800876
877 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100878 if (unlikely(error)) {
879 spin_lock_irqsave(&io->endio_lock, flags);
880 if (!(io->error > 0 && __noflush_suspending(md)))
881 io->error = error;
882 spin_unlock_irqrestore(&io->endio_lock, flags);
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800886 if (io->error == DM_ENDIO_REQUEUE) {
887 /*
888 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800889 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100890 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200891 if (__noflush_suspending(md))
892 bio_list_add_head(&md->deferred, io->bio);
893 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800894 /* noflush suspend was interrupted. */
895 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100896 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800897 }
898
Milan Brozb35f8ca2009-03-16 17:44:36 +0000899 io_error = io->error;
900 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200901 end_io_acct(io);
902 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100903
Tejun Heo6a8736d2010-09-08 18:07:00 +0200904 if (io_error == DM_ENDIO_REQUEUE)
905 return;
906
Kent Overstreet4f024f32013-10-11 15:44:27 -0700907 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100908 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200909 * Preflush done for flush with data, reissue
910 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100911 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200912 bio->bi_rw &= ~REQ_FLUSH;
913 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100914 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200915 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700916 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200917 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920}
921
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400922static void disable_write_same(struct mapped_device *md)
923{
924 struct queue_limits *limits = dm_get_queue_limits(md);
925
926 /* device doesn't really support WRITE SAME, disable it */
927 limits->max_write_same_sectors = 0;
928}
929
NeilBrown6712ecf2007-09-27 12:47:43 +0200930static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
zhendong chen5164bec2014-12-17 14:37:04 +0800932 int r = error;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500933 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000934 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700935 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 dm_endio_fn endio = tio->ti->type->end_io;
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
939 error = -EIO;
940
941 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000942 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800943 if (r < 0 || r == DM_ENDIO_REQUEUE)
944 /*
945 * error and requeue request are handled
946 * in dec_pending().
947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800949 else if (r == DM_ENDIO_INCOMPLETE)
950 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200951 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800952 else if (r) {
953 DMWARN("unimplemented target endio return value: %d", r);
954 BUG();
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400958 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
959 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
960 disable_write_same(md);
961
Stefan Bader9faf4002006-10-03 01:15:41 -0700962 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000963 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100966/*
967 * Partial completion handling for request-based dm
968 */
969static void end_clone_bio(struct bio *clone, int error)
970{
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500971 struct dm_rq_clone_bio_info *info =
972 container_of(clone, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100973 struct dm_rq_target_io *tio = info->tio;
974 struct bio *bio = info->orig;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700975 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100976
977 bio_put(clone);
978
979 if (tio->error)
980 /*
981 * An error has already been detected on the request.
982 * Once error occurred, just let clone->end_io() handle
983 * the remainder.
984 */
985 return;
986 else if (error) {
987 /*
988 * Don't notice the error to the upper layer yet.
989 * The error handling decision is made by the target driver,
990 * when the request is completed.
991 */
992 tio->error = error;
993 return;
994 }
995
996 /*
997 * I/O for the bio successfully completed.
998 * Notice the data completion to the upper layer.
999 */
1000
1001 /*
1002 * bios are processed from the head of the list.
1003 * So the completing bio should always be rq->bio.
1004 * If it's not, something wrong is happening.
1005 */
1006 if (tio->orig->bio != bio)
1007 DMERR("bio completion is going in the middle of the request");
1008
1009 /*
1010 * Update the original request.
1011 * Do not use blk_end_request() here, because it may complete
1012 * the original request before the clone, and break the ordering.
1013 */
1014 blk_update_request(tio->orig, 0, nr_bytes);
1015}
1016
1017/*
1018 * Don't touch any member of the md after calling this function because
1019 * the md may be freed in dm_put() at the end of this function.
1020 * Or do dm_get() before calling this function and dm_put() later.
1021 */
Keith Busch466d89a2014-10-17 17:46:37 -06001022static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001023{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001024 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001025
1026 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001027 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001028 wake_up(&md->wait);
1029
Jens Axboea8c32a52012-11-06 12:24:26 +01001030 /*
1031 * Run this off this callpath, as drivers could invoke end_io while
1032 * inside their request_fn (and holding the queue lock). Calling
1033 * back into ->request_fn() could deadlock attempting to grab the
1034 * queue lock again.
1035 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001036 if (run_queue)
Jens Axboea8c32a52012-11-06 12:24:26 +01001037 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001038
1039 /*
1040 * dm_put() must be at the end of this function. See the comment above
1041 */
1042 dm_put(md);
1043}
1044
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001045static void free_rq_clone(struct request *clone)
1046{
1047 struct dm_rq_target_io *tio = clone->end_io_data;
1048
1049 blk_rq_unprep_clone(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -05001050 if (clone->q && clone->q->mq_ops)
1051 tio->ti->type->release_clone_rq(clone);
1052 else
1053 free_clone_request(tio->md, clone);
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +01001054 free_rq_tio(tio);
1055}
1056
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001057/*
1058 * Complete the clone and the original request.
Keith Busch466d89a2014-10-17 17:46:37 -06001059 * Must be called without clone's queue lock held,
1060 * see end_clone_request() for more details.
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001061 */
1062static void dm_end_request(struct request *clone, int error)
1063{
1064 int rw = rq_data_dir(clone);
1065 struct dm_rq_target_io *tio = clone->end_io_data;
1066 struct mapped_device *md = tio->md;
1067 struct request *rq = tio->orig;
1068
Tejun Heo29e40132010-09-08 18:07:00 +02001069 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001070 rq->errors = clone->errors;
1071 rq->resid_len = clone->resid_len;
1072
1073 if (rq->sense)
1074 /*
1075 * We are using the sense buffer of the original
1076 * request.
1077 * So setting the length of the sense data is enough.
1078 */
1079 rq->sense_len = clone->sense_len;
1080 }
1081
1082 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +02001083 blk_end_request_all(rq, error);
1084 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +00001085}
1086
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001087static void dm_unprep_request(struct request *rq)
1088{
Keith Busch466d89a2014-10-17 17:46:37 -06001089 struct dm_rq_target_io *tio = rq->special;
1090 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001091
1092 rq->special = NULL;
1093 rq->cmd_flags &= ~REQ_DONTPREP;
1094
Mike Snitzere5863d92014-12-17 21:08:12 -05001095 if (clone)
1096 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001097}
1098
1099/*
1100 * Requeue the original request of a clone.
1101 */
Keith Busch466d89a2014-10-17 17:46:37 -06001102static void dm_requeue_unmapped_original_request(struct mapped_device *md,
1103 struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001104{
Keith Busch466d89a2014-10-17 17:46:37 -06001105 int rw = rq_data_dir(rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001106 struct request_queue *q = rq->q;
1107 unsigned long flags;
1108
1109 dm_unprep_request(rq);
1110
1111 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001112 blk_requeue_request(q, rq);
1113 spin_unlock_irqrestore(q->queue_lock, flags);
1114
Keith Busch466d89a2014-10-17 17:46:37 -06001115 rq_completed(md, rw, false);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001116}
Keith Busch466d89a2014-10-17 17:46:37 -06001117
1118static void dm_requeue_unmapped_request(struct request *clone)
1119{
1120 struct dm_rq_target_io *tio = clone->end_io_data;
1121
1122 dm_requeue_unmapped_original_request(tio->md, tio->orig);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001123}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001124
1125static void __stop_queue(struct request_queue *q)
1126{
1127 blk_stop_queue(q);
1128}
1129
1130static void stop_queue(struct request_queue *q)
1131{
1132 unsigned long flags;
1133
1134 spin_lock_irqsave(q->queue_lock, flags);
1135 __stop_queue(q);
1136 spin_unlock_irqrestore(q->queue_lock, flags);
1137}
1138
1139static void __start_queue(struct request_queue *q)
1140{
1141 if (blk_queue_stopped(q))
1142 blk_start_queue(q);
1143}
1144
1145static void start_queue(struct request_queue *q)
1146{
1147 unsigned long flags;
1148
1149 spin_lock_irqsave(q->queue_lock, flags);
1150 __start_queue(q);
1151 spin_unlock_irqrestore(q->queue_lock, flags);
1152}
1153
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001154static void dm_done(struct request *clone, int error, bool mapped)
1155{
1156 int r = error;
1157 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001158 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001159
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001160 if (tio->ti) {
1161 rq_end_io = tio->ti->type->rq_end_io;
1162
1163 if (mapped && rq_end_io)
1164 r = rq_end_io(tio->ti, clone, error, &tio->info);
1165 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001166
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001167 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1168 !clone->q->limits.max_write_same_sectors))
1169 disable_write_same(tio->md);
1170
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001171 if (r <= 0)
1172 /* The target wants to complete the I/O */
1173 dm_end_request(clone, r);
1174 else if (r == DM_ENDIO_INCOMPLETE)
1175 /* The target will handle the I/O */
1176 return;
1177 else if (r == DM_ENDIO_REQUEUE)
1178 /* The target wants to requeue the I/O */
1179 dm_requeue_unmapped_request(clone);
1180 else {
1181 DMWARN("unimplemented target endio return value: %d", r);
1182 BUG();
1183 }
1184}
1185
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001186/*
1187 * Request completion handler for request-based dm
1188 */
1189static void dm_softirq_done(struct request *rq)
1190{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001191 bool mapped = true;
Keith Busch466d89a2014-10-17 17:46:37 -06001192 struct dm_rq_target_io *tio = rq->special;
1193 struct request *clone = tio->clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001194
Mike Snitzere5863d92014-12-17 21:08:12 -05001195 if (!clone) {
1196 blk_end_request_all(rq, tio->error);
1197 rq_completed(tio->md, rq_data_dir(rq), false);
1198 free_rq_tio(tio);
1199 return;
1200 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001201
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001202 if (rq->cmd_flags & REQ_FAILED)
1203 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001204
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001205 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001206}
1207
1208/*
1209 * Complete the clone and the original request with the error status
1210 * through softirq context.
1211 */
Keith Busch466d89a2014-10-17 17:46:37 -06001212static void dm_complete_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001213{
Keith Busch466d89a2014-10-17 17:46:37 -06001214 struct dm_rq_target_io *tio = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001215
1216 tio->error = error;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001217 blk_complete_request(rq);
1218}
1219
1220/*
1221 * Complete the not-mapped clone and the original request with the error status
1222 * through softirq context.
1223 * Target's rq_end_io() function isn't called.
Mike Snitzere5863d92014-12-17 21:08:12 -05001224 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001225 */
Keith Busch466d89a2014-10-17 17:46:37 -06001226static void dm_kill_unmapped_request(struct request *rq, int error)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001227{
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001228 rq->cmd_flags |= REQ_FAILED;
Keith Busch466d89a2014-10-17 17:46:37 -06001229 dm_complete_request(rq, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001230}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001231
1232/*
Keith Busch466d89a2014-10-17 17:46:37 -06001233 * Called with the clone's queue lock held
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001234 */
1235static void end_clone_request(struct request *clone, int error)
1236{
Keith Busch466d89a2014-10-17 17:46:37 -06001237 struct dm_rq_target_io *tio = clone->end_io_data;
1238
Mike Snitzere5863d92014-12-17 21:08:12 -05001239 if (!clone->q->mq_ops) {
1240 /*
1241 * For just cleaning up the information of the queue in which
1242 * the clone was dispatched.
1243 * The clone is *NOT* freed actually here because it is alloced
1244 * from dm own mempool (REQ_ALLOCED isn't set).
1245 */
1246 __blk_put_request(clone->q, clone);
1247 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001248
1249 /*
1250 * Actual request completion is done in a softirq context which doesn't
Keith Busch466d89a2014-10-17 17:46:37 -06001251 * hold the clone's queue lock. Otherwise, deadlock could occur because:
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001252 * - another request may be submitted by the upper level driver
1253 * of the stacking during the completion
1254 * - the submission which requires queue lock may be done
Keith Busch466d89a2014-10-17 17:46:37 -06001255 * against this clone's queue
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001256 */
Keith Busch466d89a2014-10-17 17:46:37 -06001257 dm_complete_request(tio->orig, error);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001258}
1259
Mike Snitzer56a67df2010-08-12 04:14:10 +01001260/*
1261 * Return maximum size of I/O possible at the supplied sector up to the current
1262 * target boundary.
1263 */
1264static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001266 sector_t target_offset = dm_target_offset(ti, sector);
1267
1268 return ti->len - target_offset;
1269}
1270
1271static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1272{
1273 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001274 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001277 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001279 if (ti->max_io_len) {
1280 offset = dm_target_offset(ti, sector);
1281 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1282 max_len = sector_div(offset, ti->max_io_len);
1283 else
1284 max_len = offset & (ti->max_io_len - 1);
1285 max_len = ti->max_io_len - max_len;
1286
1287 if (len > max_len)
1288 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291 return len;
1292}
1293
Mike Snitzer542f9032012-07-27 15:08:00 +01001294int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1295{
1296 if (len > UINT_MAX) {
1297 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1298 (unsigned long long)len, UINT_MAX);
1299 ti->error = "Maximum size of target IO is too large";
1300 return -EINVAL;
1301 }
1302
1303 ti->max_io_len = (uint32_t) len;
1304
1305 return 0;
1306}
1307EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1308
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001309/*
1310 * A target may call dm_accept_partial_bio only from the map routine. It is
1311 * allowed for all bio types except REQ_FLUSH.
1312 *
1313 * dm_accept_partial_bio informs the dm that the target only wants to process
1314 * additional n_sectors sectors of the bio and the rest of the data should be
1315 * sent in a next bio.
1316 *
1317 * A diagram that explains the arithmetics:
1318 * +--------------------+---------------+-------+
1319 * | 1 | 2 | 3 |
1320 * +--------------------+---------------+-------+
1321 *
1322 * <-------------- *tio->len_ptr --------------->
1323 * <------- bi_size ------->
1324 * <-- n_sectors -->
1325 *
1326 * Region 1 was already iterated over with bio_advance or similar function.
1327 * (it may be empty if the target doesn't use bio_advance)
1328 * Region 2 is the remaining bio size that the target wants to process.
1329 * (it may be empty if region 1 is non-empty, although there is no reason
1330 * to make it empty)
1331 * The target requires that region 3 is to be sent in the next bio.
1332 *
1333 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1334 * the partially processed part (the sum of regions 1+2) must be the same for all
1335 * copies of the bio.
1336 */
1337void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1338{
1339 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1340 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1341 BUG_ON(bio->bi_rw & REQ_FLUSH);
1342 BUG_ON(bi_size > *tio->len_ptr);
1343 BUG_ON(n_sectors > bi_size);
1344 *tio->len_ptr -= bi_size - n_sectors;
1345 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1346}
1347EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1348
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001349static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001352 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001353 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001354 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001355 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /*
1360 * Map the clone. If r == 0 we don't need to do
1361 * anything, the target has assumed ownership of
1362 * this io.
1363 */
1364 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001365 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001366 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001367 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001369
Mike Snitzerd07335e2010-11-16 12:52:38 +01001370 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1371 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001374 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1375 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001376 md = tio->io->md;
1377 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001378 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001379 } else if (r) {
1380 DMWARN("unimplemented target map return value: %d", r);
1381 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383}
1384
1385struct clone_info {
1386 struct mapped_device *md;
1387 struct dm_table *map;
1388 struct bio *bio;
1389 struct dm_io *io;
1390 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001391 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392};
1393
Mikulas Patockae0d66092014-03-14 18:40:39 -04001394static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001395{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001396 bio->bi_iter.bi_sector = sector;
1397 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400/*
1401 * Creates a bio that consists of range of complete bvecs.
1402 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001403static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001404 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Mikulas Patockadba14162012-10-12 21:02:15 +01001406 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001408 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001410 if (bio_integrity(bio))
1411 bio_integrity_clone(clone, bio, GFP_NOIO);
1412
1413 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1414 clone->bi_iter.bi_size = to_bytes(len);
1415
1416 if (bio_integrity(bio))
1417 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001420static struct dm_target_io *alloc_tio(struct clone_info *ci,
Junichi Nomura99778272014-10-03 11:55:16 +00001421 struct dm_target *ti,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001422 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001423{
Mikulas Patockadba14162012-10-12 21:02:15 +01001424 struct dm_target_io *tio;
1425 struct bio *clone;
1426
Junichi Nomura99778272014-10-03 11:55:16 +00001427 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
Mikulas Patockadba14162012-10-12 21:02:15 +01001428 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001429
1430 tio->io = ci->io;
1431 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001432 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001433
1434 return tio;
1435}
1436
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001437static void __clone_and_map_simple_bio(struct clone_info *ci,
1438 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001439 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001440{
Junichi Nomura99778272014-10-03 11:55:16 +00001441 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001442 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001443
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001444 tio->len_ptr = len;
1445
Junichi Nomura99778272014-10-03 11:55:16 +00001446 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001447 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001448 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001449
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001450 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001451}
1452
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001453static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001454 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001455{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001456 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001457
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001458 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001459 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001460}
1461
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001462static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001463{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001464 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001465 struct dm_target *ti;
1466
Mike Snitzerb372d362010-09-08 18:07:01 +02001467 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001468 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001469 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001470
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001471 return 0;
1472}
1473
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001474static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001475 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001476{
Mikulas Patockadba14162012-10-12 21:02:15 +01001477 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001478 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001479 unsigned target_bio_nr;
1480 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001481
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001482 /*
1483 * Does the target want to receive duplicate copies of the bio?
1484 */
1485 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1486 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001487
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001488 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Junichi Nomura99778272014-10-03 11:55:16 +00001489 tio = alloc_tio(ci, ti, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001490 tio->len_ptr = len;
1491 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001492 __map_bio(tio);
1493 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001494}
1495
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001496typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001497
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001498static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001499{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001500 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001501}
1502
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001503static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001504{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001505 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001506}
1507
1508typedef bool (*is_split_required_fn)(struct dm_target *ti);
1509
1510static bool is_split_required_for_discard(struct dm_target *ti)
1511{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001512 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001513}
1514
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001515static int __send_changing_extent_only(struct clone_info *ci,
1516 get_num_bios_fn get_num_bios,
1517 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001518{
1519 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001520 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001521 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001522
Mike Snitzera79245b2010-08-12 04:14:24 +01001523 do {
1524 ti = dm_table_find_target(ci->map, ci->sector);
1525 if (!dm_target_is_valid(ti))
1526 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001527
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001528 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001529 * Even though the device advertised support for this type of
1530 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001531 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001532 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001533 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001534 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1535 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001536 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001537
Mike Snitzer23508a92012-12-21 20:23:37 +00001538 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001539 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001540 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001541 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001542
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001543 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001544
1545 ci->sector += len;
1546 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001547
1548 return 0;
1549}
1550
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001551static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001552{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001553 return __send_changing_extent_only(ci, get_num_discard_bios,
1554 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001555}
1556
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001557static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001558{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001559 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001560}
1561
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001562/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001563 * Select the correct strategy for processing a non-flush bio.
1564 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001565static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Mikulas Patockadba14162012-10-12 21:02:15 +01001567 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001568 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001569 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001571 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001572 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001573 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001574 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001575
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001576 ti = dm_table_find_target(ci->map, ci->sector);
1577 if (!dm_target_is_valid(ti))
1578 return -EIO;
1579
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001580 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001581
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001582 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001584 ci->sector += len;
1585 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
1590/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001591 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001593static void __split_and_process_bio(struct mapped_device *md,
1594 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001597 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001599 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001600 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001601 return;
1602 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001603
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001604 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 ci.io = alloc_io(md);
1607 ci.io->error = 0;
1608 atomic_set(&ci.io->io_count, 1);
1609 ci.io->bio = bio;
1610 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001611 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001612 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001614 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001615
Mike Snitzerb372d362010-09-08 18:07:01 +02001616 if (bio->bi_rw & REQ_FLUSH) {
1617 ci.bio = &ci.md->flush_bio;
1618 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001619 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001620 /* dec_pending submits any data associated with flush */
1621 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001622 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001623 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001624 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001625 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001629 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631/*-----------------------------------------------------------------
1632 * CRUD END
1633 *---------------------------------------------------------------*/
1634
Milan Brozf6fccb12008-07-21 12:00:37 +01001635static int dm_merge_bvec(struct request_queue *q,
1636 struct bvec_merge_data *bvm,
1637 struct bio_vec *biovec)
1638{
1639 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001640 struct dm_table *map = dm_get_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001641 struct dm_target *ti;
1642 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001643 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001644
1645 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001646 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001647
1648 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001649 if (!dm_target_is_valid(ti))
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001650 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001651
1652 /*
1653 * Find maximum amount of I/O that won't need splitting
1654 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001655 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Mike Snitzer148e51b2014-10-09 19:32:22 -04001656 (sector_t) queue_max_sectors(q));
Milan Brozf6fccb12008-07-21 12:00:37 +01001657 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
Mike Snitzer148e51b2014-10-09 19:32:22 -04001658 if (unlikely(max_size < 0)) /* this shouldn't _ever_ happen */
Milan Brozf6fccb12008-07-21 12:00:37 +01001659 max_size = 0;
1660
1661 /*
1662 * merge_bvec_fn() returns number of bytes
1663 * it can accept at this offset
1664 * max is precomputed maximal io size
1665 */
1666 if (max_size && ti->type->merge)
1667 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001668 /*
1669 * If the target doesn't support merge method and some of the devices
Mike Snitzer148e51b2014-10-09 19:32:22 -04001670 * provided their merge_bvec method (we know this by looking for the
1671 * max_hw_sectors that dm_set_device_limits may set), then we can't
1672 * allow bios with multiple vector entries. So always set max_size
1673 * to 0, and the code below allows just one page.
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001674 */
1675 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001676 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001677
Mikulas Patocka50371082008-10-01 14:39:17 +01001678out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001679 dm_put_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001680 /*
1681 * Always allow an entire first page
1682 */
1683 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1684 max_size = biovec->bv_len;
1685
Milan Brozf6fccb12008-07-21 12:00:37 +01001686 return max_size;
1687}
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689/*
1690 * The request function that just remaps the bio built up by
1691 * dm_merge_bvec.
1692 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001693static void _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Kevin Corry12f03a42006-02-01 03:04:52 -08001695 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001697 int srcu_idx;
1698 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001700 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Gu Zheng18c0b222014-11-24 11:05:26 +08001702 generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
Kevin Corry12f03a42006-02-01 03:04:52 -08001703
Tejun Heo6a8736d2010-09-08 18:07:00 +02001704 /* if we're suspended, we have to queue this io for later */
1705 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001706 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Tejun Heo6a8736d2010-09-08 18:07:00 +02001708 if (bio_rw(bio) != READA)
1709 queue_io(md, bio);
1710 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001711 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001712 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001715 __split_and_process_bio(md, map, bio);
1716 dm_put_live_table(md, srcu_idx);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001717 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001718}
1719
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001720int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001721{
1722 return blk_queue_stackable(md->queue);
1723}
1724
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001725static void dm_request(struct request_queue *q, struct bio *bio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001726{
1727 struct mapped_device *md = q->queuedata;
1728
1729 if (dm_request_based(md))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001730 blk_queue_bio(q, bio);
1731 else
1732 _dm_request(q, bio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001733}
1734
Keith Busch466d89a2014-10-17 17:46:37 -06001735static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001736{
1737 int r;
1738
Keith Busch466d89a2014-10-17 17:46:37 -06001739 if (blk_queue_io_stat(clone->q))
1740 clone->cmd_flags |= REQ_IO_STAT;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001741
Keith Busch466d89a2014-10-17 17:46:37 -06001742 clone->start_time = jiffies;
1743 r = blk_insert_cloned_request(clone->q, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001744 if (r)
Keith Busch466d89a2014-10-17 17:46:37 -06001745 /* must complete clone in terms of original request */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001746 dm_complete_request(rq, r);
1747}
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001748
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001749static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1750 void *data)
1751{
1752 struct dm_rq_target_io *tio = data;
Kent Overstreet94818742012-09-07 13:44:01 -07001753 struct dm_rq_clone_bio_info *info =
1754 container_of(bio, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001755
1756 info->orig = bio_orig;
1757 info->tio = tio;
1758 bio->bi_end_io = end_clone_bio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001759
1760 return 0;
1761}
1762
1763static int setup_clone(struct request *clone, struct request *rq,
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001764 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001765{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001766 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001767
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001768 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
Tejun Heo29e40132010-09-08 18:07:00 +02001769 dm_rq_bio_constructor, tio);
1770 if (r)
1771 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001772
Tejun Heo29e40132010-09-08 18:07:00 +02001773 clone->cmd = rq->cmd;
1774 clone->cmd_len = rq->cmd_len;
1775 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001776 clone->end_io = end_clone_request;
1777 clone->end_io_data = tio;
1778
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001779 tio->clone = clone;
1780
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001781 return 0;
1782}
1783
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001784static struct request *clone_rq(struct request *rq, struct mapped_device *md,
Keith Busch466d89a2014-10-17 17:46:37 -06001785 struct dm_rq_target_io *tio, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001786{
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001787 struct request *clone = alloc_clone_request(md, gfp_mask);
1788
1789 if (!clone)
1790 return NULL;
1791
1792 blk_rq_init(NULL, clone);
1793 if (setup_clone(clone, rq, tio, gfp_mask)) {
1794 /* -ENOMEM */
1795 free_clone_request(md, clone);
1796 return NULL;
1797 }
1798
1799 return clone;
1800}
1801
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001802static void map_tio_request(struct kthread_work *work);
1803
Keith Busch466d89a2014-10-17 17:46:37 -06001804static struct dm_rq_target_io *prep_tio(struct request *rq,
1805 struct mapped_device *md, gfp_t gfp_mask)
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001806{
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001807 struct dm_rq_target_io *tio;
Mike Snitzere5863d92014-12-17 21:08:12 -05001808 int srcu_idx;
1809 struct dm_table *table;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001810
1811 tio = alloc_rq_tio(md, gfp_mask);
1812 if (!tio)
1813 return NULL;
1814
1815 tio->md = md;
1816 tio->ti = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05001817 tio->clone = NULL;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001818 tio->orig = rq;
1819 tio->error = 0;
1820 memset(&tio->info, 0, sizeof(tio->info));
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001821 init_kthread_work(&tio->work, map_tio_request);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001822
Mike Snitzere5863d92014-12-17 21:08:12 -05001823 table = dm_get_live_table(md, &srcu_idx);
1824 if (!dm_table_mq_request_based(table)) {
1825 if (!clone_rq(rq, md, tio, gfp_mask)) {
1826 dm_put_live_table(md, srcu_idx);
1827 free_rq_tio(tio);
1828 return NULL;
1829 }
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001830 }
Mike Snitzere5863d92014-12-17 21:08:12 -05001831 dm_put_live_table(md, srcu_idx);
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001832
Keith Busch466d89a2014-10-17 17:46:37 -06001833 return tio;
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001834}
1835
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001836/*
1837 * Called with the queue lock held.
1838 */
1839static int dm_prep_fn(struct request_queue *q, struct request *rq)
1840{
1841 struct mapped_device *md = q->queuedata;
Keith Busch466d89a2014-10-17 17:46:37 -06001842 struct dm_rq_target_io *tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001843
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001844 if (unlikely(rq->special)) {
1845 DMWARN("Already has something in rq->special.");
1846 return BLKPREP_KILL;
1847 }
1848
Keith Busch466d89a2014-10-17 17:46:37 -06001849 tio = prep_tio(rq, md, GFP_ATOMIC);
1850 if (!tio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001851 return BLKPREP_DEFER;
1852
Keith Busch466d89a2014-10-17 17:46:37 -06001853 rq->special = tio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001854 rq->cmd_flags |= REQ_DONTPREP;
1855
1856 return BLKPREP_OK;
1857}
1858
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001859/*
1860 * Returns:
Mike Snitzere5863d92014-12-17 21:08:12 -05001861 * 0 : the request has been processed
1862 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1863 * < 0 : the request was completed due to failure
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001864 */
Keith Busch466d89a2014-10-17 17:46:37 -06001865static int map_request(struct dm_target *ti, struct request *rq,
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001866 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001867{
Mike Snitzere5863d92014-12-17 21:08:12 -05001868 int r;
Keith Busch466d89a2014-10-17 17:46:37 -06001869 struct dm_rq_target_io *tio = rq->special;
Mike Snitzere5863d92014-12-17 21:08:12 -05001870 struct request *clone = NULL;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001871
Mike Snitzere5863d92014-12-17 21:08:12 -05001872 if (tio->clone) {
1873 clone = tio->clone;
1874 r = ti->type->map_rq(ti, clone, &tio->info);
1875 } else {
1876 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1877 if (r < 0) {
1878 /* The target wants to complete the I/O */
1879 dm_kill_unmapped_request(rq, r);
1880 return r;
1881 }
1882 if (IS_ERR(clone))
1883 return DM_MAPIO_REQUEUE;
1884 if (setup_clone(clone, rq, tio, GFP_KERNEL)) {
1885 /* -ENOMEM */
1886 ti->type->release_clone_rq(clone);
1887 return DM_MAPIO_REQUEUE;
1888 }
1889 }
1890
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001891 switch (r) {
1892 case DM_MAPIO_SUBMITTED:
1893 /* The target has taken the I/O to submit by itself later */
1894 break;
1895 case DM_MAPIO_REMAPPED:
1896 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001897 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
Keith Busch466d89a2014-10-17 17:46:37 -06001898 blk_rq_pos(rq));
1899 dm_dispatch_clone_request(clone, rq);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001900 break;
1901 case DM_MAPIO_REQUEUE:
1902 /* The target wants to requeue the I/O */
1903 dm_requeue_unmapped_request(clone);
1904 break;
1905 default:
1906 if (r > 0) {
1907 DMWARN("unimplemented target map return value: %d", r);
1908 BUG();
1909 }
1910
1911 /* The target wants to complete the I/O */
Keith Busch466d89a2014-10-17 17:46:37 -06001912 dm_kill_unmapped_request(rq, r);
Mike Snitzere5863d92014-12-17 21:08:12 -05001913 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001914 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001915
Mike Snitzere5863d92014-12-17 21:08:12 -05001916 return 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001917}
1918
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001919static void map_tio_request(struct kthread_work *work)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001920{
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001921 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
Mike Snitzere5863d92014-12-17 21:08:12 -05001922 struct request *rq = tio->orig;
1923 struct mapped_device *md = tio->md;
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001924
Mike Snitzere5863d92014-12-17 21:08:12 -05001925 if (map_request(tio->ti, rq, md) == DM_MAPIO_REQUEUE)
1926 dm_requeue_unmapped_original_request(md, rq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001927}
1928
Keith Busch466d89a2014-10-17 17:46:37 -06001929static void dm_start_request(struct mapped_device *md, struct request *orig)
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001930{
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001931 blk_start_request(orig);
Keith Busch466d89a2014-10-17 17:46:37 -06001932 atomic_inc(&md->pending[rq_data_dir(orig)]);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001933
1934 /*
1935 * Hold the md reference here for the in-flight I/O.
1936 * We can't rely on the reference count by device opener,
1937 * because the device may be closed during the request completion
1938 * when all bios are completed.
1939 * See the comment in rq_completed() too.
1940 */
1941 dm_get(md);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001942}
1943
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001944/*
1945 * q->request_fn for request-based dm.
1946 * Called with the queue lock held.
1947 */
1948static void dm_request_fn(struct request_queue *q)
1949{
1950 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001951 int srcu_idx;
1952 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001953 struct dm_target *ti;
Keith Busch466d89a2014-10-17 17:46:37 -06001954 struct request *rq;
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001955 struct dm_rq_target_io *tio;
Tejun Heo29e40132010-09-08 18:07:00 +02001956 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001957
1958 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001959 * For suspend, check blk_queue_stopped() and increment
1960 * ->pending within a single queue_lock not to increment the
1961 * number of in-flight I/Os after the queue is stopped in
1962 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001963 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001964 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001965 rq = blk_peek_request(q);
1966 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001967 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001968
Tejun Heo29e40132010-09-08 18:07:00 +02001969 /* always use block 0 to find the target for flushes for now */
1970 pos = 0;
1971 if (!(rq->cmd_flags & REQ_FLUSH))
1972 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001973
Tejun Heo29e40132010-09-08 18:07:00 +02001974 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001975 if (!dm_target_is_valid(ti)) {
1976 /*
Keith Busch466d89a2014-10-17 17:46:37 -06001977 * Must perform setup, that rq_completed() requires,
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001978 * before calling dm_kill_unmapped_request
1979 */
1980 DMERR_LIMIT("request attempted access beyond the end of device");
Keith Busch466d89a2014-10-17 17:46:37 -06001981 dm_start_request(md, rq);
1982 dm_kill_unmapped_request(rq, -EIO);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001983 continue;
1984 }
Tejun Heo29e40132010-09-08 18:07:00 +02001985
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001986 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001987 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001988
Keith Busch466d89a2014-10-17 17:46:37 -06001989 dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001990
Keith Busch2eb6e1e2014-10-17 17:46:36 -06001991 tio = rq->special;
1992 /* Establish tio->ti before queuing work (map_tio_request) */
1993 tio->ti = ti;
1994 queue_kthread_work(&md->kworker, &tio->work);
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001995 BUG_ON(!irqs_disabled());
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001996 }
1997
1998 goto out;
1999
Jens Axboe7eaceac2011-03-10 08:52:07 +01002000delay_and_out:
2001 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002002out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002003 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002004}
2005
2006int dm_underlying_device_busy(struct request_queue *q)
2007{
2008 return blk_lld_busy(q);
2009}
2010EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
2011
2012static int dm_lld_busy(struct request_queue *q)
2013{
2014 int r;
2015 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002016 struct dm_table *map = dm_get_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002017
2018 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
2019 r = 1;
2020 else
2021 r = dm_table_any_busy_target(map);
2022
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002023 dm_put_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002024
2025 return r;
2026}
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028static int dm_any_congested(void *congested_data, int bdi_bits)
2029{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002030 int r = bdi_bits;
2031 struct mapped_device *md = congested_data;
2032 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002034 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002035 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002036 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002037 /*
2038 * Request-based dm cares about only own queue for
2039 * the query about congestion status of request_queue
2040 */
2041 if (dm_request_based(md))
2042 r = md->queue->backing_dev_info.state &
2043 bdi_bits;
2044 else
2045 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002046 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002047 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00002048 }
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return r;
2051}
2052
2053/*-----------------------------------------------------------------
2054 * An IDR is used to keep track of allocated minor numbers.
2055 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002056static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002058 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002060 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
2063/*
2064 * See if the device with a specific minor # is free.
2065 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002066static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002068 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 if (minor >= (1 << MINORBITS))
2071 return -EINVAL;
2072
Tejun Heoc9d76be2013-02-27 17:04:26 -08002073 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002074 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Tejun Heoc9d76be2013-02-27 17:04:26 -08002076 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002078 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002079 idr_preload_end();
2080 if (r < 0)
2081 return r == -ENOSPC ? -EBUSY : r;
2082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002085static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Tejun Heoc9d76be2013-02-27 17:04:26 -08002087 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Tejun Heoc9d76be2013-02-27 17:04:26 -08002089 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002090 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Tejun Heoc9d76be2013-02-27 17:04:26 -08002092 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002094 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08002095 idr_preload_end();
2096 if (r < 0)
2097 return r;
2098 *minor = r;
2099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002102static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Mikulas Patocka53d59142009-04-02 19:55:37 +01002104static void dm_wq_work(struct work_struct *work);
2105
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002106static void dm_init_md_queue(struct mapped_device *md)
2107{
2108 /*
2109 * Request-based dm devices cannot be stacked on top of bio-based dm
2110 * devices. The type of this dm device has not been decided yet.
2111 * The type is decided at the first table loading time.
2112 * To prevent problematic device stacking, clear the queue flag
2113 * for request stacking support until then.
2114 *
2115 * This queue is new, so no concurrency on the queue_flags.
2116 */
2117 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
2118
2119 md->queue->queuedata = md;
2120 md->queue->backing_dev_info.congested_fn = dm_any_congested;
2121 md->queue->backing_dev_info.congested_data = md;
2122 blk_queue_make_request(md->queue, dm_request);
2123 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002124 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
2125}
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/*
2128 * Allocate and initialise a blank device with a given minor.
2129 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002130static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002133 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002134 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if (!md) {
2137 DMWARN("unable to allocate device, out of memory.");
2138 return NULL;
2139 }
2140
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002141 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00002142 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002145 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002146 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002147 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01002148 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002150 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002152 r = init_srcu_struct(&md->io_barrier);
2153 if (r < 0)
2154 goto bad_io_barrier;
2155
Mike Snitzera5664da2010-08-12 04:14:01 +01002156 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00002157 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01002158 mutex_init(&md->type_lock);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002159 mutex_init(&md->table_devices_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002160 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07002162 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002164 atomic_set(&md->uevent_seq, 0);
2165 INIT_LIST_HEAD(&md->uevent_list);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002166 INIT_LIST_HEAD(&md->table_devices);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002167 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002169 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002171 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002173 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 md->disk = alloc_disk(1);
2176 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002177 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002179 atomic_set(&md->pending[0], 0);
2180 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002181 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002182 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002183 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002184 init_completion(&md->kobj_holder.completion);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002185 md->kworker_task = NULL;
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 md->disk->major = _major;
2188 md->disk->first_minor = minor;
2189 md->disk->fops = &dm_blk_dops;
2190 md->disk->queue = md->queue;
2191 md->disk->private_data = md;
2192 sprintf(md->disk->disk_name, "dm-%d", minor);
2193 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002194 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Tejun Heo670368a2013-07-30 08:40:21 -04002196 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002197 if (!md->wq)
2198 goto bad_thread;
2199
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002200 md->bdev = bdget_disk(md->disk, 0);
2201 if (!md->bdev)
2202 goto bad_bdev;
2203
Tejun Heo6a8736d2010-09-08 18:07:00 +02002204 bio_init(&md->flush_bio);
2205 md->flush_bio.bi_bdev = md->bdev;
2206 md->flush_bio.bi_rw = WRITE_FLUSH;
2207
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002208 dm_stats_init(&md->stats);
2209
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002210 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002211 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002212 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002213 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002214
2215 BUG_ON(old_md != MINOR_ALLOCED);
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return md;
2218
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002219bad_bdev:
2220 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00002221bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01002222 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00002223 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002224bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05002225 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002226bad_queue:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002227 cleanup_srcu_struct(&md->io_barrier);
2228bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002230bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002231 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002232bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 kfree(md);
2234 return NULL;
2235}
2236
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002237static void unlock_fs(struct mapped_device *md);
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239static void free_dev(struct mapped_device *md)
2240{
Tejun Heof331c022008-09-03 09:01:48 +02002241 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002242
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002243 unlock_fs(md);
2244 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00002245 destroy_workqueue(md->wq);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002246
2247 if (md->kworker_task)
2248 kthread_stop(md->kworker_task);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002249 if (md->io_pool)
2250 mempool_destroy(md->io_pool);
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002251 if (md->rq_pool)
2252 mempool_destroy(md->rq_pool);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002253 if (md->bs)
2254 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01002255 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 del_gendisk(md->disk);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002257 cleanup_srcu_struct(&md->io_barrier);
Benjamin Marzinski86f11522014-08-13 13:53:43 -05002258 free_table_devices(&md->table_devices);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002259 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002260
2261 spin_lock(&_minor_lock);
2262 md->disk->private_data = NULL;
2263 spin_unlock(&_minor_lock);
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05002266 blk_cleanup_queue(md->queue);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002267 dm_stats_cleanup(&md->stats);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002268 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 kfree(md);
2270}
2271
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002272static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2273{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002274 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002275
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002276 if (md->io_pool && md->bs) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002277 /* The md already has necessary mempools. */
2278 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2279 /*
2280 * Reload bioset because front_pad may have changed
2281 * because a different table was loaded.
2282 */
2283 bioset_free(md->bs);
2284 md->bs = p->bs;
2285 p->bs = NULL;
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002286 }
Keith Busch466d89a2014-10-17 17:46:37 -06002287 /*
2288 * There's no need to reload with request-based dm
2289 * because the size of front_pad doesn't change.
2290 * Note for future: If you are to reload bioset,
2291 * prep-ed requests in the queue may refer
2292 * to bio from the old bioset, so you must walk
2293 * through the queue to unprep.
2294 */
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002295 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002296 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002297
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002298 BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002299
2300 md->io_pool = p->io_pool;
2301 p->io_pool = NULL;
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05002302 md->rq_pool = p->rq_pool;
2303 p->rq_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002304 md->bs = p->bs;
2305 p->bs = NULL;
2306
2307out:
2308 /* mempool bind completed, now no need any mempools in the table */
2309 dm_table_free_md_mempools(t);
2310}
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312/*
2313 * Bind a table to the device.
2314 */
2315static void event_callback(void *context)
2316{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002317 unsigned long flags;
2318 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 struct mapped_device *md = (struct mapped_device *) context;
2320
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002321 spin_lock_irqsave(&md->uevent_lock, flags);
2322 list_splice_init(&md->uevent_list, &uevents);
2323 spin_unlock_irqrestore(&md->uevent_lock, flags);
2324
Tejun Heoed9e1982008-08-25 19:56:05 +09002325 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 atomic_inc(&md->event_nr);
2328 wake_up(&md->eventq);
2329}
2330
Mike Snitzerc2176492011-01-13 19:53:46 +00002331/*
2332 * Protected by md->suspend_lock obtained by dm_swap_table().
2333 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002334static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002336 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002338 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002341/*
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002342 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2343 *
2344 * If this function returns 0, then the device is either a non-dm
2345 * device without a merge_bvec_fn, or it is a dm device that is
2346 * able to split any bios it receives that are too big.
2347 */
2348int dm_queue_merge_is_compulsory(struct request_queue *q)
2349{
2350 struct mapped_device *dev_md;
2351
2352 if (!q->merge_bvec_fn)
2353 return 0;
2354
2355 if (q->make_request_fn == dm_request) {
2356 dev_md = q->queuedata;
2357 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2358 return 0;
2359 }
2360
2361 return 1;
2362}
2363
2364static int dm_device_merge_is_compulsory(struct dm_target *ti,
2365 struct dm_dev *dev, sector_t start,
2366 sector_t len, void *data)
2367{
2368 struct block_device *bdev = dev->bdev;
2369 struct request_queue *q = bdev_get_queue(bdev);
2370
2371 return dm_queue_merge_is_compulsory(q);
2372}
2373
2374/*
2375 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2376 * on the properties of the underlying devices.
2377 */
2378static int dm_table_merge_is_optional(struct dm_table *table)
2379{
2380 unsigned i = 0;
2381 struct dm_target *ti;
2382
2383 while (i < dm_table_get_num_targets(table)) {
2384 ti = dm_table_get_target(table, i++);
2385
2386 if (ti->type->iterate_devices &&
2387 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2388 return 0;
2389 }
2390
2391 return 1;
2392}
2393
2394/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002395 * Returns old map, which caller must destroy.
2396 */
2397static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2398 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002400 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002401 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 sector_t size;
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002403 int merge_is_optional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002406
2407 /*
2408 * Wipe any geometry if the size of the table changed.
2409 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002410 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002411 memset(&md->geometry, 0, sizeof(md->geometry));
2412
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002413 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002415 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002416
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002417 /*
2418 * The queue hasn't been stopped yet, if the old table type wasn't
2419 * for request-based during suspension. So stop it to prevent
2420 * I/O mapping before resume.
2421 * This must be done before setting the queue restrictions,
2422 * because request-based dm may be run just after the setting.
2423 */
2424 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2425 stop_queue(q);
2426
2427 __bind_mempools(md, t);
2428
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002429 merge_is_optional = dm_table_merge_is_optional(t);
2430
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002431 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002432 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002433 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2434
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002435 dm_table_set_restrictions(t, q, limits);
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002436 if (merge_is_optional)
2437 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2438 else
2439 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002440 if (old_map)
2441 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002442
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002443 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Alasdair G Kergona7940152009-12-10 23:52:23 +00002446/*
2447 * Returns unbound table for the caller to free.
2448 */
2449static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002451 struct dm_table *map = rcu_dereference_protected(md->map, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002454 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302457 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002458 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002459
2460 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
2463/*
2464 * Constructor for a new device.
2465 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002466int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 struct mapped_device *md;
2469
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002470 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (!md)
2472 return -ENXIO;
2473
Milan Broz784aae72009-01-06 03:05:12 +00002474 dm_sysfs_init(md);
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 *result = md;
2477 return 0;
2478}
2479
Mike Snitzera5664da2010-08-12 04:14:01 +01002480/*
2481 * Functions to manage md->type.
2482 * All are required to hold md->type_lock.
2483 */
2484void dm_lock_md_type(struct mapped_device *md)
2485{
2486 mutex_lock(&md->type_lock);
2487}
2488
2489void dm_unlock_md_type(struct mapped_device *md)
2490{
2491 mutex_unlock(&md->type_lock);
2492}
2493
2494void dm_set_md_type(struct mapped_device *md, unsigned type)
2495{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002496 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002497 md->type = type;
2498}
2499
2500unsigned dm_get_md_type(struct mapped_device *md)
2501{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002502 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002503 return md->type;
2504}
2505
Mike Snitzere5863d92014-12-17 21:08:12 -05002506static bool dm_md_type_request_based(struct mapped_device *md)
2507{
2508 unsigned table_type = dm_get_md_type(md);
2509
2510 return (table_type == DM_TYPE_REQUEST_BASED ||
2511 table_type == DM_TYPE_MQ_REQUEST_BASED);
2512}
2513
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002514struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2515{
2516 return md->immutable_target_type;
2517}
2518
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002519/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002520 * The queue_limits are only valid as long as you have a reference
2521 * count on 'md'.
2522 */
2523struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2524{
2525 BUG_ON(!atomic_read(&md->holders));
2526 return &md->queue->limits;
2527}
2528EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2529
2530/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002531 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2532 */
2533static int dm_init_request_based_queue(struct mapped_device *md)
2534{
2535 struct request_queue *q = NULL;
2536
2537 if (md->queue->elevator)
2538 return 1;
2539
2540 /* Fully initialize the queue */
2541 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2542 if (!q)
2543 return 0;
2544
2545 md->queue = q;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002546 dm_init_md_queue(md);
2547 blk_queue_softirq_done(md->queue, dm_softirq_done);
2548 blk_queue_prep_rq(md->queue, dm_prep_fn);
2549 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002550
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002551 /* Also initialize the request-based DM worker thread */
2552 init_kthread_worker(&md->kworker);
2553 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2554 "kdmwork-%s", dm_device_name(md));
2555
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002556 elv_register_queue(md->queue);
2557
2558 return 1;
2559}
2560
2561/*
2562 * Setup the DM device's queue based on md's type
2563 */
2564int dm_setup_md_queue(struct mapped_device *md)
2565{
Mike Snitzere5863d92014-12-17 21:08:12 -05002566 if (dm_md_type_request_based(md) && !dm_init_request_based_queue(md)) {
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002567 DMWARN("Cannot initialize queue for request-based mapped device");
2568 return -EINVAL;
2569 }
2570
2571 return 0;
2572}
2573
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002574struct mapped_device *dm_get_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
2576 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 unsigned minor = MINOR(dev);
2578
2579 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2580 return NULL;
2581
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002582 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 md = idr_find(&_minor_idr, minor);
Mikulas Patocka2bec1f42015-02-17 14:30:53 -05002585 if (md) {
2586 if ((md == MINOR_ALLOCED ||
2587 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2588 dm_deleting_md(md) ||
2589 test_bit(DMF_FREEING, &md->flags))) {
2590 md = NULL;
2591 goto out;
2592 }
2593 dm_get(md);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002596out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002597 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
David Teigland637842c2006-01-06 00:20:00 -08002599 return md;
2600}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002601EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002602
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002603void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002604{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002605 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
2608void dm_set_mdptr(struct mapped_device *md, void *ptr)
2609{
2610 md->interface_ptr = ptr;
2611}
2612
2613void dm_get(struct mapped_device *md)
2614{
2615 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002616 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002619const char *dm_device_name(struct mapped_device *md)
2620{
2621 return md->name;
2622}
2623EXPORT_SYMBOL_GPL(dm_device_name);
2624
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002625static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002627 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002628 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002630 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002631
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002632 spin_lock(&_minor_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002633 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002634 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2635 set_bit(DMF_FREEING, &md->flags);
2636 spin_unlock(&_minor_lock);
2637
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002638 if (dm_request_based(md))
2639 flush_kthread_worker(&md->kworker);
2640
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002641 if (!dm_suspended_md(md)) {
2642 dm_table_presuspend_targets(map);
2643 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002645
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002646 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2647 dm_put_live_table(md, srcu_idx);
2648
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002649 /*
2650 * Rare, but there may be I/O requests still going to complete,
2651 * for example. Wait for all references to disappear.
2652 * No one should increment the reference count of the mapped_device,
2653 * after the mapped_device state becomes DMF_FREEING.
2654 */
2655 if (wait)
2656 while (atomic_read(&md->holders))
2657 msleep(1);
2658 else if (atomic_read(&md->holders))
2659 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2660 dm_device_name(md), atomic_read(&md->holders));
2661
2662 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002663 dm_table_destroy(__unbind(md));
2664 free_dev(md);
2665}
2666
2667void dm_destroy(struct mapped_device *md)
2668{
2669 __dm_destroy(md, true);
2670}
2671
2672void dm_destroy_immediate(struct mapped_device *md)
2673{
2674 __dm_destroy(md, false);
2675}
2676
2677void dm_put(struct mapped_device *md)
2678{
2679 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
Edward Goggin79eb8852007-05-09 02:32:56 -07002681EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Mikulas Patocka401600d2009-04-02 19:55:38 +01002683static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002684{
2685 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002686 DECLARE_WAITQUEUE(wait, current);
2687
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002688 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002689
2690 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002691 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002692
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002693 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002694 break;
2695
Mikulas Patocka401600d2009-04-02 19:55:38 +01002696 if (interruptible == TASK_INTERRUPTIBLE &&
2697 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002698 r = -EINTR;
2699 break;
2700 }
2701
2702 io_schedule();
2703 }
2704 set_current_state(TASK_RUNNING);
2705
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002706 remove_wait_queue(&md->wait, &wait);
2707
Milan Broz46125c12008-02-08 02:10:30 +00002708 return r;
2709}
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711/*
2712 * Process the deferred bios
2713 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002714static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Mikulas Patockaef208582009-04-02 19:55:38 +01002716 struct mapped_device *md = container_of(work, struct mapped_device,
2717 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002718 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002719 int srcu_idx;
2720 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002722 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002723
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002724 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002725 spin_lock_irq(&md->deferred_lock);
2726 c = bio_list_pop(&md->deferred);
2727 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002728
Tejun Heo6a8736d2010-09-08 18:07:00 +02002729 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002730 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002731
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002732 if (dm_request_based(md))
2733 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002734 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002735 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002736 }
Milan Broz73d410c2008-02-08 02:10:25 +00002737
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002738 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002741static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002742{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002743 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002744 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002745 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002746}
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002749 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002751struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
Mike Christie87eb5b22013-03-01 22:45:48 +00002753 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002754 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002755 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Daniel Walkere61290a2008-02-08 02:10:08 +00002757 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002760 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002761 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Mike Snitzer3ae70652012-09-26 23:45:45 +01002763 /*
2764 * If the new table has no data devices, retain the existing limits.
2765 * This helps multipath with queue_if_no_path if all paths disappear,
2766 * then new I/O is queued based on these limits, and then some paths
2767 * reappear.
2768 */
2769 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002770 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002771 if (live_map)
2772 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002773 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002774 }
2775
Mike Christie87eb5b22013-03-01 22:45:48 +00002776 if (!live_map) {
2777 r = dm_calculate_queue_limits(table, &limits);
2778 if (r) {
2779 map = ERR_PTR(r);
2780 goto out;
2781 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002782 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002783
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002784 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002786out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002787 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002788 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
2791/*
2792 * Functions to lock and unlock any filesystem running on the
2793 * device.
2794 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002795static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002797 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002800
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002801 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002802 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002803 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002804 md->frozen_sb = NULL;
2805 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002806 }
2807
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002808 set_bit(DMF_FROZEN, &md->flags);
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return 0;
2811}
2812
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002813static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002815 if (!test_bit(DMF_FROZEN, &md->flags))
2816 return;
2817
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002818 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002820 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
2823/*
Mike Snitzerffcc3932014-10-28 18:34:52 -04002824 * If __dm_suspend returns 0, the device is completely quiescent
2825 * now. There is no request-processing activity. All new requests
2826 * are being added to md->deferred list.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002827 *
Mike Snitzerffcc3932014-10-28 18:34:52 -04002828 * Caller must hold md->suspend_lock
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002829 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04002830static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2831 unsigned suspend_flags, int interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002833 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2834 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2835 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002837 /*
2838 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2839 * This flag is cleared before dm_suspend returns.
2840 */
2841 if (noflush)
2842 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2843
Mike Snitzerd67ee212014-10-28 20:13:31 -04002844 /*
2845 * This gets reverted if there's an error later and the targets
2846 * provide the .presuspend_undo hook.
2847 */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002848 dm_table_presuspend_targets(map);
2849
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002850 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002851 * Flush I/O to the device.
2852 * Any I/O submitted after lock_fs() may not be flushed.
2853 * noflush takes precedence over do_lockfs.
2854 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002855 */
2856 if (!noflush && do_lockfs) {
2857 r = lock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002858 if (r) {
2859 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002860 return r;
Mike Snitzerd67ee212014-10-28 20:13:31 -04002861 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002865 * Here we must make sure that no processes are submitting requests
2866 * to target drivers i.e. no one may be executing
2867 * __split_and_process_bio. This is called from dm_request and
2868 * dm_wq_work.
2869 *
2870 * To get all processes out of __split_and_process_bio in dm_request,
2871 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002872 * __split_and_process_bio from dm_request and quiesce the thread
2873 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2874 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002876 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002877 if (map)
2878 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002880 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002881 * Stop md->queue before flushing md->wq in case request-based
2882 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002883 */
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002884 if (dm_request_based(md)) {
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002885 stop_queue(md->queue);
Keith Busch2eb6e1e2014-10-17 17:46:36 -06002886 flush_kthread_worker(&md->kworker);
2887 }
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002888
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002889 flush_workqueue(md->wq);
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002892 * At this point no more requests are entering target request routines.
2893 * We call dm_wait_for_completion to wait for all existing requests
2894 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 */
Mike Snitzerffcc3932014-10-28 18:34:52 -04002896 r = dm_wait_for_completion(md, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Milan Broz6d6f10d2008-02-08 02:10:22 +00002898 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002899 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Hannes Reinecke41abc4e2014-11-05 14:35:50 +01002900 if (map)
2901 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002904 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002905 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002906
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002907 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002908 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002909
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002910 unlock_fs(md);
Mike Snitzerd67ee212014-10-28 20:13:31 -04002911 dm_table_presuspend_undo_targets(map);
Mike Snitzerffcc3932014-10-28 18:34:52 -04002912 /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002913 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002914
Mike Snitzerffcc3932014-10-28 18:34:52 -04002915 return r;
2916}
2917
2918/*
2919 * We need to be able to change a mapping table under a mounted
2920 * filesystem. For example we might want to move some data in
2921 * the background. Before the table can be swapped with
2922 * dm_bind_table, dm_suspend must be called to flush any in
2923 * flight bios and ensure that any further io gets deferred.
2924 */
2925/*
2926 * Suspend mechanism in request-based dm.
2927 *
2928 * 1. Flush all I/Os by lock_fs() if needed.
2929 * 2. Stop dispatching any I/O by stopping the request_queue.
2930 * 3. Wait for all in-flight I/Os to be completed or requeued.
2931 *
2932 * To abort suspend, start the request_queue.
2933 */
2934int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2935{
2936 struct dm_table *map = NULL;
2937 int r = 0;
2938
2939retry:
2940 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2941
2942 if (dm_suspended_md(md)) {
2943 r = -EINVAL;
2944 goto out_unlock;
2945 }
2946
2947 if (dm_suspended_internally_md(md)) {
2948 /* already internally suspended, wait for internal resume */
2949 mutex_unlock(&md->suspend_lock);
2950 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2951 if (r)
2952 return r;
2953 goto retry;
2954 }
2955
Eric Dumazeta12f5d42014-11-23 09:34:29 -08002956 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04002957
2958 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
2959 if (r)
2960 goto out_unlock;
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 set_bit(DMF_SUSPENDED, &md->flags);
2963
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002964 dm_table_postsuspend_targets(map);
2965
Alasdair G Kergond2874832006-11-08 17:44:43 -08002966out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002967 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002968 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
Mike Snitzerffcc3932014-10-28 18:34:52 -04002971static int __dm_resume(struct mapped_device *md, struct dm_table *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Mike Snitzerffcc3932014-10-28 18:34:52 -04002973 if (map) {
2974 int r = dm_table_resume_targets(map);
2975 if (r)
2976 return r;
2977 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002978
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002979 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002980
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002981 /*
2982 * Flushing deferred I/Os must be done after targets are resumed
2983 * so that mapping of targets can work correctly.
2984 * Request-based dm is queueing the deferred I/Os in its request_queue.
2985 */
2986 if (dm_request_based(md))
2987 start_queue(md->queue);
2988
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002989 unlock_fs(md);
2990
Mike Snitzerffcc3932014-10-28 18:34:52 -04002991 return 0;
2992}
2993
2994int dm_resume(struct mapped_device *md)
2995{
2996 int r = -EINVAL;
2997 struct dm_table *map = NULL;
2998
2999retry:
3000 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3001
3002 if (!dm_suspended_md(md))
3003 goto out;
3004
3005 if (dm_suspended_internally_md(md)) {
3006 /* already internally suspended, wait for internal resume */
3007 mutex_unlock(&md->suspend_lock);
3008 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3009 if (r)
3010 return r;
3011 goto retry;
3012 }
3013
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003014 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003015 if (!map || !dm_table_get_size(map))
3016 goto out;
3017
3018 r = __dm_resume(md, map);
3019 if (r)
3020 goto out;
3021
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003022 clear_bit(DMF_SUSPENDED, &md->flags);
3023
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003024 r = 0;
3025out:
Daniel Walkere61290a2008-02-08 02:10:08 +00003026 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07003027
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07003028 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003031/*
3032 * Internal suspend/resume works like userspace-driven suspend. It waits
3033 * until all bios finish and prevents issuing new bios to the target drivers.
3034 * It may be used only from the kernel.
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003035 */
3036
Mike Snitzerffcc3932014-10-28 18:34:52 -04003037static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3038{
3039 struct dm_table *map = NULL;
3040
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003041 if (md->internal_suspend_count++)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003042 return; /* nested internal suspend */
3043
3044 if (dm_suspended_md(md)) {
3045 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3046 return; /* nest suspend */
3047 }
3048
Eric Dumazeta12f5d42014-11-23 09:34:29 -08003049 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
Mike Snitzerffcc3932014-10-28 18:34:52 -04003050
3051 /*
3052 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3053 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3054 * would require changing .presuspend to return an error -- avoid this
3055 * until there is a need for more elaborate variants of internal suspend.
3056 */
3057 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3058
3059 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3060
3061 dm_table_postsuspend_targets(map);
3062}
3063
3064static void __dm_internal_resume(struct mapped_device *md)
3065{
Mikulas Patocka96b26c82015-01-08 18:52:26 -05003066 BUG_ON(!md->internal_suspend_count);
3067
3068 if (--md->internal_suspend_count)
Mike Snitzerffcc3932014-10-28 18:34:52 -04003069 return; /* resume from nested internal suspend */
3070
3071 if (dm_suspended_md(md))
3072 goto done; /* resume from nested suspend */
3073
3074 /*
3075 * NOTE: existing callers don't need to call dm_table_resume_targets
3076 * (which may fail -- so best to avoid it for now by passing NULL map)
3077 */
3078 (void) __dm_resume(md, NULL);
3079
3080done:
3081 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3082 smp_mb__after_atomic();
3083 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3084}
3085
3086void dm_internal_suspend_noflush(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003087{
3088 mutex_lock(&md->suspend_lock);
Mike Snitzerffcc3932014-10-28 18:34:52 -04003089 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3090 mutex_unlock(&md->suspend_lock);
3091}
3092EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3093
3094void dm_internal_resume(struct mapped_device *md)
3095{
3096 mutex_lock(&md->suspend_lock);
3097 __dm_internal_resume(md);
3098 mutex_unlock(&md->suspend_lock);
3099}
3100EXPORT_SYMBOL_GPL(dm_internal_resume);
3101
3102/*
3103 * Fast variants of internal suspend/resume hold md->suspend_lock,
3104 * which prevents interaction with userspace-driven suspend.
3105 */
3106
3107void dm_internal_suspend_fast(struct mapped_device *md)
3108{
3109 mutex_lock(&md->suspend_lock);
3110 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003111 return;
3112
3113 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3114 synchronize_srcu(&md->io_barrier);
3115 flush_workqueue(md->wq);
3116 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3117}
3118
Mike Snitzerffcc3932014-10-28 18:34:52 -04003119void dm_internal_resume_fast(struct mapped_device *md)
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003120{
Mike Snitzerffcc3932014-10-28 18:34:52 -04003121 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04003122 goto done;
3123
3124 dm_queue_flush(md);
3125
3126done:
3127 mutex_unlock(&md->suspend_lock);
3128}
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130/*-----------------------------------------------------------------
3131 * Event notification.
3132 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003133int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01003134 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003135{
Milan Broz60935eb2009-06-22 10:12:30 +01003136 char udev_cookie[DM_COOKIE_LENGTH];
3137 char *envp[] = { udev_cookie, NULL };
3138
3139 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003140 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01003141 else {
3142 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
3143 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00003144 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
3145 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01003146 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00003147}
3148
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003149uint32_t dm_next_uevent_seq(struct mapped_device *md)
3150{
3151 return atomic_add_return(1, &md->uevent_seq);
3152}
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154uint32_t dm_get_event_nr(struct mapped_device *md)
3155{
3156 return atomic_read(&md->event_nr);
3157}
3158
3159int dm_wait_event(struct mapped_device *md, int event_nr)
3160{
3161 return wait_event_interruptible(md->eventq,
3162 (event_nr != atomic_read(&md->event_nr)));
3163}
3164
Mike Anderson7a8c3d32007-10-19 22:48:01 +01003165void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3166{
3167 unsigned long flags;
3168
3169 spin_lock_irqsave(&md->uevent_lock, flags);
3170 list_add(elist, &md->uevent_list);
3171 spin_unlock_irqrestore(&md->uevent_lock, flags);
3172}
3173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174/*
3175 * The gendisk is only valid as long as you have a reference
3176 * count on 'md'.
3177 */
3178struct gendisk *dm_disk(struct mapped_device *md)
3179{
3180 return md->disk;
3181}
3182
Milan Broz784aae72009-01-06 03:05:12 +00003183struct kobject *dm_kobject(struct mapped_device *md)
3184{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003185 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00003186}
3187
Milan Broz784aae72009-01-06 03:05:12 +00003188struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3189{
3190 struct mapped_device *md;
3191
Mikulas Patocka2995fa72014-01-13 19:37:54 -05003192 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00003193
Milan Broz4d89b7b2009-06-22 10:12:11 +01003194 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00003195 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01003196 return NULL;
3197
Milan Broz784aae72009-01-06 03:05:12 +00003198 dm_get(md);
3199 return md;
3200}
3201
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00003202int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
3204 return test_bit(DMF_SUSPENDED, &md->flags);
3205}
3206
Mike Snitzerffcc3932014-10-28 18:34:52 -04003207int dm_suspended_internally_md(struct mapped_device *md)
3208{
3209 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3210}
3211
Mikulas Patocka2c140a22013-11-01 18:27:41 -04003212int dm_test_deferred_remove_flag(struct mapped_device *md)
3213{
3214 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3215}
3216
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003217int dm_suspended(struct dm_target *ti)
3218{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003219 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00003220}
3221EXPORT_SYMBOL_GPL(dm_suspended);
3222
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003223int dm_noflush_suspending(struct dm_target *ti)
3224{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00003225 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08003226}
3227EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3228
Mikulas Patockac0820cf2012-12-21 20:23:38 +00003229struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003230{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003231 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3232 struct kmem_cache *cachep;
Mike Snitzere5863d92014-12-17 21:08:12 -05003233 unsigned int pool_size = 0;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003234 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003235
3236 if (!pools)
3237 return NULL;
3238
Mike Snitzere5863d92014-12-17 21:08:12 -05003239 switch (type) {
3240 case DM_TYPE_BIO_BASED:
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003241 cachep = _io_cache;
Mike Snitzere8603132013-09-12 18:06:12 -04003242 pool_size = dm_get_reserved_bio_based_ios();
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003243 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
Mike Snitzere5863d92014-12-17 21:08:12 -05003244 break;
3245 case DM_TYPE_REQUEST_BASED:
Mike Snitzerf4790822013-09-12 18:06:12 -04003246 pool_size = dm_get_reserved_rq_based_ios();
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003247 pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3248 if (!pools->rq_pool)
3249 goto out;
Mike Snitzere5863d92014-12-17 21:08:12 -05003250 /* fall through to setup remaining rq-based pools */
3251 case DM_TYPE_MQ_REQUEST_BASED:
3252 cachep = _rq_tio_cache;
3253 if (!pool_size)
3254 pool_size = dm_get_reserved_rq_based_ios();
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003255 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3256 /* per_bio_data_size is not used. See __bind_mempools(). */
3257 WARN_ON(per_bio_data_size != 0);
Mike Snitzere5863d92014-12-17 21:08:12 -05003258 break;
3259 default:
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003260 goto out;
Mike Snitzere5863d92014-12-17 21:08:12 -05003261 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003262
Mike Snitzer6cfa5852013-09-12 18:06:11 -04003263 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003264 if (!pools->io_pool)
3265 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003266
Junichi Nomura3d8aab22014-10-03 11:55:26 +00003267 pools->bs = bioset_create_nobvec(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003268 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003269 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003270
Martin K. Petersena91a2782011-03-17 11:11:05 +01003271 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003272 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01003273
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003274 return pools;
3275
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00003276out:
3277 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003278
3279 return NULL;
3280}
3281
3282void dm_free_md_mempools(struct dm_md_mempools *pools)
3283{
3284 if (!pools)
3285 return;
3286
3287 if (pools->io_pool)
3288 mempool_destroy(pools->io_pool);
3289
Mike Snitzer1ae49ea2014-12-05 17:11:05 -05003290 if (pools->rq_pool)
3291 mempool_destroy(pools->rq_pool);
3292
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01003293 if (pools->bs)
3294 bioset_free(pools->bs);
3295
3296 kfree(pools);
3297}
3298
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003299static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 .open = dm_blk_open,
3301 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07003302 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08003303 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 .owner = THIS_MODULE
3305};
3306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307/*
3308 * module hooks
3309 */
3310module_init(dm_init);
3311module_exit(dm_exit);
3312
3313module_param(major, uint, 0);
3314MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04003315
Mike Snitzere8603132013-09-12 18:06:12 -04003316module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3317MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3318
Mike Snitzerf4790822013-09-12 18:06:12 -04003319module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3320MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322MODULE_DESCRIPTION(DM_NAME " driver");
3323MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3324MODULE_LICENSE("GPL");