]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/md/dm.c
dm raid1: clear region outside spinlock
[net-next-2.6.git] / drivers / md / dm.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
2b06cfff 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
1da177e4
LT
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>
48c9c27b 13#include <linux/mutex.h>
1da177e4
LT
14#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>
3ac51e74 21#include <linux/hdreg.h>
2056a782 22#include <linux/blktrace_api.h>
aa129a22 23#include <linux/smp_lock.h>
1da177e4 24
72d94861
AK
25#define DM_MSG_PREFIX "core"
26
1da177e4
LT
27static const char *_name = DM_NAME;
28
29static unsigned int major = 0;
30static unsigned int _major = 0;
31
f32c10b0 32static DEFINE_SPINLOCK(_minor_lock);
1da177e4
LT
33/*
34 * One of these is allocated per bio.
35 */
36struct dm_io {
37 struct mapped_device *md;
38 int error;
39 struct bio *bio;
40 atomic_t io_count;
3eaf840e 41 unsigned long start_time;
1da177e4
LT
42};
43
44/*
45 * One of these is allocated per target within a bio. Hopefully
46 * this will be simplified out one day.
47 */
028867ac 48struct dm_target_io {
1da177e4
LT
49 struct dm_io *io;
50 struct dm_target *ti;
51 union map_info info;
52};
53
54union map_info *dm_get_mapinfo(struct bio *bio)
55{
17b2f66f 56 if (bio && bio->bi_private)
028867ac 57 return &((struct dm_target_io *)bio->bi_private)->info;
17b2f66f 58 return NULL;
1da177e4
LT
59}
60
ba61fdd1
JM
61#define MINOR_ALLOCED ((void *)-1)
62
1da177e4
LT
63/*
64 * Bits for the md->flags field.
65 */
66#define DMF_BLOCK_IO 0
67#define DMF_SUSPENDED 1
aa8d7c2f 68#define DMF_FROZEN 2
fba9f90e 69#define DMF_FREEING 3
5c6bd75d 70#define DMF_DELETING 4
2e93ccc1 71#define DMF_NOFLUSH_SUSPENDING 5
1da177e4
LT
72
73struct mapped_device {
2ca3310e
AK
74 struct rw_semaphore io_lock;
75 struct semaphore suspend_lock;
2e93ccc1 76 spinlock_t pushback_lock;
1da177e4
LT
77 rwlock_t map_lock;
78 atomic_t holders;
5c6bd75d 79 atomic_t open_count;
1da177e4
LT
80
81 unsigned long flags;
82
83 request_queue_t *queue;
84 struct gendisk *disk;
7e51f257 85 char name[16];
1da177e4
LT
86
87 void *interface_ptr;
88
89 /*
90 * A list of ios that arrived while we were suspended.
91 */
92 atomic_t pending;
93 wait_queue_head_t wait;
74859364 94 struct bio_list deferred;
2e93ccc1 95 struct bio_list pushback;
1da177e4
LT
96
97 /*
98 * The current mapping.
99 */
100 struct dm_table *map;
101
102 /*
103 * io objects are allocated from here.
104 */
105 mempool_t *io_pool;
106 mempool_t *tio_pool;
107
9faf400f
SB
108 struct bio_set *bs;
109
1da177e4
LT
110 /*
111 * Event handling.
112 */
113 atomic_t event_nr;
114 wait_queue_head_t eventq;
115
116 /*
117 * freeze/thaw support require holding onto a super block
118 */
119 struct super_block *frozen_sb;
e39e2e95 120 struct block_device *suspended_bdev;
3ac51e74
DW
121
122 /* forced geometry settings */
123 struct hd_geometry geometry;
1da177e4
LT
124};
125
126#define MIN_IOS 256
e18b890b
CL
127static struct kmem_cache *_io_cache;
128static struct kmem_cache *_tio_cache;
1da177e4 129
1da177e4
LT
130static int __init local_init(void)
131{
132 int r;
133
1da177e4 134 /* allocate a slab for the dm_ios */
028867ac 135 _io_cache = KMEM_CACHE(dm_io, 0);
1da177e4
LT
136 if (!_io_cache)
137 return -ENOMEM;
138
139 /* allocate a slab for the target ios */
028867ac 140 _tio_cache = KMEM_CACHE(dm_target_io, 0);
1da177e4
LT
141 if (!_tio_cache) {
142 kmem_cache_destroy(_io_cache);
143 return -ENOMEM;
144 }
145
146 _major = major;
147 r = register_blkdev(_major, _name);
148 if (r < 0) {
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
151 return r;
152 }
153
154 if (!_major)
155 _major = r;
156
157 return 0;
158}
159
160static void local_exit(void)
161{
162 kmem_cache_destroy(_tio_cache);
163 kmem_cache_destroy(_io_cache);
164
1da177e4 165 if (unregister_blkdev(_major, _name) < 0)
890fbae2 166 DMERR("unregister_blkdev failed");
1da177e4
LT
167
168 _major = 0;
169
170 DMINFO("cleaned up");
171}
172
173int (*_inits[])(void) __initdata = {
174 local_init,
175 dm_target_init,
176 dm_linear_init,
177 dm_stripe_init,
178 dm_interface_init,
179};
180
181void (*_exits[])(void) = {
182 local_exit,
183 dm_target_exit,
184 dm_linear_exit,
185 dm_stripe_exit,
186 dm_interface_exit,
187};
188
189static int __init dm_init(void)
190{
191 const int count = ARRAY_SIZE(_inits);
192
193 int r, i;
194
195 for (i = 0; i < count; i++) {
196 r = _inits[i]();
197 if (r)
198 goto bad;
199 }
200
201 return 0;
202
203 bad:
204 while (i--)
205 _exits[i]();
206
207 return r;
208}
209
210static void __exit dm_exit(void)
211{
212 int i = ARRAY_SIZE(_exits);
213
214 while (i--)
215 _exits[i]();
216}
217
218/*
219 * Block device functions
220 */
221static int dm_blk_open(struct inode *inode, struct file *file)
222{
223 struct mapped_device *md;
224
fba9f90e
JM
225 spin_lock(&_minor_lock);
226
1da177e4 227 md = inode->i_bdev->bd_disk->private_data;
fba9f90e
JM
228 if (!md)
229 goto out;
230
5c6bd75d
AK
231 if (test_bit(DMF_FREEING, &md->flags) ||
232 test_bit(DMF_DELETING, &md->flags)) {
fba9f90e
JM
233 md = NULL;
234 goto out;
235 }
236
1da177e4 237 dm_get(md);
5c6bd75d 238 atomic_inc(&md->open_count);
fba9f90e
JM
239
240out:
241 spin_unlock(&_minor_lock);
242
243 return md ? 0 : -ENXIO;
1da177e4
LT
244}
245
246static int dm_blk_close(struct inode *inode, struct file *file)
247{
248 struct mapped_device *md;
249
250 md = inode->i_bdev->bd_disk->private_data;
5c6bd75d 251 atomic_dec(&md->open_count);
1da177e4
LT
252 dm_put(md);
253 return 0;
254}
255
5c6bd75d
AK
256int dm_open_count(struct mapped_device *md)
257{
258 return atomic_read(&md->open_count);
259}
260
261/*
262 * Guarantees nothing is using the device before it's deleted.
263 */
264int dm_lock_for_deletion(struct mapped_device *md)
265{
266 int r = 0;
267
268 spin_lock(&_minor_lock);
269
270 if (dm_open_count(md))
271 r = -EBUSY;
272 else
273 set_bit(DMF_DELETING, &md->flags);
274
275 spin_unlock(&_minor_lock);
276
277 return r;
278}
279
3ac51e74
DW
280static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
281{
282 struct mapped_device *md = bdev->bd_disk->private_data;
283
284 return dm_get_geometry(md, geo);
285}
286
aa129a22
MB
287static int dm_blk_ioctl(struct inode *inode, struct file *file,
288 unsigned int cmd, unsigned long arg)
289{
290 struct mapped_device *md;
291 struct dm_table *map;
292 struct dm_target *tgt;
293 int r = -ENOTTY;
294
295 /* We don't really need this lock, but we do need 'inode'. */
296 unlock_kernel();
297
298 md = inode->i_bdev->bd_disk->private_data;
299
300 map = dm_get_table(md);
301
302 if (!map || !dm_table_get_size(map))
303 goto out;
304
305 /* We only support devices that have a single target */
306 if (dm_table_get_num_targets(map) != 1)
307 goto out;
308
309 tgt = dm_table_get_target(map, 0);
310
311 if (dm_suspended(md)) {
312 r = -EAGAIN;
313 goto out;
314 }
315
316 if (tgt->type->ioctl)
317 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
318
319out:
320 dm_table_put(map);
321
322 lock_kernel();
323 return r;
324}
325
028867ac 326static struct dm_io *alloc_io(struct mapped_device *md)
1da177e4
LT
327{
328 return mempool_alloc(md->io_pool, GFP_NOIO);
329}
330
028867ac 331static void free_io(struct mapped_device *md, struct dm_io *io)
1da177e4
LT
332{
333 mempool_free(io, md->io_pool);
334}
335
028867ac 336static struct dm_target_io *alloc_tio(struct mapped_device *md)
1da177e4
LT
337{
338 return mempool_alloc(md->tio_pool, GFP_NOIO);
339}
340
028867ac 341static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
1da177e4
LT
342{
343 mempool_free(tio, md->tio_pool);
344}
345
3eaf840e
JNN
346static void start_io_acct(struct dm_io *io)
347{
348 struct mapped_device *md = io->md;
349
350 io->start_time = jiffies;
351
352 preempt_disable();
353 disk_round_stats(dm_disk(md));
354 preempt_enable();
355 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
356}
357
358static int end_io_acct(struct dm_io *io)
359{
360 struct mapped_device *md = io->md;
361 struct bio *bio = io->bio;
362 unsigned long duration = jiffies - io->start_time;
363 int pending;
364 int rw = bio_data_dir(bio);
365
366 preempt_disable();
367 disk_round_stats(dm_disk(md));
368 preempt_enable();
369 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
370
371 disk_stat_add(dm_disk(md), ticks[rw], duration);
372
373 return !pending;
374}
375
1da177e4
LT
376/*
377 * Add the bio to the list of deferred io.
378 */
379static int queue_io(struct mapped_device *md, struct bio *bio)
380{
2ca3310e 381 down_write(&md->io_lock);
1da177e4
LT
382
383 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
2ca3310e 384 up_write(&md->io_lock);
1da177e4
LT
385 return 1;
386 }
387
388 bio_list_add(&md->deferred, bio);
389
2ca3310e 390 up_write(&md->io_lock);
1da177e4
LT
391 return 0; /* deferred successfully */
392}
393
394/*
395 * Everyone (including functions in this file), should use this
396 * function to access the md->map field, and make sure they call
397 * dm_table_put() when finished.
398 */
399struct dm_table *dm_get_table(struct mapped_device *md)
400{
401 struct dm_table *t;
402
403 read_lock(&md->map_lock);
404 t = md->map;
405 if (t)
406 dm_table_get(t);
407 read_unlock(&md->map_lock);
408
409 return t;
410}
411
3ac51e74
DW
412/*
413 * Get the geometry associated with a dm device
414 */
415int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
416{
417 *geo = md->geometry;
418
419 return 0;
420}
421
422/*
423 * Set the geometry of a device.
424 */
425int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
426{
427 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
428
429 if (geo->start > sz) {
430 DMWARN("Start sector is beyond the geometry limits.");
431 return -EINVAL;
432 }
433
434 md->geometry = *geo;
435
436 return 0;
437}
438
1da177e4
LT
439/*-----------------------------------------------------------------
440 * CRUD START:
441 * A more elegant soln is in the works that uses the queue
442 * merge fn, unfortunately there are a couple of changes to
443 * the block layer that I want to make for this. So in the
444 * interests of getting something for people to use I give
445 * you this clearly demarcated crap.
446 *---------------------------------------------------------------*/
447
2e93ccc1
KU
448static int __noflush_suspending(struct mapped_device *md)
449{
450 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
451}
452
1da177e4
LT
453/*
454 * Decrements the number of outstanding ios that a bio has been
455 * cloned into, completing the original io if necc.
456 */
858119e1 457static void dec_pending(struct dm_io *io, int error)
1da177e4 458{
2e93ccc1
KU
459 unsigned long flags;
460
461 /* Push-back supersedes any I/O errors */
462 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
1da177e4
LT
463 io->error = error;
464
465 if (atomic_dec_and_test(&io->io_count)) {
2e93ccc1
KU
466 if (io->error == DM_ENDIO_REQUEUE) {
467 /*
468 * Target requested pushing back the I/O.
469 * This must be handled before the sleeper on
470 * suspend queue merges the pushback list.
471 */
472 spin_lock_irqsave(&io->md->pushback_lock, flags);
473 if (__noflush_suspending(io->md))
474 bio_list_add(&io->md->pushback, io->bio);
475 else
476 /* noflush suspend was interrupted. */
477 io->error = -EIO;
478 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
479 }
480
3eaf840e 481 if (end_io_acct(io))
1da177e4
LT
482 /* nudge anyone waiting on suspend queue */
483 wake_up(&io->md->wait);
484
2e93ccc1
KU
485 if (io->error != DM_ENDIO_REQUEUE) {
486 blk_add_trace_bio(io->md->queue, io->bio,
487 BLK_TA_COMPLETE);
488
489 bio_endio(io->bio, io->bio->bi_size, io->error);
490 }
2056a782 491
1da177e4
LT
492 free_io(io->md, io);
493 }
494}
495
496static int clone_endio(struct bio *bio, unsigned int done, int error)
497{
498 int r = 0;
028867ac 499 struct dm_target_io *tio = bio->bi_private;
9faf400f 500 struct mapped_device *md = tio->io->md;
1da177e4
LT
501 dm_endio_fn endio = tio->ti->type->end_io;
502
503 if (bio->bi_size)
504 return 1;
505
506 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
507 error = -EIO;
508
509 if (endio) {
510 r = endio(tio->ti, bio, error, &tio->info);
2e93ccc1
KU
511 if (r < 0 || r == DM_ENDIO_REQUEUE)
512 /*
513 * error and requeue request are handled
514 * in dec_pending().
515 */
1da177e4 516 error = r;
45cbcd79
KU
517 else if (r == DM_ENDIO_INCOMPLETE)
518 /* The target will handle the io */
1da177e4 519 return 1;
45cbcd79
KU
520 else if (r) {
521 DMWARN("unimplemented target endio return value: %d", r);
522 BUG();
523 }
1da177e4
LT
524 }
525
9faf400f
SB
526 dec_pending(tio->io, error);
527
528 /*
529 * Store md for cleanup instead of tio which is about to get freed.
530 */
531 bio->bi_private = md->bs;
532
1da177e4 533 bio_put(bio);
9faf400f 534 free_tio(md, tio);
1da177e4
LT
535 return r;
536}
537
538static sector_t max_io_len(struct mapped_device *md,
539 sector_t sector, struct dm_target *ti)
540{
541 sector_t offset = sector - ti->begin;
542 sector_t len = ti->len - offset;
543
544 /*
545 * Does the target need to split even further ?
546 */
547 if (ti->split_io) {
548 sector_t boundary;
549 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
550 - offset;
551 if (len > boundary)
552 len = boundary;
553 }
554
555 return len;
556}
557
558static void __map_bio(struct dm_target *ti, struct bio *clone,
028867ac 559 struct dm_target_io *tio)
1da177e4
LT
560{
561 int r;
2056a782 562 sector_t sector;
9faf400f 563 struct mapped_device *md;
1da177e4
LT
564
565 /*
566 * Sanity checks.
567 */
568 BUG_ON(!clone->bi_size);
569
570 clone->bi_end_io = clone_endio;
571 clone->bi_private = tio;
572
573 /*
574 * Map the clone. If r == 0 we don't need to do
575 * anything, the target has assumed ownership of
576 * this io.
577 */
578 atomic_inc(&tio->io->io_count);
2056a782 579 sector = clone->bi_sector;
1da177e4 580 r = ti->type->map(ti, clone, &tio->info);
45cbcd79 581 if (r == DM_MAPIO_REMAPPED) {
1da177e4 582 /* the bio has been remapped so dispatch it */
2056a782 583
17b2f66f
AK
584 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
585 tio->io->bio->bi_bdev->bd_dev, sector,
2056a782
JA
586 clone->bi_sector);
587
1da177e4 588 generic_make_request(clone);
2e93ccc1
KU
589 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
590 /* error the io and bail out, or requeue it if needed */
9faf400f
SB
591 md = tio->io->md;
592 dec_pending(tio->io, r);
593 /*
594 * Store bio_set for cleanup.
595 */
596 clone->bi_private = md->bs;
1da177e4 597 bio_put(clone);
9faf400f 598 free_tio(md, tio);
45cbcd79
KU
599 } else if (r) {
600 DMWARN("unimplemented target map return value: %d", r);
601 BUG();
1da177e4
LT
602 }
603}
604
605struct clone_info {
606 struct mapped_device *md;
607 struct dm_table *map;
608 struct bio *bio;
609 struct dm_io *io;
610 sector_t sector;
611 sector_t sector_count;
612 unsigned short idx;
613};
614
3676347a
PO
615static void dm_bio_destructor(struct bio *bio)
616{
9faf400f
SB
617 struct bio_set *bs = bio->bi_private;
618
619 bio_free(bio, bs);
3676347a
PO
620}
621
1da177e4
LT
622/*
623 * Creates a little bio that is just does part of a bvec.
624 */
625static struct bio *split_bvec(struct bio *bio, sector_t sector,
626 unsigned short idx, unsigned int offset,
9faf400f 627 unsigned int len, struct bio_set *bs)
1da177e4
LT
628{
629 struct bio *clone;
630 struct bio_vec *bv = bio->bi_io_vec + idx;
631
9faf400f 632 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
3676347a 633 clone->bi_destructor = dm_bio_destructor;
1da177e4
LT
634 *clone->bi_io_vec = *bv;
635
636 clone->bi_sector = sector;
637 clone->bi_bdev = bio->bi_bdev;
638 clone->bi_rw = bio->bi_rw;
639 clone->bi_vcnt = 1;
640 clone->bi_size = to_bytes(len);
641 clone->bi_io_vec->bv_offset = offset;
642 clone->bi_io_vec->bv_len = clone->bi_size;
643
644 return clone;
645}
646
647/*
648 * Creates a bio that consists of range of complete bvecs.
649 */
650static struct bio *clone_bio(struct bio *bio, sector_t sector,
651 unsigned short idx, unsigned short bv_count,
9faf400f 652 unsigned int len, struct bio_set *bs)
1da177e4
LT
653{
654 struct bio *clone;
655
9faf400f
SB
656 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
657 __bio_clone(clone, bio);
658 clone->bi_destructor = dm_bio_destructor;
1da177e4
LT
659 clone->bi_sector = sector;
660 clone->bi_idx = idx;
661 clone->bi_vcnt = idx + bv_count;
662 clone->bi_size = to_bytes(len);
663 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
664
665 return clone;
666}
667
668static void __clone_and_map(struct clone_info *ci)
669{
670 struct bio *clone, *bio = ci->bio;
671 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
672 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
028867ac 673 struct dm_target_io *tio;
1da177e4
LT
674
675 /*
676 * Allocate a target io object.
677 */
678 tio = alloc_tio(ci->md);
679 tio->io = ci->io;
680 tio->ti = ti;
681 memset(&tio->info, 0, sizeof(tio->info));
682
683 if (ci->sector_count <= max) {
684 /*
685 * Optimise for the simple case where we can do all of
686 * the remaining io with a single clone.
687 */
688 clone = clone_bio(bio, ci->sector, ci->idx,
9faf400f
SB
689 bio->bi_vcnt - ci->idx, ci->sector_count,
690 ci->md->bs);
1da177e4
LT
691 __map_bio(ti, clone, tio);
692 ci->sector_count = 0;
693
694 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
695 /*
696 * There are some bvecs that don't span targets.
697 * Do as many of these as possible.
698 */
699 int i;
700 sector_t remaining = max;
701 sector_t bv_len;
702
703 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
704 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
705
706 if (bv_len > remaining)
707 break;
708
709 remaining -= bv_len;
710 len += bv_len;
711 }
712
9faf400f
SB
713 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
714 ci->md->bs);
1da177e4
LT
715 __map_bio(ti, clone, tio);
716
717 ci->sector += len;
718 ci->sector_count -= len;
719 ci->idx = i;
720
721 } else {
722 /*
d2044a94 723 * Handle a bvec that must be split between two or more targets.
1da177e4
LT
724 */
725 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
d2044a94
AK
726 sector_t remaining = to_sector(bv->bv_len);
727 unsigned int offset = 0;
1da177e4 728
d2044a94
AK
729 do {
730 if (offset) {
731 ti = dm_table_find_target(ci->map, ci->sector);
732 max = max_io_len(ci->md, ci->sector, ti);
1da177e4 733
d2044a94
AK
734 tio = alloc_tio(ci->md);
735 tio->io = ci->io;
736 tio->ti = ti;
737 memset(&tio->info, 0, sizeof(tio->info));
738 }
739
740 len = min(remaining, max);
741
742 clone = split_bvec(bio, ci->sector, ci->idx,
9faf400f
SB
743 bv->bv_offset + offset, len,
744 ci->md->bs);
d2044a94
AK
745
746 __map_bio(ti, clone, tio);
747
748 ci->sector += len;
749 ci->sector_count -= len;
750 offset += to_bytes(len);
751 } while (remaining -= len);
1da177e4 752
1da177e4
LT
753 ci->idx++;
754 }
755}
756
757/*
758 * Split the bio into several clones.
759 */
760static void __split_bio(struct mapped_device *md, struct bio *bio)
761{
762 struct clone_info ci;
763
764 ci.map = dm_get_table(md);
765 if (!ci.map) {
766 bio_io_error(bio, bio->bi_size);
767 return;
768 }
769
770 ci.md = md;
771 ci.bio = bio;
772 ci.io = alloc_io(md);
773 ci.io->error = 0;
774 atomic_set(&ci.io->io_count, 1);
775 ci.io->bio = bio;
776 ci.io->md = md;
777 ci.sector = bio->bi_sector;
778 ci.sector_count = bio_sectors(bio);
779 ci.idx = bio->bi_idx;
780
3eaf840e 781 start_io_acct(ci.io);
1da177e4
LT
782 while (ci.sector_count)
783 __clone_and_map(&ci);
784
785 /* drop the extra reference count */
786 dec_pending(ci.io, 0);
787 dm_table_put(ci.map);
788}
789/*-----------------------------------------------------------------
790 * CRUD END
791 *---------------------------------------------------------------*/
792
793/*
794 * The request function that just remaps the bio built up by
795 * dm_merge_bvec.
796 */
797static int dm_request(request_queue_t *q, struct bio *bio)
798{
799 int r;
12f03a49 800 int rw = bio_data_dir(bio);
1da177e4
LT
801 struct mapped_device *md = q->queuedata;
802
2ca3310e 803 down_read(&md->io_lock);
1da177e4 804
12f03a49
KC
805 disk_stat_inc(dm_disk(md), ios[rw]);
806 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
807
1da177e4
LT
808 /*
809 * If we're suspended we have to queue
810 * this io for later.
811 */
812 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
2ca3310e 813 up_read(&md->io_lock);
1da177e4
LT
814
815 if (bio_rw(bio) == READA) {
816 bio_io_error(bio, bio->bi_size);
817 return 0;
818 }
819
820 r = queue_io(md, bio);
821 if (r < 0) {
822 bio_io_error(bio, bio->bi_size);
823 return 0;
824
825 } else if (r == 0)
826 return 0; /* deferred successfully */
827
828 /*
829 * We're in a while loop, because someone could suspend
830 * before we get to the following read lock.
831 */
2ca3310e 832 down_read(&md->io_lock);
1da177e4
LT
833 }
834
835 __split_bio(md, bio);
2ca3310e 836 up_read(&md->io_lock);
1da177e4
LT
837 return 0;
838}
839
840static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
841 sector_t *error_sector)
842{
843 struct mapped_device *md = q->queuedata;
844 struct dm_table *map = dm_get_table(md);
845 int ret = -ENXIO;
846
847 if (map) {
cf222b37 848 ret = dm_table_flush_all(map);
1da177e4
LT
849 dm_table_put(map);
850 }
851
852 return ret;
853}
854
855static void dm_unplug_all(request_queue_t *q)
856{
857 struct mapped_device *md = q->queuedata;
858 struct dm_table *map = dm_get_table(md);
859
860 if (map) {
861 dm_table_unplug_all(map);
862 dm_table_put(map);
863 }
864}
865
866static int dm_any_congested(void *congested_data, int bdi_bits)
867{
868 int r;
869 struct mapped_device *md = (struct mapped_device *) congested_data;
870 struct dm_table *map = dm_get_table(md);
871
872 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
873 r = bdi_bits;
874 else
875 r = dm_table_any_congested(map, bdi_bits);
876
877 dm_table_put(map);
878 return r;
879}
880
881/*-----------------------------------------------------------------
882 * An IDR is used to keep track of allocated minor numbers.
883 *---------------------------------------------------------------*/
1da177e4
LT
884static DEFINE_IDR(_minor_idr);
885
2b06cfff 886static void free_minor(int minor)
1da177e4 887{
f32c10b0 888 spin_lock(&_minor_lock);
1da177e4 889 idr_remove(&_minor_idr, minor);
f32c10b0 890 spin_unlock(&_minor_lock);
1da177e4
LT
891}
892
893/*
894 * See if the device with a specific minor # is free.
895 */
2b06cfff 896static int specific_minor(struct mapped_device *md, int minor)
1da177e4
LT
897{
898 int r, m;
899
900 if (minor >= (1 << MINORBITS))
901 return -EINVAL;
902
62f75c2f
JM
903 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
904 if (!r)
905 return -ENOMEM;
906
f32c10b0 907 spin_lock(&_minor_lock);
1da177e4
LT
908
909 if (idr_find(&_minor_idr, minor)) {
910 r = -EBUSY;
911 goto out;
912 }
913
ba61fdd1 914 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
62f75c2f 915 if (r)
1da177e4 916 goto out;
1da177e4
LT
917
918 if (m != minor) {
919 idr_remove(&_minor_idr, m);
920 r = -EBUSY;
921 goto out;
922 }
923
924out:
f32c10b0 925 spin_unlock(&_minor_lock);
1da177e4
LT
926 return r;
927}
928
2b06cfff 929static int next_free_minor(struct mapped_device *md, int *minor)
1da177e4 930{
2b06cfff 931 int r, m;
1da177e4 932
1da177e4 933 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
62f75c2f
JM
934 if (!r)
935 return -ENOMEM;
936
f32c10b0 937 spin_lock(&_minor_lock);
1da177e4 938
ba61fdd1 939 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1da177e4
LT
940 if (r) {
941 goto out;
942 }
943
944 if (m >= (1 << MINORBITS)) {
945 idr_remove(&_minor_idr, m);
946 r = -ENOSPC;
947 goto out;
948 }
949
950 *minor = m;
951
952out:
f32c10b0 953 spin_unlock(&_minor_lock);
1da177e4
LT
954 return r;
955}
956
957static struct block_device_operations dm_blk_dops;
958
959/*
960 * Allocate and initialise a blank device with a given minor.
961 */
2b06cfff 962static struct mapped_device *alloc_dev(int minor)
1da177e4
LT
963{
964 int r;
965 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
ba61fdd1 966 void *old_md;
1da177e4
LT
967
968 if (!md) {
969 DMWARN("unable to allocate device, out of memory.");
970 return NULL;
971 }
972
10da4f79
JM
973 if (!try_module_get(THIS_MODULE))
974 goto bad0;
975
1da177e4 976 /* get a minor number for the dev */
2b06cfff
AK
977 if (minor == DM_ANY_MINOR)
978 r = next_free_minor(md, &minor);
979 else
980 r = specific_minor(md, minor);
1da177e4
LT
981 if (r < 0)
982 goto bad1;
983
984 memset(md, 0, sizeof(*md));
2ca3310e
AK
985 init_rwsem(&md->io_lock);
986 init_MUTEX(&md->suspend_lock);
2e93ccc1 987 spin_lock_init(&md->pushback_lock);
1da177e4
LT
988 rwlock_init(&md->map_lock);
989 atomic_set(&md->holders, 1);
5c6bd75d 990 atomic_set(&md->open_count, 0);
1da177e4
LT
991 atomic_set(&md->event_nr, 0);
992
993 md->queue = blk_alloc_queue(GFP_KERNEL);
994 if (!md->queue)
e3f6ac61 995 goto bad1_free_minor;
1da177e4
LT
996
997 md->queue->queuedata = md;
998 md->queue->backing_dev_info.congested_fn = dm_any_congested;
999 md->queue->backing_dev_info.congested_data = md;
1000 blk_queue_make_request(md->queue, dm_request);
daef265f 1001 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1da177e4
LT
1002 md->queue->unplug_fn = dm_unplug_all;
1003 md->queue->issue_flush_fn = dm_flush_all;
1004
93d2341c 1005 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
74859364
KU
1006 if (!md->io_pool)
1007 goto bad2;
1da177e4 1008
93d2341c 1009 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1da177e4
LT
1010 if (!md->tio_pool)
1011 goto bad3;
1012
5972511b 1013 md->bs = bioset_create(16, 16);
9faf400f
SB
1014 if (!md->bs)
1015 goto bad_no_bioset;
1016
1da177e4
LT
1017 md->disk = alloc_disk(1);
1018 if (!md->disk)
1019 goto bad4;
1020
f0b04115
JM
1021 atomic_set(&md->pending, 0);
1022 init_waitqueue_head(&md->wait);
1023 init_waitqueue_head(&md->eventq);
1024
1da177e4
LT
1025 md->disk->major = _major;
1026 md->disk->first_minor = minor;
1027 md->disk->fops = &dm_blk_dops;
1028 md->disk->queue = md->queue;
1029 md->disk->private_data = md;
1030 sprintf(md->disk->disk_name, "dm-%d", minor);
1031 add_disk(md->disk);
7e51f257 1032 format_dev_t(md->name, MKDEV(_major, minor));
1da177e4 1033
ba61fdd1 1034 /* Populate the mapping, nobody knows we exist yet */
f32c10b0 1035 spin_lock(&_minor_lock);
ba61fdd1 1036 old_md = idr_replace(&_minor_idr, md, minor);
f32c10b0 1037 spin_unlock(&_minor_lock);
ba61fdd1
JM
1038
1039 BUG_ON(old_md != MINOR_ALLOCED);
1040
1da177e4
LT
1041 return md;
1042
1043 bad4:
9faf400f
SB
1044 bioset_free(md->bs);
1045 bad_no_bioset:
1da177e4
LT
1046 mempool_destroy(md->tio_pool);
1047 bad3:
1048 mempool_destroy(md->io_pool);
1049 bad2:
1312f40e 1050 blk_cleanup_queue(md->queue);
e3f6ac61 1051 bad1_free_minor:
1da177e4
LT
1052 free_minor(minor);
1053 bad1:
10da4f79
JM
1054 module_put(THIS_MODULE);
1055 bad0:
1da177e4
LT
1056 kfree(md);
1057 return NULL;
1058}
1059
1060static void free_dev(struct mapped_device *md)
1061{
2b06cfff 1062 int minor = md->disk->first_minor;
63d94e48 1063
d9dde59b
JN
1064 if (md->suspended_bdev) {
1065 thaw_bdev(md->suspended_bdev, NULL);
1066 bdput(md->suspended_bdev);
1067 }
1da177e4
LT
1068 mempool_destroy(md->tio_pool);
1069 mempool_destroy(md->io_pool);
9faf400f 1070 bioset_free(md->bs);
1da177e4 1071 del_gendisk(md->disk);
63d94e48 1072 free_minor(minor);
fba9f90e
JM
1073
1074 spin_lock(&_minor_lock);
1075 md->disk->private_data = NULL;
1076 spin_unlock(&_minor_lock);
1077
1da177e4 1078 put_disk(md->disk);
1312f40e 1079 blk_cleanup_queue(md->queue);
10da4f79 1080 module_put(THIS_MODULE);
1da177e4
LT
1081 kfree(md);
1082}
1083
1084/*
1085 * Bind a table to the device.
1086 */
1087static void event_callback(void *context)
1088{
1089 struct mapped_device *md = (struct mapped_device *) context;
1090
1091 atomic_inc(&md->event_nr);
1092 wake_up(&md->eventq);
1093}
1094
4e90188b 1095static void __set_size(struct mapped_device *md, sector_t size)
1da177e4 1096{
4e90188b 1097 set_capacity(md->disk, size);
1da177e4 1098
1b1dcc1b 1099 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
e39e2e95 1100 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1b1dcc1b 1101 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1da177e4
LT
1102}
1103
1104static int __bind(struct mapped_device *md, struct dm_table *t)
1105{
1106 request_queue_t *q = md->queue;
1107 sector_t size;
1108
1109 size = dm_table_get_size(t);
3ac51e74
DW
1110
1111 /*
1112 * Wipe any geometry if the size of the table changed.
1113 */
1114 if (size != get_capacity(md->disk))
1115 memset(&md->geometry, 0, sizeof(md->geometry));
1116
bfa152fa
JN
1117 if (md->suspended_bdev)
1118 __set_size(md, size);
1da177e4
LT
1119 if (size == 0)
1120 return 0;
1121
2ca3310e
AK
1122 dm_table_get(t);
1123 dm_table_event_callback(t, event_callback, md);
1124
1da177e4
LT
1125 write_lock(&md->map_lock);
1126 md->map = t;
2ca3310e 1127 dm_table_set_restrictions(t, q);
1da177e4
LT
1128 write_unlock(&md->map_lock);
1129
1da177e4
LT
1130 return 0;
1131}
1132
1133static void __unbind(struct mapped_device *md)
1134{
1135 struct dm_table *map = md->map;
1136
1137 if (!map)
1138 return;
1139
1140 dm_table_event_callback(map, NULL, NULL);
1141 write_lock(&md->map_lock);
1142 md->map = NULL;
1143 write_unlock(&md->map_lock);
1144 dm_table_put(map);
1145}
1146
1147/*
1148 * Constructor for a new device.
1149 */
2b06cfff 1150int dm_create(int minor, struct mapped_device **result)
1da177e4
LT
1151{
1152 struct mapped_device *md;
1153
2b06cfff 1154 md = alloc_dev(minor);
1da177e4
LT
1155 if (!md)
1156 return -ENXIO;
1157
1158 *result = md;
1159 return 0;
1160}
1161
637842cf 1162static struct mapped_device *dm_find_md(dev_t dev)
1da177e4
LT
1163{
1164 struct mapped_device *md;
1da177e4
LT
1165 unsigned minor = MINOR(dev);
1166
1167 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1168 return NULL;
1169
f32c10b0 1170 spin_lock(&_minor_lock);
1da177e4
LT
1171
1172 md = idr_find(&_minor_idr, minor);
fba9f90e
JM
1173 if (md && (md == MINOR_ALLOCED ||
1174 (dm_disk(md)->first_minor != minor) ||
17b2f66f 1175 test_bit(DMF_FREEING, &md->flags))) {
637842cf 1176 md = NULL;
fba9f90e
JM
1177 goto out;
1178 }
1da177e4 1179
fba9f90e 1180out:
f32c10b0 1181 spin_unlock(&_minor_lock);
1da177e4 1182
637842cf
DT
1183 return md;
1184}
1185
d229a958
DT
1186struct mapped_device *dm_get_md(dev_t dev)
1187{
1188 struct mapped_device *md = dm_find_md(dev);
1189
1190 if (md)
1191 dm_get(md);
1192
1193 return md;
1194}
1195
9ade92a9 1196void *dm_get_mdptr(struct mapped_device *md)
637842cf 1197{
9ade92a9 1198 return md->interface_ptr;
1da177e4
LT
1199}
1200
1201void dm_set_mdptr(struct mapped_device *md, void *ptr)
1202{
1203 md->interface_ptr = ptr;
1204}
1205
1206void dm_get(struct mapped_device *md)
1207{
1208 atomic_inc(&md->holders);
1209}
1210
72d94861
AK
1211const char *dm_device_name(struct mapped_device *md)
1212{
1213 return md->name;
1214}
1215EXPORT_SYMBOL_GPL(dm_device_name);
1216
1da177e4
LT
1217void dm_put(struct mapped_device *md)
1218{
1134e5ae 1219 struct dm_table *map;
1da177e4 1220
fba9f90e
JM
1221 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1222
f32c10b0 1223 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1134e5ae 1224 map = dm_get_table(md);
ba61fdd1 1225 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
fba9f90e 1226 set_bit(DMF_FREEING, &md->flags);
f32c10b0 1227 spin_unlock(&_minor_lock);
cf222b37 1228 if (!dm_suspended(md)) {
1da177e4
LT
1229 dm_table_presuspend_targets(map);
1230 dm_table_postsuspend_targets(map);
1231 }
1232 __unbind(md);
1134e5ae 1233 dm_table_put(map);
1da177e4
LT
1234 free_dev(md);
1235 }
1da177e4 1236}
79eb885c 1237EXPORT_SYMBOL_GPL(dm_put);
1da177e4
LT
1238
1239/*
1240 * Process the deferred bios
1241 */
1242static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1243{
1244 struct bio *n;
1245
1246 while (c) {
1247 n = c->bi_next;
1248 c->bi_next = NULL;
1249 __split_bio(md, c);
1250 c = n;
1251 }
1252}
1253
1254/*
1255 * Swap in a new table (destroying old one).
1256 */
1257int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1258{
93c534ae 1259 int r = -EINVAL;
1da177e4 1260
2ca3310e 1261 down(&md->suspend_lock);
1da177e4
LT
1262
1263 /* device must be suspended */
cf222b37 1264 if (!dm_suspended(md))
93c534ae 1265 goto out;
1da177e4 1266
bfa152fa
JN
1267 /* without bdev, the device size cannot be changed */
1268 if (!md->suspended_bdev)
1269 if (get_capacity(md->disk) != dm_table_get_size(table))
1270 goto out;
1271
1da177e4
LT
1272 __unbind(md);
1273 r = __bind(md, table);
1da177e4 1274
93c534ae 1275out:
2ca3310e 1276 up(&md->suspend_lock);
93c534ae 1277 return r;
1da177e4
LT
1278}
1279
1280/*
1281 * Functions to lock and unlock any filesystem running on the
1282 * device.
1283 */
2ca3310e 1284static int lock_fs(struct mapped_device *md)
1da177e4 1285{
e39e2e95 1286 int r;
1da177e4
LT
1287
1288 WARN_ON(md->frozen_sb);
dfbe03f6 1289
e39e2e95 1290 md->frozen_sb = freeze_bdev(md->suspended_bdev);
dfbe03f6 1291 if (IS_ERR(md->frozen_sb)) {
cf222b37 1292 r = PTR_ERR(md->frozen_sb);
e39e2e95
AK
1293 md->frozen_sb = NULL;
1294 return r;
dfbe03f6
AK
1295 }
1296
aa8d7c2f
AK
1297 set_bit(DMF_FROZEN, &md->flags);
1298
1da177e4 1299 /* don't bdput right now, we don't want the bdev
e39e2e95 1300 * to go away while it is locked.
1da177e4
LT
1301 */
1302 return 0;
1303}
1304
2ca3310e 1305static void unlock_fs(struct mapped_device *md)
1da177e4 1306{
aa8d7c2f
AK
1307 if (!test_bit(DMF_FROZEN, &md->flags))
1308 return;
1309
e39e2e95 1310 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1da177e4 1311 md->frozen_sb = NULL;
aa8d7c2f 1312 clear_bit(DMF_FROZEN, &md->flags);
1da177e4
LT
1313}
1314
1315/*
1316 * We need to be able to change a mapping table under a mounted
1317 * filesystem. For example we might want to move some data in
1318 * the background. Before the table can be swapped with
1319 * dm_bind_table, dm_suspend must be called to flush any in
1320 * flight bios and ensure that any further io gets deferred.
1321 */
a3d77d35 1322int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1da177e4 1323{
2ca3310e 1324 struct dm_table *map = NULL;
2e93ccc1 1325 unsigned long flags;
1da177e4 1326 DECLARE_WAITQUEUE(wait, current);
1ecac7fd 1327 struct bio *def;
cf222b37 1328 int r = -EINVAL;
a3d77d35 1329 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2e93ccc1 1330 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1da177e4 1331
2ca3310e
AK
1332 down(&md->suspend_lock);
1333
1334 if (dm_suspended(md))
d287483d 1335 goto out_unlock;
1da177e4
LT
1336
1337 map = dm_get_table(md);
1da177e4 1338
2e93ccc1
KU
1339 /*
1340 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1341 * This flag is cleared before dm_suspend returns.
1342 */
1343 if (noflush)
1344 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1345
cf222b37
AK
1346 /* This does not get reverted if there's an error later. */
1347 dm_table_presuspend_targets(map);
1348
bfa152fa
JN
1349 /* bdget() can stall if the pending I/Os are not flushed */
1350 if (!noflush) {
1351 md->suspended_bdev = bdget_disk(md->disk, 0);
1352 if (!md->suspended_bdev) {
1353 DMWARN("bdget failed in dm_suspend");
1354 r = -ENOMEM;
1355 goto flush_and_out;
1356 }
e39e2e95
AK
1357 }
1358
2e93ccc1
KU
1359 /*
1360 * Flush I/O to the device.
1361 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1362 */
1363 if (do_lockfs && !noflush) {
aa8d7c2f
AK
1364 r = lock_fs(md);
1365 if (r)
1366 goto out;
1367 }
1da177e4
LT
1368
1369 /*
354e0071 1370 * First we set the BLOCK_IO flag so no more ios will be mapped.
1da177e4 1371 */
2ca3310e
AK
1372 down_write(&md->io_lock);
1373 set_bit(DMF_BLOCK_IO, &md->flags);
1da177e4 1374
1da177e4 1375 add_wait_queue(&md->wait, &wait);
2ca3310e 1376 up_write(&md->io_lock);
1da177e4
LT
1377
1378 /* unplug */
2ca3310e 1379 if (map)
1da177e4 1380 dm_table_unplug_all(map);
1da177e4
LT
1381
1382 /*
1383 * Then we wait for the already mapped ios to
1384 * complete.
1385 */
1386 while (1) {
1387 set_current_state(TASK_INTERRUPTIBLE);
1388
1389 if (!atomic_read(&md->pending) || signal_pending(current))
1390 break;
1391
1392 io_schedule();
1393 }
1394 set_current_state(TASK_RUNNING);
1395
2ca3310e 1396 down_write(&md->io_lock);
1da177e4
LT
1397 remove_wait_queue(&md->wait, &wait);
1398
2e93ccc1
KU
1399 if (noflush) {
1400 spin_lock_irqsave(&md->pushback_lock, flags);
1401 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1402 bio_list_merge_head(&md->deferred, &md->pushback);
1403 bio_list_init(&md->pushback);
1404 spin_unlock_irqrestore(&md->pushback_lock, flags);
1405 }
1406
1da177e4 1407 /* were we interrupted ? */
cf222b37 1408 r = -EINTR;
2ca3310e 1409 if (atomic_read(&md->pending)) {
1ecac7fd
JN
1410 clear_bit(DMF_BLOCK_IO, &md->flags);
1411 def = bio_list_get(&md->deferred);
1412 __flush_deferred_io(md, def);
2ca3310e
AK
1413 up_write(&md->io_lock);
1414 unlock_fs(md);
2e93ccc1 1415 goto out; /* pushback list is already flushed, so skip flush */
2ca3310e
AK
1416 }
1417 up_write(&md->io_lock);
1da177e4 1418
cf222b37 1419 dm_table_postsuspend_targets(map);
1da177e4 1420
2ca3310e 1421 set_bit(DMF_SUSPENDED, &md->flags);
b84b0287 1422
2ca3310e 1423 r = 0;
b84b0287 1424
2e93ccc1
KU
1425flush_and_out:
1426 if (r && noflush) {
1427 /*
1428 * Because there may be already I/Os in the pushback list,
1429 * flush them before return.
1430 */
1431 down_write(&md->io_lock);
1432
1433 spin_lock_irqsave(&md->pushback_lock, flags);
1434 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1435 bio_list_merge_head(&md->deferred, &md->pushback);
1436 bio_list_init(&md->pushback);
1437 spin_unlock_irqrestore(&md->pushback_lock, flags);
1438
1439 def = bio_list_get(&md->deferred);
1440 __flush_deferred_io(md, def);
1441 up_write(&md->io_lock);
1442 }
1443
2ca3310e 1444out:
e39e2e95
AK
1445 if (r && md->suspended_bdev) {
1446 bdput(md->suspended_bdev);
1447 md->suspended_bdev = NULL;
1448 }
1449
2ca3310e 1450 dm_table_put(map);
d287483d
AK
1451
1452out_unlock:
2ca3310e 1453 up(&md->suspend_lock);
cf222b37 1454 return r;
1da177e4
LT
1455}
1456
1457int dm_resume(struct mapped_device *md)
1458{
cf222b37 1459 int r = -EINVAL;
1da177e4 1460 struct bio *def;
cf222b37 1461 struct dm_table *map = NULL;
1da177e4 1462
2ca3310e
AK
1463 down(&md->suspend_lock);
1464 if (!dm_suspended(md))
cf222b37 1465 goto out;
cf222b37
AK
1466
1467 map = dm_get_table(md);
2ca3310e 1468 if (!map || !dm_table_get_size(map))
cf222b37 1469 goto out;
1da177e4 1470
8757b776
MB
1471 r = dm_table_resume_targets(map);
1472 if (r)
1473 goto out;
2ca3310e
AK
1474
1475 down_write(&md->io_lock);
1da177e4
LT
1476 clear_bit(DMF_BLOCK_IO, &md->flags);
1477
1478 def = bio_list_get(&md->deferred);
1479 __flush_deferred_io(md, def);
2ca3310e
AK
1480 up_write(&md->io_lock);
1481
1482 unlock_fs(md);
1483
bfa152fa
JN
1484 if (md->suspended_bdev) {
1485 bdput(md->suspended_bdev);
1486 md->suspended_bdev = NULL;
1487 }
e39e2e95 1488
2ca3310e
AK
1489 clear_bit(DMF_SUSPENDED, &md->flags);
1490
1da177e4 1491 dm_table_unplug_all(map);
1da177e4 1492
8560ed6f
HR
1493 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1494
cf222b37 1495 r = 0;
2ca3310e 1496
cf222b37
AK
1497out:
1498 dm_table_put(map);
2ca3310e
AK
1499 up(&md->suspend_lock);
1500
cf222b37 1501 return r;
1da177e4
LT
1502}
1503
1504/*-----------------------------------------------------------------
1505 * Event notification.
1506 *---------------------------------------------------------------*/
1507uint32_t dm_get_event_nr(struct mapped_device *md)
1508{
1509 return atomic_read(&md->event_nr);
1510}
1511
1512int dm_wait_event(struct mapped_device *md, int event_nr)
1513{
1514 return wait_event_interruptible(md->eventq,
1515 (event_nr != atomic_read(&md->event_nr)));
1516}
1517
1518/*
1519 * The gendisk is only valid as long as you have a reference
1520 * count on 'md'.
1521 */
1522struct gendisk *dm_disk(struct mapped_device *md)
1523{
1524 return md->disk;
1525}
1526
1527int dm_suspended(struct mapped_device *md)
1528{
1529 return test_bit(DMF_SUSPENDED, &md->flags);
1530}
1531
2e93ccc1
KU
1532int dm_noflush_suspending(struct dm_target *ti)
1533{
1534 struct mapped_device *md = dm_table_get_md(ti->table);
1535 int r = __noflush_suspending(md);
1536
1537 dm_put(md);
1538
1539 return r;
1540}
1541EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1542
1da177e4
LT
1543static struct block_device_operations dm_blk_dops = {
1544 .open = dm_blk_open,
1545 .release = dm_blk_close,
aa129a22 1546 .ioctl = dm_blk_ioctl,
3ac51e74 1547 .getgeo = dm_blk_getgeo,
1da177e4
LT
1548 .owner = THIS_MODULE
1549};
1550
1551EXPORT_SYMBOL(dm_get_mapinfo);
1552
1553/*
1554 * module hooks
1555 */
1556module_init(dm_init);
1557module_exit(dm_exit);
1558
1559module_param(major, uint, 0);
1560MODULE_PARM_DESC(major, "The major number of the device mapper");
1561MODULE_DESCRIPTION(DM_NAME " driver");
1562MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1563MODULE_LICENSE("GPL");