Skip to contentSkip to conversation

erofs-utils: lib: fix short write retries

PATCH New Yifan Zhao 1 participant 4 messages Sep 21 Library code

lore
Share & tools
lore

Yifan Zhao

author
#0 lore raw

PATCH v2 1/4 erofs-utils: lib: fix short write retries

Retry with only the remaining byte count in both the pwrite() and pwrite64() paths.

Assisted-by Codex:gpt-6-astra
---
 lib/io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/io.c +2 -2
index 5c5449f..f62129f 100644
@@ -75,9 +75,9 @@ ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf,
 	pos += vf->offset;
 	do {
 #ifdef HAVE_PWRITE64
-		ret = pwrite64(vf->fd, buf, len, (off64_t)pos);
+		ret = pwrite64(vf->fd, buf, len - written, (off64_t)pos);
 #else
-		ret = pwrite(vf->fd, buf, len, (off_t)pos);
+		ret = pwrite(vf->fd, buf, len - written, (off_t)pos);
 #endif
 		if (ret <= 0) {
 			if (!ret)
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v2 2/4 erofs-utils: lib: fix large chunk index writes

Use ssize_t for chunk index write results in erofs_write_chunk_indexes() and erofs_iflush(). Widen index buffer offsets and inode metadata size calculations to avoid truncation with large index tables.

Assisted-by Codex:gpt-6-astra
---
 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 8f43dd0..2f42541 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)
+ssize_t 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;
+	size_t dst, src;
 	bool _48bit;
 
 	if (inode->u.chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
lib/inode.c +9 -10
index ec96556..0cf09b8 100644
@@ -816,7 +816,8 @@ int erofs_iflush(struct erofs_inode *inode)
 	struct iovec iov[2];
 	char *xattrs = NULL;
 	bool nlink_1 = true;
-	int ret, fmt;
+	ssize_t ret;
+	int fmt;
 
 	DBG_BUGON(inode->nid == EROFS_NID_UNALLOCATED);
 	DBG_BUGON(bh && erofs_btell(bh, false) != off);
@@ -998,11 +999,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 erofs_off_t erofs_inode_meta_size(struct erofs_inode *inode)
 {
-	unsigned int inodesize;
+	erofs_off_t inodesize;
 
-	inodesize = inode->inode_isize + inode->xattr_isize;
+	inodesize = inode->inode_isize + (erofs_off_t)inode->xattr_isize;
 	if (inode->extent_isize)
 		inodesize = roundup(inodesize, 8) + inode->extent_isize;
 	return inodesize;
@@ -1014,7 +1015,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;
+	erofs_off_t inodesize;
 	struct erofs_buffer_head *bh, *ibh;
 
 	DBG_BUGON(inode->bh || inode->bh_inline);
@@ -2456,7 +2457,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;
 
@@ -2484,10 +2486,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..64e7ec8 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);
+ssize_t 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.55.0
author
#2 lore raw

PATCH v2 3/4 erofs-utils: mkfs: add file-delta framework

In reply to Yifan Zhao

Add --file-delta to rebuild a parent image with logical file updates. Represent updates as DATA or ZERO extents, separating input parsing from file layout handling. Keep delta state in the rebuild context.

Implement Chunk updates with independent parent, update and output granularities. Split inherited large chunks without copying their data and retain historical device references. Store changed nonzero data in the output primary device and zero updates as holes.

Match targets by parent inode, including hard links. Reject unsupported 48-bit layouts and output block ranges.

For example, using the dm-persistent backend from the next patch:

  mkfs.erofs -Eforce-chunk-indexes --chunksize=4096 \
    parent.erofs rootdir/
  mkfs.erofs -Eforce-chunk-indexes --chunksize=4096 \
    --file-delta=dm-persistent:/disk.img:changes.cow \
    updated.erofs parent.erofs

Here rootdir/disk.img is the base file and changes.cow is a sealed COW store with 4 KiB chunks. The output inherits unchanged data from parent.erofs.

Assisted-by Codex:gpt-6-astra
---
 include/erofs/io.h        |   1 +
 lib/Makefile.am           |   4 +-
 lib/blobchunk.c           |   5 +-
 lib/file_delta.c          | 324 ++++++++++++++++++++++++++++++++++++++
 lib/io.c                  |   2 +-
 lib/liberofs_chunk.h      |   2 +
 lib/liberofs_file_delta.h |  51 ++++++
 lib/liberofs_rebuild.h    |   9 +-
 lib/rebuild.c             | 183 ++++++++++++++++++---
 man/mkfs.erofs.1          |  19 +++
 mkfs/main.c               | 126 +++++++++++++--
 11 files changed, 692 insertions(+), 34 deletions(-)
 create mode 100644 lib/file_delta.c
 create mode 100644 lib/liberofs_file_delta.h
include/erofs/io.h +1 -0
index 96309fd..52713dc 100644
@@ -70,6 +70,7 @@ struct erofs_vfile {
 
 ssize_t __erofs_io_write(int fd, const void *buf, size_t len);
 int __erofs_0write(int fd, size_t len);
+int erofs_get_bdev_size(int fd, u64 *bytes);
 
 int erofs_io_fstat(struct erofs_vfile *vf, struct stat *buf);
 ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf, u64 pos, size_t len);
lib/Makefile.am +3 -1
index 0a82915..7f86409 100644
@@ -24,6 +24,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 \
@@ -44,7 +45,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
+		      vmdk.c metabox.c global.c importer.c base64.c \
+		      file_delta.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 liberofs_la_LIBADD = $(LIBEROFS_LIBS)
lib/blobchunk.c +2 -3
index 2f42541..fae12f3 100644
@@ -80,9 +80,8 @@ static unsigned int memhash(const void *buf, size_t len)
 	return hash;
 }
 
-static struct erofs_chunkitem *erofs_get_chunk(struct erofs_sb_info *sbi,
-					       int device_id,
-					       u8 *buf, u64 size)
+struct erofs_chunkitem *erofs_get_chunk(struct erofs_sb_info *sbi,
+				      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;
lib/file_delta.c new file +324 -0

324 lines not on this page · raw message

lib/io.c +1 -1
index f62129f..115dabb 100644
@@ -250,7 +250,7 @@ ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, size_t len, u64 pos)
 	return read;
 }
 
