blob: f3cc5d99fe8dcd6064a6c6880de3b3a90b12b6ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
17#include <linux/buffer_head.h>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020018#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mempool.h>
20#include <linux/slab.h>
21#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080022#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010023#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080024
25#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "core"
28
Milan Broz60935eb2009-06-22 10:12:30 +010029/*
30 * Cookies are numeric values sent with CHANGE and REMOVE
31 * uevents while resuming, removing or renaming the device.
32 */
33#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
34#define DM_COOKIE_LENGTH 24
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static const char *_name = DM_NAME;
37
38static unsigned int major = 0;
39static unsigned int _major = 0;
40
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070041static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000043 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * One of these is allocated per bio.
45 */
46struct dm_io {
47 struct mapped_device *md;
48 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010050 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080051 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010052 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000056 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * One of these is allocated per target within a bio. Hopefully
58 * this will be simplified out one day.
59 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010060struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct dm_io *io;
62 struct dm_target *ti;
63 union map_info info;
64};
65
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000066/*
67 * For request-based dm.
68 * One of these is allocated per request.
69 */
70struct dm_rq_target_io {
71 struct mapped_device *md;
72 struct dm_target *ti;
73 struct request *orig, clone;
74 int error;
75 union map_info info;
76};
77
78/*
79 * For request-based dm.
80 * One of these is allocated per bio.
81 */
82struct dm_rq_clone_bio_info {
83 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010084 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087union map_info *dm_get_mapinfo(struct bio *bio)
88{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070089 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010090 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070091 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010094union map_info *dm_get_rq_mapinfo(struct request *rq)
95{
96 if (rq && rq->end_io_data)
97 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
98 return NULL;
99}
100EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
101
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700102#define MINOR_ALLOCED ((void *)-1)
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Bits for the md->flags field.
106 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100107#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800109#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700110#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700111#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800112#define DMF_NOFLUSH_SUSPENDING 5
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100113#define DMF_QUEUE_IO_TO_THREAD 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Milan Broz304f3f62008-02-08 02:11:17 +0000115/*
116 * Work processed by per-device workqueue.
117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700119 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000120 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 rwlock_t map_lock;
122 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700123 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 unsigned long flags;
126
Jens Axboe165125e2007-07-24 09:28:11 +0200127 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800129 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 void *interface_ptr;
132
133 /*
134 * A list of ios that arrived while we were suspended.
135 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200136 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100138 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800139 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100140 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /*
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100143 * An error from the barrier request currently being processed.
144 */
145 int barrier_error;
146
147 /*
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000148 * Protect barrier_error from concurrent endio processing
149 * in request-based dm.
150 */
151 spinlock_t barrier_error_lock;
152
153 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000154 * Processing queue (flush/barriers)
155 */
156 struct workqueue_struct *wq;
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000157 struct work_struct barrier_work;
158
159 /* A pointer to the currently processing pre/post flush request */
160 struct request *flush_request;
Milan Broz304f3f62008-02-08 02:11:17 +0000161
162 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * The current mapping.
164 */
165 struct dm_table *map;
166
167 /*
168 * io objects are allocated from here.
169 */
170 mempool_t *io_pool;
171 mempool_t *tio_pool;
172
Stefan Bader9faf4002006-10-03 01:15:41 -0700173 struct bio_set *bs;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /*
176 * Event handling.
177 */
178 atomic_t event_nr;
179 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100180 atomic_t uevent_seq;
181 struct list_head uevent_list;
182 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /*
185 * freeze/thaw support require holding onto a super block
186 */
187 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100188 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800189
190 /* forced geometry settings */
191 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000192
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100193 /* For saving the address of __make_request for request based dm */
194 make_request_fn *saved_make_request_fn;
195
Milan Broz784aae72009-01-06 03:05:12 +0000196 /* sysfs handle */
197 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100198
199 /* zero-length barrier that will be cloned and submitted to targets */
200 struct bio barrier_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100203/*
204 * For mempools pre-allocation at the table loading time.
205 */
206struct dm_md_mempools {
207 mempool_t *io_pool;
208 mempool_t *tio_pool;
209 struct bio_set *bs;
210};
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800213static struct kmem_cache *_io_cache;
214static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000215static struct kmem_cache *_rq_tio_cache;
216static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static int __init local_init(void)
219{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100220 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100223 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100225 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100228 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100229 if (!_tio_cache)
230 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000232 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
233 if (!_rq_tio_cache)
234 goto out_free_tio_cache;
235
236 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
237 if (!_rq_bio_info_cache)
238 goto out_free_rq_tio_cache;
239
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100240 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100241 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000242 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 _major = major;
245 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100246 if (r < 0)
247 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (!_major)
250 _major = r;
251
252 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100253
254out_uevent_exit:
255 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000256out_free_rq_bio_info_cache:
257 kmem_cache_destroy(_rq_bio_info_cache);
258out_free_rq_tio_cache:
259 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100260out_free_tio_cache:
261 kmem_cache_destroy(_tio_cache);
262out_free_io_cache:
263 kmem_cache_destroy(_io_cache);
264
265 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268static void local_exit(void)
269{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000270 kmem_cache_destroy(_rq_bio_info_cache);
271 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 kmem_cache_destroy(_tio_cache);
273 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700274 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100275 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 _major = 0;
278
279 DMINFO("cleaned up");
280}
281
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000282static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 local_init,
284 dm_target_init,
285 dm_linear_init,
286 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000287 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100288 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dm_interface_init,
290};
291
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000292static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 local_exit,
294 dm_target_exit,
295 dm_linear_exit,
296 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000297 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100298 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 dm_interface_exit,
300};
301
302static int __init dm_init(void)
303{
304 const int count = ARRAY_SIZE(_inits);
305
306 int r, i;
307
308 for (i = 0; i < count; i++) {
309 r = _inits[i]();
310 if (r)
311 goto bad;
312 }
313
314 return 0;
315
316 bad:
317 while (i--)
318 _exits[i]();
319
320 return r;
321}
322
323static void __exit dm_exit(void)
324{
325 int i = ARRAY_SIZE(_exits);
326
327 while (i--)
328 _exits[i]();
329}
330
331/*
332 * Block device functions
333 */
Mike Anderson432a2122009-12-10 23:52:20 +0000334int dm_deleting_md(struct mapped_device *md)
335{
336 return test_bit(DMF_DELETING, &md->flags);
337}
338
Al Virofe5f9f22008-03-02 10:29:31 -0500339static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct mapped_device *md;
342
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200343 lock_kernel();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700344 spin_lock(&_minor_lock);
345
Al Virofe5f9f22008-03-02 10:29:31 -0500346 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700347 if (!md)
348 goto out;
349
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700350 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000351 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700352 md = NULL;
353 goto out;
354 }
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700357 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700358
359out:
360 spin_unlock(&_minor_lock);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200361 unlock_kernel();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700362
363 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Al Virofe5f9f22008-03-02 10:29:31 -0500366static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Al Virofe5f9f22008-03-02 10:29:31 -0500368 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200369
370 lock_kernel();
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700371 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dm_put(md);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200373 unlock_kernel();
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376}
377
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700378int dm_open_count(struct mapped_device *md)
379{
380 return atomic_read(&md->open_count);
381}
382
383/*
384 * Guarantees nothing is using the device before it's deleted.
385 */
386int dm_lock_for_deletion(struct mapped_device *md)
387{
388 int r = 0;
389
390 spin_lock(&_minor_lock);
391
392 if (dm_open_count(md))
393 r = -EBUSY;
394 else
395 set_bit(DMF_DELETING, &md->flags);
396
397 spin_unlock(&_minor_lock);
398
399 return r;
400}
401
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800402static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
403{
404 struct mapped_device *md = bdev->bd_disk->private_data;
405
406 return dm_get_geometry(md, geo);
407}
408
Al Virofe5f9f22008-03-02 10:29:31 -0500409static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700410 unsigned int cmd, unsigned long arg)
411{
Al Virofe5f9f22008-03-02 10:29:31 -0500412 struct mapped_device *md = bdev->bd_disk->private_data;
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000413 struct dm_table *map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700414 struct dm_target *tgt;
415 int r = -ENOTTY;
416
Milan Brozaa129a22006-10-03 01:15:15 -0700417 if (!map || !dm_table_get_size(map))
418 goto out;
419
420 /* We only support devices that have a single target */
421 if (dm_table_get_num_targets(map) != 1)
422 goto out;
423
424 tgt = dm_table_get_target(map, 0);
425
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000426 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700427 r = -EAGAIN;
428 goto out;
429 }
430
431 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400432 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700433
434out:
435 dm_table_put(map);
436
Milan Brozaa129a22006-10-03 01:15:15 -0700437 return r;
438}
439
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100440static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 return mempool_alloc(md->io_pool, GFP_NOIO);
443}
444
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100445static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 mempool_free(io, md->io_pool);
448}
449
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100450static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 mempool_free(tio, md->tio_pool);
453}
454
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000455static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
456 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100457{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000458 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100459}
460
461static void free_rq_tio(struct dm_rq_target_io *tio)
462{
463 mempool_free(tio, tio->md->tio_pool);
464}
465
466static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
467{
468 return mempool_alloc(md->io_pool, GFP_ATOMIC);
469}
470
471static void free_bio_info(struct dm_rq_clone_bio_info *info)
472{
473 mempool_free(info, info->tio->md->io_pool);
474}
475
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000476static int md_in_flight(struct mapped_device *md)
477{
478 return atomic_read(&md->pending[READ]) +
479 atomic_read(&md->pending[WRITE]);
480}
481
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800482static void start_io_acct(struct dm_io *io)
483{
484 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900485 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200486 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800487
488 io->start_time = jiffies;
489
Tejun Heo074a7ac2008-08-25 19:56:14 +0900490 cpu = part_stat_lock();
491 part_round_stats(cpu, &dm_disk(md)->part0);
492 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200493 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800494}
495
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000496static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800497{
498 struct mapped_device *md = io->md;
499 struct bio *bio = io->bio;
500 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900501 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800502 int rw = bio_data_dir(bio);
503
Tejun Heo074a7ac2008-08-25 19:56:14 +0900504 cpu = part_stat_lock();
505 part_round_stats(cpu, &dm_disk(md)->part0);
506 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
507 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800508
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100509 /*
510 * After this is decremented the bio must not be touched if it is
511 * a barrier.
512 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200513 dm_disk(md)->part0.in_flight[rw] = pending =
514 atomic_dec_return(&md->pending[rw]);
515 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800516
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000517 /* nudge anyone waiting on suspend queue */
518 if (!pending)
519 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
523 * Add the bio to the list of deferred io.
524 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100525static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700527 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Mikulas Patocka022c2612009-04-02 19:55:39 +0100529 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100531 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Mikulas Patocka92c63902009-04-09 00:27:15 +0100533 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
534 queue_work(md->wq, &md->work);
535
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700536 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/*
540 * Everyone (including functions in this file), should use this
541 * function to access the md->map field, and make sure they call
542 * dm_table_put() when finished.
543 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000544struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100547 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100549 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 t = md->map;
551 if (t)
552 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100553 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 return t;
556}
557
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800558/*
559 * Get the geometry associated with a dm device
560 */
561int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
562{
563 *geo = md->geometry;
564
565 return 0;
566}
567
568/*
569 * Set the geometry of a device.
570 */
571int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
572{
573 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
574
575 if (geo->start > sz) {
576 DMWARN("Start sector is beyond the geometry limits.");
577 return -EINVAL;
578 }
579
580 md->geometry = *geo;
581
582 return 0;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*-----------------------------------------------------------------
586 * CRUD START:
587 * A more elegant soln is in the works that uses the queue
588 * merge fn, unfortunately there are a couple of changes to
589 * the block layer that I want to make for this. So in the
590 * interests of getting something for people to use I give
591 * you this clearly demarcated crap.
592 *---------------------------------------------------------------*/
593
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800594static int __noflush_suspending(struct mapped_device *md)
595{
596 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * Decrements the number of outstanding ios that a bio has been
601 * cloned into, completing the original io if necc.
602 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800603static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800605 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000606 int io_error;
607 struct bio *bio;
608 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800609
610 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100611 if (unlikely(error)) {
612 spin_lock_irqsave(&io->endio_lock, flags);
613 if (!(io->error > 0 && __noflush_suspending(md)))
614 io->error = error;
615 spin_unlock_irqrestore(&io->endio_lock, flags);
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800619 if (io->error == DM_ENDIO_REQUEUE) {
620 /*
621 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800622 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100623 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100624 if (__noflush_suspending(md)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200625 if (!(io->bio->bi_rw & REQ_HARDBARRIER))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100626 bio_list_add_head(&md->deferred,
627 io->bio);
628 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800629 /* noflush suspend was interrupted. */
630 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100631 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800632 }
633
Milan Brozb35f8ca2009-03-16 17:44:36 +0000634 io_error = io->error;
635 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100636
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200637 if (bio->bi_rw & REQ_HARDBARRIER) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100638 /*
639 * There can be just one barrier request so we use
640 * a per-device variable for error reporting.
641 * Note that you can't touch the bio after end_io_acct
Mikulas Patocka708e9292010-08-12 04:14:00 +0100642 *
643 * We ignore -EOPNOTSUPP for empty flush reported by
644 * underlying devices. We assume that if the device
645 * doesn't support empty barriers, it doesn't need
646 * cache flushing commands.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100647 */
Mikulas Patocka708e9292010-08-12 04:14:00 +0100648 if (!md->barrier_error &&
649 !(bio_empty_barrier(bio) && io_error == -EOPNOTSUPP))
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100650 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100651 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000652 free_io(md, io);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100653 } else {
654 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000655 free_io(md, io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000656
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100657 if (io_error != DM_ENDIO_REQUEUE) {
658 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000659
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100660 bio_endio(bio, io_error);
661 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664}
665
NeilBrown6712ecf2007-09-27 12:47:43 +0200666static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100669 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000670 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700671 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 dm_endio_fn endio = tio->ti->type->end_io;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
675 error = -EIO;
676
677 if (endio) {
678 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800679 if (r < 0 || r == DM_ENDIO_REQUEUE)
680 /*
681 * error and requeue request are handled
682 * in dec_pending().
683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800685 else if (r == DM_ENDIO_INCOMPLETE)
686 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200687 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800688 else if (r) {
689 DMWARN("unimplemented target endio return value: %d", r);
690 BUG();
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
Stefan Bader9faf4002006-10-03 01:15:41 -0700694 /*
695 * Store md for cleanup instead of tio which is about to get freed.
696 */
697 bio->bi_private = md->bs;
698
Stefan Bader9faf4002006-10-03 01:15:41 -0700699 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000700 bio_put(bio);
701 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100704/*
705 * Partial completion handling for request-based dm
706 */
707static void end_clone_bio(struct bio *clone, int error)
708{
709 struct dm_rq_clone_bio_info *info = clone->bi_private;
710 struct dm_rq_target_io *tio = info->tio;
711 struct bio *bio = info->orig;
712 unsigned int nr_bytes = info->orig->bi_size;
713
714 bio_put(clone);
715
716 if (tio->error)
717 /*
718 * An error has already been detected on the request.
719 * Once error occurred, just let clone->end_io() handle
720 * the remainder.
721 */
722 return;
723 else if (error) {
724 /*
725 * Don't notice the error to the upper layer yet.
726 * The error handling decision is made by the target driver,
727 * when the request is completed.
728 */
729 tio->error = error;
730 return;
731 }
732
733 /*
734 * I/O for the bio successfully completed.
735 * Notice the data completion to the upper layer.
736 */
737
738 /*
739 * bios are processed from the head of the list.
740 * So the completing bio should always be rq->bio.
741 * If it's not, something wrong is happening.
742 */
743 if (tio->orig->bio != bio)
744 DMERR("bio completion is going in the middle of the request");
745
746 /*
747 * Update the original request.
748 * Do not use blk_end_request() here, because it may complete
749 * the original request before the clone, and break the ordering.
750 */
751 blk_update_request(tio->orig, 0, nr_bytes);
752}
753
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000754static void store_barrier_error(struct mapped_device *md, int error)
755{
756 unsigned long flags;
757
758 spin_lock_irqsave(&md->barrier_error_lock, flags);
759 /*
760 * Basically, the first error is taken, but:
761 * -EOPNOTSUPP supersedes any I/O error.
762 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
763 */
764 if (!md->barrier_error || error == -EOPNOTSUPP ||
765 (md->barrier_error != -EOPNOTSUPP &&
766 error == DM_ENDIO_REQUEUE))
767 md->barrier_error = error;
768 spin_unlock_irqrestore(&md->barrier_error_lock, flags);
769}
770
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100771/*
772 * Don't touch any member of the md after calling this function because
773 * the md may be freed in dm_put() at the end of this function.
774 * Or do dm_get() before calling this function and dm_put() later.
775 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000776static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100777{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000778 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100779
780 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000781 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100782 wake_up(&md->wait);
783
784 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000785 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100786
787 /*
788 * dm_put() must be at the end of this function. See the comment above
789 */
790 dm_put(md);
791}
792
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100793static void free_rq_clone(struct request *clone)
794{
795 struct dm_rq_target_io *tio = clone->end_io_data;
796
797 blk_rq_unprep_clone(clone);
798 free_rq_tio(tio);
799}
800
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000801/*
802 * Complete the clone and the original request.
803 * Must be called without queue lock.
804 */
805static void dm_end_request(struct request *clone, int error)
806{
807 int rw = rq_data_dir(clone);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000808 int run_queue = 1;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200809 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000810 struct dm_rq_target_io *tio = clone->end_io_data;
811 struct mapped_device *md = tio->md;
812 struct request *rq = tio->orig;
813
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200814 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000815 rq->errors = clone->errors;
816 rq->resid_len = clone->resid_len;
817
818 if (rq->sense)
819 /*
820 * We are using the sense buffer of the original
821 * request.
822 * So setting the length of the sense data is enough.
823 */
824 rq->sense_len = clone->sense_len;
825 }
826
827 free_rq_clone(clone);
828
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000829 if (unlikely(is_barrier)) {
830 if (unlikely(error))
831 store_barrier_error(md, error);
832 run_queue = 0;
833 } else
834 blk_end_request_all(rq, error);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000835
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000836 rq_completed(md, rw, run_queue);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000837}
838
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100839static void dm_unprep_request(struct request *rq)
840{
841 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100842
843 rq->special = NULL;
844 rq->cmd_flags &= ~REQ_DONTPREP;
845
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100846 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100847}
848
849/*
850 * Requeue the original request of a clone.
851 */
852void dm_requeue_unmapped_request(struct request *clone)
853{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000854 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100855 struct dm_rq_target_io *tio = clone->end_io_data;
856 struct mapped_device *md = tio->md;
857 struct request *rq = tio->orig;
858 struct request_queue *q = rq->q;
859 unsigned long flags;
860
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200861 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000862 /*
863 * Barrier clones share an original request.
864 * Leave it to dm_end_request(), which handles this special
865 * case.
866 */
867 dm_end_request(clone, DM_ENDIO_REQUEUE);
868 return;
869 }
870
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100871 dm_unprep_request(rq);
872
873 spin_lock_irqsave(q->queue_lock, flags);
874 if (elv_queue_empty(q))
875 blk_plug_device(q);
876 blk_requeue_request(q, rq);
877 spin_unlock_irqrestore(q->queue_lock, flags);
878
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000879 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100880}
881EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
882
883static void __stop_queue(struct request_queue *q)
884{
885 blk_stop_queue(q);
886}
887
888static void stop_queue(struct request_queue *q)
889{
890 unsigned long flags;
891
892 spin_lock_irqsave(q->queue_lock, flags);
893 __stop_queue(q);
894 spin_unlock_irqrestore(q->queue_lock, flags);
895}
896
897static void __start_queue(struct request_queue *q)
898{
899 if (blk_queue_stopped(q))
900 blk_start_queue(q);
901}
902
903static void start_queue(struct request_queue *q)
904{
905 unsigned long flags;
906
907 spin_lock_irqsave(q->queue_lock, flags);
908 __start_queue(q);
909 spin_unlock_irqrestore(q->queue_lock, flags);
910}
911
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000912static void dm_done(struct request *clone, int error, bool mapped)
913{
914 int r = error;
915 struct dm_rq_target_io *tio = clone->end_io_data;
916 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
917
918 if (mapped && rq_end_io)
919 r = rq_end_io(tio->ti, clone, error, &tio->info);
920
921 if (r <= 0)
922 /* The target wants to complete the I/O */
923 dm_end_request(clone, r);
924 else if (r == DM_ENDIO_INCOMPLETE)
925 /* The target will handle the I/O */
926 return;
927 else if (r == DM_ENDIO_REQUEUE)
928 /* The target wants to requeue the I/O */
929 dm_requeue_unmapped_request(clone);
930 else {
931 DMWARN("unimplemented target endio return value: %d", r);
932 BUG();
933 }
934}
935
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100936/*
937 * Request completion handler for request-based dm
938 */
939static void dm_softirq_done(struct request *rq)
940{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000941 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100942 struct request *clone = rq->completion_data;
943 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100944
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000945 if (rq->cmd_flags & REQ_FAILED)
946 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100947
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000948 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100949}
950
951/*
952 * Complete the clone and the original request with the error status
953 * through softirq context.
954 */
955static void dm_complete_request(struct request *clone, int error)
956{
957 struct dm_rq_target_io *tio = clone->end_io_data;
958 struct request *rq = tio->orig;
959
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200960 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000961 /*
962 * Barrier clones share an original request. So can't use
963 * softirq_done with the original.
964 * Pass the clone to dm_done() directly in this special case.
965 * It is safe (even if clone->q->queue_lock is held here)
966 * because there is no I/O dispatching during the completion
967 * of barrier clone.
968 */
969 dm_done(clone, error, true);
970 return;
971 }
972
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100973 tio->error = error;
974 rq->completion_data = clone;
975 blk_complete_request(rq);
976}
977
978/*
979 * Complete the not-mapped clone and the original request with the error status
980 * through softirq context.
981 * Target's rq_end_io() function isn't called.
982 * This may be used when the target's map_rq() function fails.
983 */
984void dm_kill_unmapped_request(struct request *clone, int error)
985{
986 struct dm_rq_target_io *tio = clone->end_io_data;
987 struct request *rq = tio->orig;
988
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200989 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000990 /*
991 * Barrier clones share an original request.
992 * Leave it to dm_end_request(), which handles this special
993 * case.
994 */
995 BUG_ON(error > 0);
996 dm_end_request(clone, error);
997 return;
998 }
999
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001000 rq->cmd_flags |= REQ_FAILED;
1001 dm_complete_request(clone, error);
1002}
1003EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1004
1005/*
1006 * Called with the queue lock held
1007 */
1008static void end_clone_request(struct request *clone, int error)
1009{
1010 /*
1011 * For just cleaning up the information of the queue in which
1012 * the clone was dispatched.
1013 * The clone is *NOT* freed actually here because it is alloced from
1014 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1015 */
1016 __blk_put_request(clone->q, clone);
1017
1018 /*
1019 * Actual request completion is done in a softirq context which doesn't
1020 * hold the queue lock. Otherwise, deadlock could occur because:
1021 * - another request may be submitted by the upper level driver
1022 * of the stacking during the completion
1023 * - the submission which requires queue lock may be done
1024 * against this queue
1025 */
1026 dm_complete_request(clone, error);
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029static sector_t max_io_len(struct mapped_device *md,
1030 sector_t sector, struct dm_target *ti)
1031{
1032 sector_t offset = sector - ti->begin;
1033 sector_t len = ti->len - offset;
1034
1035 /*
1036 * Does the target need to split even further ?
1037 */
1038 if (ti->split_io) {
1039 sector_t boundary;
1040 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
1041 - offset;
1042 if (len > boundary)
1043 len = boundary;
1044 }
1045
1046 return len;
1047}
1048
1049static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001050 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001053 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001054 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 clone->bi_end_io = clone_endio;
1057 clone->bi_private = tio;
1058
1059 /*
1060 * Map the clone. If r == 0 we don't need to do
1061 * anything, the target has assumed ownership of
1062 * this io.
1063 */
1064 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001065 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001067 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001069
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001070 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001071 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001074 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1075 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001076 md = tio->io->md;
1077 dec_pending(tio->io, r);
1078 /*
1079 * Store bio_set for cleanup.
1080 */
1081 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001083 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001084 } else if (r) {
1085 DMWARN("unimplemented target map return value: %d", r);
1086 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088}
1089
1090struct clone_info {
1091 struct mapped_device *md;
1092 struct dm_table *map;
1093 struct bio *bio;
1094 struct dm_io *io;
1095 sector_t sector;
1096 sector_t sector_count;
1097 unsigned short idx;
1098};
1099
Peter Osterlund36763472005-09-06 15:16:42 -07001100static void dm_bio_destructor(struct bio *bio)
1101{
Stefan Bader9faf4002006-10-03 01:15:41 -07001102 struct bio_set *bs = bio->bi_private;
1103
1104 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
1108 * Creates a little bio that is just does part of a bvec.
1109 */
1110static struct bio *split_bvec(struct bio *bio, sector_t sector,
1111 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001112 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct bio *clone;
1115 struct bio_vec *bv = bio->bi_io_vec + idx;
1116
Stefan Bader9faf4002006-10-03 01:15:41 -07001117 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001118 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *clone->bi_io_vec = *bv;
1120
1121 clone->bi_sector = sector;
1122 clone->bi_bdev = bio->bi_bdev;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001123 clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 clone->bi_vcnt = 1;
1125 clone->bi_size = to_bytes(len);
1126 clone->bi_io_vec->bv_offset = offset;
1127 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001128 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Martin K. Petersen9c470082009-04-09 00:27:12 +01001130 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001131 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001132 bio_integrity_trim(clone,
1133 bio_sector_offset(bio, idx, offset), len);
1134 }
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return clone;
1137}
1138
1139/*
1140 * Creates a bio that consists of range of complete bvecs.
1141 */
1142static struct bio *clone_bio(struct bio *bio, sector_t sector,
1143 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001144 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 struct bio *clone;
1147
Stefan Bader9faf4002006-10-03 01:15:41 -07001148 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1149 __bio_clone(clone, bio);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001150 clone->bi_rw &= ~REQ_HARDBARRIER;
Stefan Bader9faf4002006-10-03 01:15:41 -07001151 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 clone->bi_sector = sector;
1153 clone->bi_idx = idx;
1154 clone->bi_vcnt = idx + bv_count;
1155 clone->bi_size = to_bytes(len);
1156 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1157
Martin K. Petersen9c470082009-04-09 00:27:12 +01001158 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001159 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001160
1161 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1162 bio_integrity_trim(clone,
1163 bio_sector_offset(bio, idx, 0), len);
1164 }
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return clone;
1167}
1168
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001169static struct dm_target_io *alloc_tio(struct clone_info *ci,
1170 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001171{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001172 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001173
1174 tio->io = ci->io;
1175 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001176 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001177
1178 return tio;
1179}
1180
1181static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1182 unsigned flush_nr)
1183{
1184 struct dm_target_io *tio = alloc_tio(ci, ti);
1185 struct bio *clone;
1186
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001187 tio->info.flush_request = flush_nr;
1188
1189 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1190 __bio_clone(clone, ci->bio);
1191 clone->bi_destructor = dm_bio_destructor;
1192
1193 __map_bio(ti, clone, tio);
1194}
1195
1196static int __clone_and_map_empty_barrier(struct clone_info *ci)
1197{
1198 unsigned target_nr = 0, flush_nr;
1199 struct dm_target *ti;
1200
1201 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1202 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1203 flush_nr++)
1204 __flush_target(ci, ti, flush_nr);
1205
1206 ci->sector_count = 0;
1207
1208 return 0;
1209}
1210
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001211static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001214 struct dm_target *ti;
1215 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001216 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001218 if (unlikely(bio_empty_barrier(bio)))
1219 return __clone_and_map_empty_barrier(ci);
1220
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001221 ti = dm_table_find_target(ci->map, ci->sector);
1222 if (!dm_target_is_valid(ti))
1223 return -EIO;
1224
1225 max = max_io_len(ci->md, ci->sector, ti);
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * Allocate a target io object.
1229 */
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001230 tio = alloc_tio(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (ci->sector_count <= max) {
1233 /*
1234 * Optimise for the simple case where we can do all of
1235 * the remaining io with a single clone.
1236 */
1237 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001238 bio->bi_vcnt - ci->idx, ci->sector_count,
1239 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 __map_bio(ti, clone, tio);
1241 ci->sector_count = 0;
1242
1243 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1244 /*
1245 * There are some bvecs that don't span targets.
1246 * Do as many of these as possible.
1247 */
1248 int i;
1249 sector_t remaining = max;
1250 sector_t bv_len;
1251
1252 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1253 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1254
1255 if (bv_len > remaining)
1256 break;
1257
1258 remaining -= bv_len;
1259 len += bv_len;
1260 }
1261
Stefan Bader9faf4002006-10-03 01:15:41 -07001262 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1263 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 __map_bio(ti, clone, tio);
1265
1266 ci->sector += len;
1267 ci->sector_count -= len;
1268 ci->idx = i;
1269
1270 } else {
1271 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001272 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
1274 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001275 sector_t remaining = to_sector(bv->bv_len);
1276 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001278 do {
1279 if (offset) {
1280 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001281 if (!dm_target_is_valid(ti))
1282 return -EIO;
1283
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001284 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001286 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001289 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001291 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001292 bv->bv_offset + offset, len,
1293 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001294
1295 __map_bio(ti, clone, tio);
1296
1297 ci->sector += len;
1298 ci->sector_count -= len;
1299 offset += to_bytes(len);
1300 } while (remaining -= len);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ci->idx++;
1303 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001304
1305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001309 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001311static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001314 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001316 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001317 if (unlikely(!ci.map)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001318 if (!(bio->bi_rw & REQ_HARDBARRIER))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001319 bio_io_error(bio);
1320 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001321 if (!md->barrier_error)
1322 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001323 return;
1324 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 ci.md = md;
1327 ci.bio = bio;
1328 ci.io = alloc_io(md);
1329 ci.io->error = 0;
1330 atomic_set(&ci.io->io_count, 1);
1331 ci.io->bio = bio;
1332 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001333 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 ci.sector = bio->bi_sector;
1335 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001336 if (unlikely(bio_empty_barrier(bio)))
1337 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 ci.idx = bio->bi_idx;
1339
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001340 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001341 while (ci.sector_count && !error)
1342 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001345 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 dm_table_put(ci.map);
1347}
1348/*-----------------------------------------------------------------
1349 * CRUD END
1350 *---------------------------------------------------------------*/
1351
Milan Brozf6fccb12008-07-21 12:00:37 +01001352static int dm_merge_bvec(struct request_queue *q,
1353 struct bvec_merge_data *bvm,
1354 struct bio_vec *biovec)
1355{
1356 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001357 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001358 struct dm_target *ti;
1359 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001360 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001361
1362 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001363 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001364
1365 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001366 if (!dm_target_is_valid(ti))
1367 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001368
1369 /*
1370 * Find maximum amount of I/O that won't need splitting
1371 */
1372 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1373 (sector_t) BIO_MAX_SECTORS);
1374 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1375 if (max_size < 0)
1376 max_size = 0;
1377
1378 /*
1379 * merge_bvec_fn() returns number of bytes
1380 * it can accept at this offset
1381 * max is precomputed maximal io size
1382 */
1383 if (max_size && ti->type->merge)
1384 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001385 /*
1386 * If the target doesn't support merge method and some of the devices
1387 * provided their merge_bvec method (we know this by looking at
1388 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1389 * entries. So always set max_size to 0, and the code below allows
1390 * just one page.
1391 */
1392 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1393
1394 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001395
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001396out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001397 dm_table_put(map);
1398
1399out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001400 /*
1401 * Always allow an entire first page
1402 */
1403 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1404 max_size = biovec->bv_len;
1405
Milan Brozf6fccb12008-07-21 12:00:37 +01001406 return max_size;
1407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/*
1410 * The request function that just remaps the bio built up by
1411 * dm_merge_bvec.
1412 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001413static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Kevin Corry12f03a42006-02-01 03:04:52 -08001415 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001417 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001419 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Tejun Heo074a7ac2008-08-25 19:56:14 +09001421 cpu = part_stat_lock();
1422 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1423 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1424 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001427 * If we're suspended or the thread is processing barriers
1428 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001430 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001431 unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001432 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001434 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1435 bio_rw(bio) == READA) {
1436 bio_io_error(bio);
1437 return 0;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Mikulas Patocka92c63902009-04-09 00:27:15 +01001440 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Mikulas Patocka92c63902009-04-09 00:27:15 +01001442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001445 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001446 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001450static int dm_make_request(struct request_queue *q, struct bio *bio)
1451{
1452 struct mapped_device *md = q->queuedata;
1453
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001454 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1455}
1456
1457static int dm_request_based(struct mapped_device *md)
1458{
1459 return blk_queue_stackable(md->queue);
1460}
1461
1462static int dm_request(struct request_queue *q, struct bio *bio)
1463{
1464 struct mapped_device *md = q->queuedata;
1465
1466 if (dm_request_based(md))
1467 return dm_make_request(q, bio);
1468
1469 return _dm_request(q, bio);
1470}
1471
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001472static bool dm_rq_is_flush_request(struct request *rq)
1473{
FUJITA Tomonori144d6ed2010-07-03 17:45:37 +09001474 if (rq->cmd_flags & REQ_FLUSH)
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001475 return true;
1476 else
1477 return false;
1478}
1479
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001480void dm_dispatch_request(struct request *rq)
1481{
1482 int r;
1483
1484 if (blk_queue_io_stat(rq->q))
1485 rq->cmd_flags |= REQ_IO_STAT;
1486
1487 rq->start_time = jiffies;
1488 r = blk_insert_cloned_request(rq->q, rq);
1489 if (r)
1490 dm_complete_request(rq, r);
1491}
1492EXPORT_SYMBOL_GPL(dm_dispatch_request);
1493
1494static void dm_rq_bio_destructor(struct bio *bio)
1495{
1496 struct dm_rq_clone_bio_info *info = bio->bi_private;
1497 struct mapped_device *md = info->tio->md;
1498
1499 free_bio_info(info);
1500 bio_free(bio, md->bs);
1501}
1502
1503static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1504 void *data)
1505{
1506 struct dm_rq_target_io *tio = data;
1507 struct mapped_device *md = tio->md;
1508 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1509
1510 if (!info)
1511 return -ENOMEM;
1512
1513 info->orig = bio_orig;
1514 info->tio = tio;
1515 bio->bi_end_io = end_clone_bio;
1516 bio->bi_private = info;
1517 bio->bi_destructor = dm_rq_bio_destructor;
1518
1519 return 0;
1520}
1521
1522static int setup_clone(struct request *clone, struct request *rq,
1523 struct dm_rq_target_io *tio)
1524{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001525 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001526
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001527 if (dm_rq_is_flush_request(rq)) {
1528 blk_rq_init(NULL, clone);
1529 clone->cmd_type = REQ_TYPE_FS;
1530 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1531 } else {
1532 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1533 dm_rq_bio_constructor, tio);
1534 if (r)
1535 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001536
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001537 clone->cmd = rq->cmd;
1538 clone->cmd_len = rq->cmd_len;
1539 clone->sense = rq->sense;
1540 clone->buffer = rq->buffer;
1541 }
1542
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001543 clone->end_io = end_clone_request;
1544 clone->end_io_data = tio;
1545
1546 return 0;
1547}
1548
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001549static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1550 gfp_t gfp_mask)
1551{
1552 struct request *clone;
1553 struct dm_rq_target_io *tio;
1554
1555 tio = alloc_rq_tio(md, gfp_mask);
1556 if (!tio)
1557 return NULL;
1558
1559 tio->md = md;
1560 tio->ti = NULL;
1561 tio->orig = rq;
1562 tio->error = 0;
1563 memset(&tio->info, 0, sizeof(tio->info));
1564
1565 clone = &tio->clone;
1566 if (setup_clone(clone, rq, tio)) {
1567 /* -ENOMEM */
1568 free_rq_tio(tio);
1569 return NULL;
1570 }
1571
1572 return clone;
1573}
1574
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001575/*
1576 * Called with the queue lock held.
1577 */
1578static int dm_prep_fn(struct request_queue *q, struct request *rq)
1579{
1580 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001581 struct request *clone;
1582
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001583 if (unlikely(dm_rq_is_flush_request(rq)))
1584 return BLKPREP_OK;
1585
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001586 if (unlikely(rq->special)) {
1587 DMWARN("Already has something in rq->special.");
1588 return BLKPREP_KILL;
1589 }
1590
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001591 clone = clone_rq(rq, md, GFP_ATOMIC);
1592 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001593 return BLKPREP_DEFER;
1594
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001595 rq->special = clone;
1596 rq->cmd_flags |= REQ_DONTPREP;
1597
1598 return BLKPREP_OK;
1599}
1600
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001601/*
1602 * Returns:
1603 * 0 : the request has been processed (not requeued)
1604 * !0 : the request has been requeued
1605 */
1606static int map_request(struct dm_target *ti, struct request *clone,
1607 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001608{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001609 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001610 struct dm_rq_target_io *tio = clone->end_io_data;
1611
1612 /*
1613 * Hold the md reference here for the in-flight I/O.
1614 * We can't rely on the reference count by device opener,
1615 * because the device may be closed during the request completion
1616 * when all bios are completed.
1617 * See the comment in rq_completed() too.
1618 */
1619 dm_get(md);
1620
1621 tio->ti = ti;
1622 r = ti->type->map_rq(ti, clone, &tio->info);
1623 switch (r) {
1624 case DM_MAPIO_SUBMITTED:
1625 /* The target has taken the I/O to submit by itself later */
1626 break;
1627 case DM_MAPIO_REMAPPED:
1628 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001629 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1630 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001631 dm_dispatch_request(clone);
1632 break;
1633 case DM_MAPIO_REQUEUE:
1634 /* The target wants to requeue the I/O */
1635 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001636 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001637 break;
1638 default:
1639 if (r > 0) {
1640 DMWARN("unimplemented target map return value: %d", r);
1641 BUG();
1642 }
1643
1644 /* The target wants to complete the I/O */
1645 dm_kill_unmapped_request(clone, r);
1646 break;
1647 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001648
1649 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001650}
1651
1652/*
1653 * q->request_fn for request-based dm.
1654 * Called with the queue lock held.
1655 */
1656static void dm_request_fn(struct request_queue *q)
1657{
1658 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001659 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001660 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001661 struct request *rq, *clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001662
1663 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001664 * For suspend, check blk_queue_stopped() and increment
1665 * ->pending within a single queue_lock not to increment the
1666 * number of in-flight I/Os after the queue is stopped in
1667 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001668 */
1669 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1670 rq = blk_peek_request(q);
1671 if (!rq)
1672 goto plug_and_out;
1673
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001674 if (unlikely(dm_rq_is_flush_request(rq))) {
1675 BUG_ON(md->flush_request);
1676 md->flush_request = rq;
1677 blk_start_request(rq);
1678 queue_work(md->wq, &md->barrier_work);
1679 goto out;
1680 }
1681
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001682 ti = dm_table_find_target(map, blk_rq_pos(rq));
1683 if (ti->type->busy && ti->type->busy(ti))
1684 goto plug_and_out;
1685
1686 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001687 clone = rq->special;
1688 atomic_inc(&md->pending[rq_data_dir(clone)]);
1689
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001690 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001691 if (map_request(ti, clone, md))
1692 goto requeued;
1693
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001694 spin_lock_irq(q->queue_lock);
1695 }
1696
1697 goto out;
1698
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001699requeued:
1700 spin_lock_irq(q->queue_lock);
1701
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001702plug_and_out:
1703 if (!elv_queue_empty(q))
1704 /* Some requests still remain, retry later */
1705 blk_plug_device(q);
1706
1707out:
1708 dm_table_put(map);
1709
1710 return;
1711}
1712
1713int dm_underlying_device_busy(struct request_queue *q)
1714{
1715 return blk_lld_busy(q);
1716}
1717EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1718
1719static int dm_lld_busy(struct request_queue *q)
1720{
1721 int r;
1722 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001723 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001724
1725 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1726 r = 1;
1727 else
1728 r = dm_table_any_busy_target(map);
1729
1730 dm_table_put(map);
1731
1732 return r;
1733}
1734
Jens Axboe165125e2007-07-24 09:28:11 +02001735static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001738 struct dm_table *map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001741 if (dm_request_based(md))
1742 generic_unplug_device(q);
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 dm_table_unplug_all(map);
1745 dm_table_put(map);
1746 }
1747}
1748
1749static int dm_any_congested(void *congested_data, int bdi_bits)
1750{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001751 int r = bdi_bits;
1752 struct mapped_device *md = congested_data;
1753 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001755 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001756 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001757 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001758 /*
1759 * Request-based dm cares about only own queue for
1760 * the query about congestion status of request_queue
1761 */
1762 if (dm_request_based(md))
1763 r = md->queue->backing_dev_info.state &
1764 bdi_bits;
1765 else
1766 r = dm_table_any_congested(map, bdi_bits);
1767
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001768 dm_table_put(map);
1769 }
1770 }
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return r;
1773}
1774
1775/*-----------------------------------------------------------------
1776 * An IDR is used to keep track of allocated minor numbers.
1777 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778static DEFINE_IDR(_minor_idr);
1779
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001780static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001782 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001784 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
1787/*
1788 * See if the device with a specific minor # is free.
1789 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001790static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 int r, m;
1793
1794 if (minor >= (1 << MINORBITS))
1795 return -EINVAL;
1796
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001797 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1798 if (!r)
1799 return -ENOMEM;
1800
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001801 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 if (idr_find(&_minor_idr, minor)) {
1804 r = -EBUSY;
1805 goto out;
1806 }
1807
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001808 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001809 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 if (m != minor) {
1813 idr_remove(&_minor_idr, m);
1814 r = -EBUSY;
1815 goto out;
1816 }
1817
1818out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001819 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return r;
1821}
1822
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001823static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001825 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001828 if (!r)
1829 return -ENOMEM;
1830
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001831 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001833 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001834 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 if (m >= (1 << MINORBITS)) {
1838 idr_remove(&_minor_idr, m);
1839 r = -ENOSPC;
1840 goto out;
1841 }
1842
1843 *minor = m;
1844
1845out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001846 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return r;
1848}
1849
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001850static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Mikulas Patocka53d59142009-04-02 19:55:37 +01001852static void dm_wq_work(struct work_struct *work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001853static void dm_rq_barrier_work(struct work_struct *work);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/*
1856 * Allocate and initialise a blank device with a given minor.
1857 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001858static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001861 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001862 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 if (!md) {
1865 DMWARN("unable to allocate device, out of memory.");
1866 return NULL;
1867 }
1868
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001869 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001870 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001873 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001874 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001875 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001876 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001878 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001880 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001881 mutex_init(&md->suspend_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001882 spin_lock_init(&md->deferred_lock);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001883 spin_lock_init(&md->barrier_error_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 rwlock_init(&md->map_lock);
1885 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001886 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001888 atomic_set(&md->uevent_seq, 0);
1889 INIT_LIST_HEAD(&md->uevent_list);
1890 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001892 md->queue = blk_init_queue(dm_request_fn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001894 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001896 /*
1897 * Request-based dm devices cannot be stacked on top of bio-based dm
1898 * devices. The type of this dm device has not been decided yet,
1899 * although we initialized the queue using blk_init_queue().
1900 * The type is decided at the first table loading time.
1901 * To prevent problematic device stacking, clear the queue flag
1902 * for request stacking support until then.
1903 *
1904 * This queue is new, so no concurrency on the queue_flags.
1905 */
1906 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1907 md->saved_make_request_fn = md->queue->make_request_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 md->queue->queuedata = md;
1909 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1910 md->queue->backing_dev_info.congested_data = md;
1911 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +01001912 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001914 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001915 blk_queue_softirq_done(md->queue, dm_softirq_done);
1916 blk_queue_prep_rq(md->queue, dm_prep_fn);
1917 blk_queue_lld_busy(md->queue, dm_lld_busy);
FUJITA Tomonori00fff262010-07-03 17:45:40 +09001918 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH);
Stefan Bader9faf4002006-10-03 01:15:41 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 md->disk = alloc_disk(1);
1921 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001922 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001924 atomic_set(&md->pending[0], 0);
1925 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001926 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001927 INIT_WORK(&md->work, dm_wq_work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001928 INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001929 init_waitqueue_head(&md->eventq);
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 md->disk->major = _major;
1932 md->disk->first_minor = minor;
1933 md->disk->fops = &dm_blk_dops;
1934 md->disk->queue = md->queue;
1935 md->disk->private_data = md;
1936 sprintf(md->disk->disk_name, "dm-%d", minor);
1937 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001938 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Milan Broz304f3f62008-02-08 02:11:17 +00001940 md->wq = create_singlethread_workqueue("kdmflush");
1941 if (!md->wq)
1942 goto bad_thread;
1943
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001944 md->bdev = bdget_disk(md->disk, 0);
1945 if (!md->bdev)
1946 goto bad_bdev;
1947
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001948 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001949 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001950 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001951 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001952
1953 BUG_ON(old_md != MINOR_ALLOCED);
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return md;
1956
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001957bad_bdev:
1958 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001959bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001960 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001961 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001962bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001963 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001964bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001966bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001967 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001968bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 kfree(md);
1970 return NULL;
1971}
1972
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001973static void unlock_fs(struct mapped_device *md);
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975static void free_dev(struct mapped_device *md)
1976{
Tejun Heof331c022008-09-03 09:01:48 +02001977 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001978
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001979 unlock_fs(md);
1980 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001981 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001982 if (md->tio_pool)
1983 mempool_destroy(md->tio_pool);
1984 if (md->io_pool)
1985 mempool_destroy(md->io_pool);
1986 if (md->bs)
1987 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001988 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001990 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001991
1992 spin_lock(&_minor_lock);
1993 md->disk->private_data = NULL;
1994 spin_unlock(&_minor_lock);
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001997 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001998 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 kfree(md);
2000}
2001
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002002static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2003{
2004 struct dm_md_mempools *p;
2005
2006 if (md->io_pool && md->tio_pool && md->bs)
2007 /* the md already has necessary mempools */
2008 goto out;
2009
2010 p = dm_table_get_md_mempools(t);
2011 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
2012
2013 md->io_pool = p->io_pool;
2014 p->io_pool = NULL;
2015 md->tio_pool = p->tio_pool;
2016 p->tio_pool = NULL;
2017 md->bs = p->bs;
2018 p->bs = NULL;
2019
2020out:
2021 /* mempool bind completed, now no need any mempools in the table */
2022 dm_table_free_md_mempools(t);
2023}
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025/*
2026 * Bind a table to the device.
2027 */
2028static void event_callback(void *context)
2029{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002030 unsigned long flags;
2031 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 struct mapped_device *md = (struct mapped_device *) context;
2033
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002034 spin_lock_irqsave(&md->uevent_lock, flags);
2035 list_splice_init(&md->uevent_list, &uevents);
2036 spin_unlock_irqrestore(&md->uevent_lock, flags);
2037
Tejun Heoed9e1982008-08-25 19:56:05 +09002038 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 atomic_inc(&md->event_nr);
2041 wake_up(&md->eventq);
2042}
2043
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002044static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002046 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002048 mutex_lock(&md->bdev->bd_inode->i_mutex);
2049 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2050 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002053/*
2054 * Returns old map, which caller must destroy.
2055 */
2056static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2057 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002059 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002060 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002062 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002065
2066 /*
2067 * Wipe any geometry if the size of the table changed.
2068 */
2069 if (size != get_capacity(md->disk))
2070 memset(&md->geometry, 0, sizeof(md->geometry));
2071
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002072 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002074 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002075
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002076 /*
2077 * The queue hasn't been stopped yet, if the old table type wasn't
2078 * for request-based during suspension. So stop it to prevent
2079 * I/O mapping before resume.
2080 * This must be done before setting the queue restrictions,
2081 * because request-based dm may be run just after the setting.
2082 */
2083 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2084 stop_queue(q);
2085
2086 __bind_mempools(md, t);
2087
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002088 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002089 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002090 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002091 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002092 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002093
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002094 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Alasdair G Kergona7940152009-12-10 23:52:23 +00002097/*
2098 * Returns unbound table for the caller to free.
2099 */
2100static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002103 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002106 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002109 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002111 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002112
2113 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116/*
2117 * Constructor for a new device.
2118 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002119int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct mapped_device *md;
2122
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002123 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (!md)
2125 return -ENXIO;
2126
Milan Broz784aae72009-01-06 03:05:12 +00002127 dm_sysfs_init(md);
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 *result = md;
2130 return 0;
2131}
2132
David Teigland637842c2006-01-06 00:20:00 -08002133static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 unsigned minor = MINOR(dev);
2137
2138 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2139 return NULL;
2140
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002141 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002144 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002145 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002146 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002147 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002148 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002149 goto out;
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002152out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002153 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
David Teigland637842c2006-01-06 00:20:00 -08002155 return md;
2156}
2157
David Teiglandd229a952006-01-06 00:20:01 -08002158struct mapped_device *dm_get_md(dev_t dev)
2159{
2160 struct mapped_device *md = dm_find_md(dev);
2161
2162 if (md)
2163 dm_get(md);
2164
2165 return md;
2166}
2167
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002168void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002169{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002170 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
2173void dm_set_mdptr(struct mapped_device *md, void *ptr)
2174{
2175 md->interface_ptr = ptr;
2176}
2177
2178void dm_get(struct mapped_device *md)
2179{
2180 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002181 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002184const char *dm_device_name(struct mapped_device *md)
2185{
2186 return md->name;
2187}
2188EXPORT_SYMBOL_GPL(dm_device_name);
2189
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002190static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002192 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002194 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002195
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002196 spin_lock(&_minor_lock);
2197 map = dm_get_live_table(md);
2198 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2199 set_bit(DMF_FREEING, &md->flags);
2200 spin_unlock(&_minor_lock);
2201
2202 if (!dm_suspended_md(md)) {
2203 dm_table_presuspend_targets(map);
2204 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002206
2207 /*
2208 * Rare, but there may be I/O requests still going to complete,
2209 * for example. Wait for all references to disappear.
2210 * No one should increment the reference count of the mapped_device,
2211 * after the mapped_device state becomes DMF_FREEING.
2212 */
2213 if (wait)
2214 while (atomic_read(&md->holders))
2215 msleep(1);
2216 else if (atomic_read(&md->holders))
2217 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2218 dm_device_name(md), atomic_read(&md->holders));
2219
2220 dm_sysfs_exit(md);
2221 dm_table_put(map);
2222 dm_table_destroy(__unbind(md));
2223 free_dev(md);
2224}
2225
2226void dm_destroy(struct mapped_device *md)
2227{
2228 __dm_destroy(md, true);
2229}
2230
2231void dm_destroy_immediate(struct mapped_device *md)
2232{
2233 __dm_destroy(md, false);
2234}
2235
2236void dm_put(struct mapped_device *md)
2237{
2238 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
Edward Goggin79eb8852007-05-09 02:32:56 -07002240EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Mikulas Patocka401600d2009-04-02 19:55:38 +01002242static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002243{
2244 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002245 DECLARE_WAITQUEUE(wait, current);
2246
2247 dm_unplug_all(md->queue);
2248
2249 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002250
2251 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002252 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002253
2254 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002255 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002256 break;
2257
Mikulas Patocka401600d2009-04-02 19:55:38 +01002258 if (interruptible == TASK_INTERRUPTIBLE &&
2259 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002260 r = -EINTR;
2261 break;
2262 }
2263
2264 io_schedule();
2265 }
2266 set_current_state(TASK_RUNNING);
2267
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002268 remove_wait_queue(&md->wait, &wait);
2269
Milan Broz46125c12008-02-08 02:10:30 +00002270 return r;
2271}
2272
Mikulas Patocka531fe962009-06-22 10:12:17 +01002273static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002274{
2275 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002276
2277 bio_init(&md->barrier_bio);
2278 md->barrier_bio.bi_bdev = md->bdev;
2279 md->barrier_bio.bi_rw = WRITE_BARRIER;
2280 __split_and_process_bio(md, &md->barrier_bio);
2281
2282 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002283}
2284
2285static void process_barrier(struct mapped_device *md, struct bio *bio)
2286{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002287 md->barrier_error = 0;
2288
Mikulas Patocka531fe962009-06-22 10:12:17 +01002289 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002290
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002291 if (!bio_empty_barrier(bio)) {
2292 __split_and_process_bio(md, bio);
Mikulas Patocka708e9292010-08-12 04:14:00 +01002293 /*
2294 * If the request isn't supported, don't waste time with
2295 * the second flush.
2296 */
2297 if (md->barrier_error != -EOPNOTSUPP)
2298 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002299 }
2300
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002301 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002302 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002303 else {
2304 spin_lock_irq(&md->deferred_lock);
2305 bio_list_add_head(&md->deferred, bio);
2306 spin_unlock_irq(&md->deferred_lock);
2307 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002308}
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310/*
2311 * Process the deferred bios
2312 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002313static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Mikulas Patockaef208582009-04-02 19:55:38 +01002315 struct mapped_device *md = container_of(work, struct mapped_device,
2316 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002317 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Mikulas Patockaef208582009-04-02 19:55:38 +01002319 down_write(&md->io_lock);
2320
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002321 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002322 spin_lock_irq(&md->deferred_lock);
2323 c = bio_list_pop(&md->deferred);
2324 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002325
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002326 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002327 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002328 break;
2329 }
2330
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002331 up_write(&md->io_lock);
2332
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002333 if (dm_request_based(md))
2334 generic_make_request(c);
2335 else {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002336 if (c->bi_rw & REQ_HARDBARRIER)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002337 process_barrier(md, c);
2338 else
2339 __split_and_process_bio(md, c);
2340 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002341
2342 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002343 }
Milan Broz73d410c2008-02-08 02:10:25 +00002344
Mikulas Patockaef208582009-04-02 19:55:38 +01002345 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002348static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002349{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002350 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2351 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002352 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002353}
2354
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002355static void dm_rq_set_flush_nr(struct request *clone, unsigned flush_nr)
2356{
2357 struct dm_rq_target_io *tio = clone->end_io_data;
2358
2359 tio->info.flush_request = flush_nr;
2360}
2361
2362/* Issue barrier requests to targets and wait for their completion. */
2363static int dm_rq_barrier(struct mapped_device *md)
2364{
2365 int i, j;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002366 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002367 unsigned num_targets = dm_table_get_num_targets(map);
2368 struct dm_target *ti;
2369 struct request *clone;
2370
2371 md->barrier_error = 0;
2372
2373 for (i = 0; i < num_targets; i++) {
2374 ti = dm_table_get_target(map, i);
2375 for (j = 0; j < ti->num_flush_requests; j++) {
2376 clone = clone_rq(md->flush_request, md, GFP_NOIO);
2377 dm_rq_set_flush_nr(clone, j);
2378 atomic_inc(&md->pending[rq_data_dir(clone)]);
2379 map_request(ti, clone, md);
2380 }
2381 }
2382
2383 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2384 dm_table_put(map);
2385
2386 return md->barrier_error;
2387}
2388
2389static void dm_rq_barrier_work(struct work_struct *work)
2390{
2391 int error;
2392 struct mapped_device *md = container_of(work, struct mapped_device,
2393 barrier_work);
2394 struct request_queue *q = md->queue;
2395 struct request *rq;
2396 unsigned long flags;
2397
2398 /*
2399 * Hold the md reference here and leave it at the last part so that
2400 * the md can't be deleted by device opener when the barrier request
2401 * completes.
2402 */
2403 dm_get(md);
2404
2405 error = dm_rq_barrier(md);
2406
2407 rq = md->flush_request;
2408 md->flush_request = NULL;
2409
2410 if (error == DM_ENDIO_REQUEUE) {
2411 spin_lock_irqsave(q->queue_lock, flags);
2412 blk_requeue_request(q, rq);
2413 spin_unlock_irqrestore(q->queue_lock, flags);
2414 } else
2415 blk_end_request_all(rq, error);
2416
2417 blk_run_queue(q);
2418
2419 dm_put(md);
2420}
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002423 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002425struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002427 struct dm_table *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002428 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002429 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Daniel Walkere61290a2008-02-08 02:10:08 +00002431 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002434 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002435 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002437 r = dm_calculate_queue_limits(table, &limits);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002438 if (r) {
2439 map = ERR_PTR(r);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002440 goto out;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002441 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002442
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002443 /* cannot change the device type, once a table is bound */
2444 if (md->map &&
2445 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2446 DMWARN("can't change the device type after a table is bound");
2447 goto out;
2448 }
2449
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002450 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002452out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002453 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002454 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
2457/*
2458 * Functions to lock and unlock any filesystem running on the
2459 * device.
2460 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002461static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002463 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002466
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002467 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002468 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002469 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002470 md->frozen_sb = NULL;
2471 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002472 }
2473
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002474 set_bit(DMF_FROZEN, &md->flags);
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return 0;
2477}
2478
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002479static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002481 if (!test_bit(DMF_FROZEN, &md->flags))
2482 return;
2483
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002484 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002486 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489/*
2490 * We need to be able to change a mapping table under a mounted
2491 * filesystem. For example we might want to move some data in
2492 * the background. Before the table can be swapped with
2493 * dm_bind_table, dm_suspend must be called to flush any in
2494 * flight bios and ensure that any further io gets deferred.
2495 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002496/*
2497 * Suspend mechanism in request-based dm.
2498 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002499 * 1. Flush all I/Os by lock_fs() if needed.
2500 * 2. Stop dispatching any I/O by stopping the request_queue.
2501 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002502 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002503 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002504 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002505int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002507 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002508 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002509 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002510 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Daniel Walkere61290a2008-02-08 02:10:08 +00002512 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002513
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002514 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002515 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002516 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002519 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002521 /*
2522 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2523 * This flag is cleared before dm_suspend returns.
2524 */
2525 if (noflush)
2526 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2527
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002528 /* This does not get reverted if there's an error later. */
2529 dm_table_presuspend_targets(map);
2530
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002531 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002532 * Flush I/O to the device.
2533 * Any I/O submitted after lock_fs() may not be flushed.
2534 * noflush takes precedence over do_lockfs.
2535 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002536 */
2537 if (!noflush && do_lockfs) {
2538 r = lock_fs(md);
2539 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002540 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002544 * Here we must make sure that no processes are submitting requests
2545 * to target drivers i.e. no one may be executing
2546 * __split_and_process_bio. This is called from dm_request and
2547 * dm_wq_work.
2548 *
2549 * To get all processes out of __split_and_process_bio in dm_request,
2550 * we take the write lock. To prevent any process from reentering
2551 * __split_and_process_bio from dm_request, we set
2552 * DMF_QUEUE_IO_TO_THREAD.
2553 *
2554 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2555 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2556 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2557 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002559 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002560 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2561 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002562 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002564 /*
2565 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2566 * can be kicked until md->queue is stopped. So stop md->queue before
2567 * flushing md->wq.
2568 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002569 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002570 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002571
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002572 flush_workqueue(md->wq);
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002575 * At this point no more requests are entering target request routines.
2576 * We call dm_wait_for_completion to wait for all existing requests
2577 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002579 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002581 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002582 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002583 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002584 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002587 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002588 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002589
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002590 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002591 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002592
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002593 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002594 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002595 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002596
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002597 /*
2598 * If dm_wait_for_completion returned 0, the device is completely
2599 * quiescent now. There is no request-processing activity. All new
2600 * requests are being added to md->deferred list.
2601 */
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 set_bit(DMF_SUSPENDED, &md->flags);
2604
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002605 dm_table_postsuspend_targets(map);
2606
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002607out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002609
2610out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002611 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002612 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
2615int dm_resume(struct mapped_device *md)
2616{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002617 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002618 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Daniel Walkere61290a2008-02-08 02:10:08 +00002620 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002621 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002622 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002623
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002624 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002625 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Milan Broz8757b772006-10-03 01:15:36 -07002628 r = dm_table_resume_targets(map);
2629 if (r)
2630 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002631
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002632 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002633
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002634 /*
2635 * Flushing deferred I/Os must be done after targets are resumed
2636 * so that mapping of targets can work correctly.
2637 * Request-based dm is queueing the deferred I/Os in its request_queue.
2638 */
2639 if (dm_request_based(md))
2640 start_queue(md->queue);
2641
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002642 unlock_fs(md);
2643
2644 clear_bit(DMF_SUSPENDED, &md->flags);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002647 r = 0;
2648out:
2649 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002650 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002651
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002652 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
2654
2655/*-----------------------------------------------------------------
2656 * Event notification.
2657 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002658int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002659 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002660{
Milan Broz60935eb2009-06-22 10:12:30 +01002661 char udev_cookie[DM_COOKIE_LENGTH];
2662 char *envp[] = { udev_cookie, NULL };
2663
2664 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002665 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002666 else {
2667 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2668 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002669 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2670 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002671 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002672}
2673
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002674uint32_t dm_next_uevent_seq(struct mapped_device *md)
2675{
2676 return atomic_add_return(1, &md->uevent_seq);
2677}
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679uint32_t dm_get_event_nr(struct mapped_device *md)
2680{
2681 return atomic_read(&md->event_nr);
2682}
2683
2684int dm_wait_event(struct mapped_device *md, int event_nr)
2685{
2686 return wait_event_interruptible(md->eventq,
2687 (event_nr != atomic_read(&md->event_nr)));
2688}
2689
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002690void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2691{
2692 unsigned long flags;
2693
2694 spin_lock_irqsave(&md->uevent_lock, flags);
2695 list_add(elist, &md->uevent_list);
2696 spin_unlock_irqrestore(&md->uevent_lock, flags);
2697}
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699/*
2700 * The gendisk is only valid as long as you have a reference
2701 * count on 'md'.
2702 */
2703struct gendisk *dm_disk(struct mapped_device *md)
2704{
2705 return md->disk;
2706}
2707
Milan Broz784aae72009-01-06 03:05:12 +00002708struct kobject *dm_kobject(struct mapped_device *md)
2709{
2710 return &md->kobj;
2711}
2712
2713/*
2714 * struct mapped_device should not be exported outside of dm.c
2715 * so use this check to verify that kobj is part of md structure
2716 */
2717struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2718{
2719 struct mapped_device *md;
2720
2721 md = container_of(kobj, struct mapped_device, kobj);
2722 if (&md->kobj != kobj)
2723 return NULL;
2724
Milan Broz4d89b7b2009-06-22 10:12:11 +01002725 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002726 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002727 return NULL;
2728
Milan Broz784aae72009-01-06 03:05:12 +00002729 dm_get(md);
2730 return md;
2731}
2732
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002733int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
2735 return test_bit(DMF_SUSPENDED, &md->flags);
2736}
2737
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002738int dm_suspended(struct dm_target *ti)
2739{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002740 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002741}
2742EXPORT_SYMBOL_GPL(dm_suspended);
2743
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002744int dm_noflush_suspending(struct dm_target *ti)
2745{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002746 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002747}
2748EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2749
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002750struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2751{
2752 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2753
2754 if (!pools)
2755 return NULL;
2756
2757 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2758 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2759 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2760 if (!pools->io_pool)
2761 goto free_pools_and_out;
2762
2763 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2764 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2765 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2766 if (!pools->tio_pool)
2767 goto free_io_pool_and_out;
2768
2769 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2770 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2771 if (!pools->bs)
2772 goto free_tio_pool_and_out;
2773
2774 return pools;
2775
2776free_tio_pool_and_out:
2777 mempool_destroy(pools->tio_pool);
2778
2779free_io_pool_and_out:
2780 mempool_destroy(pools->io_pool);
2781
2782free_pools_and_out:
2783 kfree(pools);
2784
2785 return NULL;
2786}
2787
2788void dm_free_md_mempools(struct dm_md_mempools *pools)
2789{
2790 if (!pools)
2791 return;
2792
2793 if (pools->io_pool)
2794 mempool_destroy(pools->io_pool);
2795
2796 if (pools->tio_pool)
2797 mempool_destroy(pools->tio_pool);
2798
2799 if (pools->bs)
2800 bioset_free(pools->bs);
2801
2802 kfree(pools);
2803}
2804
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002805static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 .open = dm_blk_open,
2807 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002808 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002809 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 .owner = THIS_MODULE
2811};
2812
2813EXPORT_SYMBOL(dm_get_mapinfo);
2814
2815/*
2816 * module hooks
2817 */
2818module_init(dm_init);
2819module_exit(dm_exit);
2820
2821module_param(major, uint, 0);
2822MODULE_PARM_DESC(major, "The major number of the device mapper");
2823MODULE_DESCRIPTION(DM_NAME " driver");
2824MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2825MODULE_LICENSE("GPL");