Skip to contentSkip to conversation

erofs-utils: lib: fix a misuse of `params->all_fragments`

PATCH New Gao Xiang 1 participant 7 messages Oct 16, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/7 erofs-utils: lib: fix a misuse of `params->all_fragments`

Should use all_fragments to skip special inodes.

Fixes 7c153f1875dc ("erofs-utils: lib: migrate advance compression configurations")
---
 lib/compress.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/compress.c +1 -1
index b7ca3ad..7f34b66 100644
@@ -1873,7 +1873,7 @@ void *erofs_begin_compressed_file(struct erofs_importer *im,
 	ictx->fragemitted = false;
 	ictx->dedupe = false;
 
-	if (params->all_fragments && !inode->fragment_size) {
+	if (all_fragments && !inode->fragment_size) {
 		ret = erofs_pack_file_from_fd(inode, fd, ictx->tofh);
 		if (ret)
 			goto err_free_idata;
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/7 erofs-utils: mkfs: separate pnid fixup for incremental builds from `validnid`

This ensures they are not buried in the validnid dump.

---
 include/erofs/internal.h |  8 +++++---
 lib/inode.c              | 13 +++++++------
 lib/rebuild.c            |  6 ++++--
 3 files changed, 16 insertions(+), 11 deletions(-)
include/erofs/internal.h +5 -3
index e8d8667..4de6563 100644
@@ -338,15 +338,17 @@ static inline struct erofs_inode *erofs_parent_inode(struct erofs_inode *inode)
 #define IS_ROOT(x)	((x) == erofs_parent_inode(x))
 
 #define EROFS_DENTRY_NAME_ALIGNMENT	4
+
+#define EROFS_DENTRY_FLAG_VALIDNID	0x01
+#define EROFS_DENTRY_FLAG_FIXUP_PNID	0x02
 struct erofs_dentry {
 	struct list_head d_child;	/* child of parent list */
 	union {
 		struct erofs_inode *inode;
 		erofs_nid_t nid;
 	};
-	u8 namelen;
-	u8 type;
-	bool validnid;
+	u8 namelen, type;
+	u8 flags;
 	char name[];
 };
 
lib/inode.c +7 -6
index 4a834b4..d16468d 100644
@@ -187,7 +187,7 @@ struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
 	d->inode = NULL;
 	d->namelen = namelen;
 	d->type = EROFS_FT_UNKNOWN;
-	d->validnid = false;
+	d->flags = 0;
 	list_add_tail(&d->d_child, &parent->i_subdirs);
 	return d;
 }
@@ -425,10 +425,10 @@ static void erofs_d_invalidate(struct erofs_dentry *d)
 {
 	struct erofs_inode *const inode = d->inode;
 
-	if (d->validnid)
+	if (d->flags & EROFS_DENTRY_FLAG_VALIDNID)
 		return;
 	d->nid = erofs_lookupnid(inode);
-	d->validnid = true;
+	d->flags |= EROFS_DENTRY_FLAG_VALIDNID;
 	erofs_iput(inode);
 }
 
@@ -536,7 +536,7 @@ static int erofs_write_dir_file(struct erofs_inode *dir)
 		unsigned int len = d->namelen + sizeof(struct erofs_dirent);
 
 		/* XXX: a bit hacky, but to avoid another traversal */
-		if (d->validnid && d->type == EROFS_FT_DIR) {
+		if (d->flags & EROFS_DENTRY_FLAG_FIXUP_PNID) {
 			ret = erofs_rebuild_inode_fix_pnid(dir, d->nid);
 			if (ret)
 				return ret;
@@ -1690,7 +1690,7 @@ err_closedir:
 
 bool erofs_dentry_is_wht(struct erofs_sb_info *sbi, struct erofs_dentry *d)
 {
-	if (!d->validnid)
+	if (!(d->flags & EROFS_DENTRY_FLAG_VALIDNID))
 		return erofs_inode_is_whiteout(d->inode);
 	if (d->type == EROFS_FT_CHRDEV) {
 		struct erofs_inode ei = { .sbi = sbi, .nid = d->nid };
@@ -1933,7 +1933,8 @@ static int erofs_mkfs_dump_tree(struct erofs_importer *im, bool rebuild,
 		list_for_each_entry(d, &dir->i_subdirs, d_child) {
 			struct erofs_inode *inode = d->inode;
 
-			if (is_dot_dotdot(d->name) || d->validnid)
+			if (is_dot_dotdot(d->name) ||
+			    (d->flags & EROFS_DENTRY_FLAG_VALIDNID))
 				continue;
 
 			if (!erofs_inode_visited(inode)) {
lib/rebuild.c +4 -2
index c5b44d5..f89a17c 100644
@@ -484,12 +484,14 @@ static int erofs_rebuild_basedir_dirent_iter(struct erofs_dir_context *ctx)
 	if (d->type == EROFS_FT_UNKNOWN) {
 		d->nid = ctx->de_nid;
 		d->type = ctx->de_ftype;
-		d->validnid = true;
+		d->flags |= EROFS_DENTRY_FLAG_VALIDNID;
+		if (d->type == EROFS_FT_DIR)
+			d->flags |= EROFS_DENTRY_FLAG_FIXUP_PNID;
 		if (!mergedir->whiteouts && erofs_dentry_is_wht(dir->sbi, d))
 			mergedir->whiteouts = true;
 		*rctx->i_nlink += (ctx->de_ftype == EROFS_FT_DIR);
 		++*rctx->nr_subdirs;
-	} else if (__erofs_unlikely(d->validnid)) {
+	} else if (__erofs_unlikely(d->flags & EROFS_DENTRY_FLAG_VALIDNID)) {
 		/* The base image appears to be corrupted */
 		DBG_BUGON(1);
 		ret = -EFSCORRUPTED;
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 3/7 erofs-utils: lib: unexport "erofs/fragments.h"

In reply to Gao Xiang

Disallow external users to use those internal APIs.

---
 dump/main.c                                   |  1 -
 fsck/main.c                                   |  1 -
 fuse/main.c                                   |  1 -
 include/erofs/internal.h                      | 14 +++++++++
 lib/Makefile.am                               |  2 +-
 lib/compress.c                                |  2 +-
 lib/data.c                                    |  2 +-
 lib/fragments.c                               |  2 +-
 lib/importer.c                                |  2 +-
 lib/inode.c                                   |  4 +--
 .../fragments.h => lib/liberofs_fragments.h   | 30 +++----------------
 lib/xattr.c                                   |  2 +-
 mkfs/main.c                                   |  1 -
 13 files changed, 26 insertions(+), 38 deletions(-)
 rename include/erofs/fragments.h => lib/liberofs_fragments.h (50%)
dump/main.c +0 -1
index 4ad3f7b..57ec3b7 100644
@@ -13,7 +13,6 @@
 #include "erofs/print.h"
 #include "erofs/inode.h"
 #include "erofs/dir.h"
-#include "erofs/fragments.h"
 #include "../lib/liberofs_compress.h"
 #include "../lib/liberofs_private.h"
 #include "../lib/liberofs_uuid.h"
fsck/main.c +0 -1
index a1cb0cd..8aba964 100644
@@ -16,7 +16,6 @@
 #include "erofs/xattr.h"
 #include "../lib/compressor.h"
 #include "../lib/liberofs_compress.h"
-#include "erofs/fragments.h"
 
 static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid);
 
fuse/main.c +0 -1
index c129a0c..82aca8c 100644
@@ -12,7 +12,6 @@
 #include "erofs/print.h"
 #include "erofs/dir.h"
 #include "erofs/inode.h"
-#include "erofs/fragments.h"
 
 #include <float.h>
 #include <fuse.h>
include/erofs/internal.h +14 -0
index 4de6563..6106501 100644
@@ -535,6 +535,20 @@ static inline int erofs_blk_read(struct erofs_sb_info *sbi, int device_id,
 /* vmdk.c */
 int erofs_dump_vmdk_desc(FILE *f, struct erofs_sb_info *sbi);
 
+extern const char *erofs_frags_packedname;
+#define EROFS_PACKED_INODE	erofs_frags_packedname
+
+static inline bool erofs_is_packed_inode(struct erofs_inode *inode)
+{
+	return inode->i_srcpath == EROFS_PACKED_INODE;
+}
+
+int erofs_packedfile_init(struct erofs_sb_info *sbi, bool fragments_mkfs);
+void erofs_packedfile_exit(struct erofs_sb_info *sbi);
+
+int erofs_packedfile_read(struct erofs_sb_info *sbi,
+			  void *buf, erofs_off_t len, erofs_off_t pos);
+
 /* XXX: will find a better way later */
 erofs_blk_t erofs_total_metablocks(struct erofs_bufmgr *bmgr);
 
lib/Makefile.am +1 -1
index 9991119..286932d 100644
@@ -22,11 +22,11 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/trace.h \
       $(top_srcdir)/include/erofs/xattr.h \
       $(top_srcdir)/include/erofs/compress_hints.h \
-      $(top_srcdir)/include/erofs/fragments.h \
       $(top_srcdir)/include/erofs/importer.h \
       $(top_srcdir)/lib/liberofs_base64.h \
       $(top_srcdir)/lib/liberofs_cache.h \
       $(top_srcdir)/lib/liberofs_compress.h \
+      $(top_srcdir)/lib/liberofs_fragments.h \
       $(top_srcdir)/lib/liberofs_private.h \
       $(top_srcdir)/lib/liberofs_rebuild.h \
       $(top_srcdir)/lib/liberofs_xxhash.h \
lib/compress.c +1 -1
index 7f34b66..20f511e 100644
@@ -19,12 +19,12 @@
 #include "compressor.h"
 #include "erofs/block_list.h"
 #include "erofs/compress_hints.h"
-#include "erofs/fragments.h"
 #ifdef EROFS_MT_ENABLED
 #include "erofs/workqueue.h"
 #endif
 #include "liberofs_cache.h"
 #include "liberofs_compress.h"
+#include "liberofs_fragments.h"
 #include "liberofs_metabox.h"
 
 #define Z_EROFS_DESTBUF_SZ	(Z_EROFS_PCLUSTER_MAX_SIZE + EROFS_MAX_BLOCK_SIZE * 2)
lib/data.c +1 -1
index 6791fda..1e51134 100644
@@ -8,7 +8,7 @@
 #include "erofs/internal.h"
 #include "erofs/trace.h"
 #include "erofs/decompress.h"
-#include "erofs/fragments.h"
+#include "liberofs_fragments.h"
 
 void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 {
lib/fragments.c +1 -1
index 6a30785..244608f 100644
@@ -16,11 +16,11 @@
 #include "erofs/inode.h"
 #include "erofs/print.h"
 #include "erofs/internal.h"
-#include "erofs/fragments.h"
 #include "erofs/bitops.h"
 #include "erofs/lock.h"
 #include "erofs/importer.h"
 #include "liberofs_compress.h"
+#include "liberofs_fragments.h"
 #include "liberofs_private.h"
 #ifdef HAVE_SYS_SENDFILE_H
 #include <sys/sendfile.h>
lib/importer.c +1 -1
index fed7bab..ad4bed8 100644
@@ -2,7 +2,6 @@
 /*
  * Copyright (C) 2025 Alibaba Cloud
  */
-#include "erofs/fragments.h"
 #include "erofs/importer.h"
 #include "erofs/config.h"
 #include "erofs/dedupe.h"
@@ -12,6 +11,7 @@
 #include "erofs/xattr.h"
 #include "liberofs_cache.h"
 #include "liberofs_compress.h"
+#include "liberofs_fragments.h"
 #include "liberofs_metabox.h"
 
 static EROFS_DEFINE_MUTEX(erofs_importer_global_mutex);
lib/inode.c +2 -2
index d16468d..14a65af 100644
@@ -24,12 +24,12 @@
 #include "erofs/block_list.h"
 #include "erofs/compress_hints.h"
 #include "erofs/blobchunk.h"
-#include "erofs/fragments.h"
 #include "erofs/importer.h"
 #include "liberofs_cache.h"
 #include "liberofs_compress.h"
-#include "liberofs_private.h"
+#include "liberofs_fragments.h"
 #include "liberofs_metabox.h"
+#include "liberofs_private.h"
 #include "liberofs_rebuild.h"
 
 static inline bool erofs_is_special_identifier(const char *path)
lib/liberofs_fragments.h renamed from include/erofs/fragments.h +4 -26
similarity index 50%
rename from include/erofs/fragments.h
rename to lib/liberofs_fragments.h
index 00e1e46..ca71e52 100644
@@ -1,25 +1,13 @@
 /* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
 /*
- * Copyright (C), 2022, Coolpad Group Limited.
+ * Copyright (C) 2022, Coolpad Group Limited.
+ * Copyright (C) 2025 Alibaba Cloud
  */
-#ifndef __EROFS_FRAGMENTS_H
-#define __EROFS_FRAGMENTS_H
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
+#ifndef __EROFS_LIB_LIBEROFS_FRAGMENTS_H
+#define __EROFS_LIB_LIBEROFS_FRAGMENTS_H
 
 #include "erofs/internal.h"
 
-extern const char *erofs_frags_packedname;
-#define EROFS_PACKED_INODE	erofs_frags_packedname
-
-static inline bool erofs_is_packed_inode(struct erofs_inode *inode)
-{
-	return inode->i_srcpath == EROFS_PACKED_INODE;
-}
-
 struct erofs_importer;
 
 u32 z_erofs_fragments_tofh(struct erofs_inode *inode, int fd, erofs_off_t fpos);
@@ -32,14 +20,4 @@ int erofs_fragment_commit(struct erofs_inode *inode, u32 tofh);
 int erofs_flush_packed_inode(struct erofs_importer *im);
 int erofs_packedfile(struct erofs_sb_info *sbi);
 
-int erofs_packedfile_init(struct erofs_sb_info *sbi, bool fragments_mkfs);
-void erofs_packedfile_exit(struct erofs_sb_info *sbi);
-
-int erofs_packedfile_read(struct erofs_sb_info *sbi,
-			  void *buf, erofs_off_t len, erofs_off_t pos);
-
-#ifdef __cplusplus
-}
-#endif
-
 #endif
lib/xattr.c +1 -1
index e02e8cb..fc22c81 100644
@@ -15,9 +15,9 @@
 #include "erofs/print.h"
 #include "erofs/list.h"
 #include "erofs/xattr.h"
-#include "erofs/fragments.h"
 #include "erofs/importer.h"
 #include "liberofs_cache.h"
+#include "liberofs_fragments.h"
 #include "liberofs_metabox.h"
 #include "liberofs_xxhash.h"
 #include "liberofs_private.h"
mkfs/main.c +0 -1
index 5657b1d..11e3032 100644
@@ -25,7 +25,6 @@
 #include "erofs/block_list.h"
 #include "erofs/compress_hints.h"
 #include "erofs/blobchunk.h"
-#include "erofs/fragments.h"
 #include "../lib/compressor.h"
 #include "../lib/liberofs_gzran.h"
 #include "../lib/liberofs_metabox.h"
Signature · 1 line
2.43.5
author
#3 lore raw

PATCH 4/7 erofs-utils: lib: switch to vfile interfaces for fragments

In reply to Gao Xiang

Avoid using raw FDs for the upcoming directory data compression.

---
 lib/compress.c           | 21 ++++++++------
 lib/fragments.c          | 62 ++++++++++++++++++----------------------
 lib/liberofs_fragments.h | 10 ++++---
 3 files changed, 47 insertions(+), 46 deletions(-)
lib/compress.c +13 -8
index 20f511e..74d40b1 100644
@@ -1258,13 +1258,14 @@ int z_erofs_compress_segment(struct z_erofs_compress_sctx *ctx,
 	bool frag = params->fragments && !erofs_is_packed_inode(inode) &&
 		!erofs_is_metabox_inode(inode) &&
 		ctx->seg_idx >= ictx->seg_num - 1;
-	int fd = ictx->fd;
+	struct erofs_vfile vf = { .fd = ictx->fd };
 	int ret;
 
 	DBG_BUGON(offset != -1 && frag && inode->fragment_size);
 	if (offset != -1 && frag && !inode->fragment_size &&
 	    params->fragdedupe != EROFS_FRAGDEDUPE_OFF) {
-		ret = erofs_fragment_findmatch(inode, fd, ictx->tofh);
+		ret = erofs_fragment_findmatch(inode,
+					       &vf, ictx->fpos, ictx->tofh);
 		if (ret < 0)
 			return ret;
 		if (inode->fragment_size > ctx->remaining)
@@ -1280,9 +1281,9 @@ int z_erofs_compress_segment(struct z_erofs_compress_sctx *ctx,
 		int ret;
 
 		ret = (offset == -1 ?
-			read(fd, ctx->queue + ctx->tail, rx) :
-			pread(fd, ctx->queue + ctx->tail, rx,
-			      ictx->fpos + offset));
+			erofs_io_read(&vf, ctx->queue + ctx->tail, rx) :
+			erofs_io_pread(&vf, ctx->queue + ctx->tail, rx,
+				       ictx->fpos + offset));
 		if (ret != rx)
 			return -errno;
 
@@ -1848,14 +1849,17 @@ void *erofs_begin_compressed_file(struct erofs_importer *im,
 		inode->z_advise |= Z_EROFS_ADVISE_INTERLACED_PCLUSTER;
 
 	if (frag) {
-		ictx->tofh = z_erofs_fragments_tofh(inode, fd, fpos);
+		struct erofs_vfile vf = { .fd = fd };
+
+		ictx->tofh = z_erofs_fragments_tofh(inode, &vf, fpos);
 		if (ictx == &g_ictx &&
 		    params->fragdedupe != EROFS_FRAGDEDUPE_OFF) {
 			/*
 			 * Handle tails in advance to avoid writing duplicated
 			 * parts into the packed inode.
 			 */
-			ret = erofs_fragment_findmatch(inode, fd, ictx->tofh);
+			ret = erofs_fragment_findmatch(inode,
+						       &vf, fpos, ictx->tofh);
 			if (ret < 0)
 				goto err_free_ictx;
 
@@ -1874,7 +1878,8 @@ void *erofs_begin_compressed_file(struct erofs_importer *im,
 	ictx->dedupe = false;
 
 	if (all_fragments && !inode->fragment_size) {
-		ret = erofs_pack_file_from_fd(inode, fd, ictx->tofh);
+		ret = erofs_pack_file_from_fd(inode,
+			&((struct erofs_vfile){ .fd = fd }), fpos, ictx->tofh);
 		if (ret)
 			goto err_free_idata;
 	}
lib/fragments.c +28 -34

132 lines not on this page · raw message

lib/liberofs_fragments.h +6 -4
index ca71e52..11833eb 100644
@@ -10,10 +10,12 @@
 
 struct erofs_importer;
 
-u32 z_erofs_fragments_tofh(struct erofs_inode *inode, int fd, erofs_off_t fpos);
-int erofs_fragment_findmatch(struct erofs_inode *inode, int fd, u32 tofh);
-
-int erofs_pack_file_from_fd(struct erofs_inode *inode, int fd, u32 tofcrc);
+u32 z_erofs_fragments_tofh(struct erofs_inode *inode,
+			   struct erofs_vfile *vf, erofs_off_t fpos);
+int erofs_fragment_findmatch(struct erofs_inode *inode,
+			     struct erofs_vfile *vf, erofs_off_t fpos, u32 tofh);
+int erofs_pack_file_from_fd(struct erofs_inode *inode,
+			    struct erofs_vfile *vf, erofs_off_t fpos, u32 tofh);
 int erofs_fragment_pack(struct erofs_inode *inode, void *data,
 			erofs_off_t pos, erofs_off_t len, u32 tofh, bool tail);
 int erofs_fragment_commit(struct erofs_inode *inode, u32 tofh);
Signature · 1 line
2.43.5
author
#4 lore raw

PATCH 5/7 erofs-utils: lib: switch to vfile interfaces for compression

In reply to Gao Xiang

Split erofs_begin_compressed_file() into three interfaces:
- erofs_prepare_compressed_file();
- erofs_bind_compressed_file_with_fd();
- erofs_begin_compressed_file().

---
 lib/compress.c          | 74 +++++++++++++++++++++++++----------------
 lib/inode.c             | 16 ++++++---
 lib/liberofs_compress.h |  7 ++--
 3 files changed, 63 insertions(+), 34 deletions(-)
lib/compress.c +46 -28
index 74d40b1..1417967 100644
@@ -35,6 +35,7 @@ struct z_erofs_extent_item {
 };
 
 struct z_erofs_compress_ictx {		/* inode context */
+	struct erofs_vfile _vf, *vf;
 	struct erofs_importer *im;
 	struct erofs_inode *inode;
 	struct erofs_compress_cfg *ccfg;
@@ -1258,14 +1259,14 @@ int z_erofs_compress_segment(struct z_erofs_compress_sctx *ctx,
 	bool frag = params->fragments && !erofs_is_packed_inode(inode) &&
 		!erofs_is_metabox_inode(inode) &&
 		ctx->seg_idx >= ictx->seg_num - 1;
-	struct erofs_vfile vf = { .fd = ictx->fd };
+	struct erofs_vfile *vf = ictx->vf;
 	int ret;
 
 	DBG_BUGON(offset != -1 && frag && inode->fragment_size);
 	if (offset != -1 && frag && !inode->fragment_size &&
 	    params->fragdedupe != EROFS_FRAGDEDUPE_OFF) {
-		ret = erofs_fragment_findmatch(inode,
-					       &vf, ictx->fpos, ictx->tofh);
+		ret = erofs_fragment_findmatch(inode, vf, ictx->fpos,
+					       ictx->tofh);
 		if (ret < 0)
 			return ret;
 		if (inode->fragment_size > ctx->remaining)
@@ -1281,8 +1282,8 @@ int z_erofs_compress_segment(struct z_erofs_compress_sctx *ctx,
 		int ret;
 
 		ret = (offset == -1 ?
-			erofs_io_read(&vf, ctx->queue + ctx->tail, rx) :
-			erofs_io_pread(&vf, ctx->queue + ctx->tail, rx,
+			erofs_io_read(vf, ctx->queue + ctx->tail, rx) :
+			erofs_io_pread(vf, ctx->queue + ctx->tail, rx,
 				       ictx->fpos + offset));
 		if (ret != rx)
 			return -errno;
@@ -1780,6 +1781,8 @@ int z_erofs_mt_global_exit(void)
 	return 0;
 }
 #else
+
+
 static int z_erofs_mt_global_init(struct erofs_importer *im)
 {
 	z_erofs_mt_enabled = false;
@@ -1792,8 +1795,8 @@ int z_erofs_mt_global_exit(void)
 }
 #endif
 
-void *erofs_begin_compressed_file(struct erofs_importer *im,
-				  struct erofs_inode *inode, int fd, u64 fpos)
+void *erofs_prepare_compressed_file(struct erofs_importer *im,
+				    struct erofs_inode *inode)
 {
 	const struct erofs_importer_params *params = im->params;
 	struct erofs_sb_info *sbi = inode->sbi;
@@ -1801,7 +1804,6 @@ void *erofs_begin_compressed_file(struct erofs_importer *im,
 	bool frag = params->fragments && !erofs_is_packed_inode(inode) &&
 		!erofs_is_metabox_inode(inode);
 	bool all_fragments = params->all_fragments && frag;
-	int ret;
 
 	/* initialize per-file compression setting */
 	inode->z_advise = 0;
@@ -1835,7 +1837,6 @@ void *erofs_begin_compressed_file(struct erofs_importer *im,
 	}
 	ictx->im = im;
 	ictx->inode = inode;
-	ictx->fd = fd;
 	if (erofs_is_metabox_inode(inode))
 		ictx->ccfg = &sbi->zmgr->ccfg[cfg.c_mkfs_metabox_algid];
 	else
@@ -1848,10 +1849,33 @@ void *erofs_begin_compressed_file(struct erofs_importer *im,
 	if (params->fragments && !params->dedupe && !ictx->data_unaligned)
 		inode->z_advise |= Z_EROFS_ADVISE_INTERLACED_PCLUSTER;
 
-	if (frag) {
-		struct erofs_vfile vf = { .fd = fd };
+	init_list_head(&ictx->extents);
+	ictx->fix_dedupedfrag = false;
+	ictx->fragemitted = false;
+	ictx->dedupe = false;
+	return ictx;
+}
+
+void erofs_bind_compressed_file_with_fd(struct z_erofs_compress_ictx *ictx,
+					int fd, u64 fpos)
+{
+	ictx->_vf = (struct erofs_vfile){ .fd = fd };
+	ictx->vf = &ictx->_vf;
+	ictx->fpos = fpos;
+}
+
+int erofs_begin_compressed_file(struct z_erofs_compress_ictx *ictx)
+{
+	const struct erofs_importer_params *params = ictx->im->params;
+	struct erofs_inode *inode = ictx->inode;
+	bool frag = params->fragments && !erofs_is_packed_inode(inode) &&
+		!erofs_is_metabox_inode(inode);
+	bool all_fragments = params->all_fragments && frag;
+	int ret;
 
-		ictx->tofh = z_erofs_fragments_tofh(inode, &vf, fpos);
+	if (frag) {
+		ictx->tofh = z_erofs_fragments_tofh(inode,
+						    ictx->vf, ictx->fpos);
 		if (ictx == &g_ictx &&
 		    params->fragdedupe != EROFS_FRAGDEDUPE_OFF) {
 			/*
@@ -1859,9 +1883,9 @@ void *erofs_begin_compressed_file(struct erofs_importer *im,
 			 * parts into the packed inode.
 			 */
 			ret = erofs_fragment_findmatch(inode,
-						       &vf, fpos, ictx->tofh);
+					ictx->vf, ictx->fpos, ictx->tofh);
 			if (ret < 0)
-				goto err_free_ictx;
+				goto err_out;
 
 			if (params->fragdedupe == EROFS_FRAGDEDUPE_INODE &&
 			    inode->fragment_size < inode->i_size) {
@@ -1871,36 +1895,30 @@ void *erofs_begin_compressed_file(struct erofs_importer *im,
 			}
 		}
 	}
-	ictx->fpos = fpos;
-	init_list_head(&ictx->extents);
-	ictx->fix_dedupedfrag = false;
-	ictx->fragemitted = false;
-	ictx->dedupe = false;
 
 	if (all_fragments && !inode->fragment_size) {
-		ret = erofs_pack_file_from_fd(inode,
-			&((struct erofs_vfile){ .fd = fd }), fpos, ictx->tofh);
+		ret = erofs_pack_file_from_fd(inode, ictx->vf, ictx->fpos,
+					      ictx->tofh);
 		if (ret)
-			goto err_free_idata;
+			goto err_out;
 	}
+
 #ifdef EROFS_MT_ENABLED
 	if (ictx != &g_ictx) {
 		ret = z_erofs_mt_compress(ictx);
 		if (ret)
-			goto err_free_idata;
+			goto err_out;
 	}
 #endif
-	return ictx;
-
-err_free_idata:
+	return 0;
+err_out:
 	if (inode->idata) {
 		free(inode->idata);
 		inode->idata = NULL;
 	}
-err_free_ictx:
 	if (ictx != &g_ictx)
 		free(ictx);
-	return ERR_PTR(ret);
+	return ret;
 }
 
 int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx)
lib/inode.c +12 -4
index 14a65af..e7c3edf 100644
@@ -1790,6 +1790,7 @@ static int erofs_mkfs_begin_nondirectory(struct erofs_importer *im,
 {
 	struct erofs_mkfs_job_ndir_ctx ctx =
 		{ .inode = inode, .fd = -1 };
+	int ret;
 
 	if (S_ISREG(inode->i_mode) && inode->i_size) {
 		switch (inode->datasource) {
@@ -1808,10 +1809,14 @@ static int erofs_mkfs_begin_nondirectory(struct erofs_importer *im,
 		}
 		if (ctx.fd >= 0 && cfg.c_compr_opts[0].alg &&
 		    erofs_file_is_compressible(im, inode)) {
-			ctx.ictx = erofs_begin_compressed_file(im, inode,
-							ctx.fd, ctx.fpos);
+			ctx.ictx = erofs_prepare_compressed_file(im, inode);
 			if (IS_ERR(ctx.ictx))
 				return PTR_ERR(ctx.ictx);
+			erofs_bind_compressed_file_with_fd(ctx.ictx,
+							   ctx.fd, ctx.fpos);
+			ret = erofs_begin_compressed_file(ctx.ictx);
+			if (ret)
+				return ret;
 		}
 	}
 	return erofs_mkfs_go(im, EROFS_MKFS_JOB_NDIR, &ctx, sizeof(ctx));
@@ -2129,11 +2134,14 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_importer *im,
 
 	if (cfg.c_compr_opts[0].alg &&
 	    erofs_file_is_compressible(im, inode)) {
-		ictx = erofs_begin_compressed_file(im, inode, fd, 0);
+		ictx = erofs_prepare_compressed_file(im, inode);
 		if (IS_ERR(ictx))
 			return ERR_CAST(ictx);
 
-		DBG_BUGON(!ictx);
+		erofs_bind_compressed_file_with_fd(ictx, fd, 0);
+		ret = erofs_begin_compressed_file(ictx);
+		if (ret)
+			return ERR_PTR(ret);
 		ret = erofs_write_compressed_file(ictx);
 		if (!ret)
 			goto out;
lib/liberofs_compress.h +5 -2
index e0f4d24..8b39735 100644
@@ -15,8 +15,11 @@
 struct z_erofs_compress_ictx;
 
 void z_erofs_drop_inline_pcluster(struct erofs_inode *inode);
-void *erofs_begin_compressed_file(struct erofs_importer *im,
-				  struct erofs_inode *inode, int fd, u64 fpos);
+void *erofs_prepare_compressed_file(struct erofs_importer *im,
+				    struct erofs_inode *inode);
+void erofs_bind_compressed_file_with_fd(struct z_erofs_compress_ictx *ictx,
+					int fd, u64 fpos);
+int erofs_begin_compressed_file(struct z_erofs_compress_ictx *ictx);
 int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx);
 
 int z_erofs_compress_init(struct erofs_importer *im);
Signature · 1 line
2.43.5
author
#5 lore raw

PATCH 6/7 erofs-utils: lib: refine erofs_write_unencoded_data()

In reply to Gao Xiang

It is now used as an unencoded layout writer by leveraging virtual files.

---
 lib/inode.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
lib/inode.c +16 -12
index e7c3edf..264c4ae 100644
@@ -612,33 +612,31 @@ static bool erofs_file_is_compressible(struct erofs_importer *im,
 	return true;
 }
 
-static int write_uncompressed_file_from_fd(struct erofs_inode *inode, int fd,
-					   erofs_off_t fpos)
+static int erofs_write_unencoded_data(struct erofs_inode *inode,
+				      struct erofs_vfile *vf, erofs_off_t fpos,
+				      bool noseek)
 {
 	struct erofs_sb_info *sbi = inode->sbi;
 	erofs_blk_t nblocks, i;
 	unsigned int len;
 	int ret;
-	bool noseek = inode->datasource == EROFS_INODE_DATA_SOURCE_DISKBUF;
 
 	if (!noseek && erofs_sb_has_48bit(sbi)) {
-		if (lseek(fd, fpos, SEEK_DATA) < 0 && errno == ENXIO) {
+		if (erofs_io_lseek(vf, fpos, SEEK_DATA) < 0 && errno == ENXIO) {
 			ret = erofs_allocate_inode_bh_data(inode, 0);
 			if (ret)
 				return ret;
 			inode->datalayout = EROFS_INODE_FLAT_PLAIN;
 			return 0;
 		}
-		ret = lseek(fd, fpos, SEEK_SET);
+		ret = erofs_io_lseek(vf, fpos, SEEK_SET);
 		if (ret < 0)
 			return ret;
 		else if (ret != fpos)
 			return -EIO;
 	}
 
-	inode->datalayout = EROFS_INODE_FLAT_INLINE;
 	nblocks = inode->i_size >> sbi->blkszbits;
-
 	ret = erofs_allocate_inode_bh_data(inode, nblocks);
 	if (ret)
 		return ret;
@@ -648,8 +646,7 @@ static int write_uncompressed_file_from_fd(struct erofs_inode *inode, int fd,
 			    erofs_pos(sbi, nblocks - i));
 		ret = erofs_io_xcopy(&sbi->bdev,
 				     erofs_pos(sbi, inode->u.i_blkaddr + i),
-				     &((struct erofs_vfile){ .fd = fd }), len,
-				     noseek);
+				     vf, len, noseek);
 		if (ret)
 			return ret;
 	}
@@ -661,7 +658,7 @@ static int write_uncompressed_file_from_fd(struct erofs_inode *inode, int fd,
 		if (!inode->idata)
 			return -ENOMEM;
 
-		ret = read(fd, inode->idata, inode->idata_size);
+		ret = erofs_io_read(vf, inode->idata, inode->idata_size);
 		if (ret < inode->idata_size) {
 			free(inode->idata);
 			inode->idata = NULL;
@@ -682,8 +679,11 @@ int erofs_write_unencoded_file(struct erofs_inode *inode, int fd, u64 fpos)
 		return erofs_blob_write_chunked_file(inode, fd, fpos);
 	}
 
+	inode->datalayout = EROFS_INODE_FLAT_INLINE;
 	/* fallback to all data uncompressed */
-	return write_uncompressed_file_from_fd(inode, fd, fpos);
+	return erofs_write_unencoded_data(inode,
+			&(struct erofs_vfile){ .fd = fd }, fpos,
+			inode->datasource == EROFS_INODE_DATA_SOURCE_DISKBUF);
 }
 
 int erofs_iflush(struct erofs_inode *inode)
@@ -2152,7 +2152,11 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_importer *im,
 		if (ret < 0)
 			return ERR_PTR(-errno);
 	}
-	ret = write_uncompressed_file_from_fd(inode, fd, 0);
+
+	inode->datalayout = EROFS_INODE_FLAT_INLINE;
+	ret = erofs_write_unencoded_data(inode,
+			&(struct erofs_vfile){ .fd = fd }, 0,
+			inode->datasource == EROFS_INODE_DATA_SOURCE_DISKBUF);
 	if (ret)
 		return ERR_PTR(ret);
 out:
Signature · 1 line
2.43.5
author
#6 lore raw

PATCH 7/7 erofs-utils: lib: introduce directory writer

In reply to Gao Xiang

Directories are now written to disk using virtual files too.

---
 lib/inode.c | 153 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 90 insertions(+), 63 deletions(-)
lib/inode.c +90 -63
index 264c4ae..7587248 100644
@@ -377,16 +377,6 @@ static void fill_dirblock(char *buf, unsigned int size, unsigned int q,
 	memset(buf + q, 0, size - q);
 }
 
-static int write_dirblock(struct erofs_sb_info *sbi,
-			  unsigned int q, struct erofs_dentry *head,
-			  struct erofs_dentry *end, erofs_blk_t blkaddr)
-{
-	char buf[EROFS_MAX_BLOCK_SIZE];
-
-	fill_dirblock(buf, erofs_blksiz(sbi), q, head, end);
-	return erofs_blk_write(sbi, buf, blkaddr, 1);
-}
-
 erofs_nid_t erofs_lookupnid(struct erofs_inode *inode)
 {
 	struct erofs_buffer_head *const bh = inode->bh;
@@ -515,64 +505,85 @@ static int erofs_rebuild_inode_fix_pnid(struct erofs_inode *parent,
 	return -EFSCORRUPTED;
 }
 
-static int erofs_write_dir_file(struct erofs_inode *dir)
-{
-	struct erofs_dentry *head = list_first_entry(&dir->i_subdirs,
-						     struct erofs_dentry,
-						     d_child);
-	struct erofs_sb_info *sbi = dir->sbi;
-	struct erofs_dentry *d;
-	int ret;
-	unsigned int q, used, blkno;
-
-	q = used = blkno = 0;
-
-	/* allocate dir main data */
-	ret = erofs_allocate_inode_bh_data(dir, erofs_blknr(sbi, dir->i_size));
-	if (ret)
-		return ret;
-
-	list_for_each_entry(d, &dir->i_subdirs, d_child) {
-		unsigned int len = d->namelen + sizeof(struct erofs_dirent);
-
-		/* XXX: a bit hacky, but to avoid another traversal */
-		if (d->flags & EROFS_DENTRY_FLAG_FIXUP_PNID) {
-			ret = erofs_rebuild_inode_fix_pnid(dir, d->nid);
-			if (ret)
-				return ret;
-		}
-
-		erofs_d_invalidate(d);
-		if (used + len > erofs_blksiz(sbi)) {
-			ret = write_dirblock(sbi, q, head, d,
-					     dir->u.i_blkaddr + blkno);
-			if (ret)
-				return ret;
+struct erofs_dirwriter_vf {
+	struct erofs_vfile vf;
+	struct erofs_inode *dir;
+	struct list_head *head;
+	erofs_off_t offset;
+	char dirdata[];
+};
 
-			head = d;
+static ssize_t erofs_dirwriter_vfread(struct erofs_vfile *vf,
+				      void *buf, size_t len)
+{
+	struct erofs_dirwriter_vf *dwv = (struct erofs_dirwriter_vf *)vf;
+	struct erofs_inode *dir = dwv->dir;
+	unsigned int bsz = erofs_blksiz(dir->sbi);
+	size_t processed = 0;
+
+	if (len > dir->i_size - dwv->offset)
+		len = dir->i_size - dwv->offset;
+	while (processed < len) {
+		unsigned int off, dblen, count;
+
+		off = dwv->offset & (bsz - 1);
+		dblen = min_t(u64, dir->i_size - dwv->offset + off, bsz);
+		/* generate a directory block to `dwv->dirdata` */
+		if (!off) {
+			struct erofs_dentry *head, *d;
+			unsigned int q, used, len;
+			int err;
+
+			d = head = list_entry(dwv->head,
+					      struct erofs_dentry, d_child);
 			q = used = 0;
-			++blkno;
+			do {
+				/* XXX: a bit hacky, but avoids another traversal */
+				if (d->flags & EROFS_DENTRY_FLAG_FIXUP_PNID) {
+					err = erofs_rebuild_inode_fix_pnid(dir, d->nid);
+					if (err)
+						return err;
+				}
+				len = d->namelen + sizeof(struct erofs_dirent);
+				erofs_d_invalidate(d);
+				if ((used += len) > bsz)
+					break;
+				d = list_next_entry(d, d_child);
+				q += sizeof(struct erofs_dirent);
+			} while (&d->d_child != &dir->i_subdirs);
+			fill_dirblock(dwv->dirdata, dblen, q, head, d);
+			dwv->head = &d->d_child;
 		}
-		used += len;
-		q += sizeof(struct erofs_dirent);
+		count = min_t(size_t, dblen - off, len - processed);
+		memcpy(buf + processed, dwv->dirdata + off, count);
+		processed += count;
+		dwv->offset += count;
 	}
+	return processed;
+}
 
-	DBG_BUGON(used > erofs_blksiz(sbi));
-	if (used == erofs_blksiz(sbi)) {
-		DBG_BUGON(dir->i_size % erofs_blksiz(sbi));
-		DBG_BUGON(dir->idata_size);
-		return write_dirblock(sbi, q, head, d, dir->u.i_blkaddr + blkno);
-	}
-	DBG_BUGON(used != dir->i_size % erofs_blksiz(sbi));
-	if (used) {
-		/* fill tail-end dir block */
-		dir->idata = malloc(used);
-		if (!dir->idata)
-			return -ENOMEM;
-		DBG_BUGON(used != dir->idata_size);
-		fill_dirblock(dir->idata, dir->idata_size, q, head, d);
-	}
-	return 0;
+void erofs_dirwriter_vfclose(struct erofs_vfile *vf)
+{
+	free((void *)vf);
+}
+
+static struct erofs_vfops erofs_dirwriter_vfops = {
+	.read = erofs_dirwriter_vfread,
+	.close = erofs_dirwriter_vfclose,
+};
+
+static struct erofs_vfile *erofs_dirwriter_open(struct erofs_inode *dir)
+{
+	struct erofs_dirwriter_vf *dwv;
+
+	dwv = malloc(sizeof(*dwv) + erofs_blksiz(dir->sbi));
+	if (!dwv)
+		return ERR_PTR(-ENOMEM);
+	dwv->vf.ops = &erofs_dirwriter_vfops;
+	dwv->dir = dir;
+	dwv->head = dir->i_subdirs.next;
+	dwv->offset = 0;
+	return (struct erofs_vfile *)dwv;
 }
 
 int erofs_write_file_from_buffer(struct erofs_inode *inode, char *buf)
@@ -686,6 +697,22 @@ int erofs_write_unencoded_file(struct erofs_inode *inode, int fd, u64 fpos)
 			inode->datasource == EROFS_INODE_DATA_SOURCE_DISKBUF);
 }
 
+static int erofs_write_dir_file(struct erofs_inode *dir)
+{
+	unsigned int bsz = erofs_blksiz(dir->sbi);
+	struct erofs_vfile *vf;
+	int err;
+
+	DBG_BUGON(dir->idata_size != (dir->i_size & (bsz - 1)));
+	vf = erofs_dirwriter_open(dir);
+	if (IS_ERR(vf))
+		return PTR_ERR(vf);
+
+	err = erofs_write_unencoded_data(dir, vf, 0, true);
+	erofs_io_close(vf);
+	return err;
+}
+
 int erofs_iflush(struct erofs_inode *inode)
 {
 	u16 icount = EROFS_INODE_XATTR_ICOUNT(inode->xattr_isize);
Signature · 1 line
2.43.5