GFS2: Fix error code
[safe/jmp/linux-2.6] / fs / gfs2 / rgrp.c
index c0abe69..503b842 100644 (file)
@@ -29,7 +29,7 @@
 #include "util.h"
 #include "log.h"
 #include "inode.h"
-#include "ops_address.h"
+#include "trace_gfs2.h"
 
 #define BFITNOENT ((u32)~0)
 #define NO_BLOCK ((u64)~0)
@@ -153,10 +153,10 @@ static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
 {
        u64 tmp;
        static const u64 search[] = {
-               [0] = 0xffffffffffffffff,
-               [1] = 0xaaaaaaaaaaaaaaaa,
-               [2] = 0x5555555555555555,
-               [3] = 0x0000000000000000,
+               [0] = 0xffffffffffffffffULL,
+               [1] = 0xaaaaaaaaaaaaaaaaULL,
+               [2] = 0x5555555555555555ULL,
+               [3] = 0x0000000000000000ULL,
        };
        tmp = le64_to_cpu(*ptr) ^ search[state];
        tmp &= (tmp >> 1);
@@ -179,19 +179,20 @@ static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
  * always aligned to a 64 bit boundary.
  *
  * The size of the buffer is in bytes, but is it assumed that it is
- * always ok to to read a complete multiple of 64 bits at the end
+ * always ok to read a complete multiple of 64 bits at the end
  * of the block in case the end is no aligned to a natural boundary.
  *
  * Return: the block number (bitmap buffer scope) that was found
  */
 
-u32 gfs2_bitfit(const u8 *buf, const unsigned int len, u32 goal, u8 state)
+static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
+                      u32 goal, u8 state)
 {
        u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
        const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
        const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
        u64 tmp;
-       u64 mask = 0x5555555555555555;
+       u64 mask = 0x5555555555555555ULL;
        u32 bit;
 
        BUG_ON(state > 3);
@@ -201,7 +202,7 @@ u32 gfs2_bitfit(const u8 *buf, const unsigned int len, u32 goal, u8 state)
        tmp = gfs2_bit_search(ptr, mask, state);
        ptr++;
        while(tmp == 0 && ptr < end) {
-               tmp = gfs2_bit_search(ptr, 0x5555555555555555, state);
+               tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
                ptr++;
        }
        /* Mask off any bits which are more than len bytes from the start */
@@ -211,8 +212,7 @@ u32 gfs2_bitfit(const u8 *buf, const unsigned int len, u32 goal, u8 state)
        if (tmp == 0)
                return BFITNOENT;
        ptr--;
-       bit = fls64(tmp);
-       bit--;          /* fls64 always adds one to the bit count */
+       bit = __ffs64(tmp);
        bit /= 2;       /* two bits per entry in the bitmap */
        return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
 }
@@ -285,27 +285,19 @@ void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
        }
 
        tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
-       if (count[1] + count[2] != tmp) {
+       if (count[1] != tmp) {
                if (gfs2_consist_rgrpd(rgd))
                        fs_err(sdp, "used data mismatch:  %u != %u\n",
                               count[1], tmp);
                return;
        }
 
-       if (count[3] != rgd->rd_dinodes) {
+       if (count[2] + count[3] != rgd->rd_dinodes) {
                if (gfs2_consist_rgrpd(rgd))
                        fs_err(sdp, "used metadata mismatch:  %u != %u\n",
-                              count[3], rgd->rd_dinodes);
+                              count[2] + count[3], rgd->rd_dinodes);
                return;
        }
-
-       if (count[2] > count[3]) {
-               if (gfs2_consist_rgrpd(rgd))
-                       fs_err(sdp, "unlinked inodes > inodes:  %u\n",
-                              count[2]);
-               return;
-       }
-
 }
 
 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
