blob: f2e5a50ee84ebcb2afa393cdbca6bd2e71839aee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080020#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010021#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080022
23#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Namhyung Kim71a16732011-10-31 20:18:54 +000027#ifdef CONFIG_PRINTK
28/*
29 * ratelimit state to be used in DMXXX_LIMIT().
30 */
31DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
32 DEFAULT_RATELIMIT_INTERVAL,
33 DEFAULT_RATELIMIT_BURST);
34EXPORT_SYMBOL(dm_ratelimit_state);
35#endif
36
Milan Broz60935eb2009-06-22 10:12:30 +010037/*
38 * Cookies are numeric values sent with CHANGE and REMOVE
39 * uevents while resuming, removing or renaming the device.
40 */
41#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42#define DM_COOKIE_LENGTH 24
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static const char *_name = DM_NAME;
45
46static unsigned int major = 0;
47static unsigned int _major = 0;
48
Alasdair G Kergond15b7742011-08-02 12:32:01 +010049static DEFINE_IDR(_minor_idr);
50
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070051static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000053 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * One of these is allocated per bio.
55 */
56struct dm_io {
57 struct mapped_device *md;
58 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010060 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080061 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010062 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040063 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000067 * For request-based dm.
68 * One of these is allocated per request.
69 */
70struct dm_rq_target_io {
71 struct mapped_device *md;
72 struct dm_target *ti;
73 struct request *orig, clone;
74 int error;
75 union map_info info;
76};
77
78/*
Kent Overstreet94818742012-09-07 13:44:01 -070079 * For request-based dm - the bio clones we allocate are embedded in these
80 * structs.
81 *
82 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
83 * the bioset is created - this means the bio has to come at the end of the
84 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000085 */
86struct dm_rq_clone_bio_info {
87 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010088 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -070089 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000090};
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092union map_info *dm_get_mapinfo(struct bio *bio)
93{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070094 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010095 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070096 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010099union map_info *dm_get_rq_mapinfo(struct request *rq)
100{
101 if (rq && rq->end_io_data)
102 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
103 return NULL;
104}
105EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
106
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700107#define MINOR_ALLOCED ((void *)-1)
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Bits for the md->flags field.
111 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100112#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800114#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700115#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700116#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800117#define DMF_NOFLUSH_SUSPENDING 5
Mikulas Patockad5b9dd02011-08-02 12:32:04 +0100118#define DMF_MERGE_IS_OPTIONAL 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Milan Broz304f3f62008-02-08 02:11:17 +0000120/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100121 * A dummy definition to make RCU happy.
122 * struct dm_table should never be dereferenced in this file.
123 */
124struct dm_table {
125 int undefined__;
126};
127
128/*
Milan Broz304f3f62008-02-08 02:11:17 +0000129 * Work processed by per-device workqueue.
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100132 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000133 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700135 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100137 /*
138 * The current mapping.
139 * Use dm_get_live_table{_fast} or take suspend_lock for
140 * dereference.
141 */
142 struct dm_table *map;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long flags;
145
Jens Axboe165125e2007-07-24 09:28:11 +0200146 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100147 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100148 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100149 struct mutex type_lock;
150
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000151 struct target_type *immutable_target_type;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800154 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 void *interface_ptr;
157
158 /*
159 * A list of ios that arrived while we were suspended.
160 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200161 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100163 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800164 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100165 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200168 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000169 */
170 struct workqueue_struct *wq;
171
172 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * io objects are allocated from here.
174 */
175 mempool_t *io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Stefan Bader9faf4002006-10-03 01:15:41 -0700177 struct bio_set *bs;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
180 * Event handling.
181 */
182 atomic_t event_nr;
183 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100184 atomic_t uevent_seq;
185 struct list_head uevent_list;
186 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /*
189 * freeze/thaw support require holding onto a super block
190 */
191 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100192 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800193
194 /* forced geometry settings */
195 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000196
197 /* sysfs handle */
198 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100199
Tejun Heod87f4c12010-09-03 11:56:19 +0200200 /* zero-length flush that will be cloned and submitted to targets */
201 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400202
203 struct dm_stats stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100206/*
207 * For mempools pre-allocation at the table loading time.
208 */
209struct dm_md_mempools {
210 mempool_t *io_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100211 struct bio_set *bs;
212};
213
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400214#define RESERVED_BIO_BASED_IOS 16
215#define RESERVED_REQUEST_BASED_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800216static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000217static struct kmem_cache *_rq_tio_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static int __init local_init(void)
220{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100221 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100224 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100226 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000228 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
229 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100230 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000231
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100232 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100233 if (r)
Jun'ichi Nomura23e50832013-03-01 22:45:48 +0000234 goto out_free_rq_tio_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 _major = major;
237 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100238 if (r < 0)
239 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!_major)
242 _major = r;
243
244 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100245
246out_uevent_exit:
247 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000248out_free_rq_tio_cache:
249 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100250out_free_io_cache:
251 kmem_cache_destroy(_io_cache);
252
253 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256static void local_exit(void)
257{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000258 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700260 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100261 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 _major = 0;
264
265 DMINFO("cleaned up");
266}
267
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000268static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 local_init,
270 dm_target_init,
271 dm_linear_init,
272 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000273 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100274 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400276 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277};
278
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000279static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 local_exit,
281 dm_target_exit,
282 dm_linear_exit,
283 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000284 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100285 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400287 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288};
289
290static int __init dm_init(void)
291{
292 const int count = ARRAY_SIZE(_inits);
293
294 int r, i;
295
296 for (i = 0; i < count; i++) {
297 r = _inits[i]();
298 if (r)
299 goto bad;
300 }
301
302 return 0;
303
304 bad:
305 while (i--)
306 _exits[i]();
307
308 return r;
309}
310
311static void __exit dm_exit(void)
312{
313 int i = ARRAY_SIZE(_exits);
314
315 while (i--)
316 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100317
318 /*
319 * Should be empty by this point.
320 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100321 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
325 * Block device functions
326 */
Mike Anderson432a2122009-12-10 23:52:20 +0000327int dm_deleting_md(struct mapped_device *md)
328{
329 return test_bit(DMF_DELETING, &md->flags);
330}
331
Al Virofe5f9f22008-03-02 10:29:31 -0500332static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct mapped_device *md;
335
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700336 spin_lock(&_minor_lock);
337
Al Virofe5f9f22008-03-02 10:29:31 -0500338 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700339 if (!md)
340 goto out;
341
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700342 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000343 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700344 md = NULL;
345 goto out;
346 }
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700349 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700350
351out:
352 spin_unlock(&_minor_lock);
353
354 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Al Virodb2a1442013-05-05 21:52:57 -0400357static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Al Virofe5f9f22008-03-02 10:29:31 -0500359 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200360
Milan Broz4a1aeb92011-01-13 19:59:48 +0000361 spin_lock(&_minor_lock);
362
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700363 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000365
366 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700369int dm_open_count(struct mapped_device *md)
370{
371 return atomic_read(&md->open_count);
372}
373
374/*
375 * Guarantees nothing is using the device before it's deleted.
376 */
377int dm_lock_for_deletion(struct mapped_device *md)
378{
379 int r = 0;
380
381 spin_lock(&_minor_lock);
382
383 if (dm_open_count(md))
384 r = -EBUSY;
385 else
386 set_bit(DMF_DELETING, &md->flags);
387
388 spin_unlock(&_minor_lock);
389
390 return r;
391}
392
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400393sector_t dm_get_size(struct mapped_device *md)
394{
395 return get_capacity(md->disk);
396}
397
398struct dm_stats *dm_get_stats(struct mapped_device *md)
399{
400 return &md->stats;
401}
402
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800403static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
404{
405 struct mapped_device *md = bdev->bd_disk->private_data;
406
407 return dm_get_geometry(md, geo);
408}
409
Al Virofe5f9f22008-03-02 10:29:31 -0500410static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700411 unsigned int cmd, unsigned long arg)
412{
Al Virofe5f9f22008-03-02 10:29:31 -0500413 struct mapped_device *md = bdev->bd_disk->private_data;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100414 int srcu_idx;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100415 struct dm_table *map;
Milan Brozaa129a22006-10-03 01:15:15 -0700416 struct dm_target *tgt;
417 int r = -ENOTTY;
418
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100419retry:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100420 map = dm_get_live_table(md, &srcu_idx);
421
Milan Brozaa129a22006-10-03 01:15:15 -0700422 if (!map || !dm_table_get_size(map))
423 goto out;
424
425 /* We only support devices that have a single target */
426 if (dm_table_get_num_targets(map) != 1)
427 goto out;
428
429 tgt = dm_table_get_target(map, 0);
430
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000431 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700432 r = -EAGAIN;
433 goto out;
434 }
435
436 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400437 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700438
439out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100440 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700441
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100442 if (r == -ENOTCONN) {
443 msleep(10);
444 goto retry;
445 }
446
Milan Brozaa129a22006-10-03 01:15:15 -0700447 return r;
448}
449
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100450static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 return mempool_alloc(md->io_pool, GFP_NOIO);
453}
454
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100455static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 mempool_free(io, md->io_pool);
458}
459
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100460static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Mikulas Patockadba14162012-10-12 21:02:15 +0100462 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000465static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
466 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100467{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000468 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100469}
470
471static void free_rq_tio(struct dm_rq_target_io *tio)
472{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000473 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100474}
475
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000476static int md_in_flight(struct mapped_device *md)
477{
478 return atomic_read(&md->pending[READ]) +
479 atomic_read(&md->pending[WRITE]);
480}
481
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800482static void start_io_acct(struct dm_io *io)
483{
484 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400485 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900486 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400487 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800488
489 io->start_time = jiffies;
490
Tejun Heo074a7ac2008-08-25 19:56:14 +0900491 cpu = part_stat_lock();
492 part_round_stats(cpu, &dm_disk(md)->part0);
493 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100494 atomic_set(&dm_disk(md)->part0.in_flight[rw],
495 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400496
497 if (unlikely(dm_stats_used(&md->stats)))
498 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
499 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800500}
501
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000502static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800503{
504 struct mapped_device *md = io->md;
505 struct bio *bio = io->bio;
506 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900507 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800508 int rw = bio_data_dir(bio);
509
Tejun Heo074a7ac2008-08-25 19:56:14 +0900510 cpu = part_stat_lock();
511 part_round_stats(cpu, &dm_disk(md)->part0);
512 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
513 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800514
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400515 if (unlikely(dm_stats_used(&md->stats)))
516 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
517 bio_sectors(bio), true, duration, &io->stats_aux);
518
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100519 /*
520 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200521 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100522 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100523 pending = atomic_dec_return(&md->pending[rw]);
524 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200525 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800526
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000527 /* nudge anyone waiting on suspend queue */
528 if (!pending)
529 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
533 * Add the bio to the list of deferred io.
534 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100535static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200537 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200539 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200541 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200542 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/*
546 * Everyone (including functions in this file), should use this
547 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100548 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100550struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100552 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100554 return srcu_dereference(md->map, &md->io_barrier);
555}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100557void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
558{
559 srcu_read_unlock(&md->io_barrier, srcu_idx);
560}
561
562void dm_sync_table(struct mapped_device *md)
563{
564 synchronize_srcu(&md->io_barrier);
565 synchronize_rcu_expedited();
566}
567
568/*
569 * A fast alternative to dm_get_live_table/dm_put_live_table.
570 * The caller must not block between these two functions.
571 */
572static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
573{
574 rcu_read_lock();
575 return rcu_dereference(md->map);
576}
577
578static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
579{
580 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800583/*
584 * Get the geometry associated with a dm device
585 */
586int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
587{
588 *geo = md->geometry;
589
590 return 0;
591}
592
593/*
594 * Set the geometry of a device.
595 */
596int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
597{
598 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
599
600 if (geo->start > sz) {
601 DMWARN("Start sector is beyond the geometry limits.");
602 return -EINVAL;
603 }
604
605 md->geometry = *geo;
606
607 return 0;
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*-----------------------------------------------------------------
611 * CRUD START:
612 * A more elegant soln is in the works that uses the queue
613 * merge fn, unfortunately there are a couple of changes to
614 * the block layer that I want to make for this. So in the
615 * interests of getting something for people to use I give
616 * you this clearly demarcated crap.
617 *---------------------------------------------------------------*/
618
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800619static int __noflush_suspending(struct mapped_device *md)
620{
621 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/*
625 * Decrements the number of outstanding ios that a bio has been
626 * cloned into, completing the original io if necc.
627 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800628static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800630 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000631 int io_error;
632 struct bio *bio;
633 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800634
635 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100636 if (unlikely(error)) {
637 spin_lock_irqsave(&io->endio_lock, flags);
638 if (!(io->error > 0 && __noflush_suspending(md)))
639 io->error = error;
640 spin_unlock_irqrestore(&io->endio_lock, flags);
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800644 if (io->error == DM_ENDIO_REQUEUE) {
645 /*
646 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800647 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100648 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200649 if (__noflush_suspending(md))
650 bio_list_add_head(&md->deferred, io->bio);
651 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800652 /* noflush suspend was interrupted. */
653 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100654 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800655 }
656
Milan Brozb35f8ca2009-03-16 17:44:36 +0000657 io_error = io->error;
658 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200659 end_io_acct(io);
660 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100661
Tejun Heo6a8736d2010-09-08 18:07:00 +0200662 if (io_error == DM_ENDIO_REQUEUE)
663 return;
664
Mike Snitzerb372d362010-09-08 18:07:01 +0200665 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100666 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200667 * Preflush done for flush with data, reissue
668 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100669 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200670 bio->bi_rw &= ~REQ_FLUSH;
671 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100672 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200673 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700674 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200675 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678}
679
NeilBrown6712ecf2007-09-27 12:47:43 +0200680static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100683 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000684 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700685 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 dm_endio_fn endio = tio->ti->type->end_io;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
689 error = -EIO;
690
691 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000692 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800693 if (r < 0 || r == DM_ENDIO_REQUEUE)
694 /*
695 * error and requeue request are handled
696 * in dec_pending().
697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800699 else if (r == DM_ENDIO_INCOMPLETE)
700 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200701 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800702 else if (r) {
703 DMWARN("unimplemented target endio return value: %d", r);
704 BUG();
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
Stefan Bader9faf4002006-10-03 01:15:41 -0700708 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000709 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100712/*
713 * Partial completion handling for request-based dm
714 */
715static void end_clone_bio(struct bio *clone, int error)
716{
717 struct dm_rq_clone_bio_info *info = clone->bi_private;
718 struct dm_rq_target_io *tio = info->tio;
719 struct bio *bio = info->orig;
720 unsigned int nr_bytes = info->orig->bi_size;
721
722 bio_put(clone);
723
724 if (tio->error)
725 /*
726 * An error has already been detected on the request.
727 * Once error occurred, just let clone->end_io() handle
728 * the remainder.
729 */
730 return;
731 else if (error) {
732 /*
733 * Don't notice the error to the upper layer yet.
734 * The error handling decision is made by the target driver,
735 * when the request is completed.
736 */
737 tio->error = error;
738 return;
739 }
740
741 /*
742 * I/O for the bio successfully completed.
743 * Notice the data completion to the upper layer.
744 */
745
746 /*
747 * bios are processed from the head of the list.
748 * So the completing bio should always be rq->bio.
749 * If it's not, something wrong is happening.
750 */
751 if (tio->orig->bio != bio)
752 DMERR("bio completion is going in the middle of the request");
753
754 /*
755 * Update the original request.
756 * Do not use blk_end_request() here, because it may complete
757 * the original request before the clone, and break the ordering.
758 */
759 blk_update_request(tio->orig, 0, nr_bytes);
760}
761
762/*
763 * Don't touch any member of the md after calling this function because
764 * the md may be freed in dm_put() at the end of this function.
765 * Or do dm_get() before calling this function and dm_put() later.
766 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000767static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100768{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000769 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100770
771 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000772 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100773 wake_up(&md->wait);
774
Jens Axboea8c32a52012-11-06 12:24:26 +0100775 /*
776 * Run this off this callpath, as drivers could invoke end_io while
777 * inside their request_fn (and holding the queue lock). Calling
778 * back into ->request_fn() could deadlock attempting to grab the
779 * queue lock again.
780 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100781 if (run_queue)
Jens Axboea8c32a52012-11-06 12:24:26 +0100782 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100783
784 /*
785 * dm_put() must be at the end of this function. See the comment above
786 */
787 dm_put(md);
788}
789
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100790static void free_rq_clone(struct request *clone)
791{
792 struct dm_rq_target_io *tio = clone->end_io_data;
793
794 blk_rq_unprep_clone(clone);
795 free_rq_tio(tio);
796}
797
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000798/*
799 * Complete the clone and the original request.
800 * Must be called without queue lock.
801 */
802static void dm_end_request(struct request *clone, int error)
803{
804 int rw = rq_data_dir(clone);
805 struct dm_rq_target_io *tio = clone->end_io_data;
806 struct mapped_device *md = tio->md;
807 struct request *rq = tio->orig;
808
Tejun Heo29e40132010-09-08 18:07:00 +0200809 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000810 rq->errors = clone->errors;
811 rq->resid_len = clone->resid_len;
812
813 if (rq->sense)
814 /*
815 * We are using the sense buffer of the original
816 * request.
817 * So setting the length of the sense data is enough.
818 */
819 rq->sense_len = clone->sense_len;
820 }
821
822 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200823 blk_end_request_all(rq, error);
824 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000825}
826
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100827static void dm_unprep_request(struct request *rq)
828{
829 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100830
831 rq->special = NULL;
832 rq->cmd_flags &= ~REQ_DONTPREP;
833
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100834 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100835}
836
837/*
838 * Requeue the original request of a clone.
839 */
840void dm_requeue_unmapped_request(struct request *clone)
841{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000842 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100843 struct dm_rq_target_io *tio = clone->end_io_data;
844 struct mapped_device *md = tio->md;
845 struct request *rq = tio->orig;
846 struct request_queue *q = rq->q;
847 unsigned long flags;
848
849 dm_unprep_request(rq);
850
851 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100852 blk_requeue_request(q, rq);
853 spin_unlock_irqrestore(q->queue_lock, flags);
854
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000855 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100856}
857EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
858
859static void __stop_queue(struct request_queue *q)
860{
861 blk_stop_queue(q);
862}
863
864static void stop_queue(struct request_queue *q)
865{
866 unsigned long flags;
867
868 spin_lock_irqsave(q->queue_lock, flags);
869 __stop_queue(q);
870 spin_unlock_irqrestore(q->queue_lock, flags);
871}
872
873static void __start_queue(struct request_queue *q)
874{
875 if (blk_queue_stopped(q))
876 blk_start_queue(q);
877}
878
879static void start_queue(struct request_queue *q)
880{
881 unsigned long flags;
882
883 spin_lock_irqsave(q->queue_lock, flags);
884 __start_queue(q);
885 spin_unlock_irqrestore(q->queue_lock, flags);
886}
887
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000888static void dm_done(struct request *clone, int error, bool mapped)
889{
890 int r = error;
891 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100892 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000893
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100894 if (tio->ti) {
895 rq_end_io = tio->ti->type->rq_end_io;
896
897 if (mapped && rq_end_io)
898 r = rq_end_io(tio->ti, clone, error, &tio->info);
899 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000900
901 if (r <= 0)
902 /* The target wants to complete the I/O */
903 dm_end_request(clone, r);
904 else if (r == DM_ENDIO_INCOMPLETE)
905 /* The target will handle the I/O */
906 return;
907 else if (r == DM_ENDIO_REQUEUE)
908 /* The target wants to requeue the I/O */
909 dm_requeue_unmapped_request(clone);
910 else {
911 DMWARN("unimplemented target endio return value: %d", r);
912 BUG();
913 }
914}
915
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100916/*
917 * Request completion handler for request-based dm
918 */
919static void dm_softirq_done(struct request *rq)
920{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000921 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100922 struct request *clone = rq->completion_data;
923 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100924
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000925 if (rq->cmd_flags & REQ_FAILED)
926 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100927
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000928 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100929}
930
931/*
932 * Complete the clone and the original request with the error status
933 * through softirq context.
934 */
935static void dm_complete_request(struct request *clone, int error)
936{
937 struct dm_rq_target_io *tio = clone->end_io_data;
938 struct request *rq = tio->orig;
939
940 tio->error = error;
941 rq->completion_data = clone;
942 blk_complete_request(rq);
943}
944
945/*
946 * Complete the not-mapped clone and the original request with the error status
947 * through softirq context.
948 * Target's rq_end_io() function isn't called.
949 * This may be used when the target's map_rq() function fails.
950 */
951void dm_kill_unmapped_request(struct request *clone, int error)
952{
953 struct dm_rq_target_io *tio = clone->end_io_data;
954 struct request *rq = tio->orig;
955
956 rq->cmd_flags |= REQ_FAILED;
957 dm_complete_request(clone, error);
958}
959EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
960
961/*
962 * Called with the queue lock held
963 */
964static void end_clone_request(struct request *clone, int error)
965{
966 /*
967 * For just cleaning up the information of the queue in which
968 * the clone was dispatched.
969 * The clone is *NOT* freed actually here because it is alloced from
970 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
971 */
972 __blk_put_request(clone->q, clone);
973
974 /*
975 * Actual request completion is done in a softirq context which doesn't
976 * hold the queue lock. Otherwise, deadlock could occur because:
977 * - another request may be submitted by the upper level driver
978 * of the stacking during the completion
979 * - the submission which requires queue lock may be done
980 * against this queue
981 */
982 dm_complete_request(clone, error);
983}
984
Mike Snitzer56a67df2010-08-12 04:14:10 +0100985/*
986 * Return maximum size of I/O possible at the supplied sector up to the current
987 * target boundary.
988 */
989static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100991 sector_t target_offset = dm_target_offset(ti, sector);
992
993 return ti->len - target_offset;
994}
995
996static sector_t max_io_len(sector_t sector, struct dm_target *ti)
997{
998 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +0100999 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001002 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001004 if (ti->max_io_len) {
1005 offset = dm_target_offset(ti, sector);
1006 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1007 max_len = sector_div(offset, ti->max_io_len);
1008 else
1009 max_len = offset & (ti->max_io_len - 1);
1010 max_len = ti->max_io_len - max_len;
1011
1012 if (len > max_len)
1013 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
1016 return len;
1017}
1018
Mike Snitzer542f9032012-07-27 15:08:00 +01001019int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1020{
1021 if (len > UINT_MAX) {
1022 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1023 (unsigned long long)len, UINT_MAX);
1024 ti->error = "Maximum size of target IO is too large";
1025 return -EINVAL;
1026 }
1027
1028 ti->max_io_len = (uint32_t) len;
1029
1030 return 0;
1031}
1032EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1033
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001034static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001037 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001038 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001039 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001040 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 clone->bi_end_io = clone_endio;
1043 clone->bi_private = tio;
1044
1045 /*
1046 * Map the clone. If r == 0 we don't need to do
1047 * anything, the target has assumed ownership of
1048 * this io.
1049 */
1050 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001051 sector = clone->bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001052 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001053 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001055
Mike Snitzerd07335e2010-11-16 12:52:38 +01001056 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1057 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001060 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1061 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001062 md = tio->io->md;
1063 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001064 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001065 } else if (r) {
1066 DMWARN("unimplemented target map return value: %d", r);
1067 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069}
1070
1071struct clone_info {
1072 struct mapped_device *md;
1073 struct dm_table *map;
1074 struct bio *bio;
1075 struct dm_io *io;
1076 sector_t sector;
1077 sector_t sector_count;
1078 unsigned short idx;
1079};
1080
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001081static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1082{
1083 bio->bi_sector = sector;
1084 bio->bi_size = to_bytes(len);
1085}
1086
1087static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1088{
1089 bio->bi_idx = idx;
1090 bio->bi_vcnt = idx + bv_count;
1091 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1092}
1093
1094static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1095 unsigned short idx, unsigned len, unsigned offset,
1096 unsigned trim)
1097{
1098 if (!bio_integrity(bio))
1099 return;
1100
1101 bio_integrity_clone(clone, bio, GFP_NOIO);
1102
1103 if (trim)
1104 bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001108 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001110static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
1111 sector_t sector, unsigned short idx,
1112 unsigned offset, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Mikulas Patockadba14162012-10-12 21:02:15 +01001114 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct bio_vec *bv = bio->bi_io_vec + idx;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *clone->bi_io_vec = *bv;
1118
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001119 bio_setup_sector(clone, sector, len);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001122 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 clone->bi_vcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 clone->bi_io_vec->bv_offset = offset;
1125 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001126 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001128 clone_bio_integrity(bio, clone, idx, len, offset, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131/*
1132 * Creates a bio that consists of range of complete bvecs.
1133 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001134static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1135 sector_t sector, unsigned short idx,
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001136 unsigned short bv_count, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Mikulas Patockadba14162012-10-12 21:02:15 +01001138 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001139 unsigned trim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Stefan Bader9faf4002006-10-03 01:15:41 -07001141 __bio_clone(clone, bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001142 bio_setup_sector(clone, sector, len);
1143 bio_setup_bv(clone, idx, bv_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001145 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1146 trim = 1;
1147 clone_bio_integrity(bio, clone, idx, len, 0, trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001150static struct dm_target_io *alloc_tio(struct clone_info *ci,
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001151 struct dm_target *ti, int nr_iovecs,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001152 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001153{
Mikulas Patockadba14162012-10-12 21:02:15 +01001154 struct dm_target_io *tio;
1155 struct bio *clone;
1156
1157 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1158 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001159
1160 tio->io = ci->io;
1161 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001162 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001163 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001164
1165 return tio;
1166}
1167
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001168static void __clone_and_map_simple_bio(struct clone_info *ci,
1169 struct dm_target *ti,
1170 unsigned target_bio_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001171{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001172 struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001173 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001174
Mike Snitzer06a426c2010-08-12 04:14:09 +01001175 /*
1176 * Discard requests require the bio's inline iovecs be initialized.
1177 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1178 * and discard, so no need for concern about wasted bvec allocations.
1179 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001180 __bio_clone(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001181 if (len)
1182 bio_setup_sector(clone, ci->sector, len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001183
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001184 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001185}
1186
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001187static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1188 unsigned num_bios, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001189{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001190 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001191
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001192 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001193 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001194}
1195
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001196static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001197{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001198 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001199 struct dm_target *ti;
1200
Mike Snitzerb372d362010-09-08 18:07:01 +02001201 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001202 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001203 __send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001204
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001205 return 0;
1206}
1207
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001208static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1209 sector_t sector, int nr_iovecs,
1210 unsigned short idx, unsigned short bv_count,
1211 unsigned offset, unsigned len,
1212 unsigned split_bvec)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001213{
Mikulas Patockadba14162012-10-12 21:02:15 +01001214 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001215 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001216 unsigned target_bio_nr;
1217 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001218
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001219 /*
1220 * Does the target want to receive duplicate copies of the bio?
1221 */
1222 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1223 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001224
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001225 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1226 tio = alloc_tio(ci, ti, nr_iovecs, target_bio_nr);
1227 if (split_bvec)
1228 clone_split_bio(tio, bio, sector, idx, offset, len);
1229 else
1230 clone_bio(tio, bio, sector, idx, bv_count, len);
1231 __map_bio(tio);
1232 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001233}
1234
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001235typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001236
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001237static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001238{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001239 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001240}
1241
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001242static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001243{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001244 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001245}
1246
1247typedef bool (*is_split_required_fn)(struct dm_target *ti);
1248
1249static bool is_split_required_for_discard(struct dm_target *ti)
1250{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001251 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001252}
1253
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001254static int __send_changing_extent_only(struct clone_info *ci,
1255 get_num_bios_fn get_num_bios,
1256 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001257{
1258 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001259 sector_t len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001260 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001261
Mike Snitzera79245b2010-08-12 04:14:24 +01001262 do {
1263 ti = dm_table_find_target(ci->map, ci->sector);
1264 if (!dm_target_is_valid(ti))
1265 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001266
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001267 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001268 * Even though the device advertised support for this type of
1269 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001270 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001271 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001272 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001273 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1274 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001275 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001276
Mike Snitzer23508a92012-12-21 20:23:37 +00001277 if (is_split_required && !is_split_required(ti))
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001278 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1279 else
1280 len = min(ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001281
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001282 __send_duplicate_bios(ci, ti, num_bios, len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001283
1284 ci->sector += len;
1285 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001286
1287 return 0;
1288}
1289
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001290static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001291{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001292 return __send_changing_extent_only(ci, get_num_discard_bios,
1293 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001294}
1295
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001296static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001297{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001298 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001299}
1300
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001301/*
1302 * Find maximum number of sectors / bvecs we can process with a single bio.
1303 */
1304static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
1305{
1306 struct bio *bio = ci->bio;
1307 sector_t bv_len, total_len = 0;
1308
1309 for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1310 bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
1311
1312 if (bv_len > max)
1313 break;
1314
1315 max -= bv_len;
1316 total_len += bv_len;
1317 }
1318
1319 return total_len;
1320}
1321
1322static int __split_bvec_across_targets(struct clone_info *ci,
1323 struct dm_target *ti, sector_t max)
1324{
1325 struct bio *bio = ci->bio;
1326 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1327 sector_t remaining = to_sector(bv->bv_len);
1328 unsigned offset = 0;
1329 sector_t len;
1330
1331 do {
1332 if (offset) {
1333 ti = dm_table_find_target(ci->map, ci->sector);
1334 if (!dm_target_is_valid(ti))
1335 return -EIO;
1336
1337 max = max_io_len(ci->sector, ti);
1338 }
1339
1340 len = min(remaining, max);
1341
1342 __clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1343 bv->bv_offset + offset, len, 1);
1344
1345 ci->sector += len;
1346 ci->sector_count -= len;
1347 offset += to_bytes(len);
1348 } while (remaining -= len);
1349
1350 ci->idx++;
1351
1352 return 0;
1353}
1354
1355/*
1356 * Select the correct strategy for processing a non-flush bio.
1357 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001358static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Mikulas Patockadba14162012-10-12 21:02:15 +01001360 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001361 struct dm_target *ti;
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001362 sector_t len, max;
1363 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001365 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001366 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001367 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001368 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001369
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001370 ti = dm_table_find_target(ci->map, ci->sector);
1371 if (!dm_target_is_valid(ti))
1372 return -EIO;
1373
Mike Snitzer56a67df2010-08-12 04:14:10 +01001374 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001375
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001376 /*
1377 * Optimise for the simple case where we can do all of
1378 * the remaining io with a single clone.
1379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (ci->sector_count <= max) {
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001381 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1382 ci->idx, bio->bi_vcnt - ci->idx, 0,
1383 ci->sector_count, 0);
1384 ci->sector_count = 0;
1385 return 0;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001388 /*
1389 * There are some bvecs that don't span targets.
1390 * Do as many of these as possible.
1391 */
1392 if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1393 len = __len_within_target(ci, max, &idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001395 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1396 ci->idx, idx - ci->idx, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 ci->sector += len;
1399 ci->sector_count -= len;
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001400 ci->idx = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001404
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001405 /*
1406 * Handle a bvec that must be split between two or more targets.
1407 */
1408 return __split_bvec_across_targets(ci, ti, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001412 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001414static void __split_and_process_bio(struct mapped_device *md,
1415 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001418 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001420 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001421 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001422 return;
1423 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001424
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001425 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ci.io = alloc_io(md);
1428 ci.io->error = 0;
1429 atomic_set(&ci.io->io_count, 1);
1430 ci.io->bio = bio;
1431 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001432 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ci.sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 ci.idx = bio->bi_idx;
1435
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001436 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001437
Mike Snitzerb372d362010-09-08 18:07:01 +02001438 if (bio->bi_rw & REQ_FLUSH) {
1439 ci.bio = &ci.md->flush_bio;
1440 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001441 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001442 /* dec_pending submits any data associated with flush */
1443 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001444 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001445 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001446 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001447 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001451 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453/*-----------------------------------------------------------------
1454 * CRUD END
1455 *---------------------------------------------------------------*/
1456
Milan Brozf6fccb12008-07-21 12:00:37 +01001457static int dm_merge_bvec(struct request_queue *q,
1458 struct bvec_merge_data *bvm,
1459 struct bio_vec *biovec)
1460{
1461 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001462 struct dm_table *map = dm_get_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001463 struct dm_target *ti;
1464 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001465 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001466
1467 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001468 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001469
1470 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001471 if (!dm_target_is_valid(ti))
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001472 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001473
1474 /*
1475 * Find maximum amount of I/O that won't need splitting
1476 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001477 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001478 (sector_t) BIO_MAX_SECTORS);
1479 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1480 if (max_size < 0)
1481 max_size = 0;
1482
1483 /*
1484 * merge_bvec_fn() returns number of bytes
1485 * it can accept at this offset
1486 * max is precomputed maximal io size
1487 */
1488 if (max_size && ti->type->merge)
1489 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001490 /*
1491 * If the target doesn't support merge method and some of the devices
1492 * provided their merge_bvec method (we know this by looking at
1493 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1494 * entries. So always set max_size to 0, and the code below allows
1495 * just one page.
1496 */
1497 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1498
1499 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001500
Mikulas Patocka50371082008-10-01 14:39:17 +01001501out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001502 dm_put_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001503 /*
1504 * Always allow an entire first page
1505 */
1506 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1507 max_size = biovec->bv_len;
1508
Milan Brozf6fccb12008-07-21 12:00:37 +01001509 return max_size;
1510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512/*
1513 * The request function that just remaps the bio built up by
1514 * dm_merge_bvec.
1515 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001516static void _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Kevin Corry12f03a42006-02-01 03:04:52 -08001518 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001520 int cpu;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001521 int srcu_idx;
1522 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001524 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Tejun Heo074a7ac2008-08-25 19:56:14 +09001526 cpu = part_stat_lock();
1527 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1528 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1529 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001530
Tejun Heo6a8736d2010-09-08 18:07:00 +02001531 /* if we're suspended, we have to queue this io for later */
1532 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001533 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Tejun Heo6a8736d2010-09-08 18:07:00 +02001535 if (bio_rw(bio) != READA)
1536 queue_io(md, bio);
1537 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001538 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001539 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001542 __split_and_process_bio(md, map, bio);
1543 dm_put_live_table(md, srcu_idx);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001544 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001545}
1546
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001547int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001548{
1549 return blk_queue_stackable(md->queue);
1550}
1551
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001552static void dm_request(struct request_queue *q, struct bio *bio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001553{
1554 struct mapped_device *md = q->queuedata;
1555
1556 if (dm_request_based(md))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001557 blk_queue_bio(q, bio);
1558 else
1559 _dm_request(q, bio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001560}
1561
1562void dm_dispatch_request(struct request *rq)
1563{
1564 int r;
1565
1566 if (blk_queue_io_stat(rq->q))
1567 rq->cmd_flags |= REQ_IO_STAT;
1568
1569 rq->start_time = jiffies;
1570 r = blk_insert_cloned_request(rq->q, rq);
1571 if (r)
1572 dm_complete_request(rq, r);
1573}
1574EXPORT_SYMBOL_GPL(dm_dispatch_request);
1575
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001576static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1577 void *data)
1578{
1579 struct dm_rq_target_io *tio = data;
Kent Overstreet94818742012-09-07 13:44:01 -07001580 struct dm_rq_clone_bio_info *info =
1581 container_of(bio, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001582
1583 info->orig = bio_orig;
1584 info->tio = tio;
1585 bio->bi_end_io = end_clone_bio;
1586 bio->bi_private = info;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001587
1588 return 0;
1589}
1590
1591static int setup_clone(struct request *clone, struct request *rq,
1592 struct dm_rq_target_io *tio)
1593{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001594 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001595
Tejun Heo29e40132010-09-08 18:07:00 +02001596 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1597 dm_rq_bio_constructor, tio);
1598 if (r)
1599 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001600
Tejun Heo29e40132010-09-08 18:07:00 +02001601 clone->cmd = rq->cmd;
1602 clone->cmd_len = rq->cmd_len;
1603 clone->sense = rq->sense;
1604 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001605 clone->end_io = end_clone_request;
1606 clone->end_io_data = tio;
1607
1608 return 0;
1609}
1610
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001611static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1612 gfp_t gfp_mask)
1613{
1614 struct request *clone;
1615 struct dm_rq_target_io *tio;
1616
1617 tio = alloc_rq_tio(md, gfp_mask);
1618 if (!tio)
1619 return NULL;
1620
1621 tio->md = md;
1622 tio->ti = NULL;
1623 tio->orig = rq;
1624 tio->error = 0;
1625 memset(&tio->info, 0, sizeof(tio->info));
1626
1627 clone = &tio->clone;
1628 if (setup_clone(clone, rq, tio)) {
1629 /* -ENOMEM */
1630 free_rq_tio(tio);
1631 return NULL;
1632 }
1633
1634 return clone;
1635}
1636
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001637/*
1638 * Called with the queue lock held.
1639 */
1640static int dm_prep_fn(struct request_queue *q, struct request *rq)
1641{
1642 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001643 struct request *clone;
1644
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001645 if (unlikely(rq->special)) {
1646 DMWARN("Already has something in rq->special.");
1647 return BLKPREP_KILL;
1648 }
1649
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001650 clone = clone_rq(rq, md, GFP_ATOMIC);
1651 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001652 return BLKPREP_DEFER;
1653
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001654 rq->special = clone;
1655 rq->cmd_flags |= REQ_DONTPREP;
1656
1657 return BLKPREP_OK;
1658}
1659
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001660/*
1661 * Returns:
1662 * 0 : the request has been processed (not requeued)
1663 * !0 : the request has been requeued
1664 */
1665static int map_request(struct dm_target *ti, struct request *clone,
1666 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001667{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001668 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001669 struct dm_rq_target_io *tio = clone->end_io_data;
1670
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001671 tio->ti = ti;
1672 r = ti->type->map_rq(ti, clone, &tio->info);
1673 switch (r) {
1674 case DM_MAPIO_SUBMITTED:
1675 /* The target has taken the I/O to submit by itself later */
1676 break;
1677 case DM_MAPIO_REMAPPED:
1678 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001679 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1680 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001681 dm_dispatch_request(clone);
1682 break;
1683 case DM_MAPIO_REQUEUE:
1684 /* The target wants to requeue the I/O */
1685 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001686 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001687 break;
1688 default:
1689 if (r > 0) {
1690 DMWARN("unimplemented target map return value: %d", r);
1691 BUG();
1692 }
1693
1694 /* The target wants to complete the I/O */
1695 dm_kill_unmapped_request(clone, r);
1696 break;
1697 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001698
1699 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001700}
1701
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001702static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1703{
1704 struct request *clone;
1705
1706 blk_start_request(orig);
1707 clone = orig->special;
1708 atomic_inc(&md->pending[rq_data_dir(clone)]);
1709
1710 /*
1711 * Hold the md reference here for the in-flight I/O.
1712 * We can't rely on the reference count by device opener,
1713 * because the device may be closed during the request completion
1714 * when all bios are completed.
1715 * See the comment in rq_completed() too.
1716 */
1717 dm_get(md);
1718
1719 return clone;
1720}
1721
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001722/*
1723 * q->request_fn for request-based dm.
1724 * Called with the queue lock held.
1725 */
1726static void dm_request_fn(struct request_queue *q)
1727{
1728 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001729 int srcu_idx;
1730 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001731 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001732 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001733 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001734
1735 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001736 * For suspend, check blk_queue_stopped() and increment
1737 * ->pending within a single queue_lock not to increment the
1738 * number of in-flight I/Os after the queue is stopped in
1739 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001740 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001741 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001742 rq = blk_peek_request(q);
1743 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001744 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001745
Tejun Heo29e40132010-09-08 18:07:00 +02001746 /* always use block 0 to find the target for flushes for now */
1747 pos = 0;
1748 if (!(rq->cmd_flags & REQ_FLUSH))
1749 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001750
Tejun Heo29e40132010-09-08 18:07:00 +02001751 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001752 if (!dm_target_is_valid(ti)) {
1753 /*
1754 * Must perform setup, that dm_done() requires,
1755 * before calling dm_kill_unmapped_request
1756 */
1757 DMERR_LIMIT("request attempted access beyond the end of device");
1758 clone = dm_start_request(md, rq);
1759 dm_kill_unmapped_request(clone, -EIO);
1760 continue;
1761 }
Tejun Heo29e40132010-09-08 18:07:00 +02001762
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001763 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001764 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001765
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001766 clone = dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001767
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001768 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001769 if (map_request(ti, clone, md))
1770 goto requeued;
1771
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001772 BUG_ON(!irqs_disabled());
1773 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001774 }
1775
1776 goto out;
1777
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001778requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001779 BUG_ON(!irqs_disabled());
1780 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001781
Jens Axboe7eaceac2011-03-10 08:52:07 +01001782delay_and_out:
1783 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001784out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001785 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001786}
1787
1788int dm_underlying_device_busy(struct request_queue *q)
1789{
1790 return blk_lld_busy(q);
1791}
1792EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1793
1794static int dm_lld_busy(struct request_queue *q)
1795{
1796 int r;
1797 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001798 struct dm_table *map = dm_get_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001799
1800 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1801 r = 1;
1802 else
1803 r = dm_table_any_busy_target(map);
1804
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001805 dm_put_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001806
1807 return r;
1808}
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810static int dm_any_congested(void *congested_data, int bdi_bits)
1811{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001812 int r = bdi_bits;
1813 struct mapped_device *md = congested_data;
1814 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001816 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001817 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001818 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001819 /*
1820 * Request-based dm cares about only own queue for
1821 * the query about congestion status of request_queue
1822 */
1823 if (dm_request_based(md))
1824 r = md->queue->backing_dev_info.state &
1825 bdi_bits;
1826 else
1827 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001828 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001829 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001830 }
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return r;
1833}
1834
1835/*-----------------------------------------------------------------
1836 * An IDR is used to keep track of allocated minor numbers.
1837 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001838static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001840 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001842 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
1845/*
1846 * See if the device with a specific minor # is free.
1847 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001848static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001850 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 if (minor >= (1 << MINORBITS))
1853 return -EINVAL;
1854
Tejun Heoc9d76be2013-02-27 17:04:26 -08001855 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001856 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Tejun Heoc9d76be2013-02-27 17:04:26 -08001858 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001860 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001861 idr_preload_end();
1862 if (r < 0)
1863 return r == -ENOSPC ? -EBUSY : r;
1864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001867static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001869 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Tejun Heoc9d76be2013-02-27 17:04:26 -08001871 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001872 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Tejun Heoc9d76be2013-02-27 17:04:26 -08001874 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001876 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001877 idr_preload_end();
1878 if (r < 0)
1879 return r;
1880 *minor = r;
1881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001884static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Mikulas Patocka53d59142009-04-02 19:55:37 +01001886static void dm_wq_work(struct work_struct *work);
1887
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001888static void dm_init_md_queue(struct mapped_device *md)
1889{
1890 /*
1891 * Request-based dm devices cannot be stacked on top of bio-based dm
1892 * devices. The type of this dm device has not been decided yet.
1893 * The type is decided at the first table loading time.
1894 * To prevent problematic device stacking, clear the queue flag
1895 * for request stacking support until then.
1896 *
1897 * This queue is new, so no concurrency on the queue_flags.
1898 */
1899 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1900
1901 md->queue->queuedata = md;
1902 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1903 md->queue->backing_dev_info.congested_data = md;
1904 blk_queue_make_request(md->queue, dm_request);
1905 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001906 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1907}
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909/*
1910 * Allocate and initialise a blank device with a given minor.
1911 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001912static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001915 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001916 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 if (!md) {
1919 DMWARN("unable to allocate device, out of memory.");
1920 return NULL;
1921 }
1922
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001923 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001924 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001927 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001928 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001929 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001930 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001932 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001934 r = init_srcu_struct(&md->io_barrier);
1935 if (r < 0)
1936 goto bad_io_barrier;
1937
Mike Snitzera5664da2010-08-12 04:14:01 +01001938 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001939 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001940 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001941 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001943 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001945 atomic_set(&md->uevent_seq, 0);
1946 INIT_LIST_HEAD(&md->uevent_list);
1947 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001949 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001951 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001953 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 md->disk = alloc_disk(1);
1956 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001957 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001959 atomic_set(&md->pending[0], 0);
1960 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001961 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001962 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001963 init_waitqueue_head(&md->eventq);
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 md->disk->major = _major;
1966 md->disk->first_minor = minor;
1967 md->disk->fops = &dm_blk_dops;
1968 md->disk->queue = md->queue;
1969 md->disk->private_data = md;
1970 sprintf(md->disk->disk_name, "dm-%d", minor);
1971 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001972 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Tejun Heo670368a2013-07-30 08:40:21 -04001974 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001975 if (!md->wq)
1976 goto bad_thread;
1977
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001978 md->bdev = bdget_disk(md->disk, 0);
1979 if (!md->bdev)
1980 goto bad_bdev;
1981
Tejun Heo6a8736d2010-09-08 18:07:00 +02001982 bio_init(&md->flush_bio);
1983 md->flush_bio.bi_bdev = md->bdev;
1984 md->flush_bio.bi_rw = WRITE_FLUSH;
1985
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001986 dm_stats_init(&md->stats);
1987
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001988 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001989 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001990 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001991 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001992
1993 BUG_ON(old_md != MINOR_ALLOCED);
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return md;
1996
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001997bad_bdev:
1998 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001999bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01002000 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00002001 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002002bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05002003 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002004bad_queue:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002005 cleanup_srcu_struct(&md->io_barrier);
2006bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002008bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002009 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002010bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 kfree(md);
2012 return NULL;
2013}
2014
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002015static void unlock_fs(struct mapped_device *md);
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017static void free_dev(struct mapped_device *md)
2018{
Tejun Heof331c022008-09-03 09:01:48 +02002019 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002020
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002021 unlock_fs(md);
2022 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00002023 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002024 if (md->io_pool)
2025 mempool_destroy(md->io_pool);
2026 if (md->bs)
2027 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01002028 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 del_gendisk(md->disk);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002030 cleanup_srcu_struct(&md->io_barrier);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002031 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002032
2033 spin_lock(&_minor_lock);
2034 md->disk->private_data = NULL;
2035 spin_unlock(&_minor_lock);
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05002038 blk_cleanup_queue(md->queue);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002039 dm_stats_cleanup(&md->stats);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002040 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 kfree(md);
2042}
2043
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002044static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2045{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002046 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002047
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002048 if (md->io_pool && md->bs) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002049 /* The md already has necessary mempools. */
2050 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2051 /*
2052 * Reload bioset because front_pad may have changed
2053 * because a different table was loaded.
2054 */
2055 bioset_free(md->bs);
2056 md->bs = p->bs;
2057 p->bs = NULL;
2058 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002059 /*
2060 * There's no need to reload with request-based dm
2061 * because the size of front_pad doesn't change.
2062 * Note for future: If you are to reload bioset,
2063 * prep-ed requests in the queue may refer
2064 * to bio from the old bioset, so you must walk
2065 * through the queue to unprep.
2066 */
2067 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002068 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002069 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002070
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002071 BUG_ON(!p || md->io_pool || md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002072
2073 md->io_pool = p->io_pool;
2074 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002075 md->bs = p->bs;
2076 p->bs = NULL;
2077
2078out:
2079 /* mempool bind completed, now no need any mempools in the table */
2080 dm_table_free_md_mempools(t);
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083/*
2084 * Bind a table to the device.
2085 */
2086static void event_callback(void *context)
2087{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002088 unsigned long flags;
2089 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct mapped_device *md = (struct mapped_device *) context;
2091
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002092 spin_lock_irqsave(&md->uevent_lock, flags);
2093 list_splice_init(&md->uevent_list, &uevents);
2094 spin_unlock_irqrestore(&md->uevent_lock, flags);
2095
Tejun Heoed9e1982008-08-25 19:56:05 +09002096 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 atomic_inc(&md->event_nr);
2099 wake_up(&md->eventq);
2100}
2101
Mike Snitzerc2176492011-01-13 19:53:46 +00002102/*
2103 * Protected by md->suspend_lock obtained by dm_swap_table().
2104 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002105static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002107 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002109 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002112/*
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002113 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2114 *
2115 * If this function returns 0, then the device is either a non-dm
2116 * device without a merge_bvec_fn, or it is a dm device that is
2117 * able to split any bios it receives that are too big.
2118 */
2119int dm_queue_merge_is_compulsory(struct request_queue *q)
2120{
2121 struct mapped_device *dev_md;
2122
2123 if (!q->merge_bvec_fn)
2124 return 0;
2125
2126 if (q->make_request_fn == dm_request) {
2127 dev_md = q->queuedata;
2128 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2129 return 0;
2130 }
2131
2132 return 1;
2133}
2134
2135static int dm_device_merge_is_compulsory(struct dm_target *ti,
2136 struct dm_dev *dev, sector_t start,
2137 sector_t len, void *data)
2138{
2139 struct block_device *bdev = dev->bdev;
2140 struct request_queue *q = bdev_get_queue(bdev);
2141
2142 return dm_queue_merge_is_compulsory(q);
2143}
2144
2145/*
2146 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2147 * on the properties of the underlying devices.
2148 */
2149static int dm_table_merge_is_optional(struct dm_table *table)
2150{
2151 unsigned i = 0;
2152 struct dm_target *ti;
2153
2154 while (i < dm_table_get_num_targets(table)) {
2155 ti = dm_table_get_target(table, i++);
2156
2157 if (ti->type->iterate_devices &&
2158 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2159 return 0;
2160 }
2161
2162 return 1;
2163}
2164
2165/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002166 * Returns old map, which caller must destroy.
2167 */
2168static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2169 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002171 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002172 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 sector_t size;
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002174 int merge_is_optional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002177
2178 /*
2179 * Wipe any geometry if the size of the table changed.
2180 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002181 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002182 memset(&md->geometry, 0, sizeof(md->geometry));
2183
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002184 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002186 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002187
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002188 /*
2189 * The queue hasn't been stopped yet, if the old table type wasn't
2190 * for request-based during suspension. So stop it to prevent
2191 * I/O mapping before resume.
2192 * This must be done before setting the queue restrictions,
2193 * because request-based dm may be run just after the setting.
2194 */
2195 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2196 stop_queue(q);
2197
2198 __bind_mempools(md, t);
2199
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002200 merge_is_optional = dm_table_merge_is_optional(t);
2201
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002202 old_map = md->map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002203 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002204 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2205
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002206 dm_table_set_restrictions(t, q, limits);
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002207 if (merge_is_optional)
2208 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2209 else
2210 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002211 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002212
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002213 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Alasdair G Kergona7940152009-12-10 23:52:23 +00002216/*
2217 * Returns unbound table for the caller to free.
2218 */
2219static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 struct dm_table *map = md->map;
2222
2223 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002224 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 dm_table_event_callback(map, NULL, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002227 rcu_assign_pointer(md->map, NULL);
2228 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002229
2230 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233/*
2234 * Constructor for a new device.
2235 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002236int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 struct mapped_device *md;
2239
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002240 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (!md)
2242 return -ENXIO;
2243
Milan Broz784aae72009-01-06 03:05:12 +00002244 dm_sysfs_init(md);
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 *result = md;
2247 return 0;
2248}
2249
Mike Snitzera5664da2010-08-12 04:14:01 +01002250/*
2251 * Functions to manage md->type.
2252 * All are required to hold md->type_lock.
2253 */
2254void dm_lock_md_type(struct mapped_device *md)
2255{
2256 mutex_lock(&md->type_lock);
2257}
2258
2259void dm_unlock_md_type(struct mapped_device *md)
2260{
2261 mutex_unlock(&md->type_lock);
2262}
2263
2264void dm_set_md_type(struct mapped_device *md, unsigned type)
2265{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002266 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002267 md->type = type;
2268}
2269
2270unsigned dm_get_md_type(struct mapped_device *md)
2271{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002272 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002273 return md->type;
2274}
2275
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002276struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2277{
2278 return md->immutable_target_type;
2279}
2280
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002281/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002282 * The queue_limits are only valid as long as you have a reference
2283 * count on 'md'.
2284 */
2285struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2286{
2287 BUG_ON(!atomic_read(&md->holders));
2288 return &md->queue->limits;
2289}
2290EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2291
2292/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002293 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2294 */
2295static int dm_init_request_based_queue(struct mapped_device *md)
2296{
2297 struct request_queue *q = NULL;
2298
2299 if (md->queue->elevator)
2300 return 1;
2301
2302 /* Fully initialize the queue */
2303 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2304 if (!q)
2305 return 0;
2306
2307 md->queue = q;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002308 dm_init_md_queue(md);
2309 blk_queue_softirq_done(md->queue, dm_softirq_done);
2310 blk_queue_prep_rq(md->queue, dm_prep_fn);
2311 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002312
2313 elv_register_queue(md->queue);
2314
2315 return 1;
2316}
2317
2318/*
2319 * Setup the DM device's queue based on md's type
2320 */
2321int dm_setup_md_queue(struct mapped_device *md)
2322{
2323 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2324 !dm_init_request_based_queue(md)) {
2325 DMWARN("Cannot initialize queue for request-based mapped device");
2326 return -EINVAL;
2327 }
2328
2329 return 0;
2330}
2331
David Teigland637842c2006-01-06 00:20:00 -08002332static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 unsigned minor = MINOR(dev);
2336
2337 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2338 return NULL;
2339
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002340 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002343 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002344 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002345 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002346 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002347 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002348 goto out;
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002351out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002352 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
David Teigland637842c2006-01-06 00:20:00 -08002354 return md;
2355}
2356
David Teiglandd229a952006-01-06 00:20:01 -08002357struct mapped_device *dm_get_md(dev_t dev)
2358{
2359 struct mapped_device *md = dm_find_md(dev);
2360
2361 if (md)
2362 dm_get(md);
2363
2364 return md;
2365}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002366EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002367
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002368void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002369{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002370 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
2373void dm_set_mdptr(struct mapped_device *md, void *ptr)
2374{
2375 md->interface_ptr = ptr;
2376}
2377
2378void dm_get(struct mapped_device *md)
2379{
2380 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002381 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002384const char *dm_device_name(struct mapped_device *md)
2385{
2386 return md->name;
2387}
2388EXPORT_SYMBOL_GPL(dm_device_name);
2389
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002390static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002392 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002393 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002395 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002396
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002397 spin_lock(&_minor_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002398 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002399 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2400 set_bit(DMF_FREEING, &md->flags);
2401 spin_unlock(&_minor_lock);
2402
2403 if (!dm_suspended_md(md)) {
2404 dm_table_presuspend_targets(map);
2405 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002407
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002408 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2409 dm_put_live_table(md, srcu_idx);
2410
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002411 /*
2412 * Rare, but there may be I/O requests still going to complete,
2413 * for example. Wait for all references to disappear.
2414 * No one should increment the reference count of the mapped_device,
2415 * after the mapped_device state becomes DMF_FREEING.
2416 */
2417 if (wait)
2418 while (atomic_read(&md->holders))
2419 msleep(1);
2420 else if (atomic_read(&md->holders))
2421 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2422 dm_device_name(md), atomic_read(&md->holders));
2423
2424 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002425 dm_table_destroy(__unbind(md));
2426 free_dev(md);
2427}
2428
2429void dm_destroy(struct mapped_device *md)
2430{
2431 __dm_destroy(md, true);
2432}
2433
2434void dm_destroy_immediate(struct mapped_device *md)
2435{
2436 __dm_destroy(md, false);
2437}
2438
2439void dm_put(struct mapped_device *md)
2440{
2441 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
Edward Goggin79eb8852007-05-09 02:32:56 -07002443EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Mikulas Patocka401600d2009-04-02 19:55:38 +01002445static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002446{
2447 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002448 DECLARE_WAITQUEUE(wait, current);
2449
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002450 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002451
2452 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002453 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002454
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002455 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002456 break;
2457
Mikulas Patocka401600d2009-04-02 19:55:38 +01002458 if (interruptible == TASK_INTERRUPTIBLE &&
2459 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002460 r = -EINTR;
2461 break;
2462 }
2463
2464 io_schedule();
2465 }
2466 set_current_state(TASK_RUNNING);
2467
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002468 remove_wait_queue(&md->wait, &wait);
2469
Milan Broz46125c12008-02-08 02:10:30 +00002470 return r;
2471}
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473/*
2474 * Process the deferred bios
2475 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002476static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Mikulas Patockaef208582009-04-02 19:55:38 +01002478 struct mapped_device *md = container_of(work, struct mapped_device,
2479 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002480 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002481 int srcu_idx;
2482 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002484 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002485
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002486 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002487 spin_lock_irq(&md->deferred_lock);
2488 c = bio_list_pop(&md->deferred);
2489 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002490
Tejun Heo6a8736d2010-09-08 18:07:00 +02002491 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002492 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002493
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002494 if (dm_request_based(md))
2495 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002496 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002497 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002498 }
Milan Broz73d410c2008-02-08 02:10:25 +00002499
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002500 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002503static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002504{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002505 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2506 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002507 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002508}
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002511 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002513struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Mike Christie87eb5b22013-03-01 22:45:48 +00002515 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002516 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002517 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Daniel Walkere61290a2008-02-08 02:10:08 +00002519 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002522 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002523 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Mike Snitzer3ae70652012-09-26 23:45:45 +01002525 /*
2526 * If the new table has no data devices, retain the existing limits.
2527 * This helps multipath with queue_if_no_path if all paths disappear,
2528 * then new I/O is queued based on these limits, and then some paths
2529 * reappear.
2530 */
2531 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002532 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002533 if (live_map)
2534 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002535 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002536 }
2537
Mike Christie87eb5b22013-03-01 22:45:48 +00002538 if (!live_map) {
2539 r = dm_calculate_queue_limits(table, &limits);
2540 if (r) {
2541 map = ERR_PTR(r);
2542 goto out;
2543 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002544 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002545
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002546 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002548out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002549 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002550 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
2553/*
2554 * Functions to lock and unlock any filesystem running on the
2555 * device.
2556 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002557static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002559 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002562
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002563 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002564 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002565 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002566 md->frozen_sb = NULL;
2567 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002568 }
2569
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002570 set_bit(DMF_FROZEN, &md->flags);
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return 0;
2573}
2574
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002575static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002577 if (!test_bit(DMF_FROZEN, &md->flags))
2578 return;
2579
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002580 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002582 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
2585/*
2586 * We need to be able to change a mapping table under a mounted
2587 * filesystem. For example we might want to move some data in
2588 * the background. Before the table can be swapped with
2589 * dm_bind_table, dm_suspend must be called to flush any in
2590 * flight bios and ensure that any further io gets deferred.
2591 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002592/*
2593 * Suspend mechanism in request-based dm.
2594 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002595 * 1. Flush all I/Os by lock_fs() if needed.
2596 * 2. Stop dispatching any I/O by stopping the request_queue.
2597 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002598 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002599 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002600 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002601int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002603 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002604 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002605 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002606 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Daniel Walkere61290a2008-02-08 02:10:08 +00002608 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002609
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002610 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002611 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002612 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002615 map = md->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002617 /*
2618 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2619 * This flag is cleared before dm_suspend returns.
2620 */
2621 if (noflush)
2622 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2623
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002624 /* This does not get reverted if there's an error later. */
2625 dm_table_presuspend_targets(map);
2626
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002627 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002628 * Flush I/O to the device.
2629 * Any I/O submitted after lock_fs() may not be flushed.
2630 * noflush takes precedence over do_lockfs.
2631 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002632 */
2633 if (!noflush && do_lockfs) {
2634 r = lock_fs(md);
2635 if (r)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002636 goto out_unlock;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002640 * Here we must make sure that no processes are submitting requests
2641 * to target drivers i.e. no one may be executing
2642 * __split_and_process_bio. This is called from dm_request and
2643 * dm_wq_work.
2644 *
2645 * To get all processes out of __split_and_process_bio in dm_request,
2646 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002647 * __split_and_process_bio from dm_request and quiesce the thread
2648 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2649 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002651 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002652 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002654 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002655 * Stop md->queue before flushing md->wq in case request-based
2656 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002657 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002658 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002659 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002660
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002661 flush_workqueue(md->wq);
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002664 * At this point no more requests are entering target request routines.
2665 * We call dm_wait_for_completion to wait for all existing requests
2666 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002668 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Milan Broz6d6f10d2008-02-08 02:10:22 +00002670 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002671 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002672 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002675 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002676 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002677
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002678 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002679 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002680
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002681 unlock_fs(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002682 goto out_unlock; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002683 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002684
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002685 /*
2686 * If dm_wait_for_completion returned 0, the device is completely
2687 * quiescent now. There is no request-processing activity. All new
2688 * requests are being added to md->deferred list.
2689 */
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 set_bit(DMF_SUSPENDED, &md->flags);
2692
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002693 dm_table_postsuspend_targets(map);
2694
Alasdair G Kergond2874832006-11-08 17:44:43 -08002695out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002696 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002697 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700int dm_resume(struct mapped_device *md)
2701{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002702 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002703 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Daniel Walkere61290a2008-02-08 02:10:08 +00002705 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002706 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002707 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002708
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002709 map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002710 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002711 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Milan Broz8757b772006-10-03 01:15:36 -07002713 r = dm_table_resume_targets(map);
2714 if (r)
2715 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002716
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002717 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002718
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002719 /*
2720 * Flushing deferred I/Os must be done after targets are resumed
2721 * so that mapping of targets can work correctly.
2722 * Request-based dm is queueing the deferred I/Os in its request_queue.
2723 */
2724 if (dm_request_based(md))
2725 start_queue(md->queue);
2726
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002727 unlock_fs(md);
2728
2729 clear_bit(DMF_SUSPENDED, &md->flags);
2730
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002731 r = 0;
2732out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002733 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002734
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002735 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002738/*
2739 * Internal suspend/resume works like userspace-driven suspend. It waits
2740 * until all bios finish and prevents issuing new bios to the target drivers.
2741 * It may be used only from the kernel.
2742 *
2743 * Internal suspend holds md->suspend_lock, which prevents interaction with
2744 * userspace-driven suspend.
2745 */
2746
2747void dm_internal_suspend(struct mapped_device *md)
2748{
2749 mutex_lock(&md->suspend_lock);
2750 if (dm_suspended_md(md))
2751 return;
2752
2753 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2754 synchronize_srcu(&md->io_barrier);
2755 flush_workqueue(md->wq);
2756 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2757}
2758
2759void dm_internal_resume(struct mapped_device *md)
2760{
2761 if (dm_suspended_md(md))
2762 goto done;
2763
2764 dm_queue_flush(md);
2765
2766done:
2767 mutex_unlock(&md->suspend_lock);
2768}
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770/*-----------------------------------------------------------------
2771 * Event notification.
2772 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002773int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002774 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002775{
Milan Broz60935eb2009-06-22 10:12:30 +01002776 char udev_cookie[DM_COOKIE_LENGTH];
2777 char *envp[] = { udev_cookie, NULL };
2778
2779 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002780 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002781 else {
2782 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2783 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002784 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2785 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002786 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002787}
2788
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002789uint32_t dm_next_uevent_seq(struct mapped_device *md)
2790{
2791 return atomic_add_return(1, &md->uevent_seq);
2792}
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794uint32_t dm_get_event_nr(struct mapped_device *md)
2795{
2796 return atomic_read(&md->event_nr);
2797}
2798
2799int dm_wait_event(struct mapped_device *md, int event_nr)
2800{
2801 return wait_event_interruptible(md->eventq,
2802 (event_nr != atomic_read(&md->event_nr)));
2803}
2804
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002805void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2806{
2807 unsigned long flags;
2808
2809 spin_lock_irqsave(&md->uevent_lock, flags);
2810 list_add(elist, &md->uevent_list);
2811 spin_unlock_irqrestore(&md->uevent_lock, flags);
2812}
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814/*
2815 * The gendisk is only valid as long as you have a reference
2816 * count on 'md'.
2817 */
2818struct gendisk *dm_disk(struct mapped_device *md)
2819{
2820 return md->disk;
2821}
2822
Milan Broz784aae72009-01-06 03:05:12 +00002823struct kobject *dm_kobject(struct mapped_device *md)
2824{
2825 return &md->kobj;
2826}
2827
2828/*
2829 * struct mapped_device should not be exported outside of dm.c
2830 * so use this check to verify that kobj is part of md structure
2831 */
2832struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2833{
2834 struct mapped_device *md;
2835
2836 md = container_of(kobj, struct mapped_device, kobj);
2837 if (&md->kobj != kobj)
2838 return NULL;
2839
Milan Broz4d89b7b2009-06-22 10:12:11 +01002840 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002841 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002842 return NULL;
2843
Milan Broz784aae72009-01-06 03:05:12 +00002844 dm_get(md);
2845 return md;
2846}
2847
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002848int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
2850 return test_bit(DMF_SUSPENDED, &md->flags);
2851}
2852
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002853int dm_suspended(struct dm_target *ti)
2854{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002855 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002856}
2857EXPORT_SYMBOL_GPL(dm_suspended);
2858
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002859int dm_noflush_suspending(struct dm_target *ti)
2860{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002861 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002862}
2863EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2864
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002865struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002866{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002867 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
2868 struct kmem_cache *cachep;
2869 unsigned int pool_size;
2870 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002871
2872 if (!pools)
2873 return NULL;
2874
Jun'ichi Nomura23e50832013-03-01 22:45:48 +00002875 if (type == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002876 cachep = _io_cache;
Mike Snitzer6cfa5852013-09-12 18:06:11 -04002877 pool_size = RESERVED_BIO_BASED_IOS;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002878 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2879 } else if (type == DM_TYPE_REQUEST_BASED) {
2880 cachep = _rq_tio_cache;
Mike Snitzer6cfa5852013-09-12 18:06:11 -04002881 pool_size = RESERVED_REQUEST_BASED_IOS;
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002882 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2883 /* per_bio_data_size is not used. See __bind_mempools(). */
2884 WARN_ON(per_bio_data_size != 0);
2885 } else
2886 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002887
Mike Snitzer6cfa5852013-09-12 18:06:11 -04002888 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002889 if (!pools->io_pool)
2890 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002891
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002892 pools->bs = bioset_create(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002893 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002894 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002895
Martin K. Petersena91a2782011-03-17 11:11:05 +01002896 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002897 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002898
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002899 return pools;
2900
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002901out:
2902 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002903
2904 return NULL;
2905}
2906
2907void dm_free_md_mempools(struct dm_md_mempools *pools)
2908{
2909 if (!pools)
2910 return;
2911
2912 if (pools->io_pool)
2913 mempool_destroy(pools->io_pool);
2914
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002915 if (pools->bs)
2916 bioset_free(pools->bs);
2917
2918 kfree(pools);
2919}
2920
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002921static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 .open = dm_blk_open,
2923 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002924 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002925 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 .owner = THIS_MODULE
2927};
2928
2929EXPORT_SYMBOL(dm_get_mapinfo);
2930
2931/*
2932 * module hooks
2933 */
2934module_init(dm_init);
2935module_exit(dm_exit);
2936
2937module_param(major, uint, 0);
2938MODULE_PARM_DESC(major, "The major number of the device mapper");
2939MODULE_DESCRIPTION(DM_NAME " driver");
2940MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2941MODULE_LICENSE("GPL");