[GFS2] Add generation number
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / gfs2 / rgrp.c
index 8df6fba20fac183d0e08d6d81500e223ce513930..65eea0b88bf7d858b54fef6095f12cdb5073e830 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
- * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
  *
  * This copyrighted material is made available to anyone wishing to use,
  * modify, copy, or redistribute it subject to the terms and conditions
 #include <linux/spinlock.h>
 #include <linux/completion.h>
 #include <linux/buffer_head.h>
-#include <asm/semaphore.h>
+#include <linux/fs.h>
+#include <linux/gfs2_ondisk.h>
 
 #include "gfs2.h"
-#include "bits.h"
+#include "lm_interface.h"
+#include "incore.h"
 #include "glock.h"
 #include "glops.h"
-#include "jdata.h"
 #include "lops.h"
 #include "meta_io.h"
 #include "quota.h"
 #include "rgrp.h"
 #include "super.h"
 #include "trans.h"
+#include "ops_file.h"
+#include "util.h"
+
+#define BFITNOENT 0xFFFFFFFF
+
+/*
+ * These routines are used by the resource group routines (rgrp.c)
+ * to keep track of block allocation.  Each block is represented by two
+ * bits.  So, each byte represents GFS2_NBBY (i.e. 4) blocks.
+ *
+ * 0 = Free
+ * 1 = Used (not metadata)
+ * 2 = Unlinked (still in use) inode
+ * 3 = Used (metadata)
+ */
+
+static const char valid_change[16] = {
+               /* current */
+       /* n */ 0, 1, 1, 1,
+       /* e */ 1, 0, 0, 0,
+       /* w */ 0, 0, 0, 1,
+               1, 0, 0, 0
+};
+
+/**
+ * gfs2_setbit - Set a bit in the bitmaps
+ * @buffer: the buffer that holds the bitmaps
+ * @buflen: the length (in bytes) of the buffer
+ * @block: the block to set
+ * @new_state: the new state of the block
+ *
+ */
+
+static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
+                       unsigned int buflen, uint32_t block,
+                       unsigned char new_state)
+{
+       unsigned char *byte, *end, cur_state;
+       unsigned int bit;
+
+       byte = buffer + (block / GFS2_NBBY);
+       bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
+       end = buffer + buflen;
+
+       gfs2_assert(rgd->rd_sbd, byte < end);
+
+       cur_state = (*byte >> bit) & GFS2_BIT_MASK;
+
+       if (valid_change[new_state * 4 + cur_state]) {
+               *byte ^= cur_state << bit;
+               *byte |= new_state << bit;
+       } else
+               gfs2_consist_rgrpd(rgd);
+}
+
+/**
+ * gfs2_testbit - test a bit in the bitmaps
+ * @buffer: the buffer that holds the bitmaps
+ * @buflen: the length (in bytes) of the buffer
+ * @block: the block to read
+ *
+ */
+
+static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
+                                 unsigned int buflen, uint32_t block)
+{
+       unsigned char *byte, *end, cur_state;
+       unsigned int bit;
+
+       byte = buffer + (block / GFS2_NBBY);
+       bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
+       end = buffer + buflen;
+
+       gfs2_assert(rgd->rd_sbd, byte < end);
+
+       cur_state = (*byte >> bit) & GFS2_BIT_MASK;
+
+       return cur_state;
+}
+
+/**
+ * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
+ *       a block in a given allocation state.
+ * @buffer: the buffer that holds the bitmaps
+ * @buflen: the length (in bytes) of the buffer
+ * @goal: start search at this block's bit-pair (within @buffer)
+ * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
+ *       bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
+ *
+ * Scope of @goal and returned block number is only within this bitmap buffer,
+ * not entire rgrp or filesystem.  @buffer will be offset from the actual
+ * beginning of a bitmap block buffer, skipping any header structures.
+ *
+ * Return: the block number (bitmap buffer scope) that was found
+ */
+
+static uint32_t gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
+                           unsigned int buflen, uint32_t goal,
+                           unsigned char old_state)
+{
+       unsigned char *byte, *end, alloc;
+       uint32_t blk = goal;
+       unsigned int bit;
+
+       byte = buffer + (goal / GFS2_NBBY);
+       bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
+       end = buffer + buflen;
+       alloc = (old_state & 1) ? 0 : 0x55;
+
+       while (byte < end) {
+               if ((*byte & 0x55) == alloc) {
+                       blk += (8 - bit) >> 1;
+
+                       bit = 0;
+                       byte++;
+
+                       continue;
+               }
+
+               if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
+                       return blk;
+
+               bit += GFS2_BIT_SIZE;
+               if (bit >= 8) {
+                       bit = 0;
+                       byte++;
+               }
+
+               blk++;
+       }
+
+       return BFITNOENT;
+}
+
+/**
+ * gfs2_bitcount - count the number of bits in a certain state
+ * @buffer: the buffer that holds the bitmaps
+ * @buflen: the length (in bytes) of the buffer
+ * @state: the state of the block we're looking for
+ *
+ * Returns: The number of bits
+ */
+
+static uint32_t gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
+                             unsigned int buflen, unsigned char state)
+{
+       unsigned char *byte = buffer;
+       unsigned char *end = buffer + buflen;
+       unsigned char state1 = state << 2;
+       unsigned char state2 = state << 4;
+       unsigned char state3 = state << 6;
+       uint32_t count = 0;
+
+       for (; byte < end; byte++) {
+               if (((*byte) & 0x03) == state)
+                       count++;
+               if (((*byte) & 0x0C) == state1)
+                       count++;
+               if (((*byte) & 0x30) == state2)
+                       count++;
+               if (((*byte) & 0xC0) == state3)
+                       count++;
+       }
+
+       return count;
+}
 
 /**
  * gfs2_rgrp_verify - Verify that a resource group is consistent
@@ -63,26 +230,27 @@ void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
        tmp = rgd->rd_ri.ri_data -
                rgd->rd_rg.rg_free -
                rgd->rd_rg.rg_dinodes;
-       if (count[1] != tmp) {
+       if (count[1] + count[2] != tmp) {
                if (gfs2_consist_rgrpd(rgd))
                        fs_err(sdp, "used data mismatch:  %u != %u\n",
                               count[1], tmp);
                return;
        }
 
-       if (count[2]) {
+       if (count[3] != rgd->rd_rg.rg_dinodes) {
                if (gfs2_consist_rgrpd(rgd))
-                       fs_err(sdp, "free metadata mismatch:  %u != 0\n",
-                              count[2]);
+                       fs_err(sdp, "used metadata mismatch:  %u != %u\n",
+                              count[3], rgd->rd_rg.rg_dinodes);
                return;
        }
 
-       if (count[3] != rgd->rd_rg.rg_dinodes) {
+       if (count[2] > count[3]) {
                if (gfs2_consist_rgrpd(rgd))
-                       fs_err(sdp, "used metadata mismatch:  %u != %u\n",
-                              count[3], rgd->rd_rg.rg_dinodes);
+                       fs_err(sdp, "unlinked inodes > inodes:  %u\n",
+                              count[2]);
                return;
        }
+
 }
 
 static inline int rgrp_contains_block(struct gfs2_rindex *ri, uint64_t block)
@@ -170,7 +338,7 @@ static void clear_rgrpdi(struct gfs2_sbd *sdp)
                list_del(&rgd->rd_list_mru);
 
                if (gl) {
-                       set_gl2rgd(gl, NULL);
+                       gl->gl_object = NULL;
                        gfs2_glock_put(gl);
                }
 
@@ -181,9 +349,9 @@ static void clear_rgrpdi(struct gfs2_sbd *sdp)
 
 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
 {
-       down(&sdp->sd_rindex_mutex);
+       mutex_lock(&sdp->sd_rindex_mutex);
        clear_rgrpdi(sdp);
-       up(&sdp->sd_rindex_mutex);
+       mutex_unlock(&sdp->sd_rindex_mutex);
 }
 
 /**
@@ -203,6 +371,9 @@ static int compute_bitstructs(struct gfs2_rgrpd *rgd)
        uint32_t bytes_left, bytes;
        int x;
 
+       if (!length)
+               return -EINVAL;
+
        rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_KERNEL);
        if (!rgd->rd_bits)
                return -ENOMEM;
@@ -232,7 +403,8 @@ static int compute_bitstructs(struct gfs2_rgrpd *rgd)
                        bi->bi_len = bytes;
                /* other blocks */
                } else {
-                       bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
+                       bytes = sdp->sd_sb.sb_bsize -
+                               sizeof(struct gfs2_meta_header);
                        bi->bi_offset = sizeof(struct gfs2_meta_header);
                        bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
                        bi->bi_len = bytes;
@@ -267,9 +439,11 @@ static int compute_bitstructs(struct gfs2_rgrpd *rgd)
 
 static int gfs2_ri_update(struct gfs2_inode *ip)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
+       struct inode *inode = &ip->i_inode;
        struct gfs2_rgrpd *rgd;
        char buf[sizeof(struct gfs2_rindex)];
+       struct file_ra_state ra_state;
        uint64_t junk = ip->i_di.di_size;
        int error;
 
@@ -280,10 +454,10 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
 
        clear_rgrpdi(sdp);
 
+       file_ra_state_init(&ra_state, inode->i_mapping);
        for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
-               error = gfs2_jdata_read_mem(ip, buf,
-                                           sdp->sd_rgrps *
-                                           sizeof(struct gfs2_rindex),
+               loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
+               error = gfs2_internal_read(ip, &ra_state, buf, &pos,
                                            sizeof(struct gfs2_rindex));
                if (!error)
                        break;
@@ -298,7 +472,7 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
                if (!rgd)
                        goto fail;
 
-               init_MUTEX(&rgd->rd_mutex);
+               mutex_init(&rgd->rd_mutex);
                lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
                rgd->rd_sbd = sdp;
 
@@ -306,7 +480,6 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
                list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
 
                gfs2_rindex_in(&rgd->rd_ri, buf);
-
                error = compute_bitstructs(rgd);
                if (error)
                        goto fail;
@@ -316,17 +489,15 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
                if (error)
                        goto fail;
 
-               set_gl2rgd(rgd->rd_gl, rgd);
+               rgd->rd_gl->gl_object = rgd;
                rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
        }
 
        sdp->sd_rindex_vn = ip->i_gl->gl_vn;
-
        return 0;
 
- fail:
+fail:
        clear_rgrpdi(sdp);
-
        return error;
 }
 
