blob: 33f20103d8d566d4a2dbb556acd22fcbd7b628f7 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000066 * For request-based dm.
67 * One of these is allocated per request.
68 */
69struct dm_rq_target_io {
70 struct mapped_device *md;
71 struct dm_target *ti;
72 struct request *orig, clone;
73 int error;
74 union map_info info;
75};
76
77/*
Kent Overstreet94818742012-09-07 13:44:01 -070078 * For request-based dm - the bio clones we allocate are embedded in these
79 * structs.
80 *
81 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
82 * the bioset is created - this means the bio has to come at the end of the
83 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000084 */
85struct dm_rq_clone_bio_info {
86 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010087 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -070088 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000089};
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091union map_info *dm_get_mapinfo(struct bio *bio)
92{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070093 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010094 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070095 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010098union map_info *dm_get_rq_mapinfo(struct request *rq)
99{
100 if (rq && rq->end_io_data)
101 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
102 return NULL;
103}
104EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
105
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700106#define MINOR_ALLOCED ((void *)-1)
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * Bits for the md->flags field.
110 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100111#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800113#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700114#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700115#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800116#define DMF_NOFLUSH_SUSPENDING 5
Mikulas Patockad5b9dd02011-08-02 12:32:04 +0100117#define DMF_MERGE_IS_OPTIONAL 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Milan Broz304f3f62008-02-08 02:11:17 +0000119/*
120 * Work processed by per-device workqueue.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700123 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000124 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 rwlock_t map_lock;
126 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700127 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 unsigned long flags;
130
Jens Axboe165125e2007-07-24 09:28:11 +0200131 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100132 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100133 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100134 struct mutex type_lock;
135
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000136 struct target_type *immutable_target_type;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800139 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 void *interface_ptr;
142
143 /*
144 * A list of ios that arrived while we were suspended.
145 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200146 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100148 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800149 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100150 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200153 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000154 */
155 struct workqueue_struct *wq;
156
157 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * The current mapping.
159 */
160 struct dm_table *map;
161
162 /*
163 * io objects are allocated from here.
164 */
165 mempool_t *io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Stefan Bader9faf4002006-10-03 01:15:41 -0700167 struct bio_set *bs;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
170 * Event handling.
171 */
172 atomic_t event_nr;
173 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100174 atomic_t uevent_seq;
175 struct list_head uevent_list;
176 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /*
179 * freeze/thaw support require holding onto a super block
180 */
181 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100182 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800183
184 /* forced geometry settings */
185 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000186
187 /* sysfs handle */
188 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100189
Tejun Heod87f4c12010-09-03 11:56:19 +0200190 /* zero-length flush that will be cloned and submitted to targets */
191 struct bio flush_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100194/*
195 * For mempools pre-allocation at the table loading time.
196 */
197struct dm_md_mempools {
198 mempool_t *io_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100199 struct bio_set *bs;
200};
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800203static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000204static struct kmem_cache *_rq_tio_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static int __init local_init(void)
207{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100208 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100211 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100213 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000215 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
216 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100217 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000218
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100219 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100220 if (r)
Jun'ichi Nomura23e50832013-03-01 22:45:48 +0000221 goto out_free_rq_tio_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 _major = major;
224 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100225 if (r < 0)
226 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (!_major)
229 _major = r;
230
231 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100232
233out_uevent_exit:
234 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000235out_free_rq_tio_cache:
236 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100237out_free_io_cache:
238 kmem_cache_destroy(_io_cache);
239
240 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243static void local_exit(void)
244{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000245 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700247 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100248 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 _major = 0;
251
252 DMINFO("cleaned up");
253}
254
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000255static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 local_init,
257 dm_target_init,
258 dm_linear_init,
259 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000260 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100261 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dm_interface_init,
263};
264
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000265static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 local_exit,
267 dm_target_exit,
268 dm_linear_exit,
269 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000270 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100271 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dm_interface_exit,
273};
274
275static int __init dm_init(void)
276{
277 const int count = ARRAY_SIZE(_inits);
278
279 int r, i;
280
281 for (i = 0; i < count; i++) {
282 r = _inits[i]();
283 if (r)
284 goto bad;
285 }
286
287 return 0;
288
289 bad:
290 while (i--)
291 _exits[i]();
292
293 return r;
294}
295
296static void __exit dm_exit(void)
297{
298 int i = ARRAY_SIZE(_exits);
299
300 while (i--)
301 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100302
303 /*
304 * Should be empty by this point.
305 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100306 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
310 * Block device functions
311 */
Mike Anderson432a2122009-12-10 23:52:20 +0000312int dm_deleting_md(struct mapped_device *md)
313{
314 return test_bit(DMF_DELETING, &md->flags);
315}
316
Al Virofe5f9f22008-03-02 10:29:31 -0500317static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct mapped_device *md;
320
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700321 spin_lock(&_minor_lock);
322
Al Virofe5f9f22008-03-02 10:29:31 -0500323 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700324 if (!md)
325 goto out;
326
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700327 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000328 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700329 md = NULL;
330 goto out;
331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700334 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700335
336out:
337 spin_unlock(&_minor_lock);
338
339 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Al Virodb2a1442013-05-05 21:52:57 -0400342static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Al Virofe5f9f22008-03-02 10:29:31 -0500344 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200345
Milan Broz4a1aeb92011-01-13 19:59:48 +0000346 spin_lock(&_minor_lock);
347
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700348 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000350
351 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700354int dm_open_count(struct mapped_device *md)
355{
356 return atomic_read(&md->open_count);
357}
358
359/*
360 * Guarantees nothing is using the device before it's deleted.
361 */
362int dm_lock_for_deletion(struct mapped_device *md)
363{
364 int r = 0;
365
366 spin_lock(&_minor_lock);
367
368 if (dm_open_count(md))
369 r = -EBUSY;
370 else
371 set_bit(DMF_DELETING, &md->flags);
372
373 spin_unlock(&_minor_lock);
374
375 return r;
376}
377
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800378static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
379{
380 struct mapped_device *md = bdev->bd_disk->private_data;
381
382 return dm_get_geometry(md, geo);
383}
384
Al Virofe5f9f22008-03-02 10:29:31 -0500385static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700386 unsigned int cmd, unsigned long arg)
387{
Al Virofe5f9f22008-03-02 10:29:31 -0500388 struct mapped_device *md = bdev->bd_disk->private_data;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100389 struct dm_table *map;
Milan Brozaa129a22006-10-03 01:15:15 -0700390 struct dm_target *tgt;
391 int r = -ENOTTY;
392
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100393retry:
394 map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700395 if (!map || !dm_table_get_size(map))
396 goto out;
397
398 /* We only support devices that have a single target */
399 if (dm_table_get_num_targets(map) != 1)
400 goto out;
401
402 tgt = dm_table_get_target(map, 0);
403
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000404 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700405 r = -EAGAIN;
406 goto out;
407 }
408
409 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400410 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700411
412out:
413 dm_table_put(map);
414
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100415 if (r == -ENOTCONN) {
416 msleep(10);
417 goto retry;
418 }
419
Milan Brozaa129a22006-10-03 01:15:15 -0700420 return r;
421}
422
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100423static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 return mempool_alloc(md->io_pool, GFP_NOIO);
426}
427
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100428static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 mempool_free(io, md->io_pool);
431}
432
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100433static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Mikulas Patockadba14162012-10-12 21:02:15 +0100435 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000438static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
439 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100440{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000441 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100442}
443
444static void free_rq_tio(struct dm_rq_target_io *tio)
445{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000446 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100447}
448
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000449static int md_in_flight(struct mapped_device *md)
450{
451 return atomic_read(&md->pending[READ]) +
452 atomic_read(&md->pending[WRITE]);
453}
454
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800455static void start_io_acct(struct dm_io *io)
456{
457 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900458 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200459 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800460
461 io->start_time = jiffies;
462
Tejun Heo074a7ac2008-08-25 19:56:14 +0900463 cpu = part_stat_lock();
464 part_round_stats(cpu, &dm_disk(md)->part0);
465 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100466 atomic_set(&dm_disk(md)->part0.in_flight[rw],
467 atomic_inc_return(&md->pending[rw]));
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800468}
469
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000470static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800471{
472 struct mapped_device *md = io->md;
473 struct bio *bio = io->bio;
474 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900475 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800476 int rw = bio_data_dir(bio);
477
Tejun Heo074a7ac2008-08-25 19:56:14 +0900478 cpu = part_stat_lock();
479 part_round_stats(cpu, &dm_disk(md)->part0);
480 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
481 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800482
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100483 /*
484 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200485 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100486 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100487 pending = atomic_dec_return(&md->pending[rw]);
488 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200489 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800490
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000491 /* nudge anyone waiting on suspend queue */
492 if (!pending)
493 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
497 * Add the bio to the list of deferred io.
498 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100499static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200501 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200503 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200505 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200506 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509/*
510 * Everyone (including functions in this file), should use this
511 * function to access the md->map field, and make sure they call
512 * dm_table_put() when finished.
513 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000514struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100517 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100519 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 t = md->map;
521 if (t)
522 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100523 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 return t;
526}
527
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800528/*
529 * Get the geometry associated with a dm device
530 */
531int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
532{
533 *geo = md->geometry;
534
535 return 0;
536}
537
538/*
539 * Set the geometry of a device.
540 */
541int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
542{
543 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
544
545 if (geo->start > sz) {
546 DMWARN("Start sector is beyond the geometry limits.");
547 return -EINVAL;
548 }
549
550 md->geometry = *geo;
551
552 return 0;
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/*-----------------------------------------------------------------
556 * CRUD START:
557 * A more elegant soln is in the works that uses the queue
558 * merge fn, unfortunately there are a couple of changes to
559 * the block layer that I want to make for this. So in the
560 * interests of getting something for people to use I give
561 * you this clearly demarcated crap.
562 *---------------------------------------------------------------*/
563
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800564static int __noflush_suspending(struct mapped_device *md)
565{
566 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/*
570 * Decrements the number of outstanding ios that a bio has been
571 * cloned into, completing the original io if necc.
572 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800573static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800575 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000576 int io_error;
577 struct bio *bio;
578 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800579
580 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100581 if (unlikely(error)) {
582 spin_lock_irqsave(&io->endio_lock, flags);
583 if (!(io->error > 0 && __noflush_suspending(md)))
584 io->error = error;
585 spin_unlock_irqrestore(&io->endio_lock, flags);
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800589 if (io->error == DM_ENDIO_REQUEUE) {
590 /*
591 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800592 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100593 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200594 if (__noflush_suspending(md))
595 bio_list_add_head(&md->deferred, io->bio);
596 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800597 /* noflush suspend was interrupted. */
598 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100599 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800600 }
601
Milan Brozb35f8ca2009-03-16 17:44:36 +0000602 io_error = io->error;
603 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200604 end_io_acct(io);
605 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100606
Tejun Heo6a8736d2010-09-08 18:07:00 +0200607 if (io_error == DM_ENDIO_REQUEUE)
608 return;
609
Mike Snitzerb372d362010-09-08 18:07:01 +0200610 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100611 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200612 * Preflush done for flush with data, reissue
613 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100614 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200615 bio->bi_rw &= ~REQ_FLUSH;
616 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100617 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200618 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700619 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200620 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623}
624
NeilBrown6712ecf2007-09-27 12:47:43 +0200625static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100628 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000629 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700630 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 dm_endio_fn endio = tio->ti->type->end_io;
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
634 error = -EIO;
635
636 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000637 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800638 if (r < 0 || r == DM_ENDIO_REQUEUE)
639 /*
640 * error and requeue request are handled
641 * in dec_pending().
642 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800644 else if (r == DM_ENDIO_INCOMPLETE)
645 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200646 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800647 else if (r) {
648 DMWARN("unimplemented target endio return value: %d", r);
649 BUG();
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Stefan Bader9faf4002006-10-03 01:15:41 -0700653 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000654 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100657/*
658 * Partial completion handling for request-based dm
659 */
660static void end_clone_bio(struct bio *clone, int error)
661{
662 struct dm_rq_clone_bio_info *info = clone->bi_private;
663 struct dm_rq_target_io *tio = info->tio;
664 struct bio *bio = info->orig;
665 unsigned int nr_bytes = info->orig->bi_size;
666
667 bio_put(clone);
668
669 if (tio->error)
670 /*
671 * An error has already been detected on the request.
672 * Once error occurred, just let clone->end_io() handle
673 * the remainder.
674 */
675 return;
676 else if (error) {
677 /*
678 * Don't notice the error to the upper layer yet.
679 * The error handling decision is made by the target driver,
680 * when the request is completed.
681 */
682 tio->error = error;
683 return;
684 }
685
686 /*
687 * I/O for the bio successfully completed.
688 * Notice the data completion to the upper layer.
689 */
690
691 /*
692 * bios are processed from the head of the list.
693 * So the completing bio should always be rq->bio.
694 * If it's not, something wrong is happening.
695 */
696 if (tio->orig->bio != bio)
697 DMERR("bio completion is going in the middle of the request");
698
699 /*
700 * Update the original request.
701 * Do not use blk_end_request() here, because it may complete
702 * the original request before the clone, and break the ordering.
703 */
704 blk_update_request(tio->orig, 0, nr_bytes);
705}
706
707/*
708 * Don't touch any member of the md after calling this function because
709 * the md may be freed in dm_put() at the end of this function.
710 * Or do dm_get() before calling this function and dm_put() later.
711 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000712static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100713{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000714 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100715
716 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000717 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100718 wake_up(&md->wait);
719
Jens Axboea8c32a52012-11-06 12:24:26 +0100720 /*
721 * Run this off this callpath, as drivers could invoke end_io while
722 * inside their request_fn (and holding the queue lock). Calling
723 * back into ->request_fn() could deadlock attempting to grab the
724 * queue lock again.
725 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100726 if (run_queue)
Jens Axboea8c32a52012-11-06 12:24:26 +0100727 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100728
729 /*
730 * dm_put() must be at the end of this function. See the comment above
731 */
732 dm_put(md);
733}
734
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100735static void free_rq_clone(struct request *clone)
736{
737 struct dm_rq_target_io *tio = clone->end_io_data;
738
739 blk_rq_unprep_clone(clone);
740 free_rq_tio(tio);
741}
742
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000743/*
744 * Complete the clone and the original request.
745 * Must be called without queue lock.
746 */
747static void dm_end_request(struct request *clone, int error)
748{
749 int rw = rq_data_dir(clone);
750 struct dm_rq_target_io *tio = clone->end_io_data;
751 struct mapped_device *md = tio->md;
752 struct request *rq = tio->orig;
753
Tejun Heo29e40132010-09-08 18:07:00 +0200754 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000755 rq->errors = clone->errors;
756 rq->resid_len = clone->resid_len;
757
758 if (rq->sense)
759 /*
760 * We are using the sense buffer of the original
761 * request.
762 * So setting the length of the sense data is enough.
763 */
764 rq->sense_len = clone->sense_len;
765 }
766
767 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200768 blk_end_request_all(rq, error);
769 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000770}
771
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100772static void dm_unprep_request(struct request *rq)
773{
774 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100775
776 rq->special = NULL;
777 rq->cmd_flags &= ~REQ_DONTPREP;
778
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100779 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100780}
781
782/*
783 * Requeue the original request of a clone.
784 */
785void dm_requeue_unmapped_request(struct request *clone)
786{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000787 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100788 struct dm_rq_target_io *tio = clone->end_io_data;
789 struct mapped_device *md = tio->md;
790 struct request *rq = tio->orig;
791 struct request_queue *q = rq->q;
792 unsigned long flags;
793
794 dm_unprep_request(rq);
795
796 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100797 blk_requeue_request(q, rq);
798 spin_unlock_irqrestore(q->queue_lock, flags);
799
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000800 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100801}
802EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
803
804static void __stop_queue(struct request_queue *q)
805{
806 blk_stop_queue(q);
807}
808
809static void stop_queue(struct request_queue *q)
810{
811 unsigned long flags;
812
813 spin_lock_irqsave(q->queue_lock, flags);
814 __stop_queue(q);
815 spin_unlock_irqrestore(q->queue_lock, flags);
816}
817
818static void __start_queue(struct request_queue *q)
819{
820 if (blk_queue_stopped(q))
821 blk_start_queue(q);
822}
823
824static void start_queue(struct request_queue *q)
825{
826 unsigned long flags;
827
828 spin_lock_irqsave(q->queue_lock, flags);
829 __start_queue(q);
830 spin_unlock_irqrestore(q->queue_lock, flags);
831}
832
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000833static void dm_done(struct request *clone, int error, bool mapped)
834{
835 int r = error;
836 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100837 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000838
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100839 if (tio->ti) {
840 rq_end_io = tio->ti->type->rq_end_io;
841
842 if (mapped && rq_end_io)
843 r = rq_end_io(tio->ti, clone, error, &tio->info);
844 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000845
846 if (r <= 0)
847 /* The target wants to complete the I/O */
848 dm_end_request(clone, r);
849 else if (r == DM_ENDIO_INCOMPLETE)
850 /* The target will handle the I/O */
851 return;
852 else if (r == DM_ENDIO_REQUEUE)
853 /* The target wants to requeue the I/O */
854 dm_requeue_unmapped_request(clone);
855 else {
856 DMWARN("unimplemented target endio return value: %d", r);
857 BUG();
858 }
859}
860
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100861/*
862 * Request completion handler for request-based dm
863 */
864static void dm_softirq_done(struct request *rq)
865{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000866 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100867 struct request *clone = rq->completion_data;
868 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100869
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000870 if (rq->cmd_flags & REQ_FAILED)
871 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100872
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000873 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100874}
875
876/*
877 * Complete the clone and the original request with the error status
878 * through softirq context.
879 */
880static void dm_complete_request(struct request *clone, int error)
881{
882 struct dm_rq_target_io *tio = clone->end_io_data;
883 struct request *rq = tio->orig;
884
885 tio->error = error;
886 rq->completion_data = clone;
887 blk_complete_request(rq);
888}
889
890/*
891 * Complete the not-mapped clone and the original request with the error status
892 * through softirq context.
893 * Target's rq_end_io() function isn't called.
894 * This may be used when the target's map_rq() function fails.
895 */
896void dm_kill_unmapped_request(struct request *clone, int error)
897{
898 struct dm_rq_target_io *tio = clone->end_io_data;
899 struct request *rq = tio->orig;
900
901 rq->cmd_flags |= REQ_FAILED;
902 dm_complete_request(clone, error);
903}
904EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
905
906/*
907 * Called with the queue lock held
908 */
909static void end_clone_request(struct request *clone, int error)
910{
911 /*
912 * For just cleaning up the information of the queue in which
913 * the clone was dispatched.
914 * The clone is *NOT* freed actually here because it is alloced from
915 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
916 */
917 __blk_put_request(clone->q, clone);
918
919 /*
920 * Actual request completion is done in a softirq context which doesn't
921 * hold the queue lock. Otherwise, deadlock could occur because:
922 * - another request may be submitted by the upper level driver
923 * of the stacking during the completion
924 * - the submission which requires queue lock may be done
925 * against this queue
926 */
927 dm_complete_request(clone, error);
928}
929
Mike Snitzer56a67df2010-08-12 04:14:10 +0100930/*
931 * Return maximum size of I/O possible at the supplied sector up to the current
932 * target boundary.
933 */
934static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100936 sector_t target_offset = dm_target_offset(ti, sector);
937
938 return ti->len - target_offset;
939}
940
941static sector_t max_io_len(sector_t sector, struct dm_target *ti)
942{
943 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +0100944 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 /*
Mike Snitzer542f9032012-07-27 15:08:00 +0100947 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100949 if (ti->max_io_len) {
950 offset = dm_target_offset(ti, sector);
951 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
952 max_len = sector_div(offset, ti->max_io_len);
953 else
954 max_len = offset & (ti->max_io_len - 1);
955 max_len = ti->max_io_len - max_len;
956
957 if (len > max_len)
958 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
961 return len;
962}
963
Mike Snitzer542f9032012-07-27 15:08:00 +0100964int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
965{
966 if (len > UINT_MAX) {
967 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
968 (unsigned long long)len, UINT_MAX);
969 ti->error = "Maximum size of target IO is too large";
970 return -EINVAL;
971 }
972
973 ti->max_io_len = (uint32_t) len;
974
975 return 0;
976}
977EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
978
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +0000979static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100982 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700983 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +0100984 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +0000985 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 clone->bi_end_io = clone_endio;
988 clone->bi_private = tio;
989
990 /*
991 * Map the clone. If r == 0 we don't need to do
992 * anything, the target has assumed ownership of
993 * this io.
994 */
995 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100996 sector = clone->bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000997 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800998 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001000
Mike Snitzerd07335e2010-11-16 12:52:38 +01001001 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1002 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001005 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1006 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001007 md = tio->io->md;
1008 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001009 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001010 } else if (r) {
1011 DMWARN("unimplemented target map return value: %d", r);
1012 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014}
1015
1016struct clone_info {
1017 struct mapped_device *md;
1018 struct dm_table *map;
1019 struct bio *bio;
1020 struct dm_io *io;
1021 sector_t sector;
1022 sector_t sector_count;
1023 unsigned short idx;
1024};
1025
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001026static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1027{
1028 bio->bi_sector = sector;
1029 bio->bi_size = to_bytes(len);
1030}
1031
1032static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1033{
1034 bio->bi_idx = idx;
1035 bio->bi_vcnt = idx + bv_count;
1036 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1037}
1038
1039static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1040 unsigned short idx, unsigned len, unsigned offset,
1041 unsigned trim)
1042{
1043 if (!bio_integrity(bio))
1044 return;
1045
1046 bio_integrity_clone(clone, bio, GFP_NOIO);
1047
1048 if (trim)
1049 bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001053 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001055static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
1056 sector_t sector, unsigned short idx,
1057 unsigned offset, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Mikulas Patockadba14162012-10-12 21:02:15 +01001059 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 struct bio_vec *bv = bio->bi_io_vec + idx;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 *clone->bi_io_vec = *bv;
1063
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001064 bio_setup_sector(clone, sector, len);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001067 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 clone->bi_vcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 clone->bi_io_vec->bv_offset = offset;
1070 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001071 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001073 clone_bio_integrity(bio, clone, idx, len, offset, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076/*
1077 * Creates a bio that consists of range of complete bvecs.
1078 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001079static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1080 sector_t sector, unsigned short idx,
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001081 unsigned short bv_count, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Mikulas Patockadba14162012-10-12 21:02:15 +01001083 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001084 unsigned trim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Stefan Bader9faf4002006-10-03 01:15:41 -07001086 __bio_clone(clone, bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001087 bio_setup_sector(clone, sector, len);
1088 bio_setup_bv(clone, idx, bv_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001090 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1091 trim = 1;
1092 clone_bio_integrity(bio, clone, idx, len, 0, trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001095static struct dm_target_io *alloc_tio(struct clone_info *ci,
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001096 struct dm_target *ti, int nr_iovecs,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001097 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001098{
Mikulas Patockadba14162012-10-12 21:02:15 +01001099 struct dm_target_io *tio;
1100 struct bio *clone;
1101
1102 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1103 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001104
1105 tio->io = ci->io;
1106 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001107 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001108 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001109
1110 return tio;
1111}
1112
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001113static void __clone_and_map_simple_bio(struct clone_info *ci,
1114 struct dm_target *ti,
1115 unsigned target_bio_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001116{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001117 struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001118 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001119
Mike Snitzer06a426c2010-08-12 04:14:09 +01001120 /*
1121 * Discard requests require the bio's inline iovecs be initialized.
1122 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1123 * and discard, so no need for concern about wasted bvec allocations.
1124 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001125 __bio_clone(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001126 if (len)
1127 bio_setup_sector(clone, ci->sector, len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001128
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001129 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001130}
1131
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001132static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1133 unsigned num_bios, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001134{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001135 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001136
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001137 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001138 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001139}
1140
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001141static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001142{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001143 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001144 struct dm_target *ti;
1145
Mike Snitzerb372d362010-09-08 18:07:01 +02001146 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001147 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001148 __send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001149
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001150 return 0;
1151}
1152
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001153static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1154 sector_t sector, int nr_iovecs,
1155 unsigned short idx, unsigned short bv_count,
1156 unsigned offset, unsigned len,
1157 unsigned split_bvec)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001158{
Mikulas Patockadba14162012-10-12 21:02:15 +01001159 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001160 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001161 unsigned target_bio_nr;
1162 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001163
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001164 /*
1165 * Does the target want to receive duplicate copies of the bio?
1166 */
1167 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1168 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001169
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001170 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1171 tio = alloc_tio(ci, ti, nr_iovecs, target_bio_nr);
1172 if (split_bvec)
1173 clone_split_bio(tio, bio, sector, idx, offset, len);
1174 else
1175 clone_bio(tio, bio, sector, idx, bv_count, len);
1176 __map_bio(tio);
1177 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001178}
1179
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001180typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001181
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001182static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001183{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001184 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001185}
1186
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001187static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001188{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001189 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001190}
1191
1192typedef bool (*is_split_required_fn)(struct dm_target *ti);
1193
1194static bool is_split_required_for_discard(struct dm_target *ti)
1195{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001196 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001197}
1198
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001199static int __send_changing_extent_only(struct clone_info *ci,
1200 get_num_bios_fn get_num_bios,
1201 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001202{
1203 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001204 sector_t len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001205 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001206
Mike Snitzera79245b2010-08-12 04:14:24 +01001207 do {
1208 ti = dm_table_find_target(ci->map, ci->sector);
1209 if (!dm_target_is_valid(ti))
1210 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001211
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001212 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001213 * Even though the device advertised support for this type of
1214 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001215 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001216 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001217 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001218 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1219 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001220 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001221
Mike Snitzer23508a92012-12-21 20:23:37 +00001222 if (is_split_required && !is_split_required(ti))
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001223 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1224 else
1225 len = min(ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001226
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001227 __send_duplicate_bios(ci, ti, num_bios, len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001228
1229 ci->sector += len;
1230 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001231
1232 return 0;
1233}
1234
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001235static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001236{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001237 return __send_changing_extent_only(ci, get_num_discard_bios,
1238 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001239}
1240
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001241static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001242{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001243 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001244}
1245
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001246/*
1247 * Find maximum number of sectors / bvecs we can process with a single bio.
1248 */
1249static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
1250{
1251 struct bio *bio = ci->bio;
1252 sector_t bv_len, total_len = 0;
1253
1254 for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1255 bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
1256
1257 if (bv_len > max)
1258 break;
1259
1260 max -= bv_len;
1261 total_len += bv_len;
1262 }
1263
1264 return total_len;
1265}
1266
1267static int __split_bvec_across_targets(struct clone_info *ci,
1268 struct dm_target *ti, sector_t max)
1269{
1270 struct bio *bio = ci->bio;
1271 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1272 sector_t remaining = to_sector(bv->bv_len);
1273 unsigned offset = 0;
1274 sector_t len;
1275
1276 do {
1277 if (offset) {
1278 ti = dm_table_find_target(ci->map, ci->sector);
1279 if (!dm_target_is_valid(ti))
1280 return -EIO;
1281
1282 max = max_io_len(ci->sector, ti);
1283 }
1284
1285 len = min(remaining, max);
1286
1287 __clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1288 bv->bv_offset + offset, len, 1);
1289
1290 ci->sector += len;
1291 ci->sector_count -= len;
1292 offset += to_bytes(len);
1293 } while (remaining -= len);
1294
1295 ci->idx++;
1296
1297 return 0;
1298}
1299
1300/*
1301 * Select the correct strategy for processing a non-flush bio.
1302 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001303static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Mikulas Patockadba14162012-10-12 21:02:15 +01001305 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001306 struct dm_target *ti;
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001307 sector_t len, max;
1308 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001310 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001311 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001312 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001313 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001314
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001315 ti = dm_table_find_target(ci->map, ci->sector);
1316 if (!dm_target_is_valid(ti))
1317 return -EIO;
1318
Mike Snitzer56a67df2010-08-12 04:14:10 +01001319 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001320
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001321 /*
1322 * Optimise for the simple case where we can do all of
1323 * the remaining io with a single clone.
1324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (ci->sector_count <= max) {
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001326 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1327 ci->idx, bio->bi_vcnt - ci->idx, 0,
1328 ci->sector_count, 0);
1329 ci->sector_count = 0;
1330 return 0;
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001333 /*
1334 * There are some bvecs that don't span targets.
1335 * Do as many of these as possible.
1336 */
1337 if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1338 len = __len_within_target(ci, max, &idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001340 __clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1341 ci->idx, idx - ci->idx, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 ci->sector += len;
1344 ci->sector_count -= len;
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001345 ci->idx = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001349
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001350 /*
1351 * Handle a bvec that must be split between two or more targets.
1352 */
1353 return __split_bvec_across_targets(ci, ti, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001357 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001359static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001362 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001364 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001365 if (unlikely(!ci.map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001366 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001367 return;
1368 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ci.io = alloc_io(md);
1372 ci.io->error = 0;
1373 atomic_set(&ci.io->io_count, 1);
1374 ci.io->bio = bio;
1375 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001376 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 ci.sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 ci.idx = bio->bi_idx;
1379
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001380 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001381
Mike Snitzerb372d362010-09-08 18:07:01 +02001382 if (bio->bi_rw & REQ_FLUSH) {
1383 ci.bio = &ci.md->flush_bio;
1384 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001385 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001386 /* dec_pending submits any data associated with flush */
1387 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001388 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001389 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001390 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001391 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001395 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 dm_table_put(ci.map);
1397}
1398/*-----------------------------------------------------------------
1399 * CRUD END
1400 *---------------------------------------------------------------*/
1401
Milan Brozf6fccb12008-07-21 12:00:37 +01001402static int dm_merge_bvec(struct request_queue *q,
1403 struct bvec_merge_data *bvm,
1404 struct bio_vec *biovec)
1405{
1406 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001407 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001408 struct dm_target *ti;
1409 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001410 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001411
1412 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001413 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001414
1415 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001416 if (!dm_target_is_valid(ti))
1417 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001418
1419 /*
1420 * Find maximum amount of I/O that won't need splitting
1421 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001422 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001423 (sector_t) BIO_MAX_SECTORS);
1424 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1425 if (max_size < 0)
1426 max_size = 0;
1427
1428 /*
1429 * merge_bvec_fn() returns number of bytes
1430 * it can accept at this offset
1431 * max is precomputed maximal io size
1432 */
1433 if (max_size && ti->type->merge)
1434 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001435 /*
1436 * If the target doesn't support merge method and some of the devices
1437 * provided their merge_bvec method (we know this by looking at
1438 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1439 * entries. So always set max_size to 0, and the code below allows
1440 * just one page.
1441 */
1442 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1443
1444 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001445
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001446out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001447 dm_table_put(map);
1448
1449out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001450 /*
1451 * Always allow an entire first page
1452 */
1453 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1454 max_size = biovec->bv_len;
1455
Milan Brozf6fccb12008-07-21 12:00:37 +01001456 return max_size;
1457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459/*
1460 * The request function that just remaps the bio built up by
1461 * dm_merge_bvec.
1462 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001463static void _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Kevin Corry12f03a42006-02-01 03:04:52 -08001465 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001467 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001469 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Tejun Heo074a7ac2008-08-25 19:56:14 +09001471 cpu = part_stat_lock();
1472 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1473 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1474 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001475
Tejun Heo6a8736d2010-09-08 18:07:00 +02001476 /* if we're suspended, we have to queue this io for later */
1477 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001478 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Tejun Heo6a8736d2010-09-08 18:07:00 +02001480 if (bio_rw(bio) != READA)
1481 queue_io(md, bio);
1482 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001483 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001487 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001488 up_read(&md->io_lock);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001489 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001490}
1491
1492static int dm_request_based(struct mapped_device *md)
1493{
1494 return blk_queue_stackable(md->queue);
1495}
1496
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001497static void dm_request(struct request_queue *q, struct bio *bio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001498{
1499 struct mapped_device *md = q->queuedata;
1500
1501 if (dm_request_based(md))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001502 blk_queue_bio(q, bio);
1503 else
1504 _dm_request(q, bio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001505}
1506
1507void dm_dispatch_request(struct request *rq)
1508{
1509 int r;
1510
1511 if (blk_queue_io_stat(rq->q))
1512 rq->cmd_flags |= REQ_IO_STAT;
1513
1514 rq->start_time = jiffies;
1515 r = blk_insert_cloned_request(rq->q, rq);
1516 if (r)
1517 dm_complete_request(rq, r);
1518}
1519EXPORT_SYMBOL_GPL(dm_dispatch_request);
1520
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001521static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1522 void *data)
1523{
1524 struct dm_rq_target_io *tio = data;
Kent Overstreet94818742012-09-07 13:44:01 -07001525 struct dm_rq_clone_bio_info *info =
1526 container_of(bio, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001527
1528 info->orig = bio_orig;
1529 info->tio = tio;
1530 bio->bi_end_io = end_clone_bio;
1531 bio->bi_private = info;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001532
1533 return 0;
1534}
1535
1536static int setup_clone(struct request *clone, struct request *rq,
1537 struct dm_rq_target_io *tio)
1538{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001539 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001540
Tejun Heo29e40132010-09-08 18:07:00 +02001541 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1542 dm_rq_bio_constructor, tio);
1543 if (r)
1544 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001545
Tejun Heo29e40132010-09-08 18:07:00 +02001546 clone->cmd = rq->cmd;
1547 clone->cmd_len = rq->cmd_len;
1548 clone->sense = rq->sense;
1549 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001550 clone->end_io = end_clone_request;
1551 clone->end_io_data = tio;
1552
1553 return 0;
1554}
1555
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001556static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1557 gfp_t gfp_mask)
1558{
1559 struct request *clone;
1560 struct dm_rq_target_io *tio;
1561
1562 tio = alloc_rq_tio(md, gfp_mask);
1563 if (!tio)
1564 return NULL;
1565
1566 tio->md = md;
1567 tio->ti = NULL;
1568 tio->orig = rq;
1569 tio->error = 0;
1570 memset(&tio->info, 0, sizeof(tio->info));
1571
1572 clone = &tio->clone;
1573 if (setup_clone(clone, rq, tio)) {
1574 /* -ENOMEM */
1575 free_rq_tio(tio);
1576 return NULL;
1577 }
1578
1579 return clone;
1580}
1581
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001582/*
1583 * Called with the queue lock held.
1584 */
1585static int dm_prep_fn(struct request_queue *q, struct request *rq)
1586{
1587 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001588 struct request *clone;
1589
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001590 if (unlikely(rq->special)) {
1591 DMWARN("Already has something in rq->special.");
1592 return BLKPREP_KILL;
1593 }
1594
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001595 clone = clone_rq(rq, md, GFP_ATOMIC);
1596 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001597 return BLKPREP_DEFER;
1598
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001599 rq->special = clone;
1600 rq->cmd_flags |= REQ_DONTPREP;
1601
1602 return BLKPREP_OK;
1603}
1604
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001605/*
1606 * Returns:
1607 * 0 : the request has been processed (not requeued)
1608 * !0 : the request has been requeued
1609 */
1610static int map_request(struct dm_target *ti, struct request *clone,
1611 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001612{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001613 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001614 struct dm_rq_target_io *tio = clone->end_io_data;
1615
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001616 tio->ti = ti;
1617 r = ti->type->map_rq(ti, clone, &tio->info);
1618 switch (r) {
1619 case DM_MAPIO_SUBMITTED:
1620 /* The target has taken the I/O to submit by itself later */
1621 break;
1622 case DM_MAPIO_REMAPPED:
1623 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001624 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1625 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001626 dm_dispatch_request(clone);
1627 break;
1628 case DM_MAPIO_REQUEUE:
1629 /* The target wants to requeue the I/O */
1630 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001631 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001632 break;
1633 default:
1634 if (r > 0) {
1635 DMWARN("unimplemented target map return value: %d", r);
1636 BUG();
1637 }
1638
1639 /* The target wants to complete the I/O */
1640 dm_kill_unmapped_request(clone, r);
1641 break;
1642 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001643
1644 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001645}
1646
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001647static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1648{
1649 struct request *clone;
1650
1651 blk_start_request(orig);
1652 clone = orig->special;
1653 atomic_inc(&md->pending[rq_data_dir(clone)]);
1654
1655 /*
1656 * Hold the md reference here for the in-flight I/O.
1657 * We can't rely on the reference count by device opener,
1658 * because the device may be closed during the request completion
1659 * when all bios are completed.
1660 * See the comment in rq_completed() too.
1661 */
1662 dm_get(md);
1663
1664 return clone;
1665}
1666
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001667/*
1668 * q->request_fn for request-based dm.
1669 * Called with the queue lock held.
1670 */
1671static void dm_request_fn(struct request_queue *q)
1672{
1673 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001674 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001675 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001676 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001677 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001678
1679 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001680 * For suspend, check blk_queue_stopped() and increment
1681 * ->pending within a single queue_lock not to increment the
1682 * number of in-flight I/Os after the queue is stopped in
1683 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001684 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001685 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001686 rq = blk_peek_request(q);
1687 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001688 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001689
Tejun Heo29e40132010-09-08 18:07:00 +02001690 /* always use block 0 to find the target for flushes for now */
1691 pos = 0;
1692 if (!(rq->cmd_flags & REQ_FLUSH))
1693 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001694
Tejun Heo29e40132010-09-08 18:07:00 +02001695 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001696 if (!dm_target_is_valid(ti)) {
1697 /*
1698 * Must perform setup, that dm_done() requires,
1699 * before calling dm_kill_unmapped_request
1700 */
1701 DMERR_LIMIT("request attempted access beyond the end of device");
1702 clone = dm_start_request(md, rq);
1703 dm_kill_unmapped_request(clone, -EIO);
1704 continue;
1705 }
Tejun Heo29e40132010-09-08 18:07:00 +02001706
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001707 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001708 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001709
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001710 clone = dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001711
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001712 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001713 if (map_request(ti, clone, md))
1714 goto requeued;
1715
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001716 BUG_ON(!irqs_disabled());
1717 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001718 }
1719
1720 goto out;
1721
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001722requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001723 BUG_ON(!irqs_disabled());
1724 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001725
Jens Axboe7eaceac2011-03-10 08:52:07 +01001726delay_and_out:
1727 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001728out:
1729 dm_table_put(map);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001730}
1731
1732int dm_underlying_device_busy(struct request_queue *q)
1733{
1734 return blk_lld_busy(q);
1735}
1736EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1737
1738static int dm_lld_busy(struct request_queue *q)
1739{
1740 int r;
1741 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001742 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001743
1744 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1745 r = 1;
1746 else
1747 r = dm_table_any_busy_target(map);
1748
1749 dm_table_put(map);
1750
1751 return r;
1752}
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754static int dm_any_congested(void *congested_data, int bdi_bits)
1755{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001756 int r = bdi_bits;
1757 struct mapped_device *md = congested_data;
1758 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001760 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001761 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001762 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001763 /*
1764 * Request-based dm cares about only own queue for
1765 * the query about congestion status of request_queue
1766 */
1767 if (dm_request_based(md))
1768 r = md->queue->backing_dev_info.state &
1769 bdi_bits;
1770 else
1771 r = dm_table_any_congested(map, bdi_bits);
1772
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001773 dm_table_put(map);
1774 }
1775 }
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return r;
1778}
1779
1780/*-----------------------------------------------------------------
1781 * An IDR is used to keep track of allocated minor numbers.
1782 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001783static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001785 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001787 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
1790/*
1791 * See if the device with a specific minor # is free.
1792 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001793static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001795 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 if (minor >= (1 << MINORBITS))
1798 return -EINVAL;
1799
Tejun Heoc9d76be2013-02-27 17:04:26 -08001800 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001801 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Tejun Heoc9d76be2013-02-27 17:04:26 -08001803 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001805 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001806 idr_preload_end();
1807 if (r < 0)
1808 return r == -ENOSPC ? -EBUSY : r;
1809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001812static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001814 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Tejun Heoc9d76be2013-02-27 17:04:26 -08001816 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001817 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Tejun Heoc9d76be2013-02-27 17:04:26 -08001819 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001821 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001822 idr_preload_end();
1823 if (r < 0)
1824 return r;
1825 *minor = r;
1826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001829static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Mikulas Patocka53d59142009-04-02 19:55:37 +01001831static void dm_wq_work(struct work_struct *work);
1832
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001833static void dm_init_md_queue(struct mapped_device *md)
1834{
1835 /*
1836 * Request-based dm devices cannot be stacked on top of bio-based dm
1837 * devices. The type of this dm device has not been decided yet.
1838 * The type is decided at the first table loading time.
1839 * To prevent problematic device stacking, clear the queue flag
1840 * for request stacking support until then.
1841 *
1842 * This queue is new, so no concurrency on the queue_flags.
1843 */
1844 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1845
1846 md->queue->queuedata = md;
1847 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1848 md->queue->backing_dev_info.congested_data = md;
1849 blk_queue_make_request(md->queue, dm_request);
1850 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001851 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854/*
1855 * Allocate and initialise a blank device with a given minor.
1856 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001857static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001860 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001861 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (!md) {
1864 DMWARN("unable to allocate device, out of memory.");
1865 return NULL;
1866 }
1867
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001868 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001869 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001872 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001873 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001874 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001875 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001877 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Mike Snitzera5664da2010-08-12 04:14:01 +01001879 md->type = DM_TYPE_NONE;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001880 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001881 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001882 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001883 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 rwlock_init(&md->map_lock);
1885 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001886 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001888 atomic_set(&md->uevent_seq, 0);
1889 INIT_LIST_HEAD(&md->uevent_list);
1890 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001892 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001894 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001896 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 md->disk = alloc_disk(1);
1899 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001900 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001902 atomic_set(&md->pending[0], 0);
1903 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001904 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001905 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001906 init_waitqueue_head(&md->eventq);
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 md->disk->major = _major;
1909 md->disk->first_minor = minor;
1910 md->disk->fops = &dm_blk_dops;
1911 md->disk->queue = md->queue;
1912 md->disk->private_data = md;
1913 sprintf(md->disk->disk_name, "dm-%d", minor);
1914 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001915 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Tejun Heo9c4376d2011-01-13 19:59:58 +00001917 md->wq = alloc_workqueue("kdmflush",
1918 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001919 if (!md->wq)
1920 goto bad_thread;
1921
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001922 md->bdev = bdget_disk(md->disk, 0);
1923 if (!md->bdev)
1924 goto bad_bdev;
1925
Tejun Heo6a8736d2010-09-08 18:07:00 +02001926 bio_init(&md->flush_bio);
1927 md->flush_bio.bi_bdev = md->bdev;
1928 md->flush_bio.bi_rw = WRITE_FLUSH;
1929
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001930 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001931 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001932 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001933 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001934
1935 BUG_ON(old_md != MINOR_ALLOCED);
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return md;
1938
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001939bad_bdev:
1940 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001941bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001942 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001943 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001944bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001945 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001946bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001948bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001949 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001950bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 kfree(md);
1952 return NULL;
1953}
1954
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001955static void unlock_fs(struct mapped_device *md);
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957static void free_dev(struct mapped_device *md)
1958{
Tejun Heof331c022008-09-03 09:01:48 +02001959 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001960
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001961 unlock_fs(md);
1962 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001963 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001964 if (md->io_pool)
1965 mempool_destroy(md->io_pool);
1966 if (md->bs)
1967 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001968 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001970 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001971
1972 spin_lock(&_minor_lock);
1973 md->disk->private_data = NULL;
1974 spin_unlock(&_minor_lock);
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001977 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001978 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 kfree(md);
1980}
1981
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001982static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1983{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001984 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001985
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00001986 if (md->io_pool && md->bs) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001987 /* The md already has necessary mempools. */
1988 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
1989 /*
1990 * Reload bioset because front_pad may have changed
1991 * because a different table was loaded.
1992 */
1993 bioset_free(md->bs);
1994 md->bs = p->bs;
1995 p->bs = NULL;
1996 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001997 /*
1998 * There's no need to reload with request-based dm
1999 * because the size of front_pad doesn't change.
2000 * Note for future: If you are to reload bioset,
2001 * prep-ed requests in the queue may refer
2002 * to bio from the old bioset, so you must walk
2003 * through the queue to unprep.
2004 */
2005 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002006 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002007 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002008
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002009 BUG_ON(!p || md->io_pool || md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002010
2011 md->io_pool = p->io_pool;
2012 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002013 md->bs = p->bs;
2014 p->bs = NULL;
2015
2016out:
2017 /* mempool bind completed, now no need any mempools in the table */
2018 dm_table_free_md_mempools(t);
2019}
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021/*
2022 * Bind a table to the device.
2023 */
2024static void event_callback(void *context)
2025{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002026 unsigned long flags;
2027 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 struct mapped_device *md = (struct mapped_device *) context;
2029
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002030 spin_lock_irqsave(&md->uevent_lock, flags);
2031 list_splice_init(&md->uevent_list, &uevents);
2032 spin_unlock_irqrestore(&md->uevent_lock, flags);
2033
Tejun Heoed9e1982008-08-25 19:56:05 +09002034 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 atomic_inc(&md->event_nr);
2037 wake_up(&md->eventq);
2038}
2039
Mike Snitzerc2176492011-01-13 19:53:46 +00002040/*
2041 * Protected by md->suspend_lock obtained by dm_swap_table().
2042 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002043static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002045 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002047 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002050/*
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002051 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2052 *
2053 * If this function returns 0, then the device is either a non-dm
2054 * device without a merge_bvec_fn, or it is a dm device that is
2055 * able to split any bios it receives that are too big.
2056 */
2057int dm_queue_merge_is_compulsory(struct request_queue *q)
2058{
2059 struct mapped_device *dev_md;
2060
2061 if (!q->merge_bvec_fn)
2062 return 0;
2063
2064 if (q->make_request_fn == dm_request) {
2065 dev_md = q->queuedata;
2066 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2067 return 0;
2068 }
2069
2070 return 1;
2071}
2072
2073static int dm_device_merge_is_compulsory(struct dm_target *ti,
2074 struct dm_dev *dev, sector_t start,
2075 sector_t len, void *data)
2076{
2077 struct block_device *bdev = dev->bdev;
2078 struct request_queue *q = bdev_get_queue(bdev);
2079
2080 return dm_queue_merge_is_compulsory(q);
2081}
2082
2083/*
2084 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2085 * on the properties of the underlying devices.
2086 */
2087static int dm_table_merge_is_optional(struct dm_table *table)
2088{
2089 unsigned i = 0;
2090 struct dm_target *ti;
2091
2092 while (i < dm_table_get_num_targets(table)) {
2093 ti = dm_table_get_target(table, i++);
2094
2095 if (ti->type->iterate_devices &&
2096 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2097 return 0;
2098 }
2099
2100 return 1;
2101}
2102
2103/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002104 * Returns old map, which caller must destroy.
2105 */
2106static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2107 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002109 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002110 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002112 unsigned long flags;
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002113 int merge_is_optional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002116
2117 /*
2118 * Wipe any geometry if the size of the table changed.
2119 */
2120 if (size != get_capacity(md->disk))
2121 memset(&md->geometry, 0, sizeof(md->geometry));
2122
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002123 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002125 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002126
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002127 /*
2128 * The queue hasn't been stopped yet, if the old table type wasn't
2129 * for request-based during suspension. So stop it to prevent
2130 * I/O mapping before resume.
2131 * This must be done before setting the queue restrictions,
2132 * because request-based dm may be run just after the setting.
2133 */
2134 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2135 stop_queue(q);
2136
2137 __bind_mempools(md, t);
2138
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002139 merge_is_optional = dm_table_merge_is_optional(t);
2140
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002141 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002142 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002143 md->map = t;
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002144 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2145
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002146 dm_table_set_restrictions(t, q, limits);
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002147 if (merge_is_optional)
2148 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2149 else
2150 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002151 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002152
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002153 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Alasdair G Kergona7940152009-12-10 23:52:23 +00002156/*
2157 * Returns unbound table for the caller to free.
2158 */
2159static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002162 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002165 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002168 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002170 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002171
2172 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
2175/*
2176 * Constructor for a new device.
2177 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002178int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct mapped_device *md;
2181
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002182 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (!md)
2184 return -ENXIO;
2185
Milan Broz784aae72009-01-06 03:05:12 +00002186 dm_sysfs_init(md);
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 *result = md;
2189 return 0;
2190}
2191
Mike Snitzera5664da2010-08-12 04:14:01 +01002192/*
2193 * Functions to manage md->type.
2194 * All are required to hold md->type_lock.
2195 */
2196void dm_lock_md_type(struct mapped_device *md)
2197{
2198 mutex_lock(&md->type_lock);
2199}
2200
2201void dm_unlock_md_type(struct mapped_device *md)
2202{
2203 mutex_unlock(&md->type_lock);
2204}
2205
2206void dm_set_md_type(struct mapped_device *md, unsigned type)
2207{
2208 md->type = type;
2209}
2210
2211unsigned dm_get_md_type(struct mapped_device *md)
2212{
2213 return md->type;
2214}
2215
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002216struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2217{
2218 return md->immutable_target_type;
2219}
2220
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002221/*
2222 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2223 */
2224static int dm_init_request_based_queue(struct mapped_device *md)
2225{
2226 struct request_queue *q = NULL;
2227
2228 if (md->queue->elevator)
2229 return 1;
2230
2231 /* Fully initialize the queue */
2232 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2233 if (!q)
2234 return 0;
2235
2236 md->queue = q;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002237 dm_init_md_queue(md);
2238 blk_queue_softirq_done(md->queue, dm_softirq_done);
2239 blk_queue_prep_rq(md->queue, dm_prep_fn);
2240 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002241
2242 elv_register_queue(md->queue);
2243
2244 return 1;
2245}
2246
2247/*
2248 * Setup the DM device's queue based on md's type
2249 */
2250int dm_setup_md_queue(struct mapped_device *md)
2251{
2252 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2253 !dm_init_request_based_queue(md)) {
2254 DMWARN("Cannot initialize queue for request-based mapped device");
2255 return -EINVAL;
2256 }
2257
2258 return 0;
2259}
2260
David Teigland637842c2006-01-06 00:20:00 -08002261static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 unsigned minor = MINOR(dev);
2265
2266 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2267 return NULL;
2268
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002269 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002272 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002273 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002274 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002275 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002276 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002277 goto out;
2278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002280out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002281 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
David Teigland637842c2006-01-06 00:20:00 -08002283 return md;
2284}
2285
David Teiglandd229a952006-01-06 00:20:01 -08002286struct mapped_device *dm_get_md(dev_t dev)
2287{
2288 struct mapped_device *md = dm_find_md(dev);
2289
2290 if (md)
2291 dm_get(md);
2292
2293 return md;
2294}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002295EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002296
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002297void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002298{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002299 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
2302void dm_set_mdptr(struct mapped_device *md, void *ptr)
2303{
2304 md->interface_ptr = ptr;
2305}
2306
2307void dm_get(struct mapped_device *md)
2308{
2309 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002310 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002313const char *dm_device_name(struct mapped_device *md)
2314{
2315 return md->name;
2316}
2317EXPORT_SYMBOL_GPL(dm_device_name);
2318
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002319static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002321 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002323 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002324
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002325 spin_lock(&_minor_lock);
2326 map = dm_get_live_table(md);
2327 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2328 set_bit(DMF_FREEING, &md->flags);
2329 spin_unlock(&_minor_lock);
2330
2331 if (!dm_suspended_md(md)) {
2332 dm_table_presuspend_targets(map);
2333 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002335
2336 /*
2337 * Rare, but there may be I/O requests still going to complete,
2338 * for example. Wait for all references to disappear.
2339 * No one should increment the reference count of the mapped_device,
2340 * after the mapped_device state becomes DMF_FREEING.
2341 */
2342 if (wait)
2343 while (atomic_read(&md->holders))
2344 msleep(1);
2345 else if (atomic_read(&md->holders))
2346 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2347 dm_device_name(md), atomic_read(&md->holders));
2348
2349 dm_sysfs_exit(md);
2350 dm_table_put(map);
2351 dm_table_destroy(__unbind(md));
2352 free_dev(md);
2353}
2354
2355void dm_destroy(struct mapped_device *md)
2356{
2357 __dm_destroy(md, true);
2358}
2359
2360void dm_destroy_immediate(struct mapped_device *md)
2361{
2362 __dm_destroy(md, false);
2363}
2364
2365void dm_put(struct mapped_device *md)
2366{
2367 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
Edward Goggin79eb8852007-05-09 02:32:56 -07002369EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Mikulas Patocka401600d2009-04-02 19:55:38 +01002371static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002372{
2373 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002374 DECLARE_WAITQUEUE(wait, current);
2375
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002376 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002377
2378 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002379 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002380
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002381 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002382 break;
2383
Mikulas Patocka401600d2009-04-02 19:55:38 +01002384 if (interruptible == TASK_INTERRUPTIBLE &&
2385 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002386 r = -EINTR;
2387 break;
2388 }
2389
2390 io_schedule();
2391 }
2392 set_current_state(TASK_RUNNING);
2393
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002394 remove_wait_queue(&md->wait, &wait);
2395
Milan Broz46125c12008-02-08 02:10:30 +00002396 return r;
2397}
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399/*
2400 * Process the deferred bios
2401 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002402static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Mikulas Patockaef208582009-04-02 19:55:38 +01002404 struct mapped_device *md = container_of(work, struct mapped_device,
2405 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002406 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Tejun Heo6a8736d2010-09-08 18:07:00 +02002408 down_read(&md->io_lock);
Mikulas Patockaef208582009-04-02 19:55:38 +01002409
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002410 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002411 spin_lock_irq(&md->deferred_lock);
2412 c = bio_list_pop(&md->deferred);
2413 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002414
Tejun Heo6a8736d2010-09-08 18:07:00 +02002415 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002416 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002417
Tejun Heo6a8736d2010-09-08 18:07:00 +02002418 up_read(&md->io_lock);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002419
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002420 if (dm_request_based(md))
2421 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002422 else
2423 __split_and_process_bio(md, c);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002424
Tejun Heo6a8736d2010-09-08 18:07:00 +02002425 down_read(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002426 }
Milan Broz73d410c2008-02-08 02:10:25 +00002427
Tejun Heo6a8736d2010-09-08 18:07:00 +02002428 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002431static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002432{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002433 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2434 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002435 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002436}
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002439 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002441struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Mike Christie87eb5b22013-03-01 22:45:48 +00002443 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002444 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002445 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Daniel Walkere61290a2008-02-08 02:10:08 +00002447 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002450 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Mike Snitzer3ae70652012-09-26 23:45:45 +01002453 /*
2454 * If the new table has no data devices, retain the existing limits.
2455 * This helps multipath with queue_if_no_path if all paths disappear,
2456 * then new I/O is queued based on these limits, and then some paths
2457 * reappear.
2458 */
2459 if (dm_table_has_no_data_devices(table)) {
2460 live_map = dm_get_live_table(md);
2461 if (live_map)
2462 limits = md->queue->limits;
2463 dm_table_put(live_map);
2464 }
2465
Mike Christie87eb5b22013-03-01 22:45:48 +00002466 if (!live_map) {
2467 r = dm_calculate_queue_limits(table, &limits);
2468 if (r) {
2469 map = ERR_PTR(r);
2470 goto out;
2471 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002472 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002473
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002474 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002476out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002477 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002478 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
2481/*
2482 * Functions to lock and unlock any filesystem running on the
2483 * device.
2484 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002485static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002487 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002490
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002491 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002492 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002493 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002494 md->frozen_sb = NULL;
2495 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002496 }
2497
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002498 set_bit(DMF_FROZEN, &md->flags);
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501}
2502
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002503static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002505 if (!test_bit(DMF_FROZEN, &md->flags))
2506 return;
2507
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002508 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002510 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
2513/*
2514 * We need to be able to change a mapping table under a mounted
2515 * filesystem. For example we might want to move some data in
2516 * the background. Before the table can be swapped with
2517 * dm_bind_table, dm_suspend must be called to flush any in
2518 * flight bios and ensure that any further io gets deferred.
2519 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002520/*
2521 * Suspend mechanism in request-based dm.
2522 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002523 * 1. Flush all I/Os by lock_fs() if needed.
2524 * 2. Stop dispatching any I/O by stopping the request_queue.
2525 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002526 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002527 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002528 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002529int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002531 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002532 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002533 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002534 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Daniel Walkere61290a2008-02-08 02:10:08 +00002536 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002537
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002538 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002539 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002540 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002543 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002545 /*
2546 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2547 * This flag is cleared before dm_suspend returns.
2548 */
2549 if (noflush)
2550 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2551
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002552 /* This does not get reverted if there's an error later. */
2553 dm_table_presuspend_targets(map);
2554
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002555 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002556 * Flush I/O to the device.
2557 * Any I/O submitted after lock_fs() may not be flushed.
2558 * noflush takes precedence over do_lockfs.
2559 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002560 */
2561 if (!noflush && do_lockfs) {
2562 r = lock_fs(md);
2563 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002564 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002568 * Here we must make sure that no processes are submitting requests
2569 * to target drivers i.e. no one may be executing
2570 * __split_and_process_bio. This is called from dm_request and
2571 * dm_wq_work.
2572 *
2573 * To get all processes out of __split_and_process_bio in dm_request,
2574 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002575 * __split_and_process_bio from dm_request and quiesce the thread
2576 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2577 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002579 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002580 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002581 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002583 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002584 * Stop md->queue before flushing md->wq in case request-based
2585 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002586 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002587 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002588 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002589
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002590 flush_workqueue(md->wq);
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002593 * At this point no more requests are entering target request routines.
2594 * We call dm_wait_for_completion to wait for all existing requests
2595 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002597 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002599 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002600 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002601 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002602 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002605 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002606 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002607
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002608 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002609 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002610
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002611 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002612 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002613 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002614
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002615 /*
2616 * If dm_wait_for_completion returned 0, the device is completely
2617 * quiescent now. There is no request-processing activity. All new
2618 * requests are being added to md->deferred list.
2619 */
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 set_bit(DMF_SUSPENDED, &md->flags);
2622
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002623 dm_table_postsuspend_targets(map);
2624
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002625out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002627
2628out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002629 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002630 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
2633int dm_resume(struct mapped_device *md)
2634{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002635 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002636 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Daniel Walkere61290a2008-02-08 02:10:08 +00002638 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002639 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002640 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002641
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002642 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002643 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002644 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Milan Broz8757b772006-10-03 01:15:36 -07002646 r = dm_table_resume_targets(map);
2647 if (r)
2648 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002649
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002650 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002651
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002652 /*
2653 * Flushing deferred I/Os must be done after targets are resumed
2654 * so that mapping of targets can work correctly.
2655 * Request-based dm is queueing the deferred I/Os in its request_queue.
2656 */
2657 if (dm_request_based(md))
2658 start_queue(md->queue);
2659
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002660 unlock_fs(md);
2661
2662 clear_bit(DMF_SUSPENDED, &md->flags);
2663
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002664 r = 0;
2665out:
2666 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002667 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002668
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002669 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
2672/*-----------------------------------------------------------------
2673 * Event notification.
2674 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002675int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002676 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002677{
Milan Broz60935eb2009-06-22 10:12:30 +01002678 char udev_cookie[DM_COOKIE_LENGTH];
2679 char *envp[] = { udev_cookie, NULL };
2680
2681 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002682 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002683 else {
2684 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2685 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002686 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2687 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002688 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002689}
2690
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002691uint32_t dm_next_uevent_seq(struct mapped_device *md)
2692{
2693 return atomic_add_return(1, &md->uevent_seq);
2694}
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696uint32_t dm_get_event_nr(struct mapped_device *md)
2697{
2698 return atomic_read(&md->event_nr);
2699}
2700
2701int dm_wait_event(struct mapped_device *md, int event_nr)
2702{
2703 return wait_event_interruptible(md->eventq,
2704 (event_nr != atomic_read(&md->event_nr)));
2705}
2706
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002707void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2708{
2709 unsigned long flags;
2710
2711 spin_lock_irqsave(&md->uevent_lock, flags);
2712 list_add(elist, &md->uevent_list);
2713 spin_unlock_irqrestore(&md->uevent_lock, flags);
2714}
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716/*
2717 * The gendisk is only valid as long as you have a reference
2718 * count on 'md'.
2719 */
2720struct gendisk *dm_disk(struct mapped_device *md)
2721{
2722 return md->disk;
2723}
2724
Milan Broz784aae72009-01-06 03:05:12 +00002725struct kobject *dm_kobject(struct mapped_device *md)
2726{
2727 return &md->kobj;
2728}
2729
2730/*
2731 * struct mapped_device should not be exported outside of dm.c
2732 * so use this check to verify that kobj is part of md structure
2733 */
2734struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2735{
2736 struct mapped_device *md;
2737
2738 md = container_of(kobj, struct mapped_device, kobj);
2739 if (&md->kobj != kobj)
2740 return NULL;
2741
Milan Broz4d89b7b2009-06-22 10:12:11 +01002742 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002743 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002744 return NULL;
2745
Milan Broz784aae72009-01-06 03:05:12 +00002746 dm_get(md);
2747 return md;
2748}
2749
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002750int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 return test_bit(DMF_SUSPENDED, &md->flags);
2753}
2754
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002755int dm_suspended(struct dm_target *ti)
2756{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002757 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002758}
2759EXPORT_SYMBOL_GPL(dm_suspended);
2760
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002761int dm_noflush_suspending(struct dm_target *ti)
2762{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002763 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002764}
2765EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2766
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002767struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002768{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002769 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
2770 struct kmem_cache *cachep;
2771 unsigned int pool_size;
2772 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002773
2774 if (!pools)
2775 return NULL;
2776
Jun'ichi Nomura23e50832013-03-01 22:45:48 +00002777 if (type == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002778 cachep = _io_cache;
2779 pool_size = 16;
2780 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2781 } else if (type == DM_TYPE_REQUEST_BASED) {
2782 cachep = _rq_tio_cache;
2783 pool_size = MIN_IOS;
2784 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2785 /* per_bio_data_size is not used. See __bind_mempools(). */
2786 WARN_ON(per_bio_data_size != 0);
2787 } else
2788 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002789
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002790 pools->io_pool = mempool_create_slab_pool(MIN_IOS, cachep);
2791 if (!pools->io_pool)
2792 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002793
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002794 pools->bs = bioset_create(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002795 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002796 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002797
Martin K. Petersena91a2782011-03-17 11:11:05 +01002798 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002799 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002800
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002801 return pools;
2802
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002803out:
2804 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002805
2806 return NULL;
2807}
2808
2809void dm_free_md_mempools(struct dm_md_mempools *pools)
2810{
2811 if (!pools)
2812 return;
2813
2814 if (pools->io_pool)
2815 mempool_destroy(pools->io_pool);
2816
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002817 if (pools->bs)
2818 bioset_free(pools->bs);
2819
2820 kfree(pools);
2821}
2822
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002823static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 .open = dm_blk_open,
2825 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002826 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002827 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 .owner = THIS_MODULE
2829};
2830
2831EXPORT_SYMBOL(dm_get_mapinfo);
2832
2833/*
2834 * module hooks
2835 */
2836module_init(dm_init);
2837module_exit(dm_exit);
2838
2839module_param(major, uint, 0);
2840MODULE_PARM_DESC(major, "The major number of the device mapper");
2841MODULE_DESCRIPTION(DM_NAME " driver");
2842MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2843MODULE_LICENSE("GPL");