Skip to contentSkip to conversation

erofs-utils: lib: reject incremental builds with external devices

PATCH New Gao Xiang 3 participants 17 messages Nov 26 – Dec 1 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH v3 1/10 erofs-utils: lib: reject incremental builds with external devices

Growing the primary device during an incremental build moves external devices in the unified address space. Inherited inodes still use the old unified block addresses, so unchanged files can return wrong data after the device table is updated.

Reject incremental builds of images with external devices for now. Fix up inherited unified block addresses before allowing these builds.

Fixes 54dd7ebd10b5 ("erofs-utils: mkfs: enable `--blobdev` for flat inode layouts")
Assisted-by Codex:gpt-6.1-sol
---
 lib/super.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/super.c +12 -0
index c6a9230..db8f23f 100644
@@ -521,6 +521,18 @@ int erofs_mkfs_load_fs(struct erofs_sb_info *sbi, unsigned int dsunit)
 		return -EOPNOTSUPP;
 	}
 
+	/*
+	 * TODO: fix up unified block addresses in inherited inodes.
+	 *
+	 * Growing the primary device moves external devices in the unified
+	 * address space, while inherited inodes still use the old addresses.
+	 * Disable incremental builds with external devices for now.
+	 */
+	if (sbi->extra_devices) {
+		erofs_err("incremental builds with external devices are not supported");
+		return -EOPNOTSUPP;
+	}
+
 	err = erofs_io_fstat(&sbi->bdev, &u.st);
 	if (!err && S_ISREG(u.st.st_mode))
 		u.startblk = DIV_ROUND_UP(u.st.st_size, erofs_blksiz(sbi));
Signature · 1 line
2.54.0 (Apple Git-157)
author
#1 lore raw

PATCH v3 2/10 erofs-utils: lib: reject overflowing device-table offsets

The on-disk devt_slotoff field is a 16-bit index of 128-byte slots. Incremental builds can allocate a device table beyond this limit, truncating the offset and making the superblock point into unrelated image data.

Check the slot offset before assigning it to devt_slotoff and return -EOVERFLOW if it does not fit. Revoke the buffer allocation and free the device array on failure.

Fixes 7550a30c332c ("erofs-utils: enable incremental builds")
Assisted-by Codex:gpt-6.1-sol
---
 lib/super.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
lib/super.c +10 -1
index db8f23f..920d572 100644
@@ -391,6 +391,7 @@ int erofs_superblock_csum_verify(struct erofs_sb_info *sbi)
 int erofs_mkfs_init_devices(struct erofs_sb_info *sbi, unsigned int devices)
 {
 	struct erofs_buffer_head *bh;
+	erofs_off_t slotoff;
 
 	if (!devices)
 		return 0;
@@ -407,9 +408,17 @@ int erofs_mkfs_init_devices(struct erofs_sb_info *sbi, unsigned int devices)
 		return PTR_ERR(bh);
 	}
 	erofs_mapbh(NULL, bh->block);
+	slotoff = erofs_btell(bh, false) / EROFS_DEVT_SLOT_SIZE;
+	/* the on-disk devt_slotoff is a 16-bit index */
+	if (slotoff > UINT16_MAX) {
+		erofs_bdrop(bh, true);
+		free(sbi->devs);
+		sbi->devs = NULL;
+		return -EOVERFLOW;
+	}
 	bh->op = &erofs_skip_write_bhops;
 	sbi->bh_devt = bh;
-	sbi->devt_slotoff = erofs_btell(bh, false) / EROFS_DEVT_SLOT_SIZE;
+	sbi->devt_slotoff = slotoff;
 	sbi->extra_devices = devices;
 	erofs_sb_set_device_table(sbi);
 	return 0;
Signature · 1 line
2.54.0 (Apple Git-157)
author
#2 lore raw

PATCH v3 3/10 erofs-utils: lib: fix large chunk index writes

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

For large chunk index tables, int cannot hold the write result and the 32-bit offset into the chunk pointer array can wrap around. Inode metadata size calculations can overflow as well.

Use 64-bit types for the write result, buffer offsets and metadata size. Use erofs_inode_meta_size() for the root inode size check too.

Fixes 03cbf7b8f7f7 ("erofs-utils: mkfs: support chunk-based uncompressed files")
Fixes bbebbbda4b27 ("erofs-utils: lib: consolidate erofs_iflush()")
Assisted-by Codex:gpt-6.1-sol
---
 lib/blobchunk.c      |  7 ++++---
 lib/inode.c          | 19 +++++++++----------
 lib/liberofs_chunk.h |  4 ++--
 3 files changed, 15 insertions(+), 15 deletions(-)
lib/blobchunk.c +4 -3
index 8217512..a3a88d3 100644
@@ -192,8 +192,8 @@ void erofs_inode_fixup_chunkformat(struct erofs_inode *inode)
 		inode->u.chunkformat |= EROFS_CHUNK_FORMAT_48BIT;
 }
 