@@ -350,7 +521,7 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
 
 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
 {
-       struct gfs2_inode *ip = sdp->sd_rindex;
+       struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
        struct gfs2_glock *gl = ip->i_gl;
        int error;
 
@@ -360,13 +531,13 @@ int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
 
        /* Read new copy from disk if we don't have the latest */
        if (sdp->sd_rindex_vn != gl->gl_vn) {
-               down(&sdp->sd_rindex_mutex);
+               mutex_lock(&sdp->sd_rindex_mutex);
                if (sdp->sd_rindex_vn != gl->gl_vn) {
                        error = gfs2_ri_update(ip);
                        if (error)
                                gfs2_glock_dq_uninit(ri_gh);
                }
-               up(&sdp->sd_rindex_mutex);
+               mutex_unlock(&sdp->sd_rindex_mutex);
        }
 
        return error;
@@ -391,13 +562,13 @@ int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
        unsigned int x, y;
        int error;
 
-       down(&rgd->rd_mutex);
+       mutex_lock(&rgd->rd_mutex);
 
        spin_lock(&sdp->sd_rindex_spin);
        if (rgd->rd_bh_count) {
                rgd->rd_bh_count++;
                spin_unlock(&sdp->sd_rindex_spin);
-               up(&rgd->rd_mutex);
+               mutex_unlock(&rgd->rd_mutex);
                return 0;
        }
        spin_unlock(&sdp->sd_rindex_spin);
@@ -415,8 +586,7 @@ int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
                error = gfs2_meta_reread(sdp, bi->bi_bh, DIO_WAIT);
                if (error)
                        goto fail;
-               if (gfs2_metatype_check(sdp, bi->bi_bh,
-                                       (y) ? GFS2_METATYPE_RB :
+               if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
                                              GFS2_METATYPE_RG)) {
                        error = -EIO;
                        goto fail;
@@ -433,18 +603,18 @@ int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
        rgd->rd_bh_count++;
        spin_unlock(&sdp->sd_rindex_spin);
 
-       up(&rgd->rd_mutex);
+       mutex_unlock(&rgd->rd_mutex);
 
        return 0;
 
- fail:
+fail:
        while (x--) {
                bi = rgd->rd_bits + x;
                brelse(bi->bi_bh);
                bi->bi_bh = NULL;
                gfs2_assert_warn(sdp, !bi->bi_clone);
        }
-       up(&rgd->rd_mutex);
+       mutex_unlock(&rgd->rd_mutex);
 
        return error;
 }
@@ -499,8 +669,7 @@ void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
                if (!bi->bi_clone)
                        continue;
                memcpy(bi->bi_clone + bi->bi_offset,
-                      bi->bi_bh->b_data + bi->bi_offset,
-                      bi->bi_len);
+                      bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
        }
 
        spin_lock(&sdp->sd_rindex_spin);
