netns xfrm: fix "ip xfrm state|policy count" misreport
[safe/jmp/linux-2.6] / fs / bio-integrity.c
index 31b0887..49a34e7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * bio-integrity.c - bio data integrity extensions
  *
- * Copyright (C) 2007, 2008 Oracle Corporation
+ * Copyright (C) 2007, 2008, 2009 Oracle Corporation
  * Written by: Martin K. Petersen <martin.petersen@oracle.com>
  *
  * This program is free software; you can redistribute it and/or
 #include <linux/bio.h>
 #include <linux/workqueue.h>
 
-static struct kmem_cache *bio_integrity_slab __read_mostly;
+struct integrity_slab {
+       struct kmem_cache *slab;
+       unsigned short nr_vecs;
+       char name[8];
+};
+
+#define IS(x) { .nr_vecs = x, .name = "bip-"__stringify(x) }
+struct integrity_slab bip_slab[BIOVEC_NR_POOLS] __read_mostly = {
+       IS(1), IS(4), IS(16), IS(64), IS(128), IS(BIO_MAX_PAGES),
+};
+#undef IS
+
 static struct workqueue_struct *kintegrityd_wq;
 
+static inline unsigned int vecs_to_idx(unsigned int nr)
+{
+       switch (nr) {
+       case 1:
+               return 0;
+       case 2 ... 4:
+               return 1;
+       case 5 ... 16:
+               return 2;
+       case 17 ... 64:
+               return 3;
+       case 65 ... 128:
+               return 4;
+       case 129 ... BIO_MAX_PAGES:
+               return 5;
+       default:
+               BUG();
+       }
+}
+
+static inline int use_bip_pool(unsigned int idx)
+{
+       if (idx == BIOVEC_NR_POOLS)
+               return 1;
+
+       return 0;
+}
+
 /**
  * bio_integrity_alloc_bioset - Allocate integrity payload and attach it to bio
  * @bio:       bio to attach integrity metadata to
@@ -39,31 +78,34 @@ static struct workqueue_struct *kintegrityd_wq;
  * 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_bioset(struct bio *bio,
+                                                        gfp_t gfp_mask,
+                                                        unsigned int nr_vecs,
+                                                        struct bio_set *bs)
 {
        struct bio_integrity_payload *bip;
-       struct bio_vec *iv;
-       unsigned long idx;
+       unsigned int idx = vecs_to_idx(nr_vecs);
 
        BUG_ON(bio == NULL);
+       bip = NULL;
 
-       bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
-       if (unlikely(bip == NULL)) {
-               printk(KERN_ERR "%s: could not alloc bip\n", __func__);
-               return NULL;
-       }
+       /* Lower order allocations come straight from slab */
+       if (!use_bip_pool(idx))
+               bip = kmem_cache_alloc(bip_slab[idx].slab, gfp_mask);
 
-       memset(bip, 0, sizeof(*bip));
+       /* Use mempool if lower order alloc failed or max vecs were requested */
+       if (bip == NULL) {
+               bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
 
-       iv = bvec_alloc_bs(gfp_mask, nr_vecs, &idx, bs);
-       if (unlikely(iv == NULL)) {
-               printk(KERN_ERR "%s: could not alloc bip_vec\n", __func__);
-               mempool_free(bip, bs->bio_integrity_pool);
-               return NULL;
+               if (unlikely(bip == NULL)) {
+                       printk(KERN_ERR "%s: could not alloc bip\n", __func__);
+                       return NULL;
+               }
        }
 
-       bip->bip_pool = idx;
-       bip->bip_vec = iv;
+       memset(bip, 0, sizeof(*bip));
+
+       bip->bip_slab = idx;
        bip->bip_bio = bio;
        bio->bi_integrity = bip;
 
@@ -81,7 +123,9 @@ EXPORT_SYMBOL(bio_integrity_alloc_bioset);
  * 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)
+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);
 }
@@ -102,11 +146,14 @@ void bio_integrity_free(struct bio *bio, struct bio_set *bs)
        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);
+       if (use_bip_pool(bip->bip_slab))
+               mempool_free(bip, bs->bio_integrity_pool);
+       else
+               kmem_cache_free(bip_slab[bip->bip_slab].slab, bip);
 
        bio->bi_integrity = NULL;
 }
