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