Skip to contentSkip to conversation

erofs-utils: lib: fix dentries returned for terminal path components

PATCH Superseded Gao Xiang 1 participant 4 messages Nov 18 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH v2 1/4 erofs-utils: lib: fix dentries returned for terminal path components

Paths ending in .. or a followed symlink can return a stale dentry, causing directory metadata updates to affect the wrong inode or fail with ENOTDIR.

Return the resolved directory's dentry, reusing the cached parent for .. when possible.

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")
Fixes fd041ac99272 ("erofs-utils: lib: follow symlinks on intermediate tar path components")
Assisted-by ChatGPT:gpt-6.1-sol

---
v1: https://lore.kernel.org/r/[email protected]
v2:
- only support two-field format for now.

 lib/rebuild.c | 45 +++++++++++++++++++++++++++++++++------------
 1 file changed, 33 insertions(+), 12 deletions(-)
lib/rebuild.c +33 -12
index 2fa8feb..a5dd4f3 100644
@@ -86,11 +86,23 @@ struct erofs_dentry *erofs_d_lookup(struct erofs_inode *dir, const char *name)
 /* Bound symlink chains during path resolution, like the kernel's MAXSYMLINKS. */
 #define EROFS_REBUILD_SYMLINK_DEPTH	40
 
+static struct erofs_dentry *erofs_d_find_any_alias(struct erofs_inode *inode)
+{
+	struct erofs_dentry *d;
+
+	if (IS_ROOT(inode))
+		return NULL;
+	list_for_each_entry(d, &erofs_parent_inode(inode)->i_subdirs, d_child)
+		if (d->inode == inode)
+			return d;
+	return ERR_PTR(-ENOENT);
+}
+
 static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
 		struct erofs_inode *pwd, char *path, bool aufs,
 		bool *whout, bool *opq, bool to_head, unsigned int depth);
 