@@ -589,13 +758,11 @@ static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
                        goto out;
        }
 
- first:
+first:
        rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
                         rd_recent);
-
- out:
+out:
        spin_unlock(&sdp->sd_rindex_spin);
-
        return rgd;
 }
 
@@ -637,9 +804,8 @@ static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
        if (!list_empty(head))
                rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
 
- out:
+out:
        spin_unlock(&sdp->sd_rindex_spin);
-
        return rgd;
 }
 
@@ -667,7 +833,7 @@ static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
        }
        list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
 
- out:
+out:
        spin_unlock(&sdp->sd_rindex_spin);
 }
 
@@ -730,7 +896,7 @@ static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
 
 static int get_local_rgrp(struct gfs2_inode *ip)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_rgrpd *rgd, *begin = NULL;
        struct gfs2_alloc *al = &ip->i_alloc;
        int flags = LM_FLAG_TRY;
@@ -797,7 +963,7 @@ static int get_local_rgrp(struct gfs2_inode *ip)
                }
        }
 
- out:
+out:
        ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
 
        if (begin) {
@@ -820,7 +986,7 @@ static int get_local_rgrp(struct gfs2_inode *ip)
 
 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_alloc *al = &ip->i_alloc;
        int error;
 
@@ -852,7 +1018,7 @@ int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
 
 void gfs2_inplace_release(struct gfs2_inode *ip)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_alloc *al = &ip->i_alloc;
 
        if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
@@ -893,8 +1059,7 @@ unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, uint64_t block)
        gfs2_assert(rgd->rd_sbd, buf < length);
        buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
 