@@ -442,6 +434,7 @@ static int compute_bitstructs(struct gfs2_rgrpd *rgd)
        for (x = 0; x < length; x++) {
                bi = rgd->rd_bits + x;
 
+               bi->bi_flags = 0;
                /* small rgrp; bitmap stored completely in header block */
                if (length == 1) {
                        bytes = bytes_left;
@@ -580,7 +573,6 @@ static int read_rindex_entry(struct gfs2_inode *ip,
 
        rgd->rd_gl->gl_object = rgd;
        rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
-       rgd->rd_flags |= GFS2_RDF_CHECK;
        return error;
 }
 
@@ -599,11 +591,7 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
        u64 rgrp_count = ip->i_disksize;
        int error;
 
-       if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
-               gfs2_consist_inode(ip);
-               return -EIO;
-       }
-
+       do_div(rgrp_count, sizeof(struct gfs2_rindex));
        clear_rgrpdi(sdp);
 
        file_ra_state_init(&ra_state, inode->i_mapping);
@@ -701,10 +689,9 @@ static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
        u32 rg_flags;
 
        rg_flags = be32_to_cpu(str->rg_flags);
-       if (rg_flags & GFS2_RGF_NOALLOC)
-               rgd->rd_flags |= GFS2_RDF_NOALLOC;
-       else
-               rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
+       rg_flags &= ~GFS2_RDF_MASK;
+       rgd->rd_flags &= GFS2_RDF_MASK;
+       rgd->rd_flags |= rg_flags;
        rgd->rd_free = be32_to_cpu(str->rg_free);
        rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
        rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
@@ -713,11 +700,8 @@ static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
 static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
 {
        struct gfs2_rgrp *str = buf;
-       u32 rg_flags = 0;
 
-       if (rgd->rd_flags & GFS2_RDF_NOALLOC)
-               rg_flags |= GFS2_RGF_NOALLOC;
-       str->rg_flags = cpu_to_be32(rg_flags);
+       str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
        str->rg_free = cpu_to_be32(rgd->rd_free);
        str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
        str->__pad = cpu_to_be32(0);
@@ -775,8 +759,10 @@ int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
        }
 
        if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
+               for (x = 0; x < length; x++)
+                       clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
                gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
-               rgd->rd_flags |= GFS2_RDF_UPTODATE;
+               rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
        }
 
        spin_lock(&sdp->sd_rindex_spin);
@@ -845,7 +831,7 @@ static void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
        struct super_block *sb = sdp->sd_vfs;
        struct block_device *bdev = sb->s_bdev;
        const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
-                                          bdev_hardsect_size(sb->s_bdev);
+                                          bdev_logical_block_size(sb->s_bdev);
        u64 blk;
        sector_t start = 0;
        sector_t nr_sects = 0;
@@ -867,7 +853,8 @@ static void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
                                        goto start_new_extent;
                                if ((start + nr_sects) != blk) {
                                        rv = blkdev_issue_discard(bdev, start,
-                                                           nr_sects, GFP_NOFS);
+                                                           nr_sects, GFP_NOFS,
+                                                           DISCARD_FL_BARRIER);
                                        if (rv)
                                                goto fail;
                                        nr_sects = 0;
@@ -881,7 +868,8 @@ start_new_extent:
                }
        }
        if (nr_sects) {
-               rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS);
+               rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS,
+                                        DISCARD_FL_BARRIER);
                if (rv)
                        goto fail;
        }
@@ -903,6 +891,7 @@ void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
                        continue;
                if (sdp->sd_args.ar_discard)
                        gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bi);
+               clear_bit(GBF_FULL, &bi->bi_flags);
                memcpy(bi->bi_clone + bi->bi_offset,
                       bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
        }
@@ -922,7 +911,7 @@ void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
 {
        BUG_ON(ip->i_alloc != NULL);
-       ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
+       ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_NOFS);
        return ip->i_alloc;
 }
 
@@ -942,7 +931,7 @@ static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
        struct gfs2_sbd *sdp = rgd->rd_sbd;
        int ret = 0;
 
-       if (rgd->rd_flags & GFS2_RDF_NOALLOC)
+       if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
                return 0;
 
        spin_lock(&sdp->sd_rindex_spin);
