blob: 7b986e77b75e8189c1c2e0d2d4beefbe973ecaf5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
17#include <linux/buffer_head.h>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080021#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010022#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080023
24#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "core"
27
Milan Broz60935eb2009-06-22 10:12:30 +010028/*
29 * Cookies are numeric values sent with CHANGE and REMOVE
30 * uevents while resuming, removing or renaming the device.
31 */
32#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
33#define DM_COOKIE_LENGTH 24
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static const char *_name = DM_NAME;
36
37static unsigned int major = 0;
38static unsigned int _major = 0;
39
Alasdair G Kergond15b7742011-08-02 12:32:01 +010040static DEFINE_IDR(_minor_idr);
41
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070042static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000044 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * One of these is allocated per bio.
46 */
47struct dm_io {
48 struct mapped_device *md;
49 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010051 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080052 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010053 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000057 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * One of these is allocated per target within a bio. Hopefully
59 * this will be simplified out one day.
60 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010061struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct dm_io *io;
63 struct dm_target *ti;
64 union map_info info;
65};
66
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000067/*
68 * For request-based dm.
69 * One of these is allocated per request.
70 */
71struct dm_rq_target_io {
72 struct mapped_device *md;
73 struct dm_target *ti;
74 struct request *orig, clone;
75 int error;
76 union map_info info;
77};
78
79/*
80 * For request-based dm.
81 * One of these is allocated per bio.
82 */
83struct dm_rq_clone_bio_info {
84 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010085 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000086};
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088union map_info *dm_get_mapinfo(struct bio *bio)
89{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070090 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010091 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070092 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010095union map_info *dm_get_rq_mapinfo(struct request *rq)
96{
97 if (rq && rq->end_io_data)
98 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
99 return NULL;
100}
101EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
102
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700103#define MINOR_ALLOCED ((void *)-1)
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * Bits for the md->flags field.
107 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100108#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800110#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700111#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700112#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800113#define DMF_NOFLUSH_SUSPENDING 5
Mikulas Patockad5b9dd02011-08-02 12:32:04 +0100114#define DMF_MERGE_IS_OPTIONAL 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Milan Broz304f3f62008-02-08 02:11:17 +0000116/*
117 * Work processed by per-device workqueue.
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700120 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000121 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 rwlock_t map_lock;
123 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700124 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 unsigned long flags;
127
Jens Axboe165125e2007-07-24 09:28:11 +0200128 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100129 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100130 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100131 struct mutex type_lock;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800134 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 void *interface_ptr;
137
138 /*
139 * A list of ios that arrived while we were suspended.
140 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200141 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100143 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800144 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100145 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200148 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000149 */
150 struct workqueue_struct *wq;
151
152 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * The current mapping.
154 */
155 struct dm_table *map;
156
157 /*
158 * io objects are allocated from here.
159 */
160 mempool_t *io_pool;
161 mempool_t *tio_pool;
162
Stefan Bader9faf4002006-10-03 01:15:41 -0700163 struct bio_set *bs;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * Event handling.
167 */
168 atomic_t event_nr;
169 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100170 atomic_t uevent_seq;
171 struct list_head uevent_list;
172 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /*
175 * freeze/thaw support require holding onto a super block
176 */
177 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100178 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800179
180 /* forced geometry settings */
181 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000182
183 /* sysfs handle */
184 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100185
Tejun Heod87f4c12010-09-03 11:56:19 +0200186 /* zero-length flush that will be cloned and submitted to targets */
187 struct bio flush_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100190/*
191 * For mempools pre-allocation at the table loading time.
192 */
193struct dm_md_mempools {
194 mempool_t *io_pool;
195 mempool_t *tio_pool;
196 struct bio_set *bs;
197};
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800200static struct kmem_cache *_io_cache;
201static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000202static struct kmem_cache *_rq_tio_cache;
203static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static int __init local_init(void)
206{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100207 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100210 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100212 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100215 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100216 if (!_tio_cache)
217 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000219 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
220 if (!_rq_tio_cache)
221 goto out_free_tio_cache;
222
223 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
224 if (!_rq_bio_info_cache)
225 goto out_free_rq_tio_cache;
226
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100227 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100228 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000229 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 _major = major;
232 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100233 if (r < 0)
234 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!_major)
237 _major = r;
238
239 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100240
241out_uevent_exit:
242 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000243out_free_rq_bio_info_cache:
244 kmem_cache_destroy(_rq_bio_info_cache);
245out_free_rq_tio_cache:
246 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100247out_free_tio_cache:
248 kmem_cache_destroy(_tio_cache);
249out_free_io_cache:
250 kmem_cache_destroy(_io_cache);
251
252 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255static void local_exit(void)
256{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000257 kmem_cache_destroy(_rq_bio_info_cache);
258 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 kmem_cache_destroy(_tio_cache);
260 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700261 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100262 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 _major = 0;
265
266 DMINFO("cleaned up");
267}
268
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000269static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 local_init,
271 dm_target_init,
272 dm_linear_init,
273 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000274 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100275 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 dm_interface_init,
277};
278
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000279static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 local_exit,
281 dm_target_exit,
282 dm_linear_exit,
283 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000284 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100285 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 dm_interface_exit,
287};
288
289static int __init dm_init(void)
290{
291 const int count = ARRAY_SIZE(_inits);
292
293 int r, i;
294
295 for (i = 0; i < count; i++) {
296 r = _inits[i]();
297 if (r)
298 goto bad;
299 }
300
301 return 0;
302
303 bad:
304 while (i--)
305 _exits[i]();
306
307 return r;
308}
309
310static void __exit dm_exit(void)
311{
312 int i = ARRAY_SIZE(_exits);
313
314 while (i--)
315 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100316
317 /*
318 * Should be empty by this point.
319 */
320 idr_remove_all(&_minor_idr);
321 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
325 * Block device functions
326 */
Mike Anderson432a2122009-12-10 23:52:20 +0000327int dm_deleting_md(struct mapped_device *md)
328{
329 return test_bit(DMF_DELETING, &md->flags);
330}
331
Al Virofe5f9f22008-03-02 10:29:31 -0500332static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct mapped_device *md;
335
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700336 spin_lock(&_minor_lock);
337
Al Virofe5f9f22008-03-02 10:29:31 -0500338 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700339 if (!md)
340 goto out;
341
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700342 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000343 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700344 md = NULL;
345 goto out;
346 }
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700349 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700350
351out:
352 spin_unlock(&_minor_lock);
353
354 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Al Virofe5f9f22008-03-02 10:29:31 -0500357static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Al Virofe5f9f22008-03-02 10:29:31 -0500359 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200360
Milan Broz4a1aeb92011-01-13 19:59:48 +0000361 spin_lock(&_minor_lock);
362
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700363 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000365
366 spin_unlock(&_minor_lock);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700371int dm_open_count(struct mapped_device *md)
372{
373 return atomic_read(&md->open_count);
374}
375
376/*
377 * Guarantees nothing is using the device before it's deleted.
378 */
379int dm_lock_for_deletion(struct mapped_device *md)
380{
381 int r = 0;
382
383 spin_lock(&_minor_lock);
384
385 if (dm_open_count(md))
386 r = -EBUSY;
387 else
388 set_bit(DMF_DELETING, &md->flags);
389
390 spin_unlock(&_minor_lock);
391
392 return r;
393}
394
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800395static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
396{
397 struct mapped_device *md = bdev->bd_disk->private_data;
398
399 return dm_get_geometry(md, geo);
400}
401
Al Virofe5f9f22008-03-02 10:29:31 -0500402static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700403 unsigned int cmd, unsigned long arg)
404{
Al Virofe5f9f22008-03-02 10:29:31 -0500405 struct mapped_device *md = bdev->bd_disk->private_data;
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000406 struct dm_table *map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700407 struct dm_target *tgt;
408 int r = -ENOTTY;
409
Milan Brozaa129a22006-10-03 01:15:15 -0700410 if (!map || !dm_table_get_size(map))
411 goto out;
412
413 /* We only support devices that have a single target */
414 if (dm_table_get_num_targets(map) != 1)
415 goto out;
416
417 tgt = dm_table_get_target(map, 0);
418
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000419 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700420 r = -EAGAIN;
421 goto out;
422 }
423
424 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400425 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700426
427out:
428 dm_table_put(map);
429
Milan Brozaa129a22006-10-03 01:15:15 -0700430 return r;
431}
432
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100433static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 return mempool_alloc(md->io_pool, GFP_NOIO);
436}
437
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100438static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 mempool_free(io, md->io_pool);
441}
442
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100443static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 mempool_free(tio, md->tio_pool);
446}
447
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000448static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
449 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100450{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000451 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100452}
453
454static void free_rq_tio(struct dm_rq_target_io *tio)
455{
456 mempool_free(tio, tio->md->tio_pool);
457}
458
459static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
460{
461 return mempool_alloc(md->io_pool, GFP_ATOMIC);
462}
463
464static void free_bio_info(struct dm_rq_clone_bio_info *info)
465{
466 mempool_free(info, info->tio->md->io_pool);
467}
468
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000469static int md_in_flight(struct mapped_device *md)
470{
471 return atomic_read(&md->pending[READ]) +
472 atomic_read(&md->pending[WRITE]);
473}
474
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800475static void start_io_acct(struct dm_io *io)
476{
477 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900478 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200479 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800480
481 io->start_time = jiffies;
482
Tejun Heo074a7ac2008-08-25 19:56:14 +0900483 cpu = part_stat_lock();
484 part_round_stats(cpu, &dm_disk(md)->part0);
485 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100486 atomic_set(&dm_disk(md)->part0.in_flight[rw],
487 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 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100507 pending = atomic_dec_return(&md->pending[rw]);
508 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200509 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{
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200521 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200523 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda05447422010-09-08 18:07:01 +0200525 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200526 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/*
530 * Everyone (including functions in this file), should use this
531 * function to access the md->map field, and make sure they call
532 * dm_table_put() when finished.
533 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000534struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100537 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100539 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 t = md->map;
541 if (t)
542 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100543 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return t;
546}
547
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800548/*
549 * Get the geometry associated with a dm device
550 */
551int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
552{
553 *geo = md->geometry;
554
555 return 0;
556}
557
558/*
559 * Set the geometry of a device.
560 */
561int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
562{
563 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
564
565 if (geo->start > sz) {
566 DMWARN("Start sector is beyond the geometry limits.");
567 return -EINVAL;
568 }
569
570 md->geometry = *geo;
571
572 return 0;
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*-----------------------------------------------------------------
576 * CRUD START:
577 * A more elegant soln is in the works that uses the queue
578 * merge fn, unfortunately there are a couple of changes to
579 * the block layer that I want to make for this. So in the
580 * interests of getting something for people to use I give
581 * you this clearly demarcated crap.
582 *---------------------------------------------------------------*/
583
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800584static int __noflush_suspending(struct mapped_device *md)
585{
586 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*
590 * Decrements the number of outstanding ios that a bio has been
591 * cloned into, completing the original io if necc.
592 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800593static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800595 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000596 int io_error;
597 struct bio *bio;
598 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800599
600 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100601 if (unlikely(error)) {
602 spin_lock_irqsave(&io->endio_lock, flags);
603 if (!(io->error > 0 && __noflush_suspending(md)))
604 io->error = error;
605 spin_unlock_irqrestore(&io->endio_lock, flags);
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800609 if (io->error == DM_ENDIO_REQUEUE) {
610 /*
611 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800612 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100613 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200614 if (__noflush_suspending(md))
615 bio_list_add_head(&md->deferred, io->bio);
616 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800617 /* noflush suspend was interrupted. */
618 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100619 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800620 }
621
Milan Brozb35f8ca2009-03-16 17:44:36 +0000622 io_error = io->error;
623 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200624 end_io_acct(io);
625 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100626
Tejun Heo6a8736d2010-09-08 18:07:00 +0200627 if (io_error == DM_ENDIO_REQUEUE)
628 return;
629
Mike Snitzerb372d362010-09-08 18:07:01 +0200630 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100631 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200632 * Preflush done for flush with data, reissue
633 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100634 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200635 bio->bi_rw &= ~REQ_FLUSH;
636 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100637 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200638 /* done with normal IO or empty flush */
Jeff Moyerb7908c12011-01-06 20:41:42 +0100639 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200640 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643}
644
NeilBrown6712ecf2007-09-27 12:47:43 +0200645static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100648 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000649 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700650 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 dm_endio_fn endio = tio->ti->type->end_io;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
654 error = -EIO;
655
656 if (endio) {
657 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800658 if (r < 0 || r == DM_ENDIO_REQUEUE)
659 /*
660 * error and requeue request are handled
661 * in dec_pending().
662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800664 else if (r == DM_ENDIO_INCOMPLETE)
665 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200666 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800667 else if (r) {
668 DMWARN("unimplemented target endio return value: %d", r);
669 BUG();
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
Stefan Bader9faf4002006-10-03 01:15:41 -0700673 /*
674 * Store md for cleanup instead of tio which is about to get freed.
675 */
676 bio->bi_private = md->bs;
677
Stefan Bader9faf4002006-10-03 01:15:41 -0700678 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000679 bio_put(bio);
680 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100683/*
684 * Partial completion handling for request-based dm
685 */
686static void end_clone_bio(struct bio *clone, int error)
687{
688 struct dm_rq_clone_bio_info *info = clone->bi_private;
689 struct dm_rq_target_io *tio = info->tio;
690 struct bio *bio = info->orig;
691 unsigned int nr_bytes = info->orig->bi_size;
692
693 bio_put(clone);
694
695 if (tio->error)
696 /*
697 * An error has already been detected on the request.
698 * Once error occurred, just let clone->end_io() handle
699 * the remainder.
700 */
701 return;
702 else if (error) {
703 /*
704 * Don't notice the error to the upper layer yet.
705 * The error handling decision is made by the target driver,
706 * when the request is completed.
707 */
708 tio->error = error;
709 return;
710 }
711
712 /*
713 * I/O for the bio successfully completed.
714 * Notice the data completion to the upper layer.
715 */
716
717 /*
718 * bios are processed from the head of the list.
719 * So the completing bio should always be rq->bio.
720 * If it's not, something wrong is happening.
721 */
722 if (tio->orig->bio != bio)
723 DMERR("bio completion is going in the middle of the request");
724
725 /*
726 * Update the original request.
727 * Do not use blk_end_request() here, because it may complete
728 * the original request before the clone, and break the ordering.
729 */
730 blk_update_request(tio->orig, 0, nr_bytes);
731}
732
733/*
734 * Don't touch any member of the md after calling this function because
735 * the md may be freed in dm_put() at the end of this function.
736 * Or do dm_get() before calling this function and dm_put() later.
737 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000738static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100739{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000740 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100741
742 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000743 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100744 wake_up(&md->wait);
745
746 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000747 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100748
749 /*
750 * dm_put() must be at the end of this function. See the comment above
751 */
752 dm_put(md);
753}
754
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100755static void free_rq_clone(struct request *clone)
756{
757 struct dm_rq_target_io *tio = clone->end_io_data;
758
759 blk_rq_unprep_clone(clone);
760 free_rq_tio(tio);
761}
762
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000763/*
764 * Complete the clone and the original request.
765 * Must be called without queue lock.
766 */
767static void dm_end_request(struct request *clone, int error)
768{
769 int rw = rq_data_dir(clone);
770 struct dm_rq_target_io *tio = clone->end_io_data;
771 struct mapped_device *md = tio->md;
772 struct request *rq = tio->orig;
773
Tejun Heo29e40132010-09-08 18:07:00 +0200774 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000775 rq->errors = clone->errors;
776 rq->resid_len = clone->resid_len;
777
778 if (rq->sense)
779 /*
780 * We are using the sense buffer of the original
781 * request.
782 * So setting the length of the sense data is enough.
783 */
784 rq->sense_len = clone->sense_len;
785 }
786
787 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200788 blk_end_request_all(rq, error);
789 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000790}
791
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100792static void dm_unprep_request(struct request *rq)
793{
794 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100795
796 rq->special = NULL;
797 rq->cmd_flags &= ~REQ_DONTPREP;
798
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100799 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100800}
801
802/*
803 * Requeue the original request of a clone.
804 */
805void dm_requeue_unmapped_request(struct request *clone)
806{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000807 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100808 struct dm_rq_target_io *tio = clone->end_io_data;
809 struct mapped_device *md = tio->md;
810 struct request *rq = tio->orig;
811 struct request_queue *q = rq->q;
812 unsigned long flags;
813
814 dm_unprep_request(rq);
815
816 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100817 blk_requeue_request(q, rq);
818 spin_unlock_irqrestore(q->queue_lock, flags);
819
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000820 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100821}
822EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
823
824static void __stop_queue(struct request_queue *q)
825{
826 blk_stop_queue(q);
827}
828
829static void stop_queue(struct request_queue *q)
830{
831 unsigned long flags;
832
833 spin_lock_irqsave(q->queue_lock, flags);
834 __stop_queue(q);
835 spin_unlock_irqrestore(q->queue_lock, flags);
836}
837
838static void __start_queue(struct request_queue *q)
839{
840 if (blk_queue_stopped(q))
841 blk_start_queue(q);
842}
843
844static void start_queue(struct request_queue *q)
845{
846 unsigned long flags;
847
848 spin_lock_irqsave(q->queue_lock, flags);
849 __start_queue(q);
850 spin_unlock_irqrestore(q->queue_lock, flags);
851}
852
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000853static void dm_done(struct request *clone, int error, bool mapped)
854{
855 int r = error;
856 struct dm_rq_target_io *tio = clone->end_io_data;
857 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
858
859 if (mapped && rq_end_io)
860 r = rq_end_io(tio->ti, clone, error, &tio->info);
861
862 if (r <= 0)
863 /* The target wants to complete the I/O */
864 dm_end_request(clone, r);
865 else if (r == DM_ENDIO_INCOMPLETE)
866 /* The target will handle the I/O */
867 return;
868 else if (r == DM_ENDIO_REQUEUE)
869 /* The target wants to requeue the I/O */
870 dm_requeue_unmapped_request(clone);
871 else {
872 DMWARN("unimplemented target endio return value: %d", r);
873 BUG();
874 }
875}
876
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100877/*
878 * Request completion handler for request-based dm
879 */
880static void dm_softirq_done(struct request *rq)
881{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000882 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100883 struct request *clone = rq->completion_data;
884 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100885
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000886 if (rq->cmd_flags & REQ_FAILED)
887 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100888
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000889 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100890}
891
892/*
893 * Complete the clone and the original request with the error status
894 * through softirq context.
895 */
896static void dm_complete_request(struct request *clone, int error)
897{
898 struct dm_rq_target_io *tio = clone->end_io_data;
899 struct request *rq = tio->orig;
900
901 tio->error = error;
902 rq->completion_data = clone;
903 blk_complete_request(rq);
904}
905
906/*
907 * Complete the not-mapped clone and the original request with the error status
908 * through softirq context.
909 * Target's rq_end_io() function isn't called.
910 * This may be used when the target's map_rq() function fails.
911 */
912void dm_kill_unmapped_request(struct request *clone, int error)
913{
914 struct dm_rq_target_io *tio = clone->end_io_data;
915 struct request *rq = tio->orig;
916
917 rq->cmd_flags |= REQ_FAILED;
918 dm_complete_request(clone, error);
919}
920EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
921
922/*
923 * Called with the queue lock held
924 */
925static void end_clone_request(struct request *clone, int error)
926{
927 /*
928 * For just cleaning up the information of the queue in which
929 * the clone was dispatched.
930 * The clone is *NOT* freed actually here because it is alloced from
931 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
932 */
933 __blk_put_request(clone->q, clone);
934
935 /*
936 * Actual request completion is done in a softirq context which doesn't
937 * hold the queue lock. Otherwise, deadlock could occur because:
938 * - another request may be submitted by the upper level driver
939 * of the stacking during the completion
940 * - the submission which requires queue lock may be done
941 * against this queue
942 */
943 dm_complete_request(clone, error);
944}
945
Mike Snitzer56a67df2010-08-12 04:14:10 +0100946/*
947 * Return maximum size of I/O possible at the supplied sector up to the current
948 * target boundary.
949 */
950static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Mike Snitzer56a67df2010-08-12 04:14:10 +0100952 sector_t target_offset = dm_target_offset(ti, sector);
953
954 return ti->len - target_offset;
955}
956
957static sector_t max_io_len(sector_t sector, struct dm_target *ti)
958{
959 sector_t len = max_io_len_target_boundary(sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /*
962 * Does the target need to split even further ?
963 */
964 if (ti->split_io) {
965 sector_t boundary;
Mike Snitzer56a67df2010-08-12 04:14:10 +0100966 sector_t offset = dm_target_offset(ti, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
968 - offset;
969 if (len > boundary)
970 len = boundary;
971 }
972
973 return len;
974}
975
976static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100977 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100980 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700981 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 clone->bi_end_io = clone_endio;
984 clone->bi_private = tio;
985
986 /*
987 * Map the clone. If r == 0 we don't need to do
988 * anything, the target has assumed ownership of
989 * this io.
990 */
991 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100992 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800994 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100996
Mike Snitzerd07335e2010-11-16 12:52:38 +0100997 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
998 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001001 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1002 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001003 md = tio->io->md;
1004 dec_pending(tio->io, r);
1005 /*
1006 * Store bio_set for cleanup.
1007 */
1008 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001010 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001011 } else if (r) {
1012 DMWARN("unimplemented target map return value: %d", r);
1013 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015}
1016
1017struct clone_info {
1018 struct mapped_device *md;
1019 struct dm_table *map;
1020 struct bio *bio;
1021 struct dm_io *io;
1022 sector_t sector;
1023 sector_t sector_count;
1024 unsigned short idx;
1025};
1026
Peter Osterlund36763472005-09-06 15:16:42 -07001027static void dm_bio_destructor(struct bio *bio)
1028{
Stefan Bader9faf4002006-10-03 01:15:41 -07001029 struct bio_set *bs = bio->bi_private;
1030
1031 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001035 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
1037static struct bio *split_bvec(struct bio *bio, sector_t sector,
1038 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001039 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 struct bio *clone;
1042 struct bio_vec *bv = bio->bi_io_vec + idx;
1043
Stefan Bader9faf4002006-10-03 01:15:41 -07001044 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001045 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 *clone->bi_io_vec = *bv;
1047
1048 clone->bi_sector = sector;
1049 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001050 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 clone->bi_vcnt = 1;
1052 clone->bi_size = to_bytes(len);
1053 clone->bi_io_vec->bv_offset = offset;
1054 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001055 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Martin K. Petersen9c470082009-04-09 00:27:12 +01001057 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001058 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001059 bio_integrity_trim(clone,
1060 bio_sector_offset(bio, idx, offset), len);
1061 }
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return clone;
1064}
1065
1066/*
1067 * Creates a bio that consists of range of complete bvecs.
1068 */
1069static struct bio *clone_bio(struct bio *bio, sector_t sector,
1070 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001071 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 struct bio *clone;
1074
Stefan Bader9faf4002006-10-03 01:15:41 -07001075 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1076 __bio_clone(clone, bio);
1077 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 clone->bi_sector = sector;
1079 clone->bi_idx = idx;
1080 clone->bi_vcnt = idx + bv_count;
1081 clone->bi_size = to_bytes(len);
1082 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1083
Martin K. Petersen9c470082009-04-09 00:27:12 +01001084 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001085 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001086
1087 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1088 bio_integrity_trim(clone,
1089 bio_sector_offset(bio, idx, 0), len);
1090 }
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return clone;
1093}
1094
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001095static struct dm_target_io *alloc_tio(struct clone_info *ci,
1096 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001097{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001098 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001099
1100 tio->io = ci->io;
1101 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001102 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001103
1104 return tio;
1105}
1106
Mike Snitzer06a426c2010-08-12 04:14:09 +01001107static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001108 unsigned request_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001109{
1110 struct dm_target_io *tio = alloc_tio(ci, ti);
1111 struct bio *clone;
1112
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001113 tio->info.target_request_nr = request_nr;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001114
Mike Snitzer06a426c2010-08-12 04:14:09 +01001115 /*
1116 * Discard requests require the bio's inline iovecs be initialized.
1117 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1118 * and discard, so no need for concern about wasted bvec allocations.
1119 */
1120 clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001121 __bio_clone(clone, ci->bio);
1122 clone->bi_destructor = dm_bio_destructor;
Mike Snitzera79245b2010-08-12 04:14:24 +01001123 if (len) {
1124 clone->bi_sector = ci->sector;
1125 clone->bi_size = to_bytes(len);
1126 }
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001127
1128 __map_bio(ti, clone, tio);
1129}
1130
Mike Snitzer06a426c2010-08-12 04:14:09 +01001131static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001132 unsigned num_requests, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001133{
1134 unsigned request_nr;
1135
1136 for (request_nr = 0; request_nr < num_requests; request_nr++)
Mike Snitzera79245b2010-08-12 04:14:24 +01001137 __issue_target_request(ci, ti, request_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001138}
1139
Mike Snitzerb372d362010-09-08 18:07:01 +02001140static int __clone_and_map_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001141{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001142 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001143 struct dm_target *ti;
1144
Mike Snitzerb372d362010-09-08 18:07:01 +02001145 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001146 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mike Snitzera79245b2010-08-12 04:14:24 +01001147 __issue_target_requests(ci, ti, ti->num_flush_requests, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001148
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001149 return 0;
1150}
1151
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001152/*
1153 * Perform all io with a single clone.
1154 */
1155static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
1156{
1157 struct bio *clone, *bio = ci->bio;
1158 struct dm_target_io *tio;
1159
1160 tio = alloc_tio(ci, ti);
1161 clone = clone_bio(bio, ci->sector, ci->idx,
1162 bio->bi_vcnt - ci->idx, ci->sector_count,
1163 ci->md->bs);
1164 __map_bio(ti, clone, tio);
1165 ci->sector_count = 0;
1166}
1167
1168static int __clone_and_map_discard(struct clone_info *ci)
1169{
1170 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001171 sector_t len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001172
Mike Snitzera79245b2010-08-12 04:14:24 +01001173 do {
1174 ti = dm_table_find_target(ci->map, ci->sector);
1175 if (!dm_target_is_valid(ti))
1176 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001177
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001178 /*
Mike Snitzera79245b2010-08-12 04:14:24 +01001179 * Even though the device advertised discard support,
Mike Snitzer936688d2011-08-02 12:32:01 +01001180 * that does not mean every target supports it, and
1181 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001182 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001183 */
Mike Snitzera79245b2010-08-12 04:14:24 +01001184 if (!ti->num_discard_requests)
1185 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001186
Mike Snitzera79245b2010-08-12 04:14:24 +01001187 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001188
Mike Snitzera79245b2010-08-12 04:14:24 +01001189 __issue_target_requests(ci, ti, ti->num_discard_requests, len);
1190
1191 ci->sector += len;
1192 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001193
1194 return 0;
1195}
1196
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001197static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001200 struct dm_target *ti;
1201 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001202 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001204 if (unlikely(bio->bi_rw & REQ_DISCARD))
1205 return __clone_and_map_discard(ci);
1206
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001207 ti = dm_table_find_target(ci->map, ci->sector);
1208 if (!dm_target_is_valid(ti))
1209 return -EIO;
1210
Mike Snitzer56a67df2010-08-12 04:14:10 +01001211 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (ci->sector_count <= max) {
1214 /*
1215 * Optimise for the simple case where we can do all of
1216 * the remaining io with a single clone.
1217 */
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001218 __clone_and_map_simple(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1221 /*
1222 * There are some bvecs that don't span targets.
1223 * Do as many of these as possible.
1224 */
1225 int i;
1226 sector_t remaining = max;
1227 sector_t bv_len;
1228
1229 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1230 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1231
1232 if (bv_len > remaining)
1233 break;
1234
1235 remaining -= bv_len;
1236 len += bv_len;
1237 }
1238
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001239 tio = alloc_tio(ci, ti);
Stefan Bader9faf4002006-10-03 01:15:41 -07001240 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1241 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 __map_bio(ti, clone, tio);
1243
1244 ci->sector += len;
1245 ci->sector_count -= len;
1246 ci->idx = i;
1247
1248 } else {
1249 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001250 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
1252 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001253 sector_t remaining = to_sector(bv->bv_len);
1254 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001256 do {
1257 if (offset) {
1258 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001259 if (!dm_target_is_valid(ti))
1260 return -EIO;
1261
Mike Snitzer56a67df2010-08-12 04:14:10 +01001262 max = max_io_len(ci->sector, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001265 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001267 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001268 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001269 bv->bv_offset + offset, len,
1270 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001271
1272 __map_bio(ti, clone, tio);
1273
1274 ci->sector += len;
1275 ci->sector_count -= len;
1276 offset += to_bytes(len);
1277 } while (remaining -= len);
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 ci->idx++;
1280 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001281
1282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
1285/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001286 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001288static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001291 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001293 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001294 if (unlikely(!ci.map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001295 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001296 return;
1297 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 ci.io = alloc_io(md);
1301 ci.io->error = 0;
1302 atomic_set(&ci.io->io_count, 1);
1303 ci.io->bio = bio;
1304 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001305 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 ci.sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 ci.idx = bio->bi_idx;
1308
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001309 start_io_acct(ci.io);
Mike Snitzerb372d362010-09-08 18:07:01 +02001310 if (bio->bi_rw & REQ_FLUSH) {
1311 ci.bio = &ci.md->flush_bio;
1312 ci.sector_count = 0;
1313 error = __clone_and_map_empty_flush(&ci);
1314 /* dec_pending submits any data associated with flush */
1315 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001316 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001317 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001318 while (ci.sector_count && !error)
Tejun Heod87f4c12010-09-03 11:56:19 +02001319 error = __clone_and_map(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001323 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 dm_table_put(ci.map);
1325}
1326/*-----------------------------------------------------------------
1327 * CRUD END
1328 *---------------------------------------------------------------*/
1329
Milan Brozf6fccb12008-07-21 12:00:37 +01001330static int dm_merge_bvec(struct request_queue *q,
1331 struct bvec_merge_data *bvm,
1332 struct bio_vec *biovec)
1333{
1334 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001335 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001336 struct dm_target *ti;
1337 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001338 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001339
1340 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001341 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001342
1343 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001344 if (!dm_target_is_valid(ti))
1345 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001346
1347 /*
1348 * Find maximum amount of I/O that won't need splitting
1349 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001350 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001351 (sector_t) BIO_MAX_SECTORS);
1352 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1353 if (max_size < 0)
1354 max_size = 0;
1355
1356 /*
1357 * merge_bvec_fn() returns number of bytes
1358 * it can accept at this offset
1359 * max is precomputed maximal io size
1360 */
1361 if (max_size && ti->type->merge)
1362 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001363 /*
1364 * If the target doesn't support merge method and some of the devices
1365 * provided their merge_bvec method (we know this by looking at
1366 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1367 * entries. So always set max_size to 0, and the code below allows
1368 * just one page.
1369 */
1370 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1371
1372 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001373
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001374out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001375 dm_table_put(map);
1376
1377out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001378 /*
1379 * Always allow an entire first page
1380 */
1381 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1382 max_size = biovec->bv_len;
1383
Milan Brozf6fccb12008-07-21 12:00:37 +01001384 return max_size;
1385}
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387/*
1388 * The request function that just remaps the bio built up by
1389 * dm_merge_bvec.
1390 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001391static void _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Kevin Corry12f03a42006-02-01 03:04:52 -08001393 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001395 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001397 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Tejun Heo074a7ac2008-08-25 19:56:14 +09001399 cpu = part_stat_lock();
1400 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1401 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1402 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001403
Tejun Heo6a8736d2010-09-08 18:07:00 +02001404 /* if we're suspended, we have to queue this io for later */
1405 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001406 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Tejun Heo6a8736d2010-09-08 18:07:00 +02001408 if (bio_rw(bio) != READA)
1409 queue_io(md, bio);
1410 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001411 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001412 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001415 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001416 up_read(&md->io_lock);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001420static int dm_request_based(struct mapped_device *md)
1421{
1422 return blk_queue_stackable(md->queue);
1423}
1424
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001425static void dm_request(struct request_queue *q, struct bio *bio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001426{
1427 struct mapped_device *md = q->queuedata;
1428
1429 if (dm_request_based(md))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001430 blk_queue_bio(q, bio);
1431 else
1432 _dm_request(q, bio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001433}
1434
1435void dm_dispatch_request(struct request *rq)
1436{
1437 int r;
1438
1439 if (blk_queue_io_stat(rq->q))
1440 rq->cmd_flags |= REQ_IO_STAT;
1441
1442 rq->start_time = jiffies;
1443 r = blk_insert_cloned_request(rq->q, rq);
1444 if (r)
1445 dm_complete_request(rq, r);
1446}
1447EXPORT_SYMBOL_GPL(dm_dispatch_request);
1448
1449static void dm_rq_bio_destructor(struct bio *bio)
1450{
1451 struct dm_rq_clone_bio_info *info = bio->bi_private;
1452 struct mapped_device *md = info->tio->md;
1453
1454 free_bio_info(info);
1455 bio_free(bio, md->bs);
1456}
1457
1458static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1459 void *data)
1460{
1461 struct dm_rq_target_io *tio = data;
1462 struct mapped_device *md = tio->md;
1463 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1464
1465 if (!info)
1466 return -ENOMEM;
1467
1468 info->orig = bio_orig;
1469 info->tio = tio;
1470 bio->bi_end_io = end_clone_bio;
1471 bio->bi_private = info;
1472 bio->bi_destructor = dm_rq_bio_destructor;
1473
1474 return 0;
1475}
1476
1477static int setup_clone(struct request *clone, struct request *rq,
1478 struct dm_rq_target_io *tio)
1479{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001480 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001481
Tejun Heo29e40132010-09-08 18:07:00 +02001482 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1483 dm_rq_bio_constructor, tio);
1484 if (r)
1485 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001486
Tejun Heo29e40132010-09-08 18:07:00 +02001487 clone->cmd = rq->cmd;
1488 clone->cmd_len = rq->cmd_len;
1489 clone->sense = rq->sense;
1490 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001491 clone->end_io = end_clone_request;
1492 clone->end_io_data = tio;
1493
1494 return 0;
1495}
1496
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001497static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1498 gfp_t gfp_mask)
1499{
1500 struct request *clone;
1501 struct dm_rq_target_io *tio;
1502
1503 tio = alloc_rq_tio(md, gfp_mask);
1504 if (!tio)
1505 return NULL;
1506
1507 tio->md = md;
1508 tio->ti = NULL;
1509 tio->orig = rq;
1510 tio->error = 0;
1511 memset(&tio->info, 0, sizeof(tio->info));
1512
1513 clone = &tio->clone;
1514 if (setup_clone(clone, rq, tio)) {
1515 /* -ENOMEM */
1516 free_rq_tio(tio);
1517 return NULL;
1518 }
1519
1520 return clone;
1521}
1522
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001523/*
1524 * Called with the queue lock held.
1525 */
1526static int dm_prep_fn(struct request_queue *q, struct request *rq)
1527{
1528 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001529 struct request *clone;
1530
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001531 if (unlikely(rq->special)) {
1532 DMWARN("Already has something in rq->special.");
1533 return BLKPREP_KILL;
1534 }
1535
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001536 clone = clone_rq(rq, md, GFP_ATOMIC);
1537 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001538 return BLKPREP_DEFER;
1539
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001540 rq->special = clone;
1541 rq->cmd_flags |= REQ_DONTPREP;
1542
1543 return BLKPREP_OK;
1544}
1545
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001546/*
1547 * Returns:
1548 * 0 : the request has been processed (not requeued)
1549 * !0 : the request has been requeued
1550 */
1551static int map_request(struct dm_target *ti, struct request *clone,
1552 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001553{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001554 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001555 struct dm_rq_target_io *tio = clone->end_io_data;
1556
1557 /*
1558 * Hold the md reference here for the in-flight I/O.
1559 * We can't rely on the reference count by device opener,
1560 * because the device may be closed during the request completion
1561 * when all bios are completed.
1562 * See the comment in rq_completed() too.
1563 */
1564 dm_get(md);
1565
1566 tio->ti = ti;
1567 r = ti->type->map_rq(ti, clone, &tio->info);
1568 switch (r) {
1569 case DM_MAPIO_SUBMITTED:
1570 /* The target has taken the I/O to submit by itself later */
1571 break;
1572 case DM_MAPIO_REMAPPED:
1573 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001574 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1575 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001576 dm_dispatch_request(clone);
1577 break;
1578 case DM_MAPIO_REQUEUE:
1579 /* The target wants to requeue the I/O */
1580 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001581 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001582 break;
1583 default:
1584 if (r > 0) {
1585 DMWARN("unimplemented target map return value: %d", r);
1586 BUG();
1587 }
1588
1589 /* The target wants to complete the I/O */
1590 dm_kill_unmapped_request(clone, r);
1591 break;
1592 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001593
1594 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001595}
1596
1597/*
1598 * q->request_fn for request-based dm.
1599 * Called with the queue lock held.
1600 */
1601static void dm_request_fn(struct request_queue *q)
1602{
1603 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001604 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001605 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001606 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001607 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001608
1609 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001610 * For suspend, check blk_queue_stopped() and increment
1611 * ->pending within a single queue_lock not to increment the
1612 * number of in-flight I/Os after the queue is stopped in
1613 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001614 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001615 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001616 rq = blk_peek_request(q);
1617 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001618 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001619
Tejun Heo29e40132010-09-08 18:07:00 +02001620 /* always use block 0 to find the target for flushes for now */
1621 pos = 0;
1622 if (!(rq->cmd_flags & REQ_FLUSH))
1623 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001624
Tejun Heo29e40132010-09-08 18:07:00 +02001625 ti = dm_table_find_target(map, pos);
1626 BUG_ON(!dm_target_is_valid(ti));
1627
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001628 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001629 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001630
1631 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001632 clone = rq->special;
1633 atomic_inc(&md->pending[rq_data_dir(clone)]);
1634
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001635 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001636 if (map_request(ti, clone, md))
1637 goto requeued;
1638
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001639 BUG_ON(!irqs_disabled());
1640 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001641 }
1642
1643 goto out;
1644
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001645requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001646 BUG_ON(!irqs_disabled());
1647 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001648
Jens Axboe7eaceac2011-03-10 08:52:07 +01001649delay_and_out:
1650 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001651out:
1652 dm_table_put(map);
1653
1654 return;
1655}
1656
1657int dm_underlying_device_busy(struct request_queue *q)
1658{
1659 return blk_lld_busy(q);
1660}
1661EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1662
1663static int dm_lld_busy(struct request_queue *q)
1664{
1665 int r;
1666 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001667 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001668
1669 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1670 r = 1;
1671 else
1672 r = dm_table_any_busy_target(map);
1673
1674 dm_table_put(map);
1675
1676 return r;
1677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679static int dm_any_congested(void *congested_data, int bdi_bits)
1680{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001681 int r = bdi_bits;
1682 struct mapped_device *md = congested_data;
1683 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001685 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001686 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001687 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001688 /*
1689 * Request-based dm cares about only own queue for
1690 * the query about congestion status of request_queue
1691 */
1692 if (dm_request_based(md))
1693 r = md->queue->backing_dev_info.state &
1694 bdi_bits;
1695 else
1696 r = dm_table_any_congested(map, bdi_bits);
1697
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001698 dm_table_put(map);
1699 }
1700 }
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return r;
1703}
1704
1705/*-----------------------------------------------------------------
1706 * An IDR is used to keep track of allocated minor numbers.
1707 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001708static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001710 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001712 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
1715/*
1716 * See if the device with a specific minor # is free.
1717 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001718static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 int r, m;
1721
1722 if (minor >= (1 << MINORBITS))
1723 return -EINVAL;
1724
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001725 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1726 if (!r)
1727 return -ENOMEM;
1728
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001729 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 if (idr_find(&_minor_idr, minor)) {
1732 r = -EBUSY;
1733 goto out;
1734 }
1735
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001736 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001737 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 if (m != minor) {
1741 idr_remove(&_minor_idr, m);
1742 r = -EBUSY;
1743 goto out;
1744 }
1745
1746out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001747 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return r;
1749}
1750
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001751static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001753 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001756 if (!r)
1757 return -ENOMEM;
1758
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001759 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001761 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001762 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 if (m >= (1 << MINORBITS)) {
1766 idr_remove(&_minor_idr, m);
1767 r = -ENOSPC;
1768 goto out;
1769 }
1770
1771 *minor = m;
1772
1773out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001774 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return r;
1776}
1777
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001778static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Mikulas Patocka53d59142009-04-02 19:55:37 +01001780static void dm_wq_work(struct work_struct *work);
1781
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001782static void dm_init_md_queue(struct mapped_device *md)
1783{
1784 /*
1785 * Request-based dm devices cannot be stacked on top of bio-based dm
1786 * devices. The type of this dm device has not been decided yet.
1787 * The type is decided at the first table loading time.
1788 * To prevent problematic device stacking, clear the queue flag
1789 * for request stacking support until then.
1790 *
1791 * This queue is new, so no concurrency on the queue_flags.
1792 */
1793 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1794
1795 md->queue->queuedata = md;
1796 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1797 md->queue->backing_dev_info.congested_data = md;
1798 blk_queue_make_request(md->queue, dm_request);
1799 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001800 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*
1804 * Allocate and initialise a blank device with a given minor.
1805 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001806static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001809 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001810 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 if (!md) {
1813 DMWARN("unable to allocate device, out of memory.");
1814 return NULL;
1815 }
1816
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001817 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001818 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001821 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001822 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001823 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001824 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001826 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Mike Snitzera5664da2010-08-12 04:14:01 +01001828 md->type = DM_TYPE_NONE;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001829 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001830 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001831 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001832 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 rwlock_init(&md->map_lock);
1834 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001835 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001837 atomic_set(&md->uevent_seq, 0);
1838 INIT_LIST_HEAD(&md->uevent_list);
1839 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001841 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001843 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001845 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 md->disk = alloc_disk(1);
1848 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001849 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001851 atomic_set(&md->pending[0], 0);
1852 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001853 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001854 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001855 init_waitqueue_head(&md->eventq);
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 md->disk->major = _major;
1858 md->disk->first_minor = minor;
1859 md->disk->fops = &dm_blk_dops;
1860 md->disk->queue = md->queue;
1861 md->disk->private_data = md;
1862 sprintf(md->disk->disk_name, "dm-%d", minor);
1863 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001864 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Tejun Heo9c4376d2011-01-13 19:59:58 +00001866 md->wq = alloc_workqueue("kdmflush",
1867 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001868 if (!md->wq)
1869 goto bad_thread;
1870
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001871 md->bdev = bdget_disk(md->disk, 0);
1872 if (!md->bdev)
1873 goto bad_bdev;
1874
Tejun Heo6a8736d2010-09-08 18:07:00 +02001875 bio_init(&md->flush_bio);
1876 md->flush_bio.bi_bdev = md->bdev;
1877 md->flush_bio.bi_rw = WRITE_FLUSH;
1878
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001879 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001880 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001881 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001882 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001883
1884 BUG_ON(old_md != MINOR_ALLOCED);
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return md;
1887
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001888bad_bdev:
1889 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001890bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001891 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001892 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001893bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001894 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001895bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001897bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001898 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001899bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 kfree(md);
1901 return NULL;
1902}
1903
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001904static void unlock_fs(struct mapped_device *md);
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906static void free_dev(struct mapped_device *md)
1907{
Tejun Heof331c022008-09-03 09:01:48 +02001908 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001909
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001910 unlock_fs(md);
1911 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001912 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001913 if (md->tio_pool)
1914 mempool_destroy(md->tio_pool);
1915 if (md->io_pool)
1916 mempool_destroy(md->io_pool);
1917 if (md->bs)
1918 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001919 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001921 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001922
1923 spin_lock(&_minor_lock);
1924 md->disk->private_data = NULL;
1925 spin_unlock(&_minor_lock);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001928 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001929 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 kfree(md);
1931}
1932
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001933static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1934{
1935 struct dm_md_mempools *p;
1936
1937 if (md->io_pool && md->tio_pool && md->bs)
1938 /* the md already has necessary mempools */
1939 goto out;
1940
1941 p = dm_table_get_md_mempools(t);
1942 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1943
1944 md->io_pool = p->io_pool;
1945 p->io_pool = NULL;
1946 md->tio_pool = p->tio_pool;
1947 p->tio_pool = NULL;
1948 md->bs = p->bs;
1949 p->bs = NULL;
1950
1951out:
1952 /* mempool bind completed, now no need any mempools in the table */
1953 dm_table_free_md_mempools(t);
1954}
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956/*
1957 * Bind a table to the device.
1958 */
1959static void event_callback(void *context)
1960{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001961 unsigned long flags;
1962 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 struct mapped_device *md = (struct mapped_device *) context;
1964
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001965 spin_lock_irqsave(&md->uevent_lock, flags);
1966 list_splice_init(&md->uevent_list, &uevents);
1967 spin_unlock_irqrestore(&md->uevent_lock, flags);
1968
Tejun Heoed9e1982008-08-25 19:56:05 +09001969 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 atomic_inc(&md->event_nr);
1972 wake_up(&md->eventq);
1973}
1974
Mike Snitzerc2176492011-01-13 19:53:46 +00001975/*
1976 * Protected by md->suspend_lock obtained by dm_swap_table().
1977 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001978static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001980 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001982 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001985/*
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01001986 * Return 1 if the queue has a compulsory merge_bvec_fn function.
1987 *
1988 * If this function returns 0, then the device is either a non-dm
1989 * device without a merge_bvec_fn, or it is a dm device that is
1990 * able to split any bios it receives that are too big.
1991 */
1992int dm_queue_merge_is_compulsory(struct request_queue *q)
1993{
1994 struct mapped_device *dev_md;
1995
1996 if (!q->merge_bvec_fn)
1997 return 0;
1998
1999 if (q->make_request_fn == dm_request) {
2000 dev_md = q->queuedata;
2001 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2002 return 0;
2003 }
2004
2005 return 1;
2006}
2007
2008static int dm_device_merge_is_compulsory(struct dm_target *ti,
2009 struct dm_dev *dev, sector_t start,
2010 sector_t len, void *data)
2011{
2012 struct block_device *bdev = dev->bdev;
2013 struct request_queue *q = bdev_get_queue(bdev);
2014
2015 return dm_queue_merge_is_compulsory(q);
2016}
2017
2018/*
2019 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2020 * on the properties of the underlying devices.
2021 */
2022static int dm_table_merge_is_optional(struct dm_table *table)
2023{
2024 unsigned i = 0;
2025 struct dm_target *ti;
2026
2027 while (i < dm_table_get_num_targets(table)) {
2028 ti = dm_table_get_target(table, i++);
2029
2030 if (ti->type->iterate_devices &&
2031 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2032 return 0;
2033 }
2034
2035 return 1;
2036}
2037
2038/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002039 * Returns old map, which caller must destroy.
2040 */
2041static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2042 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002044 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002045 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002047 unsigned long flags;
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002048 int merge_is_optional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002051
2052 /*
2053 * Wipe any geometry if the size of the table changed.
2054 */
2055 if (size != get_capacity(md->disk))
2056 memset(&md->geometry, 0, sizeof(md->geometry));
2057
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002058 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002060 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002061
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002062 /*
2063 * The queue hasn't been stopped yet, if the old table type wasn't
2064 * for request-based during suspension. So stop it to prevent
2065 * I/O mapping before resume.
2066 * This must be done before setting the queue restrictions,
2067 * because request-based dm may be run just after the setting.
2068 */
2069 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2070 stop_queue(q);
2071
2072 __bind_mempools(md, t);
2073
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002074 merge_is_optional = dm_table_merge_is_optional(t);
2075
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002076 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002077 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002078 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002079 dm_table_set_restrictions(t, q, limits);
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002080 if (merge_is_optional)
2081 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2082 else
2083 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002084 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002085
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002086 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Alasdair G Kergona7940152009-12-10 23:52:23 +00002089/*
2090 * Returns unbound table for the caller to free.
2091 */
2092static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002095 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002098 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002101 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002103 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002104
2105 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
2108/*
2109 * Constructor for a new device.
2110 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002111int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct mapped_device *md;
2114
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002115 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (!md)
2117 return -ENXIO;
2118
Milan Broz784aae72009-01-06 03:05:12 +00002119 dm_sysfs_init(md);
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 *result = md;
2122 return 0;
2123}
2124
Mike Snitzera5664da2010-08-12 04:14:01 +01002125/*
2126 * Functions to manage md->type.
2127 * All are required to hold md->type_lock.
2128 */
2129void dm_lock_md_type(struct mapped_device *md)
2130{
2131 mutex_lock(&md->type_lock);
2132}
2133
2134void dm_unlock_md_type(struct mapped_device *md)
2135{
2136 mutex_unlock(&md->type_lock);
2137}
2138
2139void dm_set_md_type(struct mapped_device *md, unsigned type)
2140{
2141 md->type = type;
2142}
2143
2144unsigned dm_get_md_type(struct mapped_device *md)
2145{
2146 return md->type;
2147}
2148
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002149/*
2150 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2151 */
2152static int dm_init_request_based_queue(struct mapped_device *md)
2153{
2154 struct request_queue *q = NULL;
2155
2156 if (md->queue->elevator)
2157 return 1;
2158
2159 /* Fully initialize the queue */
2160 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2161 if (!q)
2162 return 0;
2163
2164 md->queue = q;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002165 dm_init_md_queue(md);
2166 blk_queue_softirq_done(md->queue, dm_softirq_done);
2167 blk_queue_prep_rq(md->queue, dm_prep_fn);
2168 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002169
2170 elv_register_queue(md->queue);
2171
2172 return 1;
2173}
2174
2175/*
2176 * Setup the DM device's queue based on md's type
2177 */
2178int dm_setup_md_queue(struct mapped_device *md)
2179{
2180 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2181 !dm_init_request_based_queue(md)) {
2182 DMWARN("Cannot initialize queue for request-based mapped device");
2183 return -EINVAL;
2184 }
2185
2186 return 0;
2187}
2188
David Teigland637842c2006-01-06 00:20:00 -08002189static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 unsigned minor = MINOR(dev);
2193
2194 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2195 return NULL;
2196
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002197 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002200 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002201 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002202 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002203 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002204 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002205 goto out;
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002208out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002209 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
David Teigland637842c2006-01-06 00:20:00 -08002211 return md;
2212}
2213
David Teiglandd229a952006-01-06 00:20:01 -08002214struct mapped_device *dm_get_md(dev_t dev)
2215{
2216 struct mapped_device *md = dm_find_md(dev);
2217
2218 if (md)
2219 dm_get(md);
2220
2221 return md;
2222}
2223
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002224void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002225{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002226 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229void dm_set_mdptr(struct mapped_device *md, void *ptr)
2230{
2231 md->interface_ptr = ptr;
2232}
2233
2234void dm_get(struct mapped_device *md)
2235{
2236 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002237 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002240const char *dm_device_name(struct mapped_device *md)
2241{
2242 return md->name;
2243}
2244EXPORT_SYMBOL_GPL(dm_device_name);
2245
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002246static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002248 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002250 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002251
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002252 spin_lock(&_minor_lock);
2253 map = dm_get_live_table(md);
2254 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2255 set_bit(DMF_FREEING, &md->flags);
2256 spin_unlock(&_minor_lock);
2257
2258 if (!dm_suspended_md(md)) {
2259 dm_table_presuspend_targets(map);
2260 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002262
2263 /*
2264 * Rare, but there may be I/O requests still going to complete,
2265 * for example. Wait for all references to disappear.
2266 * No one should increment the reference count of the mapped_device,
2267 * after the mapped_device state becomes DMF_FREEING.
2268 */
2269 if (wait)
2270 while (atomic_read(&md->holders))
2271 msleep(1);
2272 else if (atomic_read(&md->holders))
2273 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2274 dm_device_name(md), atomic_read(&md->holders));
2275
2276 dm_sysfs_exit(md);
2277 dm_table_put(map);
2278 dm_table_destroy(__unbind(md));
2279 free_dev(md);
2280}
2281
2282void dm_destroy(struct mapped_device *md)
2283{
2284 __dm_destroy(md, true);
2285}
2286
2287void dm_destroy_immediate(struct mapped_device *md)
2288{
2289 __dm_destroy(md, false);
2290}
2291
2292void dm_put(struct mapped_device *md)
2293{
2294 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
Edward Goggin79eb8852007-05-09 02:32:56 -07002296EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Mikulas Patocka401600d2009-04-02 19:55:38 +01002298static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002299{
2300 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002301 DECLARE_WAITQUEUE(wait, current);
2302
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002303 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002304
2305 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002306 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002307
2308 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002309 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002310 break;
2311
Mikulas Patocka401600d2009-04-02 19:55:38 +01002312 if (interruptible == TASK_INTERRUPTIBLE &&
2313 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002314 r = -EINTR;
2315 break;
2316 }
2317
2318 io_schedule();
2319 }
2320 set_current_state(TASK_RUNNING);
2321
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002322 remove_wait_queue(&md->wait, &wait);
2323
Milan Broz46125c12008-02-08 02:10:30 +00002324 return r;
2325}
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/*
2328 * Process the deferred bios
2329 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002330static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Mikulas Patockaef208582009-04-02 19:55:38 +01002332 struct mapped_device *md = container_of(work, struct mapped_device,
2333 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002334 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Tejun Heo6a8736d2010-09-08 18:07:00 +02002336 down_read(&md->io_lock);
Mikulas Patockaef208582009-04-02 19:55:38 +01002337
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002338 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002339 spin_lock_irq(&md->deferred_lock);
2340 c = bio_list_pop(&md->deferred);
2341 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002342
Tejun Heo6a8736d2010-09-08 18:07:00 +02002343 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002344 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002345
Tejun Heo6a8736d2010-09-08 18:07:00 +02002346 up_read(&md->io_lock);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002347
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002348 if (dm_request_based(md))
2349 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002350 else
2351 __split_and_process_bio(md, c);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002352
Tejun Heo6a8736d2010-09-08 18:07:00 +02002353 down_read(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002354 }
Milan Broz73d410c2008-02-08 02:10:25 +00002355
Tejun Heo6a8736d2010-09-08 18:07:00 +02002356 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002359static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002360{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002361 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2362 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002363 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002364}
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002367 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002369struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002371 struct dm_table *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002372 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002373 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Daniel Walkere61290a2008-02-08 02:10:08 +00002375 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002378 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002381 r = dm_calculate_queue_limits(table, &limits);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002382 if (r) {
2383 map = ERR_PTR(r);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002384 goto out;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002385 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002386
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002387 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002389out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002390 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002391 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
2394/*
2395 * Functions to lock and unlock any filesystem running on the
2396 * device.
2397 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002398static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002400 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002403
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002404 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002405 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002406 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002407 md->frozen_sb = NULL;
2408 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002409 }
2410
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002411 set_bit(DMF_FROZEN, &md->flags);
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return 0;
2414}
2415
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002416static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002418 if (!test_bit(DMF_FROZEN, &md->flags))
2419 return;
2420
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002421 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002423 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
2426/*
2427 * We need to be able to change a mapping table under a mounted
2428 * filesystem. For example we might want to move some data in
2429 * the background. Before the table can be swapped with
2430 * dm_bind_table, dm_suspend must be called to flush any in
2431 * flight bios and ensure that any further io gets deferred.
2432 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002433/*
2434 * Suspend mechanism in request-based dm.
2435 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002436 * 1. Flush all I/Os by lock_fs() if needed.
2437 * 2. Stop dispatching any I/O by stopping the request_queue.
2438 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002439 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002440 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002441 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002442int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002444 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002445 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002446 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002447 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Daniel Walkere61290a2008-02-08 02:10:08 +00002449 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002450
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002451 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002452 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002453 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002456 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002458 /*
2459 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2460 * This flag is cleared before dm_suspend returns.
2461 */
2462 if (noflush)
2463 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2464
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002465 /* This does not get reverted if there's an error later. */
2466 dm_table_presuspend_targets(map);
2467
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002468 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002469 * Flush I/O to the device.
2470 * Any I/O submitted after lock_fs() may not be flushed.
2471 * noflush takes precedence over do_lockfs.
2472 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002473 */
2474 if (!noflush && do_lockfs) {
2475 r = lock_fs(md);
2476 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002477 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002481 * Here we must make sure that no processes are submitting requests
2482 * to target drivers i.e. no one may be executing
2483 * __split_and_process_bio. This is called from dm_request and
2484 * dm_wq_work.
2485 *
2486 * To get all processes out of __split_and_process_bio in dm_request,
2487 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002488 * __split_and_process_bio from dm_request and quiesce the thread
2489 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2490 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002492 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002493 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002494 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002496 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002497 * Stop md->queue before flushing md->wq in case request-based
2498 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002499 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002500 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002501 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002502
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002503 flush_workqueue(md->wq);
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002506 * At this point no more requests are entering target request routines.
2507 * We call dm_wait_for_completion to wait for all existing requests
2508 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002510 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002512 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002513 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002514 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002515 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002518 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002519 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002520
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002521 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002522 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002523
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002524 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002525 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002526 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002527
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002528 /*
2529 * If dm_wait_for_completion returned 0, the device is completely
2530 * quiescent now. There is no request-processing activity. All new
2531 * requests are being added to md->deferred list.
2532 */
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 set_bit(DMF_SUSPENDED, &md->flags);
2535
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002536 dm_table_postsuspend_targets(map);
2537
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002538out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002540
2541out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002542 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002543 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
2546int dm_resume(struct mapped_device *md)
2547{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002548 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002549 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Daniel Walkere61290a2008-02-08 02:10:08 +00002551 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002552 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002553 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002554
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002555 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002556 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Milan Broz8757b772006-10-03 01:15:36 -07002559 r = dm_table_resume_targets(map);
2560 if (r)
2561 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002562
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002563 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002564
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002565 /*
2566 * Flushing deferred I/Os must be done after targets are resumed
2567 * so that mapping of targets can work correctly.
2568 * Request-based dm is queueing the deferred I/Os in its request_queue.
2569 */
2570 if (dm_request_based(md))
2571 start_queue(md->queue);
2572
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002573 unlock_fs(md);
2574
2575 clear_bit(DMF_SUSPENDED, &md->flags);
2576
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002577 r = 0;
2578out:
2579 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002580 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002581
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002582 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
2585/*-----------------------------------------------------------------
2586 * Event notification.
2587 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002588int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002589 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002590{
Milan Broz60935eb2009-06-22 10:12:30 +01002591 char udev_cookie[DM_COOKIE_LENGTH];
2592 char *envp[] = { udev_cookie, NULL };
2593
2594 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002595 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002596 else {
2597 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2598 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002599 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2600 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002601 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002602}
2603
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002604uint32_t dm_next_uevent_seq(struct mapped_device *md)
2605{
2606 return atomic_add_return(1, &md->uevent_seq);
2607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609uint32_t dm_get_event_nr(struct mapped_device *md)
2610{
2611 return atomic_read(&md->event_nr);
2612}
2613
2614int dm_wait_event(struct mapped_device *md, int event_nr)
2615{
2616 return wait_event_interruptible(md->eventq,
2617 (event_nr != atomic_read(&md->event_nr)));
2618}
2619
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002620void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2621{
2622 unsigned long flags;
2623
2624 spin_lock_irqsave(&md->uevent_lock, flags);
2625 list_add(elist, &md->uevent_list);
2626 spin_unlock_irqrestore(&md->uevent_lock, flags);
2627}
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629/*
2630 * The gendisk is only valid as long as you have a reference
2631 * count on 'md'.
2632 */
2633struct gendisk *dm_disk(struct mapped_device *md)
2634{
2635 return md->disk;
2636}
2637
Milan Broz784aae72009-01-06 03:05:12 +00002638struct kobject *dm_kobject(struct mapped_device *md)
2639{
2640 return &md->kobj;
2641}
2642
2643/*
2644 * struct mapped_device should not be exported outside of dm.c
2645 * so use this check to verify that kobj is part of md structure
2646 */
2647struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2648{
2649 struct mapped_device *md;
2650
2651 md = container_of(kobj, struct mapped_device, kobj);
2652 if (&md->kobj != kobj)
2653 return NULL;
2654
Milan Broz4d89b7b2009-06-22 10:12:11 +01002655 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002656 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002657 return NULL;
2658
Milan Broz784aae72009-01-06 03:05:12 +00002659 dm_get(md);
2660 return md;
2661}
2662
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002663int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
2665 return test_bit(DMF_SUSPENDED, &md->flags);
2666}
2667
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002668int dm_suspended(struct dm_target *ti)
2669{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002670 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002671}
2672EXPORT_SYMBOL_GPL(dm_suspended);
2673
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002674int dm_noflush_suspending(struct dm_target *ti)
2675{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002676 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002677}
2678EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2679
Martin K. Petersena91a2782011-03-17 11:11:05 +01002680struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002681{
2682 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
Martin K. Petersena91a2782011-03-17 11:11:05 +01002683 unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002684
2685 if (!pools)
2686 return NULL;
2687
2688 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2689 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2690 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2691 if (!pools->io_pool)
2692 goto free_pools_and_out;
2693
2694 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2695 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2696 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2697 if (!pools->tio_pool)
2698 goto free_io_pool_and_out;
2699
Martin K. Petersena91a2782011-03-17 11:11:05 +01002700 pools->bs = bioset_create(pool_size, 0);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002701 if (!pools->bs)
2702 goto free_tio_pool_and_out;
2703
Martin K. Petersena91a2782011-03-17 11:11:05 +01002704 if (integrity && bioset_integrity_create(pools->bs, pool_size))
2705 goto free_bioset_and_out;
2706
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002707 return pools;
2708
Martin K. Petersena91a2782011-03-17 11:11:05 +01002709free_bioset_and_out:
2710 bioset_free(pools->bs);
2711
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002712free_tio_pool_and_out:
2713 mempool_destroy(pools->tio_pool);
2714
2715free_io_pool_and_out:
2716 mempool_destroy(pools->io_pool);
2717
2718free_pools_and_out:
2719 kfree(pools);
2720
2721 return NULL;
2722}
2723
2724void dm_free_md_mempools(struct dm_md_mempools *pools)
2725{
2726 if (!pools)
2727 return;
2728
2729 if (pools->io_pool)
2730 mempool_destroy(pools->io_pool);
2731
2732 if (pools->tio_pool)
2733 mempool_destroy(pools->tio_pool);
2734
2735 if (pools->bs)
2736 bioset_free(pools->bs);
2737
2738 kfree(pools);
2739}
2740
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002741static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 .open = dm_blk_open,
2743 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002744 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002745 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 .owner = THIS_MODULE
2747};
2748
2749EXPORT_SYMBOL(dm_get_mapinfo);
2750
2751/*
2752 * module hooks
2753 */
2754module_init(dm_init);
2755module_exit(dm_exit);
2756
2757module_param(major, uint, 0);
2758MODULE_PARM_DESC(major, "The major number of the device mapper");
2759MODULE_DESCRIPTION(DM_NAME " driver");
2760MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2761MODULE_LICENSE("GPL");