[PATCH] erofs-utils: lib: introduce erofs_io_close()
Gao Xiang
hsiangkao at linux.alibaba.com
Thu Sep 4 18:01:46 AEST 2025
Since virtual files may have their own `.close()`.
Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
include/erofs/io.h | 3 +++
lib/io.c | 11 +++++++++--
lib/metabox.c | 2 +-
lib/tar.c | 2 +-
mount/main.c | 24 ++++++++++++------------
5 files changed, 26 insertions(+), 16 deletions(-)
diff --git a/include/erofs/io.h b/include/erofs/io.h
index 370765f..a9f6d2e 100644
--- a/include/erofs/io.h
+++ b/include/erofs/io.h
@@ -41,6 +41,7 @@ struct erofs_vfops {
off_t *pos, size_t count);
int (*xcopy)(struct erofs_vfile *vout, off_t pos,
struct erofs_vfile *vin, unsigned int len, bool noseek);
+ void (*close)(struct erofs_vfile *vf);
};
/* don't extend this; instead, use payload for any extra information */
@@ -87,6 +88,8 @@ static inline int erofs_pread(struct erofs_vfile *vf, void *buf,
return read != len ? -EIO : 0;
}
+void erofs_io_close(struct erofs_vfile *vf);
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/io.c b/lib/io.c
index ff3b794..629af9c 100644
--- a/lib/io.c
+++ b/lib/io.c
@@ -380,8 +380,7 @@ out:
void erofs_dev_close(struct erofs_sb_info *sbi)
{
- if (!sbi->bdev.ops)
- close(sbi->bdev.fd);
+ erofs_io_close(&sbi->bdev);
free(sbi->devname);
sbi->devname = NULL;
sbi->bdev.fd = -1;
@@ -657,3 +656,11 @@ int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos,
} while (len);
return 0;
}
+
+void erofs_io_close(struct erofs_vfile *vf)
+{
+ if (vf->ops)
+ return vf->ops->close(vf);
+ close(vf->fd);
+ vf->fd = -1;
+}
diff --git a/lib/metabox.c b/lib/metabox.c
index fdc46eb..abde5e6 100644
--- a/lib/metabox.c
+++ b/lib/metabox.c
@@ -21,7 +21,7 @@ void erofs_metabox_exit(struct erofs_sb_info *sbi)
return;
DBG_BUGON(!m2gr->bmgr);
erofs_buffer_exit(m2gr->bmgr);
- close(m2gr->vf.fd);
+ erofs_io_close(&m2gr->vf);
free(m2gr);
}
diff --git a/lib/tar.c b/lib/tar.c
index fe801e2..c8fd48e 100644
--- a/lib/tar.c
+++ b/lib/tar.c
@@ -65,7 +65,7 @@ void erofs_iostream_close(struct erofs_iostream *ios)
#endif
return;
}
- close(ios->vf.fd);
+ erofs_io_close(&ios->vf);
}
int erofs_iostream_open(struct erofs_iostream *ios, int fd, int decoder)
diff --git a/mount/main.c b/mount/main.c
index a270f0a..149bb53 100644
--- a/mount/main.c
+++ b/mount/main.c
@@ -267,8 +267,8 @@ static void *erofsmount_nbd_loopfn(void *arg)
break;
}
}
- close(ctx->vd.fd);
- close(ctx->sk.fd);
+ erofs_io_close(&ctx->vd);
+ erofs_io_close(&ctx->sk);
return (void *)(uintptr_t)err;
}
@@ -288,15 +288,15 @@ static int erofsmount_startnbd(int nbdfd, const char *source)
err = erofs_nbd_connect(nbdfd, 9, INT64_MAX >> 9);
if (err < 0) {
- close(ctx.vd.fd);
+ erofs_io_close(&ctx.vd);
goto out_closefd;
}
ctx.sk.fd = err;
err = -pthread_create(&th, NULL, erofsmount_nbd_loopfn, &ctx);
if (err) {
- close(ctx.vd.fd);
- close(ctx.sk.fd);
+ erofs_io_close(&ctx.vd);
+ erofs_io_close(&ctx.sk);
goto out_closefd;
}
@@ -392,7 +392,7 @@ static int erofsmount_startnbd_nl(pid_t *pid, const char *source)
err = pipe(pipefd);
if (err < 0) {
err = -errno;
- close(ctx.vd.fd);
+ erofs_io_close(&ctx.vd);
return err;
}
if ((*pid = fork()) == 0) {
@@ -400,12 +400,12 @@ static int erofsmount_startnbd_nl(pid_t *pid, const char *source)
/* Otherwise, NBD disconnect sends SIGPIPE, skipping cleanup */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
- close(ctx.vd.fd);
+ erofs_io_close(&ctx.vd);
exit(EXIT_FAILURE);
}
recp = erofsmount_write_recovery_info(source);
if (IS_ERR(recp)) {
- close(ctx.vd.fd);
+ erofs_io_close(&ctx.vd);
exit(EXIT_FAILURE);
}
num = -1;
@@ -414,7 +414,7 @@ static int erofsmount_startnbd_nl(pid_t *pid, const char *source)
ctx.sk.fd = err;
err = erofsmount_nbd_fix_backend_linkage(num, &recp);
if (err) {
- close(ctx.sk.fd);
+ erofs_io_close(&ctx.sk);
} else {
err = write(pipefd[1], &num, sizeof(int));
if (err < 0)
@@ -427,7 +427,7 @@ static int erofsmount_startnbd_nl(pid_t *pid, const char *source)
}
}
}
- close(ctx.vd.fd);
+ erofs_io_close(&ctx.vd);
out_fork:
(void)unlink(recp);
free(recp);
@@ -529,10 +529,10 @@ static int erofsmount_reattach(const char *target)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
- close(ctx.sk.fd);
+ erofs_io_close(&ctx.sk);
err = 0;
}
- close(ctx.vd.fd);
+ erofs_io_close(&ctx.vd);
err_line:
free(line);
err_identifier:
--
2.43.5
More information about the Linux-erofs
mailing list