@@ -127,14 +174,13 @@ int bio_integrity_add_page(struct bio *bio, struct page *page,
        struct bio_integrity_payload *bip = bio->bi_integrity;
        struct bio_vec *iv;
 
-       if (bip->bip_vcnt >= bvec_nr_vecs(bip->bip_pool)) {
+       if (bip->bip_vcnt >= bvec_nr_vecs(bip->bip_slab)) {
                printk(KERN_ERR "%s: bip_vec full\n", __func__);
                return 0;
        }
 
        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;
@@ -145,6 +191,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
@@ -174,7 +238,8 @@ EXPORT_SYMBOL(bio_integrity_enabled);
  * sector size of the storage device.  Convert the block layer sectors
  * to physical sectors.
  */
-static inline unsigned int bio_integrity_hw_sectors(struct blk_integrity *bi, unsigned int sectors)
+static inline unsigned int bio_integrity_hw_sectors(struct blk_integrity *bi,
+                                                   unsigned int sectors)
 {
        /* At this point there are only 512b or 4096b DIF/EPP devices */
        if (bi->sector_size == 4096)
@@ -212,7 +277,8 @@ int bio_integrity_tag(struct bio *bio, void *tag_buf, unsigned int len, int set)
        if (bi->tag_size == 0)
                return -1;
 
-       nr_sectors = bio_integrity_hw_sectors(bi, DIV_ROUND_UP(len, bi->tag_size));
+       nr_sectors = bio_integrity_hw_sectors(bi,
+                                       DIV_ROUND_UP(len, bi->tag_size));
 
        if (nr_sectors * bi->tuple_size > bip->bip_size) {
                printk(KERN_ERR "%s: tag too big for bio: %u > %u\n",
@@ -306,6 +372,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
@@ -431,7 +505,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;
@@ -456,21 +530,16 @@ static int bio_integrity_verify(struct bio *bio)
  */
 static void bio_integrity_verify_fn(struct work_struct *work)
 {
-       struct bio_integrity_payload *bip = 
+       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);
 }
 
 /**
@@ -491,7 +560,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);
 }
@@ -502,7 +581,8 @@ EXPORT_SYMBOL(bio_integrity_endio);
  * @bip:       Integrity vector to advance
  * @skip:      Number of bytes to advance it
  */
-void bio_integrity_mark_head(struct bio_integrity_payload *bip, unsigned int skip)
+void bio_integrity_mark_head(struct bio_integrity_payload *bip,
+                            unsigned int skip)
 {
        struct bio_vec *iv;
        unsigned int i;
@@ -527,7 +607,8 @@ void bio_integrity_mark_head(struct bio_integrity_payload *bip, unsigned int ski
  * @bip:       Integrity vector to truncate
  * @len:       New length of integrity vector
  */
-void bio_integrity_mark_tail(struct bio_integrity_payload *bip, unsigned int len)
+void bio_integrity_mark_tail(struct bio_integrity_payload *bip,
+                            unsigned int len)
 {
        struct bio_vec *iv;
        unsigned int i;
@@ -579,7 +660,8 @@ EXPORT_SYMBOL(bio_integrity_advance);
  * and the length will be truncated corresponding to 'len' data
  * sectors.
  */
-void bio_integrity_trim(struct bio *bio, unsigned int offset, unsigned int sectors)
+void bio_integrity_trim(struct bio *bio, unsigned int offset,
+                       unsigned int sectors)
 {
        struct bio_integrity_payload *bip = bio->bi_integrity;
        struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
@@ -625,8 +707,8 @@ void bio_integrity_split(struct bio *bio, struct bio_pair *bp, int sectors)
        bp->iv1 = bip->bip_vec[0];
        bp->iv2 = bip->bip_vec[0];
 
-       bp->bip1.bip_vec = &bp->iv1;
-       bp->bip2.bip_vec = &bp->iv2;
+       bp->bip1.bip_vec[0] = bp->iv1;
+       bp->bip2.bip_vec[0] = bp->iv2;
 
        bp->iv1.bv_len = sectors * bi->tuple_size;
        bp->iv2.bv_offset += sectors * bi->tuple_size;
@@ -644,18 +726,20 @@ EXPORT_SYMBOL(bio_integrity_split);
  * bio_integrity_clone - Callback for cloning bios with integrity metadata
  * @bio:       New bio
  * @bio_src:   Original bio
+ * @gfp_mask:  Memory allocation mask
  * @bs:                bio_set to allocate bip from
  *
  * 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_set *bs)
 {
        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_bioset(bio, gfp_mask, bip_src->bip_vcnt, bs);
 
        if (bip == NULL)
                return -EIO;
@@ -673,8 +757,11 @@ EXPORT_SYMBOL(bio_integrity_clone);
 
 int bioset_integrity_create(struct bio_set *bs, int pool_size)
 {
-       bs->bio_integrity_pool = mempool_create_slab_pool(pool_size,
-                                                         bio_integrity_slab);
+       unsigned int max_slab = vecs_to_idx(BIO_MAX_PAGES);
+
+       bs->bio_integrity_pool =
+               mempool_create_slab_pool(pool_size, bip_slab[max_slab].slab);
+
        if (!bs->bio_integrity_pool)
                return -1;
 
@@ -689,20 +776,22 @@ void bioset_integrity_free(struct bio_set *bs)
 }
 EXPORT_SYMBOL(bioset_integrity_free);
 
-void __init bio_integrity_init_slab(void)
+void __init bio_integrity_init(void)
 {
-       bio_integrity_slab = KMEM_CACHE(bio_integrity_payload,
-                                       SLAB_HWCACHE_ALIGN|SLAB_PANIC);
-}
-EXPORT_SYMBOL(bio_integrity_init_slab);
+       unsigned int i;
 
-static int __init integrity_init(void)
-{
        kintegrityd_wq = create_workqueue("kintegrityd");
-
        if (!kintegrityd_wq)
                panic("Failed to create kintegrityd\n");
 
-       return 0;
+       for (i = 0 ; i < BIOVEC_NR_POOLS ; i++) {
+               unsigned int size;
+
+               size = sizeof(struct bio_integrity_payload)
+                       + bip_slab[i].nr_vecs * sizeof(struct bio_vec);
+
+               bip_slab[i].slab =
+                       kmem_cache_create(bip_slab[i].name, size, 0,
+                                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
+       }
 }
-subsys_initcall(integrity_init);