This allows the caller to call inode_put() when it's convenient and also
allows the debug message printing to be moved out of the function.
fsck.gfs2 passes the function by reference so it needs a shim until the
other builder functions can be given the same signature.

Signed-off-by: Andrew Price <anpr...@redhat.com>
---
 gfs2/convert/gfs2_convert.c |  5 ++---
 gfs2/fsck/initialize.c      | 16 +++++++++-------
 gfs2/fsck/pass1.c           | 11 ++++++++++-
 gfs2/libgfs2/libgfs2.h      |  2 +-
 gfs2/libgfs2/structures.c   | 14 ++------------
 gfs2/mkfs/main_mkfs.c       |  9 ++++++---
 6 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/gfs2/convert/gfs2_convert.c b/gfs2/convert/gfs2_convert.c
index 210528c9..93c9755b 100644
--- a/gfs2/convert/gfs2_convert.c
+++ b/gfs2/convert/gfs2_convert.c
@@ -2350,13 +2350,12 @@ int main(int argc, char **argv)
                        exit(-1);
                }
                /* Create the statfs file */
-               error = build_statfs(&sb2); /* Does not do inode_put */
-               if (error) {
+               sb2.md.statfs = build_statfs(&sb2); /* Does not do inode_put */
+               if (sb2.md.statfs == NULL) {
                        log_crit(_("Error building statfs inode: %s\n"),
                                 strerror(error));
                        exit(-1);
                }
-               gfs2_lookupi(sb2.master_dir, "statfs", 6, &sb2.md.statfs);
                do_init_statfs(&sb2);
 
                /* Create the resource group index file */
diff --git a/gfs2/fsck/initialize.c b/gfs2/fsck/initialize.c
index a46d3ecb..bb2bcfb7 100644
--- a/gfs2/fsck/initialize.c
+++ b/gfs2/fsck/initialize.c
@@ -524,12 +524,14 @@ static int rebuild_master(struct gfs2_sbd *sdp)
                        exit(FSCK_ERROR);
                }
        } else {
-               err = build_statfs(sdp);
-               if (err) {
+               sdp->md.statfs = build_statfs(sdp);
+               if (sdp->md.statfs == NULL) {
                        log_crit(_("Error %d building statfs inode\n"), err);
                        exit(FSCK_ERROR);
                }
-               gfs2_lookupi(sdp->master_dir, "statfs", 6, &sdp->md.statfs);
+               /* Write the inode but don't free it, to avoid doing an extra 
lookup */
+               lgfs2_dinode_out(sdp->md.statfs, sdp->md.statfs->i_bh->b_data);
+               bwrite(sdp->md.statfs->i_bh);
        }
 
        if (fix_md.riinode) {
@@ -851,13 +853,13 @@ static int init_system_inodes(struct gfs2_sbd *sdp)
                                   "statfs file; aborting.\n"));
                        goto fail;
                }
