blob: 3de8d6d5b0b8c521e6227240015b4f7c871482c4 [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 /*
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000146 * Protect barrier_error from concurrent endio processing
147 * in request-based dm.
148 */
149 spinlock_t barrier_error_lock;
150
151 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000152 * Processing queue (flush/barriers)
153 */
154 struct workqueue_struct *wq;
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000155 struct work_struct barrier_work;
156
157 /* A pointer to the currently processing pre/post flush request */
158 struct request *flush_request;
Milan Broz304f3f62008-02-08 02:11:17 +0000159
160 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * The current mapping.
162 */
163 struct dm_table *map;
164
165 /*
166 * io objects are allocated from here.
167 */
168 mempool_t *io_pool;
169 mempool_t *tio_pool;
170
Stefan Bader9faf4002006-10-03 01:15:41 -0700171 struct bio_set *bs;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /*
174 * Event handling.
175 */
176 atomic_t event_nr;
177 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100178 atomic_t uevent_seq;
179 struct list_head uevent_list;
180 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /*
183 * freeze/thaw support require holding onto a super block
184 */
185 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100186 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800187
188 /* forced geometry settings */
189 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000190
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100191 /* For saving the address of __make_request for request based dm */
192 make_request_fn *saved_make_request_fn;
193
Milan Broz784aae72009-01-06 03:05:12 +0000194 /* sysfs handle */
195 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100196
197 /* zero-length barrier that will be cloned and submitted to targets */
198 struct bio barrier_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100201/*
202 * For mempools pre-allocation at the table loading time.
203 */
204struct dm_md_mempools {
205 mempool_t *io_pool;
206 mempool_t *tio_pool;
207 struct bio_set *bs;
208};
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800211static struct kmem_cache *_io_cache;
212static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000213static struct kmem_cache *_rq_tio_cache;
214static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static int __init local_init(void)
217{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100218 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100221 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100223 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100226 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100227 if (!_tio_cache)
228 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000230 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
231 if (!_rq_tio_cache)
232 goto out_free_tio_cache;
233
234 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
235 if (!_rq_bio_info_cache)
236 goto out_free_rq_tio_cache;
237
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100238 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100239 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000240 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 _major = major;
243 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100244 if (r < 0)
245 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (!_major)
248 _major = r;
249
250 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100251
252out_uevent_exit:
253 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000254out_free_rq_bio_info_cache:
255 kmem_cache_destroy(_rq_bio_info_cache);
256out_free_rq_tio_cache:
257 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100258out_free_tio_cache:
259 kmem_cache_destroy(_tio_cache);
260out_free_io_cache:
261 kmem_cache_destroy(_io_cache);
262
263 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static void local_exit(void)
267{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000268 kmem_cache_destroy(_rq_bio_info_cache);
269 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 kmem_cache_destroy(_tio_cache);
271 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700272 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100273 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 _major = 0;
276
277 DMINFO("cleaned up");
278}
279
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000280static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 local_init,
282 dm_target_init,
283 dm_linear_init,
284 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000285 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100286 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dm_interface_init,
288};
289
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000290static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 local_exit,
292 dm_target_exit,
293 dm_linear_exit,
294 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000295 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100296 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 dm_interface_exit,
298};
299
300static int __init dm_init(void)
301{
302 const int count = ARRAY_SIZE(_inits);
303
304 int r, i;
305
306 for (i = 0; i < count; i++) {
307 r = _inits[i]();
308 if (r)
309 goto bad;
310 }
311
312 return 0;
313
314 bad:
315 while (i--)
316 _exits[i]();
317
318 return r;
319}
320
321static void __exit dm_exit(void)
322{
323 int i = ARRAY_SIZE(_exits);
324
325 while (i--)
326 _exits[i]();
327}
328
329/*
330 * Block device functions
331 */
Al Virofe5f9f22008-03-02 10:29:31 -0500332static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct mapped_device *md;
335
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700336 spin_lock(&_minor_lock);
337
Al Virofe5f9f22008-03-02 10:29:31 -0500338 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700339 if (!md)
340 goto out;
341
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700342 if (test_bit(DMF_FREEING, &md->flags) ||
343 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700344 md = NULL;
345 goto out;
346 }
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700349 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700350
351out:
352 spin_unlock(&_minor_lock);
353
354 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Al Virofe5f9f22008-03-02 10:29:31 -0500357static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Al Virofe5f9f22008-03-02 10:29:31 -0500359 struct mapped_device *md = disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700360 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dm_put(md);
362 return 0;
363}
364
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700365int dm_open_count(struct mapped_device *md)
366{
367 return atomic_read(&md->open_count);
368}
369
370/*
371 * Guarantees nothing is using the device before it's deleted.
372 */
373int dm_lock_for_deletion(struct mapped_device *md)
374{
375 int r = 0;
376
377 spin_lock(&_minor_lock);
378
379 if (dm_open_count(md))
380 r = -EBUSY;
381 else
382 set_bit(DMF_DELETING, &md->flags);
383
384 spin_unlock(&_minor_lock);
385
386 return r;
387}
388
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800389static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
390{
391 struct mapped_device *md = bdev->bd_disk->private_data;
392
393 return dm_get_geometry(md, geo);
394}
395
Al Virofe5f9f22008-03-02 10:29:31 -0500396static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700397 unsigned int cmd, unsigned long arg)
398{
Al Virofe5f9f22008-03-02 10:29:31 -0500399 struct mapped_device *md = bdev->bd_disk->private_data;
400 struct dm_table *map = dm_get_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700401 struct dm_target *tgt;
402 int r = -ENOTTY;
403
Milan Brozaa129a22006-10-03 01:15:15 -0700404 if (!map || !dm_table_get_size(map))
405 goto out;
406
407 /* We only support devices that have a single target */
408 if (dm_table_get_num_targets(map) != 1)
409 goto out;
410
411 tgt = dm_table_get_target(map, 0);
412
413 if (dm_suspended(md)) {
414 r = -EAGAIN;
415 goto out;
416 }
417
418 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400419 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700420
421out:
422 dm_table_put(map);
423
Milan Brozaa129a22006-10-03 01:15:15 -0700424 return r;
425}
426
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100427static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 return mempool_alloc(md->io_pool, GFP_NOIO);
430}
431
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100432static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 mempool_free(io, md->io_pool);
435}
436
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100437static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 mempool_free(tio, md->tio_pool);
440}
441
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000442static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
443 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100444{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000445 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100446}
447
448static void free_rq_tio(struct dm_rq_target_io *tio)
449{
450 mempool_free(tio, tio->md->tio_pool);
451}
452
453static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
454{
455 return mempool_alloc(md->io_pool, GFP_ATOMIC);
456}
457
458static void free_bio_info(struct dm_rq_clone_bio_info *info)
459{
460 mempool_free(info, info->tio->md->io_pool);
461}
462
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000463static int md_in_flight(struct mapped_device *md)
464{
465 return atomic_read(&md->pending[READ]) +
466 atomic_read(&md->pending[WRITE]);
467}
468
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800469static void start_io_acct(struct dm_io *io)
470{
471 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900472 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200473 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800474
475 io->start_time = jiffies;
476
Tejun Heo074a7ac2008-08-25 19:56:14 +0900477 cpu = part_stat_lock();
478 part_round_stats(cpu, &dm_disk(md)->part0);
479 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200480 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800481}
482
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000483static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800484{
485 struct mapped_device *md = io->md;
486 struct bio *bio = io->bio;
487 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900488 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800489 int rw = bio_data_dir(bio);
490
Tejun Heo074a7ac2008-08-25 19:56:14 +0900491 cpu = part_stat_lock();
492 part_round_stats(cpu, &dm_disk(md)->part0);
493 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
494 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800495
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100496 /*
497 * After this is decremented the bio must not be touched if it is
498 * a barrier.
499 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200500 dm_disk(md)->part0.in_flight[rw] = pending =
501 atomic_dec_return(&md->pending[rw]);
502 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800503
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000504 /* nudge anyone waiting on suspend queue */
505 if (!pending)
506 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
510 * Add the bio to the list of deferred io.
511 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100512static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700514 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Mikulas Patocka022c2612009-04-02 19:55:39 +0100516 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100518 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Mikulas Patocka92c63902009-04-09 00:27:15 +0100520 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
521 queue_work(md->wq, &md->work);
522
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700523 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526/*
527 * Everyone (including functions in this file), should use this
528 * function to access the md->map field, and make sure they call
529 * dm_table_put() when finished.
530 */
531struct dm_table *dm_get_table(struct mapped_device *md)
532{
533 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100534 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100536 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 t = md->map;
538 if (t)
539 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100540 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 return t;
543}
544
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800545/*
546 * Get the geometry associated with a dm device
547 */
548int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
549{
550 *geo = md->geometry;
551
552 return 0;
553}
554
555/*
556 * Set the geometry of a device.
557 */
558int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
559{
560 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
561
562 if (geo->start > sz) {
563 DMWARN("Start sector is beyond the geometry limits.");
564 return -EINVAL;
565 }
566
567 md->geometry = *geo;
568
569 return 0;
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*-----------------------------------------------------------------
573 * CRUD START:
574 * A more elegant soln is in the works that uses the queue
575 * merge fn, unfortunately there are a couple of changes to
576 * the block layer that I want to make for this. So in the
577 * interests of getting something for people to use I give
578 * you this clearly demarcated crap.
579 *---------------------------------------------------------------*/
580
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800581static int __noflush_suspending(struct mapped_device *md)
582{
583 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * Decrements the number of outstanding ios that a bio has been
588 * cloned into, completing the original io if necc.
589 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800590static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800592 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000593 int io_error;
594 struct bio *bio;
595 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800596
597 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100598 if (unlikely(error)) {
599 spin_lock_irqsave(&io->endio_lock, flags);
600 if (!(io->error > 0 && __noflush_suspending(md)))
601 io->error = error;
602 spin_unlock_irqrestore(&io->endio_lock, flags);
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800606 if (io->error == DM_ENDIO_REQUEUE) {
607 /*
608 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800609 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100610 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100611 if (__noflush_suspending(md)) {
Jens Axboe1f98a132009-09-11 14:32:04 +0200612 if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100613 bio_list_add_head(&md->deferred,
614 io->bio);
615 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800616 /* noflush suspend was interrupted. */
617 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100618 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800619 }
620
Milan Brozb35f8ca2009-03-16 17:44:36 +0000621 io_error = io->error;
622 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100623
Jens Axboe1f98a132009-09-11 14:32:04 +0200624 if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100625 /*
626 * There can be just one barrier request so we use
627 * a per-device variable for error reporting.
628 * Note that you can't touch the bio after end_io_acct
629 */
Mikulas Patockafdb95722009-06-22 10:12:19 +0100630 if (!md->barrier_error && io_error != -EOPNOTSUPP)
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100631 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100632 end_io_acct(io);
633 } else {
634 end_io_acct(io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000635
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100636 if (io_error != DM_ENDIO_REQUEUE) {
637 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000638
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100639 bio_endio(bio, io_error);
640 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800641 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100642
643 free_io(md, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645}
646
NeilBrown6712ecf2007-09-27 12:47:43 +0200647static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100650 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000651 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700652 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 dm_endio_fn endio = tio->ti->type->end_io;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
656 error = -EIO;
657
658 if (endio) {
659 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800660 if (r < 0 || r == DM_ENDIO_REQUEUE)
661 /*
662 * error and requeue request are handled
663 * in dec_pending().
664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800666 else if (r == DM_ENDIO_INCOMPLETE)
667 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200668 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800669 else if (r) {
670 DMWARN("unimplemented target endio return value: %d", r);
671 BUG();
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
Stefan Bader9faf4002006-10-03 01:15:41 -0700675 /*
676 * Store md for cleanup instead of tio which is about to get freed.
677 */
678 bio->bi_private = md->bs;
679
Stefan Bader9faf4002006-10-03 01:15:41 -0700680 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000681 bio_put(bio);
682 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100685/*
686 * Partial completion handling for request-based dm
687 */
688static void end_clone_bio(struct bio *clone, int error)
689{
690 struct dm_rq_clone_bio_info *info = clone->bi_private;
691 struct dm_rq_target_io *tio = info->tio;
692 struct bio *bio = info->orig;
693 unsigned int nr_bytes = info->orig->bi_size;
694
695 bio_put(clone);
696
697 if (tio->error)
698 /*
699 * An error has already been detected on the request.
700 * Once error occurred, just let clone->end_io() handle
701 * the remainder.
702 */
703 return;
704 else if (error) {
705 /*
706 * Don't notice the error to the upper layer yet.
707 * The error handling decision is made by the target driver,
708 * when the request is completed.
709 */
710 tio->error = error;
711 return;
712 }
713
714 /*
715 * I/O for the bio successfully completed.
716 * Notice the data completion to the upper layer.
717 */
718
719 /*
720 * bios are processed from the head of the list.
721 * So the completing bio should always be rq->bio.
722 * If it's not, something wrong is happening.
723 */
724 if (tio->orig->bio != bio)
725 DMERR("bio completion is going in the middle of the request");
726
727 /*
728 * Update the original request.
729 * Do not use blk_end_request() here, because it may complete
730 * the original request before the clone, and break the ordering.
731 */
732 blk_update_request(tio->orig, 0, nr_bytes);
733}
734
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000735static void store_barrier_error(struct mapped_device *md, int error)
736{
737 unsigned long flags;
738
739 spin_lock_irqsave(&md->barrier_error_lock, flags);
740 /*
741 * Basically, the first error is taken, but:
742 * -EOPNOTSUPP supersedes any I/O error.
743 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
744 */
745 if (!md->barrier_error || error == -EOPNOTSUPP ||
746 (md->barrier_error != -EOPNOTSUPP &&
747 error == DM_ENDIO_REQUEUE))
748 md->barrier_error = error;
749 spin_unlock_irqrestore(&md->barrier_error_lock, flags);
750}
751
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100752/*
753 * Don't touch any member of the md after calling this function because
754 * the md may be freed in dm_put() at the end of this function.
755 * Or do dm_get() before calling this function and dm_put() later.
756 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000757static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100758{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000759 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100760
761 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000762 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100763 wake_up(&md->wait);
764
765 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000766 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100767
768 /*
769 * dm_put() must be at the end of this function. See the comment above
770 */
771 dm_put(md);
772}
773
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100774static void free_rq_clone(struct request *clone)
775{
776 struct dm_rq_target_io *tio = clone->end_io_data;
777
778 blk_rq_unprep_clone(clone);
779 free_rq_tio(tio);
780}
781
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000782/*
783 * Complete the clone and the original request.
784 * Must be called without queue lock.
785 */
786static void dm_end_request(struct request *clone, int error)
787{
788 int rw = rq_data_dir(clone);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000789 int run_queue = 1;
790 bool is_barrier = blk_barrier_rq(clone);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000791 struct dm_rq_target_io *tio = clone->end_io_data;
792 struct mapped_device *md = tio->md;
793 struct request *rq = tio->orig;
794
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000795 if (blk_pc_request(rq) && !is_barrier) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000796 rq->errors = clone->errors;
797 rq->resid_len = clone->resid_len;
798
799 if (rq->sense)
800 /*
801 * We are using the sense buffer of the original
802 * request.
803 * So setting the length of the sense data is enough.
804 */
805 rq->sense_len = clone->sense_len;
806 }
807
808 free_rq_clone(clone);
809
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000810 if (unlikely(is_barrier)) {
811 if (unlikely(error))
812 store_barrier_error(md, error);
813 run_queue = 0;
814 } else
815 blk_end_request_all(rq, error);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000816
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000817 rq_completed(md, rw, run_queue);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000818}
819
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100820static void dm_unprep_request(struct request *rq)
821{
822 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100823
824 rq->special = NULL;
825 rq->cmd_flags &= ~REQ_DONTPREP;
826
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100827 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100828}
829
830/*
831 * Requeue the original request of a clone.
832 */
833void dm_requeue_unmapped_request(struct request *clone)
834{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000835 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100836 struct dm_rq_target_io *tio = clone->end_io_data;
837 struct mapped_device *md = tio->md;
838 struct request *rq = tio->orig;
839 struct request_queue *q = rq->q;
840 unsigned long flags;
841
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000842 if (unlikely(blk_barrier_rq(clone))) {
843 /*
844 * Barrier clones share an original request.
845 * Leave it to dm_end_request(), which handles this special
846 * case.
847 */
848 dm_end_request(clone, DM_ENDIO_REQUEUE);
849 return;
850 }
851
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100852 dm_unprep_request(rq);
853
854 spin_lock_irqsave(q->queue_lock, flags);
855 if (elv_queue_empty(q))
856 blk_plug_device(q);
857 blk_requeue_request(q, rq);
858 spin_unlock_irqrestore(q->queue_lock, flags);
859
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000860 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100861}
862EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
863
864static void __stop_queue(struct request_queue *q)
865{
866 blk_stop_queue(q);
867}
868
869static void stop_queue(struct request_queue *q)
870{
871 unsigned long flags;
872
873 spin_lock_irqsave(q->queue_lock, flags);
874 __stop_queue(q);
875 spin_unlock_irqrestore(q->queue_lock, flags);
876}
877
878static void __start_queue(struct request_queue *q)
879{
880 if (blk_queue_stopped(q))
881 blk_start_queue(q);
882}
883
884static void start_queue(struct request_queue *q)
885{
886 unsigned long flags;
887
888 spin_lock_irqsave(q->queue_lock, flags);
889 __start_queue(q);
890 spin_unlock_irqrestore(q->queue_lock, flags);
891}
892
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000893static void dm_done(struct request *clone, int error, bool mapped)
894{
895 int r = error;
896 struct dm_rq_target_io *tio = clone->end_io_data;
897 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
898
899 if (mapped && rq_end_io)
900 r = rq_end_io(tio->ti, clone, error, &tio->info);
901
902 if (r <= 0)
903 /* The target wants to complete the I/O */
904 dm_end_request(clone, r);
905 else if (r == DM_ENDIO_INCOMPLETE)
906 /* The target will handle the I/O */
907 return;
908 else if (r == DM_ENDIO_REQUEUE)
909 /* The target wants to requeue the I/O */
910 dm_requeue_unmapped_request(clone);
911 else {
912 DMWARN("unimplemented target endio return value: %d", r);
913 BUG();
914 }
915}
916
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100917/*
918 * Request completion handler for request-based dm
919 */
920static void dm_softirq_done(struct request *rq)
921{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000922 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100923 struct request *clone = rq->completion_data;
924 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100925
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000926 if (rq->cmd_flags & REQ_FAILED)
927 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100928
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000929 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100930}
931
932/*
933 * Complete the clone and the original request with the error status
934 * through softirq context.
935 */
936static void dm_complete_request(struct request *clone, int error)
937{
938 struct dm_rq_target_io *tio = clone->end_io_data;
939 struct request *rq = tio->orig;
940
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000941 if (unlikely(blk_barrier_rq(clone))) {
942 /*
943 * Barrier clones share an original request. So can't use
944 * softirq_done with the original.
945 * Pass the clone to dm_done() directly in this special case.
946 * It is safe (even if clone->q->queue_lock is held here)
947 * because there is no I/O dispatching during the completion
948 * of barrier clone.
949 */
950 dm_done(clone, error, true);
951 return;
952 }
953
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100954 tio->error = error;
955 rq->completion_data = clone;
956 blk_complete_request(rq);
957}
958
959/*
960 * Complete the not-mapped clone and the original request with the error status
961 * through softirq context.
962 * Target's rq_end_io() function isn't called.
963 * This may be used when the target's map_rq() function fails.
964 */
965void dm_kill_unmapped_request(struct request *clone, int error)
966{
967 struct dm_rq_target_io *tio = clone->end_io_data;
968 struct request *rq = tio->orig;
969
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000970 if (unlikely(blk_barrier_rq(clone))) {
971 /*
972 * Barrier clones share an original request.
973 * Leave it to dm_end_request(), which handles this special
974 * case.
975 */
976 BUG_ON(error > 0);
977 dm_end_request(clone, error);
978 return;
979 }
980
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100981 rq->cmd_flags |= REQ_FAILED;
982 dm_complete_request(clone, error);
983}
984EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
985
986/*
987 * Called with the queue lock held
988 */
989static void end_clone_request(struct request *clone, int error)
990{
991 /*
992 * For just cleaning up the information of the queue in which
993 * the clone was dispatched.
994 * The clone is *NOT* freed actually here because it is alloced from
995 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
996 */
997 __blk_put_request(clone->q, clone);
998
999 /*
1000 * Actual request completion is done in a softirq context which doesn't
1001 * hold the queue lock. Otherwise, deadlock could occur because:
1002 * - another request may be submitted by the upper level driver
1003 * of the stacking during the completion
1004 * - the submission which requires queue lock may be done
1005 * against this queue
1006 */
1007 dm_complete_request(clone, error);
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010static sector_t max_io_len(struct mapped_device *md,
1011 sector_t sector, struct dm_target *ti)
1012{
1013 sector_t offset = sector - ti->begin;
1014 sector_t len = ti->len - offset;
1015
1016 /*
1017 * Does the target need to split even further ?
1018 */
1019 if (ti->split_io) {
1020 sector_t boundary;
1021 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
1022 - offset;
1023 if (len > boundary)
1024 len = boundary;
1025 }
1026
1027 return len;
1028}
1029
1030static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001031 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001034 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001035 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 clone->bi_end_io = clone_endio;
1038 clone->bi_private = tio;
1039
1040 /*
1041 * Map the clone. If r == 0 we don't need to do
1042 * anything, the target has assumed ownership of
1043 * this io.
1044 */
1045 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001046 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001048 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001050
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001051 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001052 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001055 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1056 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001057 md = tio->io->md;
1058 dec_pending(tio->io, r);
1059 /*
1060 * Store bio_set for cleanup.
1061 */
1062 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001064 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001065 } else if (r) {
1066 DMWARN("unimplemented target map return value: %d", r);
1067 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069}
1070
1071struct clone_info {
1072 struct mapped_device *md;
1073 struct dm_table *map;
1074 struct bio *bio;
1075 struct dm_io *io;
1076 sector_t sector;
1077 sector_t sector_count;
1078 unsigned short idx;
1079};
1080
Peter Osterlund36763472005-09-06 15:16:42 -07001081static void dm_bio_destructor(struct bio *bio)
1082{
Stefan Bader9faf4002006-10-03 01:15:41 -07001083 struct bio_set *bs = bio->bi_private;
1084
1085 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088/*
1089 * Creates a little bio that is just does part of a bvec.
1090 */
1091static struct bio *split_bvec(struct bio *bio, sector_t sector,
1092 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001093 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 struct bio *clone;
1096 struct bio_vec *bv = bio->bi_io_vec + idx;
1097
Stefan Bader9faf4002006-10-03 01:15:41 -07001098 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001099 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 *clone->bi_io_vec = *bv;
1101
1102 clone->bi_sector = sector;
1103 clone->bi_bdev = bio->bi_bdev;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001104 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 clone->bi_vcnt = 1;
1106 clone->bi_size = to_bytes(len);
1107 clone->bi_io_vec->bv_offset = offset;
1108 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001109 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Martin K. Petersen9c470082009-04-09 00:27:12 +01001111 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001112 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001113 bio_integrity_trim(clone,
1114 bio_sector_offset(bio, idx, offset), len);
1115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return clone;
1118}
1119
1120/*
1121 * Creates a bio that consists of range of complete bvecs.
1122 */
1123static struct bio *clone_bio(struct bio *bio, sector_t sector,
1124 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001125 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct bio *clone;
1128
Stefan Bader9faf4002006-10-03 01:15:41 -07001129 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1130 __bio_clone(clone, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001131 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
Stefan Bader9faf4002006-10-03 01:15:41 -07001132 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 clone->bi_sector = sector;
1134 clone->bi_idx = idx;
1135 clone->bi_vcnt = idx + bv_count;
1136 clone->bi_size = to_bytes(len);
1137 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1138
Martin K. Petersen9c470082009-04-09 00:27:12 +01001139 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001140 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001141
1142 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1143 bio_integrity_trim(clone,
1144 bio_sector_offset(bio, idx, 0), len);
1145 }
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return clone;
1148}
1149
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001150static struct dm_target_io *alloc_tio(struct clone_info *ci,
1151 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001152{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001153 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001154
1155 tio->io = ci->io;
1156 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001157 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001158
1159 return tio;
1160}
1161
1162static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1163 unsigned flush_nr)
1164{
1165 struct dm_target_io *tio = alloc_tio(ci, ti);
1166 struct bio *clone;
1167
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001168 tio->info.flush_request = flush_nr;
1169
1170 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1171 __bio_clone(clone, ci->bio);
1172 clone->bi_destructor = dm_bio_destructor;
1173
1174 __map_bio(ti, clone, tio);
1175}
1176
1177static int __clone_and_map_empty_barrier(struct clone_info *ci)
1178{
1179 unsigned target_nr = 0, flush_nr;
1180 struct dm_target *ti;
1181
1182 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1183 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1184 flush_nr++)
1185 __flush_target(ci, ti, flush_nr);
1186
1187 ci->sector_count = 0;
1188
1189 return 0;
1190}
1191
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001192static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001195 struct dm_target *ti;
1196 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001197 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001199 if (unlikely(bio_empty_barrier(bio)))
1200 return __clone_and_map_empty_barrier(ci);
1201
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001202 ti = dm_table_find_target(ci->map, ci->sector);
1203 if (!dm_target_is_valid(ti))
1204 return -EIO;
1205
1206 max = max_io_len(ci->md, ci->sector, ti);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /*
1209 * Allocate a target io object.
1210 */
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001211 tio = alloc_tio(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (ci->sector_count <= max) {
1214 /*
1215 * Optimise for the simple case where we can do all of
1216 * the remaining io with a single clone.
1217 */
1218 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001219 bio->bi_vcnt - ci->idx, ci->sector_count,
1220 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 __map_bio(ti, clone, tio);
1222 ci->sector_count = 0;
1223
1224 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1225 /*
1226 * There are some bvecs that don't span targets.
1227 * Do as many of these as possible.
1228 */
1229 int i;
1230 sector_t remaining = max;
1231 sector_t bv_len;
1232
1233 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1234 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1235
1236 if (bv_len > remaining)
1237 break;
1238
1239 remaining -= bv_len;
1240 len += bv_len;
1241 }
1242
Stefan Bader9faf4002006-10-03 01:15:41 -07001243 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1244 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 __map_bio(ti, clone, tio);
1246
1247 ci->sector += len;
1248 ci->sector_count -= len;
1249 ci->idx = i;
1250
1251 } else {
1252 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001253 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
1255 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001256 sector_t remaining = to_sector(bv->bv_len);
1257 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001259 do {
1260 if (offset) {
1261 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001262 if (!dm_target_is_valid(ti))
1263 return -EIO;
1264
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001265 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001267 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001270 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001272 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001273 bv->bv_offset + offset, len,
1274 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001275
1276 __map_bio(ti, clone, tio);
1277
1278 ci->sector += len;
1279 ci->sector_count -= len;
1280 offset += to_bytes(len);
1281 } while (remaining -= len);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 ci->idx++;
1284 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001285
1286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001290 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001292static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001295 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 ci.map = dm_get_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001298 if (unlikely(!ci.map)) {
Jens Axboe1f98a132009-09-11 14:32:04 +02001299 if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001300 bio_io_error(bio);
1301 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001302 if (!md->barrier_error)
1303 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001304 return;
1305 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 ci.md = md;
1308 ci.bio = bio;
1309 ci.io = alloc_io(md);
1310 ci.io->error = 0;
1311 atomic_set(&ci.io->io_count, 1);
1312 ci.io->bio = bio;
1313 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001314 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ci.sector = bio->bi_sector;
1316 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001317 if (unlikely(bio_empty_barrier(bio)))
1318 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ci.idx = bio->bi_idx;
1320
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001321 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001322 while (ci.sector_count && !error)
1323 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001326 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 dm_table_put(ci.map);
1328}
1329/*-----------------------------------------------------------------
1330 * CRUD END
1331 *---------------------------------------------------------------*/
1332
Milan Brozf6fccb12008-07-21 12:00:37 +01001333static int dm_merge_bvec(struct request_queue *q,
1334 struct bvec_merge_data *bvm,
1335 struct bio_vec *biovec)
1336{
1337 struct mapped_device *md = q->queuedata;
1338 struct dm_table *map = dm_get_table(md);
1339 struct dm_target *ti;
1340 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001341 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001342
1343 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001344 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001345
1346 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001347 if (!dm_target_is_valid(ti))
1348 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001349
1350 /*
1351 * Find maximum amount of I/O that won't need splitting
1352 */
1353 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1354 (sector_t) BIO_MAX_SECTORS);
1355 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1356 if (max_size < 0)
1357 max_size = 0;
1358
1359 /*
1360 * merge_bvec_fn() returns number of bytes
1361 * it can accept at this offset
1362 * max is precomputed maximal io size
1363 */
1364 if (max_size && ti->type->merge)
1365 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001366 /*
1367 * If the target doesn't support merge method and some of the devices
1368 * provided their merge_bvec method (we know this by looking at
1369 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1370 * entries. So always set max_size to 0, and the code below allows
1371 * just one page.
1372 */
1373 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1374
1375 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001376
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001377out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001378 dm_table_put(map);
1379
1380out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001381 /*
1382 * Always allow an entire first page
1383 */
1384 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1385 max_size = biovec->bv_len;
1386
Milan Brozf6fccb12008-07-21 12:00:37 +01001387 return max_size;
1388}
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/*
1391 * The request function that just remaps the bio built up by
1392 * dm_merge_bvec.
1393 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001394static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Kevin Corry12f03a42006-02-01 03:04:52 -08001396 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001398 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001400 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Tejun Heo074a7ac2008-08-25 19:56:14 +09001402 cpu = part_stat_lock();
1403 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1404 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1405 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001408 * If we're suspended or the thread is processing barriers
1409 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001411 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Jens Axboe1f98a132009-09-11 14:32:04 +02001412 unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001413 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001415 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1416 bio_rw(bio) == READA) {
1417 bio_io_error(bio);
1418 return 0;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Mikulas Patocka92c63902009-04-09 00:27:15 +01001421 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Mikulas Patocka92c63902009-04-09 00:27:15 +01001423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001426 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001427 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001431static int dm_make_request(struct request_queue *q, struct bio *bio)
1432{
1433 struct mapped_device *md = q->queuedata;
1434
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001435 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1436}
1437
1438static int dm_request_based(struct mapped_device *md)
1439{
1440 return blk_queue_stackable(md->queue);
1441}
1442
1443static int dm_request(struct request_queue *q, struct bio *bio)
1444{
1445 struct mapped_device *md = q->queuedata;
1446
1447 if (dm_request_based(md))
1448 return dm_make_request(q, bio);
1449
1450 return _dm_request(q, bio);
1451}
1452
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001453/*
1454 * Mark this request as flush request, so that dm_request_fn() can
1455 * recognize.
1456 */
1457static void dm_rq_prepare_flush(struct request_queue *q, struct request *rq)
1458{
1459 rq->cmd_type = REQ_TYPE_LINUX_BLOCK;
1460 rq->cmd[0] = REQ_LB_OP_FLUSH;
1461}
1462
1463static bool dm_rq_is_flush_request(struct request *rq)
1464{
1465 if (rq->cmd_type == REQ_TYPE_LINUX_BLOCK &&
1466 rq->cmd[0] == REQ_LB_OP_FLUSH)
1467 return true;
1468 else
1469 return false;
1470}
1471
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001472void dm_dispatch_request(struct request *rq)
1473{
1474 int r;
1475
1476 if (blk_queue_io_stat(rq->q))
1477 rq->cmd_flags |= REQ_IO_STAT;
1478
1479 rq->start_time = jiffies;
1480 r = blk_insert_cloned_request(rq->q, rq);
1481 if (r)
1482 dm_complete_request(rq, r);
1483}
1484EXPORT_SYMBOL_GPL(dm_dispatch_request);
1485
1486static void dm_rq_bio_destructor(struct bio *bio)
1487{
1488 struct dm_rq_clone_bio_info *info = bio->bi_private;
1489 struct mapped_device *md = info->tio->md;
1490
1491 free_bio_info(info);
1492 bio_free(bio, md->bs);
1493}
1494
1495static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1496 void *data)
1497{
1498 struct dm_rq_target_io *tio = data;
1499 struct mapped_device *md = tio->md;
1500 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1501
1502 if (!info)
1503 return -ENOMEM;
1504
1505 info->orig = bio_orig;
1506 info->tio = tio;
1507 bio->bi_end_io = end_clone_bio;
1508 bio->bi_private = info;
1509 bio->bi_destructor = dm_rq_bio_destructor;
1510
1511 return 0;
1512}
1513
1514static int setup_clone(struct request *clone, struct request *rq,
1515 struct dm_rq_target_io *tio)
1516{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001517 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001518
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001519 if (dm_rq_is_flush_request(rq)) {
1520 blk_rq_init(NULL, clone);
1521 clone->cmd_type = REQ_TYPE_FS;
1522 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1523 } else {
1524 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1525 dm_rq_bio_constructor, tio);
1526 if (r)
1527 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001528
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001529 clone->cmd = rq->cmd;
1530 clone->cmd_len = rq->cmd_len;
1531 clone->sense = rq->sense;
1532 clone->buffer = rq->buffer;
1533 }
1534
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001535 clone->end_io = end_clone_request;
1536 clone->end_io_data = tio;
1537
1538 return 0;
1539}
1540
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001541static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1542 gfp_t gfp_mask)
1543{
1544 struct request *clone;
1545 struct dm_rq_target_io *tio;
1546
1547 tio = alloc_rq_tio(md, gfp_mask);
1548 if (!tio)
1549 return NULL;
1550
1551 tio->md = md;
1552 tio->ti = NULL;
1553 tio->orig = rq;
1554 tio->error = 0;
1555 memset(&tio->info, 0, sizeof(tio->info));
1556
1557 clone = &tio->clone;
1558 if (setup_clone(clone, rq, tio)) {
1559 /* -ENOMEM */
1560 free_rq_tio(tio);
1561 return NULL;
1562 }
1563
1564 return clone;
1565}
1566
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001567/*
1568 * Called with the queue lock held.
1569 */
1570static int dm_prep_fn(struct request_queue *q, struct request *rq)
1571{
1572 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001573 struct request *clone;
1574
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001575 if (unlikely(dm_rq_is_flush_request(rq)))
1576 return BLKPREP_OK;
1577
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001578 if (unlikely(rq->special)) {
1579 DMWARN("Already has something in rq->special.");
1580 return BLKPREP_KILL;
1581 }
1582
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001583 clone = clone_rq(rq, md, GFP_ATOMIC);
1584 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001585 return BLKPREP_DEFER;
1586
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001587 rq->special = clone;
1588 rq->cmd_flags |= REQ_DONTPREP;
1589
1590 return BLKPREP_OK;
1591}
1592
Kiyoshi Ueda598de402009-12-10 23:52:14 +00001593static void map_request(struct dm_target *ti, struct request *clone,
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001594 struct mapped_device *md)
1595{
1596 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001597 struct dm_rq_target_io *tio = clone->end_io_data;
1598
1599 /*
1600 * Hold the md reference here for the in-flight I/O.
1601 * We can't rely on the reference count by device opener,
1602 * because the device may be closed during the request completion
1603 * when all bios are completed.
1604 * See the comment in rq_completed() too.
1605 */
1606 dm_get(md);
1607
1608 tio->ti = ti;
1609 r = ti->type->map_rq(ti, clone, &tio->info);
1610 switch (r) {
1611 case DM_MAPIO_SUBMITTED:
1612 /* The target has taken the I/O to submit by itself later */
1613 break;
1614 case DM_MAPIO_REMAPPED:
1615 /* The target has remapped the I/O so dispatch it */
1616 dm_dispatch_request(clone);
1617 break;
1618 case DM_MAPIO_REQUEUE:
1619 /* The target wants to requeue the I/O */
1620 dm_requeue_unmapped_request(clone);
1621 break;
1622 default:
1623 if (r > 0) {
1624 DMWARN("unimplemented target map return value: %d", r);
1625 BUG();
1626 }
1627
1628 /* The target wants to complete the I/O */
1629 dm_kill_unmapped_request(clone, r);
1630 break;
1631 }
1632}
1633
1634/*
1635 * q->request_fn for request-based dm.
1636 * Called with the queue lock held.
1637 */
1638static void dm_request_fn(struct request_queue *q)
1639{
1640 struct mapped_device *md = q->queuedata;
1641 struct dm_table *map = dm_get_table(md);
1642 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001643 struct request *rq, *clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001644
1645 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001646 * For suspend, check blk_queue_stopped() and increment
1647 * ->pending within a single queue_lock not to increment the
1648 * number of in-flight I/Os after the queue is stopped in
1649 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001650 */
1651 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1652 rq = blk_peek_request(q);
1653 if (!rq)
1654 goto plug_and_out;
1655
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001656 if (unlikely(dm_rq_is_flush_request(rq))) {
1657 BUG_ON(md->flush_request);
1658 md->flush_request = rq;
1659 blk_start_request(rq);
1660 queue_work(md->wq, &md->barrier_work);
1661 goto out;
1662 }
1663
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001664 ti = dm_table_find_target(map, blk_rq_pos(rq));
1665 if (ti->type->busy && ti->type->busy(ti))
1666 goto plug_and_out;
1667
1668 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001669 clone = rq->special;
1670 atomic_inc(&md->pending[rq_data_dir(clone)]);
1671
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001672 spin_unlock(q->queue_lock);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001673 map_request(ti, clone, md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001674 spin_lock_irq(q->queue_lock);
1675 }
1676
1677 goto out;
1678
1679plug_and_out:
1680 if (!elv_queue_empty(q))
1681 /* Some requests still remain, retry later */
1682 blk_plug_device(q);
1683
1684out:
1685 dm_table_put(map);
1686
1687 return;
1688}
1689
1690int dm_underlying_device_busy(struct request_queue *q)
1691{
1692 return blk_lld_busy(q);
1693}
1694EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1695
1696static int dm_lld_busy(struct request_queue *q)
1697{
1698 int r;
1699 struct mapped_device *md = q->queuedata;
1700 struct dm_table *map = dm_get_table(md);
1701
1702 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1703 r = 1;
1704 else
1705 r = dm_table_any_busy_target(map);
1706
1707 dm_table_put(map);
1708
1709 return r;
1710}
1711
Jens Axboe165125e2007-07-24 09:28:11 +02001712static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct mapped_device *md = q->queuedata;
1715 struct dm_table *map = dm_get_table(md);
1716
1717 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001718 if (dm_request_based(md))
1719 generic_unplug_device(q);
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 dm_table_unplug_all(map);
1722 dm_table_put(map);
1723 }
1724}
1725
1726static int dm_any_congested(void *congested_data, int bdi_bits)
1727{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001728 int r = bdi_bits;
1729 struct mapped_device *md = congested_data;
1730 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001732 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001733 map = dm_get_table(md);
1734 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001735 /*
1736 * Request-based dm cares about only own queue for
1737 * the query about congestion status of request_queue
1738 */
1739 if (dm_request_based(md))
1740 r = md->queue->backing_dev_info.state &
1741 bdi_bits;
1742 else
1743 r = dm_table_any_congested(map, bdi_bits);
1744
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001745 dm_table_put(map);
1746 }
1747 }
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return r;
1750}
1751
1752/*-----------------------------------------------------------------
1753 * An IDR is used to keep track of allocated minor numbers.
1754 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755static DEFINE_IDR(_minor_idr);
1756
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001757static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001759 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001761 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
1764/*
1765 * See if the device with a specific minor # is free.
1766 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001767static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 int r, m;
1770
1771 if (minor >= (1 << MINORBITS))
1772 return -EINVAL;
1773
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001774 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1775 if (!r)
1776 return -ENOMEM;
1777
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001778 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 if (idr_find(&_minor_idr, minor)) {
1781 r = -EBUSY;
1782 goto out;
1783 }
1784
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001785 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001786 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if (m != minor) {
1790 idr_remove(&_minor_idr, m);
1791 r = -EBUSY;
1792 goto out;
1793 }
1794
1795out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001796 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return r;
1798}
1799
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001800static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001802 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001805 if (!r)
1806 return -ENOMEM;
1807
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001808 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001810 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001811 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 if (m >= (1 << MINORBITS)) {
1815 idr_remove(&_minor_idr, m);
1816 r = -ENOSPC;
1817 goto out;
1818 }
1819
1820 *minor = m;
1821
1822out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001823 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return r;
1825}
1826
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001827static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Mikulas Patocka53d59142009-04-02 19:55:37 +01001829static void dm_wq_work(struct work_struct *work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001830static void dm_rq_barrier_work(struct work_struct *work);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832/*
1833 * Allocate and initialise a blank device with a given minor.
1834 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001835static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001838 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001839 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 if (!md) {
1842 DMWARN("unable to allocate device, out of memory.");
1843 return NULL;
1844 }
1845
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001846 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001847 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001850 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001851 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001852 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001853 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001855 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001857 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001858 mutex_init(&md->suspend_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001859 spin_lock_init(&md->deferred_lock);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001860 spin_lock_init(&md->barrier_error_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 rwlock_init(&md->map_lock);
1862 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001863 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001865 atomic_set(&md->uevent_seq, 0);
1866 INIT_LIST_HEAD(&md->uevent_list);
1867 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001869 md->queue = blk_init_queue(dm_request_fn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001871 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001873 /*
1874 * Request-based dm devices cannot be stacked on top of bio-based dm
1875 * devices. The type of this dm device has not been decided yet,
1876 * although we initialized the queue using blk_init_queue().
1877 * The type is decided at the first table loading time.
1878 * To prevent problematic device stacking, clear the queue flag
1879 * for request stacking support until then.
1880 *
1881 * This queue is new, so no concurrency on the queue_flags.
1882 */
1883 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1884 md->saved_make_request_fn = md->queue->make_request_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 md->queue->queuedata = md;
1886 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1887 md->queue->backing_dev_info.congested_data = md;
1888 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +01001889 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001891 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001892 blk_queue_softirq_done(md->queue, dm_softirq_done);
1893 blk_queue_prep_rq(md->queue, dm_prep_fn);
1894 blk_queue_lld_busy(md->queue, dm_lld_busy);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001895 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH,
1896 dm_rq_prepare_flush);
Stefan Bader9faf4002006-10-03 01:15:41 -07001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 md->disk = alloc_disk(1);
1899 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001900 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001902 atomic_set(&md->pending[0], 0);
1903 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001904 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001905 INIT_WORK(&md->work, dm_wq_work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001906 INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001907 init_waitqueue_head(&md->eventq);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 md->disk->major = _major;
1910 md->disk->first_minor = minor;
1911 md->disk->fops = &dm_blk_dops;
1912 md->disk->queue = md->queue;
1913 md->disk->private_data = md;
1914 sprintf(md->disk->disk_name, "dm-%d", minor);
1915 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001916 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Milan Broz304f3f62008-02-08 02:11:17 +00001918 md->wq = create_singlethread_workqueue("kdmflush");
1919 if (!md->wq)
1920 goto bad_thread;
1921
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001922 md->bdev = bdget_disk(md->disk, 0);
1923 if (!md->bdev)
1924 goto bad_bdev;
1925
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001926 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001927 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001928 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001929 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001930
1931 BUG_ON(old_md != MINOR_ALLOCED);
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return md;
1934
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001935bad_bdev:
1936 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001937bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001938 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001939 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001940bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001941 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001942bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001944bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001945 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001946bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 kfree(md);
1948 return NULL;
1949}
1950
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001951static void unlock_fs(struct mapped_device *md);
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953static void free_dev(struct mapped_device *md)
1954{
Tejun Heof331c022008-09-03 09:01:48 +02001955 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001956
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001957 unlock_fs(md);
1958 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001959 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001960 if (md->tio_pool)
1961 mempool_destroy(md->tio_pool);
1962 if (md->io_pool)
1963 mempool_destroy(md->io_pool);
1964 if (md->bs)
1965 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001966 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001968 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001969
1970 spin_lock(&_minor_lock);
1971 md->disk->private_data = NULL;
1972 spin_unlock(&_minor_lock);
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001975 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001976 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 kfree(md);
1978}
1979
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001980static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1981{
1982 struct dm_md_mempools *p;
1983
1984 if (md->io_pool && md->tio_pool && md->bs)
1985 /* the md already has necessary mempools */
1986 goto out;
1987
1988 p = dm_table_get_md_mempools(t);
1989 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1990
1991 md->io_pool = p->io_pool;
1992 p->io_pool = NULL;
1993 md->tio_pool = p->tio_pool;
1994 p->tio_pool = NULL;
1995 md->bs = p->bs;
1996 p->bs = NULL;
1997
1998out:
1999 /* mempool bind completed, now no need any mempools in the table */
2000 dm_table_free_md_mempools(t);
2001}
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003/*
2004 * Bind a table to the device.
2005 */
2006static void event_callback(void *context)
2007{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002008 unsigned long flags;
2009 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct mapped_device *md = (struct mapped_device *) context;
2011
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002012 spin_lock_irqsave(&md->uevent_lock, flags);
2013 list_splice_init(&md->uevent_list, &uevents);
2014 spin_unlock_irqrestore(&md->uevent_lock, flags);
2015
Tejun Heoed9e1982008-08-25 19:56:05 +09002016 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 atomic_inc(&md->event_nr);
2019 wake_up(&md->eventq);
2020}
2021
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002022static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002024 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002026 mutex_lock(&md->bdev->bd_inode->i_mutex);
2027 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2028 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002031static int __bind(struct mapped_device *md, struct dm_table *t,
2032 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Jens Axboe165125e2007-07-24 09:28:11 +02002034 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002036 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002039
2040 /*
2041 * Wipe any geometry if the size of the table changed.
2042 */
2043 if (size != get_capacity(md->disk))
2044 memset(&md->geometry, 0, sizeof(md->geometry));
2045
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002046 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Mikulas Patockad5816872009-01-06 03:05:10 +00002048 if (!size) {
2049 dm_table_destroy(t);
2050 return 0;
2051 }
2052
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002053 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002054
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002055 /*
2056 * The queue hasn't been stopped yet, if the old table type wasn't
2057 * for request-based during suspension. So stop it to prevent
2058 * I/O mapping before resume.
2059 * This must be done before setting the queue restrictions,
2060 * because request-based dm may be run just after the setting.
2061 */
2062 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2063 stop_queue(q);
2064
2065 __bind_mempools(md, t);
2066
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002067 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002068 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002069 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002070 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
2073}
2074
2075static void __unbind(struct mapped_device *md)
2076{
2077 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002078 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if (!map)
2081 return;
2082
2083 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002084 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002086 write_unlock_irqrestore(&md->map_lock, flags);
Mikulas Patockad5816872009-01-06 03:05:10 +00002087 dm_table_destroy(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
2090/*
2091 * Constructor for a new device.
2092 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002093int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 struct mapped_device *md;
2096
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002097 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (!md)
2099 return -ENXIO;
2100
Milan Broz784aae72009-01-06 03:05:12 +00002101 dm_sysfs_init(md);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 *result = md;
2104 return 0;
2105}
2106
David Teigland637842c2006-01-06 00:20:00 -08002107static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 unsigned minor = MINOR(dev);
2111
2112 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2113 return NULL;
2114
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002115 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002118 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002119 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002120 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002121 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002122 goto out;
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002125out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002126 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
David Teigland637842c2006-01-06 00:20:00 -08002128 return md;
2129}
2130
David Teiglandd229a952006-01-06 00:20:01 -08002131struct mapped_device *dm_get_md(dev_t dev)
2132{
2133 struct mapped_device *md = dm_find_md(dev);
2134
2135 if (md)
2136 dm_get(md);
2137
2138 return md;
2139}
2140
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002141void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002142{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002143 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146void dm_set_mdptr(struct mapped_device *md, void *ptr)
2147{
2148 md->interface_ptr = ptr;
2149}
2150
2151void dm_get(struct mapped_device *md)
2152{
2153 atomic_inc(&md->holders);
2154}
2155
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002156const char *dm_device_name(struct mapped_device *md)
2157{
2158 return md->name;
2159}
2160EXPORT_SYMBOL_GPL(dm_device_name);
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162void dm_put(struct mapped_device *md)
2163{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002164 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002166 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2167
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002168 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08002169 map = dm_get_table(md);
Tejun Heof331c022008-09-03 09:01:48 +02002170 idr_replace(&_minor_idr, MINOR_ALLOCED,
2171 MINOR(disk_devt(dm_disk(md))));
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002172 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002173 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002174 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 dm_table_presuspend_targets(map);
2176 dm_table_postsuspend_targets(map);
2177 }
Milan Broz784aae72009-01-06 03:05:12 +00002178 dm_sysfs_exit(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002179 dm_table_put(map);
Mikulas Patockaa1b51e92009-01-06 03:04:53 +00002180 __unbind(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 free_dev(md);
2182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
Edward Goggin79eb8852007-05-09 02:32:56 -07002184EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Mikulas Patocka401600d2009-04-02 19:55:38 +01002186static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002187{
2188 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002189 DECLARE_WAITQUEUE(wait, current);
2190
2191 dm_unplug_all(md->queue);
2192
2193 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002194
2195 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002196 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002197
2198 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002199 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002200 break;
2201
Mikulas Patocka401600d2009-04-02 19:55:38 +01002202 if (interruptible == TASK_INTERRUPTIBLE &&
2203 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002204 r = -EINTR;
2205 break;
2206 }
2207
2208 io_schedule();
2209 }
2210 set_current_state(TASK_RUNNING);
2211
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002212 remove_wait_queue(&md->wait, &wait);
2213
Milan Broz46125c12008-02-08 02:10:30 +00002214 return r;
2215}
2216
Mikulas Patocka531fe962009-06-22 10:12:17 +01002217static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002218{
2219 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002220
2221 bio_init(&md->barrier_bio);
2222 md->barrier_bio.bi_bdev = md->bdev;
2223 md->barrier_bio.bi_rw = WRITE_BARRIER;
2224 __split_and_process_bio(md, &md->barrier_bio);
2225
2226 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002227}
2228
2229static void process_barrier(struct mapped_device *md, struct bio *bio)
2230{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002231 md->barrier_error = 0;
2232
Mikulas Patocka531fe962009-06-22 10:12:17 +01002233 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002234
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002235 if (!bio_empty_barrier(bio)) {
2236 __split_and_process_bio(md, bio);
2237 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002238 }
2239
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002240 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002241 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002242 else {
2243 spin_lock_irq(&md->deferred_lock);
2244 bio_list_add_head(&md->deferred, bio);
2245 spin_unlock_irq(&md->deferred_lock);
2246 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002247}
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249/*
2250 * Process the deferred bios
2251 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002252static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Mikulas Patockaef208582009-04-02 19:55:38 +01002254 struct mapped_device *md = container_of(work, struct mapped_device,
2255 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002256 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Mikulas Patockaef208582009-04-02 19:55:38 +01002258 down_write(&md->io_lock);
2259
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002260 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002261 spin_lock_irq(&md->deferred_lock);
2262 c = bio_list_pop(&md->deferred);
2263 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002264
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002265 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002266 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002267 break;
2268 }
2269
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002270 up_write(&md->io_lock);
2271
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002272 if (dm_request_based(md))
2273 generic_make_request(c);
2274 else {
Jens Axboe1f98a132009-09-11 14:32:04 +02002275 if (bio_rw_flagged(c, BIO_RW_BARRIER))
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002276 process_barrier(md, c);
2277 else
2278 __split_and_process_bio(md, c);
2279 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002280
2281 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002282 }
Milan Broz73d410c2008-02-08 02:10:25 +00002283
Mikulas Patockaef208582009-04-02 19:55:38 +01002284 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002287static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002288{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002289 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2290 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002291 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002292}
2293
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002294static void dm_rq_set_flush_nr(struct request *clone, unsigned flush_nr)
2295{
2296 struct dm_rq_target_io *tio = clone->end_io_data;
2297
2298 tio->info.flush_request = flush_nr;
2299}
2300
2301/* Issue barrier requests to targets and wait for their completion. */
2302static int dm_rq_barrier(struct mapped_device *md)
2303{
2304 int i, j;
2305 struct dm_table *map = dm_get_table(md);
2306 unsigned num_targets = dm_table_get_num_targets(map);
2307 struct dm_target *ti;
2308 struct request *clone;
2309
2310 md->barrier_error = 0;
2311
2312 for (i = 0; i < num_targets; i++) {
2313 ti = dm_table_get_target(map, i);
2314 for (j = 0; j < ti->num_flush_requests; j++) {
2315 clone = clone_rq(md->flush_request, md, GFP_NOIO);
2316 dm_rq_set_flush_nr(clone, j);
2317 atomic_inc(&md->pending[rq_data_dir(clone)]);
2318 map_request(ti, clone, md);
2319 }
2320 }
2321
2322 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2323 dm_table_put(map);
2324
2325 return md->barrier_error;
2326}
2327
2328static void dm_rq_barrier_work(struct work_struct *work)
2329{
2330 int error;
2331 struct mapped_device *md = container_of(work, struct mapped_device,
2332 barrier_work);
2333 struct request_queue *q = md->queue;
2334 struct request *rq;
2335 unsigned long flags;
2336
2337 /*
2338 * Hold the md reference here and leave it at the last part so that
2339 * the md can't be deleted by device opener when the barrier request
2340 * completes.
2341 */
2342 dm_get(md);
2343
2344 error = dm_rq_barrier(md);
2345
2346 rq = md->flush_request;
2347 md->flush_request = NULL;
2348
2349 if (error == DM_ENDIO_REQUEUE) {
2350 spin_lock_irqsave(q->queue_lock, flags);
2351 blk_requeue_request(q, rq);
2352 spin_unlock_irqrestore(q->queue_lock, flags);
2353 } else
2354 blk_end_request_all(rq, error);
2355
2356 blk_run_queue(q);
2357
2358 dm_put(md);
2359}
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361/*
2362 * Swap in a new table (destroying old one).
2363 */
2364int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2365{
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002366 struct queue_limits limits;
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002367 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Daniel Walkere61290a2008-02-08 02:10:08 +00002369 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002372 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002375 r = dm_calculate_queue_limits(table, &limits);
2376 if (r)
2377 goto out;
2378
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002379 /* cannot change the device type, once a table is bound */
2380 if (md->map &&
2381 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2382 DMWARN("can't change the device type after a table is bound");
2383 goto out;
2384 }
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 __unbind(md);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002387 r = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002389out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002390 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002391 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
2394/*
2395 * Functions to lock and unlock any filesystem running on the
2396 * device.
2397 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002398static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002400 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002403
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002404 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002405 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002406 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002407 md->frozen_sb = NULL;
2408 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002409 }
2410
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002411 set_bit(DMF_FROZEN, &md->flags);
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return 0;
2414}
2415
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002416static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002418 if (!test_bit(DMF_FROZEN, &md->flags))
2419 return;
2420
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002421 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002423 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
2426/*
2427 * We need to be able to change a mapping table under a mounted
2428 * filesystem. For example we might want to move some data in
2429 * the background. Before the table can be swapped with
2430 * dm_bind_table, dm_suspend must be called to flush any in
2431 * flight bios and ensure that any further io gets deferred.
2432 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002433/*
2434 * Suspend mechanism in request-based dm.
2435 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002436 * 1. Flush all I/Os by lock_fs() if needed.
2437 * 2. Stop dispatching any I/O by stopping the request_queue.
2438 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002439 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002440 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002441 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002442int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002444 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002445 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002446 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002447 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Daniel Walkere61290a2008-02-08 02:10:08 +00002449 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002450
Milan Broz73d410c2008-02-08 02:10:25 +00002451 if (dm_suspended(md)) {
2452 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002453 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002458 /*
2459 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2460 * This flag is cleared before dm_suspend returns.
2461 */
2462 if (noflush)
2463 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2464
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002465 /* This does not get reverted if there's an error later. */
2466 dm_table_presuspend_targets(map);
2467
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002468 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002469 * Flush I/O to the device.
2470 * Any I/O submitted after lock_fs() may not be flushed.
2471 * noflush takes precedence over do_lockfs.
2472 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002473 */
2474 if (!noflush && do_lockfs) {
2475 r = lock_fs(md);
2476 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002477 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002481 * Here we must make sure that no processes are submitting requests
2482 * to target drivers i.e. no one may be executing
2483 * __split_and_process_bio. This is called from dm_request and
2484 * dm_wq_work.
2485 *
2486 * To get all processes out of __split_and_process_bio in dm_request,
2487 * we take the write lock. To prevent any process from reentering
2488 * __split_and_process_bio from dm_request, we set
2489 * DMF_QUEUE_IO_TO_THREAD.
2490 *
2491 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2492 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2493 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2494 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002496 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002497 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2498 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002499 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002501 /*
2502 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2503 * can be kicked until md->queue is stopped. So stop md->queue before
2504 * flushing md->wq.
2505 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002506 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002507 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002508
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002509 flush_workqueue(md->wq);
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002512 * At this point no more requests are entering target request routines.
2513 * We call dm_wait_for_completion to wait for all existing requests
2514 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002516 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002518 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002519 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002520 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002521 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002524 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002525 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002526
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002527 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002528 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002529
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002530 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002531 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002532 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002533
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002534 /*
2535 * If dm_wait_for_completion returned 0, the device is completely
2536 * quiescent now. There is no request-processing activity. All new
2537 * requests are being added to md->deferred list.
2538 */
2539
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002540 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 set_bit(DMF_SUSPENDED, &md->flags);
2543
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002544out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002546
2547out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002548 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002549 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552int dm_resume(struct mapped_device *md)
2553{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002554 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002555 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Daniel Walkere61290a2008-02-08 02:10:08 +00002557 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002558 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002559 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002560
2561 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002562 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002563 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Milan Broz8757b772006-10-03 01:15:36 -07002565 r = dm_table_resume_targets(map);
2566 if (r)
2567 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002568
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002569 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002570
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002571 /*
2572 * Flushing deferred I/Os must be done after targets are resumed
2573 * so that mapping of targets can work correctly.
2574 * Request-based dm is queueing the deferred I/Os in its request_queue.
2575 */
2576 if (dm_request_based(md))
2577 start_queue(md->queue);
2578
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002579 unlock_fs(md);
2580
2581 clear_bit(DMF_SUSPENDED, &md->flags);
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002584 r = 0;
2585out:
2586 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002587 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002588
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002589 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
2592/*-----------------------------------------------------------------
2593 * Event notification.
2594 *---------------------------------------------------------------*/
Milan Broz60935eb2009-06-22 10:12:30 +01002595void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2596 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002597{
Milan Broz60935eb2009-06-22 10:12:30 +01002598 char udev_cookie[DM_COOKIE_LENGTH];
2599 char *envp[] = { udev_cookie, NULL };
2600
2601 if (!cookie)
2602 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2603 else {
2604 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2605 DM_COOKIE_ENV_VAR_NAME, cookie);
2606 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2607 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002608}
2609
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002610uint32_t dm_next_uevent_seq(struct mapped_device *md)
2611{
2612 return atomic_add_return(1, &md->uevent_seq);
2613}
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615uint32_t dm_get_event_nr(struct mapped_device *md)
2616{
2617 return atomic_read(&md->event_nr);
2618}
2619
2620int dm_wait_event(struct mapped_device *md, int event_nr)
2621{
2622 return wait_event_interruptible(md->eventq,
2623 (event_nr != atomic_read(&md->event_nr)));
2624}
2625
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002626void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2627{
2628 unsigned long flags;
2629
2630 spin_lock_irqsave(&md->uevent_lock, flags);
2631 list_add(elist, &md->uevent_list);
2632 spin_unlock_irqrestore(&md->uevent_lock, flags);
2633}
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635/*
2636 * The gendisk is only valid as long as you have a reference
2637 * count on 'md'.
2638 */
2639struct gendisk *dm_disk(struct mapped_device *md)
2640{
2641 return md->disk;
2642}
2643
Milan Broz784aae72009-01-06 03:05:12 +00002644struct kobject *dm_kobject(struct mapped_device *md)
2645{
2646 return &md->kobj;
2647}
2648
2649/*
2650 * struct mapped_device should not be exported outside of dm.c
2651 * so use this check to verify that kobj is part of md structure
2652 */
2653struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2654{
2655 struct mapped_device *md;
2656
2657 md = container_of(kobj, struct mapped_device, kobj);
2658 if (&md->kobj != kobj)
2659 return NULL;
2660
Milan Broz4d89b7b2009-06-22 10:12:11 +01002661 if (test_bit(DMF_FREEING, &md->flags) ||
2662 test_bit(DMF_DELETING, &md->flags))
2663 return NULL;
2664
Milan Broz784aae72009-01-06 03:05:12 +00002665 dm_get(md);
2666 return md;
2667}
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669int dm_suspended(struct mapped_device *md)
2670{
2671 return test_bit(DMF_SUSPENDED, &md->flags);
2672}
2673
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002674int dm_noflush_suspending(struct dm_target *ti)
2675{
2676 struct mapped_device *md = dm_table_get_md(ti->table);
2677 int r = __noflush_suspending(md);
2678
2679 dm_put(md);
2680
2681 return r;
2682}
2683EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2684
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002685struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2686{
2687 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2688
2689 if (!pools)
2690 return NULL;
2691
2692 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2693 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2694 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2695 if (!pools->io_pool)
2696 goto free_pools_and_out;
2697
2698 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2699 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2700 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2701 if (!pools->tio_pool)
2702 goto free_io_pool_and_out;
2703
2704 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2705 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2706 if (!pools->bs)
2707 goto free_tio_pool_and_out;
2708
2709 return pools;
2710
2711free_tio_pool_and_out:
2712 mempool_destroy(pools->tio_pool);
2713
2714free_io_pool_and_out:
2715 mempool_destroy(pools->io_pool);
2716
2717free_pools_and_out:
2718 kfree(pools);
2719
2720 return NULL;
2721}
2722
2723void dm_free_md_mempools(struct dm_md_mempools *pools)
2724{
2725 if (!pools)
2726 return;
2727
2728 if (pools->io_pool)
2729 mempool_destroy(pools->io_pool);
2730
2731 if (pools->tio_pool)
2732 mempool_destroy(pools->tio_pool);
2733
2734 if (pools->bs)
2735 bioset_free(pools->bs);
2736
2737 kfree(pools);
2738}
2739
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002740static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 .open = dm_blk_open,
2742 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002743 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002744 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 .owner = THIS_MODULE
2746};
2747
2748EXPORT_SYMBOL(dm_get_mapinfo);
2749
2750/*
2751 * module hooks
2752 */
2753module_init(dm_init);
2754module_exit(dm_exit);
2755
2756module_param(major, uint, 0);
2757MODULE_PARM_DESC(major, "The major number of the device mapper");
2758MODULE_DESCRIPTION(DM_NAME " driver");
2759MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2760MODULE_LICENSE("GPL");