]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/gfs2/rgrp.c
[GFS2] Shrink & rename di_depth
[linux-2.6-omap-h63xx.git] / fs / gfs2 / rgrp.c
index 708c287e1d0ef7327d08a8ca5d14ca36ac6e04ad..3f10b1fafd663a6186b1102ce9f36b4db5e05ea8 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
- * Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2004-2008 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 "rgrp.h"
 #include "super.h"
 #include "trans.h"
-#include "ops_file.h"
 #include "util.h"
 #include "log.h"
 #include "inode.h"
+#include "ops_address.h"
 
 #define BFITNOENT ((u32)~0)
 #define NO_BLOCK ((u64)~0)
@@ -126,41 +126,44 @@ static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
  * Return: the block number (bitmap buffer scope) that was found
  */
 
-static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
-                           unsigned int buflen, u32 goal,
-                           unsigned char old_state)
+static u32 gfs2_bitfit(const u8 *buffer, unsigned int buflen, u32 goal,
+                      u8 old_state)
 {
-       unsigned char *byte, *end, alloc;
+       const u8 *byte;
        u32 blk = goal;
-       unsigned int bit;
+       unsigned int bit, bitlong;
+       const unsigned long *plong;
+#if BITS_PER_LONG == 32
+       const unsigned long plong55 = 0x55555555;
+#else
+       const unsigned long plong55 = 0x5555555555555555;
+#endif
 
        byte = buffer + (goal / GFS2_NBBY);
+       plong = (const unsigned long *)(buffer + (goal / GFS2_NBBY));
        bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
-       end = buffer + buflen;
-       alloc = (old_state == GFS2_BLKST_FREE) ? 0x55 : 0;
-
-       while (byte < end) {
-               /* If we're looking for a free block we can eliminate all
-                  bitmap settings with 0x55, which represents four data
-                  blocks in a row.  If we're looking for a data block, we can
-                  eliminate 0x00 which corresponds to four free blocks. */
-               if ((*byte & 0x55) == alloc) {
-                       blk += (8 - bit) >> 1;
+       bitlong = bit;
 
-                       bit = 0;
-                       byte++;
+       while (byte < buffer + buflen) {
 
+               if (bitlong == 0 && old_state == 0 && *plong == plong55) {
+                       plong++;
+                       byte += sizeof(unsigned long);
+                       blk += sizeof(unsigned long) * GFS2_NBBY;
                        continue;
                }
-
                if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
                        return blk;
-
                bit += GFS2_BIT_SIZE;
                if (bit >= 8) {
                        bit = 0;
                        byte++;
                }
+               bitlong += GFS2_BIT_SIZE;
+               if (bitlong >= sizeof(unsigned long) * 8) {
+                       bitlong = 0;
+                       plong++;
+               }
 
                blk++;
        }
@@ -177,14 +180,14 @@ static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
  * Returns: The number of bits
  */
 
-static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
-                             unsigned int buflen, unsigned char state)
+static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
+                        unsigned int buflen, u8 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;
+       const u8 *byte = buffer;
+       const u8 *end = buffer + buflen;
+       const u8 state1 = state << 2;
+       const u8 state2 = state << 4;
+       const u8 state3 = state << 6;
        u32 count = 0;
 
        for (; byte < end; byte++) {
@@ -351,7 +354,7 @@ static void clear_rgrpdi(struct gfs2_sbd *sdp)
                }
 
                kfree(rgd->rd_bits);
-               kfree(rgd);
+               kmem_cache_free(gfs2_rgrpd_cachep, rgd);
        }
 }
 
@@ -514,7 +517,7 @@ static int read_rindex_entry(struct gfs2_inode *ip,
                return error;
        }
 
-       rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
+       rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
        error = -ENOMEM;
        if (!rgd)
                return error;
@@ -537,7 +540,7 @@ static int read_rindex_entry(struct gfs2_inode *ip,
                return error;
 
        rgd->rd_gl->gl_object = rgd;
-       rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
+       rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
        rgd->rd_flags |= GFS2_RDF_CHECK;
        return error;
 }
@@ -573,7 +576,7 @@ static int gfs2_ri_update(struct gfs2_inode *ip)
                }
        }
 
-       sdp->sd_rindex_vn = ip->i_gl->gl_vn;
+       sdp->sd_rindex_uptodate = 1;
        return 0;
 }
 
@@ -607,7 +610,7 @@ static int gfs2_ri_update_special(struct gfs2_inode *ip)
                }
        }
 
-       sdp->sd_rindex_vn = ip->i_gl->gl_vn;
+       sdp->sd_rindex_uptodate = 1;
        return 0;
 }
 
@@ -640,9 +643,9 @@ int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
                return error;
 
        /* Read new copy from disk if we don't have the latest */
