blob: f913b507fc81269d60eb16b8e7e1a267a80d005d [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"
9#include "dm-bio-list.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +010010#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <linux/init.h>
13#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080014#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/moduleparam.h>
16#include <linux/blkpg.h>
17#include <linux/bio.h>
18#include <linux/buffer_head.h>
19#include <linux/mempool.h>
20#include <linux/slab.h>
21#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080022#include <linux/hdreg.h>
Jens Axboe2056a782006-03-23 20:00:26 +010023#include <linux/blktrace_api.h>
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010024#include <trace/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "core"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static const char *_name = DM_NAME;
29
30static unsigned int major = 0;
31static unsigned int _major = 0;
32
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070033static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000035 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * One of these is allocated per bio.
37 */
38struct dm_io {
39 struct mapped_device *md;
40 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010042 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080043 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000047 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * One of these is allocated per target within a bio. Hopefully
49 * this will be simplified out one day.
50 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010051struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct dm_io *io;
53 struct dm_target *ti;
54 union map_info info;
55};
56
Ingo Molnar0bfc2452008-11-26 11:59:56 +010057DEFINE_TRACE(block_bio_complete);
58
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000059/*
60 * For request-based dm.
61 * One of these is allocated per request.
62 */
63struct dm_rq_target_io {
64 struct mapped_device *md;
65 struct dm_target *ti;
66 struct request *orig, clone;
67 int error;
68 union map_info info;
69};
70
71/*
72 * For request-based dm.
73 * One of these is allocated per bio.
74 */
75struct dm_rq_clone_bio_info {
76 struct bio *orig;
77 struct request *rq;
78};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080union map_info *dm_get_mapinfo(struct bio *bio)
81{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070082 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010083 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070084 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070087#define MINOR_ALLOCED ((void *)-1)
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Bits for the md->flags field.
91 */
92#define DMF_BLOCK_IO 0
93#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080094#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -070095#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070096#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080097#define DMF_NOFLUSH_SUSPENDING 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Milan Broz304f3f62008-02-08 02:11:17 +000099/*
100 * Work processed by per-device workqueue.
101 */
102struct dm_wq_req {
Milan Broz304f3f62008-02-08 02:11:17 +0000103 struct work_struct work;
104 struct mapped_device *md;
Milan Broz304f3f62008-02-08 02:11:17 +0000105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700108 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000109 struct mutex suspend_lock;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800110 spinlock_t pushback_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 rwlock_t map_lock;
112 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700113 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 unsigned long flags;
116
Jens Axboe165125e2007-07-24 09:28:11 +0200117 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800119 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 void *interface_ptr;
122
123 /*
124 * A list of ios that arrived while we were suspended.
125 */
126 atomic_t pending;
127 wait_queue_head_t wait;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800128 struct bio_list deferred;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800129 struct bio_list pushback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000132 * Processing queue (flush/barriers)
133 */
134 struct workqueue_struct *wq;
135
136 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * The current mapping.
138 */
139 struct dm_table *map;
140
141 /*
142 * io objects are allocated from here.
143 */
144 mempool_t *io_pool;
145 mempool_t *tio_pool;
146
Stefan Bader9faf4002006-10-03 01:15:41 -0700147 struct bio_set *bs;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
150 * Event handling.
151 */
152 atomic_t event_nr;
153 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100154 atomic_t uevent_seq;
155 struct list_head uevent_list;
156 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /*
159 * freeze/thaw support require holding onto a super block
160 */
161 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800162 struct block_device *suspended_bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800163
164 /* forced geometry settings */
165 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000166
167 /* sysfs handle */
168 struct kobject kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
171#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800172static struct kmem_cache *_io_cache;
173static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000174static struct kmem_cache *_rq_tio_cache;
175static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int __init local_init(void)
178{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100179 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100182 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100184 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100187 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100188 if (!_tio_cache)
189 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000191 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
192 if (!_rq_tio_cache)
193 goto out_free_tio_cache;
194
195 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
196 if (!_rq_bio_info_cache)
197 goto out_free_rq_tio_cache;
198
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100199 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100200 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000201 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 _major = major;
204 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100205 if (r < 0)
206 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (!_major)
209 _major = r;
210
211 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100212
213out_uevent_exit:
214 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000215out_free_rq_bio_info_cache:
216 kmem_cache_destroy(_rq_bio_info_cache);
217out_free_rq_tio_cache:
218 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100219out_free_tio_cache:
220 kmem_cache_destroy(_tio_cache);
221out_free_io_cache:
222 kmem_cache_destroy(_io_cache);
223
224 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227static void local_exit(void)
228{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000229 kmem_cache_destroy(_rq_bio_info_cache);
230 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 kmem_cache_destroy(_tio_cache);
232 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700233 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100234 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 _major = 0;
237
238 DMINFO("cleaned up");
239}
240
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000241static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 local_init,
243 dm_target_init,
244 dm_linear_init,
245 dm_stripe_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100246 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dm_interface_init,
248};
249
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000250static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 local_exit,
252 dm_target_exit,
253 dm_linear_exit,
254 dm_stripe_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100255 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 dm_interface_exit,
257};
258
259static int __init dm_init(void)
260{
261 const int count = ARRAY_SIZE(_inits);
262
263 int r, i;
264
265 for (i = 0; i < count; i++) {
266 r = _inits[i]();
267 if (r)
268 goto bad;
269 }
270
271 return 0;
272
273 bad:
274 while (i--)
275 _exits[i]();
276
277 return r;
278}
279
280static void __exit dm_exit(void)
281{
282 int i = ARRAY_SIZE(_exits);
283
284 while (i--)
285 _exits[i]();
286}
287
288/*
289 * Block device functions
290 */
Al Virofe5f9f22008-03-02 10:29:31 -0500291static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct mapped_device *md;
294
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700295 spin_lock(&_minor_lock);
296
Al Virofe5f9f22008-03-02 10:29:31 -0500297 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700298 if (!md)
299 goto out;
300
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700301 if (test_bit(DMF_FREEING, &md->flags) ||
302 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700303 md = NULL;
304 goto out;
305 }
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700308 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700309
310out:
311 spin_unlock(&_minor_lock);
312
313 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Al Virofe5f9f22008-03-02 10:29:31 -0500316static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Al Virofe5f9f22008-03-02 10:29:31 -0500318 struct mapped_device *md = disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700319 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 dm_put(md);
321 return 0;
322}
323
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700324int dm_open_count(struct mapped_device *md)
325{
326 return atomic_read(&md->open_count);
327}
328
329/*
330 * Guarantees nothing is using the device before it's deleted.
331 */
332int dm_lock_for_deletion(struct mapped_device *md)
333{
334 int r = 0;
335
336 spin_lock(&_minor_lock);
337
338 if (dm_open_count(md))
339 r = -EBUSY;
340 else
341 set_bit(DMF_DELETING, &md->flags);
342
343 spin_unlock(&_minor_lock);
344
345 return r;
346}
347
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800348static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
349{
350 struct mapped_device *md = bdev->bd_disk->private_data;
351
352 return dm_get_geometry(md, geo);
353}
354
Al Virofe5f9f22008-03-02 10:29:31 -0500355static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700356 unsigned int cmd, unsigned long arg)
357{
Al Virofe5f9f22008-03-02 10:29:31 -0500358 struct mapped_device *md = bdev->bd_disk->private_data;
359 struct dm_table *map = dm_get_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700360 struct dm_target *tgt;
361 int r = -ENOTTY;
362
Milan Brozaa129a22006-10-03 01:15:15 -0700363 if (!map || !dm_table_get_size(map))
364 goto out;
365
366 /* We only support devices that have a single target */
367 if (dm_table_get_num_targets(map) != 1)
368 goto out;
369
370 tgt = dm_table_get_target(map, 0);
371
372 if (dm_suspended(md)) {
373 r = -EAGAIN;
374 goto out;
375 }
376
377 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400378 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700379
380out:
381 dm_table_put(map);
382
Milan Brozaa129a22006-10-03 01:15:15 -0700383 return r;
384}
385
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100386static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 return mempool_alloc(md->io_pool, GFP_NOIO);
389}
390
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100391static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 mempool_free(io, md->io_pool);
394}
395
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100396static struct dm_target_io *alloc_tio(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 return mempool_alloc(md->tio_pool, GFP_NOIO);
399}
400
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100401static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 mempool_free(tio, md->tio_pool);
404}
405
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800406static void start_io_acct(struct dm_io *io)
407{
408 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900409 int cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800410
411 io->start_time = jiffies;
412
Tejun Heo074a7ac2008-08-25 19:56:14 +0900413 cpu = part_stat_lock();
414 part_round_stats(cpu, &dm_disk(md)->part0);
415 part_stat_unlock();
416 dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800417}
418
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000419static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800420{
421 struct mapped_device *md = io->md;
422 struct bio *bio = io->bio;
423 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900424 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800425 int rw = bio_data_dir(bio);
426
Tejun Heo074a7ac2008-08-25 19:56:14 +0900427 cpu = part_stat_lock();
428 part_round_stats(cpu, &dm_disk(md)->part0);
429 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
430 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800431
Tejun Heo074a7ac2008-08-25 19:56:14 +0900432 dm_disk(md)->part0.in_flight = pending =
433 atomic_dec_return(&md->pending);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800434
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000435 /* nudge anyone waiting on suspend queue */
436 if (!pending)
437 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
441 * Add the bio to the list of deferred io.
442 */
443static int queue_io(struct mapped_device *md, struct bio *bio)
444{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700445 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700448 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 1;
450 }
451
452 bio_list_add(&md->deferred, bio);
453
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700454 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0; /* deferred successfully */
456}
457
458/*
459 * Everyone (including functions in this file), should use this
460 * function to access the md->map field, and make sure they call
461 * dm_table_put() when finished.
462 */
463struct dm_table *dm_get_table(struct mapped_device *md)
464{
465 struct dm_table *t;
466
467 read_lock(&md->map_lock);
468 t = md->map;
469 if (t)
470 dm_table_get(t);
471 read_unlock(&md->map_lock);
472
473 return t;
474}
475
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800476/*
477 * Get the geometry associated with a dm device
478 */
479int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
480{
481 *geo = md->geometry;
482
483 return 0;
484}
485
486/*
487 * Set the geometry of a device.
488 */
489int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
490{
491 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
492
493 if (geo->start > sz) {
494 DMWARN("Start sector is beyond the geometry limits.");
495 return -EINVAL;
496 }
497
498 md->geometry = *geo;
499
500 return 0;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*-----------------------------------------------------------------
504 * CRUD START:
505 * A more elegant soln is in the works that uses the queue
506 * merge fn, unfortunately there are a couple of changes to
507 * the block layer that I want to make for this. So in the
508 * interests of getting something for people to use I give
509 * you this clearly demarcated crap.
510 *---------------------------------------------------------------*/
511
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800512static int __noflush_suspending(struct mapped_device *md)
513{
514 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/*
518 * Decrements the number of outstanding ios that a bio has been
519 * cloned into, completing the original io if necc.
520 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800521static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800523 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000524 int io_error;
525 struct bio *bio;
526 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800527
528 /* Push-back supersedes any I/O errors */
Milan Brozb35f8ca2009-03-16 17:44:36 +0000529 if (error && !(io->error > 0 && __noflush_suspending(md)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 io->error = error;
531
532 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800533 if (io->error == DM_ENDIO_REQUEUE) {
534 /*
535 * Target requested pushing back the I/O.
536 * This must be handled before the sleeper on
537 * suspend queue merges the pushback list.
538 */
Milan Brozb35f8ca2009-03-16 17:44:36 +0000539 spin_lock_irqsave(&md->pushback_lock, flags);
540 if (__noflush_suspending(md))
541 bio_list_add(&md->pushback, io->bio);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800542 else
543 /* noflush suspend was interrupted. */
544 io->error = -EIO;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000545 spin_unlock_irqrestore(&md->pushback_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800546 }
547
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000548 end_io_acct(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Milan Brozb35f8ca2009-03-16 17:44:36 +0000550 io_error = io->error;
551 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100552
Milan Brozb35f8ca2009-03-16 17:44:36 +0000553 free_io(md, io);
554
555 if (io_error != DM_ENDIO_REQUEUE) {
556 trace_block_bio_complete(md->queue, bio);
557
558 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561}
562
NeilBrown6712ecf2007-09-27 12:47:43 +0200563static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100566 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000567 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700568 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 dm_endio_fn endio = tio->ti->type->end_io;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
572 error = -EIO;
573
574 if (endio) {
575 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800576 if (r < 0 || r == DM_ENDIO_REQUEUE)
577 /*
578 * error and requeue request are handled
579 * in dec_pending().
580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800582 else if (r == DM_ENDIO_INCOMPLETE)
583 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200584 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800585 else if (r) {
586 DMWARN("unimplemented target endio return value: %d", r);
587 BUG();
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Stefan Bader9faf4002006-10-03 01:15:41 -0700591 /*
592 * Store md for cleanup instead of tio which is about to get freed.
593 */
594 bio->bi_private = md->bs;
595
Stefan Bader9faf4002006-10-03 01:15:41 -0700596 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000597 bio_put(bio);
598 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601static sector_t max_io_len(struct mapped_device *md,
602 sector_t sector, struct dm_target *ti)
603{
604 sector_t offset = sector - ti->begin;
605 sector_t len = ti->len - offset;
606
607 /*
608 * Does the target need to split even further ?
609 */
610 if (ti->split_io) {
611 sector_t boundary;
612 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
613 - offset;
614 if (len > boundary)
615 len = boundary;
616 }
617
618 return len;
619}
620
621static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100622 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100625 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700626 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /*
629 * Sanity checks.
630 */
631 BUG_ON(!clone->bi_size);
632
633 clone->bi_end_io = clone_endio;
634 clone->bi_private = tio;
635
636 /*
637 * Map the clone. If r == 0 we don't need to do
638 * anything, the target has assumed ownership of
639 * this io.
640 */
641 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100642 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800644 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100646
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100647 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunellec7149d62007-08-07 15:30:23 +0200648 tio->io->bio->bi_bdev->bd_dev,
649 clone->bi_sector, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800652 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
653 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700654 md = tio->io->md;
655 dec_pending(tio->io, r);
656 /*
657 * Store bio_set for cleanup.
658 */
659 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700661 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800662 } else if (r) {
663 DMWARN("unimplemented target map return value: %d", r);
664 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666}
667
668struct clone_info {
669 struct mapped_device *md;
670 struct dm_table *map;
671 struct bio *bio;
672 struct dm_io *io;
673 sector_t sector;
674 sector_t sector_count;
675 unsigned short idx;
676};
677
Peter Osterlund36763472005-09-06 15:16:42 -0700678static void dm_bio_destructor(struct bio *bio)
679{
Stefan Bader9faf4002006-10-03 01:15:41 -0700680 struct bio_set *bs = bio->bi_private;
681
682 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/*
686 * Creates a little bio that is just does part of a bvec.
687 */
688static struct bio *split_bvec(struct bio *bio, sector_t sector,
689 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -0700690 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct bio *clone;
693 struct bio_vec *bv = bio->bi_io_vec + idx;
694
Stefan Bader9faf4002006-10-03 01:15:41 -0700695 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700696 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *clone->bi_io_vec = *bv;
698
699 clone->bi_sector = sector;
700 clone->bi_bdev = bio->bi_bdev;
701 clone->bi_rw = bio->bi_rw;
702 clone->bi_vcnt = 1;
703 clone->bi_size = to_bytes(len);
704 clone->bi_io_vec->bv_offset = offset;
705 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +0100706 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 return clone;
709}
710
711/*
712 * Creates a bio that consists of range of complete bvecs.
713 */
714static struct bio *clone_bio(struct bio *bio, sector_t sector,
715 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -0700716 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct bio *clone;
719
Stefan Bader9faf4002006-10-03 01:15:41 -0700720 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
721 __bio_clone(clone, bio);
722 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 clone->bi_sector = sector;
724 clone->bi_idx = idx;
725 clone->bi_vcnt = idx + bv_count;
726 clone->bi_size = to_bytes(len);
727 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
728
729 return clone;
730}
731
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000732static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000735 struct dm_target *ti;
736 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100737 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000739 ti = dm_table_find_target(ci->map, ci->sector);
740 if (!dm_target_is_valid(ti))
741 return -EIO;
742
743 max = max_io_len(ci->md, ci->sector, ti);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
746 * Allocate a target io object.
747 */
748 tio = alloc_tio(ci->md);
749 tio->io = ci->io;
750 tio->ti = ti;
751 memset(&tio->info, 0, sizeof(tio->info));
752
753 if (ci->sector_count <= max) {
754 /*
755 * Optimise for the simple case where we can do all of
756 * the remaining io with a single clone.
757 */
758 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700759 bio->bi_vcnt - ci->idx, ci->sector_count,
760 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 __map_bio(ti, clone, tio);
762 ci->sector_count = 0;
763
764 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
765 /*
766 * There are some bvecs that don't span targets.
767 * Do as many of these as possible.
768 */
769 int i;
770 sector_t remaining = max;
771 sector_t bv_len;
772
773 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
774 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
775
776 if (bv_len > remaining)
777 break;
778
779 remaining -= bv_len;
780 len += bv_len;
781 }
782
Stefan Bader9faf4002006-10-03 01:15:41 -0700783 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
784 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 __map_bio(ti, clone, tio);
786
787 ci->sector += len;
788 ci->sector_count -= len;
789 ci->idx = i;
790
791 } else {
792 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800793 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
795 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800796 sector_t remaining = to_sector(bv->bv_len);
797 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800799 do {
800 if (offset) {
801 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000802 if (!dm_target_is_valid(ti))
803 return -EIO;
804
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800805 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800807 tio = alloc_tio(ci->md);
808 tio->io = ci->io;
809 tio->ti = ti;
810 memset(&tio->info, 0, sizeof(tio->info));
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800813 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800815 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700816 bv->bv_offset + offset, len,
817 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800818
819 __map_bio(ti, clone, tio);
820
821 ci->sector += len;
822 ci->sector_count -= len;
823 offset += to_bytes(len);
824 } while (remaining -= len);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ci->idx++;
827 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000828
829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*
833 * Split the bio into several clones.
834 */
Milan Broz9e4e5f82007-10-19 22:38:53 +0100835static int __split_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000838 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 ci.map = dm_get_table(md);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100841 if (unlikely(!ci.map))
842 return -EIO;
Andi Kleenab4c142482009-01-06 03:05:09 +0000843 if (unlikely(bio_barrier(bio) && !dm_table_barrier_ok(ci.map))) {
844 dm_table_put(ci.map);
845 bio_endio(bio, -EOPNOTSUPP);
846 return 0;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ci.md = md;
849 ci.bio = bio;
850 ci.io = alloc_io(md);
851 ci.io->error = 0;
852 atomic_set(&ci.io->io_count, 1);
853 ci.io->bio = bio;
854 ci.io->md = md;
855 ci.sector = bio->bi_sector;
856 ci.sector_count = bio_sectors(bio);
857 ci.idx = bio->bi_idx;
858
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800859 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000860 while (ci.sector_count && !error)
861 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000864 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 dm_table_put(ci.map);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100866
867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869/*-----------------------------------------------------------------
870 * CRUD END
871 *---------------------------------------------------------------*/
872
Milan Brozf6fccb12008-07-21 12:00:37 +0100873static int dm_merge_bvec(struct request_queue *q,
874 struct bvec_merge_data *bvm,
875 struct bio_vec *biovec)
876{
877 struct mapped_device *md = q->queuedata;
878 struct dm_table *map = dm_get_table(md);
879 struct dm_target *ti;
880 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +0100881 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +0100882
883 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +0100884 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +0100885
886 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +0100887 if (!dm_target_is_valid(ti))
888 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +0100889
890 /*
891 * Find maximum amount of I/O that won't need splitting
892 */
893 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
894 (sector_t) BIO_MAX_SECTORS);
895 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
896 if (max_size < 0)
897 max_size = 0;
898
899 /*
900 * merge_bvec_fn() returns number of bytes
901 * it can accept at this offset
902 * max is precomputed maximal io size
903 */
904 if (max_size && ti->type->merge)
905 max_size = ti->type->merge(ti, bvm, biovec, max_size);
906
Mikulas Patockab01cd5a2008-10-01 14:39:24 +0100907out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +0100908 dm_table_put(map);
909
910out:
Milan Brozf6fccb12008-07-21 12:00:37 +0100911 /*
912 * Always allow an entire first page
913 */
914 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
915 max_size = biovec->bv_len;
916
Milan Brozf6fccb12008-07-21 12:00:37 +0100917 return max_size;
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * The request function that just remaps the bio built up by
922 * dm_merge_bvec.
923 */
Jens Axboe165125e2007-07-24 09:28:11 +0200924static int dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Milan Broz9e4e5f82007-10-19 22:38:53 +0100926 int r = -EIO;
Kevin Corry12f03a42006-02-01 03:04:52 -0800927 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +0900929 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700931 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Tejun Heo074a7ac2008-08-25 19:56:14 +0900933 cpu = part_stat_lock();
934 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
935 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
936 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -0800937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
939 * If we're suspended we have to queue
940 * this io for later.
941 */
942 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700943 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Milan Broz9e4e5f82007-10-19 22:38:53 +0100945 if (bio_rw(bio) != READA)
946 r = queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Milan Broz9e4e5f82007-10-19 22:38:53 +0100948 if (r <= 0)
949 goto out_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /*
952 * We're in a while loop, because someone could suspend
953 * before we get to the following read lock.
954 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700955 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
Milan Broz9e4e5f82007-10-19 22:38:53 +0100958 r = __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700959 up_read(&md->io_lock);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100960
961out_req:
962 if (r < 0)
963 bio_io_error(bio);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 0;
966}
967
Jens Axboe165125e2007-07-24 09:28:11 +0200968static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct mapped_device *md = q->queuedata;
971 struct dm_table *map = dm_get_table(md);
972
973 if (map) {
974 dm_table_unplug_all(map);
975 dm_table_put(map);
976 }
977}
978
979static int dm_any_congested(void *congested_data, int bdi_bits)
980{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +0000981 int r = bdi_bits;
982 struct mapped_device *md = congested_data;
983 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +0000985 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
986 map = dm_get_table(md);
987 if (map) {
988 r = dm_table_any_congested(map, bdi_bits);
989 dm_table_put(map);
990 }
991 }
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return r;
994}
995
996/*-----------------------------------------------------------------
997 * An IDR is used to keep track of allocated minor numbers.
998 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999static DEFINE_IDR(_minor_idr);
1000
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001001static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001003 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001005 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008/*
1009 * See if the device with a specific minor # is free.
1010 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001011static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 int r, m;
1014
1015 if (minor >= (1 << MINORBITS))
1016 return -EINVAL;
1017
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001018 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1019 if (!r)
1020 return -ENOMEM;
1021
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001022 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (idr_find(&_minor_idr, minor)) {
1025 r = -EBUSY;
1026 goto out;
1027 }
1028
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001029 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001030 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 if (m != minor) {
1034 idr_remove(&_minor_idr, m);
1035 r = -EBUSY;
1036 goto out;
1037 }
1038
1039out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001040 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return r;
1042}
1043
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001044static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001046 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001049 if (!r)
1050 return -ENOMEM;
1051
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001052 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001054 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001055 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (m >= (1 << MINORBITS)) {
1059 idr_remove(&_minor_idr, m);
1060 r = -ENOSPC;
1061 goto out;
1062 }
1063
1064 *minor = m;
1065
1066out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001067 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return r;
1069}
1070
1071static struct block_device_operations dm_blk_dops;
1072
1073/*
1074 * Allocate and initialise a blank device with a given minor.
1075 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001076static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001079 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001080 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (!md) {
1083 DMWARN("unable to allocate device, out of memory.");
1084 return NULL;
1085 }
1086
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001087 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001088 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001091 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001092 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001093 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001094 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001096 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001098 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001099 mutex_init(&md->suspend_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001100 spin_lock_init(&md->pushback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 rwlock_init(&md->map_lock);
1102 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001103 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001105 atomic_set(&md->uevent_seq, 0);
1106 INIT_LIST_HEAD(&md->uevent_list);
1107 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 md->queue = blk_alloc_queue(GFP_KERNEL);
1110 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001111 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 md->queue->queuedata = md;
1114 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1115 md->queue->backing_dev_info.congested_data = md;
1116 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +01001117 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001119 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Matthew Dobson93d23412006-03-26 01:37:50 -08001121 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
Kiyoshi Ueda74859362006-12-08 02:41:02 -08001122 if (!md->io_pool)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001123 goto bad_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Matthew Dobson93d23412006-03-26 01:37:50 -08001125 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!md->tio_pool)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001127 goto bad_tio_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Jens Axboebb799ca2008-12-10 15:35:05 +01001129 md->bs = bioset_create(16, 0);
Stefan Bader9faf4002006-10-03 01:15:41 -07001130 if (!md->bs)
1131 goto bad_no_bioset;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 md->disk = alloc_disk(1);
1134 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001135 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001137 atomic_set(&md->pending, 0);
1138 init_waitqueue_head(&md->wait);
1139 init_waitqueue_head(&md->eventq);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 md->disk->major = _major;
1142 md->disk->first_minor = minor;
1143 md->disk->fops = &dm_blk_dops;
1144 md->disk->queue = md->queue;
1145 md->disk->private_data = md;
1146 sprintf(md->disk->disk_name, "dm-%d", minor);
1147 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001148 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Milan Broz304f3f62008-02-08 02:11:17 +00001150 md->wq = create_singlethread_workqueue("kdmflush");
1151 if (!md->wq)
1152 goto bad_thread;
1153
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001154 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001155 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001156 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001157 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001158
1159 BUG_ON(old_md != MINOR_ALLOCED);
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return md;
1162
Milan Broz304f3f62008-02-08 02:11:17 +00001163bad_thread:
1164 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001165bad_disk:
Stefan Bader9faf4002006-10-03 01:15:41 -07001166 bioset_free(md->bs);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001167bad_no_bioset:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 mempool_destroy(md->tio_pool);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001169bad_tio_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 mempool_destroy(md->io_pool);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001171bad_io_pool:
Al Viro1312f402006-03-12 11:02:03 -05001172 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001173bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001175bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001176 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001177bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 kfree(md);
1179 return NULL;
1180}
1181
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001182static void unlock_fs(struct mapped_device *md);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184static void free_dev(struct mapped_device *md)
1185{
Tejun Heof331c022008-09-03 09:01:48 +02001186 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001187
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001188 if (md->suspended_bdev) {
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001189 unlock_fs(md);
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001190 bdput(md->suspended_bdev);
1191 }
Milan Broz304f3f62008-02-08 02:11:17 +00001192 destroy_workqueue(md->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 mempool_destroy(md->tio_pool);
1194 mempool_destroy(md->io_pool);
Stefan Bader9faf4002006-10-03 01:15:41 -07001195 bioset_free(md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001197 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001198
1199 spin_lock(&_minor_lock);
1200 md->disk->private_data = NULL;
1201 spin_unlock(&_minor_lock);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001204 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001205 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 kfree(md);
1207}
1208
1209/*
1210 * Bind a table to the device.
1211 */
1212static void event_callback(void *context)
1213{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001214 unsigned long flags;
1215 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct mapped_device *md = (struct mapped_device *) context;
1217
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001218 spin_lock_irqsave(&md->uevent_lock, flags);
1219 list_splice_init(&md->uevent_list, &uevents);
1220 spin_unlock_irqrestore(&md->uevent_lock, flags);
1221
Tejun Heoed9e1982008-08-25 19:56:05 +09001222 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 atomic_inc(&md->event_nr);
1225 wake_up(&md->eventq);
1226}
1227
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001228static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001230 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001232 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001233 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001234 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237static int __bind(struct mapped_device *md, struct dm_table *t)
1238{
Jens Axboe165125e2007-07-24 09:28:11 +02001239 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 sector_t size;
1241
1242 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001243
1244 /*
1245 * Wipe any geometry if the size of the table changed.
1246 */
1247 if (size != get_capacity(md->disk))
1248 memset(&md->geometry, 0, sizeof(md->geometry));
1249
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001250 if (md->suspended_bdev)
1251 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Mikulas Patockad5816872009-01-06 03:05:10 +00001253 if (!size) {
1254 dm_table_destroy(t);
1255 return 0;
1256 }
1257
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001258 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001259
1260 write_lock(&md->map_lock);
1261 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001263 write_unlock(&md->map_lock);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 0;
1266}
1267
1268static void __unbind(struct mapped_device *md)
1269{
1270 struct dm_table *map = md->map;
1271
1272 if (!map)
1273 return;
1274
1275 dm_table_event_callback(map, NULL, NULL);
1276 write_lock(&md->map_lock);
1277 md->map = NULL;
1278 write_unlock(&md->map_lock);
Mikulas Patockad5816872009-01-06 03:05:10 +00001279 dm_table_destroy(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282/*
1283 * Constructor for a new device.
1284 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001285int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct mapped_device *md;
1288
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001289 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (!md)
1291 return -ENXIO;
1292
Milan Broz784aae72009-01-06 03:05:12 +00001293 dm_sysfs_init(md);
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 *result = md;
1296 return 0;
1297}
1298
David Teigland637842c2006-01-06 00:20:00 -08001299static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 unsigned minor = MINOR(dev);
1303
1304 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1305 return NULL;
1306
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001307 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001310 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02001311 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07001312 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08001313 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001314 goto out;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001317out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001318 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
David Teigland637842c2006-01-06 00:20:00 -08001320 return md;
1321}
1322
David Teiglandd229a952006-01-06 00:20:01 -08001323struct mapped_device *dm_get_md(dev_t dev)
1324{
1325 struct mapped_device *md = dm_find_md(dev);
1326
1327 if (md)
1328 dm_get(md);
1329
1330 return md;
1331}
1332
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001333void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001334{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001335 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338void dm_set_mdptr(struct mapped_device *md, void *ptr)
1339{
1340 md->interface_ptr = ptr;
1341}
1342
1343void dm_get(struct mapped_device *md)
1344{
1345 atomic_inc(&md->holders);
1346}
1347
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001348const char *dm_device_name(struct mapped_device *md)
1349{
1350 return md->name;
1351}
1352EXPORT_SYMBOL_GPL(dm_device_name);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354void dm_put(struct mapped_device *md)
1355{
Mike Anderson1134e5a2006-03-27 01:17:54 -08001356 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001358 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1359
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001360 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08001361 map = dm_get_table(md);
Tejun Heof331c022008-09-03 09:01:48 +02001362 idr_replace(&_minor_idr, MINOR_ALLOCED,
1363 MINOR(disk_devt(dm_disk(md))));
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001364 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001365 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001366 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 dm_table_presuspend_targets(map);
1368 dm_table_postsuspend_targets(map);
1369 }
Milan Broz784aae72009-01-06 03:05:12 +00001370 dm_sysfs_exit(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001371 dm_table_put(map);
Mikulas Patockaa1b51e92009-01-06 03:04:53 +00001372 __unbind(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 free_dev(md);
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
Edward Goggin79eb8852007-05-09 02:32:56 -07001376EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Milan Broz46125c12008-02-08 02:10:30 +00001378static int dm_wait_for_completion(struct mapped_device *md)
1379{
1380 int r = 0;
1381
1382 while (1) {
1383 set_current_state(TASK_INTERRUPTIBLE);
1384
1385 smp_mb();
1386 if (!atomic_read(&md->pending))
1387 break;
1388
1389 if (signal_pending(current)) {
1390 r = -EINTR;
1391 break;
1392 }
1393
1394 io_schedule();
1395 }
1396 set_current_state(TASK_RUNNING);
1397
1398 return r;
1399}
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401/*
1402 * Process the deferred bios
1403 */
Milan Broz6d6f10d2008-02-08 02:10:22 +00001404static void __flush_deferred_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Milan Broz6d6f10d2008-02-08 02:10:22 +00001406 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Milan Broz6d6f10d2008-02-08 02:10:22 +00001408 while ((c = bio_list_pop(&md->deferred))) {
Milan Broz9e4e5f82007-10-19 22:38:53 +01001409 if (__split_bio(md, c))
1410 bio_io_error(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Milan Broz73d410c2008-02-08 02:10:25 +00001412
1413 clear_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Milan Broz6d6f10d2008-02-08 02:10:22 +00001416static void __merge_pushback_list(struct mapped_device *md)
1417{
1418 unsigned long flags;
1419
1420 spin_lock_irqsave(&md->pushback_lock, flags);
1421 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1422 bio_list_merge_head(&md->deferred, &md->pushback);
1423 bio_list_init(&md->pushback);
1424 spin_unlock_irqrestore(&md->pushback_lock, flags);
1425}
1426
Milan Broz304f3f62008-02-08 02:11:17 +00001427static void dm_wq_work(struct work_struct *work)
1428{
1429 struct dm_wq_req *req = container_of(work, struct dm_wq_req, work);
1430 struct mapped_device *md = req->md;
1431
1432 down_write(&md->io_lock);
Mikulas Patocka14377392009-04-02 19:55:36 +01001433 __flush_deferred_io(md);
Milan Broz304f3f62008-02-08 02:11:17 +00001434 up_write(&md->io_lock);
1435}
1436
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01001437static void dm_wq_queue(struct mapped_device *md, struct dm_wq_req *req)
Milan Broz304f3f62008-02-08 02:11:17 +00001438{
Milan Broz304f3f62008-02-08 02:11:17 +00001439 req->md = md;
Milan Broz304f3f62008-02-08 02:11:17 +00001440 INIT_WORK(&req->work, dm_wq_work);
1441 queue_work(md->wq, &req->work);
1442}
1443
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01001444static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00001445{
1446 struct dm_wq_req req;
1447
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01001448 dm_wq_queue(md, &req);
Milan Broz304f3f62008-02-08 02:11:17 +00001449 flush_workqueue(md->wq);
1450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452/*
1453 * Swap in a new table (destroying old one).
1454 */
1455int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1456{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001457 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Daniel Walkere61290a2008-02-08 02:10:08 +00001459 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001462 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001465 /* without bdev, the device size cannot be changed */
1466 if (!md->suspended_bdev)
1467 if (get_capacity(md->disk) != dm_table_get_size(table))
1468 goto out;
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 __unbind(md);
1471 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001473out:
Daniel Walkere61290a2008-02-08 02:10:08 +00001474 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001475 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478/*
1479 * Functions to lock and unlock any filesystem running on the
1480 * device.
1481 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001482static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001484 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001487
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001488 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001489 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001490 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001491 md->frozen_sb = NULL;
1492 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001493 }
1494
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001495 set_bit(DMF_FROZEN, &md->flags);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001498 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
1500 return 0;
1501}
1502
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001503static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001505 if (!test_bit(DMF_FROZEN, &md->flags))
1506 return;
1507
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001508 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001510 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513/*
1514 * We need to be able to change a mapping table under a mounted
1515 * filesystem. For example we might want to move some data in
1516 * the background. Before the table can be swapped with
1517 * dm_bind_table, dm_suspend must be called to flush any in
1518 * flight bios and ensure that any further io gets deferred.
1519 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001520int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001522 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 DECLARE_WAITQUEUE(wait, current);
Milan Broz46125c12008-02-08 02:10:30 +00001524 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001525 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001526 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Daniel Walkere61290a2008-02-08 02:10:08 +00001528 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001529
Milan Broz73d410c2008-02-08 02:10:25 +00001530 if (dm_suspended(md)) {
1531 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08001532 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001537 /*
1538 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1539 * This flag is cleared before dm_suspend returns.
1540 */
1541 if (noflush)
1542 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1543
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001544 /* This does not get reverted if there's an error later. */
1545 dm_table_presuspend_targets(map);
1546
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001547 /* bdget() can stall if the pending I/Os are not flushed */
1548 if (!noflush) {
1549 md->suspended_bdev = bdget_disk(md->disk, 0);
1550 if (!md->suspended_bdev) {
1551 DMWARN("bdget failed in dm_suspend");
1552 r = -ENOMEM;
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01001553 goto out;
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001554 }
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001555
Milan Broz6d6f10d2008-02-08 02:10:22 +00001556 /*
1557 * Flush I/O to the device. noflush supersedes do_lockfs,
1558 * because lock_fs() needs to flush I/Os.
1559 */
1560 if (do_lockfs) {
1561 r = lock_fs(md);
1562 if (r)
1563 goto out;
1564 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001568 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001570 down_write(&md->io_lock);
1571 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001574 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001577 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /*
Milan Broz46125c12008-02-08 02:10:30 +00001581 * Wait for the already-mapped ios to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
Milan Broz46125c12008-02-08 02:10:30 +00001583 r = dm_wait_for_completion(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001585 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 remove_wait_queue(&md->wait, &wait);
1587
Milan Broz6d6f10d2008-02-08 02:10:22 +00001588 if (noflush)
1589 __merge_pushback_list(md);
Milan Broz94d63512008-02-08 02:10:27 +00001590 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00001593 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01001594 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00001595
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001596 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001597 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001598 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001599
1600 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 set_bit(DMF_SUSPENDED, &md->flags);
1603
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001604out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001605 if (r && md->suspended_bdev) {
1606 bdput(md->suspended_bdev);
1607 md->suspended_bdev = NULL;
1608 }
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08001611
1612out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00001613 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001614 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617int dm_resume(struct mapped_device *md)
1618{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001619 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001620 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Daniel Walkere61290a2008-02-08 02:10:08 +00001622 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001623 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001624 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001625
1626 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001627 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Milan Broz8757b772006-10-03 01:15:36 -07001630 r = dm_table_resume_targets(map);
1631 if (r)
1632 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001633
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01001634 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001635
1636 unlock_fs(md);
1637
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001638 if (md->suspended_bdev) {
1639 bdput(md->suspended_bdev);
1640 md->suspended_bdev = NULL;
1641 }
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001642
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001643 clear_bit(DMF_SUSPENDED, &md->flags);
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Alasdair G Kergon69267a32007-12-13 14:15:57 +00001647 dm_kobject_uevent(md);
Hannes Reinecke8560ed62006-10-03 01:15:35 -07001648
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001649 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001650
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001651out:
1652 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00001653 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001654
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001655 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658/*-----------------------------------------------------------------
1659 * Event notification.
1660 *---------------------------------------------------------------*/
Alasdair G Kergon69267a32007-12-13 14:15:57 +00001661void dm_kobject_uevent(struct mapped_device *md)
1662{
Tejun Heoed9e1982008-08-25 19:56:05 +09001663 kobject_uevent(&disk_to_dev(md->disk)->kobj, KOBJ_CHANGE);
Alasdair G Kergon69267a32007-12-13 14:15:57 +00001664}
1665
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001666uint32_t dm_next_uevent_seq(struct mapped_device *md)
1667{
1668 return atomic_add_return(1, &md->uevent_seq);
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671uint32_t dm_get_event_nr(struct mapped_device *md)
1672{
1673 return atomic_read(&md->event_nr);
1674}
1675
1676int dm_wait_event(struct mapped_device *md, int event_nr)
1677{
1678 return wait_event_interruptible(md->eventq,
1679 (event_nr != atomic_read(&md->event_nr)));
1680}
1681
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001682void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
1683{
1684 unsigned long flags;
1685
1686 spin_lock_irqsave(&md->uevent_lock, flags);
1687 list_add(elist, &md->uevent_list);
1688 spin_unlock_irqrestore(&md->uevent_lock, flags);
1689}
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691/*
1692 * The gendisk is only valid as long as you have a reference
1693 * count on 'md'.
1694 */
1695struct gendisk *dm_disk(struct mapped_device *md)
1696{
1697 return md->disk;
1698}
1699
Milan Broz784aae72009-01-06 03:05:12 +00001700struct kobject *dm_kobject(struct mapped_device *md)
1701{
1702 return &md->kobj;
1703}
1704
1705/*
1706 * struct mapped_device should not be exported outside of dm.c
1707 * so use this check to verify that kobj is part of md structure
1708 */
1709struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
1710{
1711 struct mapped_device *md;
1712
1713 md = container_of(kobj, struct mapped_device, kobj);
1714 if (&md->kobj != kobj)
1715 return NULL;
1716
1717 dm_get(md);
1718 return md;
1719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721int dm_suspended(struct mapped_device *md)
1722{
1723 return test_bit(DMF_SUSPENDED, &md->flags);
1724}
1725
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001726int dm_noflush_suspending(struct dm_target *ti)
1727{
1728 struct mapped_device *md = dm_table_get_md(ti->table);
1729 int r = __noflush_suspending(md);
1730
1731 dm_put(md);
1732
1733 return r;
1734}
1735EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737static struct block_device_operations dm_blk_dops = {
1738 .open = dm_blk_open,
1739 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07001740 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001741 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 .owner = THIS_MODULE
1743};
1744
1745EXPORT_SYMBOL(dm_get_mapinfo);
1746
1747/*
1748 * module hooks
1749 */
1750module_init(dm_init);
1751module_exit(dm_exit);
1752
1753module_param(major, uint, 0);
1754MODULE_PARM_DESC(major, "The major number of the device mapper");
1755MODULE_DESCRIPTION(DM_NAME " driver");
1756MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1757MODULE_LICENSE("GPL");