]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - fs/bio-integrity.c
ocfs2: Use nd_set_link().
[linux-2.6.git] / fs / bio-integrity.c
index c3e174b35fe6a179baac8ef098b465f0ab51f515..31c46a241bac6838de2be1af7a9f452d58b9affb 100644 (file)
 #include <linux/workqueue.h>
 
 static struct kmem_cache *bio_integrity_slab __read_mostly;
+static mempool_t *bio_integrity_pool;
+static struct bio_set *integrity_bio_set;
 static struct workqueue_struct *kintegrityd_wq;
 
 /**
- * bio_integrity_alloc_bioset - Allocate integrity payload and attach it to bio
+ * bio_integrity_alloc - Allocate integrity payload and attach it to bio
  * @bio:       bio to attach integrity metadata to
  * @gfp_mask:  Memory allocation mask
  * @nr_vecs:   Number of integrity metadata scatter-gather elements
- * @bs:                bio_set to allocate from
  *
  * Description: This function prepares a bio for attaching integrity
  * metadata.  nr_vecs specifies the maximum number of pages containing
  * integrity metadata that can be attached.
  */
-struct bio_integrity_payload *bio_integrity_alloc_bioset(struct bio *bio,
-                                                        gfp_t gfp_mask,
-                                                        unsigned int nr_vecs,
-                                                        struct bio_set *bs)
+struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
+                                                 gfp_t gfp_mask,
+                                                 unsigned int nr_vecs)
 {
        struct bio_integrity_payload *bip;
        struct bio_vec *iv;
@@ -50,7 +50,7 @@ struct bio_integrity_payload *bio_integrity_alloc_bioset(struct bio *bio,
 
        BUG_ON(bio == NULL);
 
-       bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
+       bip = mempool_alloc(bio_integrity_pool, gfp_mask);
        if (unlikely(bip == NULL)) {
                printk(KERN_ERR "%s: could not alloc bip\n", __func__);
                return NULL;
@@ -58,10 +58,10 @@ struct bio_integrity_payload *bio_integrity_alloc_bioset(struct bio *bio,
 
        memset(bip, 0, sizeof(*bip));
 
-       iv = bvec_alloc_bs(gfp_mask, nr_vecs, &idx, bs);
+       iv = bvec_alloc_bs(gfp_mask, nr_vecs, &idx, integrity_bio_set);
        if (unlikely(iv == NULL)) {
                printk(KERN_ERR "%s: could not alloc bip_vec\n", __func__);
-               mempool_free(bip, bs->bio_integrity_pool);
+               mempool_free(bip, bio_integrity_pool);
                return NULL;
        }
 
@@ -72,46 +72,28 @@ struct bio_integrity_payload *bio_integrity_alloc_bioset(struct bio *bio,
 
        return bip;
 }
-EXPORT_SYMBOL(bio_integrity_alloc_bioset);
-
-/**
- * bio_integrity_alloc - Allocate integrity payload and attach it to bio
- * @bio:       bio to attach integrity metadata to
- * @gfp_mask:  Memory allocation mask
- * @nr_vecs:   Number of integrity metadata scatter-gather elements
- *
- * Description: This function prepares a bio for attaching integrity
- * metadata.  nr_vecs specifies the maximum number of pages containing
- * integrity metadata that can be attached.
- */
-struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
-                                                 gfp_t gfp_mask,
-                                                 unsigned int nr_vecs)
-{
-       return bio_integrity_alloc_bioset(bio, gfp_mask, nr_vecs, fs_bio_set);
-}
 EXPORT_SYMBOL(bio_integrity_alloc);
 
 /**
  * bio_integrity_free - Free bio integrity payload
  * @bio:       bio containing bip to be freed
- * @bs:                bio_set this bio was allocated from
  *
  * Description: Used to free the integrity portion of a bio. Usually
  * called from bio_free().
  */
-void bio_integrity_free(struct bio *bio, struct bio_set *bs)
+void bio_integrity_free(struct bio *bio)
 {
        struct bio_integrity_payload *bip = bio->bi_integrity;
 
        BUG_ON(bip == NULL);
 
        /* A cloned bio doesn't own the integrity metadata */
-       if (!bio_flagged(bio, BIO_CLONED) && bip->bip_buf != NULL)
+       if (!bio_flagged(bio, BIO_CLONED) && !bio_flagged(bio, BIO_FS_INTEGRITY)
+           && bip->bip_buf != NULL)
                kfree(bip->bip_buf);
 
-       mempool_free(bip->bip_vec, bs->bvec_pools[bip->bip_pool]);
-       mempool_free(bip, bs->bio_integrity_pool);
+       bvec_free_bs(integrity_bio_set, bip->bip_vec, bip->bip_pool);
+       mempool_free(bip, bio_integrity_pool);
 
        bio->bi_integrity = NULL;
 }
@@ -139,7 +121,6 @@ int bio_integrity_add_page(struct bio *bio, struct page *page,
 
        iv = bip_vec_idx(bip, bip->bip_vcnt);
        BUG_ON(iv == NULL);
-       BUG_ON(iv->bv_page != NULL);
 
        iv->bv_page = page;
        iv->bv_len = len;
@@ -150,6 +131,24 @@ int bio_integrity_add_page(struct bio *bio, struct page *page,
 }
 EXPORT_SYMBOL(bio_integrity_add_page);
 
+static int bdev_integrity_enabled(struct block_device *bdev, int rw)
+{
+       struct blk_integrity *bi = bdev_get_integrity(bdev);
+
+       if (bi == NULL)
+               return 0;
+
+       if (rw == READ && bi->verify_fn != NULL &&
+           (bi->flags & INTEGRITY_FLAG_READ))
+               return 1;
+
+       if (rw == WRITE && bi->generate_fn != NULL &&
+           (bi->flags & INTEGRITY_FLAG_WRITE))
+               return 1;
+
+       return 0;
+}
+
 /**
  * bio_integrity_enabled - Check whether integrity can be passed
  * @bio:       bio to check
@@ -313,6 +312,14 @@ static void bio_integrity_generate(struct bio *bio)
        }
 }
 
+static inline unsigned short blk_integrity_tuple_size(struct blk_integrity *bi)
+{
+       if (bi)
+               return bi->tuple_size;
+
+       return 0;
+}
+
 /**
  * bio_integrity_prep - Prepare bio for integrity I/O
  * @bio:       bio to prepare
@@ -438,7 +445,7 @@ static int bio_integrity_verify(struct bio *bio)
 
                if (ret) {
                        kunmap_atomic(kaddr, KM_USER0);
-                       break;
+                       return ret;
                }
 
                sectors = bv->bv_len / bi->sector_size;
@@ -466,18 +473,13 @@ static void bio_integrity_verify_fn(struct work_struct *work)
        struct bio_integrity_payload *bip =
                container_of(work, struct bio_integrity_payload, bip_work);
        struct bio *bio = bip->bip_bio;
-       int error = bip->bip_error;
+       int error;
 
-       if (bio_integrity_verify(bio)) {
-               clear_bit(BIO_UPTODATE, &bio->bi_flags);
-               error = -EIO;
-       }
+       error = bio_integrity_verify(bio);
 
        /* Restore original bio completion handler */
        bio->bi_end_io = bip->bip_end_io;
-
-       if (bio->bi_end_io)
-               bio->bi_end_io(bio, error);
+       bio_endio(bio, error);
 }
 
 /**
@@ -498,7 +500,17 @@ void bio_integrity_endio(struct bio *bio, int error)
 
        BUG_ON(bip->bip_bio != bio);
 
-       bip->bip_error = error;
+       /* In case of an I/O error there is no point in verifying the
+        * integrity metadata.  Restore original bio end_io handler
+        * and run it.
+        */
+       if (error) {
+               bio->bi_end_io = bip->bip_end_io;
+               bio_endio(bio, error);
+
+               return;
+       }
+
        INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
        queue_work(kintegrityd_wq, &bip->bip_work);
 }
@@ -654,19 +666,18 @@ EXPORT_SYMBOL(bio_integrity_split);
  * bio_integrity_clone - Callback for cloning bios with integrity metadata
  * @bio:       New bio
  * @bio_src:   Original bio
- * @bs:                bio_set to allocate bip from
+ * @gfp_mask:  Memory allocation mask
  *
  * Description:        Called to allocate a bip when cloning a bio
  */
-int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
-                       struct bio_set *bs)
+int bio_integrity_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp_mask)
 {
        struct bio_integrity_payload *bip_src = bio_src->bi_integrity;
        struct bio_integrity_payload *bip;
 
        BUG_ON(bip_src == NULL);
 
-       bip = bio_integrity_alloc_bioset(bio, GFP_NOIO, bip_src->bip_vcnt, bs);
+       bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
 
        if (bip == NULL)
                return -EIO;
@@ -682,37 +693,25 @@ int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
 }
 EXPORT_SYMBOL(bio_integrity_clone);
 
