blob: d22b9905c1682e79e6f998dc6e638f9915d57355 [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
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070040static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000042 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * One of these is allocated per bio.
44 */
45struct dm_io {
46 struct mapped_device *md;
47 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010049 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080050 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010051 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000055 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * One of these is allocated per target within a bio. Hopefully
57 * this will be simplified out one day.
58 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010059struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct dm_io *io;
61 struct dm_target *ti;
62 union map_info info;
63};
64
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000065/*
66 * For request-based dm.
67 * One of these is allocated per request.
68 */
69struct dm_rq_target_io {
70 struct mapped_device *md;
71 struct dm_target *ti;
72 struct request *orig, clone;
73 int error;
74 union map_info info;
75};
76
77/*
78 * For request-based dm.
79 * One of these is allocated per bio.
80 */
81struct dm_rq_clone_bio_info {
82 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010083 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000084};
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086union map_info *dm_get_mapinfo(struct bio *bio)
87{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070088 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010089 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070090 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010093union map_info *dm_get_rq_mapinfo(struct request *rq)
94{
95 if (rq && rq->end_io_data)
96 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
97 return NULL;
98}
99EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
100
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700101#define MINOR_ALLOCED ((void *)-1)
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Bits for the md->flags field.
105 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100106#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800108#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700109#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700110#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800111#define DMF_NOFLUSH_SUSPENDING 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Milan Broz304f3f62008-02-08 02:11:17 +0000113/*
114 * Work processed by per-device workqueue.
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700117 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000118 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 rwlock_t map_lock;
120 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700121 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 unsigned long flags;
124
Jens Axboe165125e2007-07-24 09:28:11 +0200125 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100126 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100127 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100128 struct mutex type_lock;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800131 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 void *interface_ptr;
134
135 /*
136 * A list of ios that arrived while we were suspended.
137 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200138 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100140 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800141 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100142 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200145 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000146 */
147 struct workqueue_struct *wq;
148
149 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * The current mapping.
151 */
152 struct dm_table *map;
153
154 /*
155 * io objects are allocated from here.
156 */
157 mempool_t *io_pool;
158 mempool_t *tio_pool;
159
Stefan Bader9faf4002006-10-03 01:15:41 -0700160 struct bio_set *bs;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * Event handling.
164 */
165 atomic_t event_nr;
166 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100167 atomic_t uevent_seq;
168 struct list_head uevent_list;
169 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /*
172 * freeze/thaw support require holding onto a super block
173 */
174 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100175 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800176
177 /* forced geometry settings */
178 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000179
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100180 /* For saving the address of __make_request for request based dm */
181 make_request_fn *saved_make_request_fn;
182
Milan Broz784aae72009-01-06 03:05:12 +0000183 /* sysfs handle */
184 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100185
Tejun Heod87f4c12010-09-03 11:56:19 +0200186 /* zero-length flush that will be cloned and submitted to targets */
187 struct bio flush_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100190/*
191 * For mempools pre-allocation at the table loading time.
192 */
193struct dm_md_mempools {
194 mempool_t *io_pool;
195 mempool_t *tio_pool;
196 struct bio_set *bs;
197};
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800200static struct kmem_cache *_io_cache;
201static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000202static struct kmem_cache *_rq_tio_cache;
203static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static int __init local_init(void)
206{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100207 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100210 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100212 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100215 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100216 if (!_tio_cache)
217 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000219 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
220 if (!_rq_tio_cache)
221 goto out_free_tio_cache;
222
223 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
224 if (!_rq_bio_info_cache)
225 goto out_free_rq_tio_cache;
226
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100227 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100228 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000229 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 _major = major;
232 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100233 if (r < 0)
234 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!_major)
237 _major = r;
238
239 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100240
241out_uevent_exit:
242 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000243out_free_rq_bio_info_cache:
244 kmem_cache_destroy(_rq_bio_info_cache);
245out_free_rq_tio_cache:
246 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100247out_free_tio_cache:
248 kmem_cache_destroy(_tio_cache);
249out_free_io_cache:
250 kmem_cache_destroy(_io_cache);
251
252 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255static void local_exit(void)
256{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000257 kmem_cache_destroy(_rq_bio_info_cache);
258 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 kmem_cache_destroy(_tio_cache);
260 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700261 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100262 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 _major = 0;
265
266 DMINFO("cleaned up");
267}
268
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000269static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 local_init,
271 dm_target_init,
272 dm_linear_init,
273 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000274 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100275 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 dm_interface_init,
277};
278
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000279static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 local_exit,
281 dm_target_exit,
282 dm_linear_exit,
283 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000284 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100285 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 dm_interface_exit,
287};
288
289static int __init dm_init(void)
290{
291 const int count = ARRAY_SIZE(_inits);
292
293 int r, i;
294
295 for (i = 0; i < count; i++) {
296 r = _inits[i]();
297 if (r)
298 goto bad;
299 }
300
301 return 0;
302
303 bad:
304 while (i--)
305 _exits[i]();
306
307 return r;
308}
309
310static void __exit dm_exit(void)
311{
312 int i = ARRAY_SIZE(_exits);
313
314 while (i--)
315 _exits[i]();
316}
317
318/*
319 * Block device functions
320 */
Mike Anderson432a2122009-12-10 23:52:20 +0000321int dm_deleting_md(struct mapped_device *md)
322{
323 return test_bit(DMF_DELETING, &md->flags);
324}
325
Al Virofe5f9f22008-03-02 10:29:31 -0500326static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct mapped_device *md;
329
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700330 spin_lock(&_minor_lock);
331
Al Virofe5f9f22008-03-02 10:29:31 -0500332 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700333 if (!md)
334 goto out;
335
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700336 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000337 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700338 md = NULL;
339 goto out;
340 }
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700343 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700344
345out:
346 spin_unlock(&_minor_lock);
347
348 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Al Virofe5f9f22008-03-02 10:29:31 -0500351static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Al Virofe5f9f22008-03-02 10:29:31 -0500353 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200354
Milan Broz4a1aeb92011-01-13 19:59:48 +0000355 spin_lock(&_minor_lock);
356
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700357 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000359
360 spin_unlock(&_minor_lock);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363}
364
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700365int dm_open_count(struct mapped_device *md)
366{
367 return atomic_read(&md->open_count);
368}
369
370/*
371 * Guarantees nothing is using the device before it's deleted.
372 */
373int dm_lock_for_deletion(struct mapped_device *md)
374{
375 int r = 0;
376
377 spin_lock(&_minor_lock);
378
379 if (dm_open_count(md))
380 r = -EBUSY;
381 else
382 set_bit(DMF_DELETING, &md->flags);
383
384 spin_unlock(&_minor_lock);
385
386 return r;
387}
388
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800389static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
390{
391 struct mapped_device *md = bdev->bd_disk->private_data;
392
393 return dm_get_geometry(md, geo);
394}
395
Al Virofe5f9f22008-03-02 10:29:31 -0500396static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700397 unsigned int cmd, unsigned long arg)
398{
Al Virofe5f9f22008-03-02 10:29:31 -0500399 struct mapped_device *md = bdev->bd_disk->private_data;
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000400 struct dm_table *map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700401 struct dm_target *tgt;
402 int r = -ENOTTY;
403
Milan Brozaa129a22006-10-03 01:15:15 -0700404 if (!map || !dm_table_get_size(map))
405 goto out;
406
407 /* We only support devices that have a single target */
408 if (dm_table_get_num_targets(map) != 1)
409 goto out;
410
411 tgt = dm_table_get_target(map, 0);
412
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000413 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700414 r = -EAGAIN;
415 goto out;
416 }
417
418 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400419 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700420
421out:
422 dm_table_put(map);
423
Milan Brozaa129a22006-10-03 01:15:15 -0700424 return r;
425}
426
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100427static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 return mempool_alloc(md->io_pool, GFP_NOIO);
430}
431
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100432static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 mempool_free(io, md->io_pool);
435}
436
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100437static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 mempool_free(tio, md->tio_pool);
440}
441
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000442static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
443 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100444{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000445 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100446}
447
448static void free_rq_tio(struct dm_rq_target_io *tio)
449{
450 mempool_free(tio, tio->md->tio_pool);
451}
452
453static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
454{
455 return mempool_alloc(md->io_pool, GFP_ATOMIC);
456}
457
458static void free_bio_info(struct dm_rq_clone_bio_info *info)
459{
460 mempool_free(info, info->tio->md->io_pool);
461}
462
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000463static int md_in_flight(struct mapped_device *md)
464{
465 return atomic_read(&md->pending[READ]) +
466 atomic_read(&md->pending[WRITE]);
467}
468
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800469static void start_io_acct(struct dm_io *io)
470{
471 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900472 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200473 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800474
475 io->start_time = jiffies;
476
Tejun Heo074a7ac2008-08-25 19:56:14 +0900477 cpu = part_stat_lock();
478 part_round_stats(cpu, &dm_disk(md)->part0);
479 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200480 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800481}
482
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000483static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800484{
485 struct mapped_device *md = io->md;
486 struct bio *bio = io->bio;
487 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900488 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800489 int rw = bio_data_dir(bio);
490
Tejun Heo074a7ac2008-08-25 19:56:14 +0900491 cpu = part_stat_lock();
492 part_round_stats(cpu, &dm_disk(md)->part0);
493 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
494 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800495
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100496 /*
497 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200498 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100499 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200500 dm_disk(md)->part0.in_flight[rw] = pending =
501 atomic_dec_return(&md->pending[rw]);
502 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800503
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000504 /* nudge anyone waiting on suspend queue */
505 if (!pending)
506 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
510 * Add the bio to the list of deferred io.
511 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100512static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200514 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200516 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200518 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200519 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/*
523 * Everyone (including functions in this file), should use this
524 * function to access the md->map field, and make sure they call
525 * dm_table_put() when finished.
526 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000527struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100530 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100532 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 t = md->map;
534 if (t)
535 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100536 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 return t;
539}
540
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800541/*
542 * Get the geometry associated with a dm device
543 */
544int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
545{
546 *geo = md->geometry;
547
548 return 0;
549}
550
551/*
552 * Set the geometry of a device.
553 */
554int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
555{
556 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
557
558 if (geo->start > sz) {
559 DMWARN("Start sector is beyond the geometry limits.");
560 return -EINVAL;
561 }
562
563 md->geometry = *geo;
564
565 return 0;
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/*-----------------------------------------------------------------
569 * CRUD START:
570 * A more elegant soln is in the works that uses the queue
571 * merge fn, unfortunately there are a couple of changes to
572 * the block layer that I want to make for this. So in the
573 * interests of getting something for people to use I give
574 * you this clearly demarcated crap.
575 *---------------------------------------------------------------*/
576
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800577static int __noflush_suspending(struct mapped_device *md)
578{
579 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
583 * Decrements the number of outstanding ios that a bio has been
584 * cloned into, completing the original io if necc.
585 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800586static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800588 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000589 int io_error;
590 struct bio *bio;
591 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800592
593 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100594 if (unlikely(error)) {
595 spin_lock_irqsave(&io->endio_lock, flags);
596 if (!(io->error > 0 && __noflush_suspending(md)))
597 io->error = error;
598 spin_unlock_irqrestore(&io->endio_lock, flags);
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800602 if (io->error == DM_ENDIO_REQUEUE) {
603 /*
604 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800605 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100606 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200607 if (__noflush_suspending(md))
608 bio_list_add_head(&md->deferred, io->bio);
609 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800610 /* noflush suspend was interrupted. */
611 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100612 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800613 }
614
Milan Brozb35f8ca2009-03-16 17:44:36 +0000615 io_error = io->error;
616 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200617 end_io_acct(io);
618 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100619
Tejun Heo6a8736d2010-09-08 18:07:00 +0200620 if (io_error == DM_ENDIO_REQUEUE)
621 return;
622
Mike Snitzerb372d362010-09-08 18:07:01 +0200623 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100624 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200625 * Preflush done for flush with data, reissue
626 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100627 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200628 bio->bi_rw &= ~REQ_FLUSH;
629 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100630 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200631 /* done with normal IO or empty flush */
Jeff Moyerb7908c12011-01-06 20:41:42 +0100632 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200633 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636}
637
NeilBrown6712ecf2007-09-27 12:47:43 +0200638static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100641 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000642 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700643 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 dm_endio_fn endio = tio->ti->type->end_io;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
647 error = -EIO;
648
649 if (endio) {
650 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800651 if (r < 0 || r == DM_ENDIO_REQUEUE)
652 /*
653 * error and requeue request are handled
654 * in dec_pending().
655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800657 else if (r == DM_ENDIO_INCOMPLETE)
658 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200659 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800660 else if (r) {
661 DMWARN("unimplemented target endio return value: %d", r);
662 BUG();
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
Stefan Bader9faf4002006-10-03 01:15:41 -0700666 /*
667 * Store md for cleanup instead of tio which is about to get freed.
668 */
669 bio->bi_private = md->bs;
670
Stefan Bader9faf4002006-10-03 01:15:41 -0700671 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000672 bio_put(bio);
673 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100676/*
677 * Partial completion handling for request-based dm
678 */
679static void end_clone_bio(struct bio *clone, int error)
680{
681 struct dm_rq_clone_bio_info *info = clone->bi_private;
682 struct dm_rq_target_io *tio = info->tio;
683 struct bio *bio = info->orig;
684 unsigned int nr_bytes = info->orig->bi_size;
685
686 bio_put(clone);
687
688 if (tio->error)
689 /*
690 * An error has already been detected on the request.
691 * Once error occurred, just let clone->end_io() handle
692 * the remainder.
693 */
694 return;
695 else if (error) {
696 /*
697 * Don't notice the error to the upper layer yet.
698 * The error handling decision is made by the target driver,
699 * when the request is completed.
700 */
701 tio->error = error;
702 return;
703 }
704
705 /*
706 * I/O for the bio successfully completed.
707 * Notice the data completion to the upper layer.
708 */
709
710 /*
711 * bios are processed from the head of the list.
712 * So the completing bio should always be rq->bio.
713 * If it's not, something wrong is happening.
714 */
715 if (tio->orig->bio != bio)
716 DMERR("bio completion is going in the middle of the request");
717
718 /*
719 * Update the original request.
720 * Do not use blk_end_request() here, because it may complete
721 * the original request before the clone, and break the ordering.
722 */
723 blk_update_request(tio->orig, 0, nr_bytes);
724}
725
726/*
727 * Don't touch any member of the md after calling this function because
728 * the md may be freed in dm_put() at the end of this function.
729 * Or do dm_get() before calling this function and dm_put() later.
730 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000731static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100732{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000733 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100734
735 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000736 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100737 wake_up(&md->wait);
738
739 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000740 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100741
742 /*
743 * dm_put() must be at the end of this function. See the comment above
744 */
745 dm_put(md);
746}
747
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100748static void free_rq_clone(struct request *clone)
749{
750 struct dm_rq_target_io *tio = clone->end_io_data;
751
752 blk_rq_unprep_clone(clone);
753 free_rq_tio(tio);
754}
755
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000756/*
757 * Complete the clone and the original request.
758 * Must be called without queue lock.
759 */
760static void dm_end_request(struct request *clone, int error)
761{
762 int rw = rq_data_dir(clone);
763 struct dm_rq_target_io *tio = clone->end_io_data;
764 struct mapped_device *md = tio->md;
765 struct request *rq = tio->orig;
766
Tejun Heo29e40132010-09-08 18:07:00 +0200767 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000768 rq->errors = clone->errors;
769 rq->resid_len = clone->resid_len;
770
771 if (rq->sense)
772 /*
773 * We are using the sense buffer of the original
774 * request.
775 * So setting the length of the sense data is enough.
776 */
777 rq->sense_len = clone->sense_len;
778 }
779
780 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200781 blk_end_request_all(rq, error);
782 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000783}
784
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100785static void dm_unprep_request(struct request *rq)
786{
787 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100788
789 rq->special = NULL;
790 rq->cmd_flags &= ~REQ_DONTPREP;
791
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100792 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100793}
794
795/*
796 * Requeue the original request of a clone.
797 */
798void dm_requeue_unmapped_request(struct request *clone)
799{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000800 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100801 struct dm_rq_target_io *tio = clone->end_io_data;
802 struct mapped_device *md = tio->md;
803 struct request *rq = tio->orig;
804 struct request_queue *q = rq->q;
805 unsigned long flags;
806
807 dm_unprep_request(rq);
808
809 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100810 blk_requeue_request(q, rq);
811 spin_unlock_irqrestore(q->queue_lock, flags);
812
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000813 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100814}
815EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
816
817static void __stop_queue(struct request_queue *q)
818{
819 blk_stop_queue(q);
820}
821
822static void stop_queue(struct request_queue *q)
823{
824 unsigned long flags;
825
826 spin_lock_irqsave(q->queue_lock, flags);
827 __stop_queue(q);
828 spin_unlock_irqrestore(q->queue_lock, flags);
829}
830
831static void __start_queue(struct request_queue *q)
832{
833 if (blk_queue_stopped(q))
834 blk_start_queue(q);
835}
836
837static void start_queue(struct request_queue *q)
838{
839 unsigned long flags;
840
841 spin_lock_irqsave(q->queue_lock, flags);
842 __start_queue(q);
843 spin_unlock_irqrestore(q->queue_lock, flags);
844}
845
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000846static void dm_done(struct request *clone, int error, bool mapped)
847{
848 int r = error;
849 struct dm_rq_target_io *tio = clone->end_io_data;
850 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
851
852 if (mapped && rq_end_io)
853 r = rq_end_io(tio->ti, clone, error, &tio->info);
854
855 if (r <= 0)
856 /* The target wants to complete the I/O */
857 dm_end_request(clone, r);
858 else if (r == DM_ENDIO_INCOMPLETE)
859 /* The target will handle the I/O */
860 return;
861 else if (r == DM_ENDIO_REQUEUE)
862 /* The target wants to requeue the I/O */
863 dm_requeue_unmapped_request(clone);
864 else {
865 DMWARN("unimplemented target endio return value: %d", r);
866 BUG();
867 }
868}
869
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100870/*
871 * Request completion handler for request-based dm
872 */
873static void dm_softirq_done(struct request *rq)
874{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000875 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100876 struct request *clone = rq->completion_data;
877 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100878
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000879 if (rq->cmd_flags & REQ_FAILED)
880 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100881
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000882 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100883}
884
885/*
886 * Complete the clone and the original request with the error status
887 * through softirq context.
888 */
889static void dm_complete_request(struct request *clone, int error)
890{
891 struct dm_rq_target_io *tio = clone->end_io_data;
892 struct request *rq = tio->orig;
893
894 tio->error = error;
895 rq->completion_data = clone;
896 blk_complete_request(rq);
897}
898
899/*
900 * Complete the not-mapped clone and the original request with the error status
901 * through softirq context.
902 * Target's rq_end_io() function isn't called.
903 * This may be used when the target's map_rq() function fails.
904 */
905void dm_kill_unmapped_request(struct request *clone, int error)
906{
907 struct dm_rq_target_io *tio = clone->end_io_data;
908 struct request *rq = tio->orig;
909
910 rq->cmd_flags |= REQ_FAILED;
911 dm_complete_request(clone, error);
912}
913EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
914
915/*
916 * Called with the queue lock held
917 */
918static void end_clone_request(struct request *clone, int error)
919{
920 /*
921 * For just cleaning up the information of the queue in which
922 * the clone was dispatched.
923 * The clone is *NOT* freed actually here because it is alloced from
924 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
925 */
926 __blk_put_request(clone->q, clone);
927
928 /*
929 * Actual request completion is done in a softirq context which doesn't
930 * hold the queue lock. Otherwise, deadlock could occur because:
931 * - another request may be submitted by the upper level driver
932 * of the stacking during the completion
933 * - the submission which requires queue lock may be done
934 * against this queue
935 */
936 dm_complete_request(clone, error);
937}
938
Mike Snitzer56a67df2010-08-12 04:14:10 +0100939/*
940 * Return maximum size of I/O possible at the supplied sector up to the current
941 * target boundary.
942 */
943static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100945 sector_t target_offset = dm_target_offset(ti, sector);
946
947 return ti->len - target_offset;
948}
949
950static sector_t max_io_len(sector_t sector, struct dm_target *ti)
951{
952 sector_t len = max_io_len_target_boundary(sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /*
955 * Does the target need to split even further ?
956 */
957 if (ti->split_io) {
958 sector_t boundary;
Mike Snitzer56a67df2010-08-12 04:14:10 +0100959 sector_t offset = dm_target_offset(ti, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
961 - offset;
962 if (len > boundary)
963 len = boundary;
964 }
965
966 return len;
967}
968
969static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100970 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100973 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700974 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 clone->bi_end_io = clone_endio;
977 clone->bi_private = tio;
978
979 /*
980 * Map the clone. If r == 0 we don't need to do
981 * anything, the target has assumed ownership of
982 * this io.
983 */
984 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100985 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800987 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100989
Mike Snitzerd07335e2010-11-16 12:52:38 +0100990 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
991 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800994 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
995 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700996 md = tio->io->md;
997 dec_pending(tio->io, r);
998 /*
999 * Store bio_set for cleanup.
1000 */
1001 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001003 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001004 } else if (r) {
1005 DMWARN("unimplemented target map return value: %d", r);
1006 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008}
1009
1010struct clone_info {
1011 struct mapped_device *md;
1012 struct dm_table *map;
1013 struct bio *bio;
1014 struct dm_io *io;
1015 sector_t sector;
1016 sector_t sector_count;
1017 unsigned short idx;
1018};
1019
Peter Osterlund36763472005-09-06 15:16:42 -07001020static void dm_bio_destructor(struct bio *bio)
1021{
Stefan Bader9faf4002006-10-03 01:15:41 -07001022 struct bio_set *bs = bio->bi_private;
1023
1024 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001028 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
1030static struct bio *split_bvec(struct bio *bio, sector_t sector,
1031 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001032 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 struct bio *clone;
1035 struct bio_vec *bv = bio->bi_io_vec + idx;
1036
Stefan Bader9faf4002006-10-03 01:15:41 -07001037 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001038 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 *clone->bi_io_vec = *bv;
1040
1041 clone->bi_sector = sector;
1042 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001043 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 clone->bi_vcnt = 1;
1045 clone->bi_size = to_bytes(len);
1046 clone->bi_io_vec->bv_offset = offset;
1047 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001048 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Martin K. Petersen9c470082009-04-09 00:27:12 +01001050 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001051 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001052 bio_integrity_trim(clone,
1053 bio_sector_offset(bio, idx, offset), len);
1054 }
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return clone;
1057}
1058
1059/*
1060 * Creates a bio that consists of range of complete bvecs.
1061 */
1062static struct bio *clone_bio(struct bio *bio, sector_t sector,
1063 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001064 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct bio *clone;
1067
Stefan Bader9faf4002006-10-03 01:15:41 -07001068 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1069 __bio_clone(clone, bio);
1070 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 clone->bi_sector = sector;
1072 clone->bi_idx = idx;
1073 clone->bi_vcnt = idx + bv_count;
1074 clone->bi_size = to_bytes(len);
1075 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1076
Martin K. Petersen9c470082009-04-09 00:27:12 +01001077 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001078 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001079
1080 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1081 bio_integrity_trim(clone,
1082 bio_sector_offset(bio, idx, 0), len);
1083 }
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return clone;
1086}
1087
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001088static struct dm_target_io *alloc_tio(struct clone_info *ci,
1089 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001090{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001091 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001092
1093 tio->io = ci->io;
1094 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001095 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001096
1097 return tio;
1098}
1099
Mike Snitzer06a426c2010-08-12 04:14:09 +01001100static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001101 unsigned request_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001102{
1103 struct dm_target_io *tio = alloc_tio(ci, ti);
1104 struct bio *clone;
1105
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001106 tio->info.target_request_nr = request_nr;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001107
Mike Snitzer06a426c2010-08-12 04:14:09 +01001108 /*
1109 * Discard requests require the bio's inline iovecs be initialized.
1110 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1111 * and discard, so no need for concern about wasted bvec allocations.
1112 */
1113 clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001114 __bio_clone(clone, ci->bio);
1115 clone->bi_destructor = dm_bio_destructor;
Mike Snitzera79245b2010-08-12 04:14:24 +01001116 if (len) {
1117 clone->bi_sector = ci->sector;
1118 clone->bi_size = to_bytes(len);
1119 }
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001120
1121 __map_bio(ti, clone, tio);
1122}
1123
Mike Snitzer06a426c2010-08-12 04:14:09 +01001124static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001125 unsigned num_requests, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001126{
1127 unsigned request_nr;
1128
1129 for (request_nr = 0; request_nr < num_requests; request_nr++)
Mike Snitzera79245b2010-08-12 04:14:24 +01001130 __issue_target_request(ci, ti, request_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001131}
1132
Mike Snitzerb372d362010-09-08 18:07:01 +02001133static int __clone_and_map_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001134{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001135 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001136 struct dm_target *ti;
1137
Mike Snitzerb372d362010-09-08 18:07:01 +02001138 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001139 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mike Snitzera79245b2010-08-12 04:14:24 +01001140 __issue_target_requests(ci, ti, ti->num_flush_requests, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001141
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001142 return 0;
1143}
1144
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001145/*
1146 * Perform all io with a single clone.
1147 */
1148static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
1149{
1150 struct bio *clone, *bio = ci->bio;
1151 struct dm_target_io *tio;
1152
1153 tio = alloc_tio(ci, ti);
1154 clone = clone_bio(bio, ci->sector, ci->idx,
1155 bio->bi_vcnt - ci->idx, ci->sector_count,
1156 ci->md->bs);
1157 __map_bio(ti, clone, tio);
1158 ci->sector_count = 0;
1159}
1160
1161static int __clone_and_map_discard(struct clone_info *ci)
1162{
1163 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001164 sector_t len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001165
Mike Snitzera79245b2010-08-12 04:14:24 +01001166 do {
1167 ti = dm_table_find_target(ci->map, ci->sector);
1168 if (!dm_target_is_valid(ti))
1169 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001170
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001171 /*
Mike Snitzera79245b2010-08-12 04:14:24 +01001172 * Even though the device advertised discard support,
1173 * reconfiguration might have changed that since the
1174 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001175 */
Mike Snitzera79245b2010-08-12 04:14:24 +01001176 if (!ti->num_discard_requests)
1177 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001178
Mike Snitzera79245b2010-08-12 04:14:24 +01001179 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001180
Mike Snitzera79245b2010-08-12 04:14:24 +01001181 __issue_target_requests(ci, ti, ti->num_discard_requests, len);
1182
1183 ci->sector += len;
1184 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001185
1186 return 0;
1187}
1188
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001189static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001192 struct dm_target *ti;
1193 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001194 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001196 if (unlikely(bio->bi_rw & REQ_DISCARD))
1197 return __clone_and_map_discard(ci);
1198
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001199 ti = dm_table_find_target(ci->map, ci->sector);
1200 if (!dm_target_is_valid(ti))
1201 return -EIO;
1202
Mike Snitzer56a67df2010-08-12 04:14:10 +01001203 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (ci->sector_count <= max) {
1206 /*
1207 * Optimise for the simple case where we can do all of
1208 * the remaining io with a single clone.
1209 */
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001210 __clone_and_map_simple(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1213 /*
1214 * There are some bvecs that don't span targets.
1215 * Do as many of these as possible.
1216 */
1217 int i;
1218 sector_t remaining = max;
1219 sector_t bv_len;
1220
1221 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1222 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1223
1224 if (bv_len > remaining)
1225 break;
1226
1227 remaining -= bv_len;
1228 len += bv_len;
1229 }
1230
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001231 tio = alloc_tio(ci, ti);
Stefan Bader9faf4002006-10-03 01:15:41 -07001232 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1233 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 __map_bio(ti, clone, tio);
1235
1236 ci->sector += len;
1237 ci->sector_count -= len;
1238 ci->idx = i;
1239
1240 } else {
1241 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001242 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
1244 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001245 sector_t remaining = to_sector(bv->bv_len);
1246 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001248 do {
1249 if (offset) {
1250 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001251 if (!dm_target_is_valid(ti))
1252 return -EIO;
1253
Mike Snitzer56a67df2010-08-12 04:14:10 +01001254 max = max_io_len(ci->sector, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001257 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001259 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001260 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001261 bv->bv_offset + offset, len,
1262 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001263
1264 __map_bio(ti, clone, tio);
1265
1266 ci->sector += len;
1267 ci->sector_count -= len;
1268 offset += to_bytes(len);
1269 } while (remaining -= len);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 ci->idx++;
1272 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001273
1274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001278 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001280static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001283 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001285 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001286 if (unlikely(!ci.map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001287 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001288 return;
1289 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 ci.io = alloc_io(md);
1293 ci.io->error = 0;
1294 atomic_set(&ci.io->io_count, 1);
1295 ci.io->bio = bio;
1296 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001297 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ci.sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ci.idx = bio->bi_idx;
1300
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001301 start_io_acct(ci.io);
Mike Snitzerb372d362010-09-08 18:07:01 +02001302 if (bio->bi_rw & REQ_FLUSH) {
1303 ci.bio = &ci.md->flush_bio;
1304 ci.sector_count = 0;
1305 error = __clone_and_map_empty_flush(&ci);
1306 /* dec_pending submits any data associated with flush */
1307 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001308 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001309 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001310 while (ci.sector_count && !error)
Tejun Heod87f4c12010-09-03 11:56:19 +02001311 error = __clone_and_map(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001315 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 dm_table_put(ci.map);
1317}
1318/*-----------------------------------------------------------------
1319 * CRUD END
1320 *---------------------------------------------------------------*/
1321
Milan Brozf6fccb12008-07-21 12:00:37 +01001322static int dm_merge_bvec(struct request_queue *q,
1323 struct bvec_merge_data *bvm,
1324 struct bio_vec *biovec)
1325{
1326 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001327 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001328 struct dm_target *ti;
1329 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001330 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001331
1332 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001333 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001334
1335 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001336 if (!dm_target_is_valid(ti))
1337 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001338
1339 /*
1340 * Find maximum amount of I/O that won't need splitting
1341 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001342 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001343 (sector_t) BIO_MAX_SECTORS);
1344 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1345 if (max_size < 0)
1346 max_size = 0;
1347
1348 /*
1349 * merge_bvec_fn() returns number of bytes
1350 * it can accept at this offset
1351 * max is precomputed maximal io size
1352 */
1353 if (max_size && ti->type->merge)
1354 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001355 /*
1356 * If the target doesn't support merge method and some of the devices
1357 * provided their merge_bvec method (we know this by looking at
1358 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1359 * entries. So always set max_size to 0, and the code below allows
1360 * just one page.
1361 */
1362 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1363
1364 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001365
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001366out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001367 dm_table_put(map);
1368
1369out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001370 /*
1371 * Always allow an entire first page
1372 */
1373 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1374 max_size = biovec->bv_len;
1375
Milan Brozf6fccb12008-07-21 12:00:37 +01001376 return max_size;
1377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379/*
1380 * The request function that just remaps the bio built up by
1381 * dm_merge_bvec.
1382 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001383static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Kevin Corry12f03a42006-02-01 03:04:52 -08001385 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001387 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001389 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Tejun Heo074a7ac2008-08-25 19:56:14 +09001391 cpu = part_stat_lock();
1392 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1393 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1394 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001395
Tejun Heo6a8736d2010-09-08 18:07:00 +02001396 /* if we're suspended, we have to queue this io for later */
1397 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001398 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Tejun Heo6a8736d2010-09-08 18:07:00 +02001400 if (bio_rw(bio) != READA)
1401 queue_io(md, bio);
1402 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001403 bio_io_error(bio);
Mikulas Patocka92c63902009-04-09 00:27:15 +01001404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001407 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001408 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001412static int dm_make_request(struct request_queue *q, struct bio *bio)
1413{
1414 struct mapped_device *md = q->queuedata;
1415
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001416 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1417}
1418
1419static int dm_request_based(struct mapped_device *md)
1420{
1421 return blk_queue_stackable(md->queue);
1422}
1423
1424static int dm_request(struct request_queue *q, struct bio *bio)
1425{
1426 struct mapped_device *md = q->queuedata;
1427
1428 if (dm_request_based(md))
1429 return dm_make_request(q, bio);
1430
1431 return _dm_request(q, bio);
1432}
1433
1434void dm_dispatch_request(struct request *rq)
1435{
1436 int r;
1437
1438 if (blk_queue_io_stat(rq->q))
1439 rq->cmd_flags |= REQ_IO_STAT;
1440
1441 rq->start_time = jiffies;
1442 r = blk_insert_cloned_request(rq->q, rq);
1443 if (r)
1444 dm_complete_request(rq, r);
1445}
1446EXPORT_SYMBOL_GPL(dm_dispatch_request);
1447
1448static void dm_rq_bio_destructor(struct bio *bio)
1449{
1450 struct dm_rq_clone_bio_info *info = bio->bi_private;
1451 struct mapped_device *md = info->tio->md;
1452
1453 free_bio_info(info);
1454 bio_free(bio, md->bs);
1455}
1456
1457static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1458 void *data)
1459{
1460 struct dm_rq_target_io *tio = data;
1461 struct mapped_device *md = tio->md;
1462 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1463
1464 if (!info)
1465 return -ENOMEM;
1466
1467 info->orig = bio_orig;
1468 info->tio = tio;
1469 bio->bi_end_io = end_clone_bio;
1470 bio->bi_private = info;
1471 bio->bi_destructor = dm_rq_bio_destructor;
1472
1473 return 0;
1474}
1475
1476static int setup_clone(struct request *clone, struct request *rq,
1477 struct dm_rq_target_io *tio)
1478{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001479 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001480
Tejun Heo29e40132010-09-08 18:07:00 +02001481 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1482 dm_rq_bio_constructor, tio);
1483 if (r)
1484 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001485
Tejun Heo29e40132010-09-08 18:07:00 +02001486 clone->cmd = rq->cmd;
1487 clone->cmd_len = rq->cmd_len;
1488 clone->sense = rq->sense;
1489 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001490 clone->end_io = end_clone_request;
1491 clone->end_io_data = tio;
1492
1493 return 0;
1494}
1495
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001496static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1497 gfp_t gfp_mask)
1498{
1499 struct request *clone;
1500 struct dm_rq_target_io *tio;
1501
1502 tio = alloc_rq_tio(md, gfp_mask);
1503 if (!tio)
1504 return NULL;
1505
1506 tio->md = md;
1507 tio->ti = NULL;
1508 tio->orig = rq;
1509 tio->error = 0;
1510 memset(&tio->info, 0, sizeof(tio->info));
1511
1512 clone = &tio->clone;
1513 if (setup_clone(clone, rq, tio)) {
1514 /* -ENOMEM */
1515 free_rq_tio(tio);
1516 return NULL;
1517 }
1518
1519 return clone;
1520}
1521
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001522/*
1523 * Called with the queue lock held.
1524 */
1525static int dm_prep_fn(struct request_queue *q, struct request *rq)
1526{
1527 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001528 struct request *clone;
1529
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001530 if (unlikely(rq->special)) {
1531 DMWARN("Already has something in rq->special.");
1532 return BLKPREP_KILL;
1533 }
1534
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001535 clone = clone_rq(rq, md, GFP_ATOMIC);
1536 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001537 return BLKPREP_DEFER;
1538
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001539 rq->special = clone;
1540 rq->cmd_flags |= REQ_DONTPREP;
1541
1542 return BLKPREP_OK;
1543}
1544
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001545/*
1546 * Returns:
1547 * 0 : the request has been processed (not requeued)
1548 * !0 : the request has been requeued
1549 */
1550static int map_request(struct dm_target *ti, struct request *clone,
1551 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001552{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001553 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001554 struct dm_rq_target_io *tio = clone->end_io_data;
1555
1556 /*
1557 * Hold the md reference here for the in-flight I/O.
1558 * We can't rely on the reference count by device opener,
1559 * because the device may be closed during the request completion
1560 * when all bios are completed.
1561 * See the comment in rq_completed() too.
1562 */
1563 dm_get(md);
1564
1565 tio->ti = ti;
1566 r = ti->type->map_rq(ti, clone, &tio->info);
1567 switch (r) {
1568 case DM_MAPIO_SUBMITTED:
1569 /* The target has taken the I/O to submit by itself later */
1570 break;
1571 case DM_MAPIO_REMAPPED:
1572 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001573 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1574 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001575 dm_dispatch_request(clone);
1576 break;
1577 case DM_MAPIO_REQUEUE:
1578 /* The target wants to requeue the I/O */
1579 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001580 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001581 break;
1582 default:
1583 if (r > 0) {
1584 DMWARN("unimplemented target map return value: %d", r);
1585 BUG();
1586 }
1587
1588 /* The target wants to complete the I/O */
1589 dm_kill_unmapped_request(clone, r);
1590 break;
1591 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001592
1593 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001594}
1595
1596/*
1597 * q->request_fn for request-based dm.
1598 * Called with the queue lock held.
1599 */
1600static void dm_request_fn(struct request_queue *q)
1601{
1602 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001603 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001604 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001605 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001606 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001607
1608 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001609 * For suspend, check blk_queue_stopped() and increment
1610 * ->pending within a single queue_lock not to increment the
1611 * number of in-flight I/Os after the queue is stopped in
1612 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001613 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001614 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001615 rq = blk_peek_request(q);
1616 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001617 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001618
Tejun Heo29e40132010-09-08 18:07:00 +02001619 /* always use block 0 to find the target for flushes for now */
1620 pos = 0;
1621 if (!(rq->cmd_flags & REQ_FLUSH))
1622 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001623
Tejun Heo29e40132010-09-08 18:07:00 +02001624 ti = dm_table_find_target(map, pos);
1625 BUG_ON(!dm_target_is_valid(ti));
1626
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001627 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001628 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001629
1630 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001631 clone = rq->special;
1632 atomic_inc(&md->pending[rq_data_dir(clone)]);
1633
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001634 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001635 if (map_request(ti, clone, md))
1636 goto requeued;
1637
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001638 BUG_ON(!irqs_disabled());
1639 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001640 }
1641
1642 goto out;
1643
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001644requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001645 BUG_ON(!irqs_disabled());
1646 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001647
Jens Axboe7eaceac2011-03-10 08:52:07 +01001648delay_and_out:
1649 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001650out:
1651 dm_table_put(map);
1652
1653 return;
1654}
1655
1656int dm_underlying_device_busy(struct request_queue *q)
1657{
1658 return blk_lld_busy(q);
1659}
1660EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1661
1662static int dm_lld_busy(struct request_queue *q)
1663{
1664 int r;
1665 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001666 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001667
1668 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1669 r = 1;
1670 else
1671 r = dm_table_any_busy_target(map);
1672
1673 dm_table_put(map);
1674
1675 return r;
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678static int dm_any_congested(void *congested_data, int bdi_bits)
1679{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001680 int r = bdi_bits;
1681 struct mapped_device *md = congested_data;
1682 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001684 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001685 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001686 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001687 /*
1688 * Request-based dm cares about only own queue for
1689 * the query about congestion status of request_queue
1690 */
1691 if (dm_request_based(md))
1692 r = md->queue->backing_dev_info.state &
1693 bdi_bits;
1694 else
1695 r = dm_table_any_congested(map, bdi_bits);
1696
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001697 dm_table_put(map);
1698 }
1699 }
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return r;
1702}
1703
1704/*-----------------------------------------------------------------
1705 * An IDR is used to keep track of allocated minor numbers.
1706 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707static DEFINE_IDR(_minor_idr);
1708
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001709static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001711 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001713 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
1716/*
1717 * See if the device with a specific minor # is free.
1718 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001719static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 int r, m;
1722
1723 if (minor >= (1 << MINORBITS))
1724 return -EINVAL;
1725
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001726 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1727 if (!r)
1728 return -ENOMEM;
1729
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001730 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 if (idr_find(&_minor_idr, minor)) {
1733 r = -EBUSY;
1734 goto out;
1735 }
1736
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001737 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001738 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 if (m != minor) {
1742 idr_remove(&_minor_idr, m);
1743 r = -EBUSY;
1744 goto out;
1745 }
1746
1747out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001748 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return r;
1750}
1751
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001752static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001754 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001757 if (!r)
1758 return -ENOMEM;
1759
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001760 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001762 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001763 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 if (m >= (1 << MINORBITS)) {
1767 idr_remove(&_minor_idr, m);
1768 r = -ENOSPC;
1769 goto out;
1770 }
1771
1772 *minor = m;
1773
1774out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001775 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return r;
1777}
1778
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001779static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Mikulas Patocka53d59142009-04-02 19:55:37 +01001781static void dm_wq_work(struct work_struct *work);
1782
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001783static void dm_init_md_queue(struct mapped_device *md)
1784{
1785 /*
1786 * Request-based dm devices cannot be stacked on top of bio-based dm
1787 * devices. The type of this dm device has not been decided yet.
1788 * The type is decided at the first table loading time.
1789 * To prevent problematic device stacking, clear the queue flag
1790 * for request stacking support until then.
1791 *
1792 * This queue is new, so no concurrency on the queue_flags.
1793 */
1794 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1795
1796 md->queue->queuedata = md;
1797 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1798 md->queue->backing_dev_info.congested_data = md;
1799 blk_queue_make_request(md->queue, dm_request);
1800 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001801 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Tejun Heod87f4c12010-09-03 11:56:19 +02001802 blk_queue_flush(md->queue, REQ_FLUSH | REQ_FUA);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805/*
1806 * Allocate and initialise a blank device with a given minor.
1807 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001808static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001811 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001812 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 if (!md) {
1815 DMWARN("unable to allocate device, out of memory.");
1816 return NULL;
1817 }
1818
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001819 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001820 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001823 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001824 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001825 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001826 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001828 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Mike Snitzera5664da2010-08-12 04:14:01 +01001830 md->type = DM_TYPE_NONE;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001831 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001832 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001833 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001834 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 rwlock_init(&md->map_lock);
1836 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001837 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001839 atomic_set(&md->uevent_seq, 0);
1840 INIT_LIST_HEAD(&md->uevent_list);
1841 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001843 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001845 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001847 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 md->disk = alloc_disk(1);
1850 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001851 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001853 atomic_set(&md->pending[0], 0);
1854 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001855 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001856 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001857 init_waitqueue_head(&md->eventq);
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 md->disk->major = _major;
1860 md->disk->first_minor = minor;
1861 md->disk->fops = &dm_blk_dops;
1862 md->disk->queue = md->queue;
1863 md->disk->private_data = md;
1864 sprintf(md->disk->disk_name, "dm-%d", minor);
1865 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001866 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Tejun Heo9c4376d2011-01-13 19:59:58 +00001868 md->wq = alloc_workqueue("kdmflush",
1869 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001870 if (!md->wq)
1871 goto bad_thread;
1872
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001873 md->bdev = bdget_disk(md->disk, 0);
1874 if (!md->bdev)
1875 goto bad_bdev;
1876
Tejun Heo6a8736d2010-09-08 18:07:00 +02001877 bio_init(&md->flush_bio);
1878 md->flush_bio.bi_bdev = md->bdev;
1879 md->flush_bio.bi_rw = WRITE_FLUSH;
1880
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001881 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001882 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001883 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001884 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001885
1886 BUG_ON(old_md != MINOR_ALLOCED);
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return md;
1889
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001890bad_bdev:
1891 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001892bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001893 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001894 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001895bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001896 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001897bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001899bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001900 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001901bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 kfree(md);
1903 return NULL;
1904}
1905
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001906static void unlock_fs(struct mapped_device *md);
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908static void free_dev(struct mapped_device *md)
1909{
Tejun Heof331c022008-09-03 09:01:48 +02001910 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001911
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001912 unlock_fs(md);
1913 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001914 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001915 if (md->tio_pool)
1916 mempool_destroy(md->tio_pool);
1917 if (md->io_pool)
1918 mempool_destroy(md->io_pool);
1919 if (md->bs)
1920 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001921 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001923 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001924
1925 spin_lock(&_minor_lock);
1926 md->disk->private_data = NULL;
1927 spin_unlock(&_minor_lock);
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001930 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001931 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 kfree(md);
1933}
1934
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001935static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1936{
1937 struct dm_md_mempools *p;
1938
1939 if (md->io_pool && md->tio_pool && md->bs)
1940 /* the md already has necessary mempools */
1941 goto out;
1942
1943 p = dm_table_get_md_mempools(t);
1944 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1945
1946 md->io_pool = p->io_pool;
1947 p->io_pool = NULL;
1948 md->tio_pool = p->tio_pool;
1949 p->tio_pool = NULL;
1950 md->bs = p->bs;
1951 p->bs = NULL;
1952
1953out:
1954 /* mempool bind completed, now no need any mempools in the table */
1955 dm_table_free_md_mempools(t);
1956}
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958/*
1959 * Bind a table to the device.
1960 */
1961static void event_callback(void *context)
1962{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001963 unsigned long flags;
1964 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct mapped_device *md = (struct mapped_device *) context;
1966
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001967 spin_lock_irqsave(&md->uevent_lock, flags);
1968 list_splice_init(&md->uevent_list, &uevents);
1969 spin_unlock_irqrestore(&md->uevent_lock, flags);
1970
Tejun Heoed9e1982008-08-25 19:56:05 +09001971 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 atomic_inc(&md->event_nr);
1974 wake_up(&md->eventq);
1975}
1976
Mike Snitzerc2176492011-01-13 19:53:46 +00001977/*
1978 * Protected by md->suspend_lock obtained by dm_swap_table().
1979 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001980static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001982 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001984 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001987/*
1988 * Returns old map, which caller must destroy.
1989 */
1990static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1991 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001993 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02001994 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01001996 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001999
2000 /*
2001 * Wipe any geometry if the size of the table changed.
2002 */
2003 if (size != get_capacity(md->disk))
2004 memset(&md->geometry, 0, sizeof(md->geometry));
2005
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002006 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002008 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002009
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002010 /*
2011 * The queue hasn't been stopped yet, if the old table type wasn't
2012 * for request-based during suspension. So stop it to prevent
2013 * I/O mapping before resume.
2014 * This must be done before setting the queue restrictions,
2015 * because request-based dm may be run just after the setting.
2016 */
2017 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2018 stop_queue(q);
2019
2020 __bind_mempools(md, t);
2021
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002022 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002023 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002024 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002025 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002026 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002027
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002028 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Alasdair G Kergona7940152009-12-10 23:52:23 +00002031/*
2032 * Returns unbound table for the caller to free.
2033 */
2034static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002037 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002040 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002043 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002045 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002046
2047 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050/*
2051 * Constructor for a new device.
2052 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002053int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 struct mapped_device *md;
2056
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002057 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (!md)
2059 return -ENXIO;
2060
Milan Broz784aae72009-01-06 03:05:12 +00002061 dm_sysfs_init(md);
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 *result = md;
2064 return 0;
2065}
2066
Mike Snitzera5664da2010-08-12 04:14:01 +01002067/*
2068 * Functions to manage md->type.
2069 * All are required to hold md->type_lock.
2070 */
2071void dm_lock_md_type(struct mapped_device *md)
2072{
2073 mutex_lock(&md->type_lock);
2074}
2075
2076void dm_unlock_md_type(struct mapped_device *md)
2077{
2078 mutex_unlock(&md->type_lock);
2079}
2080
2081void dm_set_md_type(struct mapped_device *md, unsigned type)
2082{
2083 md->type = type;
2084}
2085
2086unsigned dm_get_md_type(struct mapped_device *md)
2087{
2088 return md->type;
2089}
2090
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002091/*
2092 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2093 */
2094static int dm_init_request_based_queue(struct mapped_device *md)
2095{
2096 struct request_queue *q = NULL;
2097
2098 if (md->queue->elevator)
2099 return 1;
2100
2101 /* Fully initialize the queue */
2102 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2103 if (!q)
2104 return 0;
2105
2106 md->queue = q;
2107 md->saved_make_request_fn = md->queue->make_request_fn;
2108 dm_init_md_queue(md);
2109 blk_queue_softirq_done(md->queue, dm_softirq_done);
2110 blk_queue_prep_rq(md->queue, dm_prep_fn);
2111 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002112
2113 elv_register_queue(md->queue);
2114
2115 return 1;
2116}
2117
2118/*
2119 * Setup the DM device's queue based on md's type
2120 */
2121int dm_setup_md_queue(struct mapped_device *md)
2122{
2123 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2124 !dm_init_request_based_queue(md)) {
2125 DMWARN("Cannot initialize queue for request-based mapped device");
2126 return -EINVAL;
2127 }
2128
2129 return 0;
2130}
2131
David Teigland637842c2006-01-06 00:20:00 -08002132static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 unsigned minor = MINOR(dev);
2136
2137 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2138 return NULL;
2139
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002140 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002143 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002144 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002145 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002146 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002147 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002148 goto out;
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002151out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002152 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
David Teigland637842c2006-01-06 00:20:00 -08002154 return md;
2155}
2156
David Teiglandd229a952006-01-06 00:20:01 -08002157struct mapped_device *dm_get_md(dev_t dev)
2158{
2159 struct mapped_device *md = dm_find_md(dev);
2160
2161 if (md)
2162 dm_get(md);
2163
2164 return md;
2165}
2166
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002167void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002168{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002169 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
2172void dm_set_mdptr(struct mapped_device *md, void *ptr)
2173{
2174 md->interface_ptr = ptr;
2175}
2176
2177void dm_get(struct mapped_device *md)
2178{
2179 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002180 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002183const char *dm_device_name(struct mapped_device *md)
2184{
2185 return md->name;
2186}
2187EXPORT_SYMBOL_GPL(dm_device_name);
2188
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002189static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002191 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002193 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002194
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002195 spin_lock(&_minor_lock);
2196 map = dm_get_live_table(md);
2197 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2198 set_bit(DMF_FREEING, &md->flags);
2199 spin_unlock(&_minor_lock);
2200
2201 if (!dm_suspended_md(md)) {
2202 dm_table_presuspend_targets(map);
2203 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002205
2206 /*
2207 * Rare, but there may be I/O requests still going to complete,
2208 * for example. Wait for all references to disappear.
2209 * No one should increment the reference count of the mapped_device,
2210 * after the mapped_device state becomes DMF_FREEING.
2211 */
2212 if (wait)
2213 while (atomic_read(&md->holders))
2214 msleep(1);
2215 else if (atomic_read(&md->holders))
2216 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2217 dm_device_name(md), atomic_read(&md->holders));
2218
2219 dm_sysfs_exit(md);
2220 dm_table_put(map);
2221 dm_table_destroy(__unbind(md));
2222 free_dev(md);
2223}
2224
2225void dm_destroy(struct mapped_device *md)
2226{
2227 __dm_destroy(md, true);
2228}
2229
2230void dm_destroy_immediate(struct mapped_device *md)
2231{
2232 __dm_destroy(md, false);
2233}
2234
2235void dm_put(struct mapped_device *md)
2236{
2237 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
Edward Goggin79eb8852007-05-09 02:32:56 -07002239EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Mikulas Patocka401600d2009-04-02 19:55:38 +01002241static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002242{
2243 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002244 DECLARE_WAITQUEUE(wait, current);
2245
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002246 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002247
2248 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002249 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002250
2251 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002252 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002253 break;
2254
Mikulas Patocka401600d2009-04-02 19:55:38 +01002255 if (interruptible == TASK_INTERRUPTIBLE &&
2256 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002257 r = -EINTR;
2258 break;
2259 }
2260
2261 io_schedule();
2262 }
2263 set_current_state(TASK_RUNNING);
2264
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002265 remove_wait_queue(&md->wait, &wait);
2266
Milan Broz46125c12008-02-08 02:10:30 +00002267 return r;
2268}
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270/*
2271 * Process the deferred bios
2272 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002273static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Mikulas Patockaef208582009-04-02 19:55:38 +01002275 struct mapped_device *md = container_of(work, struct mapped_device,
2276 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002277 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Tejun Heo6a8736d2010-09-08 18:07:00 +02002279 down_read(&md->io_lock);
Mikulas Patockaef208582009-04-02 19:55:38 +01002280
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002281 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002282 spin_lock_irq(&md->deferred_lock);
2283 c = bio_list_pop(&md->deferred);
2284 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002285
Tejun Heo6a8736d2010-09-08 18:07:00 +02002286 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002287 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002288
Tejun Heo6a8736d2010-09-08 18:07:00 +02002289 up_read(&md->io_lock);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002290
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002291 if (dm_request_based(md))
2292 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002293 else
2294 __split_and_process_bio(md, c);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002295
Tejun Heo6a8736d2010-09-08 18:07:00 +02002296 down_read(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002297 }
Milan Broz73d410c2008-02-08 02:10:25 +00002298
Tejun Heo6a8736d2010-09-08 18:07:00 +02002299 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002302static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002303{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002304 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2305 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002306 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002307}
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002310 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002312struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002314 struct dm_table *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002315 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002316 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Daniel Walkere61290a2008-02-08 02:10:08 +00002318 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002321 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002324 r = dm_calculate_queue_limits(table, &limits);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002325 if (r) {
2326 map = ERR_PTR(r);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002327 goto out;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002328 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002329
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002330 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002332out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002333 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002334 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
2337/*
2338 * Functions to lock and unlock any filesystem running on the
2339 * device.
2340 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002341static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002343 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002346
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002347 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002348 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002349 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002350 md->frozen_sb = NULL;
2351 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002352 }
2353
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002354 set_bit(DMF_FROZEN, &md->flags);
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return 0;
2357}
2358
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002359static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002361 if (!test_bit(DMF_FROZEN, &md->flags))
2362 return;
2363
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002364 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002366 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
2369/*
2370 * We need to be able to change a mapping table under a mounted
2371 * filesystem. For example we might want to move some data in
2372 * the background. Before the table can be swapped with
2373 * dm_bind_table, dm_suspend must be called to flush any in
2374 * flight bios and ensure that any further io gets deferred.
2375 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002376/*
2377 * Suspend mechanism in request-based dm.
2378 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002379 * 1. Flush all I/Os by lock_fs() if needed.
2380 * 2. Stop dispatching any I/O by stopping the request_queue.
2381 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002382 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002383 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002384 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002385int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002387 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002388 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002389 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002390 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Daniel Walkere61290a2008-02-08 02:10:08 +00002392 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002393
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002394 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002395 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002396 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002399 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002401 /*
2402 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2403 * This flag is cleared before dm_suspend returns.
2404 */
2405 if (noflush)
2406 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2407
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002408 /* This does not get reverted if there's an error later. */
2409 dm_table_presuspend_targets(map);
2410
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002411 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002412 * Flush I/O to the device.
2413 * Any I/O submitted after lock_fs() may not be flushed.
2414 * noflush takes precedence over do_lockfs.
2415 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002416 */
2417 if (!noflush && do_lockfs) {
2418 r = lock_fs(md);
2419 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002420 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002424 * Here we must make sure that no processes are submitting requests
2425 * to target drivers i.e. no one may be executing
2426 * __split_and_process_bio. This is called from dm_request and
2427 * dm_wq_work.
2428 *
2429 * To get all processes out of __split_and_process_bio in dm_request,
2430 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002431 * __split_and_process_bio from dm_request and quiesce the thread
2432 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2433 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002435 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002436 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002437 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002439 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002440 * Stop md->queue before flushing md->wq in case request-based
2441 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002442 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002443 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002444 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002445
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002446 flush_workqueue(md->wq);
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002449 * At this point no more requests are entering target request routines.
2450 * We call dm_wait_for_completion to wait for all existing requests
2451 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002453 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002455 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002456 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002457 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002458 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002461 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002462 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002463
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002464 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002465 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002466
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002467 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002468 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002469 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002470
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002471 /*
2472 * If dm_wait_for_completion returned 0, the device is completely
2473 * quiescent now. There is no request-processing activity. All new
2474 * requests are being added to md->deferred list.
2475 */
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 set_bit(DMF_SUSPENDED, &md->flags);
2478
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002479 dm_table_postsuspend_targets(map);
2480
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002481out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002483
2484out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002485 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002486 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489int dm_resume(struct mapped_device *md)
2490{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002491 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002492 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Daniel Walkere61290a2008-02-08 02:10:08 +00002494 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002495 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002496 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002497
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002498 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002499 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002500 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Milan Broz8757b772006-10-03 01:15:36 -07002502 r = dm_table_resume_targets(map);
2503 if (r)
2504 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002505
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002506 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002507
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002508 /*
2509 * Flushing deferred I/Os must be done after targets are resumed
2510 * so that mapping of targets can work correctly.
2511 * Request-based dm is queueing the deferred I/Os in its request_queue.
2512 */
2513 if (dm_request_based(md))
2514 start_queue(md->queue);
2515
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002516 unlock_fs(md);
2517
2518 clear_bit(DMF_SUSPENDED, &md->flags);
2519
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002520 r = 0;
2521out:
2522 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002523 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002524
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002525 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
2528/*-----------------------------------------------------------------
2529 * Event notification.
2530 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002531int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002532 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002533{
Milan Broz60935eb2009-06-22 10:12:30 +01002534 char udev_cookie[DM_COOKIE_LENGTH];
2535 char *envp[] = { udev_cookie, NULL };
2536
2537 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002538 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002539 else {
2540 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2541 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002542 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2543 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002544 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002545}
2546
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002547uint32_t dm_next_uevent_seq(struct mapped_device *md)
2548{
2549 return atomic_add_return(1, &md->uevent_seq);
2550}
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552uint32_t dm_get_event_nr(struct mapped_device *md)
2553{
2554 return atomic_read(&md->event_nr);
2555}
2556
2557int dm_wait_event(struct mapped_device *md, int event_nr)
2558{
2559 return wait_event_interruptible(md->eventq,
2560 (event_nr != atomic_read(&md->event_nr)));
2561}
2562
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002563void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2564{
2565 unsigned long flags;
2566
2567 spin_lock_irqsave(&md->uevent_lock, flags);
2568 list_add(elist, &md->uevent_list);
2569 spin_unlock_irqrestore(&md->uevent_lock, flags);
2570}
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572/*
2573 * The gendisk is only valid as long as you have a reference
2574 * count on 'md'.
2575 */
2576struct gendisk *dm_disk(struct mapped_device *md)
2577{
2578 return md->disk;
2579}
2580
Milan Broz784aae72009-01-06 03:05:12 +00002581struct kobject *dm_kobject(struct mapped_device *md)
2582{
2583 return &md->kobj;
2584}
2585
2586/*
2587 * struct mapped_device should not be exported outside of dm.c
2588 * so use this check to verify that kobj is part of md structure
2589 */
2590struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2591{
2592 struct mapped_device *md;
2593
2594 md = container_of(kobj, struct mapped_device, kobj);
2595 if (&md->kobj != kobj)
2596 return NULL;
2597
Milan Broz4d89b7b2009-06-22 10:12:11 +01002598 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002599 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002600 return NULL;
2601
Milan Broz784aae72009-01-06 03:05:12 +00002602 dm_get(md);
2603 return md;
2604}
2605
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002606int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 return test_bit(DMF_SUSPENDED, &md->flags);
2609}
2610
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002611int dm_suspended(struct dm_target *ti)
2612{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002613 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002614}
2615EXPORT_SYMBOL_GPL(dm_suspended);
2616
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002617int dm_noflush_suspending(struct dm_target *ti)
2618{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002619 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002620}
2621EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2622
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002623struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2624{
2625 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2626
2627 if (!pools)
2628 return NULL;
2629
2630 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2631 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2632 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2633 if (!pools->io_pool)
2634 goto free_pools_and_out;
2635
2636 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2637 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2638 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2639 if (!pools->tio_pool)
2640 goto free_io_pool_and_out;
2641
2642 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2643 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2644 if (!pools->bs)
2645 goto free_tio_pool_and_out;
2646
2647 return pools;
2648
2649free_tio_pool_and_out:
2650 mempool_destroy(pools->tio_pool);
2651
2652free_io_pool_and_out:
2653 mempool_destroy(pools->io_pool);
2654
2655free_pools_and_out:
2656 kfree(pools);
2657
2658 return NULL;
2659}
2660
2661void dm_free_md_mempools(struct dm_md_mempools *pools)
2662{
2663 if (!pools)
2664 return;
2665
2666 if (pools->io_pool)
2667 mempool_destroy(pools->io_pool);
2668
2669 if (pools->tio_pool)
2670 mempool_destroy(pools->tio_pool);
2671
2672 if (pools->bs)
2673 bioset_free(pools->bs);
2674
2675 kfree(pools);
2676}
2677
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002678static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .open = dm_blk_open,
2680 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002681 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002682 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 .owner = THIS_MODULE
2684};
2685
2686EXPORT_SYMBOL(dm_get_mapinfo);
2687
2688/*
2689 * module hooks
2690 */
2691module_init(dm_init);
2692module_exit(dm_exit);
2693
2694module_param(major, uint, 0);
2695MODULE_PARM_DESC(major, "The major number of the device mapper");
2696MODULE_DESCRIPTION(DM_NAME " driver");
2697MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2698MODULE_LICENSE("GPL");