blob: be003e5fea3d8d1ba6c4cea4da758c885e623a06 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000053 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * One of these is allocated per target within a bio. Hopefully
55 * this will be simplified out one day.
56 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010057struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct dm_io *io;
59 struct dm_target *ti;
60 union map_info info;
61};
62
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000063/*
64 * For request-based dm.
65 * One of these is allocated per request.
66 */
67struct dm_rq_target_io {
68 struct mapped_device *md;
69 struct dm_target *ti;
70 struct request *orig, clone;
71 int error;
72 union map_info info;
73};
74
75/*
76 * For request-based dm.
77 * One of these is allocated per bio.
78 */
79struct dm_rq_clone_bio_info {
80 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010081 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000082};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084union map_info *dm_get_mapinfo(struct bio *bio)
85{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070086 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010087 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070088 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010091union map_info *dm_get_rq_mapinfo(struct request *rq)
92{
93 if (rq && rq->end_io_data)
94 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
95 return NULL;
96}
97EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
98
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070099#define MINOR_ALLOCED ((void *)-1)
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * Bits for the md->flags field.
103 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100104#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800106#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700107#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700108#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800109#define DMF_NOFLUSH_SUSPENDING 5
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100110#define DMF_QUEUE_IO_TO_THREAD 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Milan Broz304f3f62008-02-08 02:11:17 +0000112/*
113 * Work processed by per-device workqueue.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700116 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000117 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 rwlock_t map_lock;
119 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700120 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 unsigned long flags;
123
Jens Axboe165125e2007-07-24 09:28:11 +0200124 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800126 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 void *interface_ptr;
129
130 /*
131 * A list of ios that arrived while we were suspended.
132 */
133 atomic_t pending;
134 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100135 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800136 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100137 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /*
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100140 * An error from the barrier request currently being processed.
141 */
142 int barrier_error;
143
144 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000145 * Processing queue (flush/barriers)
146 */
147 struct workqueue_struct *wq;
148
149 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * The current mapping.
151 */
152 struct dm_table *map;
153
154 /*
155 * io objects are allocated from here.
156 */
157 mempool_t *io_pool;
158 mempool_t *tio_pool;
159
Stefan Bader9faf4002006-10-03 01:15:41 -0700160 struct bio_set *bs;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * Event handling.
164 */
165 atomic_t event_nr;
166 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100167 atomic_t uevent_seq;
168 struct list_head uevent_list;
169 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /*
172 * freeze/thaw support require holding onto a super block
173 */
174 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100175 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800176
177 /* forced geometry settings */
178 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000179
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100180 /* marker of flush suspend for request-based dm */
181 struct request suspend_rq;
182
183 /* For saving the address of __make_request for request based dm */
184 make_request_fn *saved_make_request_fn;
185
Milan Broz784aae72009-01-06 03:05:12 +0000186 /* sysfs handle */
187 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100188
189 /* zero-length barrier that will be cloned and submitted to targets */
190 struct bio barrier_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
193#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800194static struct kmem_cache *_io_cache;
195static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000196static struct kmem_cache *_rq_tio_cache;
197static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int __init local_init(void)
200{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100201 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100204 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100206 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100209 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100210 if (!_tio_cache)
211 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000213 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
214 if (!_rq_tio_cache)
215 goto out_free_tio_cache;
216
217 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
218 if (!_rq_bio_info_cache)
219 goto out_free_rq_tio_cache;
220
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100221 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100222 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000223 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 _major = major;
226 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100227 if (r < 0)
228 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (!_major)
231 _major = r;
232
233 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100234
235out_uevent_exit:
236 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000237out_free_rq_bio_info_cache:
238 kmem_cache_destroy(_rq_bio_info_cache);
239out_free_rq_tio_cache:
240 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100241out_free_tio_cache:
242 kmem_cache_destroy(_tio_cache);
243out_free_io_cache:
244 kmem_cache_destroy(_io_cache);
245
246 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249static void local_exit(void)
250{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000251 kmem_cache_destroy(_rq_bio_info_cache);
252 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 kmem_cache_destroy(_tio_cache);
254 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700255 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100256 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 _major = 0;
259
260 DMINFO("cleaned up");
261}
262
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000263static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 local_init,
265 dm_target_init,
266 dm_linear_init,
267 dm_stripe_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100268 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dm_interface_init,
270};
271
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000272static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 local_exit,
274 dm_target_exit,
275 dm_linear_exit,
276 dm_stripe_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100277 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dm_interface_exit,
279};
280
281static int __init dm_init(void)
282{
283 const int count = ARRAY_SIZE(_inits);
284
285 int r, i;
286
287 for (i = 0; i < count; i++) {
288 r = _inits[i]();
289 if (r)
290 goto bad;
291 }
292
293 return 0;
294
295 bad:
296 while (i--)
297 _exits[i]();
298
299 return r;
300}
301
302static void __exit dm_exit(void)
303{
304 int i = ARRAY_SIZE(_exits);
305
306 while (i--)
307 _exits[i]();
308}
309
310/*
311 * Block device functions
312 */
Al Virofe5f9f22008-03-02 10:29:31 -0500313static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct mapped_device *md;
316
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700317 spin_lock(&_minor_lock);
318
Al Virofe5f9f22008-03-02 10:29:31 -0500319 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700320 if (!md)
321 goto out;
322
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700323 if (test_bit(DMF_FREEING, &md->flags) ||
324 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700325 md = NULL;
326 goto out;
327 }
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700330 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700331
332out:
333 spin_unlock(&_minor_lock);
334
335 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Al Virofe5f9f22008-03-02 10:29:31 -0500338static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Al Virofe5f9f22008-03-02 10:29:31 -0500340 struct mapped_device *md = disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700341 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dm_put(md);
343 return 0;
344}
345
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700346int dm_open_count(struct mapped_device *md)
347{
348 return atomic_read(&md->open_count);
349}
350
351/*
352 * Guarantees nothing is using the device before it's deleted.
353 */
354int dm_lock_for_deletion(struct mapped_device *md)
355{
356 int r = 0;
357
358 spin_lock(&_minor_lock);
359
360 if (dm_open_count(md))
361 r = -EBUSY;
362 else
363 set_bit(DMF_DELETING, &md->flags);
364
365 spin_unlock(&_minor_lock);
366
367 return r;
368}
369
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800370static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
371{
372 struct mapped_device *md = bdev->bd_disk->private_data;
373
374 return dm_get_geometry(md, geo);
375}
376
Al Virofe5f9f22008-03-02 10:29:31 -0500377static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700378 unsigned int cmd, unsigned long arg)
379{
Al Virofe5f9f22008-03-02 10:29:31 -0500380 struct mapped_device *md = bdev->bd_disk->private_data;
381 struct dm_table *map = dm_get_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700382 struct dm_target *tgt;
383 int r = -ENOTTY;
384
Milan Brozaa129a22006-10-03 01:15:15 -0700385 if (!map || !dm_table_get_size(map))
386 goto out;
387
388 /* We only support devices that have a single target */
389 if (dm_table_get_num_targets(map) != 1)
390 goto out;
391
392 tgt = dm_table_get_target(map, 0);
393
394 if (dm_suspended(md)) {
395 r = -EAGAIN;
396 goto out;
397 }
398
399 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400400 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700401
402out:
403 dm_table_put(map);
404
Milan Brozaa129a22006-10-03 01:15:15 -0700405 return r;
406}
407
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100408static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 return mempool_alloc(md->io_pool, GFP_NOIO);
411}
412
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100413static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 mempool_free(io, md->io_pool);
416}
417
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100418static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 mempool_free(tio, md->tio_pool);
421}
422
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100423static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md)
424{
425 return mempool_alloc(md->tio_pool, GFP_ATOMIC);
426}
427
428static void free_rq_tio(struct dm_rq_target_io *tio)
429{
430 mempool_free(tio, tio->md->tio_pool);
431}
432
433static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
434{
435 return mempool_alloc(md->io_pool, GFP_ATOMIC);
436}
437
438static void free_bio_info(struct dm_rq_clone_bio_info *info)
439{
440 mempool_free(info, info->tio->md->io_pool);
441}
442
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800443static void start_io_acct(struct dm_io *io)
444{
445 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900446 int cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800447
448 io->start_time = jiffies;
449
Tejun Heo074a7ac2008-08-25 19:56:14 +0900450 cpu = part_stat_lock();
451 part_round_stats(cpu, &dm_disk(md)->part0);
452 part_stat_unlock();
453 dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800454}
455
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000456static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800457{
458 struct mapped_device *md = io->md;
459 struct bio *bio = io->bio;
460 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900461 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800462 int rw = bio_data_dir(bio);
463
Tejun Heo074a7ac2008-08-25 19:56:14 +0900464 cpu = part_stat_lock();
465 part_round_stats(cpu, &dm_disk(md)->part0);
466 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
467 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800468
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100469 /*
470 * After this is decremented the bio must not be touched if it is
471 * a barrier.
472 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900473 dm_disk(md)->part0.in_flight = pending =
474 atomic_dec_return(&md->pending);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800475
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000476 /* nudge anyone waiting on suspend queue */
477 if (!pending)
478 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * Add the bio to the list of deferred io.
483 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100484static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700486 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Mikulas Patocka022c2612009-04-02 19:55:39 +0100488 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100490 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Mikulas Patocka92c63902009-04-09 00:27:15 +0100492 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
493 queue_work(md->wq, &md->work);
494
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700495 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/*
499 * Everyone (including functions in this file), should use this
500 * function to access the md->map field, and make sure they call
501 * dm_table_put() when finished.
502 */
503struct dm_table *dm_get_table(struct mapped_device *md)
504{
505 struct dm_table *t;
506
507 read_lock(&md->map_lock);
508 t = md->map;
509 if (t)
510 dm_table_get(t);
511 read_unlock(&md->map_lock);
512
513 return t;
514}
515
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800516/*
517 * Get the geometry associated with a dm device
518 */
519int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
520{
521 *geo = md->geometry;
522
523 return 0;
524}
525
526/*
527 * Set the geometry of a device.
528 */
529int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
530{
531 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
532
533 if (geo->start > sz) {
534 DMWARN("Start sector is beyond the geometry limits.");
535 return -EINVAL;
536 }
537
538 md->geometry = *geo;
539
540 return 0;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*-----------------------------------------------------------------
544 * CRUD START:
545 * A more elegant soln is in the works that uses the queue
546 * merge fn, unfortunately there are a couple of changes to
547 * the block layer that I want to make for this. So in the
548 * interests of getting something for people to use I give
549 * you this clearly demarcated crap.
550 *---------------------------------------------------------------*/
551
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800552static int __noflush_suspending(struct mapped_device *md)
553{
554 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/*
558 * Decrements the number of outstanding ios that a bio has been
559 * cloned into, completing the original io if necc.
560 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800561static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800563 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000564 int io_error;
565 struct bio *bio;
566 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800567
568 /* Push-back supersedes any I/O errors */
Milan Brozb35f8ca2009-03-16 17:44:36 +0000569 if (error && !(io->error > 0 && __noflush_suspending(md)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 io->error = error;
571
572 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800573 if (io->error == DM_ENDIO_REQUEUE) {
574 /*
575 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800576 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100577 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100578 if (__noflush_suspending(md)) {
579 if (!bio_barrier(io->bio))
580 bio_list_add_head(&md->deferred,
581 io->bio);
582 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800583 /* noflush suspend was interrupted. */
584 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100585 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800586 }
587
Milan Brozb35f8ca2009-03-16 17:44:36 +0000588 io_error = io->error;
589 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100590
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100591 if (bio_barrier(bio)) {
592 /*
593 * There can be just one barrier request so we use
594 * a per-device variable for error reporting.
595 * Note that you can't touch the bio after end_io_acct
596 */
Mikulas Patockafdb95722009-06-22 10:12:19 +0100597 if (!md->barrier_error && io_error != -EOPNOTSUPP)
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100598 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100599 end_io_acct(io);
600 } else {
601 end_io_acct(io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000602
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100603 if (io_error != DM_ENDIO_REQUEUE) {
604 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000605
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100606 bio_endio(bio, io_error);
607 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800608 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100609
610 free_io(md, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612}
613
NeilBrown6712ecf2007-09-27 12:47:43 +0200614static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100617 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000618 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700619 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 dm_endio_fn endio = tio->ti->type->end_io;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
623 error = -EIO;
624
625 if (endio) {
626 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800627 if (r < 0 || r == DM_ENDIO_REQUEUE)
628 /*
629 * error and requeue request are handled
630 * in dec_pending().
631 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800633 else if (r == DM_ENDIO_INCOMPLETE)
634 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200635 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800636 else if (r) {
637 DMWARN("unimplemented target endio return value: %d", r);
638 BUG();
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
Stefan Bader9faf4002006-10-03 01:15:41 -0700642 /*
643 * Store md for cleanup instead of tio which is about to get freed.
644 */
645 bio->bi_private = md->bs;
646
Stefan Bader9faf4002006-10-03 01:15:41 -0700647 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000648 bio_put(bio);
649 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100652/*
653 * Partial completion handling for request-based dm
654 */
655static void end_clone_bio(struct bio *clone, int error)
656{
657 struct dm_rq_clone_bio_info *info = clone->bi_private;
658 struct dm_rq_target_io *tio = info->tio;
659 struct bio *bio = info->orig;
660 unsigned int nr_bytes = info->orig->bi_size;
661
662 bio_put(clone);
663
664 if (tio->error)
665 /*
666 * An error has already been detected on the request.
667 * Once error occurred, just let clone->end_io() handle
668 * the remainder.
669 */
670 return;
671 else if (error) {
672 /*
673 * Don't notice the error to the upper layer yet.
674 * The error handling decision is made by the target driver,
675 * when the request is completed.
676 */
677 tio->error = error;
678 return;
679 }
680
681 /*
682 * I/O for the bio successfully completed.
683 * Notice the data completion to the upper layer.
684 */
685
686 /*
687 * bios are processed from the head of the list.
688 * So the completing bio should always be rq->bio.
689 * If it's not, something wrong is happening.
690 */
691 if (tio->orig->bio != bio)
692 DMERR("bio completion is going in the middle of the request");
693
694 /*
695 * Update the original request.
696 * Do not use blk_end_request() here, because it may complete
697 * the original request before the clone, and break the ordering.
698 */
699 blk_update_request(tio->orig, 0, nr_bytes);
700}
701
702/*
703 * Don't touch any member of the md after calling this function because
704 * the md may be freed in dm_put() at the end of this function.
705 * Or do dm_get() before calling this function and dm_put() later.
706 */
707static void rq_completed(struct mapped_device *md, int run_queue)
708{
709 int wakeup_waiters = 0;
710 struct request_queue *q = md->queue;
711 unsigned long flags;
712
713 spin_lock_irqsave(q->queue_lock, flags);
714 if (!queue_in_flight(q))
715 wakeup_waiters = 1;
716 spin_unlock_irqrestore(q->queue_lock, flags);
717
718 /* nudge anyone waiting on suspend queue */
719 if (wakeup_waiters)
720 wake_up(&md->wait);
721
722 if (run_queue)
723 blk_run_queue(q);
724
725 /*
726 * dm_put() must be at the end of this function. See the comment above
727 */
728 dm_put(md);
729}
730
731static void dm_unprep_request(struct request *rq)
732{
733 struct request *clone = rq->special;
734 struct dm_rq_target_io *tio = clone->end_io_data;
735
736 rq->special = NULL;
737 rq->cmd_flags &= ~REQ_DONTPREP;
738
739 blk_rq_unprep_clone(clone);
740 free_rq_tio(tio);
741}
742
743/*
744 * Requeue the original request of a clone.
745 */
746void dm_requeue_unmapped_request(struct request *clone)
747{
748 struct dm_rq_target_io *tio = clone->end_io_data;
749 struct mapped_device *md = tio->md;
750 struct request *rq = tio->orig;
751 struct request_queue *q = rq->q;
752 unsigned long flags;
753
754 dm_unprep_request(rq);
755
756 spin_lock_irqsave(q->queue_lock, flags);
757 if (elv_queue_empty(q))
758 blk_plug_device(q);
759 blk_requeue_request(q, rq);
760 spin_unlock_irqrestore(q->queue_lock, flags);
761
762 rq_completed(md, 0);
763}
764EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
765
766static void __stop_queue(struct request_queue *q)
767{
768 blk_stop_queue(q);
769}
770
771static void stop_queue(struct request_queue *q)
772{
773 unsigned long flags;
774
775 spin_lock_irqsave(q->queue_lock, flags);
776 __stop_queue(q);
777 spin_unlock_irqrestore(q->queue_lock, flags);
778}
779
780static void __start_queue(struct request_queue *q)
781{
782 if (blk_queue_stopped(q))
783 blk_start_queue(q);
784}
785
786static void start_queue(struct request_queue *q)
787{
788 unsigned long flags;
789
790 spin_lock_irqsave(q->queue_lock, flags);
791 __start_queue(q);
792 spin_unlock_irqrestore(q->queue_lock, flags);
793}
794
795/*
796 * Complete the clone and the original request.
797 * Must be called without queue lock.
798 */
799static void dm_end_request(struct request *clone, int error)
800{
801 struct dm_rq_target_io *tio = clone->end_io_data;
802 struct mapped_device *md = tio->md;
803 struct request *rq = tio->orig;
804
805 if (blk_pc_request(rq)) {
806 rq->errors = clone->errors;
807 rq->resid_len = clone->resid_len;
808
809 if (rq->sense)
810 /*
811 * We are using the sense buffer of the original
812 * request.
813 * So setting the length of the sense data is enough.
814 */
815 rq->sense_len = clone->sense_len;
816 }
817
818 BUG_ON(clone->bio);
819 free_rq_tio(tio);
820
821 blk_end_request_all(rq, error);
822
823 rq_completed(md, 1);
824}
825
826/*
827 * Request completion handler for request-based dm
828 */
829static void dm_softirq_done(struct request *rq)
830{
831 struct request *clone = rq->completion_data;
832 struct dm_rq_target_io *tio = clone->end_io_data;
833 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
834 int error = tio->error;
835
836 if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
837 error = rq_end_io(tio->ti, clone, error, &tio->info);
838
839 if (error <= 0)
840 /* The target wants to complete the I/O */
841 dm_end_request(clone, error);
842 else if (error == DM_ENDIO_INCOMPLETE)
843 /* The target will handle the I/O */
844 return;
845 else if (error == DM_ENDIO_REQUEUE)
846 /* The target wants to requeue the I/O */
847 dm_requeue_unmapped_request(clone);
848 else {
849 DMWARN("unimplemented target endio return value: %d", error);
850 BUG();
851 }
852}
853
854/*
855 * Complete the clone and the original request with the error status
856 * through softirq context.
857 */
858static void dm_complete_request(struct request *clone, int error)
859{
860 struct dm_rq_target_io *tio = clone->end_io_data;
861 struct request *rq = tio->orig;
862
863 tio->error = error;
864 rq->completion_data = clone;
865 blk_complete_request(rq);
866}
867
868/*
869 * Complete the not-mapped clone and the original request with the error status
870 * through softirq context.
871 * Target's rq_end_io() function isn't called.
872 * This may be used when the target's map_rq() function fails.
873 */
874void dm_kill_unmapped_request(struct request *clone, int error)
875{
876 struct dm_rq_target_io *tio = clone->end_io_data;
877 struct request *rq = tio->orig;
878
879 rq->cmd_flags |= REQ_FAILED;
880 dm_complete_request(clone, error);
881}
882EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
883
884/*
885 * Called with the queue lock held
886 */
887static void end_clone_request(struct request *clone, int error)
888{
889 /*
890 * For just cleaning up the information of the queue in which
891 * the clone was dispatched.
892 * The clone is *NOT* freed actually here because it is alloced from
893 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
894 */
895 __blk_put_request(clone->q, clone);
896
897 /*
898 * Actual request completion is done in a softirq context which doesn't
899 * hold the queue lock. Otherwise, deadlock could occur because:
900 * - another request may be submitted by the upper level driver
901 * of the stacking during the completion
902 * - the submission which requires queue lock may be done
903 * against this queue
904 */
905 dm_complete_request(clone, error);
906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908static sector_t max_io_len(struct mapped_device *md,
909 sector_t sector, struct dm_target *ti)
910{
911 sector_t offset = sector - ti->begin;
912 sector_t len = ti->len - offset;
913
914 /*
915 * Does the target need to split even further ?
916 */
917 if (ti->split_io) {
918 sector_t boundary;
919 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
920 - offset;
921 if (len > boundary)
922 len = boundary;
923 }
924
925 return len;
926}
927
928static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100929 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100932 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700933 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 clone->bi_end_io = clone_endio;
936 clone->bi_private = tio;
937
938 /*
939 * Map the clone. If r == 0 we don't need to do
940 * anything, the target has assumed ownership of
941 * this io.
942 */
943 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100944 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800946 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100948
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100949 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400950 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800953 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
954 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700955 md = tio->io->md;
956 dec_pending(tio->io, r);
957 /*
958 * Store bio_set for cleanup.
959 */
960 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700962 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800963 } else if (r) {
964 DMWARN("unimplemented target map return value: %d", r);
965 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967}
968
969struct clone_info {
970 struct mapped_device *md;
971 struct dm_table *map;
972 struct bio *bio;
973 struct dm_io *io;
974 sector_t sector;
975 sector_t sector_count;
976 unsigned short idx;
977};
978
Peter Osterlund36763472005-09-06 15:16:42 -0700979static void dm_bio_destructor(struct bio *bio)
980{
Stefan Bader9faf4002006-10-03 01:15:41 -0700981 struct bio_set *bs = bio->bi_private;
982
983 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
987 * Creates a little bio that is just does part of a bvec.
988 */
989static struct bio *split_bvec(struct bio *bio, sector_t sector,
990 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -0700991 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 struct bio *clone;
994 struct bio_vec *bv = bio->bi_io_vec + idx;
995
Stefan Bader9faf4002006-10-03 01:15:41 -0700996 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700997 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 *clone->bi_io_vec = *bv;
999
1000 clone->bi_sector = sector;
1001 clone->bi_bdev = bio->bi_bdev;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001002 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 clone->bi_vcnt = 1;
1004 clone->bi_size = to_bytes(len);
1005 clone->bi_io_vec->bv_offset = offset;
1006 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001007 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Martin K. Petersen9c470082009-04-09 00:27:12 +01001009 if (bio_integrity(bio)) {
1010 bio_integrity_clone(clone, bio, GFP_NOIO);
1011 bio_integrity_trim(clone,
1012 bio_sector_offset(bio, idx, offset), len);
1013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return clone;
1016}
1017
1018/*
1019 * Creates a bio that consists of range of complete bvecs.
1020 */
1021static struct bio *clone_bio(struct bio *bio, sector_t sector,
1022 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001023 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct bio *clone;
1026
Stefan Bader9faf4002006-10-03 01:15:41 -07001027 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1028 __bio_clone(clone, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001029 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
Stefan Bader9faf4002006-10-03 01:15:41 -07001030 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 clone->bi_sector = sector;
1032 clone->bi_idx = idx;
1033 clone->bi_vcnt = idx + bv_count;
1034 clone->bi_size = to_bytes(len);
1035 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1036
Martin K. Petersen9c470082009-04-09 00:27:12 +01001037 if (bio_integrity(bio)) {
1038 bio_integrity_clone(clone, bio, GFP_NOIO);
1039
1040 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1041 bio_integrity_trim(clone,
1042 bio_sector_offset(bio, idx, 0), len);
1043 }
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return clone;
1046}
1047
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001048static struct dm_target_io *alloc_tio(struct clone_info *ci,
1049 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001050{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001051 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001052
1053 tio->io = ci->io;
1054 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001055 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001056
1057 return tio;
1058}
1059
1060static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1061 unsigned flush_nr)
1062{
1063 struct dm_target_io *tio = alloc_tio(ci, ti);
1064 struct bio *clone;
1065
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001066 tio->info.flush_request = flush_nr;
1067
1068 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1069 __bio_clone(clone, ci->bio);
1070 clone->bi_destructor = dm_bio_destructor;
1071
1072 __map_bio(ti, clone, tio);
1073}
1074
1075static int __clone_and_map_empty_barrier(struct clone_info *ci)
1076{
1077 unsigned target_nr = 0, flush_nr;
1078 struct dm_target *ti;
1079
1080 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1081 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1082 flush_nr++)
1083 __flush_target(ci, ti, flush_nr);
1084
1085 ci->sector_count = 0;
1086
1087 return 0;
1088}
1089
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001090static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001093 struct dm_target *ti;
1094 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001095 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001097 if (unlikely(bio_empty_barrier(bio)))
1098 return __clone_and_map_empty_barrier(ci);
1099
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001100 ti = dm_table_find_target(ci->map, ci->sector);
1101 if (!dm_target_is_valid(ti))
1102 return -EIO;
1103
1104 max = max_io_len(ci->md, ci->sector, ti);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * Allocate a target io object.
1108 */
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001109 tio = alloc_tio(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (ci->sector_count <= max) {
1112 /*
1113 * Optimise for the simple case where we can do all of
1114 * the remaining io with a single clone.
1115 */
1116 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001117 bio->bi_vcnt - ci->idx, ci->sector_count,
1118 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 __map_bio(ti, clone, tio);
1120 ci->sector_count = 0;
1121
1122 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1123 /*
1124 * There are some bvecs that don't span targets.
1125 * Do as many of these as possible.
1126 */
1127 int i;
1128 sector_t remaining = max;
1129 sector_t bv_len;
1130
1131 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1132 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1133
1134 if (bv_len > remaining)
1135 break;
1136
1137 remaining -= bv_len;
1138 len += bv_len;
1139 }
1140
Stefan Bader9faf4002006-10-03 01:15:41 -07001141 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1142 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 __map_bio(ti, clone, tio);
1144
1145 ci->sector += len;
1146 ci->sector_count -= len;
1147 ci->idx = i;
1148
1149 } else {
1150 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001151 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
1153 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001154 sector_t remaining = to_sector(bv->bv_len);
1155 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001157 do {
1158 if (offset) {
1159 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001160 if (!dm_target_is_valid(ti))
1161 return -EIO;
1162
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001163 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001165 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001168 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001170 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001171 bv->bv_offset + offset, len,
1172 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001173
1174 __map_bio(ti, clone, tio);
1175
1176 ci->sector += len;
1177 ci->sector_count -= len;
1178 offset += to_bytes(len);
1179 } while (remaining -= len);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 ci->idx++;
1182 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001183
1184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001188 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001190static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001193 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 ci.map = dm_get_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001196 if (unlikely(!ci.map)) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001197 if (!bio_barrier(bio))
1198 bio_io_error(bio);
1199 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001200 if (!md->barrier_error)
1201 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001202 return;
1203 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ci.md = md;
1206 ci.bio = bio;
1207 ci.io = alloc_io(md);
1208 ci.io->error = 0;
1209 atomic_set(&ci.io->io_count, 1);
1210 ci.io->bio = bio;
1211 ci.io->md = md;
1212 ci.sector = bio->bi_sector;
1213 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001214 if (unlikely(bio_empty_barrier(bio)))
1215 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ci.idx = bio->bi_idx;
1217
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001218 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001219 while (ci.sector_count && !error)
1220 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001223 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 dm_table_put(ci.map);
1225}
1226/*-----------------------------------------------------------------
1227 * CRUD END
1228 *---------------------------------------------------------------*/
1229
Milan Brozf6fccb12008-07-21 12:00:37 +01001230static int dm_merge_bvec(struct request_queue *q,
1231 struct bvec_merge_data *bvm,
1232 struct bio_vec *biovec)
1233{
1234 struct mapped_device *md = q->queuedata;
1235 struct dm_table *map = dm_get_table(md);
1236 struct dm_target *ti;
1237 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001238 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001239
1240 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001241 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001242
1243 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001244 if (!dm_target_is_valid(ti))
1245 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001246
1247 /*
1248 * Find maximum amount of I/O that won't need splitting
1249 */
1250 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1251 (sector_t) BIO_MAX_SECTORS);
1252 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1253 if (max_size < 0)
1254 max_size = 0;
1255
1256 /*
1257 * merge_bvec_fn() returns number of bytes
1258 * it can accept at this offset
1259 * max is precomputed maximal io size
1260 */
1261 if (max_size && ti->type->merge)
1262 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001263 /*
1264 * If the target doesn't support merge method and some of the devices
1265 * provided their merge_bvec method (we know this by looking at
1266 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1267 * entries. So always set max_size to 0, and the code below allows
1268 * just one page.
1269 */
1270 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1271
1272 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001273
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001274out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001275 dm_table_put(map);
1276
1277out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001278 /*
1279 * Always allow an entire first page
1280 */
1281 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1282 max_size = biovec->bv_len;
1283
Milan Brozf6fccb12008-07-21 12:00:37 +01001284 return max_size;
1285}
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/*
1288 * The request function that just remaps the bio built up by
1289 * dm_merge_bvec.
1290 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001291static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Kevin Corry12f03a42006-02-01 03:04:52 -08001293 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001295 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001297 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Tejun Heo074a7ac2008-08-25 19:56:14 +09001299 cpu = part_stat_lock();
1300 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1301 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1302 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001305 * If we're suspended or the thread is processing barriers
1306 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001308 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1309 unlikely(bio_barrier(bio))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001310 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001312 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1313 bio_rw(bio) == READA) {
1314 bio_io_error(bio);
1315 return 0;
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Mikulas Patocka92c63902009-04-09 00:27:15 +01001318 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Mikulas Patocka92c63902009-04-09 00:27:15 +01001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001323 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001324 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001328static int dm_make_request(struct request_queue *q, struct bio *bio)
1329{
1330 struct mapped_device *md = q->queuedata;
1331
1332 if (unlikely(bio_barrier(bio))) {
1333 bio_endio(bio, -EOPNOTSUPP);
1334 return 0;
1335 }
1336
1337 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1338}
1339
1340static int dm_request_based(struct mapped_device *md)
1341{
1342 return blk_queue_stackable(md->queue);
1343}
1344
1345static int dm_request(struct request_queue *q, struct bio *bio)
1346{
1347 struct mapped_device *md = q->queuedata;
1348
1349 if (dm_request_based(md))
1350 return dm_make_request(q, bio);
1351
1352 return _dm_request(q, bio);
1353}
1354
1355void dm_dispatch_request(struct request *rq)
1356{
1357 int r;
1358
1359 if (blk_queue_io_stat(rq->q))
1360 rq->cmd_flags |= REQ_IO_STAT;
1361
1362 rq->start_time = jiffies;
1363 r = blk_insert_cloned_request(rq->q, rq);
1364 if (r)
1365 dm_complete_request(rq, r);
1366}
1367EXPORT_SYMBOL_GPL(dm_dispatch_request);
1368
1369static void dm_rq_bio_destructor(struct bio *bio)
1370{
1371 struct dm_rq_clone_bio_info *info = bio->bi_private;
1372 struct mapped_device *md = info->tio->md;
1373
1374 free_bio_info(info);
1375 bio_free(bio, md->bs);
1376}
1377
1378static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1379 void *data)
1380{
1381 struct dm_rq_target_io *tio = data;
1382 struct mapped_device *md = tio->md;
1383 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1384
1385 if (!info)
1386 return -ENOMEM;
1387
1388 info->orig = bio_orig;
1389 info->tio = tio;
1390 bio->bi_end_io = end_clone_bio;
1391 bio->bi_private = info;
1392 bio->bi_destructor = dm_rq_bio_destructor;
1393
1394 return 0;
1395}
1396
1397static int setup_clone(struct request *clone, struct request *rq,
1398 struct dm_rq_target_io *tio)
1399{
1400 int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1401 dm_rq_bio_constructor, tio);
1402
1403 if (r)
1404 return r;
1405
1406 clone->cmd = rq->cmd;
1407 clone->cmd_len = rq->cmd_len;
1408 clone->sense = rq->sense;
1409 clone->buffer = rq->buffer;
1410 clone->end_io = end_clone_request;
1411 clone->end_io_data = tio;
1412
1413 return 0;
1414}
1415
1416static int dm_rq_flush_suspending(struct mapped_device *md)
1417{
1418 return !md->suspend_rq.special;
1419}
1420
1421/*
1422 * Called with the queue lock held.
1423 */
1424static int dm_prep_fn(struct request_queue *q, struct request *rq)
1425{
1426 struct mapped_device *md = q->queuedata;
1427 struct dm_rq_target_io *tio;
1428 struct request *clone;
1429
1430 if (unlikely(rq == &md->suspend_rq)) {
1431 if (dm_rq_flush_suspending(md))
1432 return BLKPREP_OK;
1433 else
1434 /* The flush suspend was interrupted */
1435 return BLKPREP_KILL;
1436 }
1437
1438 if (unlikely(rq->special)) {
1439 DMWARN("Already has something in rq->special.");
1440 return BLKPREP_KILL;
1441 }
1442
1443 tio = alloc_rq_tio(md); /* Only one for each original request */
1444 if (!tio)
1445 /* -ENOMEM */
1446 return BLKPREP_DEFER;
1447
1448 tio->md = md;
1449 tio->ti = NULL;
1450 tio->orig = rq;
1451 tio->error = 0;
1452 memset(&tio->info, 0, sizeof(tio->info));
1453
1454 clone = &tio->clone;
1455 if (setup_clone(clone, rq, tio)) {
1456 /* -ENOMEM */
1457 free_rq_tio(tio);
1458 return BLKPREP_DEFER;
1459 }
1460
1461 rq->special = clone;
1462 rq->cmd_flags |= REQ_DONTPREP;
1463
1464 return BLKPREP_OK;
1465}
1466
1467static void map_request(struct dm_target *ti, struct request *rq,
1468 struct mapped_device *md)
1469{
1470 int r;
1471 struct request *clone = rq->special;
1472 struct dm_rq_target_io *tio = clone->end_io_data;
1473
1474 /*
1475 * Hold the md reference here for the in-flight I/O.
1476 * We can't rely on the reference count by device opener,
1477 * because the device may be closed during the request completion
1478 * when all bios are completed.
1479 * See the comment in rq_completed() too.
1480 */
1481 dm_get(md);
1482
1483 tio->ti = ti;
1484 r = ti->type->map_rq(ti, clone, &tio->info);
1485 switch (r) {
1486 case DM_MAPIO_SUBMITTED:
1487 /* The target has taken the I/O to submit by itself later */
1488 break;
1489 case DM_MAPIO_REMAPPED:
1490 /* The target has remapped the I/O so dispatch it */
1491 dm_dispatch_request(clone);
1492 break;
1493 case DM_MAPIO_REQUEUE:
1494 /* The target wants to requeue the I/O */
1495 dm_requeue_unmapped_request(clone);
1496 break;
1497 default:
1498 if (r > 0) {
1499 DMWARN("unimplemented target map return value: %d", r);
1500 BUG();
1501 }
1502
1503 /* The target wants to complete the I/O */
1504 dm_kill_unmapped_request(clone, r);
1505 break;
1506 }
1507}
1508
1509/*
1510 * q->request_fn for request-based dm.
1511 * Called with the queue lock held.
1512 */
1513static void dm_request_fn(struct request_queue *q)
1514{
1515 struct mapped_device *md = q->queuedata;
1516 struct dm_table *map = dm_get_table(md);
1517 struct dm_target *ti;
1518 struct request *rq;
1519
1520 /*
1521 * For noflush suspend, check blk_queue_stopped() to immediately
1522 * quit I/O dispatching.
1523 */
1524 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1525 rq = blk_peek_request(q);
1526 if (!rq)
1527 goto plug_and_out;
1528
1529 if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1530 if (queue_in_flight(q))
1531 /* Not quiet yet. Wait more */
1532 goto plug_and_out;
1533
1534 /* This device should be quiet now */
1535 __stop_queue(q);
1536 blk_start_request(rq);
1537 __blk_end_request_all(rq, 0);
1538 wake_up(&md->wait);
1539 goto out;
1540 }
1541
1542 ti = dm_table_find_target(map, blk_rq_pos(rq));
1543 if (ti->type->busy && ti->type->busy(ti))
1544 goto plug_and_out;
1545
1546 blk_start_request(rq);
1547 spin_unlock(q->queue_lock);
1548 map_request(ti, rq, md);
1549 spin_lock_irq(q->queue_lock);
1550 }
1551
1552 goto out;
1553
1554plug_and_out:
1555 if (!elv_queue_empty(q))
1556 /* Some requests still remain, retry later */
1557 blk_plug_device(q);
1558
1559out:
1560 dm_table_put(map);
1561
1562 return;
1563}
1564
1565int dm_underlying_device_busy(struct request_queue *q)
1566{
1567 return blk_lld_busy(q);
1568}
1569EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1570
1571static int dm_lld_busy(struct request_queue *q)
1572{
1573 int r;
1574 struct mapped_device *md = q->queuedata;
1575 struct dm_table *map = dm_get_table(md);
1576
1577 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1578 r = 1;
1579 else
1580 r = dm_table_any_busy_target(map);
1581
1582 dm_table_put(map);
1583
1584 return r;
1585}
1586
Jens Axboe165125e2007-07-24 09:28:11 +02001587static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 struct mapped_device *md = q->queuedata;
1590 struct dm_table *map = dm_get_table(md);
1591
1592 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001593 if (dm_request_based(md))
1594 generic_unplug_device(q);
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 dm_table_unplug_all(map);
1597 dm_table_put(map);
1598 }
1599}
1600
1601static int dm_any_congested(void *congested_data, int bdi_bits)
1602{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001603 int r = bdi_bits;
1604 struct mapped_device *md = congested_data;
1605 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001607 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001608 map = dm_get_table(md);
1609 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001610 /*
1611 * Request-based dm cares about only own queue for
1612 * the query about congestion status of request_queue
1613 */
1614 if (dm_request_based(md))
1615 r = md->queue->backing_dev_info.state &
1616 bdi_bits;
1617 else
1618 r = dm_table_any_congested(map, bdi_bits);
1619
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001620 dm_table_put(map);
1621 }
1622 }
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return r;
1625}
1626
1627/*-----------------------------------------------------------------
1628 * An IDR is used to keep track of allocated minor numbers.
1629 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630static DEFINE_IDR(_minor_idr);
1631
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001632static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001634 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001636 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639/*
1640 * See if the device with a specific minor # is free.
1641 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001642static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 int r, m;
1645
1646 if (minor >= (1 << MINORBITS))
1647 return -EINVAL;
1648
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001649 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1650 if (!r)
1651 return -ENOMEM;
1652
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001653 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (idr_find(&_minor_idr, minor)) {
1656 r = -EBUSY;
1657 goto out;
1658 }
1659
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001660 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001661 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (m != minor) {
1665 idr_remove(&_minor_idr, m);
1666 r = -EBUSY;
1667 goto out;
1668 }
1669
1670out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001671 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return r;
1673}
1674
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001675static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001677 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001680 if (!r)
1681 return -ENOMEM;
1682
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001683 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001685 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001686 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (m >= (1 << MINORBITS)) {
1690 idr_remove(&_minor_idr, m);
1691 r = -ENOSPC;
1692 goto out;
1693 }
1694
1695 *minor = m;
1696
1697out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001698 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return r;
1700}
1701
1702static struct block_device_operations dm_blk_dops;
1703
Mikulas Patocka53d59142009-04-02 19:55:37 +01001704static void dm_wq_work(struct work_struct *work);
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * Allocate and initialise a blank device with a given minor.
1708 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001709static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001712 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001713 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 if (!md) {
1716 DMWARN("unable to allocate device, out of memory.");
1717 return NULL;
1718 }
1719
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001720 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001721 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001724 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001725 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001726 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001727 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001729 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001731 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001732 mutex_init(&md->suspend_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001733 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 rwlock_init(&md->map_lock);
1735 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001736 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001738 atomic_set(&md->uevent_seq, 0);
1739 INIT_LIST_HEAD(&md->uevent_list);
1740 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 md->queue = blk_alloc_queue(GFP_KERNEL);
1743 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001744 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 md->queue->queuedata = md;
1747 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1748 md->queue->backing_dev_info.congested_data = md;
1749 blk_queue_make_request(md->queue, dm_request);
Mikulas Patocka99360b42009-04-02 19:55:39 +01001750 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN, NULL);
Jens Axboedaef2652006-01-10 10:48:02 +01001751 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001753 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Matthew Dobson93d23412006-03-26 01:37:50 -08001755 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
Kiyoshi Ueda74859362006-12-08 02:41:02 -08001756 if (!md->io_pool)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001757 goto bad_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Matthew Dobson93d23412006-03-26 01:37:50 -08001759 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (!md->tio_pool)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001761 goto bad_tio_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Jens Axboebb799ca2008-12-10 15:35:05 +01001763 md->bs = bioset_create(16, 0);
Stefan Bader9faf4002006-10-03 01:15:41 -07001764 if (!md->bs)
1765 goto bad_no_bioset;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 md->disk = alloc_disk(1);
1768 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001769 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001771 atomic_set(&md->pending, 0);
1772 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001773 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001774 init_waitqueue_head(&md->eventq);
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 md->disk->major = _major;
1777 md->disk->first_minor = minor;
1778 md->disk->fops = &dm_blk_dops;
1779 md->disk->queue = md->queue;
1780 md->disk->private_data = md;
1781 sprintf(md->disk->disk_name, "dm-%d", minor);
1782 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001783 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Milan Broz304f3f62008-02-08 02:11:17 +00001785 md->wq = create_singlethread_workqueue("kdmflush");
1786 if (!md->wq)
1787 goto bad_thread;
1788
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001789 md->bdev = bdget_disk(md->disk, 0);
1790 if (!md->bdev)
1791 goto bad_bdev;
1792
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001793 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001794 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001795 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001796 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001797
1798 BUG_ON(old_md != MINOR_ALLOCED);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return md;
1801
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001802bad_bdev:
1803 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001804bad_thread:
1805 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001806bad_disk:
Stefan Bader9faf4002006-10-03 01:15:41 -07001807 bioset_free(md->bs);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001808bad_no_bioset:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 mempool_destroy(md->tio_pool);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001810bad_tio_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 mempool_destroy(md->io_pool);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001812bad_io_pool:
Al Viro1312f402006-03-12 11:02:03 -05001813 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001814bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001816bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001817 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001818bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 kfree(md);
1820 return NULL;
1821}
1822
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001823static void unlock_fs(struct mapped_device *md);
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825static void free_dev(struct mapped_device *md)
1826{
Tejun Heof331c022008-09-03 09:01:48 +02001827 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001828
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001829 unlock_fs(md);
1830 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001831 destroy_workqueue(md->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 mempool_destroy(md->tio_pool);
1833 mempool_destroy(md->io_pool);
Stefan Bader9faf4002006-10-03 01:15:41 -07001834 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001835 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001837 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001838
1839 spin_lock(&_minor_lock);
1840 md->disk->private_data = NULL;
1841 spin_unlock(&_minor_lock);
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001844 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001845 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 kfree(md);
1847}
1848
1849/*
1850 * Bind a table to the device.
1851 */
1852static void event_callback(void *context)
1853{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001854 unsigned long flags;
1855 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct mapped_device *md = (struct mapped_device *) context;
1857
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001858 spin_lock_irqsave(&md->uevent_lock, flags);
1859 list_splice_init(&md->uevent_list, &uevents);
1860 spin_unlock_irqrestore(&md->uevent_lock, flags);
1861
Tejun Heoed9e1982008-08-25 19:56:05 +09001862 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 atomic_inc(&md->event_nr);
1865 wake_up(&md->eventq);
1866}
1867
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001868static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001870 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001872 mutex_lock(&md->bdev->bd_inode->i_mutex);
1873 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1874 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001877static int __bind(struct mapped_device *md, struct dm_table *t,
1878 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Jens Axboe165125e2007-07-24 09:28:11 +02001880 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 sector_t size;
1882
1883 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001884
1885 /*
1886 * Wipe any geometry if the size of the table changed.
1887 */
1888 if (size != get_capacity(md->disk))
1889 memset(&md->geometry, 0, sizeof(md->geometry));
1890
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001891 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Mikulas Patockad5816872009-01-06 03:05:10 +00001893 if (!size) {
1894 dm_table_destroy(t);
1895 return 0;
1896 }
1897
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001898 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001899
1900 write_lock(&md->map_lock);
1901 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001902 dm_table_set_restrictions(t, q, limits);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001903 write_unlock(&md->map_lock);
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return 0;
1906}
1907
1908static void __unbind(struct mapped_device *md)
1909{
1910 struct dm_table *map = md->map;
1911
1912 if (!map)
1913 return;
1914
1915 dm_table_event_callback(map, NULL, NULL);
1916 write_lock(&md->map_lock);
1917 md->map = NULL;
1918 write_unlock(&md->map_lock);
Mikulas Patockad5816872009-01-06 03:05:10 +00001919 dm_table_destroy(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
1922/*
1923 * Constructor for a new device.
1924 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001925int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 struct mapped_device *md;
1928
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001929 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (!md)
1931 return -ENXIO;
1932
Milan Broz784aae72009-01-06 03:05:12 +00001933 dm_sysfs_init(md);
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 *result = md;
1936 return 0;
1937}
1938
David Teigland637842c2006-01-06 00:20:00 -08001939static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 unsigned minor = MINOR(dev);
1943
1944 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1945 return NULL;
1946
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001947 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001950 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02001951 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07001952 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08001953 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001954 goto out;
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001957out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001958 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
David Teigland637842c2006-01-06 00:20:00 -08001960 return md;
1961}
1962
David Teiglandd229a952006-01-06 00:20:01 -08001963struct mapped_device *dm_get_md(dev_t dev)
1964{
1965 struct mapped_device *md = dm_find_md(dev);
1966
1967 if (md)
1968 dm_get(md);
1969
1970 return md;
1971}
1972
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001973void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001974{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001975 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
1978void dm_set_mdptr(struct mapped_device *md, void *ptr)
1979{
1980 md->interface_ptr = ptr;
1981}
1982
1983void dm_get(struct mapped_device *md)
1984{
1985 atomic_inc(&md->holders);
1986}
1987
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001988const char *dm_device_name(struct mapped_device *md)
1989{
1990 return md->name;
1991}
1992EXPORT_SYMBOL_GPL(dm_device_name);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994void dm_put(struct mapped_device *md)
1995{
Mike Anderson1134e5a2006-03-27 01:17:54 -08001996 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001998 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1999
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002000 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08002001 map = dm_get_table(md);
Tejun Heof331c022008-09-03 09:01:48 +02002002 idr_replace(&_minor_idr, MINOR_ALLOCED,
2003 MINOR(disk_devt(dm_disk(md))));
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002004 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002005 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002006 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 dm_table_presuspend_targets(map);
2008 dm_table_postsuspend_targets(map);
2009 }
Milan Broz784aae72009-01-06 03:05:12 +00002010 dm_sysfs_exit(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002011 dm_table_put(map);
Mikulas Patockaa1b51e92009-01-06 03:04:53 +00002012 __unbind(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 free_dev(md);
2014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
Edward Goggin79eb8852007-05-09 02:32:56 -07002016EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Mikulas Patocka401600d2009-04-02 19:55:38 +01002018static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002019{
2020 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002021 DECLARE_WAITQUEUE(wait, current);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002022 struct request_queue *q = md->queue;
2023 unsigned long flags;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002024
2025 dm_unplug_all(md->queue);
2026
2027 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002028
2029 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002030 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002031
2032 smp_mb();
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002033 if (dm_request_based(md)) {
2034 spin_lock_irqsave(q->queue_lock, flags);
2035 if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2036 spin_unlock_irqrestore(q->queue_lock, flags);
2037 break;
2038 }
2039 spin_unlock_irqrestore(q->queue_lock, flags);
2040 } else if (!atomic_read(&md->pending))
Milan Broz46125c12008-02-08 02:10:30 +00002041 break;
2042
Mikulas Patocka401600d2009-04-02 19:55:38 +01002043 if (interruptible == TASK_INTERRUPTIBLE &&
2044 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002045 r = -EINTR;
2046 break;
2047 }
2048
2049 io_schedule();
2050 }
2051 set_current_state(TASK_RUNNING);
2052
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002053 remove_wait_queue(&md->wait, &wait);
2054
Milan Broz46125c12008-02-08 02:10:30 +00002055 return r;
2056}
2057
Mikulas Patocka531fe962009-06-22 10:12:17 +01002058static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002059{
2060 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002061
2062 bio_init(&md->barrier_bio);
2063 md->barrier_bio.bi_bdev = md->bdev;
2064 md->barrier_bio.bi_rw = WRITE_BARRIER;
2065 __split_and_process_bio(md, &md->barrier_bio);
2066
2067 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002068}
2069
2070static void process_barrier(struct mapped_device *md, struct bio *bio)
2071{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002072 md->barrier_error = 0;
2073
Mikulas Patocka531fe962009-06-22 10:12:17 +01002074 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002075
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002076 if (!bio_empty_barrier(bio)) {
2077 __split_and_process_bio(md, bio);
2078 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002079 }
2080
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002081 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002082 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002083 else {
2084 spin_lock_irq(&md->deferred_lock);
2085 bio_list_add_head(&md->deferred, bio);
2086 spin_unlock_irq(&md->deferred_lock);
2087 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002088}
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090/*
2091 * Process the deferred bios
2092 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002093static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Mikulas Patockaef208582009-04-02 19:55:38 +01002095 struct mapped_device *md = container_of(work, struct mapped_device,
2096 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002097 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Mikulas Patockaef208582009-04-02 19:55:38 +01002099 down_write(&md->io_lock);
2100
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002101 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002102 spin_lock_irq(&md->deferred_lock);
2103 c = bio_list_pop(&md->deferred);
2104 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002105
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002106 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002107 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002108 break;
2109 }
2110
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002111 up_write(&md->io_lock);
2112
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002113 if (bio_barrier(c))
2114 process_barrier(md, c);
2115 else
2116 __split_and_process_bio(md, c);
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002117
2118 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002119 }
Milan Broz73d410c2008-02-08 02:10:25 +00002120
Mikulas Patockaef208582009-04-02 19:55:38 +01002121 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002124static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002125{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002126 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2127 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002128 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002129}
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131/*
2132 * Swap in a new table (destroying old one).
2133 */
2134int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2135{
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002136 struct queue_limits limits;
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002137 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Daniel Walkere61290a2008-02-08 02:10:08 +00002139 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002142 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002145 r = dm_calculate_queue_limits(table, &limits);
2146 if (r)
2147 goto out;
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 __unbind(md);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002150 r = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002152out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002153 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002154 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002157static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2158{
2159 md->suspend_rq.special = (void *)0x1;
2160}
2161
2162static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2163{
2164 struct request_queue *q = md->queue;
2165 unsigned long flags;
2166
2167 spin_lock_irqsave(q->queue_lock, flags);
2168 if (!noflush)
2169 dm_rq_invalidate_suspend_marker(md);
2170 __start_queue(q);
2171 spin_unlock_irqrestore(q->queue_lock, flags);
2172}
2173
2174static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2175{
2176 struct request *rq = &md->suspend_rq;
2177 struct request_queue *q = md->queue;
2178
2179 if (noflush)
2180 stop_queue(q);
2181 else {
2182 blk_rq_init(q, rq);
2183 blk_insert_request(q, rq, 0, NULL);
2184 }
2185}
2186
2187static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2188{
2189 int r = 1;
2190 struct request *rq = &md->suspend_rq;
2191 struct request_queue *q = md->queue;
2192 unsigned long flags;
2193
2194 if (noflush)
2195 return r;
2196
2197 /* The marker must be protected by queue lock if it is in use */
2198 spin_lock_irqsave(q->queue_lock, flags);
2199 if (unlikely(rq->ref_count)) {
2200 /*
2201 * This can happen, when the previous flush suspend was
2202 * interrupted, the marker is still in the queue and
2203 * this flush suspend has been invoked, because we don't
2204 * remove the marker at the time of suspend interruption.
2205 * We have only one marker per mapped_device, so we can't
2206 * start another flush suspend while it is in use.
2207 */
2208 BUG_ON(!rq->special); /* The marker should be invalidated */
2209 DMWARN("Invalidating the previous flush suspend is still in"
2210 " progress. Please retry later.");
2211 r = 0;
2212 }
2213 spin_unlock_irqrestore(q->queue_lock, flags);
2214
2215 return r;
2216}
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218/*
2219 * Functions to lock and unlock any filesystem running on the
2220 * device.
2221 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002222static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002224 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002227
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002228 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002229 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002230 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002231 md->frozen_sb = NULL;
2232 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002233 }
2234
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002235 set_bit(DMF_FROZEN, &md->flags);
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238}
2239
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002240static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002242 if (!test_bit(DMF_FROZEN, &md->flags))
2243 return;
2244
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002245 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002247 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
2250/*
2251 * We need to be able to change a mapping table under a mounted
2252 * filesystem. For example we might want to move some data in
2253 * the background. Before the table can be swapped with
2254 * dm_bind_table, dm_suspend must be called to flush any in
2255 * flight bios and ensure that any further io gets deferred.
2256 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002257/*
2258 * Suspend mechanism in request-based dm.
2259 *
2260 * After the suspend starts, further incoming requests are kept in
2261 * the request_queue and deferred.
2262 * Remaining requests in the request_queue at the start of suspend are flushed
2263 * if it is flush suspend.
2264 * The suspend completes when the following conditions have been satisfied,
2265 * so wait for it:
2266 * 1. q->in_flight is 0 (which means no in_flight request)
2267 * 2. queue has been stopped (which means no request dispatching)
2268 *
2269 *
2270 * Noflush suspend
2271 * ---------------
2272 * Noflush suspend doesn't need to dispatch remaining requests.
2273 * So stop the queue immediately. Then, wait for all in_flight requests
2274 * to be completed or requeued.
2275 *
2276 * To abort noflush suspend, start the queue.
2277 *
2278 *
2279 * Flush suspend
2280 * -------------
2281 * Flush suspend needs to dispatch remaining requests. So stop the queue
2282 * after the remaining requests are completed. (Requeued request must be also
2283 * re-dispatched and completed. Until then, we can't stop the queue.)
2284 *
2285 * During flushing the remaining requests, further incoming requests are also
2286 * inserted to the same queue. To distinguish which requests are to be
2287 * flushed, we insert a marker request to the queue at the time of starting
2288 * flush suspend, like a barrier.
2289 * The dispatching is blocked when the marker is found on the top of the queue.
2290 * And the queue is stopped when all in_flight requests are completed, since
2291 * that means the remaining requests are completely flushed.
2292 * Then, the marker is removed from the queue.
2293 *
2294 * To abort flush suspend, we also need to take care of the marker, not only
2295 * starting the queue.
2296 * We don't remove the marker forcibly from the queue since it's against
2297 * the block-layer manner. Instead, we put a invalidated mark on the marker.
2298 * When the invalidated marker is found on the top of the queue, it is
2299 * immediately removed from the queue, so it doesn't block dispatching.
2300 * Because we have only one marker per mapped_device, we can't start another
2301 * flush suspend until the invalidated marker is removed from the queue.
2302 * So fail and return with -EBUSY in such a case.
2303 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002304int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002306 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002307 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002308 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002309 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Daniel Walkere61290a2008-02-08 02:10:08 +00002311 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002312
Milan Broz73d410c2008-02-08 02:10:25 +00002313 if (dm_suspended(md)) {
2314 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002315 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002318 if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2319 r = -EBUSY;
2320 goto out_unlock;
2321 }
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002325 /*
2326 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2327 * This flag is cleared before dm_suspend returns.
2328 */
2329 if (noflush)
2330 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2331
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002332 /* This does not get reverted if there's an error later. */
2333 dm_table_presuspend_targets(map);
2334
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002335 /*
2336 * Flush I/O to the device. noflush supersedes do_lockfs,
2337 * because lock_fs() needs to flush I/Os.
2338 */
2339 if (!noflush && do_lockfs) {
2340 r = lock_fs(md);
2341 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002342 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002346 * Here we must make sure that no processes are submitting requests
2347 * to target drivers i.e. no one may be executing
2348 * __split_and_process_bio. This is called from dm_request and
2349 * dm_wq_work.
2350 *
2351 * To get all processes out of __split_and_process_bio in dm_request,
2352 * we take the write lock. To prevent any process from reentering
2353 * __split_and_process_bio from dm_request, we set
2354 * DMF_QUEUE_IO_TO_THREAD.
2355 *
2356 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2357 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2358 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2359 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002361 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002362 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2363 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002364 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002366 flush_workqueue(md->wq);
2367
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002368 if (dm_request_based(md))
2369 dm_rq_start_suspend(md, noflush);
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002372 * At this point no more requests are entering target request routines.
2373 * We call dm_wait_for_completion to wait for all existing requests
2374 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002376 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002378 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002379 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002380 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002381 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002384 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002385 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002386
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002387 if (dm_request_based(md))
2388 dm_rq_abort_suspend(md, noflush);
2389
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002390 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002391 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002392 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002393
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002394 /*
2395 * If dm_wait_for_completion returned 0, the device is completely
2396 * quiescent now. There is no request-processing activity. All new
2397 * requests are being added to md->deferred list.
2398 */
2399
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002400 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 set_bit(DMF_SUSPENDED, &md->flags);
2403
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002404out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002406
2407out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002408 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002409 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
2412int dm_resume(struct mapped_device *md)
2413{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002414 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002415 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Daniel Walkere61290a2008-02-08 02:10:08 +00002417 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002418 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002419 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002420
2421 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002422 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Milan Broz8757b772006-10-03 01:15:36 -07002425 r = dm_table_resume_targets(map);
2426 if (r)
2427 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002428
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002429 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002430
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002431 /*
2432 * Flushing deferred I/Os must be done after targets are resumed
2433 * so that mapping of targets can work correctly.
2434 * Request-based dm is queueing the deferred I/Os in its request_queue.
2435 */
2436 if (dm_request_based(md))
2437 start_queue(md->queue);
2438
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002439 unlock_fs(md);
2440
2441 clear_bit(DMF_SUSPENDED, &md->flags);
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002444 r = 0;
2445out:
2446 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002447 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002448
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002449 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452/*-----------------------------------------------------------------
2453 * Event notification.
2454 *---------------------------------------------------------------*/
Milan Broz60935eb2009-06-22 10:12:30 +01002455void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2456 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002457{
Milan Broz60935eb2009-06-22 10:12:30 +01002458 char udev_cookie[DM_COOKIE_LENGTH];
2459 char *envp[] = { udev_cookie, NULL };
2460
2461 if (!cookie)
2462 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2463 else {
2464 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2465 DM_COOKIE_ENV_VAR_NAME, cookie);
2466 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2467 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002468}
2469
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002470uint32_t dm_next_uevent_seq(struct mapped_device *md)
2471{
2472 return atomic_add_return(1, &md->uevent_seq);
2473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475uint32_t dm_get_event_nr(struct mapped_device *md)
2476{
2477 return atomic_read(&md->event_nr);
2478}
2479
2480int dm_wait_event(struct mapped_device *md, int event_nr)
2481{
2482 return wait_event_interruptible(md->eventq,
2483 (event_nr != atomic_read(&md->event_nr)));
2484}
2485
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002486void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2487{
2488 unsigned long flags;
2489
2490 spin_lock_irqsave(&md->uevent_lock, flags);
2491 list_add(elist, &md->uevent_list);
2492 spin_unlock_irqrestore(&md->uevent_lock, flags);
2493}
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495/*
2496 * The gendisk is only valid as long as you have a reference
2497 * count on 'md'.
2498 */
2499struct gendisk *dm_disk(struct mapped_device *md)
2500{
2501 return md->disk;
2502}
2503
Milan Broz784aae72009-01-06 03:05:12 +00002504struct kobject *dm_kobject(struct mapped_device *md)
2505{
2506 return &md->kobj;
2507}
2508
2509/*
2510 * struct mapped_device should not be exported outside of dm.c
2511 * so use this check to verify that kobj is part of md structure
2512 */
2513struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2514{
2515 struct mapped_device *md;
2516
2517 md = container_of(kobj, struct mapped_device, kobj);
2518 if (&md->kobj != kobj)
2519 return NULL;
2520
Milan Broz4d89b7b2009-06-22 10:12:11 +01002521 if (test_bit(DMF_FREEING, &md->flags) ||
2522 test_bit(DMF_DELETING, &md->flags))
2523 return NULL;
2524
Milan Broz784aae72009-01-06 03:05:12 +00002525 dm_get(md);
2526 return md;
2527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529int dm_suspended(struct mapped_device *md)
2530{
2531 return test_bit(DMF_SUSPENDED, &md->flags);
2532}
2533
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002534int dm_noflush_suspending(struct dm_target *ti)
2535{
2536 struct mapped_device *md = dm_table_get_md(ti->table);
2537 int r = __noflush_suspending(md);
2538
2539 dm_put(md);
2540
2541 return r;
2542}
2543EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545static struct block_device_operations dm_blk_dops = {
2546 .open = dm_blk_open,
2547 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002548 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002549 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 .owner = THIS_MODULE
2551};
2552
2553EXPORT_SYMBOL(dm_get_mapinfo);
2554
2555/*
2556 * module hooks
2557 */
2558module_init(dm_init);
2559module_exit(dm_exit);
2560
2561module_param(major, uint, 0);
2562MODULE_PARM_DESC(major, "The major number of the device mapper");
2563MODULE_DESCRIPTION(DM_NAME " driver");
2564MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2565MODULE_LICENSE("GPL");