[GFS2] Add generation number
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / gfs2 / rgrp.c
index 5a32d69329788f785d0425b07c1d032826f6c021..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
@@ -14,7 +14,6 @@
 #include <linux/buffer_head.h>
 #include <linux/fs.h>
 #include <linux/gfs2_ondisk.h>
-#include <asm/semaphore.h>
 
 #include "gfs2.h"
 #include "lm_interface.h"
 /*
  * These routines are used by the resource group routines (rgrp.c)
  * to keep track of block allocation.  Each block is represented by two
- * bits.  One bit indicates whether or not the block is used.  (1=used,
- * 0=free)  The other bit indicates whether or not the block contains a
- * dinode or not.  (1=dinode, 0=not-dinode) So, each byte represents
- * GFS2_NBBY (i.e. 4) blocks.
+ * 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, 0, 1,
+       /* n */ 0, 1, 1, 1,
        /* e */ 1, 0, 0, 0,
-       /* w */ 0, 0, 0, 0,
+       /* w */ 0, 0, 0, 1,
                1, 0, 0, 0
 };
 
@@ -58,8 +59,9 @@ static const char valid_change[16] = {
  *
  */
 
-void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
-                unsigned int buflen, uint32_t block, unsigned char new_state)
+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;
@@ -87,8 +89,8 @@ void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
  *
  */
 
-unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
-                          unsigned int buflen, uint32_t block)
+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;
@@ -120,9 +122,9 @@ unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
  * Return: the block number (bitmap buffer scope) that was found
  */
 
-uint32_t gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
-                    unsigned int buflen, uint32_t goal,
-                    unsigned char old_state)
+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;
@@ -167,8 +169,8 @@ uint32_t gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
  * Returns: The number of bits
  */
 
-uint32_t gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
-                      unsigned int buflen, unsigned char state)
+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;
@@ -228,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)
@@ -368,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;
@@ -433,8 +439,8 @@ 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 inode *inode = ip->i_vnode;
+       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;
@@ -474,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;
@@ -489,12 +494,10 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
        }
 
        sdp->sd_rindex_vn = ip->i_gl->gl_vn;
-
        return 0;
 
- fail:
+fail:
        clear_rgrpdi(sdp);
-
        return error;
 }
 
@@ -518,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->u.generic_ip;
+       struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
        struct gfs2_glock *gl = ip->i_gl;
        int error;
 
@@ -583,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;
@@ -605,7 +607,7 @@ int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
 
        return 0;
 
- fail:
+fail:
        while (x--) {
                bi = rgd->rd_bits + x;
                brelse(bi->bi_bh);
@@ -667,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);
@@ -757,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;
 }
 
@@ -805,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;
 }
 
@@ -835,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);
 }
 
@@ -898,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;
@@ -965,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) {
@@ -988,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;
 
@@ -1020,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)
@@ -1061,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;
@@ -1165,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;
        }
 
@@ -1208,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;
@@ -1252,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;
@@ -1297,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);
@@ -1315,7 +1312,7 @@ 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++;
-
+       *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);
 
@@ -1341,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);
@@ -1370,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);
@@ -1385,12 +1382,26 @@ void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
        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;
@@ -1412,12 +1423,6 @@ void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
        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)
 {
@@ -1452,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;
        }