[PATCH 2/4] erofs-utils: mount: extract reusable source-opening and recovery helpers
Chengyu
hudson at cyzhu.com
Mon Mar 16 01:27:43 AEDT 2026
From: Chengyu Zhu <hudsonzhu at tencent.com>
Factor out erofsmount_open_source() and erofsmount_open_recovery_source()
helpers; introduce EROFSMOUNT_RUNDIR / EROFSMOUNT_NBD_REC_FMT macros.
Signed-off-by: Chengyu Zhu <hudsonzhu at tencent.com>
---
mount/main.c | 196 +++++++++++++++++++++++++--------------------------
1 file changed, 98 insertions(+), 98 deletions(-)
diff --git a/mount/main.c b/mount/main.c
index 350738d..93c8444 100644
--- a/mount/main.c
+++ b/mount/main.c
@@ -41,6 +41,9 @@ struct loop_info {
/* Device boundary probe */
#define EROFSMOUNT_NBD_DISK_SIZE (INT64_MAX >> 9)
+#define EROFSMOUNT_RUNDIR "/var/run/erofs"
+#define EROFSMOUNT_NBD_REC_FMT EROFSMOUNT_RUNDIR "/mountnbd_nbd%d"
+
enum erofs_backend_drv {
EROFSAUTO,
EROFSLOCAL,
@@ -613,6 +616,25 @@ err_out:
return err;
}
+static int erofsmount_open_source(struct erofs_vfile *vf,
+ struct erofsmount_source *source)
+{
+ int err;
+
+ if (source->type == EROFSMOUNT_SOURCE_OCI) {
+ if (source->ocicfg.tarindex_path || source->ocicfg.zinfo_path)
+ return erofsmount_tarindex_open(vf, &source->ocicfg,
+ source->ocicfg.tarindex_path,
+ source->ocicfg.zinfo_path);
+ return ocierofs_io_open(vf, &source->ocicfg);
+ }
+ err = open(source->device_path, O_RDONLY);
+ if (err < 0)
+ return -errno;
+ vf->fd = err;
+ return 0;
+}
+
struct erofsmount_nbd_ctx {
struct erofs_vfile vd; /* virtual device */
struct erofs_vfile sk; /* socket file */
@@ -671,26 +693,9 @@ static int erofsmount_startnbd(int nbdfd, struct erofsmount_source *source)
pthread_t th;
int err, err2;
- if (source->type == EROFSMOUNT_SOURCE_OCI) {
- if (source->ocicfg.tarindex_path || source->ocicfg.zinfo_path) {
- err = erofsmount_tarindex_open(&ctx.vd, &source->ocicfg,
- source->ocicfg.tarindex_path,
- source->ocicfg.zinfo_path);
- if (err)
- goto out_closefd;
- } else {
- err = ocierofs_io_open(&ctx.vd, &source->ocicfg);
- if (err)
- goto out_closefd;
- }
- } else {
- err = open(source->device_path, O_RDONLY);
- if (err < 0) {
- err = -errno;
- goto out_closefd;
- }
- ctx.vd.fd = err;
- }
+ err = erofsmount_open_source(&ctx.vd, source);
+ if (err)
+ goto out_closefd;
err = erofs_nbd_connect(nbdfd, 9, EROFSMOUNT_NBD_DISK_SIZE);
if (err < 0) {
@@ -797,13 +802,13 @@ static int erofsmount_write_recovery_local(FILE *f, struct erofsmount_source *so
static char *erofsmount_write_recovery_info(struct erofsmount_source *source)
{
- char recp[] = "/var/run/erofs/mountnbd_XXXXXX";
+ char recp[] = EROFSMOUNT_RUNDIR "/mountnbd_XXXXXX";
int fd, err;
FILE *f;
fd = mkstemp(recp);
if (fd < 0 && errno == ENOENT) {
- err = mkdir("/var/run/erofs", 0700);
+ err = mkdir(EROFSMOUNT_RUNDIR, 0700);
if (err)
return ERR_PTR(-errno);
fd = mkstemp(recp);
@@ -945,7 +950,7 @@ static int erofsmount_reattach_oci(struct erofs_vfile *vf,
}
#endif
-static int erofsmount_reattach_gzran_oci(struct erofsmount_nbd_ctx *ctx,
+static int erofsmount_reattach_gzran_oci(struct erofs_vfile *vd,
char *source)
{
char *tokens[6] = {0}, *p = source, *space, *oci_source;
@@ -975,12 +980,12 @@ static int erofsmount_reattach_gzran_oci(struct erofsmount_nbd_ctx *ctx,
if (err < 0)
return -ENOMEM;
- err = erofsmount_reattach_oci(&ctx->vd, "OCI_NATIVE_BLOB", oci_source);
+ err = erofsmount_reattach_oci(vd, "OCI_NATIVE_BLOB", oci_source);
free(oci_source);
if (err)
return err;
- temp_vd = ctx->vd;
+ temp_vd = *vd;
oci_cfg.image_ref = strdup(source);
if (!oci_cfg.image_ref) {
erofs_io_close(&temp_vd);
@@ -992,13 +997,60 @@ static int erofsmount_reattach_gzran_oci(struct erofsmount_nbd_ctx *ctx,
if (token_count > 4 && tokens[4] && *tokens[4])
zinfo_path = tokens[4];
- err = erofsmount_tarindex_open(&ctx->vd, &oci_cfg,
+ err = erofsmount_tarindex_open(vd, &oci_cfg,
meta_path, zinfo_path);
free(oci_cfg.image_ref);
erofs_io_close(&temp_vd);
return err;
}
+static int erofsmount_open_recovery_source(FILE *f,
+ struct erofs_vfile *vd)
+{
+ char *line = NULL, *source;
+ size_t n = 0;
+ int err;
+
+ err = getline(&line, &n, f);
+ if (err <= 0) {
+ err = -errno;
+ fclose(f);
+ goto out;
+ }
+ fclose(f);
+ if (err && line[err - 1] == '\n')
+ line[err - 1] = '\0';
+
+ source = strchr(line, ' ');
+ if (!source) {
+ erofs_err("invalid source in recovery file: %s", line);
+ err = -EINVAL;
+ goto out;
+ }
+ *(source++) = '\0';
+
+ if (!strcmp(line, "LOCAL")) {
+ err = open(source, O_RDONLY);
+ if (err < 0) {
+ err = -errno;
+ goto out;
+ }
+ vd->fd = err;
+ err = 0;
+ } else if (!strcmp(line, "TARINDEX_OCI_BLOB")) {
+ err = erofsmount_reattach_gzran_oci(vd, source);
+ } else if (!strcmp(line, "OCI_LAYER") || !strcmp(line, "OCI_NATIVE_BLOB")) {
+ err = erofsmount_reattach_oci(vd, line, source);
+ } else {
+ erofs_err("unsupported source type %s in recovery file",
+ line);
+ err = -EOPNOTSUPP;
+ }
+out:
+ free(line);
+ return err;
+}
+
static int erofsmount_nbd_fix_backend_linkage(int num, char **recp)
{
char *newrecp;
@@ -1013,7 +1065,7 @@ static int erofsmount_nbd_fix_backend_linkage(int num, char **recp)
return err;
}
- if (asprintf(&newrecp, "/var/run/erofs/mountnbd_nbd%d", num) <= 0)
+ if (asprintf(&newrecp, EROFSMOUNT_NBD_REC_FMT, num) <= 0)
return -ENOMEM;
if (rename(*recp, newrecp) < 0) {
@@ -1042,24 +1094,9 @@ static int erofsmount_startnbd_nl(pid_t *pid, struct erofsmount_source *source)
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
exit(EXIT_FAILURE);
- if (source->type == EROFSMOUNT_SOURCE_OCI) {
- if (source->ocicfg.tarindex_path || source->ocicfg.zinfo_path) {
- err = erofsmount_tarindex_open(&ctx.vd, &source->ocicfg,
- source->ocicfg.tarindex_path,
- source->ocicfg.zinfo_path);
- if (err)
- exit(EXIT_FAILURE);
- } else {
- err = ocierofs_io_open(&ctx.vd, &source->ocicfg);
- if (err)
- exit(EXIT_FAILURE);
- }
- } else {
- err = open(source->device_path, O_RDONLY);
- if (err < 0)
- exit(EXIT_FAILURE);
- ctx.vd.fd = err;
- }
+ err = erofsmount_open_source(&ctx.vd, source);
+ if (err)
+ exit(EXIT_FAILURE);
recp = erofsmount_write_recovery_info(source);
if (IS_ERR(recp)) {
erofs_io_close(&ctx.vd);
@@ -1101,11 +1138,10 @@ out_fork:
static int erofsmount_reattach(const char *target)
{
- char *identifier, *line, *source, *recp = NULL;
+ char *identifier;
struct erofsmount_nbd_ctx ctx = {};
int nbdnum, err;
struct stat st;
- size_t n;
FILE *f;
err = lstat(target, &st);
@@ -1126,65 +1162,31 @@ static int erofsmount_reattach(const char *target)
identifier = NULL;
}
- if (!identifier &&
- (asprintf(&recp, "/var/run/erofs/mountnbd_nbd%d", nbdnum) <= 0)) {
- err = -ENOMEM;
- goto err_identifier;
- }
+ if (!identifier) {
+ char *recp;
- f = fopen(identifier ?: recp, "r");
- if (!f) {
- err = -errno;
+ if (asprintf(&recp, EROFSMOUNT_NBD_REC_FMT, nbdnum) <= 0) {
+ err = -ENOMEM;
+ goto err_out;
+ }
+ f = fopen(recp, "r");
free(recp);
- goto err_identifier;
+ } else {
+ f = fopen(identifier, "r");
}
- free(recp);
-
- line = NULL;
- if ((err = getline(&line, &n, f)) <= 0) {
+ if (!f) {
err = -errno;
- fclose(f);
- goto err_identifier;
- }
- fclose(f);
- if (err && line[err - 1] == '\n')
- line[err - 1] = '\0';
-
- source = strchr(line, ' ');
- if (!source) {
- erofs_err("invalid source recorded in recovery file: %s", line);
- err = -EINVAL;
- goto err_line;
- } else {
- *(source++) = '\0';
+ goto err_out;
}
- if (!strcmp(line, "LOCAL")) {
- err = open(source, O_RDONLY);
- if (err < 0) {
- err = -errno;
- goto err_line;
- }
- ctx.vd.fd = err;
- } else if (!strcmp(line, "TARINDEX_OCI_BLOB")) {
- err = erofsmount_reattach_gzran_oci(&ctx, source);
- if (err)
- goto err_line;
- } else if (!strcmp(line, "OCI_LAYER") || !strcmp(line, "OCI_NATIVE_BLOB")) {
- err = erofsmount_reattach_oci(&ctx.vd, line, source);
- if (err)
- goto err_line;
- } else {
- err = -EOPNOTSUPP;
- erofs_err("unsupported source type %s recorded in recovery file", line);
- goto err_line;
- }
+ err = erofsmount_open_recovery_source(f, &ctx.vd);
+ if (err)
+ goto err_out;
err = erofs_nbd_nl_reconnect(nbdnum, identifier);
if (err >= 0) {
ctx.sk.fd = err;
if (fork() == 0) {
- free(line);
free(identifier);
if ((uintptr_t)erofsmount_nbd_loopfn(&ctx))
return EXIT_FAILURE;
@@ -1194,9 +1196,7 @@ static int erofsmount_reattach(const char *target)
err = 0;
}
erofs_io_close(&ctx.vd);
-err_line:
- free(line);
-err_identifier:
+err_out:
free(identifier);
return err;
}
--
2.47.1
More information about the Linux-erofs
mailing list