blob: 01d741a0c07916161702f2633cb4116ce4897f02 [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 /* For saving the address of __make_request for request based dm */
182 make_request_fn *saved_make_request_fn;
183
Milan Broz784aae72009-01-06 03:05:12 +0000184 /* sysfs handle */
185 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100186
187 /* zero-length barrier that will be cloned and submitted to targets */
188 struct bio barrier_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100191/*
192 * For mempools pre-allocation at the table loading time.
193 */
194struct dm_md_mempools {
195 mempool_t *io_pool;
196 mempool_t *tio_pool;
197 struct bio_set *bs;
198};
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800201static struct kmem_cache *_io_cache;
202static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000203static struct kmem_cache *_rq_tio_cache;
204static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static int __init local_init(void)
207{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100208 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100211 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100213 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100216 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100217 if (!_tio_cache)
218 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000220 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
221 if (!_rq_tio_cache)
222 goto out_free_tio_cache;
223
224 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
225 if (!_rq_bio_info_cache)
226 goto out_free_rq_tio_cache;
227
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100228 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100229 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000230 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 _major = major;
233 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100234 if (r < 0)
235 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (!_major)
238 _major = r;
239
240 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100241
242out_uevent_exit:
243 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000244out_free_rq_bio_info_cache:
245 kmem_cache_destroy(_rq_bio_info_cache);
246out_free_rq_tio_cache:
247 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100248out_free_tio_cache:
249 kmem_cache_destroy(_tio_cache);
250out_free_io_cache:
251 kmem_cache_destroy(_io_cache);
252
253 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256static void local_exit(void)
257{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000258 kmem_cache_destroy(_rq_bio_info_cache);
259 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 kmem_cache_destroy(_tio_cache);
261 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700262 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100263 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 _major = 0;
266
267 DMINFO("cleaned up");
268}
269
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000270static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 local_init,
272 dm_target_init,
273 dm_linear_init,
274 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000275 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100276 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dm_interface_init,
278};
279
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000280static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 local_exit,
282 dm_target_exit,
283 dm_linear_exit,
284 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000285 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100286 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dm_interface_exit,
288};
289
290static int __init dm_init(void)
291{
292 const int count = ARRAY_SIZE(_inits);
293
294 int r, i;
295
296 for (i = 0; i < count; i++) {
297 r = _inits[i]();
298 if (r)
299 goto bad;
300 }
301
302 return 0;
303
304 bad:
305 while (i--)
306 _exits[i]();
307
308 return r;
309}
310
311static void __exit dm_exit(void)
312{
313 int i = ARRAY_SIZE(_exits);
314
315 while (i--)
316 _exits[i]();
317}
318
319/*
320 * Block device functions
321 */
Al Virofe5f9f22008-03-02 10:29:31 -0500322static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct mapped_device *md;
325
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700326 spin_lock(&_minor_lock);
327
Al Virofe5f9f22008-03-02 10:29:31 -0500328 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700329 if (!md)
330 goto out;
331
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700332 if (test_bit(DMF_FREEING, &md->flags) ||
333 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700334 md = NULL;
335 goto out;
336 }
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700339 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700340
341out:
342 spin_unlock(&_minor_lock);
343
344 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Al Virofe5f9f22008-03-02 10:29:31 -0500347static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Al Virofe5f9f22008-03-02 10:29:31 -0500349 struct mapped_device *md = disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700350 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dm_put(md);
352 return 0;
353}
354
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700355int dm_open_count(struct mapped_device *md)
356{
357 return atomic_read(&md->open_count);
358}
359
360/*
361 * Guarantees nothing is using the device before it's deleted.
362 */
363int dm_lock_for_deletion(struct mapped_device *md)
364{
365 int r = 0;
366
367 spin_lock(&_minor_lock);
368
369 if (dm_open_count(md))
370 r = -EBUSY;
371 else
372 set_bit(DMF_DELETING, &md->flags);
373
374 spin_unlock(&_minor_lock);
375
376 return r;
377}
378
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800379static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
380{
381 struct mapped_device *md = bdev->bd_disk->private_data;
382
383 return dm_get_geometry(md, geo);
384}
385
Al Virofe5f9f22008-03-02 10:29:31 -0500386static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700387 unsigned int cmd, unsigned long arg)
388{
Al Virofe5f9f22008-03-02 10:29:31 -0500389 struct mapped_device *md = bdev->bd_disk->private_data;
390 struct dm_table *map = dm_get_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700391 struct dm_target *tgt;
392 int r = -ENOTTY;
393
Milan Brozaa129a22006-10-03 01:15:15 -0700394 if (!map || !dm_table_get_size(map))
395 goto out;
396
397 /* We only support devices that have a single target */
398 if (dm_table_get_num_targets(map) != 1)
399 goto out;
400
401 tgt = dm_table_get_target(map, 0);
402
403 if (dm_suspended(md)) {
404 r = -EAGAIN;
405 goto out;
406 }
407
408 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400409 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700410
411out:
412 dm_table_put(map);
413
Milan Brozaa129a22006-10-03 01:15:15 -0700414 return r;
415}
416
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100417static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 return mempool_alloc(md->io_pool, GFP_NOIO);
420}
421
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100422static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 mempool_free(io, md->io_pool);
425}
426
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100427static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 mempool_free(tio, md->tio_pool);
430}
431
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000432static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
433 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100434{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000435 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100436}
437
438static void free_rq_tio(struct dm_rq_target_io *tio)
439{
440 mempool_free(tio, tio->md->tio_pool);
441}
442
443static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
444{
445 return mempool_alloc(md->io_pool, GFP_ATOMIC);
446}
447
448static void free_bio_info(struct dm_rq_clone_bio_info *info)
449{
450 mempool_free(info, info->tio->md->io_pool);
451}
452
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000453static int md_in_flight(struct mapped_device *md)
454{
455 return atomic_read(&md->pending[READ]) +
456 atomic_read(&md->pending[WRITE]);
457}
458
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800459static void start_io_acct(struct dm_io *io)
460{
461 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900462 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200463 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800464
465 io->start_time = jiffies;
466
Tejun Heo074a7ac2008-08-25 19:56:14 +0900467 cpu = part_stat_lock();
468 part_round_stats(cpu, &dm_disk(md)->part0);
469 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200470 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800471}
472
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000473static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800474{
475 struct mapped_device *md = io->md;
476 struct bio *bio = io->bio;
477 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900478 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800479 int rw = bio_data_dir(bio);
480
Tejun Heo074a7ac2008-08-25 19:56:14 +0900481 cpu = part_stat_lock();
482 part_round_stats(cpu, &dm_disk(md)->part0);
483 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
484 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800485
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100486 /*
487 * After this is decremented the bio must not be touched if it is
488 * a barrier.
489 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200490 dm_disk(md)->part0.in_flight[rw] = pending =
491 atomic_dec_return(&md->pending[rw]);
492 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800493
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000494 /* nudge anyone waiting on suspend queue */
495 if (!pending)
496 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/*
500 * Add the bio to the list of deferred io.
501 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100502static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700504 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Mikulas Patocka022c2612009-04-02 19:55:39 +0100506 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100508 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Mikulas Patocka92c63902009-04-09 00:27:15 +0100510 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
511 queue_work(md->wq, &md->work);
512
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700513 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516/*
517 * Everyone (including functions in this file), should use this
518 * function to access the md->map field, and make sure they call
519 * dm_table_put() when finished.
520 */
521struct dm_table *dm_get_table(struct mapped_device *md)
522{
523 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100524 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100526 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 t = md->map;
528 if (t)
529 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100530 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 return t;
533}
534
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800535/*
536 * Get the geometry associated with a dm device
537 */
538int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
539{
540 *geo = md->geometry;
541
542 return 0;
543}
544
545/*
546 * Set the geometry of a device.
547 */
548int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
549{
550 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
551
552 if (geo->start > sz) {
553 DMWARN("Start sector is beyond the geometry limits.");
554 return -EINVAL;
555 }
556
557 md->geometry = *geo;
558
559 return 0;
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*-----------------------------------------------------------------
563 * CRUD START:
564 * A more elegant soln is in the works that uses the queue
565 * merge fn, unfortunately there are a couple of changes to
566 * the block layer that I want to make for this. So in the
567 * interests of getting something for people to use I give
568 * you this clearly demarcated crap.
569 *---------------------------------------------------------------*/
570
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800571static int __noflush_suspending(struct mapped_device *md)
572{
573 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
577 * Decrements the number of outstanding ios that a bio has been
578 * cloned into, completing the original io if necc.
579 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800580static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800582 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000583 int io_error;
584 struct bio *bio;
585 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800586
587 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100588 if (unlikely(error)) {
589 spin_lock_irqsave(&io->endio_lock, flags);
590 if (!(io->error > 0 && __noflush_suspending(md)))
591 io->error = error;
592 spin_unlock_irqrestore(&io->endio_lock, flags);
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800596 if (io->error == DM_ENDIO_REQUEUE) {
597 /*
598 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800599 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100600 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100601 if (__noflush_suspending(md)) {
Jens Axboe1f98a132009-09-11 14:32:04 +0200602 if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100603 bio_list_add_head(&md->deferred,
604 io->bio);
605 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800606 /* noflush suspend was interrupted. */
607 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100608 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800609 }
610
Milan Brozb35f8ca2009-03-16 17:44:36 +0000611 io_error = io->error;
612 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100613
Jens Axboe1f98a132009-09-11 14:32:04 +0200614 if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100615 /*
616 * There can be just one barrier request so we use
617 * a per-device variable for error reporting.
618 * Note that you can't touch the bio after end_io_acct
619 */
Mikulas Patockafdb95722009-06-22 10:12:19 +0100620 if (!md->barrier_error && io_error != -EOPNOTSUPP)
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100621 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100622 end_io_acct(io);
623 } else {
624 end_io_acct(io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000625
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100626 if (io_error != DM_ENDIO_REQUEUE) {
627 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000628
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100629 bio_endio(bio, io_error);
630 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800631 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100632
633 free_io(md, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635}
636
NeilBrown6712ecf2007-09-27 12:47:43 +0200637static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100640 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000641 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700642 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 dm_endio_fn endio = tio->ti->type->end_io;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
646 error = -EIO;
647
648 if (endio) {
649 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800650 if (r < 0 || r == DM_ENDIO_REQUEUE)
651 /*
652 * error and requeue request are handled
653 * in dec_pending().
654 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800656 else if (r == DM_ENDIO_INCOMPLETE)
657 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200658 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800659 else if (r) {
660 DMWARN("unimplemented target endio return value: %d", r);
661 BUG();
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Stefan Bader9faf4002006-10-03 01:15:41 -0700665 /*
666 * Store md for cleanup instead of tio which is about to get freed.
667 */
668 bio->bi_private = md->bs;
669
Stefan Bader9faf4002006-10-03 01:15:41 -0700670 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000671 bio_put(bio);
672 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100675/*
676 * Partial completion handling for request-based dm
677 */
678static void end_clone_bio(struct bio *clone, int error)
679{
680 struct dm_rq_clone_bio_info *info = clone->bi_private;
681 struct dm_rq_target_io *tio = info->tio;
682 struct bio *bio = info->orig;
683 unsigned int nr_bytes = info->orig->bi_size;
684
685 bio_put(clone);
686
687 if (tio->error)
688 /*
689 * An error has already been detected on the request.
690 * Once error occurred, just let clone->end_io() handle
691 * the remainder.
692 */
693 return;
694 else if (error) {
695 /*
696 * Don't notice the error to the upper layer yet.
697 * The error handling decision is made by the target driver,
698 * when the request is completed.
699 */
700 tio->error = error;
701 return;
702 }
703
704 /*
705 * I/O for the bio successfully completed.
706 * Notice the data completion to the upper layer.
707 */
708
709 /*
710 * bios are processed from the head of the list.
711 * So the completing bio should always be rq->bio.
712 * If it's not, something wrong is happening.
713 */
714 if (tio->orig->bio != bio)
715 DMERR("bio completion is going in the middle of the request");
716
717 /*
718 * Update the original request.
719 * Do not use blk_end_request() here, because it may complete
720 * the original request before the clone, and break the ordering.
721 */
722 blk_update_request(tio->orig, 0, nr_bytes);
723}
724
725/*
726 * Don't touch any member of the md after calling this function because
727 * the md may be freed in dm_put() at the end of this function.
728 * Or do dm_get() before calling this function and dm_put() later.
729 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000730static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100731{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000732 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100733
734 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000735 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100736 wake_up(&md->wait);
737
738 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000739 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100740
741 /*
742 * dm_put() must be at the end of this function. See the comment above
743 */
744 dm_put(md);
745}
746
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100747static void free_rq_clone(struct request *clone)
748{
749 struct dm_rq_target_io *tio = clone->end_io_data;
750
751 blk_rq_unprep_clone(clone);
752 free_rq_tio(tio);
753}
754
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100755static void dm_unprep_request(struct request *rq)
756{
757 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100758
759 rq->special = NULL;
760 rq->cmd_flags &= ~REQ_DONTPREP;
761
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100762 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100763}
764
765/*
766 * Requeue the original request of a clone.
767 */
768void dm_requeue_unmapped_request(struct request *clone)
769{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000770 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100771 struct dm_rq_target_io *tio = clone->end_io_data;
772 struct mapped_device *md = tio->md;
773 struct request *rq = tio->orig;
774 struct request_queue *q = rq->q;
775 unsigned long flags;
776
777 dm_unprep_request(rq);
778
779 spin_lock_irqsave(q->queue_lock, flags);
780 if (elv_queue_empty(q))
781 blk_plug_device(q);
782 blk_requeue_request(q, rq);
783 spin_unlock_irqrestore(q->queue_lock, flags);
784
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000785 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100786}
787EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
788
789static void __stop_queue(struct request_queue *q)
790{
791 blk_stop_queue(q);
792}
793
794static void stop_queue(struct request_queue *q)
795{
796 unsigned long flags;
797
798 spin_lock_irqsave(q->queue_lock, flags);
799 __stop_queue(q);
800 spin_unlock_irqrestore(q->queue_lock, flags);
801}
802
803static void __start_queue(struct request_queue *q)
804{
805 if (blk_queue_stopped(q))
806 blk_start_queue(q);
807}
808
809static void start_queue(struct request_queue *q)
810{
811 unsigned long flags;
812
813 spin_lock_irqsave(q->queue_lock, flags);
814 __start_queue(q);
815 spin_unlock_irqrestore(q->queue_lock, flags);
816}
817
818/*
819 * Complete the clone and the original request.
820 * Must be called without queue lock.
821 */
822static void dm_end_request(struct request *clone, int error)
823{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000824 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100825 struct dm_rq_target_io *tio = clone->end_io_data;
826 struct mapped_device *md = tio->md;
827 struct request *rq = tio->orig;
828
829 if (blk_pc_request(rq)) {
830 rq->errors = clone->errors;
831 rq->resid_len = clone->resid_len;
832
833 if (rq->sense)
834 /*
835 * We are using the sense buffer of the original
836 * request.
837 * So setting the length of the sense data is enough.
838 */
839 rq->sense_len = clone->sense_len;
840 }
841
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100842 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100843
844 blk_end_request_all(rq, error);
845
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000846 rq_completed(md, rw, 1);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100847}
848
849/*
850 * Request completion handler for request-based dm
851 */
852static void dm_softirq_done(struct request *rq)
853{
854 struct request *clone = rq->completion_data;
855 struct dm_rq_target_io *tio = clone->end_io_data;
856 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
857 int error = tio->error;
858
859 if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
860 error = rq_end_io(tio->ti, clone, error, &tio->info);
861
862 if (error <= 0)
863 /* The target wants to complete the I/O */
864 dm_end_request(clone, error);
865 else if (error == DM_ENDIO_INCOMPLETE)
866 /* The target will handle the I/O */
867 return;
868 else if (error == DM_ENDIO_REQUEUE)
869 /* The target wants to requeue the I/O */
870 dm_requeue_unmapped_request(clone);
871 else {
872 DMWARN("unimplemented target endio return value: %d", error);
873 BUG();
874 }
875}
876
877/*
878 * Complete the clone and the original request with the error status
879 * through softirq context.
880 */
881static void dm_complete_request(struct request *clone, int error)
882{
883 struct dm_rq_target_io *tio = clone->end_io_data;
884 struct request *rq = tio->orig;
885
886 tio->error = error;
887 rq->completion_data = clone;
888 blk_complete_request(rq);
889}
890
891/*
892 * Complete the not-mapped clone and the original request with the error status
893 * through softirq context.
894 * Target's rq_end_io() function isn't called.
895 * This may be used when the target's map_rq() function fails.
896 */
897void dm_kill_unmapped_request(struct request *clone, int error)
898{
899 struct dm_rq_target_io *tio = clone->end_io_data;
900 struct request *rq = tio->orig;
901
902 rq->cmd_flags |= REQ_FAILED;
903 dm_complete_request(clone, error);
904}
905EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
906
907/*
908 * Called with the queue lock held
909 */
910static void end_clone_request(struct request *clone, int error)
911{
912 /*
913 * For just cleaning up the information of the queue in which
914 * the clone was dispatched.
915 * The clone is *NOT* freed actually here because it is alloced from
916 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
917 */
918 __blk_put_request(clone->q, clone);
919
920 /*
921 * Actual request completion is done in a softirq context which doesn't
922 * hold the queue lock. Otherwise, deadlock could occur because:
923 * - another request may be submitted by the upper level driver
924 * of the stacking during the completion
925 * - the submission which requires queue lock may be done
926 * against this queue
927 */
928 dm_complete_request(clone, error);
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static sector_t max_io_len(struct mapped_device *md,
932 sector_t sector, struct dm_target *ti)
933{
934 sector_t offset = sector - ti->begin;
935 sector_t len = ti->len - offset;
936
937 /*
938 * Does the target need to split even further ?
939 */
940 if (ti->split_io) {
941 sector_t boundary;
942 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
943 - offset;
944 if (len > boundary)
945 len = boundary;
946 }
947
948 return len;
949}
950
951static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100952 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100955 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700956 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 clone->bi_end_io = clone_endio;
959 clone->bi_private = tio;
960
961 /*
962 * Map the clone. If r == 0 we don't need to do
963 * anything, the target has assumed ownership of
964 * this io.
965 */
966 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100967 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800969 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100971
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100972 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400973 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800976 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
977 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700978 md = tio->io->md;
979 dec_pending(tio->io, r);
980 /*
981 * Store bio_set for cleanup.
982 */
983 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700985 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800986 } else if (r) {
987 DMWARN("unimplemented target map return value: %d", r);
988 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990}
991
992struct clone_info {
993 struct mapped_device *md;
994 struct dm_table *map;
995 struct bio *bio;
996 struct dm_io *io;
997 sector_t sector;
998 sector_t sector_count;
999 unsigned short idx;
1000};
1001
Peter Osterlund36763472005-09-06 15:16:42 -07001002static void dm_bio_destructor(struct bio *bio)
1003{
Stefan Bader9faf4002006-10-03 01:15:41 -07001004 struct bio_set *bs = bio->bi_private;
1005
1006 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/*
1010 * Creates a little bio that is just does part of a bvec.
1011 */
1012static struct bio *split_bvec(struct bio *bio, sector_t sector,
1013 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001014 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 struct bio *clone;
1017 struct bio_vec *bv = bio->bi_io_vec + idx;
1018
Stefan Bader9faf4002006-10-03 01:15:41 -07001019 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001020 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 *clone->bi_io_vec = *bv;
1022
1023 clone->bi_sector = sector;
1024 clone->bi_bdev = bio->bi_bdev;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001025 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 clone->bi_vcnt = 1;
1027 clone->bi_size = to_bytes(len);
1028 clone->bi_io_vec->bv_offset = offset;
1029 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001030 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Martin K. Petersen9c470082009-04-09 00:27:12 +01001032 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001033 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001034 bio_integrity_trim(clone,
1035 bio_sector_offset(bio, idx, offset), len);
1036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return clone;
1039}
1040
1041/*
1042 * Creates a bio that consists of range of complete bvecs.
1043 */
1044static struct bio *clone_bio(struct bio *bio, sector_t sector,
1045 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001046 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct bio *clone;
1049
Stefan Bader9faf4002006-10-03 01:15:41 -07001050 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1051 __bio_clone(clone, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001052 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
Stefan Bader9faf4002006-10-03 01:15:41 -07001053 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 clone->bi_sector = sector;
1055 clone->bi_idx = idx;
1056 clone->bi_vcnt = idx + bv_count;
1057 clone->bi_size = to_bytes(len);
1058 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1059
Martin K. Petersen9c470082009-04-09 00:27:12 +01001060 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001061 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001062
1063 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1064 bio_integrity_trim(clone,
1065 bio_sector_offset(bio, idx, 0), len);
1066 }
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return clone;
1069}
1070
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001071static struct dm_target_io *alloc_tio(struct clone_info *ci,
1072 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001073{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001074 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001075
1076 tio->io = ci->io;
1077 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001078 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001079
1080 return tio;
1081}
1082
1083static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1084 unsigned flush_nr)
1085{
1086 struct dm_target_io *tio = alloc_tio(ci, ti);
1087 struct bio *clone;
1088
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001089 tio->info.flush_request = flush_nr;
1090
1091 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1092 __bio_clone(clone, ci->bio);
1093 clone->bi_destructor = dm_bio_destructor;
1094
1095 __map_bio(ti, clone, tio);
1096}
1097
1098static int __clone_and_map_empty_barrier(struct clone_info *ci)
1099{
1100 unsigned target_nr = 0, flush_nr;
1101 struct dm_target *ti;
1102
1103 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1104 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1105 flush_nr++)
1106 __flush_target(ci, ti, flush_nr);
1107
1108 ci->sector_count = 0;
1109
1110 return 0;
1111}
1112
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001113static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001116 struct dm_target *ti;
1117 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001118 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001120 if (unlikely(bio_empty_barrier(bio)))
1121 return __clone_and_map_empty_barrier(ci);
1122
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001123 ti = dm_table_find_target(ci->map, ci->sector);
1124 if (!dm_target_is_valid(ti))
1125 return -EIO;
1126
1127 max = max_io_len(ci->md, ci->sector, ti);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * Allocate a target io object.
1131 */
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001132 tio = alloc_tio(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 if (ci->sector_count <= max) {
1135 /*
1136 * Optimise for the simple case where we can do all of
1137 * the remaining io with a single clone.
1138 */
1139 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001140 bio->bi_vcnt - ci->idx, ci->sector_count,
1141 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 __map_bio(ti, clone, tio);
1143 ci->sector_count = 0;
1144
1145 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1146 /*
1147 * There are some bvecs that don't span targets.
1148 * Do as many of these as possible.
1149 */
1150 int i;
1151 sector_t remaining = max;
1152 sector_t bv_len;
1153
1154 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1155 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1156
1157 if (bv_len > remaining)
1158 break;
1159
1160 remaining -= bv_len;
1161 len += bv_len;
1162 }
1163
Stefan Bader9faf4002006-10-03 01:15:41 -07001164 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1165 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 __map_bio(ti, clone, tio);
1167
1168 ci->sector += len;
1169 ci->sector_count -= len;
1170 ci->idx = i;
1171
1172 } else {
1173 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001174 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
1176 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001177 sector_t remaining = to_sector(bv->bv_len);
1178 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001180 do {
1181 if (offset) {
1182 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001183 if (!dm_target_is_valid(ti))
1184 return -EIO;
1185
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001186 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001188 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001191 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001193 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001194 bv->bv_offset + offset, len,
1195 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001196
1197 __map_bio(ti, clone, tio);
1198
1199 ci->sector += len;
1200 ci->sector_count -= len;
1201 offset += to_bytes(len);
1202 } while (remaining -= len);
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ci->idx++;
1205 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001206
1207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001211 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001213static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001216 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 ci.map = dm_get_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001219 if (unlikely(!ci.map)) {
Jens Axboe1f98a132009-09-11 14:32:04 +02001220 if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001221 bio_io_error(bio);
1222 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001223 if (!md->barrier_error)
1224 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001225 return;
1226 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 ci.md = md;
1229 ci.bio = bio;
1230 ci.io = alloc_io(md);
1231 ci.io->error = 0;
1232 atomic_set(&ci.io->io_count, 1);
1233 ci.io->bio = bio;
1234 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001235 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ci.sector = bio->bi_sector;
1237 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001238 if (unlikely(bio_empty_barrier(bio)))
1239 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ci.idx = bio->bi_idx;
1241
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001242 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001243 while (ci.sector_count && !error)
1244 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001247 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 dm_table_put(ci.map);
1249}
1250/*-----------------------------------------------------------------
1251 * CRUD END
1252 *---------------------------------------------------------------*/
1253
Milan Brozf6fccb12008-07-21 12:00:37 +01001254static int dm_merge_bvec(struct request_queue *q,
1255 struct bvec_merge_data *bvm,
1256 struct bio_vec *biovec)
1257{
1258 struct mapped_device *md = q->queuedata;
1259 struct dm_table *map = dm_get_table(md);
1260 struct dm_target *ti;
1261 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001262 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001263
1264 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001265 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001266
1267 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001268 if (!dm_target_is_valid(ti))
1269 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001270
1271 /*
1272 * Find maximum amount of I/O that won't need splitting
1273 */
1274 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1275 (sector_t) BIO_MAX_SECTORS);
1276 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1277 if (max_size < 0)
1278 max_size = 0;
1279
1280 /*
1281 * merge_bvec_fn() returns number of bytes
1282 * it can accept at this offset
1283 * max is precomputed maximal io size
1284 */
1285 if (max_size && ti->type->merge)
1286 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001287 /*
1288 * If the target doesn't support merge method and some of the devices
1289 * provided their merge_bvec method (we know this by looking at
1290 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1291 * entries. So always set max_size to 0, and the code below allows
1292 * just one page.
1293 */
1294 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1295
1296 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001297
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001298out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001299 dm_table_put(map);
1300
1301out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001302 /*
1303 * Always allow an entire first page
1304 */
1305 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1306 max_size = biovec->bv_len;
1307
Milan Brozf6fccb12008-07-21 12:00:37 +01001308 return max_size;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/*
1312 * The request function that just remaps the bio built up by
1313 * dm_merge_bvec.
1314 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001315static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Kevin Corry12f03a42006-02-01 03:04:52 -08001317 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001319 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001321 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Tejun Heo074a7ac2008-08-25 19:56:14 +09001323 cpu = part_stat_lock();
1324 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1325 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1326 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001329 * If we're suspended or the thread is processing barriers
1330 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001332 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Jens Axboe1f98a132009-09-11 14:32:04 +02001333 unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001334 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001336 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1337 bio_rw(bio) == READA) {
1338 bio_io_error(bio);
1339 return 0;
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Mikulas Patocka92c63902009-04-09 00:27:15 +01001342 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Mikulas Patocka92c63902009-04-09 00:27:15 +01001344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001347 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001348 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001352static int dm_make_request(struct request_queue *q, struct bio *bio)
1353{
1354 struct mapped_device *md = q->queuedata;
1355
Jens Axboe1f98a132009-09-11 14:32:04 +02001356 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001357 bio_endio(bio, -EOPNOTSUPP);
1358 return 0;
1359 }
1360
1361 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1362}
1363
1364static int dm_request_based(struct mapped_device *md)
1365{
1366 return blk_queue_stackable(md->queue);
1367}
1368
1369static int dm_request(struct request_queue *q, struct bio *bio)
1370{
1371 struct mapped_device *md = q->queuedata;
1372
1373 if (dm_request_based(md))
1374 return dm_make_request(q, bio);
1375
1376 return _dm_request(q, bio);
1377}
1378
1379void dm_dispatch_request(struct request *rq)
1380{
1381 int r;
1382
1383 if (blk_queue_io_stat(rq->q))
1384 rq->cmd_flags |= REQ_IO_STAT;
1385
1386 rq->start_time = jiffies;
1387 r = blk_insert_cloned_request(rq->q, rq);
1388 if (r)
1389 dm_complete_request(rq, r);
1390}
1391EXPORT_SYMBOL_GPL(dm_dispatch_request);
1392
1393static void dm_rq_bio_destructor(struct bio *bio)
1394{
1395 struct dm_rq_clone_bio_info *info = bio->bi_private;
1396 struct mapped_device *md = info->tio->md;
1397
1398 free_bio_info(info);
1399 bio_free(bio, md->bs);
1400}
1401
1402static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1403 void *data)
1404{
1405 struct dm_rq_target_io *tio = data;
1406 struct mapped_device *md = tio->md;
1407 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1408
1409 if (!info)
1410 return -ENOMEM;
1411
1412 info->orig = bio_orig;
1413 info->tio = tio;
1414 bio->bi_end_io = end_clone_bio;
1415 bio->bi_private = info;
1416 bio->bi_destructor = dm_rq_bio_destructor;
1417
1418 return 0;
1419}
1420
1421static int setup_clone(struct request *clone, struct request *rq,
1422 struct dm_rq_target_io *tio)
1423{
1424 int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1425 dm_rq_bio_constructor, tio);
1426
1427 if (r)
1428 return r;
1429
1430 clone->cmd = rq->cmd;
1431 clone->cmd_len = rq->cmd_len;
1432 clone->sense = rq->sense;
1433 clone->buffer = rq->buffer;
1434 clone->end_io = end_clone_request;
1435 clone->end_io_data = tio;
1436
1437 return 0;
1438}
1439
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001440static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1441 gfp_t gfp_mask)
1442{
1443 struct request *clone;
1444 struct dm_rq_target_io *tio;
1445
1446 tio = alloc_rq_tio(md, gfp_mask);
1447 if (!tio)
1448 return NULL;
1449
1450 tio->md = md;
1451 tio->ti = NULL;
1452 tio->orig = rq;
1453 tio->error = 0;
1454 memset(&tio->info, 0, sizeof(tio->info));
1455
1456 clone = &tio->clone;
1457 if (setup_clone(clone, rq, tio)) {
1458 /* -ENOMEM */
1459 free_rq_tio(tio);
1460 return NULL;
1461 }
1462
1463 return clone;
1464}
1465
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001466/*
1467 * Called with the queue lock held.
1468 */
1469static int dm_prep_fn(struct request_queue *q, struct request *rq)
1470{
1471 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001472 struct request *clone;
1473
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001474 if (unlikely(rq->special)) {
1475 DMWARN("Already has something in rq->special.");
1476 return BLKPREP_KILL;
1477 }
1478
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001479 clone = clone_rq(rq, md, GFP_ATOMIC);
1480 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001481 return BLKPREP_DEFER;
1482
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001483 rq->special = clone;
1484 rq->cmd_flags |= REQ_DONTPREP;
1485
1486 return BLKPREP_OK;
1487}
1488
Kiyoshi Ueda598de402009-12-10 23:52:14 +00001489static void map_request(struct dm_target *ti, struct request *clone,
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001490 struct mapped_device *md)
1491{
1492 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001493 struct dm_rq_target_io *tio = clone->end_io_data;
1494
1495 /*
1496 * Hold the md reference here for the in-flight I/O.
1497 * We can't rely on the reference count by device opener,
1498 * because the device may be closed during the request completion
1499 * when all bios are completed.
1500 * See the comment in rq_completed() too.
1501 */
1502 dm_get(md);
1503
1504 tio->ti = ti;
1505 r = ti->type->map_rq(ti, clone, &tio->info);
1506 switch (r) {
1507 case DM_MAPIO_SUBMITTED:
1508 /* The target has taken the I/O to submit by itself later */
1509 break;
1510 case DM_MAPIO_REMAPPED:
1511 /* The target has remapped the I/O so dispatch it */
1512 dm_dispatch_request(clone);
1513 break;
1514 case DM_MAPIO_REQUEUE:
1515 /* The target wants to requeue the I/O */
1516 dm_requeue_unmapped_request(clone);
1517 break;
1518 default:
1519 if (r > 0) {
1520 DMWARN("unimplemented target map return value: %d", r);
1521 BUG();
1522 }
1523
1524 /* The target wants to complete the I/O */
1525 dm_kill_unmapped_request(clone, r);
1526 break;
1527 }
1528}
1529
1530/*
1531 * q->request_fn for request-based dm.
1532 * Called with the queue lock held.
1533 */
1534static void dm_request_fn(struct request_queue *q)
1535{
1536 struct mapped_device *md = q->queuedata;
1537 struct dm_table *map = dm_get_table(md);
1538 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001539 struct request *rq, *clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001540
1541 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001542 * For suspend, check blk_queue_stopped() and increment
1543 * ->pending within a single queue_lock not to increment the
1544 * number of in-flight I/Os after the queue is stopped in
1545 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001546 */
1547 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1548 rq = blk_peek_request(q);
1549 if (!rq)
1550 goto plug_and_out;
1551
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001552 ti = dm_table_find_target(map, blk_rq_pos(rq));
1553 if (ti->type->busy && ti->type->busy(ti))
1554 goto plug_and_out;
1555
1556 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001557 clone = rq->special;
1558 atomic_inc(&md->pending[rq_data_dir(clone)]);
1559
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001560 spin_unlock(q->queue_lock);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001561 map_request(ti, clone, md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001562 spin_lock_irq(q->queue_lock);
1563 }
1564
1565 goto out;
1566
1567plug_and_out:
1568 if (!elv_queue_empty(q))
1569 /* Some requests still remain, retry later */
1570 blk_plug_device(q);
1571
1572out:
1573 dm_table_put(map);
1574
1575 return;
1576}
1577
1578int dm_underlying_device_busy(struct request_queue *q)
1579{
1580 return blk_lld_busy(q);
1581}
1582EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1583
1584static int dm_lld_busy(struct request_queue *q)
1585{
1586 int r;
1587 struct mapped_device *md = q->queuedata;
1588 struct dm_table *map = dm_get_table(md);
1589
1590 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1591 r = 1;
1592 else
1593 r = dm_table_any_busy_target(map);
1594
1595 dm_table_put(map);
1596
1597 return r;
1598}
1599
Jens Axboe165125e2007-07-24 09:28:11 +02001600static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct mapped_device *md = q->queuedata;
1603 struct dm_table *map = dm_get_table(md);
1604
1605 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001606 if (dm_request_based(md))
1607 generic_unplug_device(q);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 dm_table_unplug_all(map);
1610 dm_table_put(map);
1611 }
1612}
1613
1614static int dm_any_congested(void *congested_data, int bdi_bits)
1615{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001616 int r = bdi_bits;
1617 struct mapped_device *md = congested_data;
1618 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001620 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001621 map = dm_get_table(md);
1622 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001623 /*
1624 * Request-based dm cares about only own queue for
1625 * the query about congestion status of request_queue
1626 */
1627 if (dm_request_based(md))
1628 r = md->queue->backing_dev_info.state &
1629 bdi_bits;
1630 else
1631 r = dm_table_any_congested(map, bdi_bits);
1632
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001633 dm_table_put(map);
1634 }
1635 }
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return r;
1638}
1639
1640/*-----------------------------------------------------------------
1641 * An IDR is used to keep track of allocated minor numbers.
1642 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643static DEFINE_IDR(_minor_idr);
1644
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001645static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001647 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001649 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
1652/*
1653 * See if the device with a specific minor # is free.
1654 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001655static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 int r, m;
1658
1659 if (minor >= (1 << MINORBITS))
1660 return -EINVAL;
1661
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001662 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1663 if (!r)
1664 return -ENOMEM;
1665
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001666 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (idr_find(&_minor_idr, minor)) {
1669 r = -EBUSY;
1670 goto out;
1671 }
1672
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001673 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001674 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (m != minor) {
1678 idr_remove(&_minor_idr, m);
1679 r = -EBUSY;
1680 goto out;
1681 }
1682
1683out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001684 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return r;
1686}
1687
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001688static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001690 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001693 if (!r)
1694 return -ENOMEM;
1695
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001696 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001698 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001699 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 if (m >= (1 << MINORBITS)) {
1703 idr_remove(&_minor_idr, m);
1704 r = -ENOSPC;
1705 goto out;
1706 }
1707
1708 *minor = m;
1709
1710out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001711 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return r;
1713}
1714
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001715static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Mikulas Patocka53d59142009-04-02 19:55:37 +01001717static void dm_wq_work(struct work_struct *work);
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719/*
1720 * Allocate and initialise a blank device with a given minor.
1721 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001722static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001725 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001726 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 if (!md) {
1729 DMWARN("unable to allocate device, out of memory.");
1730 return NULL;
1731 }
1732
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001733 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001734 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001737 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001738 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001739 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001740 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001742 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001744 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001745 mutex_init(&md->suspend_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001746 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 rwlock_init(&md->map_lock);
1748 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001749 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001751 atomic_set(&md->uevent_seq, 0);
1752 INIT_LIST_HEAD(&md->uevent_list);
1753 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001755 md->queue = blk_init_queue(dm_request_fn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001757 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001759 /*
1760 * Request-based dm devices cannot be stacked on top of bio-based dm
1761 * devices. The type of this dm device has not been decided yet,
1762 * although we initialized the queue using blk_init_queue().
1763 * The type is decided at the first table loading time.
1764 * To prevent problematic device stacking, clear the queue flag
1765 * for request stacking support until then.
1766 *
1767 * This queue is new, so no concurrency on the queue_flags.
1768 */
1769 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1770 md->saved_make_request_fn = md->queue->make_request_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 md->queue->queuedata = md;
1772 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1773 md->queue->backing_dev_info.congested_data = md;
1774 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +01001775 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001777 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001778 blk_queue_softirq_done(md->queue, dm_softirq_done);
1779 blk_queue_prep_rq(md->queue, dm_prep_fn);
1780 blk_queue_lld_busy(md->queue, dm_lld_busy);
Stefan Bader9faf4002006-10-03 01:15:41 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 md->disk = alloc_disk(1);
1783 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001784 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001786 atomic_set(&md->pending[0], 0);
1787 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001788 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001789 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001790 init_waitqueue_head(&md->eventq);
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 md->disk->major = _major;
1793 md->disk->first_minor = minor;
1794 md->disk->fops = &dm_blk_dops;
1795 md->disk->queue = md->queue;
1796 md->disk->private_data = md;
1797 sprintf(md->disk->disk_name, "dm-%d", minor);
1798 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001799 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Milan Broz304f3f62008-02-08 02:11:17 +00001801 md->wq = create_singlethread_workqueue("kdmflush");
1802 if (!md->wq)
1803 goto bad_thread;
1804
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001805 md->bdev = bdget_disk(md->disk, 0);
1806 if (!md->bdev)
1807 goto bad_bdev;
1808
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001809 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001810 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001811 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001812 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001813
1814 BUG_ON(old_md != MINOR_ALLOCED);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return md;
1817
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001818bad_bdev:
1819 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001820bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001821 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001822 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001823bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001824 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001825bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001827bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001828 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001829bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 kfree(md);
1831 return NULL;
1832}
1833
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001834static void unlock_fs(struct mapped_device *md);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836static void free_dev(struct mapped_device *md)
1837{
Tejun Heof331c022008-09-03 09:01:48 +02001838 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001839
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001840 unlock_fs(md);
1841 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001842 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001843 if (md->tio_pool)
1844 mempool_destroy(md->tio_pool);
1845 if (md->io_pool)
1846 mempool_destroy(md->io_pool);
1847 if (md->bs)
1848 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001849 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001851 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001852
1853 spin_lock(&_minor_lock);
1854 md->disk->private_data = NULL;
1855 spin_unlock(&_minor_lock);
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001858 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001859 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 kfree(md);
1861}
1862
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001863static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1864{
1865 struct dm_md_mempools *p;
1866
1867 if (md->io_pool && md->tio_pool && md->bs)
1868 /* the md already has necessary mempools */
1869 goto out;
1870
1871 p = dm_table_get_md_mempools(t);
1872 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1873
1874 md->io_pool = p->io_pool;
1875 p->io_pool = NULL;
1876 md->tio_pool = p->tio_pool;
1877 p->tio_pool = NULL;
1878 md->bs = p->bs;
1879 p->bs = NULL;
1880
1881out:
1882 /* mempool bind completed, now no need any mempools in the table */
1883 dm_table_free_md_mempools(t);
1884}
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886/*
1887 * Bind a table to the device.
1888 */
1889static void event_callback(void *context)
1890{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001891 unsigned long flags;
1892 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 struct mapped_device *md = (struct mapped_device *) context;
1894
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001895 spin_lock_irqsave(&md->uevent_lock, flags);
1896 list_splice_init(&md->uevent_list, &uevents);
1897 spin_unlock_irqrestore(&md->uevent_lock, flags);
1898
Tejun Heoed9e1982008-08-25 19:56:05 +09001899 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 atomic_inc(&md->event_nr);
1902 wake_up(&md->eventq);
1903}
1904
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001905static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001907 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001909 mutex_lock(&md->bdev->bd_inode->i_mutex);
1910 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1911 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001914static int __bind(struct mapped_device *md, struct dm_table *t,
1915 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Jens Axboe165125e2007-07-24 09:28:11 +02001917 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001919 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001922
1923 /*
1924 * Wipe any geometry if the size of the table changed.
1925 */
1926 if (size != get_capacity(md->disk))
1927 memset(&md->geometry, 0, sizeof(md->geometry));
1928
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001929 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Mikulas Patockad5816872009-01-06 03:05:10 +00001931 if (!size) {
1932 dm_table_destroy(t);
1933 return 0;
1934 }
1935
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001936 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001937
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001938 /*
1939 * The queue hasn't been stopped yet, if the old table type wasn't
1940 * for request-based during suspension. So stop it to prevent
1941 * I/O mapping before resume.
1942 * This must be done before setting the queue restrictions,
1943 * because request-based dm may be run just after the setting.
1944 */
1945 if (dm_table_request_based(t) && !blk_queue_stopped(q))
1946 stop_queue(q);
1947
1948 __bind_mempools(md, t);
1949
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001950 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001951 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001952 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001953 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return 0;
1956}
1957
1958static void __unbind(struct mapped_device *md)
1959{
1960 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001961 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 if (!map)
1964 return;
1965
1966 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001967 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001969 write_unlock_irqrestore(&md->map_lock, flags);
Mikulas Patockad5816872009-01-06 03:05:10 +00001970 dm_table_destroy(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973/*
1974 * Constructor for a new device.
1975 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001976int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 struct mapped_device *md;
1979
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001980 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (!md)
1982 return -ENXIO;
1983
Milan Broz784aae72009-01-06 03:05:12 +00001984 dm_sysfs_init(md);
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 *result = md;
1987 return 0;
1988}
1989
David Teigland637842c2006-01-06 00:20:00 -08001990static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 unsigned minor = MINOR(dev);
1994
1995 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1996 return NULL;
1997
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001998 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002001 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002002 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002003 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002004 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002005 goto out;
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002008out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002009 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
David Teigland637842c2006-01-06 00:20:00 -08002011 return md;
2012}
2013
David Teiglandd229a952006-01-06 00:20:01 -08002014struct mapped_device *dm_get_md(dev_t dev)
2015{
2016 struct mapped_device *md = dm_find_md(dev);
2017
2018 if (md)
2019 dm_get(md);
2020
2021 return md;
2022}
2023
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002024void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002025{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002026 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
2029void dm_set_mdptr(struct mapped_device *md, void *ptr)
2030{
2031 md->interface_ptr = ptr;
2032}
2033
2034void dm_get(struct mapped_device *md)
2035{
2036 atomic_inc(&md->holders);
2037}
2038
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002039const char *dm_device_name(struct mapped_device *md)
2040{
2041 return md->name;
2042}
2043EXPORT_SYMBOL_GPL(dm_device_name);
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045void dm_put(struct mapped_device *md)
2046{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002047 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002049 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2050
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002051 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08002052 map = dm_get_table(md);
Tejun Heof331c022008-09-03 09:01:48 +02002053 idr_replace(&_minor_idr, MINOR_ALLOCED,
2054 MINOR(disk_devt(dm_disk(md))));
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002055 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002056 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002057 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 dm_table_presuspend_targets(map);
2059 dm_table_postsuspend_targets(map);
2060 }
Milan Broz784aae72009-01-06 03:05:12 +00002061 dm_sysfs_exit(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002062 dm_table_put(map);
Mikulas Patockaa1b51e92009-01-06 03:04:53 +00002063 __unbind(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 free_dev(md);
2065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
Edward Goggin79eb8852007-05-09 02:32:56 -07002067EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Mikulas Patocka401600d2009-04-02 19:55:38 +01002069static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002070{
2071 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002072 DECLARE_WAITQUEUE(wait, current);
2073
2074 dm_unplug_all(md->queue);
2075
2076 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002077
2078 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002079 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002080
2081 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002082 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002083 break;
2084
Mikulas Patocka401600d2009-04-02 19:55:38 +01002085 if (interruptible == TASK_INTERRUPTIBLE &&
2086 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002087 r = -EINTR;
2088 break;
2089 }
2090
2091 io_schedule();
2092 }
2093 set_current_state(TASK_RUNNING);
2094
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002095 remove_wait_queue(&md->wait, &wait);
2096
Milan Broz46125c12008-02-08 02:10:30 +00002097 return r;
2098}
2099
Mikulas Patocka531fe962009-06-22 10:12:17 +01002100static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002101{
2102 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002103
2104 bio_init(&md->barrier_bio);
2105 md->barrier_bio.bi_bdev = md->bdev;
2106 md->barrier_bio.bi_rw = WRITE_BARRIER;
2107 __split_and_process_bio(md, &md->barrier_bio);
2108
2109 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002110}
2111
2112static void process_barrier(struct mapped_device *md, struct bio *bio)
2113{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002114 md->barrier_error = 0;
2115
Mikulas Patocka531fe962009-06-22 10:12:17 +01002116 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002117
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002118 if (!bio_empty_barrier(bio)) {
2119 __split_and_process_bio(md, bio);
2120 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002121 }
2122
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002123 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002124 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002125 else {
2126 spin_lock_irq(&md->deferred_lock);
2127 bio_list_add_head(&md->deferred, bio);
2128 spin_unlock_irq(&md->deferred_lock);
2129 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002130}
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132/*
2133 * Process the deferred bios
2134 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002135static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Mikulas Patockaef208582009-04-02 19:55:38 +01002137 struct mapped_device *md = container_of(work, struct mapped_device,
2138 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002139 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Mikulas Patockaef208582009-04-02 19:55:38 +01002141 down_write(&md->io_lock);
2142
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002143 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002144 spin_lock_irq(&md->deferred_lock);
2145 c = bio_list_pop(&md->deferred);
2146 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002147
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002148 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002149 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002150 break;
2151 }
2152
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002153 up_write(&md->io_lock);
2154
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002155 if (dm_request_based(md))
2156 generic_make_request(c);
2157 else {
Jens Axboe1f98a132009-09-11 14:32:04 +02002158 if (bio_rw_flagged(c, BIO_RW_BARRIER))
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002159 process_barrier(md, c);
2160 else
2161 __split_and_process_bio(md, c);
2162 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002163
2164 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002165 }
Milan Broz73d410c2008-02-08 02:10:25 +00002166
Mikulas Patockaef208582009-04-02 19:55:38 +01002167 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002170static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002171{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002172 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2173 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002174 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002175}
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177/*
2178 * Swap in a new table (destroying old one).
2179 */
2180int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2181{
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002182 struct queue_limits limits;
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002183 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Daniel Walkere61290a2008-02-08 02:10:08 +00002185 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002188 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002191 r = dm_calculate_queue_limits(table, &limits);
2192 if (r)
2193 goto out;
2194
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002195 /* cannot change the device type, once a table is bound */
2196 if (md->map &&
2197 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2198 DMWARN("can't change the device type after a table is bound");
2199 goto out;
2200 }
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 __unbind(md);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002203 r = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002205out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002206 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002207 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
2210/*
2211 * Functions to lock and unlock any filesystem running on the
2212 * device.
2213 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002214static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002216 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002219
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002220 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002221 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002222 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002223 md->frozen_sb = NULL;
2224 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002225 }
2226
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002227 set_bit(DMF_FROZEN, &md->flags);
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return 0;
2230}
2231
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002232static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002234 if (!test_bit(DMF_FROZEN, &md->flags))
2235 return;
2236
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002237 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002239 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242/*
2243 * We need to be able to change a mapping table under a mounted
2244 * filesystem. For example we might want to move some data in
2245 * the background. Before the table can be swapped with
2246 * dm_bind_table, dm_suspend must be called to flush any in
2247 * flight bios and ensure that any further io gets deferred.
2248 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002249/*
2250 * Suspend mechanism in request-based dm.
2251 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002252 * 1. Flush all I/Os by lock_fs() if needed.
2253 * 2. Stop dispatching any I/O by stopping the request_queue.
2254 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002255 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002256 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002257 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002258int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002260 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002261 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002262 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002263 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Daniel Walkere61290a2008-02-08 02:10:08 +00002265 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002266
Milan Broz73d410c2008-02-08 02:10:25 +00002267 if (dm_suspended(md)) {
2268 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002269 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002274 /*
2275 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2276 * This flag is cleared before dm_suspend returns.
2277 */
2278 if (noflush)
2279 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2280
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002281 /* This does not get reverted if there's an error later. */
2282 dm_table_presuspend_targets(map);
2283
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002284 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002285 * Flush I/O to the device.
2286 * Any I/O submitted after lock_fs() may not be flushed.
2287 * noflush takes precedence over do_lockfs.
2288 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002289 */
2290 if (!noflush && do_lockfs) {
2291 r = lock_fs(md);
2292 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002293 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002297 * Here we must make sure that no processes are submitting requests
2298 * to target drivers i.e. no one may be executing
2299 * __split_and_process_bio. This is called from dm_request and
2300 * dm_wq_work.
2301 *
2302 * To get all processes out of __split_and_process_bio in dm_request,
2303 * we take the write lock. To prevent any process from reentering
2304 * __split_and_process_bio from dm_request, we set
2305 * DMF_QUEUE_IO_TO_THREAD.
2306 *
2307 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2308 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2309 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2310 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002312 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002313 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2314 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002315 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002317 flush_workqueue(md->wq);
2318
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002319 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002320 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002323 * At this point no more requests are entering target request routines.
2324 * We call dm_wait_for_completion to wait for all existing requests
2325 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002327 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002329 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002330 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002331 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002332 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002335 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002336 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002337
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002338 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002339 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002340
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002341 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002342 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002343 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002344
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002345 /*
2346 * If dm_wait_for_completion returned 0, the device is completely
2347 * quiescent now. There is no request-processing activity. All new
2348 * requests are being added to md->deferred list.
2349 */
2350
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002351 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 set_bit(DMF_SUSPENDED, &md->flags);
2354
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002355out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002357
2358out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002359 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002360 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363int dm_resume(struct mapped_device *md)
2364{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002365 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002366 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Daniel Walkere61290a2008-02-08 02:10:08 +00002368 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002369 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002370 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002371
2372 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002373 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Milan Broz8757b772006-10-03 01:15:36 -07002376 r = dm_table_resume_targets(map);
2377 if (r)
2378 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002379
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002380 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002381
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002382 /*
2383 * Flushing deferred I/Os must be done after targets are resumed
2384 * so that mapping of targets can work correctly.
2385 * Request-based dm is queueing the deferred I/Os in its request_queue.
2386 */
2387 if (dm_request_based(md))
2388 start_queue(md->queue);
2389
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002390 unlock_fs(md);
2391
2392 clear_bit(DMF_SUSPENDED, &md->flags);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002395 r = 0;
2396out:
2397 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002398 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002399
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002400 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
2403/*-----------------------------------------------------------------
2404 * Event notification.
2405 *---------------------------------------------------------------*/
Milan Broz60935eb2009-06-22 10:12:30 +01002406void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2407 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002408{
Milan Broz60935eb2009-06-22 10:12:30 +01002409 char udev_cookie[DM_COOKIE_LENGTH];
2410 char *envp[] = { udev_cookie, NULL };
2411
2412 if (!cookie)
2413 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2414 else {
2415 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2416 DM_COOKIE_ENV_VAR_NAME, cookie);
2417 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2418 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002419}
2420
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002421uint32_t dm_next_uevent_seq(struct mapped_device *md)
2422{
2423 return atomic_add_return(1, &md->uevent_seq);
2424}
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426uint32_t dm_get_event_nr(struct mapped_device *md)
2427{
2428 return atomic_read(&md->event_nr);
2429}
2430
2431int dm_wait_event(struct mapped_device *md, int event_nr)
2432{
2433 return wait_event_interruptible(md->eventq,
2434 (event_nr != atomic_read(&md->event_nr)));
2435}
2436
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002437void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2438{
2439 unsigned long flags;
2440
2441 spin_lock_irqsave(&md->uevent_lock, flags);
2442 list_add(elist, &md->uevent_list);
2443 spin_unlock_irqrestore(&md->uevent_lock, flags);
2444}
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446/*
2447 * The gendisk is only valid as long as you have a reference
2448 * count on 'md'.
2449 */
2450struct gendisk *dm_disk(struct mapped_device *md)
2451{
2452 return md->disk;
2453}
2454
Milan Broz784aae72009-01-06 03:05:12 +00002455struct kobject *dm_kobject(struct mapped_device *md)
2456{
2457 return &md->kobj;
2458}
2459
2460/*
2461 * struct mapped_device should not be exported outside of dm.c
2462 * so use this check to verify that kobj is part of md structure
2463 */
2464struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2465{
2466 struct mapped_device *md;
2467
2468 md = container_of(kobj, struct mapped_device, kobj);
2469 if (&md->kobj != kobj)
2470 return NULL;
2471
Milan Broz4d89b7b2009-06-22 10:12:11 +01002472 if (test_bit(DMF_FREEING, &md->flags) ||
2473 test_bit(DMF_DELETING, &md->flags))
2474 return NULL;
2475
Milan Broz784aae72009-01-06 03:05:12 +00002476 dm_get(md);
2477 return md;
2478}
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480int dm_suspended(struct mapped_device *md)
2481{
2482 return test_bit(DMF_SUSPENDED, &md->flags);
2483}
2484
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002485int dm_noflush_suspending(struct dm_target *ti)
2486{
2487 struct mapped_device *md = dm_table_get_md(ti->table);
2488 int r = __noflush_suspending(md);
2489
2490 dm_put(md);
2491
2492 return r;
2493}
2494EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2495
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002496struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2497{
2498 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2499
2500 if (!pools)
2501 return NULL;
2502
2503 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2504 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2505 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2506 if (!pools->io_pool)
2507 goto free_pools_and_out;
2508
2509 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2510 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2511 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2512 if (!pools->tio_pool)
2513 goto free_io_pool_and_out;
2514
2515 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2516 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2517 if (!pools->bs)
2518 goto free_tio_pool_and_out;
2519
2520 return pools;
2521
2522free_tio_pool_and_out:
2523 mempool_destroy(pools->tio_pool);
2524
2525free_io_pool_and_out:
2526 mempool_destroy(pools->io_pool);
2527
2528free_pools_and_out:
2529 kfree(pools);
2530
2531 return NULL;
2532}
2533
2534void dm_free_md_mempools(struct dm_md_mempools *pools)
2535{
2536 if (!pools)
2537 return;
2538
2539 if (pools->io_pool)
2540 mempool_destroy(pools->io_pool);
2541
2542 if (pools->tio_pool)
2543 mempool_destroy(pools->tio_pool);
2544
2545 if (pools->bs)
2546 bioset_free(pools->bs);
2547
2548 kfree(pools);
2549}
2550
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002551static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 .open = dm_blk_open,
2553 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002554 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002555 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 .owner = THIS_MODULE
2557};
2558
2559EXPORT_SYMBOL(dm_get_mapinfo);
2560
2561/*
2562 * module hooks
2563 */
2564module_init(dm_init);
2565module_exit(dm_exit);
2566
2567module_param(major, uint, 0);
2568MODULE_PARM_DESC(major, "The major number of the device mapper");
2569MODULE_DESCRIPTION(DM_NAME " driver");
2570MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2571MODULE_LICENSE("GPL");