blob: b3e26c7d141771c74d24726f8b5a56ea2134b156 [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>
Li Zefan55782132009-06-09 13:43:05 +080022
23#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Namhyung Kim71a16732011-10-31 20:18:54 +000027#ifdef CONFIG_PRINTK
28/*
29 * ratelimit state to be used in DMXXX_LIMIT().
30 */
31DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
32 DEFAULT_RATELIMIT_INTERVAL,
33 DEFAULT_RATELIMIT_BURST);
34EXPORT_SYMBOL(dm_ratelimit_state);
35#endif
36
Milan Broz60935eb2009-06-22 10:12:30 +010037/*
38 * Cookies are numeric values sent with CHANGE and REMOVE
39 * uevents while resuming, removing or renaming the device.
40 */
41#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42#define DM_COOKIE_LENGTH 24
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static const char *_name = DM_NAME;
45
46static unsigned int major = 0;
47static unsigned int _major = 0;
48
Alasdair G Kergond15b7742011-08-02 12:32:01 +010049static DEFINE_IDR(_minor_idr);
50
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070051static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000053 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * One of these is allocated per bio.
55 */
56struct dm_io {
57 struct mapped_device *md;
58 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010060 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080061 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010062 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040063 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000067 * For request-based dm.
68 * One of these is allocated per request.
69 */
70struct dm_rq_target_io {
71 struct mapped_device *md;
72 struct dm_target *ti;
73 struct request *orig, clone;
74 int error;
75 union map_info info;
76};
77
78/*
Kent Overstreet94818742012-09-07 13:44:01 -070079 * For request-based dm - the bio clones we allocate are embedded in these
80 * structs.
81 *
82 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
83 * the bioset is created - this means the bio has to come at the end of the
84 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000085 */
86struct dm_rq_clone_bio_info {
87 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010088 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -070089 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000090};
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092union map_info *dm_get_mapinfo(struct bio *bio)
93{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070094 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010095 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070096 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010099union map_info *dm_get_rq_mapinfo(struct request *rq)
100{
101 if (rq && rq->end_io_data)
102 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
103 return NULL;
104}
105EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
106
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700107#define MINOR_ALLOCED ((void *)-1)
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Bits for the md->flags field.
111 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100112#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800114#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700115#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700116#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800117#define DMF_NOFLUSH_SUSPENDING 5
Mikulas Patockad5b9dd02011-08-02 12:32:04 +0100118#define DMF_MERGE_IS_OPTIONAL 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Milan Broz304f3f62008-02-08 02:11:17 +0000120/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100121 * A dummy definition to make RCU happy.
122 * struct dm_table should never be dereferenced in this file.
123 */
124struct dm_table {
125 int undefined__;
126};
127
128/*
Milan Broz304f3f62008-02-08 02:11:17 +0000129 * Work processed by per-device workqueue.
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100132 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000133 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700135 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100137 /*
138 * The current mapping.
139 * Use dm_get_live_table{_fast} or take suspend_lock for
140 * dereference.
141 */
142 struct dm_table *map;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long flags;
145
Jens Axboe165125e2007-07-24 09:28:11 +0200146 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100147 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100148 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100149 struct mutex type_lock;
150
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000151 struct target_type *immutable_target_type;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800154 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 void *interface_ptr;
157
158 /*
159 * A list of ios that arrived while we were suspended.
160 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200161 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100163 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800164 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100165 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200168 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000169 */
170 struct workqueue_struct *wq;
171
172 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * io objects are allocated from here.
174 */
175 mempool_t *io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Stefan Bader9faf4002006-10-03 01:15:41 -0700177 struct bio_set *bs;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
180 * Event handling.
181 */
182 atomic_t event_nr;
183 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100184 atomic_t uevent_seq;
185 struct list_head uevent_list;
186 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /*
189 * freeze/thaw support require holding onto a super block
190 */
191 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100192 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800193
194 /* forced geometry settings */
195 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000196
197 /* sysfs handle */
198 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100199
Tejun Heod87f4c12010-09-03 11:56:19 +0200200 /* zero-length flush that will be cloned and submitted to targets */
201 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400202
203 struct dm_stats stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100206/*
207 * For mempools pre-allocation at the table loading time.
208 */
209struct dm_md_mempools {
210 mempool_t *io_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100211 struct bio_set *bs;
212};
213
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400214#define RESERVED_BIO_BASED_IOS 16
215#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400216#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800217static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000218static struct kmem_cache *_rq_tio_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700219
Mike Snitzerf4790822013-09-12 18:06:12 -0400220/*
Mike Snitzere8603132013-09-12 18:06:12 -0400221 * Bio-based DM's mempools' reserved IOs set by the user.
222 */
223static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
224
225/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400226 * Request-based DM's mempools' reserved IOs set by the user.
227 */
228static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
229
230static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
231 unsigned def, unsigned max)
232{
233 unsigned ios = ACCESS_ONCE(*reserved_ios);
234 unsigned modified_ios = 0;
235
236 if (!ios)
237 modified_ios = def;
238 else if (ios > max)
239 modified_ios = max;
240
241 if (modified_ios) {
242 (void)cmpxchg(reserved_ios, ios, modified_ios);
243 ios = modified_ios;
244 }
245
246 return ios;
247}
248
Mike Snitzere8603132013-09-12 18:06:12 -0400249unsigned dm_get_reserved_bio_based_ios(void)
250{
251 return __dm_get_reserved_ios(&reserved_bio_based_ios,
252 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
253}
254EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
255
Mike Snitzerf4790822013-09-12 18:06:12 -0400256unsigned dm_get_reserved_rq_based_ios(void)
257{
258 return __dm_get_reserved_ios(&reserved_rq_based_ios,
259 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
260}
261EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static int __init local_init(void)
264{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100265 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100268 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100270 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000272 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
273 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100274 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000275
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100276 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100277 if (r)
Jun'ichi Nomura23e50832013-03-01 22:45:48 +0000278 goto out_free_rq_tio_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 _major = major;
281 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100282 if (r < 0)
283 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (!_major)
286 _major = r;
287
288 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100289
290out_uevent_exit:
291 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000292out_free_rq_tio_cache:
293 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100294out_free_io_cache:
295 kmem_cache_destroy(_io_cache);
296
297 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300static void local_exit(void)
301{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000302 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700304 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100305 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 _major = 0;
308
309 DMINFO("cleaned up");
310}
311
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000312static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 local_init,
314 dm_target_init,
315 dm_linear_init,
316 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000317 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100318 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400320 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321};
322
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000323static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 local_exit,
325 dm_target_exit,
326 dm_linear_exit,
327 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000328 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100329 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400331 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332};
333
334static int __init dm_init(void)
335{
336 const int count = ARRAY_SIZE(_inits);
337
338 int r, i;
339
340 for (i = 0; i < count; i++) {
341 r = _inits[i]();
342 if (r)
343 goto bad;
344 }
345
346 return 0;
347
348 bad:
349 while (i--)
350 _exits[i]();
351
352 return r;
353}
354
355static void __exit dm_exit(void)
356{
357 int i = ARRAY_SIZE(_exits);
358
359 while (i--)
360 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100361
362 /*
363 * Should be empty by this point.
364 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100365 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368/*
369 * Block device functions
370 */
Mike Anderson432a2122009-12-10 23:52:20 +0000371int dm_deleting_md(struct mapped_device *md)
372{
373 return test_bit(DMF_DELETING, &md->flags);
374}
375
Al Virofe5f9f22008-03-02 10:29:31 -0500376static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct mapped_device *md;
379
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700380 spin_lock(&_minor_lock);
381
Al Virofe5f9f22008-03-02 10:29:31 -0500382 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700383 if (!md)
384 goto out;
385
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700386 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000387 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700388 md = NULL;
389 goto out;
390 }
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700393 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700394
395out:
396 spin_unlock(&_minor_lock);
397
398 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Al Virodb2a1442013-05-05 21:52:57 -0400401static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Al Virofe5f9f22008-03-02 10:29:31 -0500403 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200404
Milan Broz4a1aeb92011-01-13 19:59:48 +0000405 spin_lock(&_minor_lock);
406
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700407 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000409
410 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700413int dm_open_count(struct mapped_device *md)
414{
415 return atomic_read(&md->open_count);
416}
417
418/*
419 * Guarantees nothing is using the device before it's deleted.
420 */
421int dm_lock_for_deletion(struct mapped_device *md)
422{
423 int r = 0;
424
425 spin_lock(&_minor_lock);
426
427 if (dm_open_count(md))
428 r = -EBUSY;
429 else
430 set_bit(DMF_DELETING, &md->flags);
431
432 spin_unlock(&_minor_lock);
433
434 return r;
435}
436
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400437sector_t dm_get_size(struct mapped_device *md)
438{
439 return get_capacity(md->disk);
440}
441
442struct dm_stats *dm_get_stats(struct mapped_device *md)
443{
444 return &md->stats;
445}
446
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800447static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
448{
449 struct mapped_device *md = bdev->bd_disk->private_data;
450
451 return dm_get_geometry(md, geo);
452}
453
Al Virofe5f9f22008-03-02 10:29:31 -0500454static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700455 unsigned int cmd, unsigned long arg)
456{
Al Virofe5f9f22008-03-02 10:29:31 -0500457 struct mapped_device *md = bdev->bd_disk->private_data;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100458 int srcu_idx;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100459 struct dm_table *map;
Milan Brozaa129a22006-10-03 01:15:15 -0700460 struct dm_target *tgt;
461 int r = -ENOTTY;
462
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100463retry:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100464 map = dm_get_live_table(md, &srcu_idx);
465
Milan Brozaa129a22006-10-03 01:15:15 -0700466 if (!map || !dm_table_get_size(map))
467 goto out;
468
469 /* We only support devices that have a single target */
470 if (dm_table_get_num_targets(map) != 1)
471 goto out;
472
473 tgt = dm_table_get_target(map, 0);
474
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000475 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700476 r = -EAGAIN;
477 goto out;
478 }
479
480 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400481 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700482
483out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100484 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700485
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100486 if (r == -ENOTCONN) {
487 msleep(10);
488 goto retry;
489 }
490
Milan Brozaa129a22006-10-03 01:15:15 -0700491 return r;
492}
493
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100494static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 return mempool_alloc(md->io_pool, GFP_NOIO);
497}
498
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100499static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 mempool_free(io, md->io_pool);
502}
503
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100504static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Mikulas Patockadba14162012-10-12 21:02:15 +0100506 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000509static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
510 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100511{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000512 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100513}
514
515static void free_rq_tio(struct dm_rq_target_io *tio)
516{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000517 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100518}
519
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000520static int md_in_flight(struct mapped_device *md)
521{
522 return atomic_read(&md->pending[READ]) +
523 atomic_read(&md->pending[WRITE]);
524}
525
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800526static void start_io_acct(struct dm_io *io)
527{
528 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400529 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900530 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400531 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800532
533 io->start_time = jiffies;
534
Tejun Heo074a7ac2008-08-25 19:56:14 +0900535 cpu = part_stat_lock();
536 part_round_stats(cpu, &dm_disk(md)->part0);
537 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100538 atomic_set(&dm_disk(md)->part0.in_flight[rw],
539 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400540
541 if (unlikely(dm_stats_used(&md->stats)))
542 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
543 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800544}
545
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000546static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800547{
548 struct mapped_device *md = io->md;
549 struct bio *bio = io->bio;
550 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900551 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800552 int rw = bio_data_dir(bio);
553
Tejun Heo074a7ac2008-08-25 19:56:14 +0900554 cpu = part_stat_lock();
555 part_round_stats(cpu, &dm_disk(md)->part0);
556 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
557 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800558
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400559 if (unlikely(dm_stats_used(&md->stats)))
560 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
561 bio_sectors(bio), true, duration, &io->stats_aux);
562
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100563 /*
564 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200565 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100566 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100567 pending = atomic_dec_return(&md->pending[rw]);
568 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200569 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800570
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000571 /* nudge anyone waiting on suspend queue */
572 if (!pending)
573 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
577 * Add the bio to the list of deferred io.
578 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100579static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200581 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200583 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200585 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200586 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/*
590 * Everyone (including functions in this file), should use this
591 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100592 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100594struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100596 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100598 return srcu_dereference(md->map, &md->io_barrier);
599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100601void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
602{
603 srcu_read_unlock(&md->io_barrier, srcu_idx);
604}
605
606void dm_sync_table(struct mapped_device *md)
607{
608 synchronize_srcu(&md->io_barrier);
609 synchronize_rcu_expedited();
610}
611
612/*
613 * A fast alternative to dm_get_live_table/dm_put_live_table.
614 * The caller must not block between these two functions.
615 */
616static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
617{
618 rcu_read_lock();
619 return rcu_dereference(md->map);
620}
621
622static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
623{
624 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800627/*
628 * Get the geometry associated with a dm device
629 */
630int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
631{
632 *geo = md->geometry;
633
634 return 0;
635}
636
637/*
638 * Set the geometry of a device.
639 */
640int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
641{
642 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
643
644 if (geo->start > sz) {
645 DMWARN("Start sector is beyond the geometry limits.");
646 return -EINVAL;
647 }
648
649 md->geometry = *geo;
650
651 return 0;
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/*-----------------------------------------------------------------
655 * CRUD START:
656 * A more elegant soln is in the works that uses the queue
657 * merge fn, unfortunately there are a couple of changes to
658 * the block layer that I want to make for this. So in the
659 * interests of getting something for people to use I give
660 * you this clearly demarcated crap.
661 *---------------------------------------------------------------*/
662
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800663static int __noflush_suspending(struct mapped_device *md)
664{
665 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/*
669 * Decrements the number of outstanding ios that a bio has been
670 * cloned into, completing the original io if necc.
671 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800672static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800674 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000675 int io_error;
676 struct bio *bio;
677 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800678
679 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100680 if (unlikely(error)) {
681 spin_lock_irqsave(&io->endio_lock, flags);
682 if (!(io->error > 0 && __noflush_suspending(md)))
683 io->error = error;
684 spin_unlock_irqrestore(&io->endio_lock, flags);
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800688 if (io->error == DM_ENDIO_REQUEUE) {
689 /*
690 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800691 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100692 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200693 if (__noflush_suspending(md))
694 bio_list_add_head(&md->deferred, io->bio);
695 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800696 /* noflush suspend was interrupted. */
697 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100698 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800699 }
700
Milan Brozb35f8ca2009-03-16 17:44:36 +0000701 io_error = io->error;
702 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200703 end_io_acct(io);
704 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100705
Tejun Heo6a8736d2010-09-08 18:07:00 +0200706 if (io_error == DM_ENDIO_REQUEUE)
707 return;
708
Mike Snitzerb372d362010-09-08 18:07:01 +0200709 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100710 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200711 * Preflush done for flush with data, reissue
712 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100713 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200714 bio->bi_rw &= ~REQ_FLUSH;
715 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100716 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200717 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700718 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200719 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722}
723
NeilBrown6712ecf2007-09-27 12:47:43 +0200724static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100727 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000728 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700729 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 dm_endio_fn endio = tio->ti->type->end_io;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
733 error = -EIO;
734
735 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000736 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800737 if (r < 0 || r == DM_ENDIO_REQUEUE)
738 /*
739 * error and requeue request are handled
740 * in dec_pending().
741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800743 else if (r == DM_ENDIO_INCOMPLETE)
744 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200745 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800746 else if (r) {
747 DMWARN("unimplemented target endio return value: %d", r);
748 BUG();
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Stefan Bader9faf4002006-10-03 01:15:41 -0700752 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000753 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100756/*
757 * Partial completion handling for request-based dm
758 */
759static void end_clone_bio(struct bio *clone, int error)
760{
761 struct dm_rq_clone_bio_info *info = clone->bi_private;
762 struct dm_rq_target_io *tio = info->tio;
763 struct bio *bio = info->orig;
764 unsigned int nr_bytes = info->orig->bi_size;
765
766 bio_put(clone);
767
768 if (tio->error)
769 /*
770 * An error has already been detected on the request.
771 * Once error occurred, just let clone->end_io() handle
772 * the remainder.
773 */
774 return;
775 else if (error) {
776 /*
777 * Don't notice the error to the upper layer yet.
778 * The error handling decision is made by the target driver,
779 * when the request is completed.
780 */
781 tio->error = error;
782 return;
783 }
784
785 /*
786 * I/O for the bio successfully completed.
787 * Notice the data completion to the upper layer.
788 */
789
790 /*
791 * bios are processed from the head of the list.
792 * So the completing bio should always be rq->bio.
793 * If it's not, something wrong is happening.
794 */
795 if (tio->orig->bio != bio)
796 DMERR("bio completion is going in the middle of the request");
797
798 /*
799 * Update the original request.
800 * Do not use blk_end_request() here, because it may complete
801 * the original request before the clone, and break the ordering.
802 */
803 blk_update_request(tio->orig, 0, nr_bytes);
804}
805
806/*
807 * Don't touch any member of the md after calling this function because
808 * the md may be freed in dm_put() at the end of this function.
809 * Or do dm_get() before calling this function and dm_put() later.
810 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000811static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100812{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000813 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100814
815 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000816 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100817 wake_up(&md->wait);
818
Jens Axboea8c32a52012-11-06 12:24:26 +0100819 /*
820 * Run this off this callpath, as drivers could invoke end_io while
821 * inside their request_fn (and holding the queue lock). Calling
822 * back into ->request_fn() could deadlock attempting to grab the
823 * queue lock again.
824 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100825 if (run_queue)
Jens Axboea8c32a52012-11-06 12:24:26 +0100826 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100827
828 /*
829 * dm_put() must be at the end of this function. See the comment above
830 */
831 dm_put(md);
832}
833
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100834static void free_rq_clone(struct request *clone)
835{
836 struct dm_rq_target_io *tio = clone->end_io_data;
837
838 blk_rq_unprep_clone(clone);
839 free_rq_tio(tio);
840}
841
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000842/*
843 * Complete the clone and the original request.
844 * Must be called without queue lock.
845 */
846static void dm_end_request(struct request *clone, int error)
847{
848 int rw = rq_data_dir(clone);
849 struct dm_rq_target_io *tio = clone->end_io_data;
850 struct mapped_device *md = tio->md;
851 struct request *rq = tio->orig;
852
Tejun Heo29e40132010-09-08 18:07:00 +0200853 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000854 rq->errors = clone->errors;
855 rq->resid_len = clone->resid_len;
856
857 if (rq->sense)
858 /*
859 * We are using the sense buffer of the original
860 * request.
861 * So setting the length of the sense data is enough.
862 */
863 rq->sense_len = clone->sense_len;
864 }
865
866 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200867 blk_end_request_all(rq, error);
868 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000869}
870
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100871static void dm_unprep_request(struct request *rq)
872{
873 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100874
875 rq->special = NULL;
876 rq->cmd_flags &= ~REQ_DONTPREP;
877
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100878 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100879}
880
881/*
882 * Requeue the original request of a clone.
883 */
884void dm_requeue_unmapped_request(struct request *clone)
885{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000886 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100887 struct dm_rq_target_io *tio = clone->end_io_data;
888 struct mapped_device *md = tio->md;
889 struct request *rq = tio->orig;
890 struct request_queue *q = rq->q;
891 unsigned long flags;
892
893 dm_unprep_request(rq);
894
895 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100896 blk_requeue_request(q, rq);
897 spin_unlock_irqrestore(q->queue_lock, flags);
898
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000899 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100900}
901EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
902
903static void __stop_queue(struct request_queue *q)
904{
905 blk_stop_queue(q);
906}
907
908static void stop_queue(struct request_queue *q)
909{
910 unsigned long flags;
911
912 spin_lock_irqsave(q->queue_lock, flags);
913 __stop_queue(q);
914 spin_unlock_irqrestore(q->queue_lock, flags);
915}
916
917static void __start_queue(struct request_queue *q)
918{
919 if (blk_queue_stopped(q))
920 blk_start_queue(q);
921}
922
923static void start_queue(struct request_queue *q)
924{
925 unsigned long flags;
926
927 spin_lock_irqsave(q->queue_lock, flags);
928 __start_queue(q);
929 spin_unlock_irqrestore(q->queue_lock, flags);
930}
931
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000932static void dm_done(struct request *clone, int error, bool mapped)
933{
934 int r = error;
935 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100936 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000937
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100938 if (tio->ti) {
939 rq_end_io = tio->ti->type->rq_end_io;
940
941 if (mapped && rq_end_io)
942 r = rq_end_io(tio->ti, clone, error, &tio->info);
943 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000944
945 if (r <= 0)
946 /* The target wants to complete the I/O */
947 dm_end_request(clone, r);
948 else if (r == DM_ENDIO_INCOMPLETE)
949 /* The target will handle the I/O */
950 return;
951 else if (r == DM_ENDIO_REQUEUE)
952 /* The target wants to requeue the I/O */
953 dm_requeue_unmapped_request(clone);
954 else {
955 DMWARN("unimplemented target endio return value: %d", r);
956 BUG();
957 }
958}
959
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100960/*
961 * Request completion handler for request-based dm
962 */
963static void dm_softirq_done(struct request *rq)
964{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000965 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100966 struct request *clone = rq->completion_data;
967 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100968
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000969 if (rq->cmd_flags & REQ_FAILED)
970 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100971
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000972 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100973}
974
975/*
976 * Complete the clone and the original request with the error status
977 * through softirq context.
978 */
979static void dm_complete_request(struct request *clone, int error)
980{
981 struct dm_rq_target_io *tio = clone->end_io_data;
982 struct request *rq = tio->orig;
983
984 tio->error = error;
985 rq->completion_data = clone;
986 blk_complete_request(rq);
987}
988
989/*
990 * Complete the not-mapped clone and the original request with the error status
991 * through softirq context.
992 * Target's rq_end_io() function isn't called.
993 * This may be used when the target's map_rq() function fails.
994 */
995void dm_kill_unmapped_request(struct request *clone, int error)
996{
997 struct dm_rq_target_io *tio = clone->end_io_data;
998 struct request *rq = tio->orig;
999
1000 rq->cmd_flags |= REQ_FAILED;
1001 dm_complete_request(clone, error);
1002}
1003EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1004
1005/*
1006 * Called with the queue lock held
1007 */
1008static void end_clone_request(struct request *clone, int error)
1009{
1010 /*
1011 * For just cleaning up the information of the queue in which
1012 * the clone was dispatched.
1013 * The clone is *NOT* freed actually here because it is alloced from
1014 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1015 */
1016 __blk_put_request(clone->q, clone);
1017
1018 /*
1019 * Actual request completion is done in a softirq context which doesn't
1020 * hold the queue lock. Otherwise, deadlock could occur because:
1021 * - another request may be submitted by the upper level driver
1022 * of the stacking during the completion
1023 * - the submission which requires queue lock may be done
1024 * against this queue
1025 */
1026 dm_complete_request(clone, error);
1027}
1028
Mike Snitzer56a67df2010-08-12 04:14:10 +01001029/*
1030 * Return maximum size of I/O possible at the supplied sector up to the current
1031 * target boundary.
1032 */
1033static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001035 sector_t target_offset = dm_target_offset(ti, sector);
1036
1037 return ti->len - target_offset;
1038}
1039
1040static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1041{
1042 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001043 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001046 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001048 if (ti->max_io_len) {
1049 offset = dm_target_offset(ti, sector);
1050 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1051 max_len = sector_div(offset, ti->max_io_len);
1052 else
1053 max_len = offset & (ti->max_io_len - 1);
1054 max_len = ti->max_io_len - max_len;
1055
1056 if (len > max_len)
1057 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
1060 return len;
1061}
1062
Mike Snitzer542f9032012-07-27 15:08:00 +01001063int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1064{
1065 if (len > UINT_MAX) {
1066 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1067 (unsigned long long)len, UINT_MAX);
1068 ti->error = "Maximum size of target IO is too large";
1069 return -EINVAL;
1070 }
1071
1072 ti->max_io_len = (uint32_t) len;
1073
1074 return 0;
1075}
1076EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1077
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001078static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001081 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001082 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001083 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001084 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 clone->bi_end_io = clone_endio;
1087 clone->bi_private = tio;
1088
1089 /*
1090 * Map the clone. If r == 0 we don't need to do
1091 * anything, the target has assumed ownership of
1092 * this io.
1093 */
1094 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001095 sector = clone->bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001096 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001097 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001099
Mike Snitzerd07335e2010-11-16 12:52:38 +01001100 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1101 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001104 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1105 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001106 md = tio->io->md;
1107 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001108 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001109 } else if (r) {
1110 DMWARN("unimplemented target map return value: %d", r);
1111 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113}
1114
1115struct clone_info {
1116 struct mapped_device *md;
1117 struct dm_table *map;
1118 struct bio *bio;
1119 struct dm_io *io;
1120 sector_t sector;
1121 sector_t sector_count;
1122 unsigned short idx;
1123};
1124
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001125static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1126{
1127 bio->bi_sector = sector;
1128 bio->bi_size = to_bytes(len);
1129}
1130
1131static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1132{
1133 bio->bi_idx = idx;
1134 bio->bi_vcnt = idx + bv_count;
1135 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1136}
1137
1138static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1139 unsigned short idx, unsigned len, unsigned offset,
1140 unsigned trim)
1141{
1142 if (!bio_integrity(bio))
1143 return;
1144
1145 bio_integrity_clone(clone, bio, GFP_NOIO);
1146
1147 if (trim)
1148 bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1149}
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001152 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001154static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
1155 sector_t sector, unsigned short idx,
1156 unsigned offset, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Mikulas Patockadba14162012-10-12 21:02:15 +01001158 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct bio_vec *bv = bio->bi_io_vec + idx;
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 *clone->bi_io_vec = *bv;
1162
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001163 bio_setup_sector(clone, sector, len);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001166 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 clone->bi_vcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 clone->bi_io_vec->bv_offset = offset;
1169 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001170 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001172 clone_bio_integrity(bio, clone, idx, len, offset, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175/*
1176 * Creates a bio that consists of range of complete bvecs.
1177 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001178static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1179 sector_t sector, unsigned short idx,
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001180 unsigned short bv_count, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Mikulas Patockadba14162012-10-12 21:02:15 +01001182 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001183 unsigned trim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Stefan Bader9faf4002006-10-03 01:15:41 -07001185 __bio_clone(clone, bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001186 bio_setup_sector(clone, sector, len);
1187 bio_setup_bv(clone, idx, bv_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001189 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1190 trim = 1;
1191 clone_bio_integrity(bio, clone, idx, len, 0, trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001194static struct dm_target_io *alloc_tio(struct clone_info *ci,
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001195 struct dm_target *ti, int nr_iovecs,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001196 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001197{
Mikulas Patockadba14162012-10-12 21:02:15 +01001198 struct dm_target_io *tio;
1199 struct bio *clone;
1200
1201 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1202 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001203
1204 tio->io = ci->io;
1205 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001206 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001207 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001208
1209 return tio;
1210}
1211
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001212static void __clone_and_map_simple_bio(struct clone_info *ci,
1213 struct dm_target *ti,
1214 unsigned target_bio_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001215{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001216 struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001217 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001218
Mike Snitzer06a426c2010-08-12 04:14:09 +01001219 /*
1220 * Discard requests require the bio's inline iovecs be initialized.
1221 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1222 * and discard, so no need for concern about wasted bvec allocations.
1223 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001224 __bio_clone(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001225 if (len)
1226 bio_setup_sector(clone, ci->sector, len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001227
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001228 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001229}
1230
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001231static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1232 unsigned num_bios, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001233{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001234 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001235
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001236 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001237 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001238}
1239
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001240static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001241{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001242 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001243 struct dm_target *ti;
1244
Mike Snitzerb372d362010-09-08 18:07:01 +02001245 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001246 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001247 __send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001248
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001249 return 0;
1250}
1251
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001252static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1253 sector_t sector, int nr_iovecs,
1254 unsigned short idx, unsigned short bv_count,
1255 unsigned offset, unsigned len,
1256 unsigned split_bvec)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001257{
Mikulas Patockadba14162012-10-12 21:02:15 +01001258 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001259 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001260 unsigned target_bio_nr;
1261 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001262
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001263 /*
1264 * Does the target want to receive duplicate copies of the bio?
1265 */
1266 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1267 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001268
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001269 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1270 tio = alloc_tio(ci, ti, nr_iovecs, target_bio_nr);
1271 if (split_bvec)
1272 clone_split_bio(tio, bio, sector, idx, offset, len);
1273 else
1274 clone_bio(tio, bio, sector, idx, bv_count, len);
1275 __map_bio(tio);
1276 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001277}
1278
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001279typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001280
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001281static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001282{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001283 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001284}
1285
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001286static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001287{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001288 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001289}
1290
1291typedef bool (*is_split_required_fn)(struct dm_target *ti);
1292
1293static bool is_split_required_for_discard(struct dm_target *ti)
1294{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001295 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001296}
1297
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001298static int __send_changing_extent_only(struct clone_info *ci,
1299 get_num_bios_fn get_num_bios,
1300 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001301{
1302 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001303 sector_t len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001304 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001305
Mike Snitzera79245b2010-08-12 04:14:24 +01001306 do {
1307 ti = dm_table_find_target(ci->map, ci->sector);
1308 if (!dm_target_is_valid(ti))
1309 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001310
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001311 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001312 * Even though the device advertised support for this type of
1313 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001314 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001315 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001316 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001317 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1318 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001319 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001320
Mike Snitzer23508a92012-12-21 20:23:37 +00001321 if (is_split_required && !is_split_required(ti))
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001322 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1323 else
1324 len = min(ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001325
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001326 __send_duplicate_bios(ci, ti, num_bios, len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001327
1328 ci->sector += len;
1329 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001330
1331 return 0;
1332}
1333
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001334static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001335{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001336 return __send_changing_extent_only(ci, get_num_discard_bios,
1337 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001338}
1339
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001340static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001341{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001342 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001343}
1344
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001345/*
1346 * Find maximum number of sectors / bvecs we can process with a single bio.
1347 */
1348static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
1349{
1350 struct bio *bio = ci->bio;
1351 sector_t bv_len, total_len = 0;
1352
1353 for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1354 bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
1355
1356 if (bv_len > max)
1357 break;
1358
1359 max -= bv_len;
1360 total_len += bv_len;
1361 }
1362
1363 return total_len;
1364}
1365
1366static int __split_bvec_across_targets(struct clone_info *ci,
1367 struct dm_target *ti, sector_t max)
1368{
1369 struct bio *bio = ci->bio;
1370 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1371 sector_t remaining = to_sector(bv->bv_len);
1372 unsigned offset = 0;
1373 sector_t len;
1374
1375 do {
1376 if (offset) {
1377 ti = dm_table_find_target(ci->map, ci->sector);
1378 if (!dm_target_is_valid(ti))
1379 return -EIO;
1380
1381 max = max_io_len(ci->sector, ti);
1382 }
1383
1384 len = min(remaining, max);
1385
1386 __clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1387 bv->bv_offset + offset, len, 1);
1388
1389 ci->sector += len;
1390 ci->sector_count -= len;
1391 offset += to_bytes(len);
1392 } while (remaining -= len);
1393
1394 ci->idx++;
1395
1396 return 0;
1397}
1398
1399/*
1400 * Select the correct strategy for processing a non-flush bio.
1401 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001402static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Mikulas Patockadba14162012-10-12 21:02:15 +01001404 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001405 struct dm_target *ti;
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001406 sector_t len, max;
1407 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001409 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001410 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001411 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001412 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001413
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001414 ti = dm_table_find_target(ci->map, ci->sector);
1415 if (!dm_target_is_valid(ti))
1416 return -EIO;
1417
Mike Snitzer56a67df2010-08-12 04:14:10 +01001418 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001419
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001420 /*
1421 * Optimise for the simple case where we can do all of
1422 * the remaining io with a single clone.
1423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (ci->sector_count <= max) {
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001425 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1426 ci->idx, bio->bi_vcnt - ci->idx, 0,
1427 ci->sector_count, 0);
1428 ci->sector_count = 0;
1429 return 0;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001432 /*
1433 * There are some bvecs that don't span targets.
1434 * Do as many of these as possible.
1435 */
1436 if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1437 len = __len_within_target(ci, max, &idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001439 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1440 ci->idx, idx - ci->idx, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 ci->sector += len;
1443 ci->sector_count -= len;
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001444 ci->idx = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001448
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001449 /*
1450 * Handle a bvec that must be split between two or more targets.
1451 */
1452 return __split_bvec_across_targets(ci, ti, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001456 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001458static void __split_and_process_bio(struct mapped_device *md,
1459 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001462 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001464 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001465 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001466 return;
1467 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001468
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001469 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ci.io = alloc_io(md);
1472 ci.io->error = 0;
1473 atomic_set(&ci.io->io_count, 1);
1474 ci.io->bio = bio;
1475 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001476 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 ci.sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 ci.idx = bio->bi_idx;
1479
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001480 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001481
Mike Snitzerb372d362010-09-08 18:07:01 +02001482 if (bio->bi_rw & REQ_FLUSH) {
1483 ci.bio = &ci.md->flush_bio;
1484 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001485 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001486 /* dec_pending submits any data associated with flush */
1487 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001488 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001489 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001490 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001491 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001495 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497/*-----------------------------------------------------------------
1498 * CRUD END
1499 *---------------------------------------------------------------*/
1500
Milan Brozf6fccb12008-07-21 12:00:37 +01001501static int dm_merge_bvec(struct request_queue *q,
1502 struct bvec_merge_data *bvm,
1503 struct bio_vec *biovec)
1504{
1505 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001506 struct dm_table *map = dm_get_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001507 struct dm_target *ti;
1508 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001509 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001510
1511 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001512 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001513
1514 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001515 if (!dm_target_is_valid(ti))
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001516 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001517
1518 /*
1519 * Find maximum amount of I/O that won't need splitting
1520 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001521 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001522 (sector_t) BIO_MAX_SECTORS);
1523 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1524 if (max_size < 0)
1525 max_size = 0;
1526
1527 /*
1528 * merge_bvec_fn() returns number of bytes
1529 * it can accept at this offset
1530 * max is precomputed maximal io size
1531 */
1532 if (max_size && ti->type->merge)
1533 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001534 /*
1535 * If the target doesn't support merge method and some of the devices
1536 * provided their merge_bvec method (we know this by looking at
1537 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1538 * entries. So always set max_size to 0, and the code below allows
1539 * just one page.
1540 */
1541 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1542
1543 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001544
Mikulas Patocka50371082008-10-01 14:39:17 +01001545out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001546 dm_put_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001547 /*
1548 * Always allow an entire first page
1549 */
1550 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1551 max_size = biovec->bv_len;
1552
Milan Brozf6fccb12008-07-21 12:00:37 +01001553 return max_size;
1554}
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556/*
1557 * The request function that just remaps the bio built up by
1558 * dm_merge_bvec.
1559 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001560static void _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Kevin Corry12f03a42006-02-01 03:04:52 -08001562 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001564 int cpu;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001565 int srcu_idx;
1566 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001568 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Tejun Heo074a7ac2008-08-25 19:56:14 +09001570 cpu = part_stat_lock();
1571 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1572 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1573 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001574
Tejun Heo6a8736d2010-09-08 18:07:00 +02001575 /* if we're suspended, we have to queue this io for later */
1576 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001577 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Tejun Heo6a8736d2010-09-08 18:07:00 +02001579 if (bio_rw(bio) != READA)
1580 queue_io(md, bio);
1581 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001582 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001583 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001586 __split_and_process_bio(md, map, bio);
1587 dm_put_live_table(md, srcu_idx);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001588 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001589}
1590
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001591int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001592{
1593 return blk_queue_stackable(md->queue);
1594}
1595
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001596static void dm_request(struct request_queue *q, struct bio *bio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001597{
1598 struct mapped_device *md = q->queuedata;
1599
1600 if (dm_request_based(md))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001601 blk_queue_bio(q, bio);
1602 else
1603 _dm_request(q, bio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001604}
1605
1606void dm_dispatch_request(struct request *rq)
1607{
1608 int r;
1609
1610 if (blk_queue_io_stat(rq->q))
1611 rq->cmd_flags |= REQ_IO_STAT;
1612
1613 rq->start_time = jiffies;
1614 r = blk_insert_cloned_request(rq->q, rq);
1615 if (r)
1616 dm_complete_request(rq, r);
1617}
1618EXPORT_SYMBOL_GPL(dm_dispatch_request);
1619
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001620static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1621 void *data)
1622{
1623 struct dm_rq_target_io *tio = data;
Kent Overstreet94818742012-09-07 13:44:01 -07001624 struct dm_rq_clone_bio_info *info =
1625 container_of(bio, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001626
1627 info->orig = bio_orig;
1628 info->tio = tio;
1629 bio->bi_end_io = end_clone_bio;
1630 bio->bi_private = info;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001631
1632 return 0;
1633}
1634
1635static int setup_clone(struct request *clone, struct request *rq,
1636 struct dm_rq_target_io *tio)
1637{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001638 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001639
Tejun Heo29e40132010-09-08 18:07:00 +02001640 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1641 dm_rq_bio_constructor, tio);
1642 if (r)
1643 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001644
Tejun Heo29e40132010-09-08 18:07:00 +02001645 clone->cmd = rq->cmd;
1646 clone->cmd_len = rq->cmd_len;
1647 clone->sense = rq->sense;
1648 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001649 clone->end_io = end_clone_request;
1650 clone->end_io_data = tio;
1651
1652 return 0;
1653}
1654
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001655static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1656 gfp_t gfp_mask)
1657{
1658 struct request *clone;
1659 struct dm_rq_target_io *tio;
1660
1661 tio = alloc_rq_tio(md, gfp_mask);
1662 if (!tio)
1663 return NULL;
1664
1665 tio->md = md;
1666 tio->ti = NULL;
1667 tio->orig = rq;
1668 tio->error = 0;
1669 memset(&tio->info, 0, sizeof(tio->info));
1670
1671 clone = &tio->clone;
1672 if (setup_clone(clone, rq, tio)) {
1673 /* -ENOMEM */
1674 free_rq_tio(tio);
1675 return NULL;
1676 }
1677
1678 return clone;
1679}
1680
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001681/*
1682 * Called with the queue lock held.
1683 */
1684static int dm_prep_fn(struct request_queue *q, struct request *rq)
1685{
1686 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001687 struct request *clone;
1688
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001689 if (unlikely(rq->special)) {
1690 DMWARN("Already has something in rq->special.");
1691 return BLKPREP_KILL;
1692 }
1693
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001694 clone = clone_rq(rq, md, GFP_ATOMIC);
1695 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001696 return BLKPREP_DEFER;
1697
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001698 rq->special = clone;
1699 rq->cmd_flags |= REQ_DONTPREP;
1700
1701 return BLKPREP_OK;
1702}
1703
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001704/*
1705 * Returns:
1706 * 0 : the request has been processed (not requeued)
1707 * !0 : the request has been requeued
1708 */
1709static int map_request(struct dm_target *ti, struct request *clone,
1710 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001711{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001712 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001713 struct dm_rq_target_io *tio = clone->end_io_data;
1714
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001715 tio->ti = ti;
1716 r = ti->type->map_rq(ti, clone, &tio->info);
1717 switch (r) {
1718 case DM_MAPIO_SUBMITTED:
1719 /* The target has taken the I/O to submit by itself later */
1720 break;
1721 case DM_MAPIO_REMAPPED:
1722 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001723 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1724 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001725 dm_dispatch_request(clone);
1726 break;
1727 case DM_MAPIO_REQUEUE:
1728 /* The target wants to requeue the I/O */
1729 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001730 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001731 break;
1732 default:
1733 if (r > 0) {
1734 DMWARN("unimplemented target map return value: %d", r);
1735 BUG();
1736 }
1737
1738 /* The target wants to complete the I/O */
1739 dm_kill_unmapped_request(clone, r);
1740 break;
1741 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001742
1743 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001744}
1745
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001746static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1747{
1748 struct request *clone;
1749
1750 blk_start_request(orig);
1751 clone = orig->special;
1752 atomic_inc(&md->pending[rq_data_dir(clone)]);
1753
1754 /*
1755 * Hold the md reference here for the in-flight I/O.
1756 * We can't rely on the reference count by device opener,
1757 * because the device may be closed during the request completion
1758 * when all bios are completed.
1759 * See the comment in rq_completed() too.
1760 */
1761 dm_get(md);
1762
1763 return clone;
1764}
1765
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001766/*
1767 * q->request_fn for request-based dm.
1768 * Called with the queue lock held.
1769 */
1770static void dm_request_fn(struct request_queue *q)
1771{
1772 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001773 int srcu_idx;
1774 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001775 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001776 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001777 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001778
1779 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001780 * For suspend, check blk_queue_stopped() and increment
1781 * ->pending within a single queue_lock not to increment the
1782 * number of in-flight I/Os after the queue is stopped in
1783 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001784 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001785 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001786 rq = blk_peek_request(q);
1787 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001788 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001789
Tejun Heo29e40132010-09-08 18:07:00 +02001790 /* always use block 0 to find the target for flushes for now */
1791 pos = 0;
1792 if (!(rq->cmd_flags & REQ_FLUSH))
1793 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001794
Tejun Heo29e40132010-09-08 18:07:00 +02001795 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001796 if (!dm_target_is_valid(ti)) {
1797 /*
1798 * Must perform setup, that dm_done() requires,
1799 * before calling dm_kill_unmapped_request
1800 */
1801 DMERR_LIMIT("request attempted access beyond the end of device");
1802 clone = dm_start_request(md, rq);
1803 dm_kill_unmapped_request(clone, -EIO);
1804 continue;
1805 }
Tejun Heo29e40132010-09-08 18:07:00 +02001806
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001807 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001808 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001809
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001810 clone = dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001811
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001812 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001813 if (map_request(ti, clone, md))
1814 goto requeued;
1815
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001816 BUG_ON(!irqs_disabled());
1817 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001818 }
1819
1820 goto out;
1821
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001822requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001823 BUG_ON(!irqs_disabled());
1824 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001825
Jens Axboe7eaceac2011-03-10 08:52:07 +01001826delay_and_out:
1827 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001828out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001829 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001830}
1831
1832int dm_underlying_device_busy(struct request_queue *q)
1833{
1834 return blk_lld_busy(q);
1835}
1836EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1837
1838static int dm_lld_busy(struct request_queue *q)
1839{
1840 int r;
1841 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001842 struct dm_table *map = dm_get_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001843
1844 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1845 r = 1;
1846 else
1847 r = dm_table_any_busy_target(map);
1848
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001849 dm_put_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001850
1851 return r;
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854static int dm_any_congested(void *congested_data, int bdi_bits)
1855{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001856 int r = bdi_bits;
1857 struct mapped_device *md = congested_data;
1858 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001860 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001861 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001862 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001863 /*
1864 * Request-based dm cares about only own queue for
1865 * the query about congestion status of request_queue
1866 */
1867 if (dm_request_based(md))
1868 r = md->queue->backing_dev_info.state &
1869 bdi_bits;
1870 else
1871 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001872 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001873 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001874 }
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return r;
1877}
1878
1879/*-----------------------------------------------------------------
1880 * An IDR is used to keep track of allocated minor numbers.
1881 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001882static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001884 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001886 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889/*
1890 * See if the device with a specific minor # is free.
1891 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001892static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001894 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 if (minor >= (1 << MINORBITS))
1897 return -EINVAL;
1898
Tejun Heoc9d76be2013-02-27 17:04:26 -08001899 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001900 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Tejun Heoc9d76be2013-02-27 17:04:26 -08001902 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001904 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001905 idr_preload_end();
1906 if (r < 0)
1907 return r == -ENOSPC ? -EBUSY : r;
1908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001911static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001913 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Tejun Heoc9d76be2013-02-27 17:04:26 -08001915 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001916 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Tejun Heoc9d76be2013-02-27 17:04:26 -08001918 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001920 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001921 idr_preload_end();
1922 if (r < 0)
1923 return r;
1924 *minor = r;
1925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001928static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Mikulas Patocka53d59142009-04-02 19:55:37 +01001930static void dm_wq_work(struct work_struct *work);
1931
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001932static void dm_init_md_queue(struct mapped_device *md)
1933{
1934 /*
1935 * Request-based dm devices cannot be stacked on top of bio-based dm
1936 * devices. The type of this dm device has not been decided yet.
1937 * The type is decided at the first table loading time.
1938 * To prevent problematic device stacking, clear the queue flag
1939 * for request stacking support until then.
1940 *
1941 * This queue is new, so no concurrency on the queue_flags.
1942 */
1943 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1944
1945 md->queue->queuedata = md;
1946 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1947 md->queue->backing_dev_info.congested_data = md;
1948 blk_queue_make_request(md->queue, dm_request);
1949 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001950 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1951}
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953/*
1954 * Allocate and initialise a blank device with a given minor.
1955 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001956static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001959 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001960 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 if (!md) {
1963 DMWARN("unable to allocate device, out of memory.");
1964 return NULL;
1965 }
1966
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001967 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001968 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001971 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001972 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001973 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001974 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001976 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001978 r = init_srcu_struct(&md->io_barrier);
1979 if (r < 0)
1980 goto bad_io_barrier;
1981
Mike Snitzera5664da2010-08-12 04:14:01 +01001982 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001983 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001984 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001985 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001987 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001989 atomic_set(&md->uevent_seq, 0);
1990 INIT_LIST_HEAD(&md->uevent_list);
1991 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001993 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001995 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001997 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 md->disk = alloc_disk(1);
2000 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00002001 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002003 atomic_set(&md->pending[0], 0);
2004 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002005 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01002006 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002007 init_waitqueue_head(&md->eventq);
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 md->disk->major = _major;
2010 md->disk->first_minor = minor;
2011 md->disk->fops = &dm_blk_dops;
2012 md->disk->queue = md->queue;
2013 md->disk->private_data = md;
2014 sprintf(md->disk->disk_name, "dm-%d", minor);
2015 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002016 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Tejun Heo670368a2013-07-30 08:40:21 -04002018 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00002019 if (!md->wq)
2020 goto bad_thread;
2021
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002022 md->bdev = bdget_disk(md->disk, 0);
2023 if (!md->bdev)
2024 goto bad_bdev;
2025
Tejun Heo6a8736d2010-09-08 18:07:00 +02002026 bio_init(&md->flush_bio);
2027 md->flush_bio.bi_bdev = md->bdev;
2028 md->flush_bio.bi_rw = WRITE_FLUSH;
2029
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002030 dm_stats_init(&md->stats);
2031
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002032 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002033 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002034 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002035 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002036
2037 BUG_ON(old_md != MINOR_ALLOCED);
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return md;
2040
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002041bad_bdev:
2042 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00002043bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01002044 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00002045 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002046bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05002047 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002048bad_queue:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002049 cleanup_srcu_struct(&md->io_barrier);
2050bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002052bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002053 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002054bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 kfree(md);
2056 return NULL;
2057}
2058
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002059static void unlock_fs(struct mapped_device *md);
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061static void free_dev(struct mapped_device *md)
2062{
Tejun Heof331c022008-09-03 09:01:48 +02002063 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002064
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002065 unlock_fs(md);
2066 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00002067 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002068 if (md->io_pool)
2069 mempool_destroy(md->io_pool);
2070 if (md->bs)
2071 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01002072 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 del_gendisk(md->disk);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002074 cleanup_srcu_struct(&md->io_barrier);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002075 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002076
2077 spin_lock(&_minor_lock);
2078 md->disk->private_data = NULL;
2079 spin_unlock(&_minor_lock);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05002082 blk_cleanup_queue(md->queue);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002083 dm_stats_cleanup(&md->stats);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002084 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 kfree(md);
2086}
2087
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002088static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2089{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002090 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002091
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002092 if (md->io_pool && md->bs) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002093 /* The md already has necessary mempools. */
2094 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2095 /*
2096 * Reload bioset because front_pad may have changed
2097 * because a different table was loaded.
2098 */
2099 bioset_free(md->bs);
2100 md->bs = p->bs;
2101 p->bs = NULL;
2102 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002103 /*
2104 * There's no need to reload with request-based dm
2105 * because the size of front_pad doesn't change.
2106 * Note for future: If you are to reload bioset,
2107 * prep-ed requests in the queue may refer
2108 * to bio from the old bioset, so you must walk
2109 * through the queue to unprep.
2110 */
2111 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002112 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002113 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002114
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002115 BUG_ON(!p || md->io_pool || md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002116
2117 md->io_pool = p->io_pool;
2118 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002119 md->bs = p->bs;
2120 p->bs = NULL;
2121
2122out:
2123 /* mempool bind completed, now no need any mempools in the table */
2124 dm_table_free_md_mempools(t);
2125}
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/*
2128 * Bind a table to the device.
2129 */
2130static void event_callback(void *context)
2131{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002132 unsigned long flags;
2133 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 struct mapped_device *md = (struct mapped_device *) context;
2135
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002136 spin_lock_irqsave(&md->uevent_lock, flags);
2137 list_splice_init(&md->uevent_list, &uevents);
2138 spin_unlock_irqrestore(&md->uevent_lock, flags);
2139
Tejun Heoed9e1982008-08-25 19:56:05 +09002140 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 atomic_inc(&md->event_nr);
2143 wake_up(&md->eventq);
2144}
2145
Mike Snitzerc2176492011-01-13 19:53:46 +00002146/*
2147 * Protected by md->suspend_lock obtained by dm_swap_table().
2148 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002149static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002151 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002153 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002156/*
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002157 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2158 *
2159 * If this function returns 0, then the device is either a non-dm
2160 * device without a merge_bvec_fn, or it is a dm device that is
2161 * able to split any bios it receives that are too big.
2162 */
2163int dm_queue_merge_is_compulsory(struct request_queue *q)
2164{
2165 struct mapped_device *dev_md;
2166
2167 if (!q->merge_bvec_fn)
2168 return 0;
2169
2170 if (q->make_request_fn == dm_request) {
2171 dev_md = q->queuedata;
2172 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2173 return 0;
2174 }
2175
2176 return 1;
2177}
2178
2179static int dm_device_merge_is_compulsory(struct dm_target *ti,
2180 struct dm_dev *dev, sector_t start,
2181 sector_t len, void *data)
2182{
2183 struct block_device *bdev = dev->bdev;
2184 struct request_queue *q = bdev_get_queue(bdev);
2185
2186 return dm_queue_merge_is_compulsory(q);
2187}
2188
2189/*
2190 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2191 * on the properties of the underlying devices.
2192 */
2193static int dm_table_merge_is_optional(struct dm_table *table)
2194{
2195 unsigned i = 0;
2196 struct dm_target *ti;
2197
2198 while (i < dm_table_get_num_targets(table)) {
2199 ti = dm_table_get_target(table, i++);
2200
2201 if (ti->type->iterate_devices &&
2202 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2203 return 0;
2204 }
2205
2206 return 1;
2207}
2208
2209/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002210 * Returns old map, which caller must destroy.
2211 */
2212static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2213 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002215 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002216 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 sector_t size;
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002218 int merge_is_optional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002221
2222 /*
2223 * Wipe any geometry if the size of the table changed.
2224 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002225 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002226 memset(&md->geometry, 0, sizeof(md->geometry));
2227
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002228 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002230 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002231
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002232 /*
2233 * The queue hasn't been stopped yet, if the old table type wasn't
2234 * for request-based during suspension. So stop it to prevent
2235 * I/O mapping before resume.
2236 * This must be done before setting the queue restrictions,
2237 * because request-based dm may be run just after the setting.
2238 */
2239 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2240 stop_queue(q);
2241
2242 __bind_mempools(md, t);
2243
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002244 merge_is_optional = dm_table_merge_is_optional(t);
2245
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002246 old_map = md->map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002247 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002248 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2249
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002250 dm_table_set_restrictions(t, q, limits);
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002251 if (merge_is_optional)
2252 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2253 else
2254 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002255 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002256
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002257 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Alasdair G Kergona7940152009-12-10 23:52:23 +00002260/*
2261 * Returns unbound table for the caller to free.
2262 */
2263static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 struct dm_table *map = md->map;
2266
2267 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002268 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 dm_table_event_callback(map, NULL, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002271 rcu_assign_pointer(md->map, NULL);
2272 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002273
2274 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
2277/*
2278 * Constructor for a new device.
2279 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002280int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 struct mapped_device *md;
2283
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002284 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (!md)
2286 return -ENXIO;
2287
Milan Broz784aae72009-01-06 03:05:12 +00002288 dm_sysfs_init(md);
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 *result = md;
2291 return 0;
2292}
2293
Mike Snitzera5664da2010-08-12 04:14:01 +01002294/*
2295 * Functions to manage md->type.
2296 * All are required to hold md->type_lock.
2297 */
2298void dm_lock_md_type(struct mapped_device *md)
2299{
2300 mutex_lock(&md->type_lock);
2301}
2302
2303void dm_unlock_md_type(struct mapped_device *md)
2304{
2305 mutex_unlock(&md->type_lock);
2306}
2307
2308void dm_set_md_type(struct mapped_device *md, unsigned type)
2309{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002310 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002311 md->type = type;
2312}
2313
2314unsigned dm_get_md_type(struct mapped_device *md)
2315{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002316 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002317 return md->type;
2318}
2319
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002320struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2321{
2322 return md->immutable_target_type;
2323}
2324
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002325/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002326 * The queue_limits are only valid as long as you have a reference
2327 * count on 'md'.
2328 */
2329struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2330{
2331 BUG_ON(!atomic_read(&md->holders));
2332 return &md->queue->limits;
2333}
2334EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2335
2336/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002337 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2338 */
2339static int dm_init_request_based_queue(struct mapped_device *md)
2340{
2341 struct request_queue *q = NULL;
2342
2343 if (md->queue->elevator)
2344 return 1;
2345
2346 /* Fully initialize the queue */
2347 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2348 if (!q)
2349 return 0;
2350
2351 md->queue = q;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002352 dm_init_md_queue(md);
2353 blk_queue_softirq_done(md->queue, dm_softirq_done);
2354 blk_queue_prep_rq(md->queue, dm_prep_fn);
2355 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002356
2357 elv_register_queue(md->queue);
2358
2359 return 1;
2360}
2361
2362/*
2363 * Setup the DM device's queue based on md's type
2364 */
2365int dm_setup_md_queue(struct mapped_device *md)
2366{
2367 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2368 !dm_init_request_based_queue(md)) {
2369 DMWARN("Cannot initialize queue for request-based mapped device");
2370 return -EINVAL;
2371 }
2372
2373 return 0;
2374}
2375
David Teigland637842c2006-01-06 00:20:00 -08002376static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
2378 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 unsigned minor = MINOR(dev);
2380
2381 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2382 return NULL;
2383
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002384 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002387 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002388 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002389 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002390 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002391 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002392 goto out;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002395out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002396 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
David Teigland637842c2006-01-06 00:20:00 -08002398 return md;
2399}
2400
David Teiglandd229a952006-01-06 00:20:01 -08002401struct mapped_device *dm_get_md(dev_t dev)
2402{
2403 struct mapped_device *md = dm_find_md(dev);
2404
2405 if (md)
2406 dm_get(md);
2407
2408 return md;
2409}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002410EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002411
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002412void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002413{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002414 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
2417void dm_set_mdptr(struct mapped_device *md, void *ptr)
2418{
2419 md->interface_ptr = ptr;
2420}
2421
2422void dm_get(struct mapped_device *md)
2423{
2424 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002425 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002428const char *dm_device_name(struct mapped_device *md)
2429{
2430 return md->name;
2431}
2432EXPORT_SYMBOL_GPL(dm_device_name);
2433
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002434static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002436 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002437 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002439 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002440
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002441 spin_lock(&_minor_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002442 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002443 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2444 set_bit(DMF_FREEING, &md->flags);
2445 spin_unlock(&_minor_lock);
2446
2447 if (!dm_suspended_md(md)) {
2448 dm_table_presuspend_targets(map);
2449 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002451
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002452 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2453 dm_put_live_table(md, srcu_idx);
2454
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002455 /*
2456 * Rare, but there may be I/O requests still going to complete,
2457 * for example. Wait for all references to disappear.
2458 * No one should increment the reference count of the mapped_device,
2459 * after the mapped_device state becomes DMF_FREEING.
2460 */
2461 if (wait)
2462 while (atomic_read(&md->holders))
2463 msleep(1);
2464 else if (atomic_read(&md->holders))
2465 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2466 dm_device_name(md), atomic_read(&md->holders));
2467
2468 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002469 dm_table_destroy(__unbind(md));
2470 free_dev(md);
2471}
2472
2473void dm_destroy(struct mapped_device *md)
2474{
2475 __dm_destroy(md, true);
2476}
2477
2478void dm_destroy_immediate(struct mapped_device *md)
2479{
2480 __dm_destroy(md, false);
2481}
2482
2483void dm_put(struct mapped_device *md)
2484{
2485 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
Edward Goggin79eb8852007-05-09 02:32:56 -07002487EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Mikulas Patocka401600d2009-04-02 19:55:38 +01002489static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002490{
2491 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002492 DECLARE_WAITQUEUE(wait, current);
2493
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002494 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002495
2496 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002497 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002498
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002499 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002500 break;
2501
Mikulas Patocka401600d2009-04-02 19:55:38 +01002502 if (interruptible == TASK_INTERRUPTIBLE &&
2503 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002504 r = -EINTR;
2505 break;
2506 }
2507
2508 io_schedule();
2509 }
2510 set_current_state(TASK_RUNNING);
2511
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002512 remove_wait_queue(&md->wait, &wait);
2513
Milan Broz46125c12008-02-08 02:10:30 +00002514 return r;
2515}
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517/*
2518 * Process the deferred bios
2519 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002520static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Mikulas Patockaef208582009-04-02 19:55:38 +01002522 struct mapped_device *md = container_of(work, struct mapped_device,
2523 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002524 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002525 int srcu_idx;
2526 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002528 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002529
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002530 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002531 spin_lock_irq(&md->deferred_lock);
2532 c = bio_list_pop(&md->deferred);
2533 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002534
Tejun Heo6a8736d2010-09-08 18:07:00 +02002535 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002536 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002537
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002538 if (dm_request_based(md))
2539 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002540 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002541 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002542 }
Milan Broz73d410c2008-02-08 02:10:25 +00002543
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002544 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002547static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002548{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002549 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2550 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002551 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002552}
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002555 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002557struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Mike Christie87eb5b22013-03-01 22:45:48 +00002559 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002560 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002561 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Daniel Walkere61290a2008-02-08 02:10:08 +00002563 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002566 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002567 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Mike Snitzer3ae70652012-09-26 23:45:45 +01002569 /*
2570 * If the new table has no data devices, retain the existing limits.
2571 * This helps multipath with queue_if_no_path if all paths disappear,
2572 * then new I/O is queued based on these limits, and then some paths
2573 * reappear.
2574 */
2575 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002576 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002577 if (live_map)
2578 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002579 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002580 }
2581
Mike Christie87eb5b22013-03-01 22:45:48 +00002582 if (!live_map) {
2583 r = dm_calculate_queue_limits(table, &limits);
2584 if (r) {
2585 map = ERR_PTR(r);
2586 goto out;
2587 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002588 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002589
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002590 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002592out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002593 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002594 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597/*
2598 * Functions to lock and unlock any filesystem running on the
2599 * device.
2600 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002601static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002603 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002606
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002607 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002608 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002609 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002610 md->frozen_sb = NULL;
2611 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002612 }
2613
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002614 set_bit(DMF_FROZEN, &md->flags);
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return 0;
2617}
2618
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002619static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002621 if (!test_bit(DMF_FROZEN, &md->flags))
2622 return;
2623
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002624 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002626 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
2629/*
2630 * We need to be able to change a mapping table under a mounted
2631 * filesystem. For example we might want to move some data in
2632 * the background. Before the table can be swapped with
2633 * dm_bind_table, dm_suspend must be called to flush any in
2634 * flight bios and ensure that any further io gets deferred.
2635 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002636/*
2637 * Suspend mechanism in request-based dm.
2638 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002639 * 1. Flush all I/Os by lock_fs() if needed.
2640 * 2. Stop dispatching any I/O by stopping the request_queue.
2641 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002642 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002643 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002644 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002645int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002647 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002648 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002649 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002650 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Daniel Walkere61290a2008-02-08 02:10:08 +00002652 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002653
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002654 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002655 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002656 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002659 map = md->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002661 /*
2662 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2663 * This flag is cleared before dm_suspend returns.
2664 */
2665 if (noflush)
2666 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2667
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002668 /* This does not get reverted if there's an error later. */
2669 dm_table_presuspend_targets(map);
2670
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002671 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002672 * Flush I/O to the device.
2673 * Any I/O submitted after lock_fs() may not be flushed.
2674 * noflush takes precedence over do_lockfs.
2675 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002676 */
2677 if (!noflush && do_lockfs) {
2678 r = lock_fs(md);
2679 if (r)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002680 goto out_unlock;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
2683 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002684 * Here we must make sure that no processes are submitting requests
2685 * to target drivers i.e. no one may be executing
2686 * __split_and_process_bio. This is called from dm_request and
2687 * dm_wq_work.
2688 *
2689 * To get all processes out of __split_and_process_bio in dm_request,
2690 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002691 * __split_and_process_bio from dm_request and quiesce the thread
2692 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2693 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002695 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002696 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002698 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002699 * Stop md->queue before flushing md->wq in case request-based
2700 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002701 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002702 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002703 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002704
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002705 flush_workqueue(md->wq);
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002708 * At this point no more requests are entering target request routines.
2709 * We call dm_wait_for_completion to wait for all existing requests
2710 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002712 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Milan Broz6d6f10d2008-02-08 02:10:22 +00002714 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002715 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002716 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002719 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002720 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002721
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002722 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002723 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002724
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002725 unlock_fs(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002726 goto out_unlock; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002727 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002728
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002729 /*
2730 * If dm_wait_for_completion returned 0, the device is completely
2731 * quiescent now. There is no request-processing activity. All new
2732 * requests are being added to md->deferred list.
2733 */
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 set_bit(DMF_SUSPENDED, &md->flags);
2736
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002737 dm_table_postsuspend_targets(map);
2738
Alasdair G Kergond2874832006-11-08 17:44:43 -08002739out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002740 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002741 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
2744int dm_resume(struct mapped_device *md)
2745{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002746 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002747 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Daniel Walkere61290a2008-02-08 02:10:08 +00002749 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002750 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002751 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002752
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002753 map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002754 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002755 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Milan Broz8757b772006-10-03 01:15:36 -07002757 r = dm_table_resume_targets(map);
2758 if (r)
2759 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002760
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002761 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002762
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002763 /*
2764 * Flushing deferred I/Os must be done after targets are resumed
2765 * so that mapping of targets can work correctly.
2766 * Request-based dm is queueing the deferred I/Os in its request_queue.
2767 */
2768 if (dm_request_based(md))
2769 start_queue(md->queue);
2770
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002771 unlock_fs(md);
2772
2773 clear_bit(DMF_SUSPENDED, &md->flags);
2774
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002775 r = 0;
2776out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002777 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002778
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002779 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002782/*
2783 * Internal suspend/resume works like userspace-driven suspend. It waits
2784 * until all bios finish and prevents issuing new bios to the target drivers.
2785 * It may be used only from the kernel.
2786 *
2787 * Internal suspend holds md->suspend_lock, which prevents interaction with
2788 * userspace-driven suspend.
2789 */
2790
2791void dm_internal_suspend(struct mapped_device *md)
2792{
2793 mutex_lock(&md->suspend_lock);
2794 if (dm_suspended_md(md))
2795 return;
2796
2797 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2798 synchronize_srcu(&md->io_barrier);
2799 flush_workqueue(md->wq);
2800 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2801}
2802
2803void dm_internal_resume(struct mapped_device *md)
2804{
2805 if (dm_suspended_md(md))
2806 goto done;
2807
2808 dm_queue_flush(md);
2809
2810done:
2811 mutex_unlock(&md->suspend_lock);
2812}
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814/*-----------------------------------------------------------------
2815 * Event notification.
2816 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002817int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002818 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002819{
Milan Broz60935eb2009-06-22 10:12:30 +01002820 char udev_cookie[DM_COOKIE_LENGTH];
2821 char *envp[] = { udev_cookie, NULL };
2822
2823 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002824 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002825 else {
2826 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2827 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002828 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2829 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002830 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002831}
2832
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002833uint32_t dm_next_uevent_seq(struct mapped_device *md)
2834{
2835 return atomic_add_return(1, &md->uevent_seq);
2836}
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838uint32_t dm_get_event_nr(struct mapped_device *md)
2839{
2840 return atomic_read(&md->event_nr);
2841}
2842
2843int dm_wait_event(struct mapped_device *md, int event_nr)
2844{
2845 return wait_event_interruptible(md->eventq,
2846 (event_nr != atomic_read(&md->event_nr)));
2847}
2848
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002849void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2850{
2851 unsigned long flags;
2852
2853 spin_lock_irqsave(&md->uevent_lock, flags);
2854 list_add(elist, &md->uevent_list);
2855 spin_unlock_irqrestore(&md->uevent_lock, flags);
2856}
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858/*
2859 * The gendisk is only valid as long as you have a reference
2860 * count on 'md'.
2861 */
2862struct gendisk *dm_disk(struct mapped_device *md)
2863{
2864 return md->disk;
2865}
2866
Milan Broz784aae72009-01-06 03:05:12 +00002867struct kobject *dm_kobject(struct mapped_device *md)
2868{
2869 return &md->kobj;
2870}
2871
2872/*
2873 * struct mapped_device should not be exported outside of dm.c
2874 * so use this check to verify that kobj is part of md structure
2875 */
2876struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2877{
2878 struct mapped_device *md;
2879
2880 md = container_of(kobj, struct mapped_device, kobj);
2881 if (&md->kobj != kobj)
2882 return NULL;
2883
Milan Broz4d89b7b2009-06-22 10:12:11 +01002884 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002885 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002886 return NULL;
2887
Milan Broz784aae72009-01-06 03:05:12 +00002888 dm_get(md);
2889 return md;
2890}
2891
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002892int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
2894 return test_bit(DMF_SUSPENDED, &md->flags);
2895}
2896
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002897int dm_suspended(struct dm_target *ti)
2898{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002899 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002900}
2901EXPORT_SYMBOL_GPL(dm_suspended);
2902
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002903int dm_noflush_suspending(struct dm_target *ti)
2904{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002905 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002906}
2907EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2908
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002909struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002910{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002911 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
2912 struct kmem_cache *cachep;
2913 unsigned int pool_size;
2914 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002915
2916 if (!pools)
2917 return NULL;
2918
Jun'ichi Nomura23e50832013-03-01 22:45:48 +00002919 if (type == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002920 cachep = _io_cache;
Mike Snitzere8603132013-09-12 18:06:12 -04002921 pool_size = dm_get_reserved_bio_based_ios();
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002922 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2923 } else if (type == DM_TYPE_REQUEST_BASED) {
2924 cachep = _rq_tio_cache;
Mike Snitzerf4790822013-09-12 18:06:12 -04002925 pool_size = dm_get_reserved_rq_based_ios();
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002926 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2927 /* per_bio_data_size is not used. See __bind_mempools(). */
2928 WARN_ON(per_bio_data_size != 0);
2929 } else
2930 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002931
Mike Snitzer6cfa5852013-09-12 18:06:11 -04002932 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002933 if (!pools->io_pool)
2934 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002935
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002936 pools->bs = bioset_create(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002937 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002938 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002939
Martin K. Petersena91a2782011-03-17 11:11:05 +01002940 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002941 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002942
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002943 return pools;
2944
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002945out:
2946 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002947
2948 return NULL;
2949}
2950
2951void dm_free_md_mempools(struct dm_md_mempools *pools)
2952{
2953 if (!pools)
2954 return;
2955
2956 if (pools->io_pool)
2957 mempool_destroy(pools->io_pool);
2958
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002959 if (pools->bs)
2960 bioset_free(pools->bs);
2961
2962 kfree(pools);
2963}
2964
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002965static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 .open = dm_blk_open,
2967 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002968 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002969 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 .owner = THIS_MODULE
2971};
2972
2973EXPORT_SYMBOL(dm_get_mapinfo);
2974
2975/*
2976 * module hooks
2977 */
2978module_init(dm_init);
2979module_exit(dm_exit);
2980
2981module_param(major, uint, 0);
2982MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04002983
Mike Snitzere8603132013-09-12 18:06:12 -04002984module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
2985MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
2986
Mike Snitzerf4790822013-09-12 18:06:12 -04002987module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
2988MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990MODULE_DESCRIPTION(DM_NAME " driver");
2991MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2992MODULE_LICENSE("GPL");