Split erofs_begin_compressed_file() into three interfaces:
 - erofs_prepare_compressed_file();
 - erofs_bind_compressed_file_with_fd();
 - erofs_begin_compressed_file().

Signed-off-by: Gao Xiang <[email protected]>
---
 lib/compress.c          | 74 +++++++++++++++++++++++++----------------
 lib/inode.c             | 16 ++++++---
 lib/liberofs_compress.h |  7 ++--
 3 files changed, 63 insertions(+), 34 deletions(-)

diff --git a/lib/compress.c b/lib/compress.c
index 74d40b1..1417967 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -35,6 +35,7 @@ struct z_erofs_extent_item {
 };
 
 struct z_erofs_compress_ictx {         /* inode context */
+       struct erofs_vfile _vf, *vf;
        struct erofs_importer *im;
        struct erofs_inode *inode;
        struct erofs_compress_cfg *ccfg;
@@ -1258,14 +1259,14 @@ int z_erofs_compress_segment(struct 
z_erofs_compress_sctx *ctx,
        bool frag = params->fragments && !erofs_is_packed_inode(inode) &&
                !erofs_is_metabox_inode(inode) &&
                ctx->seg_idx >= ictx->seg_num - 1;
-       struct erofs_vfile vf = { .fd = ictx->fd };
+       struct erofs_vfile *vf = ictx->vf;
        int ret;
 
        DBG_BUGON(offset != -1 && frag && inode->fragment_size);
        if (offset != -1 && frag && !inode->fragment_size &&
            params->fragdedupe != EROFS_FRAGDEDUPE_OFF) {
-               ret = erofs_fragment_findmatch(inode,
-                                              &vf, ictx->fpos, ictx->tofh);
+               ret = erofs_fragment_findmatch(inode, vf, ictx->fpos,
+                                              ictx->tofh);
                if (ret < 0)
                        return ret;
                if (inode->fragment_size > ctx->remaining)
@@ -1281,8 +1282,8 @@ int z_erofs_compress_segment(struct z_erofs_compress_sctx 
*ctx,
                int ret;
 
                ret = (offset == -1 ?
-                       erofs_io_read(&vf, ctx->queue + ctx->tail, rx) :
-                       erofs_io_pread(&vf, ctx->queue + ctx->tail, rx,
+                       erofs_io_read(vf, ctx->queue + ctx->tail, rx) :
+                       erofs_io_pread(vf, ctx->queue + ctx->tail, rx,
                                       ictx->fpos + offset));
                if (ret != rx)
                        return -errno;
@@ -1780,6 +1781,8 @@ int z_erofs_mt_global_exit(void)
        return 0;
 }
 #else
+
+
 static int z_erofs_mt_global_init(struct erofs_importer *im)
 {
        z_erofs_mt_enabled = false;
@@ -1792,8 +1795,8 @@ int z_erofs_mt_global_exit(void)
 }
 #endif
 
-void *erofs_begin_compressed_file(struct erofs_importer *im,
-                                 struct erofs_inode *inode, int fd, u64 fpos)
+void *erofs_prepare_compressed_file(struct erofs_importer *im,
+                                   struct erofs_inode *inode)
 {
        const struct erofs_importer_params *params = im->params;
        struct erofs_sb_info *sbi = inode->sbi;
@@ -1801,7 +1804,6 @@ void *erofs_begin_compressed_file(struct erofs_importer 
*im,
        bool frag = params->fragments && !erofs_is_packed_inode(inode) &&
                !erofs_is_metabox_inode(inode);
        bool all_fragments = params->all_fragments && frag;
-       int ret;
 
        /* initialize per-file compression setting */
        inode->z_advise = 0;
@@ -1835,7 +1837,6 @@ void *erofs_begin_compressed_file(struct erofs_importer 
*im,
        }
        ictx->im = im;
        ictx->inode = inode;
-       ictx->fd = fd;
        if (erofs_is_metabox_inode(inode))
                ictx->ccfg = &sbi->zmgr->ccfg[cfg.c_mkfs_metabox_algid];
        else
@@ -1848,10 +1849,33 @@ void *erofs_begin_compressed_file(struct erofs_importer 
*im,
        if (params->fragments && !params->dedupe && !ictx->data_unaligned)
                inode->z_advise |= Z_EROFS_ADVISE_INTERLACED_PCLUSTER;
 
-       if (frag) {
-               struct erofs_vfile vf = { .fd = fd };
+       init_list_head(&ictx->extents);
+       ictx->fix_dedupedfrag = false;
+       ictx->fragemitted = false;
+       ictx->dedupe = false;
+       return ictx;
+}
+
+void erofs_bind_compressed_file_with_fd(struct z_erofs_compress_ictx *ictx,
+                                       int fd, u64 fpos)
+{
+       ictx->_vf = (struct erofs_vfile){ .fd = fd };
+       ictx->vf = &ictx->_vf;
+       ictx->fpos = fpos;
+}
+
+int erofs_begin_compressed_file(struct z_erofs_compress_ictx *ictx)
+{
+       const struct erofs_importer_params *params = ictx->im->params;
+       struct erofs_inode *inode = ictx->inode;
+       bool frag = params->fragments && !erofs_is_packed_inode(inode) &&
+               !erofs_is_metabox_inode(inode);
+       bool all_fragments = params->all_fragments && frag;
+       int ret;
 
-               ictx->tofh = z_erofs_fragments_tofh(inode, &vf, fpos);
+       if (frag) {
+               ictx->tofh = z_erofs_fragments_tofh(inode,
+                                                   ictx->vf, ictx->fpos);
                if (ictx == &g_ictx &&
                    params->fragdedupe != EROFS_FRAGDEDUPE_OFF) {
                        /*
@@ -1859,9 +1883,9 @@ void *erofs_begin_compressed_file(struct erofs_importer 
*im,
                         * parts into the packed inode.
                         */
                        ret = erofs_fragment_findmatch(inode,
-                                                      &vf, fpos, ictx->tofh);
+                                       ictx->vf, ictx->fpos, ictx->tofh);
                        if (ret < 0)
-                               goto err_free_ictx;
+                               goto err_out;
 
                        if (params->fragdedupe == EROFS_FRAGDEDUPE_INODE &&
                            inode->fragment_size < inode->i_size) {
@@ -1871,36 +1895,30 @@ void *erofs_begin_compressed_file(struct erofs_importer 
*im,
                        }
                }
        }
-       ictx->fpos = fpos;
-       init_list_head(&ictx->extents);
-       ictx->fix_dedupedfrag = false;
-       ictx->fragemitted = false;
-       ictx->dedupe = false;
 
        if (all_fragments && !inode->fragment_size) {
-               ret = erofs_pack_file_from_fd(inode,
-                       &((struct erofs_vfile){ .fd = fd }), fpos, ictx->tofh);
+               ret = erofs_pack_file_from_fd(inode, ictx->vf, ictx->fpos,
+                                             ictx->tofh);
                if (ret)
-                       goto err_free_idata;
+                       goto err_out;
        }
+
 #ifdef EROFS_MT_ENABLED
        if (ictx != &g_ictx) {
                ret = z_erofs_mt_compress(ictx);
                if (ret)
-                       goto err_free_idata;
+                       goto err_out;
        }
 #endif
-       return ictx;
-
-err_free_idata:
+       return 0;
+err_out:
        if (inode->idata) {
                free(inode->idata);
                inode->idata = NULL;
        }
-err_free_ictx:
        if (ictx != &g_ictx)
                free(ictx);
-       return ERR_PTR(ret);
+       return ret;
 }
 
 int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx)
diff --git a/lib/inode.c b/lib/inode.c
index 14a65af..e7c3edf 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -1790,6 +1790,7 @@ static int erofs_mkfs_begin_nondirectory(struct 
erofs_importer *im,
 {
        struct erofs_mkfs_job_ndir_ctx ctx =
                { .inode = inode, .fd = -1 };
+       int ret;
 
        if (S_ISREG(inode->i_mode) && inode->i_size) {
                switch (inode->datasource) {
@@ -1808,10 +1809,14 @@ static int erofs_mkfs_begin_nondirectory(struct 
erofs_importer *im,
                }
                if (ctx.fd >= 0 && cfg.c_compr_opts[0].alg &&
                    erofs_file_is_compressible(im, inode)) {
-                       ctx.ictx = erofs_begin_compressed_file(im, inode,
-                                                       ctx.fd, ctx.fpos);
+                       ctx.ictx = erofs_prepare_compressed_file(im, inode);
                        if (IS_ERR(ctx.ictx))
                                return PTR_ERR(ctx.ictx);
+                       erofs_bind_compressed_file_with_fd(ctx.ictx,
+                                                          ctx.fd, ctx.fpos);
+                       ret = erofs_begin_compressed_file(ctx.ictx);
+                       if (ret)
+                               return ret;
                }
        }
        return erofs_mkfs_go(im, EROFS_MKFS_JOB_NDIR, &ctx, sizeof(ctx));
@@ -2129,11 +2134,14 @@ struct erofs_inode 
*erofs_mkfs_build_special_from_fd(struct erofs_importer *im,
 
        if (cfg.c_compr_opts[0].alg &&
            erofs_file_is_compressible(im, inode)) {
-               ictx = erofs_begin_compressed_file(im, inode, fd, 0);
+               ictx = erofs_prepare_compressed_file(im, inode);
                if (IS_ERR(ictx))
                        return ERR_CAST(ictx);
 
-               DBG_BUGON(!ictx);
+               erofs_bind_compressed_file_with_fd(ictx, fd, 0);
+               ret = erofs_begin_compressed_file(ictx);
+               if (ret)
+                       return ERR_PTR(ret);
                ret = erofs_write_compressed_file(ictx);
                if (!ret)
                        goto out;
diff --git a/lib/liberofs_compress.h b/lib/liberofs_compress.h
index e0f4d24..8b39735 100644
--- a/lib/liberofs_compress.h
+++ b/lib/liberofs_compress.h
@@ -15,8 +15,11 @@
 struct z_erofs_compress_ictx;
 
 void z_erofs_drop_inline_pcluster(struct erofs_inode *inode);
-void *erofs_begin_compressed_file(struct erofs_importer *im,
-                                 struct erofs_inode *inode, int fd, u64 fpos);
+void *erofs_prepare_compressed_file(struct erofs_importer *im,
+                                   struct erofs_inode *inode);
+void erofs_bind_compressed_file_with_fd(struct z_erofs_compress_ictx *ictx,
+                                       int fd, u64 fpos);
+int erofs_begin_compressed_file(struct z_erofs_compress_ictx *ictx);
 int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx);
 
 int z_erofs_compress_init(struct erofs_importer *im);
-- 
2.43.5


Reply via email to