blob: 561313a7dac23b76ce5a4cce1ced33dc067be5d5 [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>
17#include <linux/buffer_head.h>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020018#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mempool.h>
20#include <linux/slab.h>
21#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080022#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010023#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080024
25#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "core"
28
Milan Broz60935eb2009-06-22 10:12:30 +010029/*
30 * Cookies are numeric values sent with CHANGE and REMOVE
31 * uevents while resuming, removing or renaming the device.
32 */
33#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
34#define DM_COOKIE_LENGTH 24
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static const char *_name = DM_NAME;
37
38static unsigned int major = 0;
39static unsigned int _major = 0;
40
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070041static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000043 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * One of these is allocated per bio.
45 */
46struct dm_io {
47 struct mapped_device *md;
48 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010050 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080051 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010052 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000056 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * One of these is allocated per target within a bio. Hopefully
58 * this will be simplified out one day.
59 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010060struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct dm_io *io;
62 struct dm_target *ti;
63 union map_info info;
64};
65
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000066/*
67 * For request-based dm.
68 * One of these is allocated per request.
69 */
70struct dm_rq_target_io {
71 struct mapped_device *md;
72 struct dm_target *ti;
73 struct request *orig, clone;
74 int error;
75 union map_info info;
76};
77
78/*
79 * For request-based dm.
80 * One of these is allocated per bio.
81 */
82struct dm_rq_clone_bio_info {
83 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010084 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087union map_info *dm_get_mapinfo(struct bio *bio)
88{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070089 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010090 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070091 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010094union map_info *dm_get_rq_mapinfo(struct request *rq)
95{
96 if (rq && rq->end_io_data)
97 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
98 return NULL;
99}
100EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
101
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700102#define MINOR_ALLOCED ((void *)-1)
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Bits for the md->flags field.
106 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100107#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800109#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700110#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700111#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800112#define DMF_NOFLUSH_SUSPENDING 5
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100113#define DMF_QUEUE_IO_TO_THREAD 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Milan Broz304f3f62008-02-08 02:11:17 +0000115/*
116 * Work processed by per-device workqueue.
117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700119 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000120 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 rwlock_t map_lock;
122 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700123 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 unsigned long flags;
126
Jens Axboe165125e2007-07-24 09:28:11 +0200127 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100128 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100129 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100130 struct mutex type_lock;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800133 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 void *interface_ptr;
136
137 /*
138 * A list of ios that arrived while we were suspended.
139 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200140 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100142 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800143 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100144 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /*
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100147 * An error from the barrier request currently being processed.
148 */
149 int barrier_error;
150
151 /*
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000152 * Protect barrier_error from concurrent endio processing
153 * in request-based dm.
154 */
155 spinlock_t barrier_error_lock;
156
157 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000158 * Processing queue (flush/barriers)
159 */
160 struct workqueue_struct *wq;
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000161 struct work_struct barrier_work;
162
163 /* A pointer to the currently processing pre/post flush request */
164 struct request *flush_request;
Milan Broz304f3f62008-02-08 02:11:17 +0000165
166 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * The current mapping.
168 */
169 struct dm_table *map;
170
171 /*
172 * io objects are allocated from here.
173 */
174 mempool_t *io_pool;
175 mempool_t *tio_pool;
176
Stefan Bader9faf4002006-10-03 01:15:41 -0700177 struct bio_set *bs;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
180 * Event handling.
181 */
182 atomic_t event_nr;
183 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100184 atomic_t uevent_seq;
185 struct list_head uevent_list;
186 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /*
189 * freeze/thaw support require holding onto a super block
190 */
191 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100192 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800193
194 /* forced geometry settings */
195 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000196
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100197 /* For saving the address of __make_request for request based dm */
198 make_request_fn *saved_make_request_fn;
199
Milan Broz784aae72009-01-06 03:05:12 +0000200 /* sysfs handle */
201 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100202
203 /* zero-length barrier that will be cloned and submitted to targets */
204 struct bio barrier_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100207/*
208 * For mempools pre-allocation at the table loading time.
209 */
210struct dm_md_mempools {
211 mempool_t *io_pool;
212 mempool_t *tio_pool;
213 struct bio_set *bs;
214};
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800217static struct kmem_cache *_io_cache;
218static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000219static struct kmem_cache *_rq_tio_cache;
220static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static int __init local_init(void)
223{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100224 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100227 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100229 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100232 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100233 if (!_tio_cache)
234 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000236 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
237 if (!_rq_tio_cache)
238 goto out_free_tio_cache;
239
240 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
241 if (!_rq_bio_info_cache)
242 goto out_free_rq_tio_cache;
243
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100244 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100245 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000246 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 _major = major;
249 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100250 if (r < 0)
251 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (!_major)
254 _major = r;
255
256 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100257
258out_uevent_exit:
259 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000260out_free_rq_bio_info_cache:
261 kmem_cache_destroy(_rq_bio_info_cache);
262out_free_rq_tio_cache:
263 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100264out_free_tio_cache:
265 kmem_cache_destroy(_tio_cache);
266out_free_io_cache:
267 kmem_cache_destroy(_io_cache);
268
269 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272static void local_exit(void)
273{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000274 kmem_cache_destroy(_rq_bio_info_cache);
275 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 kmem_cache_destroy(_tio_cache);
277 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700278 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100279 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 _major = 0;
282
283 DMINFO("cleaned up");
284}
285
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000286static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 local_init,
288 dm_target_init,
289 dm_linear_init,
290 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000291 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100292 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 dm_interface_init,
294};
295
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000296static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 local_exit,
298 dm_target_exit,
299 dm_linear_exit,
300 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000301 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100302 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 dm_interface_exit,
304};
305
306static int __init dm_init(void)
307{
308 const int count = ARRAY_SIZE(_inits);
309
310 int r, i;
311
312 for (i = 0; i < count; i++) {
313 r = _inits[i]();
314 if (r)
315 goto bad;
316 }
317
318 return 0;
319
320 bad:
321 while (i--)
322 _exits[i]();
323
324 return r;
325}
326
327static void __exit dm_exit(void)
328{
329 int i = ARRAY_SIZE(_exits);
330
331 while (i--)
332 _exits[i]();
333}
334
335/*
336 * Block device functions
337 */
Mike Anderson432a2122009-12-10 23:52:20 +0000338int dm_deleting_md(struct mapped_device *md)
339{
340 return test_bit(DMF_DELETING, &md->flags);
341}
342
Al Virofe5f9f22008-03-02 10:29:31 -0500343static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct mapped_device *md;
346
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200347 lock_kernel();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700348 spin_lock(&_minor_lock);
349
Al Virofe5f9f22008-03-02 10:29:31 -0500350 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700351 if (!md)
352 goto out;
353
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700354 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000355 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700356 md = NULL;
357 goto out;
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700361 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700362
363out:
364 spin_unlock(&_minor_lock);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200365 unlock_kernel();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700366
367 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Al Virofe5f9f22008-03-02 10:29:31 -0500370static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Al Virofe5f9f22008-03-02 10:29:31 -0500372 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200373
374 lock_kernel();
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700375 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dm_put(md);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200377 unlock_kernel();
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 0;
380}
381
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700382int dm_open_count(struct mapped_device *md)
383{
384 return atomic_read(&md->open_count);
385}
386
387/*
388 * Guarantees nothing is using the device before it's deleted.
389 */
390int dm_lock_for_deletion(struct mapped_device *md)
391{
392 int r = 0;
393
394 spin_lock(&_minor_lock);
395
396 if (dm_open_count(md))
397 r = -EBUSY;
398 else
399 set_bit(DMF_DELETING, &md->flags);
400
401 spin_unlock(&_minor_lock);
402
403 return r;
404}
405
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800406static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
407{
408 struct mapped_device *md = bdev->bd_disk->private_data;
409
410 return dm_get_geometry(md, geo);
411}
412
Al Virofe5f9f22008-03-02 10:29:31 -0500413static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700414 unsigned int cmd, unsigned long arg)
415{
Al Virofe5f9f22008-03-02 10:29:31 -0500416 struct mapped_device *md = bdev->bd_disk->private_data;
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000417 struct dm_table *map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700418 struct dm_target *tgt;
419 int r = -ENOTTY;
420
Milan Brozaa129a22006-10-03 01:15:15 -0700421 if (!map || !dm_table_get_size(map))
422 goto out;
423
424 /* We only support devices that have a single target */
425 if (dm_table_get_num_targets(map) != 1)
426 goto out;
427
428 tgt = dm_table_get_target(map, 0);
429
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000430 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700431 r = -EAGAIN;
432 goto out;
433 }
434
435 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400436 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700437
438out:
439 dm_table_put(map);
440
Milan Brozaa129a22006-10-03 01:15:15 -0700441 return r;
442}
443
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100444static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 return mempool_alloc(md->io_pool, GFP_NOIO);
447}
448
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100449static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 mempool_free(io, md->io_pool);
452}
453
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100454static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 mempool_free(tio, md->tio_pool);
457}
458
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000459static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
460 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100461{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000462 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100463}
464
465static void free_rq_tio(struct dm_rq_target_io *tio)
466{
467 mempool_free(tio, tio->md->tio_pool);
468}
469
470static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
471{
472 return mempool_alloc(md->io_pool, GFP_ATOMIC);
473}
474
475static void free_bio_info(struct dm_rq_clone_bio_info *info)
476{
477 mempool_free(info, info->tio->md->io_pool);
478}
479
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000480static int md_in_flight(struct mapped_device *md)
481{
482 return atomic_read(&md->pending[READ]) +
483 atomic_read(&md->pending[WRITE]);
484}
485
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800486static void start_io_acct(struct dm_io *io)
487{
488 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900489 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200490 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800491
492 io->start_time = jiffies;
493
Tejun Heo074a7ac2008-08-25 19:56:14 +0900494 cpu = part_stat_lock();
495 part_round_stats(cpu, &dm_disk(md)->part0);
496 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200497 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800498}
499
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000500static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800501{
502 struct mapped_device *md = io->md;
503 struct bio *bio = io->bio;
504 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900505 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800506 int rw = bio_data_dir(bio);
507
Tejun Heo074a7ac2008-08-25 19:56:14 +0900508 cpu = part_stat_lock();
509 part_round_stats(cpu, &dm_disk(md)->part0);
510 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
511 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800512
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100513 /*
514 * After this is decremented the bio must not be touched if it is
515 * a barrier.
516 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200517 dm_disk(md)->part0.in_flight[rw] = pending =
518 atomic_dec_return(&md->pending[rw]);
519 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800520
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000521 /* nudge anyone waiting on suspend queue */
522 if (!pending)
523 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/*
527 * Add the bio to the list of deferred io.
528 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100529static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700531 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Mikulas Patocka022c2612009-04-02 19:55:39 +0100533 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100535 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Mikulas Patocka92c63902009-04-09 00:27:15 +0100537 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
538 queue_work(md->wq, &md->work);
539
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700540 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543/*
544 * Everyone (including functions in this file), should use this
545 * function to access the md->map field, and make sure they call
546 * dm_table_put() when finished.
547 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000548struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100551 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100553 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 t = md->map;
555 if (t)
556 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100557 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 return t;
560}
561
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800562/*
563 * Get the geometry associated with a dm device
564 */
565int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
566{
567 *geo = md->geometry;
568
569 return 0;
570}
571
572/*
573 * Set the geometry of a device.
574 */
575int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
576{
577 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
578
579 if (geo->start > sz) {
580 DMWARN("Start sector is beyond the geometry limits.");
581 return -EINVAL;
582 }
583
584 md->geometry = *geo;
585
586 return 0;
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*-----------------------------------------------------------------
590 * CRUD START:
591 * A more elegant soln is in the works that uses the queue
592 * merge fn, unfortunately there are a couple of changes to
593 * the block layer that I want to make for this. So in the
594 * interests of getting something for people to use I give
595 * you this clearly demarcated crap.
596 *---------------------------------------------------------------*/
597
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800598static int __noflush_suspending(struct mapped_device *md)
599{
600 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/*
604 * Decrements the number of outstanding ios that a bio has been
605 * cloned into, completing the original io if necc.
606 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800607static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800609 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000610 int io_error;
611 struct bio *bio;
612 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800613
614 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100615 if (unlikely(error)) {
616 spin_lock_irqsave(&io->endio_lock, flags);
617 if (!(io->error > 0 && __noflush_suspending(md)))
618 io->error = error;
619 spin_unlock_irqrestore(&io->endio_lock, flags);
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800623 if (io->error == DM_ENDIO_REQUEUE) {
624 /*
625 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800626 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100627 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100628 if (__noflush_suspending(md)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200629 if (!(io->bio->bi_rw & REQ_HARDBARRIER))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100630 bio_list_add_head(&md->deferred,
631 io->bio);
632 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800633 /* noflush suspend was interrupted. */
634 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100635 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800636 }
637
Milan Brozb35f8ca2009-03-16 17:44:36 +0000638 io_error = io->error;
639 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100640
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200641 if (bio->bi_rw & REQ_HARDBARRIER) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100642 /*
643 * There can be just one barrier request so we use
644 * a per-device variable for error reporting.
645 * Note that you can't touch the bio after end_io_acct
Mikulas Patocka708e9292010-08-12 04:14:00 +0100646 *
647 * We ignore -EOPNOTSUPP for empty flush reported by
648 * underlying devices. We assume that if the device
649 * doesn't support empty barriers, it doesn't need
650 * cache flushing commands.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100651 */
Mikulas Patocka708e9292010-08-12 04:14:00 +0100652 if (!md->barrier_error &&
653 !(bio_empty_barrier(bio) && io_error == -EOPNOTSUPP))
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100654 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100655 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000656 free_io(md, io);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100657 } else {
658 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000659 free_io(md, io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000660
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100661 if (io_error != DM_ENDIO_REQUEUE) {
662 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000663
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100664 bio_endio(bio, io_error);
665 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668}
669
NeilBrown6712ecf2007-09-27 12:47:43 +0200670static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100673 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000674 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700675 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 dm_endio_fn endio = tio->ti->type->end_io;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
679 error = -EIO;
680
681 if (endio) {
682 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800683 if (r < 0 || r == DM_ENDIO_REQUEUE)
684 /*
685 * error and requeue request are handled
686 * in dec_pending().
687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800689 else if (r == DM_ENDIO_INCOMPLETE)
690 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200691 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800692 else if (r) {
693 DMWARN("unimplemented target endio return value: %d", r);
694 BUG();
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Stefan Bader9faf4002006-10-03 01:15:41 -0700698 /*
699 * Store md for cleanup instead of tio which is about to get freed.
700 */
701 bio->bi_private = md->bs;
702
Stefan Bader9faf4002006-10-03 01:15:41 -0700703 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000704 bio_put(bio);
705 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100708/*
709 * Partial completion handling for request-based dm
710 */
711static void end_clone_bio(struct bio *clone, int error)
712{
713 struct dm_rq_clone_bio_info *info = clone->bi_private;
714 struct dm_rq_target_io *tio = info->tio;
715 struct bio *bio = info->orig;
716 unsigned int nr_bytes = info->orig->bi_size;
717
718 bio_put(clone);
719
720 if (tio->error)
721 /*
722 * An error has already been detected on the request.
723 * Once error occurred, just let clone->end_io() handle
724 * the remainder.
725 */
726 return;
727 else if (error) {
728 /*
729 * Don't notice the error to the upper layer yet.
730 * The error handling decision is made by the target driver,
731 * when the request is completed.
732 */
733 tio->error = error;
734 return;
735 }
736
737 /*
738 * I/O for the bio successfully completed.
739 * Notice the data completion to the upper layer.
740 */
741
742 /*
743 * bios are processed from the head of the list.
744 * So the completing bio should always be rq->bio.
745 * If it's not, something wrong is happening.
746 */
747 if (tio->orig->bio != bio)
748 DMERR("bio completion is going in the middle of the request");
749
750 /*
751 * Update the original request.
752 * Do not use blk_end_request() here, because it may complete
753 * the original request before the clone, and break the ordering.
754 */
755 blk_update_request(tio->orig, 0, nr_bytes);
756}
757
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000758static void store_barrier_error(struct mapped_device *md, int error)
759{
760 unsigned long flags;
761
762 spin_lock_irqsave(&md->barrier_error_lock, flags);
763 /*
764 * Basically, the first error is taken, but:
765 * -EOPNOTSUPP supersedes any I/O error.
766 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
767 */
768 if (!md->barrier_error || error == -EOPNOTSUPP ||
769 (md->barrier_error != -EOPNOTSUPP &&
770 error == DM_ENDIO_REQUEUE))
771 md->barrier_error = error;
772 spin_unlock_irqrestore(&md->barrier_error_lock, flags);
773}
774
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100775/*
776 * Don't touch any member of the md after calling this function because
777 * the md may be freed in dm_put() at the end of this function.
778 * Or do dm_get() before calling this function and dm_put() later.
779 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000780static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100781{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000782 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100783
784 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000785 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100786 wake_up(&md->wait);
787
788 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000789 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100790
791 /*
792 * dm_put() must be at the end of this function. See the comment above
793 */
794 dm_put(md);
795}
796
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100797static void free_rq_clone(struct request *clone)
798{
799 struct dm_rq_target_io *tio = clone->end_io_data;
800
801 blk_rq_unprep_clone(clone);
802 free_rq_tio(tio);
803}
804
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000805/*
806 * Complete the clone and the original request.
807 * Must be called without queue lock.
808 */
809static void dm_end_request(struct request *clone, int error)
810{
811 int rw = rq_data_dir(clone);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000812 int run_queue = 1;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200813 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000814 struct dm_rq_target_io *tio = clone->end_io_data;
815 struct mapped_device *md = tio->md;
816 struct request *rq = tio->orig;
817
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200818 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000819 rq->errors = clone->errors;
820 rq->resid_len = clone->resid_len;
821
822 if (rq->sense)
823 /*
824 * We are using the sense buffer of the original
825 * request.
826 * So setting the length of the sense data is enough.
827 */
828 rq->sense_len = clone->sense_len;
829 }
830
831 free_rq_clone(clone);
832
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000833 if (unlikely(is_barrier)) {
834 if (unlikely(error))
835 store_barrier_error(md, error);
836 run_queue = 0;
837 } else
838 blk_end_request_all(rq, error);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000839
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000840 rq_completed(md, rw, run_queue);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000841}
842
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100843static void dm_unprep_request(struct request *rq)
844{
845 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100846
847 rq->special = NULL;
848 rq->cmd_flags &= ~REQ_DONTPREP;
849
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100850 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100851}
852
853/*
854 * Requeue the original request of a clone.
855 */
856void dm_requeue_unmapped_request(struct request *clone)
857{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000858 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100859 struct dm_rq_target_io *tio = clone->end_io_data;
860 struct mapped_device *md = tio->md;
861 struct request *rq = tio->orig;
862 struct request_queue *q = rq->q;
863 unsigned long flags;
864
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200865 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000866 /*
867 * Barrier clones share an original request.
868 * Leave it to dm_end_request(), which handles this special
869 * case.
870 */
871 dm_end_request(clone, DM_ENDIO_REQUEUE);
872 return;
873 }
874
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100875 dm_unprep_request(rq);
876
877 spin_lock_irqsave(q->queue_lock, flags);
878 if (elv_queue_empty(q))
879 blk_plug_device(q);
880 blk_requeue_request(q, rq);
881 spin_unlock_irqrestore(q->queue_lock, flags);
882
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000883 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100884}
885EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
886
887static void __stop_queue(struct request_queue *q)
888{
889 blk_stop_queue(q);
890}
891
892static void stop_queue(struct request_queue *q)
893{
894 unsigned long flags;
895
896 spin_lock_irqsave(q->queue_lock, flags);
897 __stop_queue(q);
898 spin_unlock_irqrestore(q->queue_lock, flags);
899}
900
901static void __start_queue(struct request_queue *q)
902{
903 if (blk_queue_stopped(q))
904 blk_start_queue(q);
905}
906
907static void start_queue(struct request_queue *q)
908{
909 unsigned long flags;
910
911 spin_lock_irqsave(q->queue_lock, flags);
912 __start_queue(q);
913 spin_unlock_irqrestore(q->queue_lock, flags);
914}
915
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000916static void dm_done(struct request *clone, int error, bool mapped)
917{
918 int r = error;
919 struct dm_rq_target_io *tio = clone->end_io_data;
920 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
921
922 if (mapped && rq_end_io)
923 r = rq_end_io(tio->ti, clone, error, &tio->info);
924
925 if (r <= 0)
926 /* The target wants to complete the I/O */
927 dm_end_request(clone, r);
928 else if (r == DM_ENDIO_INCOMPLETE)
929 /* The target will handle the I/O */
930 return;
931 else if (r == DM_ENDIO_REQUEUE)
932 /* The target wants to requeue the I/O */
933 dm_requeue_unmapped_request(clone);
934 else {
935 DMWARN("unimplemented target endio return value: %d", r);
936 BUG();
937 }
938}
939
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100940/*
941 * Request completion handler for request-based dm
942 */
943static void dm_softirq_done(struct request *rq)
944{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000945 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100946 struct request *clone = rq->completion_data;
947 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100948
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000949 if (rq->cmd_flags & REQ_FAILED)
950 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100951
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000952 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100953}
954
955/*
956 * Complete the clone and the original request with the error status
957 * through softirq context.
958 */
959static void dm_complete_request(struct request *clone, int error)
960{
961 struct dm_rq_target_io *tio = clone->end_io_data;
962 struct request *rq = tio->orig;
963
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200964 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000965 /*
966 * Barrier clones share an original request. So can't use
967 * softirq_done with the original.
968 * Pass the clone to dm_done() directly in this special case.
969 * It is safe (even if clone->q->queue_lock is held here)
970 * because there is no I/O dispatching during the completion
971 * of barrier clone.
972 */
973 dm_done(clone, error, true);
974 return;
975 }
976
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100977 tio->error = error;
978 rq->completion_data = clone;
979 blk_complete_request(rq);
980}
981
982/*
983 * Complete the not-mapped clone and the original request with the error status
984 * through softirq context.
985 * Target's rq_end_io() function isn't called.
986 * This may be used when the target's map_rq() function fails.
987 */
988void dm_kill_unmapped_request(struct request *clone, int error)
989{
990 struct dm_rq_target_io *tio = clone->end_io_data;
991 struct request *rq = tio->orig;
992
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200993 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000994 /*
995 * Barrier clones share an original request.
996 * Leave it to dm_end_request(), which handles this special
997 * case.
998 */
999 BUG_ON(error > 0);
1000 dm_end_request(clone, error);
1001 return;
1002 }
1003
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001004 rq->cmd_flags |= REQ_FAILED;
1005 dm_complete_request(clone, error);
1006}
1007EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1008
1009/*
1010 * Called with the queue lock held
1011 */
1012static void end_clone_request(struct request *clone, int error)
1013{
1014 /*
1015 * For just cleaning up the information of the queue in which
1016 * the clone was dispatched.
1017 * The clone is *NOT* freed actually here because it is alloced from
1018 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1019 */
1020 __blk_put_request(clone->q, clone);
1021
1022 /*
1023 * Actual request completion is done in a softirq context which doesn't
1024 * hold the queue lock. Otherwise, deadlock could occur because:
1025 * - another request may be submitted by the upper level driver
1026 * of the stacking during the completion
1027 * - the submission which requires queue lock may be done
1028 * against this queue
1029 */
1030 dm_complete_request(clone, error);
1031}
1032
Mike Snitzer56a67df2010-08-12 04:14:10 +01001033/*
1034 * Return maximum size of I/O possible at the supplied sector up to the current
1035 * target boundary.
1036 */
1037static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001039 sector_t target_offset = dm_target_offset(ti, sector);
1040
1041 return ti->len - target_offset;
1042}
1043
1044static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1045{
1046 sector_t len = max_io_len_target_boundary(sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /*
1049 * Does the target need to split even further ?
1050 */
1051 if (ti->split_io) {
1052 sector_t boundary;
Mike Snitzer56a67df2010-08-12 04:14:10 +01001053 sector_t offset = dm_target_offset(ti, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
1055 - offset;
1056 if (len > boundary)
1057 len = boundary;
1058 }
1059
1060 return len;
1061}
1062
1063static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001064 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001067 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001068 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 clone->bi_end_io = clone_endio;
1071 clone->bi_private = tio;
1072
1073 /*
1074 * Map the clone. If r == 0 we don't need to do
1075 * anything, the target has assumed ownership of
1076 * this io.
1077 */
1078 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001079 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001081 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001083
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001084 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001085 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001088 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1089 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001090 md = tio->io->md;
1091 dec_pending(tio->io, r);
1092 /*
1093 * Store bio_set for cleanup.
1094 */
1095 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001097 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001098 } else if (r) {
1099 DMWARN("unimplemented target map return value: %d", r);
1100 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102}
1103
1104struct clone_info {
1105 struct mapped_device *md;
1106 struct dm_table *map;
1107 struct bio *bio;
1108 struct dm_io *io;
1109 sector_t sector;
1110 sector_t sector_count;
1111 unsigned short idx;
1112};
1113
Peter Osterlund36763472005-09-06 15:16:42 -07001114static void dm_bio_destructor(struct bio *bio)
1115{
Stefan Bader9faf4002006-10-03 01:15:41 -07001116 struct bio_set *bs = bio->bi_private;
1117
1118 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/*
1122 * Creates a little bio that is just does part of a bvec.
1123 */
1124static struct bio *split_bvec(struct bio *bio, sector_t sector,
1125 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001126 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct bio *clone;
1129 struct bio_vec *bv = bio->bi_io_vec + idx;
1130
Stefan Bader9faf4002006-10-03 01:15:41 -07001131 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001132 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *clone->bi_io_vec = *bv;
1134
1135 clone->bi_sector = sector;
1136 clone->bi_bdev = bio->bi_bdev;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001137 clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 clone->bi_vcnt = 1;
1139 clone->bi_size = to_bytes(len);
1140 clone->bi_io_vec->bv_offset = offset;
1141 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001142 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Martin K. Petersen9c470082009-04-09 00:27:12 +01001144 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001145 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001146 bio_integrity_trim(clone,
1147 bio_sector_offset(bio, idx, offset), len);
1148 }
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return clone;
1151}
1152
1153/*
1154 * Creates a bio that consists of range of complete bvecs.
1155 */
1156static struct bio *clone_bio(struct bio *bio, sector_t sector,
1157 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001158 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 struct bio *clone;
1161
Stefan Bader9faf4002006-10-03 01:15:41 -07001162 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1163 __bio_clone(clone, bio);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001164 clone->bi_rw &= ~REQ_HARDBARRIER;
Stefan Bader9faf4002006-10-03 01:15:41 -07001165 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 clone->bi_sector = sector;
1167 clone->bi_idx = idx;
1168 clone->bi_vcnt = idx + bv_count;
1169 clone->bi_size = to_bytes(len);
1170 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1171
Martin K. Petersen9c470082009-04-09 00:27:12 +01001172 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001173 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001174
1175 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1176 bio_integrity_trim(clone,
1177 bio_sector_offset(bio, idx, 0), len);
1178 }
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return clone;
1181}
1182
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001183static struct dm_target_io *alloc_tio(struct clone_info *ci,
1184 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001185{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001186 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001187
1188 tio->io = ci->io;
1189 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001190 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001191
1192 return tio;
1193}
1194
Mike Snitzer06a426c2010-08-12 04:14:09 +01001195static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
1196 unsigned request_nr)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001197{
1198 struct dm_target_io *tio = alloc_tio(ci, ti);
1199 struct bio *clone;
1200
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001201 tio->info.target_request_nr = request_nr;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001202
Mike Snitzer06a426c2010-08-12 04:14:09 +01001203 /*
1204 * Discard requests require the bio's inline iovecs be initialized.
1205 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1206 * and discard, so no need for concern about wasted bvec allocations.
1207 */
1208 clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001209 __bio_clone(clone, ci->bio);
1210 clone->bi_destructor = dm_bio_destructor;
1211
1212 __map_bio(ti, clone, tio);
1213}
1214
Mike Snitzer06a426c2010-08-12 04:14:09 +01001215static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
1216 unsigned num_requests)
1217{
1218 unsigned request_nr;
1219
1220 for (request_nr = 0; request_nr < num_requests; request_nr++)
1221 __issue_target_request(ci, ti, request_nr);
1222}
1223
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001224static int __clone_and_map_empty_barrier(struct clone_info *ci)
1225{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001226 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001227 struct dm_target *ti;
1228
1229 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mike Snitzer06a426c2010-08-12 04:14:09 +01001230 __issue_target_requests(ci, ti, ti->num_flush_requests);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001231
1232 ci->sector_count = 0;
1233
1234 return 0;
1235}
1236
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001237/*
1238 * Perform all io with a single clone.
1239 */
1240static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
1241{
1242 struct bio *clone, *bio = ci->bio;
1243 struct dm_target_io *tio;
1244
1245 tio = alloc_tio(ci, ti);
1246 clone = clone_bio(bio, ci->sector, ci->idx,
1247 bio->bi_vcnt - ci->idx, ci->sector_count,
1248 ci->md->bs);
1249 __map_bio(ti, clone, tio);
1250 ci->sector_count = 0;
1251}
1252
1253static int __clone_and_map_discard(struct clone_info *ci)
1254{
1255 struct dm_target *ti;
1256 sector_t max;
1257
1258 ti = dm_table_find_target(ci->map, ci->sector);
1259 if (!dm_target_is_valid(ti))
1260 return -EIO;
1261
1262 /*
1263 * Even though the device advertised discard support,
1264 * reconfiguration might have changed that since the
1265 * check was performed.
1266 */
1267
1268 if (!ti->num_discard_requests)
1269 return -EOPNOTSUPP;
1270
Mike Snitzer56a67df2010-08-12 04:14:10 +01001271 max = max_io_len(ci->sector, ti);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001272
1273 if (ci->sector_count > max)
1274 /*
1275 * FIXME: Handle a discard that spans two or more targets.
1276 */
1277 return -EOPNOTSUPP;
1278
Mike Snitzer06a426c2010-08-12 04:14:09 +01001279 __issue_target_requests(ci, ti, ti->num_discard_requests);
1280
1281 ci->sector_count = 0;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001282
1283 return 0;
1284}
1285
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001286static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001289 struct dm_target *ti;
1290 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001291 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001293 if (unlikely(bio_empty_barrier(bio)))
1294 return __clone_and_map_empty_barrier(ci);
1295
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001296 if (unlikely(bio->bi_rw & REQ_DISCARD))
1297 return __clone_and_map_discard(ci);
1298
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001299 ti = dm_table_find_target(ci->map, ci->sector);
1300 if (!dm_target_is_valid(ti))
1301 return -EIO;
1302
Mike Snitzer56a67df2010-08-12 04:14:10 +01001303 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (ci->sector_count <= max) {
1306 /*
1307 * Optimise for the simple case where we can do all of
1308 * the remaining io with a single clone.
1309 */
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001310 __clone_and_map_simple(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1313 /*
1314 * There are some bvecs that don't span targets.
1315 * Do as many of these as possible.
1316 */
1317 int i;
1318 sector_t remaining = max;
1319 sector_t bv_len;
1320
1321 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1322 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1323
1324 if (bv_len > remaining)
1325 break;
1326
1327 remaining -= bv_len;
1328 len += bv_len;
1329 }
1330
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001331 tio = alloc_tio(ci, ti);
Stefan Bader9faf4002006-10-03 01:15:41 -07001332 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1333 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 __map_bio(ti, clone, tio);
1335
1336 ci->sector += len;
1337 ci->sector_count -= len;
1338 ci->idx = i;
1339
1340 } else {
1341 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001342 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
1344 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001345 sector_t remaining = to_sector(bv->bv_len);
1346 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001348 do {
1349 if (offset) {
1350 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001351 if (!dm_target_is_valid(ti))
1352 return -EIO;
1353
Mike Snitzer56a67df2010-08-12 04:14:10 +01001354 max = max_io_len(ci->sector, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001357 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001359 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001360 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001361 bv->bv_offset + offset, len,
1362 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001363
1364 __map_bio(ti, clone, tio);
1365
1366 ci->sector += len;
1367 ci->sector_count -= len;
1368 offset += to_bytes(len);
1369 } while (remaining -= len);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ci->idx++;
1372 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001373
1374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001378 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001380static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001383 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001385 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001386 if (unlikely(!ci.map)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001387 if (!(bio->bi_rw & REQ_HARDBARRIER))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001388 bio_io_error(bio);
1389 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001390 if (!md->barrier_error)
1391 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001392 return;
1393 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 ci.md = md;
1396 ci.bio = bio;
1397 ci.io = alloc_io(md);
1398 ci.io->error = 0;
1399 atomic_set(&ci.io->io_count, 1);
1400 ci.io->bio = bio;
1401 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001402 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 ci.sector = bio->bi_sector;
1404 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001405 if (unlikely(bio_empty_barrier(bio)))
1406 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 ci.idx = bio->bi_idx;
1408
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001409 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001410 while (ci.sector_count && !error)
1411 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001414 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 dm_table_put(ci.map);
1416}
1417/*-----------------------------------------------------------------
1418 * CRUD END
1419 *---------------------------------------------------------------*/
1420
Milan Brozf6fccb12008-07-21 12:00:37 +01001421static int dm_merge_bvec(struct request_queue *q,
1422 struct bvec_merge_data *bvm,
1423 struct bio_vec *biovec)
1424{
1425 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001426 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001427 struct dm_target *ti;
1428 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001429 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001430
1431 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001432 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001433
1434 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001435 if (!dm_target_is_valid(ti))
1436 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001437
1438 /*
1439 * Find maximum amount of I/O that won't need splitting
1440 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001441 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001442 (sector_t) BIO_MAX_SECTORS);
1443 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1444 if (max_size < 0)
1445 max_size = 0;
1446
1447 /*
1448 * merge_bvec_fn() returns number of bytes
1449 * it can accept at this offset
1450 * max is precomputed maximal io size
1451 */
1452 if (max_size && ti->type->merge)
1453 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001454 /*
1455 * If the target doesn't support merge method and some of the devices
1456 * provided their merge_bvec method (we know this by looking at
1457 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1458 * entries. So always set max_size to 0, and the code below allows
1459 * just one page.
1460 */
1461 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1462
1463 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001464
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001465out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001466 dm_table_put(map);
1467
1468out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001469 /*
1470 * Always allow an entire first page
1471 */
1472 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1473 max_size = biovec->bv_len;
1474
Milan Brozf6fccb12008-07-21 12:00:37 +01001475 return max_size;
1476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478/*
1479 * The request function that just remaps the bio built up by
1480 * dm_merge_bvec.
1481 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001482static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Kevin Corry12f03a42006-02-01 03:04:52 -08001484 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001486 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001488 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Tejun Heo074a7ac2008-08-25 19:56:14 +09001490 cpu = part_stat_lock();
1491 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1492 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1493 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001496 * If we're suspended or the thread is processing barriers
1497 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001499 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001500 unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001501 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001503 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1504 bio_rw(bio) == READA) {
1505 bio_io_error(bio);
1506 return 0;
1507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Mikulas Patocka92c63902009-04-09 00:27:15 +01001509 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Mikulas Patocka92c63902009-04-09 00:27:15 +01001511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001514 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001515 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001519static int dm_make_request(struct request_queue *q, struct bio *bio)
1520{
1521 struct mapped_device *md = q->queuedata;
1522
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001523 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1524}
1525
1526static int dm_request_based(struct mapped_device *md)
1527{
1528 return blk_queue_stackable(md->queue);
1529}
1530
1531static int dm_request(struct request_queue *q, struct bio *bio)
1532{
1533 struct mapped_device *md = q->queuedata;
1534
1535 if (dm_request_based(md))
1536 return dm_make_request(q, bio);
1537
1538 return _dm_request(q, bio);
1539}
1540
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001541static bool dm_rq_is_flush_request(struct request *rq)
1542{
FUJITA Tomonori144d6ed2010-07-03 17:45:37 +09001543 if (rq->cmd_flags & REQ_FLUSH)
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001544 return true;
1545 else
1546 return false;
1547}
1548
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001549void dm_dispatch_request(struct request *rq)
1550{
1551 int r;
1552
1553 if (blk_queue_io_stat(rq->q))
1554 rq->cmd_flags |= REQ_IO_STAT;
1555
1556 rq->start_time = jiffies;
1557 r = blk_insert_cloned_request(rq->q, rq);
1558 if (r)
1559 dm_complete_request(rq, r);
1560}
1561EXPORT_SYMBOL_GPL(dm_dispatch_request);
1562
1563static void dm_rq_bio_destructor(struct bio *bio)
1564{
1565 struct dm_rq_clone_bio_info *info = bio->bi_private;
1566 struct mapped_device *md = info->tio->md;
1567
1568 free_bio_info(info);
1569 bio_free(bio, md->bs);
1570}
1571
1572static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1573 void *data)
1574{
1575 struct dm_rq_target_io *tio = data;
1576 struct mapped_device *md = tio->md;
1577 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1578
1579 if (!info)
1580 return -ENOMEM;
1581
1582 info->orig = bio_orig;
1583 info->tio = tio;
1584 bio->bi_end_io = end_clone_bio;
1585 bio->bi_private = info;
1586 bio->bi_destructor = dm_rq_bio_destructor;
1587
1588 return 0;
1589}
1590
1591static int setup_clone(struct request *clone, struct request *rq,
1592 struct dm_rq_target_io *tio)
1593{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001594 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001595
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001596 if (dm_rq_is_flush_request(rq)) {
1597 blk_rq_init(NULL, clone);
1598 clone->cmd_type = REQ_TYPE_FS;
1599 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1600 } else {
1601 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1602 dm_rq_bio_constructor, tio);
1603 if (r)
1604 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001605
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001606 clone->cmd = rq->cmd;
1607 clone->cmd_len = rq->cmd_len;
1608 clone->sense = rq->sense;
1609 clone->buffer = rq->buffer;
1610 }
1611
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001612 clone->end_io = end_clone_request;
1613 clone->end_io_data = tio;
1614
1615 return 0;
1616}
1617
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001618static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1619 gfp_t gfp_mask)
1620{
1621 struct request *clone;
1622 struct dm_rq_target_io *tio;
1623
1624 tio = alloc_rq_tio(md, gfp_mask);
1625 if (!tio)
1626 return NULL;
1627
1628 tio->md = md;
1629 tio->ti = NULL;
1630 tio->orig = rq;
1631 tio->error = 0;
1632 memset(&tio->info, 0, sizeof(tio->info));
1633
1634 clone = &tio->clone;
1635 if (setup_clone(clone, rq, tio)) {
1636 /* -ENOMEM */
1637 free_rq_tio(tio);
1638 return NULL;
1639 }
1640
1641 return clone;
1642}
1643
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001644/*
1645 * Called with the queue lock held.
1646 */
1647static int dm_prep_fn(struct request_queue *q, struct request *rq)
1648{
1649 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001650 struct request *clone;
1651
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001652 if (unlikely(dm_rq_is_flush_request(rq)))
1653 return BLKPREP_OK;
1654
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001655 if (unlikely(rq->special)) {
1656 DMWARN("Already has something in rq->special.");
1657 return BLKPREP_KILL;
1658 }
1659
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001660 clone = clone_rq(rq, md, GFP_ATOMIC);
1661 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001662 return BLKPREP_DEFER;
1663
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001664 rq->special = clone;
1665 rq->cmd_flags |= REQ_DONTPREP;
1666
1667 return BLKPREP_OK;
1668}
1669
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001670/*
1671 * Returns:
1672 * 0 : the request has been processed (not requeued)
1673 * !0 : the request has been requeued
1674 */
1675static int map_request(struct dm_target *ti, struct request *clone,
1676 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001677{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001678 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001679 struct dm_rq_target_io *tio = clone->end_io_data;
1680
1681 /*
1682 * Hold the md reference here for the in-flight I/O.
1683 * We can't rely on the reference count by device opener,
1684 * because the device may be closed during the request completion
1685 * when all bios are completed.
1686 * See the comment in rq_completed() too.
1687 */
1688 dm_get(md);
1689
1690 tio->ti = ti;
1691 r = ti->type->map_rq(ti, clone, &tio->info);
1692 switch (r) {
1693 case DM_MAPIO_SUBMITTED:
1694 /* The target has taken the I/O to submit by itself later */
1695 break;
1696 case DM_MAPIO_REMAPPED:
1697 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001698 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1699 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001700 dm_dispatch_request(clone);
1701 break;
1702 case DM_MAPIO_REQUEUE:
1703 /* The target wants to requeue the I/O */
1704 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001705 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001706 break;
1707 default:
1708 if (r > 0) {
1709 DMWARN("unimplemented target map return value: %d", r);
1710 BUG();
1711 }
1712
1713 /* The target wants to complete the I/O */
1714 dm_kill_unmapped_request(clone, r);
1715 break;
1716 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001717
1718 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001719}
1720
1721/*
1722 * q->request_fn for request-based dm.
1723 * Called with the queue lock held.
1724 */
1725static void dm_request_fn(struct request_queue *q)
1726{
1727 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001728 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001729 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001730 struct request *rq, *clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001731
1732 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001733 * For suspend, check blk_queue_stopped() and increment
1734 * ->pending within a single queue_lock not to increment the
1735 * number of in-flight I/Os after the queue is stopped in
1736 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001737 */
1738 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1739 rq = blk_peek_request(q);
1740 if (!rq)
1741 goto plug_and_out;
1742
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001743 if (unlikely(dm_rq_is_flush_request(rq))) {
1744 BUG_ON(md->flush_request);
1745 md->flush_request = rq;
1746 blk_start_request(rq);
1747 queue_work(md->wq, &md->barrier_work);
1748 goto out;
1749 }
1750
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001751 ti = dm_table_find_target(map, blk_rq_pos(rq));
1752 if (ti->type->busy && ti->type->busy(ti))
1753 goto plug_and_out;
1754
1755 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001756 clone = rq->special;
1757 atomic_inc(&md->pending[rq_data_dir(clone)]);
1758
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001759 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001760 if (map_request(ti, clone, md))
1761 goto requeued;
1762
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001763 spin_lock_irq(q->queue_lock);
1764 }
1765
1766 goto out;
1767
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001768requeued:
1769 spin_lock_irq(q->queue_lock);
1770
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001771plug_and_out:
1772 if (!elv_queue_empty(q))
1773 /* Some requests still remain, retry later */
1774 blk_plug_device(q);
1775
1776out:
1777 dm_table_put(map);
1778
1779 return;
1780}
1781
1782int dm_underlying_device_busy(struct request_queue *q)
1783{
1784 return blk_lld_busy(q);
1785}
1786EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1787
1788static int dm_lld_busy(struct request_queue *q)
1789{
1790 int r;
1791 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001792 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001793
1794 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1795 r = 1;
1796 else
1797 r = dm_table_any_busy_target(map);
1798
1799 dm_table_put(map);
1800
1801 return r;
1802}
1803
Jens Axboe165125e2007-07-24 09:28:11 +02001804static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001807 struct dm_table *map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001810 if (dm_request_based(md))
1811 generic_unplug_device(q);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 dm_table_unplug_all(map);
1814 dm_table_put(map);
1815 }
1816}
1817
1818static int dm_any_congested(void *congested_data, int bdi_bits)
1819{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001820 int r = bdi_bits;
1821 struct mapped_device *md = congested_data;
1822 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001824 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001825 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001826 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001827 /*
1828 * Request-based dm cares about only own queue for
1829 * the query about congestion status of request_queue
1830 */
1831 if (dm_request_based(md))
1832 r = md->queue->backing_dev_info.state &
1833 bdi_bits;
1834 else
1835 r = dm_table_any_congested(map, bdi_bits);
1836
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001837 dm_table_put(map);
1838 }
1839 }
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return r;
1842}
1843
1844/*-----------------------------------------------------------------
1845 * An IDR is used to keep track of allocated minor numbers.
1846 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847static DEFINE_IDR(_minor_idr);
1848
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001849static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001851 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001853 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856/*
1857 * See if the device with a specific minor # is free.
1858 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001859static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 int r, m;
1862
1863 if (minor >= (1 << MINORBITS))
1864 return -EINVAL;
1865
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001866 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1867 if (!r)
1868 return -ENOMEM;
1869
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001870 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 if (idr_find(&_minor_idr, minor)) {
1873 r = -EBUSY;
1874 goto out;
1875 }
1876
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001877 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001878 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 if (m != minor) {
1882 idr_remove(&_minor_idr, m);
1883 r = -EBUSY;
1884 goto out;
1885 }
1886
1887out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001888 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return r;
1890}
1891
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001892static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001894 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001897 if (!r)
1898 return -ENOMEM;
1899
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001900 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001902 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001903 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 if (m >= (1 << MINORBITS)) {
1907 idr_remove(&_minor_idr, m);
1908 r = -ENOSPC;
1909 goto out;
1910 }
1911
1912 *minor = m;
1913
1914out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001915 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return r;
1917}
1918
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001919static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Mikulas Patocka53d59142009-04-02 19:55:37 +01001921static void dm_wq_work(struct work_struct *work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001922static void dm_rq_barrier_work(struct work_struct *work);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001923
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001924static void dm_init_md_queue(struct mapped_device *md)
1925{
1926 /*
1927 * Request-based dm devices cannot be stacked on top of bio-based dm
1928 * devices. The type of this dm device has not been decided yet.
1929 * The type is decided at the first table loading time.
1930 * To prevent problematic device stacking, clear the queue flag
1931 * for request stacking support until then.
1932 *
1933 * This queue is new, so no concurrency on the queue_flags.
1934 */
1935 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1936
1937 md->queue->queuedata = md;
1938 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1939 md->queue->backing_dev_info.congested_data = md;
1940 blk_queue_make_request(md->queue, dm_request);
1941 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1942 md->queue->unplug_fn = dm_unplug_all;
1943 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1944}
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946/*
1947 * Allocate and initialise a blank device with a given minor.
1948 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001949static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001952 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001953 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 if (!md) {
1956 DMWARN("unable to allocate device, out of memory.");
1957 return NULL;
1958 }
1959
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001960 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001961 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001964 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001965 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001966 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001967 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001969 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Mike Snitzera5664da2010-08-12 04:14:01 +01001971 md->type = DM_TYPE_NONE;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001972 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001973 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001974 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001975 spin_lock_init(&md->deferred_lock);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001976 spin_lock_init(&md->barrier_error_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 rwlock_init(&md->map_lock);
1978 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001979 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001981 atomic_set(&md->uevent_seq, 0);
1982 INIT_LIST_HEAD(&md->uevent_list);
1983 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001985 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001987 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001989 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 md->disk = alloc_disk(1);
1992 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001993 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001995 atomic_set(&md->pending[0], 0);
1996 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001997 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001998 INIT_WORK(&md->work, dm_wq_work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001999 INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07002000 init_waitqueue_head(&md->eventq);
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 md->disk->major = _major;
2003 md->disk->first_minor = minor;
2004 md->disk->fops = &dm_blk_dops;
2005 md->disk->queue = md->queue;
2006 md->disk->private_data = md;
2007 sprintf(md->disk->disk_name, "dm-%d", minor);
2008 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002009 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Milan Broz304f3f62008-02-08 02:11:17 +00002011 md->wq = create_singlethread_workqueue("kdmflush");
2012 if (!md->wq)
2013 goto bad_thread;
2014
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002015 md->bdev = bdget_disk(md->disk, 0);
2016 if (!md->bdev)
2017 goto bad_bdev;
2018
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002019 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002020 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002021 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002022 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002023
2024 BUG_ON(old_md != MINOR_ALLOCED);
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return md;
2027
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002028bad_bdev:
2029 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00002030bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01002031 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00002032 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002033bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05002034 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002035bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002037bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002038 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002039bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 kfree(md);
2041 return NULL;
2042}
2043
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002044static void unlock_fs(struct mapped_device *md);
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046static void free_dev(struct mapped_device *md)
2047{
Tejun Heof331c022008-09-03 09:01:48 +02002048 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002049
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002050 unlock_fs(md);
2051 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00002052 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002053 if (md->tio_pool)
2054 mempool_destroy(md->tio_pool);
2055 if (md->io_pool)
2056 mempool_destroy(md->io_pool);
2057 if (md->bs)
2058 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01002059 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002061 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002062
2063 spin_lock(&_minor_lock);
2064 md->disk->private_data = NULL;
2065 spin_unlock(&_minor_lock);
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05002068 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002069 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 kfree(md);
2071}
2072
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002073static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2074{
2075 struct dm_md_mempools *p;
2076
2077 if (md->io_pool && md->tio_pool && md->bs)
2078 /* the md already has necessary mempools */
2079 goto out;
2080
2081 p = dm_table_get_md_mempools(t);
2082 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
2083
2084 md->io_pool = p->io_pool;
2085 p->io_pool = NULL;
2086 md->tio_pool = p->tio_pool;
2087 p->tio_pool = NULL;
2088 md->bs = p->bs;
2089 p->bs = NULL;
2090
2091out:
2092 /* mempool bind completed, now no need any mempools in the table */
2093 dm_table_free_md_mempools(t);
2094}
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096/*
2097 * Bind a table to the device.
2098 */
2099static void event_callback(void *context)
2100{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002101 unsigned long flags;
2102 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct mapped_device *md = (struct mapped_device *) context;
2104
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002105 spin_lock_irqsave(&md->uevent_lock, flags);
2106 list_splice_init(&md->uevent_list, &uevents);
2107 spin_unlock_irqrestore(&md->uevent_lock, flags);
2108
Tejun Heoed9e1982008-08-25 19:56:05 +09002109 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 atomic_inc(&md->event_nr);
2112 wake_up(&md->eventq);
2113}
2114
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002115static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002117 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002119 mutex_lock(&md->bdev->bd_inode->i_mutex);
2120 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2121 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002124/*
2125 * Returns old map, which caller must destroy.
2126 */
2127static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2128 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002130 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002131 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002133 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002136
2137 /*
2138 * Wipe any geometry if the size of the table changed.
2139 */
2140 if (size != get_capacity(md->disk))
2141 memset(&md->geometry, 0, sizeof(md->geometry));
2142
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002143 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002145 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002146
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002147 /*
2148 * The queue hasn't been stopped yet, if the old table type wasn't
2149 * for request-based during suspension. So stop it to prevent
2150 * I/O mapping before resume.
2151 * This must be done before setting the queue restrictions,
2152 * because request-based dm may be run just after the setting.
2153 */
2154 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2155 stop_queue(q);
2156
2157 __bind_mempools(md, t);
2158
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002159 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002160 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002161 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002162 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002163 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002164
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002165 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Alasdair G Kergona7940152009-12-10 23:52:23 +00002168/*
2169 * Returns unbound table for the caller to free.
2170 */
2171static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002174 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002177 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002180 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002182 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002183
2184 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
2187/*
2188 * Constructor for a new device.
2189 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002190int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 struct mapped_device *md;
2193
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002194 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (!md)
2196 return -ENXIO;
2197
Milan Broz784aae72009-01-06 03:05:12 +00002198 dm_sysfs_init(md);
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 *result = md;
2201 return 0;
2202}
2203
Mike Snitzera5664da2010-08-12 04:14:01 +01002204/*
2205 * Functions to manage md->type.
2206 * All are required to hold md->type_lock.
2207 */
2208void dm_lock_md_type(struct mapped_device *md)
2209{
2210 mutex_lock(&md->type_lock);
2211}
2212
2213void dm_unlock_md_type(struct mapped_device *md)
2214{
2215 mutex_unlock(&md->type_lock);
2216}
2217
2218void dm_set_md_type(struct mapped_device *md, unsigned type)
2219{
2220 md->type = type;
2221}
2222
2223unsigned dm_get_md_type(struct mapped_device *md)
2224{
2225 return md->type;
2226}
2227
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002228/*
2229 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2230 */
2231static int dm_init_request_based_queue(struct mapped_device *md)
2232{
2233 struct request_queue *q = NULL;
2234
2235 if (md->queue->elevator)
2236 return 1;
2237
2238 /* Fully initialize the queue */
2239 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2240 if (!q)
2241 return 0;
2242
2243 md->queue = q;
2244 md->saved_make_request_fn = md->queue->make_request_fn;
2245 dm_init_md_queue(md);
2246 blk_queue_softirq_done(md->queue, dm_softirq_done);
2247 blk_queue_prep_rq(md->queue, dm_prep_fn);
2248 blk_queue_lld_busy(md->queue, dm_lld_busy);
2249 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH);
2250
2251 elv_register_queue(md->queue);
2252
2253 return 1;
2254}
2255
2256/*
2257 * Setup the DM device's queue based on md's type
2258 */
2259int dm_setup_md_queue(struct mapped_device *md)
2260{
2261 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2262 !dm_init_request_based_queue(md)) {
2263 DMWARN("Cannot initialize queue for request-based mapped device");
2264 return -EINVAL;
2265 }
2266
2267 return 0;
2268}
2269
David Teigland637842c2006-01-06 00:20:00 -08002270static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 unsigned minor = MINOR(dev);
2274
2275 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2276 return NULL;
2277
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002278 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002281 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002282 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002283 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002284 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002285 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002286 goto out;
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002289out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002290 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
David Teigland637842c2006-01-06 00:20:00 -08002292 return md;
2293}
2294
David Teiglandd229a952006-01-06 00:20:01 -08002295struct mapped_device *dm_get_md(dev_t dev)
2296{
2297 struct mapped_device *md = dm_find_md(dev);
2298
2299 if (md)
2300 dm_get(md);
2301
2302 return md;
2303}
2304
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002305void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002306{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002307 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
2310void dm_set_mdptr(struct mapped_device *md, void *ptr)
2311{
2312 md->interface_ptr = ptr;
2313}
2314
2315void dm_get(struct mapped_device *md)
2316{
2317 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002318 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002321const char *dm_device_name(struct mapped_device *md)
2322{
2323 return md->name;
2324}
2325EXPORT_SYMBOL_GPL(dm_device_name);
2326
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002327static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002329 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002331 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002332
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002333 spin_lock(&_minor_lock);
2334 map = dm_get_live_table(md);
2335 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2336 set_bit(DMF_FREEING, &md->flags);
2337 spin_unlock(&_minor_lock);
2338
2339 if (!dm_suspended_md(md)) {
2340 dm_table_presuspend_targets(map);
2341 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002343
2344 /*
2345 * Rare, but there may be I/O requests still going to complete,
2346 * for example. Wait for all references to disappear.
2347 * No one should increment the reference count of the mapped_device,
2348 * after the mapped_device state becomes DMF_FREEING.
2349 */
2350 if (wait)
2351 while (atomic_read(&md->holders))
2352 msleep(1);
2353 else if (atomic_read(&md->holders))
2354 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2355 dm_device_name(md), atomic_read(&md->holders));
2356
2357 dm_sysfs_exit(md);
2358 dm_table_put(map);
2359 dm_table_destroy(__unbind(md));
2360 free_dev(md);
2361}
2362
2363void dm_destroy(struct mapped_device *md)
2364{
2365 __dm_destroy(md, true);
2366}
2367
2368void dm_destroy_immediate(struct mapped_device *md)
2369{
2370 __dm_destroy(md, false);
2371}
2372
2373void dm_put(struct mapped_device *md)
2374{
2375 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
Edward Goggin79eb8852007-05-09 02:32:56 -07002377EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Mikulas Patocka401600d2009-04-02 19:55:38 +01002379static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002380{
2381 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002382 DECLARE_WAITQUEUE(wait, current);
2383
2384 dm_unplug_all(md->queue);
2385
2386 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002387
2388 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002389 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002390
2391 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002392 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002393 break;
2394
Mikulas Patocka401600d2009-04-02 19:55:38 +01002395 if (interruptible == TASK_INTERRUPTIBLE &&
2396 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002397 r = -EINTR;
2398 break;
2399 }
2400
2401 io_schedule();
2402 }
2403 set_current_state(TASK_RUNNING);
2404
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002405 remove_wait_queue(&md->wait, &wait);
2406
Milan Broz46125c12008-02-08 02:10:30 +00002407 return r;
2408}
2409
Mikulas Patocka531fe962009-06-22 10:12:17 +01002410static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002411{
2412 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002413
2414 bio_init(&md->barrier_bio);
2415 md->barrier_bio.bi_bdev = md->bdev;
2416 md->barrier_bio.bi_rw = WRITE_BARRIER;
2417 __split_and_process_bio(md, &md->barrier_bio);
2418
2419 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002420}
2421
2422static void process_barrier(struct mapped_device *md, struct bio *bio)
2423{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002424 md->barrier_error = 0;
2425
Mikulas Patocka531fe962009-06-22 10:12:17 +01002426 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002427
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002428 if (!bio_empty_barrier(bio)) {
2429 __split_and_process_bio(md, bio);
Mikulas Patocka708e9292010-08-12 04:14:00 +01002430 /*
2431 * If the request isn't supported, don't waste time with
2432 * the second flush.
2433 */
2434 if (md->barrier_error != -EOPNOTSUPP)
2435 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002436 }
2437
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002438 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002439 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002440 else {
2441 spin_lock_irq(&md->deferred_lock);
2442 bio_list_add_head(&md->deferred, bio);
2443 spin_unlock_irq(&md->deferred_lock);
2444 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447/*
2448 * Process the deferred bios
2449 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002450static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Mikulas Patockaef208582009-04-02 19:55:38 +01002452 struct mapped_device *md = container_of(work, struct mapped_device,
2453 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002454 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Mikulas Patockaef208582009-04-02 19:55:38 +01002456 down_write(&md->io_lock);
2457
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002458 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002459 spin_lock_irq(&md->deferred_lock);
2460 c = bio_list_pop(&md->deferred);
2461 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002462
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002463 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002464 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002465 break;
2466 }
2467
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002468 up_write(&md->io_lock);
2469
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002470 if (dm_request_based(md))
2471 generic_make_request(c);
2472 else {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002473 if (c->bi_rw & REQ_HARDBARRIER)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002474 process_barrier(md, c);
2475 else
2476 __split_and_process_bio(md, c);
2477 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002478
2479 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002480 }
Milan Broz73d410c2008-02-08 02:10:25 +00002481
Mikulas Patockaef208582009-04-02 19:55:38 +01002482 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002485static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002486{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002487 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2488 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002489 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002490}
2491
Mike Snitzer57cba5d2010-08-12 04:14:04 +01002492static void dm_rq_set_target_request_nr(struct request *clone, unsigned request_nr)
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002493{
2494 struct dm_rq_target_io *tio = clone->end_io_data;
2495
Mike Snitzer57cba5d2010-08-12 04:14:04 +01002496 tio->info.target_request_nr = request_nr;
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002497}
2498
2499/* Issue barrier requests to targets and wait for their completion. */
2500static int dm_rq_barrier(struct mapped_device *md)
2501{
2502 int i, j;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002503 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002504 unsigned num_targets = dm_table_get_num_targets(map);
2505 struct dm_target *ti;
2506 struct request *clone;
2507
2508 md->barrier_error = 0;
2509
2510 for (i = 0; i < num_targets; i++) {
2511 ti = dm_table_get_target(map, i);
2512 for (j = 0; j < ti->num_flush_requests; j++) {
2513 clone = clone_rq(md->flush_request, md, GFP_NOIO);
Mike Snitzer57cba5d2010-08-12 04:14:04 +01002514 dm_rq_set_target_request_nr(clone, j);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002515 atomic_inc(&md->pending[rq_data_dir(clone)]);
2516 map_request(ti, clone, md);
2517 }
2518 }
2519
2520 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2521 dm_table_put(map);
2522
2523 return md->barrier_error;
2524}
2525
2526static void dm_rq_barrier_work(struct work_struct *work)
2527{
2528 int error;
2529 struct mapped_device *md = container_of(work, struct mapped_device,
2530 barrier_work);
2531 struct request_queue *q = md->queue;
2532 struct request *rq;
2533 unsigned long flags;
2534
2535 /*
2536 * Hold the md reference here and leave it at the last part so that
2537 * the md can't be deleted by device opener when the barrier request
2538 * completes.
2539 */
2540 dm_get(md);
2541
2542 error = dm_rq_barrier(md);
2543
2544 rq = md->flush_request;
2545 md->flush_request = NULL;
2546
2547 if (error == DM_ENDIO_REQUEUE) {
2548 spin_lock_irqsave(q->queue_lock, flags);
2549 blk_requeue_request(q, rq);
2550 spin_unlock_irqrestore(q->queue_lock, flags);
2551 } else
2552 blk_end_request_all(rq, error);
2553
2554 blk_run_queue(q);
2555
2556 dm_put(md);
2557}
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002560 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002562struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002564 struct dm_table *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002565 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002566 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Daniel Walkere61290a2008-02-08 02:10:08 +00002568 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002571 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002574 r = dm_calculate_queue_limits(table, &limits);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002575 if (r) {
2576 map = ERR_PTR(r);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002577 goto out;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002578 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002579
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002580 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002582out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002583 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002584 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
2587/*
2588 * Functions to lock and unlock any filesystem running on the
2589 * device.
2590 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002591static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002593 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002596
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002597 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002598 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002599 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002600 md->frozen_sb = NULL;
2601 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002602 }
2603
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002604 set_bit(DMF_FROZEN, &md->flags);
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return 0;
2607}
2608
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002609static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002611 if (!test_bit(DMF_FROZEN, &md->flags))
2612 return;
2613
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002614 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002616 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619/*
2620 * We need to be able to change a mapping table under a mounted
2621 * filesystem. For example we might want to move some data in
2622 * the background. Before the table can be swapped with
2623 * dm_bind_table, dm_suspend must be called to flush any in
2624 * flight bios and ensure that any further io gets deferred.
2625 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002626/*
2627 * Suspend mechanism in request-based dm.
2628 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002629 * 1. Flush all I/Os by lock_fs() if needed.
2630 * 2. Stop dispatching any I/O by stopping the request_queue.
2631 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002632 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002633 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002634 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002635int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002637 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002638 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002639 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002640 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Daniel Walkere61290a2008-02-08 02:10:08 +00002642 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002643
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002644 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002645 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002646 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002649 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002651 /*
2652 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2653 * This flag is cleared before dm_suspend returns.
2654 */
2655 if (noflush)
2656 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2657
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002658 /* This does not get reverted if there's an error later. */
2659 dm_table_presuspend_targets(map);
2660
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002661 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002662 * Flush I/O to the device.
2663 * Any I/O submitted after lock_fs() may not be flushed.
2664 * noflush takes precedence over do_lockfs.
2665 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002666 */
2667 if (!noflush && do_lockfs) {
2668 r = lock_fs(md);
2669 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002670 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002674 * Here we must make sure that no processes are submitting requests
2675 * to target drivers i.e. no one may be executing
2676 * __split_and_process_bio. This is called from dm_request and
2677 * dm_wq_work.
2678 *
2679 * To get all processes out of __split_and_process_bio in dm_request,
2680 * we take the write lock. To prevent any process from reentering
2681 * __split_and_process_bio from dm_request, we set
2682 * DMF_QUEUE_IO_TO_THREAD.
2683 *
2684 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2685 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2686 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2687 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002689 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002690 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2691 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002692 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002694 /*
2695 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2696 * can be kicked until md->queue is stopped. So stop md->queue before
2697 * flushing md->wq.
2698 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002699 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002700 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002701
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002702 flush_workqueue(md->wq);
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002705 * At this point no more requests are entering target request routines.
2706 * We call dm_wait_for_completion to wait for all existing requests
2707 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002709 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002711 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002712 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002713 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002714 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002717 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002718 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002719
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002720 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002721 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002722
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002723 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002724 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002725 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002726
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002727 /*
2728 * If dm_wait_for_completion returned 0, the device is completely
2729 * quiescent now. There is no request-processing activity. All new
2730 * requests are being added to md->deferred list.
2731 */
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 set_bit(DMF_SUSPENDED, &md->flags);
2734
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002735 dm_table_postsuspend_targets(map);
2736
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002737out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002739
2740out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002741 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002742 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
2745int dm_resume(struct mapped_device *md)
2746{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002747 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002748 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Daniel Walkere61290a2008-02-08 02:10:08 +00002750 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002751 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002752 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002753
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002754 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002755 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002756 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Milan Broz8757b772006-10-03 01:15:36 -07002758 r = dm_table_resume_targets(map);
2759 if (r)
2760 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002761
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002762 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002763
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002764 /*
2765 * Flushing deferred I/Os must be done after targets are resumed
2766 * so that mapping of targets can work correctly.
2767 * Request-based dm is queueing the deferred I/Os in its request_queue.
2768 */
2769 if (dm_request_based(md))
2770 start_queue(md->queue);
2771
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002772 unlock_fs(md);
2773
2774 clear_bit(DMF_SUSPENDED, &md->flags);
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002777 r = 0;
2778out:
2779 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002780 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002781
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002782 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
2785/*-----------------------------------------------------------------
2786 * Event notification.
2787 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002788int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002789 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002790{
Milan Broz60935eb2009-06-22 10:12:30 +01002791 char udev_cookie[DM_COOKIE_LENGTH];
2792 char *envp[] = { udev_cookie, NULL };
2793
2794 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002795 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002796 else {
2797 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2798 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002799 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2800 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002801 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002802}
2803
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002804uint32_t dm_next_uevent_seq(struct mapped_device *md)
2805{
2806 return atomic_add_return(1, &md->uevent_seq);
2807}
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809uint32_t dm_get_event_nr(struct mapped_device *md)
2810{
2811 return atomic_read(&md->event_nr);
2812}
2813
2814int dm_wait_event(struct mapped_device *md, int event_nr)
2815{
2816 return wait_event_interruptible(md->eventq,
2817 (event_nr != atomic_read(&md->event_nr)));
2818}
2819
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002820void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2821{
2822 unsigned long flags;
2823
2824 spin_lock_irqsave(&md->uevent_lock, flags);
2825 list_add(elist, &md->uevent_list);
2826 spin_unlock_irqrestore(&md->uevent_lock, flags);
2827}
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829/*
2830 * The gendisk is only valid as long as you have a reference
2831 * count on 'md'.
2832 */
2833struct gendisk *dm_disk(struct mapped_device *md)
2834{
2835 return md->disk;
2836}
2837
Milan Broz784aae72009-01-06 03:05:12 +00002838struct kobject *dm_kobject(struct mapped_device *md)
2839{
2840 return &md->kobj;
2841}
2842
2843/*
2844 * struct mapped_device should not be exported outside of dm.c
2845 * so use this check to verify that kobj is part of md structure
2846 */
2847struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2848{
2849 struct mapped_device *md;
2850
2851 md = container_of(kobj, struct mapped_device, kobj);
2852 if (&md->kobj != kobj)
2853 return NULL;
2854
Milan Broz4d89b7b2009-06-22 10:12:11 +01002855 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002856 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002857 return NULL;
2858
Milan Broz784aae72009-01-06 03:05:12 +00002859 dm_get(md);
2860 return md;
2861}
2862
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002863int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 return test_bit(DMF_SUSPENDED, &md->flags);
2866}
2867
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002868int dm_suspended(struct dm_target *ti)
2869{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002870 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002871}
2872EXPORT_SYMBOL_GPL(dm_suspended);
2873
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002874int dm_noflush_suspending(struct dm_target *ti)
2875{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002876 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002877}
2878EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2879
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002880struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2881{
2882 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2883
2884 if (!pools)
2885 return NULL;
2886
2887 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2888 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2889 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2890 if (!pools->io_pool)
2891 goto free_pools_and_out;
2892
2893 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2894 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2895 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2896 if (!pools->tio_pool)
2897 goto free_io_pool_and_out;
2898
2899 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2900 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2901 if (!pools->bs)
2902 goto free_tio_pool_and_out;
2903
2904 return pools;
2905
2906free_tio_pool_and_out:
2907 mempool_destroy(pools->tio_pool);
2908
2909free_io_pool_and_out:
2910 mempool_destroy(pools->io_pool);
2911
2912free_pools_and_out:
2913 kfree(pools);
2914
2915 return NULL;
2916}
2917
2918void dm_free_md_mempools(struct dm_md_mempools *pools)
2919{
2920 if (!pools)
2921 return;
2922
2923 if (pools->io_pool)
2924 mempool_destroy(pools->io_pool);
2925
2926 if (pools->tio_pool)
2927 mempool_destroy(pools->tio_pool);
2928
2929 if (pools->bs)
2930 bioset_free(pools->bs);
2931
2932 kfree(pools);
2933}
2934
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002935static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 .open = dm_blk_open,
2937 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002938 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002939 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 .owner = THIS_MODULE
2941};
2942
2943EXPORT_SYMBOL(dm_get_mapinfo);
2944
2945/*
2946 * module hooks
2947 */
2948module_init(dm_init);
2949module_exit(dm_exit);
2950
2951module_param(major, uint, 0);
2952MODULE_PARM_DESC(major, "The major number of the device mapper");
2953MODULE_DESCRIPTION(DM_NAME " driver");
2954MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2955MODULE_LICENSE("GPL");