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