-       type = gfs2_testbit(rgd,
-                          bi->bi_bh->b_data + bi->bi_offset,
+       type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
                           bi->bi_len, buf_block);
 
        return type;
@@ -948,8 +1113,7 @@ static uint32_t rgblk_search(struct gfs2_rgrpd *rgd, uint32_t goal,
           search in the first part of our first-searched bit block.  */
        for (x = 0; x <= length; x++) {
                if (bi->bi_clone)
-                       blk = gfs2_bitfit(rgd,
-                                         bi->bi_clone + bi->bi_offset,
+                       blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
                                          bi->bi_len, goal, old_state);
                else
                        blk = gfs2_bitfit(rgd,
@@ -967,13 +1131,11 @@ static uint32_t rgblk_search(struct gfs2_rgrpd *rgd, uint32_t goal,
        if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
                blk = 0;
 
-       gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh);
-       gfs2_setbit(rgd,
-                   bi->bi_bh->b_data + bi->bi_offset,
+       gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
+       gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
                    bi->bi_len, blk, new_state);
        if (bi->bi_clone)
-               gfs2_setbit(rgd,
-                           bi->bi_clone + bi->bi_offset,
+               gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
                            bi->bi_len, blk, new_state);
 
        return bi->bi_start * GFS2_NBBY + blk;
@@ -1000,7 +1162,7 @@ static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
        rgd = gfs2_blk2rgrpd(sdp, bstart);
        if (!rgd) {
                if (gfs2_consist(sdp))
-                       fs_err(sdp, "block = %llu\n", bstart);
+                       fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
                return NULL;
        }
 
@@ -1027,7 +1189,7 @@ static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
                               bi->bi_bh->b_data + bi->bi_offset,
                               bi->bi_len);
                }
-               gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh);
+               gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
                gfs2_setbit(rgd,
                            bi->bi_bh->b_data + bi->bi_offset,
                            bi->bi_len, buf_blk, new_state);
@@ -1043,9 +1205,9 @@ static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
  * Returns: the allocated block
  */
 
-uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
+u64 gfs2_alloc_data(struct gfs2_inode *ip)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_alloc *al = &ip->i_alloc;
        struct gfs2_rgrpd *rgd = al->al_rgd;
        uint32_t goal, blk;
@@ -1056,8 +1218,7 @@ uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
        else
                goal = rgd->rd_last_alloc_data;
 
-       blk = rgblk_search(rgd, goal,
-                          GFS2_BLKST_FREE, GFS2_BLKST_USED);
+       blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
        rgd->rd_last_alloc_data = blk;
 
        block = rgd->rd_ri.ri_data0 + blk;
