erofs-utils: lib: fix dentries returned for terminal path components
PATCH v2 1/5 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.
---
v2:
- fix several LLM issue reports.
lib/rebuild.c | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-)
lib/rebuild.c +33 -12
@@ -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)
PATCH v2 2/5 erofs-utils: lib: drop unresolved symlink target dentries
Resolving a dangling intermediate symlink leaves an UNKNOWN dentry pointing to its parent inode. Remove this placeholder and return ENOENT when the target does not exist.
--- lib/rebuild.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/rebuild.c +6 -0
@@ -127,6 +127,12 @@ static struct erofs_dentry *erofs_rebuild_follow_link(struct erofs_inode *root, &dumb_wh, &dumb_opq, false, depth);if (IS_ERR (d) || !d || d->type ==EROFS_FT_DIR )return d; +if (d->type ==EROFS_FT_UNKNOWN ) { +/* The unresolved target does not own an inode. */ +list_del (&d->d_child); +free (d); +return ERR_PTR (-ENOENT ); + }if (d->type !=EROFS_FT_SYMLINK )return ERR_PTR (-ENOTDIR );/* the target is itself a symlink: keep following the chain */
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v2 3/5 erofs-utils: mkfs: init root inode in local source mode
In reply to Gao XiangFrom: 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
@@ -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
@@ -1335,7 +1335,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
@@ -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)
PATCH v2 4/5 erofs-utils: mkfs: add manifest build mode
In reply to Gao XiangFrom: 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.
--- configure.ac | 2 + include/erofs/manifest.h | 19 +++ lib/Makefile.am | 3 +- lib/inode.c | 1 + lib/manifest.c | 294 +++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 40 +++++- 6 files changed, 355 insertions(+), 4 deletions(-) create mode 100644 include/erofs/manifest.h create mode 100644 lib/manifest.c
configure.ac +2 -0
@@ -300,6 +300,7 @@ AC_CHECK_FUNCS(m4_flatten([ copy_file_range fallocate getrandom + getline getrlimit gettid gettimeofday
@@ -317,6 +318,7 @@ AC_CHECK_FUNCS(m4_flatten([ strdup strerror strrchr + strsep strtoull sysconf utimensat]))
include/erofs/manifest.h new file +19 -0
@@ -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
@@ -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/inode.c +1 -0
@@ -98,6 +98,7 @@ void erofs_remove_ihash(struct erofs_inode *inode) {erofs_down_write (&erofs_ihashlock);list_del (&inode->i_hash); +init_list_head (&inode->i_hash);erofs_up_write (&erofs_ihashlock); }
lib/manifest.c new file +294 -0
294 lines not on this page · raw message
mkfs/main.c +37 -3
@@ -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)
PATCH v2 5/5 erofs-utils: mkfs: support importing xattrs for manifests
In reply to Gao XiangFrom: 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.
--- include/erofs/xattr.h | 3 ++ lib/manifest.c | 8 ++++++ lib/xattr.c | 65 +++++++++++++++++++++++++++++++++++-------- mkfs/main.c | 18 +++++++++++- 4 files changed, 81 insertions(+), 13 deletions(-)
include/erofs/xattr.h +3 -0
@@ -33,10 +33,13 @@ ssize_t erofs_sys_lsetxattr(const char *path, const char *name,int erofs_xattr_init (struct erofs_sb_info *sbi);int erofs_scan_file_xattrs (struct erofs_inode *inode); +void erofs_inode_clear_xattrs (struct erofs_inode *inode);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 +8 -0
@@ -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"
@@ -136,6 +137,7 @@ static int erofs_manifest_load_local(struct erofs_importer *im,free (inode->i_srcpath); inode->i_srcpath =NULL ;erofs_remove_ihash (inode); +erofs_inode_clear_xattrs (inode); }else { parent = d->inode;if (!S_ISDIR (st.st_mode) && !im->params->hard_dereference) {
@@ -181,6 +183,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 +53 -12
92 lines not on this page · raw message
mkfs/main.c +17 -1
@@ -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)