@@ -962,7 +951,8 @@ static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
  * Returns: The inode, if one has been found
  */
 
-static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
+static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked,
+                                    u64 skip)
 {
        struct inode *inode;
        u32 goal = 0, block;
@@ -986,6 +976,8 @@ static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
                goal++;
                if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
                        continue;
+               if (no_addr == skip)
+                       continue;
                *last_unlinked = no_addr;
                inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
                                          no_addr, -1, 1);
@@ -1105,7 +1097,7 @@ static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
                        if (try_rgrp_fit(rgd, al))
                                goto out;
                        if (rgd->rd_flags & GFS2_RDF_CHECK)
-                               inode = try_rgrp_unlink(rgd, last_unlinked);
+                               inode = try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
                        if (!rg_locked)
                                gfs2_glock_dq_uninit(&al->al_rgd_gh);
                        if (inode)
@@ -1139,7 +1131,7 @@ static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
                        if (try_rgrp_fit(rgd, al))
                                goto out;
                        if (rgd->rd_flags & GFS2_RDF_CHECK)
-                               inode = try_rgrp_unlink(rgd, last_unlinked);
+                               inode = try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
                        if (!rg_locked)
                                gfs2_glock_dq_uninit(&al->al_rgd_gh);
                        if (inode)
@@ -1262,7 +1254,7 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
  * Returns: The block type (GFS2_BLKST_*)
  */
 
-unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
+static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
 {
        struct gfs2_bitmap *bi = NULL;
        u32 length, rgrp_block, buf_block;
@@ -1315,30 +1307,37 @@ static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
 {
        struct gfs2_bitmap *bi = NULL;
        const u32 length = rgd->rd_length;
-       u32 blk = 0;
+       u32 blk = BFITNOENT;
        unsigned int buf, x;
        const unsigned int elen = *n;
-       const u8 *buffer;
+       const u8 *buffer = NULL;
 
        *n = 0;
        /* Find bitmap block that contains bits for goal block */
        for (buf = 0; buf < length; buf++) {
                bi = rgd->rd_bits + buf;
-               if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
-                       break;
+               /* Convert scope of "goal" from rgrp-wide to within found bit block */
+               if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
+                       goal -= bi->bi_start * GFS2_NBBY;
+                       goto do_search;
+               }
        }
+       buf = 0;
+       goal = 0;
 
-       gfs2_assert(rgd->rd_sbd, buf < length);
-
-       /* Convert scope of "goal" from rgrp-wide to within found bit block */
-       goal -= bi->bi_start * GFS2_NBBY;
-
+do_search:
        /* Search (up to entire) bitmap in this rgrp for allocatable block.
           "x <= length", instead of "x < length", because we typically start
           the search in the middle of a bit block, but if we can't find an
           allocatable block anywhere else, we want to be able wrap around and
           search in the first part of our first-searched bit block.  */
        for (x = 0; x <= length; x++) {
+               bi = rgd->rd_bits + buf;
+
+               if (test_bit(GBF_FULL, &bi->bi_flags) &&
+                   (old_state == GFS2_BLKST_FREE))
+                       goto skip;
+
                /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
                   bitmaps, so we must search the originals for that. */
                buffer = bi->bi_bh->b_data + bi->bi_offset;
@@ -1349,33 +1348,39 @@ static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
                if (blk != BFITNOENT)
                        break;
 
+               if ((goal == 0) && (old_state == GFS2_BLKST_FREE))
+                       set_bit(GBF_FULL, &bi->bi_flags);
+
                /* Try next bitmap block (wrap back to rgrp header if at end) */
-               buf = (buf + 1) % length;
-               bi = rgd->rd_bits + buf;
+skip:
+               buf++;
+               buf %= length;
                goal = 0;
        }
 
-       if (blk != BFITNOENT && old_state != new_state) {
-               *n = 1;
-               gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
+       if (blk == BFITNOENT)
+               return blk;
+       *n = 1;
+       if (old_state == new_state)
+               goto out;
+
+       gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
+       gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
+                   bi->bi_len, blk, new_state);
+       goal = blk;
+       while (*n < elen) {
+               goal++;
+               if (goal >= (bi->bi_len * GFS2_NBBY))
+                       break;
+               if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
+                   GFS2_BLKST_FREE)
+                       break;
                gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
-                           bi->bi_len, blk, new_state);
-               goal = blk;
-               while (*n < elen) {
-                       goal++;
-                       if (goal >= (bi->bi_len * GFS2_NBBY))
-                               break;
-                       if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
-                           GFS2_BLKST_FREE)
-                               break;
-                       gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
-                                   bi->bi_offset, bi->bi_len, goal,
-                                   new_state);
-                       (*n)++;
-               }
+                           bi->bi_len, goal, new_state);
+               (*n)++;
        }
