blob: 7b0ccdc5d65c4c34ef9a32888582db75c65da3e0 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800215static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000216static struct kmem_cache *_rq_tio_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static int __init local_init(void)
219{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100220 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100223 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100225 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000227 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
228 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100229 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000230
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100231 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100232 if (r)
Jun'ichi Nomura23e50832013-03-01 22:45:48 +0000233 goto out_free_rq_tio_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 _major = major;
236 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100237 if (r < 0)
238 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (!_major)
241 _major = r;
242
243 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100244
245out_uevent_exit:
246 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000247out_free_rq_tio_cache:
248 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100249out_free_io_cache:
250 kmem_cache_destroy(_io_cache);
251
252 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255static void local_exit(void)
256{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000257 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700259 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100260 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 _major = 0;
263
264 DMINFO("cleaned up");
265}
266
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000267static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 local_init,
269 dm_target_init,
270 dm_linear_init,
271 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000272 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100273 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400275 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000278static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 local_exit,
280 dm_target_exit,
281 dm_linear_exit,
282 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000283 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100284 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400286 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
289static int __init dm_init(void)
290{
291 const int count = ARRAY_SIZE(_inits);
292
293 int r, i;
294
295 for (i = 0; i < count; i++) {
296 r = _inits[i]();
297 if (r)
298 goto bad;
299 }
300
301 return 0;
302
303 bad:
304 while (i--)
305 _exits[i]();
306
307 return r;
308}
309
310static void __exit dm_exit(void)
311{
312 int i = ARRAY_SIZE(_exits);
313
314 while (i--)
315 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100316
317 /*
318 * Should be empty by this point.
319 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100320 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/*
324 * Block device functions
325 */
Mike Anderson432a2122009-12-10 23:52:20 +0000326int dm_deleting_md(struct mapped_device *md)
327{
328 return test_bit(DMF_DELETING, &md->flags);
329}
330
Al Virofe5f9f22008-03-02 10:29:31 -0500331static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct mapped_device *md;
334
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700335 spin_lock(&_minor_lock);
336
Al Virofe5f9f22008-03-02 10:29:31 -0500337 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700338 if (!md)
339 goto out;
340
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700341 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000342 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700343 md = NULL;
344 goto out;
345 }
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700348 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700349
350out:
351 spin_unlock(&_minor_lock);
352
353 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Al Virodb2a1442013-05-05 21:52:57 -0400356static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Al Virofe5f9f22008-03-02 10:29:31 -0500358 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200359
Milan Broz4a1aeb92011-01-13 19:59:48 +0000360 spin_lock(&_minor_lock);
361
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700362 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000364
365 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700368int dm_open_count(struct mapped_device *md)
369{
370 return atomic_read(&md->open_count);
371}
372
373/*
374 * Guarantees nothing is using the device before it's deleted.
375 */
376int dm_lock_for_deletion(struct mapped_device *md)
377{
378 int r = 0;
379
380 spin_lock(&_minor_lock);
381
382 if (dm_open_count(md))
383 r = -EBUSY;
384 else
385 set_bit(DMF_DELETING, &md->flags);
386
387 spin_unlock(&_minor_lock);
388
389 return r;
390}
391
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400392sector_t dm_get_size(struct mapped_device *md)
393{
394 return get_capacity(md->disk);
395}
396
397struct dm_stats *dm_get_stats(struct mapped_device *md)
398{
399 return &md->stats;
400}
401
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800402static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
403{
404 struct mapped_device *md = bdev->bd_disk->private_data;
405
406 return dm_get_geometry(md, geo);
407}
408
Al Virofe5f9f22008-03-02 10:29:31 -0500409static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700410 unsigned int cmd, unsigned long arg)
411{
Al Virofe5f9f22008-03-02 10:29:31 -0500412 struct mapped_device *md = bdev->bd_disk->private_data;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100413 int srcu_idx;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100414 struct dm_table *map;
Milan Brozaa129a22006-10-03 01:15:15 -0700415 struct dm_target *tgt;
416 int r = -ENOTTY;
417
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100418retry:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100419 map = dm_get_live_table(md, &srcu_idx);
420
Milan Brozaa129a22006-10-03 01:15:15 -0700421 if (!map || !dm_table_get_size(map))
422 goto out;
423
424 /* We only support devices that have a single target */
425 if (dm_table_get_num_targets(map) != 1)
426 goto out;
427
428 tgt = dm_table_get_target(map, 0);
429
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000430 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700431 r = -EAGAIN;
432 goto out;
433 }
434
435 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400436 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700437
438out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100439 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700440
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100441 if (r == -ENOTCONN) {
442 msleep(10);
443 goto retry;
444 }
445
Milan Brozaa129a22006-10-03 01:15:15 -0700446 return r;
447}
448
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100449static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 return mempool_alloc(md->io_pool, GFP_NOIO);
452}
453
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100454static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 mempool_free(io, md->io_pool);
457}
458
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100459static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Mikulas Patockadba14162012-10-12 21:02:15 +0100461 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000464static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
465 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100466{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000467 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100468}
469
470static void free_rq_tio(struct dm_rq_target_io *tio)
471{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000472 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100473}
474
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000475static int md_in_flight(struct mapped_device *md)
476{
477 return atomic_read(&md->pending[READ]) +
478 atomic_read(&md->pending[WRITE]);
479}
480
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800481static void start_io_acct(struct dm_io *io)
482{
483 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400484 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900485 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400486 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800487
488 io->start_time = jiffies;
489
Tejun Heo074a7ac2008-08-25 19:56:14 +0900490 cpu = part_stat_lock();
491 part_round_stats(cpu, &dm_disk(md)->part0);
492 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100493 atomic_set(&dm_disk(md)->part0.in_flight[rw],
494 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400495
496 if (unlikely(dm_stats_used(&md->stats)))
497 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
498 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800499}
500
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000501static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800502{
503 struct mapped_device *md = io->md;
504 struct bio *bio = io->bio;
505 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900506 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800507 int rw = bio_data_dir(bio);
508
Tejun Heo074a7ac2008-08-25 19:56:14 +0900509 cpu = part_stat_lock();
510 part_round_stats(cpu, &dm_disk(md)->part0);
511 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
512 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800513
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400514 if (unlikely(dm_stats_used(&md->stats)))
515 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
516 bio_sectors(bio), true, duration, &io->stats_aux);
517
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100518 /*
519 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200520 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100521 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100522 pending = atomic_dec_return(&md->pending[rw]);
523 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200524 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800525
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000526 /* nudge anyone waiting on suspend queue */
527 if (!pending)
528 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/*
532 * Add the bio to the list of deferred io.
533 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100534static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200536 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200538 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200540 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200541 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/*
545 * Everyone (including functions in this file), should use this
546 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100547 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100549struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100551 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100553 return srcu_dereference(md->map, &md->io_barrier);
554}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100556void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
557{
558 srcu_read_unlock(&md->io_barrier, srcu_idx);
559}
560
561void dm_sync_table(struct mapped_device *md)
562{
563 synchronize_srcu(&md->io_barrier);
564 synchronize_rcu_expedited();
565}
566
567/*
568 * A fast alternative to dm_get_live_table/dm_put_live_table.
569 * The caller must not block between these two functions.
570 */
571static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
572{
573 rcu_read_lock();
574 return rcu_dereference(md->map);
575}
576
577static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
578{
579 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800582/*
583 * Get the geometry associated with a dm device
584 */
585int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
586{
587 *geo = md->geometry;
588
589 return 0;
590}
591
592/*
593 * Set the geometry of a device.
594 */
595int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
596{
597 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
598
599 if (geo->start > sz) {
600 DMWARN("Start sector is beyond the geometry limits.");
601 return -EINVAL;
602 }
603
604 md->geometry = *geo;
605
606 return 0;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*-----------------------------------------------------------------
610 * CRUD START:
611 * A more elegant soln is in the works that uses the queue
612 * merge fn, unfortunately there are a couple of changes to
613 * the block layer that I want to make for this. So in the
614 * interests of getting something for people to use I give
615 * you this clearly demarcated crap.
616 *---------------------------------------------------------------*/
617
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800618static int __noflush_suspending(struct mapped_device *md)
619{
620 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
624 * Decrements the number of outstanding ios that a bio has been
625 * cloned into, completing the original io if necc.
626 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800627static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800629 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000630 int io_error;
631 struct bio *bio;
632 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800633
634 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100635 if (unlikely(error)) {
636 spin_lock_irqsave(&io->endio_lock, flags);
637 if (!(io->error > 0 && __noflush_suspending(md)))
638 io->error = error;
639 spin_unlock_irqrestore(&io->endio_lock, flags);
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800643 if (io->error == DM_ENDIO_REQUEUE) {
644 /*
645 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800646 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100647 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200648 if (__noflush_suspending(md))
649 bio_list_add_head(&md->deferred, io->bio);
650 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800651 /* noflush suspend was interrupted. */
652 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100653 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800654 }
655
Milan Brozb35f8ca2009-03-16 17:44:36 +0000656 io_error = io->error;
657 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200658 end_io_acct(io);
659 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100660
Tejun Heo6a8736d2010-09-08 18:07:00 +0200661 if (io_error == DM_ENDIO_REQUEUE)
662 return;
663
Mike Snitzerb372d362010-09-08 18:07:01 +0200664 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100665 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200666 * Preflush done for flush with data, reissue
667 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100668 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200669 bio->bi_rw &= ~REQ_FLUSH;
670 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100671 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200672 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700673 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200674 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677}
678
NeilBrown6712ecf2007-09-27 12:47:43 +0200679static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100682 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000683 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700684 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dm_endio_fn endio = tio->ti->type->end_io;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
688 error = -EIO;
689
690 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000691 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800692 if (r < 0 || r == DM_ENDIO_REQUEUE)
693 /*
694 * error and requeue request are handled
695 * in dec_pending().
696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800698 else if (r == DM_ENDIO_INCOMPLETE)
699 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200700 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800701 else if (r) {
702 DMWARN("unimplemented target endio return value: %d", r);
703 BUG();
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Stefan Bader9faf4002006-10-03 01:15:41 -0700707 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000708 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100711/*
712 * Partial completion handling for request-based dm
713 */
714static void end_clone_bio(struct bio *clone, int error)
715{
716 struct dm_rq_clone_bio_info *info = clone->bi_private;
717 struct dm_rq_target_io *tio = info->tio;
718 struct bio *bio = info->orig;
719 unsigned int nr_bytes = info->orig->bi_size;
720
721 bio_put(clone);
722
723 if (tio->error)
724 /*
725 * An error has already been detected on the request.
726 * Once error occurred, just let clone->end_io() handle
727 * the remainder.
728 */
729 return;
730 else if (error) {
731 /*
732 * Don't notice the error to the upper layer yet.
733 * The error handling decision is made by the target driver,
734 * when the request is completed.
735 */
736 tio->error = error;
737 return;
738 }
739
740 /*
741 * I/O for the bio successfully completed.
742 * Notice the data completion to the upper layer.
743 */
744
745 /*
746 * bios are processed from the head of the list.
747 * So the completing bio should always be rq->bio.
748 * If it's not, something wrong is happening.
749 */
750 if (tio->orig->bio != bio)
751 DMERR("bio completion is going in the middle of the request");
752
753 /*
754 * Update the original request.
755 * Do not use blk_end_request() here, because it may complete
756 * the original request before the clone, and break the ordering.
757 */
758 blk_update_request(tio->orig, 0, nr_bytes);
759}
760
761/*
762 * Don't touch any member of the md after calling this function because
763 * the md may be freed in dm_put() at the end of this function.
764 * Or do dm_get() before calling this function and dm_put() later.
765 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000766static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100767{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000768 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100769
770 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000771 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100772 wake_up(&md->wait);
773
Jens Axboea8c32a52012-11-06 12:24:26 +0100774 /*
775 * Run this off this callpath, as drivers could invoke end_io while
776 * inside their request_fn (and holding the queue lock). Calling
777 * back into ->request_fn() could deadlock attempting to grab the
778 * queue lock again.
779 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100780 if (run_queue)
Jens Axboea8c32a52012-11-06 12:24:26 +0100781 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100782
783 /*
784 * dm_put() must be at the end of this function. See the comment above
785 */
786 dm_put(md);
787}
788
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100789static void free_rq_clone(struct request *clone)
790{
791 struct dm_rq_target_io *tio = clone->end_io_data;
792
793 blk_rq_unprep_clone(clone);
794 free_rq_tio(tio);
795}
796
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000797/*
798 * Complete the clone and the original request.
799 * Must be called without queue lock.
800 */
801static void dm_end_request(struct request *clone, int error)
802{
803 int rw = rq_data_dir(clone);
804 struct dm_rq_target_io *tio = clone->end_io_data;
805 struct mapped_device *md = tio->md;
806 struct request *rq = tio->orig;
807
Tejun Heo29e40132010-09-08 18:07:00 +0200808 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000809 rq->errors = clone->errors;
810 rq->resid_len = clone->resid_len;
811
812 if (rq->sense)
813 /*
814 * We are using the sense buffer of the original
815 * request.
816 * So setting the length of the sense data is enough.
817 */
818 rq->sense_len = clone->sense_len;
819 }
820
821 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200822 blk_end_request_all(rq, error);
823 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000824}
825
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100826static void dm_unprep_request(struct request *rq)
827{
828 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100829
830 rq->special = NULL;
831 rq->cmd_flags &= ~REQ_DONTPREP;
832
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100833 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100834}
835
836/*
837 * Requeue the original request of a clone.
838 */
839void dm_requeue_unmapped_request(struct request *clone)
840{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000841 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100842 struct dm_rq_target_io *tio = clone->end_io_data;
843 struct mapped_device *md = tio->md;
844 struct request *rq = tio->orig;
845 struct request_queue *q = rq->q;
846 unsigned long flags;
847
848 dm_unprep_request(rq);
849
850 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100851 blk_requeue_request(q, rq);
852 spin_unlock_irqrestore(q->queue_lock, flags);
853
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000854 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100855}
856EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
857
858static void __stop_queue(struct request_queue *q)
859{
860 blk_stop_queue(q);
861}
862
863static void stop_queue(struct request_queue *q)
864{
865 unsigned long flags;
866
867 spin_lock_irqsave(q->queue_lock, flags);
868 __stop_queue(q);
869 spin_unlock_irqrestore(q->queue_lock, flags);
870}
871
872static void __start_queue(struct request_queue *q)
873{
874 if (blk_queue_stopped(q))
875 blk_start_queue(q);
876}
877
878static void start_queue(struct request_queue *q)
879{
880 unsigned long flags;
881
882 spin_lock_irqsave(q->queue_lock, flags);
883 __start_queue(q);
884 spin_unlock_irqrestore(q->queue_lock, flags);
885}
886
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000887static void dm_done(struct request *clone, int error, bool mapped)
888{
889 int r = error;
890 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100891 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000892
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100893 if (tio->ti) {
894 rq_end_io = tio->ti->type->rq_end_io;
895
896 if (mapped && rq_end_io)
897 r = rq_end_io(tio->ti, clone, error, &tio->info);
898 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000899
900 if (r <= 0)
901 /* The target wants to complete the I/O */
902 dm_end_request(clone, r);
903 else if (r == DM_ENDIO_INCOMPLETE)
904 /* The target will handle the I/O */
905 return;
906 else if (r == DM_ENDIO_REQUEUE)
907 /* The target wants to requeue the I/O */
908 dm_requeue_unmapped_request(clone);
909 else {
910 DMWARN("unimplemented target endio return value: %d", r);
911 BUG();
912 }
913}
914
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100915/*
916 * Request completion handler for request-based dm
917 */
918static void dm_softirq_done(struct request *rq)
919{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000920 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100921 struct request *clone = rq->completion_data;
922 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100923
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000924 if (rq->cmd_flags & REQ_FAILED)
925 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100926
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000927 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100928}
929
930/*
931 * Complete the clone and the original request with the error status
932 * through softirq context.
933 */
934static void dm_complete_request(struct request *clone, int error)
935{
936 struct dm_rq_target_io *tio = clone->end_io_data;
937 struct request *rq = tio->orig;
938
939 tio->error = error;
940 rq->completion_data = clone;
941 blk_complete_request(rq);
942}
943
944/*
945 * Complete the not-mapped clone and the original request with the error status
946 * through softirq context.
947 * Target's rq_end_io() function isn't called.
948 * This may be used when the target's map_rq() function fails.
949 */
950void dm_kill_unmapped_request(struct request *clone, int error)
951{
952 struct dm_rq_target_io *tio = clone->end_io_data;
953 struct request *rq = tio->orig;
954
955 rq->cmd_flags |= REQ_FAILED;
956 dm_complete_request(clone, error);
957}
958EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
959
960/*
961 * Called with the queue lock held
962 */
963static void end_clone_request(struct request *clone, int error)
964{
965 /*
966 * For just cleaning up the information of the queue in which
967 * the clone was dispatched.
968 * The clone is *NOT* freed actually here because it is alloced from
969 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
970 */
971 __blk_put_request(clone->q, clone);
972
973 /*
974 * Actual request completion is done in a softirq context which doesn't
975 * hold the queue lock. Otherwise, deadlock could occur because:
976 * - another request may be submitted by the upper level driver
977 * of the stacking during the completion
978 * - the submission which requires queue lock may be done
979 * against this queue
980 */
981 dm_complete_request(clone, error);
982}
983
Mike Snitzer56a67df2010-08-12 04:14:10 +0100984/*
985 * Return maximum size of I/O possible at the supplied sector up to the current
986 * target boundary.
987 */
988static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100990 sector_t target_offset = dm_target_offset(ti, sector);
991
992 return ti->len - target_offset;
993}
994
995static sector_t max_io_len(sector_t sector, struct dm_target *ti)
996{
997 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +0100998 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001001 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001003 if (ti->max_io_len) {
1004 offset = dm_target_offset(ti, sector);
1005 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1006 max_len = sector_div(offset, ti->max_io_len);
1007 else
1008 max_len = offset & (ti->max_io_len - 1);
1009 max_len = ti->max_io_len - max_len;
1010
1011 if (len > max_len)
1012 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
1015 return len;
1016}
1017
Mike Snitzer542f9032012-07-27 15:08:00 +01001018int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1019{
1020 if (len > UINT_MAX) {
1021 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1022 (unsigned long long)len, UINT_MAX);
1023 ti->error = "Maximum size of target IO is too large";
1024 return -EINVAL;
1025 }
1026
1027 ti->max_io_len = (uint32_t) len;
1028
1029 return 0;
1030}
1031EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1032
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001033static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001036 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001037 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001038 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001039 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 clone->bi_end_io = clone_endio;
1042 clone->bi_private = tio;
1043
1044 /*
1045 * Map the clone. If r == 0 we don't need to do
1046 * anything, the target has assumed ownership of
1047 * this io.
1048 */
1049 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001050 sector = clone->bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001051 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001052 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001054
Mike Snitzerd07335e2010-11-16 12:52:38 +01001055 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1056 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001059 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1060 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001061 md = tio->io->md;
1062 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001063 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001064 } else if (r) {
1065 DMWARN("unimplemented target map return value: %d", r);
1066 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068}
1069
1070struct clone_info {
1071 struct mapped_device *md;
1072 struct dm_table *map;
1073 struct bio *bio;
1074 struct dm_io *io;
1075 sector_t sector;
1076 sector_t sector_count;
1077 unsigned short idx;
1078};
1079
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001080static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1081{
1082 bio->bi_sector = sector;
1083 bio->bi_size = to_bytes(len);
1084}
1085
1086static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1087{
1088 bio->bi_idx = idx;
1089 bio->bi_vcnt = idx + bv_count;
1090 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1091}
1092
1093static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1094 unsigned short idx, unsigned len, unsigned offset,
1095 unsigned trim)
1096{
1097 if (!bio_integrity(bio))
1098 return;
1099
1100 bio_integrity_clone(clone, bio, GFP_NOIO);
1101
1102 if (trim)
1103 bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001107 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001109static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
1110 sector_t sector, unsigned short idx,
1111 unsigned offset, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Mikulas Patockadba14162012-10-12 21:02:15 +01001113 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct bio_vec *bv = bio->bi_io_vec + idx;
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *clone->bi_io_vec = *bv;
1117
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001118 bio_setup_sector(clone, sector, len);
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001121 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 clone->bi_vcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 clone->bi_io_vec->bv_offset = offset;
1124 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001125 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001127 clone_bio_integrity(bio, clone, idx, len, offset, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/*
1131 * Creates a bio that consists of range of complete bvecs.
1132 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001133static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1134 sector_t sector, unsigned short idx,
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001135 unsigned short bv_count, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Mikulas Patockadba14162012-10-12 21:02:15 +01001137 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001138 unsigned trim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Stefan Bader9faf4002006-10-03 01:15:41 -07001140 __bio_clone(clone, bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001141 bio_setup_sector(clone, sector, len);
1142 bio_setup_bv(clone, idx, bv_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001144 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1145 trim = 1;
1146 clone_bio_integrity(bio, clone, idx, len, 0, trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001149static struct dm_target_io *alloc_tio(struct clone_info *ci,
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001150 struct dm_target *ti, int nr_iovecs,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001151 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001152{
Mikulas Patockadba14162012-10-12 21:02:15 +01001153 struct dm_target_io *tio;
1154 struct bio *clone;
1155
1156 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1157 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001158
1159 tio->io = ci->io;
1160 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001161 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001162 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001163
1164 return tio;
1165}
1166
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001167static void __clone_and_map_simple_bio(struct clone_info *ci,
1168 struct dm_target *ti,
1169 unsigned target_bio_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001170{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001171 struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001172 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001173
Mike Snitzer06a426c2010-08-12 04:14:09 +01001174 /*
1175 * Discard requests require the bio's inline iovecs be initialized.
1176 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1177 * and discard, so no need for concern about wasted bvec allocations.
1178 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001179 __bio_clone(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001180 if (len)
1181 bio_setup_sector(clone, ci->sector, len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001182
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001183 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001184}
1185
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001186static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1187 unsigned num_bios, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001188{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001189 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001190
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001191 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001192 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001193}
1194
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001195static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001196{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001197 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001198 struct dm_target *ti;
1199
Mike Snitzerb372d362010-09-08 18:07:01 +02001200 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001201 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001202 __send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001203
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001204 return 0;
1205}
1206
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001207static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1208 sector_t sector, int nr_iovecs,
1209 unsigned short idx, unsigned short bv_count,
1210 unsigned offset, unsigned len,
1211 unsigned split_bvec)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001212{
Mikulas Patockadba14162012-10-12 21:02:15 +01001213 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001214 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001215 unsigned target_bio_nr;
1216 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001217
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001218 /*
1219 * Does the target want to receive duplicate copies of the bio?
1220 */
1221 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1222 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001223
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001224 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1225 tio = alloc_tio(ci, ti, nr_iovecs, target_bio_nr);
1226 if (split_bvec)
1227 clone_split_bio(tio, bio, sector, idx, offset, len);
1228 else
1229 clone_bio(tio, bio, sector, idx, bv_count, len);
1230 __map_bio(tio);
1231 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001232}
1233
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001234typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001235
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001236static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001237{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001238 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001239}
1240
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001241static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001242{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001243 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001244}
1245
1246typedef bool (*is_split_required_fn)(struct dm_target *ti);
1247
1248static bool is_split_required_for_discard(struct dm_target *ti)
1249{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001250 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001251}
1252
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001253static int __send_changing_extent_only(struct clone_info *ci,
1254 get_num_bios_fn get_num_bios,
1255 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001256{
1257 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001258 sector_t len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001259 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001260
Mike Snitzera79245b2010-08-12 04:14:24 +01001261 do {
1262 ti = dm_table_find_target(ci->map, ci->sector);
1263 if (!dm_target_is_valid(ti))
1264 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001265
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001266 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001267 * Even though the device advertised support for this type of
1268 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001269 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001270 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001271 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001272 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1273 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001274 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001275
Mike Snitzer23508a92012-12-21 20:23:37 +00001276 if (is_split_required && !is_split_required(ti))
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001277 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1278 else
1279 len = min(ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001280
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001281 __send_duplicate_bios(ci, ti, num_bios, len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001282
1283 ci->sector += len;
1284 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001285
1286 return 0;
1287}
1288
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001289static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001290{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001291 return __send_changing_extent_only(ci, get_num_discard_bios,
1292 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001293}
1294
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001295static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001296{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001297 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001298}
1299
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001300/*
1301 * Find maximum number of sectors / bvecs we can process with a single bio.
1302 */
1303static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
1304{
1305 struct bio *bio = ci->bio;
1306 sector_t bv_len, total_len = 0;
1307
1308 for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1309 bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
1310
1311 if (bv_len > max)
1312 break;
1313
1314 max -= bv_len;
1315 total_len += bv_len;
1316 }
1317
1318 return total_len;
1319}
1320
1321static int __split_bvec_across_targets(struct clone_info *ci,
1322 struct dm_target *ti, sector_t max)
1323{
1324 struct bio *bio = ci->bio;
1325 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1326 sector_t remaining = to_sector(bv->bv_len);
1327 unsigned offset = 0;
1328 sector_t len;
1329
1330 do {
1331 if (offset) {
1332 ti = dm_table_find_target(ci->map, ci->sector);
1333 if (!dm_target_is_valid(ti))
1334 return -EIO;
1335
1336 max = max_io_len(ci->sector, ti);
1337 }
1338
1339 len = min(remaining, max);
1340
1341 __clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1342 bv->bv_offset + offset, len, 1);
1343
1344 ci->sector += len;
1345 ci->sector_count -= len;
1346 offset += to_bytes(len);
1347 } while (remaining -= len);
1348
1349 ci->idx++;
1350
1351 return 0;
1352}
1353
1354/*
1355 * Select the correct strategy for processing a non-flush bio.
1356 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001357static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Mikulas Patockadba14162012-10-12 21:02:15 +01001359 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001360 struct dm_target *ti;
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001361 sector_t len, max;
1362 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001364 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001365 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001366 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001367 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001368
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001369 ti = dm_table_find_target(ci->map, ci->sector);
1370 if (!dm_target_is_valid(ti))
1371 return -EIO;
1372
Mike Snitzer56a67df2010-08-12 04:14:10 +01001373 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001374
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001375 /*
1376 * Optimise for the simple case where we can do all of
1377 * the remaining io with a single clone.
1378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (ci->sector_count <= max) {
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001380 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1381 ci->idx, bio->bi_vcnt - ci->idx, 0,
1382 ci->sector_count, 0);
1383 ci->sector_count = 0;
1384 return 0;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001387 /*
1388 * There are some bvecs that don't span targets.
1389 * Do as many of these as possible.
1390 */
1391 if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1392 len = __len_within_target(ci, max, &idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001394 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1395 ci->idx, idx - ci->idx, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 ci->sector += len;
1398 ci->sector_count -= len;
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001399 ci->idx = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001403
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001404 /*
1405 * Handle a bvec that must be split between two or more targets.
1406 */
1407 return __split_bvec_across_targets(ci, ti, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001411 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001413static void __split_and_process_bio(struct mapped_device *md,
1414 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001417 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001419 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001420 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001421 return;
1422 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001423
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001424 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 ci.io = alloc_io(md);
1427 ci.io->error = 0;
1428 atomic_set(&ci.io->io_count, 1);
1429 ci.io->bio = bio;
1430 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001431 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ci.sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ci.idx = bio->bi_idx;
1434
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001435 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001436
Mike Snitzerb372d362010-09-08 18:07:01 +02001437 if (bio->bi_rw & REQ_FLUSH) {
1438 ci.bio = &ci.md->flush_bio;
1439 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001440 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001441 /* dec_pending submits any data associated with flush */
1442 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001443 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001444 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001445 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001446 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001450 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452/*-----------------------------------------------------------------
1453 * CRUD END
1454 *---------------------------------------------------------------*/
1455
Milan Brozf6fccb12008-07-21 12:00:37 +01001456static int dm_merge_bvec(struct request_queue *q,
1457 struct bvec_merge_data *bvm,
1458 struct bio_vec *biovec)
1459{
1460 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001461 struct dm_table *map = dm_get_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001462 struct dm_target *ti;
1463 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001464 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001465
1466 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001467 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001468
1469 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001470 if (!dm_target_is_valid(ti))
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001471 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001472
1473 /*
1474 * Find maximum amount of I/O that won't need splitting
1475 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001476 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001477 (sector_t) BIO_MAX_SECTORS);
1478 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1479 if (max_size < 0)
1480 max_size = 0;
1481
1482 /*
1483 * merge_bvec_fn() returns number of bytes
1484 * it can accept at this offset
1485 * max is precomputed maximal io size
1486 */
1487 if (max_size && ti->type->merge)
1488 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001489 /*
1490 * If the target doesn't support merge method and some of the devices
1491 * provided their merge_bvec method (we know this by looking at
1492 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1493 * entries. So always set max_size to 0, and the code below allows
1494 * just one page.
1495 */
1496 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1497
1498 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001499
Mikulas Patocka50371082008-10-01 14:39:17 +01001500out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001501 dm_put_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001502 /*
1503 * Always allow an entire first page
1504 */
1505 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1506 max_size = biovec->bv_len;
1507
Milan Brozf6fccb12008-07-21 12:00:37 +01001508 return max_size;
1509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/*
1512 * The request function that just remaps the bio built up by
1513 * dm_merge_bvec.
1514 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001515static void _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Kevin Corry12f03a42006-02-01 03:04:52 -08001517 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001519 int cpu;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001520 int srcu_idx;
1521 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001523 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Tejun Heo074a7ac2008-08-25 19:56:14 +09001525 cpu = part_stat_lock();
1526 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1527 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1528 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001529
Tejun Heo6a8736d2010-09-08 18:07:00 +02001530 /* if we're suspended, we have to queue this io for later */
1531 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001532 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Tejun Heo6a8736d2010-09-08 18:07:00 +02001534 if (bio_rw(bio) != READA)
1535 queue_io(md, bio);
1536 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001537 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001538 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001541 __split_and_process_bio(md, map, bio);
1542 dm_put_live_table(md, srcu_idx);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001543 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001544}
1545
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001546int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001547{
1548 return blk_queue_stackable(md->queue);
1549}
1550
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001551static void dm_request(struct request_queue *q, struct bio *bio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001552{
1553 struct mapped_device *md = q->queuedata;
1554
1555 if (dm_request_based(md))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001556 blk_queue_bio(q, bio);
1557 else
1558 _dm_request(q, bio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001559}
1560
1561void dm_dispatch_request(struct request *rq)
1562{
1563 int r;
1564
1565 if (blk_queue_io_stat(rq->q))
1566 rq->cmd_flags |= REQ_IO_STAT;
1567
1568 rq->start_time = jiffies;
1569 r = blk_insert_cloned_request(rq->q, rq);
1570 if (r)
1571 dm_complete_request(rq, r);
1572}
1573EXPORT_SYMBOL_GPL(dm_dispatch_request);
1574
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001575static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1576 void *data)
1577{
1578 struct dm_rq_target_io *tio = data;
Kent Overstreet94818742012-09-07 13:44:01 -07001579 struct dm_rq_clone_bio_info *info =
1580 container_of(bio, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001581
1582 info->orig = bio_orig;
1583 info->tio = tio;
1584 bio->bi_end_io = end_clone_bio;
1585 bio->bi_private = info;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001586
1587 return 0;
1588}
1589
1590static int setup_clone(struct request *clone, struct request *rq,
1591 struct dm_rq_target_io *tio)
1592{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001593 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001594
Tejun Heo29e40132010-09-08 18:07:00 +02001595 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1596 dm_rq_bio_constructor, tio);
1597 if (r)
1598 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001599
Tejun Heo29e40132010-09-08 18:07:00 +02001600 clone->cmd = rq->cmd;
1601 clone->cmd_len = rq->cmd_len;
1602 clone->sense = rq->sense;
1603 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001604 clone->end_io = end_clone_request;
1605 clone->end_io_data = tio;
1606
1607 return 0;
1608}
1609
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001610static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1611 gfp_t gfp_mask)
1612{
1613 struct request *clone;
1614 struct dm_rq_target_io *tio;
1615
1616 tio = alloc_rq_tio(md, gfp_mask);
1617 if (!tio)
1618 return NULL;
1619
1620 tio->md = md;
1621 tio->ti = NULL;
1622 tio->orig = rq;
1623 tio->error = 0;
1624 memset(&tio->info, 0, sizeof(tio->info));
1625
1626 clone = &tio->clone;
1627 if (setup_clone(clone, rq, tio)) {
1628 /* -ENOMEM */
1629 free_rq_tio(tio);
1630 return NULL;
1631 }
1632
1633 return clone;
1634}
1635
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001636/*
1637 * Called with the queue lock held.
1638 */
1639static int dm_prep_fn(struct request_queue *q, struct request *rq)
1640{
1641 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001642 struct request *clone;
1643
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001644 if (unlikely(rq->special)) {
1645 DMWARN("Already has something in rq->special.");
1646 return BLKPREP_KILL;
1647 }
1648
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001649 clone = clone_rq(rq, md, GFP_ATOMIC);
1650 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001651 return BLKPREP_DEFER;
1652
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001653 rq->special = clone;
1654 rq->cmd_flags |= REQ_DONTPREP;
1655
1656 return BLKPREP_OK;
1657}
1658
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001659/*
1660 * Returns:
1661 * 0 : the request has been processed (not requeued)
1662 * !0 : the request has been requeued
1663 */
1664static int map_request(struct dm_target *ti, struct request *clone,
1665 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001666{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001667 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001668 struct dm_rq_target_io *tio = clone->end_io_data;
1669
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001670 tio->ti = ti;
1671 r = ti->type->map_rq(ti, clone, &tio->info);
1672 switch (r) {
1673 case DM_MAPIO_SUBMITTED:
1674 /* The target has taken the I/O to submit by itself later */
1675 break;
1676 case DM_MAPIO_REMAPPED:
1677 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001678 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1679 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001680 dm_dispatch_request(clone);
1681 break;
1682 case DM_MAPIO_REQUEUE:
1683 /* The target wants to requeue the I/O */
1684 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001685 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001686 break;
1687 default:
1688 if (r > 0) {
1689 DMWARN("unimplemented target map return value: %d", r);
1690 BUG();
1691 }
1692
1693 /* The target wants to complete the I/O */
1694 dm_kill_unmapped_request(clone, r);
1695 break;
1696 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001697
1698 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001699}
1700
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001701static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1702{
1703 struct request *clone;
1704
1705 blk_start_request(orig);
1706 clone = orig->special;
1707 atomic_inc(&md->pending[rq_data_dir(clone)]);
1708
1709 /*
1710 * Hold the md reference here for the in-flight I/O.
1711 * We can't rely on the reference count by device opener,
1712 * because the device may be closed during the request completion
1713 * when all bios are completed.
1714 * See the comment in rq_completed() too.
1715 */
1716 dm_get(md);
1717
1718 return clone;
1719}
1720
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001721/*
1722 * q->request_fn for request-based dm.
1723 * Called with the queue lock held.
1724 */
1725static void dm_request_fn(struct request_queue *q)
1726{
1727 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001728 int srcu_idx;
1729 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001730 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001731 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001732 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001733
1734 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001735 * For suspend, check blk_queue_stopped() and increment
1736 * ->pending within a single queue_lock not to increment the
1737 * number of in-flight I/Os after the queue is stopped in
1738 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001739 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001740 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001741 rq = blk_peek_request(q);
1742 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001743 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001744
Tejun Heo29e40132010-09-08 18:07:00 +02001745 /* always use block 0 to find the target for flushes for now */
1746 pos = 0;
1747 if (!(rq->cmd_flags & REQ_FLUSH))
1748 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001749
Tejun Heo29e40132010-09-08 18:07:00 +02001750 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001751 if (!dm_target_is_valid(ti)) {
1752 /*
1753 * Must perform setup, that dm_done() requires,
1754 * before calling dm_kill_unmapped_request
1755 */
1756 DMERR_LIMIT("request attempted access beyond the end of device");
1757 clone = dm_start_request(md, rq);
1758 dm_kill_unmapped_request(clone, -EIO);
1759 continue;
1760 }
Tejun Heo29e40132010-09-08 18:07:00 +02001761
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001762 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001763 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001764
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001765 clone = dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001766
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001767 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001768 if (map_request(ti, clone, md))
1769 goto requeued;
1770
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001771 BUG_ON(!irqs_disabled());
1772 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001773 }
1774
1775 goto out;
1776
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001777requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001778 BUG_ON(!irqs_disabled());
1779 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001780
Jens Axboe7eaceac2011-03-10 08:52:07 +01001781delay_and_out:
1782 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001783out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001784 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001785}
1786
1787int dm_underlying_device_busy(struct request_queue *q)
1788{
1789 return blk_lld_busy(q);
1790}
1791EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1792
1793static int dm_lld_busy(struct request_queue *q)
1794{
1795 int r;
1796 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001797 struct dm_table *map = dm_get_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001798
1799 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1800 r = 1;
1801 else
1802 r = dm_table_any_busy_target(map);
1803
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001804 dm_put_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001805
1806 return r;
1807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809static int dm_any_congested(void *congested_data, int bdi_bits)
1810{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001811 int r = bdi_bits;
1812 struct mapped_device *md = congested_data;
1813 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001815 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001816 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001817 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001818 /*
1819 * Request-based dm cares about only own queue for
1820 * the query about congestion status of request_queue
1821 */
1822 if (dm_request_based(md))
1823 r = md->queue->backing_dev_info.state &
1824 bdi_bits;
1825 else
1826 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001827 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001828 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001829 }
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return r;
1832}
1833
1834/*-----------------------------------------------------------------
1835 * An IDR is used to keep track of allocated minor numbers.
1836 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001837static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001839 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001841 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844/*
1845 * See if the device with a specific minor # is free.
1846 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001847static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001849 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 if (minor >= (1 << MINORBITS))
1852 return -EINVAL;
1853
Tejun Heoc9d76be2013-02-27 17:04:26 -08001854 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001855 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Tejun Heoc9d76be2013-02-27 17:04:26 -08001857 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001859 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001860 idr_preload_end();
1861 if (r < 0)
1862 return r == -ENOSPC ? -EBUSY : r;
1863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001866static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001868 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Tejun Heoc9d76be2013-02-27 17:04:26 -08001870 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001871 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Tejun Heoc9d76be2013-02-27 17:04:26 -08001873 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001875 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001876 idr_preload_end();
1877 if (r < 0)
1878 return r;
1879 *minor = r;
1880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001883static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Mikulas Patocka53d59142009-04-02 19:55:37 +01001885static void dm_wq_work(struct work_struct *work);
1886
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001887static void dm_init_md_queue(struct mapped_device *md)
1888{
1889 /*
1890 * Request-based dm devices cannot be stacked on top of bio-based dm
1891 * devices. The type of this dm device has not been decided yet.
1892 * The type is decided at the first table loading time.
1893 * To prevent problematic device stacking, clear the queue flag
1894 * for request stacking support until then.
1895 *
1896 * This queue is new, so no concurrency on the queue_flags.
1897 */
1898 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1899
1900 md->queue->queuedata = md;
1901 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1902 md->queue->backing_dev_info.congested_data = md;
1903 blk_queue_make_request(md->queue, dm_request);
1904 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001905 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1906}
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908/*
1909 * Allocate and initialise a blank device with a given minor.
1910 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001911static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001914 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001915 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 if (!md) {
1918 DMWARN("unable to allocate device, out of memory.");
1919 return NULL;
1920 }
1921
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001922 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001923 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001926 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001927 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001928 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001929 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001931 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001933 r = init_srcu_struct(&md->io_barrier);
1934 if (r < 0)
1935 goto bad_io_barrier;
1936
Mike Snitzera5664da2010-08-12 04:14:01 +01001937 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001938 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001939 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001940 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001942 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001944 atomic_set(&md->uevent_seq, 0);
1945 INIT_LIST_HEAD(&md->uevent_list);
1946 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001948 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001950 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001952 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 md->disk = alloc_disk(1);
1955 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001956 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001958 atomic_set(&md->pending[0], 0);
1959 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001960 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001961 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001962 init_waitqueue_head(&md->eventq);
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 md->disk->major = _major;
1965 md->disk->first_minor = minor;
1966 md->disk->fops = &dm_blk_dops;
1967 md->disk->queue = md->queue;
1968 md->disk->private_data = md;
1969 sprintf(md->disk->disk_name, "dm-%d", minor);
1970 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001971 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Tejun Heo670368a2013-07-30 08:40:21 -04001973 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001974 if (!md->wq)
1975 goto bad_thread;
1976
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001977 md->bdev = bdget_disk(md->disk, 0);
1978 if (!md->bdev)
1979 goto bad_bdev;
1980
Tejun Heo6a8736d2010-09-08 18:07:00 +02001981 bio_init(&md->flush_bio);
1982 md->flush_bio.bi_bdev = md->bdev;
1983 md->flush_bio.bi_rw = WRITE_FLUSH;
1984
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001985 dm_stats_init(&md->stats);
1986
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001987 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001988 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001989 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001990 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001991
1992 BUG_ON(old_md != MINOR_ALLOCED);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return md;
1995
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001996bad_bdev:
1997 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001998bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001999 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00002000 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002001bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05002002 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002003bad_queue:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002004 cleanup_srcu_struct(&md->io_barrier);
2005bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002007bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002008 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002009bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 kfree(md);
2011 return NULL;
2012}
2013
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002014static void unlock_fs(struct mapped_device *md);
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016static void free_dev(struct mapped_device *md)
2017{
Tejun Heof331c022008-09-03 09:01:48 +02002018 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002019
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002020 unlock_fs(md);
2021 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00002022 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002023 if (md->io_pool)
2024 mempool_destroy(md->io_pool);
2025 if (md->bs)
2026 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01002027 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 del_gendisk(md->disk);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002029 cleanup_srcu_struct(&md->io_barrier);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002030 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002031
2032 spin_lock(&_minor_lock);
2033 md->disk->private_data = NULL;
2034 spin_unlock(&_minor_lock);
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05002037 blk_cleanup_queue(md->queue);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002038 dm_stats_cleanup(&md->stats);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002039 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 kfree(md);
2041}
2042
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002043static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2044{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002045 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002046
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002047 if (md->io_pool && md->bs) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002048 /* The md already has necessary mempools. */
2049 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2050 /*
2051 * Reload bioset because front_pad may have changed
2052 * because a different table was loaded.
2053 */
2054 bioset_free(md->bs);
2055 md->bs = p->bs;
2056 p->bs = NULL;
2057 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002058 /*
2059 * There's no need to reload with request-based dm
2060 * because the size of front_pad doesn't change.
2061 * Note for future: If you are to reload bioset,
2062 * prep-ed requests in the queue may refer
2063 * to bio from the old bioset, so you must walk
2064 * through the queue to unprep.
2065 */
2066 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002067 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002068 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002069
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002070 BUG_ON(!p || md->io_pool || md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002071
2072 md->io_pool = p->io_pool;
2073 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002074 md->bs = p->bs;
2075 p->bs = NULL;
2076
2077out:
2078 /* mempool bind completed, now no need any mempools in the table */
2079 dm_table_free_md_mempools(t);
2080}
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082/*
2083 * Bind a table to the device.
2084 */
2085static void event_callback(void *context)
2086{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002087 unsigned long flags;
2088 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct mapped_device *md = (struct mapped_device *) context;
2090
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002091 spin_lock_irqsave(&md->uevent_lock, flags);
2092 list_splice_init(&md->uevent_list, &uevents);
2093 spin_unlock_irqrestore(&md->uevent_lock, flags);
2094
Tejun Heoed9e1982008-08-25 19:56:05 +09002095 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 atomic_inc(&md->event_nr);
2098 wake_up(&md->eventq);
2099}
2100
Mike Snitzerc2176492011-01-13 19:53:46 +00002101/*
2102 * Protected by md->suspend_lock obtained by dm_swap_table().
2103 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002104static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002106 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002108 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002111/*
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002112 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2113 *
2114 * If this function returns 0, then the device is either a non-dm
2115 * device without a merge_bvec_fn, or it is a dm device that is
2116 * able to split any bios it receives that are too big.
2117 */
2118int dm_queue_merge_is_compulsory(struct request_queue *q)
2119{
2120 struct mapped_device *dev_md;
2121
2122 if (!q->merge_bvec_fn)
2123 return 0;
2124
2125 if (q->make_request_fn == dm_request) {
2126 dev_md = q->queuedata;
2127 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2128 return 0;
2129 }
2130
2131 return 1;
2132}
2133
2134static int dm_device_merge_is_compulsory(struct dm_target *ti,
2135 struct dm_dev *dev, sector_t start,
2136 sector_t len, void *data)
2137{
2138 struct block_device *bdev = dev->bdev;
2139 struct request_queue *q = bdev_get_queue(bdev);
2140
2141 return dm_queue_merge_is_compulsory(q);
2142}
2143
2144/*
2145 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2146 * on the properties of the underlying devices.
2147 */
2148static int dm_table_merge_is_optional(struct dm_table *table)
2149{
2150 unsigned i = 0;
2151 struct dm_target *ti;
2152
2153 while (i < dm_table_get_num_targets(table)) {
2154 ti = dm_table_get_target(table, i++);
2155
2156 if (ti->type->iterate_devices &&
2157 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2158 return 0;
2159 }
2160
2161 return 1;
2162}
2163
2164/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002165 * Returns old map, which caller must destroy.
2166 */
2167static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2168 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002170 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002171 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 sector_t size;
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002173 int merge_is_optional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002176
2177 /*
2178 * Wipe any geometry if the size of the table changed.
2179 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002180 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002181 memset(&md->geometry, 0, sizeof(md->geometry));
2182
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002183 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002185 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002186
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002187 /*
2188 * The queue hasn't been stopped yet, if the old table type wasn't
2189 * for request-based during suspension. So stop it to prevent
2190 * I/O mapping before resume.
2191 * This must be done before setting the queue restrictions,
2192 * because request-based dm may be run just after the setting.
2193 */
2194 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2195 stop_queue(q);
2196
2197 __bind_mempools(md, t);
2198
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002199 merge_is_optional = dm_table_merge_is_optional(t);
2200
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002201 old_map = md->map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002202 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002203 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2204
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002205 dm_table_set_restrictions(t, q, limits);
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002206 if (merge_is_optional)
2207 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2208 else
2209 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002210 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002211
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002212 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Alasdair G Kergona7940152009-12-10 23:52:23 +00002215/*
2216 * Returns unbound table for the caller to free.
2217 */
2218static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 struct dm_table *map = md->map;
2221
2222 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002223 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 dm_table_event_callback(map, NULL, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002226 rcu_assign_pointer(md->map, NULL);
2227 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002228
2229 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
2232/*
2233 * Constructor for a new device.
2234 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002235int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 struct mapped_device *md;
2238
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002239 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (!md)
2241 return -ENXIO;
2242
Milan Broz784aae72009-01-06 03:05:12 +00002243 dm_sysfs_init(md);
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 *result = md;
2246 return 0;
2247}
2248
Mike Snitzera5664da2010-08-12 04:14:01 +01002249/*
2250 * Functions to manage md->type.
2251 * All are required to hold md->type_lock.
2252 */
2253void dm_lock_md_type(struct mapped_device *md)
2254{
2255 mutex_lock(&md->type_lock);
2256}
2257
2258void dm_unlock_md_type(struct mapped_device *md)
2259{
2260 mutex_unlock(&md->type_lock);
2261}
2262
2263void dm_set_md_type(struct mapped_device *md, unsigned type)
2264{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002265 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002266 md->type = type;
2267}
2268
2269unsigned dm_get_md_type(struct mapped_device *md)
2270{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002271 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002272 return md->type;
2273}
2274
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002275struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2276{
2277 return md->immutable_target_type;
2278}
2279
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002280/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002281 * The queue_limits are only valid as long as you have a reference
2282 * count on 'md'.
2283 */
2284struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2285{
2286 BUG_ON(!atomic_read(&md->holders));
2287 return &md->queue->limits;
2288}
2289EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2290
2291/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002292 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2293 */
2294static int dm_init_request_based_queue(struct mapped_device *md)
2295{
2296 struct request_queue *q = NULL;
2297
2298 if (md->queue->elevator)
2299 return 1;
2300
2301 /* Fully initialize the queue */
2302 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2303 if (!q)
2304 return 0;
2305
2306 md->queue = q;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002307 dm_init_md_queue(md);
2308 blk_queue_softirq_done(md->queue, dm_softirq_done);
2309 blk_queue_prep_rq(md->queue, dm_prep_fn);
2310 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002311
2312 elv_register_queue(md->queue);
2313
2314 return 1;
2315}
2316
2317/*
2318 * Setup the DM device's queue based on md's type
2319 */
2320int dm_setup_md_queue(struct mapped_device *md)
2321{
2322 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2323 !dm_init_request_based_queue(md)) {
2324 DMWARN("Cannot initialize queue for request-based mapped device");
2325 return -EINVAL;
2326 }
2327
2328 return 0;
2329}
2330
David Teigland637842c2006-01-06 00:20:00 -08002331static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 unsigned minor = MINOR(dev);
2335
2336 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2337 return NULL;
2338
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002339 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002342 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002343 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002344 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002345 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002346 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002347 goto out;
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002350out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002351 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
David Teigland637842c2006-01-06 00:20:00 -08002353 return md;
2354}
2355
David Teiglandd229a952006-01-06 00:20:01 -08002356struct mapped_device *dm_get_md(dev_t dev)
2357{
2358 struct mapped_device *md = dm_find_md(dev);
2359
2360 if (md)
2361 dm_get(md);
2362
2363 return md;
2364}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002365EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002366
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002367void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002368{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002369 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
2372void dm_set_mdptr(struct mapped_device *md, void *ptr)
2373{
2374 md->interface_ptr = ptr;
2375}
2376
2377void dm_get(struct mapped_device *md)
2378{
2379 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002380 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002383const char *dm_device_name(struct mapped_device *md)
2384{
2385 return md->name;
2386}
2387EXPORT_SYMBOL_GPL(dm_device_name);
2388
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002389static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002391 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002392 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002394 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002395
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002396 spin_lock(&_minor_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002397 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002398 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2399 set_bit(DMF_FREEING, &md->flags);
2400 spin_unlock(&_minor_lock);
2401
2402 if (!dm_suspended_md(md)) {
2403 dm_table_presuspend_targets(map);
2404 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002406
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002407 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2408 dm_put_live_table(md, srcu_idx);
2409
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002410 /*
2411 * Rare, but there may be I/O requests still going to complete,
2412 * for example. Wait for all references to disappear.
2413 * No one should increment the reference count of the mapped_device,
2414 * after the mapped_device state becomes DMF_FREEING.
2415 */
2416 if (wait)
2417 while (atomic_read(&md->holders))
2418 msleep(1);
2419 else if (atomic_read(&md->holders))
2420 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2421 dm_device_name(md), atomic_read(&md->holders));
2422
2423 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002424 dm_table_destroy(__unbind(md));
2425 free_dev(md);
2426}
2427
2428void dm_destroy(struct mapped_device *md)
2429{
2430 __dm_destroy(md, true);
2431}
2432
2433void dm_destroy_immediate(struct mapped_device *md)
2434{
2435 __dm_destroy(md, false);
2436}
2437
2438void dm_put(struct mapped_device *md)
2439{
2440 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
Edward Goggin79eb8852007-05-09 02:32:56 -07002442EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Mikulas Patocka401600d2009-04-02 19:55:38 +01002444static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002445{
2446 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002447 DECLARE_WAITQUEUE(wait, current);
2448
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002449 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002450
2451 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002452 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002453
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002454 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002455 break;
2456
Mikulas Patocka401600d2009-04-02 19:55:38 +01002457 if (interruptible == TASK_INTERRUPTIBLE &&
2458 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002459 r = -EINTR;
2460 break;
2461 }
2462
2463 io_schedule();
2464 }
2465 set_current_state(TASK_RUNNING);
2466
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002467 remove_wait_queue(&md->wait, &wait);
2468
Milan Broz46125c12008-02-08 02:10:30 +00002469 return r;
2470}
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472/*
2473 * Process the deferred bios
2474 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002475static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Mikulas Patockaef208582009-04-02 19:55:38 +01002477 struct mapped_device *md = container_of(work, struct mapped_device,
2478 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002479 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002480 int srcu_idx;
2481 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002483 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002484
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002485 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002486 spin_lock_irq(&md->deferred_lock);
2487 c = bio_list_pop(&md->deferred);
2488 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002489
Tejun Heo6a8736d2010-09-08 18:07:00 +02002490 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002491 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002492
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002493 if (dm_request_based(md))
2494 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002495 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002496 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002497 }
Milan Broz73d410c2008-02-08 02:10:25 +00002498
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002499 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002502static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002503{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002504 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2505 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002506 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002507}
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002510 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002512struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Mike Christie87eb5b22013-03-01 22:45:48 +00002514 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002515 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002516 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Daniel Walkere61290a2008-02-08 02:10:08 +00002518 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002521 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002522 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Mike Snitzer3ae70652012-09-26 23:45:45 +01002524 /*
2525 * If the new table has no data devices, retain the existing limits.
2526 * This helps multipath with queue_if_no_path if all paths disappear,
2527 * then new I/O is queued based on these limits, and then some paths
2528 * reappear.
2529 */
2530 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002531 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002532 if (live_map)
2533 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002534 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002535 }
2536
Mike Christie87eb5b22013-03-01 22:45:48 +00002537 if (!live_map) {
2538 r = dm_calculate_queue_limits(table, &limits);
2539 if (r) {
2540 map = ERR_PTR(r);
2541 goto out;
2542 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002543 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002544
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002545 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002547out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002548 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002549 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552/*
2553 * Functions to lock and unlock any filesystem running on the
2554 * device.
2555 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002556static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002558 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002561
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002562 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002563 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002564 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002565 md->frozen_sb = NULL;
2566 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002567 }
2568
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002569 set_bit(DMF_FROZEN, &md->flags);
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return 0;
2572}
2573
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002574static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002576 if (!test_bit(DMF_FROZEN, &md->flags))
2577 return;
2578
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002579 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002581 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
2584/*
2585 * We need to be able to change a mapping table under a mounted
2586 * filesystem. For example we might want to move some data in
2587 * the background. Before the table can be swapped with
2588 * dm_bind_table, dm_suspend must be called to flush any in
2589 * flight bios and ensure that any further io gets deferred.
2590 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002591/*
2592 * Suspend mechanism in request-based dm.
2593 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002594 * 1. Flush all I/Os by lock_fs() if needed.
2595 * 2. Stop dispatching any I/O by stopping the request_queue.
2596 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002597 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002598 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002599 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002600int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002602 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002603 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002604 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002605 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Daniel Walkere61290a2008-02-08 02:10:08 +00002607 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002608
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002609 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002610 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002611 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002614 map = md->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002616 /*
2617 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2618 * This flag is cleared before dm_suspend returns.
2619 */
2620 if (noflush)
2621 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2622
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002623 /* This does not get reverted if there's an error later. */
2624 dm_table_presuspend_targets(map);
2625
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002626 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002627 * Flush I/O to the device.
2628 * Any I/O submitted after lock_fs() may not be flushed.
2629 * noflush takes precedence over do_lockfs.
2630 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002631 */
2632 if (!noflush && do_lockfs) {
2633 r = lock_fs(md);
2634 if (r)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002635 goto out_unlock;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002639 * Here we must make sure that no processes are submitting requests
2640 * to target drivers i.e. no one may be executing
2641 * __split_and_process_bio. This is called from dm_request and
2642 * dm_wq_work.
2643 *
2644 * To get all processes out of __split_and_process_bio in dm_request,
2645 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002646 * __split_and_process_bio from dm_request and quiesce the thread
2647 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2648 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002650 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002651 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002653 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002654 * Stop md->queue before flushing md->wq in case request-based
2655 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002656 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002657 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002658 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002659
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002660 flush_workqueue(md->wq);
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002663 * At this point no more requests are entering target request routines.
2664 * We call dm_wait_for_completion to wait for all existing requests
2665 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002667 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Milan Broz6d6f10d2008-02-08 02:10:22 +00002669 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002670 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002671 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002674 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002675 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002676
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002677 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002678 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002679
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002680 unlock_fs(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002681 goto out_unlock; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002682 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002683
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002684 /*
2685 * If dm_wait_for_completion returned 0, the device is completely
2686 * quiescent now. There is no request-processing activity. All new
2687 * requests are being added to md->deferred list.
2688 */
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 set_bit(DMF_SUSPENDED, &md->flags);
2691
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002692 dm_table_postsuspend_targets(map);
2693
Alasdair G Kergond2874832006-11-08 17:44:43 -08002694out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002695 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002696 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699int dm_resume(struct mapped_device *md)
2700{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002701 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002702 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Daniel Walkere61290a2008-02-08 02:10:08 +00002704 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002705 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002706 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002707
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002708 map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002709 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002710 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Milan Broz8757b772006-10-03 01:15:36 -07002712 r = dm_table_resume_targets(map);
2713 if (r)
2714 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002715
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002716 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002717
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002718 /*
2719 * Flushing deferred I/Os must be done after targets are resumed
2720 * so that mapping of targets can work correctly.
2721 * Request-based dm is queueing the deferred I/Os in its request_queue.
2722 */
2723 if (dm_request_based(md))
2724 start_queue(md->queue);
2725
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002726 unlock_fs(md);
2727
2728 clear_bit(DMF_SUSPENDED, &md->flags);
2729
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002730 r = 0;
2731out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002732 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002733
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002734 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002737/*
2738 * Internal suspend/resume works like userspace-driven suspend. It waits
2739 * until all bios finish and prevents issuing new bios to the target drivers.
2740 * It may be used only from the kernel.
2741 *
2742 * Internal suspend holds md->suspend_lock, which prevents interaction with
2743 * userspace-driven suspend.
2744 */
2745
2746void dm_internal_suspend(struct mapped_device *md)
2747{
2748 mutex_lock(&md->suspend_lock);
2749 if (dm_suspended_md(md))
2750 return;
2751
2752 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2753 synchronize_srcu(&md->io_barrier);
2754 flush_workqueue(md->wq);
2755 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2756}
2757
2758void dm_internal_resume(struct mapped_device *md)
2759{
2760 if (dm_suspended_md(md))
2761 goto done;
2762
2763 dm_queue_flush(md);
2764
2765done:
2766 mutex_unlock(&md->suspend_lock);
2767}
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769/*-----------------------------------------------------------------
2770 * Event notification.
2771 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002772int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002773 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002774{
Milan Broz60935eb2009-06-22 10:12:30 +01002775 char udev_cookie[DM_COOKIE_LENGTH];
2776 char *envp[] = { udev_cookie, NULL };
2777
2778 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002779 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002780 else {
2781 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2782 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002783 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2784 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002785 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002786}
2787
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002788uint32_t dm_next_uevent_seq(struct mapped_device *md)
2789{
2790 return atomic_add_return(1, &md->uevent_seq);
2791}
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793uint32_t dm_get_event_nr(struct mapped_device *md)
2794{
2795 return atomic_read(&md->event_nr);
2796}
2797
2798int dm_wait_event(struct mapped_device *md, int event_nr)
2799{
2800 return wait_event_interruptible(md->eventq,
2801 (event_nr != atomic_read(&md->event_nr)));
2802}
2803
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002804void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2805{
2806 unsigned long flags;
2807
2808 spin_lock_irqsave(&md->uevent_lock, flags);
2809 list_add(elist, &md->uevent_list);
2810 spin_unlock_irqrestore(&md->uevent_lock, flags);
2811}
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813/*
2814 * The gendisk is only valid as long as you have a reference
2815 * count on 'md'.
2816 */
2817struct gendisk *dm_disk(struct mapped_device *md)
2818{
2819 return md->disk;
2820}
2821
Milan Broz784aae72009-01-06 03:05:12 +00002822struct kobject *dm_kobject(struct mapped_device *md)
2823{
2824 return &md->kobj;
2825}
2826
2827/*
2828 * struct mapped_device should not be exported outside of dm.c
2829 * so use this check to verify that kobj is part of md structure
2830 */
2831struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2832{
2833 struct mapped_device *md;
2834
2835 md = container_of(kobj, struct mapped_device, kobj);
2836 if (&md->kobj != kobj)
2837 return NULL;
2838
Milan Broz4d89b7b2009-06-22 10:12:11 +01002839 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002840 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002841 return NULL;
2842
Milan Broz784aae72009-01-06 03:05:12 +00002843 dm_get(md);
2844 return md;
2845}
2846
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002847int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 return test_bit(DMF_SUSPENDED, &md->flags);
2850}
2851
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002852int dm_suspended(struct dm_target *ti)
2853{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002854 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002855}
2856EXPORT_SYMBOL_GPL(dm_suspended);
2857
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002858int dm_noflush_suspending(struct dm_target *ti)
2859{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002860 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002861}
2862EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2863
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002864struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002865{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002866 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
2867 struct kmem_cache *cachep;
2868 unsigned int pool_size;
2869 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002870
2871 if (!pools)
2872 return NULL;
2873
Jun'ichi Nomura23e50832013-03-01 22:45:48 +00002874 if (type == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002875 cachep = _io_cache;
2876 pool_size = 16;
2877 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2878 } else if (type == DM_TYPE_REQUEST_BASED) {
2879 cachep = _rq_tio_cache;
2880 pool_size = MIN_IOS;
2881 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2882 /* per_bio_data_size is not used. See __bind_mempools(). */
2883 WARN_ON(per_bio_data_size != 0);
2884 } else
2885 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002886
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002887 pools->io_pool = mempool_create_slab_pool(MIN_IOS, cachep);
2888 if (!pools->io_pool)
2889 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002890
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002891 pools->bs = bioset_create(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002892 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002893 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002894
Martin K. Petersena91a2782011-03-17 11:11:05 +01002895 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002896 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002897
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002898 return pools;
2899
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002900out:
2901 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002902
2903 return NULL;
2904}
2905
2906void dm_free_md_mempools(struct dm_md_mempools *pools)
2907{
2908 if (!pools)
2909 return;
2910
2911 if (pools->io_pool)
2912 mempool_destroy(pools->io_pool);
2913
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002914 if (pools->bs)
2915 bioset_free(pools->bs);
2916
2917 kfree(pools);
2918}
2919
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002920static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 .open = dm_blk_open,
2922 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002923 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002924 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 .owner = THIS_MODULE
2926};
2927
2928EXPORT_SYMBOL(dm_get_mapinfo);
2929
2930/*
2931 * module hooks
2932 */
2933module_init(dm_init);
2934module_exit(dm_exit);
2935
2936module_param(major, uint, 0);
2937MODULE_PARM_DESC(major, "The major number of the device mapper");
2938MODULE_DESCRIPTION(DM_NAME " driver");
2939MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2940MODULE_LICENSE("GPL");