-       if (sdp->sd_rindex_vn != gl->gl_vn) {
+       if (!sdp->sd_rindex_uptodate) {
                mutex_lock(&sdp->sd_rindex_mutex);
-               if (sdp->sd_rindex_vn != gl->gl_vn) {
+               if (!sdp->sd_rindex_uptodate) {
                        error = gfs2_ri_update(ip);
                        if (error)
                                gfs2_glock_dq_uninit(ri_gh);
@@ -653,21 +656,31 @@ int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
        return error;
 }
 
-static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf)
+static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
 {
        const struct gfs2_rgrp *str = buf;
+       struct gfs2_rgrp_host *rg = &rgd->rd_rg;
+       u32 rg_flags;
 
-       rg->rg_flags = be32_to_cpu(str->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->rg_free = be32_to_cpu(str->rg_free);
        rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
        rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
 }
 
-static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf)
+static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
 {
        struct gfs2_rgrp *str = buf;
+       struct gfs2_rgrp_host *rg = &rgd->rd_rg;
+       u32 rg_flags = 0;
 
-       str->rg_flags = cpu_to_be32(rg->rg_flags);
+       if (rgd->rd_flags & GFS2_RDF_NOALLOC)
+               rg_flags |= GFS2_RGF_NOALLOC;
+       str->rg_flags = cpu_to_be32(rg_flags);
        str->rg_free = cpu_to_be32(rg->rg_free);
        str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
        str->__pad = cpu_to_be32(0);
@@ -724,9 +737,9 @@ int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
                }
        }
 
-       if (rgd->rd_rg_vn != gl->gl_vn) {
-               gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
-               rgd->rd_rg_vn = gl->gl_vn;
+       if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
+               gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
+               rgd->rd_flags |= GFS2_RDF_UPTODATE;
        }
 
        spin_lock(&sdp->sd_rindex_spin);
@@ -817,11 +830,9 @@ void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
 
 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
 {
-       struct gfs2_alloc *al = &ip->i_alloc;
-
-       /* FIXME: Should assert that the correct locks are held here... */
-       memset(al, 0, sizeof(*al));
-       return al;
+       BUG_ON(ip->i_alloc != NULL);
+       ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
+       return ip->i_alloc;
 }
 
 /**
@@ -840,7 +851,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_rg.rg_flags & GFS2_RGF_NOALLOC)
+       if (rgd->rd_flags & GFS2_RDF_NOALLOC)
                return 0;
 
        spin_lock(&sdp->sd_rindex_spin);
@@ -1059,26 +1070,34 @@ static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
        struct inode *inode = NULL;
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_rgrpd *rgd, *begin = NULL;
-       struct gfs2_alloc *al = &ip->i_alloc;
+       struct gfs2_alloc *al = ip->i_alloc;
        int flags = LM_FLAG_TRY;
        int skipped = 0;
        int loops = 0;
-       int error;
+       int error, rg_locked;
 
        /* Try recently successful rgrps */
 
        rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
 
        while (rgd) {
-               error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
-                                          LM_FLAG_TRY, &al->al_rgd_gh);
+               rg_locked = 0;
+
+               if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
+                       rg_locked = 1;
+                       error = 0;
+               } else {
+                       error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
+                                                  LM_FLAG_TRY, &al->al_rgd_gh);
+               }
                switch (error) {
                case 0:
                        if (try_rgrp_fit(rgd, al))
                                goto out;
                        if (rgd->rd_flags & GFS2_RDF_CHECK)
                                inode = try_rgrp_unlink(rgd, last_unlinked);
-                       gfs2_glock_dq_uninit(&al->al_rgd_gh);
+                       if (!rg_locked)
+                               gfs2_glock_dq_uninit(&al->al_rgd_gh);
                        if (inode)
                                return inode;
                        rgd = recent_rgrp_next(rgd, 1);
@@ -1098,15 +1117,23 @@ static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
        begin = rgd = forward_rgrp_get(sdp);
 
        for (;;) {
-               error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
-                                         &al->al_rgd_gh);
+               rg_locked = 0;
+
+               if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
+                       rg_locked = 1;
+                       error = 0;
+               } else {
+                       error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
+                                                  &al->al_rgd_gh);
+               }
                switch (error) {
                case 0:
                        if (try_rgrp_fit(rgd, al))
                                goto out;
                        if (rgd->rd_flags & GFS2_RDF_CHECK)
                                inode = try_rgrp_unlink(rgd, last_unlinked);
-                       gfs2_glock_dq_uninit(&al->al_rgd_gh);
+                       if (!rg_locked)
+                               gfs2_glock_dq_uninit(&al->al_rgd_gh);
                        if (inode)
                                return inode;
                        break;
@@ -1158,7 +1185,7 @@ out:
 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
-       struct gfs2_alloc *al = &ip->i_alloc;
+       struct gfs2_alloc *al = ip->i_alloc;
        struct inode *inode;
        int error = 0;
        u64 last_unlinked = NO_BLOCK;
@@ -1204,7 +1231,7 @@ try_again:
 void gfs2_inplace_release(struct gfs2_inode *ip)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
-       struct gfs2_alloc *al = &ip->i_alloc;
+       struct gfs2_alloc *al = ip->i_alloc;
 
        if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
                fs_warn(sdp, "al_alloced = %u, al_requested = %u "
@@ -1213,7 +1240,8 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
                             al->al_line);
 
        al->al_rgd = NULL;
-       gfs2_glock_dq_uninit(&al->al_rgd_gh);
+       if (al->al_rgd_gh.gh_gl)
+               gfs2_glock_dq_uninit(&al->al_rgd_gh);
        if (ip != GFS2_I(sdp->sd_rindex))
                gfs2_glock_dq_uninit(&al->al_ri_gh);
 }
