blob: d505a96845c11774228cedd553646f8866cb8f7f [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>
Li Zefan55782132009-06-09 13:43:05 +080022
23#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Milan Broz60935eb2009-06-22 10:12:30 +010027/*
28 * Cookies are numeric values sent with CHANGE and REMOVE
29 * uevents while resuming, removing or renaming the device.
30 */
31#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
32#define DM_COOKIE_LENGTH 24
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static const char *_name = DM_NAME;
35
36static unsigned int major = 0;
37static unsigned int _major = 0;
38
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070039static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000041 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * One of these is allocated per bio.
43 */
44struct dm_io {
45 struct mapped_device *md;
46 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010048 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080049 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010050 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000054 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * One of these is allocated per target within a bio. Hopefully
56 * this will be simplified out one day.
57 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010058struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct dm_io *io;
60 struct dm_target *ti;
61 union map_info info;
62};
63
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000064/*
65 * For request-based dm.
66 * One of these is allocated per request.
67 */
68struct dm_rq_target_io {
69 struct mapped_device *md;
70 struct dm_target *ti;
71 struct request *orig, clone;
72 int error;
73 union map_info info;
74};
75
76/*
77 * For request-based dm.
78 * One of these is allocated per bio.
79 */
80struct dm_rq_clone_bio_info {
81 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010082 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000083};
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085union map_info *dm_get_mapinfo(struct bio *bio)
86{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070087 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010088 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070089 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010092union map_info *dm_get_rq_mapinfo(struct request *rq)
93{
94 if (rq && rq->end_io_data)
95 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
96 return NULL;
97}
98EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
99
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700100#define MINOR_ALLOCED ((void *)-1)
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * Bits for the md->flags field.
104 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100105#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800107#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700108#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700109#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800110#define DMF_NOFLUSH_SUSPENDING 5
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100111#define DMF_QUEUE_IO_TO_THREAD 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Milan Broz304f3f62008-02-08 02:11:17 +0000113/*
114 * Work processed by per-device workqueue.
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700117 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000118 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 rwlock_t map_lock;
120 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700121 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 unsigned long flags;
124
Jens Axboe165125e2007-07-24 09:28:11 +0200125 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800127 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 void *interface_ptr;
130
131 /*
132 * A list of ios that arrived while we were suspended.
133 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200134 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100136 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800137 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100138 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /*
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100141 * An error from the barrier request currently being processed.
142 */
143 int barrier_error;
144
145 /*
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000146 * Protect barrier_error from concurrent endio processing
147 * in request-based dm.
148 */
149 spinlock_t barrier_error_lock;
150
151 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000152 * Processing queue (flush/barriers)
153 */
154 struct workqueue_struct *wq;
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000155 struct work_struct barrier_work;
156
157 /* A pointer to the currently processing pre/post flush request */
158 struct request *flush_request;
Milan Broz304f3f62008-02-08 02:11:17 +0000159
160 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * The current mapping.
162 */
163 struct dm_table *map;
164
165 /*
166 * io objects are allocated from here.
167 */
168 mempool_t *io_pool;
169 mempool_t *tio_pool;
170
Stefan Bader9faf4002006-10-03 01:15:41 -0700171 struct bio_set *bs;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /*
174 * Event handling.
175 */
176 atomic_t event_nr;
177 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100178 atomic_t uevent_seq;
179 struct list_head uevent_list;
180 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /*
183 * freeze/thaw support require holding onto a super block
184 */
185 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100186 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800187
188 /* forced geometry settings */
189 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000190
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100191 /* For saving the address of __make_request for request based dm */
192 make_request_fn *saved_make_request_fn;
193
Milan Broz784aae72009-01-06 03:05:12 +0000194 /* sysfs handle */
195 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100196
197 /* zero-length barrier that will be cloned and submitted to targets */
198 struct bio barrier_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100201/*
202 * For mempools pre-allocation at the table loading time.
203 */
204struct dm_md_mempools {
205 mempool_t *io_pool;
206 mempool_t *tio_pool;
207 struct bio_set *bs;
208};
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800211static struct kmem_cache *_io_cache;
212static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000213static struct kmem_cache *_rq_tio_cache;
214static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static int __init local_init(void)
217{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100218 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100221 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100223 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100226 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100227 if (!_tio_cache)
228 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000230 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
231 if (!_rq_tio_cache)
232 goto out_free_tio_cache;
233
234 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
235 if (!_rq_bio_info_cache)
236 goto out_free_rq_tio_cache;
237
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100238 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100239 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000240 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 _major = major;
243 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100244 if (r < 0)
245 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (!_major)
248 _major = r;
249
250 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100251
252out_uevent_exit:
253 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000254out_free_rq_bio_info_cache:
255 kmem_cache_destroy(_rq_bio_info_cache);
256out_free_rq_tio_cache:
257 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100258out_free_tio_cache:
259 kmem_cache_destroy(_tio_cache);
260out_free_io_cache:
261 kmem_cache_destroy(_io_cache);
262
263 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static void local_exit(void)
267{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000268 kmem_cache_destroy(_rq_bio_info_cache);
269 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 kmem_cache_destroy(_tio_cache);
271 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700272 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100273 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 _major = 0;
276
277 DMINFO("cleaned up");
278}
279
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000280static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 local_init,
282 dm_target_init,
283 dm_linear_init,
284 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000285 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100286 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dm_interface_init,
288};
289
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000290static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 local_exit,
292 dm_target_exit,
293 dm_linear_exit,
294 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000295 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100296 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 dm_interface_exit,
298};
299
300static int __init dm_init(void)
301{
302 const int count = ARRAY_SIZE(_inits);
303
304 int r, i;
305
306 for (i = 0; i < count; i++) {
307 r = _inits[i]();
308 if (r)
309 goto bad;
310 }
311
312 return 0;
313
314 bad:
315 while (i--)
316 _exits[i]();
317
318 return r;
319}
320
321static void __exit dm_exit(void)
322{
323 int i = ARRAY_SIZE(_exits);
324
325 while (i--)
326 _exits[i]();
327}
328
329/*
330 * Block device functions
331 */
Mike Anderson432a2122009-12-10 23:52:20 +0000332int dm_deleting_md(struct mapped_device *md)
333{
334 return test_bit(DMF_DELETING, &md->flags);
335}
336
Al Virofe5f9f22008-03-02 10:29:31 -0500337static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct mapped_device *md;
340
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700341 spin_lock(&_minor_lock);
342
Al Virofe5f9f22008-03-02 10:29:31 -0500343 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700344 if (!md)
345 goto out;
346
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700347 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000348 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700349 md = NULL;
350 goto out;
351 }
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700354 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700355
356out:
357 spin_unlock(&_minor_lock);
358
359 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Al Virofe5f9f22008-03-02 10:29:31 -0500362static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Al Virofe5f9f22008-03-02 10:29:31 -0500364 struct mapped_device *md = disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700365 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 dm_put(md);
367 return 0;
368}
369
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700370int dm_open_count(struct mapped_device *md)
371{
372 return atomic_read(&md->open_count);
373}
374
375/*
376 * Guarantees nothing is using the device before it's deleted.
377 */
378int dm_lock_for_deletion(struct mapped_device *md)
379{
380 int r = 0;
381
382 spin_lock(&_minor_lock);
383
384 if (dm_open_count(md))
385 r = -EBUSY;
386 else
387 set_bit(DMF_DELETING, &md->flags);
388
389 spin_unlock(&_minor_lock);
390
391 return r;
392}
393
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800394static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
395{
396 struct mapped_device *md = bdev->bd_disk->private_data;
397
398 return dm_get_geometry(md, geo);
399}
400
Al Virofe5f9f22008-03-02 10:29:31 -0500401static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700402 unsigned int cmd, unsigned long arg)
403{
Al Virofe5f9f22008-03-02 10:29:31 -0500404 struct mapped_device *md = bdev->bd_disk->private_data;
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000405 struct dm_table *map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700406 struct dm_target *tgt;
407 int r = -ENOTTY;
408
Milan Brozaa129a22006-10-03 01:15:15 -0700409 if (!map || !dm_table_get_size(map))
410 goto out;
411
412 /* We only support devices that have a single target */
413 if (dm_table_get_num_targets(map) != 1)
414 goto out;
415
416 tgt = dm_table_get_target(map, 0);
417
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000418 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700419 r = -EAGAIN;
420 goto out;
421 }
422
423 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400424 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700425
426out:
427 dm_table_put(map);
428
Milan Brozaa129a22006-10-03 01:15:15 -0700429 return r;
430}
431
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100432static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 return mempool_alloc(md->io_pool, GFP_NOIO);
435}
436
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100437static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 mempool_free(io, md->io_pool);
440}
441
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100442static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 mempool_free(tio, md->tio_pool);
445}
446
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000447static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
448 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100449{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000450 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100451}
452
453static void free_rq_tio(struct dm_rq_target_io *tio)
454{
455 mempool_free(tio, tio->md->tio_pool);
456}
457
458static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
459{
460 return mempool_alloc(md->io_pool, GFP_ATOMIC);
461}
462
463static void free_bio_info(struct dm_rq_clone_bio_info *info)
464{
465 mempool_free(info, info->tio->md->io_pool);
466}
467
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000468static int md_in_flight(struct mapped_device *md)
469{
470 return atomic_read(&md->pending[READ]) +
471 atomic_read(&md->pending[WRITE]);
472}
473
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800474static void start_io_acct(struct dm_io *io)
475{
476 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900477 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200478 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800479
480 io->start_time = jiffies;
481
Tejun Heo074a7ac2008-08-25 19:56:14 +0900482 cpu = part_stat_lock();
483 part_round_stats(cpu, &dm_disk(md)->part0);
484 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200485 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800486}
487
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000488static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800489{
490 struct mapped_device *md = io->md;
491 struct bio *bio = io->bio;
492 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900493 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800494 int rw = bio_data_dir(bio);
495
Tejun Heo074a7ac2008-08-25 19:56:14 +0900496 cpu = part_stat_lock();
497 part_round_stats(cpu, &dm_disk(md)->part0);
498 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
499 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800500
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100501 /*
502 * After this is decremented the bio must not be touched if it is
503 * a barrier.
504 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200505 dm_disk(md)->part0.in_flight[rw] = pending =
506 atomic_dec_return(&md->pending[rw]);
507 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800508
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000509 /* nudge anyone waiting on suspend queue */
510 if (!pending)
511 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/*
515 * Add the bio to the list of deferred io.
516 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100517static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700519 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Mikulas Patocka022c2612009-04-02 19:55:39 +0100521 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100523 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Mikulas Patocka92c63902009-04-09 00:27:15 +0100525 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
526 queue_work(md->wq, &md->work);
527
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700528 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
532 * Everyone (including functions in this file), should use this
533 * function to access the md->map field, and make sure they call
534 * dm_table_put() when finished.
535 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000536struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100539 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100541 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 t = md->map;
543 if (t)
544 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100545 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 return t;
548}
549
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800550/*
551 * Get the geometry associated with a dm device
552 */
553int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
554{
555 *geo = md->geometry;
556
557 return 0;
558}
559
560/*
561 * Set the geometry of a device.
562 */
563int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
564{
565 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
566
567 if (geo->start > sz) {
568 DMWARN("Start sector is beyond the geometry limits.");
569 return -EINVAL;
570 }
571
572 md->geometry = *geo;
573
574 return 0;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*-----------------------------------------------------------------
578 * CRUD START:
579 * A more elegant soln is in the works that uses the queue
580 * merge fn, unfortunately there are a couple of changes to
581 * the block layer that I want to make for this. So in the
582 * interests of getting something for people to use I give
583 * you this clearly demarcated crap.
584 *---------------------------------------------------------------*/
585
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800586static int __noflush_suspending(struct mapped_device *md)
587{
588 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*
592 * Decrements the number of outstanding ios that a bio has been
593 * cloned into, completing the original io if necc.
594 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800595static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800597 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000598 int io_error;
599 struct bio *bio;
600 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800601
602 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100603 if (unlikely(error)) {
604 spin_lock_irqsave(&io->endio_lock, flags);
605 if (!(io->error > 0 && __noflush_suspending(md)))
606 io->error = error;
607 spin_unlock_irqrestore(&io->endio_lock, flags);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800611 if (io->error == DM_ENDIO_REQUEUE) {
612 /*
613 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800614 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100615 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100616 if (__noflush_suspending(md)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200617 if (!(io->bio->bi_rw & REQ_HARDBARRIER))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100618 bio_list_add_head(&md->deferred,
619 io->bio);
620 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800621 /* noflush suspend was interrupted. */
622 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100623 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800624 }
625
Milan Brozb35f8ca2009-03-16 17:44:36 +0000626 io_error = io->error;
627 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100628
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200629 if (bio->bi_rw & REQ_HARDBARRIER) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100630 /*
631 * There can be just one barrier request so we use
632 * a per-device variable for error reporting.
633 * Note that you can't touch the bio after end_io_acct
634 */
Mikulas Patockafdb95722009-06-22 10:12:19 +0100635 if (!md->barrier_error && io_error != -EOPNOTSUPP)
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100636 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100637 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000638 free_io(md, io);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100639 } else {
640 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000641 free_io(md, io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000642
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100643 if (io_error != DM_ENDIO_REQUEUE) {
644 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000645
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100646 bio_endio(bio, io_error);
647 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650}
651
NeilBrown6712ecf2007-09-27 12:47:43 +0200652static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100655 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000656 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700657 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dm_endio_fn endio = tio->ti->type->end_io;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
661 error = -EIO;
662
663 if (endio) {
664 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800665 if (r < 0 || r == DM_ENDIO_REQUEUE)
666 /*
667 * error and requeue request are handled
668 * in dec_pending().
669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800671 else if (r == DM_ENDIO_INCOMPLETE)
672 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200673 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800674 else if (r) {
675 DMWARN("unimplemented target endio return value: %d", r);
676 BUG();
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Stefan Bader9faf4002006-10-03 01:15:41 -0700680 /*
681 * Store md for cleanup instead of tio which is about to get freed.
682 */
683 bio->bi_private = md->bs;
684
Stefan Bader9faf4002006-10-03 01:15:41 -0700685 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000686 bio_put(bio);
687 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100690/*
691 * Partial completion handling for request-based dm
692 */
693static void end_clone_bio(struct bio *clone, int error)
694{
695 struct dm_rq_clone_bio_info *info = clone->bi_private;
696 struct dm_rq_target_io *tio = info->tio;
697 struct bio *bio = info->orig;
698 unsigned int nr_bytes = info->orig->bi_size;
699
700 bio_put(clone);
701
702 if (tio->error)
703 /*
704 * An error has already been detected on the request.
705 * Once error occurred, just let clone->end_io() handle
706 * the remainder.
707 */
708 return;
709 else if (error) {
710 /*
711 * Don't notice the error to the upper layer yet.
712 * The error handling decision is made by the target driver,
713 * when the request is completed.
714 */
715 tio->error = error;
716 return;
717 }
718
719 /*
720 * I/O for the bio successfully completed.
721 * Notice the data completion to the upper layer.
722 */
723
724 /*
725 * bios are processed from the head of the list.
726 * So the completing bio should always be rq->bio.
727 * If it's not, something wrong is happening.
728 */
729 if (tio->orig->bio != bio)
730 DMERR("bio completion is going in the middle of the request");
731
732 /*
733 * Update the original request.
734 * Do not use blk_end_request() here, because it may complete
735 * the original request before the clone, and break the ordering.
736 */
737 blk_update_request(tio->orig, 0, nr_bytes);
738}
739
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000740static void store_barrier_error(struct mapped_device *md, int error)
741{
742 unsigned long flags;
743
744 spin_lock_irqsave(&md->barrier_error_lock, flags);
745 /*
746 * Basically, the first error is taken, but:
747 * -EOPNOTSUPP supersedes any I/O error.
748 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
749 */
750 if (!md->barrier_error || error == -EOPNOTSUPP ||
751 (md->barrier_error != -EOPNOTSUPP &&
752 error == DM_ENDIO_REQUEUE))
753 md->barrier_error = error;
754 spin_unlock_irqrestore(&md->barrier_error_lock, flags);
755}
756
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100757/*
758 * Don't touch any member of the md after calling this function because
759 * the md may be freed in dm_put() at the end of this function.
760 * Or do dm_get() before calling this function and dm_put() later.
761 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000762static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100763{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000764 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100765
766 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000767 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100768 wake_up(&md->wait);
769
770 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000771 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100772
773 /*
774 * dm_put() must be at the end of this function. See the comment above
775 */
776 dm_put(md);
777}
778
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100779static void free_rq_clone(struct request *clone)
780{
781 struct dm_rq_target_io *tio = clone->end_io_data;
782
783 blk_rq_unprep_clone(clone);
784 free_rq_tio(tio);
785}
786
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000787/*
788 * Complete the clone and the original request.
789 * Must be called without queue lock.
790 */
791static void dm_end_request(struct request *clone, int error)
792{
793 int rw = rq_data_dir(clone);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000794 int run_queue = 1;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200795 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000796 struct dm_rq_target_io *tio = clone->end_io_data;
797 struct mapped_device *md = tio->md;
798 struct request *rq = tio->orig;
799
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200800 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000801 rq->errors = clone->errors;
802 rq->resid_len = clone->resid_len;
803
804 if (rq->sense)
805 /*
806 * We are using the sense buffer of the original
807 * request.
808 * So setting the length of the sense data is enough.
809 */
810 rq->sense_len = clone->sense_len;
811 }
812
813 free_rq_clone(clone);
814
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000815 if (unlikely(is_barrier)) {
816 if (unlikely(error))
817 store_barrier_error(md, error);
818 run_queue = 0;
819 } else
820 blk_end_request_all(rq, error);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000821
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000822 rq_completed(md, rw, run_queue);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000823}
824
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100825static void dm_unprep_request(struct request *rq)
826{
827 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100828
829 rq->special = NULL;
830 rq->cmd_flags &= ~REQ_DONTPREP;
831
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100832 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100833}
834
835/*
836 * Requeue the original request of a clone.
837 */
838void dm_requeue_unmapped_request(struct request *clone)
839{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000840 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100841 struct dm_rq_target_io *tio = clone->end_io_data;
842 struct mapped_device *md = tio->md;
843 struct request *rq = tio->orig;
844 struct request_queue *q = rq->q;
845 unsigned long flags;
846
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200847 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000848 /*
849 * Barrier clones share an original request.
850 * Leave it to dm_end_request(), which handles this special
851 * case.
852 */
853 dm_end_request(clone, DM_ENDIO_REQUEUE);
854 return;
855 }
856
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100857 dm_unprep_request(rq);
858
859 spin_lock_irqsave(q->queue_lock, flags);
860 if (elv_queue_empty(q))
861 blk_plug_device(q);
862 blk_requeue_request(q, rq);
863 spin_unlock_irqrestore(q->queue_lock, flags);
864
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000865 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100866}
867EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
868
869static void __stop_queue(struct request_queue *q)
870{
871 blk_stop_queue(q);
872}
873
874static void stop_queue(struct request_queue *q)
875{
876 unsigned long flags;
877
878 spin_lock_irqsave(q->queue_lock, flags);
879 __stop_queue(q);
880 spin_unlock_irqrestore(q->queue_lock, flags);
881}
882
883static void __start_queue(struct request_queue *q)
884{
885 if (blk_queue_stopped(q))
886 blk_start_queue(q);
887}
888
889static void start_queue(struct request_queue *q)
890{
891 unsigned long flags;
892
893 spin_lock_irqsave(q->queue_lock, flags);
894 __start_queue(q);
895 spin_unlock_irqrestore(q->queue_lock, flags);
896}
897
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000898static void dm_done(struct request *clone, int error, bool mapped)
899{
900 int r = error;
901 struct dm_rq_target_io *tio = clone->end_io_data;
902 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
903
904 if (mapped && rq_end_io)
905 r = rq_end_io(tio->ti, clone, error, &tio->info);
906
907 if (r <= 0)
908 /* The target wants to complete the I/O */
909 dm_end_request(clone, r);
910 else if (r == DM_ENDIO_INCOMPLETE)
911 /* The target will handle the I/O */
912 return;
913 else if (r == DM_ENDIO_REQUEUE)
914 /* The target wants to requeue the I/O */
915 dm_requeue_unmapped_request(clone);
916 else {
917 DMWARN("unimplemented target endio return value: %d", r);
918 BUG();
919 }
920}
921
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100922/*
923 * Request completion handler for request-based dm
924 */
925static void dm_softirq_done(struct request *rq)
926{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000927 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100928 struct request *clone = rq->completion_data;
929 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100930
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000931 if (rq->cmd_flags & REQ_FAILED)
932 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100933
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000934 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100935}
936
937/*
938 * Complete the clone and the original request with the error status
939 * through softirq context.
940 */
941static void dm_complete_request(struct request *clone, int error)
942{
943 struct dm_rq_target_io *tio = clone->end_io_data;
944 struct request *rq = tio->orig;
945
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200946 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000947 /*
948 * Barrier clones share an original request. So can't use
949 * softirq_done with the original.
950 * Pass the clone to dm_done() directly in this special case.
951 * It is safe (even if clone->q->queue_lock is held here)
952 * because there is no I/O dispatching during the completion
953 * of barrier clone.
954 */
955 dm_done(clone, error, true);
956 return;
957 }
958
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100959 tio->error = error;
960 rq->completion_data = clone;
961 blk_complete_request(rq);
962}
963
964/*
965 * Complete the not-mapped clone and the original request with the error status
966 * through softirq context.
967 * Target's rq_end_io() function isn't called.
968 * This may be used when the target's map_rq() function fails.
969 */
970void dm_kill_unmapped_request(struct request *clone, int error)
971{
972 struct dm_rq_target_io *tio = clone->end_io_data;
973 struct request *rq = tio->orig;
974
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200975 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000976 /*
977 * Barrier clones share an original request.
978 * Leave it to dm_end_request(), which handles this special
979 * case.
980 */
981 BUG_ON(error > 0);
982 dm_end_request(clone, error);
983 return;
984 }
985
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100986 rq->cmd_flags |= REQ_FAILED;
987 dm_complete_request(clone, error);
988}
989EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
990
991/*
992 * Called with the queue lock held
993 */
994static void end_clone_request(struct request *clone, int error)
995{
996 /*
997 * For just cleaning up the information of the queue in which
998 * the clone was dispatched.
999 * The clone is *NOT* freed actually here because it is alloced from
1000 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1001 */
1002 __blk_put_request(clone->q, clone);
1003
1004 /*
1005 * Actual request completion is done in a softirq context which doesn't
1006 * hold the queue lock. Otherwise, deadlock could occur because:
1007 * - another request may be submitted by the upper level driver
1008 * of the stacking during the completion
1009 * - the submission which requires queue lock may be done
1010 * against this queue
1011 */
1012 dm_complete_request(clone, error);
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static sector_t max_io_len(struct mapped_device *md,
1016 sector_t sector, struct dm_target *ti)
1017{
1018 sector_t offset = sector - ti->begin;
1019 sector_t len = ti->len - offset;
1020
1021 /*
1022 * Does the target need to split even further ?
1023 */
1024 if (ti->split_io) {
1025 sector_t boundary;
1026 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
1027 - offset;
1028 if (len > boundary)
1029 len = boundary;
1030 }
1031
1032 return len;
1033}
1034
1035static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001036 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001039 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001040 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 clone->bi_end_io = clone_endio;
1043 clone->bi_private = tio;
1044
1045 /*
1046 * Map the clone. If r == 0 we don't need to do
1047 * anything, the target has assumed ownership of
1048 * this io.
1049 */
1050 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001051 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001053 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001055
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001056 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001057 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001060 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1061 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001062 md = tio->io->md;
1063 dec_pending(tio->io, r);
1064 /*
1065 * Store bio_set for cleanup.
1066 */
1067 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001069 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001070 } else if (r) {
1071 DMWARN("unimplemented target map return value: %d", r);
1072 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074}
1075
1076struct clone_info {
1077 struct mapped_device *md;
1078 struct dm_table *map;
1079 struct bio *bio;
1080 struct dm_io *io;
1081 sector_t sector;
1082 sector_t sector_count;
1083 unsigned short idx;
1084};
1085
Peter Osterlund36763472005-09-06 15:16:42 -07001086static void dm_bio_destructor(struct bio *bio)
1087{
Stefan Bader9faf4002006-10-03 01:15:41 -07001088 struct bio_set *bs = bio->bi_private;
1089
1090 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093/*
1094 * Creates a little bio that is just does part of a bvec.
1095 */
1096static struct bio *split_bvec(struct bio *bio, sector_t sector,
1097 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001098 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 struct bio *clone;
1101 struct bio_vec *bv = bio->bi_io_vec + idx;
1102
Stefan Bader9faf4002006-10-03 01:15:41 -07001103 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001104 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *clone->bi_io_vec = *bv;
1106
1107 clone->bi_sector = sector;
1108 clone->bi_bdev = bio->bi_bdev;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001109 clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 clone->bi_vcnt = 1;
1111 clone->bi_size = to_bytes(len);
1112 clone->bi_io_vec->bv_offset = offset;
1113 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001114 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Martin K. Petersen9c470082009-04-09 00:27:12 +01001116 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001117 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001118 bio_integrity_trim(clone,
1119 bio_sector_offset(bio, idx, offset), len);
1120 }
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return clone;
1123}
1124
1125/*
1126 * Creates a bio that consists of range of complete bvecs.
1127 */
1128static struct bio *clone_bio(struct bio *bio, sector_t sector,
1129 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001130 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct bio *clone;
1133
Stefan Bader9faf4002006-10-03 01:15:41 -07001134 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1135 __bio_clone(clone, bio);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001136 clone->bi_rw &= ~REQ_HARDBARRIER;
Stefan Bader9faf4002006-10-03 01:15:41 -07001137 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 clone->bi_sector = sector;
1139 clone->bi_idx = idx;
1140 clone->bi_vcnt = idx + bv_count;
1141 clone->bi_size = to_bytes(len);
1142 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1143
Martin K. Petersen9c470082009-04-09 00:27:12 +01001144 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001145 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001146
1147 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1148 bio_integrity_trim(clone,
1149 bio_sector_offset(bio, idx, 0), len);
1150 }
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return clone;
1153}
1154
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001155static struct dm_target_io *alloc_tio(struct clone_info *ci,
1156 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001157{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001158 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001159
1160 tio->io = ci->io;
1161 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001162 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001163
1164 return tio;
1165}
1166
1167static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1168 unsigned flush_nr)
1169{
1170 struct dm_target_io *tio = alloc_tio(ci, ti);
1171 struct bio *clone;
1172
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001173 tio->info.flush_request = flush_nr;
1174
1175 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1176 __bio_clone(clone, ci->bio);
1177 clone->bi_destructor = dm_bio_destructor;
1178
1179 __map_bio(ti, clone, tio);
1180}
1181
1182static int __clone_and_map_empty_barrier(struct clone_info *ci)
1183{
1184 unsigned target_nr = 0, flush_nr;
1185 struct dm_target *ti;
1186
1187 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1188 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1189 flush_nr++)
1190 __flush_target(ci, ti, flush_nr);
1191
1192 ci->sector_count = 0;
1193
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
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001204 if (unlikely(bio_empty_barrier(bio)))
1205 return __clone_and_map_empty_barrier(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
1211 max = max_io_len(ci->md, ci->sector, ti);
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /*
1214 * Allocate a target io object.
1215 */
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001216 tio = alloc_tio(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if (ci->sector_count <= max) {
1219 /*
1220 * Optimise for the simple case where we can do all of
1221 * the remaining io with a single clone.
1222 */
1223 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001224 bio->bi_vcnt - ci->idx, ci->sector_count,
1225 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 __map_bio(ti, clone, tio);
1227 ci->sector_count = 0;
1228
1229 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1230 /*
1231 * There are some bvecs that don't span targets.
1232 * Do as many of these as possible.
1233 */
1234 int i;
1235 sector_t remaining = max;
1236 sector_t bv_len;
1237
1238 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1239 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1240
1241 if (bv_len > remaining)
1242 break;
1243
1244 remaining -= bv_len;
1245 len += bv_len;
1246 }
1247
Stefan Bader9faf4002006-10-03 01:15:41 -07001248 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1249 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 __map_bio(ti, clone, tio);
1251
1252 ci->sector += len;
1253 ci->sector_count -= len;
1254 ci->idx = i;
1255
1256 } else {
1257 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001258 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
1260 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001261 sector_t remaining = to_sector(bv->bv_len);
1262 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001264 do {
1265 if (offset) {
1266 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001267 if (!dm_target_is_valid(ti))
1268 return -EIO;
1269
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001270 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001272 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001275 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001277 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001278 bv->bv_offset + offset, len,
1279 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001280
1281 __map_bio(ti, clone, tio);
1282
1283 ci->sector += len;
1284 ci->sector_count -= len;
1285 offset += to_bytes(len);
1286 } while (remaining -= len);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ci->idx++;
1289 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001290
1291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001295 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001297static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001300 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001302 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001303 if (unlikely(!ci.map)) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001304 if (!(bio->bi_rw & REQ_HARDBARRIER))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001305 bio_io_error(bio);
1306 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001307 if (!md->barrier_error)
1308 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001309 return;
1310 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 ci.md = md;
1313 ci.bio = bio;
1314 ci.io = alloc_io(md);
1315 ci.io->error = 0;
1316 atomic_set(&ci.io->io_count, 1);
1317 ci.io->bio = bio;
1318 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001319 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ci.sector = bio->bi_sector;
1321 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001322 if (unlikely(bio_empty_barrier(bio)))
1323 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 ci.idx = bio->bi_idx;
1325
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001326 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001327 while (ci.sector_count && !error)
1328 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001331 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 dm_table_put(ci.map);
1333}
1334/*-----------------------------------------------------------------
1335 * CRUD END
1336 *---------------------------------------------------------------*/
1337
Milan Brozf6fccb12008-07-21 12:00:37 +01001338static int dm_merge_bvec(struct request_queue *q,
1339 struct bvec_merge_data *bvm,
1340 struct bio_vec *biovec)
1341{
1342 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001343 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001344 struct dm_target *ti;
1345 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001346 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001347
1348 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001349 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001350
1351 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001352 if (!dm_target_is_valid(ti))
1353 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001354
1355 /*
1356 * Find maximum amount of I/O that won't need splitting
1357 */
1358 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1359 (sector_t) BIO_MAX_SECTORS);
1360 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1361 if (max_size < 0)
1362 max_size = 0;
1363
1364 /*
1365 * merge_bvec_fn() returns number of bytes
1366 * it can accept at this offset
1367 * max is precomputed maximal io size
1368 */
1369 if (max_size && ti->type->merge)
1370 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001371 /*
1372 * If the target doesn't support merge method and some of the devices
1373 * provided their merge_bvec method (we know this by looking at
1374 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1375 * entries. So always set max_size to 0, and the code below allows
1376 * just one page.
1377 */
1378 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1379
1380 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001381
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001382out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001383 dm_table_put(map);
1384
1385out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001386 /*
1387 * Always allow an entire first page
1388 */
1389 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1390 max_size = biovec->bv_len;
1391
Milan Brozf6fccb12008-07-21 12:00:37 +01001392 return max_size;
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/*
1396 * The request function that just remaps the bio built up by
1397 * dm_merge_bvec.
1398 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001399static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Kevin Corry12f03a42006-02-01 03:04:52 -08001401 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001403 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001405 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Tejun Heo074a7ac2008-08-25 19:56:14 +09001407 cpu = part_stat_lock();
1408 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1409 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1410 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001413 * If we're suspended or the thread is processing barriers
1414 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001416 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001417 unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001418 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001420 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1421 bio_rw(bio) == READA) {
1422 bio_io_error(bio);
1423 return 0;
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Mikulas Patocka92c63902009-04-09 00:27:15 +01001426 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Mikulas Patocka92c63902009-04-09 00:27:15 +01001428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001431 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001432 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001436static int dm_make_request(struct request_queue *q, struct bio *bio)
1437{
1438 struct mapped_device *md = q->queuedata;
1439
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001440 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1441}
1442
1443static int dm_request_based(struct mapped_device *md)
1444{
1445 return blk_queue_stackable(md->queue);
1446}
1447
1448static int dm_request(struct request_queue *q, struct bio *bio)
1449{
1450 struct mapped_device *md = q->queuedata;
1451
1452 if (dm_request_based(md))
1453 return dm_make_request(q, bio);
1454
1455 return _dm_request(q, bio);
1456}
1457
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001458static bool dm_rq_is_flush_request(struct request *rq)
1459{
FUJITA Tomonori144d6ed2010-07-03 17:45:37 +09001460 if (rq->cmd_flags & REQ_FLUSH)
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001461 return true;
1462 else
1463 return false;
1464}
1465
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001466void dm_dispatch_request(struct request *rq)
1467{
1468 int r;
1469
1470 if (blk_queue_io_stat(rq->q))
1471 rq->cmd_flags |= REQ_IO_STAT;
1472
1473 rq->start_time = jiffies;
1474 r = blk_insert_cloned_request(rq->q, rq);
1475 if (r)
1476 dm_complete_request(rq, r);
1477}
1478EXPORT_SYMBOL_GPL(dm_dispatch_request);
1479
1480static void dm_rq_bio_destructor(struct bio *bio)
1481{
1482 struct dm_rq_clone_bio_info *info = bio->bi_private;
1483 struct mapped_device *md = info->tio->md;
1484
1485 free_bio_info(info);
1486 bio_free(bio, md->bs);
1487}
1488
1489static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1490 void *data)
1491{
1492 struct dm_rq_target_io *tio = data;
1493 struct mapped_device *md = tio->md;
1494 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1495
1496 if (!info)
1497 return -ENOMEM;
1498
1499 info->orig = bio_orig;
1500 info->tio = tio;
1501 bio->bi_end_io = end_clone_bio;
1502 bio->bi_private = info;
1503 bio->bi_destructor = dm_rq_bio_destructor;
1504
1505 return 0;
1506}
1507
1508static int setup_clone(struct request *clone, struct request *rq,
1509 struct dm_rq_target_io *tio)
1510{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001511 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001512
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001513 if (dm_rq_is_flush_request(rq)) {
1514 blk_rq_init(NULL, clone);
1515 clone->cmd_type = REQ_TYPE_FS;
1516 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1517 } else {
1518 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1519 dm_rq_bio_constructor, tio);
1520 if (r)
1521 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001522
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001523 clone->cmd = rq->cmd;
1524 clone->cmd_len = rq->cmd_len;
1525 clone->sense = rq->sense;
1526 clone->buffer = rq->buffer;
1527 }
1528
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001529 clone->end_io = end_clone_request;
1530 clone->end_io_data = tio;
1531
1532 return 0;
1533}
1534
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001535static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1536 gfp_t gfp_mask)
1537{
1538 struct request *clone;
1539 struct dm_rq_target_io *tio;
1540
1541 tio = alloc_rq_tio(md, gfp_mask);
1542 if (!tio)
1543 return NULL;
1544
1545 tio->md = md;
1546 tio->ti = NULL;
1547 tio->orig = rq;
1548 tio->error = 0;
1549 memset(&tio->info, 0, sizeof(tio->info));
1550
1551 clone = &tio->clone;
1552 if (setup_clone(clone, rq, tio)) {
1553 /* -ENOMEM */
1554 free_rq_tio(tio);
1555 return NULL;
1556 }
1557
1558 return clone;
1559}
1560
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001561/*
1562 * Called with the queue lock held.
1563 */
1564static int dm_prep_fn(struct request_queue *q, struct request *rq)
1565{
1566 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001567 struct request *clone;
1568
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001569 if (unlikely(dm_rq_is_flush_request(rq)))
1570 return BLKPREP_OK;
1571
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001572 if (unlikely(rq->special)) {
1573 DMWARN("Already has something in rq->special.");
1574 return BLKPREP_KILL;
1575 }
1576
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001577 clone = clone_rq(rq, md, GFP_ATOMIC);
1578 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001579 return BLKPREP_DEFER;
1580
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001581 rq->special = clone;
1582 rq->cmd_flags |= REQ_DONTPREP;
1583
1584 return BLKPREP_OK;
1585}
1586
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001587/*
1588 * Returns:
1589 * 0 : the request has been processed (not requeued)
1590 * !0 : the request has been requeued
1591 */
1592static int map_request(struct dm_target *ti, struct request *clone,
1593 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001594{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001595 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001596 struct dm_rq_target_io *tio = clone->end_io_data;
1597
1598 /*
1599 * Hold the md reference here for the in-flight I/O.
1600 * We can't rely on the reference count by device opener,
1601 * because the device may be closed during the request completion
1602 * when all bios are completed.
1603 * See the comment in rq_completed() too.
1604 */
1605 dm_get(md);
1606
1607 tio->ti = ti;
1608 r = ti->type->map_rq(ti, clone, &tio->info);
1609 switch (r) {
1610 case DM_MAPIO_SUBMITTED:
1611 /* The target has taken the I/O to submit by itself later */
1612 break;
1613 case DM_MAPIO_REMAPPED:
1614 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001615 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1616 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001617 dm_dispatch_request(clone);
1618 break;
1619 case DM_MAPIO_REQUEUE:
1620 /* The target wants to requeue the I/O */
1621 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001622 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001623 break;
1624 default:
1625 if (r > 0) {
1626 DMWARN("unimplemented target map return value: %d", r);
1627 BUG();
1628 }
1629
1630 /* The target wants to complete the I/O */
1631 dm_kill_unmapped_request(clone, r);
1632 break;
1633 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001634
1635 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001636}
1637
1638/*
1639 * q->request_fn for request-based dm.
1640 * Called with the queue lock held.
1641 */
1642static void dm_request_fn(struct request_queue *q)
1643{
1644 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001645 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001646 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001647 struct request *rq, *clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001648
1649 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001650 * For suspend, check blk_queue_stopped() and increment
1651 * ->pending within a single queue_lock not to increment the
1652 * number of in-flight I/Os after the queue is stopped in
1653 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001654 */
1655 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1656 rq = blk_peek_request(q);
1657 if (!rq)
1658 goto plug_and_out;
1659
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001660 if (unlikely(dm_rq_is_flush_request(rq))) {
1661 BUG_ON(md->flush_request);
1662 md->flush_request = rq;
1663 blk_start_request(rq);
1664 queue_work(md->wq, &md->barrier_work);
1665 goto out;
1666 }
1667
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001668 ti = dm_table_find_target(map, blk_rq_pos(rq));
1669 if (ti->type->busy && ti->type->busy(ti))
1670 goto plug_and_out;
1671
1672 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001673 clone = rq->special;
1674 atomic_inc(&md->pending[rq_data_dir(clone)]);
1675
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001676 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001677 if (map_request(ti, clone, md))
1678 goto requeued;
1679
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001680 spin_lock_irq(q->queue_lock);
1681 }
1682
1683 goto out;
1684
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001685requeued:
1686 spin_lock_irq(q->queue_lock);
1687
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001688plug_and_out:
1689 if (!elv_queue_empty(q))
1690 /* Some requests still remain, retry later */
1691 blk_plug_device(q);
1692
1693out:
1694 dm_table_put(map);
1695
1696 return;
1697}
1698
1699int dm_underlying_device_busy(struct request_queue *q)
1700{
1701 return blk_lld_busy(q);
1702}
1703EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1704
1705static int dm_lld_busy(struct request_queue *q)
1706{
1707 int r;
1708 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001709 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001710
1711 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1712 r = 1;
1713 else
1714 r = dm_table_any_busy_target(map);
1715
1716 dm_table_put(map);
1717
1718 return r;
1719}
1720
Jens Axboe165125e2007-07-24 09:28:11 +02001721static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001724 struct dm_table *map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001727 if (dm_request_based(md))
1728 generic_unplug_device(q);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 dm_table_unplug_all(map);
1731 dm_table_put(map);
1732 }
1733}
1734
1735static int dm_any_congested(void *congested_data, int bdi_bits)
1736{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001737 int r = bdi_bits;
1738 struct mapped_device *md = congested_data;
1739 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001741 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001742 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001743 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001744 /*
1745 * Request-based dm cares about only own queue for
1746 * the query about congestion status of request_queue
1747 */
1748 if (dm_request_based(md))
1749 r = md->queue->backing_dev_info.state &
1750 bdi_bits;
1751 else
1752 r = dm_table_any_congested(map, bdi_bits);
1753
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001754 dm_table_put(map);
1755 }
1756 }
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return r;
1759}
1760
1761/*-----------------------------------------------------------------
1762 * An IDR is used to keep track of allocated minor numbers.
1763 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764static DEFINE_IDR(_minor_idr);
1765
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001766static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001768 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001770 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
1773/*
1774 * See if the device with a specific minor # is free.
1775 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001776static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 int r, m;
1779
1780 if (minor >= (1 << MINORBITS))
1781 return -EINVAL;
1782
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001783 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1784 if (!r)
1785 return -ENOMEM;
1786
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001787 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if (idr_find(&_minor_idr, minor)) {
1790 r = -EBUSY;
1791 goto out;
1792 }
1793
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001794 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001795 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (m != minor) {
1799 idr_remove(&_minor_idr, m);
1800 r = -EBUSY;
1801 goto out;
1802 }
1803
1804out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001805 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return r;
1807}
1808
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001809static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001811 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001814 if (!r)
1815 return -ENOMEM;
1816
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001817 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001819 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001820 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 if (m >= (1 << MINORBITS)) {
1824 idr_remove(&_minor_idr, m);
1825 r = -ENOSPC;
1826 goto out;
1827 }
1828
1829 *minor = m;
1830
1831out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001832 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return r;
1834}
1835
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001836static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Mikulas Patocka53d59142009-04-02 19:55:37 +01001838static void dm_wq_work(struct work_struct *work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001839static void dm_rq_barrier_work(struct work_struct *work);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841/*
1842 * Allocate and initialise a blank device with a given minor.
1843 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001844static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001847 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001848 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 if (!md) {
1851 DMWARN("unable to allocate device, out of memory.");
1852 return NULL;
1853 }
1854
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001855 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001856 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001859 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001860 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001861 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001862 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001864 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001866 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001867 mutex_init(&md->suspend_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001868 spin_lock_init(&md->deferred_lock);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001869 spin_lock_init(&md->barrier_error_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 rwlock_init(&md->map_lock);
1871 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001872 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001874 atomic_set(&md->uevent_seq, 0);
1875 INIT_LIST_HEAD(&md->uevent_list);
1876 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001878 md->queue = blk_init_queue(dm_request_fn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001880 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001882 /*
1883 * Request-based dm devices cannot be stacked on top of bio-based dm
1884 * devices. The type of this dm device has not been decided yet,
1885 * although we initialized the queue using blk_init_queue().
1886 * The type is decided at the first table loading time.
1887 * To prevent problematic device stacking, clear the queue flag
1888 * for request stacking support until then.
1889 *
1890 * This queue is new, so no concurrency on the queue_flags.
1891 */
1892 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1893 md->saved_make_request_fn = md->queue->make_request_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 md->queue->queuedata = md;
1895 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1896 md->queue->backing_dev_info.congested_data = md;
1897 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +01001898 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001900 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001901 blk_queue_softirq_done(md->queue, dm_softirq_done);
1902 blk_queue_prep_rq(md->queue, dm_prep_fn);
1903 blk_queue_lld_busy(md->queue, dm_lld_busy);
FUJITA Tomonori00fff262010-07-03 17:45:40 +09001904 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH);
Stefan Bader9faf4002006-10-03 01:15:41 -07001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 md->disk = alloc_disk(1);
1907 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001908 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001910 atomic_set(&md->pending[0], 0);
1911 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001912 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001913 INIT_WORK(&md->work, dm_wq_work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001914 INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001915 init_waitqueue_head(&md->eventq);
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 md->disk->major = _major;
1918 md->disk->first_minor = minor;
1919 md->disk->fops = &dm_blk_dops;
1920 md->disk->queue = md->queue;
1921 md->disk->private_data = md;
1922 sprintf(md->disk->disk_name, "dm-%d", minor);
1923 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001924 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Milan Broz304f3f62008-02-08 02:11:17 +00001926 md->wq = create_singlethread_workqueue("kdmflush");
1927 if (!md->wq)
1928 goto bad_thread;
1929
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001930 md->bdev = bdget_disk(md->disk, 0);
1931 if (!md->bdev)
1932 goto bad_bdev;
1933
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001934 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001935 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001936 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001937 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001938
1939 BUG_ON(old_md != MINOR_ALLOCED);
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return md;
1942
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001943bad_bdev:
1944 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001945bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001946 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001947 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001948bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001949 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001950bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001952bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001953 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001954bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 kfree(md);
1956 return NULL;
1957}
1958
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001959static void unlock_fs(struct mapped_device *md);
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961static void free_dev(struct mapped_device *md)
1962{
Tejun Heof331c022008-09-03 09:01:48 +02001963 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001964
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001965 unlock_fs(md);
1966 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001967 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001968 if (md->tio_pool)
1969 mempool_destroy(md->tio_pool);
1970 if (md->io_pool)
1971 mempool_destroy(md->io_pool);
1972 if (md->bs)
1973 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001974 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001976 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001977
1978 spin_lock(&_minor_lock);
1979 md->disk->private_data = NULL;
1980 spin_unlock(&_minor_lock);
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001983 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001984 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 kfree(md);
1986}
1987
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001988static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1989{
1990 struct dm_md_mempools *p;
1991
1992 if (md->io_pool && md->tio_pool && md->bs)
1993 /* the md already has necessary mempools */
1994 goto out;
1995
1996 p = dm_table_get_md_mempools(t);
1997 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1998
1999 md->io_pool = p->io_pool;
2000 p->io_pool = NULL;
2001 md->tio_pool = p->tio_pool;
2002 p->tio_pool = NULL;
2003 md->bs = p->bs;
2004 p->bs = NULL;
2005
2006out:
2007 /* mempool bind completed, now no need any mempools in the table */
2008 dm_table_free_md_mempools(t);
2009}
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011/*
2012 * Bind a table to the device.
2013 */
2014static void event_callback(void *context)
2015{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002016 unsigned long flags;
2017 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct mapped_device *md = (struct mapped_device *) context;
2019
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002020 spin_lock_irqsave(&md->uevent_lock, flags);
2021 list_splice_init(&md->uevent_list, &uevents);
2022 spin_unlock_irqrestore(&md->uevent_lock, flags);
2023
Tejun Heoed9e1982008-08-25 19:56:05 +09002024 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 atomic_inc(&md->event_nr);
2027 wake_up(&md->eventq);
2028}
2029
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002030static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002032 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002034 mutex_lock(&md->bdev->bd_inode->i_mutex);
2035 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2036 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002039/*
2040 * Returns old map, which caller must destroy.
2041 */
2042static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2043 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002045 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002046 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002048 unsigned long flags;
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
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002074 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002075 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002076 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002077 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002078 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002079
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002080 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Alasdair G Kergona7940152009-12-10 23:52:23 +00002083/*
2084 * Returns unbound table for the caller to free.
2085 */
2086static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002089 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002092 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002095 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002097 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002098
2099 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102/*
2103 * Constructor for a new device.
2104 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002105int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 struct mapped_device *md;
2108
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002109 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (!md)
2111 return -ENXIO;
2112
Milan Broz784aae72009-01-06 03:05:12 +00002113 dm_sysfs_init(md);
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 *result = md;
2116 return 0;
2117}
2118
David Teigland637842c2006-01-06 00:20:00 -08002119static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 unsigned minor = MINOR(dev);
2123
2124 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2125 return NULL;
2126
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002127 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002130 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002131 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002132 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002133 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002134 goto out;
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002137out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002138 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
David Teigland637842c2006-01-06 00:20:00 -08002140 return md;
2141}
2142
David Teiglandd229a952006-01-06 00:20:01 -08002143struct mapped_device *dm_get_md(dev_t dev)
2144{
2145 struct mapped_device *md = dm_find_md(dev);
2146
2147 if (md)
2148 dm_get(md);
2149
2150 return md;
2151}
2152
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002153void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002154{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002155 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
2158void dm_set_mdptr(struct mapped_device *md, void *ptr)
2159{
2160 md->interface_ptr = ptr;
2161}
2162
2163void dm_get(struct mapped_device *md)
2164{
2165 atomic_inc(&md->holders);
2166}
2167
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002168const char *dm_device_name(struct mapped_device *md)
2169{
2170 return md->name;
2171}
2172EXPORT_SYMBOL_GPL(dm_device_name);
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174void dm_put(struct mapped_device *md)
2175{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002176 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002178 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2179
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002180 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002181 map = dm_get_live_table(md);
Tejun Heof331c022008-09-03 09:01:48 +02002182 idr_replace(&_minor_idr, MINOR_ALLOCED,
2183 MINOR(disk_devt(dm_disk(md))));
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002184 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002185 spin_unlock(&_minor_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002186 if (!dm_suspended_md(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 dm_table_presuspend_targets(map);
2188 dm_table_postsuspend_targets(map);
2189 }
Milan Broz784aae72009-01-06 03:05:12 +00002190 dm_sysfs_exit(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002191 dm_table_put(map);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002192 dm_table_destroy(__unbind(md));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 free_dev(md);
2194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
Edward Goggin79eb8852007-05-09 02:32:56 -07002196EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Mikulas Patocka401600d2009-04-02 19:55:38 +01002198static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002199{
2200 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002201 DECLARE_WAITQUEUE(wait, current);
2202
2203 dm_unplug_all(md->queue);
2204
2205 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002206
2207 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002208 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002209
2210 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002211 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002212 break;
2213
Mikulas Patocka401600d2009-04-02 19:55:38 +01002214 if (interruptible == TASK_INTERRUPTIBLE &&
2215 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002216 r = -EINTR;
2217 break;
2218 }
2219
2220 io_schedule();
2221 }
2222 set_current_state(TASK_RUNNING);
2223
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002224 remove_wait_queue(&md->wait, &wait);
2225
Milan Broz46125c12008-02-08 02:10:30 +00002226 return r;
2227}
2228
Mikulas Patocka531fe962009-06-22 10:12:17 +01002229static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002230{
2231 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002232
2233 bio_init(&md->barrier_bio);
2234 md->barrier_bio.bi_bdev = md->bdev;
2235 md->barrier_bio.bi_rw = WRITE_BARRIER;
2236 __split_and_process_bio(md, &md->barrier_bio);
2237
2238 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002239}
2240
2241static void process_barrier(struct mapped_device *md, struct bio *bio)
2242{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002243 md->barrier_error = 0;
2244
Mikulas Patocka531fe962009-06-22 10:12:17 +01002245 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002246
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002247 if (!bio_empty_barrier(bio)) {
2248 __split_and_process_bio(md, bio);
2249 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002250 }
2251
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002252 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002253 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002254 else {
2255 spin_lock_irq(&md->deferred_lock);
2256 bio_list_add_head(&md->deferred, bio);
2257 spin_unlock_irq(&md->deferred_lock);
2258 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002259}
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261/*
2262 * Process the deferred bios
2263 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002264static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Mikulas Patockaef208582009-04-02 19:55:38 +01002266 struct mapped_device *md = container_of(work, struct mapped_device,
2267 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002268 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Mikulas Patockaef208582009-04-02 19:55:38 +01002270 down_write(&md->io_lock);
2271
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002272 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002273 spin_lock_irq(&md->deferred_lock);
2274 c = bio_list_pop(&md->deferred);
2275 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002276
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002277 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002278 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002279 break;
2280 }
2281
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002282 up_write(&md->io_lock);
2283
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002284 if (dm_request_based(md))
2285 generic_make_request(c);
2286 else {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002287 if (c->bi_rw & REQ_HARDBARRIER)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002288 process_barrier(md, c);
2289 else
2290 __split_and_process_bio(md, c);
2291 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002292
2293 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002294 }
Milan Broz73d410c2008-02-08 02:10:25 +00002295
Mikulas Patockaef208582009-04-02 19:55:38 +01002296 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002299static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002300{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002301 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2302 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002303 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002304}
2305
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002306static void dm_rq_set_flush_nr(struct request *clone, unsigned flush_nr)
2307{
2308 struct dm_rq_target_io *tio = clone->end_io_data;
2309
2310 tio->info.flush_request = flush_nr;
2311}
2312
2313/* Issue barrier requests to targets and wait for their completion. */
2314static int dm_rq_barrier(struct mapped_device *md)
2315{
2316 int i, j;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002317 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002318 unsigned num_targets = dm_table_get_num_targets(map);
2319 struct dm_target *ti;
2320 struct request *clone;
2321
2322 md->barrier_error = 0;
2323
2324 for (i = 0; i < num_targets; i++) {
2325 ti = dm_table_get_target(map, i);
2326 for (j = 0; j < ti->num_flush_requests; j++) {
2327 clone = clone_rq(md->flush_request, md, GFP_NOIO);
2328 dm_rq_set_flush_nr(clone, j);
2329 atomic_inc(&md->pending[rq_data_dir(clone)]);
2330 map_request(ti, clone, md);
2331 }
2332 }
2333
2334 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2335 dm_table_put(map);
2336
2337 return md->barrier_error;
2338}
2339
2340static void dm_rq_barrier_work(struct work_struct *work)
2341{
2342 int error;
2343 struct mapped_device *md = container_of(work, struct mapped_device,
2344 barrier_work);
2345 struct request_queue *q = md->queue;
2346 struct request *rq;
2347 unsigned long flags;
2348
2349 /*
2350 * Hold the md reference here and leave it at the last part so that
2351 * the md can't be deleted by device opener when the barrier request
2352 * completes.
2353 */
2354 dm_get(md);
2355
2356 error = dm_rq_barrier(md);
2357
2358 rq = md->flush_request;
2359 md->flush_request = NULL;
2360
2361 if (error == DM_ENDIO_REQUEUE) {
2362 spin_lock_irqsave(q->queue_lock, flags);
2363 blk_requeue_request(q, rq);
2364 spin_unlock_irqrestore(q->queue_lock, flags);
2365 } else
2366 blk_end_request_all(rq, error);
2367
2368 blk_run_queue(q);
2369
2370 dm_put(md);
2371}
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002374 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002376struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002378 struct dm_table *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002379 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002380 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Daniel Walkere61290a2008-02-08 02:10:08 +00002382 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002385 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002388 r = dm_calculate_queue_limits(table, &limits);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002389 if (r) {
2390 map = ERR_PTR(r);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002391 goto out;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002392 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002393
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002394 /* cannot change the device type, once a table is bound */
2395 if (md->map &&
2396 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2397 DMWARN("can't change the device type after a table is bound");
2398 goto out;
2399 }
2400
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002401 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002403out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002404 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002405 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
2408/*
2409 * Functions to lock and unlock any filesystem running on the
2410 * device.
2411 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002412static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002414 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002417
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002418 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002419 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002420 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002421 md->frozen_sb = NULL;
2422 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002423 }
2424
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002425 set_bit(DMF_FROZEN, &md->flags);
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 0;
2428}
2429
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002430static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002432 if (!test_bit(DMF_FROZEN, &md->flags))
2433 return;
2434
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002435 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002437 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
2440/*
2441 * We need to be able to change a mapping table under a mounted
2442 * filesystem. For example we might want to move some data in
2443 * the background. Before the table can be swapped with
2444 * dm_bind_table, dm_suspend must be called to flush any in
2445 * flight bios and ensure that any further io gets deferred.
2446 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002447/*
2448 * Suspend mechanism in request-based dm.
2449 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002450 * 1. Flush all I/Os by lock_fs() if needed.
2451 * 2. Stop dispatching any I/O by stopping the request_queue.
2452 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002453 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002454 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002455 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002456int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002458 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002459 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002460 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002461 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Daniel Walkere61290a2008-02-08 02:10:08 +00002463 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002464
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002465 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002466 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002467 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002470 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002472 /*
2473 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2474 * This flag is cleared before dm_suspend returns.
2475 */
2476 if (noflush)
2477 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2478
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002479 /* This does not get reverted if there's an error later. */
2480 dm_table_presuspend_targets(map);
2481
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002482 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002483 * Flush I/O to the device.
2484 * Any I/O submitted after lock_fs() may not be flushed.
2485 * noflush takes precedence over do_lockfs.
2486 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002487 */
2488 if (!noflush && do_lockfs) {
2489 r = lock_fs(md);
2490 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002491 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002495 * Here we must make sure that no processes are submitting requests
2496 * to target drivers i.e. no one may be executing
2497 * __split_and_process_bio. This is called from dm_request and
2498 * dm_wq_work.
2499 *
2500 * To get all processes out of __split_and_process_bio in dm_request,
2501 * we take the write lock. To prevent any process from reentering
2502 * __split_and_process_bio from dm_request, we set
2503 * DMF_QUEUE_IO_TO_THREAD.
2504 *
2505 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2506 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2507 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2508 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002510 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002511 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2512 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002513 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002515 /*
2516 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2517 * can be kicked until md->queue is stopped. So stop md->queue before
2518 * flushing md->wq.
2519 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002520 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002521 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002522
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002523 flush_workqueue(md->wq);
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002526 * At this point no more requests are entering target request routines.
2527 * We call dm_wait_for_completion to wait for all existing requests
2528 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002530 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002532 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002533 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002534 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002535 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002538 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002539 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002540
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002541 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002542 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002543
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002544 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002545 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002546 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002547
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002548 /*
2549 * If dm_wait_for_completion returned 0, the device is completely
2550 * quiescent now. There is no request-processing activity. All new
2551 * requests are being added to md->deferred list.
2552 */
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 set_bit(DMF_SUSPENDED, &md->flags);
2555
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002556 dm_table_postsuspend_targets(map);
2557
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002558out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002560
2561out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002562 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002563 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
2566int dm_resume(struct mapped_device *md)
2567{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002568 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002569 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Daniel Walkere61290a2008-02-08 02:10:08 +00002571 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002572 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002573 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002574
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002575 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002576 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Milan Broz8757b772006-10-03 01:15:36 -07002579 r = dm_table_resume_targets(map);
2580 if (r)
2581 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002582
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002583 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002584
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002585 /*
2586 * Flushing deferred I/Os must be done after targets are resumed
2587 * so that mapping of targets can work correctly.
2588 * Request-based dm is queueing the deferred I/Os in its request_queue.
2589 */
2590 if (dm_request_based(md))
2591 start_queue(md->queue);
2592
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002593 unlock_fs(md);
2594
2595 clear_bit(DMF_SUSPENDED, &md->flags);
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002598 r = 0;
2599out:
2600 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002601 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002602
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002603 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
2606/*-----------------------------------------------------------------
2607 * Event notification.
2608 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002609int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002610 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002611{
Milan Broz60935eb2009-06-22 10:12:30 +01002612 char udev_cookie[DM_COOKIE_LENGTH];
2613 char *envp[] = { udev_cookie, NULL };
2614
2615 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002616 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002617 else {
2618 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2619 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002620 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2621 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002622 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002623}
2624
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002625uint32_t dm_next_uevent_seq(struct mapped_device *md)
2626{
2627 return atomic_add_return(1, &md->uevent_seq);
2628}
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630uint32_t dm_get_event_nr(struct mapped_device *md)
2631{
2632 return atomic_read(&md->event_nr);
2633}
2634
2635int dm_wait_event(struct mapped_device *md, int event_nr)
2636{
2637 return wait_event_interruptible(md->eventq,
2638 (event_nr != atomic_read(&md->event_nr)));
2639}
2640
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002641void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2642{
2643 unsigned long flags;
2644
2645 spin_lock_irqsave(&md->uevent_lock, flags);
2646 list_add(elist, &md->uevent_list);
2647 spin_unlock_irqrestore(&md->uevent_lock, flags);
2648}
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650/*
2651 * The gendisk is only valid as long as you have a reference
2652 * count on 'md'.
2653 */
2654struct gendisk *dm_disk(struct mapped_device *md)
2655{
2656 return md->disk;
2657}
2658
Milan Broz784aae72009-01-06 03:05:12 +00002659struct kobject *dm_kobject(struct mapped_device *md)
2660{
2661 return &md->kobj;
2662}
2663
2664/*
2665 * struct mapped_device should not be exported outside of dm.c
2666 * so use this check to verify that kobj is part of md structure
2667 */
2668struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2669{
2670 struct mapped_device *md;
2671
2672 md = container_of(kobj, struct mapped_device, kobj);
2673 if (&md->kobj != kobj)
2674 return NULL;
2675
Milan Broz4d89b7b2009-06-22 10:12:11 +01002676 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002677 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002678 return NULL;
2679
Milan Broz784aae72009-01-06 03:05:12 +00002680 dm_get(md);
2681 return md;
2682}
2683
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002684int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
2686 return test_bit(DMF_SUSPENDED, &md->flags);
2687}
2688
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002689int dm_suspended(struct dm_target *ti)
2690{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002691 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002692}
2693EXPORT_SYMBOL_GPL(dm_suspended);
2694
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002695int dm_noflush_suspending(struct dm_target *ti)
2696{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002697 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002698}
2699EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2700
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002701struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2702{
2703 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2704
2705 if (!pools)
2706 return NULL;
2707
2708 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2709 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2710 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2711 if (!pools->io_pool)
2712 goto free_pools_and_out;
2713
2714 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2715 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2716 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2717 if (!pools->tio_pool)
2718 goto free_io_pool_and_out;
2719
2720 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2721 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2722 if (!pools->bs)
2723 goto free_tio_pool_and_out;
2724
2725 return pools;
2726
2727free_tio_pool_and_out:
2728 mempool_destroy(pools->tio_pool);
2729
2730free_io_pool_and_out:
2731 mempool_destroy(pools->io_pool);
2732
2733free_pools_and_out:
2734 kfree(pools);
2735
2736 return NULL;
2737}
2738
2739void dm_free_md_mempools(struct dm_md_mempools *pools)
2740{
2741 if (!pools)
2742 return;
2743
2744 if (pools->io_pool)
2745 mempool_destroy(pools->io_pool);
2746
2747 if (pools->tio_pool)
2748 mempool_destroy(pools->tio_pool);
2749
2750 if (pools->bs)
2751 bioset_free(pools->bs);
2752
2753 kfree(pools);
2754}
2755
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002756static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 .open = dm_blk_open,
2758 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002759 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002760 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 .owner = THIS_MODULE
2762};
2763
2764EXPORT_SYMBOL(dm_get_mapinfo);
2765
2766/*
2767 * module hooks
2768 */
2769module_init(dm_init);
2770module_exit(dm_exit);
2771
2772module_param(major, uint, 0);
2773MODULE_PARM_DESC(major, "The major number of the device mapper");
2774MODULE_DESCRIPTION(DM_NAME " driver");
2775MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2776MODULE_LICENSE("GPL");