Skip to contentSkip to conversation

erofs-utils: rebuild: extract compressed extent loading

PATCH New Lucas Karpinski 1 participant 2 messages Nov 23 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 erofs-utils: rebuild: extract compressed extent loading

Separate source extent discovery from external-device mapping and metadata layout. As well as make extent-list ownership explicit for callers.

Assisted-by ChatGPT:gpt-6.1-sol
---
 lib/compress.c | 93 ++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 61 insertions(+), 32 deletions(-)
lib/compress.c +61 -32
index e764e44..bd2c6a8 100644
@@ -2496,19 +2496,16 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
 	return 0;
 }
 
-int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
-				  struct erofs_inode *inode)
+static int z_erofs_rebuild_load_extents(struct erofs_sb_info *dst_sbi,
+		struct erofs_inode *inode, struct list_head *extents)
 {
 	struct erofs_sb_info *sbi = inode->sbi;
 	struct erofs_map_blocks map = {
 		.buf = __EROFS_BUF_INITIALIZER,
 		.m_la = 0,
 	};
-	struct z_erofs_extent_item *ei, *n;
-	erofs_off_t pend = EROFS_NULL_ADDR;
-	bool consecutive = true;
-	LIST_HEAD(extents);
-	int err, device_id = -1;
+	struct z_erofs_extent_item *ei;
+	int err;
 
 	if (sbi->blkszbits != dst_sbi->blkszbits) {
 		erofs_err("filesystem block size mismatch: %d for image %d, target %d",
@@ -2522,7 +2519,7 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 
 		err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
 		if (err)
-			goto err_out;
+			goto out;
 
 		mdev = (struct erofs_map_dev) {
 			.m_deviceid = map.m_deviceid,
@@ -2530,23 +2527,21 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 		};
 		err = erofs_map_dev(sbi, &mdev);
 		if (err)
-			goto err_out;
+			goto out;
 
 		raw = (map.m_algorithmformat >= Z_EROFS_COMPRESSION_MAX);
 
 		ei = malloc(sizeof(*ei));
 		if (!ei) {
 			err = -ENOMEM;
-			goto err_out;
+			goto out;
 		}
 
 		if (map.m_flags & __EROFS_MAP_FRAGMENT) {
-			DBG_BUGON(!(inode->z_advise &
-					Z_EROFS_ADVISE_FRAGMENT_PCLUSTER));
+			erofs_err("fragment-backed files are not supported in rebuilds: %s",
+				  inode->i_srcpath);
 			err = -EOPNOTSUPP;
 			goto err_free_ei;
-//			inode->fragment_size = map.m_llen;
-//			DBG_BUGON(inode->fragmentoff != map.m_pa);
 		} else if (map.m_flags & EROFS_MAP_META) {
 			DBG_BUGON(inode->idata_size != map.m_plen);
 			inode->idata = malloc(inode->idata_size);
@@ -2561,8 +2556,8 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 			ei->e = (struct z_erofs_inmem_extent) {
 				.length = map.m_llen,
 				.plen = erofs_blksiz(sbi),
-				.pstart = (pend != EROFS_NULL_ADDR ? pend : 0),
-				.device_id = inode->dev,	/* FIXME! */
+				.pstart = 0,
+				.device_id = 0,
 				.raw = raw,
 				.inlined = true,
 			};
@@ -2575,16 +2570,11 @@ 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 = mdev.m_deviceid,
 				.partial = map.m_flags & EROFS_MAP_PARTIAL_REF,
 				.raw = raw,
 				.inlined = false,
 			};
-			if (pend != EROFS_NULL_ADDR && (pend != ei->e.pstart ||
-						device_id != ei->e.device_id))
-				consecutive = false;
-			pend = ei->e.pstart + ei->e.plen;
-			device_id = ei->e.device_id;
 		}
 
 		if (!raw) {
@@ -2595,23 +2585,62 @@ int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
 			}
 		}
 		init_list_head(&ei->list);
-		list_add_tail(&ei->list, &extents);
+		list_add_tail(&ei->list, extents);
 		map.m_la += map.m_llen;
 	}
-	inode->z_lclusterbits = sbi->blkszbits;
-	err = z_erofs_prepare_layout(inode, &extents, consecutive, false);
-	if (err)
-		goto err_out;
-	return 0;
+	err = 0;
+	goto out;
 
 err_free_ei:
 	free(ei);
