blob: 8c82373f7ff31f24b8c0439e17c00d5a8873f30d [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>
Jens Axboe2056a782006-03-23 20:00:26 +010020#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22static const char *_name = DM_NAME;
23
24static unsigned int major = 0;
25static unsigned int _major = 0;
26
27/*
28 * One of these is allocated per bio.
29 */
30struct dm_io {
31 struct mapped_device *md;
32 int error;
33 struct bio *bio;
34 atomic_t io_count;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080035 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
38/*
39 * One of these is allocated per target within a bio. Hopefully
40 * this will be simplified out one day.
41 */
42struct target_io {
43 struct dm_io *io;
44 struct dm_target *ti;
45 union map_info info;
46};
47
48union map_info *dm_get_mapinfo(struct bio *bio)
49{
50 if (bio && bio->bi_private)
51 return &((struct target_io *)bio->bi_private)->info;
52 return NULL;
53}
54
55/*
56 * Bits for the md->flags field.
57 */
58#define DMF_BLOCK_IO 0
59#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080060#define DMF_FROZEN 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -070063 struct rw_semaphore io_lock;
64 struct semaphore suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 rwlock_t map_lock;
66 atomic_t holders;
67
68 unsigned long flags;
69
70 request_queue_t *queue;
71 struct gendisk *disk;
72
73 void *interface_ptr;
74
75 /*
76 * A list of ios that arrived while we were suspended.
77 */
78 atomic_t pending;
79 wait_queue_head_t wait;
80 struct bio_list deferred;
81
82 /*
83 * The current mapping.
84 */
85 struct dm_table *map;
86
87 /*
88 * io objects are allocated from here.
89 */
90 mempool_t *io_pool;
91 mempool_t *tio_pool;
92
93 /*
94 * Event handling.
95 */
96 atomic_t event_nr;
97 wait_queue_head_t eventq;
98
99 /*
100 * freeze/thaw support require holding onto a super block
101 */
102 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800103 struct block_device *suspended_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106#define MIN_IOS 256
107static kmem_cache_t *_io_cache;
108static kmem_cache_t *_tio_cache;
109
110static struct bio_set *dm_set;
111
112static int __init local_init(void)
113{
114 int r;
115
116 dm_set = bioset_create(16, 16, 4);
117 if (!dm_set)
118 return -ENOMEM;
119
120 /* allocate a slab for the dm_ios */
121 _io_cache = kmem_cache_create("dm_io",
122 sizeof(struct dm_io), 0, 0, NULL, NULL);
123 if (!_io_cache)
124 return -ENOMEM;
125
126 /* allocate a slab for the target ios */
127 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
128 0, 0, NULL, NULL);
129 if (!_tio_cache) {
130 kmem_cache_destroy(_io_cache);
131 return -ENOMEM;
132 }
133
134 _major = major;
135 r = register_blkdev(_major, _name);
136 if (r < 0) {
137 kmem_cache_destroy(_tio_cache);
138 kmem_cache_destroy(_io_cache);
139 return r;
140 }
141
142 if (!_major)
143 _major = r;
144
145 return 0;
146}
147
148static void local_exit(void)
149{
150 kmem_cache_destroy(_tio_cache);
151 kmem_cache_destroy(_io_cache);
152
153 bioset_free(dm_set);
154
155 if (unregister_blkdev(_major, _name) < 0)
156 DMERR("devfs_unregister_blkdev failed");
157
158 _major = 0;
159
160 DMINFO("cleaned up");
161}
162
163int (*_inits[])(void) __initdata = {
164 local_init,
165 dm_target_init,
166 dm_linear_init,
167 dm_stripe_init,
168 dm_interface_init,
169};
170
171void (*_exits[])(void) = {
172 local_exit,
173 dm_target_exit,
174 dm_linear_exit,
175 dm_stripe_exit,
176 dm_interface_exit,
177};
178
179static int __init dm_init(void)
180{
181 const int count = ARRAY_SIZE(_inits);
182
183 int r, i;
184
185 for (i = 0; i < count; i++) {
186 r = _inits[i]();
187 if (r)
188 goto bad;
189 }
190
191 return 0;
192
193 bad:
194 while (i--)
195 _exits[i]();
196
197 return r;
198}
199
200static void __exit dm_exit(void)
201{
202 int i = ARRAY_SIZE(_exits);
203
204 while (i--)
205 _exits[i]();
206}
207
208/*
209 * Block device functions
210 */
211static int dm_blk_open(struct inode *inode, struct file *file)
212{
213 struct mapped_device *md;
214
215 md = inode->i_bdev->bd_disk->private_data;
216 dm_get(md);
217 return 0;
218}
219
220static int dm_blk_close(struct inode *inode, struct file *file)
221{
222 struct mapped_device *md;
223
224 md = inode->i_bdev->bd_disk->private_data;
225 dm_put(md);
226 return 0;
227}
228
229static inline struct dm_io *alloc_io(struct mapped_device *md)
230{
231 return mempool_alloc(md->io_pool, GFP_NOIO);
232}
233
234static inline void free_io(struct mapped_device *md, struct dm_io *io)
235{
236 mempool_free(io, md->io_pool);
237}
238
239static inline struct target_io *alloc_tio(struct mapped_device *md)
240{
241 return mempool_alloc(md->tio_pool, GFP_NOIO);
242}
243
244static inline void free_tio(struct mapped_device *md, struct target_io *tio)
245{
246 mempool_free(tio, md->tio_pool);
247}
248
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800249static void start_io_acct(struct dm_io *io)
250{
251 struct mapped_device *md = io->md;
252
253 io->start_time = jiffies;
254
255 preempt_disable();
256 disk_round_stats(dm_disk(md));
257 preempt_enable();
258 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
259}
260
261static int end_io_acct(struct dm_io *io)
262{
263 struct mapped_device *md = io->md;
264 struct bio *bio = io->bio;
265 unsigned long duration = jiffies - io->start_time;
266 int pending;
267 int rw = bio_data_dir(bio);
268
269 preempt_disable();
270 disk_round_stats(dm_disk(md));
271 preempt_enable();
272 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
273
274 disk_stat_add(dm_disk(md), ticks[rw], duration);
275
276 return !pending;
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * Add the bio to the list of deferred io.
281 */
282static int queue_io(struct mapped_device *md, struct bio *bio)
283{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700284 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700287 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 1;
289 }
290
291 bio_list_add(&md->deferred, bio);
292
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700293 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0; /* deferred successfully */
295}
296
297/*
298 * Everyone (including functions in this file), should use this
299 * function to access the md->map field, and make sure they call
300 * dm_table_put() when finished.
301 */
302struct dm_table *dm_get_table(struct mapped_device *md)
303{
304 struct dm_table *t;
305
306 read_lock(&md->map_lock);
307 t = md->map;
308 if (t)
309 dm_table_get(t);
310 read_unlock(&md->map_lock);
311
312 return t;
313}
314
315/*-----------------------------------------------------------------
316 * CRUD START:
317 * A more elegant soln is in the works that uses the queue
318 * merge fn, unfortunately there are a couple of changes to
319 * the block layer that I want to make for this. So in the
320 * interests of getting something for people to use I give
321 * you this clearly demarcated crap.
322 *---------------------------------------------------------------*/
323
324/*
325 * Decrements the number of outstanding ios that a bio has been
326 * cloned into, completing the original io if necc.
327 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800328static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 if (error)
331 io->error = error;
332
333 if (atomic_dec_and_test(&io->io_count)) {
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800334 if (end_io_acct(io))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* nudge anyone waiting on suspend queue */
336 wake_up(&io->md->wait);
337
Jens Axboe2056a782006-03-23 20:00:26 +0100338 blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 bio_endio(io->bio, io->bio->bi_size, io->error);
341 free_io(io->md, io);
342 }
343}
344
345static int clone_endio(struct bio *bio, unsigned int done, int error)
346{
347 int r = 0;
348 struct target_io *tio = bio->bi_private;
349 struct dm_io *io = tio->io;
350 dm_endio_fn endio = tio->ti->type->end_io;
351
352 if (bio->bi_size)
353 return 1;
354
355 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
356 error = -EIO;
357
358 if (endio) {
359 r = endio(tio->ti, bio, error, &tio->info);
360 if (r < 0)
361 error = r;
362
363 else if (r > 0)
364 /* the target wants another shot at the io */
365 return 1;
366 }
367
368 free_tio(io->md, tio);
369 dec_pending(io, error);
370 bio_put(bio);
371 return r;
372}
373
374static sector_t max_io_len(struct mapped_device *md,
375 sector_t sector, struct dm_target *ti)
376{
377 sector_t offset = sector - ti->begin;
378 sector_t len = ti->len - offset;
379
380 /*
381 * Does the target need to split even further ?
382 */
383 if (ti->split_io) {
384 sector_t boundary;
385 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
386 - offset;
387 if (len > boundary)
388 len = boundary;
389 }
390
391 return len;
392}
393
394static void __map_bio(struct dm_target *ti, struct bio *clone,
395 struct target_io *tio)
396{
397 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100398 sector_t sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
401 * Sanity checks.
402 */
403 BUG_ON(!clone->bi_size);
404
405 clone->bi_end_io = clone_endio;
406 clone->bi_private = tio;
407
408 /*
409 * Map the clone. If r == 0 we don't need to do
410 * anything, the target has assumed ownership of
411 * this io.
412 */
413 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100414 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 r = ti->type->map(ti, clone, &tio->info);
Jens Axboe2056a782006-03-23 20:00:26 +0100416 if (r > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100418
419 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
420 tio->io->bio->bi_bdev->bd_dev, sector,
421 clone->bi_sector);
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 generic_make_request(clone);
Jens Axboe2056a782006-03-23 20:00:26 +0100424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 else if (r < 0) {
427 /* error the io and bail out */
428 struct dm_io *io = tio->io;
429 free_tio(tio->io->md, tio);
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700430 dec_pending(io, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 bio_put(clone);
432 }
433}
434
435struct clone_info {
436 struct mapped_device *md;
437 struct dm_table *map;
438 struct bio *bio;
439 struct dm_io *io;
440 sector_t sector;
441 sector_t sector_count;
442 unsigned short idx;
443};
444
Peter Osterlund36763472005-09-06 15:16:42 -0700445static void dm_bio_destructor(struct bio *bio)
446{
447 bio_free(bio, dm_set);
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/*
451 * Creates a little bio that is just does part of a bvec.
452 */
453static struct bio *split_bvec(struct bio *bio, sector_t sector,
454 unsigned short idx, unsigned int offset,
455 unsigned int len)
456{
457 struct bio *clone;
458 struct bio_vec *bv = bio->bi_io_vec + idx;
459
460 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
Peter Osterlund36763472005-09-06 15:16:42 -0700461 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *clone->bi_io_vec = *bv;
463
464 clone->bi_sector = sector;
465 clone->bi_bdev = bio->bi_bdev;
466 clone->bi_rw = bio->bi_rw;
467 clone->bi_vcnt = 1;
468 clone->bi_size = to_bytes(len);
469 clone->bi_io_vec->bv_offset = offset;
470 clone->bi_io_vec->bv_len = clone->bi_size;
471
472 return clone;
473}
474
475/*
476 * Creates a bio that consists of range of complete bvecs.
477 */
478static struct bio *clone_bio(struct bio *bio, sector_t sector,
479 unsigned short idx, unsigned short bv_count,
480 unsigned int len)
481{
482 struct bio *clone;
483
484 clone = bio_clone(bio, GFP_NOIO);
485 clone->bi_sector = sector;
486 clone->bi_idx = idx;
487 clone->bi_vcnt = idx + bv_count;
488 clone->bi_size = to_bytes(len);
489 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
490
491 return clone;
492}
493
494static void __clone_and_map(struct clone_info *ci)
495{
496 struct bio *clone, *bio = ci->bio;
497 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
498 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
499 struct target_io *tio;
500
501 /*
502 * Allocate a target io object.
503 */
504 tio = alloc_tio(ci->md);
505 tio->io = ci->io;
506 tio->ti = ti;
507 memset(&tio->info, 0, sizeof(tio->info));
508
509 if (ci->sector_count <= max) {
510 /*
511 * Optimise for the simple case where we can do all of
512 * the remaining io with a single clone.
513 */
514 clone = clone_bio(bio, ci->sector, ci->idx,
515 bio->bi_vcnt - ci->idx, ci->sector_count);
516 __map_bio(ti, clone, tio);
517 ci->sector_count = 0;
518
519 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
520 /*
521 * There are some bvecs that don't span targets.
522 * Do as many of these as possible.
523 */
524 int i;
525 sector_t remaining = max;
526 sector_t bv_len;
527
528 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
529 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
530
531 if (bv_len > remaining)
532 break;
533
534 remaining -= bv_len;
535 len += bv_len;
536 }
537
538 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
539 __map_bio(ti, clone, tio);
540
541 ci->sector += len;
542 ci->sector_count -= len;
543 ci->idx = i;
544
545 } else {
546 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800547 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800550 sector_t remaining = to_sector(bv->bv_len);
551 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800553 do {
554 if (offset) {
555 ti = dm_table_find_target(ci->map, ci->sector);
556 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800558 tio = alloc_tio(ci->md);
559 tio->io = ci->io;
560 tio->ti = ti;
561 memset(&tio->info, 0, sizeof(tio->info));
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800564 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800566 clone = split_bvec(bio, ci->sector, ci->idx,
567 bv->bv_offset + offset, len);
568
569 __map_bio(ti, clone, tio);
570
571 ci->sector += len;
572 ci->sector_count -= len;
573 offset += to_bytes(len);
574 } while (remaining -= len);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 ci->idx++;
577 }
578}
579
580/*
581 * Split the bio into several clones.
582 */
583static void __split_bio(struct mapped_device *md, struct bio *bio)
584{
585 struct clone_info ci;
586
587 ci.map = dm_get_table(md);
588 if (!ci.map) {
589 bio_io_error(bio, bio->bi_size);
590 return;
591 }
592
593 ci.md = md;
594 ci.bio = bio;
595 ci.io = alloc_io(md);
596 ci.io->error = 0;
597 atomic_set(&ci.io->io_count, 1);
598 ci.io->bio = bio;
599 ci.io->md = md;
600 ci.sector = bio->bi_sector;
601 ci.sector_count = bio_sectors(bio);
602 ci.idx = bio->bi_idx;
603
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800604 start_io_acct(ci.io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 while (ci.sector_count)
606 __clone_and_map(&ci);
607
608 /* drop the extra reference count */
609 dec_pending(ci.io, 0);
610 dm_table_put(ci.map);
611}
612/*-----------------------------------------------------------------
613 * CRUD END
614 *---------------------------------------------------------------*/
615
616/*
617 * The request function that just remaps the bio built up by
618 * dm_merge_bvec.
619 */
620static int dm_request(request_queue_t *q, struct bio *bio)
621{
622 int r;
Kevin Corry12f03a42006-02-01 03:04:52 -0800623 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct mapped_device *md = q->queuedata;
625
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700626 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Kevin Corry12f03a42006-02-01 03:04:52 -0800628 disk_stat_inc(dm_disk(md), ios[rw]);
629 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * If we're suspended we have to queue
633 * this io for later.
634 */
635 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700636 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (bio_rw(bio) == READA) {
639 bio_io_error(bio, bio->bi_size);
640 return 0;
641 }
642
643 r = queue_io(md, bio);
644 if (r < 0) {
645 bio_io_error(bio, bio->bi_size);
646 return 0;
647
648 } else if (r == 0)
649 return 0; /* deferred successfully */
650
651 /*
652 * We're in a while loop, because someone could suspend
653 * before we get to the following read lock.
654 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700655 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
658 __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700659 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661}
662
663static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
664 sector_t *error_sector)
665{
666 struct mapped_device *md = q->queuedata;
667 struct dm_table *map = dm_get_table(md);
668 int ret = -ENXIO;
669
670 if (map) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700671 ret = dm_table_flush_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 dm_table_put(map);
673 }
674
675 return ret;
676}
677
678static void dm_unplug_all(request_queue_t *q)
679{
680 struct mapped_device *md = q->queuedata;
681 struct dm_table *map = dm_get_table(md);
682
683 if (map) {
684 dm_table_unplug_all(map);
685 dm_table_put(map);
686 }
687}
688
689static int dm_any_congested(void *congested_data, int bdi_bits)
690{
691 int r;
692 struct mapped_device *md = (struct mapped_device *) congested_data;
693 struct dm_table *map = dm_get_table(md);
694
695 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
696 r = bdi_bits;
697 else
698 r = dm_table_any_congested(map, bdi_bits);
699
700 dm_table_put(map);
701 return r;
702}
703
704/*-----------------------------------------------------------------
705 * An IDR is used to keep track of allocated minor numbers.
706 *---------------------------------------------------------------*/
707static DECLARE_MUTEX(_minor_lock);
708static DEFINE_IDR(_minor_idr);
709
710static void free_minor(unsigned int minor)
711{
712 down(&_minor_lock);
713 idr_remove(&_minor_idr, minor);
714 up(&_minor_lock);
715}
716
717/*
718 * See if the device with a specific minor # is free.
719 */
720static int specific_minor(struct mapped_device *md, unsigned int minor)
721{
722 int r, m;
723
724 if (minor >= (1 << MINORBITS))
725 return -EINVAL;
726
727 down(&_minor_lock);
728
729 if (idr_find(&_minor_idr, minor)) {
730 r = -EBUSY;
731 goto out;
732 }
733
734 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
735 if (!r) {
736 r = -ENOMEM;
737 goto out;
738 }
739
740 r = idr_get_new_above(&_minor_idr, md, minor, &m);
741 if (r) {
742 goto out;
743 }
744
745 if (m != minor) {
746 idr_remove(&_minor_idr, m);
747 r = -EBUSY;
748 goto out;
749 }
750
751out:
752 up(&_minor_lock);
753 return r;
754}
755
756static int next_free_minor(struct mapped_device *md, unsigned int *minor)
757{
758 int r;
759 unsigned int m;
760
761 down(&_minor_lock);
762
763 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
764 if (!r) {
765 r = -ENOMEM;
766 goto out;
767 }
768
769 r = idr_get_new(&_minor_idr, md, &m);
770 if (r) {
771 goto out;
772 }
773
774 if (m >= (1 << MINORBITS)) {
775 idr_remove(&_minor_idr, m);
776 r = -ENOSPC;
777 goto out;
778 }
779
780 *minor = m;
781
782out:
783 up(&_minor_lock);
784 return r;
785}
786
787static struct block_device_operations dm_blk_dops;
788
789/*
790 * Allocate and initialise a blank device with a given minor.
791 */
792static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
793{
794 int r;
795 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
796
797 if (!md) {
798 DMWARN("unable to allocate device, out of memory.");
799 return NULL;
800 }
801
802 /* get a minor number for the dev */
803 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
804 if (r < 0)
805 goto bad1;
806
807 memset(md, 0, sizeof(*md));
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700808 init_rwsem(&md->io_lock);
809 init_MUTEX(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 rwlock_init(&md->map_lock);
811 atomic_set(&md->holders, 1);
812 atomic_set(&md->event_nr, 0);
813
814 md->queue = blk_alloc_queue(GFP_KERNEL);
815 if (!md->queue)
816 goto bad1;
817
818 md->queue->queuedata = md;
819 md->queue->backing_dev_info.congested_fn = dm_any_congested;
820 md->queue->backing_dev_info.congested_data = md;
821 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +0100822 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 md->queue->unplug_fn = dm_unplug_all;
824 md->queue->issue_flush_fn = dm_flush_all;
825
826 md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
827 mempool_free_slab, _io_cache);
828 if (!md->io_pool)
829 goto bad2;
830
831 md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
832 mempool_free_slab, _tio_cache);
833 if (!md->tio_pool)
834 goto bad3;
835
836 md->disk = alloc_disk(1);
837 if (!md->disk)
838 goto bad4;
839
840 md->disk->major = _major;
841 md->disk->first_minor = minor;
842 md->disk->fops = &dm_blk_dops;
843 md->disk->queue = md->queue;
844 md->disk->private_data = md;
845 sprintf(md->disk->disk_name, "dm-%d", minor);
846 add_disk(md->disk);
847
848 atomic_set(&md->pending, 0);
849 init_waitqueue_head(&md->wait);
850 init_waitqueue_head(&md->eventq);
851
852 return md;
853
854 bad4:
855 mempool_destroy(md->tio_pool);
856 bad3:
857 mempool_destroy(md->io_pool);
858 bad2:
Al Viro1312f402006-03-12 11:02:03 -0500859 blk_cleanup_queue(md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 free_minor(minor);
861 bad1:
862 kfree(md);
863 return NULL;
864}
865
866static void free_dev(struct mapped_device *md)
867{
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -0800868 unsigned int minor = md->disk->first_minor;
869
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -0800870 if (md->suspended_bdev) {
871 thaw_bdev(md->suspended_bdev, NULL);
872 bdput(md->suspended_bdev);
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 mempool_destroy(md->tio_pool);
875 mempool_destroy(md->io_pool);
876 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -0800877 free_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -0500879 blk_cleanup_queue(md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 kfree(md);
881}
882
883/*
884 * Bind a table to the device.
885 */
886static void event_callback(void *context)
887{
888 struct mapped_device *md = (struct mapped_device *) context;
889
890 atomic_inc(&md->event_nr);
891 wake_up(&md->eventq);
892}
893
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700894static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700896 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800898 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800899 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800900 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903static int __bind(struct mapped_device *md, struct dm_table *t)
904{
905 request_queue_t *q = md->queue;
906 sector_t size;
907
908 size = dm_table_get_size(t);
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700909 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (size == 0)
911 return 0;
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 dm_table_get(t);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700914 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700915
916 write_lock(&md->map_lock);
917 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700919 write_unlock(&md->map_lock);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
923
924static void __unbind(struct mapped_device *md)
925{
926 struct dm_table *map = md->map;
927
928 if (!map)
929 return;
930
931 dm_table_event_callback(map, NULL, NULL);
932 write_lock(&md->map_lock);
933 md->map = NULL;
934 write_unlock(&md->map_lock);
935 dm_table_put(map);
936}
937
938/*
939 * Constructor for a new device.
940 */
941static int create_aux(unsigned int minor, int persistent,
942 struct mapped_device **result)
943{
944 struct mapped_device *md;
945
946 md = alloc_dev(minor, persistent);
947 if (!md)
948 return -ENXIO;
949
950 *result = md;
951 return 0;
952}
953
954int dm_create(struct mapped_device **result)
955{
956 return create_aux(0, 0, result);
957}
958
959int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
960{
961 return create_aux(minor, 1, result);
962}
963
David Teigland637842c2006-01-06 00:20:00 -0800964static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 unsigned minor = MINOR(dev);
968
969 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
970 return NULL;
971
972 down(&_minor_lock);
973
974 md = idr_find(&_minor_idr, minor);
David Teigland637842c2006-01-06 00:20:00 -0800975 if (!md || (dm_disk(md)->first_minor != minor))
976 md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 up(&_minor_lock);
979
David Teigland637842c2006-01-06 00:20:00 -0800980 return md;
981}
982
David Teiglandd229a952006-01-06 00:20:01 -0800983struct mapped_device *dm_get_md(dev_t dev)
984{
985 struct mapped_device *md = dm_find_md(dev);
986
987 if (md)
988 dm_get(md);
989
990 return md;
991}
992
David Teigland637842c2006-01-06 00:20:00 -0800993void *dm_get_mdptr(dev_t dev)
994{
995 struct mapped_device *md;
996 void *mdptr = NULL;
997
998 md = dm_find_md(dev);
999 if (md)
1000 mdptr = md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return mdptr;
1002}
1003
1004void dm_set_mdptr(struct mapped_device *md, void *ptr)
1005{
1006 md->interface_ptr = ptr;
1007}
1008
1009void dm_get(struct mapped_device *md)
1010{
1011 atomic_inc(&md->holders);
1012}
1013
1014void dm_put(struct mapped_device *md)
1015{
1016 struct dm_table *map = dm_get_table(md);
1017
1018 if (atomic_dec_and_test(&md->holders)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001019 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 dm_table_presuspend_targets(map);
1021 dm_table_postsuspend_targets(map);
1022 }
1023 __unbind(md);
1024 free_dev(md);
1025 }
1026
1027 dm_table_put(map);
1028}
1029
1030/*
1031 * Process the deferred bios
1032 */
1033static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1034{
1035 struct bio *n;
1036
1037 while (c) {
1038 n = c->bi_next;
1039 c->bi_next = NULL;
1040 __split_bio(md, c);
1041 c = n;
1042 }
1043}
1044
1045/*
1046 * Swap in a new table (destroying old one).
1047 */
1048int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1049{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001050 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001052 down(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001055 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 __unbind(md);
1059 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001061out:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001062 up(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001063 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/*
1067 * Functions to lock and unlock any filesystem running on the
1068 * device.
1069 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001070static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001072 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001075
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001076 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001077 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001078 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001079 md->frozen_sb = NULL;
1080 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001081 }
1082
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001083 set_bit(DMF_FROZEN, &md->flags);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001086 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
1088 return 0;
1089}
1090
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001091static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001093 if (!test_bit(DMF_FROZEN, &md->flags))
1094 return;
1095
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001096 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001098 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101/*
1102 * We need to be able to change a mapping table under a mounted
1103 * filesystem. For example we might want to move some data in
1104 * the background. Before the table can be swapped with
1105 * dm_bind_table, dm_suspend must be called to flush any in
1106 * flight bios and ensure that any further io gets deferred.
1107 */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001108int dm_suspend(struct mapped_device *md, int do_lockfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001110 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 DECLARE_WAITQUEUE(wait, current);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001112 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001114 down(&md->suspend_lock);
1115
1116 if (dm_suspended(md))
1117 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001121 /* This does not get reverted if there's an error later. */
1122 dm_table_presuspend_targets(map);
1123
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001124 md->suspended_bdev = bdget_disk(md->disk, 0);
1125 if (!md->suspended_bdev) {
1126 DMWARN("bdget failed in dm_suspend");
1127 r = -ENOMEM;
1128 goto out;
1129 }
1130
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001131 /* Flush I/O to the device. */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001132 if (do_lockfs) {
1133 r = lock_fs(md);
1134 if (r)
1135 goto out;
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001139 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001141 down_write(&md->io_lock);
1142 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001145 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001148 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /*
1152 * Then we wait for the already mapped ios to
1153 * complete.
1154 */
1155 while (1) {
1156 set_current_state(TASK_INTERRUPTIBLE);
1157
1158 if (!atomic_read(&md->pending) || signal_pending(current))
1159 break;
1160
1161 io_schedule();
1162 }
1163 set_current_state(TASK_RUNNING);
1164
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001165 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 remove_wait_queue(&md->wait, &wait);
1167
1168 /* were we interrupted ? */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001169 r = -EINTR;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001170 if (atomic_read(&md->pending)) {
1171 up_write(&md->io_lock);
1172 unlock_fs(md);
1173 clear_bit(DMF_BLOCK_IO, &md->flags);
1174 goto out;
1175 }
1176 up_write(&md->io_lock);
1177
1178 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 set_bit(DMF_SUSPENDED, &md->flags);
1181
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001182 r = 0;
1183
1184out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001185 if (r && md->suspended_bdev) {
1186 bdput(md->suspended_bdev);
1187 md->suspended_bdev = NULL;
1188 }
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001191 up(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001192 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
1195int dm_resume(struct mapped_device *md)
1196{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001197 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001199 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001201 down(&md->suspend_lock);
1202 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001203 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001204
1205 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001206 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001207 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 dm_table_resume_targets(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001210
1211 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 clear_bit(DMF_BLOCK_IO, &md->flags);
1213
1214 def = bio_list_get(&md->deferred);
1215 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001216 up_write(&md->io_lock);
1217
1218 unlock_fs(md);
1219
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001220 bdput(md->suspended_bdev);
1221 md->suspended_bdev = NULL;
1222
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001223 clear_bit(DMF_SUSPENDED, &md->flags);
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001227 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001228
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001229out:
1230 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001231 up(&md->suspend_lock);
1232
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001233 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236/*-----------------------------------------------------------------
1237 * Event notification.
1238 *---------------------------------------------------------------*/
1239uint32_t dm_get_event_nr(struct mapped_device *md)
1240{
1241 return atomic_read(&md->event_nr);
1242}
1243
1244int dm_wait_event(struct mapped_device *md, int event_nr)
1245{
1246 return wait_event_interruptible(md->eventq,
1247 (event_nr != atomic_read(&md->event_nr)));
1248}
1249
1250/*
1251 * The gendisk is only valid as long as you have a reference
1252 * count on 'md'.
1253 */
1254struct gendisk *dm_disk(struct mapped_device *md)
1255{
1256 return md->disk;
1257}
1258
1259int dm_suspended(struct mapped_device *md)
1260{
1261 return test_bit(DMF_SUSPENDED, &md->flags);
1262}
1263
1264static struct block_device_operations dm_blk_dops = {
1265 .open = dm_blk_open,
1266 .release = dm_blk_close,
1267 .owner = THIS_MODULE
1268};
1269
1270EXPORT_SYMBOL(dm_get_mapinfo);
1271
1272/*
1273 * module hooks
1274 */
1275module_init(dm_init);
1276module_exit(dm_exit);
1277
1278module_param(major, uint, 0);
1279MODULE_PARM_DESC(major, "The major number of the device mapper");
1280MODULE_DESCRIPTION(DM_NAME " driver");
1281MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1282MODULE_LICENSE("GPL");