From: Noboru Asai <asai@sijam.com>
To: hsiangkao@linux.alibaba.com
Cc: linux-erofs@lists.ozlabs.org
Subject: [PATCH] erofs-utils: simplify file handling
Date: Tue, 30 Apr 2024 15:37:31 +0900 [thread overview]
Message-ID: <20240430063731.1013892-1-asai@sijam.com> (raw)
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@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
next reply other threads:[~2024-04-30 6:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-30 6:37 Noboru Asai [this message]
2024-04-30 6:46 ` [PATCH] erofs-utils: simplify file handling Gao Xiang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240430063731.1013892-1-asai@sijam.com \
--to=asai@sijam.com \
--cc=hsiangkao@linux.alibaba.com \
--cc=linux-erofs@lists.ozlabs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).