patch-2_6_7-vs1_9_1_12
[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         atomic_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 int __init 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                 bio_put(clone);
373         }
374 }
375
376 struct clone_info {
377         struct mapped_device *md;
378         struct dm_table *map;
379         struct bio *bio;
380         struct dm_io *io;
381         sector_t sector;
382         sector_t sector_count;
383         unsigned short idx;
384 };
385
386 /*
387  * Creates a little bio that is just does part of a bvec.
388  */
389 static struct bio *split_bvec(struct bio *bio, sector_t sector,
390                               unsigned short idx, unsigned int offset,
391                               unsigned int len)
392 {
393         struct bio *clone;
394         struct bio_vec *bv = bio->bi_io_vec + idx;
395
396         clone = bio_alloc(GFP_NOIO, 1);
397         memcpy(clone->bi_io_vec, bv, sizeof(*bv));
398
399         clone->bi_sector = sector;
400         clone->bi_bdev = bio->bi_bdev;
401         clone->bi_rw = bio->bi_rw;
402         clone->bi_vcnt = 1;
403         clone->bi_size = to_bytes(len);
404         clone->bi_io_vec->bv_offset = offset;
405         clone->bi_io_vec->bv_len = clone->bi_size;
406
407         return clone;
408 }
409
410 /*
411  * Creates a bio that consists of range of complete bvecs.
412  */
413 static struct bio *clone_bio(struct bio *bio, sector_t sector,
414                              unsigned short idx, unsigned short bv_count,
415                              unsigned int len)
416 {
417         struct bio *clone;
418
419         clone = bio_clone(bio, GFP_NOIO);
420         clone->bi_sector = sector;
421         clone->bi_idx = idx;
422         clone->bi_vcnt = idx + bv_count;
423         clone->bi_size = to_bytes(len);
424         clone->bi_flags &= ~(1 << BIO_SEG_VALID);
425
426         return clone;
427 }
428
429 static void __clone_and_map(struct clone_info *ci)
430 {
431         struct bio *clone, *bio = ci->bio;
432         struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
433         sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
434         struct target_io *tio;
435
436         /*
437          * Allocate a target io object.
438          */
439         tio = alloc_tio(ci->md);
440         tio->io = ci->io;
441         tio->ti = ti;
442         memset(&tio->info, 0, sizeof(tio->info));
443
444         if (ci->sector_count <= max) {
445                 /*
446                  * Optimise for the simple case where we can do all of
447                  * the remaining io with a single clone.
448                  */
449                 clone = clone_bio(bio, ci->sector, ci->idx,
450                                   bio->bi_vcnt - ci->idx, ci->sector_count);
451                 __map_bio(ti, clone, tio);
452                 ci->sector_count = 0;
453
454         } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
455                 /*
456                  * There are some bvecs that don't span targets.
457                  * Do as many of these as possible.
458                  */
459                 int i;
460                 sector_t remaining = max;
461                 sector_t bv_len;
462
463                 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
464                         bv_len = to_sector(bio->bi_io_vec[i].bv_len);
465
466                         if (bv_len > remaining)
467                                 break;
468
469                         remaining -= bv_len;
470                         len += bv_len;
471                 }
472
473                 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
474                 __map_bio(ti, clone, tio);
475
476                 ci->sector += len;
477                 ci->sector_count -= len;
478                 ci->idx = i;
479
480         } else {
481                 /*
482                  * Create two copy bios to deal with io that has
483                  * been split across a target.
484                  */
485                 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
486
487                 clone = split_bvec(bio, ci->sector, ci->idx,
488                                    bv->bv_offset, max);
489                 __map_bio(ti, clone, tio);
490
491                 ci->sector += max;
492                 ci->sector_count -= max;
493                 ti = dm_table_find_target(ci->map, ci->sector);
494
495                 len = to_sector(bv->bv_len) - max;
496                 clone = split_bvec(bio, ci->sector, ci->idx,
497                                    bv->bv_offset + to_bytes(max), len);
498                 tio = alloc_tio(ci->md);
499                 tio->io = ci->io;
500                 tio->ti = ti;
501                 memset(&tio->info, 0, sizeof(tio->info));
502                 __map_bio(ti, clone, tio);
503
504                 ci->sector += len;
505                 ci->sector_count -= len;
506                 ci->idx++;
507         }
508 }
509
510 /*
511  * Split the bio into several clones.
512  */
513 static void __split_bio(struct mapped_device *md, struct bio *bio)
514 {
515         struct clone_info ci;
516
517         ci.map = dm_get_table(md);
518         if (!ci.map) {
519                 bio_io_error(bio, bio->bi_size);
520                 return;
521         }
522
523         ci.md = md;
524         ci.bio = bio;
525         ci.io = alloc_io(md);
526         ci.io->error = 0;
527         atomic_set(&ci.io->io_count, 1);
528         ci.io->bio = bio;
529         ci.io->md = md;
530         ci.sector = bio->bi_sector;
531         ci.sector_count = bio_sectors(bio);
532         ci.idx = bio->bi_idx;
533
534         atomic_inc(&md->pending);
535         while (ci.sector_count)
536                 __clone_and_map(&ci);
537
538         /* drop the extra reference count */
539         dec_pending(ci.io, 0);
540         dm_table_put(ci.map);
541 }
542 /*-----------------------------------------------------------------
543  * CRUD END
544  *---------------------------------------------------------------*/
545
546 /*
547  * The request function that just remaps the bio built up by
548  * dm_merge_bvec.
549  */
550 static int dm_request(request_queue_t *q, struct bio *bio)
551 {
552         int r;
553         struct mapped_device *md = q->queuedata;
554
555         down_read(&md->lock);
556
557         /*
558          * If we're suspended we have to queue
559          * this io for later.
560          */
561         while (test_bit(DMF_BLOCK_IO, &md->flags)) {
562                 up_read(&md->lock);
563
564                 if (bio_rw(bio) == READA) {
565                         bio_io_error(bio, bio->bi_size);
566                         return 0;
567                 }
568
569                 r = queue_io(md, bio);
570                 if (r < 0) {
571                         bio_io_error(bio, bio->bi_size);
572                         return 0;
573
574                 } else if (r == 0)
575                         return 0;       /* deferred successfully */
576
577                 /*
578                  * We're in a while loop, because someone could suspend
579                  * before we get to the following read lock.
580                  */
581                 down_read(&md->lock);
582         }
583
584         __split_bio(md, bio);
585         up_read(&md->lock);
586         return 0;
587 }
588
589 static void dm_unplug_all(request_queue_t *q)
590 {
591         struct mapped_device *md = q->queuedata;
592         struct dm_table *map = dm_get_table(md);
593
594         if (map) {
595                 dm_table_unplug_all(map);
596                 dm_table_put(map);
597         }
598 }
599
600 static int dm_any_congested(void *congested_data, int bdi_bits)
601 {
602         int r;
603         struct mapped_device *md = (struct mapped_device *) congested_data;
604         struct dm_table *map = dm_get_table(md);
605
606         if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
607                 r = bdi_bits;
608         else
609                 r = dm_table_any_congested(map, bdi_bits);
610
611         dm_table_put(map);
612         return r;
613 }
614
615 /*-----------------------------------------------------------------
616  * A bitset is used to keep track of allocated minor numbers.
617  *---------------------------------------------------------------*/
618 static spinlock_t _minor_lock = SPIN_LOCK_UNLOCKED;
619 static unsigned long _minor_bits[MAX_DEVICES / BITS_PER_LONG];
620
621 static void free_minor(unsigned int minor)
622 {
623         spin_lock(&_minor_lock);
624         clear_bit(minor, _minor_bits);
625         spin_unlock(&_minor_lock);
626 }
627
628 /*
629  * See if the device with a specific minor # is free.
630  */
631 static int specific_minor(unsigned int minor)
632 {
633         int r = -EBUSY;
634
635         if (minor >= MAX_DEVICES) {
636                 DMWARN("request for a mapped_device beyond MAX_DEVICES (%d)",
637                        MAX_DEVICES);
638                 return -EINVAL;
639         }
640
641         spin_lock(&_minor_lock);
642         if (!test_and_set_bit(minor, _minor_bits))
643                 r = 0;
644         spin_unlock(&_minor_lock);
645
646         return r;
647 }
648
649 static int next_free_minor(unsigned int *minor)
650 {
651         int r = -EBUSY;
652         unsigned int m;
653
654         spin_lock(&_minor_lock);
655         m = find_first_zero_bit(_minor_bits, MAX_DEVICES);
656         if (m != MAX_DEVICES) {
657                 set_bit(m, _minor_bits);
658                 *minor = m;
659                 r = 0;
660         }
661         spin_unlock(&_minor_lock);
662
663         return r;
664 }
665
666 static struct block_device_operations dm_blk_dops;
667
668 /*
669  * Allocate and initialise a blank device with a given minor.
670  */
671 static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
672 {
673         int r;
674         struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
675
676         if (!md) {
677                 DMWARN("unable to allocate device, out of memory.");
678                 return NULL;
679         }
680
681         /* get a minor number for the dev */
682         r = persistent ? specific_minor(minor) : next_free_minor(&minor);
683         if (r < 0)
684                 goto bad1;
685
686         memset(md, 0, sizeof(*md));
687         init_rwsem(&md->lock);
688         rwlock_init(&md->map_lock);
689         atomic_set(&md->holders, 1);
690         atomic_set(&md->event_nr, 0);
691
692         md->queue = blk_alloc_queue(GFP_KERNEL);
693         if (!md->queue)
694                 goto bad1;
695
696         md->queue->queuedata = md;
697         md->queue->backing_dev_info.congested_fn = dm_any_congested;
698         md->queue->backing_dev_info.congested_data = md;
699         blk_queue_make_request(md->queue, dm_request);
700         md->queue->unplug_fn = dm_unplug_all;
701
702         md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
703                                      mempool_free_slab, _io_cache);
704         if (!md->io_pool)
705                 goto bad2;
706
707         md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
708                                       mempool_free_slab, _tio_cache);
709         if (!md->tio_pool)
710                 goto bad3;
711
712         md->disk = alloc_disk(1);
713         if (!md->disk)
714                 goto bad4;
715
716         md->disk->major = _major;
717         md->disk->first_minor = minor;
718         md->disk->fops = &dm_blk_dops;
719         md->disk->queue = md->queue;
720         md->disk->private_data = md;
721         sprintf(md->disk->disk_name, "dm-%d", minor);
722         add_disk(md->disk);
723
724         atomic_set(&md->pending, 0);
725         init_waitqueue_head(&md->wait);
726         init_waitqueue_head(&md->eventq);
727
728         return md;
729
730  bad4:
731         mempool_destroy(md->tio_pool);
732  bad3:
733         mempool_destroy(md->io_pool);
734  bad2:
735         blk_put_queue(md->queue);
736         free_minor(minor);
737  bad1:
738         kfree(md);
739         return NULL;
740 }
741
742 static void free_dev(struct mapped_device *md)
743 {
744         free_minor(md->disk->first_minor);
745         mempool_destroy(md->tio_pool);
746         mempool_destroy(md->io_pool);
747         del_gendisk(md->disk);
748         put_disk(md->disk);
749         blk_put_queue(md->queue);
750         kfree(md);
751 }
752
753 /*
754  * Bind a table to the device.
755  */
756 static void event_callback(void *context)
757 {
758         struct mapped_device *md = (struct mapped_device *) context;
759
760         atomic_inc(&md->event_nr);;
761         wake_up(&md->eventq);
762 }
763
764 static void __set_size(struct gendisk *disk, sector_t size)
765 {
766         struct block_device *bdev;
767
768         set_capacity(disk, size);
769         bdev = bdget_disk(disk, 0);
770         if (bdev) {
771                 down(&bdev->bd_inode->i_sem);
772                 i_size_write(bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
773                 up(&bdev->bd_inode->i_sem);
774                 bdput(bdev);
775         }
776 }
777
778 static int __bind(struct mapped_device *md, struct dm_table *t)
779 {
780         request_queue_t *q = md->queue;
781         sector_t size;
782
783         size = dm_table_get_size(t);
784         __set_size(md->disk, size);
785         if (size == 0)
786                 return 0;
787
788         write_lock(&md->map_lock);
789         md->map = t;
790         write_unlock(&md->map_lock);
791
792         dm_table_get(t);
793         dm_table_event_callback(md->map, event_callback, md);
794         dm_table_set_restrictions(t, q);
795         return 0;
796 }
797
798 static void __unbind(struct mapped_device *md)
799 {
800         struct dm_table *map = md->map;
801
802         if (!map)
803                 return;
804
805         dm_table_event_callback(map, NULL, NULL);
806         write_lock(&md->map_lock);
807         md->map = NULL;
808         write_unlock(&md->map_lock);
809         dm_table_put(map);
810 }
811
812 /*
813  * Constructor for a new device.
814  */
815 static int create_aux(unsigned int minor, int persistent,
816                       struct mapped_device **result)
817 {
818         struct mapped_device *md;
819
820         md = alloc_dev(minor, persistent);
821         if (!md)
822                 return -ENXIO;
823
824         *result = md;
825         return 0;
826 }
827
828 int dm_create(struct mapped_device **result)
829 {
830         return create_aux(0, 0, result);
831 }
832
833 int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
834 {
835         return create_aux(minor, 1, result);
836 }
837
838 void dm_get(struct mapped_device *md)
839 {
840         atomic_inc(&md->holders);
841 }
842
843 void dm_put(struct mapped_device *md)
844 {
845         struct dm_table *map = dm_get_table(md);
846
847         if (atomic_dec_and_test(&md->holders)) {
848                 if (!test_bit(DMF_SUSPENDED, &md->flags) && map)
849                         dm_table_suspend_targets(map);
850                 __unbind(md);
851                 free_dev(md);
852         }
853
854         dm_table_put(map);
855 }
856
857 /*
858  * Process the deferred bios
859  */
860 static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
861 {
862         struct bio *n;
863
864         while (c) {
865                 n = c->bi_next;
866                 c->bi_next = NULL;
867                 __split_bio(md, c);
868                 c = n;
869         }
870 }
871
872 /*
873  * Swap in a new table (destroying old one).
874  */
875 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
876 {
877         int r;
878
879         down_write(&md->lock);
880
881         /* device must be suspended */
882         if (!test_bit(DMF_SUSPENDED, &md->flags)) {
883                 up_write(&md->lock);
884                 return -EPERM;
885         }
886
887         __unbind(md);
888         r = __bind(md, table);
889         if (r)
890                 return r;
891
892         up_write(&md->lock);
893         return 0;
894 }
895
896 /*
897  * Functions to lock and unlock any filesystem running on the
898  * device.
899  */
900 static int __lock_fs(struct mapped_device *md)
901 {
902         struct block_device *bdev;
903
904         if (test_and_set_bit(DMF_FS_LOCKED, &md->flags))
905                 return 0;
906
907         bdev = bdget_disk(md->disk, 0);
908         if (!bdev) {
909                 DMWARN("bdget failed in __lock_fs");
910                 return -ENOMEM;
911         }
912
913         WARN_ON(md->frozen_sb);
914         md->frozen_sb = freeze_bdev(bdev);
915         /* don't bdput right now, we don't want the bdev
916          * to go away while it is locked.  We'll bdput
917          * in __unlock_fs
918          */
919         return 0;
920 }
921
922 static int __unlock_fs(struct mapped_device *md)
923 {
924         struct block_device *bdev;
925
926         if (!test_and_clear_bit(DMF_FS_LOCKED, &md->flags))
927                 return 0;
928
929         bdev = bdget_disk(md->disk, 0);
930         if (!bdev) {
931                 DMWARN("bdget failed in __unlock_fs");
932                 return -ENOMEM;
933         }
934
935         thaw_bdev(bdev, md->frozen_sb);
936         md->frozen_sb = NULL;
937         bdput(bdev);
938         bdput(bdev);
939         return 0;
940 }
941
942 /*
943  * We need to be able to change a mapping table under a mounted
944  * filesystem.  For example we might want to move some data in
945  * the background.  Before the table can be swapped with
946  * dm_bind_table, dm_suspend must be called to flush any in
947  * flight bios and ensure that any further io gets deferred.
948  */
949 int dm_suspend(struct mapped_device *md)
950 {
951         struct dm_table *map;
952         DECLARE_WAITQUEUE(wait, current);
953
954         /* Flush I/O to the device. */
955         down_read(&md->lock);
956         if (test_bit(DMF_BLOCK_IO, &md->flags)) {
957                 up_read(&md->lock);
958                 return -EINVAL;
959         }
960
961         __lock_fs(md);
962         up_read(&md->lock);
963
964         /*
965          * First we set the BLOCK_IO flag so no more ios will be
966          * mapped.
967          */
968         down_write(&md->lock);
969         if (test_bit(DMF_BLOCK_IO, &md->flags)) {
970                 /*
971                  * If we get here we know another thread is
972                  * trying to suspend as well, so we leave the fs
973                  * locked for this thread.
974                  */
975                 up_write(&md->lock);
976                 return -EINVAL;
977         }
978
979         set_bit(DMF_BLOCK_IO, &md->flags);
980         add_wait_queue(&md->wait, &wait);
981         up_write(&md->lock);
982
983         /* unplug */
984         map = dm_get_table(md);
985         if (map) {
986                 dm_table_unplug_all(map);
987                 dm_table_put(map);
988         }
989
990         /*
991          * Then we wait for the already mapped ios to
992          * complete.
993          */
994         while (1) {
995                 set_current_state(TASK_INTERRUPTIBLE);
996
997                 if (!atomic_read(&md->pending) || signal_pending(current))
998                         break;
999
1000                 io_schedule();
1001         }
1002         set_current_state(TASK_RUNNING);
1003
1004         down_write(&md->lock);
1005         remove_wait_queue(&md->wait, &wait);
1006
1007         /* were we interrupted ? */
1008         if (atomic_read(&md->pending)) {
1009                 __unlock_fs(md);
1010                 clear_bit(DMF_BLOCK_IO, &md->flags);
1011                 up_write(&md->lock);
1012                 return -EINTR;
1013         }
1014
1015         set_bit(DMF_SUSPENDED, &md->flags);
1016
1017         map = dm_get_table(md);
1018         if (map)
1019                 dm_table_suspend_targets(map);
1020         dm_table_put(map);
1021         up_write(&md->lock);
1022
1023         return 0;
1024 }
1025
1026 int dm_resume(struct mapped_device *md)
1027 {
1028         struct bio *def;
1029         struct dm_table *map = dm_get_table(md);
1030
1031         down_write(&md->lock);
1032         if (!map ||
1033             !test_bit(DMF_SUSPENDED, &md->flags) ||
1034             !dm_table_get_size(map)) {
1035                 up_write(&md->lock);
1036                 dm_table_put(map);
1037                 return -EINVAL;
1038         }
1039
1040         dm_table_resume_targets(map);
1041         clear_bit(DMF_SUSPENDED, &md->flags);
1042         clear_bit(DMF_BLOCK_IO, &md->flags);
1043
1044         def = bio_list_get(&md->deferred);
1045         __flush_deferred_io(md, def);
1046         up_write(&md->lock);
1047         __unlock_fs(md);
1048         dm_table_unplug_all(map);
1049         dm_table_put(map);
1050
1051         return 0;
1052 }
1053
1054 /*-----------------------------------------------------------------
1055  * Event notification.
1056  *---------------------------------------------------------------*/
1057 uint32_t dm_get_event_nr(struct mapped_device *md)
1058 {
1059         return atomic_read(&md->event_nr);
1060 }
1061
1062 int dm_wait_event(struct mapped_device *md, int event_nr)
1063 {
1064         return wait_event_interruptible(md->eventq,
1065                         (event_nr != atomic_read(&md->event_nr)));
1066 }
1067
1068 /*
1069  * The gendisk is only valid as long as you have a reference
1070  * count on 'md'.
1071  */
1072 struct gendisk *dm_disk(struct mapped_device *md)
1073 {
1074         return md->disk;
1075 }
1076
1077 int dm_suspended(struct mapped_device *md)
1078 {
1079         return test_bit(DMF_SUSPENDED, &md->flags);
1080 }
1081
1082 static struct block_device_operations dm_blk_dops = {
1083         .open = dm_blk_open,
1084         .release = dm_blk_close,
1085         .owner = THIS_MODULE
1086 };
1087
1088 /*
1089  * module hooks
1090  */
1091 module_init(dm_init);
1092 module_exit(dm_exit);
1093
1094 module_param(major, uint, 0);
1095 MODULE_PARM_DESC(major, "The major number of the device mapper");
1096 MODULE_DESCRIPTION(DM_NAME " driver");
1097 MODULE_AUTHOR("Joe Thornber <thornber@sistina.com>");
1098 MODULE_LICENSE("GPL");