-
-       return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
+out:
+       return (bi->bi_start * GFS2_NBBY) + blk;
 }
 
 /**
@@ -1435,19 +1440,51 @@ static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
 }
 
 /**
- * gfs2_alloc_block - Allocate a block
+ * gfs2_rgrp_dump - print out an rgrp
+ * @seq: The iterator
+ * @gl: The glock in question
+ *
+ */
+
+int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
+{
+       const struct gfs2_rgrpd *rgd = gl->gl_object;
+       if (rgd == NULL)
+               return 0;
+       gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
+                      (unsigned long long)rgd->rd_addr, rgd->rd_flags,
+                      rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
+       return 0;
+}
+
+static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
+{
+       struct gfs2_sbd *sdp = rgd->rd_sbd;
+       fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
+               (unsigned long long)rgd->rd_addr);
+       fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
+       gfs2_rgrp_dump(NULL, rgd->rd_gl);
+       rgd->rd_flags |= GFS2_RDF_ERROR;
+}
+
+/**
+ * gfs2_alloc_block - Allocate one or more blocks
  * @ip: the inode to allocate the block for
+ * @bn: Used to return the starting block number
+ * @n: requested number of blocks/extent length (value/result)
  *
- * Returns: the allocated block
+ * Returns: 0 or error
  */
 
-u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
+int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
+       struct buffer_head *dibh;
        struct gfs2_alloc *al = ip->i_alloc;
        struct gfs2_rgrpd *rgd = al->al_rgd;
        u32 goal, blk;
        u64 block;
+       int error;
 
        if (rgrp_contains_block(rgd, ip->i_goal))
                goal = ip->i_goal - rgd->rd_data0;
@@ -1455,13 +1492,24 @@ u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
                goal = rgd->rd_last_alloc;
 
        blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
-       BUG_ON(blk == BFITNOENT);
+
+       /* Since all blocks are reserved in advance, this shouldn't happen */
+       if (blk == BFITNOENT)
+               goto rgrp_error;
 
        rgd->rd_last_alloc = blk;
        block = rgd->rd_data0 + blk;
        ip->i_goal = block;
+       error = gfs2_meta_inode_buffer(ip, &dibh);
+       if (error == 0) {
+               struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
+               gfs2_trans_add_bh(ip->i_gl, dibh, 1);
+               di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_goal);
+               brelse(dibh);
+       }
+       if (rgd->rd_free < *n)
+               goto rgrp_error;
 
-       gfs2_assert_withdraw(sdp, rgd->rd_free >= *n);
        rgd->rd_free -= *n;
 
        gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
@@ -1475,18 +1523,25 @@ u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
        spin_lock(&sdp->sd_rindex_spin);
        rgd->rd_free_clone -= *n;
        spin_unlock(&sdp->sd_rindex_spin);
+       trace_gfs2_block_alloc(ip, block, *n, GFS2_BLKST_USED);
+       *bn = block;
+       return 0;
 
-       return block;
+rgrp_error:
+       gfs2_rgrp_error(rgd);
+       return -EIO;
 }
 
 /**
  * gfs2_alloc_di - Allocate a dinode
  * @dip: the directory that the inode is going in
+ * @bn: the block number which is allocated
+ * @generation: the generation number of the inode
  *
- * Returns: the block allocated
+ * Returns: 0 on success or error
  */
 