-static int erofs_get_bdev_size(int fd, u64 *bytes)
+int erofs_get_bdev_size(int fd, u64 *bytes)
 {
 	errno = ENOTSUP;
 #ifdef BLKGETSIZE64
lib/liberofs_chunk.h +2 -0
index 64e7ec8..78f909e 100644
@@ -17,6 +17,8 @@ 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_chunkitem *erofs_get_chunk(struct erofs_sb_info *sbi,
+				      int device_id, u8 *buf, u64 size);
 void erofs_inode_fixup_chunkformat(struct erofs_inode *inode);
 ssize_t erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
 				 erofs_off_t off);
lib/liberofs_file_delta.h new file +51 -0

51 lines not on this page · raw message

lib/liberofs_rebuild.h +8 -1
index 6459dbd..88117e0 100644
@@ -10,11 +10,18 @@ enum erofs_rebuild_datamode {
 	EROFS_REBUILD_DATA_FULL,
 };
 
+struct erofs_rebuild_options {
+	enum erofs_rebuild_datamode datamode;
+	/* Optional list of erofs_delta_spec, borrowed until return. */
+	const struct list_head *delta_specs;
+	erofs_off_t expected_delta_unit;
+};
+
 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);
+			    const struct erofs_rebuild_options *opts);
 
 int erofs_rebuild_load_basedir(struct erofs_inode *dir, u64 *nr_subdirs,
 			       unsigned int *i_nlink);
lib/rebuild.c +164 -19

268 lines not on this page · raw message

man/mkfs.erofs.1 +19 -0
index 65ec807..b599232 100644
@@ -339,6 +339,25 @@ Note that \fBrvsp\fR takes precedence over \fB--tar=i\fR or \fB--tar=headerball\
 Only \fBrvsp\fR is supported.
 .RE
 .TP
