blob: 26b08ee425c74d6b0ce9ecee09a5d817a1e51841 [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;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080034 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
37/*
38 * One of these is allocated per target within a bio. Hopefully
39 * this will be simplified out one day.
40 */
41struct target_io {
42 struct dm_io *io;
43 struct dm_target *ti;
44 union map_info info;
45};
46
47union map_info *dm_get_mapinfo(struct bio *bio)
48{
49 if (bio && bio->bi_private)
50 return &((struct target_io *)bio->bi_private)->info;
51 return NULL;
52}
53
54/*
55 * Bits for the md->flags field.
56 */
57#define DMF_BLOCK_IO 0
58#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080059#define DMF_FROZEN 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -070062 struct rw_semaphore io_lock;
63 struct semaphore suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 rwlock_t map_lock;
65 atomic_t holders;
66
67 unsigned long flags;
68
69 request_queue_t *queue;
70 struct gendisk *disk;
71
72 void *interface_ptr;
73
74 /*
75 * A list of ios that arrived while we were suspended.
76 */
77 atomic_t pending;
78 wait_queue_head_t wait;
79 struct bio_list deferred;
80
81 /*
82 * The current mapping.
83 */
84 struct dm_table *map;
85
86 /*
87 * io objects are allocated from here.
88 */
89 mempool_t *io_pool;
90 mempool_t *tio_pool;
91
92 /*
93 * Event handling.
94 */
95 atomic_t event_nr;
96 wait_queue_head_t eventq;
97
98 /*
99 * freeze/thaw support require holding onto a super block
100 */
101 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800102 struct block_device *suspended_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105#define MIN_IOS 256
106static kmem_cache_t *_io_cache;
107static kmem_cache_t *_tio_cache;
108
109static struct bio_set *dm_set;
110
111static int __init local_init(void)
112{
113 int r;
114
115 dm_set = bioset_create(16, 16, 4);
116 if (!dm_set)
117 return -ENOMEM;
118
119 /* allocate a slab for the dm_ios */
120 _io_cache = kmem_cache_create("dm_io",
121 sizeof(struct dm_io), 0, 0, NULL, NULL);
122 if (!_io_cache)
123 return -ENOMEM;
124
125 /* allocate a slab for the target ios */
126 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
127 0, 0, NULL, NULL);
128 if (!_tio_cache) {
129 kmem_cache_destroy(_io_cache);
130 return -ENOMEM;
131 }
132
133 _major = major;
134 r = register_blkdev(_major, _name);
135 if (r < 0) {
136 kmem_cache_destroy(_tio_cache);
137 kmem_cache_destroy(_io_cache);
138 return r;
139 }
140
141 if (!_major)
142 _major = r;
143
144 return 0;
145}
146
147static void local_exit(void)
148{
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
151
152 bioset_free(dm_set);
153
154 if (unregister_blkdev(_major, _name) < 0)
155 DMERR("devfs_unregister_blkdev failed");
156
157 _major = 0;
158
159 DMINFO("cleaned up");
160}
161
162int (*_inits[])(void) __initdata = {
163 local_init,
164 dm_target_init,
165 dm_linear_init,
166 dm_stripe_init,
167 dm_interface_init,
168};
169
170void (*_exits[])(void) = {
171 local_exit,
172 dm_target_exit,
173 dm_linear_exit,
174 dm_stripe_exit,
175 dm_interface_exit,
176};
177
178static int __init dm_init(void)
179{
180 const int count = ARRAY_SIZE(_inits);
181
182 int r, i;
183
184 for (i = 0; i < count; i++) {
185 r = _inits[i]();
186 if (r)
187 goto bad;
188 }
189
190 return 0;
191
192 bad:
193 while (i--)
194 _exits[i]();
195
196 return r;
197}
198
199static void __exit dm_exit(void)
200{
201 int i = ARRAY_SIZE(_exits);
202
203 while (i--)
204 _exits[i]();
205}
206
207/*
208 * Block device functions
209 */
210static int dm_blk_open(struct inode *inode, struct file *file)
211{
212 struct mapped_device *md;
213
214 md = inode->i_bdev->bd_disk->private_data;
215 dm_get(md);
216 return 0;
217}
218
219static int dm_blk_close(struct inode *inode, struct file *file)
220{
221 struct mapped_device *md;
222
223 md = inode->i_bdev->bd_disk->private_data;
224 dm_put(md);
225 return 0;
226}
227
228static inline struct dm_io *alloc_io(struct mapped_device *md)
229{
230 return mempool_alloc(md->io_pool, GFP_NOIO);
231}
232
233static inline void free_io(struct mapped_device *md, struct dm_io *io)
234{
235 mempool_free(io, md->io_pool);
236}
237
238static inline struct target_io *alloc_tio(struct mapped_device *md)
239{
240 return mempool_alloc(md->tio_pool, GFP_NOIO);
241}
242
243static inline void free_tio(struct mapped_device *md, struct target_io *tio)
244{
245 mempool_free(tio, md->tio_pool);
246}
247
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800248static void start_io_acct(struct dm_io *io)
249{
250 struct mapped_device *md = io->md;
251
252 io->start_time = jiffies;
253
254 preempt_disable();
255 disk_round_stats(dm_disk(md));
256 preempt_enable();
257 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
258}
259
260static int end_io_acct(struct dm_io *io)
261{
262 struct mapped_device *md = io->md;
263 struct bio *bio = io->bio;
264 unsigned long duration = jiffies - io->start_time;
265 int pending;
266 int rw = bio_data_dir(bio);
267
268 preempt_disable();
269 disk_round_stats(dm_disk(md));
270 preempt_enable();
271 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
272
273 disk_stat_add(dm_disk(md), ticks[rw], duration);
274
275 return !pending;
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
279 * Add the bio to the list of deferred io.
280 */
281static int queue_io(struct mapped_device *md, struct bio *bio)
282{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700283 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700286 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 1;
288 }
289
290 bio_list_add(&md->deferred, bio);
291
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700292 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0; /* deferred successfully */
294}
295
296/*
297 * Everyone (including functions in this file), should use this
298 * function to access the md->map field, and make sure they call
299 * dm_table_put() when finished.
300 */
301struct dm_table *dm_get_table(struct mapped_device *md)
302{
303 struct dm_table *t;
304
305 read_lock(&md->map_lock);
306 t = md->map;
307 if (t)
308 dm_table_get(t);
309 read_unlock(&md->map_lock);
310
311 return t;
312}
313
314/*-----------------------------------------------------------------
315 * CRUD START:
316 * A more elegant soln is in the works that uses the queue
317 * merge fn, unfortunately there are a couple of changes to
318 * the block layer that I want to make for this. So in the
319 * interests of getting something for people to use I give
320 * you this clearly demarcated crap.
321 *---------------------------------------------------------------*/
322
323/*
324 * Decrements the number of outstanding ios that a bio has been
325 * cloned into, completing the original io if necc.
326 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800327static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 if (error)
330 io->error = error;
331
332 if (atomic_dec_and_test(&io->io_count)) {
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800333 if (end_io_acct(io))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* nudge anyone waiting on suspend queue */
335 wake_up(&io->md->wait);
336
337 bio_endio(io->bio, io->bio->bi_size, io->error);
338 free_io(io->md, io);
339 }
340}
341
342static int clone_endio(struct bio *bio, unsigned int done, int error)
343{
344 int r = 0;
345 struct target_io *tio = bio->bi_private;
346 struct dm_io *io = tio->io;
347 dm_endio_fn endio = tio->ti->type->end_io;
348
349 if (bio->bi_size)
350 return 1;
351
352 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
353 error = -EIO;
354
355 if (endio) {
356 r = endio(tio->ti, bio, error, &tio->info);
357 if (r < 0)
358 error = r;
359
360 else if (r > 0)
361 /* the target wants another shot at the io */
362 return 1;
363 }
364
365 free_tio(io->md, tio);
366 dec_pending(io, error);
367 bio_put(bio);
368 return r;
369}
370
371static sector_t max_io_len(struct mapped_device *md,
372 sector_t sector, struct dm_target *ti)
373{
374 sector_t offset = sector - ti->begin;
375 sector_t len = ti->len - offset;
376
377 /*
378 * Does the target need to split even further ?
379 */
380 if (ti->split_io) {
381 sector_t boundary;
382 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
383 - offset;
384 if (len > boundary)
385 len = boundary;
386 }
387
388 return len;
389}
390
391static void __map_bio(struct dm_target *ti, struct bio *clone,
392 struct target_io *tio)
393{
394 int r;
395
396 /*
397 * Sanity checks.
398 */
399 BUG_ON(!clone->bi_size);
400
401 clone->bi_end_io = clone_endio;
402 clone->bi_private = tio;
403
404 /*
405 * Map the clone. If r == 0 we don't need to do
406 * anything, the target has assumed ownership of
407 * this io.
408 */
409 atomic_inc(&tio->io->io_count);
410 r = ti->type->map(ti, clone, &tio->info);
411 if (r > 0)
412 /* the bio has been remapped so dispatch it */
413 generic_make_request(clone);
414
415 else if (r < 0) {
416 /* error the io and bail out */
417 struct dm_io *io = tio->io;
418 free_tio(tio->io->md, tio);
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700419 dec_pending(io, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 bio_put(clone);
421 }
422}
423
424struct clone_info {
425 struct mapped_device *md;
426 struct dm_table *map;
427 struct bio *bio;
428 struct dm_io *io;
429 sector_t sector;
430 sector_t sector_count;
431 unsigned short idx;
432};
433
Peter Osterlund36763472005-09-06 15:16:42 -0700434static void dm_bio_destructor(struct bio *bio)
435{
436 bio_free(bio, dm_set);
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*
440 * Creates a little bio that is just does part of a bvec.
441 */
442static struct bio *split_bvec(struct bio *bio, sector_t sector,
443 unsigned short idx, unsigned int offset,
444 unsigned int len)
445{
446 struct bio *clone;
447 struct bio_vec *bv = bio->bi_io_vec + idx;
448
449 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
Peter Osterlund36763472005-09-06 15:16:42 -0700450 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *clone->bi_io_vec = *bv;
452
453 clone->bi_sector = sector;
454 clone->bi_bdev = bio->bi_bdev;
455 clone->bi_rw = bio->bi_rw;
456 clone->bi_vcnt = 1;
457 clone->bi_size = to_bytes(len);
458 clone->bi_io_vec->bv_offset = offset;
459 clone->bi_io_vec->bv_len = clone->bi_size;
460
461 return clone;
462}
463
464/*
465 * Creates a bio that consists of range of complete bvecs.
466 */
467static struct bio *clone_bio(struct bio *bio, sector_t sector,
468 unsigned short idx, unsigned short bv_count,
469 unsigned int len)
470{
471 struct bio *clone;
472
473 clone = bio_clone(bio, GFP_NOIO);
474 clone->bi_sector = sector;
475 clone->bi_idx = idx;
476 clone->bi_vcnt = idx + bv_count;
477 clone->bi_size = to_bytes(len);
478 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
479
480 return clone;
481}
482
483static void __clone_and_map(struct clone_info *ci)
484{
485 struct bio *clone, *bio = ci->bio;
486 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
487 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
488 struct target_io *tio;
489
490 /*
491 * Allocate a target io object.
492 */
493 tio = alloc_tio(ci->md);
494 tio->io = ci->io;
495 tio->ti = ti;
496 memset(&tio->info, 0, sizeof(tio->info));
497
498 if (ci->sector_count <= max) {
499 /*
500 * Optimise for the simple case where we can do all of
501 * the remaining io with a single clone.
502 */
503 clone = clone_bio(bio, ci->sector, ci->idx,
504 bio->bi_vcnt - ci->idx, ci->sector_count);
505 __map_bio(ti, clone, tio);
506 ci->sector_count = 0;
507
508 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
509 /*
510 * There are some bvecs that don't span targets.
511 * Do as many of these as possible.
512 */
513 int i;
514 sector_t remaining = max;
515 sector_t bv_len;
516
517 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
518 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
519
520 if (bv_len > remaining)
521 break;
522
523 remaining -= bv_len;
524 len += bv_len;
525 }
526
527 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
528 __map_bio(ti, clone, tio);
529
530 ci->sector += len;
531 ci->sector_count -= len;
532 ci->idx = i;
533
534 } else {
535 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800536 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800539 sector_t remaining = to_sector(bv->bv_len);
540 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800542 do {
543 if (offset) {
544 ti = dm_table_find_target(ci->map, ci->sector);
545 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800547 tio = alloc_tio(ci->md);
548 tio->io = ci->io;
549 tio->ti = ti;
550 memset(&tio->info, 0, sizeof(tio->info));
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800553 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800555 clone = split_bvec(bio, ci->sector, ci->idx,
556 bv->bv_offset + offset, len);
557
558 __map_bio(ti, clone, tio);
559
560 ci->sector += len;
561 ci->sector_count -= len;
562 offset += to_bytes(len);
563 } while (remaining -= len);
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ci->idx++;
566 }
567}
568
569/*
570 * Split the bio into several clones.
571 */
572static void __split_bio(struct mapped_device *md, struct bio *bio)
573{
574 struct clone_info ci;
575
576 ci.map = dm_get_table(md);
577 if (!ci.map) {
578 bio_io_error(bio, bio->bi_size);
579 return;
580 }
581
582 ci.md = md;
583 ci.bio = bio;
584 ci.io = alloc_io(md);
585 ci.io->error = 0;
586 atomic_set(&ci.io->io_count, 1);
587 ci.io->bio = bio;
588 ci.io->md = md;
589 ci.sector = bio->bi_sector;
590 ci.sector_count = bio_sectors(bio);
591 ci.idx = bio->bi_idx;
592
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800593 start_io_acct(ci.io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 while (ci.sector_count)
595 __clone_and_map(&ci);
596
597 /* drop the extra reference count */
598 dec_pending(ci.io, 0);
599 dm_table_put(ci.map);
600}
601/*-----------------------------------------------------------------
602 * CRUD END
603 *---------------------------------------------------------------*/
604
605/*
606 * The request function that just remaps the bio built up by
607 * dm_merge_bvec.
608 */
609static int dm_request(request_queue_t *q, struct bio *bio)
610{
611 int r;
Kevin Corry12f03a42006-02-01 03:04:52 -0800612 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct mapped_device *md = q->queuedata;
614
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700615 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Kevin Corry12f03a42006-02-01 03:04:52 -0800617 disk_stat_inc(dm_disk(md), ios[rw]);
618 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /*
621 * If we're suspended we have to queue
622 * this io for later.
623 */
624 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700625 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (bio_rw(bio) == READA) {
628 bio_io_error(bio, bio->bi_size);
629 return 0;
630 }
631
632 r = queue_io(md, bio);
633 if (r < 0) {
634 bio_io_error(bio, bio->bi_size);
635 return 0;
636
637 } else if (r == 0)
638 return 0; /* deferred successfully */
639
640 /*
641 * We're in a while loop, because someone could suspend
642 * before we get to the following read lock.
643 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700644 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700648 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650}
651
652static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
653 sector_t *error_sector)
654{
655 struct mapped_device *md = q->queuedata;
656 struct dm_table *map = dm_get_table(md);
657 int ret = -ENXIO;
658
659 if (map) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700660 ret = dm_table_flush_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 dm_table_put(map);
662 }
663
664 return ret;
665}
666
667static void dm_unplug_all(request_queue_t *q)
668{
669 struct mapped_device *md = q->queuedata;
670 struct dm_table *map = dm_get_table(md);
671
672 if (map) {
673 dm_table_unplug_all(map);
674 dm_table_put(map);
675 }
676}
677
678static int dm_any_congested(void *congested_data, int bdi_bits)
679{
680 int r;
681 struct mapped_device *md = (struct mapped_device *) congested_data;
682 struct dm_table *map = dm_get_table(md);
683
684 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
685 r = bdi_bits;
686 else
687 r = dm_table_any_congested(map, bdi_bits);
688
689 dm_table_put(map);
690 return r;
691}
692
693/*-----------------------------------------------------------------
694 * An IDR is used to keep track of allocated minor numbers.
695 *---------------------------------------------------------------*/
696static DECLARE_MUTEX(_minor_lock);
697static DEFINE_IDR(_minor_idr);
698
699static void free_minor(unsigned int minor)
700{
701 down(&_minor_lock);
702 idr_remove(&_minor_idr, minor);
703 up(&_minor_lock);
704}
705
706/*
707 * See if the device with a specific minor # is free.
708 */
709static int specific_minor(struct mapped_device *md, unsigned int minor)
710{
711 int r, m;
712
713 if (minor >= (1 << MINORBITS))
714 return -EINVAL;
715
716 down(&_minor_lock);
717
718 if (idr_find(&_minor_idr, minor)) {
719 r = -EBUSY;
720 goto out;
721 }
722
723 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
724 if (!r) {
725 r = -ENOMEM;
726 goto out;
727 }
728
729 r = idr_get_new_above(&_minor_idr, md, minor, &m);
730 if (r) {
731 goto out;
732 }
733
734 if (m != minor) {
735 idr_remove(&_minor_idr, m);
736 r = -EBUSY;
737 goto out;
738 }
739
740out:
741 up(&_minor_lock);
742 return r;
743}
744
745static int next_free_minor(struct mapped_device *md, unsigned int *minor)
746{
747 int r;
748 unsigned int m;
749
750 down(&_minor_lock);
751
752 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
753 if (!r) {
754 r = -ENOMEM;
755 goto out;
756 }
757
758 r = idr_get_new(&_minor_idr, md, &m);
759 if (r) {
760 goto out;
761 }
762
763 if (m >= (1 << MINORBITS)) {
764 idr_remove(&_minor_idr, m);
765 r = -ENOSPC;
766 goto out;
767 }
768
769 *minor = m;
770
771out:
772 up(&_minor_lock);
773 return r;
774}
775
776static struct block_device_operations dm_blk_dops;
777
778/*
779 * Allocate and initialise a blank device with a given minor.
780 */
781static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
782{
783 int r;
784 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
785
786 if (!md) {
787 DMWARN("unable to allocate device, out of memory.");
788 return NULL;
789 }
790
791 /* get a minor number for the dev */
792 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
793 if (r < 0)
794 goto bad1;
795
796 memset(md, 0, sizeof(*md));
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700797 init_rwsem(&md->io_lock);
798 init_MUTEX(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 rwlock_init(&md->map_lock);
800 atomic_set(&md->holders, 1);
801 atomic_set(&md->event_nr, 0);
802
803 md->queue = blk_alloc_queue(GFP_KERNEL);
804 if (!md->queue)
805 goto bad1;
806
807 md->queue->queuedata = md;
808 md->queue->backing_dev_info.congested_fn = dm_any_congested;
809 md->queue->backing_dev_info.congested_data = md;
810 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +0100811 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 md->queue->unplug_fn = dm_unplug_all;
813 md->queue->issue_flush_fn = dm_flush_all;
814
815 md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
816 mempool_free_slab, _io_cache);
817 if (!md->io_pool)
818 goto bad2;
819
820 md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
821 mempool_free_slab, _tio_cache);
822 if (!md->tio_pool)
823 goto bad3;
824
825 md->disk = alloc_disk(1);
826 if (!md->disk)
827 goto bad4;
828
829 md->disk->major = _major;
830 md->disk->first_minor = minor;
831 md->disk->fops = &dm_blk_dops;
832 md->disk->queue = md->queue;
833 md->disk->private_data = md;
834 sprintf(md->disk->disk_name, "dm-%d", minor);
835 add_disk(md->disk);
836
837 atomic_set(&md->pending, 0);
838 init_waitqueue_head(&md->wait);
839 init_waitqueue_head(&md->eventq);
840
841 return md;
842
843 bad4:
844 mempool_destroy(md->tio_pool);
845 bad3:
846 mempool_destroy(md->io_pool);
847 bad2:
Al Viro1312f402006-03-12 11:02:03 -0500848 blk_cleanup_queue(md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 free_minor(minor);
850 bad1:
851 kfree(md);
852 return NULL;
853}
854
855static void free_dev(struct mapped_device *md)
856{
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -0800857 unsigned int minor = md->disk->first_minor;
858
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -0800859 if (md->suspended_bdev) {
860 thaw_bdev(md->suspended_bdev, NULL);
861 bdput(md->suspended_bdev);
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 mempool_destroy(md->tio_pool);
864 mempool_destroy(md->io_pool);
865 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -0800866 free_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -0500868 blk_cleanup_queue(md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 kfree(md);
870}
871
872/*
873 * Bind a table to the device.
874 */
875static void event_callback(void *context)
876{
877 struct mapped_device *md = (struct mapped_device *) context;
878
879 atomic_inc(&md->event_nr);
880 wake_up(&md->eventq);
881}
882
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -0700883static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -0700885 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800887 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800888 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800889 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static int __bind(struct mapped_device *md, struct dm_table *t)
893{
894 request_queue_t *q = md->queue;
895 sector_t size;
896
897 size = dm_table_get_size(t);
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -0700898 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (size == 0)
900 return 0;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 dm_table_get(t);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700903 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700904
905 write_lock(&md->map_lock);
906 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700908 write_unlock(&md->map_lock);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return 0;
911}
912
913static void __unbind(struct mapped_device *md)
914{
915 struct dm_table *map = md->map;
916
917 if (!map)
918 return;
919
920 dm_table_event_callback(map, NULL, NULL);
921 write_lock(&md->map_lock);
922 md->map = NULL;
923 write_unlock(&md->map_lock);
924 dm_table_put(map);
925}
926
927/*
928 * Constructor for a new device.
929 */
930static int create_aux(unsigned int minor, int persistent,
931 struct mapped_device **result)
932{
933 struct mapped_device *md;
934
935 md = alloc_dev(minor, persistent);
936 if (!md)
937 return -ENXIO;
938
939 *result = md;
940 return 0;
941}
942
943int dm_create(struct mapped_device **result)
944{
945 return create_aux(0, 0, result);
946}
947
948int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
949{
950 return create_aux(minor, 1, result);
951}
952
David Teigland637842c2006-01-06 00:20:00 -0800953static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned minor = MINOR(dev);
957
958 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
959 return NULL;
960
961 down(&_minor_lock);
962
963 md = idr_find(&_minor_idr, minor);
David Teigland637842c2006-01-06 00:20:00 -0800964 if (!md || (dm_disk(md)->first_minor != minor))
965 md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 up(&_minor_lock);
968
David Teigland637842c2006-01-06 00:20:00 -0800969 return md;
970}
971
David Teiglandd229a952006-01-06 00:20:01 -0800972struct mapped_device *dm_get_md(dev_t dev)
973{
974 struct mapped_device *md = dm_find_md(dev);
975
976 if (md)
977 dm_get(md);
978
979 return md;
980}
981
David Teigland637842c2006-01-06 00:20:00 -0800982void *dm_get_mdptr(dev_t dev)
983{
984 struct mapped_device *md;
985 void *mdptr = NULL;
986
987 md = dm_find_md(dev);
988 if (md)
989 mdptr = md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return mdptr;
991}
992
993void dm_set_mdptr(struct mapped_device *md, void *ptr)
994{
995 md->interface_ptr = ptr;
996}
997
998void dm_get(struct mapped_device *md)
999{
1000 atomic_inc(&md->holders);
1001}
1002
1003void dm_put(struct mapped_device *md)
1004{
1005 struct dm_table *map = dm_get_table(md);
1006
1007 if (atomic_dec_and_test(&md->holders)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001008 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 dm_table_presuspend_targets(map);
1010 dm_table_postsuspend_targets(map);
1011 }
1012 __unbind(md);
1013 free_dev(md);
1014 }
1015
1016 dm_table_put(map);
1017}
1018
1019/*
1020 * Process the deferred bios
1021 */
1022static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1023{
1024 struct bio *n;
1025
1026 while (c) {
1027 n = c->bi_next;
1028 c->bi_next = NULL;
1029 __split_bio(md, c);
1030 c = n;
1031 }
1032}
1033
1034/*
1035 * Swap in a new table (destroying old one).
1036 */
1037int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1038{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001039 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001041 down(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001044 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 __unbind(md);
1048 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001050out:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001051 up(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001052 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055/*
1056 * Functions to lock and unlock any filesystem running on the
1057 * device.
1058 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001059static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001061 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001064
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001065 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001066 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001067 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001068 md->frozen_sb = NULL;
1069 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001070 }
1071
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001072 set_bit(DMF_FROZEN, &md->flags);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001075 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
1077 return 0;
1078}
1079
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001080static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001082 if (!test_bit(DMF_FROZEN, &md->flags))
1083 return;
1084
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001085 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001087 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090/*
1091 * We need to be able to change a mapping table under a mounted
1092 * filesystem. For example we might want to move some data in
1093 * the background. Before the table can be swapped with
1094 * dm_bind_table, dm_suspend must be called to flush any in
1095 * flight bios and ensure that any further io gets deferred.
1096 */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001097int dm_suspend(struct mapped_device *md, int do_lockfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001099 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 DECLARE_WAITQUEUE(wait, current);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001101 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001103 down(&md->suspend_lock);
1104
1105 if (dm_suspended(md))
1106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001110 /* This does not get reverted if there's an error later. */
1111 dm_table_presuspend_targets(map);
1112
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001113 md->suspended_bdev = bdget_disk(md->disk, 0);
1114 if (!md->suspended_bdev) {
1115 DMWARN("bdget failed in dm_suspend");
1116 r = -ENOMEM;
1117 goto out;
1118 }
1119
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001120 /* Flush I/O to the device. */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001121 if (do_lockfs) {
1122 r = lock_fs(md);
1123 if (r)
1124 goto out;
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001128 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001130 down_write(&md->io_lock);
1131 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001134 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001137 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /*
1141 * Then we wait for the already mapped ios to
1142 * complete.
1143 */
1144 while (1) {
1145 set_current_state(TASK_INTERRUPTIBLE);
1146
1147 if (!atomic_read(&md->pending) || signal_pending(current))
1148 break;
1149
1150 io_schedule();
1151 }
1152 set_current_state(TASK_RUNNING);
1153
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001154 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 remove_wait_queue(&md->wait, &wait);
1156
1157 /* were we interrupted ? */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001158 r = -EINTR;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001159 if (atomic_read(&md->pending)) {
1160 up_write(&md->io_lock);
1161 unlock_fs(md);
1162 clear_bit(DMF_BLOCK_IO, &md->flags);
1163 goto out;
1164 }
1165 up_write(&md->io_lock);
1166
1167 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 set_bit(DMF_SUSPENDED, &md->flags);
1170
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001171 r = 0;
1172
1173out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001174 if (r && md->suspended_bdev) {
1175 bdput(md->suspended_bdev);
1176 md->suspended_bdev = NULL;
1177 }
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001180 up(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001181 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184int dm_resume(struct mapped_device *md)
1185{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001186 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001188 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001190 down(&md->suspend_lock);
1191 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001192 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001193
1194 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001195 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001196 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 dm_table_resume_targets(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001199
1200 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 clear_bit(DMF_BLOCK_IO, &md->flags);
1202
1203 def = bio_list_get(&md->deferred);
1204 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001205 up_write(&md->io_lock);
1206
1207 unlock_fs(md);
1208
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001209 bdput(md->suspended_bdev);
1210 md->suspended_bdev = NULL;
1211
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001212 clear_bit(DMF_SUSPENDED, &md->flags);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001216 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001217
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001218out:
1219 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001220 up(&md->suspend_lock);
1221
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001222 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225/*-----------------------------------------------------------------
1226 * Event notification.
1227 *---------------------------------------------------------------*/
1228uint32_t dm_get_event_nr(struct mapped_device *md)
1229{
1230 return atomic_read(&md->event_nr);
1231}
1232
1233int dm_wait_event(struct mapped_device *md, int event_nr)
1234{
1235 return wait_event_interruptible(md->eventq,
1236 (event_nr != atomic_read(&md->event_nr)));
1237}
1238
1239/*
1240 * The gendisk is only valid as long as you have a reference
1241 * count on 'md'.
1242 */
1243struct gendisk *dm_disk(struct mapped_device *md)
1244{
1245 return md->disk;
1246}
1247
1248int dm_suspended(struct mapped_device *md)
1249{
1250 return test_bit(DMF_SUSPENDED, &md->flags);
1251}
1252
1253static struct block_device_operations dm_blk_dops = {
1254 .open = dm_blk_open,
1255 .release = dm_blk_close,
1256 .owner = THIS_MODULE
1257};
1258
1259EXPORT_SYMBOL(dm_get_mapinfo);
1260
1261/*
1262 * module hooks
1263 */
1264module_init(dm_init);
1265module_exit(dm_exit);
1266
1267module_param(major, uint, 0);
1268MODULE_PARM_DESC(major, "The major number of the device mapper");
1269MODULE_DESCRIPTION(DM_NAME " driver");
1270MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1271MODULE_LICENSE("GPL");