-int erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
-			      erofs_off_t off)
+s64 erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
+				 erofs_off_t off)
 {
 	struct erofs_sb_info *sbi = inode->sbi;
 	erofs_blk_t remaining_blks = BLK_ROUND_UP(sbi, inode->i_size);
@@ -201,7 +201,8 @@ int erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
 	erofs_blk_t extent_end = EROFS_NULL_ADDR, chunkblks, addrmask;
 	erofs_blk_t extent_start = EROFS_NULL_ADDR;
 	erofs_off_t source_offset;
-	unsigned int dst, src, unit, zeroedlen;
+	unsigned int unit, zeroedlen;
+	u64 dst, src;
 	bool _48bit;
 
 	if (inode->u.chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
lib/inode.c +9 -10
index ad6f081..ef39a4e 100644
@@ -799,7 +799,8 @@ int erofs_iflush(struct erofs_inode *inode)
 	struct iovec iov[2];
 	char *xattrs = NULL;
 	bool nlink_1 = true;
-	int ret, fmt;
+	int fmt;
+	s64 ret;
 
 	DBG_BUGON(inode->nid == EROFS_NID_UNALLOCATED);
 	DBG_BUGON(bh && erofs_btell(bh, false) != off);
@@ -981,11 +982,11 @@ static bool erofs_inode_need_48bit(struct erofs_inode *inode)
 	return false;
 }
 
-static unsigned int erofs_inode_meta_size(struct erofs_inode *inode)
+static u64 erofs_inode_meta_size(struct erofs_inode *inode)
 {
-	unsigned int inodesize;
+	u64 inodesize;
 
-	inodesize = inode->inode_isize + inode->xattr_isize;
+	inodesize = inode->inode_isize + (u64)inode->xattr_isize;
 	if (inode->extent_isize)
 		inodesize = roundup(inodesize, 8) + inode->extent_isize;
 	return inodesize;
@@ -997,7 +998,7 @@ static int erofs_prepare_inode_buffer(struct erofs_importer *im,
 	const struct erofs_importer_params *params = im->params;
 	struct erofs_sb_info *sbi = im->sbi;
 	struct erofs_bufmgr *ibmgr;
-	unsigned int inodesize;
+	u64 inodesize;
 	struct erofs_buffer_head *bh, *ibh;
 
 	DBG_BUGON(inode->bh || inode->bh_inline);
@@ -2443,7 +2444,8 @@ int erofs_fixup_root_inode(struct erofs_inode *root)
 {
 	struct erofs_sb_info *sbi = root->sbi;
 	struct erofs_inode oi;
-	unsigned int ondisk_capacity, ondisk_size;
+	unsigned int ondisk_capacity;
+	erofs_off_t ondisk_size;
 	char *ibuf;
 	int err;
 
@@ -2471,10 +2473,7 @@ int erofs_fixup_root_inode(struct erofs_inode *root)
 	if (oi.datalayout == EROFS_INODE_FLAT_INLINE)
 		ondisk_capacity += erofs_blkoff(sbi, oi.i_size);
 
-	ondisk_size = root->inode_isize + root->xattr_isize;
-	if (root->extent_isize)
-		ondisk_size = roundup(ondisk_size, 8) + root->extent_isize;
-	ondisk_size += root->idata_size;
+	ondisk_size = erofs_inode_meta_size(root) + root->idata_size;
 
 	if (ondisk_size > ondisk_capacity) {
 		erofs_err("no enough room for the root inode from nid %llu",
lib/liberofs_chunk.h +2 -2
index 16f3d05..4ef6640 100644
@@ -18,8 +18,8 @@ struct erofs_chunkitem *erofs_get_unhashed_chunk(struct erofs_sb_info *sbi,
 		unsigned int device_id, erofs_blk_t blkaddr,
 		erofs_off_t sourceoffset);
 void erofs_inode_fixup_chunkformat(struct erofs_inode *inode);
-int erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
-			      erofs_off_t off);
+s64 erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
+				 erofs_off_t off);
 int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 				  erofs_off_t startoff, int device_id);
 int erofs_write_zero_inode(struct erofs_inode *inode);
Signature · 1 line
2.54.0 (Apple Git-157)
author
#3 lore raw

PATCH v3 4/10 erofs-utils: lib: fix hole detection in block maps

In reply to Gao Xiang

Block-map entries store holes as 0xffffffff. After erofs_blk_t and EROFS_NULL_ADDR became 64-bit, this value no longer matched EROFS_NULL_ADDR and holes were treated as mapped blocks.

Compare against UINT32_MAX to recognize the 32-bit hole marker.

Fixes eeceb2289513 ("erofs-utils: implement 48-bit block addressing for unencoded inodes")
Assisted-by Codex:gpt-6.1-sol
---
 lib/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/data.c +1 -1
index 91809d0..3ccd109 100644
@@ -151,7 +151,7 @@ int __erofs_map_blocks(struct erofs_inode *inode,
 		}
 	} else {
 		startblk = le32_to_cpu(*(__le32 *)idx);
-		if (startblk != EROFS_NULL_ADDR) {
+		if (startblk != UINT32_MAX) {
 			map->m_pa = erofs_pos(sbi, startblk);
 			map->m_flags = EROFS_MAP_MAPPED;
 		}
Signature · 1 line
2.54.0 (Apple Git-157)
author
#4 lore raw

PATCH v3 5/10 erofs-utils: lib: preserve device table if initialization fails

In reply to Gao Xiang

erofs_mkfs_init_devices() overwrites sbi->devs before allocating the device-table buffer. If either allocation fails, the existing device array is lost. The offset overflow path has the same problem.

Keep the new array local until initialization succeeds and free only that array on failure.

Assisted-by Codex:gpt-6.1-sol
---
 lib/super.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/super.c +6 -6
index 920d572..60b5b51 100644
@@ -390,21 +390,21 @@ int erofs_superblock_csum_verify(struct erofs_sb_info *sbi)
 
 int erofs_mkfs_init_devices(struct erofs_sb_info *sbi, unsigned int devices)
 {
+	struct erofs_device_info *devs;
 	struct erofs_buffer_head *bh;
 	erofs_off_t slotoff;
 
 	if (!devices)
 		return 0;
 
-	sbi->devs = calloc(devices, sizeof(sbi->devs[0]));
-	if (!sbi->devs)
+	devs = calloc(devices, sizeof(devs[0]));
+	if (!devs)
 		return -ENOMEM;
 
 	bh = erofs_balloc(sbi->bmgr, DEVT,
 			  sizeof(struct erofs_deviceslot) * devices, 0);
 	if (IS_ERR(bh)) {
-		free(sbi->devs);
-		sbi->devs = NULL;
+		free(devs);
 		return PTR_ERR(bh);
 	}
 	erofs_mapbh(NULL, bh->block);
@@ -412,8 +412,7 @@ int erofs_mkfs_init_devices(struct erofs_sb_info *sbi, unsigned int devices)
 	/* the on-disk devt_slotoff is a 16-bit index */
 	if (slotoff > UINT16_MAX) {
 		erofs_bdrop(bh, true);
-		free(sbi->devs);
-		sbi->devs = NULL;
+		free(devs);
 		return -EOVERFLOW;
 	}
 	bh->op = &erofs_skip_write_bhops;
@@ -421,6 +420,7 @@ int erofs_mkfs_init_devices(struct erofs_sb_info *sbi, unsigned int devices)
 	sbi->devt_slotoff = slotoff;
 	sbi->extra_devices = devices;
 	erofs_sb_set_device_table(sbi);
+	sbi->devs = devs;
 	return 0;
 }
 
Signature · 1 line
2.54.0 (Apple Git-157)
author
#5 lore raw

PATCH v3 6/10 erofs-utils: lib: introduce erofs_rebuild_params

In reply to Gao Xiang

Group the merged tree root, source superblock and data import mode in struct erofs_rebuild_params and pass it to erofs_rebuild_load_tree(). Share these parameters with recursive directory iteration and inode updates, using mergeroot->sbi instead of g_sbi for the destination.

Assisted-by Codex:gpt-6.1-sol
---
 lib/liberofs_rebuild.h |  9 +++++++--
 lib/rebuild.c          | 27 +++++++++++++--------------
 mkfs/main.c            | 15 ++++++++-------
 3 files changed, 28 insertions(+), 23 deletions(-)
lib/liberofs_rebuild.h +7 -2
index 6459dbd..2cf228e 100644
@@ -10,11 +10,16 @@ enum erofs_rebuild_datamode {
 	EROFS_REBUILD_DATA_FULL,
 };
 
+struct erofs_rebuild_params {
+	struct erofs_inode *mergeroot;
+	struct erofs_sb_info *sbi;
+	enum erofs_rebuild_datamode datamode;
+};
+
 struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 		char *path, bool aufs, bool *whout, bool *opq, bool to_head);
 
-int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_sb_info *sbi,
-			    enum erofs_rebuild_datamode mode);
+int erofs_rebuild_load_tree(struct erofs_rebuild_params *params);
 
 int erofs_rebuild_load_basedir(struct erofs_inode *dir, u64 *nr_subdirs,
 			       unsigned int *i_nlink);
lib/rebuild.c +13 -14
index e83fd52..6a4aff6 100644
@@ -389,10 +389,11 @@ static int erofs_rebuild_write_full_data(struct erofs_inode *inode)
 	return err;
 }
 
-static int erofs_rebuild_update_inode(struct erofs_sb_info *dst_sb,
-				      struct erofs_inode *inode,
-				      enum erofs_rebuild_datamode datamode)
+static int erofs_rebuild_update_inode(struct erofs_rebuild_params *params,
+				      struct erofs_inode *inode)
 {
+	struct erofs_sb_info *dst_sb = params->mergeroot->sbi;
+	enum erofs_rebuild_datamode datamode = params->datamode;
 	int err = 0;
 
 	switch (inode->i_mode & S_IFMT) {
@@ -450,8 +451,7 @@ struct erofs_rebuild_dir_context {
 	struct erofs_dir_context ctx;
 	struct erofs_inode *mergedir;	/* parent directory in the merged tree */
 	union {
-		/* indicate how to import inode data */
-		enum erofs_rebuild_datamode datamode;
+		struct erofs_rebuild_params *params;
 		struct {
 			u64 *nr_subdirs;
 			unsigned int *i_nlink;
@@ -581,8 +581,7 @@ static int erofs_rebuild_dirent_iter(struct erofs_dir_context *ctx)
 			inode->i_ino[1] = inode->nid;
 			inode->i_nlink = 1;
 
-			ret = erofs_rebuild_update_inode(&g_sbi, inode,
-							 rctx->datamode);
+			ret = erofs_rebuild_update_inode(rctx->params, inode);
 			if (ret) {
 				erofs_iput(inode);
 				goto out;
@@ -607,17 +606,17 @@ static int erofs_rebuild_dirent_iter(struct erofs_dir_context *ctx)
 	}
 
 	/* reset sbi, nid after subdirs are all loaded for the final dump */
-	inode->sbi = &g_sbi;
+	inode->sbi = rctx->params->mergeroot->sbi;
 	inode->nid = EROFS_NID_UNALLOCATED;
 out:
 	free(path);
 	return ret;
 }
 
-int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_sb_info *sbi,
-			    enum erofs_rebuild_datamode mode)
+int erofs_rebuild_load_tree(struct erofs_rebuild_params *params)
 {
-	struct erofs_sb_info *dst_sbi = root->sbi;
+	struct erofs_sb_info *dst_sbi = params->mergeroot->sbi;
+	struct erofs_sb_info *sbi = params->sbi;
 	struct erofs_inode inode = {};
 	struct erofs_rebuild_dir_context ctx;
 	char uuid_str[37];
@@ -635,7 +634,7 @@ int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_sb_info *sbi,
 		return ret;
 	}
 
-	if (mode == EROFS_REBUILD_DATA_BLOB_INDEX) {
+	if (params->datamode == EROFS_REBUILD_DATA_BLOB_INDEX) {
 		if (erofs_sb_has_ztailpacking(sbi))
 			erofs_sb_set_ztailpacking(dst_sbi);
 		if (erofs_sb_has_dedupe(sbi))
@@ -664,8 +663,8 @@ int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_sb_info *sbi,
 	ctx = (struct erofs_rebuild_dir_context) {
 		.ctx.dir = &inode,
 		.ctx.cb = erofs_rebuild_dirent_iter,
-		.mergedir = root,
-		.datamode = mode,
+		.mergedir = params->mergeroot,
+		.params = params,
 	};
 	ret = erofs_iterate_dir(&ctx.ctx, false);
 	free(inode.i_srcpath);
mkfs/main.c +8 -7
index 22ef23d..ce93166 100644
@@ -1688,17 +1688,17 @@ static int erofs_mkfs_rebuild_load_trees(struct erofs_inode *root)
 	unsigned int extra_devices = 0;
 	erofs_blk_t nblocks;
 	int ret, idx;
-	enum erofs_rebuild_datamode datamode;
+	struct erofs_rebuild_params params = { .mergeroot = root };
 
 	switch (dataimport_mode) {
 	case EROFS_MKFS_DATA_IMPORT_DEFAULT:
-		datamode = EROFS_REBUILD_DATA_BLOB_INDEX;
+		params.datamode = EROFS_REBUILD_DATA_BLOB_INDEX;
 		break;
 	case EROFS_MKFS_DATA_IMPORT_FULLDATA:
-		datamode = EROFS_REBUILD_DATA_FULL;
+		params.datamode = EROFS_REBUILD_DATA_FULL;
 		break;
 	case EROFS_MKFS_DATA_IMPORT_RVSP:
-		datamode = EROFS_REBUILD_DATA_RESVSP;
+		params.datamode = EROFS_REBUILD_DATA_RESVSP;
 		break;
 	default:
 		return -EINVAL;
@@ -1706,7 +1706,8 @@ static int erofs_mkfs_rebuild_load_trees(struct erofs_inode *root)
 
 	list_for_each_entry(src, &rebuild_src_list, list) {
 		src->xamgr = g_sbi.xamgr;
-		ret = erofs_rebuild_load_tree(root, src, datamode);
+		params.sbi = src;
+		ret = erofs_rebuild_load_tree(&params);
 		src->xamgr = NULL;
 		if (ret) {
 			erofs_err("failed to load %s", src->dif0.src_path);
@@ -1720,7 +1721,7 @@ static int erofs_mkfs_rebuild_load_trees(struct erofs_inode *root)
 		extra_devices += src->extra_devices;
 	}
 
-	if (datamode == EROFS_REBUILD_DATA_RESVSP)
+	if (params.datamode == EROFS_REBUILD_DATA_RESVSP)
 		return 0;
 
 	/* Each blob has either no extra device or only one device for TarFS */
@@ -1730,7 +1731,7 @@ static int erofs_mkfs_rebuild_load_trees(struct erofs_inode *root)
 		return -EOPNOTSUPP;
 	}
 
-	if (datamode == EROFS_REBUILD_DATA_FULL)
+	if (params.datamode == EROFS_REBUILD_DATA_FULL)
 		return 0;
 
 	ret = erofs_mkfs_init_devices(&g_sbi, rebuild_src_count);
Signature · 1 line
2.54.0 (Apple Git-157)
author
#6 lore raw

PATCH v3 7/10 erofs-utils: lib: add persistent dm-snapshot parser

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

Add file-delta support for version 1 dm-snapshot-persistent stores. Validate the header and exception records, sort exceptions by logical offset and derive the covered range from them.

The parser borrows the COW vfile from its caller. Rebuild integration will follow.

Assisted-by Codex:gpt-6-astra
Assisted-by Codex:gpt-6.1-sol
---
 lib/Makefile.am            |   4 +-
 lib/deltas/dm_persistent.c | 239 +++++++++++++++++++++++++++++++++++++
 lib/liberofs_file_delta.h  |  32 +++++
 3 files changed, 274 insertions(+), 1 deletion(-)
 create mode 100644 lib/deltas/dm_persistent.c
 create mode 100644 lib/liberofs_file_delta.h
lib/Makefile.am +3 -1
index 8654479..e84030d 100644
@@ -28,6 +28,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/lib/liberofs_cache.h \
       $(top_srcdir)/lib/liberofs_chunk.h \
       $(top_srcdir)/lib/liberofs_compress.h \
+      $(top_srcdir)/lib/liberofs_file_delta.h \
       $(top_srcdir)/lib/liberofs_fragments.h \
       $(top_srcdir)/lib/liberofs_private.h \
       $(top_srcdir)/lib/liberofs_rebuild.h \
@@ -48,7 +49,8 @@ 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 manifest.c
+		      vmdk.c metabox.c global.c importer.c base64.c manifest.c \
+		      deltas/dm_persistent.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 liberofs_la_LIBADD = $(LIBEROFS_LIBS)
lib/deltas/dm_persistent.c new file +239 -0

239 lines not on this page · raw message

lib/liberofs_file_delta.h new file +32 -0
new file mode 100644
index 0000000..17fb0d5
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR MIT */
+#ifndef __EROFS_LIB_LIBEROFS_FILE_DELTA_H
+#define __EROFS_LIB_LIBEROFS_FILE_DELTA_H
+
+#include "erofs/internal.h"
+
+/* A logical replacement; gaps between extents inherit the parent contents. */
+struct erofs_delta_map {
+	erofs_off_t offset, length;
+	/* Borrowed raw container, never a guest-addressed view. NULL for ZERO. */
+	struct erofs_vfile *vf;
+	erofs_off_t sourceoffset;
+};
+
+struct erofs_file_delta;
+
+struct erofs_file_delta_ops {
+	/* Find a replacement at or after map->offset; length 0 means none. */
+	int (*map)(struct erofs_file_delta *fdl, struct erofs_delta_map *map);
+	void (*close)(struct erofs_file_delta *fdl);
+	const char *name;		/* delta format name */
+};
+
+struct erofs_file_delta {
+	const struct erofs_file_delta_ops *ops;
+	u8 private[];
+};
+
+/* The caller retains ownership of vf. Return ERR_PTR() on failure. */
+struct erofs_file_delta *erofs_dm_persistent_open(struct erofs_vfile *vf);
+
+#endif
Signature · 1 line
2.54.0 (Apple Git-157)
author
#7 lore raw

PATCH v3 8/10 erofs-utils: lib: add file-delta alignment callback

In reply to Gao Xiang

In order to select chunk sizes for file-delta updates, add a get_alignment callback reporting the greatest power-of-two alignment shared by replacement boundaries.

Accumulate these boundaries when adding dm-snapshot exceptions and keep the exception array and counters in the snapshot handle.

Assisted-by Codex:gpt-6.1-sol
---
 lib/deltas/dm_persistent.c | 58 ++++++++++++++++++++++----------------
 lib/liberofs_file_delta.h  | 10 ++++++-
 2 files changed, 43 insertions(+), 25 deletions(-)
lib/deltas/dm_persistent.c +34 -24
index 9e4c5c7..21f4f0d 100644
@@ -24,17 +24,15 @@ struct erofs_dm_exception {
 	erofs_off_t offset, source_offset;
 };
 
-/* Sorted by logical file offset after opening the snapshot. */
-struct erofs_dm_exceptions {
-	struct erofs_dm_exception *entries;
-	size_t count, capacity;
-};
-
 struct erofs_dm_snap_persistent {
 	struct erofs_file_delta fdl;
-	struct erofs_dm_exceptions exceptions;
+	struct {
+		/* Sorted by logical file offset after opening the snapshot. */
+		struct erofs_dm_exception *entries;
+		size_t count, capacity;
+	} exceptions;
 	struct erofs_vfile *vf;
-	erofs_off_t target_size, chunk_bytes;
+	erofs_off_t target_size, chunk_bytes, boundary;
 };
 
 static const struct erofs_file_delta_ops erofs_file_delta_dm_persistent_ops;
@@ -46,35 +44,35 @@ static int erofs_dm_exception_cmp(const void *a, const void *b)
 	return cmpsgn(ea->offset, eb->offset);
 }
 
-static int erofs_dm_add_exception(struct erofs_dm_exceptions *exceptions,
+static int erofs_dm_add_exception(struct erofs_dm_snap_persistent *dsp,
 				 erofs_off_t offset,
 				 erofs_off_t source_offset)
 {
 	struct erofs_dm_exception *p;
 	size_t capacity;
 
-	if (exceptions->count == exceptions->capacity) {
-		capacity = exceptions->capacity ? exceptions->capacity * 2 : 64;
-		if (capacity < exceptions->capacity)
+	if (dsp->exceptions.count == dsp->exceptions.capacity) {
+		capacity = dsp->exceptions.capacity ? dsp->exceptions.capacity * 2 : 64;
+		if (capacity < dsp->exceptions.capacity)
 			return -E2BIG;
 		if (capacity > SIZE_MAX / sizeof(*p))
 			return -E2BIG;
-		p = realloc(exceptions->entries, capacity * sizeof(*p));
+		p = realloc(dsp->exceptions.entries, capacity * sizeof(*p));
 		if (!p)
 			return -ENOMEM;
-		exceptions->entries = p;
-		exceptions->capacity = capacity;
+		dsp->exceptions.entries = p;
+		dsp->exceptions.capacity = capacity;
 	}
-	exceptions->entries[exceptions->count++] = (struct erofs_dm_exception) {
+	dsp->exceptions.entries[dsp->exceptions.count++] = (struct erofs_dm_exception) {
 		.offset = offset,
 		.source_offset = source_offset,
 	};
+	dsp->boundary |= offset | (offset + dsp->chunk_bytes);
 	return 0;
 }
 
 static int erofs_dm_parse_exceptions(struct erofs_dm_snap_persistent *dsp)
 {
-	struct erofs_dm_exceptions *exceptions = &dsp->exceptions;
 	erofs_off_t chunk_bytes = dsp->chunk_bytes;
 	/* Read metadata in bounded batches, independently of the dm chunk size. */
 	struct erofs_dm_disk_exception buf[256];
@@ -121,8 +119,8 @@ static int erofs_dm_parse_exceptions(struct erofs_dm_snap_persistent *dsp)
 					goto out;
 				}
 				source_offset = new_chunk * chunk_bytes;
-				ret = erofs_dm_add_exception(exceptions,
-							     offset, source_offset);
+				ret = erofs_dm_add_exception(dsp, offset,
+							     source_offset);
 				if (ret)
 					goto out;
 			}
@@ -134,16 +132,16 @@ static int erofs_dm_parse_exceptions(struct erofs_dm_snap_persistent *dsp)
 	}
 terminated:
 	/* Exceptions are stored in commit order, not logical file order. */
-	if (exceptions->count > 1)
-		qsort(exceptions->entries, exceptions->count,
-		      sizeof(*exceptions->entries), erofs_dm_exception_cmp);
+	if (dsp->exceptions.count > 1)
+		qsort(dsp->exceptions.entries, dsp->exceptions.count,
+		      sizeof(*dsp->exceptions.entries), erofs_dm_exception_cmp);
 	/*
 	 * The kernel reuses existing exceptions on repeated writes, but its
 	 * loader does not reject duplicate origin chunks.  Reject them here
 	 * rather than choosing an ambiguous mapping.
 	 */
-	for (n = 1; n < exceptions->count; ++n) {
-		if (exceptions->entries[n - 1].offset == exceptions->entries[n].offset)
+	for (n = 1; n < dsp->exceptions.count; ++n) {
+		if (dsp->exceptions.entries[n - 1].offset == dsp->exceptions.entries[n].offset)
 			return -EINVAL;
 	}
 	ret = 0;
@@ -151,6 +149,17 @@ out:
 	return ret;
 }
 
+static erofs_off_t erofs_dm_persistent_get_alignment(struct erofs_file_delta *fdl)
+{
+	struct erofs_dm_snap_persistent *dsp =
+		container_of(fdl, struct erofs_dm_snap_persistent, fdl);
+	erofs_off_t boundary = dsp->boundary;
+
+	if (!dsp->exceptions.count)
+		return 0;
+	return boundary ? (boundary & -boundary) : BIT_ULL(63);
+}
+
 static int erofs_dm_persistent_map(struct erofs_file_delta *fdl,
 				   struct erofs_delta_map *map)
 {
@@ -233,6 +242,7 @@ struct erofs_file_delta *erofs_dm_persistent_open(struct erofs_vfile *vf)
 }
 
 static const struct erofs_file_delta_ops erofs_file_delta_dm_persistent_ops = {
+	.get_alignment = erofs_dm_persistent_get_alignment,
 	.map = erofs_dm_persistent_map,
 	.close = erofs_dm_persistent_close,
 	.name = "dm-persistent",
lib/liberofs_file_delta.h +9 -1
index 17fb0d5..307086e 100644
@@ -15,7 +15,15 @@ struct erofs_delta_map {
 struct erofs_file_delta;
 
 struct erofs_file_delta_ops {
-	/* Find a replacement at or after map->offset; length 0 means none. */
+	/*
+	 * Maximum power-of-two boundary alignment in bytes; 0 means no updates.
+	 * EOF need not be aligned. BIT_ULL(63) means no boundary constraint.
+	 */
+	erofs_off_t (*get_alignment)(struct erofs_file_delta *fdl);
+	/*
+	 * Find a replacement at or after map->offset; length 0 means none.
+	 * Mappings remain stable until close; callers may scan them again.
+	 */
 	int (*map)(struct erofs_file_delta *fdl, struct erofs_delta_map *map);
 	void (*close)(struct erofs_file_delta *fdl);
 	const char *name;		/* delta format name */
Signature · 1 line
2.54.0 (Apple Git-157)
author
#8 lore raw

PATCH v3 9/10 erofs-utils: mkfs: give each rebuild source its own device range

In reply to Gao Xiang

Blob-index rebuilds assume that each source uses only one data device. This does not work when a source uses both its main image and external devices, or has more than one external device.

Set up the output device table before loading trees. Give each source image and its external devices their own range of device IDs, and update chunk indexes and compressed extents to use the new IDs. Keep holes as unmapped chunks.

Keep source image filenames for VMDK paths and check chunk-index sizes before allocating their buffers.

Assisted-by Codex:gpt-6.1-sol
---
 lib/compress.c |  4 +--
 lib/rebuild.c  | 37 ++++++++++++++++++-----
 mkfs/main.c    | 81 ++++++++++++++++++++++----------------------------
 3 files changed, 68 insertions(+), 54 deletions(-)
lib/compress.c +2 -2
index e764e44..088d5b7 100644
@@ -2562,7 +2562,7 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 				.length = map.m_llen,
 				.plen = erofs_blksiz(sbi),
 				.pstart = (pend != EROFS_NULL_ADDR ? pend : 0),
-				.device_id = inode->dev,	/* FIXME! */
+				.device_id = inode->dev + mdev.m_deviceid,
 				.raw = raw,
 				.inlined = true,
 			};
@@ -2575,7 +2575,7 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 				.length = map.m_llen,
 				.plen = map.m_plen,
 				.pstart = mdev.m_pa,
-				.device_id = inode->dev,	/* FIXME! */
+				.device_id = inode->dev + mdev.m_deviceid,
 				.partial = map.m_flags & EROFS_MAP_PARTIAL_REF,
 				.raw = raw,
 				.inlined = false,
lib/rebuild.c +30 -7
index 6a4aff6..85f263b 100644
@@ -247,8 +247,9 @@ static int erofs_rebuild_load_chunk_index(struct erofs_sb_info *dst_sb,
 					  struct erofs_inode *inode)
 {
 	int ret;
-	unsigned int count, unit, chunkbits, i;
-	struct erofs_inode_chunk_index *idx;
+	unsigned int unit, chunkbits;
+	struct erofs_chunkitem **idx;
+	u64 count, i;
 	erofs_off_t chunksize;
 	erofs_blk_t blkaddr;
 
@@ -274,11 +275,13 @@ static int erofs_rebuild_load_chunk_index(struct erofs_sb_info *dst_sb,
 	}
 
 	chunksize = 1ULL << chunkbits;
-	count = DIV_ROUND_UP(inode->i_size, chunksize);
+	count = inode->i_size / chunksize + !!(inode->i_size % chunksize);
 
 	unit = sizeof(struct erofs_inode_chunk_index);
+	if (count > min_t(u64, UINT_MAX, SSIZE_MAX) / unit)
+		return -E2BIG;
 	inode->extent_isize = count * unit;
-	idx = calloc(count, max(sizeof(*idx), sizeof(void *)));
+	idx = calloc(count, max_t(size_t, sizeof(*idx), unit));
 	if (!idx)
 		return -ENOMEM;
 	inode->chunkindexes = idx;
@@ -294,16 +297,36 @@ static int erofs_rebuild_load_chunk_index(struct erofs_sb_info *dst_sb,
 		if (ret)
 			goto err;
 
-		blkaddr = erofs_blknr(dst_sb, map.m_pa);
-		chunk = erofs_get_unhashed_chunk(dst_sb, inode->dev, blkaddr, 0);
+		if (!(map.m_flags & EROFS_MAP_MAPPED)) {
+			chunk = erofs_get_unhashed_chunk(dst_sb, 0, EROFS_NULL_ADDR, 0);
+		} else {
+			struct erofs_sb_info *src = inode->sbi;
+			struct erofs_map_dev mdev = {
+				.m_deviceid = map.m_deviceid,
+				.m_pa = map.m_pa,
+			};
+
+			ret = erofs_map_dev(src, &mdev);
+			if (ret)
+				goto err;
+			blkaddr = erofs_blknr(src, mdev.m_pa);
+			/* dif->blocks may be 0; only uniaddr is valid for mapping */
+			if (erofs_blkoff(src, mdev.m_pa)) {
+				ret = -EFSCORRUPTED;
+				goto err;
+			}
+			chunk = erofs_get_unhashed_chunk(dst_sb,
+					inode->dev + mdev.m_deviceid, blkaddr, 0);
+		}
 		if (IS_ERR(chunk)) {
 			ret = PTR_ERR(chunk);
 			goto err;
 		}
-		*(void **)idx++ = chunk;
+		*idx++ = chunk;
 
 	}
 	inode->datalayout = EROFS_INODE_CHUNK_BASED;
+	inode->u.chunkbits = chunkbits;
 	inode->u.chunkformat = EROFS_CHUNK_FORMAT_INDEXES;
 	inode->u.chunkformat |= chunkbits - dst_sb->blkszbits;
 	return 0;
mkfs/main.c +36 -45

99 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
author
#9 lore raw

PATCH v3 10/10 erofs-utils: mkfs: support file deltas in manifests

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

Add DIFF[format] manifest records to apply file updates after merging source images. Initially support DM_SNAP for sealed dm-persistent snapshot COW stores, with COW paths relative to the manifest data directory.

For example, add the following record to foo.m:

DIFF[DM_SNAP] /disk.img changes.cow

Then apply it to the merged source tree:

  mkfs.erofs --manifest=foo.m l2.erofs l0.erofs l1.erofs datadir/

Reuse unchanged chunk indexes and split inherited chunks as needed to match replacement boundaries. Write changed data to the primary device and turn all-zero replacement chunks into holes. Preserve file sizes and hard links. Apply records in manifest order, with later records overriding overlapping changes to the same inode.

Updates currently require prepared chunk indexes. Preparing targets from local files, full-data or reserved-space imports, and refreshing inode-digest xattrs remain TODOs.

Assisted-by Codex:gpt-6.1-sol
---
 lib/blobchunk.c      | 130 ++++++++++++++++++++++++++++++++++++++++++-
 lib/liberofs_chunk.h |   3 +
 lib/manifest.c       | 124 ++++++++++++++++++++++++++++++-----------
 lib/rebuild.c        |  12 ++--
 mkfs/main.c          | 118 ++++++++++++++++++++++++---------------
 5 files changed, 302 insertions(+), 85 deletions(-)
lib/blobchunk.c +128 -2
index a3a88d3..5b51d93 100644
@@ -10,6 +10,7 @@
 #include "erofs/importer.h"
 #include "liberofs_cache.h"
 #include "liberofs_chunk.h"
+#include "liberofs_file_delta.h"
 #include "liberofs_private.h"
 #include "liberofs_sha256.h"
 #include <unistd.h>
@@ -81,8 +82,7 @@ static unsigned int memhash(const void *buf, size_t len)
 }
 
 static struct erofs_chunkitem *erofs_get_chunk(struct erofs_sb_info *sbi,
-					       int device_id,
-					       u8 *buf, u64 size)
+					       int device_id, u8 *buf, u64 size)
 {
 	struct erofs_bufmgr *bmgr = device_id ? sbi->devs[device_id - 1].bmgr : sbi->bmgr;
 	struct erofs_chunkmgr *chunkmgr = sbi->chunkmgr;
@@ -161,6 +161,132 @@ static struct erofs_chunkitem *erofs_get_chunk(struct erofs_sb_info *sbi,
 	return chunk;
 }
 
+/* Bound the buffer for a single newly written chunk, even after extent merging. */
+#define EROFS_DELTA_CHUNK_MAX_BITS	26
+
+int erofs_chunk_apply_deltas(struct erofs_inode *inode,
+			     struct erofs_file_delta *fdl)
+{
+	struct erofs_sb_info *sbi = inode->sbi;
+	struct erofs_delta_map delta;
+	struct erofs_chunkitem **indexes, **old = inode->chunkindexes, *chunk;
+	erofs_off_t pos, alignment, len;
+	unsigned int oldbits, chunkbits;
+	u64 count, i;
+	size_t buflen;
+	u8 *buf = NULL;
+	int ret;
+
+	if (inode->datalayout != EROFS_INODE_CHUNK_BASED)
+		return -EOPNOTSUPP;
+	if (!fdl)
+		return 0;
+	alignment = fdl->ops->get_alignment(fdl);
+	if (!alignment)
+		return 0;
+	DBG_BUGON(!old);
+	oldbits = inode->u.chunkbits;
+	chunkbits = min_t(unsigned int, oldbits, ilog2(alignment));
+	chunkbits = min_t(unsigned int, chunkbits, EROFS_DELTA_CHUNK_MAX_BITS);
+	if (chunkbits < sbi->blkszbits)
+		return -EOPNOTSUPP;
+	count = round_up(inode->i_size, 1ULL << chunkbits) >> chunkbits;
+	if (count > min_t(u64, UINT_MAX, SSIZE_MAX) /
+		    sizeof(struct erofs_inode_chunk_index))
+		return -E2BIG;
+	indexes = calloc(count, sizeof(*indexes));
+	if (!indexes)
+		return -ENOMEM;
+	ret = erofs_blob_init(sbi, 0);
+	if (ret)
+		goto out;
+	for (i = 0; i < count; ++i) {
+		erofs_off_t within;
+
+		pos = i << chunkbits;
+		chunk = old[pos >> oldbits];
+		within = pos & ((1ULL << oldbits) - 1);
+		if (within && chunk->blkaddr != EROFS_NULL_ADDR) {
+			chunk = erofs_get_unhashed_chunk(sbi, chunk->device_id,
+				chunk->blkaddr + erofs_blknr(sbi, within), 0);
+			if (IS_ERR(chunk)) {
+				ret = PTR_ERR(chunk);
+				goto out;
+			}
+		}
+		indexes[i] = chunk;
+	}
+	delta.offset = 0;
+	while (delta.offset < inode->i_size) {
+		pos = delta.offset;
+		ret = fdl->ops->map(fdl, &delta);
+		if (ret)
+			goto out;
+		if (!delta.length)
+			break;
+		if (delta.offset < pos) {
+			ret = -EINVAL;
+			goto out;
+		}
+		if (delta.offset >= inode->i_size)
+			break;
+		delta.length = min(delta.length, inode->i_size - delta.offset);
+		if (delta.vf && delta.sourceoffset > UINT64_MAX - delta.length) {
+			ret = -EINVAL;
+			goto out;
+		}
+		if (delta.vf && !buf) {
+			buflen = min_t(erofs_off_t, 1ULL << chunkbits, inode->i_size);
+			buf = malloc(buflen);
+			if (!buf) {
+				ret = -ENOMEM;
+				goto out;
+			}
+		}
+		for (pos = 0; pos < delta.length; pos += len) {
+			bool zero = !delta.vf;
+
+			len = min_t(erofs_off_t, 1ULL << chunkbits, delta.length - pos);
+			if (!zero) {
+				const u8 *p = buf, *end = buf + len;
+
+				ret = erofs_pread(delta.vf, buf, len,
+						  delta.sourceoffset + pos);
+				if (ret)
+					goto out;
+				while (p < end && !*p)
+					++p;
+				zero = p == end;
+			}
+			/* All-zero chunks collapse into a hole. */
+			if (zero)
+				chunk = erofs_get_unhashed_chunk(sbi, 0, EROFS_NULL_ADDR, 0);
+			else
+				chunk = erofs_get_chunk(sbi, 0, buf, len);
+			if (IS_ERR(chunk)) {
+				ret = PTR_ERR(chunk);
+				goto out;
+			}
+			indexes[(delta.offset + pos) >> chunkbits] = chunk;
+		}
+		delta.offset += delta.length;
+	}
+	free(old);
+	inode->chunkindexes = indexes;
+	inode->extent_isize = count * sizeof(struct erofs_inode_chunk_index);
+	inode->datalayout = EROFS_INODE_CHUNK_BASED;
+	inode->u.chunkbits = chunkbits;
+	inode->u.chunkformat = (inode->u.chunkformat & EROFS_CHUNK_FORMAT_48BIT) |
+		EROFS_CHUNK_FORMAT_INDEXES |
+		(chunkbits - sbi->blkszbits);
+	indexes = NULL;
+	ret = 0;
+out:
+	free(buf);
+	free(indexes);
+	return ret;
+}
+
 void erofs_inode_fixup_chunkformat(struct erofs_inode *inode)
 {
 	unsigned int unit, src;
lib/liberofs_chunk.h +3 -0
index 4ef6640..bf73bc6 100644
@@ -17,6 +17,9 @@ extern "C"
 struct erofs_chunkitem *erofs_get_unhashed_chunk(struct erofs_sb_info *sbi,
 		unsigned int device_id, erofs_blk_t blkaddr,
 		erofs_off_t sourceoffset);
+struct erofs_file_delta;
+int erofs_chunk_apply_deltas(struct erofs_inode *inode,
+			     struct erofs_file_delta *fdl);
 void erofs_inode_fixup_chunkformat(struct erofs_inode *inode);
 s64 erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
 				 erofs_off_t off);
lib/manifest.c +91 -33

184 lines not on this page · raw message

lib/rebuild.c +8 -4
index 85f263b..e6f0d13 100644
@@ -666,7 +666,8 @@ int erofs_rebuild_load_tree(struct erofs_rebuild_params *params)
 		    !erofs_sb_has_big_pcluster(dst_sbi)) {
 			erofs_err("failed to load tree from image %d with big pclusters",
 				  sbi->dev);
-			return -EOPNOTSUPP;
+			ret = -EOPNOTSUPP;
+			goto out;
 		}
 		if (erofs_sb_has_48bit(sbi))
 			erofs_sb_set_48bit(dst_sbi);
@@ -677,11 +678,13 @@ int erofs_rebuild_load_tree(struct erofs_rebuild_params *params)
 	ret = erofs_read_inode_from_disk(&inode);
 	if (ret) {
 		erofs_err("failed to read root inode of %s", fsid);
-		return ret;
+		goto out;
 	}
 	inode.i_srcpath = strdup("/");
-	if (!inode.i_srcpath)
-		return -ENOMEM;
+	if (!inode.i_srcpath) {
+		ret = -ENOMEM;
+		goto out;
+	}
 
 	ctx = (struct erofs_rebuild_dir_context) {
 		.ctx.dir = &inode,
@@ -690,6 +693,7 @@ int erofs_rebuild_load_tree(struct erofs_rebuild_params *params)
 		.params = params,
 	};
 	ret = erofs_iterate_dir(&ctx.ctx, false);
+out:
 	free(inode.i_srcpath);
 	return ret;
 }
mkfs/main.c +72 -46

237 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
author
#10 lore raw

PATCH v4 7/10 erofs-utils: lib: add persistent dm-snapshot parser

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

Add file-delta support for version 1 dm-snapshot-persistent stores. Validate the header and exception records, sort exceptions by logical offset and derive the covered range from them.

The parser borrows the COW vfile from its caller. Rebuild integration will follow.

Assisted-by Codex:gpt-6-astra
Assisted-by Codex:gpt-6.1-sol
---
 lib/Makefile.am            |   4 +-
 lib/deltas/dm_persistent.c | 239 +++++++++++++++++++++++++++++++++++++
 lib/liberofs_file_delta.h  |  32 +++++
 3 files changed, 274 insertions(+), 1 deletion(-)
 create mode 100644 lib/deltas/dm_persistent.c
 create mode 100644 lib/liberofs_file_delta.h
lib/Makefile.am +3 -1
index 8654479..e84030d 100644
@@ -28,6 +28,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/lib/liberofs_cache.h \
       $(top_srcdir)/lib/liberofs_chunk.h \
       $(top_srcdir)/lib/liberofs_compress.h \
+      $(top_srcdir)/lib/liberofs_file_delta.h \
       $(top_srcdir)/lib/liberofs_fragments.h \
       $(top_srcdir)/lib/liberofs_private.h \
       $(top_srcdir)/lib/liberofs_rebuild.h \
@@ -48,7 +49,8 @@ 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 manifest.c
+		      vmdk.c metabox.c global.c importer.c base64.c manifest.c \
+		      deltas/dm_persistent.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 liberofs_la_LIBADD = $(LIBEROFS_LIBS)
lib/deltas/dm_persistent.c new file +239 -0

239 lines not on this page · raw message

lib/liberofs_file_delta.h new file +32 -0
new file mode 100644
index 0000000..6112a1c
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR MIT */
+#ifndef __EROFS_LIB_LIBEROFS_FILE_DELTA_H
+#define __EROFS_LIB_LIBEROFS_FILE_DELTA_H
+
+#include "erofs/internal.h"
+
+/* A logical replacement; gaps between extents inherit the parent contents. */
+struct erofs_delta_map {
+	erofs_off_t offset, length;
+	/* Borrowed raw container, never a guest-addressed view. NULL for ZERO. */
+	struct erofs_vfile *vf;
+	erofs_off_t sourceoffset;
+};
+
+struct erofs_file_delta;
+
+struct erofs_file_delta_ops {
+	/* Find a replacement at or after map->offset; length 0 means none. */
+	int (*map)(struct erofs_file_delta *fdl, struct erofs_delta_map *map);
+	void (*close)(struct erofs_file_delta *fdl);
+	const char *name;		/* delta format name */
+};
+
+struct erofs_file_delta {
+	const struct erofs_file_delta_ops *ops;
+	u8 private[0];
+};
+
+/* The caller retains ownership of vf. Return ERR_PTR() on failure. */
+struct erofs_file_delta *erofs_dm_persistent_open(struct erofs_vfile *vf);
+
+#endif
Signature · 1 line
2.54.0 (Apple Git-157)
author
#11 lore raw

PATCH v4 8/10 erofs-utils: lib: add file-delta alignment callback

In order to select chunk sizes for file-delta updates, add a get_alignment callback reporting the greatest power-of-two alignment shared by replacement boundaries.

Accumulate these boundaries when adding dm-snapshot exceptions and keep the exception array and counters in the snapshot handle.

Assisted-by Codex:gpt-6.1-sol
---
 lib/deltas/dm_persistent.c | 58 ++++++++++++++++++++++----------------
 lib/liberofs_file_delta.h  | 10 ++++++-
 2 files changed, 43 insertions(+), 25 deletions(-)
lib/deltas/dm_persistent.c +34 -24
index 9e4c5c7..21f4f0d 100644
@@ -24,17 +24,15 @@ struct erofs_dm_exception {
 	erofs_off_t offset, source_offset;
 };
 
-/* Sorted by logical file offset after opening the snapshot. */
-struct erofs_dm_exceptions {
-	struct erofs_dm_exception *entries;
-	size_t count, capacity;
-};
-
 struct erofs_dm_snap_persistent {
 	struct erofs_file_delta fdl;
-	struct erofs_dm_exceptions exceptions;
+	struct {
+		/* Sorted by logical file offset after opening the snapshot. */
+		struct erofs_dm_exception *entries;
+		size_t count, capacity;
+	} exceptions;
 	struct erofs_vfile *vf;
-	erofs_off_t target_size, chunk_bytes;
+	erofs_off_t target_size, chunk_bytes, boundary;
 };
 
 static const struct erofs_file_delta_ops erofs_file_delta_dm_persistent_ops;
@@ -46,35 +44,35 @@ static int erofs_dm_exception_cmp(const void *a, const void *b)
 	return cmpsgn(ea->offset, eb->offset);
 }
 
-static int erofs_dm_add_exception(struct erofs_dm_exceptions *exceptions,
+static int erofs_dm_add_exception(struct erofs_dm_snap_persistent *dsp,
 				 erofs_off_t offset,
 				 erofs_off_t source_offset)
 {
 	struct erofs_dm_exception *p;
 	size_t capacity;
 
-	if (exceptions->count == exceptions->capacity) {
-		capacity = exceptions->capacity ? exceptions->capacity * 2 : 64;
-		if (capacity < exceptions->capacity)
+	if (dsp->exceptions.count == dsp->exceptions.capacity) {
+		capacity = dsp->exceptions.capacity ? dsp->exceptions.capacity * 2 : 64;
+		if (capacity < dsp->exceptions.capacity)
 			return -E2BIG;
 		if (capacity > SIZE_MAX / sizeof(*p))
 			return -E2BIG;
-		p = realloc(exceptions->entries, capacity * sizeof(*p));
+		p = realloc(dsp->exceptions.entries, capacity * sizeof(*p));
 		if (!p)
 			return -ENOMEM;
-		exceptions->entries = p;
-		exceptions->capacity = capacity;
+		dsp->exceptions.entries = p;
+		dsp->exceptions.capacity = capacity;
 	}
-	exceptions->entries[exceptions->count++] = (struct erofs_dm_exception) {
+	dsp->exceptions.entries[dsp->exceptions.count++] = (struct erofs_dm_exception) {
 		.offset = offset,
 		.source_offset = source_offset,
 	};
+	dsp->boundary |= offset | (offset + dsp->chunk_bytes);
 	return 0;
 }
 
 static int erofs_dm_parse_exceptions(struct erofs_dm_snap_persistent *dsp)
 {
-	struct erofs_dm_exceptions *exceptions = &dsp->exceptions;
 	erofs_off_t chunk_bytes = dsp->chunk_bytes;
 	/* Read metadata in bounded batches, independently of the dm chunk size. */
 	struct erofs_dm_disk_exception buf[256];
@@ -121,8 +119,8 @@ static int erofs_dm_parse_exceptions(struct erofs_dm_snap_persistent *dsp)
 					goto out;
 				}
 				source_offset = new_chunk * chunk_bytes;
-				ret = erofs_dm_add_exception(exceptions,
-							     offset, source_offset);
+				ret = erofs_dm_add_exception(dsp, offset,
+							     source_offset);
 				if (ret)
 					goto out;
 			}
@@ -134,16 +132,16 @@ static int erofs_dm_parse_exceptions(struct erofs_dm_snap_persistent *dsp)
 	}
 terminated:
 	/* Exceptions are stored in commit order, not logical file order. */
-	if (exceptions->count > 1)
-		qsort(exceptions->entries, exceptions->count,
-		      sizeof(*exceptions->entries), erofs_dm_exception_cmp);
+	if (dsp->exceptions.count > 1)
+		qsort(dsp->exceptions.entries, dsp->exceptions.count,
+		      sizeof(*dsp->exceptions.entries), erofs_dm_exception_cmp);
 	/*
 	 * The kernel reuses existing exceptions on repeated writes, but its
 	 * loader does not reject duplicate origin chunks.  Reject them here
 	 * rather than choosing an ambiguous mapping.
 	 */
-	for (n = 1; n < exceptions->count; ++n) {
-		if (exceptions->entries[n - 1].offset == exceptions->entries[n].offset)
+	for (n = 1; n < dsp->exceptions.count; ++n) {
+		if (dsp->exceptions.entries[n - 1].offset == dsp->exceptions.entries[n].offset)
 			return -EINVAL;
 	}
 	ret = 0;
@@ -151,6 +149,17 @@ out:
 	return ret;
 }
 
+static erofs_off_t erofs_dm_persistent_get_alignment(struct erofs_file_delta *fdl)
+{
+	struct erofs_dm_snap_persistent *dsp =
+		container_of(fdl, struct erofs_dm_snap_persistent, fdl);
+	erofs_off_t boundary = dsp->boundary;
+
+	if (!dsp->exceptions.count)
+		return 0;
+	return boundary ? (boundary & -boundary) : BIT_ULL(63);
+}
+
 static int erofs_dm_persistent_map(struct erofs_file_delta *fdl,
 				   struct erofs_delta_map *map)
 {
@@ -233,6 +242,7 @@ struct erofs_file_delta *erofs_dm_persistent_open(struct erofs_vfile *vf)
 }
 
 static const struct erofs_file_delta_ops erofs_file_delta_dm_persistent_ops = {
+	.get_alignment = erofs_dm_persistent_get_alignment,
 	.map = erofs_dm_persistent_map,
 	.close = erofs_dm_persistent_close,
 	.name = "dm-persistent",
lib/liberofs_file_delta.h +9 -1
index 6112a1c..ab6d54c 100644
@@ -15,7 +15,15 @@ struct erofs_delta_map {
 struct erofs_file_delta;
 
 struct erofs_file_delta_ops {
-	/* Find a replacement at or after map->offset; length 0 means none. */
+	/*
+	 * Maximum power-of-two boundary alignment in bytes; 0 means no updates.
+	 * EOF need not be aligned. BIT_ULL(63) means no boundary constraint.
+	 */
+	erofs_off_t (*get_alignment)(struct erofs_file_delta *fdl);
+	/*
+	 * Find a replacement at or after map->offset; length 0 means none.
+	 * Mappings remain stable until close; callers may scan them again.
+	 */
 	int (*map)(struct erofs_file_delta *fdl, struct erofs_delta_map *map);
 	void (*close)(struct erofs_file_delta *fdl);
 	const char *name;		/* delta format name */
Signature · 1 line
2.54.0 (Apple Git-157)
author
#12 lore raw

PATCH v4 9/10 erofs-utils: mkfs: support multiple data devices per source

In reply to Gao Xiang

Support source images that store file data in both the primary device and external devices.

This changes the device list for TarFS fsmerge: source primary devices are now included by default. Use --omit-fsmeta to keep the previous behavior, referencing only tar blobs. Fail if data refers to an omitted device. Sources without external devices keep their primary device.

Assisted-by Codex:gpt-6.1-sol
---
 lib/compress.c         |  17 ++++--
 lib/liberofs_rebuild.h |   1 +
 lib/rebuild.c          |  58 +++++++++++++++-----
 man/mkfs.erofs.1       |   5 ++
 mkfs/main.c            | 117 +++++++++++++++++++++++++----------------
 5 files changed, 135 insertions(+), 63 deletions(-)
lib/compress.c +14 -3
index e764e44..fac15b5 100644
@@ -24,6 +24,7 @@
 #endif
 #include "liberofs_cache.h"
 #include "liberofs_compress.h"
+#include "liberofs_rebuild.h"
 #include "liberofs_fragments.h"
 #include "liberofs_metabox.h"
 #include "erofs/decompress.h"
@@ -2496,9 +2497,10 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
 	return 0;
 }
 
-int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
+int z_erofs_rebuild_load_metadata(struct erofs_rebuild_params *params,
 				  struct erofs_inode *inode)
 {
+	struct erofs_sb_info *dst_sbi = params->mergeroot->sbi;
 	struct erofs_sb_info *sbi = inode->sbi;
 	struct erofs_map_blocks map = {
 		.buf = __EROFS_BUF_INITIALIZER,
@@ -2532,6 +2534,15 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 		if (err)
 			goto err_out;
 
+		if (params->force_omit_fsmeta && (map.m_flags & EROFS_MAP_MAPPED) &&
+		    !(map.m_flags & (EROFS_MAP_META | __EROFS_MAP_FRAGMENT)) &&
+		    !mdev.m_deviceid) {
+			erofs_err("%s: file data refers to the source primary device",
+				  inode->i_srcpath);
+			err = -EINVAL;
+			goto err_out;
+		}
+
 		raw = (map.m_algorithmformat >= Z_EROFS_COMPRESSION_MAX);
 
 		ei = malloc(sizeof(*ei));
@@ -2562,7 +2573,7 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 				.length = map.m_llen,
 				.plen = erofs_blksiz(sbi),
 				.pstart = (pend != EROFS_NULL_ADDR ? pend : 0),
-				.device_id = inode->dev,	/* FIXME! */
+				.device_id = device_id < 0 ? 0 : device_id,
 				.raw = raw,
 				.inlined = true,
 			};
@@ -2575,7 +2586,7 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 				.length = map.m_llen,
 				.plen = map.m_plen,
 				.pstart = mdev.m_pa,
-				.device_id = inode->dev,	/* FIXME! */
+				.device_id = sbi->dev + mdev.m_deviceid - params->force_omit_fsmeta,
 				.partial = map.m_flags & EROFS_MAP_PARTIAL_REF,
 				.raw = raw,
 				.inlined = false,
lib/liberofs_rebuild.h +1 -0
index 2cf228e..38b0b82 100644
@@ -14,6 +14,7 @@ struct erofs_rebuild_params {
 	struct erofs_inode *mergeroot;
 	struct erofs_sb_info *sbi;
 	enum erofs_rebuild_datamode datamode;
+	bool force_omit_fsmeta;	/* reject data references to the primary device */
 };
 
 struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
lib/rebuild.c +44 -14

112 lines not on this page · raw message

man/mkfs.erofs.1 +5 -0
index 65ec807..a66025c 100644
@@ -349,6 +349,11 @@ time.
 .BI "\-\-mount-point=" path
 Specify the prefix of target filesystem path (default: /).
 .TP
+.B \-\-omit-fsmeta
+Omit source fsmeta devices, e.g. for TarFS fsmerge with only tar blobs.
+Fail if data refers to an omitted device.
+Sources without external devices keep their primary device.
+.TP
 .BI "\-\-oci\fR[\fP=<f|i>\fR]\fP"
 Generate a full (f) or index-only (i) image from OCI remote source.
 Additional options can be specified:
mkfs/main.c +71 -46

172 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
author
#13 lore raw

PATCH v4 10/10 erofs-utils: mkfs: support file deltas in manifests

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

Add DIFF[format] manifest records to apply file updates after merging source images. Initially support DM_SNAP for sealed dm-persistent snapshot COW stores, with COW paths relative to the manifest data directory.

For example, add the following record to foo.m:

DIFF[DM_SNAP] /disk disk_cow

Then apply it to the merged source tree:

  mkfs.erofs --manifest=foo.m l2.erofs l0.erofs l1.erofs datadir/

Note that delta updates currently require a chunk-based inode layout:

- Reuse unchanged chunk indexes and split inherited chunks as needed to match replacement boundaries;

- Write changed data to the primary device and turn all-zero replacement chunks into holes;

- Preserve file sizes and hard links;

- Apply records in manifest order, with later records overriding overlapping changes to the same inode.

Other on-disk layouts like PLAIN, INLINE, and, most importantly, COMPRESSED will be implemented as follow-ups. Preparing targets from local files, full-data or reserved-space imports, and refreshing inode-digest xattrs remain TODOs.

Assisted-by Codex:gpt-6.1-sol
---
 include/erofs/xattr.h |   1 +
 lib/blobchunk.c       | 130 ++++++++++++++++++++++++++++++++++++++++-
 lib/inode.c           |   4 +-
 lib/liberofs_chunk.h  |   3 +
 lib/manifest.c        | 131 +++++++++++++++++++++++++++++++-----------
 lib/rebuild.c         |  12 ++--
 lib/xattr.c           |  17 ++++++
 mkfs/main.c           | 119 +++++++++++++++++++++++---------------
 8 files changed, 330 insertions(+), 87 deletions(-)
include/erofs/xattr.h +1 -0
index c1cfd01..d46d9cc 100644
@@ -34,6 +34,7 @@ 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);
+void erofs_inode_clear_digest(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,
lib/blobchunk.c +128 -2

148 lines not on this page · raw message

lib/inode.c +3 -1
index ef39a4e..10fc336 100644
@@ -1021,8 +1021,10 @@ static int erofs_prepare_inode_buffer(struct erofs_importer *im,
 		goto noinline;
 
 	/* TODO: tailpacking inline of chunk-based format isn't finalized */
-	if (inode->datalayout == EROFS_INODE_CHUNK_BASED)
+	if (inode->datalayout == EROFS_INODE_CHUNK_BASED) {
+		erofs_sb_set_chunked_file(sbi);
 		goto noinline;
+	}
 
 	if (!is_inode_layout_compression(inode)) {
 		if (params->no_datainline && S_ISREG(inode->i_mode)) {
lib/liberofs_chunk.h +3 -0
index 4ef6640..bf73bc6 100644
@@ -17,6 +17,9 @@ extern "C"
 struct erofs_chunkitem *erofs_get_unhashed_chunk(struct erofs_sb_info *sbi,
 		unsigned int device_id, erofs_blk_t blkaddr,
 		erofs_off_t sourceoffset);
+struct erofs_file_delta;
+int erofs_chunk_apply_deltas(struct erofs_inode *inode,
+			     struct erofs_file_delta *fdl);
 void erofs_inode_fixup_chunkformat(struct erofs_inode *inode);
 s64 erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
 				 erofs_off_t off);
lib/manifest.c +98 -33

191 lines not on this page · raw message

lib/rebuild.c +8 -4
index 6161c12..5249968 100644
@@ -673,7 +673,8 @@ int erofs_rebuild_load_tree(struct erofs_rebuild_params *params)
 		    !erofs_sb_has_big_pcluster(dst_sbi)) {
 			erofs_err("failed to load tree from image %d with big pclusters",
 				  sbi->dev);
-			return -EOPNOTSUPP;
+			ret = -EOPNOTSUPP;
+			goto out;
 		}
 		if (erofs_sb_has_48bit(sbi))
 			erofs_sb_set_48bit(dst_sbi);
@@ -684,11 +685,13 @@ int erofs_rebuild_load_tree(struct erofs_rebuild_params *params)
 	ret = erofs_read_inode_from_disk(&inode);
 	if (ret) {
 		erofs_err("failed to read root inode of %s", fsid);
-		return ret;
+		goto out;
 	}
 	inode.i_srcpath = strdup("/");
-	if (!inode.i_srcpath)
-		return -ENOMEM;
+	if (!inode.i_srcpath) {
+		ret = -ENOMEM;
+		goto out;
+	}
 
 	ctx = (struct erofs_rebuild_dir_context) {
 		.ctx.dir = &inode,
@@ -697,6 +700,7 @@ int erofs_rebuild_load_tree(struct erofs_rebuild_params *params)
 		.params = params,
 	};
 	ret = erofs_iterate_dir(&ctx.ctx, false);
+out:
 	free(inode.i_srcpath);
 	return ret;
 }
lib/xattr.c +17 -0
index 0647b3b..95158e5 100644
@@ -597,6 +597,23 @@ static void erofs_removexattr(struct erofs_inode *inode, const char *key)
 	}
 }
 
+void erofs_inode_clear_digest(struct erofs_inode *inode)
+{
+	u8 prefix = inode->sbi->ishare_xattr_prefix_id;
+	struct erofs_inode_xattr_node *node, *n;
+
+	if (!prefix)
+		return;
+	list_for_each_entry_safe(node, n, &inode->i_xattrs, list) {
+		if (node->item->prefix != prefix ||
+		    node->item->len[0] != node->item->prefix_len)
+			continue;
+		list_del(&node->list);
+		put_xattritem(node->item);
+		free(node);
+	}
+}
+
 static const char * const erofs_ovl_xattr_names[] = {
 	[EROFS_OVL_XATTR_OPAQUE] = OVL_XATTR_OPAQUE,
 	[EROFS_OVL_XATTR_ORIGIN] = OVL_XATTR_ORIGIN,
mkfs/main.c +72 -47

244 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
#14 lore raw

Re: [PATCH v4 10/10] erofs-utils: mkfs: support file deltas in manifests

Quoted 37 lines · Gao Xiang

static int erofs_manifest_parse_record(struct erofs_importer *im,

lib/manifest.c +17 -14
-				 struct erofs_inode *root, char *line)
+		struct erofs_inode *root,
+		char *line)
  {
-	char *fields[2], *token, *path;
-	int i, ret;
+	char *fields[3], *token, *imagepath, *srcpath;
+	unsigned int nr = 0;
+	bool dm_snap = false;
+	int ret;
 
-	for (i = 0; i < ARRAY_SIZE(fields); ++i) {
-		do {
-			token = strsep(&line, " ");
-		} while (token && !*token);
-		if (!token)
+	while ((token = strsep(&line, " "))) {
+		if (!*token)
+			continue;
+		if (nr == ARRAY_SIZE(fields))
  			return -EINVAL;
-		fields[i] = token;
+		fields[nr++] = token;
  	}
-	while ((token = strsep(&line, " ")))
-		if (*token)
-			return -EINVAL;
+	if (!nr)
+		return -EINVAL;
+	if (!strncmp(fields[0], "DIFF[", 5)) {
+		size_t len = strlen(fields[0]);
 
-	if (erofs_manifest_decode(fields[0]) || *fields[0] != '/' ||
-	    erofs_manifest_decode(fields[1]))
+		if (len <= 6 || fields[0][len - 1] != ']' || nr != 3)
+			return -EINVAL;

lib/manifest.c out_dentry: · line 281

Quoted 10 lines
lib/manifest.c +8 -1
+		fields[0][len - 1] = '\0';
+		if (strcmp(fields[0] + 5, "DM_SNAP"))
+			return -EOPNOTSUPP;
+		dm_snap = true;
+	}
+	if (nr != (dm_snap ? 3 : 2))
  		return -EINVAL;
-	if (asprintf(&path, "%s/%s", im->params->source, fields[1]) < 0)
+	imagepath = fields[dm_snap ? 1 : 0];
+	token = fields[dm_snap ? 2 : 1];

Can we get a more meaningful name here instead of "token"?

Quoted 19 lines
lib/manifest.c +8 -3
+	if (erofs_manifest_decode(imagepath) || *imagepath != '/' ||
+	    erofs_manifest_decode(token))
+		return -EINVAL;
+	if (asprintf(&srcpath, "%s/%s", im->params->source, token) < 0)
  		return -ENOMEM;
-	ret = erofs_manifest_load_local(im, root, fields[0], path);
-	free(path);
+	ret = erofs_manifest_load_local(im, root,
+				       imagepath, srcpath, dm_snap);
+	free(srcpath);
  	return ret;
  }
 
-/* The importer has not taken ownership of the tree on a parse error. */
+/* The importer has not taken ownership of a new tree on a load error. */
  static void erofs_manifest_drop_children(struct erofs_inode *dir)
  {
  	struct erofs_dentry *d, *next;

lib/manifest.c static void erofs_manifest_drop_children(struct erofs_inode *dir) · line 312

Quoted 6 lines
lib/manifest.c +2 -1
@@ -253,18 +308,26 @@ static void erofs_manifest_drop_children(struct erofs_inode *dir)
 
  int erofs_manifest_load(struct erofs_importer *im, FILE *fp)
  {
-	struct erofs_inode *root;
+	struct erofs_inode *root = im->root;
+	bool newroot = !root;

It seems that im->root is always non-NULL.

mkfs/main.c static int mkfs_parse_sources(int argc, char *argv[], int optind) · line 986

Quoted 12 lines
mkfs/main.c +6 -1
@@ -976,10 +977,14 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
  {
  	erofs_os_stat_t st;
  	int err, fd;
-	char *s;
+	char *s, *datadir = NULL;
 
+	if (manifest_file) {
+		datadir = argv[--argc];
+		if (optind == argc)
+			goto out_datadir;
+	}

Can we simplify the logic a bit here? somethig like:

```

-static int mkfs_parse_sources(int argc, char *argv[], int optind)
+static int mkfs_parse_rebuild_sources(int argc, char *argv[])
+{
+    for (i = 0; i < argc; ++i) {
+
+        err = erofs_dev_open(src, argv[i], O_RDONLY);

           ...

+}
+
+static int mkfs_parse_manifest_sources(int argc, char *argv[])
+{
+    const char *datadir = argv[argc - 1];
+    erofs_os_stat_t st;
+
+    if (erofs_os_stat(datadir, &st))
+        return -errno;
+    if (!S_ISDIR(st.st_mode))
+        return -ENOTDIR;
+    cfg.c_src_path = erofs_os_realpath(datadir, NULL);
+    if (!cfg.c_src_path)
+        return -errno;
+    if (argc == 1)
+        return 0;
+    return mkfs_parse_rebuild_sources(argc - 1, argv);
+}
+
+static int mkfs_parse_sources(int argc, char *argv[])
 {
+
+    if (manifest_file)
+        return mkfs_parse_manifest_sources(argc, argv);

-    if (manifest_file) {
-        datadir = argv[--argc];
-        if (optind == argc)
-            goto out_datadir;
-    }
     switch (source_mode) {
     case EROFS_MKFS_SOURCE_LOCALDIR:
-        err = erofs_os_lstat((s = argv[optind++]), &st);
-        if (err) {
+        if (erofs_os_lstat(s, &st)) {
             erofs_err("failed to stat %s: %s", s,
                   erofs_strerror(-errno));
             return -ENOENT;
         }
-        if (S_ISDIR(st.st_mode)) {
-            if (datadir)
-                return -EINVAL;
-            cfg.c_src_path = erofs_os_realpath(s, NULL);
-            if (!cfg.c_src_path) {
-                erofs_err("failed to parse source directory: %s",
-                      erofs_strerror(-errno));
-                return -ENOENT;
-            }
-            erofs_set_fs_root(cfg.c_src_path);
-        } else {
-            cfg.c_src_path = strdup(s);
-            if (!cfg.c_src_path)
-                return -ENOMEM;
-            source_mode = EROFS_MKFS_SOURCE_REBUILD;
+        if (!S_ISDIR(st.st_mode))
+            return mkfs_parse_rebuild_sources(argc, argv);
+
+        cfg.c_src_path = erofs_os_realpath(s, NULL);
+        if (!cfg.c_src_path) {
+            erofs_err("failed to parse source directory: %s",
+                  erofs_strerror(-errno));
+            return -ENOENT;
         }
+        erofs_set_fs_root(cfg.c_src_path);
         break;
...

-    if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
-        char *srcpath = cfg.c_src_path;
-        struct erofs_sb_info *src;
-
-        do {
-            src = calloc(1, sizeof(struct erofs_sb_info));
-            if (!src) {
-                erofs_rebuild_cleanup();
-                return -ENOMEM;
-            }
-
-            err = erofs_dev_open(src, srcpath, O_RDONLY);
-            if (err) {
-                free(src);
-                erofs_rebuild_cleanup();
-                return err;
-            }
-
-            /* extra device index starts from 1 */
-            src->dev = ++rebuild_src_count;
-            list_add(&src->list, &rebuild_src_list);
-        } while (optind < argc && (srcpath = argv[optind++]));
-    } else if (optind < argc) {
-        erofs_err("unexpected argument: %s\n", argv[optind]);
+    if (argc > 1) {
+        erofs_err("unexpected argument: %s\n", argv[1]);
         return -EINVAL;
     }
-out_datadir:
-    if (datadir) {
-        if (erofs_os_stat(datadir, &st))
-            return -errno;
-        if (!S_ISDIR(st.st_mode))
-            return -ENOTDIR;
-        s = erofs_os_realpath(datadir, NULL);
-        if (!s)
-            return -errno;
-        free(cfg.c_src_path);
-        cfg.c_src_path = s;
-    }
     return 0;
 }

```

Thansk,

Wenyuan

#15 lore raw

Re: [PATCH v4 10/10] erofs-utils: mkfs: support file deltas in manifests

Quoted 41 lines · wy.xu
Quoted 39 lines · Gao Xiang

static int erofs_manifest_parse_record(struct erofs_importer *im,

lib/manifest.c +21 -14
-				 struct erofs_inode *root, char *line)
+		struct erofs_inode *root,
+		char *line)
  {
-	char *fields[2], *token, *path;
-	int i, ret;
+	char *fields[3], *token, *imagepath, *srcpath;
+	unsigned int nr = 0;
+	bool dm_snap = false;
+	int ret;
-	for (i = 0; i < ARRAY_SIZE(fields); ++i) {
-		do {
-			token = strsep(&line, " ");
-		} while (token && !*token);
-		if (!token)
+	while ((token = strsep(&line, " "))) {
+		if (!*token)
+			continue;
+		if (nr == ARRAY_SIZE(fields))
  			return -EINVAL;
-		fields[i] = token;
+		fields[nr++] = token;
  	}
-	while ((token = strsep(&line, " ")))
-		if (*token)
-			return -EINVAL;
+	if (!nr)
+		return -EINVAL;
+	if (!strncmp(fields[0], "DIFF[", 5)) {
+		size_t len = strlen(fields[0]);
-	if (erofs_manifest_decode(fields[0]) || *fields[0] != '/' ||
-	    erofs_manifest_decode(fields[1]))
+		if (len <= 6 || fields[0][len - 1] != ']' || nr != 3)
+			return -EINVAL;
+		fields[0][len - 1] = '\0';
+		if (strcmp(fields[0] + 5, "DM_SNAP"))
+			return -EOPNOTSUPP;
+		dm_snap = true;
Quoted 8 lines

lib/manifest.c out_dentry: · line 281

Quoted 6 lines
lib/manifest.c +4 -1
+	}
+	if (nr != (dm_snap ? 3 : 2))
  		return -EINVAL;
-	if (asprintf(&path, "%s/%s", im->params->source, fields[1]) < 0)
+	imagepath = fields[dm_snap ? 1 : 0];
+	token = fields[dm_snap ? 2 : 1];

Can we get a more meaningful name here instead of "token"?

Fixed.

Quoted 19 lines
Quoted 19 lines
lib/manifest.c +8 -3
+	if (erofs_manifest_decode(imagepath) || *imagepath != '/' ||
+	    erofs_manifest_decode(token))
+		return -EINVAL;
+	if (asprintf(&srcpath, "%s/%s", im->params->source, token) < 0)
  		return -ENOMEM;
-	ret = erofs_manifest_load_local(im, root, fields[0], path);
-	free(path);
+	ret = erofs_manifest_load_local(im, root,
+				       imagepath, srcpath, dm_snap);
+	free(srcpath);
  	return ret;
  }
-/* The importer has not taken ownership of the tree on a parse error. */
+/* The importer has not taken ownership of a new tree on a load error. */
  static void erofs_manifest_drop_children(struct erofs_inode *dir)
  {
  	struct erofs_dentry *d, *next;
Quoted 8 lines

lib/manifest.c static void erofs_manifest_drop_children(struct erofs_inode *dir) · line 311

Quoted 6 lines
lib/manifest.c +2 -1
@@ -253,18 +308,26 @@ static void erofs_manifest_drop_children(struct erofs_inode *dir)
  int erofs_manifest_load(struct erofs_importer *im, FILE *fp)
  {
-	struct erofs_inode *root;
+	struct erofs_inode *root = im->root;
+	bool newroot = !root;

It seems that im->root is always non-NULL.

Fixed.

Quoted 16 lines

mkfs/main.c static int mkfs_parse_sources(int argc, char *argv[], int optind) · line 985

Quoted 13 lines
mkfs/main.c +6 -1
@@ -976,10 +977,14 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
  {
  	erofs_os_stat_t st;
  	int err, fd;
-	char *s;
+	char *s, *datadir = NULL;
+	if (manifest_file) {
+		datadir = argv[--argc];
+		if (optind == argc)
+			goto out_datadir;
+	}

Can we simplify the logic a bit here? somethig like:

Could you just submit a follow-up patch to improve this? That would be helpful to me.

Thanks,
Gao Xiang

#16 lore raw

Re: [PATCH v4 10/10] erofs-utils: mkfs: support file deltas in manifests

Quoted 91 lines · Gao Xiang
Quoted 39 lines · wy.xu
Quoted 38 lines · Gao Xiang

static int erofs_manifest_parse_record(struct erofs_importer *im,

lib/manifest.c +20 -14
-				 struct erofs_inode *root, char *line)
+		struct erofs_inode *root,
+		char *line)
   {
-	char *fields[2], *token, *path;
-	int i, ret;
+	char *fields[3], *token, *imagepath, *srcpath;
+	unsigned int nr = 0;
+	bool dm_snap = false;
+	int ret;
-	for (i = 0; i < ARRAY_SIZE(fields); ++i) {
-		do {
-			token = strsep(&line, " ");
-		} while (token && !*token);
-		if (!token)
+	while ((token = strsep(&line, " "))) {
+		if (!*token)
+			continue;
+		if (nr == ARRAY_SIZE(fields))
   			return -EINVAL;
-		fields[i] = token;
+		fields[nr++] = token;
   	}
-	while ((token = strsep(&line, " ")))
-		if (*token)
-			return -EINVAL;
+	if (!nr)
+		return -EINVAL;
+	if (!strncmp(fields[0], "DIFF[", 5)) {
+		size_t len = strlen(fields[0]);
-	if (erofs_manifest_decode(fields[0]) || *fields[0] != '/' ||
-	    erofs_manifest_decode(fields[1]))
+		if (len <= 6 || fields[0][len - 1] != ']' || nr != 3)
+			return -EINVAL;
+		fields[0][len - 1] = '\0';
+		if (strcmp(fields[0] + 5, "DM_SNAP"))
+			return -EOPNOTSUPP;
Quoted 8 lines

lib/manifest.c out_dentry: · line 281

Quoted 7 lines
lib/manifest.c +5 -1
+		dm_snap = true;
+	}
+	if (nr != (dm_snap ? 3 : 2))
   		return -EINVAL;
-	if (asprintf(&path, "%s/%s", im->params->source, fields[1]) < 0)
+	imagepath = fields[dm_snap ? 1 : 0];
+	token = fields[dm_snap ? 2 : 1];

Can we get a more meaningful name here instead of "token"?

Fixed.

Quoted 17 lines
Quoted 17 lines
lib/manifest.c +8 -3
+	if (erofs_manifest_decode(imagepath) || *imagepath != '/' ||
+	    erofs_manifest_decode(token))
+		return -EINVAL;
+	if (asprintf(&srcpath, "%s/%s", im->params->source, token) < 0)
   		return -ENOMEM;
-	ret = erofs_manifest_load_local(im, root, fields[0], path);
-	free(path);
+	ret = erofs_manifest_load_local(im, root,
+				       imagepath, srcpath, dm_snap);
+	free(srcpath);
   	return ret;
   }
-/* The importer has not taken ownership of the tree on a parse error. */
+/* The importer has not taken ownership of a new tree on a load error. */
   static void erofs_manifest_drop_children(struct erofs_inode *dir)
   {
Quoted 8 lines

lib/manifest.c static void erofs_manifest_drop_children(struct erofs_inode *dir) · line 311

Quoted 7 lines
   	struct erofs_dentry *d, *next;
lib/manifest.c +2 -1
@@ -253,18 +308,26 @@ static void erofs_manifest_drop_children(struct erofs_inode *dir)
   int erofs_manifest_load(struct erofs_importer *im, FILE *fp)
   {
-	struct erofs_inode *root;
+	struct erofs_inode *root = im->root;
+	bool newroot = !root;

It seems that im->root is always non-NULL.

Fixed.

Quoted 14 lines

mkfs/main.c static int mkfs_parse_sources(int argc, char *argv[], int optind) · line 985

Quoted 12 lines
mkfs/main.c +6 -1
@@ -976,10 +977,14 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
   {
   	erofs_os_stat_t st;
   	int err, fd;
-	char *s;
+	char *s, *datadir = NULL;
+	if (manifest_file) {
+		datadir = argv[--argc];
+		if (optind == argc)
+			goto out_datadir;
+	}

Can we simplify the logic a bit here? somethig like:

Quoted 3 lines

Could you just submit a follow-up patch to improve this? That would be helpful to me.

Okay I will do it later.

Thanks,

Wenyuan