-               err = build_statfs(sdp);
-               if (err) {
+               sdp->md.statfs = build_statfs(sdp);
+               if (sdp->md.statfs == NULL) {
                        log_crit(_("Error %d rebuilding statfs inode\n"), err);
                        exit(FSCK_ERROR);
                }
-               gfs2_lookupi(sdp->master_dir, "statfs", 6, &sdp->md.statfs);
-               if (!sdp->md.statfs) {
+               lgfs2_dinode_out(sdp->md.statfs, sdp->md.statfs->i_bh->b_data);
+               if (bwrite(sdp->md.statfs->i_bh) != 0) {
                        log_err( _("Rebuild of statfs system file failed."));
                        log_err( _("fsck.gfs2 cannot continue without "
                                   "a valid statfs file; aborting.\n"));
diff --git a/gfs2/fsck/pass1.c b/gfs2/fsck/pass1.c
index acd9929f..b19292eb 100644
--- a/gfs2/fsck/pass1.c
+++ b/gfs2/fsck/pass1.c
@@ -1628,6 +1628,15 @@ static int fsck_build_inum(struct gfs2_sbd *sdp)
        return 0;
 }
 
+static int fsck_build_statfs(struct gfs2_sbd *sdp)
+{
+       struct gfs2_inode *ip = build_statfs(sdp);
+       if (ip == NULL)
+               return -1;
+       inode_put(&ip);
+       return 0;
+}
+
 static int check_system_inodes(struct gfs2_sbd *sdp)
 {
        int journal_count;
@@ -1662,7 +1671,7 @@ static int check_system_inodes(struct gfs2_sbd *sdp)
                stack;
                return -1;
        }
-       if (check_system_inode(sdp, &sdp->md.statfs, "statfs", build_statfs, 0,
+       if (check_system_inode(sdp, &sdp->md.statfs, "statfs", 
fsck_build_statfs, 0,
                               sdp->master_dir, !sdp->gfs1)) {
                stack;
                return -1;
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
index aea2b676..83fc90ce 100644
--- a/gfs2/libgfs2/libgfs2.h
+++ b/gfs2/libgfs2/libgfs2.h
@@ -778,7 +778,7 @@ extern int build_journal(struct gfs2_sbd *sdp, int j,
                         struct gfs2_inode *jindex);
 extern struct gfs2_inode *lgfs2_build_jindex(struct gfs2_inode *metafs, struct 
lgfs2_inum *jnls, size_t nmemb);
 extern struct gfs2_inode *build_inum(struct gfs2_sbd *sdp);
-extern int build_statfs(struct gfs2_sbd *sdp);
+extern struct gfs2_inode *build_statfs(struct gfs2_sbd *sdp);
 extern int build_rindex(struct gfs2_sbd *sdp);
 extern int build_quota(struct gfs2_sbd *sdp);
 extern int build_root(struct gfs2_sbd *sdp);
diff --git a/gfs2/libgfs2/structures.c b/gfs2/libgfs2/structures.c
index 69cce676..f790adcc 100644
--- a/gfs2/libgfs2/structures.c
+++ b/gfs2/libgfs2/structures.c
@@ -359,23 +359,13 @@ struct gfs2_inode *build_inum(struct gfs2_sbd *sdp)
        return ip;
 }
 
-int build_statfs(struct gfs2_sbd *sdp)
+struct gfs2_inode *build_statfs(struct gfs2_sbd *sdp)
 {
        struct gfs2_inode *ip;
 
        ip = createi(sdp->master_dir, "statfs", S_IFREG | 0600,
                     GFS2_DIF_SYSTEM | GFS2_DIF_JDATA);
-       if (ip == NULL) {
-               return errno;
-       }
-
-       if (cfg_debug) {
-               printf("\nStatFS Inode:\n");
-               lgfs2_dinode_print(ip->i_bh->b_data);
-       }
-
-       inode_put(&ip);
-       return 0;
+       return ip;
 }
 
 int build_rindex(struct gfs2_sbd *sdp)
diff --git a/gfs2/mkfs/main_mkfs.c b/gfs2/mkfs/main_mkfs.c
index 0aff266f..647c969b 100644
--- a/gfs2/mkfs/main_mkfs.c
+++ b/gfs2/mkfs/main_mkfs.c
@@ -1305,12 +1305,15 @@ int main(int argc, char *argv[])
                printf("\nInum Inode:\n");
                lgfs2_dinode_print(sbd.md.inum->i_bh->b_data);
        }
-       error = build_statfs(&sbd);
-       if (error) {
+       sbd.md.statfs = build_statfs(&sbd);
+       if (sbd.md.statfs == NULL) {
                fprintf(stderr, _("Error building '%s': %s\n"), "statfs", 
strerror(errno));
                exit(EXIT_FAILURE);
        }
-       gfs2_lookupi(sbd.master_dir, "statfs", 6, &sbd.md.statfs);
+       if (opts.debug) {
+               printf("\nStatFS Inode:\n");
+               lgfs2_dinode_print(sbd.md.statfs->i_bh->b_data);
+       }
        error = build_rindex(&sbd);
        if (error) {
                fprintf(stderr, _("Error building '%s': %s\n"), "rindex", 
strerror(errno));
-- 
2.34.1

Reply via email to