[PATCH 03/27] GFS2: Add structure to contain rgrp, bitmap, offset tuple

2012-09-26 Thread Steven Whitehouse
This patch introduces a new structure, gfs2_rbm, which is a
tuple of a resource group, a bitmap within the resource group
and an offset within that bitmap. This is designed to make
manipulating these sets of variables easier. There is also a
new helper function which converts this representation back
to a disk block address.

In addition, the rbtree nodes which are used for the reservations
were not being correctly initialised, which is now fixed. Also,
the tracing was not passing through the inode where it should
have been. That is mostly fixed aside from one corner case. This
needs to be revisited since there can also be a NULL rgrp in
some cases which results in the device being incorrect in the
trace.

This is intended to be the first step towards cleaning up some
of the allocation code, and some further bug fixes.

Signed-off-by: Steven Whitehouse 

diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c
index 49cd7dd..1fd3ae2 100644
--- a/fs/gfs2/bmap.c
+++ b/fs/gfs2/bmap.c
@@ -786,7 +786,7 @@ static int do_strip(struct gfs2_inode *ip, struct 
buffer_head *dibh,
goto out_rlist;
 
if (gfs2_rs_active(ip->i_res)) /* needs to be done with the rgrp glock 
held */
-   gfs2_rs_deltree(ip->i_res);
+   gfs2_rs_deltree(ip, ip->i_res);
 
error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
 RES_INDIRECT + RES_STATFS + RES_QUOTA,
diff --git a/fs/gfs2/incore.h b/fs/gfs2/incore.h
index 52078a1..d5e2546 100644
--- a/fs/gfs2/incore.h
+++ b/fs/gfs2/incore.h
@@ -102,6 +102,17 @@ struct gfs2_rgrpd {
u32 rd_rs_cnt;  /* count of current reservations */
 };
 
+struct gfs2_rbm {
+   struct gfs2_rgrpd *rgd;
+   struct gfs2_bitmap *bi; /* Bitmap must belong to the rgd */
+   u32 offset; /* The offset is bitmap relative */
+};
+
+static inline u64 gfs2_rbm_to_block(const struct gfs2_rbm *rbm)
+{
+   return rbm->rgd->rd_data0 + (rbm->bi->bi_start * GFS2_NBBY) + 
rbm->offset;
+}
+
 enum gfs2_state_bits {
BH_Pinned = BH_PrivateStart,
BH_Escaped = BH_PrivateStart + 1,
@@ -251,13 +262,11 @@ struct gfs2_blkreserv {
atomic_t rs_sizehint; /* hint of the write size */
 
/* components used during get_local_rgrp (step 3): */
-   struct gfs2_rgrpd *rs_rgd;/* pointer to the gfs2_rgrpd */
+   struct gfs2_rbm rs_rbm;
struct gfs2_holder rs_rgd_gh; /* Filled in by get_local_rgrp */
struct rb_node rs_node;   /* link to other block reservations */
 
/* components used during block searches and assignments (step 4): */
-   struct gfs2_bitmap *rs_bi;/* bitmap for the current allocation */
-   u32 rs_biblk; /* start block relative to the bi */
u32 rs_free;  /* how many blocks are still free */
 
/* ancillary quota stuff */
diff --git a/fs/gfs2/rgrp.c b/fs/gfs2/rgrp.c
index a2b43bb..eaa4188 100644
--- a/fs/gfs2/rgrp.c
+++ b/fs/gfs2/rgrp.c
@@ -192,7 +192,7 @@ static inline u64 gfs2_bit_search(const __le64 *ptr, u64 
mask, u8 state)
  */
 static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs)
 {
-   u64 startblk = gfs2_rs_startblk(rs);
+   u64 startblk = gfs2_rbm_to_block(>rs_rbm);
 
if (blk >= startblk + rs->rs_free)
return 1;
@@ -487,6 +487,8 @@ int gfs2_rs_alloc(struct gfs2_inode *ip)
if (!res)
error = -ENOMEM;
 
+   rb_init_node(>rs_node);
+
down_write(>i_rw_mutex);
if (ip->i_res)
kmem_cache_free(gfs2_rsrv_cachep, res);
@@ -499,8 +501,8 @@ int gfs2_rs_alloc(struct gfs2_inode *ip)
 static void dump_rs(struct seq_file *seq, struct gfs2_blkreserv *rs)
 {
gfs2_print_dbg(seq, "  r: %llu s:%llu b:%u f:%u\n",
-  rs->rs_rgd->rd_addr, gfs2_rs_startblk(rs), rs->rs_biblk,
-  rs->rs_free);
+  rs->rs_rbm.rgd->rd_addr, gfs2_rbm_to_block(>rs_rbm), 
+  rs->rs_rbm.offset, rs->rs_free);
 }
 
 /**
@@ -508,40 +510,28 @@ static void dump_rs(struct seq_file *seq, struct 
gfs2_blkreserv *rs)
  * @rs: The reservation to remove
  *
  */
-static void __rs_deltree(struct gfs2_blkreserv *rs)
+static void __rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
 {
struct gfs2_rgrpd *rgd;
 
if (!gfs2_rs_active(rs))
return;
 
-   rgd = rs->rs_rgd;
-   /* We can't do this: The reason is that when the rgrp is invalidated,
-  it's in the "middle" of acquiring the glock, but the HOLDER bit
-  isn't set yet:
-  BUG_ON(!gfs2_glock_is_locked_by_me(rs->rs_rgd->rd_gl));*/
-   trace_gfs2_rs(NULL, rs, TRACE_RS_TREEDEL);
-
-   if (!RB_EMPTY_ROOT(>rd_rstree))
-   rb_erase(>rs_node, >rd_rstree);
+   rgd = rs->rs_rbm.rgd;
+   trace_gfs2_rs(ip, rs, TRACE_RS_TREEDEL);
+   rb_erase(>rs_node, >rd_rstree);
+   rb_init_node(>rs_node);
 

[PATCH 03/27] GFS2: Add structure to contain rgrp, bitmap, offset tuple

2012-09-26 Thread Steven Whitehouse
This patch introduces a new structure, gfs2_rbm, which is a
tuple of a resource group, a bitmap within the resource group
and an offset within that bitmap. This is designed to make
manipulating these sets of variables easier. There is also a
new helper function which converts this representation back
to a disk block address.

In addition, the rbtree nodes which are used for the reservations
were not being correctly initialised, which is now fixed. Also,
the tracing was not passing through the inode where it should
have been. That is mostly fixed aside from one corner case. This
needs to be revisited since there can also be a NULL rgrp in
some cases which results in the device being incorrect in the
trace.

This is intended to be the first step towards cleaning up some
of the allocation code, and some further bug fixes.

Signed-off-by: Steven Whitehouse swhit...@redhat.com

diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c
index 49cd7dd..1fd3ae2 100644
--- a/fs/gfs2/bmap.c
+++ b/fs/gfs2/bmap.c
@@ -786,7 +786,7 @@ static int do_strip(struct gfs2_inode *ip, struct 
buffer_head *dibh,
goto out_rlist;
 
if (gfs2_rs_active(ip-i_res)) /* needs to be done with the rgrp glock 
held */
-   gfs2_rs_deltree(ip-i_res);
+   gfs2_rs_deltree(ip, ip-i_res);
 
error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
 RES_INDIRECT + RES_STATFS + RES_QUOTA,
diff --git a/fs/gfs2/incore.h b/fs/gfs2/incore.h
index 52078a1..d5e2546 100644
--- a/fs/gfs2/incore.h
+++ b/fs/gfs2/incore.h
@@ -102,6 +102,17 @@ struct gfs2_rgrpd {
u32 rd_rs_cnt;  /* count of current reservations */
 };
 
+struct gfs2_rbm {
+   struct gfs2_rgrpd *rgd;
+   struct gfs2_bitmap *bi; /* Bitmap must belong to the rgd */
+   u32 offset; /* The offset is bitmap relative */
+};
+
+static inline u64 gfs2_rbm_to_block(const struct gfs2_rbm *rbm)
+{
+   return rbm-rgd-rd_data0 + (rbm-bi-bi_start * GFS2_NBBY) + 
rbm-offset;
+}
+
 enum gfs2_state_bits {
BH_Pinned = BH_PrivateStart,
BH_Escaped = BH_PrivateStart + 1,
@@ -251,13 +262,11 @@ struct gfs2_blkreserv {
atomic_t rs_sizehint; /* hint of the write size */
 
/* components used during get_local_rgrp (step 3): */
-   struct gfs2_rgrpd *rs_rgd;/* pointer to the gfs2_rgrpd */
+   struct gfs2_rbm rs_rbm;
struct gfs2_holder rs_rgd_gh; /* Filled in by get_local_rgrp */
struct rb_node rs_node;   /* link to other block reservations */
 
/* components used during block searches and assignments (step 4): */
-   struct gfs2_bitmap *rs_bi;/* bitmap for the current allocation */
-   u32 rs_biblk; /* start block relative to the bi */
u32 rs_free;  /* how many blocks are still free */
 
/* ancillary quota stuff */
diff --git a/fs/gfs2/rgrp.c b/fs/gfs2/rgrp.c
index a2b43bb..eaa4188 100644
--- a/fs/gfs2/rgrp.c
+++ b/fs/gfs2/rgrp.c
@@ -192,7 +192,7 @@ static inline u64 gfs2_bit_search(const __le64 *ptr, u64 
mask, u8 state)
  */
 static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs)
 {
-   u64 startblk = gfs2_rs_startblk(rs);
+   u64 startblk = gfs2_rbm_to_block(rs-rs_rbm);
 
if (blk = startblk + rs-rs_free)
return 1;
@@ -487,6 +487,8 @@ int gfs2_rs_alloc(struct gfs2_inode *ip)
if (!res)
error = -ENOMEM;
 
+   rb_init_node(res-rs_node);
+
down_write(ip-i_rw_mutex);
if (ip-i_res)
kmem_cache_free(gfs2_rsrv_cachep, res);
@@ -499,8 +501,8 @@ int gfs2_rs_alloc(struct gfs2_inode *ip)
 static void dump_rs(struct seq_file *seq, struct gfs2_blkreserv *rs)
 {
gfs2_print_dbg(seq,   r: %llu s:%llu b:%u f:%u\n,
-  rs-rs_rgd-rd_addr, gfs2_rs_startblk(rs), rs-rs_biblk,
-  rs-rs_free);
+  rs-rs_rbm.rgd-rd_addr, gfs2_rbm_to_block(rs-rs_rbm), 
+  rs-rs_rbm.offset, rs-rs_free);
 }
 
 /**
@@ -508,40 +510,28 @@ static void dump_rs(struct seq_file *seq, struct 
gfs2_blkreserv *rs)
  * @rs: The reservation to remove
  *
  */
-static void __rs_deltree(struct gfs2_blkreserv *rs)
+static void __rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
 {
struct gfs2_rgrpd *rgd;
 
if (!gfs2_rs_active(rs))
return;
 
-   rgd = rs-rs_rgd;
-   /* We can't do this: The reason is that when the rgrp is invalidated,
-  it's in the middle of acquiring the glock, but the HOLDER bit
-  isn't set yet:
-  BUG_ON(!gfs2_glock_is_locked_by_me(rs-rs_rgd-rd_gl));*/
-   trace_gfs2_rs(NULL, rs, TRACE_RS_TREEDEL);
-
-   if (!RB_EMPTY_ROOT(rgd-rd_rstree))
-   rb_erase(rs-rs_node, rgd-rd_rstree);
+   rgd = rs-rs_rbm.rgd;
+   trace_gfs2_rs(ip, rs, TRACE_RS_TREEDEL);
+   rb_erase(rs-rs_node, rgd-rd_rstree);
+