@@ -1066,7 +1227,7 @@ uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
        gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
        rgd->rd_rg.rg_free--;
 
-       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
+       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
 
        al->al_alloced++;
@@ -1088,9 +1249,9 @@ uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
  * Returns: the allocated block
  */
 
-uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
+u64 gfs2_alloc_meta(struct gfs2_inode *ip)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_alloc *al = &ip->i_alloc;
        struct gfs2_rgrpd *rgd = al->al_rgd;
        uint32_t goal, blk;
@@ -1101,8 +1262,7 @@ uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
        else
                goal = rgd->rd_last_alloc_meta;
 
-       blk = rgblk_search(rgd, goal,
-                          GFS2_BLKST_FREE, GFS2_BLKST_USED);
+       blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
        rgd->rd_last_alloc_meta = blk;
 
        block = rgd->rd_ri.ri_data0 + blk;
@@ -1111,7 +1271,7 @@ uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
        gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
        rgd->rd_rg.rg_free--;
 
-       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
+       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
 
        al->al_alloced++;
@@ -1134,13 +1294,13 @@ uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
  * Returns: the block allocated
  */
 
-uint64_t gfs2_alloc_di(struct gfs2_inode *dip)
+u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
 {
-       struct gfs2_sbd *sdp = dip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
        struct gfs2_alloc *al = &dip->i_alloc;
        struct gfs2_rgrpd *rgd = al->al_rgd;
-       uint32_t blk;
-       uint64_t block;
+       u32 blk;
+       u64 block;
 
        blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
                           GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
@@ -1152,8 +1312,8 @@ uint64_t gfs2_alloc_di(struct gfs2_inode *dip)
        gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
        rgd->rd_rg.rg_free--;
        rgd->rd_rg.rg_dinodes++;
-
-       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
+       *generation = rgd->rd_rg.rg_igeneration++;
+       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
 
        al->al_alloced++;
@@ -1178,7 +1338,7 @@ uint64_t gfs2_alloc_di(struct gfs2_inode *dip)
 
 void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_rgrpd *rgd;
 
        rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
@@ -1187,7 +1347,7 @@ void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
 
        rgd->rd_rg.rg_free += blen;
 
-       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
+       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
 
        gfs2_trans_add_rg(rgd);
@@ -1207,7 +1367,7 @@ void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
 
 void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
+       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_rgrpd *rgd;
 
        rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
@@ -1216,18 +1376,32 @@ void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
 
        rgd->rd_rg.rg_free += blen;
 
-       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
+       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
 
        gfs2_trans_add_rg(rgd);
 
        gfs2_statfs_change(sdp, 0, +blen, 0);
-       gfs2_quota_change(ip, -(int64_t)blen,
-                        ip->i_di.di_uid, ip->i_di.di_gid);
+       gfs2_quota_change(ip, -(int64_t)blen, ip->i_di.di_uid, ip->i_di.di_gid);
        gfs2_meta_wipe(ip, bstart, blen);
 }
 
-void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
+void gfs2_unlink_di(struct inode *inode)
+{
+       struct gfs2_inode *ip = GFS2_I(inode);
+       struct gfs2_sbd *sdp = GFS2_SB(inode);
+       struct gfs2_rgrpd *rgd;
+       u64 blkno = ip->i_num.no_addr;
+
+       rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
+       if (!rgd)
+               return;
+       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
+       gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
+       gfs2_trans_add_rg(rgd);
+}
+
+static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
 {
        struct gfs2_sbd *sdp = rgd->rd_sbd;
        struct gfs2_rgrpd *tmp_rgd;
@@ -1242,19 +1416,13 @@ void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
        rgd->rd_rg.rg_dinodes--;
        rgd->rd_rg.rg_free++;
 
-       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
+       gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
        gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
 
        gfs2_statfs_change(sdp, 0, +1, -1);
        gfs2_trans_add_rg(rgd);
 }
 
-/**
- * gfs2_free_uninit_di - free a dinode block
- * @rgd: the resource group that contains the dinode
- * @ip: the inode
- *
- */
 
 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
 {
@@ -1289,7 +1457,7 @@ void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
        rgd = gfs2_blk2rgrpd(sdp, block);
        if (!rgd) {
                if (gfs2_consist(sdp))
-                       fs_err(sdp, "block = %llu\n", block);
+                       fs_err(sdp, "block = %llu\n", (unsigned long long)block);
                return;
        }