blob: 11f422ecfda000a6ee35ead97dbf8d4c0d433e08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07003 * Copyright (C) 2004-2006 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>
Milan Brozaa129a22006-10-03 01:15:15 -070024#include <linux/smp_lock.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/*
35 * One of these is allocated per bio.
36 */
37struct dm_io {
38 struct mapped_device *md;
39 int error;
40 struct bio *bio;
41 atomic_t io_count;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080042 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
45/*
46 * One of these is allocated per target within a bio. Hopefully
47 * this will be simplified out one day.
48 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010049struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct dm_io *io;
51 struct dm_target *ti;
52 union map_info info;
53};
54
55union map_info *dm_get_mapinfo(struct bio *bio)
56{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070057 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010058 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070059 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070062#define MINOR_ALLOCED ((void *)-1)
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * Bits for the md->flags field.
66 */
67#define DMF_BLOCK_IO 0
68#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080069#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -070070#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070071#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080072#define DMF_NOFLUSH_SUSPENDING 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -070075 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +000076 struct mutex suspend_lock;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080077 spinlock_t pushback_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 rwlock_t map_lock;
79 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070080 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 unsigned long flags;
83
Jens Axboe165125e2007-07-24 09:28:11 +020084 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -080086 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 void *interface_ptr;
89
90 /*
91 * A list of ios that arrived while we were suspended.
92 */
93 atomic_t pending;
94 wait_queue_head_t wait;
Kiyoshi Ueda74859362006-12-08 02:41:02 -080095 struct bio_list deferred;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080096 struct bio_list pushback;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /*
99 * The current mapping.
100 */
101 struct dm_table *map;
102
103 /*
104 * io objects are allocated from here.
105 */
106 mempool_t *io_pool;
107 mempool_t *tio_pool;
108
Stefan Bader9faf4002006-10-03 01:15:41 -0700109 struct bio_set *bs;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /*
112 * Event handling.
113 */
114 atomic_t event_nr;
115 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100116 atomic_t uevent_seq;
117 struct list_head uevent_list;
118 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /*
121 * freeze/thaw support require holding onto a super block
122 */
123 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800124 struct block_device *suspended_bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800125
126 /* forced geometry settings */
127 struct hd_geometry geometry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800131static struct kmem_cache *_io_cache;
132static struct kmem_cache *_tio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int __init local_init(void)
135{
136 int r;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100139 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!_io_cache)
141 return -ENOMEM;
142
143 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100144 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!_tio_cache) {
146 kmem_cache_destroy(_io_cache);
147 return -ENOMEM;
148 }
149
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100150 r = dm_uevent_init();
151 if (r) {
152 kmem_cache_destroy(_tio_cache);
153 kmem_cache_destroy(_io_cache);
154 return r;
155 }
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 _major = major;
158 r = register_blkdev(_major, _name);
159 if (r < 0) {
160 kmem_cache_destroy(_tio_cache);
161 kmem_cache_destroy(_io_cache);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100162 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return r;
164 }
165
166 if (!_major)
167 _major = r;
168
169 return 0;
170}
171
172static void local_exit(void)
173{
174 kmem_cache_destroy(_tio_cache);
175 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700176 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100177 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 _major = 0;
180
181 DMINFO("cleaned up");
182}
183
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000184static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 local_init,
186 dm_target_init,
187 dm_linear_init,
188 dm_stripe_init,
189 dm_interface_init,
190};
191
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000192static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 local_exit,
194 dm_target_exit,
195 dm_linear_exit,
196 dm_stripe_exit,
197 dm_interface_exit,
198};
199
200static int __init dm_init(void)
201{
202 const int count = ARRAY_SIZE(_inits);
203
204 int r, i;
205
206 for (i = 0; i < count; i++) {
207 r = _inits[i]();
208 if (r)
209 goto bad;
210 }
211
212 return 0;
213
214 bad:
215 while (i--)
216 _exits[i]();
217
218 return r;
219}
220
221static void __exit dm_exit(void)
222{
223 int i = ARRAY_SIZE(_exits);
224
225 while (i--)
226 _exits[i]();
227}
228
229/*
230 * Block device functions
231 */
232static int dm_blk_open(struct inode *inode, struct file *file)
233{
234 struct mapped_device *md;
235
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700236 spin_lock(&_minor_lock);
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 md = inode->i_bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700239 if (!md)
240 goto out;
241
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700242 if (test_bit(DMF_FREEING, &md->flags) ||
243 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700244 md = NULL;
245 goto out;
246 }
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700249 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700250
251out:
252 spin_unlock(&_minor_lock);
253
254 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257static int dm_blk_close(struct inode *inode, struct file *file)
258{
259 struct mapped_device *md;
260
261 md = inode->i_bdev->bd_disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700262 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 dm_put(md);
264 return 0;
265}
266
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700267int dm_open_count(struct mapped_device *md)
268{
269 return atomic_read(&md->open_count);
270}
271
272/*
273 * Guarantees nothing is using the device before it's deleted.
274 */
275int dm_lock_for_deletion(struct mapped_device *md)
276{
277 int r = 0;
278
279 spin_lock(&_minor_lock);
280
281 if (dm_open_count(md))
282 r = -EBUSY;
283 else
284 set_bit(DMF_DELETING, &md->flags);
285
286 spin_unlock(&_minor_lock);
287
288 return r;
289}
290
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800291static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
292{
293 struct mapped_device *md = bdev->bd_disk->private_data;
294
295 return dm_get_geometry(md, geo);
296}
297
Milan Brozaa129a22006-10-03 01:15:15 -0700298static int dm_blk_ioctl(struct inode *inode, struct file *file,
299 unsigned int cmd, unsigned long arg)
300{
301 struct mapped_device *md;
302 struct dm_table *map;
303 struct dm_target *tgt;
304 int r = -ENOTTY;
305
306 /* We don't really need this lock, but we do need 'inode'. */
307 unlock_kernel();
308
309 md = inode->i_bdev->bd_disk->private_data;
310
311 map = dm_get_table(md);
312
313 if (!map || !dm_table_get_size(map))
314 goto out;
315
316 /* We only support devices that have a single target */
317 if (dm_table_get_num_targets(map) != 1)
318 goto out;
319
320 tgt = dm_table_get_target(map, 0);
321
322 if (dm_suspended(md)) {
323 r = -EAGAIN;
324 goto out;
325 }
326
327 if (tgt->type->ioctl)
328 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
329
330out:
331 dm_table_put(map);
332
333 lock_kernel();
334 return r;
335}
336
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100337static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 return mempool_alloc(md->io_pool, GFP_NOIO);
340}
341
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100342static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 mempool_free(io, md->io_pool);
345}
346
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100347static struct dm_target_io *alloc_tio(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 return mempool_alloc(md->tio_pool, GFP_NOIO);
350}
351
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100352static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 mempool_free(tio, md->tio_pool);
355}
356
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800357static void start_io_acct(struct dm_io *io)
358{
359 struct mapped_device *md = io->md;
360
361 io->start_time = jiffies;
362
363 preempt_disable();
364 disk_round_stats(dm_disk(md));
365 preempt_enable();
366 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
367}
368
369static int end_io_acct(struct dm_io *io)
370{
371 struct mapped_device *md = io->md;
372 struct bio *bio = io->bio;
373 unsigned long duration = jiffies - io->start_time;
374 int pending;
375 int rw = bio_data_dir(bio);
376
377 preempt_disable();
378 disk_round_stats(dm_disk(md));
379 preempt_enable();
380 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
381
382 disk_stat_add(dm_disk(md), ticks[rw], duration);
383
384 return !pending;
385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * Add the bio to the list of deferred io.
389 */
390static int queue_io(struct mapped_device *md, struct bio *bio)
391{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700392 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700395 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 1;
397 }
398
399 bio_list_add(&md->deferred, bio);
400
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700401 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0; /* deferred successfully */
403}
404
405/*
406 * Everyone (including functions in this file), should use this
407 * function to access the md->map field, and make sure they call
408 * dm_table_put() when finished.
409 */
410struct dm_table *dm_get_table(struct mapped_device *md)
411{
412 struct dm_table *t;
413
414 read_lock(&md->map_lock);
415 t = md->map;
416 if (t)
417 dm_table_get(t);
418 read_unlock(&md->map_lock);
419
420 return t;
421}
422
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800423/*
424 * Get the geometry associated with a dm device
425 */
426int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
427{
428 *geo = md->geometry;
429
430 return 0;
431}
432
433/*
434 * Set the geometry of a device.
435 */
436int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
437{
438 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
439
440 if (geo->start > sz) {
441 DMWARN("Start sector is beyond the geometry limits.");
442 return -EINVAL;
443 }
444
445 md->geometry = *geo;
446
447 return 0;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/*-----------------------------------------------------------------
451 * CRUD START:
452 * A more elegant soln is in the works that uses the queue
453 * merge fn, unfortunately there are a couple of changes to
454 * the block layer that I want to make for this. So in the
455 * interests of getting something for people to use I give
456 * you this clearly demarcated crap.
457 *---------------------------------------------------------------*/
458
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800459static int __noflush_suspending(struct mapped_device *md)
460{
461 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
465 * Decrements the number of outstanding ios that a bio has been
466 * cloned into, completing the original io if necc.
467 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800468static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800470 unsigned long flags;
471
472 /* Push-back supersedes any I/O errors */
473 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 io->error = error;
475
476 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800477 if (io->error == DM_ENDIO_REQUEUE) {
478 /*
479 * Target requested pushing back the I/O.
480 * This must be handled before the sleeper on
481 * suspend queue merges the pushback list.
482 */
483 spin_lock_irqsave(&io->md->pushback_lock, flags);
484 if (__noflush_suspending(io->md))
485 bio_list_add(&io->md->pushback, io->bio);
486 else
487 /* noflush suspend was interrupted. */
488 io->error = -EIO;
489 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
490 }
491
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800492 if (end_io_acct(io))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* nudge anyone waiting on suspend queue */
494 wake_up(&io->md->wait);
495
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800496 if (io->error != DM_ENDIO_REQUEUE) {
497 blk_add_trace_bio(io->md->queue, io->bio,
498 BLK_TA_COMPLETE);
Jens Axboe2056a782006-03-23 20:00:26 +0100499
NeilBrown6712ecf2007-09-27 12:47:43 +0200500 bio_endio(io->bio, io->error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800501 }
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 free_io(io->md, io);
504 }
505}
506
NeilBrown6712ecf2007-09-27 12:47:43 +0200507static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100510 struct dm_target_io *tio = bio->bi_private;
Stefan Bader9faf4002006-10-03 01:15:41 -0700511 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dm_endio_fn endio = tio->ti->type->end_io;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
515 error = -EIO;
516
517 if (endio) {
518 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800519 if (r < 0 || r == DM_ENDIO_REQUEUE)
520 /*
521 * error and requeue request are handled
522 * in dec_pending().
523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800525 else if (r == DM_ENDIO_INCOMPLETE)
526 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200527 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800528 else if (r) {
529 DMWARN("unimplemented target endio return value: %d", r);
530 BUG();
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
Stefan Bader9faf4002006-10-03 01:15:41 -0700534 dec_pending(tio->io, error);
535
536 /*
537 * Store md for cleanup instead of tio which is about to get freed.
538 */
539 bio->bi_private = md->bs;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 bio_put(bio);
Stefan Bader9faf4002006-10-03 01:15:41 -0700542 free_tio(md, tio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static sector_t max_io_len(struct mapped_device *md,
546 sector_t sector, struct dm_target *ti)
547{
548 sector_t offset = sector - ti->begin;
549 sector_t len = ti->len - offset;
550
551 /*
552 * Does the target need to split even further ?
553 */
554 if (ti->split_io) {
555 sector_t boundary;
556 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
557 - offset;
558 if (len > boundary)
559 len = boundary;
560 }
561
562 return len;
563}
564
565static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100566 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100569 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700570 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /*
573 * Sanity checks.
574 */
575 BUG_ON(!clone->bi_size);
576
577 clone->bi_end_io = clone_endio;
578 clone->bi_private = tio;
579
580 /*
581 * Map the clone. If r == 0 we don't need to do
582 * anything, the target has assumed ownership of
583 * this io.
584 */
585 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100586 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800588 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100590
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700591 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunellec7149d62007-08-07 15:30:23 +0200592 tio->io->bio->bi_bdev->bd_dev,
593 clone->bi_sector, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800596 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
597 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700598 md = tio->io->md;
599 dec_pending(tio->io, r);
600 /*
601 * Store bio_set for cleanup.
602 */
603 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700605 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800606 } else if (r) {
607 DMWARN("unimplemented target map return value: %d", r);
608 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610}
611
612struct clone_info {
613 struct mapped_device *md;
614 struct dm_table *map;
615 struct bio *bio;
616 struct dm_io *io;
617 sector_t sector;
618 sector_t sector_count;
619 unsigned short idx;
620};
621
Peter Osterlund36763472005-09-06 15:16:42 -0700622static void dm_bio_destructor(struct bio *bio)
623{
Stefan Bader9faf4002006-10-03 01:15:41 -0700624 struct bio_set *bs = bio->bi_private;
625
626 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
630 * Creates a little bio that is just does part of a bvec.
631 */
632static struct bio *split_bvec(struct bio *bio, sector_t sector,
633 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -0700634 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct bio *clone;
637 struct bio_vec *bv = bio->bi_io_vec + idx;
638
Stefan Bader9faf4002006-10-03 01:15:41 -0700639 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700640 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 *clone->bi_io_vec = *bv;
642
643 clone->bi_sector = sector;
644 clone->bi_bdev = bio->bi_bdev;
645 clone->bi_rw = bio->bi_rw;
646 clone->bi_vcnt = 1;
647 clone->bi_size = to_bytes(len);
648 clone->bi_io_vec->bv_offset = offset;
649 clone->bi_io_vec->bv_len = clone->bi_size;
650
651 return clone;
652}
653
654/*
655 * Creates a bio that consists of range of complete bvecs.
656 */
657static struct bio *clone_bio(struct bio *bio, sector_t sector,
658 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -0700659 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct bio *clone;
662
Stefan Bader9faf4002006-10-03 01:15:41 -0700663 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
664 __bio_clone(clone, bio);
665 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 clone->bi_sector = sector;
667 clone->bi_idx = idx;
668 clone->bi_vcnt = idx + bv_count;
669 clone->bi_size = to_bytes(len);
670 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
671
672 return clone;
673}
674
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000675static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000678 struct dm_target *ti;
679 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100680 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000682 ti = dm_table_find_target(ci->map, ci->sector);
683 if (!dm_target_is_valid(ti))
684 return -EIO;
685
686 max = max_io_len(ci->md, ci->sector, ti);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * Allocate a target io object.
690 */
691 tio = alloc_tio(ci->md);
692 tio->io = ci->io;
693 tio->ti = ti;
694 memset(&tio->info, 0, sizeof(tio->info));
695
696 if (ci->sector_count <= max) {
697 /*
698 * Optimise for the simple case where we can do all of
699 * the remaining io with a single clone.
700 */
701 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700702 bio->bi_vcnt - ci->idx, ci->sector_count,
703 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 __map_bio(ti, clone, tio);
705 ci->sector_count = 0;
706
707 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
708 /*
709 * There are some bvecs that don't span targets.
710 * Do as many of these as possible.
711 */
712 int i;
713 sector_t remaining = max;
714 sector_t bv_len;
715
716 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
717 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
718
719 if (bv_len > remaining)
720 break;
721
722 remaining -= bv_len;
723 len += bv_len;
724 }
725
Stefan Bader9faf4002006-10-03 01:15:41 -0700726 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
727 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 __map_bio(ti, clone, tio);
729
730 ci->sector += len;
731 ci->sector_count -= len;
732 ci->idx = i;
733
734 } else {
735 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800736 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
738 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800739 sector_t remaining = to_sector(bv->bv_len);
740 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800742 do {
743 if (offset) {
744 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000745 if (!dm_target_is_valid(ti))
746 return -EIO;
747
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800748 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800750 tio = alloc_tio(ci->md);
751 tio->io = ci->io;
752 tio->ti = ti;
753 memset(&tio->info, 0, sizeof(tio->info));
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800756 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800758 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700759 bv->bv_offset + offset, len,
760 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800761
762 __map_bio(ti, clone, tio);
763
764 ci->sector += len;
765 ci->sector_count -= len;
766 offset += to_bytes(len);
767 } while (remaining -= len);
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ci->idx++;
770 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000771
772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/*
776 * Split the bio into several clones.
777 */
Milan Broz9e4e5f82007-10-19 22:38:53 +0100778static int __split_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000781 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 ci.map = dm_get_table(md);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100784 if (unlikely(!ci.map))
785 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 ci.md = md;
788 ci.bio = bio;
789 ci.io = alloc_io(md);
790 ci.io->error = 0;
791 atomic_set(&ci.io->io_count, 1);
792 ci.io->bio = bio;
793 ci.io->md = md;
794 ci.sector = bio->bi_sector;
795 ci.sector_count = bio_sectors(bio);
796 ci.idx = bio->bi_idx;
797
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800798 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000799 while (ci.sector_count && !error)
800 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +0000803 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 dm_table_put(ci.map);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100805
806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808/*-----------------------------------------------------------------
809 * CRUD END
810 *---------------------------------------------------------------*/
811
812/*
813 * The request function that just remaps the bio built up by
814 * dm_merge_bvec.
815 */
Jens Axboe165125e2007-07-24 09:28:11 +0200816static int dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Milan Broz9e4e5f82007-10-19 22:38:53 +0100818 int r = -EIO;
Kevin Corry12f03a42006-02-01 03:04:52 -0800819 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct mapped_device *md = q->queuedata;
821
Stefan Bader07a83c42007-07-12 17:28:33 +0100822 /*
823 * There is no use in forwarding any barrier request since we can't
824 * guarantee it is (or can be) handled by the targets correctly.
825 */
826 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200827 bio_endio(bio, -EOPNOTSUPP);
Stefan Bader07a83c42007-07-12 17:28:33 +0100828 return 0;
829 }
830
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700831 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Kevin Corry12f03a42006-02-01 03:04:52 -0800833 disk_stat_inc(dm_disk(md), ios[rw]);
834 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /*
837 * If we're suspended we have to queue
838 * this io for later.
839 */
840 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700841 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Milan Broz9e4e5f82007-10-19 22:38:53 +0100843 if (bio_rw(bio) != READA)
844 r = queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Milan Broz9e4e5f82007-10-19 22:38:53 +0100846 if (r <= 0)
847 goto out_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /*
850 * We're in a while loop, because someone could suspend
851 * before we get to the following read lock.
852 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700853 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Milan Broz9e4e5f82007-10-19 22:38:53 +0100856 r = __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700857 up_read(&md->io_lock);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100858
859out_req:
860 if (r < 0)
861 bio_io_error(bio);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
864}
865
Jens Axboe165125e2007-07-24 09:28:11 +0200866static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct mapped_device *md = q->queuedata;
869 struct dm_table *map = dm_get_table(md);
870
871 if (map) {
872 dm_table_unplug_all(map);
873 dm_table_put(map);
874 }
875}
876
877static int dm_any_congested(void *congested_data, int bdi_bits)
878{
879 int r;
880 struct mapped_device *md = (struct mapped_device *) congested_data;
881 struct dm_table *map = dm_get_table(md);
882
883 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
884 r = bdi_bits;
885 else
886 r = dm_table_any_congested(map, bdi_bits);
887
888 dm_table_put(map);
889 return r;
890}
891
892/*-----------------------------------------------------------------
893 * An IDR is used to keep track of allocated minor numbers.
894 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895static DEFINE_IDR(_minor_idr);
896
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700897static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700899 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700901 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904/*
905 * See if the device with a specific minor # is free.
906 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700907static int specific_minor(struct mapped_device *md, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 int r, m;
910
911 if (minor >= (1 << MINORBITS))
912 return -EINVAL;
913
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700914 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
915 if (!r)
916 return -ENOMEM;
917
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700918 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (idr_find(&_minor_idr, minor)) {
921 r = -EBUSY;
922 goto out;
923 }
924
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700925 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700926 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (m != minor) {
930 idr_remove(&_minor_idr, m);
931 r = -EBUSY;
932 goto out;
933 }
934
935out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700936 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return r;
938}
939
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700940static int next_free_minor(struct mapped_device *md, int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700942 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700945 if (!r)
946 return -ENOMEM;
947
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700948 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700950 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (r) {
952 goto out;
953 }
954
955 if (m >= (1 << MINORBITS)) {
956 idr_remove(&_minor_idr, m);
957 r = -ENOSPC;
958 goto out;
959 }
960
961 *minor = m;
962
963out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700964 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return r;
966}
967
968static struct block_device_operations dm_blk_dops;
969
970/*
971 * Allocate and initialise a blank device with a given minor.
972 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700973static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 int r;
976 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700977 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (!md) {
980 DMWARN("unable to allocate device, out of memory.");
981 return NULL;
982 }
983
Jeff Mahoney10da4f72006-06-26 00:27:25 -0700984 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +0000985 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -0700986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700988 if (minor == DM_ANY_MINOR)
989 r = next_free_minor(md, &minor);
990 else
991 r = specific_minor(md, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +0000993 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 memset(md, 0, sizeof(*md));
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700996 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +0000997 mutex_init(&md->suspend_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800998 spin_lock_init(&md->pushback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 rwlock_init(&md->map_lock);
1000 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001001 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001003 atomic_set(&md->uevent_seq, 0);
1004 INIT_LIST_HEAD(&md->uevent_list);
1005 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 md->queue = blk_alloc_queue(GFP_KERNEL);
1008 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001009 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 md->queue->queuedata = md;
1012 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1013 md->queue->backing_dev_info.congested_data = md;
1014 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +01001015 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 md->queue->unplug_fn = dm_unplug_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Matthew Dobson93d23412006-03-26 01:37:50 -08001018 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
Kiyoshi Ueda74859362006-12-08 02:41:02 -08001019 if (!md->io_pool)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001020 goto bad_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Matthew Dobson93d23412006-03-26 01:37:50 -08001022 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (!md->tio_pool)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001024 goto bad_tio_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Jens Axboe59725112007-04-02 10:06:42 +02001026 md->bs = bioset_create(16, 16);
Stefan Bader9faf4002006-10-03 01:15:41 -07001027 if (!md->bs)
1028 goto bad_no_bioset;
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 md->disk = alloc_disk(1);
1031 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001032 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001034 atomic_set(&md->pending, 0);
1035 init_waitqueue_head(&md->wait);
1036 init_waitqueue_head(&md->eventq);
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 md->disk->major = _major;
1039 md->disk->first_minor = minor;
1040 md->disk->fops = &dm_blk_dops;
1041 md->disk->queue = md->queue;
1042 md->disk->private_data = md;
1043 sprintf(md->disk->disk_name, "dm-%d", minor);
1044 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001045 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001047 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001048 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001049 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001050 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001051
1052 BUG_ON(old_md != MINOR_ALLOCED);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return md;
1055
Milan Broz6ed7ade2008-02-08 02:10:19 +00001056bad_disk:
Stefan Bader9faf4002006-10-03 01:15:41 -07001057 bioset_free(md->bs);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001058bad_no_bioset:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 mempool_destroy(md->tio_pool);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001060bad_tio_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 mempool_destroy(md->io_pool);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001062bad_io_pool:
Al Viro1312f402006-03-12 11:02:03 -05001063 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001064bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001066bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001067 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001068bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 kfree(md);
1070 return NULL;
1071}
1072
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001073static void unlock_fs(struct mapped_device *md);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075static void free_dev(struct mapped_device *md)
1076{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001077 int minor = md->disk->first_minor;
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001078
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001079 if (md->suspended_bdev) {
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001080 unlock_fs(md);
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001081 bdput(md->suspended_bdev);
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 mempool_destroy(md->tio_pool);
1084 mempool_destroy(md->io_pool);
Stefan Bader9faf4002006-10-03 01:15:41 -07001085 bioset_free(md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001087 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001088
1089 spin_lock(&_minor_lock);
1090 md->disk->private_data = NULL;
1091 spin_unlock(&_minor_lock);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001094 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001095 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree(md);
1097}
1098
1099/*
1100 * Bind a table to the device.
1101 */
1102static void event_callback(void *context)
1103{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001104 unsigned long flags;
1105 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct mapped_device *md = (struct mapped_device *) context;
1107
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001108 spin_lock_irqsave(&md->uevent_lock, flags);
1109 list_splice_init(&md->uevent_list, &uevents);
1110 spin_unlock_irqrestore(&md->uevent_lock, flags);
1111
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001112 dm_send_uevents(&uevents, &md->disk->dev.kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 atomic_inc(&md->event_nr);
1115 wake_up(&md->eventq);
1116}
1117
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001118static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001120 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001122 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001123 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001124 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127static int __bind(struct mapped_device *md, struct dm_table *t)
1128{
Jens Axboe165125e2007-07-24 09:28:11 +02001129 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sector_t size;
1131
1132 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001133
1134 /*
1135 * Wipe any geometry if the size of the table changed.
1136 */
1137 if (size != get_capacity(md->disk))
1138 memset(&md->geometry, 0, sizeof(md->geometry));
1139
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001140 if (md->suspended_bdev)
1141 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (size == 0)
1143 return 0;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 dm_table_get(t);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001146 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001147
1148 write_lock(&md->map_lock);
1149 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001151 write_unlock(&md->map_lock);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return 0;
1154}
1155
1156static void __unbind(struct mapped_device *md)
1157{
1158 struct dm_table *map = md->map;
1159
1160 if (!map)
1161 return;
1162
1163 dm_table_event_callback(map, NULL, NULL);
1164 write_lock(&md->map_lock);
1165 md->map = NULL;
1166 write_unlock(&md->map_lock);
1167 dm_table_put(map);
1168}
1169
1170/*
1171 * Constructor for a new device.
1172 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001173int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 struct mapped_device *md;
1176
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001177 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (!md)
1179 return -ENXIO;
1180
1181 *result = md;
1182 return 0;
1183}
1184
David Teigland637842c2006-01-06 00:20:00 -08001185static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 unsigned minor = MINOR(dev);
1189
1190 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1191 return NULL;
1192
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001193 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001196 if (md && (md == MINOR_ALLOCED ||
1197 (dm_disk(md)->first_minor != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07001198 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08001199 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001200 goto out;
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001203out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001204 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
David Teigland637842c2006-01-06 00:20:00 -08001206 return md;
1207}
1208
David Teiglandd229a952006-01-06 00:20:01 -08001209struct mapped_device *dm_get_md(dev_t dev)
1210{
1211 struct mapped_device *md = dm_find_md(dev);
1212
1213 if (md)
1214 dm_get(md);
1215
1216 return md;
1217}
1218
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001219void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001220{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001221 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224void dm_set_mdptr(struct mapped_device *md, void *ptr)
1225{
1226 md->interface_ptr = ptr;
1227}
1228
1229void dm_get(struct mapped_device *md)
1230{
1231 atomic_inc(&md->holders);
1232}
1233
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001234const char *dm_device_name(struct mapped_device *md)
1235{
1236 return md->name;
1237}
1238EXPORT_SYMBOL_GPL(dm_device_name);
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240void dm_put(struct mapped_device *md)
1241{
Mike Anderson1134e5a2006-03-27 01:17:54 -08001242 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001244 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1245
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001246 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08001247 map = dm_get_table(md);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001248 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001249 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001250 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001251 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 dm_table_presuspend_targets(map);
1253 dm_table_postsuspend_targets(map);
1254 }
1255 __unbind(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001256 dm_table_put(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 free_dev(md);
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
Edward Goggin79eb8852007-05-09 02:32:56 -07001260EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262/*
1263 * Process the deferred bios
1264 */
Milan Broz6d6f10d2008-02-08 02:10:22 +00001265static void __flush_deferred_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Milan Broz6d6f10d2008-02-08 02:10:22 +00001267 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Milan Broz6d6f10d2008-02-08 02:10:22 +00001269 while ((c = bio_list_pop(&md->deferred))) {
Milan Broz9e4e5f82007-10-19 22:38:53 +01001270 if (__split_bio(md, c))
1271 bio_io_error(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
Milan Broz73d410c2008-02-08 02:10:25 +00001273
1274 clear_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Milan Broz6d6f10d2008-02-08 02:10:22 +00001277static void __merge_pushback_list(struct mapped_device *md)
1278{
1279 unsigned long flags;
1280
1281 spin_lock_irqsave(&md->pushback_lock, flags);
1282 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1283 bio_list_merge_head(&md->deferred, &md->pushback);
1284 bio_list_init(&md->pushback);
1285 spin_unlock_irqrestore(&md->pushback_lock, flags);
1286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/*
1289 * Swap in a new table (destroying old one).
1290 */
1291int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1292{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001293 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Daniel Walkere61290a2008-02-08 02:10:08 +00001295 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001298 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001301 /* without bdev, the device size cannot be changed */
1302 if (!md->suspended_bdev)
1303 if (get_capacity(md->disk) != dm_table_get_size(table))
1304 goto out;
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 __unbind(md);
1307 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001309out:
Daniel Walkere61290a2008-02-08 02:10:08 +00001310 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001311 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314/*
1315 * Functions to lock and unlock any filesystem running on the
1316 * device.
1317 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001318static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001320 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001323
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001324 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001325 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001326 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001327 md->frozen_sb = NULL;
1328 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001329 }
1330
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001331 set_bit(DMF_FROZEN, &md->flags);
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001334 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
1336 return 0;
1337}
1338
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001339static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001341 if (!test_bit(DMF_FROZEN, &md->flags))
1342 return;
1343
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001344 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001346 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
1349/*
1350 * We need to be able to change a mapping table under a mounted
1351 * filesystem. For example we might want to move some data in
1352 * the background. Before the table can be swapped with
1353 * dm_bind_table, dm_suspend must be called to flush any in
1354 * flight bios and ensure that any further io gets deferred.
1355 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001356int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001358 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 DECLARE_WAITQUEUE(wait, current);
Milan Broz73d410c2008-02-08 02:10:25 +00001360 int pending, r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001361 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001362 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Daniel Walkere61290a2008-02-08 02:10:08 +00001364 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001365
Milan Broz73d410c2008-02-08 02:10:25 +00001366 if (dm_suspended(md)) {
1367 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08001368 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001373 /*
1374 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1375 * This flag is cleared before dm_suspend returns.
1376 */
1377 if (noflush)
1378 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1379
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001380 /* This does not get reverted if there's an error later. */
1381 dm_table_presuspend_targets(map);
1382
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001383 /* bdget() can stall if the pending I/Os are not flushed */
1384 if (!noflush) {
1385 md->suspended_bdev = bdget_disk(md->disk, 0);
1386 if (!md->suspended_bdev) {
1387 DMWARN("bdget failed in dm_suspend");
1388 r = -ENOMEM;
1389 goto flush_and_out;
1390 }
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001391
Milan Broz6d6f10d2008-02-08 02:10:22 +00001392 /*
1393 * Flush I/O to the device. noflush supersedes do_lockfs,
1394 * because lock_fs() needs to flush I/Os.
1395 */
1396 if (do_lockfs) {
1397 r = lock_fs(md);
1398 if (r)
1399 goto out;
1400 }
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001404 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001406 down_write(&md->io_lock);
1407 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001410 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001413 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /*
1417 * Then we wait for the already mapped ios to
1418 * complete.
1419 */
1420 while (1) {
1421 set_current_state(TASK_INTERRUPTIBLE);
1422
Milan Broz7e5c1e82008-02-08 02:09:49 +00001423 smp_mb();
Milan Broz73d410c2008-02-08 02:10:25 +00001424 pending = atomic_read(&md->pending);
1425 if (!pending || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427
1428 io_schedule();
1429 }
1430 set_current_state(TASK_RUNNING);
1431
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001432 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 remove_wait_queue(&md->wait, &wait);
1434
Milan Broz6d6f10d2008-02-08 02:10:22 +00001435 if (noflush)
1436 __merge_pushback_list(md);
Milan Broz94d63512008-02-08 02:10:27 +00001437 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /* were we interrupted ? */
Milan Broz73d410c2008-02-08 02:10:25 +00001440 if (pending) {
Milan Broz94d63512008-02-08 02:10:27 +00001441 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00001442 __flush_deferred_io(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001443 up_write(&md->io_lock);
Milan Broz73d410c2008-02-08 02:10:25 +00001444
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001445 unlock_fs(md);
Milan Broz73d410c2008-02-08 02:10:25 +00001446 r = -EINTR;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001447 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001448 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001449
1450 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 set_bit(DMF_SUSPENDED, &md->flags);
1453
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001454flush_and_out:
1455 if (r && noflush) {
1456 /*
1457 * Because there may be already I/Os in the pushback list,
1458 * flush them before return.
1459 */
1460 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00001461 __merge_pushback_list(md);
1462 __flush_deferred_io(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001463 up_write(&md->io_lock);
1464 }
1465
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001466out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001467 if (r && md->suspended_bdev) {
1468 bdput(md->suspended_bdev);
1469 md->suspended_bdev = NULL;
1470 }
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08001473
1474out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00001475 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001476 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479int dm_resume(struct mapped_device *md)
1480{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001481 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001482 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Daniel Walkere61290a2008-02-08 02:10:08 +00001484 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001485 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001486 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001487
1488 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001489 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001490 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Milan Broz8757b772006-10-03 01:15:36 -07001492 r = dm_table_resume_targets(map);
1493 if (r)
1494 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001495
1496 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00001497 __flush_deferred_io(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001498 up_write(&md->io_lock);
1499
1500 unlock_fs(md);
1501
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001502 if (md->suspended_bdev) {
1503 bdput(md->suspended_bdev);
1504 md->suspended_bdev = NULL;
1505 }
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001506
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001507 clear_bit(DMF_SUSPENDED, &md->flags);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Alasdair G Kergon69267a32007-12-13 14:15:57 +00001511 dm_kobject_uevent(md);
Hannes Reinecke8560ed62006-10-03 01:15:35 -07001512
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001513 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001514
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001515out:
1516 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00001517 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001518
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001519 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522/*-----------------------------------------------------------------
1523 * Event notification.
1524 *---------------------------------------------------------------*/
Alasdair G Kergon69267a32007-12-13 14:15:57 +00001525void dm_kobject_uevent(struct mapped_device *md)
1526{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001527 kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE);
Alasdair G Kergon69267a32007-12-13 14:15:57 +00001528}
1529
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001530uint32_t dm_next_uevent_seq(struct mapped_device *md)
1531{
1532 return atomic_add_return(1, &md->uevent_seq);
1533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535uint32_t dm_get_event_nr(struct mapped_device *md)
1536{
1537 return atomic_read(&md->event_nr);
1538}
1539
1540int dm_wait_event(struct mapped_device *md, int event_nr)
1541{
1542 return wait_event_interruptible(md->eventq,
1543 (event_nr != atomic_read(&md->event_nr)));
1544}
1545
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001546void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
1547{
1548 unsigned long flags;
1549
1550 spin_lock_irqsave(&md->uevent_lock, flags);
1551 list_add(elist, &md->uevent_list);
1552 spin_unlock_irqrestore(&md->uevent_lock, flags);
1553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * The gendisk is only valid as long as you have a reference
1557 * count on 'md'.
1558 */
1559struct gendisk *dm_disk(struct mapped_device *md)
1560{
1561 return md->disk;
1562}
1563
1564int dm_suspended(struct mapped_device *md)
1565{
1566 return test_bit(DMF_SUSPENDED, &md->flags);
1567}
1568
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001569int dm_noflush_suspending(struct dm_target *ti)
1570{
1571 struct mapped_device *md = dm_table_get_md(ti->table);
1572 int r = __noflush_suspending(md);
1573
1574 dm_put(md);
1575
1576 return r;
1577}
1578EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580static struct block_device_operations dm_blk_dops = {
1581 .open = dm_blk_open,
1582 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07001583 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001584 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 .owner = THIS_MODULE
1586};
1587
1588EXPORT_SYMBOL(dm_get_mapinfo);
1589
1590/*
1591 * module hooks
1592 */
1593module_init(dm_init);
1594module_exit(dm_exit);
1595
1596module_param(major, uint, 0);
1597MODULE_PARM_DESC(major, "The major number of the device mapper");
1598MODULE_DESCRIPTION(DM_NAME " driver");
1599MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1600MODULE_LICENSE("GPL");