-static struct erofs_inode *erofs_rebuild_follow_link(struct erofs_inode *root,
+static struct erofs_dentry *erofs_rebuild_follow_link(struct erofs_inode *root,
 		struct erofs_inode *pwd, struct erofs_inode *link,
 		unsigned int depth)
 {
@@ -113,13 +125,8 @@ static struct erofs_inode *erofs_rebuild_follow_link(struct erofs_inode *root,
 		base = target[0] == '/' ? root : pwd;
 		d = __erofs_rebuild_get_dentry(root, base, target, false,
 					       &dumb_wh, &dumb_opq, false, depth);
-		if (IS_ERR(d))
-			return ERR_CAST(d);
-		/* target referred to the root or base directory itself ("/", ".") */
-		if (!d)
-			return base;
-		if (d->type == EROFS_FT_DIR)
-			return d->inode;
+		if (IS_ERR(d) || !d || d->type == EROFS_FT_DIR)
+			return d;
 		if (d->type != EROFS_FT_SYMLINK)
 			return ERR_PTR(-ENOTDIR);
 		/* the target is itself a symlink: keep following the chain */
@@ -132,9 +139,13 @@ static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
 		struct erofs_inode *pwd, char *path, bool aufs,
 		bool *whout, bool *opq, bool to_head, unsigned int depth)
 {
-	struct erofs_dentry *d = NULL;
+	struct erofs_dentry *d, *parent = NULL;
 	char *s = path;
+	bool parent_cached = false;
 
+	d = (pwd == root ? NULL : erofs_d_find_any_alias(pwd));
+	if (IS_ERR(d))
+		return d;
 	*whout = false;
 	*opq = false;
 
@@ -154,6 +165,11 @@ static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
 		if (__erofs_unlikely(is_dot_dotdot(s))) {
 			if (s[1] == '.') {
 				pwd = pwd->i_parent;
+				d = parent_cached ? parent :
+					(pwd == root ? NULL : erofs_d_find_any_alias(pwd));
+				if (IS_ERR(d))
+					return d;
+				parent_cached = false;
 			}
 		} else {
 			if (aufs && !slash) {
@@ -167,13 +183,17 @@ static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
 				}
 			}
 
+			parent = d;
 			d = erofs_d_lookup(pwd, s);
 			/* follow a symlinked intermediate component like a real fs */
 			if (d && slash && d->type == EROFS_FT_SYMLINK) {
-				pwd = erofs_rebuild_follow_link(root, pwd,
+				d = erofs_rebuild_follow_link(root, pwd,
 							d->inode, depth);
-				if (IS_ERR(pwd))
-					return ERR_CAST(pwd);
+				if (IS_ERR(d))
+					return d;
+				pwd = d ? d->inode : root;
+				/* The target may have a different parent. */
+				parent_cached = false;
 				*slash = '/';
 				s = slash + 1;
 				continue;
@@ -198,6 +218,7 @@ static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
 				d->type = EROFS_FT_UNKNOWN;
 				d->inode = pwd;
 			}
+			parent_cached = true;
 			pwd = d->inode;
 		}
 
Signature · 1 line
2.54.0 (Apple Git-157)
author
#1 lore raw

PATCH v2 2/4 erofs-utils: mkfs: init root inode in local source mode

From: Wenyuan Xu <[email protected]>

Initialize the root inode in local source mode so other input modes can supply their own root directory metadata.

---
 include/erofs/inode.h |  2 ++
 lib/inode.c           | 30 ++++--------------------------
 mkfs/main.c           | 10 ++++++++++
 3 files changed, 16 insertions(+), 26 deletions(-)
include/erofs/inode.h +2 -0
index 8c22dae..6745a73 100644
@@ -42,6 +42,8 @@ int erofs_allocate_inode_bh_data(struct erofs_inode *inode, erofs_blk_t nblocks,
 bool erofs_dentry_is_wht(struct erofs_sb_info *sbi, struct erofs_dentry *d);
 int __erofs_fill_inode(struct erofs_importer *im, struct erofs_inode *inode,
 		       erofs_os_stat_t *st, const char *path);
+int erofs_fill_inode(struct erofs_importer *im, struct erofs_inode *inode,
+			    erofs_os_stat_t *st, const char *path);
 struct erofs_inode *erofs_new_inode(struct erofs_sb_info *sbi);
 int erofs_importer_load_tree(struct erofs_importer *im, bool rebuild,
 			     bool incremental);
lib/inode.c +4 -26
index f687857..867cab2 100644
@@ -1334,7 +1334,7 @@ int __erofs_fill_inode(struct erofs_importer *im, struct erofs_inode *inode,
 	return 0;
 }
 
-static int erofs_fill_inode(struct erofs_importer *im, struct erofs_inode *inode,
+int erofs_fill_inode(struct erofs_importer *im, struct erofs_inode *inode,
 			    erofs_os_stat_t *st, const char *path)
 {
 	const struct erofs_importer_params *params = im->params;
@@ -2268,30 +2268,6 @@ struct erofs_mkfs_buildtree_ctx {
 	struct erofs_importer *im;
 	bool rebuild, incremental;
 };
-#ifndef EROFS_MT_ENABLED
-#define __erofs_mkfs_build_tree erofs_mkfs_build_tree
-#endif
-
-static int __erofs_mkfs_build_tree(const struct erofs_mkfs_btctx *ctx)
-{
-	struct erofs_importer *im = ctx->im;
-
-	if (!ctx->rebuild) {
-		struct erofs_importer_params *params = im->params;
-		erofs_os_stat_t st;
-		int err;
-
-		err = erofs_os_lstat(params->source, &st);
-		if (err)
-			return -errno;
-
-		err = erofs_fill_inode(im, im->root, &st, params->source);
-		if (err)
-			return err;
-	}
-	return erofs_mkfs_dump_tree(ctx);
-}
-
 #ifdef EROFS_MT_ENABLED
 
 #ifdef HAVE_SYS_RESOURCE_H
@@ -2358,7 +2334,7 @@ static int erofs_mkfs_build_tree(struct erofs_mkfs_btctx *ctx)
 	if (err)
 		goto fail;
 
-	err = __erofs_mkfs_build_tree(ctx);
+	err = erofs_mkfs_dump_tree(ctx);
 	erofs_mkfs_go(ctx, ~0, NULL, 0);
 	err2 = pthread_join(sbi->dfops_worker, &retval);
 	DBG_BUGON(!q->exited);
@@ -2377,6 +2353,8 @@ fail:
 	free(q);
 	return err;
 }
+#else
+#define erofs_mkfs_build_tree erofs_mkfs_dump_tree
 #endif
 
 int erofs_importer_load_tree(struct erofs_importer *im, bool rebuild,
mkfs/main.c +10 -0
index f75225b..dbda00e 100644
@@ -1950,6 +1950,8 @@ int main(int argc, char **argv)
 	}
 
 	if (source_mode == EROFS_MKFS_SOURCE_LOCALDIR) {
+		erofs_os_stat_t st;
+
 		err = erofs_load_shared_xattrs_from_path(&g_sbi, cfg.c_src_path,
 						mkfscfg.inlinexattr_tolerance);
 		if (err) {
@@ -1971,6 +1973,14 @@ int main(int argc, char **argv)
 			err = PTR_ERR(root);
 			goto exit;
 		}
+		err = erofs_os_lstat(cfg.c_src_path, &st);
+		if (err) {
+			err = -errno;
+			goto exit;
+		}
+		err = erofs_fill_inode(&importer, root, &st, cfg.c_src_path);
+		if (err)
+			goto exit;
 	} else {
 		err = erofs_xattr_flush_name_prefixes(&importer,
 						      mkfs_plain_xattr_pfx);
Signature · 1 line
2.54.0 (Apple Git-157)
author
#2 lore raw

PATCH v2 3/4 erofs-utils: mkfs: add manifest build mode

In reply to Gao Xiang

From: Wenyuan Xu <[email protected]>

Add --manifest=FILE to build an image from selected source entries without preparing a matching staging directory:

    mkfs.erofs --manifest=FILE IMAGE DATADIR

Starting with a simple two-field format: each record contains an image path beginning with '/' and a backing file path relative to DATADIR:

    /usr/bin/bash objects/ab/cdef...
    /usr/bin/ls objects/12/3456...

File records import contents and metadata from the backing file. Directory records import metadata only; children are specified by separate records. Missing parent directories are created automatically.

Support for extended attributes is left for a follow-up.

Assisted-by LLM
[ Gao Xiang: It also prepares for the upcoming diff updates for VM snapshots. ]
---
 include/erofs/manifest.h |  19 ++++
 lib/Makefile.am          |   3 +-
 lib/manifest.c           | 232 +++++++++++++++++++++++++++++++++++++++
 mkfs/main.c              |  40 ++++++-
 4 files changed, 290 insertions(+), 4 deletions(-)
 create mode 100644 include/erofs/manifest.h
 create mode 100644 lib/manifest.c
include/erofs/manifest.h new file +19 -0
new file mode 100644
index 0000000..c1fa281
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR MIT */
+#ifndef __EROFS_MANIFEST_H
+#define __EROFS_MANIFEST_H
+
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+struct erofs_importer;
+int erofs_manifest_load(struct erofs_importer *im, FILE *fp);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/Makefile.am +2 -1
index 8177c52..8654479 100644
@@ -23,6 +23,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/windows_compat.h \
       $(top_srcdir)/include/erofs/compress_hints.h \
       $(top_srcdir)/include/erofs/importer.h \
+      $(top_srcdir)/include/erofs/manifest.h \
       $(top_srcdir)/lib/liberofs_base64.h \
       $(top_srcdir)/lib/liberofs_cache.h \
       $(top_srcdir)/lib/liberofs_chunk.h \
@@ -47,7 +48,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      compress_hints.c sha256.c blobchunk.c dir.c \
 		      fragments.c dedupe.c uuid_unparse.c uuid.c tar.c \
 		      block_list.c rebuild.c diskbuf.c bitops.c dedupe_ext.c \
-		      vmdk.c metabox.c global.c importer.c base64.c
+		      vmdk.c metabox.c global.c importer.c base64.c manifest.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 liberofs_la_LIBADD = $(LIBEROFS_LIBS)
lib/manifest.c new file +232 -0

232 lines not on this page · raw message

mkfs/main.c +37 -3
index dbda00e..fe996c2 100644
@@ -24,6 +24,7 @@
 #include "erofs/exclude.h"
 #include "erofs/block_list.h"
 #include "erofs/compress_hints.h"
+#include "erofs/manifest.h"
 #include "../lib/compressor.h"
 #include "../lib/liberofs_chunk.h"
 #include "../lib/liberofs_gzran.h"
@@ -105,6 +106,7 @@ static struct option long_options[] = {
 	{"MZ", optional_argument, NULL, 537},
 	{"xattr-prefix", required_argument, NULL, 538},
 	{"xattr-inode-digest", optional_argument, NULL, 539},
+	{"manifest", required_argument, NULL, 540},
 	{0, 0, 0, 0},
 };
 
@@ -212,6 +214,7 @@ static void usage(int argc, char **argv)
 		" --hard-dereference     dereference hardlinks, add links as separate inodes\n"
 		" --ignore-mtime         use build time instead of strict per-file modification time\n"
 		" --max-extent-bytes=#   set maximum decompressed extent size # in bytes\n"
+		" --manifest=X           build an image from a manifest file X and DATADIR\n"
 		" --mount-point=X        X=prefix of target fs path (default: /)\n"
 		" --preserve-mtime       keep per-file modification time strictly\n"
 		" --offset=#             skip # bytes at the beginning of IMAGE.\n"
@@ -324,8 +327,10 @@ static enum {
 	EROFS_MKFS_SOURCE_S3,
 	EROFS_MKFS_SOURCE_OCI,
 	EROFS_MKFS_SOURCE_REBUILD,
+	EROFS_MKFS_SOURCE_MANIFEST,
 } source_mode;
 
+static const char *manifest_file;
 static unsigned int rebuild_src_count;
 static LIST_HEAD(rebuild_src_list);
 static u8 fixeduuid[16];
@@ -971,6 +976,7 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
 	char *s;
 
 	switch (source_mode) {
+	case EROFS_MKFS_SOURCE_MANIFEST:
 	case EROFS_MKFS_SOURCE_LOCALDIR:
 		err = erofs_os_lstat((s = argv[optind++]), &st);
 		if (err) {
@@ -985,7 +991,10 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
 					  erofs_strerror(-errno));
 				return -ENOENT;
 			}
-			erofs_set_fs_root(cfg.c_src_path);
+			if (source_mode == EROFS_MKFS_SOURCE_LOCALDIR)
+				erofs_set_fs_root(cfg.c_src_path);
+		} else if (source_mode == EROFS_MKFS_SOURCE_MANIFEST) {
+			return -ENOTDIR;
 		} else {
 			cfg.c_src_path = strdup(s);
 			if (!cfg.c_src_path)
@@ -1497,6 +1506,14 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 				return err;
 			}
 			break;
+		case 540:
+			if (manifest_file) {
+				erofs_err("duplicated manifest %s", optarg);
+				return -EOPNOTSUPP;
+			}
+			manifest_file = optarg;
+			source_mode = EROFS_MKFS_SOURCE_MANIFEST;
+			break;
 		case 'V':
 			version();
 			exit(0);
@@ -1534,6 +1551,9 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 			erofs_err("--blobdev is currently incompatible with rebuild mode");
 			return -EINVAL;
 		}
+	} else if (source_mode == EROFS_MKFS_SOURCE_MANIFEST) {
+		erofs_err("missing argument: DATADIR");
+		return -EINVAL;
 	} else if (source_mode != EROFS_MKFS_SOURCE_TAR) {
 		erofs_err("missing argument: SOURCE(s)");
 		return -EINVAL;
@@ -1981,7 +2001,7 @@ int main(int argc, char **argv)
 		err = erofs_fill_inode(&importer, root, &st, cfg.c_src_path);
 		if (err)
 			goto exit;
-	} else {
+	} else if (source_mode != EROFS_MKFS_SOURCE_MANIFEST) {
 		err = erofs_xattr_flush_name_prefixes(&importer,
 						      mkfs_plain_xattr_pfx);
 		if (err) {
@@ -1998,7 +2018,21 @@ int main(int argc, char **argv)
 	}
 
 	importer.root = root;
-	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
+	if (source_mode == EROFS_MKFS_SOURCE_MANIFEST) {
+		FILE *fp = fopen(manifest_file, "r");
+
+		if (!fp) {
+			err = -errno;
+			erofs_err("cannot open manifest %s: %s",
+				  manifest_file, erofs_strerror(err));
+			goto exit;
+		}
+		err = erofs_manifest_load(&importer, fp);
+		fclose(fp);
+		if (err)
+			goto exit;
+		root = importer.root;
+	} else if (source_mode == EROFS_MKFS_SOURCE_TAR) {
 		while (!(err = tarerofs_parse_tar(&importer, &erofstar)))
 			;
 	} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
Signature · 1 line
2.54.0 (Apple Git-157)
author
#3 lore raw

PATCH v2 4/4 erofs-utils: mkfs: support importing xattrs for manifests

In reply to Gao Xiang

From: Wenyuan Xu <[email protected]>

Import xattrs from backing files listed in the manifest. Reuse shared xattr serialization for the loaded inodes and support xattr name prefixes.

Assisted-by LLM
Assisted-by ChatGPT:gpt-6.1-sol
---
 include/erofs/xattr.h |  2 ++
 lib/manifest.c        |  7 ++++++
 lib/xattr.c           | 52 +++++++++++++++++++++++++++++++++----------
 mkfs/main.c           | 18 ++++++++++++++-
 4 files changed, 66 insertions(+), 13 deletions(-)
include/erofs/xattr.h +2 -0
index c74f0ae..a8faca7 100644
@@ -37,6 +37,8 @@ int erofs_prepare_xattr_ibody(struct erofs_inode *inode, bool noroom);
 char *erofs_export_xattr_ibody(struct erofs_inode *inode);
 int erofs_load_shared_xattrs_from_path(struct erofs_sb_info *sbi, const char *path,
 				       long inlinexattr_tolerance);
+int erofs_build_shared_xattrs(struct erofs_sb_info *sbi,
+			      long inlinexattr_tolerance);
 int erofs_xattr_insert_name_prefix(const char *prefix);
 int erofs_xattr_set_ishare_prefix(struct erofs_sb_info *sbi,
 				  const char *prefix);
lib/manifest.c +7 -0
index 775e462..e7d5b77 100644
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include "erofs/importer.h"
 #include "erofs/inode.h"
+#include "erofs/xattr.h"
 #include "erofs/print.h"
 #include "erofs/manifest.h"
 #include "liberofs_rebuild.h"
@@ -128,6 +129,12 @@ static int erofs_manifest_load_local(struct erofs_importer *im,
 		inode->i_size = len;
 	}
 
+	if (!im->params->no_xattrs) {
+		ret = erofs_scan_file_xattrs(inode);
+		if (ret)
+			goto out_inode;
+	}
+
 	if (!existing) {
 		d->inode = inode;
 		d->type = erofs_mode_to_ftype(inode->i_mode);
lib/xattr.c +40 -12

73 lines not on this page · raw message

mkfs/main.c +17 -1
index fe996c2..22ef23d 100644
@@ -2001,7 +2001,15 @@ int main(int argc, char **argv)
 		err = erofs_fill_inode(&importer, root, &st, cfg.c_src_path);
 		if (err)
 			goto exit;
-	} else if (source_mode != EROFS_MKFS_SOURCE_MANIFEST) {
+	} else if (source_mode == EROFS_MKFS_SOURCE_MANIFEST)  {
+		err = erofs_xattr_flush_name_prefixes(&importer,
+						      mkfs_plain_xattr_pfx);
+		if (err) {
+			erofs_err("failed to flush long xattr prefixes: %s",
+				  erofs_strerror(err));
+			goto exit;
+		}
+	} else {
 		err = erofs_xattr_flush_name_prefixes(&importer,
 						      mkfs_plain_xattr_pfx);
 		if (err) {
@@ -2032,6 +2040,14 @@ int main(int argc, char **argv)
 		if (err)
 			goto exit;
 		root = importer.root;
+
+		if (!incremental_mode) {
+			err = erofs_build_shared_xattrs(&g_sbi, mkfscfg.inlinexattr_tolerance);
+			if (err) {
+				erofs_err("failed to build shared xattrs: %s", erofs_strerror(err));
+				goto exit;
+			}
+		}
 	} else if (source_mode == EROFS_MKFS_SOURCE_TAR) {
 		while (!(err = tarerofs_parse_tar(&importer, &erofstar)))
 			;
Signature · 1 line
2.54.0 (Apple Git-157)