-int bioset_integrity_create(struct bio_set *bs, int pool_size)
+static int __init bio_integrity_init(void)
 {
-       bs->bio_integrity_pool = mempool_create_slab_pool(pool_size,
-                                                         bio_integrity_slab);
-       if (!bs->bio_integrity_pool)
-               return -1;
-
-       return 0;
-}
-EXPORT_SYMBOL(bioset_integrity_create);
+       kintegrityd_wq = create_workqueue("kintegrityd");
 
-void bioset_integrity_free(struct bio_set *bs)
-{
-       if (bs->bio_integrity_pool)
-               mempool_destroy(bs->bio_integrity_pool);
-}
-EXPORT_SYMBOL(bioset_integrity_free);
+       if (!kintegrityd_wq)
+               panic("Failed to create kintegrityd\n");
 
-void __init bio_integrity_init_slab(void)
-{
        bio_integrity_slab = KMEM_CACHE(bio_integrity_payload,
                                        SLAB_HWCACHE_ALIGN|SLAB_PANIC);
-}
 
-static int __init integrity_init(void)
-{
-       kintegrityd_wq = create_workqueue("kintegrityd");
+       bio_integrity_pool = mempool_create_slab_pool(BIO_POOL_SIZE,
+                                                     bio_integrity_slab);
+       if (!bio_integrity_pool)
+               panic("bio_integrity: can't allocate bip pool\n");
 
-       if (!kintegrityd_wq)
-               panic("Failed to create kintegrityd\n");
+       integrity_bio_set = bioset_create(BIO_POOL_SIZE, 0);
+       if (!integrity_bio_set)
+               panic("bio_integrity: can't allocate bio_set\n");
 
        return 0;
 }
-subsys_initcall(integrity_init);
+subsys_initcall(bio_integrity_init);