ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / md / dm.c
1 /*
2  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3  *
4  * This file is released under the GPL.
5  */
6
7 #include "dm.h"
8 #include "dm-bio-list.h"
9
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/blkpg.h>
14 #include <linux/bio.h>
15 #include <linux/buffer_head.h>
16 #include <linux/mempool.h>
17 #include <linux/slab.h>
18
19 static const char *_name = DM_NAME;
20 #define MAX_DEVICES 1024
21
22 static unsigned int major = 0;
23 static unsigned int _major = 0;
24
25 /*
26  * One of these is allocated per bio.
27  */
28 struct dm_io {
29         struct mapped_device *md;
30         int error;
31         struct bio *bio;
32         atomic_t io_count;
33 };
34
35 /*
36  * One of these is allocated per target within a bio.  Hopefully
37  * this will be simplified out one day.
38  */
39 struct target_io {
40         struct dm_io *io;
41         struct dm_target *ti;
42         union map_info info;
43 };
44
45 /*
46  * Bits for the md->flags field.
47  */
48 #define DMF_BLOCK_IO 0
49 #define DMF_SUSPENDED 1
50 #define DMF_FS_LOCKED 2
51
52 struct mapped_device {
53         struct rw_semaphore lock;
54         rwlock_t map_lock;
55         atomic_t holders;
56
57         unsigned long flags;
58
59         request_queue_t *queue;
60         struct gendisk *disk;
61
62         /*
63          * A list of ios that arrived while we were suspended.
64          */
65         atomic_t pending;
66         wait_queue_head_t wait;
67         struct bio_list deferred;
68
69         /*
70          * The current mapping.
71          */
72         struct dm_table *map;
73
74         /*
75          * io objects are allocated from here.
76          */
77         mempool_t *io_pool;
78         mempool_t *tio_pool;
79
80         /*
81          * Event handling.
82          */
83         uint32_t event_nr;
84         wait_queue_head_t eventq;
85
86         /*
87          * freeze/thaw support require holding onto a super block
88          */
89         struct super_block *frozen_sb;
90 };
91
92 #define MIN_IOS 256
93 static kmem_cache_t *_io_cache;
94 static kmem_cache_t *_tio_cache;
95
96 static __init int local_init(void)
97 {
98         int r;
99
100         /* allocate a slab for the dm_ios */
101         _io_cache = kmem_cache_create("dm_io",
102                                       sizeof(struct dm_io), 0, 0, NULL, NULL);
103         if (!_io_cache)
104                 return -ENOMEM;
105
106         /* allocate a slab for the target ios */
107         _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
108                                        0, 0, NULL, NULL);
109         if (!_tio_cache) {
110                 kmem_cache_destroy(_io_cache);
111                 return -ENOMEM;
112         }
113
114         _major = major;
115         r = register_blkdev(_major, _name);
116         if (r < 0) {
117                 kmem_cache_destroy(_tio_cache);
118                 kmem_cache_destroy(_io_cache);
119                 return r;
120         }
121
122         if (!_major)
123                 _major = r;
124
125         return 0;
126 }
127
128 static void local_exit(void)
129 {
130         kmem_cache_destroy(_tio_cache);
131         kmem_cache_destroy(_io_cache);
132
133         if (unregister_blkdev(_major, _name) < 0)
134                 DMERR("devfs_unregister_blkdev failed");
135
136         _major = 0;
137
138         DMINFO("cleaned up");
139 }
140
141 /*
142  * We have a lot of init/exit functions, so it seems easier to
143  * store them in an array.  The disposable macro 'xx'
144  * expands a prefix into a pair of function names.
145  */
146 static struct {
147         int (*init) (void);
148         void (*exit) (void);
149
150 } _inits[] = {
151 #define xx(n) {n ## _init, n ## _exit},
152         xx(local)
153         xx(dm_target)
154         xx(dm_linear)
155         xx(dm_stripe)
156         xx(dm_interface)
157 #undef xx
158 };
159
160 static int __init dm_init(void)
161 {
162         const int count = ARRAY_SIZE(_inits);
163
164         int r, i;
165
166         for (i = 0; i < count; i++) {
167                 r = _inits[i].init();
168                 if (r)
169                         goto bad;
170         }
171
172         return 0;
173
174       bad:
175         while (i--)
176                 _inits[i].exit();
177
178         return r;
179 }
180
181 static void __exit dm_exit(void)
182 {
183         int i = ARRAY_SIZE(_inits);
184
185         while (i--)
186                 _inits[i].exit();
187 }
188
189 /*
190  * Block device functions
191  */
192 static int dm_blk_open(struct inode *inode, struct file *file)
193 {
194         struct mapped_device *md;
195
196         md = inode->i_bdev->bd_disk->private_data;
197         dm_get(md);
198         return 0;
199 }
200
201 static int dm_blk_close(struct inode *inode, struct file *file)
202 {
203         struct mapped_device *md;
204
205         md = inode->i_bdev->bd_disk->private_data;
206         dm_put(md);
207         return 0;
208 }
209
210 static inline struct dm_io *alloc_io(struct mapped_device *md)
211 {
212         return mempool_alloc(md->io_pool, GFP_NOIO);
213 }
214
215 static inline void free_io(struct mapped_device *md, struct dm_io *io)
216 {
217         mempool_free(io, md->io_pool);
218 }
219
220 static inline struct target_io *alloc_tio(struct mapped_device *md)
221 {
222         return mempool_alloc(md->tio_pool, GFP_NOIO);
223 }
224
225 static inline void free_tio(struct mapped_device *md, struct target_io *tio)
226 {
227         mempool_free(tio, md->tio_pool);
228 }
229
230 /*
231  * Add the bio to the list of deferred io.
232  */
233 static int queue_io(struct mapped_device *md, struct bio *bio)
234 {
235         down_write(&md->lock);
236
237         if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
238                 up_write(&md->lock);
239                 return 1;
240         }
241
242         bio_list_add(&md->deferred, bio);
243
244         up_write(&md->lock);
245         return 0;               /* deferred successfully */
246 }
247
248 /*
249  * Everyone (including functions in this file), should use this
250  * function to access the md->map field, and make sure they call
251  * dm_table_put() when finished.
252  */
253 struct dm_table *dm_get_table(struct mapped_device *md)
254 {
255         struct dm_table *t;
256
257         read_lock(&md->map_lock);
258         t = md->map;
259         if (t)
260                 dm_table_get(t);
261         read_unlock(&md->map_lock);
262
263         return t;
264 }
265
266 /*-----------------------------------------------------------------
267  * CRUD START:
268  *   A more elegant soln is in the works that uses the queue
269  *   merge fn, unfortunately there are a couple of changes to
270  *   the block layer that I want to make for this.  So in the
271  *   interests of getting something for people to use I give
272  *   you this clearly demarcated crap.
273  *---------------------------------------------------------------*/
274
275 /*
276  * Decrements the number of outstanding ios that a bio has been
277  * cloned into, completing the original io if necc.
278  */
279 static inline void dec_pending(struct dm_io *io, int error)
280 {
281         if (error)
282                 io->error = error;
283
284         if (atomic_dec_and_test(&io->io_count)) {
285                 if (atomic_dec_and_test(&io->md->pending))
286                         /* nudge anyone waiting on suspend queue */
287                         wake_up(&io->md->wait);
288
289                 bio_endio(io->bio, io->bio->bi_size, io->error);
290                 free_io(io->md, io);
291         }
292 }
293
294 static int clone_endio(struct bio *bio, unsigned int done, int error)
295 {
296         int r = 0;
297         struct target_io *tio = bio->bi_private;
298         struct dm_io *io = tio->io;
299         dm_endio_fn endio = tio->ti->type->end_io;
300
301         if (bio->bi_size)
302                 return 1;
303
304         if (!bio_flagged(bio, BIO_UPTODATE) && !error)
305                 error = -EIO;
306
307         if (endio) {
308                 r = endio(tio->ti, bio, error, &tio->info);
309                 if (r < 0)
310                         error = r;
311
312                 else if (r > 0)
313                         /* the target wants another shot at the io */
314                         return 1;
315         }
316
317         free_tio(io->md, tio);
318         dec_pending(io, error);
319         bio_put(bio);
320         return r;
321 }
322
323 static sector_t max_io_len(struct mapped_device *md,
324                            sector_t sector, struct dm_target *ti)
325 {
326         sector_t offset = sector - ti->begin;
327         sector_t len = ti->len - offset;
328
329         /*
330          * Does the target need to split even further ?
331          */
332         if (ti->split_io) {
333                 sector_t boundary;
334                 boundary = dm_round_up(offset + 1, ti->split_io) - offset;
335
336                 if (len > boundary)
337                         len = boundary;
338         }
339
340         return len;
341 }
342
343 static void __map_bio(struct dm_target *ti, struct bio *clone,
344                       struct target_io *tio)
345 {
346         int r;
347
348         /*
349          * Sanity checks.
350          */
351         BUG_ON(!clone->bi_size);
352
353         clone->bi_end_io = clone_endio;
354         clone->bi_private = tio;
355
356         /*
357          * Map the clone.  If r == 0 we don't need to do
358          * anything, the target has assumed ownership of
359          * this io.
360          */
361         atomic_inc(&tio->io->io_count);
362         r = ti->type->map(ti, clone, &tio->info);
363         if (r > 0)
364                 /* the bio has been remapped so dispatch it */
365                 generic_make_request(clone);
366
367         else if (r < 0) {
368                 /* error the io and bail out */
369                 struct dm_io *io = tio->io;
370                 free_tio(tio->io->md, tio);
371                 dec_pending(io, -EIO);
372         }
373 }
374
375 struct clone_info {
376         struct mapped_device *md;
377         struct dm_table *map;
378         struct bio *bio;
379         struct dm_io *io;
380         sector_t sector;
381         sector_t sector_count;
382         unsigned short idx;
383 };
384
385 /*
386  * Creates a little bio that is just does part of a bvec.
387  */
388 static struct bio *split_bvec(struct bio *bio, sector_t sector,
389                               unsigned short idx, unsigned int offset,
390                               unsigned int len)
391 {
392         struct bio *clone;
393         struct bio_vec *bv = bio->bi_io_vec + idx;
394
395         clone = bio_alloc(GFP_NOIO, 1);
396         memcpy(clone->bi_io_vec, bv, sizeof(*bv));
397
398         clone->bi_sector = sector;
399         clone->bi_bdev = bio->bi_bdev;
400         clone->bi_rw = bio->bi_rw;
401         clone->bi_vcnt = 1;
402         clone->bi_size = to_bytes(len);
403         clone->bi_io_vec->bv_offset = offset;
404         clone->bi_io_vec->bv_len = clone->bi_size;
405
406         return clone;
407 }
408
409 /*
410  * Creates a bio that consists of range of complete bvecs.
411  */
412 static struct bio *clone_bio(struct bio *bio, sector_t sector,
413                              unsigned short idx, unsigned short bv_count,
414                              unsigned int len)
415 {
416         struct bio *clone;
417
418         clone = bio_clone(bio, GFP_NOIO);
419         clone->bi_sector = sector;
420         clone->bi_idx = idx;
421         clone->bi_vcnt = idx + bv_count;
422         clone->bi_size = to_bytes(len);
423         clone->bi_flags &= ~(1 << BIO_SEG_VALID);
424
425         return clone;
426 }
427
428 static void __clone_and_map(struct clone_info *ci)
429 {
430         struct bio *clone, *bio = ci->bio;
431         struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
432         sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
433         struct target_io *tio;
434
435         /*
436          * Allocate a target io object.
437          */
438         tio = alloc_tio(ci->md);
439         tio->io = ci->io;
440         tio->ti = ti;
441         memset(&tio->info, 0, sizeof(tio->info));
442
443         if (ci->sector_count <= max) {
444                 /*
445                  * Optimise for the simple case where we can do all of
446                  * the remaining io with a single clone.
447                  */
448                 clone = clone_bio(bio, ci->sector, ci->idx,
449                                   bio->bi_vcnt - ci->idx, ci->sector_count);
450                 __map_bio(ti, clone, tio);
451                 ci->sector_count = 0;
452
453         } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
454                 /*
455                  * There are some bvecs that don't span targets.
456                  * Do as many of these as possible.
457                  */
458                 int i;
459                 sector_t remaining = max;
460                 sector_t bv_len;
461
462                 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
463                         bv_len = to_sector(bio->bi_io_vec[i].bv_len);
464
465                         if (bv_len > remaining)
466                                 break;
467
468                         remaining -= bv_len;
469                         len += bv_len;
470                 }
471
472                 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
473                 __map_bio(ti, clone, tio);
474
475                 ci->sector += len;
476                 ci->sector_count -= len;
477                 ci->idx = i;
478
479         } else {
480                 /*
481                  * Create two copy bios to deal with io that has
482                  * been split across a target.
483                  */
484                 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
485
486                 clone = split_bvec(bio, ci->sector, ci->idx,
487                                    bv->bv_offset, max);
488                 __map_bio(ti, clone, tio);
489
490                 ci->sector += max;
491                 ci->sector_count -= max;
492                 ti = dm_table_find_target(ci->map, ci->sector);
493
494                 len = to_sector(bv->bv_len) - max;
495                 clone = split_bvec(bio, ci->sector, ci->idx,
496                                    bv->bv_offset + to_bytes(max), len);
497                 tio = alloc_tio(ci->md);
498                 tio->io = ci->io;
499                 tio->ti = ti;
500                 memset(&tio->info, 0, sizeof(tio->info));
501                 __map_bio(ti, clone, tio);
502
503                 ci->sector += len;
504                 ci->sector_count -= len;
505                 ci->idx++;
506         }
507 }
508
509 /*
510  * Split the bio into several clones.
511  */
512 static void __split_bio(struct mapped_device *md, struct bio *bio)
513 {
514         struct clone_info ci;
515
516         ci.map = dm_get_table(md);
517         if (!ci.map) {
518                 bio_io_error(bio, bio->bi_size);
519                 return;
520         }
521
522         ci.md = md;
523         ci.bio = bio;
524         ci.io = alloc_io(md);
525         ci.io->error = 0;
526         atomic_set(&ci.io->io_count, 1);
527         ci.io->bio = bio;
528         ci.io->md = md;
529         ci.sector = bio->bi_sector;
530         ci.sector_count = bio_sectors(bio);
531         ci.idx = bio->bi_idx;
532
533         atomic_inc(&md->pending);
534         while (ci.sector_count)
535                 __clone_and_map(&ci);
536
537         /* drop the extra reference count */
538         dec_pending(ci.io, 0);
539         dm_table_put(ci.map);
540 }
541 /*-----------------------------------------------------------------
542  * CRUD END
543  *---------------------------------------------------------------*/
544
545 /*
546  * The request function that just remaps the bio built up by
547  * dm_merge_bvec.
548  */
549 static int dm_request(request_queue_t *q, struct bio *bio)
550 {
551         int r;
552         struct mapped_device *md = q->queuedata;
553
554         down_read(&md->lock);
555
556         /*
557          * If we're suspended we have to queue
558          * this io for later.
559          */
560         while (test_bit(DMF_BLOCK_IO, &md->flags)) {
561                 up_read(&md->lock);
562
563                 if (bio_rw(bio) == READA) {
564                         bio_io_error(bio, bio->bi_size);
565                         return 0;
566                 }
567
568                 r = queue_io(md, bio);
569                 if (r < 0) {
570                         bio_io_error(bio, bio->bi_size);
571                         return 0;
572
573                 } else if (r == 0)
574                         return 0;       /* deferred successfully */
575
576                 /*
577                  * We're in a while loop, because someone could suspend
578                  * before we get to the following read lock.
579                  */
580                 down_read(&md->lock);
581         }
582
583         __split_bio(md, bio);
584         up_read(&md->lock);
585         return 0;
586 }
587
588 static void dm_unplug_all(request_queue_t *q)
589 {
590         struct mapped_device *md = q->queuedata;
591         struct dm_table *map = dm_get_table(md);
592
593         if (map) {
594                 dm_table_unplug_all(map);
595                 dm_table_put(map);
596         }
597 }
598
599 static int dm_any_congested(void *congested_data, int bdi_bits)
600 {
601         int r;
602         struct mapped_device *md = (struct mapped_device *) congested_data;
603         struct dm_table *map = dm_get_table(md);
604
605         if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
606                 r = bdi_bits;
607         else
608                 r = dm_table_any_congested(map, bdi_bits);
609
610         dm_table_put(map);
611         return r;
612 }
613
614 /*-----------------------------------------------------------------
615  * A bitset is used to keep track of allocated minor numbers.
616  *---------------------------------------------------------------*/
617 static spinlock_t _minor_lock = SPIN_LOCK_UNLOCKED;
618 static unsigned long _minor_bits[MAX_DEVICES / BITS_PER_LONG];
619
620 static void free_minor(unsigned int minor)
621 {
622         spin_lock(&_minor_lock);
623         clear_bit(minor, _minor_bits);
624         spin_unlock(&_minor_lock);
625 }
626
627 /*
628  * See if the device with a specific minor # is free.
629  */
630 static int specific_minor(unsigned int minor)
631 {
632         int r = -EBUSY;
633
634         if (minor >= MAX_DEVICES) {
635                 DMWARN("request for a mapped_device beyond MAX_DEVICES (%d)",
636                        MAX_DEVICES);
637                 return -EINVAL;
638         }
639
640         spin_lock(&_minor_lock);
641         if (!test_and_set_bit(minor, _minor_bits))
642                 r = 0;
643         spin_unlock(&_minor_lock);
644
645         return r;
646 }
647
648 static int next_free_minor(unsigned int *minor)
649 {
650         int r = -EBUSY;
651         unsigned int m;
652
653         spin_lock(&_minor_lock);
654         m = find_first_zero_bit(_minor_bits, MAX_DEVICES);
655         if (m != MAX_DEVICES) {
656                 set_bit(m, _minor_bits);
657                 *minor = m;
658                 r = 0;
659         }
660         spin_unlock(&_minor_lock);
661
662         return r;
663 }
664
665 /*
666  * Allocate and initialise a blank device with a given minor.
667  */
668 static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
669 {
670         int r;
671         struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
672
673         if (!md) {
674                 DMWARN("unable to allocate device, out of memory.");
675                 return NULL;
676         }
677
678         /* get a minor number for the dev */
679         r = persistent ? specific_minor(minor) : next_free_minor(&minor);
680         if (r < 0)
681                 goto bad1;
682
683         memset(md, 0, sizeof(*md));
684         init_rwsem(&md->lock);
685         rwlock_init(&md->map_lock);
686         atomic_set(&md->holders, 1);
687
688         md->queue = blk_alloc_queue(GFP_KERNEL);
689         if (!md->queue)
690                 goto bad1;
691
692         md->queue->queuedata = md;
693         md->queue->backing_dev_info.congested_fn = dm_any_congested;
694         md->queue->backing_dev_info.congested_data = md;
695         blk_queue_make_request(md->queue, dm_request);
696         md->queue->unplug_fn = dm_unplug_all;
697
698         md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
699                                      mempool_free_slab, _io_cache);
700         if (!md->io_pool)
701                 goto bad2;
702
703         md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
704                                       mempool_free_slab, _tio_cache);
705         if (!md->tio_pool)
706                 goto bad3;
707
708         md->disk = alloc_disk(1);
709         if (!md->disk)
710                 goto bad4;
711
712         md->disk->major = _major;
713         md->disk->first_minor = minor;
714         md->disk->fops = &dm_blk_dops;
715         md->disk->queue = md->queue;
716         md->disk->private_data = md;
717         sprintf(md->disk->disk_name, "dm-%d", minor);
718         add_disk(md->disk);
719
720         atomic_set(&md->pending, 0);
721         init_waitqueue_head(&md->wait);
722         init_waitqueue_head(&md->eventq);
723
724         return md;
725
726  bad4:
727         mempool_destroy(md->tio_pool);
728  bad3:
729         mempool_destroy(md->io_pool);
730  bad2:
731         blk_put_queue(md->queue);
732         free_minor(minor);
733  bad1:
734         kfree(md);
735         return NULL;
736 }
737
738 static void free_dev(struct mapped_device *md)
739 {
740         free_minor(md->disk->first_minor);
741         mempool_destroy(md->tio_pool);
742         mempool_destroy(md->io_pool);
743         del_gendisk(md->disk);
744         put_disk(md->disk);
745         blk_put_queue(md->queue);
746         kfree(md);
747 }
748
749 /*
750  * Bind a table to the device.
751  */
752 static void event_callback(void *context)
753 {
754         struct mapped_device *md = (struct mapped_device *) context;
755
756         down_write(&md->lock);
757         md->event_nr++;
758         wake_up(&md->eventq);
759         up_write(&md->lock);
760 }
761
762 static void __set_size(struct gendisk *disk, sector_t size)
763 {
764         struct block_device *bdev;
765
766         set_capacity(disk, size);
767         bdev = bdget_disk(disk, 0);
768         if (bdev) {
769                 down(&bdev->bd_inode->i_sem);
770                 i_size_write(bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
771                 up(&bdev->bd_inode->i_sem);
772                 bdput(bdev);
773         }
774 }
775
776 static int __bind(struct mapped_device *md, struct dm_table *t)
777 {
778         request_queue_t *q = md->queue;
779         sector_t size;
780
781         size = dm_table_get_size(t);
782         __set_size(md->disk, size);
783         if (size == 0)
784                 return 0;
785
786         write_lock(&md->map_lock);
787         md->map = t;
788         write_unlock(&md->map_lock);
789
790         dm_table_get(t);
791         dm_table_event_callback(md->map, event_callback, md);
792         dm_table_set_restrictions(t, q);
793         return 0;
794 }
795
796 static void __unbind(struct mapped_device *md)
797 {
798         struct dm_table *map = md->map;
799
800         if (!map)
801                 return;
802
803         dm_table_event_callback(map, NULL, NULL);
804         write_lock(&md->map_lock);
805         md->map = NULL;
806         write_unlock(&md->map_lock);
807         dm_table_put(map);
808 }
809
810 /*
811  * Constructor for a new device.
812  */
813 static int create_aux(unsigned int minor, int persistent,
814                       struct mapped_device **result)
815 {
816         struct mapped_device *md;
817
818         md = alloc_dev(minor, persistent);
819         if (!md)
820                 return -ENXIO;
821
822         *result = md;
823         return 0;
824 }
825
826 int dm_create(struct mapped_device **result)
827 {
828         return create_aux(0, 0, result);
829 }
830
831 int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
832 {
833         return create_aux(minor, 1, result);
834 }
835
836 void dm_get(struct mapped_device *md)
837 {
838         atomic_inc(&md->holders);
839 }
840
841 void dm_put(struct mapped_device *md)
842 {
843         struct dm_table *map = dm_get_table(md);
844
845         if (atomic_dec_and_test(&md->holders)) {
846                 if (!test_bit(DMF_SUSPENDED, &md->flags) && map)
847                         dm_table_suspend_targets(map);
848                 __unbind(md);
849                 free_dev(md);
850         }
851
852         dm_table_put(map);
853 }
854
855 /*
856  * Process the deferred bios
857  */
858 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
859 {
860         struct bio *n;
861
862         while (c) {
863                 n = c->bi_next;
864                 c->bi_next = NULL;
865                 __split_bio(md, c);
866                 c = n;
867         }
868 }
869
870 /*
871  * Swap in a new table (destroying old one).
872  */
873 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
874 {
875         int r;
876
877         down_write(&md->lock);
878
879         /* device must be suspended */
880         if (!test_bit(DMF_SUSPENDED, &md->flags)) {
881                 up_write(&md->lock);
882                 return -EPERM;
883         }
884
885         __unbind(md);
886         r = __bind(md, table);
887         if (r)
888                 return r;
889
890         up_write(&md->lock);
891         return 0;
892 }
893
894 /*
895  * Functions to lock and unlock any filesystem running on the
896  * device.
897  */
898 static int __lock_fs(struct mapped_device *md)
899 {
900         struct block_device *bdev;
901
902         if (test_and_set_bit(DMF_FS_LOCKED, &md->flags))
903                 return 0;
904
905         bdev = bdget_disk(md->disk, 0);
906         if (!bdev) {
907                 DMWARN("bdget failed in __lock_fs");
908                 return -ENOMEM;
909         }
910
911         WARN_ON(md->frozen_sb);
912         md->frozen_sb = freeze_bdev(bdev);
913         /* don't bdput right now, we don't want the bdev
914          * to go away while it is locked.  We'll bdput
915          * in __unlock_fs
916          */
917         return 0;
918 }
919
920 static int __unlock_fs(struct mapped_device *md)
921 {
922         struct block_device *bdev;
923
924         if (!test_and_clear_bit(DMF_FS_LOCKED, &md->flags))
925                 return 0;
926
927         bdev = bdget_disk(md->disk, 0);
928         if (!bdev) {
929                 DMWARN("bdget failed in __unlock_fs");
930                 return -ENOMEM;
931         }
932
933         thaw_bdev(bdev, md->frozen_sb);
934         md->frozen_sb = NULL;
935         bdput(bdev);
936         bdput(bdev);
937         return 0;
938 }
939
940 /*
941  * We need to be able to change a mapping table under a mounted
942  * filesystem.  For example we might want to move some data in
943  * the background.  Before the table can be swapped with
944  * dm_bind_table, dm_suspend must be called to flush any in
945  * flight bios and ensure that any further io gets deferred.
946  */
947 int dm_suspend(struct mapped_device *md)
948 {
949         struct dm_table *map;
950         DECLARE_WAITQUEUE(wait, current);
951
952         /* Flush I/O to the device. */
953         down_read(&md->lock);
954         if (test_bit(DMF_BLOCK_IO, &md->flags)) {
955                 up_read(&md->lock);
956                 return -EINVAL;
957         }
958
959         __lock_fs(md);
960         up_read(&md->lock);
961
962         /*
963          * First we set the BLOCK_IO flag so no more ios will be
964          * mapped.
965          */
966         down_write(&md->lock);
967         if (test_bit(DMF_BLOCK_IO, &md->flags)) {
968                 /*
969                  * If we get here we know another thread is
970                  * trying to suspend as well, so we leave the fs
971                  * locked for this thread.
972                  */
973                 up_write(&md->lock);
974                 return -EINVAL;
975         }
976
977         set_bit(DMF_BLOCK_IO, &md->flags);
978         add_wait_queue(&md->wait, &wait);
979         up_write(&md->lock);
980
981         /* unplug */
982         map = dm_get_table(md);
983         if (map) {
984                 dm_table_unplug_all(map);
985                 dm_table_put(map);
986         }
987
988         /*
989          * Then we wait for the already mapped ios to
990          * complete.
991          */
992         while (1) {
993                 set_current_state(TASK_INTERRUPTIBLE);
994
995                 if (!atomic_read(&md->pending) || signal_pending(current))
996                         break;
997
998                 io_schedule();
999         }
1000         set_current_state(TASK_RUNNING);
1001
1002         down_write(&md->lock);
1003         remove_wait_queue(&md->wait, &wait);
1004
1005         /* were we interrupted ? */
1006         if (atomic_read(&md->pending)) {
1007                 __unlock_fs(md);
1008                 clear_bit(DMF_BLOCK_IO, &md->flags);
1009                 up_write(&md->lock);
1010                 return -EINTR;
1011         }
1012
1013         set_bit(DMF_SUSPENDED, &md->flags);
1014
1015         map = dm_get_table(md);
1016         if (map)
1017                 dm_table_suspend_targets(map);
1018         dm_table_put(map);
1019         up_write(&md->lock);
1020
1021         return 0;
1022 }
1023
1024 int dm_resume(struct mapped_device *md)
1025 {
1026         struct bio *def;
1027         struct dm_table *map = dm_get_table(md);
1028
1029         down_write(&md->lock);
1030         if (!map ||
1031             !test_bit(DMF_SUSPENDED, &md->flags) ||
1032             !dm_table_get_size(map)) {
1033                 up_write(&md->lock);
1034                 dm_table_put(map);
1035                 return -EINVAL;
1036         }
1037
1038         dm_table_resume_targets(map);
1039         clear_bit(DMF_SUSPENDED, &md->flags);
1040         clear_bit(DMF_BLOCK_IO, &md->flags);
1041
1042         def = bio_list_get(&md->deferred);
1043         __flush_deferred_io(md, def);
1044         up_write(&md->lock);
1045         __unlock_fs(md);
1046         dm_table_unplug_all(map);
1047         dm_table_put(map);
1048
1049         return 0;
1050 }
1051
1052 /*-----------------------------------------------------------------
1053  * Event notification.
1054  *---------------------------------------------------------------*/
1055 uint32_t dm_get_event_nr(struct mapped_device *md)
1056 {
1057         uint32_t r;
1058
1059         down_read(&md->lock);
1060         r = md->event_nr;
1061         up_read(&md->lock);
1062
1063         return r;
1064 }
1065
1066 int dm_add_wait_queue(struct mapped_device *md, wait_queue_t *wq,
1067                       uint32_t event_nr)
1068 {
1069         down_write(&md->lock);
1070         if (event_nr != md->event_nr) {
1071                 up_write(&md->lock);
1072                 return 1;
1073         }
1074
1075         add_wait_queue(&md->eventq, wq);
1076         up_write(&md->lock);
1077
1078         return 0;
1079 }
1080
1081 void dm_remove_wait_queue(struct mapped_device *md, wait_queue_t *wq)
1082 {
1083         down_write(&md->lock);
1084         remove_wait_queue(&md->eventq, wq);
1085         up_write(&md->lock);
1086 }
1087
1088 /*
1089  * The gendisk is only valid as long as you have a reference
1090  * count on 'md'.
1091  */
1092 struct gendisk *dm_disk(struct mapped_device *md)
1093 {
1094         return md->disk;
1095 }
1096
1097 int dm_suspended(struct mapped_device *md)
1098 {
1099         return test_bit(DMF_SUSPENDED, &md->flags);
1100 }
1101
1102 struct block_device_operations dm_blk_dops = {
1103         .open = dm_blk_open,
1104         .release = dm_blk_close,
1105         .owner = THIS_MODULE
1106 };
1107
1108 /*
1109  * module hooks
1110  */
1111 module_init(dm_init);
1112 module_exit(dm_exit);
1113
1114 module_param(major, uint, 0);
1115 MODULE_PARM_DESC(major, "The major number of the device mapper");
1116 MODULE_DESCRIPTION(DM_NAME " driver");
1117 MODULE_AUTHOR("Joe Thornber <thornber@sistina.com>");
1118 MODULE_LICENSE("GPL");