blob: aeb0fa1ccfe4ede8357441a04cc5c5f67825f5e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
17#include <linux/buffer_head.h>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080021#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010022#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080023
24#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "core"
27
Milan Broz60935eb2009-06-22 10:12:30 +010028/*
29 * Cookies are numeric values sent with CHANGE and REMOVE
30 * uevents while resuming, removing or renaming the device.
31 */
32#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
33#define DM_COOKIE_LENGTH 24
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static const char *_name = DM_NAME;
36
37static unsigned int major = 0;
38static unsigned int _major = 0;
39
Alasdair G Kergond15b7742011-08-02 12:32:01 +010040static DEFINE_IDR(_minor_idr);
41
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070042static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000044 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * One of these is allocated per bio.
46 */
47struct dm_io {
48 struct mapped_device *md;
49 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010051 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080052 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010053 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000057 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * One of these is allocated per target within a bio. Hopefully
59 * this will be simplified out one day.
60 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010061struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct dm_io *io;
63 struct dm_target *ti;
64 union map_info info;
65};
66
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000067/*
68 * For request-based dm.
69 * One of these is allocated per request.
70 */
71struct dm_rq_target_io {
72 struct mapped_device *md;
73 struct dm_target *ti;
74 struct request *orig, clone;
75 int error;
76 union map_info info;
77};
78
79/*
80 * For request-based dm.
81 * One of these is allocated per bio.
82 */
83struct dm_rq_clone_bio_info {
84 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010085 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000086};
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088union map_info *dm_get_mapinfo(struct bio *bio)
89{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070090 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010091 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070092 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010095union map_info *dm_get_rq_mapinfo(struct request *rq)
96{
97 if (rq && rq->end_io_data)
98 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
99 return NULL;
100}
101EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
102
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700103#define MINOR_ALLOCED ((void *)-1)
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * Bits for the md->flags field.
107 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100108#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800110#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700111#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700112#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800113#define DMF_NOFLUSH_SUSPENDING 5
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;
Mike Snitzera5664da2010-08-12 04:14:01 +0100128 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100129 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100130 struct mutex type_lock;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800133 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 void *interface_ptr;
136
137 /*
138 * A list of ios that arrived while we were suspended.
139 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200140 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100142 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800143 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100144 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200147 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000148 */
149 struct workqueue_struct *wq;
150
151 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * The current mapping.
153 */
154 struct dm_table *map;
155
156 /*
157 * io objects are allocated from here.
158 */
159 mempool_t *io_pool;
160 mempool_t *tio_pool;
161
Stefan Bader9faf4002006-10-03 01:15:41 -0700162 struct bio_set *bs;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * Event handling.
166 */
167 atomic_t event_nr;
168 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100169 atomic_t uevent_seq;
170 struct list_head uevent_list;
171 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /*
174 * freeze/thaw support require holding onto a super block
175 */
176 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100177 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800178
179 /* forced geometry settings */
180 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000181
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100182 /* For saving the address of __make_request for request based dm */
183 make_request_fn *saved_make_request_fn;
184
Milan Broz784aae72009-01-06 03:05:12 +0000185 /* sysfs handle */
186 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100187
Tejun Heod87f4c12010-09-03 11:56:19 +0200188 /* zero-length flush that will be cloned and submitted to targets */
189 struct bio flush_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};
191
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100192/*
193 * For mempools pre-allocation at the table loading time.
194 */
195struct dm_md_mempools {
196 mempool_t *io_pool;
197 mempool_t *tio_pool;
198 struct bio_set *bs;
199};
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800202static struct kmem_cache *_io_cache;
203static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000204static struct kmem_cache *_rq_tio_cache;
205static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static int __init local_init(void)
208{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100209 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100212 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100214 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100217 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100218 if (!_tio_cache)
219 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000221 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
222 if (!_rq_tio_cache)
223 goto out_free_tio_cache;
224
225 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
226 if (!_rq_bio_info_cache)
227 goto out_free_rq_tio_cache;
228
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100229 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100230 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000231 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 _major = major;
234 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100235 if (r < 0)
236 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (!_major)
239 _major = r;
240
241 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100242
243out_uevent_exit:
244 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000245out_free_rq_bio_info_cache:
246 kmem_cache_destroy(_rq_bio_info_cache);
247out_free_rq_tio_cache:
248 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100249out_free_tio_cache:
250 kmem_cache_destroy(_tio_cache);
251out_free_io_cache:
252 kmem_cache_destroy(_io_cache);
253
254 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257static void local_exit(void)
258{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000259 kmem_cache_destroy(_rq_bio_info_cache);
260 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 kmem_cache_destroy(_tio_cache);
262 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700263 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100264 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 _major = 0;
267
268 DMINFO("cleaned up");
269}
270
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000271static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 local_init,
273 dm_target_init,
274 dm_linear_init,
275 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000276 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100277 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dm_interface_init,
279};
280
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000281static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 local_exit,
283 dm_target_exit,
284 dm_linear_exit,
285 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000286 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100287 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 dm_interface_exit,
289};
290
291static int __init dm_init(void)
292{
293 const int count = ARRAY_SIZE(_inits);
294
295 int r, i;
296
297 for (i = 0; i < count; i++) {
298 r = _inits[i]();
299 if (r)
300 goto bad;
301 }
302
303 return 0;
304
305 bad:
306 while (i--)
307 _exits[i]();
308
309 return r;
310}
311
312static void __exit dm_exit(void)
313{
314 int i = ARRAY_SIZE(_exits);
315
316 while (i--)
317 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100318
319 /*
320 * Should be empty by this point.
321 */
322 idr_remove_all(&_minor_idr);
323 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/*
327 * Block device functions
328 */
Mike Anderson432a2122009-12-10 23:52:20 +0000329int dm_deleting_md(struct mapped_device *md)
330{
331 return test_bit(DMF_DELETING, &md->flags);
332}
333
Al Virofe5f9f22008-03-02 10:29:31 -0500334static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct mapped_device *md;
337
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700338 spin_lock(&_minor_lock);
339
Al Virofe5f9f22008-03-02 10:29:31 -0500340 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700341 if (!md)
342 goto out;
343
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700344 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000345 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700346 md = NULL;
347 goto out;
348 }
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700351 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700352
353out:
354 spin_unlock(&_minor_lock);
355
356 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Al Virofe5f9f22008-03-02 10:29:31 -0500359static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Al Virofe5f9f22008-03-02 10:29:31 -0500361 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200362
Milan Broz4a1aeb92011-01-13 19:59:48 +0000363 spin_lock(&_minor_lock);
364
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700365 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000367
368 spin_unlock(&_minor_lock);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700373int dm_open_count(struct mapped_device *md)
374{
375 return atomic_read(&md->open_count);
376}
377
378/*
379 * Guarantees nothing is using the device before it's deleted.
380 */
381int dm_lock_for_deletion(struct mapped_device *md)
382{
383 int r = 0;
384
385 spin_lock(&_minor_lock);
386
387 if (dm_open_count(md))
388 r = -EBUSY;
389 else
390 set_bit(DMF_DELETING, &md->flags);
391
392 spin_unlock(&_minor_lock);
393
394 return r;
395}
396
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800397static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
398{
399 struct mapped_device *md = bdev->bd_disk->private_data;
400
401 return dm_get_geometry(md, geo);
402}
403
Al Virofe5f9f22008-03-02 10:29:31 -0500404static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700405 unsigned int cmd, unsigned long arg)
406{
Al Virofe5f9f22008-03-02 10:29:31 -0500407 struct mapped_device *md = bdev->bd_disk->private_data;
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000408 struct dm_table *map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700409 struct dm_target *tgt;
410 int r = -ENOTTY;
411
Milan Brozaa129a22006-10-03 01:15:15 -0700412 if (!map || !dm_table_get_size(map))
413 goto out;
414
415 /* We only support devices that have a single target */
416 if (dm_table_get_num_targets(map) != 1)
417 goto out;
418
419 tgt = dm_table_get_target(map, 0);
420
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000421 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700422 r = -EAGAIN;
423 goto out;
424 }
425
426 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400427 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700428
429out:
430 dm_table_put(map);
431
Milan Brozaa129a22006-10-03 01:15:15 -0700432 return r;
433}
434
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100435static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 return mempool_alloc(md->io_pool, GFP_NOIO);
438}
439
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100440static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 mempool_free(io, md->io_pool);
443}
444
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100445static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 mempool_free(tio, md->tio_pool);
448}
449
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000450static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
451 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100452{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000453 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100454}
455
456static void free_rq_tio(struct dm_rq_target_io *tio)
457{
458 mempool_free(tio, tio->md->tio_pool);
459}
460
461static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
462{
463 return mempool_alloc(md->io_pool, GFP_ATOMIC);
464}
465
466static void free_bio_info(struct dm_rq_clone_bio_info *info)
467{
468 mempool_free(info, info->tio->md->io_pool);
469}
470
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000471static int md_in_flight(struct mapped_device *md)
472{
473 return atomic_read(&md->pending[READ]) +
474 atomic_read(&md->pending[WRITE]);
475}
476
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800477static void start_io_acct(struct dm_io *io)
478{
479 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900480 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200481 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800482
483 io->start_time = jiffies;
484
Tejun Heo074a7ac2008-08-25 19:56:14 +0900485 cpu = part_stat_lock();
486 part_round_stats(cpu, &dm_disk(md)->part0);
487 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100488 atomic_set(&dm_disk(md)->part0.in_flight[rw],
489 atomic_inc_return(&md->pending[rw]));
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800490}
491
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000492static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800493{
494 struct mapped_device *md = io->md;
495 struct bio *bio = io->bio;
496 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900497 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800498 int rw = bio_data_dir(bio);
499
Tejun Heo074a7ac2008-08-25 19:56:14 +0900500 cpu = part_stat_lock();
501 part_round_stats(cpu, &dm_disk(md)->part0);
502 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
503 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800504
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100505 /*
506 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200507 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100508 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100509 pending = atomic_dec_return(&md->pending[rw]);
510 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200511 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800512
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000513 /* nudge anyone waiting on suspend queue */
514 if (!pending)
515 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/*
519 * Add the bio to the list of deferred io.
520 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100521static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200523 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200525 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200527 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200528 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
532 * Everyone (including functions in this file), should use this
533 * function to access the md->map field, and make sure they call
534 * dm_table_put() when finished.
535 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000536struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100539 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100541 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 t = md->map;
543 if (t)
544 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100545 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 return t;
548}
549
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800550/*
551 * Get the geometry associated with a dm device
552 */
553int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
554{
555 *geo = md->geometry;
556
557 return 0;
558}
559
560/*
561 * Set the geometry of a device.
562 */
563int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
564{
565 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
566
567 if (geo->start > sz) {
568 DMWARN("Start sector is beyond the geometry limits.");
569 return -EINVAL;
570 }
571
572 md->geometry = *geo;
573
574 return 0;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*-----------------------------------------------------------------
578 * CRUD START:
579 * A more elegant soln is in the works that uses the queue
580 * merge fn, unfortunately there are a couple of changes to
581 * the block layer that I want to make for this. So in the
582 * interests of getting something for people to use I give
583 * you this clearly demarcated crap.
584 *---------------------------------------------------------------*/
585
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800586static int __noflush_suspending(struct mapped_device *md)
587{
588 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*
592 * Decrements the number of outstanding ios that a bio has been
593 * cloned into, completing the original io if necc.
594 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800595static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800597 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000598 int io_error;
599 struct bio *bio;
600 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800601
602 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100603 if (unlikely(error)) {
604 spin_lock_irqsave(&io->endio_lock, flags);
605 if (!(io->error > 0 && __noflush_suspending(md)))
606 io->error = error;
607 spin_unlock_irqrestore(&io->endio_lock, flags);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800611 if (io->error == DM_ENDIO_REQUEUE) {
612 /*
613 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800614 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100615 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200616 if (__noflush_suspending(md))
617 bio_list_add_head(&md->deferred, io->bio);
618 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800619 /* noflush suspend was interrupted. */
620 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100621 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800622 }
623
Milan Brozb35f8ca2009-03-16 17:44:36 +0000624 io_error = io->error;
625 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200626 end_io_acct(io);
627 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100628
Tejun Heo6a8736d2010-09-08 18:07:00 +0200629 if (io_error == DM_ENDIO_REQUEUE)
630 return;
631
Mike Snitzerb372d362010-09-08 18:07:01 +0200632 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100633 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200634 * Preflush done for flush with data, reissue
635 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100636 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200637 bio->bi_rw &= ~REQ_FLUSH;
638 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100639 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200640 /* done with normal IO or empty flush */
Jeff Moyerb7908c12011-01-06 20:41:42 +0100641 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200642 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645}
646
NeilBrown6712ecf2007-09-27 12:47:43 +0200647static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100650 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000651 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700652 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 dm_endio_fn endio = tio->ti->type->end_io;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
656 error = -EIO;
657
658 if (endio) {
659 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800660 if (r < 0 || r == DM_ENDIO_REQUEUE)
661 /*
662 * error and requeue request are handled
663 * in dec_pending().
664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800666 else if (r == DM_ENDIO_INCOMPLETE)
667 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200668 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800669 else if (r) {
670 DMWARN("unimplemented target endio return value: %d", r);
671 BUG();
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
Stefan Bader9faf4002006-10-03 01:15:41 -0700675 /*
676 * Store md for cleanup instead of tio which is about to get freed.
677 */
678 bio->bi_private = md->bs;
679
Stefan Bader9faf4002006-10-03 01:15:41 -0700680 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000681 bio_put(bio);
682 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100685/*
686 * Partial completion handling for request-based dm
687 */
688static void end_clone_bio(struct bio *clone, int error)
689{
690 struct dm_rq_clone_bio_info *info = clone->bi_private;
691 struct dm_rq_target_io *tio = info->tio;
692 struct bio *bio = info->orig;
693 unsigned int nr_bytes = info->orig->bi_size;
694
695 bio_put(clone);
696
697 if (tio->error)
698 /*
699 * An error has already been detected on the request.
700 * Once error occurred, just let clone->end_io() handle
701 * the remainder.
702 */
703 return;
704 else if (error) {
705 /*
706 * Don't notice the error to the upper layer yet.
707 * The error handling decision is made by the target driver,
708 * when the request is completed.
709 */
710 tio->error = error;
711 return;
712 }
713
714 /*
715 * I/O for the bio successfully completed.
716 * Notice the data completion to the upper layer.
717 */
718
719 /*
720 * bios are processed from the head of the list.
721 * So the completing bio should always be rq->bio.
722 * If it's not, something wrong is happening.
723 */
724 if (tio->orig->bio != bio)
725 DMERR("bio completion is going in the middle of the request");
726
727 /*
728 * Update the original request.
729 * Do not use blk_end_request() here, because it may complete
730 * the original request before the clone, and break the ordering.
731 */
732 blk_update_request(tio->orig, 0, nr_bytes);
733}
734
735/*
736 * Don't touch any member of the md after calling this function because
737 * the md may be freed in dm_put() at the end of this function.
738 * Or do dm_get() before calling this function and dm_put() later.
739 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000740static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100741{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000742 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100743
744 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000745 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100746 wake_up(&md->wait);
747
748 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000749 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100750
751 /*
752 * dm_put() must be at the end of this function. See the comment above
753 */
754 dm_put(md);
755}
756
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100757static void free_rq_clone(struct request *clone)
758{
759 struct dm_rq_target_io *tio = clone->end_io_data;
760
761 blk_rq_unprep_clone(clone);
762 free_rq_tio(tio);
763}
764
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000765/*
766 * Complete the clone and the original request.
767 * Must be called without queue lock.
768 */
769static void dm_end_request(struct request *clone, int error)
770{
771 int rw = rq_data_dir(clone);
772 struct dm_rq_target_io *tio = clone->end_io_data;
773 struct mapped_device *md = tio->md;
774 struct request *rq = tio->orig;
775
Tejun Heo29e40132010-09-08 18:07:00 +0200776 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000777 rq->errors = clone->errors;
778 rq->resid_len = clone->resid_len;
779
780 if (rq->sense)
781 /*
782 * We are using the sense buffer of the original
783 * request.
784 * So setting the length of the sense data is enough.
785 */
786 rq->sense_len = clone->sense_len;
787 }
788
789 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200790 blk_end_request_all(rq, error);
791 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000792}
793
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100794static void dm_unprep_request(struct request *rq)
795{
796 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100797
798 rq->special = NULL;
799 rq->cmd_flags &= ~REQ_DONTPREP;
800
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100801 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100802}
803
804/*
805 * Requeue the original request of a clone.
806 */
807void dm_requeue_unmapped_request(struct request *clone)
808{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000809 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100810 struct dm_rq_target_io *tio = clone->end_io_data;
811 struct mapped_device *md = tio->md;
812 struct request *rq = tio->orig;
813 struct request_queue *q = rq->q;
814 unsigned long flags;
815
816 dm_unprep_request(rq);
817
818 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100819 blk_requeue_request(q, rq);
820 spin_unlock_irqrestore(q->queue_lock, flags);
821
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000822 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100823}
824EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
825
826static void __stop_queue(struct request_queue *q)
827{
828 blk_stop_queue(q);
829}
830
831static void stop_queue(struct request_queue *q)
832{
833 unsigned long flags;
834
835 spin_lock_irqsave(q->queue_lock, flags);
836 __stop_queue(q);
837 spin_unlock_irqrestore(q->queue_lock, flags);
838}
839
840static void __start_queue(struct request_queue *q)
841{
842 if (blk_queue_stopped(q))
843 blk_start_queue(q);
844}
845
846static void start_queue(struct request_queue *q)
847{
848 unsigned long flags;
849
850 spin_lock_irqsave(q->queue_lock, flags);
851 __start_queue(q);
852 spin_unlock_irqrestore(q->queue_lock, flags);
853}
854
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000855static void dm_done(struct request *clone, int error, bool mapped)
856{
857 int r = error;
858 struct dm_rq_target_io *tio = clone->end_io_data;
859 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
860
861 if (mapped && rq_end_io)
862 r = rq_end_io(tio->ti, clone, error, &tio->info);
863
864 if (r <= 0)
865 /* The target wants to complete the I/O */
866 dm_end_request(clone, r);
867 else if (r == DM_ENDIO_INCOMPLETE)
868 /* The target will handle the I/O */
869 return;
870 else if (r == DM_ENDIO_REQUEUE)
871 /* The target wants to requeue the I/O */
872 dm_requeue_unmapped_request(clone);
873 else {
874 DMWARN("unimplemented target endio return value: %d", r);
875 BUG();
876 }
877}
878
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100879/*
880 * Request completion handler for request-based dm
881 */
882static void dm_softirq_done(struct request *rq)
883{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000884 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100885 struct request *clone = rq->completion_data;
886 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100887
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000888 if (rq->cmd_flags & REQ_FAILED)
889 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100890
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000891 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100892}
893
894/*
895 * Complete the clone and the original request with the error status
896 * through softirq context.
897 */
898static void dm_complete_request(struct request *clone, int error)
899{
900 struct dm_rq_target_io *tio = clone->end_io_data;
901 struct request *rq = tio->orig;
902
903 tio->error = error;
904 rq->completion_data = clone;
905 blk_complete_request(rq);
906}
907
908/*
909 * Complete the not-mapped clone and the original request with the error status
910 * through softirq context.
911 * Target's rq_end_io() function isn't called.
912 * This may be used when the target's map_rq() function fails.
913 */
914void dm_kill_unmapped_request(struct request *clone, int error)
915{
916 struct dm_rq_target_io *tio = clone->end_io_data;
917 struct request *rq = tio->orig;
918
919 rq->cmd_flags |= REQ_FAILED;
920 dm_complete_request(clone, error);
921}
922EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
923
924/*
925 * Called with the queue lock held
926 */
927static void end_clone_request(struct request *clone, int error)
928{
929 /*
930 * For just cleaning up the information of the queue in which
931 * the clone was dispatched.
932 * The clone is *NOT* freed actually here because it is alloced from
933 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
934 */
935 __blk_put_request(clone->q, clone);
936
937 /*
938 * Actual request completion is done in a softirq context which doesn't
939 * hold the queue lock. Otherwise, deadlock could occur because:
940 * - another request may be submitted by the upper level driver
941 * of the stacking during the completion
942 * - the submission which requires queue lock may be done
943 * against this queue
944 */
945 dm_complete_request(clone, error);
946}
947
Mike Snitzer56a67df2010-08-12 04:14:10 +0100948/*
949 * Return maximum size of I/O possible at the supplied sector up to the current
950 * target boundary.
951 */
952static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100954 sector_t target_offset = dm_target_offset(ti, sector);
955
956 return ti->len - target_offset;
957}
958
959static sector_t max_io_len(sector_t sector, struct dm_target *ti)
960{
961 sector_t len = max_io_len_target_boundary(sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /*
964 * Does the target need to split even further ?
965 */
966 if (ti->split_io) {
967 sector_t boundary;
Mike Snitzer56a67df2010-08-12 04:14:10 +0100968 sector_t offset = dm_target_offset(ti, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
970 - offset;
971 if (len > boundary)
972 len = boundary;
973 }
974
975 return len;
976}
977
978static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100979 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100982 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700983 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 clone->bi_end_io = clone_endio;
986 clone->bi_private = tio;
987
988 /*
989 * Map the clone. If r == 0 we don't need to do
990 * anything, the target has assumed ownership of
991 * this io.
992 */
993 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100994 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800996 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100998
Mike Snitzerd07335e2010-11-16 12:52:38 +0100999 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1000 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001003 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1004 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001005 md = tio->io->md;
1006 dec_pending(tio->io, r);
1007 /*
1008 * Store bio_set for cleanup.
1009 */
1010 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001012 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001013 } else if (r) {
1014 DMWARN("unimplemented target map return value: %d", r);
1015 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017}
1018
1019struct clone_info {
1020 struct mapped_device *md;
1021 struct dm_table *map;
1022 struct bio *bio;
1023 struct dm_io *io;
1024 sector_t sector;
1025 sector_t sector_count;
1026 unsigned short idx;
1027};
1028
Peter Osterlund36763472005-09-06 15:16:42 -07001029static void dm_bio_destructor(struct bio *bio)
1030{
Stefan Bader9faf4002006-10-03 01:15:41 -07001031 struct bio_set *bs = bio->bi_private;
1032
1033 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001037 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
1039static struct bio *split_bvec(struct bio *bio, sector_t sector,
1040 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001041 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct bio *clone;
1044 struct bio_vec *bv = bio->bi_io_vec + idx;
1045
Stefan Bader9faf4002006-10-03 01:15:41 -07001046 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001047 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 *clone->bi_io_vec = *bv;
1049
1050 clone->bi_sector = sector;
1051 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001052 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 clone->bi_vcnt = 1;
1054 clone->bi_size = to_bytes(len);
1055 clone->bi_io_vec->bv_offset = offset;
1056 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001057 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Martin K. Petersen9c470082009-04-09 00:27:12 +01001059 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001060 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001061 bio_integrity_trim(clone,
1062 bio_sector_offset(bio, idx, offset), len);
1063 }
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return clone;
1066}
1067
1068/*
1069 * Creates a bio that consists of range of complete bvecs.
1070 */
1071static struct bio *clone_bio(struct bio *bio, sector_t sector,
1072 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001073 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct bio *clone;
1076
Stefan Bader9faf4002006-10-03 01:15:41 -07001077 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1078 __bio_clone(clone, bio);
1079 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 clone->bi_sector = sector;
1081 clone->bi_idx = idx;
1082 clone->bi_vcnt = idx + bv_count;
1083 clone->bi_size = to_bytes(len);
1084 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1085
Martin K. Petersen9c470082009-04-09 00:27:12 +01001086 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001087 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001088
1089 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1090 bio_integrity_trim(clone,
1091 bio_sector_offset(bio, idx, 0), len);
1092 }
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return clone;
1095}
1096
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001097static struct dm_target_io *alloc_tio(struct clone_info *ci,
1098 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001099{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001100 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001101
1102 tio->io = ci->io;
1103 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001104 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001105
1106 return tio;
1107}
1108
Mike Snitzer06a426c2010-08-12 04:14:09 +01001109static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001110 unsigned request_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001111{
1112 struct dm_target_io *tio = alloc_tio(ci, ti);
1113 struct bio *clone;
1114
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001115 tio->info.target_request_nr = request_nr;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001116
Mike Snitzer06a426c2010-08-12 04:14:09 +01001117 /*
1118 * Discard requests require the bio's inline iovecs be initialized.
1119 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1120 * and discard, so no need for concern about wasted bvec allocations.
1121 */
1122 clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001123 __bio_clone(clone, ci->bio);
1124 clone->bi_destructor = dm_bio_destructor;
Mike Snitzera79245b2010-08-12 04:14:24 +01001125 if (len) {
1126 clone->bi_sector = ci->sector;
1127 clone->bi_size = to_bytes(len);
1128 }
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001129
1130 __map_bio(ti, clone, tio);
1131}
1132
Mike Snitzer06a426c2010-08-12 04:14:09 +01001133static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001134 unsigned num_requests, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001135{
1136 unsigned request_nr;
1137
1138 for (request_nr = 0; request_nr < num_requests; request_nr++)
Mike Snitzera79245b2010-08-12 04:14:24 +01001139 __issue_target_request(ci, ti, request_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001140}
1141
Mike Snitzerb372d362010-09-08 18:07:01 +02001142static int __clone_and_map_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001143{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001144 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001145 struct dm_target *ti;
1146
Mike Snitzerb372d362010-09-08 18:07:01 +02001147 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001148 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mike Snitzera79245b2010-08-12 04:14:24 +01001149 __issue_target_requests(ci, ti, ti->num_flush_requests, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001150
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001151 return 0;
1152}
1153
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001154/*
1155 * Perform all io with a single clone.
1156 */
1157static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
1158{
1159 struct bio *clone, *bio = ci->bio;
1160 struct dm_target_io *tio;
1161
1162 tio = alloc_tio(ci, ti);
1163 clone = clone_bio(bio, ci->sector, ci->idx,
1164 bio->bi_vcnt - ci->idx, ci->sector_count,
1165 ci->md->bs);
1166 __map_bio(ti, clone, tio);
1167 ci->sector_count = 0;
1168}
1169
1170static int __clone_and_map_discard(struct clone_info *ci)
1171{
1172 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001173 sector_t len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001174
Mike Snitzera79245b2010-08-12 04:14:24 +01001175 do {
1176 ti = dm_table_find_target(ci->map, ci->sector);
1177 if (!dm_target_is_valid(ti))
1178 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001179
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001180 /*
Mike Snitzera79245b2010-08-12 04:14:24 +01001181 * Even though the device advertised discard support,
Mike Snitzer936688d2011-08-02 12:32:01 +01001182 * that does not mean every target supports it, and
1183 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001184 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001185 */
Mike Snitzera79245b2010-08-12 04:14:24 +01001186 if (!ti->num_discard_requests)
1187 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001188
Mike Snitzera79245b2010-08-12 04:14:24 +01001189 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001190
Mike Snitzera79245b2010-08-12 04:14:24 +01001191 __issue_target_requests(ci, ti, ti->num_discard_requests, len);
1192
1193 ci->sector += len;
1194 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001195
1196 return 0;
1197}
1198
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001199static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001202 struct dm_target *ti;
1203 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001204 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001206 if (unlikely(bio->bi_rw & REQ_DISCARD))
1207 return __clone_and_map_discard(ci);
1208
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001209 ti = dm_table_find_target(ci->map, ci->sector);
1210 if (!dm_target_is_valid(ti))
1211 return -EIO;
1212
Mike Snitzer56a67df2010-08-12 04:14:10 +01001213 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (ci->sector_count <= max) {
1216 /*
1217 * Optimise for the simple case where we can do all of
1218 * the remaining io with a single clone.
1219 */
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001220 __clone_and_map_simple(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1223 /*
1224 * There are some bvecs that don't span targets.
1225 * Do as many of these as possible.
1226 */
1227 int i;
1228 sector_t remaining = max;
1229 sector_t bv_len;
1230
1231 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1232 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1233
1234 if (bv_len > remaining)
1235 break;
1236
1237 remaining -= bv_len;
1238 len += bv_len;
1239 }
1240
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001241 tio = alloc_tio(ci, ti);
Stefan Bader9faf4002006-10-03 01:15:41 -07001242 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1243 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 __map_bio(ti, clone, tio);
1245
1246 ci->sector += len;
1247 ci->sector_count -= len;
1248 ci->idx = i;
1249
1250 } else {
1251 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001252 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
1254 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001255 sector_t remaining = to_sector(bv->bv_len);
1256 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001258 do {
1259 if (offset) {
1260 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001261 if (!dm_target_is_valid(ti))
1262 return -EIO;
1263
Mike Snitzer56a67df2010-08-12 04:14:10 +01001264 max = max_io_len(ci->sector, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001267 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001269 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001270 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001271 bv->bv_offset + offset, len,
1272 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001273
1274 __map_bio(ti, clone, tio);
1275
1276 ci->sector += len;
1277 ci->sector_count -= len;
1278 offset += to_bytes(len);
1279 } while (remaining -= len);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 ci->idx++;
1282 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001283
1284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001288 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001290static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001293 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001295 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001296 if (unlikely(!ci.map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001297 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001298 return;
1299 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ci.io = alloc_io(md);
1303 ci.io->error = 0;
1304 atomic_set(&ci.io->io_count, 1);
1305 ci.io->bio = bio;
1306 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001307 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ci.sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 ci.idx = bio->bi_idx;
1310
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001311 start_io_acct(ci.io);
Mike Snitzerb372d362010-09-08 18:07:01 +02001312 if (bio->bi_rw & REQ_FLUSH) {
1313 ci.bio = &ci.md->flush_bio;
1314 ci.sector_count = 0;
1315 error = __clone_and_map_empty_flush(&ci);
1316 /* dec_pending submits any data associated with flush */
1317 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001318 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001319 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001320 while (ci.sector_count && !error)
Tejun Heod87f4c12010-09-03 11:56:19 +02001321 error = __clone_and_map(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001325 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 dm_table_put(ci.map);
1327}
1328/*-----------------------------------------------------------------
1329 * CRUD END
1330 *---------------------------------------------------------------*/
1331
Milan Brozf6fccb12008-07-21 12:00:37 +01001332static int dm_merge_bvec(struct request_queue *q,
1333 struct bvec_merge_data *bvm,
1334 struct bio_vec *biovec)
1335{
1336 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001337 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001338 struct dm_target *ti;
1339 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001340 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001341
1342 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001343 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001344
1345 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001346 if (!dm_target_is_valid(ti))
1347 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001348
1349 /*
1350 * Find maximum amount of I/O that won't need splitting
1351 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001352 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001353 (sector_t) BIO_MAX_SECTORS);
1354 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1355 if (max_size < 0)
1356 max_size = 0;
1357
1358 /*
1359 * merge_bvec_fn() returns number of bytes
1360 * it can accept at this offset
1361 * max is precomputed maximal io size
1362 */
1363 if (max_size && ti->type->merge)
1364 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001365 /*
1366 * If the target doesn't support merge method and some of the devices
1367 * provided their merge_bvec method (we know this by looking at
1368 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1369 * entries. So always set max_size to 0, and the code below allows
1370 * just one page.
1371 */
1372 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1373
1374 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001375
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001376out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001377 dm_table_put(map);
1378
1379out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001380 /*
1381 * Always allow an entire first page
1382 */
1383 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1384 max_size = biovec->bv_len;
1385
Milan Brozf6fccb12008-07-21 12:00:37 +01001386 return max_size;
1387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/*
1390 * The request function that just remaps the bio built up by
1391 * dm_merge_bvec.
1392 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001393static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Kevin Corry12f03a42006-02-01 03:04:52 -08001395 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001397 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001399 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Tejun Heo074a7ac2008-08-25 19:56:14 +09001401 cpu = part_stat_lock();
1402 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1403 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1404 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001405
Tejun Heo6a8736d2010-09-08 18:07:00 +02001406 /* if we're suspended, we have to queue this io for later */
1407 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001408 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Tejun Heo6a8736d2010-09-08 18:07:00 +02001410 if (bio_rw(bio) != READA)
1411 queue_io(md, bio);
1412 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001413 bio_io_error(bio);
Mikulas Patocka92c63902009-04-09 00:27:15 +01001414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001417 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001418 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001422static int dm_make_request(struct request_queue *q, struct bio *bio)
1423{
1424 struct mapped_device *md = q->queuedata;
1425
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001426 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1427}
1428
1429static int dm_request_based(struct mapped_device *md)
1430{
1431 return blk_queue_stackable(md->queue);
1432}
1433
1434static int dm_request(struct request_queue *q, struct bio *bio)
1435{
1436 struct mapped_device *md = q->queuedata;
1437
1438 if (dm_request_based(md))
1439 return dm_make_request(q, bio);
1440
1441 return _dm_request(q, bio);
1442}
1443
1444void dm_dispatch_request(struct request *rq)
1445{
1446 int r;
1447
1448 if (blk_queue_io_stat(rq->q))
1449 rq->cmd_flags |= REQ_IO_STAT;
1450
1451 rq->start_time = jiffies;
1452 r = blk_insert_cloned_request(rq->q, rq);
1453 if (r)
1454 dm_complete_request(rq, r);
1455}
1456EXPORT_SYMBOL_GPL(dm_dispatch_request);
1457
1458static void dm_rq_bio_destructor(struct bio *bio)
1459{
1460 struct dm_rq_clone_bio_info *info = bio->bi_private;
1461 struct mapped_device *md = info->tio->md;
1462
1463 free_bio_info(info);
1464 bio_free(bio, md->bs);
1465}
1466
1467static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1468 void *data)
1469{
1470 struct dm_rq_target_io *tio = data;
1471 struct mapped_device *md = tio->md;
1472 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1473
1474 if (!info)
1475 return -ENOMEM;
1476
1477 info->orig = bio_orig;
1478 info->tio = tio;
1479 bio->bi_end_io = end_clone_bio;
1480 bio->bi_private = info;
1481 bio->bi_destructor = dm_rq_bio_destructor;
1482
1483 return 0;
1484}
1485
1486static int setup_clone(struct request *clone, struct request *rq,
1487 struct dm_rq_target_io *tio)
1488{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001489 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001490
Tejun Heo29e40132010-09-08 18:07:00 +02001491 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1492 dm_rq_bio_constructor, tio);
1493 if (r)
1494 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001495
Tejun Heo29e40132010-09-08 18:07:00 +02001496 clone->cmd = rq->cmd;
1497 clone->cmd_len = rq->cmd_len;
1498 clone->sense = rq->sense;
1499 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001500 clone->end_io = end_clone_request;
1501 clone->end_io_data = tio;
1502
1503 return 0;
1504}
1505
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001506static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1507 gfp_t gfp_mask)
1508{
1509 struct request *clone;
1510 struct dm_rq_target_io *tio;
1511
1512 tio = alloc_rq_tio(md, gfp_mask);
1513 if (!tio)
1514 return NULL;
1515
1516 tio->md = md;
1517 tio->ti = NULL;
1518 tio->orig = rq;
1519 tio->error = 0;
1520 memset(&tio->info, 0, sizeof(tio->info));
1521
1522 clone = &tio->clone;
1523 if (setup_clone(clone, rq, tio)) {
1524 /* -ENOMEM */
1525 free_rq_tio(tio);
1526 return NULL;
1527 }
1528
1529 return clone;
1530}
1531
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001532/*
1533 * Called with the queue lock held.
1534 */
1535static int dm_prep_fn(struct request_queue *q, struct request *rq)
1536{
1537 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001538 struct request *clone;
1539
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001540 if (unlikely(rq->special)) {
1541 DMWARN("Already has something in rq->special.");
1542 return BLKPREP_KILL;
1543 }
1544
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001545 clone = clone_rq(rq, md, GFP_ATOMIC);
1546 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001547 return BLKPREP_DEFER;
1548
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001549 rq->special = clone;
1550 rq->cmd_flags |= REQ_DONTPREP;
1551
1552 return BLKPREP_OK;
1553}
1554
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001555/*
1556 * Returns:
1557 * 0 : the request has been processed (not requeued)
1558 * !0 : the request has been requeued
1559 */
1560static int map_request(struct dm_target *ti, struct request *clone,
1561 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001562{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001563 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001564 struct dm_rq_target_io *tio = clone->end_io_data;
1565
1566 /*
1567 * Hold the md reference here for the in-flight I/O.
1568 * We can't rely on the reference count by device opener,
1569 * because the device may be closed during the request completion
1570 * when all bios are completed.
1571 * See the comment in rq_completed() too.
1572 */
1573 dm_get(md);
1574
1575 tio->ti = ti;
1576 r = ti->type->map_rq(ti, clone, &tio->info);
1577 switch (r) {
1578 case DM_MAPIO_SUBMITTED:
1579 /* The target has taken the I/O to submit by itself later */
1580 break;
1581 case DM_MAPIO_REMAPPED:
1582 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001583 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1584 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001585 dm_dispatch_request(clone);
1586 break;
1587 case DM_MAPIO_REQUEUE:
1588 /* The target wants to requeue the I/O */
1589 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001590 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001591 break;
1592 default:
1593 if (r > 0) {
1594 DMWARN("unimplemented target map return value: %d", r);
1595 BUG();
1596 }
1597
1598 /* The target wants to complete the I/O */
1599 dm_kill_unmapped_request(clone, r);
1600 break;
1601 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001602
1603 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001604}
1605
1606/*
1607 * q->request_fn for request-based dm.
1608 * Called with the queue lock held.
1609 */
1610static void dm_request_fn(struct request_queue *q)
1611{
1612 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001613 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001614 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001615 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001616 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001617
1618 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001619 * For suspend, check blk_queue_stopped() and increment
1620 * ->pending within a single queue_lock not to increment the
1621 * number of in-flight I/Os after the queue is stopped in
1622 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001623 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001624 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001625 rq = blk_peek_request(q);
1626 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001627 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001628
Tejun Heo29e40132010-09-08 18:07:00 +02001629 /* always use block 0 to find the target for flushes for now */
1630 pos = 0;
1631 if (!(rq->cmd_flags & REQ_FLUSH))
1632 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001633
Tejun Heo29e40132010-09-08 18:07:00 +02001634 ti = dm_table_find_target(map, pos);
1635 BUG_ON(!dm_target_is_valid(ti));
1636
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001637 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001638 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001639
1640 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001641 clone = rq->special;
1642 atomic_inc(&md->pending[rq_data_dir(clone)]);
1643
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001644 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001645 if (map_request(ti, clone, md))
1646 goto requeued;
1647
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001648 BUG_ON(!irqs_disabled());
1649 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001650 }
1651
1652 goto out;
1653
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001654requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001655 BUG_ON(!irqs_disabled());
1656 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001657
Jens Axboe7eaceac2011-03-10 08:52:07 +01001658delay_and_out:
1659 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001660out:
1661 dm_table_put(map);
1662
1663 return;
1664}
1665
1666int dm_underlying_device_busy(struct request_queue *q)
1667{
1668 return blk_lld_busy(q);
1669}
1670EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1671
1672static int dm_lld_busy(struct request_queue *q)
1673{
1674 int r;
1675 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001676 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001677
1678 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1679 r = 1;
1680 else
1681 r = dm_table_any_busy_target(map);
1682
1683 dm_table_put(map);
1684
1685 return r;
1686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688static int dm_any_congested(void *congested_data, int bdi_bits)
1689{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001690 int r = bdi_bits;
1691 struct mapped_device *md = congested_data;
1692 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001694 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001695 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001696 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001697 /*
1698 * Request-based dm cares about only own queue for
1699 * the query about congestion status of request_queue
1700 */
1701 if (dm_request_based(md))
1702 r = md->queue->backing_dev_info.state &
1703 bdi_bits;
1704 else
1705 r = dm_table_any_congested(map, bdi_bits);
1706
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001707 dm_table_put(map);
1708 }
1709 }
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return r;
1712}
1713
1714/*-----------------------------------------------------------------
1715 * An IDR is used to keep track of allocated minor numbers.
1716 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001717static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001719 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001721 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724/*
1725 * See if the device with a specific minor # is free.
1726 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001727static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 int r, m;
1730
1731 if (minor >= (1 << MINORBITS))
1732 return -EINVAL;
1733
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001734 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1735 if (!r)
1736 return -ENOMEM;
1737
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001738 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 if (idr_find(&_minor_idr, minor)) {
1741 r = -EBUSY;
1742 goto out;
1743 }
1744
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001745 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001746 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 if (m != minor) {
1750 idr_remove(&_minor_idr, m);
1751 r = -EBUSY;
1752 goto out;
1753 }
1754
1755out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001756 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return r;
1758}
1759
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001760static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001762 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001765 if (!r)
1766 return -ENOMEM;
1767
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001768 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001770 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001771 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 if (m >= (1 << MINORBITS)) {
1775 idr_remove(&_minor_idr, m);
1776 r = -ENOSPC;
1777 goto out;
1778 }
1779
1780 *minor = m;
1781
1782out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001783 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return r;
1785}
1786
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001787static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Mikulas Patocka53d59142009-04-02 19:55:37 +01001789static void dm_wq_work(struct work_struct *work);
1790
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001791static void dm_init_md_queue(struct mapped_device *md)
1792{
1793 /*
1794 * Request-based dm devices cannot be stacked on top of bio-based dm
1795 * devices. The type of this dm device has not been decided yet.
1796 * The type is decided at the first table loading time.
1797 * To prevent problematic device stacking, clear the queue flag
1798 * for request stacking support until then.
1799 *
1800 * This queue is new, so no concurrency on the queue_flags.
1801 */
1802 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1803
1804 md->queue->queuedata = md;
1805 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1806 md->queue->backing_dev_info.congested_data = md;
1807 blk_queue_make_request(md->queue, dm_request);
1808 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001809 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Tejun Heod87f4c12010-09-03 11:56:19 +02001810 blk_queue_flush(md->queue, REQ_FLUSH | REQ_FUA);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001811}
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/*
1814 * Allocate and initialise a blank device with a given minor.
1815 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001816static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001819 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001820 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 if (!md) {
1823 DMWARN("unable to allocate device, out of memory.");
1824 return NULL;
1825 }
1826
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001827 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001828 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001831 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001832 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001833 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001834 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001836 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Mike Snitzera5664da2010-08-12 04:14:01 +01001838 md->type = DM_TYPE_NONE;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001839 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001840 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001841 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001842 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 rwlock_init(&md->map_lock);
1844 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001845 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001847 atomic_set(&md->uevent_seq, 0);
1848 INIT_LIST_HEAD(&md->uevent_list);
1849 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001851 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001853 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001855 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 md->disk = alloc_disk(1);
1858 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001859 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001861 atomic_set(&md->pending[0], 0);
1862 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001863 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001864 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001865 init_waitqueue_head(&md->eventq);
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 md->disk->major = _major;
1868 md->disk->first_minor = minor;
1869 md->disk->fops = &dm_blk_dops;
1870 md->disk->queue = md->queue;
1871 md->disk->private_data = md;
1872 sprintf(md->disk->disk_name, "dm-%d", minor);
1873 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001874 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Tejun Heo9c4376d2011-01-13 19:59:58 +00001876 md->wq = alloc_workqueue("kdmflush",
1877 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001878 if (!md->wq)
1879 goto bad_thread;
1880
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001881 md->bdev = bdget_disk(md->disk, 0);
1882 if (!md->bdev)
1883 goto bad_bdev;
1884
Tejun Heo6a8736d2010-09-08 18:07:00 +02001885 bio_init(&md->flush_bio);
1886 md->flush_bio.bi_bdev = md->bdev;
1887 md->flush_bio.bi_rw = WRITE_FLUSH;
1888
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001889 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001890 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001891 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001892 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001893
1894 BUG_ON(old_md != MINOR_ALLOCED);
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return md;
1897
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001898bad_bdev:
1899 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001900bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001901 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001902 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001903bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001904 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001905bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001907bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001908 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001909bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 kfree(md);
1911 return NULL;
1912}
1913
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001914static void unlock_fs(struct mapped_device *md);
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static void free_dev(struct mapped_device *md)
1917{
Tejun Heof331c022008-09-03 09:01:48 +02001918 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001919
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001920 unlock_fs(md);
1921 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001922 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001923 if (md->tio_pool)
1924 mempool_destroy(md->tio_pool);
1925 if (md->io_pool)
1926 mempool_destroy(md->io_pool);
1927 if (md->bs)
1928 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001929 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001931 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001932
1933 spin_lock(&_minor_lock);
1934 md->disk->private_data = NULL;
1935 spin_unlock(&_minor_lock);
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001938 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001939 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 kfree(md);
1941}
1942
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001943static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1944{
1945 struct dm_md_mempools *p;
1946
1947 if (md->io_pool && md->tio_pool && md->bs)
1948 /* the md already has necessary mempools */
1949 goto out;
1950
1951 p = dm_table_get_md_mempools(t);
1952 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1953
1954 md->io_pool = p->io_pool;
1955 p->io_pool = NULL;
1956 md->tio_pool = p->tio_pool;
1957 p->tio_pool = NULL;
1958 md->bs = p->bs;
1959 p->bs = NULL;
1960
1961out:
1962 /* mempool bind completed, now no need any mempools in the table */
1963 dm_table_free_md_mempools(t);
1964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * Bind a table to the device.
1968 */
1969static void event_callback(void *context)
1970{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001971 unsigned long flags;
1972 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 struct mapped_device *md = (struct mapped_device *) context;
1974
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001975 spin_lock_irqsave(&md->uevent_lock, flags);
1976 list_splice_init(&md->uevent_list, &uevents);
1977 spin_unlock_irqrestore(&md->uevent_lock, flags);
1978
Tejun Heoed9e1982008-08-25 19:56:05 +09001979 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 atomic_inc(&md->event_nr);
1982 wake_up(&md->eventq);
1983}
1984
Mike Snitzerc2176492011-01-13 19:53:46 +00001985/*
1986 * Protected by md->suspend_lock obtained by dm_swap_table().
1987 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001988static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001990 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001992 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001995/*
1996 * Returns old map, which caller must destroy.
1997 */
1998static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1999 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002001 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002002 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002004 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002007
2008 /*
2009 * Wipe any geometry if the size of the table changed.
2010 */
2011 if (size != get_capacity(md->disk))
2012 memset(&md->geometry, 0, sizeof(md->geometry));
2013
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002014 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002016 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002017
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002018 /*
2019 * The queue hasn't been stopped yet, if the old table type wasn't
2020 * for request-based during suspension. So stop it to prevent
2021 * I/O mapping before resume.
2022 * This must be done before setting the queue restrictions,
2023 * because request-based dm may be run just after the setting.
2024 */
2025 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2026 stop_queue(q);
2027
2028 __bind_mempools(md, t);
2029
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002030 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002031 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002032 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002033 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002034 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002035
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002036 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Alasdair G Kergona7940152009-12-10 23:52:23 +00002039/*
2040 * Returns unbound table for the caller to free.
2041 */
2042static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002045 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002048 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002051 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002053 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002054
2055 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
2058/*
2059 * Constructor for a new device.
2060 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002061int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 struct mapped_device *md;
2064
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002065 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (!md)
2067 return -ENXIO;
2068
Milan Broz784aae72009-01-06 03:05:12 +00002069 dm_sysfs_init(md);
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 *result = md;
2072 return 0;
2073}
2074
Mike Snitzera5664da2010-08-12 04:14:01 +01002075/*
2076 * Functions to manage md->type.
2077 * All are required to hold md->type_lock.
2078 */
2079void dm_lock_md_type(struct mapped_device *md)
2080{
2081 mutex_lock(&md->type_lock);
2082}
2083
2084void dm_unlock_md_type(struct mapped_device *md)
2085{
2086 mutex_unlock(&md->type_lock);
2087}
2088
2089void dm_set_md_type(struct mapped_device *md, unsigned type)
2090{
2091 md->type = type;
2092}
2093
2094unsigned dm_get_md_type(struct mapped_device *md)
2095{
2096 return md->type;
2097}
2098
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002099/*
2100 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2101 */
2102static int dm_init_request_based_queue(struct mapped_device *md)
2103{
2104 struct request_queue *q = NULL;
2105
2106 if (md->queue->elevator)
2107 return 1;
2108
2109 /* Fully initialize the queue */
2110 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2111 if (!q)
2112 return 0;
2113
2114 md->queue = q;
2115 md->saved_make_request_fn = md->queue->make_request_fn;
2116 dm_init_md_queue(md);
2117 blk_queue_softirq_done(md->queue, dm_softirq_done);
2118 blk_queue_prep_rq(md->queue, dm_prep_fn);
2119 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002120
2121 elv_register_queue(md->queue);
2122
2123 return 1;
2124}
2125
2126/*
2127 * Setup the DM device's queue based on md's type
2128 */
2129int dm_setup_md_queue(struct mapped_device *md)
2130{
2131 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2132 !dm_init_request_based_queue(md)) {
2133 DMWARN("Cannot initialize queue for request-based mapped device");
2134 return -EINVAL;
2135 }
2136
2137 return 0;
2138}
2139
David Teigland637842c2006-01-06 00:20:00 -08002140static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 unsigned minor = MINOR(dev);
2144
2145 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2146 return NULL;
2147
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002148 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002151 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002152 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002153 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002154 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002155 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002156 goto out;
2157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002159out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002160 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
David Teigland637842c2006-01-06 00:20:00 -08002162 return md;
2163}
2164
David Teiglandd229a952006-01-06 00:20:01 -08002165struct mapped_device *dm_get_md(dev_t dev)
2166{
2167 struct mapped_device *md = dm_find_md(dev);
2168
2169 if (md)
2170 dm_get(md);
2171
2172 return md;
2173}
2174
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002175void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002176{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002177 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180void dm_set_mdptr(struct mapped_device *md, void *ptr)
2181{
2182 md->interface_ptr = ptr;
2183}
2184
2185void dm_get(struct mapped_device *md)
2186{
2187 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002188 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002191const char *dm_device_name(struct mapped_device *md)
2192{
2193 return md->name;
2194}
2195EXPORT_SYMBOL_GPL(dm_device_name);
2196
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002197static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002199 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002201 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002202
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002203 spin_lock(&_minor_lock);
2204 map = dm_get_live_table(md);
2205 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2206 set_bit(DMF_FREEING, &md->flags);
2207 spin_unlock(&_minor_lock);
2208
2209 if (!dm_suspended_md(md)) {
2210 dm_table_presuspend_targets(map);
2211 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002213
2214 /*
2215 * Rare, but there may be I/O requests still going to complete,
2216 * for example. Wait for all references to disappear.
2217 * No one should increment the reference count of the mapped_device,
2218 * after the mapped_device state becomes DMF_FREEING.
2219 */
2220 if (wait)
2221 while (atomic_read(&md->holders))
2222 msleep(1);
2223 else if (atomic_read(&md->holders))
2224 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2225 dm_device_name(md), atomic_read(&md->holders));
2226
2227 dm_sysfs_exit(md);
2228 dm_table_put(map);
2229 dm_table_destroy(__unbind(md));
2230 free_dev(md);
2231}
2232
2233void dm_destroy(struct mapped_device *md)
2234{
2235 __dm_destroy(md, true);
2236}
2237
2238void dm_destroy_immediate(struct mapped_device *md)
2239{
2240 __dm_destroy(md, false);
2241}
2242
2243void dm_put(struct mapped_device *md)
2244{
2245 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
Edward Goggin79eb8852007-05-09 02:32:56 -07002247EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Mikulas Patocka401600d2009-04-02 19:55:38 +01002249static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002250{
2251 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002252 DECLARE_WAITQUEUE(wait, current);
2253
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002254 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002255
2256 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002257 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002258
2259 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002260 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002261 break;
2262
Mikulas Patocka401600d2009-04-02 19:55:38 +01002263 if (interruptible == TASK_INTERRUPTIBLE &&
2264 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002265 r = -EINTR;
2266 break;
2267 }
2268
2269 io_schedule();
2270 }
2271 set_current_state(TASK_RUNNING);
2272
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002273 remove_wait_queue(&md->wait, &wait);
2274
Milan Broz46125c12008-02-08 02:10:30 +00002275 return r;
2276}
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278/*
2279 * Process the deferred bios
2280 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002281static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Mikulas Patockaef208582009-04-02 19:55:38 +01002283 struct mapped_device *md = container_of(work, struct mapped_device,
2284 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002285 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Tejun Heo6a8736d2010-09-08 18:07:00 +02002287 down_read(&md->io_lock);
Mikulas Patockaef208582009-04-02 19:55:38 +01002288
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002289 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002290 spin_lock_irq(&md->deferred_lock);
2291 c = bio_list_pop(&md->deferred);
2292 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002293
Tejun Heo6a8736d2010-09-08 18:07:00 +02002294 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002295 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002296
Tejun Heo6a8736d2010-09-08 18:07:00 +02002297 up_read(&md->io_lock);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002298
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002299 if (dm_request_based(md))
2300 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002301 else
2302 __split_and_process_bio(md, c);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002303
Tejun Heo6a8736d2010-09-08 18:07:00 +02002304 down_read(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002305 }
Milan Broz73d410c2008-02-08 02:10:25 +00002306
Tejun Heo6a8736d2010-09-08 18:07:00 +02002307 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002310static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002311{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002312 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2313 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002314 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002315}
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002318 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002320struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002322 struct dm_table *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002323 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002324 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Daniel Walkere61290a2008-02-08 02:10:08 +00002326 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002329 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002332 r = dm_calculate_queue_limits(table, &limits);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002333 if (r) {
2334 map = ERR_PTR(r);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002335 goto out;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002336 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002337
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002338 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002340out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002341 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002342 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345/*
2346 * Functions to lock and unlock any filesystem running on the
2347 * device.
2348 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002349static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002351 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002354
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002355 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002356 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002357 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002358 md->frozen_sb = NULL;
2359 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002360 }
2361
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002362 set_bit(DMF_FROZEN, &md->flags);
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return 0;
2365}
2366
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002367static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002369 if (!test_bit(DMF_FROZEN, &md->flags))
2370 return;
2371
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002372 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002374 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
2377/*
2378 * We need to be able to change a mapping table under a mounted
2379 * filesystem. For example we might want to move some data in
2380 * the background. Before the table can be swapped with
2381 * dm_bind_table, dm_suspend must be called to flush any in
2382 * flight bios and ensure that any further io gets deferred.
2383 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002384/*
2385 * Suspend mechanism in request-based dm.
2386 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002387 * 1. Flush all I/Os by lock_fs() if needed.
2388 * 2. Stop dispatching any I/O by stopping the request_queue.
2389 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002390 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002391 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002392 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002393int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002395 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002396 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002397 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002398 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Daniel Walkere61290a2008-02-08 02:10:08 +00002400 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002401
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002402 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002403 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002404 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002407 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002409 /*
2410 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2411 * This flag is cleared before dm_suspend returns.
2412 */
2413 if (noflush)
2414 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2415
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002416 /* This does not get reverted if there's an error later. */
2417 dm_table_presuspend_targets(map);
2418
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002419 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002420 * Flush I/O to the device.
2421 * Any I/O submitted after lock_fs() may not be flushed.
2422 * noflush takes precedence over do_lockfs.
2423 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002424 */
2425 if (!noflush && do_lockfs) {
2426 r = lock_fs(md);
2427 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002428 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002432 * Here we must make sure that no processes are submitting requests
2433 * to target drivers i.e. no one may be executing
2434 * __split_and_process_bio. This is called from dm_request and
2435 * dm_wq_work.
2436 *
2437 * To get all processes out of __split_and_process_bio in dm_request,
2438 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002439 * __split_and_process_bio from dm_request and quiesce the thread
2440 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2441 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002443 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002444 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002445 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002447 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002448 * Stop md->queue before flushing md->wq in case request-based
2449 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002450 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002451 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002452 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002453
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002454 flush_workqueue(md->wq);
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002457 * At this point no more requests are entering target request routines.
2458 * We call dm_wait_for_completion to wait for all existing requests
2459 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002461 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002463 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002464 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002465 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002466 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002469 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002470 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002471
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002472 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002473 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002474
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002475 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002476 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002477 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002478
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002479 /*
2480 * If dm_wait_for_completion returned 0, the device is completely
2481 * quiescent now. There is no request-processing activity. All new
2482 * requests are being added to md->deferred list.
2483 */
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 set_bit(DMF_SUSPENDED, &md->flags);
2486
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002487 dm_table_postsuspend_targets(map);
2488
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002489out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002491
2492out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002493 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002494 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
2497int dm_resume(struct mapped_device *md)
2498{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002499 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002500 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Daniel Walkere61290a2008-02-08 02:10:08 +00002502 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002503 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002504 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002505
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002506 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002507 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002508 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Milan Broz8757b772006-10-03 01:15:36 -07002510 r = dm_table_resume_targets(map);
2511 if (r)
2512 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002513
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002514 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002515
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002516 /*
2517 * Flushing deferred I/Os must be done after targets are resumed
2518 * so that mapping of targets can work correctly.
2519 * Request-based dm is queueing the deferred I/Os in its request_queue.
2520 */
2521 if (dm_request_based(md))
2522 start_queue(md->queue);
2523
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002524 unlock_fs(md);
2525
2526 clear_bit(DMF_SUSPENDED, &md->flags);
2527
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002528 r = 0;
2529out:
2530 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002531 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002532
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002533 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
2536/*-----------------------------------------------------------------
2537 * Event notification.
2538 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002539int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002540 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002541{
Milan Broz60935eb2009-06-22 10:12:30 +01002542 char udev_cookie[DM_COOKIE_LENGTH];
2543 char *envp[] = { udev_cookie, NULL };
2544
2545 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002546 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002547 else {
2548 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2549 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002550 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2551 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002552 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002553}
2554
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002555uint32_t dm_next_uevent_seq(struct mapped_device *md)
2556{
2557 return atomic_add_return(1, &md->uevent_seq);
2558}
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560uint32_t dm_get_event_nr(struct mapped_device *md)
2561{
2562 return atomic_read(&md->event_nr);
2563}
2564
2565int dm_wait_event(struct mapped_device *md, int event_nr)
2566{
2567 return wait_event_interruptible(md->eventq,
2568 (event_nr != atomic_read(&md->event_nr)));
2569}
2570
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002571void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2572{
2573 unsigned long flags;
2574
2575 spin_lock_irqsave(&md->uevent_lock, flags);
2576 list_add(elist, &md->uevent_list);
2577 spin_unlock_irqrestore(&md->uevent_lock, flags);
2578}
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580/*
2581 * The gendisk is only valid as long as you have a reference
2582 * count on 'md'.
2583 */
2584struct gendisk *dm_disk(struct mapped_device *md)
2585{
2586 return md->disk;
2587}
2588
Milan Broz784aae72009-01-06 03:05:12 +00002589struct kobject *dm_kobject(struct mapped_device *md)
2590{
2591 return &md->kobj;
2592}
2593
2594/*
2595 * struct mapped_device should not be exported outside of dm.c
2596 * so use this check to verify that kobj is part of md structure
2597 */
2598struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2599{
2600 struct mapped_device *md;
2601
2602 md = container_of(kobj, struct mapped_device, kobj);
2603 if (&md->kobj != kobj)
2604 return NULL;
2605
Milan Broz4d89b7b2009-06-22 10:12:11 +01002606 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002607 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002608 return NULL;
2609
Milan Broz784aae72009-01-06 03:05:12 +00002610 dm_get(md);
2611 return md;
2612}
2613
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002614int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 return test_bit(DMF_SUSPENDED, &md->flags);
2617}
2618
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002619int dm_suspended(struct dm_target *ti)
2620{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002621 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002622}
2623EXPORT_SYMBOL_GPL(dm_suspended);
2624
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002625int dm_noflush_suspending(struct dm_target *ti)
2626{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002627 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002628}
2629EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2630
Martin K. Petersena91a2782011-03-17 11:11:05 +01002631struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002632{
2633 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
Martin K. Petersena91a2782011-03-17 11:11:05 +01002634 unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002635
2636 if (!pools)
2637 return NULL;
2638
2639 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2640 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2641 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2642 if (!pools->io_pool)
2643 goto free_pools_and_out;
2644
2645 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2646 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2647 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2648 if (!pools->tio_pool)
2649 goto free_io_pool_and_out;
2650
Martin K. Petersena91a2782011-03-17 11:11:05 +01002651 pools->bs = bioset_create(pool_size, 0);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002652 if (!pools->bs)
2653 goto free_tio_pool_and_out;
2654
Martin K. Petersena91a2782011-03-17 11:11:05 +01002655 if (integrity && bioset_integrity_create(pools->bs, pool_size))
2656 goto free_bioset_and_out;
2657
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002658 return pools;
2659
Martin K. Petersena91a2782011-03-17 11:11:05 +01002660free_bioset_and_out:
2661 bioset_free(pools->bs);
2662
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002663free_tio_pool_and_out:
2664 mempool_destroy(pools->tio_pool);
2665
2666free_io_pool_and_out:
2667 mempool_destroy(pools->io_pool);
2668
2669free_pools_and_out:
2670 kfree(pools);
2671
2672 return NULL;
2673}
2674
2675void dm_free_md_mempools(struct dm_md_mempools *pools)
2676{
2677 if (!pools)
2678 return;
2679
2680 if (pools->io_pool)
2681 mempool_destroy(pools->io_pool);
2682
2683 if (pools->tio_pool)
2684 mempool_destroy(pools->tio_pool);
2685
2686 if (pools->bs)
2687 bioset_free(pools->bs);
2688
2689 kfree(pools);
2690}
2691
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002692static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 .open = dm_blk_open,
2694 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002695 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002696 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 .owner = THIS_MODULE
2698};
2699
2700EXPORT_SYMBOL(dm_get_mapinfo);
2701
2702/*
2703 * module hooks
2704 */
2705module_init(dm_init);
2706module_exit(dm_exit);
2707
2708module_param(major, uint, 0);
2709MODULE_PARM_DESC(major, "The major number of the device mapper");
2710MODULE_DESCRIPTION(DM_NAME " driver");
2711MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2712MODULE_LICENSE("GPL");