Rework find_metapath to receive a pointer to a metapath structure
instead of allocating memory and exiting on allocation failure. Also
simplify some other aspects of the function and update its callers.

Signed-off-by: Andrew Price <[email protected]>
---
 gfs2/libgfs2/fs_ops.c  | 39 ++++++++++++++-------------------------
 gfs2/libgfs2/gfs1.c    | 17 +++++++----------
 gfs2/libgfs2/libgfs2.h |  2 +-
 3 files changed, 22 insertions(+), 36 deletions(-)

diff --git a/gfs2/libgfs2/fs_ops.c b/gfs2/libgfs2/fs_ops.c
index 4a84687..03b1c41 100644
--- a/gfs2/libgfs2/fs_ops.c
+++ b/gfs2/libgfs2/fs_ops.c
@@ -393,24 +393,16 @@ void build_height(struct gfs2_inode *ip, int height)
        }
 }
 
-struct metapath *find_metapath(struct gfs2_inode *ip, uint64_t block)
+void find_metapath(struct gfs2_inode *ip, uint64_t block, struct metapath *mp)
 {
-       struct gfs2_sbd *sdp = ip->i_sbd;
-       struct metapath *mp;
-       uint64_t b = block;
-       unsigned int i;
+       const uint32_t inptrs = ip->i_sbd->sd_inptrs;
+       unsigned int i = ip->i_di.di_height;
 
-       mp = calloc(1, sizeof(struct metapath));
-       if (mp == NULL) {
-               fprintf(stderr, "Out of memory in %s\n", __FUNCTION__);
-               exit(-1);
-       }
-       for (i = ip->i_di.di_height; i--;) {
-               mp->mp_list[i] = b % sdp->sd_inptrs;
-               b /= sdp->sd_inptrs;
+       memset(mp, 0, sizeof(struct metapath));
+       while (i--) {
+               mp->mp_list[i] = block % inptrs;
+               block /= inptrs;
        }
-
-       return mp;
 }
 
 void lookup_block(struct gfs2_inode *ip, struct gfs2_buffer_head *bh,
@@ -448,7 +440,7 @@ void block_map(struct gfs2_inode *ip, uint64_t lblock, int 
*new,
 {
        struct gfs2_sbd *sdp = ip->i_sbd;
        struct gfs2_buffer_head *bh;
-       struct metapath *mp;
+       struct metapath mp;
        int create = *new;
        unsigned int bsize;
        unsigned int height;
@@ -479,17 +471,17 @@ void block_map(struct gfs2_inode *ip, uint64_t lblock, 
int *new,
                build_height(ip, height);
        }
 
-       mp = find_metapath(ip, lblock);
+       find_metapath(ip, lblock, &mp);
        end_of_metadata = ip->i_di.di_height - 1;
 
        bh = ip->i_bh;
 
        for (x = 0; x < end_of_metadata; x++) {
-               lookup_block(ip, bh, x, mp, create, new, dblock);
+               lookup_block(ip, bh, x, &mp, create, new, dblock);
                if (bh != ip->i_bh)
                        brelse(bh);
                if (!*dblock)
-                       goto out;
+                       return;
 
                if (*new) {
                        struct gfs2_meta_header mh;
@@ -507,7 +499,7 @@ void block_map(struct gfs2_inode *ip, uint64_t lblock, int 
*new,
        }
 
        if (!prealloc)
-               lookup_block(ip, bh, end_of_metadata, mp, create, new, dblock);
+               lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
 
        if (extlen && *dblock) {
                *extlen = 1;
@@ -519,8 +511,8 @@ void block_map(struct gfs2_inode *ip, uint64_t lblock, int 
*new,
 
                        nptrs = (end_of_metadata) ? sdp->sd_inptrs : 
sdp->sd_diptrs;
 
-                       while (++mp->mp_list[end_of_metadata] < nptrs) {
-                               lookup_block(ip, bh, end_of_metadata, mp, 
FALSE, &tmp_new,
+                       while (++mp.mp_list[end_of_metadata] < nptrs) {
+                               lookup_block(ip, bh, end_of_metadata, &mp, 
FALSE, &tmp_new,
                                                         &tmp_dblock);
 
                                if (*dblock + *extlen != tmp_dblock)
@@ -533,9 +525,6 @@ void block_map(struct gfs2_inode *ip, uint64_t lblock, int 
*new,
 
        if (bh != ip->i_bh)
                brelse(bh);
-
- out:
-       free(mp);
 }
 
 static void
diff --git a/gfs2/libgfs2/gfs1.c b/gfs2/libgfs2/gfs1.c
index 89fb898..619542c 100644
--- a/gfs2/libgfs2/gfs1.c
+++ b/gfs2/libgfs2/gfs1.c
@@ -76,7 +76,7 @@ void gfs1_block_map(struct gfs2_inode *ip, uint64_t lblock, 
int *new,
 {
        struct gfs2_sbd *sdp = ip->i_sbd;
        struct gfs2_buffer_head *bh;
-       struct metapath *mp;
+       struct metapath mp;
        int create = *new;
        unsigned int bsize;
        unsigned int height;
@@ -107,17 +107,17 @@ void gfs1_block_map(struct gfs2_inode *ip, uint64_t 
lblock, int *new,
                build_height(ip, height);
        }
 
-       mp = find_metapath(ip, lblock);
+       find_metapath(ip, lblock, &mp);
        end_of_metadata = ip->i_di.di_height - 1;
 
        bh = ip->i_bh;
 
        for (x = 0; x < end_of_metadata; x++) {
-               gfs1_lookup_block(ip, bh, x, mp, create, new, dblock);
+               gfs1_lookup_block(ip, bh, x, &mp, create, new, dblock);
                if (bh != ip->i_bh)
                        brelse(bh);
                if (!*dblock)
-                       goto out;
+                       return;
 
                if (*new) {
                        struct gfs2_meta_header mh;
@@ -136,7 +136,7 @@ void gfs1_block_map(struct gfs2_inode *ip, uint64_t lblock, 
int *new,
        }
 
        if (!prealloc)
-               gfs1_lookup_block(ip, bh, end_of_metadata, mp, create, new,
+               gfs1_lookup_block(ip, bh, end_of_metadata, &mp, create, new,
                                  dblock);
 
        if (extlen && *dblock) {
@@ -149,8 +149,8 @@ void gfs1_block_map(struct gfs2_inode *ip, uint64_t lblock, 
int *new,
 
                        nptrs = (end_of_metadata) ? sdp->sd_inptrs : 
sdp->sd_diptrs;
 
-                       while (++mp->mp_list[end_of_metadata] < nptrs) {
-                               gfs1_lookup_block(ip, bh, end_of_metadata, mp,
+                       while (++mp.mp_list[end_of_metadata] < nptrs) {
+                               gfs1_lookup_block(ip, bh, end_of_metadata, &mp,
                                                  FALSE, &tmp_new,
                                                  &tmp_dblock);
 
@@ -164,9 +164,6 @@ void gfs1_block_map(struct gfs2_inode *ip, uint64_t lblock, 
int *new,
 
        if (bh != ip->i_bh)
                brelse(bh);
-
- out:
-       free(mp);
 }
 
 int gfs1_writei(struct gfs2_inode *ip, char *buf, uint64_t offset,
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
index 42f9d44..07cb221 100644
--- a/gfs2/libgfs2/libgfs2.h
+++ b/gfs2/libgfs2/libgfs2.h
@@ -437,7 +437,7 @@ extern void build_rgrps(struct gfs2_sbd *sdp, int write);
 #define IS_LEAF     (1)
 #define IS_DINODE   (2)
 
-extern struct metapath *find_metapath(struct gfs2_inode *ip, uint64_t block);
+extern void find_metapath(struct gfs2_inode *ip, uint64_t block, struct 
metapath *mp);
 extern void lookup_block(struct gfs2_inode *ip, struct gfs2_buffer_head *bh,
                         unsigned int height, struct metapath *mp,
                         int create, int *new, uint64_t *block);
-- 
1.8.3.1

Reply via email to