blob: 8c16359f8b0189c5a77d6eb19106ceb21231c0ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
9#include "dm-bio-list.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/blkpg.h>
15#include <linux/bio.h>
16#include <linux/buffer_head.h>
17#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
20
21static const char *_name = DM_NAME;
22
23static unsigned int major = 0;
24static unsigned int _major = 0;
25
26/*
27 * One of these is allocated per bio.
28 */
29struct dm_io {
30 struct mapped_device *md;
31 int error;
32 struct bio *bio;
33 atomic_t io_count;
34};
35
36/*
37 * One of these is allocated per target within a bio. Hopefully
38 * this will be simplified out one day.
39 */
40struct target_io {
41 struct dm_io *io;
42 struct dm_target *ti;
43 union map_info info;
44};
45
46union map_info *dm_get_mapinfo(struct bio *bio)
47{
48 if (bio && bio->bi_private)
49 return &((struct target_io *)bio->bi_private)->info;
50 return NULL;
51}
52
53/*
54 * Bits for the md->flags field.
55 */
56#define DMF_BLOCK_IO 0
57#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080058#define DMF_FROZEN 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -070061 struct rw_semaphore io_lock;
62 struct semaphore suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 rwlock_t map_lock;
64 atomic_t holders;
65
66 unsigned long flags;
67
68 request_queue_t *queue;
69 struct gendisk *disk;
70
71 void *interface_ptr;
72
73 /*
74 * A list of ios that arrived while we were suspended.
75 */
76 atomic_t pending;
77 wait_queue_head_t wait;
78 struct bio_list deferred;
79
80 /*
81 * The current mapping.
82 */
83 struct dm_table *map;
84
85 /*
86 * io objects are allocated from here.
87 */
88 mempool_t *io_pool;
89 mempool_t *tio_pool;
90
91 /*
92 * Event handling.
93 */
94 atomic_t event_nr;
95 wait_queue_head_t eventq;
96
97 /*
98 * freeze/thaw support require holding onto a super block
99 */
100 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800101 struct block_device *suspended_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104#define MIN_IOS 256
105static kmem_cache_t *_io_cache;
106static kmem_cache_t *_tio_cache;
107
108static struct bio_set *dm_set;
109
110static int __init local_init(void)
111{
112 int r;
113
114 dm_set = bioset_create(16, 16, 4);
115 if (!dm_set)
116 return -ENOMEM;
117
118 /* allocate a slab for the dm_ios */
119 _io_cache = kmem_cache_create("dm_io",
120 sizeof(struct dm_io), 0, 0, NULL, NULL);
121 if (!_io_cache)
122 return -ENOMEM;
123
124 /* allocate a slab for the target ios */
125 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
126 0, 0, NULL, NULL);
127 if (!_tio_cache) {
128 kmem_cache_destroy(_io_cache);
129 return -ENOMEM;
130 }
131
132 _major = major;
133 r = register_blkdev(_major, _name);
134 if (r < 0) {
135 kmem_cache_destroy(_tio_cache);
136 kmem_cache_destroy(_io_cache);
137 return r;
138 }
139
140 if (!_major)
141 _major = r;
142
143 return 0;
144}
145
146static void local_exit(void)
147{
148 kmem_cache_destroy(_tio_cache);
149 kmem_cache_destroy(_io_cache);
150
151 bioset_free(dm_set);
152
153 if (unregister_blkdev(_major, _name) < 0)
154 DMERR("devfs_unregister_blkdev failed");
155
156 _major = 0;
157
158 DMINFO("cleaned up");
159}
160
161int (*_inits[])(void) __initdata = {
162 local_init,
163 dm_target_init,
164 dm_linear_init,
165 dm_stripe_init,
166 dm_interface_init,
167};
168
169void (*_exits[])(void) = {
170 local_exit,
171 dm_target_exit,
172 dm_linear_exit,
173 dm_stripe_exit,
174 dm_interface_exit,
175};
176
177static int __init dm_init(void)
178{
179 const int count = ARRAY_SIZE(_inits);
180
181 int r, i;
182
183 for (i = 0; i < count; i++) {
184 r = _inits[i]();
185 if (r)
186 goto bad;
187 }
188
189 return 0;
190
191 bad:
192 while (i--)
193 _exits[i]();
194
195 return r;
196}
197
198static void __exit dm_exit(void)
199{
200 int i = ARRAY_SIZE(_exits);
201
202 while (i--)
203 _exits[i]();
204}
205
206/*
207 * Block device functions
208 */
209static int dm_blk_open(struct inode *inode, struct file *file)
210{
211 struct mapped_device *md;
212
213 md = inode->i_bdev->bd_disk->private_data;
214 dm_get(md);
215 return 0;
216}
217
218static int dm_blk_close(struct inode *inode, struct file *file)
219{
220 struct mapped_device *md;
221
222 md = inode->i_bdev->bd_disk->private_data;
223 dm_put(md);
224 return 0;
225}
226
227static inline struct dm_io *alloc_io(struct mapped_device *md)
228{
229 return mempool_alloc(md->io_pool, GFP_NOIO);
230}
231
232static inline void free_io(struct mapped_device *md, struct dm_io *io)
233{
234 mempool_free(io, md->io_pool);
235}
236
237static inline struct target_io *alloc_tio(struct mapped_device *md)
238{
239 return mempool_alloc(md->tio_pool, GFP_NOIO);
240}
241
242static inline void free_tio(struct mapped_device *md, struct target_io *tio)
243{
244 mempool_free(tio, md->tio_pool);
245}
246
247/*
248 * Add the bio to the list of deferred io.
249 */
250static int queue_io(struct mapped_device *md, struct bio *bio)
251{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700252 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700255 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 1;
257 }
258
259 bio_list_add(&md->deferred, bio);
260
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700261 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0; /* deferred successfully */
263}
264
265/*
266 * Everyone (including functions in this file), should use this
267 * function to access the md->map field, and make sure they call
268 * dm_table_put() when finished.
269 */
270struct dm_table *dm_get_table(struct mapped_device *md)
271{
272 struct dm_table *t;
273
274 read_lock(&md->map_lock);
275 t = md->map;
276 if (t)
277 dm_table_get(t);
278 read_unlock(&md->map_lock);
279
280 return t;
281}
282
283/*-----------------------------------------------------------------
284 * CRUD START:
285 * A more elegant soln is in the works that uses the queue
286 * merge fn, unfortunately there are a couple of changes to
287 * the block layer that I want to make for this. So in the
288 * interests of getting something for people to use I give
289 * you this clearly demarcated crap.
290 *---------------------------------------------------------------*/
291
292/*
293 * Decrements the number of outstanding ios that a bio has been
294 * cloned into, completing the original io if necc.
295 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800296static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 if (error)
299 io->error = error;
300
301 if (atomic_dec_and_test(&io->io_count)) {
302 if (atomic_dec_and_test(&io->md->pending))
303 /* nudge anyone waiting on suspend queue */
304 wake_up(&io->md->wait);
305
306 bio_endio(io->bio, io->bio->bi_size, io->error);
307 free_io(io->md, io);
308 }
309}
310
311static int clone_endio(struct bio *bio, unsigned int done, int error)
312{
313 int r = 0;
314 struct target_io *tio = bio->bi_private;
315 struct dm_io *io = tio->io;
316 dm_endio_fn endio = tio->ti->type->end_io;
317
318 if (bio->bi_size)
319 return 1;
320
321 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
322 error = -EIO;
323
324 if (endio) {
325 r = endio(tio->ti, bio, error, &tio->info);
326 if (r < 0)
327 error = r;
328
329 else if (r > 0)
330 /* the target wants another shot at the io */
331 return 1;
332 }
333
334 free_tio(io->md, tio);
335 dec_pending(io, error);
336 bio_put(bio);
337 return r;
338}
339
340static sector_t max_io_len(struct mapped_device *md,
341 sector_t sector, struct dm_target *ti)
342{
343 sector_t offset = sector - ti->begin;
344 sector_t len = ti->len - offset;
345
346 /*
347 * Does the target need to split even further ?
348 */
349 if (ti->split_io) {
350 sector_t boundary;
351 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
352 - offset;
353 if (len > boundary)
354 len = boundary;
355 }
356
357 return len;
358}
359
360static void __map_bio(struct dm_target *ti, struct bio *clone,
361 struct target_io *tio)
362{
363 int r;
364
365 /*
366 * Sanity checks.
367 */
368 BUG_ON(!clone->bi_size);
369
370 clone->bi_end_io = clone_endio;
371 clone->bi_private = tio;
372
373 /*
374 * Map the clone. If r == 0 we don't need to do
375 * anything, the target has assumed ownership of
376 * this io.
377 */
378 atomic_inc(&tio->io->io_count);
379 r = ti->type->map(ti, clone, &tio->info);
380 if (r > 0)
381 /* the bio has been remapped so dispatch it */
382 generic_make_request(clone);
383
384 else if (r < 0) {
385 /* error the io and bail out */
386 struct dm_io *io = tio->io;
387 free_tio(tio->io->md, tio);
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700388 dec_pending(io, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 bio_put(clone);
390 }
391}
392
393struct clone_info {
394 struct mapped_device *md;
395 struct dm_table *map;
396 struct bio *bio;
397 struct dm_io *io;
398 sector_t sector;
399 sector_t sector_count;
400 unsigned short idx;
401};
402
Peter Osterlund36763472005-09-06 15:16:42 -0700403static void dm_bio_destructor(struct bio *bio)
404{
405 bio_free(bio, dm_set);
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
409 * Creates a little bio that is just does part of a bvec.
410 */
411static struct bio *split_bvec(struct bio *bio, sector_t sector,
412 unsigned short idx, unsigned int offset,
413 unsigned int len)
414{
415 struct bio *clone;
416 struct bio_vec *bv = bio->bi_io_vec + idx;
417
418 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
Peter Osterlund36763472005-09-06 15:16:42 -0700419 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *clone->bi_io_vec = *bv;
421
422 clone->bi_sector = sector;
423 clone->bi_bdev = bio->bi_bdev;
424 clone->bi_rw = bio->bi_rw;
425 clone->bi_vcnt = 1;
426 clone->bi_size = to_bytes(len);
427 clone->bi_io_vec->bv_offset = offset;
428 clone->bi_io_vec->bv_len = clone->bi_size;
429
430 return clone;
431}
432
433/*
434 * Creates a bio that consists of range of complete bvecs.
435 */
436static struct bio *clone_bio(struct bio *bio, sector_t sector,
437 unsigned short idx, unsigned short bv_count,
438 unsigned int len)
439{
440 struct bio *clone;
441
442 clone = bio_clone(bio, GFP_NOIO);
443 clone->bi_sector = sector;
444 clone->bi_idx = idx;
445 clone->bi_vcnt = idx + bv_count;
446 clone->bi_size = to_bytes(len);
447 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
448
449 return clone;
450}
451
452static void __clone_and_map(struct clone_info *ci)
453{
454 struct bio *clone, *bio = ci->bio;
455 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
456 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
457 struct target_io *tio;
458
459 /*
460 * Allocate a target io object.
461 */
462 tio = alloc_tio(ci->md);
463 tio->io = ci->io;
464 tio->ti = ti;
465 memset(&tio->info, 0, sizeof(tio->info));
466
467 if (ci->sector_count <= max) {
468 /*
469 * Optimise for the simple case where we can do all of
470 * the remaining io with a single clone.
471 */
472 clone = clone_bio(bio, ci->sector, ci->idx,
473 bio->bi_vcnt - ci->idx, ci->sector_count);
474 __map_bio(ti, clone, tio);
475 ci->sector_count = 0;
476
477 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
478 /*
479 * There are some bvecs that don't span targets.
480 * Do as many of these as possible.
481 */
482 int i;
483 sector_t remaining = max;
484 sector_t bv_len;
485
486 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
487 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
488
489 if (bv_len > remaining)
490 break;
491
492 remaining -= bv_len;
493 len += bv_len;
494 }
495
496 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
497 __map_bio(ti, clone, tio);
498
499 ci->sector += len;
500 ci->sector_count -= len;
501 ci->idx = i;
502
503 } else {
504 /*
505 * Create two copy bios to deal with io that has
506 * been split across a target.
507 */
508 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
509
510 clone = split_bvec(bio, ci->sector, ci->idx,
511 bv->bv_offset, max);
512 __map_bio(ti, clone, tio);
513
514 ci->sector += max;
515 ci->sector_count -= max;
516 ti = dm_table_find_target(ci->map, ci->sector);
517
518 len = to_sector(bv->bv_len) - max;
519 clone = split_bvec(bio, ci->sector, ci->idx,
520 bv->bv_offset + to_bytes(max), len);
521 tio = alloc_tio(ci->md);
522 tio->io = ci->io;
523 tio->ti = ti;
524 memset(&tio->info, 0, sizeof(tio->info));
525 __map_bio(ti, clone, tio);
526
527 ci->sector += len;
528 ci->sector_count -= len;
529 ci->idx++;
530 }
531}
532
533/*
534 * Split the bio into several clones.
535 */
536static void __split_bio(struct mapped_device *md, struct bio *bio)
537{
538 struct clone_info ci;
539
540 ci.map = dm_get_table(md);
541 if (!ci.map) {
542 bio_io_error(bio, bio->bi_size);
543 return;
544 }
545
546 ci.md = md;
547 ci.bio = bio;
548 ci.io = alloc_io(md);
549 ci.io->error = 0;
550 atomic_set(&ci.io->io_count, 1);
551 ci.io->bio = bio;
552 ci.io->md = md;
553 ci.sector = bio->bi_sector;
554 ci.sector_count = bio_sectors(bio);
555 ci.idx = bio->bi_idx;
556
557 atomic_inc(&md->pending);
558 while (ci.sector_count)
559 __clone_and_map(&ci);
560
561 /* drop the extra reference count */
562 dec_pending(ci.io, 0);
563 dm_table_put(ci.map);
564}
565/*-----------------------------------------------------------------
566 * CRUD END
567 *---------------------------------------------------------------*/
568
569/*
570 * The request function that just remaps the bio built up by
571 * dm_merge_bvec.
572 */
573static int dm_request(request_queue_t *q, struct bio *bio)
574{
575 int r;
576 struct mapped_device *md = q->queuedata;
577
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700578 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /*
581 * If we're suspended we have to queue
582 * this io for later.
583 */
584 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700585 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (bio_rw(bio) == READA) {
588 bio_io_error(bio, bio->bi_size);
589 return 0;
590 }
591
592 r = queue_io(md, bio);
593 if (r < 0) {
594 bio_io_error(bio, bio->bi_size);
595 return 0;
596
597 } else if (r == 0)
598 return 0; /* deferred successfully */
599
600 /*
601 * We're in a while loop, because someone could suspend
602 * before we get to the following read lock.
603 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700604 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700608 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610}
611
612static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
613 sector_t *error_sector)
614{
615 struct mapped_device *md = q->queuedata;
616 struct dm_table *map = dm_get_table(md);
617 int ret = -ENXIO;
618
619 if (map) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700620 ret = dm_table_flush_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 dm_table_put(map);
622 }
623
624 return ret;
625}
626
627static void dm_unplug_all(request_queue_t *q)
628{
629 struct mapped_device *md = q->queuedata;
630 struct dm_table *map = dm_get_table(md);
631
632 if (map) {
633 dm_table_unplug_all(map);
634 dm_table_put(map);
635 }
636}
637
638static int dm_any_congested(void *congested_data, int bdi_bits)
639{
640 int r;
641 struct mapped_device *md = (struct mapped_device *) congested_data;
642 struct dm_table *map = dm_get_table(md);
643
644 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
645 r = bdi_bits;
646 else
647 r = dm_table_any_congested(map, bdi_bits);
648
649 dm_table_put(map);
650 return r;
651}
652
653/*-----------------------------------------------------------------
654 * An IDR is used to keep track of allocated minor numbers.
655 *---------------------------------------------------------------*/
656static DECLARE_MUTEX(_minor_lock);
657static DEFINE_IDR(_minor_idr);
658
659static void free_minor(unsigned int minor)
660{
661 down(&_minor_lock);
662 idr_remove(&_minor_idr, minor);
663 up(&_minor_lock);
664}
665
666/*
667 * See if the device with a specific minor # is free.
668 */
669static int specific_minor(struct mapped_device *md, unsigned int minor)
670{
671 int r, m;
672
673 if (minor >= (1 << MINORBITS))
674 return -EINVAL;
675
676 down(&_minor_lock);
677
678 if (idr_find(&_minor_idr, minor)) {
679 r = -EBUSY;
680 goto out;
681 }
682
683 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
684 if (!r) {
685 r = -ENOMEM;
686 goto out;
687 }
688
689 r = idr_get_new_above(&_minor_idr, md, minor, &m);
690 if (r) {
691 goto out;
692 }
693
694 if (m != minor) {
695 idr_remove(&_minor_idr, m);
696 r = -EBUSY;
697 goto out;
698 }
699
700out:
701 up(&_minor_lock);
702 return r;
703}
704
705static int next_free_minor(struct mapped_device *md, unsigned int *minor)
706{
707 int r;
708 unsigned int m;
709
710 down(&_minor_lock);
711
712 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
713 if (!r) {
714 r = -ENOMEM;
715 goto out;
716 }
717
718 r = idr_get_new(&_minor_idr, md, &m);
719 if (r) {
720 goto out;
721 }
722
723 if (m >= (1 << MINORBITS)) {
724 idr_remove(&_minor_idr, m);
725 r = -ENOSPC;
726 goto out;
727 }
728
729 *minor = m;
730
731out:
732 up(&_minor_lock);
733 return r;
734}
735
736static struct block_device_operations dm_blk_dops;
737
738/*
739 * Allocate and initialise a blank device with a given minor.
740 */
741static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
742{
743 int r;
744 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
745
746 if (!md) {
747 DMWARN("unable to allocate device, out of memory.");
748 return NULL;
749 }
750
751 /* get a minor number for the dev */
752 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
753 if (r < 0)
754 goto bad1;
755
756 memset(md, 0, sizeof(*md));
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700757 init_rwsem(&md->io_lock);
758 init_MUTEX(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 rwlock_init(&md->map_lock);
760 atomic_set(&md->holders, 1);
761 atomic_set(&md->event_nr, 0);
762
763 md->queue = blk_alloc_queue(GFP_KERNEL);
764 if (!md->queue)
765 goto bad1;
766
767 md->queue->queuedata = md;
768 md->queue->backing_dev_info.congested_fn = dm_any_congested;
769 md->queue->backing_dev_info.congested_data = md;
770 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +0100771 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 md->queue->unplug_fn = dm_unplug_all;
773 md->queue->issue_flush_fn = dm_flush_all;
774
775 md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
776 mempool_free_slab, _io_cache);
777 if (!md->io_pool)
778 goto bad2;
779
780 md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
781 mempool_free_slab, _tio_cache);
782 if (!md->tio_pool)
783 goto bad3;
784
785 md->disk = alloc_disk(1);
786 if (!md->disk)
787 goto bad4;
788
789 md->disk->major = _major;
790 md->disk->first_minor = minor;
791 md->disk->fops = &dm_blk_dops;
792 md->disk->queue = md->queue;
793 md->disk->private_data = md;
794 sprintf(md->disk->disk_name, "dm-%d", minor);
795 add_disk(md->disk);
796
797 atomic_set(&md->pending, 0);
798 init_waitqueue_head(&md->wait);
799 init_waitqueue_head(&md->eventq);
800
801 return md;
802
803 bad4:
804 mempool_destroy(md->tio_pool);
805 bad3:
806 mempool_destroy(md->io_pool);
807 bad2:
808 blk_put_queue(md->queue);
809 free_minor(minor);
810 bad1:
811 kfree(md);
812 return NULL;
813}
814
815static void free_dev(struct mapped_device *md)
816{
817 free_minor(md->disk->first_minor);
818 mempool_destroy(md->tio_pool);
819 mempool_destroy(md->io_pool);
820 del_gendisk(md->disk);
821 put_disk(md->disk);
822 blk_put_queue(md->queue);
823 kfree(md);
824}
825
826/*
827 * Bind a table to the device.
828 */
829static void event_callback(void *context)
830{
831 struct mapped_device *md = (struct mapped_device *) context;
832
833 atomic_inc(&md->event_nr);
834 wake_up(&md->eventq);
835}
836
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700837static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700839 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800841 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800842 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800843 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846static int __bind(struct mapped_device *md, struct dm_table *t)
847{
848 request_queue_t *q = md->queue;
849 sector_t size;
850
851 size = dm_table_get_size(t);
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700852 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (size == 0)
854 return 0;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 dm_table_get(t);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700857 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700858
859 write_lock(&md->map_lock);
860 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700862 write_unlock(&md->map_lock);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865}
866
867static void __unbind(struct mapped_device *md)
868{
869 struct dm_table *map = md->map;
870
871 if (!map)
872 return;
873
874 dm_table_event_callback(map, NULL, NULL);
875 write_lock(&md->map_lock);
876 md->map = NULL;
877 write_unlock(&md->map_lock);
878 dm_table_put(map);
879}
880
881/*
882 * Constructor for a new device.
883 */
884static int create_aux(unsigned int minor, int persistent,
885 struct mapped_device **result)
886{
887 struct mapped_device *md;
888
889 md = alloc_dev(minor, persistent);
890 if (!md)
891 return -ENXIO;
892
893 *result = md;
894 return 0;
895}
896
897int dm_create(struct mapped_device **result)
898{
899 return create_aux(0, 0, result);
900}
901
902int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
903{
904 return create_aux(minor, 1, result);
905}
906
David Teigland637842c2006-01-06 00:20:00 -0800907static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned minor = MINOR(dev);
911
912 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
913 return NULL;
914
915 down(&_minor_lock);
916
917 md = idr_find(&_minor_idr, minor);
David Teigland637842c2006-01-06 00:20:00 -0800918 if (!md || (dm_disk(md)->first_minor != minor))
919 md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 up(&_minor_lock);
922
David Teigland637842c2006-01-06 00:20:00 -0800923 return md;
924}
925
David Teiglandd229a952006-01-06 00:20:01 -0800926struct mapped_device *dm_get_md(dev_t dev)
927{
928 struct mapped_device *md = dm_find_md(dev);
929
930 if (md)
931 dm_get(md);
932
933 return md;
934}
935
David Teigland637842c2006-01-06 00:20:00 -0800936void *dm_get_mdptr(dev_t dev)
937{
938 struct mapped_device *md;
939 void *mdptr = NULL;
940
941 md = dm_find_md(dev);
942 if (md)
943 mdptr = md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return mdptr;
945}
946
947void dm_set_mdptr(struct mapped_device *md, void *ptr)
948{
949 md->interface_ptr = ptr;
950}
951
952void dm_get(struct mapped_device *md)
953{
954 atomic_inc(&md->holders);
955}
956
957void dm_put(struct mapped_device *md)
958{
959 struct dm_table *map = dm_get_table(md);
960
961 if (atomic_dec_and_test(&md->holders)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700962 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 dm_table_presuspend_targets(map);
964 dm_table_postsuspend_targets(map);
965 }
966 __unbind(md);
967 free_dev(md);
968 }
969
970 dm_table_put(map);
971}
972
973/*
974 * Process the deferred bios
975 */
976static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
977{
978 struct bio *n;
979
980 while (c) {
981 n = c->bi_next;
982 c->bi_next = NULL;
983 __split_bio(md, c);
984 c = n;
985 }
986}
987
988/*
989 * Swap in a new table (destroying old one).
990 */
991int dm_swap_table(struct mapped_device *md, struct dm_table *table)
992{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -0700993 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700995 down(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700998 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -0700999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 __unbind(md);
1002 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001004out:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001005 up(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001006 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009/*
1010 * Functions to lock and unlock any filesystem running on the
1011 * device.
1012 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001013static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001015 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001018
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001019 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001020 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001021 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001022 md->frozen_sb = NULL;
1023 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001024 }
1025
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001026 set_bit(DMF_FROZEN, &md->flags);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001029 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
1031 return 0;
1032}
1033
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001034static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001036 if (!test_bit(DMF_FROZEN, &md->flags))
1037 return;
1038
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001039 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001041 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/*
1045 * We need to be able to change a mapping table under a mounted
1046 * filesystem. For example we might want to move some data in
1047 * the background. Before the table can be swapped with
1048 * dm_bind_table, dm_suspend must be called to flush any in
1049 * flight bios and ensure that any further io gets deferred.
1050 */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001051int dm_suspend(struct mapped_device *md, int do_lockfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001053 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 DECLARE_WAITQUEUE(wait, current);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001055 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001057 down(&md->suspend_lock);
1058
1059 if (dm_suspended(md))
1060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001064 /* This does not get reverted if there's an error later. */
1065 dm_table_presuspend_targets(map);
1066
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001067 md->suspended_bdev = bdget_disk(md->disk, 0);
1068 if (!md->suspended_bdev) {
1069 DMWARN("bdget failed in dm_suspend");
1070 r = -ENOMEM;
1071 goto out;
1072 }
1073
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001074 /* Flush I/O to the device. */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001075 if (do_lockfs) {
1076 r = lock_fs(md);
1077 if (r)
1078 goto out;
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001082 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001084 down_write(&md->io_lock);
1085 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001088 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001091 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /*
1095 * Then we wait for the already mapped ios to
1096 * complete.
1097 */
1098 while (1) {
1099 set_current_state(TASK_INTERRUPTIBLE);
1100
1101 if (!atomic_read(&md->pending) || signal_pending(current))
1102 break;
1103
1104 io_schedule();
1105 }
1106 set_current_state(TASK_RUNNING);
1107
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001108 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 remove_wait_queue(&md->wait, &wait);
1110
1111 /* were we interrupted ? */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001112 r = -EINTR;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001113 if (atomic_read(&md->pending)) {
1114 up_write(&md->io_lock);
1115 unlock_fs(md);
1116 clear_bit(DMF_BLOCK_IO, &md->flags);
1117 goto out;
1118 }
1119 up_write(&md->io_lock);
1120
1121 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 set_bit(DMF_SUSPENDED, &md->flags);
1124
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001125 r = 0;
1126
1127out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001128 if (r && md->suspended_bdev) {
1129 bdput(md->suspended_bdev);
1130 md->suspended_bdev = NULL;
1131 }
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001134 up(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001135 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138int dm_resume(struct mapped_device *md)
1139{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001140 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001142 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001144 down(&md->suspend_lock);
1145 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001146 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001147
1148 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001149 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 dm_table_resume_targets(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001153
1154 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 clear_bit(DMF_BLOCK_IO, &md->flags);
1156
1157 def = bio_list_get(&md->deferred);
1158 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001159 up_write(&md->io_lock);
1160
1161 unlock_fs(md);
1162
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001163 bdput(md->suspended_bdev);
1164 md->suspended_bdev = NULL;
1165
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001166 clear_bit(DMF_SUSPENDED, &md->flags);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001170 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001171
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001172out:
1173 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001174 up(&md->suspend_lock);
1175
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001176 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179/*-----------------------------------------------------------------
1180 * Event notification.
1181 *---------------------------------------------------------------*/
1182uint32_t dm_get_event_nr(struct mapped_device *md)
1183{
1184 return atomic_read(&md->event_nr);
1185}
1186
1187int dm_wait_event(struct mapped_device *md, int event_nr)
1188{
1189 return wait_event_interruptible(md->eventq,
1190 (event_nr != atomic_read(&md->event_nr)));
1191}
1192
1193/*
1194 * The gendisk is only valid as long as you have a reference
1195 * count on 'md'.
1196 */
1197struct gendisk *dm_disk(struct mapped_device *md)
1198{
1199 return md->disk;
1200}
1201
1202int dm_suspended(struct mapped_device *md)
1203{
1204 return test_bit(DMF_SUSPENDED, &md->flags);
1205}
1206
1207static struct block_device_operations dm_blk_dops = {
1208 .open = dm_blk_open,
1209 .release = dm_blk_close,
1210 .owner = THIS_MODULE
1211};
1212
1213EXPORT_SYMBOL(dm_get_mapinfo);
1214
1215/*
1216 * module hooks
1217 */
1218module_init(dm_init);
1219module_exit(dm_exit);
1220
1221module_param(major, uint, 0);
1222MODULE_PARM_DESC(major, "The major number of the device mapper");
1223MODULE_DESCRIPTION(DM_NAME " driver");
1224MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1225MODULE_LICENSE("GPL");