blob: a42dfb7a718ee54dc009e22797a5b9a543ce3e35 [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>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080021#include <linux/hdreg.h>
Li Zefan55782132009-06-09 13:43:05 +080022
23#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Milan Broz60935eb2009-06-22 10:12:30 +010027/*
28 * Cookies are numeric values sent with CHANGE and REMOVE
29 * uevents while resuming, removing or renaming the device.
30 */
31#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
32#define DM_COOKIE_LENGTH 24
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static const char *_name = DM_NAME;
35
36static unsigned int major = 0;
37static unsigned int _major = 0;
38
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070039static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000041 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * One of these is allocated per bio.
43 */
44struct dm_io {
45 struct mapped_device *md;
46 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010048 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080049 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010050 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000054 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * One of these is allocated per target within a bio. Hopefully
56 * this will be simplified out one day.
57 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010058struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct dm_io *io;
60 struct dm_target *ti;
61 union map_info info;
62};
63
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000064/*
65 * For request-based dm.
66 * One of these is allocated per request.
67 */
68struct dm_rq_target_io {
69 struct mapped_device *md;
70 struct dm_target *ti;
71 struct request *orig, clone;
72 int error;
73 union map_info info;
74};
75
76/*
77 * For request-based dm.
78 * One of these is allocated per bio.
79 */
80struct dm_rq_clone_bio_info {
81 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010082 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000083};
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085union map_info *dm_get_mapinfo(struct bio *bio)
86{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070087 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010088 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070089 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010092union map_info *dm_get_rq_mapinfo(struct request *rq)
93{
94 if (rq && rq->end_io_data)
95 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
96 return NULL;
97}
98EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
99
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700100#define MINOR_ALLOCED ((void *)-1)
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * Bits for the md->flags field.
104 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100105#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800107#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700108#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700109#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800110#define DMF_NOFLUSH_SUSPENDING 5
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100111#define DMF_QUEUE_IO_TO_THREAD 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Milan Broz304f3f62008-02-08 02:11:17 +0000113/*
114 * Work processed by per-device workqueue.
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700117 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000118 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 rwlock_t map_lock;
120 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700121 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 unsigned long flags;
124
Jens Axboe165125e2007-07-24 09:28:11 +0200125 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800127 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 void *interface_ptr;
130
131 /*
132 * A list of ios that arrived while we were suspended.
133 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200134 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100136 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800137 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100138 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /*
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100141 * An error from the barrier request currently being processed.
142 */
143 int barrier_error;
144
145 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000146 * Processing queue (flush/barriers)
147 */
148 struct workqueue_struct *wq;
149
150 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * The current mapping.
152 */
153 struct dm_table *map;
154
155 /*
156 * io objects are allocated from here.
157 */
158 mempool_t *io_pool;
159 mempool_t *tio_pool;
160
Stefan Bader9faf4002006-10-03 01:15:41 -0700161 struct bio_set *bs;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /*
164 * Event handling.
165 */
166 atomic_t event_nr;
167 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100168 atomic_t uevent_seq;
169 struct list_head uevent_list;
170 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /*
173 * freeze/thaw support require holding onto a super block
174 */
175 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100176 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800177
178 /* forced geometry settings */
179 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000180
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100181 /* marker of flush suspend for request-based dm */
182 struct request suspend_rq;
183
184 /* For saving the address of __make_request for request based dm */
185 make_request_fn *saved_make_request_fn;
186
Milan Broz784aae72009-01-06 03:05:12 +0000187 /* sysfs handle */
188 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100189
190 /* zero-length barrier that will be cloned and submitted to targets */
191 struct bio barrier_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100194/*
195 * For mempools pre-allocation at the table loading time.
196 */
197struct dm_md_mempools {
198 mempool_t *io_pool;
199 mempool_t *tio_pool;
200 struct bio_set *bs;
201};
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800204static struct kmem_cache *_io_cache;
205static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000206static struct kmem_cache *_rq_tio_cache;
207static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int __init local_init(void)
210{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100211 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100214 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100216 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100219 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100220 if (!_tio_cache)
221 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000223 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
224 if (!_rq_tio_cache)
225 goto out_free_tio_cache;
226
227 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
228 if (!_rq_bio_info_cache)
229 goto out_free_rq_tio_cache;
230
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100231 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100232 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000233 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 _major = major;
236 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100237 if (r < 0)
238 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (!_major)
241 _major = r;
242
243 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100244
245out_uevent_exit:
246 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000247out_free_rq_bio_info_cache:
248 kmem_cache_destroy(_rq_bio_info_cache);
249out_free_rq_tio_cache:
250 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100251out_free_tio_cache:
252 kmem_cache_destroy(_tio_cache);
253out_free_io_cache:
254 kmem_cache_destroy(_io_cache);
255
256 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259static void local_exit(void)
260{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000261 kmem_cache_destroy(_rq_bio_info_cache);
262 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kmem_cache_destroy(_tio_cache);
264 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700265 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100266 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 _major = 0;
269
270 DMINFO("cleaned up");
271}
272
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000273static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 local_init,
275 dm_target_init,
276 dm_linear_init,
277 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000278 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100279 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dm_interface_init,
281};
282
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000283static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 local_exit,
285 dm_target_exit,
286 dm_linear_exit,
287 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000288 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100289 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 dm_interface_exit,
291};
292
293static int __init dm_init(void)
294{
295 const int count = ARRAY_SIZE(_inits);
296
297 int r, i;
298
299 for (i = 0; i < count; i++) {
300 r = _inits[i]();
301 if (r)
302 goto bad;
303 }
304
305 return 0;
306
307 bad:
308 while (i--)
309 _exits[i]();
310
311 return r;
312}
313
314static void __exit dm_exit(void)
315{
316 int i = ARRAY_SIZE(_exits);
317
318 while (i--)
319 _exits[i]();
320}
321
322/*
323 * Block device functions
324 */
Al Virofe5f9f22008-03-02 10:29:31 -0500325static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct mapped_device *md;
328
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700329 spin_lock(&_minor_lock);
330
Al Virofe5f9f22008-03-02 10:29:31 -0500331 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700332 if (!md)
333 goto out;
334
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700335 if (test_bit(DMF_FREEING, &md->flags) ||
336 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700337 md = NULL;
338 goto out;
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700342 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700343
344out:
345 spin_unlock(&_minor_lock);
346
347 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Al Virofe5f9f22008-03-02 10:29:31 -0500350static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Al Virofe5f9f22008-03-02 10:29:31 -0500352 struct mapped_device *md = disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700353 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 dm_put(md);
355 return 0;
356}
357
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700358int dm_open_count(struct mapped_device *md)
359{
360 return atomic_read(&md->open_count);
361}
362
363/*
364 * Guarantees nothing is using the device before it's deleted.
365 */
366int dm_lock_for_deletion(struct mapped_device *md)
367{
368 int r = 0;
369
370 spin_lock(&_minor_lock);
371
372 if (dm_open_count(md))
373 r = -EBUSY;
374 else
375 set_bit(DMF_DELETING, &md->flags);
376
377 spin_unlock(&_minor_lock);
378
379 return r;
380}
381
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800382static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
383{
384 struct mapped_device *md = bdev->bd_disk->private_data;
385
386 return dm_get_geometry(md, geo);
387}
388
Al Virofe5f9f22008-03-02 10:29:31 -0500389static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700390 unsigned int cmd, unsigned long arg)
391{
Al Virofe5f9f22008-03-02 10:29:31 -0500392 struct mapped_device *md = bdev->bd_disk->private_data;
393 struct dm_table *map = dm_get_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700394 struct dm_target *tgt;
395 int r = -ENOTTY;
396
Milan Brozaa129a22006-10-03 01:15:15 -0700397 if (!map || !dm_table_get_size(map))
398 goto out;
399
400 /* We only support devices that have a single target */
401 if (dm_table_get_num_targets(map) != 1)
402 goto out;
403
404 tgt = dm_table_get_target(map, 0);
405
406 if (dm_suspended(md)) {
407 r = -EAGAIN;
408 goto out;
409 }
410
411 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400412 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700413
414out:
415 dm_table_put(map);
416
Milan Brozaa129a22006-10-03 01:15:15 -0700417 return r;
418}
419
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100420static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 return mempool_alloc(md->io_pool, GFP_NOIO);
423}
424
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100425static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 mempool_free(io, md->io_pool);
428}
429
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100430static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 mempool_free(tio, md->tio_pool);
433}
434
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000435static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
436 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100437{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000438 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100439}
440
441static void free_rq_tio(struct dm_rq_target_io *tio)
442{
443 mempool_free(tio, tio->md->tio_pool);
444}
445
446static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
447{
448 return mempool_alloc(md->io_pool, GFP_ATOMIC);
449}
450
451static void free_bio_info(struct dm_rq_clone_bio_info *info)
452{
453 mempool_free(info, info->tio->md->io_pool);
454}
455
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000456static int md_in_flight(struct mapped_device *md)
457{
458 return atomic_read(&md->pending[READ]) +
459 atomic_read(&md->pending[WRITE]);
460}
461
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800462static void start_io_acct(struct dm_io *io)
463{
464 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900465 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200466 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800467
468 io->start_time = jiffies;
469
Tejun Heo074a7ac2008-08-25 19:56:14 +0900470 cpu = part_stat_lock();
471 part_round_stats(cpu, &dm_disk(md)->part0);
472 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200473 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800474}
475
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000476static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800477{
478 struct mapped_device *md = io->md;
479 struct bio *bio = io->bio;
480 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900481 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800482 int rw = bio_data_dir(bio);
483
Tejun Heo074a7ac2008-08-25 19:56:14 +0900484 cpu = part_stat_lock();
485 part_round_stats(cpu, &dm_disk(md)->part0);
486 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
487 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800488
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100489 /*
490 * After this is decremented the bio must not be touched if it is
491 * a barrier.
492 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200493 dm_disk(md)->part0.in_flight[rw] = pending =
494 atomic_dec_return(&md->pending[rw]);
495 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800496
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000497 /* nudge anyone waiting on suspend queue */
498 if (!pending)
499 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/*
503 * Add the bio to the list of deferred io.
504 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100505static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700507 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Mikulas Patocka022c2612009-04-02 19:55:39 +0100509 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100511 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Mikulas Patocka92c63902009-04-09 00:27:15 +0100513 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
514 queue_work(md->wq, &md->work);
515
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700516 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519/*
520 * Everyone (including functions in this file), should use this
521 * function to access the md->map field, and make sure they call
522 * dm_table_put() when finished.
523 */
524struct dm_table *dm_get_table(struct mapped_device *md)
525{
526 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100527 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100529 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 t = md->map;
531 if (t)
532 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100533 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 return t;
536}
537
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800538/*
539 * Get the geometry associated with a dm device
540 */
541int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
542{
543 *geo = md->geometry;
544
545 return 0;
546}
547
548/*
549 * Set the geometry of a device.
550 */
551int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
552{
553 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
554
555 if (geo->start > sz) {
556 DMWARN("Start sector is beyond the geometry limits.");
557 return -EINVAL;
558 }
559
560 md->geometry = *geo;
561
562 return 0;
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/*-----------------------------------------------------------------
566 * CRUD START:
567 * A more elegant soln is in the works that uses the queue
568 * merge fn, unfortunately there are a couple of changes to
569 * the block layer that I want to make for this. So in the
570 * interests of getting something for people to use I give
571 * you this clearly demarcated crap.
572 *---------------------------------------------------------------*/
573
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800574static int __noflush_suspending(struct mapped_device *md)
575{
576 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
580 * Decrements the number of outstanding ios that a bio has been
581 * cloned into, completing the original io if necc.
582 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800583static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800585 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000586 int io_error;
587 struct bio *bio;
588 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800589
590 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100591 if (unlikely(error)) {
592 spin_lock_irqsave(&io->endio_lock, flags);
593 if (!(io->error > 0 && __noflush_suspending(md)))
594 io->error = error;
595 spin_unlock_irqrestore(&io->endio_lock, flags);
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800599 if (io->error == DM_ENDIO_REQUEUE) {
600 /*
601 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800602 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100603 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100604 if (__noflush_suspending(md)) {
Jens Axboe1f98a132009-09-11 14:32:04 +0200605 if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100606 bio_list_add_head(&md->deferred,
607 io->bio);
608 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800609 /* noflush suspend was interrupted. */
610 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100611 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800612 }
613
Milan Brozb35f8ca2009-03-16 17:44:36 +0000614 io_error = io->error;
615 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100616
Jens Axboe1f98a132009-09-11 14:32:04 +0200617 if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100618 /*
619 * There can be just one barrier request so we use
620 * a per-device variable for error reporting.
621 * Note that you can't touch the bio after end_io_acct
622 */
Mikulas Patockafdb95722009-06-22 10:12:19 +0100623 if (!md->barrier_error && io_error != -EOPNOTSUPP)
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100624 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100625 end_io_acct(io);
626 } else {
627 end_io_acct(io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000628
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100629 if (io_error != DM_ENDIO_REQUEUE) {
630 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000631
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100632 bio_endio(bio, io_error);
633 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800634 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100635
636 free_io(md, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638}
639
NeilBrown6712ecf2007-09-27 12:47:43 +0200640static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100643 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000644 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700645 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 dm_endio_fn endio = tio->ti->type->end_io;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
649 error = -EIO;
650
651 if (endio) {
652 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800653 if (r < 0 || r == DM_ENDIO_REQUEUE)
654 /*
655 * error and requeue request are handled
656 * in dec_pending().
657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800659 else if (r == DM_ENDIO_INCOMPLETE)
660 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200661 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800662 else if (r) {
663 DMWARN("unimplemented target endio return value: %d", r);
664 BUG();
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
Stefan Bader9faf4002006-10-03 01:15:41 -0700668 /*
669 * Store md for cleanup instead of tio which is about to get freed.
670 */
671 bio->bi_private = md->bs;
672
Stefan Bader9faf4002006-10-03 01:15:41 -0700673 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000674 bio_put(bio);
675 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100678/*
679 * Partial completion handling for request-based dm
680 */
681static void end_clone_bio(struct bio *clone, int error)
682{
683 struct dm_rq_clone_bio_info *info = clone->bi_private;
684 struct dm_rq_target_io *tio = info->tio;
685 struct bio *bio = info->orig;
686 unsigned int nr_bytes = info->orig->bi_size;
687
688 bio_put(clone);
689
690 if (tio->error)
691 /*
692 * An error has already been detected on the request.
693 * Once error occurred, just let clone->end_io() handle
694 * the remainder.
695 */
696 return;
697 else if (error) {
698 /*
699 * Don't notice the error to the upper layer yet.
700 * The error handling decision is made by the target driver,
701 * when the request is completed.
702 */
703 tio->error = error;
704 return;
705 }
706
707 /*
708 * I/O for the bio successfully completed.
709 * Notice the data completion to the upper layer.
710 */
711
712 /*
713 * bios are processed from the head of the list.
714 * So the completing bio should always be rq->bio.
715 * If it's not, something wrong is happening.
716 */
717 if (tio->orig->bio != bio)
718 DMERR("bio completion is going in the middle of the request");
719
720 /*
721 * Update the original request.
722 * Do not use blk_end_request() here, because it may complete
723 * the original request before the clone, and break the ordering.
724 */
725 blk_update_request(tio->orig, 0, nr_bytes);
726}
727
728/*
729 * Don't touch any member of the md after calling this function because
730 * the md may be freed in dm_put() at the end of this function.
731 * Or do dm_get() before calling this function and dm_put() later.
732 */
733static void rq_completed(struct mapped_device *md, int run_queue)
734{
735 int wakeup_waiters = 0;
736 struct request_queue *q = md->queue;
737 unsigned long flags;
738
739 spin_lock_irqsave(q->queue_lock, flags);
740 if (!queue_in_flight(q))
741 wakeup_waiters = 1;
742 spin_unlock_irqrestore(q->queue_lock, flags);
743
744 /* nudge anyone waiting on suspend queue */
745 if (wakeup_waiters)
746 wake_up(&md->wait);
747
748 if (run_queue)
749 blk_run_queue(q);
750
751 /*
752 * dm_put() must be at the end of this function. See the comment above
753 */
754 dm_put(md);
755}
756
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100757static void free_rq_clone(struct request *clone)
758{
759 struct dm_rq_target_io *tio = clone->end_io_data;
760
761 blk_rq_unprep_clone(clone);
762 free_rq_tio(tio);
763}
764
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100765static void dm_unprep_request(struct request *rq)
766{
767 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100768
769 rq->special = NULL;
770 rq->cmd_flags &= ~REQ_DONTPREP;
771
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100772 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100773}
774
775/*
776 * Requeue the original request of a clone.
777 */
778void dm_requeue_unmapped_request(struct request *clone)
779{
780 struct dm_rq_target_io *tio = clone->end_io_data;
781 struct mapped_device *md = tio->md;
782 struct request *rq = tio->orig;
783 struct request_queue *q = rq->q;
784 unsigned long flags;
785
786 dm_unprep_request(rq);
787
788 spin_lock_irqsave(q->queue_lock, flags);
789 if (elv_queue_empty(q))
790 blk_plug_device(q);
791 blk_requeue_request(q, rq);
792 spin_unlock_irqrestore(q->queue_lock, flags);
793
794 rq_completed(md, 0);
795}
796EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
797
798static void __stop_queue(struct request_queue *q)
799{
800 blk_stop_queue(q);
801}
802
803static void stop_queue(struct request_queue *q)
804{
805 unsigned long flags;
806
807 spin_lock_irqsave(q->queue_lock, flags);
808 __stop_queue(q);
809 spin_unlock_irqrestore(q->queue_lock, flags);
810}
811
812static void __start_queue(struct request_queue *q)
813{
814 if (blk_queue_stopped(q))
815 blk_start_queue(q);
816}
817
818static void start_queue(struct request_queue *q)
819{
820 unsigned long flags;
821
822 spin_lock_irqsave(q->queue_lock, flags);
823 __start_queue(q);
824 spin_unlock_irqrestore(q->queue_lock, flags);
825}
826
827/*
828 * Complete the clone and the original request.
829 * Must be called without queue lock.
830 */
831static void dm_end_request(struct request *clone, int error)
832{
833 struct dm_rq_target_io *tio = clone->end_io_data;
834 struct mapped_device *md = tio->md;
835 struct request *rq = tio->orig;
836
837 if (blk_pc_request(rq)) {
838 rq->errors = clone->errors;
839 rq->resid_len = clone->resid_len;
840
841 if (rq->sense)
842 /*
843 * We are using the sense buffer of the original
844 * request.
845 * So setting the length of the sense data is enough.
846 */
847 rq->sense_len = clone->sense_len;
848 }
849
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100850 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100851
852 blk_end_request_all(rq, error);
853
854 rq_completed(md, 1);
855}
856
857/*
858 * Request completion handler for request-based dm
859 */
860static void dm_softirq_done(struct request *rq)
861{
862 struct request *clone = rq->completion_data;
863 struct dm_rq_target_io *tio = clone->end_io_data;
864 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
865 int error = tio->error;
866
867 if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
868 error = rq_end_io(tio->ti, clone, error, &tio->info);
869
870 if (error <= 0)
871 /* The target wants to complete the I/O */
872 dm_end_request(clone, error);
873 else if (error == DM_ENDIO_INCOMPLETE)
874 /* The target will handle the I/O */
875 return;
876 else if (error == DM_ENDIO_REQUEUE)
877 /* The target wants to requeue the I/O */
878 dm_requeue_unmapped_request(clone);
879 else {
880 DMWARN("unimplemented target endio return value: %d", error);
881 BUG();
882 }
883}
884
885/*
886 * Complete the clone and the original request with the error status
887 * through softirq context.
888 */
889static void dm_complete_request(struct request *clone, int error)
890{
891 struct dm_rq_target_io *tio = clone->end_io_data;
892 struct request *rq = tio->orig;
893
894 tio->error = error;
895 rq->completion_data = clone;
896 blk_complete_request(rq);
897}
898
899/*
900 * Complete the not-mapped clone and the original request with the error status
901 * through softirq context.
902 * Target's rq_end_io() function isn't called.
903 * This may be used when the target's map_rq() function fails.
904 */
905void dm_kill_unmapped_request(struct request *clone, int error)
906{
907 struct dm_rq_target_io *tio = clone->end_io_data;
908 struct request *rq = tio->orig;
909
910 rq->cmd_flags |= REQ_FAILED;
911 dm_complete_request(clone, error);
912}
913EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
914
915/*
916 * Called with the queue lock held
917 */
918static void end_clone_request(struct request *clone, int error)
919{
920 /*
921 * For just cleaning up the information of the queue in which
922 * the clone was dispatched.
923 * The clone is *NOT* freed actually here because it is alloced from
924 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
925 */
926 __blk_put_request(clone->q, clone);
927
928 /*
929 * Actual request completion is done in a softirq context which doesn't
930 * hold the queue lock. Otherwise, deadlock could occur because:
931 * - another request may be submitted by the upper level driver
932 * of the stacking during the completion
933 * - the submission which requires queue lock may be done
934 * against this queue
935 */
936 dm_complete_request(clone, error);
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static sector_t max_io_len(struct mapped_device *md,
940 sector_t sector, struct dm_target *ti)
941{
942 sector_t offset = sector - ti->begin;
943 sector_t len = ti->len - offset;
944
945 /*
946 * Does the target need to split even further ?
947 */
948 if (ti->split_io) {
949 sector_t boundary;
950 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
951 - offset;
952 if (len > boundary)
953 len = boundary;
954 }
955
956 return len;
957}
958
959static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100960 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100963 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700964 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 clone->bi_end_io = clone_endio;
967 clone->bi_private = tio;
968
969 /*
970 * Map the clone. If r == 0 we don't need to do
971 * anything, the target has assumed ownership of
972 * this io.
973 */
974 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100975 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800977 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100979
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100980 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400981 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800984 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
985 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700986 md = tio->io->md;
987 dec_pending(tio->io, r);
988 /*
989 * Store bio_set for cleanup.
990 */
991 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700993 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800994 } else if (r) {
995 DMWARN("unimplemented target map return value: %d", r);
996 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998}
999
1000struct clone_info {
1001 struct mapped_device *md;
1002 struct dm_table *map;
1003 struct bio *bio;
1004 struct dm_io *io;
1005 sector_t sector;
1006 sector_t sector_count;
1007 unsigned short idx;
1008};
1009
Peter Osterlund36763472005-09-06 15:16:42 -07001010static void dm_bio_destructor(struct bio *bio)
1011{
Stefan Bader9faf4002006-10-03 01:15:41 -07001012 struct bio_set *bs = bio->bi_private;
1013
1014 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/*
1018 * Creates a little bio that is just does part of a bvec.
1019 */
1020static struct bio *split_bvec(struct bio *bio, sector_t sector,
1021 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001022 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct bio *clone;
1025 struct bio_vec *bv = bio->bi_io_vec + idx;
1026
Stefan Bader9faf4002006-10-03 01:15:41 -07001027 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001028 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 *clone->bi_io_vec = *bv;
1030
1031 clone->bi_sector = sector;
1032 clone->bi_bdev = bio->bi_bdev;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001033 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 clone->bi_vcnt = 1;
1035 clone->bi_size = to_bytes(len);
1036 clone->bi_io_vec->bv_offset = offset;
1037 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001038 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Martin K. Petersen9c470082009-04-09 00:27:12 +01001040 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001041 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001042 bio_integrity_trim(clone,
1043 bio_sector_offset(bio, idx, offset), len);
1044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return clone;
1047}
1048
1049/*
1050 * Creates a bio that consists of range of complete bvecs.
1051 */
1052static struct bio *clone_bio(struct bio *bio, sector_t sector,
1053 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001054 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct bio *clone;
1057
Stefan Bader9faf4002006-10-03 01:15:41 -07001058 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1059 __bio_clone(clone, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001060 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
Stefan Bader9faf4002006-10-03 01:15:41 -07001061 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 clone->bi_sector = sector;
1063 clone->bi_idx = idx;
1064 clone->bi_vcnt = idx + bv_count;
1065 clone->bi_size = to_bytes(len);
1066 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1067
Martin K. Petersen9c470082009-04-09 00:27:12 +01001068 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001069 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001070
1071 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1072 bio_integrity_trim(clone,
1073 bio_sector_offset(bio, idx, 0), len);
1074 }
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return clone;
1077}
1078
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001079static struct dm_target_io *alloc_tio(struct clone_info *ci,
1080 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001081{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001082 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001083
1084 tio->io = ci->io;
1085 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001086 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001087
1088 return tio;
1089}
1090
1091static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1092 unsigned flush_nr)
1093{
1094 struct dm_target_io *tio = alloc_tio(ci, ti);
1095 struct bio *clone;
1096
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001097 tio->info.flush_request = flush_nr;
1098
1099 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1100 __bio_clone(clone, ci->bio);
1101 clone->bi_destructor = dm_bio_destructor;
1102
1103 __map_bio(ti, clone, tio);
1104}
1105
1106static int __clone_and_map_empty_barrier(struct clone_info *ci)
1107{
1108 unsigned target_nr = 0, flush_nr;
1109 struct dm_target *ti;
1110
1111 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1112 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1113 flush_nr++)
1114 __flush_target(ci, ti, flush_nr);
1115
1116 ci->sector_count = 0;
1117
1118 return 0;
1119}
1120
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001121static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001124 struct dm_target *ti;
1125 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001126 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001128 if (unlikely(bio_empty_barrier(bio)))
1129 return __clone_and_map_empty_barrier(ci);
1130
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001131 ti = dm_table_find_target(ci->map, ci->sector);
1132 if (!dm_target_is_valid(ti))
1133 return -EIO;
1134
1135 max = max_io_len(ci->md, ci->sector, ti);
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /*
1138 * Allocate a target io object.
1139 */
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001140 tio = alloc_tio(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 if (ci->sector_count <= max) {
1143 /*
1144 * Optimise for the simple case where we can do all of
1145 * the remaining io with a single clone.
1146 */
1147 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001148 bio->bi_vcnt - ci->idx, ci->sector_count,
1149 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 __map_bio(ti, clone, tio);
1151 ci->sector_count = 0;
1152
1153 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1154 /*
1155 * There are some bvecs that don't span targets.
1156 * Do as many of these as possible.
1157 */
1158 int i;
1159 sector_t remaining = max;
1160 sector_t bv_len;
1161
1162 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1163 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1164
1165 if (bv_len > remaining)
1166 break;
1167
1168 remaining -= bv_len;
1169 len += bv_len;
1170 }
1171
Stefan Bader9faf4002006-10-03 01:15:41 -07001172 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1173 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 __map_bio(ti, clone, tio);
1175
1176 ci->sector += len;
1177 ci->sector_count -= len;
1178 ci->idx = i;
1179
1180 } else {
1181 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001182 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
1184 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001185 sector_t remaining = to_sector(bv->bv_len);
1186 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001188 do {
1189 if (offset) {
1190 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001191 if (!dm_target_is_valid(ti))
1192 return -EIO;
1193
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001194 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001196 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001199 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001201 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001202 bv->bv_offset + offset, len,
1203 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001204
1205 __map_bio(ti, clone, tio);
1206
1207 ci->sector += len;
1208 ci->sector_count -= len;
1209 offset += to_bytes(len);
1210 } while (remaining -= len);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ci->idx++;
1213 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001214
1215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001219 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001221static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001224 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 ci.map = dm_get_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001227 if (unlikely(!ci.map)) {
Jens Axboe1f98a132009-09-11 14:32:04 +02001228 if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001229 bio_io_error(bio);
1230 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001231 if (!md->barrier_error)
1232 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001233 return;
1234 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ci.md = md;
1237 ci.bio = bio;
1238 ci.io = alloc_io(md);
1239 ci.io->error = 0;
1240 atomic_set(&ci.io->io_count, 1);
1241 ci.io->bio = bio;
1242 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001243 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 ci.sector = bio->bi_sector;
1245 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001246 if (unlikely(bio_empty_barrier(bio)))
1247 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 ci.idx = bio->bi_idx;
1249
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001250 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001251 while (ci.sector_count && !error)
1252 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001255 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 dm_table_put(ci.map);
1257}
1258/*-----------------------------------------------------------------
1259 * CRUD END
1260 *---------------------------------------------------------------*/
1261
Milan Brozf6fccb12008-07-21 12:00:37 +01001262static int dm_merge_bvec(struct request_queue *q,
1263 struct bvec_merge_data *bvm,
1264 struct bio_vec *biovec)
1265{
1266 struct mapped_device *md = q->queuedata;
1267 struct dm_table *map = dm_get_table(md);
1268 struct dm_target *ti;
1269 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001270 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001271
1272 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001273 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001274
1275 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001276 if (!dm_target_is_valid(ti))
1277 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001278
1279 /*
1280 * Find maximum amount of I/O that won't need splitting
1281 */
1282 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1283 (sector_t) BIO_MAX_SECTORS);
1284 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1285 if (max_size < 0)
1286 max_size = 0;
1287
1288 /*
1289 * merge_bvec_fn() returns number of bytes
1290 * it can accept at this offset
1291 * max is precomputed maximal io size
1292 */
1293 if (max_size && ti->type->merge)
1294 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001295 /*
1296 * If the target doesn't support merge method and some of the devices
1297 * provided their merge_bvec method (we know this by looking at
1298 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1299 * entries. So always set max_size to 0, and the code below allows
1300 * just one page.
1301 */
1302 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1303
1304 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001305
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001306out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001307 dm_table_put(map);
1308
1309out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001310 /*
1311 * Always allow an entire first page
1312 */
1313 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1314 max_size = biovec->bv_len;
1315
Milan Brozf6fccb12008-07-21 12:00:37 +01001316 return max_size;
1317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319/*
1320 * The request function that just remaps the bio built up by
1321 * dm_merge_bvec.
1322 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001323static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Kevin Corry12f03a42006-02-01 03:04:52 -08001325 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001327 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001329 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Tejun Heo074a7ac2008-08-25 19:56:14 +09001331 cpu = part_stat_lock();
1332 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1333 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1334 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001337 * If we're suspended or the thread is processing barriers
1338 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001340 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Jens Axboe1f98a132009-09-11 14:32:04 +02001341 unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001342 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001344 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1345 bio_rw(bio) == READA) {
1346 bio_io_error(bio);
1347 return 0;
1348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Mikulas Patocka92c63902009-04-09 00:27:15 +01001350 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Mikulas Patocka92c63902009-04-09 00:27:15 +01001352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001355 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001356 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001360static int dm_make_request(struct request_queue *q, struct bio *bio)
1361{
1362 struct mapped_device *md = q->queuedata;
1363
Jens Axboe1f98a132009-09-11 14:32:04 +02001364 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001365 bio_endio(bio, -EOPNOTSUPP);
1366 return 0;
1367 }
1368
1369 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1370}
1371
1372static int dm_request_based(struct mapped_device *md)
1373{
1374 return blk_queue_stackable(md->queue);
1375}
1376
1377static int dm_request(struct request_queue *q, struct bio *bio)
1378{
1379 struct mapped_device *md = q->queuedata;
1380
1381 if (dm_request_based(md))
1382 return dm_make_request(q, bio);
1383
1384 return _dm_request(q, bio);
1385}
1386
1387void dm_dispatch_request(struct request *rq)
1388{
1389 int r;
1390
1391 if (blk_queue_io_stat(rq->q))
1392 rq->cmd_flags |= REQ_IO_STAT;
1393
1394 rq->start_time = jiffies;
1395 r = blk_insert_cloned_request(rq->q, rq);
1396 if (r)
1397 dm_complete_request(rq, r);
1398}
1399EXPORT_SYMBOL_GPL(dm_dispatch_request);
1400
1401static void dm_rq_bio_destructor(struct bio *bio)
1402{
1403 struct dm_rq_clone_bio_info *info = bio->bi_private;
1404 struct mapped_device *md = info->tio->md;
1405
1406 free_bio_info(info);
1407 bio_free(bio, md->bs);
1408}
1409
1410static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1411 void *data)
1412{
1413 struct dm_rq_target_io *tio = data;
1414 struct mapped_device *md = tio->md;
1415 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1416
1417 if (!info)
1418 return -ENOMEM;
1419
1420 info->orig = bio_orig;
1421 info->tio = tio;
1422 bio->bi_end_io = end_clone_bio;
1423 bio->bi_private = info;
1424 bio->bi_destructor = dm_rq_bio_destructor;
1425
1426 return 0;
1427}
1428
1429static int setup_clone(struct request *clone, struct request *rq,
1430 struct dm_rq_target_io *tio)
1431{
1432 int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1433 dm_rq_bio_constructor, tio);
1434
1435 if (r)
1436 return r;
1437
1438 clone->cmd = rq->cmd;
1439 clone->cmd_len = rq->cmd_len;
1440 clone->sense = rq->sense;
1441 clone->buffer = rq->buffer;
1442 clone->end_io = end_clone_request;
1443 clone->end_io_data = tio;
1444
1445 return 0;
1446}
1447
1448static int dm_rq_flush_suspending(struct mapped_device *md)
1449{
1450 return !md->suspend_rq.special;
1451}
1452
1453/*
1454 * Called with the queue lock held.
1455 */
1456static int dm_prep_fn(struct request_queue *q, struct request *rq)
1457{
1458 struct mapped_device *md = q->queuedata;
1459 struct dm_rq_target_io *tio;
1460 struct request *clone;
1461
1462 if (unlikely(rq == &md->suspend_rq)) {
1463 if (dm_rq_flush_suspending(md))
1464 return BLKPREP_OK;
1465 else
1466 /* The flush suspend was interrupted */
1467 return BLKPREP_KILL;
1468 }
1469
1470 if (unlikely(rq->special)) {
1471 DMWARN("Already has something in rq->special.");
1472 return BLKPREP_KILL;
1473 }
1474
Kiyoshi Ueda08885642009-12-10 23:52:15 +00001475 tio = alloc_rq_tio(md, GFP_ATOMIC);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001476 if (!tio)
1477 /* -ENOMEM */
1478 return BLKPREP_DEFER;
1479
1480 tio->md = md;
1481 tio->ti = NULL;
1482 tio->orig = rq;
1483 tio->error = 0;
1484 memset(&tio->info, 0, sizeof(tio->info));
1485
1486 clone = &tio->clone;
1487 if (setup_clone(clone, rq, tio)) {
1488 /* -ENOMEM */
1489 free_rq_tio(tio);
1490 return BLKPREP_DEFER;
1491 }
1492
1493 rq->special = clone;
1494 rq->cmd_flags |= REQ_DONTPREP;
1495
1496 return BLKPREP_OK;
1497}
1498
Kiyoshi Ueda598de402009-12-10 23:52:14 +00001499static void map_request(struct dm_target *ti, struct request *clone,
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001500 struct mapped_device *md)
1501{
1502 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001503 struct dm_rq_target_io *tio = clone->end_io_data;
1504
1505 /*
1506 * Hold the md reference here for the in-flight I/O.
1507 * We can't rely on the reference count by device opener,
1508 * because the device may be closed during the request completion
1509 * when all bios are completed.
1510 * See the comment in rq_completed() too.
1511 */
1512 dm_get(md);
1513
1514 tio->ti = ti;
1515 r = ti->type->map_rq(ti, clone, &tio->info);
1516 switch (r) {
1517 case DM_MAPIO_SUBMITTED:
1518 /* The target has taken the I/O to submit by itself later */
1519 break;
1520 case DM_MAPIO_REMAPPED:
1521 /* The target has remapped the I/O so dispatch it */
1522 dm_dispatch_request(clone);
1523 break;
1524 case DM_MAPIO_REQUEUE:
1525 /* The target wants to requeue the I/O */
1526 dm_requeue_unmapped_request(clone);
1527 break;
1528 default:
1529 if (r > 0) {
1530 DMWARN("unimplemented target map return value: %d", r);
1531 BUG();
1532 }
1533
1534 /* The target wants to complete the I/O */
1535 dm_kill_unmapped_request(clone, r);
1536 break;
1537 }
1538}
1539
1540/*
1541 * q->request_fn for request-based dm.
1542 * Called with the queue lock held.
1543 */
1544static void dm_request_fn(struct request_queue *q)
1545{
1546 struct mapped_device *md = q->queuedata;
1547 struct dm_table *map = dm_get_table(md);
1548 struct dm_target *ti;
1549 struct request *rq;
1550
1551 /*
1552 * For noflush suspend, check blk_queue_stopped() to immediately
1553 * quit I/O dispatching.
1554 */
1555 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1556 rq = blk_peek_request(q);
1557 if (!rq)
1558 goto plug_and_out;
1559
1560 if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1561 if (queue_in_flight(q))
1562 /* Not quiet yet. Wait more */
1563 goto plug_and_out;
1564
1565 /* This device should be quiet now */
1566 __stop_queue(q);
1567 blk_start_request(rq);
1568 __blk_end_request_all(rq, 0);
1569 wake_up(&md->wait);
1570 goto out;
1571 }
1572
1573 ti = dm_table_find_target(map, blk_rq_pos(rq));
1574 if (ti->type->busy && ti->type->busy(ti))
1575 goto plug_and_out;
1576
1577 blk_start_request(rq);
1578 spin_unlock(q->queue_lock);
Kiyoshi Ueda598de402009-12-10 23:52:14 +00001579 map_request(ti, rq->special, md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001580 spin_lock_irq(q->queue_lock);
1581 }
1582
1583 goto out;
1584
1585plug_and_out:
1586 if (!elv_queue_empty(q))
1587 /* Some requests still remain, retry later */
1588 blk_plug_device(q);
1589
1590out:
1591 dm_table_put(map);
1592
1593 return;
1594}
1595
1596int dm_underlying_device_busy(struct request_queue *q)
1597{
1598 return blk_lld_busy(q);
1599}
1600EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1601
1602static int dm_lld_busy(struct request_queue *q)
1603{
1604 int r;
1605 struct mapped_device *md = q->queuedata;
1606 struct dm_table *map = dm_get_table(md);
1607
1608 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1609 r = 1;
1610 else
1611 r = dm_table_any_busy_target(map);
1612
1613 dm_table_put(map);
1614
1615 return r;
1616}
1617
Jens Axboe165125e2007-07-24 09:28:11 +02001618static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 struct mapped_device *md = q->queuedata;
1621 struct dm_table *map = dm_get_table(md);
1622
1623 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001624 if (dm_request_based(md))
1625 generic_unplug_device(q);
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 dm_table_unplug_all(map);
1628 dm_table_put(map);
1629 }
1630}
1631
1632static int dm_any_congested(void *congested_data, int bdi_bits)
1633{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001634 int r = bdi_bits;
1635 struct mapped_device *md = congested_data;
1636 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001638 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001639 map = dm_get_table(md);
1640 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001641 /*
1642 * Request-based dm cares about only own queue for
1643 * the query about congestion status of request_queue
1644 */
1645 if (dm_request_based(md))
1646 r = md->queue->backing_dev_info.state &
1647 bdi_bits;
1648 else
1649 r = dm_table_any_congested(map, bdi_bits);
1650
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001651 dm_table_put(map);
1652 }
1653 }
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return r;
1656}
1657
1658/*-----------------------------------------------------------------
1659 * An IDR is used to keep track of allocated minor numbers.
1660 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661static DEFINE_IDR(_minor_idr);
1662
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001663static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001665 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001667 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
1670/*
1671 * See if the device with a specific minor # is free.
1672 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001673static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 int r, m;
1676
1677 if (minor >= (1 << MINORBITS))
1678 return -EINVAL;
1679
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001680 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1681 if (!r)
1682 return -ENOMEM;
1683
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001684 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 if (idr_find(&_minor_idr, minor)) {
1687 r = -EBUSY;
1688 goto out;
1689 }
1690
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001691 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001692 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 if (m != minor) {
1696 idr_remove(&_minor_idr, m);
1697 r = -EBUSY;
1698 goto out;
1699 }
1700
1701out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001702 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return r;
1704}
1705
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001706static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001708 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001711 if (!r)
1712 return -ENOMEM;
1713
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001714 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001716 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001717 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (m >= (1 << MINORBITS)) {
1721 idr_remove(&_minor_idr, m);
1722 r = -ENOSPC;
1723 goto out;
1724 }
1725
1726 *minor = m;
1727
1728out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001729 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return r;
1731}
1732
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001733static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Mikulas Patocka53d59142009-04-02 19:55:37 +01001735static void dm_wq_work(struct work_struct *work);
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737/*
1738 * Allocate and initialise a blank device with a given minor.
1739 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001740static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001743 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001744 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 if (!md) {
1747 DMWARN("unable to allocate device, out of memory.");
1748 return NULL;
1749 }
1750
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001751 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001752 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001755 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001756 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001757 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001758 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001760 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001762 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001763 mutex_init(&md->suspend_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001764 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 rwlock_init(&md->map_lock);
1766 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001767 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001769 atomic_set(&md->uevent_seq, 0);
1770 INIT_LIST_HEAD(&md->uevent_list);
1771 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001773 md->queue = blk_init_queue(dm_request_fn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001775 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001777 /*
1778 * Request-based dm devices cannot be stacked on top of bio-based dm
1779 * devices. The type of this dm device has not been decided yet,
1780 * although we initialized the queue using blk_init_queue().
1781 * The type is decided at the first table loading time.
1782 * To prevent problematic device stacking, clear the queue flag
1783 * for request stacking support until then.
1784 *
1785 * This queue is new, so no concurrency on the queue_flags.
1786 */
1787 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1788 md->saved_make_request_fn = md->queue->make_request_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 md->queue->queuedata = md;
1790 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1791 md->queue->backing_dev_info.congested_data = md;
1792 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +01001793 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001795 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001796 blk_queue_softirq_done(md->queue, dm_softirq_done);
1797 blk_queue_prep_rq(md->queue, dm_prep_fn);
1798 blk_queue_lld_busy(md->queue, dm_lld_busy);
Stefan Bader9faf4002006-10-03 01:15:41 -07001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 md->disk = alloc_disk(1);
1801 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001802 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001804 atomic_set(&md->pending[0], 0);
1805 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001806 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001807 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001808 init_waitqueue_head(&md->eventq);
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 md->disk->major = _major;
1811 md->disk->first_minor = minor;
1812 md->disk->fops = &dm_blk_dops;
1813 md->disk->queue = md->queue;
1814 md->disk->private_data = md;
1815 sprintf(md->disk->disk_name, "dm-%d", minor);
1816 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001817 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Milan Broz304f3f62008-02-08 02:11:17 +00001819 md->wq = create_singlethread_workqueue("kdmflush");
1820 if (!md->wq)
1821 goto bad_thread;
1822
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001823 md->bdev = bdget_disk(md->disk, 0);
1824 if (!md->bdev)
1825 goto bad_bdev;
1826
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001827 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001828 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001829 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001830 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001831
1832 BUG_ON(old_md != MINOR_ALLOCED);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return md;
1835
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001836bad_bdev:
1837 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001838bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001839 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001840 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001841bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001842 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001843bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001845bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001846 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001847bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 kfree(md);
1849 return NULL;
1850}
1851
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001852static void unlock_fs(struct mapped_device *md);
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854static void free_dev(struct mapped_device *md)
1855{
Tejun Heof331c022008-09-03 09:01:48 +02001856 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001857
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001858 unlock_fs(md);
1859 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001860 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001861 if (md->tio_pool)
1862 mempool_destroy(md->tio_pool);
1863 if (md->io_pool)
1864 mempool_destroy(md->io_pool);
1865 if (md->bs)
1866 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001867 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001869 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001870
1871 spin_lock(&_minor_lock);
1872 md->disk->private_data = NULL;
1873 spin_unlock(&_minor_lock);
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001876 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001877 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 kfree(md);
1879}
1880
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001881static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1882{
1883 struct dm_md_mempools *p;
1884
1885 if (md->io_pool && md->tio_pool && md->bs)
1886 /* the md already has necessary mempools */
1887 goto out;
1888
1889 p = dm_table_get_md_mempools(t);
1890 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1891
1892 md->io_pool = p->io_pool;
1893 p->io_pool = NULL;
1894 md->tio_pool = p->tio_pool;
1895 p->tio_pool = NULL;
1896 md->bs = p->bs;
1897 p->bs = NULL;
1898
1899out:
1900 /* mempool bind completed, now no need any mempools in the table */
1901 dm_table_free_md_mempools(t);
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/*
1905 * Bind a table to the device.
1906 */
1907static void event_callback(void *context)
1908{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001909 unsigned long flags;
1910 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct mapped_device *md = (struct mapped_device *) context;
1912
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001913 spin_lock_irqsave(&md->uevent_lock, flags);
1914 list_splice_init(&md->uevent_list, &uevents);
1915 spin_unlock_irqrestore(&md->uevent_lock, flags);
1916
Tejun Heoed9e1982008-08-25 19:56:05 +09001917 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 atomic_inc(&md->event_nr);
1920 wake_up(&md->eventq);
1921}
1922
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001923static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001925 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001927 mutex_lock(&md->bdev->bd_inode->i_mutex);
1928 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1929 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001932static int __bind(struct mapped_device *md, struct dm_table *t,
1933 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Jens Axboe165125e2007-07-24 09:28:11 +02001935 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001937 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001940
1941 /*
1942 * Wipe any geometry if the size of the table changed.
1943 */
1944 if (size != get_capacity(md->disk))
1945 memset(&md->geometry, 0, sizeof(md->geometry));
1946
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001947 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Mikulas Patockad5816872009-01-06 03:05:10 +00001949 if (!size) {
1950 dm_table_destroy(t);
1951 return 0;
1952 }
1953
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001954 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001955
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001956 /*
1957 * The queue hasn't been stopped yet, if the old table type wasn't
1958 * for request-based during suspension. So stop it to prevent
1959 * I/O mapping before resume.
1960 * This must be done before setting the queue restrictions,
1961 * because request-based dm may be run just after the setting.
1962 */
1963 if (dm_table_request_based(t) && !blk_queue_stopped(q))
1964 stop_queue(q);
1965
1966 __bind_mempools(md, t);
1967
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001968 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001969 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001970 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001971 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return 0;
1974}
1975
1976static void __unbind(struct mapped_device *md)
1977{
1978 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001979 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 if (!map)
1982 return;
1983
1984 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001985 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001987 write_unlock_irqrestore(&md->map_lock, flags);
Mikulas Patockad5816872009-01-06 03:05:10 +00001988 dm_table_destroy(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991/*
1992 * Constructor for a new device.
1993 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001994int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 struct mapped_device *md;
1997
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001998 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (!md)
2000 return -ENXIO;
2001
Milan Broz784aae72009-01-06 03:05:12 +00002002 dm_sysfs_init(md);
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 *result = md;
2005 return 0;
2006}
2007
David Teigland637842c2006-01-06 00:20:00 -08002008static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 unsigned minor = MINOR(dev);
2012
2013 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2014 return NULL;
2015
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002016 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002019 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002020 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002021 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002022 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002023 goto out;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002026out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002027 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
David Teigland637842c2006-01-06 00:20:00 -08002029 return md;
2030}
2031
David Teiglandd229a952006-01-06 00:20:01 -08002032struct mapped_device *dm_get_md(dev_t dev)
2033{
2034 struct mapped_device *md = dm_find_md(dev);
2035
2036 if (md)
2037 dm_get(md);
2038
2039 return md;
2040}
2041
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002042void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002043{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002044 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
2047void dm_set_mdptr(struct mapped_device *md, void *ptr)
2048{
2049 md->interface_ptr = ptr;
2050}
2051
2052void dm_get(struct mapped_device *md)
2053{
2054 atomic_inc(&md->holders);
2055}
2056
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002057const char *dm_device_name(struct mapped_device *md)
2058{
2059 return md->name;
2060}
2061EXPORT_SYMBOL_GPL(dm_device_name);
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063void dm_put(struct mapped_device *md)
2064{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002065 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002067 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2068
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002069 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08002070 map = dm_get_table(md);
Tejun Heof331c022008-09-03 09:01:48 +02002071 idr_replace(&_minor_idr, MINOR_ALLOCED,
2072 MINOR(disk_devt(dm_disk(md))));
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002073 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002074 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002075 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 dm_table_presuspend_targets(map);
2077 dm_table_postsuspend_targets(map);
2078 }
Milan Broz784aae72009-01-06 03:05:12 +00002079 dm_sysfs_exit(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002080 dm_table_put(map);
Mikulas Patockaa1b51e92009-01-06 03:04:53 +00002081 __unbind(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 free_dev(md);
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
Edward Goggin79eb8852007-05-09 02:32:56 -07002085EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Mikulas Patocka401600d2009-04-02 19:55:38 +01002087static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002088{
2089 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002090 DECLARE_WAITQUEUE(wait, current);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002091 struct request_queue *q = md->queue;
2092 unsigned long flags;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002093
2094 dm_unplug_all(md->queue);
2095
2096 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002097
2098 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002099 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002100
2101 smp_mb();
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002102 if (dm_request_based(md)) {
2103 spin_lock_irqsave(q->queue_lock, flags);
2104 if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2105 spin_unlock_irqrestore(q->queue_lock, flags);
2106 break;
2107 }
2108 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +00002109 } else if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002110 break;
2111
Mikulas Patocka401600d2009-04-02 19:55:38 +01002112 if (interruptible == TASK_INTERRUPTIBLE &&
2113 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002114 r = -EINTR;
2115 break;
2116 }
2117
2118 io_schedule();
2119 }
2120 set_current_state(TASK_RUNNING);
2121
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002122 remove_wait_queue(&md->wait, &wait);
2123
Milan Broz46125c12008-02-08 02:10:30 +00002124 return r;
2125}
2126
Mikulas Patocka531fe962009-06-22 10:12:17 +01002127static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002128{
2129 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002130
2131 bio_init(&md->barrier_bio);
2132 md->barrier_bio.bi_bdev = md->bdev;
2133 md->barrier_bio.bi_rw = WRITE_BARRIER;
2134 __split_and_process_bio(md, &md->barrier_bio);
2135
2136 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002137}
2138
2139static void process_barrier(struct mapped_device *md, struct bio *bio)
2140{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002141 md->barrier_error = 0;
2142
Mikulas Patocka531fe962009-06-22 10:12:17 +01002143 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002144
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002145 if (!bio_empty_barrier(bio)) {
2146 __split_and_process_bio(md, bio);
2147 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002148 }
2149
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002150 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002151 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002152 else {
2153 spin_lock_irq(&md->deferred_lock);
2154 bio_list_add_head(&md->deferred, bio);
2155 spin_unlock_irq(&md->deferred_lock);
2156 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002157}
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159/*
2160 * Process the deferred bios
2161 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002162static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Mikulas Patockaef208582009-04-02 19:55:38 +01002164 struct mapped_device *md = container_of(work, struct mapped_device,
2165 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002166 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Mikulas Patockaef208582009-04-02 19:55:38 +01002168 down_write(&md->io_lock);
2169
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002170 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002171 spin_lock_irq(&md->deferred_lock);
2172 c = bio_list_pop(&md->deferred);
2173 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002174
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002175 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002176 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002177 break;
2178 }
2179
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002180 up_write(&md->io_lock);
2181
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002182 if (dm_request_based(md))
2183 generic_make_request(c);
2184 else {
Jens Axboe1f98a132009-09-11 14:32:04 +02002185 if (bio_rw_flagged(c, BIO_RW_BARRIER))
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002186 process_barrier(md, c);
2187 else
2188 __split_and_process_bio(md, c);
2189 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002190
2191 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002192 }
Milan Broz73d410c2008-02-08 02:10:25 +00002193
Mikulas Patockaef208582009-04-02 19:55:38 +01002194 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002197static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002198{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002199 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2200 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002201 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002202}
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204/*
2205 * Swap in a new table (destroying old one).
2206 */
2207int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2208{
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002209 struct queue_limits limits;
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002210 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Daniel Walkere61290a2008-02-08 02:10:08 +00002212 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002215 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002216 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002218 r = dm_calculate_queue_limits(table, &limits);
2219 if (r)
2220 goto out;
2221
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002222 /* cannot change the device type, once a table is bound */
2223 if (md->map &&
2224 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2225 DMWARN("can't change the device type after a table is bound");
2226 goto out;
2227 }
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 __unbind(md);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002230 r = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002232out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002233 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002234 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002237static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2238{
2239 md->suspend_rq.special = (void *)0x1;
2240}
2241
2242static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2243{
2244 struct request_queue *q = md->queue;
2245 unsigned long flags;
2246
2247 spin_lock_irqsave(q->queue_lock, flags);
2248 if (!noflush)
2249 dm_rq_invalidate_suspend_marker(md);
2250 __start_queue(q);
2251 spin_unlock_irqrestore(q->queue_lock, flags);
2252}
2253
2254static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2255{
2256 struct request *rq = &md->suspend_rq;
2257 struct request_queue *q = md->queue;
2258
2259 if (noflush)
2260 stop_queue(q);
2261 else {
2262 blk_rq_init(q, rq);
2263 blk_insert_request(q, rq, 0, NULL);
2264 }
2265}
2266
2267static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2268{
2269 int r = 1;
2270 struct request *rq = &md->suspend_rq;
2271 struct request_queue *q = md->queue;
2272 unsigned long flags;
2273
2274 if (noflush)
2275 return r;
2276
2277 /* The marker must be protected by queue lock if it is in use */
2278 spin_lock_irqsave(q->queue_lock, flags);
2279 if (unlikely(rq->ref_count)) {
2280 /*
2281 * This can happen, when the previous flush suspend was
2282 * interrupted, the marker is still in the queue and
2283 * this flush suspend has been invoked, because we don't
2284 * remove the marker at the time of suspend interruption.
2285 * We have only one marker per mapped_device, so we can't
2286 * start another flush suspend while it is in use.
2287 */
2288 BUG_ON(!rq->special); /* The marker should be invalidated */
2289 DMWARN("Invalidating the previous flush suspend is still in"
2290 " progress. Please retry later.");
2291 r = 0;
2292 }
2293 spin_unlock_irqrestore(q->queue_lock, flags);
2294
2295 return r;
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298/*
2299 * Functions to lock and unlock any filesystem running on the
2300 * device.
2301 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002302static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002304 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002307
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002308 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002309 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002310 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002311 md->frozen_sb = NULL;
2312 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002313 }
2314
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002315 set_bit(DMF_FROZEN, &md->flags);
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return 0;
2318}
2319
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002320static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002322 if (!test_bit(DMF_FROZEN, &md->flags))
2323 return;
2324
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002325 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002327 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
2330/*
2331 * We need to be able to change a mapping table under a mounted
2332 * filesystem. For example we might want to move some data in
2333 * the background. Before the table can be swapped with
2334 * dm_bind_table, dm_suspend must be called to flush any in
2335 * flight bios and ensure that any further io gets deferred.
2336 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002337/*
2338 * Suspend mechanism in request-based dm.
2339 *
2340 * After the suspend starts, further incoming requests are kept in
2341 * the request_queue and deferred.
2342 * Remaining requests in the request_queue at the start of suspend are flushed
2343 * if it is flush suspend.
2344 * The suspend completes when the following conditions have been satisfied,
2345 * so wait for it:
2346 * 1. q->in_flight is 0 (which means no in_flight request)
2347 * 2. queue has been stopped (which means no request dispatching)
2348 *
2349 *
2350 * Noflush suspend
2351 * ---------------
2352 * Noflush suspend doesn't need to dispatch remaining requests.
2353 * So stop the queue immediately. Then, wait for all in_flight requests
2354 * to be completed or requeued.
2355 *
2356 * To abort noflush suspend, start the queue.
2357 *
2358 *
2359 * Flush suspend
2360 * -------------
2361 * Flush suspend needs to dispatch remaining requests. So stop the queue
2362 * after the remaining requests are completed. (Requeued request must be also
2363 * re-dispatched and completed. Until then, we can't stop the queue.)
2364 *
2365 * During flushing the remaining requests, further incoming requests are also
2366 * inserted to the same queue. To distinguish which requests are to be
2367 * flushed, we insert a marker request to the queue at the time of starting
2368 * flush suspend, like a barrier.
2369 * The dispatching is blocked when the marker is found on the top of the queue.
2370 * And the queue is stopped when all in_flight requests are completed, since
2371 * that means the remaining requests are completely flushed.
2372 * Then, the marker is removed from the queue.
2373 *
2374 * To abort flush suspend, we also need to take care of the marker, not only
2375 * starting the queue.
2376 * We don't remove the marker forcibly from the queue since it's against
2377 * the block-layer manner. Instead, we put a invalidated mark on the marker.
2378 * When the invalidated marker is found on the top of the queue, it is
2379 * immediately removed from the queue, so it doesn't block dispatching.
2380 * Because we have only one marker per mapped_device, we can't start another
2381 * flush suspend until the invalidated marker is removed from the queue.
2382 * So fail and return with -EBUSY in such a case.
2383 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002384int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002386 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002387 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002388 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002389 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Daniel Walkere61290a2008-02-08 02:10:08 +00002391 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002392
Milan Broz73d410c2008-02-08 02:10:25 +00002393 if (dm_suspended(md)) {
2394 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002395 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002398 if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2399 r = -EBUSY;
2400 goto out_unlock;
2401 }
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002405 /*
2406 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2407 * This flag is cleared before dm_suspend returns.
2408 */
2409 if (noflush)
2410 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2411
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002412 /* This does not get reverted if there's an error later. */
2413 dm_table_presuspend_targets(map);
2414
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002415 /*
2416 * Flush I/O to the device. noflush supersedes do_lockfs,
2417 * because lock_fs() needs to flush I/Os.
2418 */
2419 if (!noflush && do_lockfs) {
2420 r = lock_fs(md);
2421 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002422 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002426 * Here we must make sure that no processes are submitting requests
2427 * to target drivers i.e. no one may be executing
2428 * __split_and_process_bio. This is called from dm_request and
2429 * dm_wq_work.
2430 *
2431 * To get all processes out of __split_and_process_bio in dm_request,
2432 * we take the write lock. To prevent any process from reentering
2433 * __split_and_process_bio from dm_request, we set
2434 * DMF_QUEUE_IO_TO_THREAD.
2435 *
2436 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2437 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2438 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2439 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002441 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002442 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2443 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002444 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002446 flush_workqueue(md->wq);
2447
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002448 if (dm_request_based(md))
2449 dm_rq_start_suspend(md, noflush);
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002452 * At this point no more requests are entering target request routines.
2453 * We call dm_wait_for_completion to wait for all existing requests
2454 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002456 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002458 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002459 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002460 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002461 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002464 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002465 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002466
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002467 if (dm_request_based(md))
2468 dm_rq_abort_suspend(md, noflush);
2469
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002470 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002471 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002472 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002473
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002474 /*
2475 * If dm_wait_for_completion returned 0, the device is completely
2476 * quiescent now. There is no request-processing activity. All new
2477 * requests are being added to md->deferred list.
2478 */
2479
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002480 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 set_bit(DMF_SUSPENDED, &md->flags);
2483
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002484out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002486
2487out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002488 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002489 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492int dm_resume(struct mapped_device *md)
2493{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002494 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002495 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Daniel Walkere61290a2008-02-08 02:10:08 +00002497 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002498 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002499 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002500
2501 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002502 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Milan Broz8757b772006-10-03 01:15:36 -07002505 r = dm_table_resume_targets(map);
2506 if (r)
2507 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002508
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002509 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002510
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002511 /*
2512 * Flushing deferred I/Os must be done after targets are resumed
2513 * so that mapping of targets can work correctly.
2514 * Request-based dm is queueing the deferred I/Os in its request_queue.
2515 */
2516 if (dm_request_based(md))
2517 start_queue(md->queue);
2518
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002519 unlock_fs(md);
2520
2521 clear_bit(DMF_SUSPENDED, &md->flags);
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002524 r = 0;
2525out:
2526 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002527 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002528
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002529 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
2532/*-----------------------------------------------------------------
2533 * Event notification.
2534 *---------------------------------------------------------------*/
Milan Broz60935eb2009-06-22 10:12:30 +01002535void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2536 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002537{
Milan Broz60935eb2009-06-22 10:12:30 +01002538 char udev_cookie[DM_COOKIE_LENGTH];
2539 char *envp[] = { udev_cookie, NULL };
2540
2541 if (!cookie)
2542 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2543 else {
2544 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2545 DM_COOKIE_ENV_VAR_NAME, cookie);
2546 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2547 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002548}
2549
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002550uint32_t dm_next_uevent_seq(struct mapped_device *md)
2551{
2552 return atomic_add_return(1, &md->uevent_seq);
2553}
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555uint32_t dm_get_event_nr(struct mapped_device *md)
2556{
2557 return atomic_read(&md->event_nr);
2558}
2559
2560int dm_wait_event(struct mapped_device *md, int event_nr)
2561{
2562 return wait_event_interruptible(md->eventq,
2563 (event_nr != atomic_read(&md->event_nr)));
2564}
2565
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002566void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2567{
2568 unsigned long flags;
2569
2570 spin_lock_irqsave(&md->uevent_lock, flags);
2571 list_add(elist, &md->uevent_list);
2572 spin_unlock_irqrestore(&md->uevent_lock, flags);
2573}
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575/*
2576 * The gendisk is only valid as long as you have a reference
2577 * count on 'md'.
2578 */
2579struct gendisk *dm_disk(struct mapped_device *md)
2580{
2581 return md->disk;
2582}
2583
Milan Broz784aae72009-01-06 03:05:12 +00002584struct kobject *dm_kobject(struct mapped_device *md)
2585{
2586 return &md->kobj;
2587}
2588
2589/*
2590 * struct mapped_device should not be exported outside of dm.c
2591 * so use this check to verify that kobj is part of md structure
2592 */
2593struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2594{
2595 struct mapped_device *md;
2596
2597 md = container_of(kobj, struct mapped_device, kobj);
2598 if (&md->kobj != kobj)
2599 return NULL;
2600
Milan Broz4d89b7b2009-06-22 10:12:11 +01002601 if (test_bit(DMF_FREEING, &md->flags) ||
2602 test_bit(DMF_DELETING, &md->flags))
2603 return NULL;
2604
Milan Broz784aae72009-01-06 03:05:12 +00002605 dm_get(md);
2606 return md;
2607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609int dm_suspended(struct mapped_device *md)
2610{
2611 return test_bit(DMF_SUSPENDED, &md->flags);
2612}
2613
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002614int dm_noflush_suspending(struct dm_target *ti)
2615{
2616 struct mapped_device *md = dm_table_get_md(ti->table);
2617 int r = __noflush_suspending(md);
2618
2619 dm_put(md);
2620
2621 return r;
2622}
2623EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2624
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002625struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2626{
2627 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2628
2629 if (!pools)
2630 return NULL;
2631
2632 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2633 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2634 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2635 if (!pools->io_pool)
2636 goto free_pools_and_out;
2637
2638 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2639 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2640 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2641 if (!pools->tio_pool)
2642 goto free_io_pool_and_out;
2643
2644 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2645 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2646 if (!pools->bs)
2647 goto free_tio_pool_and_out;
2648
2649 return pools;
2650
2651free_tio_pool_and_out:
2652 mempool_destroy(pools->tio_pool);
2653
2654free_io_pool_and_out:
2655 mempool_destroy(pools->io_pool);
2656
2657free_pools_and_out:
2658 kfree(pools);
2659
2660 return NULL;
2661}
2662
2663void dm_free_md_mempools(struct dm_md_mempools *pools)
2664{
2665 if (!pools)
2666 return;
2667
2668 if (pools->io_pool)
2669 mempool_destroy(pools->io_pool);
2670
2671 if (pools->tio_pool)
2672 mempool_destroy(pools->tio_pool);
2673
2674 if (pools->bs)
2675 bioset_free(pools->bs);
2676
2677 kfree(pools);
2678}
2679
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002680static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 .open = dm_blk_open,
2682 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002683 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002684 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 .owner = THIS_MODULE
2686};
2687
2688EXPORT_SYMBOL(dm_get_mapinfo);
2689
2690/*
2691 * module hooks
2692 */
2693module_init(dm_init);
2694module_exit(dm_exit);
2695
2696module_param(major, uint, 0);
2697MODULE_PARM_DESC(major, "The major number of the device mapper");
2698MODULE_DESCRIPTION(DM_NAME " driver");
2699MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2700MODULE_LICENSE("GPL");