blob: 65114e4d9f6500869b5fa287f7fc77edda24a677 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
17#include <linux/buffer_head.h>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020018#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mempool.h>
20#include <linux/slab.h>
21#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080022#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010023#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080024
25#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "core"
28
Milan Broz60935eb2009-06-22 10:12:30 +010029/*
30 * Cookies are numeric values sent with CHANGE and REMOVE
31 * uevents while resuming, removing or renaming the device.
32 */
33#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
34#define DM_COOKIE_LENGTH 24
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static const char *_name = DM_NAME;
37
38static unsigned int major = 0;
39static unsigned int _major = 0;
40
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070041static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000043 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * One of these is allocated per bio.
45 */
46struct dm_io {
47 struct mapped_device *md;
48 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010050 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080051 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010052 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000056 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * One of these is allocated per target within a bio. Hopefully
58 * this will be simplified out one day.
59 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010060struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct dm_io *io;
62 struct dm_target *ti;
63 union map_info info;
64};
65
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000066/*
67 * For request-based dm.
68 * One of these is allocated per request.
69 */
70struct dm_rq_target_io {
71 struct mapped_device *md;
72 struct dm_target *ti;
73 struct request *orig, clone;
74 int error;
75 union map_info info;
76};
77
78/*
79 * For request-based dm.
80 * One of these is allocated per bio.
81 */
82struct dm_rq_clone_bio_info {
83 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010084 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087union map_info *dm_get_mapinfo(struct bio *bio)
88{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070089 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010090 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070091 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010094union map_info *dm_get_rq_mapinfo(struct request *rq)
95{
96 if (rq && rq->end_io_data)
97 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
98 return NULL;
99}
100EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
101
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700102#define MINOR_ALLOCED ((void *)-1)
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Bits for the md->flags field.
106 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100107#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800109#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700110#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700111#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800112#define DMF_NOFLUSH_SUSPENDING 5
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100113#define DMF_QUEUE_IO_TO_THREAD 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Milan Broz304f3f62008-02-08 02:11:17 +0000115/*
116 * Work processed by per-device workqueue.
117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700119 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000120 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 rwlock_t map_lock;
122 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700123 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 unsigned long flags;
126
Jens Axboe165125e2007-07-24 09:28:11 +0200127 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100128 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100129 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100130 struct mutex type_lock;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800133 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 void *interface_ptr;
136
137 /*
138 * A list of ios that arrived while we were suspended.
139 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200140 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100142 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800143 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100144 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /*
Tejun Heod87f4c12010-09-03 11:56:19 +0200147 * An error from the flush request currently being processed.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100148 */
Tejun Heod87f4c12010-09-03 11:56:19 +0200149 int flush_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100150
151 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200152 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000153 */
154 struct workqueue_struct *wq;
155
156 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * The current mapping.
158 */
159 struct dm_table *map;
160
161 /*
162 * io objects are allocated from here.
163 */
164 mempool_t *io_pool;
165 mempool_t *tio_pool;
166
Stefan Bader9faf4002006-10-03 01:15:41 -0700167 struct bio_set *bs;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
170 * Event handling.
171 */
172 atomic_t event_nr;
173 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100174 atomic_t uevent_seq;
175 struct list_head uevent_list;
176 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /*
179 * freeze/thaw support require holding onto a super block
180 */
181 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100182 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800183
184 /* forced geometry settings */
185 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000186
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100187 /* For saving the address of __make_request for request based dm */
188 make_request_fn *saved_make_request_fn;
189
Milan Broz784aae72009-01-06 03:05:12 +0000190 /* sysfs handle */
191 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100192
Tejun Heod87f4c12010-09-03 11:56:19 +0200193 /* zero-length flush that will be cloned and submitted to targets */
194 struct bio flush_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100197/*
198 * For mempools pre-allocation at the table loading time.
199 */
200struct dm_md_mempools {
201 mempool_t *io_pool;
202 mempool_t *tio_pool;
203 struct bio_set *bs;
204};
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800207static struct kmem_cache *_io_cache;
208static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000209static struct kmem_cache *_rq_tio_cache;
210static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int __init local_init(void)
213{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100214 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100217 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100219 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100222 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100223 if (!_tio_cache)
224 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000226 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
227 if (!_rq_tio_cache)
228 goto out_free_tio_cache;
229
230 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
231 if (!_rq_bio_info_cache)
232 goto out_free_rq_tio_cache;
233
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100234 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100235 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000236 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 _major = major;
239 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100240 if (r < 0)
241 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (!_major)
244 _major = r;
245
246 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100247
248out_uevent_exit:
249 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000250out_free_rq_bio_info_cache:
251 kmem_cache_destroy(_rq_bio_info_cache);
252out_free_rq_tio_cache:
253 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100254out_free_tio_cache:
255 kmem_cache_destroy(_tio_cache);
256out_free_io_cache:
257 kmem_cache_destroy(_io_cache);
258
259 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262static void local_exit(void)
263{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000264 kmem_cache_destroy(_rq_bio_info_cache);
265 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 kmem_cache_destroy(_tio_cache);
267 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700268 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100269 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 _major = 0;
272
273 DMINFO("cleaned up");
274}
275
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000276static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 local_init,
278 dm_target_init,
279 dm_linear_init,
280 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000281 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100282 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 dm_interface_init,
284};
285
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000286static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 local_exit,
288 dm_target_exit,
289 dm_linear_exit,
290 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000291 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100292 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 dm_interface_exit,
294};
295
296static int __init dm_init(void)
297{
298 const int count = ARRAY_SIZE(_inits);
299
300 int r, i;
301
302 for (i = 0; i < count; i++) {
303 r = _inits[i]();
304 if (r)
305 goto bad;
306 }
307
308 return 0;
309
310 bad:
311 while (i--)
312 _exits[i]();
313
314 return r;
315}
316
317static void __exit dm_exit(void)
318{
319 int i = ARRAY_SIZE(_exits);
320
321 while (i--)
322 _exits[i]();
323}
324
325/*
326 * Block device functions
327 */
Mike Anderson432a2122009-12-10 23:52:20 +0000328int dm_deleting_md(struct mapped_device *md)
329{
330 return test_bit(DMF_DELETING, &md->flags);
331}
332
Al Virofe5f9f22008-03-02 10:29:31 -0500333static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct mapped_device *md;
336
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200337 lock_kernel();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700338 spin_lock(&_minor_lock);
339
Al Virofe5f9f22008-03-02 10:29:31 -0500340 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700341 if (!md)
342 goto out;
343
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700344 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000345 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700346 md = NULL;
347 goto out;
348 }
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700351 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700352
353out:
354 spin_unlock(&_minor_lock);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200355 unlock_kernel();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700356
357 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Al Virofe5f9f22008-03-02 10:29:31 -0500360static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Al Virofe5f9f22008-03-02 10:29:31 -0500362 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200363
364 lock_kernel();
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700365 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 dm_put(md);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200367 unlock_kernel();
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700372int dm_open_count(struct mapped_device *md)
373{
374 return atomic_read(&md->open_count);
375}
376
377/*
378 * Guarantees nothing is using the device before it's deleted.
379 */
380int dm_lock_for_deletion(struct mapped_device *md)
381{
382 int r = 0;
383
384 spin_lock(&_minor_lock);
385
386 if (dm_open_count(md))
387 r = -EBUSY;
388 else
389 set_bit(DMF_DELETING, &md->flags);
390
391 spin_unlock(&_minor_lock);
392
393 return r;
394}
395
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800396static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
397{
398 struct mapped_device *md = bdev->bd_disk->private_data;
399
400 return dm_get_geometry(md, geo);
401}
402
Al Virofe5f9f22008-03-02 10:29:31 -0500403static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700404 unsigned int cmd, unsigned long arg)
405{
Al Virofe5f9f22008-03-02 10:29:31 -0500406 struct mapped_device *md = bdev->bd_disk->private_data;
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000407 struct dm_table *map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700408 struct dm_target *tgt;
409 int r = -ENOTTY;
410
Milan Brozaa129a22006-10-03 01:15:15 -0700411 if (!map || !dm_table_get_size(map))
412 goto out;
413
414 /* We only support devices that have a single target */
415 if (dm_table_get_num_targets(map) != 1)
416 goto out;
417
418 tgt = dm_table_get_target(map, 0);
419
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000420 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700421 r = -EAGAIN;
422 goto out;
423 }
424
425 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400426 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700427
428out:
429 dm_table_put(map);
430
Milan Brozaa129a22006-10-03 01:15:15 -0700431 return r;
432}
433
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100434static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 return mempool_alloc(md->io_pool, GFP_NOIO);
437}
438
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100439static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 mempool_free(io, md->io_pool);
442}
443
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100444static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 mempool_free(tio, md->tio_pool);
447}
448
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000449static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
450 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100451{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000452 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100453}
454
455static void free_rq_tio(struct dm_rq_target_io *tio)
456{
457 mempool_free(tio, tio->md->tio_pool);
458}
459
460static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
461{
462 return mempool_alloc(md->io_pool, GFP_ATOMIC);
463}
464
465static void free_bio_info(struct dm_rq_clone_bio_info *info)
466{
467 mempool_free(info, info->tio->md->io_pool);
468}
469
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000470static int md_in_flight(struct mapped_device *md)
471{
472 return atomic_read(&md->pending[READ]) +
473 atomic_read(&md->pending[WRITE]);
474}
475
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800476static void start_io_acct(struct dm_io *io)
477{
478 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900479 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200480 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800481
482 io->start_time = jiffies;
483
Tejun Heo074a7ac2008-08-25 19:56:14 +0900484 cpu = part_stat_lock();
485 part_round_stats(cpu, &dm_disk(md)->part0);
486 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200487 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800488}
489
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000490static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800491{
492 struct mapped_device *md = io->md;
493 struct bio *bio = io->bio;
494 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900495 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800496 int rw = bio_data_dir(bio);
497
Tejun Heo074a7ac2008-08-25 19:56:14 +0900498 cpu = part_stat_lock();
499 part_round_stats(cpu, &dm_disk(md)->part0);
500 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
501 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800502
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100503 /*
504 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200505 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100506 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200507 dm_disk(md)->part0.in_flight[rw] = pending =
508 atomic_dec_return(&md->pending[rw]);
509 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800510
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000511 /* nudge anyone waiting on suspend queue */
512 if (!pending)
513 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/*
517 * Add the bio to the list of deferred io.
518 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100519static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700521 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Mikulas Patocka022c2612009-04-02 19:55:39 +0100523 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100525 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Mikulas Patocka92c63902009-04-09 00:27:15 +0100527 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
528 queue_work(md->wq, &md->work);
529
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700530 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/*
534 * Everyone (including functions in this file), should use this
535 * function to access the md->map field, and make sure they call
536 * dm_table_put() when finished.
537 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000538struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100541 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100543 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 t = md->map;
545 if (t)
546 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100547 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 return t;
550}
551
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800552/*
553 * Get the geometry associated with a dm device
554 */
555int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
556{
557 *geo = md->geometry;
558
559 return 0;
560}
561
562/*
563 * Set the geometry of a device.
564 */
565int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
566{
567 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
568
569 if (geo->start > sz) {
570 DMWARN("Start sector is beyond the geometry limits.");
571 return -EINVAL;
572 }
573
574 md->geometry = *geo;
575
576 return 0;
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*-----------------------------------------------------------------
580 * CRUD START:
581 * A more elegant soln is in the works that uses the queue
582 * merge fn, unfortunately there are a couple of changes to
583 * the block layer that I want to make for this. So in the
584 * interests of getting something for people to use I give
585 * you this clearly demarcated crap.
586 *---------------------------------------------------------------*/
587
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800588static int __noflush_suspending(struct mapped_device *md)
589{
590 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
594 * Decrements the number of outstanding ios that a bio has been
595 * cloned into, completing the original io if necc.
596 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800597static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800599 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000600 int io_error;
601 struct bio *bio;
602 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800603
604 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100605 if (unlikely(error)) {
606 spin_lock_irqsave(&io->endio_lock, flags);
607 if (!(io->error > 0 && __noflush_suspending(md)))
608 io->error = error;
609 spin_unlock_irqrestore(&io->endio_lock, flags);
610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800613 if (io->error == DM_ENDIO_REQUEUE) {
614 /*
615 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800616 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100617 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100618 if (__noflush_suspending(md)) {
Tejun Heod87f4c12010-09-03 11:56:19 +0200619 if (!(io->bio->bi_rw & REQ_FLUSH))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100620 bio_list_add_head(&md->deferred,
621 io->bio);
622 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800623 /* noflush suspend was interrupted. */
624 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100625 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800626 }
627
Milan Brozb35f8ca2009-03-16 17:44:36 +0000628 io_error = io->error;
629 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100630
Tejun Heod87f4c12010-09-03 11:56:19 +0200631 if (bio->bi_rw & REQ_FLUSH) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100632 /*
Tejun Heod87f4c12010-09-03 11:56:19 +0200633 * There can be just one flush request so we use
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100634 * a per-device variable for error reporting.
635 * Note that you can't touch the bio after end_io_acct
636 */
Tejun Heod87f4c12010-09-03 11:56:19 +0200637 if (!md->flush_error)
638 md->flush_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100639 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000640 free_io(md, io);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100641 } else {
642 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000643 free_io(md, io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000644
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100645 if (io_error != DM_ENDIO_REQUEUE) {
646 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000647
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100648 bio_endio(bio, io_error);
649 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652}
653
NeilBrown6712ecf2007-09-27 12:47:43 +0200654static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100657 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000658 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700659 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 dm_endio_fn endio = tio->ti->type->end_io;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
663 error = -EIO;
664
665 if (endio) {
666 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800667 if (r < 0 || r == DM_ENDIO_REQUEUE)
668 /*
669 * error and requeue request are handled
670 * in dec_pending().
671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800673 else if (r == DM_ENDIO_INCOMPLETE)
674 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200675 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800676 else if (r) {
677 DMWARN("unimplemented target endio return value: %d", r);
678 BUG();
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Stefan Bader9faf4002006-10-03 01:15:41 -0700682 /*
683 * Store md for cleanup instead of tio which is about to get freed.
684 */
685 bio->bi_private = md->bs;
686
Stefan Bader9faf4002006-10-03 01:15:41 -0700687 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000688 bio_put(bio);
689 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100692/*
693 * Partial completion handling for request-based dm
694 */
695static void end_clone_bio(struct bio *clone, int error)
696{
697 struct dm_rq_clone_bio_info *info = clone->bi_private;
698 struct dm_rq_target_io *tio = info->tio;
699 struct bio *bio = info->orig;
700 unsigned int nr_bytes = info->orig->bi_size;
701
702 bio_put(clone);
703
704 if (tio->error)
705 /*
706 * An error has already been detected on the request.
707 * Once error occurred, just let clone->end_io() handle
708 * the remainder.
709 */
710 return;
711 else if (error) {
712 /*
713 * Don't notice the error to the upper layer yet.
714 * The error handling decision is made by the target driver,
715 * when the request is completed.
716 */
717 tio->error = error;
718 return;
719 }
720
721 /*
722 * I/O for the bio successfully completed.
723 * Notice the data completion to the upper layer.
724 */
725
726 /*
727 * bios are processed from the head of the list.
728 * So the completing bio should always be rq->bio.
729 * If it's not, something wrong is happening.
730 */
731 if (tio->orig->bio != bio)
732 DMERR("bio completion is going in the middle of the request");
733
734 /*
735 * Update the original request.
736 * Do not use blk_end_request() here, because it may complete
737 * the original request before the clone, and break the ordering.
738 */
739 blk_update_request(tio->orig, 0, nr_bytes);
740}
741
742/*
743 * Don't touch any member of the md after calling this function because
744 * the md may be freed in dm_put() at the end of this function.
745 * Or do dm_get() before calling this function and dm_put() later.
746 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000747static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100748{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000749 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100750
751 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000752 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100753 wake_up(&md->wait);
754
755 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000756 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100757
758 /*
759 * dm_put() must be at the end of this function. See the comment above
760 */
761 dm_put(md);
762}
763
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100764static void free_rq_clone(struct request *clone)
765{
766 struct dm_rq_target_io *tio = clone->end_io_data;
767
768 blk_rq_unprep_clone(clone);
769 free_rq_tio(tio);
770}
771
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000772/*
773 * Complete the clone and the original request.
774 * Must be called without queue lock.
775 */
776static void dm_end_request(struct request *clone, int error)
777{
778 int rw = rq_data_dir(clone);
779 struct dm_rq_target_io *tio = clone->end_io_data;
780 struct mapped_device *md = tio->md;
781 struct request *rq = tio->orig;
782
Tejun Heo29e40132010-09-08 18:07:00 +0200783 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000784 rq->errors = clone->errors;
785 rq->resid_len = clone->resid_len;
786
787 if (rq->sense)
788 /*
789 * We are using the sense buffer of the original
790 * request.
791 * So setting the length of the sense data is enough.
792 */
793 rq->sense_len = clone->sense_len;
794 }
795
796 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200797 blk_end_request_all(rq, error);
798 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000799}
800
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100801static void dm_unprep_request(struct request *rq)
802{
803 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100804
805 rq->special = NULL;
806 rq->cmd_flags &= ~REQ_DONTPREP;
807
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100808 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100809}
810
811/*
812 * Requeue the original request of a clone.
813 */
814void dm_requeue_unmapped_request(struct request *clone)
815{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000816 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100817 struct dm_rq_target_io *tio = clone->end_io_data;
818 struct mapped_device *md = tio->md;
819 struct request *rq = tio->orig;
820 struct request_queue *q = rq->q;
821 unsigned long flags;
822
823 dm_unprep_request(rq);
824
825 spin_lock_irqsave(q->queue_lock, flags);
826 if (elv_queue_empty(q))
827 blk_plug_device(q);
828 blk_requeue_request(q, rq);
829 spin_unlock_irqrestore(q->queue_lock, flags);
830
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000831 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100832}
833EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
834
835static void __stop_queue(struct request_queue *q)
836{
837 blk_stop_queue(q);
838}
839
840static void stop_queue(struct request_queue *q)
841{
842 unsigned long flags;
843
844 spin_lock_irqsave(q->queue_lock, flags);
845 __stop_queue(q);
846 spin_unlock_irqrestore(q->queue_lock, flags);
847}
848
849static void __start_queue(struct request_queue *q)
850{
851 if (blk_queue_stopped(q))
852 blk_start_queue(q);
853}
854
855static void start_queue(struct request_queue *q)
856{
857 unsigned long flags;
858
859 spin_lock_irqsave(q->queue_lock, flags);
860 __start_queue(q);
861 spin_unlock_irqrestore(q->queue_lock, flags);
862}
863
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000864static void dm_done(struct request *clone, int error, bool mapped)
865{
866 int r = error;
867 struct dm_rq_target_io *tio = clone->end_io_data;
868 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
869
870 if (mapped && rq_end_io)
871 r = rq_end_io(tio->ti, clone, error, &tio->info);
872
873 if (r <= 0)
874 /* The target wants to complete the I/O */
875 dm_end_request(clone, r);
876 else if (r == DM_ENDIO_INCOMPLETE)
877 /* The target will handle the I/O */
878 return;
879 else if (r == DM_ENDIO_REQUEUE)
880 /* The target wants to requeue the I/O */
881 dm_requeue_unmapped_request(clone);
882 else {
883 DMWARN("unimplemented target endio return value: %d", r);
884 BUG();
885 }
886}
887
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100888/*
889 * Request completion handler for request-based dm
890 */
891static void dm_softirq_done(struct request *rq)
892{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000893 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100894 struct request *clone = rq->completion_data;
895 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100896
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000897 if (rq->cmd_flags & REQ_FAILED)
898 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100899
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000900 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100901}
902
903/*
904 * Complete the clone and the original request with the error status
905 * through softirq context.
906 */
907static void dm_complete_request(struct request *clone, int error)
908{
909 struct dm_rq_target_io *tio = clone->end_io_data;
910 struct request *rq = tio->orig;
911
912 tio->error = error;
913 rq->completion_data = clone;
914 blk_complete_request(rq);
915}
916
917/*
918 * Complete the not-mapped clone and the original request with the error status
919 * through softirq context.
920 * Target's rq_end_io() function isn't called.
921 * This may be used when the target's map_rq() function fails.
922 */
923void dm_kill_unmapped_request(struct request *clone, int error)
924{
925 struct dm_rq_target_io *tio = clone->end_io_data;
926 struct request *rq = tio->orig;
927
928 rq->cmd_flags |= REQ_FAILED;
929 dm_complete_request(clone, error);
930}
931EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
932
933/*
934 * Called with the queue lock held
935 */
936static void end_clone_request(struct request *clone, int error)
937{
938 /*
939 * For just cleaning up the information of the queue in which
940 * the clone was dispatched.
941 * The clone is *NOT* freed actually here because it is alloced from
942 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
943 */
944 __blk_put_request(clone->q, clone);
945
946 /*
947 * Actual request completion is done in a softirq context which doesn't
948 * hold the queue lock. Otherwise, deadlock could occur because:
949 * - another request may be submitted by the upper level driver
950 * of the stacking during the completion
951 * - the submission which requires queue lock may be done
952 * against this queue
953 */
954 dm_complete_request(clone, error);
955}
956
Mike Snitzer56a67df2010-08-12 04:14:10 +0100957/*
958 * Return maximum size of I/O possible at the supplied sector up to the current
959 * target boundary.
960 */
961static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100963 sector_t target_offset = dm_target_offset(ti, sector);
964
965 return ti->len - target_offset;
966}
967
968static sector_t max_io_len(sector_t sector, struct dm_target *ti)
969{
970 sector_t len = max_io_len_target_boundary(sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /*
973 * Does the target need to split even further ?
974 */
975 if (ti->split_io) {
976 sector_t boundary;
Mike Snitzer56a67df2010-08-12 04:14:10 +0100977 sector_t offset = dm_target_offset(ti, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
979 - offset;
980 if (len > boundary)
981 len = boundary;
982 }
983
984 return len;
985}
986
987static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100988 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100991 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700992 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 clone->bi_end_io = clone_endio;
995 clone->bi_private = tio;
996
997 /*
998 * Map the clone. If r == 0 we don't need to do
999 * anything, the target has assumed ownership of
1000 * this io.
1001 */
1002 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001003 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001005 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001007
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001008 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001009 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001012 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1013 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001014 md = tio->io->md;
1015 dec_pending(tio->io, r);
1016 /*
1017 * Store bio_set for cleanup.
1018 */
1019 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001021 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001022 } else if (r) {
1023 DMWARN("unimplemented target map return value: %d", r);
1024 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026}
1027
1028struct clone_info {
1029 struct mapped_device *md;
1030 struct dm_table *map;
1031 struct bio *bio;
1032 struct dm_io *io;
1033 sector_t sector;
1034 sector_t sector_count;
1035 unsigned short idx;
1036};
1037
Peter Osterlund36763472005-09-06 15:16:42 -07001038static void dm_bio_destructor(struct bio *bio)
1039{
Stefan Bader9faf4002006-10-03 01:15:41 -07001040 struct bio_set *bs = bio->bi_private;
1041
1042 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001043}
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001046 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
1048static struct bio *split_bvec(struct bio *bio, sector_t sector,
1049 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001050 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct bio *clone;
1053 struct bio_vec *bv = bio->bi_io_vec + idx;
1054
Stefan Bader9faf4002006-10-03 01:15:41 -07001055 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001056 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 *clone->bi_io_vec = *bv;
1058
1059 clone->bi_sector = sector;
1060 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001061 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 clone->bi_vcnt = 1;
1063 clone->bi_size = to_bytes(len);
1064 clone->bi_io_vec->bv_offset = offset;
1065 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001066 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Martin K. Petersen9c470082009-04-09 00:27:12 +01001068 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001069 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001070 bio_integrity_trim(clone,
1071 bio_sector_offset(bio, idx, offset), len);
1072 }
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return clone;
1075}
1076
1077/*
1078 * Creates a bio that consists of range of complete bvecs.
1079 */
1080static struct bio *clone_bio(struct bio *bio, sector_t sector,
1081 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001082 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 struct bio *clone;
1085
Stefan Bader9faf4002006-10-03 01:15:41 -07001086 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1087 __bio_clone(clone, bio);
1088 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 clone->bi_sector = sector;
1090 clone->bi_idx = idx;
1091 clone->bi_vcnt = idx + bv_count;
1092 clone->bi_size = to_bytes(len);
1093 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1094
Martin K. Petersen9c470082009-04-09 00:27:12 +01001095 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001096 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001097
1098 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1099 bio_integrity_trim(clone,
1100 bio_sector_offset(bio, idx, 0), len);
1101 }
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return clone;
1104}
1105
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001106static struct dm_target_io *alloc_tio(struct clone_info *ci,
1107 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001108{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001109 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001110
1111 tio->io = ci->io;
1112 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001113 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001114
1115 return tio;
1116}
1117
Mike Snitzer06a426c2010-08-12 04:14:09 +01001118static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001119 unsigned request_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001120{
1121 struct dm_target_io *tio = alloc_tio(ci, ti);
1122 struct bio *clone;
1123
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001124 tio->info.target_request_nr = request_nr;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001125
Mike Snitzer06a426c2010-08-12 04:14:09 +01001126 /*
1127 * Discard requests require the bio's inline iovecs be initialized.
1128 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1129 * and discard, so no need for concern about wasted bvec allocations.
1130 */
1131 clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001132 __bio_clone(clone, ci->bio);
1133 clone->bi_destructor = dm_bio_destructor;
Mike Snitzera79245b2010-08-12 04:14:24 +01001134 if (len) {
1135 clone->bi_sector = ci->sector;
1136 clone->bi_size = to_bytes(len);
1137 }
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001138
1139 __map_bio(ti, clone, tio);
1140}
1141
Mike Snitzer06a426c2010-08-12 04:14:09 +01001142static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001143 unsigned num_requests, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001144{
1145 unsigned request_nr;
1146
1147 for (request_nr = 0; request_nr < num_requests; request_nr++)
Mike Snitzera79245b2010-08-12 04:14:24 +01001148 __issue_target_request(ci, ti, request_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001149}
1150
Tejun Heod87f4c12010-09-03 11:56:19 +02001151static int __clone_and_map_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001152{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001153 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001154 struct dm_target *ti;
1155
1156 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mike Snitzera79245b2010-08-12 04:14:24 +01001157 __issue_target_requests(ci, ti, ti->num_flush_requests, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001158
1159 ci->sector_count = 0;
1160
1161 return 0;
1162}
1163
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001164/*
1165 * Perform all io with a single clone.
1166 */
1167static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
1168{
1169 struct bio *clone, *bio = ci->bio;
1170 struct dm_target_io *tio;
1171
1172 tio = alloc_tio(ci, ti);
1173 clone = clone_bio(bio, ci->sector, ci->idx,
1174 bio->bi_vcnt - ci->idx, ci->sector_count,
1175 ci->md->bs);
1176 __map_bio(ti, clone, tio);
1177 ci->sector_count = 0;
1178}
1179
1180static int __clone_and_map_discard(struct clone_info *ci)
1181{
1182 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001183 sector_t len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001184
Mike Snitzera79245b2010-08-12 04:14:24 +01001185 do {
1186 ti = dm_table_find_target(ci->map, ci->sector);
1187 if (!dm_target_is_valid(ti))
1188 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001189
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001190 /*
Mike Snitzera79245b2010-08-12 04:14:24 +01001191 * Even though the device advertised discard support,
1192 * reconfiguration might have changed that since the
1193 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001194 */
Mike Snitzera79245b2010-08-12 04:14:24 +01001195 if (!ti->num_discard_requests)
1196 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001197
Mike Snitzera79245b2010-08-12 04:14:24 +01001198 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001199
Mike Snitzera79245b2010-08-12 04:14:24 +01001200 __issue_target_requests(ci, ti, ti->num_discard_requests, len);
1201
1202 ci->sector += len;
1203 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001204
1205 return 0;
1206}
1207
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001208static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001211 struct dm_target *ti;
1212 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001213 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001215 if (unlikely(bio->bi_rw & REQ_DISCARD))
1216 return __clone_and_map_discard(ci);
1217
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001218 ti = dm_table_find_target(ci->map, ci->sector);
1219 if (!dm_target_is_valid(ti))
1220 return -EIO;
1221
Mike Snitzer56a67df2010-08-12 04:14:10 +01001222 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (ci->sector_count <= max) {
1225 /*
1226 * Optimise for the simple case where we can do all of
1227 * the remaining io with a single clone.
1228 */
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001229 __clone_and_map_simple(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1232 /*
1233 * There are some bvecs that don't span targets.
1234 * Do as many of these as possible.
1235 */
1236 int i;
1237 sector_t remaining = max;
1238 sector_t bv_len;
1239
1240 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1241 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1242
1243 if (bv_len > remaining)
1244 break;
1245
1246 remaining -= bv_len;
1247 len += bv_len;
1248 }
1249
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001250 tio = alloc_tio(ci, ti);
Stefan Bader9faf4002006-10-03 01:15:41 -07001251 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1252 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 __map_bio(ti, clone, tio);
1254
1255 ci->sector += len;
1256 ci->sector_count -= len;
1257 ci->idx = i;
1258
1259 } else {
1260 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001261 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
1263 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001264 sector_t remaining = to_sector(bv->bv_len);
1265 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001267 do {
1268 if (offset) {
1269 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001270 if (!dm_target_is_valid(ti))
1271 return -EIO;
1272
Mike Snitzer56a67df2010-08-12 04:14:10 +01001273 max = max_io_len(ci->sector, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001276 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001278 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001279 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001280 bv->bv_offset + offset, len,
1281 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001282
1283 __map_bio(ti, clone, tio);
1284
1285 ci->sector += len;
1286 ci->sector_count -= len;
1287 offset += to_bytes(len);
1288 } while (remaining -= len);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 ci->idx++;
1291 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001292
1293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001297 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001299static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001302 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001304 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001305 if (unlikely(!ci.map)) {
Tejun Heod87f4c12010-09-03 11:56:19 +02001306 if (!(bio->bi_rw & REQ_FLUSH))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001307 bio_io_error(bio);
1308 else
Tejun Heod87f4c12010-09-03 11:56:19 +02001309 if (!md->flush_error)
1310 md->flush_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001311 return;
1312 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 ci.md = md;
1315 ci.bio = bio;
1316 ci.io = alloc_io(md);
1317 ci.io->error = 0;
1318 atomic_set(&ci.io->io_count, 1);
1319 ci.io->bio = bio;
1320 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001321 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 ci.sector = bio->bi_sector;
Tejun Heod87f4c12010-09-03 11:56:19 +02001323 if (!(bio->bi_rw & REQ_FLUSH))
1324 ci.sector_count = bio_sectors(bio);
1325 else {
1326 /* all FLUSH bio's reaching here should be empty */
1327 WARN_ON_ONCE(bio_has_data(bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001328 ci.sector_count = 1;
Tejun Heod87f4c12010-09-03 11:56:19 +02001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ci.idx = bio->bi_idx;
1331
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001332 start_io_acct(ci.io);
Tejun Heod87f4c12010-09-03 11:56:19 +02001333 while (ci.sector_count && !error) {
1334 if (!(bio->bi_rw & REQ_FLUSH))
1335 error = __clone_and_map(&ci);
1336 else
1337 error = __clone_and_map_flush(&ci);
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001341 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 dm_table_put(ci.map);
1343}
1344/*-----------------------------------------------------------------
1345 * CRUD END
1346 *---------------------------------------------------------------*/
1347
Milan Brozf6fccb12008-07-21 12:00:37 +01001348static int dm_merge_bvec(struct request_queue *q,
1349 struct bvec_merge_data *bvm,
1350 struct bio_vec *biovec)
1351{
1352 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001353 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001354 struct dm_target *ti;
1355 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001356 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001357
1358 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001359 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001360
1361 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001362 if (!dm_target_is_valid(ti))
1363 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001364
1365 /*
1366 * Find maximum amount of I/O that won't need splitting
1367 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001368 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001369 (sector_t) BIO_MAX_SECTORS);
1370 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1371 if (max_size < 0)
1372 max_size = 0;
1373
1374 /*
1375 * merge_bvec_fn() returns number of bytes
1376 * it can accept at this offset
1377 * max is precomputed maximal io size
1378 */
1379 if (max_size && ti->type->merge)
1380 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001381 /*
1382 * If the target doesn't support merge method and some of the devices
1383 * provided their merge_bvec method (we know this by looking at
1384 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1385 * entries. So always set max_size to 0, and the code below allows
1386 * just one page.
1387 */
1388 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1389
1390 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001391
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001392out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001393 dm_table_put(map);
1394
1395out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001396 /*
1397 * Always allow an entire first page
1398 */
1399 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1400 max_size = biovec->bv_len;
1401
Milan Brozf6fccb12008-07-21 12:00:37 +01001402 return max_size;
1403}
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405/*
1406 * The request function that just remaps the bio built up by
1407 * dm_merge_bvec.
1408 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001409static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Kevin Corry12f03a42006-02-01 03:04:52 -08001411 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001413 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001415 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Tejun Heo074a7ac2008-08-25 19:56:14 +09001417 cpu = part_stat_lock();
1418 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1419 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1420 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /*
Tejun Heod87f4c12010-09-03 11:56:19 +02001423 * If we're suspended or the thread is processing flushes
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001424 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001426 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Tejun Heod87f4c12010-09-03 11:56:19 +02001427 (bio->bi_rw & REQ_FLUSH)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001428 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001430 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1431 bio_rw(bio) == READA) {
1432 bio_io_error(bio);
1433 return 0;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Mikulas Patocka92c63902009-04-09 00:27:15 +01001436 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Mikulas Patocka92c63902009-04-09 00:27:15 +01001438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001441 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001442 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001446static int dm_make_request(struct request_queue *q, struct bio *bio)
1447{
1448 struct mapped_device *md = q->queuedata;
1449
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001450 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1451}
1452
1453static int dm_request_based(struct mapped_device *md)
1454{
1455 return blk_queue_stackable(md->queue);
1456}
1457
1458static int dm_request(struct request_queue *q, struct bio *bio)
1459{
1460 struct mapped_device *md = q->queuedata;
1461
1462 if (dm_request_based(md))
1463 return dm_make_request(q, bio);
1464
1465 return _dm_request(q, bio);
1466}
1467
1468void dm_dispatch_request(struct request *rq)
1469{
1470 int r;
1471
1472 if (blk_queue_io_stat(rq->q))
1473 rq->cmd_flags |= REQ_IO_STAT;
1474
1475 rq->start_time = jiffies;
1476 r = blk_insert_cloned_request(rq->q, rq);
1477 if (r)
1478 dm_complete_request(rq, r);
1479}
1480EXPORT_SYMBOL_GPL(dm_dispatch_request);
1481
1482static void dm_rq_bio_destructor(struct bio *bio)
1483{
1484 struct dm_rq_clone_bio_info *info = bio->bi_private;
1485 struct mapped_device *md = info->tio->md;
1486
1487 free_bio_info(info);
1488 bio_free(bio, md->bs);
1489}
1490
1491static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1492 void *data)
1493{
1494 struct dm_rq_target_io *tio = data;
1495 struct mapped_device *md = tio->md;
1496 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1497
1498 if (!info)
1499 return -ENOMEM;
1500
1501 info->orig = bio_orig;
1502 info->tio = tio;
1503 bio->bi_end_io = end_clone_bio;
1504 bio->bi_private = info;
1505 bio->bi_destructor = dm_rq_bio_destructor;
1506
1507 return 0;
1508}
1509
1510static int setup_clone(struct request *clone, struct request *rq,
1511 struct dm_rq_target_io *tio)
1512{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001513 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001514
Tejun Heo29e40132010-09-08 18:07:00 +02001515 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1516 dm_rq_bio_constructor, tio);
1517 if (r)
1518 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001519
Tejun Heo29e40132010-09-08 18:07:00 +02001520 clone->cmd = rq->cmd;
1521 clone->cmd_len = rq->cmd_len;
1522 clone->sense = rq->sense;
1523 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001524 clone->end_io = end_clone_request;
1525 clone->end_io_data = tio;
1526
1527 return 0;
1528}
1529
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001530static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1531 gfp_t gfp_mask)
1532{
1533 struct request *clone;
1534 struct dm_rq_target_io *tio;
1535
1536 tio = alloc_rq_tio(md, gfp_mask);
1537 if (!tio)
1538 return NULL;
1539
1540 tio->md = md;
1541 tio->ti = NULL;
1542 tio->orig = rq;
1543 tio->error = 0;
1544 memset(&tio->info, 0, sizeof(tio->info));
1545
1546 clone = &tio->clone;
1547 if (setup_clone(clone, rq, tio)) {
1548 /* -ENOMEM */
1549 free_rq_tio(tio);
1550 return NULL;
1551 }
1552
1553 return clone;
1554}
1555
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001556/*
1557 * Called with the queue lock held.
1558 */
1559static int dm_prep_fn(struct request_queue *q, struct request *rq)
1560{
1561 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001562 struct request *clone;
1563
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001564 if (unlikely(rq->special)) {
1565 DMWARN("Already has something in rq->special.");
1566 return BLKPREP_KILL;
1567 }
1568
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001569 clone = clone_rq(rq, md, GFP_ATOMIC);
1570 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001571 return BLKPREP_DEFER;
1572
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001573 rq->special = clone;
1574 rq->cmd_flags |= REQ_DONTPREP;
1575
1576 return BLKPREP_OK;
1577}
1578
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001579/*
1580 * Returns:
1581 * 0 : the request has been processed (not requeued)
1582 * !0 : the request has been requeued
1583 */
1584static int map_request(struct dm_target *ti, struct request *clone,
1585 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001586{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001587 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001588 struct dm_rq_target_io *tio = clone->end_io_data;
1589
1590 /*
1591 * Hold the md reference here for the in-flight I/O.
1592 * We can't rely on the reference count by device opener,
1593 * because the device may be closed during the request completion
1594 * when all bios are completed.
1595 * See the comment in rq_completed() too.
1596 */
1597 dm_get(md);
1598
1599 tio->ti = ti;
1600 r = ti->type->map_rq(ti, clone, &tio->info);
1601 switch (r) {
1602 case DM_MAPIO_SUBMITTED:
1603 /* The target has taken the I/O to submit by itself later */
1604 break;
1605 case DM_MAPIO_REMAPPED:
1606 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001607 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1608 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001609 dm_dispatch_request(clone);
1610 break;
1611 case DM_MAPIO_REQUEUE:
1612 /* The target wants to requeue the I/O */
1613 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001614 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001615 break;
1616 default:
1617 if (r > 0) {
1618 DMWARN("unimplemented target map return value: %d", r);
1619 BUG();
1620 }
1621
1622 /* The target wants to complete the I/O */
1623 dm_kill_unmapped_request(clone, r);
1624 break;
1625 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001626
1627 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001628}
1629
1630/*
1631 * q->request_fn for request-based dm.
1632 * Called with the queue lock held.
1633 */
1634static void dm_request_fn(struct request_queue *q)
1635{
1636 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001637 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001638 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001639 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001640 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001641
1642 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001643 * For suspend, check blk_queue_stopped() and increment
1644 * ->pending within a single queue_lock not to increment the
1645 * number of in-flight I/Os after the queue is stopped in
1646 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001647 */
1648 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1649 rq = blk_peek_request(q);
1650 if (!rq)
1651 goto plug_and_out;
1652
Tejun Heo29e40132010-09-08 18:07:00 +02001653 /* always use block 0 to find the target for flushes for now */
1654 pos = 0;
1655 if (!(rq->cmd_flags & REQ_FLUSH))
1656 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001657
Tejun Heo29e40132010-09-08 18:07:00 +02001658 ti = dm_table_find_target(map, pos);
1659 BUG_ON(!dm_target_is_valid(ti));
1660
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001661 if (ti->type->busy && ti->type->busy(ti))
1662 goto plug_and_out;
1663
1664 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001665 clone = rq->special;
1666 atomic_inc(&md->pending[rq_data_dir(clone)]);
1667
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001668 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001669 if (map_request(ti, clone, md))
1670 goto requeued;
1671
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001672 spin_lock_irq(q->queue_lock);
1673 }
1674
1675 goto out;
1676
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001677requeued:
1678 spin_lock_irq(q->queue_lock);
1679
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001680plug_and_out:
1681 if (!elv_queue_empty(q))
1682 /* Some requests still remain, retry later */
1683 blk_plug_device(q);
1684
1685out:
1686 dm_table_put(map);
1687
1688 return;
1689}
1690
1691int dm_underlying_device_busy(struct request_queue *q)
1692{
1693 return blk_lld_busy(q);
1694}
1695EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1696
1697static int dm_lld_busy(struct request_queue *q)
1698{
1699 int r;
1700 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001701 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001702
1703 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1704 r = 1;
1705 else
1706 r = dm_table_any_busy_target(map);
1707
1708 dm_table_put(map);
1709
1710 return r;
1711}
1712
Jens Axboe165125e2007-07-24 09:28:11 +02001713static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001716 struct dm_table *map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001719 if (dm_request_based(md))
1720 generic_unplug_device(q);
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 dm_table_unplug_all(map);
1723 dm_table_put(map);
1724 }
1725}
1726
1727static int dm_any_congested(void *congested_data, int bdi_bits)
1728{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001729 int r = bdi_bits;
1730 struct mapped_device *md = congested_data;
1731 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001733 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001734 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001735 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001736 /*
1737 * Request-based dm cares about only own queue for
1738 * the query about congestion status of request_queue
1739 */
1740 if (dm_request_based(md))
1741 r = md->queue->backing_dev_info.state &
1742 bdi_bits;
1743 else
1744 r = dm_table_any_congested(map, bdi_bits);
1745
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001746 dm_table_put(map);
1747 }
1748 }
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return r;
1751}
1752
1753/*-----------------------------------------------------------------
1754 * An IDR is used to keep track of allocated minor numbers.
1755 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756static DEFINE_IDR(_minor_idr);
1757
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001758static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001760 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001762 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765/*
1766 * See if the device with a specific minor # is free.
1767 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001768static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 int r, m;
1771
1772 if (minor >= (1 << MINORBITS))
1773 return -EINVAL;
1774
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001775 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1776 if (!r)
1777 return -ENOMEM;
1778
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001779 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 if (idr_find(&_minor_idr, minor)) {
1782 r = -EBUSY;
1783 goto out;
1784 }
1785
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001786 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001787 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 if (m != minor) {
1791 idr_remove(&_minor_idr, m);
1792 r = -EBUSY;
1793 goto out;
1794 }
1795
1796out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001797 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return r;
1799}
1800
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001801static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001803 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001806 if (!r)
1807 return -ENOMEM;
1808
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001809 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001811 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001812 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 if (m >= (1 << MINORBITS)) {
1816 idr_remove(&_minor_idr, m);
1817 r = -ENOSPC;
1818 goto out;
1819 }
1820
1821 *minor = m;
1822
1823out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001824 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return r;
1826}
1827
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001828static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Mikulas Patocka53d59142009-04-02 19:55:37 +01001830static void dm_wq_work(struct work_struct *work);
1831
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001832static void dm_init_md_queue(struct mapped_device *md)
1833{
1834 /*
1835 * Request-based dm devices cannot be stacked on top of bio-based dm
1836 * devices. The type of this dm device has not been decided yet.
1837 * The type is decided at the first table loading time.
1838 * To prevent problematic device stacking, clear the queue flag
1839 * for request stacking support until then.
1840 *
1841 * This queue is new, so no concurrency on the queue_flags.
1842 */
1843 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1844
1845 md->queue->queuedata = md;
1846 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1847 md->queue->backing_dev_info.congested_data = md;
1848 blk_queue_make_request(md->queue, dm_request);
1849 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1850 md->queue->unplug_fn = dm_unplug_all;
1851 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Tejun Heod87f4c12010-09-03 11:56:19 +02001852 blk_queue_flush(md->queue, REQ_FLUSH | REQ_FUA);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/*
1856 * Allocate and initialise a blank device with a given minor.
1857 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001858static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001861 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001862 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 if (!md) {
1865 DMWARN("unable to allocate device, out of memory.");
1866 return NULL;
1867 }
1868
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001869 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001870 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001873 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001874 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001875 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001876 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001878 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Mike Snitzera5664da2010-08-12 04:14:01 +01001880 md->type = DM_TYPE_NONE;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001881 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001882 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001883 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001884 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 rwlock_init(&md->map_lock);
1886 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001887 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001889 atomic_set(&md->uevent_seq, 0);
1890 INIT_LIST_HEAD(&md->uevent_list);
1891 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001893 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001895 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001897 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 md->disk = alloc_disk(1);
1900 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001901 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001903 atomic_set(&md->pending[0], 0);
1904 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001905 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001906 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001907 init_waitqueue_head(&md->eventq);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 md->disk->major = _major;
1910 md->disk->first_minor = minor;
1911 md->disk->fops = &dm_blk_dops;
1912 md->disk->queue = md->queue;
1913 md->disk->private_data = md;
1914 sprintf(md->disk->disk_name, "dm-%d", minor);
1915 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001916 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Milan Broz304f3f62008-02-08 02:11:17 +00001918 md->wq = create_singlethread_workqueue("kdmflush");
1919 if (!md->wq)
1920 goto bad_thread;
1921
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001922 md->bdev = bdget_disk(md->disk, 0);
1923 if (!md->bdev)
1924 goto bad_bdev;
1925
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001926 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001927 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001928 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001929 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001930
1931 BUG_ON(old_md != MINOR_ALLOCED);
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return md;
1934
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001935bad_bdev:
1936 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001937bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001938 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001939 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001940bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001941 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001942bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001944bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001945 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001946bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 kfree(md);
1948 return NULL;
1949}
1950
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001951static void unlock_fs(struct mapped_device *md);
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953static void free_dev(struct mapped_device *md)
1954{
Tejun Heof331c022008-09-03 09:01:48 +02001955 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001956
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001957 unlock_fs(md);
1958 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001959 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001960 if (md->tio_pool)
1961 mempool_destroy(md->tio_pool);
1962 if (md->io_pool)
1963 mempool_destroy(md->io_pool);
1964 if (md->bs)
1965 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001966 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001968 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001969
1970 spin_lock(&_minor_lock);
1971 md->disk->private_data = NULL;
1972 spin_unlock(&_minor_lock);
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001975 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001976 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 kfree(md);
1978}
1979
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001980static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1981{
1982 struct dm_md_mempools *p;
1983
1984 if (md->io_pool && md->tio_pool && md->bs)
1985 /* the md already has necessary mempools */
1986 goto out;
1987
1988 p = dm_table_get_md_mempools(t);
1989 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1990
1991 md->io_pool = p->io_pool;
1992 p->io_pool = NULL;
1993 md->tio_pool = p->tio_pool;
1994 p->tio_pool = NULL;
1995 md->bs = p->bs;
1996 p->bs = NULL;
1997
1998out:
1999 /* mempool bind completed, now no need any mempools in the table */
2000 dm_table_free_md_mempools(t);
2001}
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003/*
2004 * Bind a table to the device.
2005 */
2006static void event_callback(void *context)
2007{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002008 unsigned long flags;
2009 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct mapped_device *md = (struct mapped_device *) context;
2011
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002012 spin_lock_irqsave(&md->uevent_lock, flags);
2013 list_splice_init(&md->uevent_list, &uevents);
2014 spin_unlock_irqrestore(&md->uevent_lock, flags);
2015
Tejun Heoed9e1982008-08-25 19:56:05 +09002016 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 atomic_inc(&md->event_nr);
2019 wake_up(&md->eventq);
2020}
2021
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002022static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002024 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002026 mutex_lock(&md->bdev->bd_inode->i_mutex);
2027 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2028 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002031/*
2032 * Returns old map, which caller must destroy.
2033 */
2034static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2035 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002037 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002038 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002040 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002043
2044 /*
2045 * Wipe any geometry if the size of the table changed.
2046 */
2047 if (size != get_capacity(md->disk))
2048 memset(&md->geometry, 0, sizeof(md->geometry));
2049
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002050 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002052 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002053
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002054 /*
2055 * The queue hasn't been stopped yet, if the old table type wasn't
2056 * for request-based during suspension. So stop it to prevent
2057 * I/O mapping before resume.
2058 * This must be done before setting the queue restrictions,
2059 * because request-based dm may be run just after the setting.
2060 */
2061 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2062 stop_queue(q);
2063
2064 __bind_mempools(md, t);
2065
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002066 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002067 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002068 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002069 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002070 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002071
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002072 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Alasdair G Kergona7940152009-12-10 23:52:23 +00002075/*
2076 * Returns unbound table for the caller to free.
2077 */
2078static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002081 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002084 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002087 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002089 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002090
2091 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094/*
2095 * Constructor for a new device.
2096 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002097int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 struct mapped_device *md;
2100
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002101 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (!md)
2103 return -ENXIO;
2104
Milan Broz784aae72009-01-06 03:05:12 +00002105 dm_sysfs_init(md);
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 *result = md;
2108 return 0;
2109}
2110
Mike Snitzera5664da2010-08-12 04:14:01 +01002111/*
2112 * Functions to manage md->type.
2113 * All are required to hold md->type_lock.
2114 */
2115void dm_lock_md_type(struct mapped_device *md)
2116{
2117 mutex_lock(&md->type_lock);
2118}
2119
2120void dm_unlock_md_type(struct mapped_device *md)
2121{
2122 mutex_unlock(&md->type_lock);
2123}
2124
2125void dm_set_md_type(struct mapped_device *md, unsigned type)
2126{
2127 md->type = type;
2128}
2129
2130unsigned dm_get_md_type(struct mapped_device *md)
2131{
2132 return md->type;
2133}
2134
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002135/*
2136 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2137 */
2138static int dm_init_request_based_queue(struct mapped_device *md)
2139{
2140 struct request_queue *q = NULL;
2141
2142 if (md->queue->elevator)
2143 return 1;
2144
2145 /* Fully initialize the queue */
2146 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2147 if (!q)
2148 return 0;
2149
2150 md->queue = q;
2151 md->saved_make_request_fn = md->queue->make_request_fn;
2152 dm_init_md_queue(md);
2153 blk_queue_softirq_done(md->queue, dm_softirq_done);
2154 blk_queue_prep_rq(md->queue, dm_prep_fn);
2155 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002156
2157 elv_register_queue(md->queue);
2158
2159 return 1;
2160}
2161
2162/*
2163 * Setup the DM device's queue based on md's type
2164 */
2165int dm_setup_md_queue(struct mapped_device *md)
2166{
2167 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2168 !dm_init_request_based_queue(md)) {
2169 DMWARN("Cannot initialize queue for request-based mapped device");
2170 return -EINVAL;
2171 }
2172
2173 return 0;
2174}
2175
David Teigland637842c2006-01-06 00:20:00 -08002176static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 unsigned minor = MINOR(dev);
2180
2181 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2182 return NULL;
2183
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002184 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002187 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002188 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002189 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002190 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002191 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002192 goto out;
2193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002195out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002196 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
David Teigland637842c2006-01-06 00:20:00 -08002198 return md;
2199}
2200
David Teiglandd229a952006-01-06 00:20:01 -08002201struct mapped_device *dm_get_md(dev_t dev)
2202{
2203 struct mapped_device *md = dm_find_md(dev);
2204
2205 if (md)
2206 dm_get(md);
2207
2208 return md;
2209}
2210
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002211void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002212{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002213 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
2216void dm_set_mdptr(struct mapped_device *md, void *ptr)
2217{
2218 md->interface_ptr = ptr;
2219}
2220
2221void dm_get(struct mapped_device *md)
2222{
2223 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002224 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002227const char *dm_device_name(struct mapped_device *md)
2228{
2229 return md->name;
2230}
2231EXPORT_SYMBOL_GPL(dm_device_name);
2232
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002233static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002235 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002237 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002238
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002239 spin_lock(&_minor_lock);
2240 map = dm_get_live_table(md);
2241 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2242 set_bit(DMF_FREEING, &md->flags);
2243 spin_unlock(&_minor_lock);
2244
2245 if (!dm_suspended_md(md)) {
2246 dm_table_presuspend_targets(map);
2247 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002249
2250 /*
2251 * Rare, but there may be I/O requests still going to complete,
2252 * for example. Wait for all references to disappear.
2253 * No one should increment the reference count of the mapped_device,
2254 * after the mapped_device state becomes DMF_FREEING.
2255 */
2256 if (wait)
2257 while (atomic_read(&md->holders))
2258 msleep(1);
2259 else if (atomic_read(&md->holders))
2260 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2261 dm_device_name(md), atomic_read(&md->holders));
2262
2263 dm_sysfs_exit(md);
2264 dm_table_put(map);
2265 dm_table_destroy(__unbind(md));
2266 free_dev(md);
2267}
2268
2269void dm_destroy(struct mapped_device *md)
2270{
2271 __dm_destroy(md, true);
2272}
2273
2274void dm_destroy_immediate(struct mapped_device *md)
2275{
2276 __dm_destroy(md, false);
2277}
2278
2279void dm_put(struct mapped_device *md)
2280{
2281 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
Edward Goggin79eb8852007-05-09 02:32:56 -07002283EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Mikulas Patocka401600d2009-04-02 19:55:38 +01002285static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002286{
2287 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002288 DECLARE_WAITQUEUE(wait, current);
2289
2290 dm_unplug_all(md->queue);
2291
2292 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002293
2294 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002295 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002296
2297 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002298 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002299 break;
2300
Mikulas Patocka401600d2009-04-02 19:55:38 +01002301 if (interruptible == TASK_INTERRUPTIBLE &&
2302 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002303 r = -EINTR;
2304 break;
2305 }
2306
2307 io_schedule();
2308 }
2309 set_current_state(TASK_RUNNING);
2310
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002311 remove_wait_queue(&md->wait, &wait);
2312
Milan Broz46125c12008-02-08 02:10:30 +00002313 return r;
2314}
2315
Tejun Heod87f4c12010-09-03 11:56:19 +02002316static void process_flush(struct mapped_device *md, struct bio *bio)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002317{
Tejun Heod87f4c12010-09-03 11:56:19 +02002318 md->flush_error = 0;
2319
2320 /* handle REQ_FLUSH */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002321 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002322
Tejun Heod87f4c12010-09-03 11:56:19 +02002323 bio_init(&md->flush_bio);
2324 md->flush_bio.bi_bdev = md->bdev;
2325 md->flush_bio.bi_rw = WRITE_FLUSH;
2326 __split_and_process_bio(md, &md->flush_bio);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002327
2328 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002329
Tejun Heod87f4c12010-09-03 11:56:19 +02002330 /* if it's an empty flush or the preflush failed, we're done */
2331 if (!bio_has_data(bio) || md->flush_error) {
2332 if (md->flush_error != DM_ENDIO_REQUEUE)
2333 bio_endio(bio, md->flush_error);
2334 else {
2335 spin_lock_irq(&md->deferred_lock);
2336 bio_list_add_head(&md->deferred, bio);
2337 spin_unlock_irq(&md->deferred_lock);
2338 }
2339 return;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002340 }
2341
Tejun Heod87f4c12010-09-03 11:56:19 +02002342 /* issue data + REQ_FUA */
2343 bio->bi_rw &= ~REQ_FLUSH;
2344 __split_and_process_bio(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002345}
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347/*
2348 * Process the deferred bios
2349 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002350static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Mikulas Patockaef208582009-04-02 19:55:38 +01002352 struct mapped_device *md = container_of(work, struct mapped_device,
2353 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002354 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Mikulas Patockaef208582009-04-02 19:55:38 +01002356 down_write(&md->io_lock);
2357
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002358 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002359 spin_lock_irq(&md->deferred_lock);
2360 c = bio_list_pop(&md->deferred);
2361 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002362
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002363 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002364 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002365 break;
2366 }
2367
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002368 up_write(&md->io_lock);
2369
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002370 if (dm_request_based(md))
2371 generic_make_request(c);
2372 else {
Tejun Heod87f4c12010-09-03 11:56:19 +02002373 if (c->bi_rw & REQ_FLUSH)
2374 process_flush(md, c);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002375 else
2376 __split_and_process_bio(md, c);
2377 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002378
2379 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002380 }
Milan Broz73d410c2008-02-08 02:10:25 +00002381
Mikulas Patockaef208582009-04-02 19:55:38 +01002382 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002385static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002386{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002387 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2388 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002389 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002390}
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002393 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002395struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002397 struct dm_table *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002398 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002399 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Daniel Walkere61290a2008-02-08 02:10:08 +00002401 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002404 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002405 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002407 r = dm_calculate_queue_limits(table, &limits);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002408 if (r) {
2409 map = ERR_PTR(r);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002410 goto out;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002411 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002412
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002413 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002415out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002416 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002417 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
2420/*
2421 * Functions to lock and unlock any filesystem running on the
2422 * device.
2423 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002424static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002426 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002429
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002430 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002431 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002432 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002433 md->frozen_sb = NULL;
2434 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002435 }
2436
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002437 set_bit(DMF_FROZEN, &md->flags);
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return 0;
2440}
2441
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002442static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002444 if (!test_bit(DMF_FROZEN, &md->flags))
2445 return;
2446
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002447 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002449 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452/*
2453 * We need to be able to change a mapping table under a mounted
2454 * filesystem. For example we might want to move some data in
2455 * the background. Before the table can be swapped with
2456 * dm_bind_table, dm_suspend must be called to flush any in
2457 * flight bios and ensure that any further io gets deferred.
2458 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002459/*
2460 * Suspend mechanism in request-based dm.
2461 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002462 * 1. Flush all I/Os by lock_fs() if needed.
2463 * 2. Stop dispatching any I/O by stopping the request_queue.
2464 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002465 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002466 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002467 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002468int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002470 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002471 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002472 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002473 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Daniel Walkere61290a2008-02-08 02:10:08 +00002475 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002476
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002477 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002478 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002479 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002482 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002484 /*
2485 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2486 * This flag is cleared before dm_suspend returns.
2487 */
2488 if (noflush)
2489 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2490
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002491 /* This does not get reverted if there's an error later. */
2492 dm_table_presuspend_targets(map);
2493
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002494 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002495 * Flush I/O to the device.
2496 * Any I/O submitted after lock_fs() may not be flushed.
2497 * noflush takes precedence over do_lockfs.
2498 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002499 */
2500 if (!noflush && do_lockfs) {
2501 r = lock_fs(md);
2502 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002503 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
2506 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002507 * Here we must make sure that no processes are submitting requests
2508 * to target drivers i.e. no one may be executing
2509 * __split_and_process_bio. This is called from dm_request and
2510 * dm_wq_work.
2511 *
2512 * To get all processes out of __split_and_process_bio in dm_request,
2513 * we take the write lock. To prevent any process from reentering
2514 * __split_and_process_bio from dm_request, we set
2515 * DMF_QUEUE_IO_TO_THREAD.
2516 *
2517 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2518 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2519 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2520 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002522 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002523 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2524 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002525 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002527 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002528 * Stop md->queue before flushing md->wq in case request-based
2529 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002530 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002531 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002532 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002533
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002534 flush_workqueue(md->wq);
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002537 * At this point no more requests are entering target request routines.
2538 * We call dm_wait_for_completion to wait for all existing requests
2539 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002541 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002543 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002544 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002545 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002546 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002549 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002550 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002551
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002552 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002553 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002554
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002555 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002556 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002557 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002558
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002559 /*
2560 * If dm_wait_for_completion returned 0, the device is completely
2561 * quiescent now. There is no request-processing activity. All new
2562 * requests are being added to md->deferred list.
2563 */
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 set_bit(DMF_SUSPENDED, &md->flags);
2566
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002567 dm_table_postsuspend_targets(map);
2568
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002569out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002571
2572out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002573 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002574 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
2577int dm_resume(struct mapped_device *md)
2578{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002579 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002580 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Daniel Walkere61290a2008-02-08 02:10:08 +00002582 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002583 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002584 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002585
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002586 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002587 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002588 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Milan Broz8757b772006-10-03 01:15:36 -07002590 r = dm_table_resume_targets(map);
2591 if (r)
2592 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002593
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002594 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002595
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002596 /*
2597 * Flushing deferred I/Os must be done after targets are resumed
2598 * so that mapping of targets can work correctly.
2599 * Request-based dm is queueing the deferred I/Os in its request_queue.
2600 */
2601 if (dm_request_based(md))
2602 start_queue(md->queue);
2603
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002604 unlock_fs(md);
2605
2606 clear_bit(DMF_SUSPENDED, &md->flags);
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002609 r = 0;
2610out:
2611 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002612 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002613
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002614 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
2617/*-----------------------------------------------------------------
2618 * Event notification.
2619 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002620int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002621 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002622{
Milan Broz60935eb2009-06-22 10:12:30 +01002623 char udev_cookie[DM_COOKIE_LENGTH];
2624 char *envp[] = { udev_cookie, NULL };
2625
2626 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002627 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002628 else {
2629 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2630 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002631 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2632 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002633 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002634}
2635
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002636uint32_t dm_next_uevent_seq(struct mapped_device *md)
2637{
2638 return atomic_add_return(1, &md->uevent_seq);
2639}
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641uint32_t dm_get_event_nr(struct mapped_device *md)
2642{
2643 return atomic_read(&md->event_nr);
2644}
2645
2646int dm_wait_event(struct mapped_device *md, int event_nr)
2647{
2648 return wait_event_interruptible(md->eventq,
2649 (event_nr != atomic_read(&md->event_nr)));
2650}
2651
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002652void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2653{
2654 unsigned long flags;
2655
2656 spin_lock_irqsave(&md->uevent_lock, flags);
2657 list_add(elist, &md->uevent_list);
2658 spin_unlock_irqrestore(&md->uevent_lock, flags);
2659}
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661/*
2662 * The gendisk is only valid as long as you have a reference
2663 * count on 'md'.
2664 */
2665struct gendisk *dm_disk(struct mapped_device *md)
2666{
2667 return md->disk;
2668}
2669
Milan Broz784aae72009-01-06 03:05:12 +00002670struct kobject *dm_kobject(struct mapped_device *md)
2671{
2672 return &md->kobj;
2673}
2674
2675/*
2676 * struct mapped_device should not be exported outside of dm.c
2677 * so use this check to verify that kobj is part of md structure
2678 */
2679struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2680{
2681 struct mapped_device *md;
2682
2683 md = container_of(kobj, struct mapped_device, kobj);
2684 if (&md->kobj != kobj)
2685 return NULL;
2686
Milan Broz4d89b7b2009-06-22 10:12:11 +01002687 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002688 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002689 return NULL;
2690
Milan Broz784aae72009-01-06 03:05:12 +00002691 dm_get(md);
2692 return md;
2693}
2694
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002695int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 return test_bit(DMF_SUSPENDED, &md->flags);
2698}
2699
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002700int dm_suspended(struct dm_target *ti)
2701{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002702 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002703}
2704EXPORT_SYMBOL_GPL(dm_suspended);
2705
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002706int dm_noflush_suspending(struct dm_target *ti)
2707{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002708 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002709}
2710EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2711
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002712struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2713{
2714 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2715
2716 if (!pools)
2717 return NULL;
2718
2719 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2720 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2721 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2722 if (!pools->io_pool)
2723 goto free_pools_and_out;
2724
2725 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2726 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2727 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2728 if (!pools->tio_pool)
2729 goto free_io_pool_and_out;
2730
2731 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2732 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2733 if (!pools->bs)
2734 goto free_tio_pool_and_out;
2735
2736 return pools;
2737
2738free_tio_pool_and_out:
2739 mempool_destroy(pools->tio_pool);
2740
2741free_io_pool_and_out:
2742 mempool_destroy(pools->io_pool);
2743
2744free_pools_and_out:
2745 kfree(pools);
2746
2747 return NULL;
2748}
2749
2750void dm_free_md_mempools(struct dm_md_mempools *pools)
2751{
2752 if (!pools)
2753 return;
2754
2755 if (pools->io_pool)
2756 mempool_destroy(pools->io_pool);
2757
2758 if (pools->tio_pool)
2759 mempool_destroy(pools->tio_pool);
2760
2761 if (pools->bs)
2762 bioset_free(pools->bs);
2763
2764 kfree(pools);
2765}
2766
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002767static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 .open = dm_blk_open,
2769 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002770 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002771 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 .owner = THIS_MODULE
2773};
2774
2775EXPORT_SYMBOL(dm_get_mapinfo);
2776
2777/*
2778 * module hooks
2779 */
2780module_init(dm_init);
2781module_exit(dm_exit);
2782
2783module_param(major, uint, 0);
2784MODULE_PARM_DESC(major, "The major number of the device mapper");
2785MODULE_DESCRIPTION(DM_NAME " driver");
2786MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2787MODULE_LICENSE("GPL");