Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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