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