blob: 4d710b7a133ba69570f0de74c5f7c78ec2aff63f [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>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
17#include <linux/buffer_head.h>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080021#include <linux/hdreg.h>
Jens Axboe2056a782006-03-23 20:00:26 +010022#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static const char *_name = DM_NAME;
25
26static unsigned int major = 0;
27static unsigned int _major = 0;
28
29/*
30 * One of these is allocated per bio.
31 */
32struct dm_io {
33 struct mapped_device *md;
34 int error;
35 struct bio *bio;
36 atomic_t io_count;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080037 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40/*
41 * One of these is allocated per target within a bio. Hopefully
42 * this will be simplified out one day.
43 */
44struct target_io {
45 struct dm_io *io;
46 struct dm_target *ti;
47 union map_info info;
48};
49
50union map_info *dm_get_mapinfo(struct bio *bio)
51{
52 if (bio && bio->bi_private)
53 return &((struct target_io *)bio->bi_private)->info;
54 return NULL;
55}
56
57/*
58 * Bits for the md->flags field.
59 */
60#define DMF_BLOCK_IO 0
61#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080062#define DMF_FROZEN 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -070065 struct rw_semaphore io_lock;
66 struct semaphore suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 rwlock_t map_lock;
68 atomic_t holders;
69
70 unsigned long flags;
71
72 request_queue_t *queue;
73 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -080074 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 void *interface_ptr;
77
78 /*
79 * A list of ios that arrived while we were suspended.
80 */
81 atomic_t pending;
82 wait_queue_head_t wait;
83 struct bio_list deferred;
84
85 /*
86 * The current mapping.
87 */
88 struct dm_table *map;
89
90 /*
91 * io objects are allocated from here.
92 */
93 mempool_t *io_pool;
94 mempool_t *tio_pool;
95
96 /*
97 * Event handling.
98 */
99 atomic_t event_nr;
100 wait_queue_head_t eventq;
101
102 /*
103 * freeze/thaw support require holding onto a super block
104 */
105 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800106 struct block_device *suspended_bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800107
108 /* forced geometry settings */
109 struct hd_geometry geometry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112#define MIN_IOS 256
113static kmem_cache_t *_io_cache;
114static kmem_cache_t *_tio_cache;
115
116static struct bio_set *dm_set;
117
118static int __init local_init(void)
119{
120 int r;
121
122 dm_set = bioset_create(16, 16, 4);
123 if (!dm_set)
124 return -ENOMEM;
125
126 /* allocate a slab for the dm_ios */
127 _io_cache = kmem_cache_create("dm_io",
128 sizeof(struct dm_io), 0, 0, NULL, NULL);
129 if (!_io_cache)
130 return -ENOMEM;
131
132 /* allocate a slab for the target ios */
133 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
134 0, 0, NULL, NULL);
135 if (!_tio_cache) {
136 kmem_cache_destroy(_io_cache);
137 return -ENOMEM;
138 }
139
140 _major = major;
141 r = register_blkdev(_major, _name);
142 if (r < 0) {
143 kmem_cache_destroy(_tio_cache);
144 kmem_cache_destroy(_io_cache);
145 return r;
146 }
147
148 if (!_major)
149 _major = r;
150
151 return 0;
152}
153
154static void local_exit(void)
155{
156 kmem_cache_destroy(_tio_cache);
157 kmem_cache_destroy(_io_cache);
158
159 bioset_free(dm_set);
160
161 if (unregister_blkdev(_major, _name) < 0)
162 DMERR("devfs_unregister_blkdev failed");
163
164 _major = 0;
165
166 DMINFO("cleaned up");
167}
168
169int (*_inits[])(void) __initdata = {
170 local_init,
171 dm_target_init,
172 dm_linear_init,
173 dm_stripe_init,
174 dm_interface_init,
175};
176
177void (*_exits[])(void) = {
178 local_exit,
179 dm_target_exit,
180 dm_linear_exit,
181 dm_stripe_exit,
182 dm_interface_exit,
183};
184
185static int __init dm_init(void)
186{
187 const int count = ARRAY_SIZE(_inits);
188
189 int r, i;
190
191 for (i = 0; i < count; i++) {
192 r = _inits[i]();
193 if (r)
194 goto bad;
195 }
196
197 return 0;
198
199 bad:
200 while (i--)
201 _exits[i]();
202
203 return r;
204}
205
206static void __exit dm_exit(void)
207{
208 int i = ARRAY_SIZE(_exits);
209
210 while (i--)
211 _exits[i]();
212}
213
214/*
215 * Block device functions
216 */
217static int dm_blk_open(struct inode *inode, struct file *file)
218{
219 struct mapped_device *md;
220
221 md = inode->i_bdev->bd_disk->private_data;
222 dm_get(md);
223 return 0;
224}
225
226static int dm_blk_close(struct inode *inode, struct file *file)
227{
228 struct mapped_device *md;
229
230 md = inode->i_bdev->bd_disk->private_data;
231 dm_put(md);
232 return 0;
233}
234
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800235static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
236{
237 struct mapped_device *md = bdev->bd_disk->private_data;
238
239 return dm_get_geometry(md, geo);
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static inline struct dm_io *alloc_io(struct mapped_device *md)
243{
244 return mempool_alloc(md->io_pool, GFP_NOIO);
245}
246
247static inline void free_io(struct mapped_device *md, struct dm_io *io)
248{
249 mempool_free(io, md->io_pool);
250}
251
252static inline struct target_io *alloc_tio(struct mapped_device *md)
253{
254 return mempool_alloc(md->tio_pool, GFP_NOIO);
255}
256
257static inline void free_tio(struct mapped_device *md, struct target_io *tio)
258{
259 mempool_free(tio, md->tio_pool);
260}
261
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800262static void start_io_acct(struct dm_io *io)
263{
264 struct mapped_device *md = io->md;
265
266 io->start_time = jiffies;
267
268 preempt_disable();
269 disk_round_stats(dm_disk(md));
270 preempt_enable();
271 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
272}
273
274static int end_io_acct(struct dm_io *io)
275{
276 struct mapped_device *md = io->md;
277 struct bio *bio = io->bio;
278 unsigned long duration = jiffies - io->start_time;
279 int pending;
280 int rw = bio_data_dir(bio);
281
282 preempt_disable();
283 disk_round_stats(dm_disk(md));
284 preempt_enable();
285 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
286
287 disk_stat_add(dm_disk(md), ticks[rw], duration);
288
289 return !pending;
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * Add the bio to the list of deferred io.
294 */
295static int queue_io(struct mapped_device *md, struct bio *bio)
296{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700297 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700300 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 1;
302 }
303
304 bio_list_add(&md->deferred, bio);
305
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700306 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0; /* deferred successfully */
308}
309
310/*
311 * Everyone (including functions in this file), should use this
312 * function to access the md->map field, and make sure they call
313 * dm_table_put() when finished.
314 */
315struct dm_table *dm_get_table(struct mapped_device *md)
316{
317 struct dm_table *t;
318
319 read_lock(&md->map_lock);
320 t = md->map;
321 if (t)
322 dm_table_get(t);
323 read_unlock(&md->map_lock);
324
325 return t;
326}
327
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800328/*
329 * Get the geometry associated with a dm device
330 */
331int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
332{
333 *geo = md->geometry;
334
335 return 0;
336}
337
338/*
339 * Set the geometry of a device.
340 */
341int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
342{
343 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
344
345 if (geo->start > sz) {
346 DMWARN("Start sector is beyond the geometry limits.");
347 return -EINVAL;
348 }
349
350 md->geometry = *geo;
351
352 return 0;
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/*-----------------------------------------------------------------
356 * CRUD START:
357 * A more elegant soln is in the works that uses the queue
358 * merge fn, unfortunately there are a couple of changes to
359 * the block layer that I want to make for this. So in the
360 * interests of getting something for people to use I give
361 * you this clearly demarcated crap.
362 *---------------------------------------------------------------*/
363
364/*
365 * Decrements the number of outstanding ios that a bio has been
366 * cloned into, completing the original io if necc.
367 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800368static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 if (error)
371 io->error = error;
372
373 if (atomic_dec_and_test(&io->io_count)) {
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800374 if (end_io_acct(io))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* nudge anyone waiting on suspend queue */
376 wake_up(&io->md->wait);
377
Jens Axboe2056a782006-03-23 20:00:26 +0100378 blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 bio_endio(io->bio, io->bio->bi_size, io->error);
381 free_io(io->md, io);
382 }
383}
384
385static int clone_endio(struct bio *bio, unsigned int done, int error)
386{
387 int r = 0;
388 struct target_io *tio = bio->bi_private;
389 struct dm_io *io = tio->io;
390 dm_endio_fn endio = tio->ti->type->end_io;
391
392 if (bio->bi_size)
393 return 1;
394
395 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
396 error = -EIO;
397
398 if (endio) {
399 r = endio(tio->ti, bio, error, &tio->info);
400 if (r < 0)
401 error = r;
402
403 else if (r > 0)
404 /* the target wants another shot at the io */
405 return 1;
406 }
407
408 free_tio(io->md, tio);
409 dec_pending(io, error);
410 bio_put(bio);
411 return r;
412}
413
414static sector_t max_io_len(struct mapped_device *md,
415 sector_t sector, struct dm_target *ti)
416{
417 sector_t offset = sector - ti->begin;
418 sector_t len = ti->len - offset;
419
420 /*
421 * Does the target need to split even further ?
422 */
423 if (ti->split_io) {
424 sector_t boundary;
425 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
426 - offset;
427 if (len > boundary)
428 len = boundary;
429 }
430
431 return len;
432}
433
434static void __map_bio(struct dm_target *ti, struct bio *clone,
435 struct target_io *tio)
436{
437 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100438 sector_t sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /*
441 * Sanity checks.
442 */
443 BUG_ON(!clone->bi_size);
444
445 clone->bi_end_io = clone_endio;
446 clone->bi_private = tio;
447
448 /*
449 * Map the clone. If r == 0 we don't need to do
450 * anything, the target has assumed ownership of
451 * this io.
452 */
453 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100454 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 r = ti->type->map(ti, clone, &tio->info);
Jens Axboe2056a782006-03-23 20:00:26 +0100456 if (r > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100458
459 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
460 tio->io->bio->bi_bdev->bd_dev, sector,
461 clone->bi_sector);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 generic_make_request(clone);
Jens Axboe2056a782006-03-23 20:00:26 +0100464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 else if (r < 0) {
467 /* error the io and bail out */
468 struct dm_io *io = tio->io;
469 free_tio(tio->io->md, tio);
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700470 dec_pending(io, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 bio_put(clone);
472 }
473}
474
475struct clone_info {
476 struct mapped_device *md;
477 struct dm_table *map;
478 struct bio *bio;
479 struct dm_io *io;
480 sector_t sector;
481 sector_t sector_count;
482 unsigned short idx;
483};
484
Peter Osterlund36763472005-09-06 15:16:42 -0700485static void dm_bio_destructor(struct bio *bio)
486{
487 bio_free(bio, dm_set);
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * Creates a little bio that is just does part of a bvec.
492 */
493static struct bio *split_bvec(struct bio *bio, sector_t sector,
494 unsigned short idx, unsigned int offset,
495 unsigned int len)
496{
497 struct bio *clone;
498 struct bio_vec *bv = bio->bi_io_vec + idx;
499
500 clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
Peter Osterlund36763472005-09-06 15:16:42 -0700501 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *clone->bi_io_vec = *bv;
503
504 clone->bi_sector = sector;
505 clone->bi_bdev = bio->bi_bdev;
506 clone->bi_rw = bio->bi_rw;
507 clone->bi_vcnt = 1;
508 clone->bi_size = to_bytes(len);
509 clone->bi_io_vec->bv_offset = offset;
510 clone->bi_io_vec->bv_len = clone->bi_size;
511
512 return clone;
513}
514
515/*
516 * Creates a bio that consists of range of complete bvecs.
517 */
518static struct bio *clone_bio(struct bio *bio, sector_t sector,
519 unsigned short idx, unsigned short bv_count,
520 unsigned int len)
521{
522 struct bio *clone;
523
524 clone = bio_clone(bio, GFP_NOIO);
525 clone->bi_sector = sector;
526 clone->bi_idx = idx;
527 clone->bi_vcnt = idx + bv_count;
528 clone->bi_size = to_bytes(len);
529 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
530
531 return clone;
532}
533
534static void __clone_and_map(struct clone_info *ci)
535{
536 struct bio *clone, *bio = ci->bio;
537 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
538 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
539 struct target_io *tio;
540
541 /*
542 * Allocate a target io object.
543 */
544 tio = alloc_tio(ci->md);
545 tio->io = ci->io;
546 tio->ti = ti;
547 memset(&tio->info, 0, sizeof(tio->info));
548
549 if (ci->sector_count <= max) {
550 /*
551 * Optimise for the simple case where we can do all of
552 * the remaining io with a single clone.
553 */
554 clone = clone_bio(bio, ci->sector, ci->idx,
555 bio->bi_vcnt - ci->idx, ci->sector_count);
556 __map_bio(ti, clone, tio);
557 ci->sector_count = 0;
558
559 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
560 /*
561 * There are some bvecs that don't span targets.
562 * Do as many of these as possible.
563 */
564 int i;
565 sector_t remaining = max;
566 sector_t bv_len;
567
568 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
569 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
570
571 if (bv_len > remaining)
572 break;
573
574 remaining -= bv_len;
575 len += bv_len;
576 }
577
578 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
579 __map_bio(ti, clone, tio);
580
581 ci->sector += len;
582 ci->sector_count -= len;
583 ci->idx = i;
584
585 } else {
586 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800587 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
589 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800590 sector_t remaining = to_sector(bv->bv_len);
591 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800593 do {
594 if (offset) {
595 ti = dm_table_find_target(ci->map, ci->sector);
596 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800598 tio = alloc_tio(ci->md);
599 tio->io = ci->io;
600 tio->ti = ti;
601 memset(&tio->info, 0, sizeof(tio->info));
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800604 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800606 clone = split_bvec(bio, ci->sector, ci->idx,
607 bv->bv_offset + offset, len);
608
609 __map_bio(ti, clone, tio);
610
611 ci->sector += len;
612 ci->sector_count -= len;
613 offset += to_bytes(len);
614 } while (remaining -= len);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ci->idx++;
617 }
618}
619
620/*
621 * Split the bio into several clones.
622 */
623static void __split_bio(struct mapped_device *md, struct bio *bio)
624{
625 struct clone_info ci;
626
627 ci.map = dm_get_table(md);
628 if (!ci.map) {
629 bio_io_error(bio, bio->bi_size);
630 return;
631 }
632
633 ci.md = md;
634 ci.bio = bio;
635 ci.io = alloc_io(md);
636 ci.io->error = 0;
637 atomic_set(&ci.io->io_count, 1);
638 ci.io->bio = bio;
639 ci.io->md = md;
640 ci.sector = bio->bi_sector;
641 ci.sector_count = bio_sectors(bio);
642 ci.idx = bio->bi_idx;
643
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800644 start_io_acct(ci.io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 while (ci.sector_count)
646 __clone_and_map(&ci);
647
648 /* drop the extra reference count */
649 dec_pending(ci.io, 0);
650 dm_table_put(ci.map);
651}
652/*-----------------------------------------------------------------
653 * CRUD END
654 *---------------------------------------------------------------*/
655
656/*
657 * The request function that just remaps the bio built up by
658 * dm_merge_bvec.
659 */
660static int dm_request(request_queue_t *q, struct bio *bio)
661{
662 int r;
Kevin Corry12f03a42006-02-01 03:04:52 -0800663 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct mapped_device *md = q->queuedata;
665
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700666 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Kevin Corry12f03a42006-02-01 03:04:52 -0800668 disk_stat_inc(dm_disk(md), ios[rw]);
669 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
672 * If we're suspended we have to queue
673 * this io for later.
674 */
675 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700676 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (bio_rw(bio) == READA) {
679 bio_io_error(bio, bio->bi_size);
680 return 0;
681 }
682
683 r = queue_io(md, bio);
684 if (r < 0) {
685 bio_io_error(bio, bio->bi_size);
686 return 0;
687
688 } else if (r == 0)
689 return 0; /* deferred successfully */
690
691 /*
692 * We're in a while loop, because someone could suspend
693 * before we get to the following read lock.
694 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700695 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
698 __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700699 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
702
703static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
704 sector_t *error_sector)
705{
706 struct mapped_device *md = q->queuedata;
707 struct dm_table *map = dm_get_table(md);
708 int ret = -ENXIO;
709
710 if (map) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700711 ret = dm_table_flush_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 dm_table_put(map);
713 }
714
715 return ret;
716}
717
718static void dm_unplug_all(request_queue_t *q)
719{
720 struct mapped_device *md = q->queuedata;
721 struct dm_table *map = dm_get_table(md);
722
723 if (map) {
724 dm_table_unplug_all(map);
725 dm_table_put(map);
726 }
727}
728
729static int dm_any_congested(void *congested_data, int bdi_bits)
730{
731 int r;
732 struct mapped_device *md = (struct mapped_device *) congested_data;
733 struct dm_table *map = dm_get_table(md);
734
735 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
736 r = bdi_bits;
737 else
738 r = dm_table_any_congested(map, bdi_bits);
739
740 dm_table_put(map);
741 return r;
742}
743
744/*-----------------------------------------------------------------
745 * An IDR is used to keep track of allocated minor numbers.
746 *---------------------------------------------------------------*/
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800747static DEFINE_MUTEX(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static DEFINE_IDR(_minor_idr);
749
750static void free_minor(unsigned int minor)
751{
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800752 mutex_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 idr_remove(&_minor_idr, minor);
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800754 mutex_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757/*
758 * See if the device with a specific minor # is free.
759 */
760static int specific_minor(struct mapped_device *md, unsigned int minor)
761{
762 int r, m;
763
764 if (minor >= (1 << MINORBITS))
765 return -EINVAL;
766
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800767 mutex_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (idr_find(&_minor_idr, minor)) {
770 r = -EBUSY;
771 goto out;
772 }
773
774 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
775 if (!r) {
776 r = -ENOMEM;
777 goto out;
778 }
779
780 r = idr_get_new_above(&_minor_idr, md, minor, &m);
781 if (r) {
782 goto out;
783 }
784
785 if (m != minor) {
786 idr_remove(&_minor_idr, m);
787 r = -EBUSY;
788 goto out;
789 }
790
791out:
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800792 mutex_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return r;
794}
795
796static int next_free_minor(struct mapped_device *md, unsigned int *minor)
797{
798 int r;
799 unsigned int m;
800
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800801 mutex_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
804 if (!r) {
805 r = -ENOMEM;
806 goto out;
807 }
808
809 r = idr_get_new(&_minor_idr, md, &m);
810 if (r) {
811 goto out;
812 }
813
814 if (m >= (1 << MINORBITS)) {
815 idr_remove(&_minor_idr, m);
816 r = -ENOSPC;
817 goto out;
818 }
819
820 *minor = m;
821
822out:
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800823 mutex_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return r;
825}
826
827static struct block_device_operations dm_blk_dops;
828
829/*
830 * Allocate and initialise a blank device with a given minor.
831 */
832static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
833{
834 int r;
835 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
836
837 if (!md) {
838 DMWARN("unable to allocate device, out of memory.");
839 return NULL;
840 }
841
842 /* get a minor number for the dev */
843 r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
844 if (r < 0)
845 goto bad1;
846
847 memset(md, 0, sizeof(*md));
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700848 init_rwsem(&md->io_lock);
849 init_MUTEX(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 rwlock_init(&md->map_lock);
851 atomic_set(&md->holders, 1);
852 atomic_set(&md->event_nr, 0);
853
854 md->queue = blk_alloc_queue(GFP_KERNEL);
855 if (!md->queue)
856 goto bad1;
857
858 md->queue->queuedata = md;
859 md->queue->backing_dev_info.congested_fn = dm_any_congested;
860 md->queue->backing_dev_info.congested_data = md;
861 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +0100862 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 md->queue->unplug_fn = dm_unplug_all;
864 md->queue->issue_flush_fn = dm_flush_all;
865
Matthew Dobson93d23412006-03-26 01:37:50 -0800866 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!md->io_pool)
868 goto bad2;
869
Matthew Dobson93d23412006-03-26 01:37:50 -0800870 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (!md->tio_pool)
872 goto bad3;
873
874 md->disk = alloc_disk(1);
875 if (!md->disk)
876 goto bad4;
877
878 md->disk->major = _major;
879 md->disk->first_minor = minor;
880 md->disk->fops = &dm_blk_dops;
881 md->disk->queue = md->queue;
882 md->disk->private_data = md;
883 sprintf(md->disk->disk_name, "dm-%d", minor);
884 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -0800885 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 atomic_set(&md->pending, 0);
888 init_waitqueue_head(&md->wait);
889 init_waitqueue_head(&md->eventq);
890
891 return md;
892
893 bad4:
894 mempool_destroy(md->tio_pool);
895 bad3:
896 mempool_destroy(md->io_pool);
897 bad2:
Al Viro1312f402006-03-12 11:02:03 -0500898 blk_cleanup_queue(md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 free_minor(minor);
900 bad1:
901 kfree(md);
902 return NULL;
903}
904
905static void free_dev(struct mapped_device *md)
906{
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -0800907 unsigned int minor = md->disk->first_minor;
908
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -0800909 if (md->suspended_bdev) {
910 thaw_bdev(md->suspended_bdev, NULL);
911 bdput(md->suspended_bdev);
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 mempool_destroy(md->tio_pool);
914 mempool_destroy(md->io_pool);
915 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -0800916 free_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -0500918 blk_cleanup_queue(md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 kfree(md);
920}
921
922/*
923 * Bind a table to the device.
924 */
925static void event_callback(void *context)
926{
927 struct mapped_device *md = (struct mapped_device *) context;
928
929 atomic_inc(&md->event_nr);
930 wake_up(&md->eventq);
931}
932
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700933static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700935 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800937 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800938 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800939 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942static int __bind(struct mapped_device *md, struct dm_table *t)
943{
944 request_queue_t *q = md->queue;
945 sector_t size;
946
947 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800948
949 /*
950 * Wipe any geometry if the size of the table changed.
951 */
952 if (size != get_capacity(md->disk))
953 memset(&md->geometry, 0, sizeof(md->geometry));
954
Alasdair G Kergon4e901882005-07-28 21:15:59 -0700955 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (size == 0)
957 return 0;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 dm_table_get(t);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700960 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700961
962 write_lock(&md->map_lock);
963 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700965 write_unlock(&md->map_lock);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
970static void __unbind(struct mapped_device *md)
971{
972 struct dm_table *map = md->map;
973
974 if (!map)
975 return;
976
977 dm_table_event_callback(map, NULL, NULL);
978 write_lock(&md->map_lock);
979 md->map = NULL;
980 write_unlock(&md->map_lock);
981 dm_table_put(map);
982}
983
984/*
985 * Constructor for a new device.
986 */
987static int create_aux(unsigned int minor, int persistent,
988 struct mapped_device **result)
989{
990 struct mapped_device *md;
991
992 md = alloc_dev(minor, persistent);
993 if (!md)
994 return -ENXIO;
995
996 *result = md;
997 return 0;
998}
999
1000int dm_create(struct mapped_device **result)
1001{
1002 return create_aux(0, 0, result);
1003}
1004
1005int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
1006{
1007 return create_aux(minor, 1, result);
1008}
1009
David Teigland637842c2006-01-06 00:20:00 -08001010static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned minor = MINOR(dev);
1014
1015 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1016 return NULL;
1017
Arjan van de Ven48c9c272006-03-27 01:18:20 -08001018 mutex_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 md = idr_find(&_minor_idr, minor);
David Teigland637842c2006-01-06 00:20:00 -08001021 if (!md || (dm_disk(md)->first_minor != minor))
1022 md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Arjan van de Ven48c9c272006-03-27 01:18:20 -08001024 mutex_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
David Teigland637842c2006-01-06 00:20:00 -08001026 return md;
1027}
1028
David Teiglandd229a952006-01-06 00:20:01 -08001029struct mapped_device *dm_get_md(dev_t dev)
1030{
1031 struct mapped_device *md = dm_find_md(dev);
1032
1033 if (md)
1034 dm_get(md);
1035
1036 return md;
1037}
1038
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001039void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001040{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001041 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044void dm_set_mdptr(struct mapped_device *md, void *ptr)
1045{
1046 md->interface_ptr = ptr;
1047}
1048
1049void dm_get(struct mapped_device *md)
1050{
1051 atomic_inc(&md->holders);
1052}
1053
1054void dm_put(struct mapped_device *md)
1055{
Mike Anderson1134e5a2006-03-27 01:17:54 -08001056 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (atomic_dec_and_test(&md->holders)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08001059 map = dm_get_table(md);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001060 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 dm_table_presuspend_targets(map);
1062 dm_table_postsuspend_targets(map);
1063 }
1064 __unbind(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001065 dm_table_put(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 free_dev(md);
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070/*
1071 * Process the deferred bios
1072 */
1073static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1074{
1075 struct bio *n;
1076
1077 while (c) {
1078 n = c->bi_next;
1079 c->bi_next = NULL;
1080 __split_bio(md, c);
1081 c = n;
1082 }
1083}
1084
1085/*
1086 * Swap in a new table (destroying old one).
1087 */
1088int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1089{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001090 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001092 down(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001095 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001096 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 __unbind(md);
1099 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001101out:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001102 up(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001103 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106/*
1107 * Functions to lock and unlock any filesystem running on the
1108 * device.
1109 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001110static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001112 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001115
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001116 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001117 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001118 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001119 md->frozen_sb = NULL;
1120 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001121 }
1122
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001123 set_bit(DMF_FROZEN, &md->flags);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001126 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
1128 return 0;
1129}
1130
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001131static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001133 if (!test_bit(DMF_FROZEN, &md->flags))
1134 return;
1135
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001136 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001138 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141/*
1142 * We need to be able to change a mapping table under a mounted
1143 * filesystem. For example we might want to move some data in
1144 * the background. Before the table can be swapped with
1145 * dm_bind_table, dm_suspend must be called to flush any in
1146 * flight bios and ensure that any further io gets deferred.
1147 */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001148int dm_suspend(struct mapped_device *md, int do_lockfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001150 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 DECLARE_WAITQUEUE(wait, current);
Jun'ichi Nomura1ecac7f2006-03-27 01:17:51 -08001152 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001153 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001155 down(&md->suspend_lock);
1156
1157 if (dm_suspended(md))
1158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001162 /* This does not get reverted if there's an error later. */
1163 dm_table_presuspend_targets(map);
1164
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001165 md->suspended_bdev = bdget_disk(md->disk, 0);
1166 if (!md->suspended_bdev) {
1167 DMWARN("bdget failed in dm_suspend");
1168 r = -ENOMEM;
1169 goto out;
1170 }
1171
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001172 /* Flush I/O to the device. */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001173 if (do_lockfs) {
1174 r = lock_fs(md);
1175 if (r)
1176 goto out;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001180 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001182 down_write(&md->io_lock);
1183 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001186 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001189 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /*
1193 * Then we wait for the already mapped ios to
1194 * complete.
1195 */
1196 while (1) {
1197 set_current_state(TASK_INTERRUPTIBLE);
1198
1199 if (!atomic_read(&md->pending) || signal_pending(current))
1200 break;
1201
1202 io_schedule();
1203 }
1204 set_current_state(TASK_RUNNING);
1205
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001206 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 remove_wait_queue(&md->wait, &wait);
1208
1209 /* were we interrupted ? */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001210 r = -EINTR;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001211 if (atomic_read(&md->pending)) {
Jun'ichi Nomura1ecac7f2006-03-27 01:17:51 -08001212 clear_bit(DMF_BLOCK_IO, &md->flags);
1213 def = bio_list_get(&md->deferred);
1214 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001215 up_write(&md->io_lock);
1216 unlock_fs(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001217 goto out;
1218 }
1219 up_write(&md->io_lock);
1220
1221 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 set_bit(DMF_SUSPENDED, &md->flags);
1224
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001225 r = 0;
1226
1227out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001228 if (r && md->suspended_bdev) {
1229 bdput(md->suspended_bdev);
1230 md->suspended_bdev = NULL;
1231 }
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001234 up(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001235 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238int dm_resume(struct mapped_device *md)
1239{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001240 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001242 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001244 down(&md->suspend_lock);
1245 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001246 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001247
1248 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001249 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001250 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 dm_table_resume_targets(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001253
1254 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 clear_bit(DMF_BLOCK_IO, &md->flags);
1256
1257 def = bio_list_get(&md->deferred);
1258 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001259 up_write(&md->io_lock);
1260
1261 unlock_fs(md);
1262
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001263 bdput(md->suspended_bdev);
1264 md->suspended_bdev = NULL;
1265
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001266 clear_bit(DMF_SUSPENDED, &md->flags);
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001270 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001271
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001272out:
1273 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001274 up(&md->suspend_lock);
1275
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001276 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279/*-----------------------------------------------------------------
1280 * Event notification.
1281 *---------------------------------------------------------------*/
1282uint32_t dm_get_event_nr(struct mapped_device *md)
1283{
1284 return atomic_read(&md->event_nr);
1285}
1286
1287int dm_wait_event(struct mapped_device *md, int event_nr)
1288{
1289 return wait_event_interruptible(md->eventq,
1290 (event_nr != atomic_read(&md->event_nr)));
1291}
1292
1293/*
1294 * The gendisk is only valid as long as you have a reference
1295 * count on 'md'.
1296 */
1297struct gendisk *dm_disk(struct mapped_device *md)
1298{
1299 return md->disk;
1300}
1301
1302int dm_suspended(struct mapped_device *md)
1303{
1304 return test_bit(DMF_SUSPENDED, &md->flags);
1305}
1306
1307static struct block_device_operations dm_blk_dops = {
1308 .open = dm_blk_open,
1309 .release = dm_blk_close,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001310 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 .owner = THIS_MODULE
1312};
1313
1314EXPORT_SYMBOL(dm_get_mapinfo);
1315
1316/*
1317 * module hooks
1318 */
1319module_init(dm_init);
1320module_exit(dm_exit);
1321
1322module_param(major, uint, 0);
1323MODULE_PARM_DESC(major, "The major number of the device mapper");
1324MODULE_DESCRIPTION(DM_NAME " driver");
1325MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1326MODULE_LICENSE("GPL");