blob: cf0b455b21ef9ebbedb9de00599d3b6b96a937f5 [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 Uedacec47e32009-06-22 10:12:35 +0100435static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md)
436{
437 return mempool_alloc(md->tio_pool, GFP_ATOMIC);
438}
439
440static void free_rq_tio(struct dm_rq_target_io *tio)
441{
442 mempool_free(tio, tio->md->tio_pool);
443}
444
445static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
446{
447 return mempool_alloc(md->io_pool, GFP_ATOMIC);
448}
449
450static void free_bio_info(struct dm_rq_clone_bio_info *info)
451{
452 mempool_free(info, info->tio->md->io_pool);
453}
454
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000455static int md_in_flight(struct mapped_device *md)
456{
457 return atomic_read(&md->pending[READ]) +
458 atomic_read(&md->pending[WRITE]);
459}
460
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800461static void start_io_acct(struct dm_io *io)
462{
463 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900464 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200465 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800466
467 io->start_time = jiffies;
468
Tejun Heo074a7ac2008-08-25 19:56:14 +0900469 cpu = part_stat_lock();
470 part_round_stats(cpu, &dm_disk(md)->part0);
471 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200472 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800473}
474
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000475static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800476{
477 struct mapped_device *md = io->md;
478 struct bio *bio = io->bio;
479 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900480 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800481 int rw = bio_data_dir(bio);
482
Tejun Heo074a7ac2008-08-25 19:56:14 +0900483 cpu = part_stat_lock();
484 part_round_stats(cpu, &dm_disk(md)->part0);
485 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
486 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800487
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100488 /*
489 * After this is decremented the bio must not be touched if it is
490 * a barrier.
491 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200492 dm_disk(md)->part0.in_flight[rw] = pending =
493 atomic_dec_return(&md->pending[rw]);
494 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800495
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000496 /* nudge anyone waiting on suspend queue */
497 if (!pending)
498 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
502 * Add the bio to the list of deferred io.
503 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100504static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700506 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Mikulas Patocka022c2612009-04-02 19:55:39 +0100508 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100510 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Mikulas Patocka92c63902009-04-09 00:27:15 +0100512 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
513 queue_work(md->wq, &md->work);
514
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700515 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/*
519 * Everyone (including functions in this file), should use this
520 * function to access the md->map field, and make sure they call
521 * dm_table_put() when finished.
522 */
523struct dm_table *dm_get_table(struct mapped_device *md)
524{
525 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100526 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100528 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 t = md->map;
530 if (t)
531 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100532 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 return t;
535}
536
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800537/*
538 * Get the geometry associated with a dm device
539 */
540int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
541{
542 *geo = md->geometry;
543
544 return 0;
545}
546
547/*
548 * Set the geometry of a device.
549 */
550int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
551{
552 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
553
554 if (geo->start > sz) {
555 DMWARN("Start sector is beyond the geometry limits.");
556 return -EINVAL;
557 }
558
559 md->geometry = *geo;
560
561 return 0;
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*-----------------------------------------------------------------
565 * CRUD START:
566 * A more elegant soln is in the works that uses the queue
567 * merge fn, unfortunately there are a couple of changes to
568 * the block layer that I want to make for this. So in the
569 * interests of getting something for people to use I give
570 * you this clearly demarcated crap.
571 *---------------------------------------------------------------*/
572
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800573static int __noflush_suspending(struct mapped_device *md)
574{
575 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Decrements the number of outstanding ios that a bio has been
580 * cloned into, completing the original io if necc.
581 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800582static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800584 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000585 int io_error;
586 struct bio *bio;
587 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800588
589 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100590 if (unlikely(error)) {
591 spin_lock_irqsave(&io->endio_lock, flags);
592 if (!(io->error > 0 && __noflush_suspending(md)))
593 io->error = error;
594 spin_unlock_irqrestore(&io->endio_lock, flags);
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800598 if (io->error == DM_ENDIO_REQUEUE) {
599 /*
600 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800601 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100602 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100603 if (__noflush_suspending(md)) {
Jens Axboe1f98a132009-09-11 14:32:04 +0200604 if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100605 bio_list_add_head(&md->deferred,
606 io->bio);
607 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800608 /* noflush suspend was interrupted. */
609 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100610 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800611 }
612
Milan Brozb35f8ca2009-03-16 17:44:36 +0000613 io_error = io->error;
614 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100615
Jens Axboe1f98a132009-09-11 14:32:04 +0200616 if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100617 /*
618 * There can be just one barrier request so we use
619 * a per-device variable for error reporting.
620 * Note that you can't touch the bio after end_io_acct
621 */
Mikulas Patockafdb95722009-06-22 10:12:19 +0100622 if (!md->barrier_error && io_error != -EOPNOTSUPP)
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100623 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100624 end_io_acct(io);
625 } else {
626 end_io_acct(io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000627
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100628 if (io_error != DM_ENDIO_REQUEUE) {
629 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000630
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100631 bio_endio(bio, io_error);
632 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800633 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100634
635 free_io(md, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637}
638
NeilBrown6712ecf2007-09-27 12:47:43 +0200639static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100642 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000643 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700644 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 dm_endio_fn endio = tio->ti->type->end_io;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
648 error = -EIO;
649
650 if (endio) {
651 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800652 if (r < 0 || r == DM_ENDIO_REQUEUE)
653 /*
654 * error and requeue request are handled
655 * in dec_pending().
656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800658 else if (r == DM_ENDIO_INCOMPLETE)
659 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200660 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800661 else if (r) {
662 DMWARN("unimplemented target endio return value: %d", r);
663 BUG();
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Stefan Bader9faf4002006-10-03 01:15:41 -0700667 /*
668 * Store md for cleanup instead of tio which is about to get freed.
669 */
670 bio->bi_private = md->bs;
671
Stefan Bader9faf4002006-10-03 01:15:41 -0700672 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000673 bio_put(bio);
674 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100677/*
678 * Partial completion handling for request-based dm
679 */
680static void end_clone_bio(struct bio *clone, int error)
681{
682 struct dm_rq_clone_bio_info *info = clone->bi_private;
683 struct dm_rq_target_io *tio = info->tio;
684 struct bio *bio = info->orig;
685 unsigned int nr_bytes = info->orig->bi_size;
686
687 bio_put(clone);
688
689 if (tio->error)
690 /*
691 * An error has already been detected on the request.
692 * Once error occurred, just let clone->end_io() handle
693 * the remainder.
694 */
695 return;
696 else if (error) {
697 /*
698 * Don't notice the error to the upper layer yet.
699 * The error handling decision is made by the target driver,
700 * when the request is completed.
701 */
702 tio->error = error;
703 return;
704 }
705
706 /*
707 * I/O for the bio successfully completed.
708 * Notice the data completion to the upper layer.
709 */
710
711 /*
712 * bios are processed from the head of the list.
713 * So the completing bio should always be rq->bio.
714 * If it's not, something wrong is happening.
715 */
716 if (tio->orig->bio != bio)
717 DMERR("bio completion is going in the middle of the request");
718
719 /*
720 * Update the original request.
721 * Do not use blk_end_request() here, because it may complete
722 * the original request before the clone, and break the ordering.
723 */
724 blk_update_request(tio->orig, 0, nr_bytes);
725}
726
727/*
728 * Don't touch any member of the md after calling this function because
729 * the md may be freed in dm_put() at the end of this function.
730 * Or do dm_get() before calling this function and dm_put() later.
731 */
732static void rq_completed(struct mapped_device *md, int run_queue)
733{
734 int wakeup_waiters = 0;
735 struct request_queue *q = md->queue;
736 unsigned long flags;
737
738 spin_lock_irqsave(q->queue_lock, flags);
739 if (!queue_in_flight(q))
740 wakeup_waiters = 1;
741 spin_unlock_irqrestore(q->queue_lock, flags);
742
743 /* nudge anyone waiting on suspend queue */
744 if (wakeup_waiters)
745 wake_up(&md->wait);
746
747 if (run_queue)
748 blk_run_queue(q);
749
750 /*
751 * dm_put() must be at the end of this function. See the comment above
752 */
753 dm_put(md);
754}
755
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100756static void free_rq_clone(struct request *clone)
757{
758 struct dm_rq_target_io *tio = clone->end_io_data;
759
760 blk_rq_unprep_clone(clone);
761 free_rq_tio(tio);
762}
763
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100764static void dm_unprep_request(struct request *rq)
765{
766 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100767
768 rq->special = NULL;
769 rq->cmd_flags &= ~REQ_DONTPREP;
770
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100771 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100772}
773
774/*
775 * Requeue the original request of a clone.
776 */
777void dm_requeue_unmapped_request(struct request *clone)
778{
779 struct dm_rq_target_io *tio = clone->end_io_data;
780 struct mapped_device *md = tio->md;
781 struct request *rq = tio->orig;
782 struct request_queue *q = rq->q;
783 unsigned long flags;
784
785 dm_unprep_request(rq);
786
787 spin_lock_irqsave(q->queue_lock, flags);
788 if (elv_queue_empty(q))
789 blk_plug_device(q);
790 blk_requeue_request(q, rq);
791 spin_unlock_irqrestore(q->queue_lock, flags);
792
793 rq_completed(md, 0);
794}
795EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
796
797static void __stop_queue(struct request_queue *q)
798{
799 blk_stop_queue(q);
800}
801
802static void stop_queue(struct request_queue *q)
803{
804 unsigned long flags;
805
806 spin_lock_irqsave(q->queue_lock, flags);
807 __stop_queue(q);
808 spin_unlock_irqrestore(q->queue_lock, flags);
809}
810
811static void __start_queue(struct request_queue *q)
812{
813 if (blk_queue_stopped(q))
814 blk_start_queue(q);
815}
816
817static void start_queue(struct request_queue *q)
818{
819 unsigned long flags;
820
821 spin_lock_irqsave(q->queue_lock, flags);
822 __start_queue(q);
823 spin_unlock_irqrestore(q->queue_lock, flags);
824}
825
826/*
827 * Complete the clone and the original request.
828 * Must be called without queue lock.
829 */
830static void dm_end_request(struct request *clone, int error)
831{
832 struct dm_rq_target_io *tio = clone->end_io_data;
833 struct mapped_device *md = tio->md;
834 struct request *rq = tio->orig;
835
836 if (blk_pc_request(rq)) {
837 rq->errors = clone->errors;
838 rq->resid_len = clone->resid_len;
839
840 if (rq->sense)
841 /*
842 * We are using the sense buffer of the original
843 * request.
844 * So setting the length of the sense data is enough.
845 */
846 rq->sense_len = clone->sense_len;
847 }
848
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100849 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100850
851 blk_end_request_all(rq, error);
852
853 rq_completed(md, 1);
854}
855
856/*
857 * Request completion handler for request-based dm
858 */
859static void dm_softirq_done(struct request *rq)
860{
861 struct request *clone = rq->completion_data;
862 struct dm_rq_target_io *tio = clone->end_io_data;
863 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
864 int error = tio->error;
865
866 if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
867 error = rq_end_io(tio->ti, clone, error, &tio->info);
868
869 if (error <= 0)
870 /* The target wants to complete the I/O */
871 dm_end_request(clone, error);
872 else if (error == DM_ENDIO_INCOMPLETE)
873 /* The target will handle the I/O */
874 return;
875 else if (error == DM_ENDIO_REQUEUE)
876 /* The target wants to requeue the I/O */
877 dm_requeue_unmapped_request(clone);
878 else {
879 DMWARN("unimplemented target endio return value: %d", error);
880 BUG();
881 }
882}
883
884/*
885 * Complete the clone and the original request with the error status
886 * through softirq context.
887 */
888static void dm_complete_request(struct request *clone, int error)
889{
890 struct dm_rq_target_io *tio = clone->end_io_data;
891 struct request *rq = tio->orig;
892
893 tio->error = error;
894 rq->completion_data = clone;
895 blk_complete_request(rq);
896}
897
898/*
899 * Complete the not-mapped clone and the original request with the error status
900 * through softirq context.
901 * Target's rq_end_io() function isn't called.
902 * This may be used when the target's map_rq() function fails.
903 */
904void dm_kill_unmapped_request(struct request *clone, int error)
905{
906 struct dm_rq_target_io *tio = clone->end_io_data;
907 struct request *rq = tio->orig;
908
909 rq->cmd_flags |= REQ_FAILED;
910 dm_complete_request(clone, error);
911}
912EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
913
914/*
915 * Called with the queue lock held
916 */
917static void end_clone_request(struct request *clone, int error)
918{
919 /*
920 * For just cleaning up the information of the queue in which
921 * the clone was dispatched.
922 * The clone is *NOT* freed actually here because it is alloced from
923 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
924 */
925 __blk_put_request(clone->q, clone);
926
927 /*
928 * Actual request completion is done in a softirq context which doesn't
929 * hold the queue lock. Otherwise, deadlock could occur because:
930 * - another request may be submitted by the upper level driver
931 * of the stacking during the completion
932 * - the submission which requires queue lock may be done
933 * against this queue
934 */
935 dm_complete_request(clone, error);
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938static sector_t max_io_len(struct mapped_device *md,
939 sector_t sector, struct dm_target *ti)
940{
941 sector_t offset = sector - ti->begin;
942 sector_t len = ti->len - offset;
943
944 /*
945 * Does the target need to split even further ?
946 */
947 if (ti->split_io) {
948 sector_t boundary;
949 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
950 - offset;
951 if (len > boundary)
952 len = boundary;
953 }
954
955 return len;
956}
957
958static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100959 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100962 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700963 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 clone->bi_end_io = clone_endio;
966 clone->bi_private = tio;
967
968 /*
969 * Map the clone. If r == 0 we don't need to do
970 * anything, the target has assumed ownership of
971 * this io.
972 */
973 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100974 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800976 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100978
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100979 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400980 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800983 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
984 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700985 md = tio->io->md;
986 dec_pending(tio->io, r);
987 /*
988 * Store bio_set for cleanup.
989 */
990 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700992 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800993 } else if (r) {
994 DMWARN("unimplemented target map return value: %d", r);
995 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997}
998
999struct clone_info {
1000 struct mapped_device *md;
1001 struct dm_table *map;
1002 struct bio *bio;
1003 struct dm_io *io;
1004 sector_t sector;
1005 sector_t sector_count;
1006 unsigned short idx;
1007};
1008
Peter Osterlund36763472005-09-06 15:16:42 -07001009static void dm_bio_destructor(struct bio *bio)
1010{
Stefan Bader9faf4002006-10-03 01:15:41 -07001011 struct bio_set *bs = bio->bi_private;
1012
1013 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/*
1017 * Creates a little bio that is just does part of a bvec.
1018 */
1019static struct bio *split_bvec(struct bio *bio, sector_t sector,
1020 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001021 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 struct bio *clone;
1024 struct bio_vec *bv = bio->bi_io_vec + idx;
1025
Stefan Bader9faf4002006-10-03 01:15:41 -07001026 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001027 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 *clone->bi_io_vec = *bv;
1029
1030 clone->bi_sector = sector;
1031 clone->bi_bdev = bio->bi_bdev;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001032 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 clone->bi_vcnt = 1;
1034 clone->bi_size = to_bytes(len);
1035 clone->bi_io_vec->bv_offset = offset;
1036 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001037 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Martin K. Petersen9c470082009-04-09 00:27:12 +01001039 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001040 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001041 bio_integrity_trim(clone,
1042 bio_sector_offset(bio, idx, offset), len);
1043 }
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return clone;
1046}
1047
1048/*
1049 * Creates a bio that consists of range of complete bvecs.
1050 */
1051static struct bio *clone_bio(struct bio *bio, sector_t sector,
1052 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001053 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct bio *clone;
1056
Stefan Bader9faf4002006-10-03 01:15:41 -07001057 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1058 __bio_clone(clone, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001059 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
Stefan Bader9faf4002006-10-03 01:15:41 -07001060 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 clone->bi_sector = sector;
1062 clone->bi_idx = idx;
1063 clone->bi_vcnt = idx + bv_count;
1064 clone->bi_size = to_bytes(len);
1065 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1066
Martin K. Petersen9c470082009-04-09 00:27:12 +01001067 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001068 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001069
1070 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1071 bio_integrity_trim(clone,
1072 bio_sector_offset(bio, idx, 0), len);
1073 }
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return clone;
1076}
1077
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001078static struct dm_target_io *alloc_tio(struct clone_info *ci,
1079 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001080{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001081 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001082
1083 tio->io = ci->io;
1084 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001085 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001086
1087 return tio;
1088}
1089
1090static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1091 unsigned flush_nr)
1092{
1093 struct dm_target_io *tio = alloc_tio(ci, ti);
1094 struct bio *clone;
1095
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001096 tio->info.flush_request = flush_nr;
1097
1098 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1099 __bio_clone(clone, ci->bio);
1100 clone->bi_destructor = dm_bio_destructor;
1101
1102 __map_bio(ti, clone, tio);
1103}
1104
1105static int __clone_and_map_empty_barrier(struct clone_info *ci)
1106{
1107 unsigned target_nr = 0, flush_nr;
1108 struct dm_target *ti;
1109
1110 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1111 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1112 flush_nr++)
1113 __flush_target(ci, ti, flush_nr);
1114
1115 ci->sector_count = 0;
1116
1117 return 0;
1118}
1119
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001120static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001123 struct dm_target *ti;
1124 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001125 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001127 if (unlikely(bio_empty_barrier(bio)))
1128 return __clone_and_map_empty_barrier(ci);
1129
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001130 ti = dm_table_find_target(ci->map, ci->sector);
1131 if (!dm_target_is_valid(ti))
1132 return -EIO;
1133
1134 max = max_io_len(ci->md, ci->sector, ti);
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /*
1137 * Allocate a target io object.
1138 */
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001139 tio = alloc_tio(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (ci->sector_count <= max) {
1142 /*
1143 * Optimise for the simple case where we can do all of
1144 * the remaining io with a single clone.
1145 */
1146 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001147 bio->bi_vcnt - ci->idx, ci->sector_count,
1148 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 __map_bio(ti, clone, tio);
1150 ci->sector_count = 0;
1151
1152 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1153 /*
1154 * There are some bvecs that don't span targets.
1155 * Do as many of these as possible.
1156 */
1157 int i;
1158 sector_t remaining = max;
1159 sector_t bv_len;
1160
1161 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1162 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1163
1164 if (bv_len > remaining)
1165 break;
1166
1167 remaining -= bv_len;
1168 len += bv_len;
1169 }
1170
Stefan Bader9faf4002006-10-03 01:15:41 -07001171 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1172 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 __map_bio(ti, clone, tio);
1174
1175 ci->sector += len;
1176 ci->sector_count -= len;
1177 ci->idx = i;
1178
1179 } else {
1180 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001181 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
1183 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001184 sector_t remaining = to_sector(bv->bv_len);
1185 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001187 do {
1188 if (offset) {
1189 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001190 if (!dm_target_is_valid(ti))
1191 return -EIO;
1192
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001193 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001195 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001198 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001200 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001201 bv->bv_offset + offset, len,
1202 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001203
1204 __map_bio(ti, clone, tio);
1205
1206 ci->sector += len;
1207 ci->sector_count -= len;
1208 offset += to_bytes(len);
1209 } while (remaining -= len);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 ci->idx++;
1212 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001213
1214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001218 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001220static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001223 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 ci.map = dm_get_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001226 if (unlikely(!ci.map)) {
Jens Axboe1f98a132009-09-11 14:32:04 +02001227 if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001228 bio_io_error(bio);
1229 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001230 if (!md->barrier_error)
1231 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001232 return;
1233 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ci.md = md;
1236 ci.bio = bio;
1237 ci.io = alloc_io(md);
1238 ci.io->error = 0;
1239 atomic_set(&ci.io->io_count, 1);
1240 ci.io->bio = bio;
1241 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001242 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 ci.sector = bio->bi_sector;
1244 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001245 if (unlikely(bio_empty_barrier(bio)))
1246 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 ci.idx = bio->bi_idx;
1248
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001249 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001250 while (ci.sector_count && !error)
1251 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001254 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 dm_table_put(ci.map);
1256}
1257/*-----------------------------------------------------------------
1258 * CRUD END
1259 *---------------------------------------------------------------*/
1260
Milan Brozf6fccb12008-07-21 12:00:37 +01001261static int dm_merge_bvec(struct request_queue *q,
1262 struct bvec_merge_data *bvm,
1263 struct bio_vec *biovec)
1264{
1265 struct mapped_device *md = q->queuedata;
1266 struct dm_table *map = dm_get_table(md);
1267 struct dm_target *ti;
1268 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001269 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001270
1271 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001272 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001273
1274 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001275 if (!dm_target_is_valid(ti))
1276 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001277
1278 /*
1279 * Find maximum amount of I/O that won't need splitting
1280 */
1281 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1282 (sector_t) BIO_MAX_SECTORS);
1283 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1284 if (max_size < 0)
1285 max_size = 0;
1286
1287 /*
1288 * merge_bvec_fn() returns number of bytes
1289 * it can accept at this offset
1290 * max is precomputed maximal io size
1291 */
1292 if (max_size && ti->type->merge)
1293 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001294 /*
1295 * If the target doesn't support merge method and some of the devices
1296 * provided their merge_bvec method (we know this by looking at
1297 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1298 * entries. So always set max_size to 0, and the code below allows
1299 * just one page.
1300 */
1301 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1302
1303 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001304
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001305out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001306 dm_table_put(map);
1307
1308out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001309 /*
1310 * Always allow an entire first page
1311 */
1312 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1313 max_size = biovec->bv_len;
1314
Milan Brozf6fccb12008-07-21 12:00:37 +01001315 return max_size;
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318/*
1319 * The request function that just remaps the bio built up by
1320 * dm_merge_bvec.
1321 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001322static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Kevin Corry12f03a42006-02-01 03:04:52 -08001324 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001326 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001328 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Tejun Heo074a7ac2008-08-25 19:56:14 +09001330 cpu = part_stat_lock();
1331 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1332 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1333 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001336 * If we're suspended or the thread is processing barriers
1337 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001339 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Jens Axboe1f98a132009-09-11 14:32:04 +02001340 unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001341 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001343 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1344 bio_rw(bio) == READA) {
1345 bio_io_error(bio);
1346 return 0;
1347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Mikulas Patocka92c63902009-04-09 00:27:15 +01001349 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Mikulas Patocka92c63902009-04-09 00:27:15 +01001351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001354 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001355 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001359static int dm_make_request(struct request_queue *q, struct bio *bio)
1360{
1361 struct mapped_device *md = q->queuedata;
1362
Jens Axboe1f98a132009-09-11 14:32:04 +02001363 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001364 bio_endio(bio, -EOPNOTSUPP);
1365 return 0;
1366 }
1367
1368 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1369}
1370
1371static int dm_request_based(struct mapped_device *md)
1372{
1373 return blk_queue_stackable(md->queue);
1374}
1375
1376static int dm_request(struct request_queue *q, struct bio *bio)
1377{
1378 struct mapped_device *md = q->queuedata;
1379
1380 if (dm_request_based(md))
1381 return dm_make_request(q, bio);
1382
1383 return _dm_request(q, bio);
1384}
1385
1386void dm_dispatch_request(struct request *rq)
1387{
1388 int r;
1389
1390 if (blk_queue_io_stat(rq->q))
1391 rq->cmd_flags |= REQ_IO_STAT;
1392
1393 rq->start_time = jiffies;
1394 r = blk_insert_cloned_request(rq->q, rq);
1395 if (r)
1396 dm_complete_request(rq, r);
1397}
1398EXPORT_SYMBOL_GPL(dm_dispatch_request);
1399
1400static void dm_rq_bio_destructor(struct bio *bio)
1401{
1402 struct dm_rq_clone_bio_info *info = bio->bi_private;
1403 struct mapped_device *md = info->tio->md;
1404
1405 free_bio_info(info);
1406 bio_free(bio, md->bs);
1407}
1408
1409static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1410 void *data)
1411{
1412 struct dm_rq_target_io *tio = data;
1413 struct mapped_device *md = tio->md;
1414 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1415
1416 if (!info)
1417 return -ENOMEM;
1418
1419 info->orig = bio_orig;
1420 info->tio = tio;
1421 bio->bi_end_io = end_clone_bio;
1422 bio->bi_private = info;
1423 bio->bi_destructor = dm_rq_bio_destructor;
1424
1425 return 0;
1426}
1427
1428static int setup_clone(struct request *clone, struct request *rq,
1429 struct dm_rq_target_io *tio)
1430{
1431 int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1432 dm_rq_bio_constructor, tio);
1433
1434 if (r)
1435 return r;
1436
1437 clone->cmd = rq->cmd;
1438 clone->cmd_len = rq->cmd_len;
1439 clone->sense = rq->sense;
1440 clone->buffer = rq->buffer;
1441 clone->end_io = end_clone_request;
1442 clone->end_io_data = tio;
1443
1444 return 0;
1445}
1446
1447static int dm_rq_flush_suspending(struct mapped_device *md)
1448{
1449 return !md->suspend_rq.special;
1450}
1451
1452/*
1453 * Called with the queue lock held.
1454 */
1455static int dm_prep_fn(struct request_queue *q, struct request *rq)
1456{
1457 struct mapped_device *md = q->queuedata;
1458 struct dm_rq_target_io *tio;
1459 struct request *clone;
1460
1461 if (unlikely(rq == &md->suspend_rq)) {
1462 if (dm_rq_flush_suspending(md))
1463 return BLKPREP_OK;
1464 else
1465 /* The flush suspend was interrupted */
1466 return BLKPREP_KILL;
1467 }
1468
1469 if (unlikely(rq->special)) {
1470 DMWARN("Already has something in rq->special.");
1471 return BLKPREP_KILL;
1472 }
1473
1474 tio = alloc_rq_tio(md); /* Only one for each original request */
1475 if (!tio)
1476 /* -ENOMEM */
1477 return BLKPREP_DEFER;
1478
1479 tio->md = md;
1480 tio->ti = NULL;
1481 tio->orig = rq;
1482 tio->error = 0;
1483 memset(&tio->info, 0, sizeof(tio->info));
1484
1485 clone = &tio->clone;
1486 if (setup_clone(clone, rq, tio)) {
1487 /* -ENOMEM */
1488 free_rq_tio(tio);
1489 return BLKPREP_DEFER;
1490 }
1491
1492 rq->special = clone;
1493 rq->cmd_flags |= REQ_DONTPREP;
1494
1495 return BLKPREP_OK;
1496}
1497
Kiyoshi Ueda598de402009-12-10 23:52:14 +00001498static void map_request(struct dm_target *ti, struct request *clone,
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001499 struct mapped_device *md)
1500{
1501 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001502 struct dm_rq_target_io *tio = clone->end_io_data;
1503
1504 /*
1505 * Hold the md reference here for the in-flight I/O.
1506 * We can't rely on the reference count by device opener,
1507 * because the device may be closed during the request completion
1508 * when all bios are completed.
1509 * See the comment in rq_completed() too.
1510 */
1511 dm_get(md);
1512
1513 tio->ti = ti;
1514 r = ti->type->map_rq(ti, clone, &tio->info);
1515 switch (r) {
1516 case DM_MAPIO_SUBMITTED:
1517 /* The target has taken the I/O to submit by itself later */
1518 break;
1519 case DM_MAPIO_REMAPPED:
1520 /* The target has remapped the I/O so dispatch it */
1521 dm_dispatch_request(clone);
1522 break;
1523 case DM_MAPIO_REQUEUE:
1524 /* The target wants to requeue the I/O */
1525 dm_requeue_unmapped_request(clone);
1526 break;
1527 default:
1528 if (r > 0) {
1529 DMWARN("unimplemented target map return value: %d", r);
1530 BUG();
1531 }
1532
1533 /* The target wants to complete the I/O */
1534 dm_kill_unmapped_request(clone, r);
1535 break;
1536 }
1537}
1538
1539/*
1540 * q->request_fn for request-based dm.
1541 * Called with the queue lock held.
1542 */
1543static void dm_request_fn(struct request_queue *q)
1544{
1545 struct mapped_device *md = q->queuedata;
1546 struct dm_table *map = dm_get_table(md);
1547 struct dm_target *ti;
1548 struct request *rq;
1549
1550 /*
1551 * For noflush suspend, check blk_queue_stopped() to immediately
1552 * quit I/O dispatching.
1553 */
1554 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1555 rq = blk_peek_request(q);
1556 if (!rq)
1557 goto plug_and_out;
1558
1559 if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1560 if (queue_in_flight(q))
1561 /* Not quiet yet. Wait more */
1562 goto plug_and_out;
1563
1564 /* This device should be quiet now */
1565 __stop_queue(q);
1566 blk_start_request(rq);
1567 __blk_end_request_all(rq, 0);
1568 wake_up(&md->wait);
1569 goto out;
1570 }
1571
1572 ti = dm_table_find_target(map, blk_rq_pos(rq));
1573 if (ti->type->busy && ti->type->busy(ti))
1574 goto plug_and_out;
1575
1576 blk_start_request(rq);
1577 spin_unlock(q->queue_lock);
Kiyoshi Ueda598de402009-12-10 23:52:14 +00001578 map_request(ti, rq->special, md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001579 spin_lock_irq(q->queue_lock);
1580 }
1581
1582 goto out;
1583
1584plug_and_out:
1585 if (!elv_queue_empty(q))
1586 /* Some requests still remain, retry later */
1587 blk_plug_device(q);
1588
1589out:
1590 dm_table_put(map);
1591
1592 return;
1593}
1594
1595int dm_underlying_device_busy(struct request_queue *q)
1596{
1597 return blk_lld_busy(q);
1598}
1599EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1600
1601static int dm_lld_busy(struct request_queue *q)
1602{
1603 int r;
1604 struct mapped_device *md = q->queuedata;
1605 struct dm_table *map = dm_get_table(md);
1606
1607 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1608 r = 1;
1609 else
1610 r = dm_table_any_busy_target(map);
1611
1612 dm_table_put(map);
1613
1614 return r;
1615}
1616
Jens Axboe165125e2007-07-24 09:28:11 +02001617static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct mapped_device *md = q->queuedata;
1620 struct dm_table *map = dm_get_table(md);
1621
1622 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001623 if (dm_request_based(md))
1624 generic_unplug_device(q);
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 dm_table_unplug_all(map);
1627 dm_table_put(map);
1628 }
1629}
1630
1631static int dm_any_congested(void *congested_data, int bdi_bits)
1632{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001633 int r = bdi_bits;
1634 struct mapped_device *md = congested_data;
1635 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001637 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001638 map = dm_get_table(md);
1639 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001640 /*
1641 * Request-based dm cares about only own queue for
1642 * the query about congestion status of request_queue
1643 */
1644 if (dm_request_based(md))
1645 r = md->queue->backing_dev_info.state &
1646 bdi_bits;
1647 else
1648 r = dm_table_any_congested(map, bdi_bits);
1649
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001650 dm_table_put(map);
1651 }
1652 }
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return r;
1655}
1656
1657/*-----------------------------------------------------------------
1658 * An IDR is used to keep track of allocated minor numbers.
1659 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660static DEFINE_IDR(_minor_idr);
1661
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001662static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001664 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001666 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
1669/*
1670 * See if the device with a specific minor # is free.
1671 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001672static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 int r, m;
1675
1676 if (minor >= (1 << MINORBITS))
1677 return -EINVAL;
1678
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001679 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1680 if (!r)
1681 return -ENOMEM;
1682
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001683 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 if (idr_find(&_minor_idr, minor)) {
1686 r = -EBUSY;
1687 goto out;
1688 }
1689
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001690 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001691 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 if (m != minor) {
1695 idr_remove(&_minor_idr, m);
1696 r = -EBUSY;
1697 goto out;
1698 }
1699
1700out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001701 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return r;
1703}
1704
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001705static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001707 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001710 if (!r)
1711 return -ENOMEM;
1712
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001713 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001715 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001716 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 if (m >= (1 << MINORBITS)) {
1720 idr_remove(&_minor_idr, m);
1721 r = -ENOSPC;
1722 goto out;
1723 }
1724
1725 *minor = m;
1726
1727out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001728 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return r;
1730}
1731
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001732static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Mikulas Patocka53d59142009-04-02 19:55:37 +01001734static void dm_wq_work(struct work_struct *work);
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736/*
1737 * Allocate and initialise a blank device with a given minor.
1738 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001739static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001742 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001743 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 if (!md) {
1746 DMWARN("unable to allocate device, out of memory.");
1747 return NULL;
1748 }
1749
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001750 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001751 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001754 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001755 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001756 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001757 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001759 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001761 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001762 mutex_init(&md->suspend_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001763 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 rwlock_init(&md->map_lock);
1765 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001766 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001768 atomic_set(&md->uevent_seq, 0);
1769 INIT_LIST_HEAD(&md->uevent_list);
1770 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001772 md->queue = blk_init_queue(dm_request_fn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001774 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001776 /*
1777 * Request-based dm devices cannot be stacked on top of bio-based dm
1778 * devices. The type of this dm device has not been decided yet,
1779 * although we initialized the queue using blk_init_queue().
1780 * The type is decided at the first table loading time.
1781 * To prevent problematic device stacking, clear the queue flag
1782 * for request stacking support until then.
1783 *
1784 * This queue is new, so no concurrency on the queue_flags.
1785 */
1786 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1787 md->saved_make_request_fn = md->queue->make_request_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 md->queue->queuedata = md;
1789 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1790 md->queue->backing_dev_info.congested_data = md;
1791 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +01001792 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001794 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001795 blk_queue_softirq_done(md->queue, dm_softirq_done);
1796 blk_queue_prep_rq(md->queue, dm_prep_fn);
1797 blk_queue_lld_busy(md->queue, dm_lld_busy);
Stefan Bader9faf4002006-10-03 01:15:41 -07001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 md->disk = alloc_disk(1);
1800 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001801 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001803 atomic_set(&md->pending[0], 0);
1804 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001805 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001806 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001807 init_waitqueue_head(&md->eventq);
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 md->disk->major = _major;
1810 md->disk->first_minor = minor;
1811 md->disk->fops = &dm_blk_dops;
1812 md->disk->queue = md->queue;
1813 md->disk->private_data = md;
1814 sprintf(md->disk->disk_name, "dm-%d", minor);
1815 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001816 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Milan Broz304f3f62008-02-08 02:11:17 +00001818 md->wq = create_singlethread_workqueue("kdmflush");
1819 if (!md->wq)
1820 goto bad_thread;
1821
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001822 md->bdev = bdget_disk(md->disk, 0);
1823 if (!md->bdev)
1824 goto bad_bdev;
1825
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001826 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001827 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001828 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001829 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001830
1831 BUG_ON(old_md != MINOR_ALLOCED);
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return md;
1834
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001835bad_bdev:
1836 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001837bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001838 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001839 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001840bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001841 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001842bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001844bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001845 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001846bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 kfree(md);
1848 return NULL;
1849}
1850
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001851static void unlock_fs(struct mapped_device *md);
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static void free_dev(struct mapped_device *md)
1854{
Tejun Heof331c022008-09-03 09:01:48 +02001855 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001856
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001857 unlock_fs(md);
1858 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001859 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001860 if (md->tio_pool)
1861 mempool_destroy(md->tio_pool);
1862 if (md->io_pool)
1863 mempool_destroy(md->io_pool);
1864 if (md->bs)
1865 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001866 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001868 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001869
1870 spin_lock(&_minor_lock);
1871 md->disk->private_data = NULL;
1872 spin_unlock(&_minor_lock);
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001875 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001876 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 kfree(md);
1878}
1879
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001880static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1881{
1882 struct dm_md_mempools *p;
1883
1884 if (md->io_pool && md->tio_pool && md->bs)
1885 /* the md already has necessary mempools */
1886 goto out;
1887
1888 p = dm_table_get_md_mempools(t);
1889 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1890
1891 md->io_pool = p->io_pool;
1892 p->io_pool = NULL;
1893 md->tio_pool = p->tio_pool;
1894 p->tio_pool = NULL;
1895 md->bs = p->bs;
1896 p->bs = NULL;
1897
1898out:
1899 /* mempool bind completed, now no need any mempools in the table */
1900 dm_table_free_md_mempools(t);
1901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/*
1904 * Bind a table to the device.
1905 */
1906static void event_callback(void *context)
1907{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001908 unsigned long flags;
1909 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct mapped_device *md = (struct mapped_device *) context;
1911
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001912 spin_lock_irqsave(&md->uevent_lock, flags);
1913 list_splice_init(&md->uevent_list, &uevents);
1914 spin_unlock_irqrestore(&md->uevent_lock, flags);
1915
Tejun Heoed9e1982008-08-25 19:56:05 +09001916 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 atomic_inc(&md->event_nr);
1919 wake_up(&md->eventq);
1920}
1921
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001922static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001924 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001926 mutex_lock(&md->bdev->bd_inode->i_mutex);
1927 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1928 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001931static int __bind(struct mapped_device *md, struct dm_table *t,
1932 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Jens Axboe165125e2007-07-24 09:28:11 +02001934 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001936 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001939
1940 /*
1941 * Wipe any geometry if the size of the table changed.
1942 */
1943 if (size != get_capacity(md->disk))
1944 memset(&md->geometry, 0, sizeof(md->geometry));
1945
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001946 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Mikulas Patockad5816872009-01-06 03:05:10 +00001948 if (!size) {
1949 dm_table_destroy(t);
1950 return 0;
1951 }
1952
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001953 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001954
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001955 /*
1956 * The queue hasn't been stopped yet, if the old table type wasn't
1957 * for request-based during suspension. So stop it to prevent
1958 * I/O mapping before resume.
1959 * This must be done before setting the queue restrictions,
1960 * because request-based dm may be run just after the setting.
1961 */
1962 if (dm_table_request_based(t) && !blk_queue_stopped(q))
1963 stop_queue(q);
1964
1965 __bind_mempools(md, t);
1966
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001967 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001968 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001969 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001970 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return 0;
1973}
1974
1975static void __unbind(struct mapped_device *md)
1976{
1977 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001978 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 if (!map)
1981 return;
1982
1983 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001984 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001986 write_unlock_irqrestore(&md->map_lock, flags);
Mikulas Patockad5816872009-01-06 03:05:10 +00001987 dm_table_destroy(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
1990/*
1991 * Constructor for a new device.
1992 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001993int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct mapped_device *md;
1996
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001997 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (!md)
1999 return -ENXIO;
2000
Milan Broz784aae72009-01-06 03:05:12 +00002001 dm_sysfs_init(md);
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 *result = md;
2004 return 0;
2005}
2006
David Teigland637842c2006-01-06 00:20:00 -08002007static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 unsigned minor = MINOR(dev);
2011
2012 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2013 return NULL;
2014
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002015 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002018 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002019 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002020 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002021 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002022 goto out;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002025out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002026 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
David Teigland637842c2006-01-06 00:20:00 -08002028 return md;
2029}
2030
David Teiglandd229a952006-01-06 00:20:01 -08002031struct mapped_device *dm_get_md(dev_t dev)
2032{
2033 struct mapped_device *md = dm_find_md(dev);
2034
2035 if (md)
2036 dm_get(md);
2037
2038 return md;
2039}
2040
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002041void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002042{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002043 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046void dm_set_mdptr(struct mapped_device *md, void *ptr)
2047{
2048 md->interface_ptr = ptr;
2049}
2050
2051void dm_get(struct mapped_device *md)
2052{
2053 atomic_inc(&md->holders);
2054}
2055
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002056const char *dm_device_name(struct mapped_device *md)
2057{
2058 return md->name;
2059}
2060EXPORT_SYMBOL_GPL(dm_device_name);
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062void dm_put(struct mapped_device *md)
2063{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002064 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002066 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2067
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002068 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08002069 map = dm_get_table(md);
Tejun Heof331c022008-09-03 09:01:48 +02002070 idr_replace(&_minor_idr, MINOR_ALLOCED,
2071 MINOR(disk_devt(dm_disk(md))));
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002072 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002073 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002074 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 dm_table_presuspend_targets(map);
2076 dm_table_postsuspend_targets(map);
2077 }
Milan Broz784aae72009-01-06 03:05:12 +00002078 dm_sysfs_exit(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002079 dm_table_put(map);
Mikulas Patockaa1b51e92009-01-06 03:04:53 +00002080 __unbind(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 free_dev(md);
2082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
Edward Goggin79eb8852007-05-09 02:32:56 -07002084EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Mikulas Patocka401600d2009-04-02 19:55:38 +01002086static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002087{
2088 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002089 DECLARE_WAITQUEUE(wait, current);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002090 struct request_queue *q = md->queue;
2091 unsigned long flags;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002092
2093 dm_unplug_all(md->queue);
2094
2095 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002096
2097 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002098 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002099
2100 smp_mb();
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002101 if (dm_request_based(md)) {
2102 spin_lock_irqsave(q->queue_lock, flags);
2103 if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2104 spin_unlock_irqrestore(q->queue_lock, flags);
2105 break;
2106 }
2107 spin_unlock_irqrestore(q->queue_lock, flags);
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +00002108 } else if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002109 break;
2110
Mikulas Patocka401600d2009-04-02 19:55:38 +01002111 if (interruptible == TASK_INTERRUPTIBLE &&
2112 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002113 r = -EINTR;
2114 break;
2115 }
2116
2117 io_schedule();
2118 }
2119 set_current_state(TASK_RUNNING);
2120
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002121 remove_wait_queue(&md->wait, &wait);
2122
Milan Broz46125c12008-02-08 02:10:30 +00002123 return r;
2124}
2125
Mikulas Patocka531fe962009-06-22 10:12:17 +01002126static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002127{
2128 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002129
2130 bio_init(&md->barrier_bio);
2131 md->barrier_bio.bi_bdev = md->bdev;
2132 md->barrier_bio.bi_rw = WRITE_BARRIER;
2133 __split_and_process_bio(md, &md->barrier_bio);
2134
2135 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002136}
2137
2138static void process_barrier(struct mapped_device *md, struct bio *bio)
2139{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002140 md->barrier_error = 0;
2141
Mikulas Patocka531fe962009-06-22 10:12:17 +01002142 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002143
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002144 if (!bio_empty_barrier(bio)) {
2145 __split_and_process_bio(md, bio);
2146 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002147 }
2148
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002149 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002150 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002151 else {
2152 spin_lock_irq(&md->deferred_lock);
2153 bio_list_add_head(&md->deferred, bio);
2154 spin_unlock_irq(&md->deferred_lock);
2155 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002156}
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158/*
2159 * Process the deferred bios
2160 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002161static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Mikulas Patockaef208582009-04-02 19:55:38 +01002163 struct mapped_device *md = container_of(work, struct mapped_device,
2164 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002165 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Mikulas Patockaef208582009-04-02 19:55:38 +01002167 down_write(&md->io_lock);
2168
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002169 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002170 spin_lock_irq(&md->deferred_lock);
2171 c = bio_list_pop(&md->deferred);
2172 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002173
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002174 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002175 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002176 break;
2177 }
2178
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002179 up_write(&md->io_lock);
2180
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002181 if (dm_request_based(md))
2182 generic_make_request(c);
2183 else {
Jens Axboe1f98a132009-09-11 14:32:04 +02002184 if (bio_rw_flagged(c, BIO_RW_BARRIER))
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002185 process_barrier(md, c);
2186 else
2187 __split_and_process_bio(md, c);
2188 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002189
2190 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002191 }
Milan Broz73d410c2008-02-08 02:10:25 +00002192
Mikulas Patockaef208582009-04-02 19:55:38 +01002193 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002196static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002197{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002198 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2199 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002200 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203/*
2204 * Swap in a new table (destroying old one).
2205 */
2206int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2207{
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002208 struct queue_limits limits;
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002209 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Daniel Walkere61290a2008-02-08 02:10:08 +00002211 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002214 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002215 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002217 r = dm_calculate_queue_limits(table, &limits);
2218 if (r)
2219 goto out;
2220
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002221 /* cannot change the device type, once a table is bound */
2222 if (md->map &&
2223 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2224 DMWARN("can't change the device type after a table is bound");
2225 goto out;
2226 }
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 __unbind(md);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002229 r = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002231out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002232 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002233 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002236static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2237{
2238 md->suspend_rq.special = (void *)0x1;
2239}
2240
2241static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2242{
2243 struct request_queue *q = md->queue;
2244 unsigned long flags;
2245
2246 spin_lock_irqsave(q->queue_lock, flags);
2247 if (!noflush)
2248 dm_rq_invalidate_suspend_marker(md);
2249 __start_queue(q);
2250 spin_unlock_irqrestore(q->queue_lock, flags);
2251}
2252
2253static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2254{
2255 struct request *rq = &md->suspend_rq;
2256 struct request_queue *q = md->queue;
2257
2258 if (noflush)
2259 stop_queue(q);
2260 else {
2261 blk_rq_init(q, rq);
2262 blk_insert_request(q, rq, 0, NULL);
2263 }
2264}
2265
2266static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2267{
2268 int r = 1;
2269 struct request *rq = &md->suspend_rq;
2270 struct request_queue *q = md->queue;
2271 unsigned long flags;
2272
2273 if (noflush)
2274 return r;
2275
2276 /* The marker must be protected by queue lock if it is in use */
2277 spin_lock_irqsave(q->queue_lock, flags);
2278 if (unlikely(rq->ref_count)) {
2279 /*
2280 * This can happen, when the previous flush suspend was
2281 * interrupted, the marker is still in the queue and
2282 * this flush suspend has been invoked, because we don't
2283 * remove the marker at the time of suspend interruption.
2284 * We have only one marker per mapped_device, so we can't
2285 * start another flush suspend while it is in use.
2286 */
2287 BUG_ON(!rq->special); /* The marker should be invalidated */
2288 DMWARN("Invalidating the previous flush suspend is still in"
2289 " progress. Please retry later.");
2290 r = 0;
2291 }
2292 spin_unlock_irqrestore(q->queue_lock, flags);
2293
2294 return r;
2295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297/*
2298 * Functions to lock and unlock any filesystem running on the
2299 * device.
2300 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002301static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002303 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002306
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002307 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002308 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002309 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002310 md->frozen_sb = NULL;
2311 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002312 }
2313
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002314 set_bit(DMF_FROZEN, &md->flags);
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return 0;
2317}
2318
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002319static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002321 if (!test_bit(DMF_FROZEN, &md->flags))
2322 return;
2323
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002324 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002326 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
2329/*
2330 * We need to be able to change a mapping table under a mounted
2331 * filesystem. For example we might want to move some data in
2332 * the background. Before the table can be swapped with
2333 * dm_bind_table, dm_suspend must be called to flush any in
2334 * flight bios and ensure that any further io gets deferred.
2335 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002336/*
2337 * Suspend mechanism in request-based dm.
2338 *
2339 * After the suspend starts, further incoming requests are kept in
2340 * the request_queue and deferred.
2341 * Remaining requests in the request_queue at the start of suspend are flushed
2342 * if it is flush suspend.
2343 * The suspend completes when the following conditions have been satisfied,
2344 * so wait for it:
2345 * 1. q->in_flight is 0 (which means no in_flight request)
2346 * 2. queue has been stopped (which means no request dispatching)
2347 *
2348 *
2349 * Noflush suspend
2350 * ---------------
2351 * Noflush suspend doesn't need to dispatch remaining requests.
2352 * So stop the queue immediately. Then, wait for all in_flight requests
2353 * to be completed or requeued.
2354 *
2355 * To abort noflush suspend, start the queue.
2356 *
2357 *
2358 * Flush suspend
2359 * -------------
2360 * Flush suspend needs to dispatch remaining requests. So stop the queue
2361 * after the remaining requests are completed. (Requeued request must be also
2362 * re-dispatched and completed. Until then, we can't stop the queue.)
2363 *
2364 * During flushing the remaining requests, further incoming requests are also
2365 * inserted to the same queue. To distinguish which requests are to be
2366 * flushed, we insert a marker request to the queue at the time of starting
2367 * flush suspend, like a barrier.
2368 * The dispatching is blocked when the marker is found on the top of the queue.
2369 * And the queue is stopped when all in_flight requests are completed, since
2370 * that means the remaining requests are completely flushed.
2371 * Then, the marker is removed from the queue.
2372 *
2373 * To abort flush suspend, we also need to take care of the marker, not only
2374 * starting the queue.
2375 * We don't remove the marker forcibly from the queue since it's against
2376 * the block-layer manner. Instead, we put a invalidated mark on the marker.
2377 * When the invalidated marker is found on the top of the queue, it is
2378 * immediately removed from the queue, so it doesn't block dispatching.
2379 * Because we have only one marker per mapped_device, we can't start another
2380 * flush suspend until the invalidated marker is removed from the queue.
2381 * So fail and return with -EBUSY in such a case.
2382 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002383int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002385 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002386 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002387 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002388 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Daniel Walkere61290a2008-02-08 02:10:08 +00002390 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002391
Milan Broz73d410c2008-02-08 02:10:25 +00002392 if (dm_suspended(md)) {
2393 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002394 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002397 if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2398 r = -EBUSY;
2399 goto out_unlock;
2400 }
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002404 /*
2405 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2406 * This flag is cleared before dm_suspend returns.
2407 */
2408 if (noflush)
2409 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2410
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002411 /* This does not get reverted if there's an error later. */
2412 dm_table_presuspend_targets(map);
2413
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002414 /*
2415 * Flush I/O to the device. noflush supersedes do_lockfs,
2416 * because lock_fs() needs to flush I/Os.
2417 */
2418 if (!noflush && do_lockfs) {
2419 r = lock_fs(md);
2420 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002421 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002425 * Here we must make sure that no processes are submitting requests
2426 * to target drivers i.e. no one may be executing
2427 * __split_and_process_bio. This is called from dm_request and
2428 * dm_wq_work.
2429 *
2430 * To get all processes out of __split_and_process_bio in dm_request,
2431 * we take the write lock. To prevent any process from reentering
2432 * __split_and_process_bio from dm_request, we set
2433 * DMF_QUEUE_IO_TO_THREAD.
2434 *
2435 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2436 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2437 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2438 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002440 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002441 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2442 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002443 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002445 flush_workqueue(md->wq);
2446
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002447 if (dm_request_based(md))
2448 dm_rq_start_suspend(md, noflush);
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002451 * At this point no more requests are entering target request routines.
2452 * We call dm_wait_for_completion to wait for all existing requests
2453 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002455 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002457 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002458 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002459 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002460 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002463 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002464 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002465
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002466 if (dm_request_based(md))
2467 dm_rq_abort_suspend(md, noflush);
2468
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002469 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002470 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002471 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002472
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002473 /*
2474 * If dm_wait_for_completion returned 0, the device is completely
2475 * quiescent now. There is no request-processing activity. All new
2476 * requests are being added to md->deferred list.
2477 */
2478
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002479 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 set_bit(DMF_SUSPENDED, &md->flags);
2482
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002483out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002485
2486out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002487 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002488 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
2491int dm_resume(struct mapped_device *md)
2492{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002493 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002494 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Daniel Walkere61290a2008-02-08 02:10:08 +00002496 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002497 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002498 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002499
2500 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002501 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Milan Broz8757b772006-10-03 01:15:36 -07002504 r = dm_table_resume_targets(map);
2505 if (r)
2506 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002507
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002508 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002509
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002510 /*
2511 * Flushing deferred I/Os must be done after targets are resumed
2512 * so that mapping of targets can work correctly.
2513 * Request-based dm is queueing the deferred I/Os in its request_queue.
2514 */
2515 if (dm_request_based(md))
2516 start_queue(md->queue);
2517
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002518 unlock_fs(md);
2519
2520 clear_bit(DMF_SUSPENDED, &md->flags);
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002523 r = 0;
2524out:
2525 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002526 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002527
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002528 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
2531/*-----------------------------------------------------------------
2532 * Event notification.
2533 *---------------------------------------------------------------*/
Milan Broz60935eb2009-06-22 10:12:30 +01002534void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2535 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002536{
Milan Broz60935eb2009-06-22 10:12:30 +01002537 char udev_cookie[DM_COOKIE_LENGTH];
2538 char *envp[] = { udev_cookie, NULL };
2539
2540 if (!cookie)
2541 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2542 else {
2543 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2544 DM_COOKIE_ENV_VAR_NAME, cookie);
2545 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2546 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002547}
2548
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002549uint32_t dm_next_uevent_seq(struct mapped_device *md)
2550{
2551 return atomic_add_return(1, &md->uevent_seq);
2552}
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554uint32_t dm_get_event_nr(struct mapped_device *md)
2555{
2556 return atomic_read(&md->event_nr);
2557}
2558
2559int dm_wait_event(struct mapped_device *md, int event_nr)
2560{
2561 return wait_event_interruptible(md->eventq,
2562 (event_nr != atomic_read(&md->event_nr)));
2563}
2564
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002565void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2566{
2567 unsigned long flags;
2568
2569 spin_lock_irqsave(&md->uevent_lock, flags);
2570 list_add(elist, &md->uevent_list);
2571 spin_unlock_irqrestore(&md->uevent_lock, flags);
2572}
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574/*
2575 * The gendisk is only valid as long as you have a reference
2576 * count on 'md'.
2577 */
2578struct gendisk *dm_disk(struct mapped_device *md)
2579{
2580 return md->disk;
2581}
2582
Milan Broz784aae72009-01-06 03:05:12 +00002583struct kobject *dm_kobject(struct mapped_device *md)
2584{
2585 return &md->kobj;
2586}
2587
2588/*
2589 * struct mapped_device should not be exported outside of dm.c
2590 * so use this check to verify that kobj is part of md structure
2591 */
2592struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2593{
2594 struct mapped_device *md;
2595
2596 md = container_of(kobj, struct mapped_device, kobj);
2597 if (&md->kobj != kobj)
2598 return NULL;
2599
Milan Broz4d89b7b2009-06-22 10:12:11 +01002600 if (test_bit(DMF_FREEING, &md->flags) ||
2601 test_bit(DMF_DELETING, &md->flags))
2602 return NULL;
2603
Milan Broz784aae72009-01-06 03:05:12 +00002604 dm_get(md);
2605 return md;
2606}
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608int dm_suspended(struct mapped_device *md)
2609{
2610 return test_bit(DMF_SUSPENDED, &md->flags);
2611}
2612
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002613int dm_noflush_suspending(struct dm_target *ti)
2614{
2615 struct mapped_device *md = dm_table_get_md(ti->table);
2616 int r = __noflush_suspending(md);
2617
2618 dm_put(md);
2619
2620 return r;
2621}
2622EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2623
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002624struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2625{
2626 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2627
2628 if (!pools)
2629 return NULL;
2630
2631 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2632 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2633 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2634 if (!pools->io_pool)
2635 goto free_pools_and_out;
2636
2637 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2638 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2639 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2640 if (!pools->tio_pool)
2641 goto free_io_pool_and_out;
2642
2643 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2644 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2645 if (!pools->bs)
2646 goto free_tio_pool_and_out;
2647
2648 return pools;
2649
2650free_tio_pool_and_out:
2651 mempool_destroy(pools->tio_pool);
2652
2653free_io_pool_and_out:
2654 mempool_destroy(pools->io_pool);
2655
2656free_pools_and_out:
2657 kfree(pools);
2658
2659 return NULL;
2660}
2661
2662void dm_free_md_mempools(struct dm_md_mempools *pools)
2663{
2664 if (!pools)
2665 return;
2666
2667 if (pools->io_pool)
2668 mempool_destroy(pools->io_pool);
2669
2670 if (pools->tio_pool)
2671 mempool_destroy(pools->tio_pool);
2672
2673 if (pools->bs)
2674 bioset_free(pools->bs);
2675
2676 kfree(pools);
2677}
2678
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002679static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .open = dm_blk_open,
2681 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002682 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002683 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 .owner = THIS_MODULE
2685};
2686
2687EXPORT_SYMBOL(dm_get_mapinfo);
2688
2689/*
2690 * module hooks
2691 */
2692module_init(dm_init);
2693module_exit(dm_exit);
2694
2695module_param(major, uint, 0);
2696MODULE_PARM_DESC(major, "The major number of the device mapper");
2697MODULE_DESCRIPTION(DM_NAME " driver");
2698MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2699MODULE_LICENSE("GPL");