-u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
+int gfs2_alloc_di(struct gfs2_inode *dip, u64 *bn, u64 *generation)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
        struct gfs2_alloc *al = dip->i_alloc;
@@ -1497,16 +1552,21 @@ u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
 
        blk = rgblk_search(rgd, rgd->rd_last_alloc,
                           GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
-       BUG_ON(blk == BFITNOENT);
 
-       rgd->rd_last_alloc = blk;
+       /* Since all blocks are reserved in advance, this shouldn't happen */
+       if (blk == BFITNOENT)
+               goto rgrp_error;
 
+       rgd->rd_last_alloc = blk;
        block = rgd->rd_data0 + blk;
+       if (rgd->rd_free == 0)
+               goto rgrp_error;
 
-       gfs2_assert_withdraw(sdp, rgd->rd_free);
        rgd->rd_free--;
        rgd->rd_dinodes++;
        *generation = rgd->rd_igeneration++;
+       if (*generation == 0)
+               *generation = rgd->rd_igeneration++;
        gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
 
@@ -1518,8 +1578,13 @@ u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
        spin_lock(&sdp->sd_rindex_spin);
        rgd->rd_free_clone--;
        spin_unlock(&sdp->sd_rindex_spin);
+       trace_gfs2_block_alloc(dip, block, 1, GFS2_BLKST_DINODE);
+       *bn = block;
+       return 0;
 
-       return block;
+rgrp_error:
+       gfs2_rgrp_error(rgd);
+       return -EIO;
 }
 
 /**
@@ -1538,7 +1603,7 @@ void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
        rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
        if (!rgd)
                return;
-
+       trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
        rgd->rd_free += blen;
 
        gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
@@ -1566,7 +1631,7 @@ void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
        rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
        if (!rgd)
                return;
-
+       trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
        rgd->rd_free += blen;
 
        gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
@@ -1589,6 +1654,7 @@ void gfs2_unlink_di(struct inode *inode)
        rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
        if (!rgd)
                return;
+       trace_gfs2_block_alloc(ip, blkno, 1, GFS2_BLKST_UNLINKED);
        gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
        gfs2_trans_add_rg(rgd);
@@ -1620,11 +1686,58 @@ static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
 {
        gfs2_free_uninit_di(rgd, ip->i_no_addr);
+       trace_gfs2_block_alloc(ip, ip->i_no_addr, 1, GFS2_BLKST_FREE);
        gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
        gfs2_meta_wipe(ip, ip->i_no_addr, 1);
 }
 
 /**
+ * gfs2_check_blk_type - Check the type of a block
+ * @sdp: The superblock
+ * @no_addr: The block number to check
+ * @type: The block type we are looking for
+ *
+ * Returns: 0 if the block type matches the expected type
+ *          -ESTALE if it doesn't match
+ *          or -ve errno if something went wrong while checking
+ */
+
+int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
+{
+       struct gfs2_rgrpd *rgd;
+       struct gfs2_holder ri_gh, rgd_gh;
+       struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
+       int ri_locked = 0;
+       int error;
+
+       if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
+               error = gfs2_rindex_hold(sdp, &ri_gh);
+               if (error)
+                       goto fail;
+               ri_locked = 1;
+       }
+
+       error = -EINVAL;
+       rgd = gfs2_blk2rgrpd(sdp, no_addr);
+       if (!rgd)
+               goto fail_rindex;
+
+       error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
+       if (error)
+               goto fail_rindex;
+
+       if (gfs2_get_block_type(rgd, no_addr) != type)
+               error = -ESTALE;
+
+       gfs2_glock_dq_uninit(&rgd_gh);
+fail_rindex:
+       if (ri_locked)
+               gfs2_glock_dq_uninit(&ri_gh);
+fail:
+       return error;
+}
+
+/**
  * gfs2_rlist_add - add a RG to a list of RGs
  * @sdp: the filesystem
  * @rlist: the list of resource groups