-err_out:
-	free(inode->idata);
-	inode->idata = NULL;
-	list_for_each_entry_safe(ei, n, &extents, list) {
+out:
+	erofs_put_metabuf(&map.buf);
+	return err;
+}
+
+static void z_erofs_rebuild_free_extents(struct list_head *extents)
+{
+	struct z_erofs_extent_item *ei, *n;
+
+	list_for_each_entry_safe(ei, n, extents, list) {
 		list_del(&ei->list);
 		free(ei);
 	}
+}
+
+int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sbi,
+				  struct erofs_inode *inode)
+{
+	struct z_erofs_extent_item *ei;
+	erofs_off_t pend = EROFS_NULL_ADDR;
+	bool consecutive = true;
+	LIST_HEAD(extents);
+	int err, device_id = -1;
+
+	err = z_erofs_rebuild_load_extents(dst_sbi, inode, &extents);
+	if (err)
+		goto out;
+
+	/* Translate source mappings into references to the external image. */
+	list_for_each_entry(ei, &extents, list) {
+		if (ei->e.inlined) {
+			ei->e.pstart = pend != EROFS_NULL_ADDR ? pend : 0;
+		} else {
+			if (pend != EROFS_NULL_ADDR &&
+			    (pend != ei->e.pstart || device_id != ei->e.device_id))
+				consecutive = false;
+			pend = ei->e.pstart + ei->e.plen;
+			device_id = ei->e.device_id;
+		}
+		ei->e.device_id = inode->dev; /* FIXME: remap source devices. */
+	}
+	inode->z_lclusterbits = inode->sbi->blkszbits;
+	err = z_erofs_prepare_layout(inode, &extents, consecutive, false);
+out:
+	if (err) {
+		free(inode->idata);
+		inode->idata = NULL;
+		z_erofs_rebuild_free_extents(&extents);
+	}
 	return err;
 }
Signature · 1 line
Git-157)
#1 lore raw

PATCH 2/2 erofs-utils: rebuild: copy compressed file data in full-data mode

Add a dedicated full-data entry point that reuses compressed extent loading, copies encoded data into destination blocks, and prepares metadata using the relocated addresses.

Requires compatible destination compression settings and fragment-backed files remain unsupported.

Assisted-by ChatGPT:gpt-6.1-sol
---
 lib/compress.c         | 105 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/liberofs_rebuild.h |   5 +++
 lib/rebuild.c          |  14 +++----
 man/mkfs.erofs.1       |   4 ++
 4 files changed, 119 insertions(+), 9 deletions(-)
lib/compress.c +105 -0
index bd2c6a8..cb17b91 100644
@@ -26,6 +26,7 @@
 #include "liberofs_compress.h"
 #include "liberofs_fragments.h"
 #include "liberofs_metabox.h"
+#include "liberofs_rebuild.h"
 #include "erofs/decompress.h"
 
 #define Z_EROFS_DESTBUF_SZ	(Z_EROFS_PCLUSTER_MAX_SIZE + EROFS_MAX_BLOCK_SIZE * 2)
@@ -2496,6 +2497,58 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
 	return 0;
 }
 
+static int z_erofs_rebuild_copy_data(struct erofs_sb_info *dst,
+		struct erofs_inode *inode, struct list_head *extents)
+{
+	struct erofs_sb_info *src = inode->sbi;
+	struct erofs_buffer_head *bh = NULL;
+	struct z_erofs_extent_item *ei;
+	erofs_off_t total = 0, pos = 0;
+	unsigned int bs = erofs_blksiz(dst);
+	int err = 0;
+
+	list_for_each_entry(ei, extents, list)
+		if (!ei->e.inlined)
+			total += round_up(ei->e.plen, bs);
+	if (total) {
+		bh = erofs_balloc(dst->bmgr, DATA, total, 0);
+		if (IS_ERR(bh))
+			return PTR_ERR(bh);
+		erofs_mapbh(NULL, bh->block);
+		pos = erofs_btell(bh, false);
+	}
+
+	list_for_each_entry(ei, extents, list) {
+		erofs_off_t off;
+
+		if (!ei->e.inlined) {
+			for (off = 0; off < ei->e.plen; off += bs) {
+				char buf[EROFS_MAX_BLOCK_SIZE] = {};
+				unsigned int len = min_t(u64, bs, ei->e.plen - off);
+
+				err = erofs_dev_read(src, ei->e.device_id, buf,
+						     ei->e.pstart + off, len);
+				if (err)
+					goto out;
+				err = erofs_dev_write(dst, 0, buf, pos + off, bs);
+				if (err)
+					goto out;
+			}
+		}
+		ei->e.pstart = pos;
+		ei->e.device_id = 0;
+		if (!ei->e.inlined)
+			pos += round_up(ei->e.plen, bs);
+	}
+	inode->u.i_blocks = erofs_blknr(dst, total);
+	erofs_dbg("copied compressed data for %s (%llu bytes)",
+		  inode->i_srcpath, total | 0ULL);
+out:
+	if (bh)
+		erofs_bdrop(bh, !!err);
+	return err;
+}
+
 static int z_erofs_rebuild_load_extents(struct erofs_sb_info *dst_sbi,
 		struct erofs_inode *inode, struct list_head *extents)
 {
@@ -2644,3 +2697,55 @@ out:
 	}
 	return err;
 }