@@ -1300,13 +1328,11 @@ static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
        for (x = 0; x <= length; x++) {
                /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
                   bitmaps, so we must search the originals for that. */
+               const u8 *buffer = bi->bi_bh->b_data + bi->bi_offset;
                if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
-                       blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
-                                         bi->bi_len, goal, old_state);
-               else
-                       blk = gfs2_bitfit(rgd,
-                                         bi->bi_bh->b_data + bi->bi_offset,
-                                         bi->bi_len, goal, old_state);
+                       buffer = bi->bi_clone + bi->bi_offset;
+
+               blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
                if (blk != BFITNOENT)
                        break;
 
@@ -1394,7 +1420,7 @@ static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
 u64 gfs2_alloc_data(struct gfs2_inode *ip)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
-       struct gfs2_alloc *al = &ip->i_alloc;
+       struct gfs2_alloc *al = ip->i_alloc;
        struct gfs2_rgrpd *rgd = al->al_rgd;
        u32 goal, blk;
        u64 block;
@@ -1415,7 +1441,7 @@ u64 gfs2_alloc_data(struct gfs2_inode *ip)
        rgd->rd_rg.rg_free--;
 
        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_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
 
        al->al_alloced++;
 
@@ -1439,7 +1465,7 @@ u64 gfs2_alloc_data(struct gfs2_inode *ip)
 u64 gfs2_alloc_meta(struct gfs2_inode *ip)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
-       struct gfs2_alloc *al = &ip->i_alloc;
+       struct gfs2_alloc *al = ip->i_alloc;
        struct gfs2_rgrpd *rgd = al->al_rgd;
        u32 goal, blk;
        u64 block;
@@ -1460,7 +1486,7 @@ u64 gfs2_alloc_meta(struct gfs2_inode *ip)
        rgd->rd_rg.rg_free--;
 
        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_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
 
        al->al_alloced++;
 
@@ -1485,7 +1511,7 @@ u64 gfs2_alloc_meta(struct gfs2_inode *ip)
 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
-       struct gfs2_alloc *al = &dip->i_alloc;
+       struct gfs2_alloc *al = dip->i_alloc;
        struct gfs2_rgrpd *rgd = al->al_rgd;
        u32 blk;
        u64 block;
@@ -1503,7 +1529,7 @@ u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
        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);
+       gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
 
        al->al_alloced++;
 
@@ -1537,7 +1563,7 @@ void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
        rgd->rd_rg.rg_free += blen;
 
        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_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
 
        gfs2_trans_add_rg(rgd);
 
@@ -1565,7 +1591,7 @@ void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
        rgd->rd_rg.rg_free += blen;
 
        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_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
 
        gfs2_trans_add_rg(rgd);
 
@@ -1585,7 +1611,7 @@ void gfs2_unlink_di(struct inode *inode)
        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_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
        gfs2_trans_add_rg(rgd);
 }
 
@@ -1605,7 +1631,7 @@ static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
        rgd->rd_rg.rg_free++;
 
        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_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
 
        gfs2_statfs_change(sdp, 0, +1, -1);
        gfs2_trans_add_rg(rgd);
@@ -1683,8 +1709,7 @@ void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  *
  */
 
-void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
-                     int flags)
+void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
 {
        unsigned int x;
 
@@ -1692,7 +1717,7 @@ void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
                                GFP_NOFS | __GFP_NOFAIL);
        for (x = 0; x < rlist->rl_rgrps; x++)
                gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
-                               state, flags,
+                               state, 0,
                                &rlist->rl_ghs[x]);
 }