+.BI "\-\-file-delta=" FORMAT:TARGET:SOURCE
+Apply a \fIFORMAT\fR delta \fISOURCE\fR to \fITARGET\fR while rebuilding
+a single parent image.  Requires \fB\-\-chunksize\fR,
+\fB\-Eforce-chunk-indexes\fR and the default rebuild data mode.
+
+\fITARGET\fR is relative to the parent image root and must name a non-empty
+regular file.  All non-empty regular files in the parent must use explicit
+chunk indexes.  File sizes are preserved.  This option may be repeated for
+distinct target inodes; hard links are updated together.  TARGET cannot contain
+a colon.
+
+Cannot be combined with \fB\-\-incremental\fR, \fB\-\-blobdev\fR or
+\fB\-\-xattr-inode-digest\fR.  48-bit features and inode-digest xattrs are
+unsupported.
+
+Changed data is stored in the output image.  The output requires the parent as
+external device 1, followed by the parent's existing external devices in order.
+\fISOURCE\fR is needed only during the build.
+.TP
 .BI "\-\-max-extent-bytes=" #
 Specify maximum decompressed extent size in bytes. The default is unlimited.
 .TP
mkfs/main.c +117 -9

230 lines not on this page · raw message

Signature · 1 line
2.55.0
author
#3 lore raw

PATCH v2 4/4 erofs-utils: mkfs: support dm-persistent file deltas

In reply to Yifan Zhao

Parse and validate version 1 dm-persistent snapshot exception stores, then convert exceptions into sorted DATA extents. Inherit ranges without exceptions and clip the last update to the target file size.

Require the source chunk size to match --chunksize, independently of the parent chunk geometry.

Assisted-by Codex:gpt-6-astra
---
 lib/Makefile.am              |   3 +-
 lib/deltas/dm_persistent.c   | 199 +++++++++++++++++++++++++++++++++++
 lib/liberofs_dm_persistent.h |   9 ++
 man/mkfs.erofs.1             |   8 ++
 mkfs/main.c                  |   2 +
 5 files changed, 220 insertions(+), 1 deletion(-)
 create mode 100644 lib/deltas/dm_persistent.c
 create mode 100644 lib/liberofs_dm_persistent.h
lib/Makefile.am +2 -1
index 7f86409..7d7411e 100644
@@ -24,6 +24,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_dm_persistent.h \
       $(top_srcdir)/lib/liberofs_file_delta.h \
       $(top_srcdir)/lib/liberofs_fragments.h \
       $(top_srcdir)/lib/liberofs_private.h \
@@ -46,7 +47,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.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 \
-		      file_delta.c
+		      file_delta.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 +199 -0

199 lines not on this page · raw message

lib/liberofs_dm_persistent.h new file +9 -0
new file mode 100644
index 0000000..190468b
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR MIT */
+#ifndef __EROFS_LIB_LIBEROFS_DM_PERSISTENT_H
+#define __EROFS_LIB_LIBEROFS_DM_PERSISTENT_H
+
+#include "liberofs_file_delta.h"
+
+extern const struct erofs_delta_spec_ops erofs_file_delta_dm_persistent_ops;
+
+#endif
man/mkfs.erofs.1 +8 -0
index b599232..217abfa 100644
@@ -357,6 +357,14 @@ unsupported.
 Changed data is stored in the output image.  The output requires the parent as
 external device 1, followed by the parent's existing external devices in order.
 \fISOURCE\fR is needed only during the build.
+.RS
+.TP
+.B dm-persistent
+\fISOURCE\fR must be a valid Linux dm-snapshot persistent v1 exception store,
+provided as a regular file or block device and unchanged throughout the build.
+Its chunk size must match \fB\-\-chunksize\fR.  Ranges without exceptions
+inherit the parent contents.
+.RE
 .TP
 .BI "\-\-max-extent-bytes=" #
 Specify maximum decompressed extent size in bytes. The default is unlimited.
mkfs/main.c +2 -0
index 199762a..1328492 100644
@@ -26,6 +26,7 @@
 #include "erofs/compress_hints.h"
 #include "../lib/compressor.h"
 #include "../lib/liberofs_chunk.h"
+#include "../lib/liberofs_dm_persistent.h"
 #include "../lib/liberofs_file_delta.h"
 #include "../lib/liberofs_gzran.h"
 #include "../lib/liberofs_metabox.h"
@@ -333,6 +334,7 @@ static unsigned int rebuild_src_count;
 static LIST_HEAD(rebuild_src_list);
 static LIST_HEAD(file_delta_specs);
 static const struct erofs_delta_spec_ops * const file_delta_backends[] = {
+	&erofs_file_delta_dm_persistent_ops,
 	NULL,
 };
 static u8 fixeduuid[16];
Signature · 1 line
2.55.0