[PATCH] erofs-utils: simplify file handling

Noboru Asai asai at sijam.com
Tue Apr 30 16:37:31 AEST 2024


Opening files again when data compression doesn't save space,
simplify file handling.

* remove dup and lseek.
* call pthread_cond_signal once per file.

I think the probability of the above case occurring is a few percent.

Signed-off-by: Noboru Asai <asai at sijam.com>
---
 lib/compress.c | 11 ++++++-----
 lib/inode.c    | 24 ++++++++++++------------
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/lib/compress.c b/lib/compress.c
index 7fef698..4c7351f 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -1261,8 +1261,10 @@ void z_erofs_mt_workfn(struct erofs_work *work, void *tlsp)
 out:
 	cwork->errcode = ret;
 	pthread_mutex_lock(&ictx->mutex);
-	++ictx->nfini;
-	pthread_cond_signal(&ictx->cond);
+	if (++ictx->nfini == ictx->seg_num) {
+		close(ictx->fd);
+		pthread_cond_signal(&ictx->cond);
+	}
 	pthread_mutex_unlock(&ictx->mutex);
 }
 
@@ -1406,7 +1408,6 @@ int erofs_mt_write_compressed_file(struct z_erofs_compress_ictx *ictx)
 			blkaddr - compressed_blocks, compressed_blocks);
 
 out:
-	close(ictx->fd);
 	free(ictx);
 	return ret;
 }
@@ -1456,7 +1457,6 @@ void *erofs_begin_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)
 		ictx = malloc(sizeof(*ictx));
 		if (!ictx)
 			return ERR_PTR(-ENOMEM);
-		ictx->fd = dup(fd);
 	} else {
 #ifdef EROFS_MT_ENABLED
 		pthread_mutex_lock(&g_ictx.mutex);
@@ -1466,8 +1466,8 @@ void *erofs_begin_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)
 		pthread_mutex_unlock(&g_ictx.mutex);
 #endif
 		ictx = &g_ictx;
-		ictx->fd = fd;
 	}
+	ictx->fd = fd;
 
 	ictx->ccfg = &erofs_ccfg[inode->z_algorithmtype[0]];
 	inode->z_algorithmtype[0] = ictx->ccfg->algorithmtype;
@@ -1551,6 +1551,7 @@ int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx)
 	init_list_head(&sctx.extents);
 
 	ret = z_erofs_compress_segment(&sctx, -1, blkaddr);
+	close(ictx->fd);
 	if (ret)
 		goto err_free_idata;
 
diff --git a/lib/inode.c b/lib/inode.c
index 44d684f..a30975b 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -1112,27 +1112,27 @@ static void erofs_fixup_meta_blkaddr(struct erofs_inode *rootdir)
 struct erofs_mkfs_job_ndir_ctx {
 	struct erofs_inode *inode;
 	void *ictx;
-	int fd;
 };
 
 static int erofs_mkfs_job_write_file(struct erofs_mkfs_job_ndir_ctx *ctx)
 {
 	struct erofs_inode *inode = ctx->inode;
+	int fd;
 	int ret;
 
 	if (ctx->ictx) {
 		ret = erofs_write_compressed_file(ctx->ictx);
 		if (ret != -ENOSPC)
-			goto out;
-		if (lseek(ctx->fd, 0, SEEK_SET) < 0) {
-			ret = -errno;
-			goto out;
-		}
+			return ret;
 	}
+
 	/* fallback to all data uncompressed */
-	ret = erofs_write_unencoded_file(inode, ctx->fd, 0);
-out:
-	close(ctx->fd);
+	fd = open(inode->i_srcpath, O_RDONLY | O_BINARY);
+	if (fd < 0)
+		return -errno;
+	ret = erofs_write_unencoded_file(inode, fd, 0);
+	close(fd);
+
 	return ret;
 }
 
@@ -1393,14 +1393,14 @@ static int erofs_mkfs_handle_inode(struct erofs_inode *inode)
 		struct erofs_mkfs_job_ndir_ctx ctx = { .inode = inode };
 
 		if (!S_ISLNK(inode->i_mode) && inode->i_size) {
-			ctx.fd = open(inode->i_srcpath, O_RDONLY | O_BINARY);
-			if (ctx.fd < 0)
+			int fd = open(inode->i_srcpath, O_RDONLY | O_BINARY);
+			if (fd < 0)
 				return -errno;
 
 			if (cfg.c_compr_opts[0].alg &&
 			    erofs_file_is_compressible(inode)) {
 				ctx.ictx = erofs_begin_compressed_file(inode,
-								ctx.fd, 0);
+								fd, 0);
 				if (IS_ERR(ctx.ictx))
 					return PTR_ERR(ctx.ictx);
 			}
-- 
2.44.0



More information about the Linux-erofs mailing list