+
+int z_erofs_rebuild_write_full_data(struct erofs_sb_info *dst_sbi,
+				   struct erofs_inode *inode)
+{
+	struct erofs_sb_info *src_sbi = inode->sbi;
+	struct z_erofs_extent_item *ei;
+	unsigned int algs = 0;
+	bool partial = false;
+	LIST_HEAD(extents);
+	int err;
+
+	err = z_erofs_rebuild_load_extents(dst_sbi, inode, &extents);
+	if (err)
+		goto out;
+	list_for_each_entry(ei, &extents, list) {
+		if (!ei->e.raw)
+			algs |= BIT(ei->e.algofmt);
+		partial |= ei->e.partial;
+	}
+	/* The lcluster-index layout can describe at most two algorithms. */
+	inode->z_algorithmtype[0] = algs ? ffs_long(algs) : 0;
+	algs &= algs - 1;
+	inode->z_algorithmtype[1] = algs ? ffs_long(algs) : 0;
+	algs &= algs - 1;
+	if (algs) {
+		err = -EOPNOTSUPP;
+		goto out;
+	}
+	err = z_erofs_rebuild_copy_data(dst_sbi, inode, &extents);
+	if (err)
+		goto out;
+	inode->z_advise &= Z_EROFS_ADVISE_INTERLACED_PCLUSTER;
+	inode->fragment_size = 0;
+	inode->fragmentoff = 0;
+	if (partial)
+		erofs_sb_set_dedupe(dst_sbi);
+	if (inode->idata_size) {
+		inode->z_advise |= Z_EROFS_ADVISE_INLINE_PCLUSTER;
+		erofs_sb_set_ztailpacking(dst_sbi);
+	}
+	inode->sbi = dst_sbi;
+	inode->z_lclusterbits = dst_sbi->blkszbits;
+	err = z_erofs_prepare_layout(inode, &extents, !partial, false);
+	inode->sbi = src_sbi;
+out:
+	if (err) {
+		free(inode->idata);
+		inode->idata = NULL;
+		z_erofs_rebuild_free_extents(&extents);
+	}
+	return err;
+}
lib/liberofs_rebuild.h +5 -0
index 6459dbd..7b45e92 100644
@@ -10,6 +10,11 @@ enum erofs_rebuild_datamode {
 	EROFS_REBUILD_DATA_FULL,
 };
 
+int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst,
+				 struct erofs_inode *inode);
+int z_erofs_rebuild_write_full_data(struct erofs_sb_info *dst,
+				   struct erofs_inode *inode);
+
 struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 		char *path, bool aufs, bool *whout, bool *opq, bool to_head);
 
lib/rebuild.c +5 -9
index e83fd52..d35f36b 100644
@@ -316,15 +316,13 @@ err:
 static int erofs_rebuild_write_blob_index(struct erofs_sb_info *dst_sb,
 					  struct erofs_inode *inode)
 {
-	int z_erofs_rebuild_load_metadata(struct erofs_sb_info *dst_sb,
-					  struct erofs_inode *inode);
-
 	if (is_inode_layout_compression(inode))
 		return z_erofs_rebuild_load_metadata(dst_sb, inode);
 	return erofs_rebuild_load_chunk_index(dst_sb, inode);
 }
 
-static int erofs_rebuild_write_full_data(struct erofs_inode *inode)
+static int erofs_rebuild_write_full_data(struct erofs_sb_info *dst_sbi,
+					struct erofs_inode *inode)
 {
 	struct erofs_sb_info *src_sbi = inode->sbi;
 	int err = 0;
@@ -382,9 +380,7 @@ static int erofs_rebuild_write_full_data(struct erofs_inode *inode)
 			  inode->i_srcpath);
 		err = -EOPNOTSUPP;
 	} else if (is_inode_layout_compression(inode)) {
-		erofs_err("compressed files not yet supported: %s",
-			  inode->i_srcpath);
-		err = -EOPNOTSUPP;
+		err = z_erofs_rebuild_write_full_data(dst_sbi, inode);
 	}
 	return err;
 }
@@ -435,7 +431,7 @@ static int erofs_rebuild_update_inode(struct erofs_sb_info *dst_sb,
 		else if (datamode == EROFS_REBUILD_DATA_RESVSP)
 			inode->datasource = EROFS_INODE_DATA_SOURCE_RESVSP;
 		else if (datamode == EROFS_REBUILD_DATA_FULL)
-			err = erofs_rebuild_write_full_data(inode);
+			err = erofs_rebuild_write_full_data(dst_sb, inode);
 		else
 			err = -EOPNOTSUPP;
 		break;
@@ -635,7 +631,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 (mode != EROFS_REBUILD_DATA_RESVSP) {
 		if (erofs_sb_has_ztailpacking(sbi))
 			erofs_sb_set_ztailpacking(dst_sbi);
 		if (erofs_sb_has_dedupe(sbi))
man/mkfs.erofs.1 +4 -0
index 65ec807..1098b22 100644
@@ -525,6 +525,10 @@ image layers.
 \fB\-\-clean=data\fR: Import complete file data from all source images into
 the destination image, producing a fully self-contained EROFS image that does
 not depend on external blob devices.
+Compressed file data is copied without decompression or recompression.
+As with blob-index rebuilds, specify destination compression settings
+compatible with the source images, such as the same \fB\-z\fR and \fB\-C\fR
+options used to create them. Fragment-backed files are not supported.
 .TP
 .I rvsp mode
 \fB\-\-clean=rvsp\fR or \fB\-\-incremental=rvsp\fR: Reserve space for file
Signature · 1 line
Git-157)