Skip to contentSkip to conversation

erofs-utils: preserve and reserve plain xattr prefixes

PATCH In review bingying lan 1 participant 5 messages Nov 10 Library code

lore
Share & tools
lore
#0 lore raw

cover erofs-utils: preserve and reserve plain xattr prefixes

Incremental builds must preserve the prefix indices referenced by inherited inodes. Replacing the base prefix table with the current command-line list can change those indices. Rewriting an unchanged table is also unnecessary and may fail once the image exceeds the prefix-table address limit.

Patch 1 fixes buffer-head cleanup when writing a prefix table fails. Patch 2 preserves every base prefix slot, appends newly requested prefixes, and reuses an unchanged plain table. It also preserves and remaps the inode digest prefix configuration.

Patch 3 adds --xattr-prefix-reserve to allocate an optional 64 KiB region for future prefix additions. Later incremental builds can append records there without relocating the table. This proposes a new compat feature bit without changing prefix record encoding. Feedback on the flag and reservation design would be appreciated; patches 1 and 2 can be considered separately from this extension.

The series was built and passed local regression tests covering inherited prefix IDs, digest-prefix handling, unchanged-table reuse beyond 16 GiB, table writes around 4 GiB, reservation allocation and migration, packed table handling, and clean failure on prefix offset overflow. Reservation checks covered 512-byte and 4 KiB blocks, compressed data, and shared xattrs. The local test scripts are not included in this series.

This series is based on erofs-utils dev at de2dbe430f06.

Thanks,
bingying lan

bingying lan (3):
erofs-utils: lib: initialize xattr prefix buffer cleanup before errors
erofs-utils: mkfs: preserve plain xattr prefixes in incremental builds
erofs-utils: mkfs: add opt-in plain xattr prefix reservations

 include/erofs/internal.h |   1 +
 include/erofs/xattr.h    |  12 ++
 include/erofs_fs.h       |   2 +
 lib/xattr.c              | 291 +++++++++++++++++++++++++++++++++++++--
 man/mkfs.erofs.1         |  49 +++++++
 mkfs/main.c              |  40 +++++-
 6 files changed, 378 insertions(+), 17 deletions(-)

base-commit: de2dbe430f066ab02d7272e6b47fcc70446bca61

Signature · 1 line
2.53.0.apsara
#1 lore raw

PATCH 1/3 erofs-utils: lib: initialize xattr prefix buffer cleanup before errors

The prefix writer can return after allocating a buffer head but before assigning its operations. Offset overflow and write errors then leave an uninitialized callback for erofs_buffer_exit(), which can crash while reporting the original error.

Install erofs_drop_directly_bhops immediately after successful allocation and remove the redundant assignments after the writes. This fixes an existing error path without changing prefix inheritance or allocation order.

---
 lib/xattr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/xattr.c +1 -2
index c670b12..8c7bbcf 100644
@@ -913,6 +913,7 @@ int erofs_xattr_flush_name_prefixes(struct erofs_importer *im, bool plain)
 		bh = erofs_balloc(bmgr, XATTR, 0, 0);
 		if (IS_ERR(bh))
 			return PTR_ERR(bh);
+		bh->op = &erofs_drop_directly_bhops;
 		(void)erofs_mapbh(bmgr, bh->block);
 		offset = erofs_btell(bh, false);
 	}
@@ -951,11 +952,9 @@ int erofs_xattr_flush_name_prefixes(struct erofs_importer *im, bool plain)
 	}
 
 	if (bh) {
-		bh->op = &erofs_drop_directly_bhops;
 		err = erofs_bh_balloon(bh, offset - start);
 		if (err < 0)
 			return err;
-		bh->op = &erofs_drop_directly_bhops;
 		erofs_bdrop(bh, false);
 	} else {
 		DBG_BUGON(bmgr);
Signature · 1 line
2.53.0.apsara
#2 lore raw

PATCH 2/3 erofs-utils: mkfs: preserve plain xattr prefixes in incremental builds

In reply to bingying lan

Replacing a prefix table with the current command-line list can change indices still referenced by inherited inodes. Preserve every base slot, including duplicates, and append only newly requested prefixes. Use the merged list for new inodes and reuse the original table when no entries are added, including when the image has grown beyond 16 GiB.

Apply this preparation only to plain tables, including legacy physical tables without PLAIN_XATTR_PFX. Remap the requested inode digest prefix before checking its identity. Ordinary incremental builds inherit an omitted digest setting and may enable digests for newly built files, but cannot rename an existing digest.

Build temporary prefix lists and decoding arrays before replacing the old state. Release the old array using its old count. Prepare prefixes before shared-xattr scanning and retain the existing prefix flush call order. Document the changed incremental behavior.

---
 include/erofs/xattr.h |   9 ++
 lib/xattr.c           | 188 +++++++++++++++++++++++++++++++++++++++++-
 man/mkfs.erofs.1      |  18 ++++
 mkfs/main.c           |  13 +++
 4 files changed, 226 insertions(+), 2 deletions(-)
include/erofs/xattr.h +9 -0
index d12c292..fb960d2 100644
@@ -33,6 +33,15 @@ char *erofs_export_xattr_ibody(struct erofs_inode *inode);
 int erofs_load_shared_xattrs_from_path(struct erofs_sb_info *sbi, const char *path,
 				       long inlinexattr_tolerance);
 int erofs_xattr_insert_name_prefix(const char *prefix);
+/*
+ * Merge base prefixes for incremental plain tables; other paths are unchanged.
+ * plain selects storage for a new table, not conversion of an existing one.
+ * ishare_prefix_id is the CLI digest ID (0 if omitted), remapped on success;
+ * sbi holds the resulting digest configuration, including inherited settings.
+ */
+int erofs_xattr_prepare_name_prefixes(struct erofs_importer *im,
+				     bool incremental, bool plain,
+				     u8 *ishare_prefix_id);
 int erofs_xattr_set_ishare_prefix(struct erofs_sb_info *sbi,
 				  const char *prefix);
 char *erofs_xattr_get_ishare_prefix(struct erofs_sb_info *sbi);
lib/xattr.c +186 -2

221 lines not on this page · raw message

man/mkfs.erofs.1 +18 -0
index 65ec807..96340ad 100644
@@ -460,12 +460,30 @@ Set the number of worker threads to # (default: number of CPUs).
 .TP
 .BI "\-\-xattr-inode-digest=" name
 Specify extended attribute name to record inode digests.
+
+For incremental builds using plain on-disk xattr prefixes, an existing digest
+name is inherited when this option is omitted.  Specifying a different
+digest name from the base image is rejected.  Enabling digests for the first
+time does not add them to inherited files that are not rebuilt.
 .TP
 .BI "\-\-xattr-prefix=" PREFIX
 Specify a customized extended attribute namespace prefix for space saving,
 e.g. "trusted.overlay.".  You may give multiple
 .B --xattr-prefix
 options (Linux 6.4+).
+
+For incremental builds using a plain on-disk prefix table, these options
+supplement the base image's table.  Existing entries retain their order and
+prefix IDs, including duplicate entries.  Prefixes already present in the
+table do not add entries; new distinct prefixes are appended in command-line
+order.  If no entries are added, the existing table is reused at its original
+location.
+
+Inherited prefixes are also available when encoding xattrs for newly built
+inodes, without repeating these options.  The first matching table entry is
+used: inherited entries are considered in their original order, followed by
+new entries in command-line order.  A later, longer matching prefix does not
+take precedence over an earlier match.
 .TP
 .BI "\-\-zfeature-bits=" #
 Toggle filesystem compression features according to given bits #.
mkfs/main.c +13 -0
index f75225b..8311686 100644
@@ -1770,6 +1770,7 @@ int main(int argc, char **argv)
 	};
 	struct erofs_inode *root = NULL;
 	bool tar_index_512b = false;
+	u8 cli_ishare_prefix_id;
 	struct timeval t;
 	FILE *blklst = NULL;
 	int err;
@@ -1803,6 +1804,9 @@ int main(int argc, char **argv)
 	else
 		importer_params.build_time = 0;
 
+	/* The base superblock may overwrite the requested digest prefix ID. */
+	cli_ishare_prefix_id = g_sbi.ishare_xattr_prefix_id;
+
 	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
 				(incremental_mode ? 0 : O_TRUNC));
 	if (err) {
@@ -1870,6 +1874,15 @@ int main(int argc, char **argv)
 	if (err)
 		goto exit;
 
+	err = erofs_xattr_prepare_name_prefixes(&importer, incremental_mode,
+					      mkfs_plain_xattr_pfx,
+					      &cli_ishare_prefix_id);
+	if (err) {
+		erofs_err("failed to prepare long xattr prefixes: %s",
+			  erofs_strerror(err));
+		goto exit;
+	}
+
 	/* Use the user-defined UUID or generate one for clean builds */
 	if (valid_fixeduuid)
 		memcpy(g_sbi.uuid, fixeduuid, sizeof(g_sbi.uuid));
Signature · 1 line
2.53.0.apsara
#3 lore raw

PATCH 3/3 erofs-utils: mkfs: add opt-in plain xattr prefix reservations

In reply to bingying lan

Add --xattr-prefix-reserve to allocate a fixed 64 KiB region for plain xattr prefixes, including empty tables. Record the reservation with a compat feature bit and the existing prefix address, without changing record encoding or adding address and capacity fields.

Use existing reservations in later incremental builds and append only new prefix records. Explicitly requesting a reservation on an unreserved image relocates the merged table even when its entries are unchanged. If the new start exceeds the address limit, warn and reuse an unchanged table; reject the operation when new entries require a new table.

Keep reservation opt-in, preserve the original record write lengths, and leave packed and metabox tables on their existing paths. Document the allocation overhead, compatibility constraints and rebuild fallback.

---
 include/erofs/internal.h |   1 +
 include/erofs/xattr.h    |   5 +-
 include/erofs_fs.h       |   2 +
 lib/xattr.c              | 124 +++++++++++++++++++++++++++++++--------
 man/mkfs.erofs.1         |  33 ++++++++++-
 mkfs/main.c              |  27 ++++++++-
 6 files changed, 165 insertions(+), 27 deletions(-)
include/erofs/internal.h +1 -0
index 19e38fb..b46136f 100644
@@ -194,6 +194,7 @@ EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
 EROFS_FEATURE_FUNCS(shared_ea_in_metabox, compat, COMPAT_SHARED_EA_IN_METABOX)
 EROFS_FEATURE_FUNCS(plain_xattr_pfx, compat, COMPAT_PLAIN_XATTR_PFX)
 EROFS_FEATURE_FUNCS(ishare_xattrs, compat, COMPAT_ISHARE_XATTRS)
+EROFS_FEATURE_FUNCS(xattr_prefix_reserved, compat, COMPAT_XATTR_PREFIX_RESERVED)
 
 #define EROFS_I_EA_INITED_BIT	0
 #define EROFS_I_Z_INITED_BIT	1
include/erofs/xattr.h +4 -1
index fb960d2..ef8a69b 100644
@@ -9,6 +9,8 @@ extern "C"
 
 #include "internal.h"
 
+#define EROFS_XATTR_PREFIX_RESERVE_SIZE	65536
+
 static inline unsigned int inlinexattr_header_size(struct erofs_inode *vi)
 {
 	return sizeof(struct erofs_xattr_ibody_header) +
@@ -36,11 +38,12 @@ int erofs_xattr_insert_name_prefix(const char *prefix);
 /*
  * Merge base prefixes for incremental plain tables; other paths are unchanged.
  * plain selects storage for a new table, not conversion of an existing one.
+ * reserve requests a fixed-size reservation and requires plain storage.
  * ishare_prefix_id is the CLI digest ID (0 if omitted), remapped on success;
  * sbi holds the resulting digest configuration, including inherited settings.
  */
 int erofs_xattr_prepare_name_prefixes(struct erofs_importer *im,
-				     bool incremental, bool plain,
+				     bool incremental, bool plain, bool reserve,
 				     u8 *ishare_prefix_id);
 int erofs_xattr_set_ishare_prefix(struct erofs_sb_info *sbi,
 				  const char *prefix);
include/erofs_fs.h +2 -0
index 5d12049..ae6f3e5 100644
@@ -19,6 +19,8 @@
 #define EROFS_FEATURE_COMPAT_SHARED_EA_IN_METABOX	0x00000008
 #define EROFS_FEATURE_COMPAT_PLAIN_XATTR_PFX		0x00000010
 #define EROFS_FEATURE_COMPAT_ISHARE_XATTRS		0x00000020
+/* 64 KiB at xattr_prefix_start * 4 is reserved for plain xattr prefixes. */
+#define EROFS_FEATURE_COMPAT_XATTR_PREFIX_RESERVED	0x00000040
 
 /*
  * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
lib/xattr.c +101 -23

230 lines not on this page · raw message

man/mkfs.erofs.1 +32 -1
index 96340ad..9bf50cb 100644
@@ -477,7 +477,7 @@ supplement the base image's table.  Existing entries retain their order and
 prefix IDs, including duplicate entries.  Prefixes already present in the
 table do not add entries; new distinct prefixes are appended in command-line
 order.  If no entries are added, the existing table is reused at its original
-location.
+location, unless \fB\-\-xattr-prefix-reserve\fR requests a new reservation.
 
 Inherited prefixes are also available when encoding xattrs for newly built
 inodes, without repeating these options.  The first matching table entry is
@@ -485,6 +485,37 @@ used: inherited entries are considered in their original order, followed by
 new entries in command-line order.  A later, longer matching prefix does not
 take precedence over an earlier match.
 .TP
+.B \-\-xattr-prefix-reserve
+Reserve a fixed 64 KiB region for a plain on-disk xattr prefix table,
+including an empty table.  This option is disabled by default.  The region
+starts at a filesystem block boundary, after the superblock and necessary
+configuration data in a full build.  Its allocation and alignment increase
+the image size.  The capacity accommodates the current format's maximum
+128 entries of up to 260 bytes each, including alignment.
+
+For incremental builds, an existing reservation is used even when this
+option is omitted.  Unchanged tables are not rewritten; new entries are
+appended within the reservation without changing old entries or their IDs.
+Specifying this option for an unreserved plain table allocates a reservation
+in the appended area and copies the merged table there, even if the table
+is unchanged.  Repeating the option does not allocate another reservation.
+
+Only the reservation's start must fit in the existing 32-bit, four-byte-unit
+offset; the region may extend beyond 16 GiB.  If an incremental build cannot
+represent the new start and no entries were added, it warns and reuses the
+old table (or keeps an empty table), without reserving space.  If entries
+were added, it fails.  To establish a reservation in this case, rebuild a
+new image from complete contents with this option; a partial incremental
+source is insufficient.
+
+Packed inode and metabox prefix tables are not supported by this option
+and are not automatically converted.  For a new table, use
+\fB\-E plain-xattr-prefixes\fR if necessary to select plain storage.
+The reservation uses a compat feature bit without changing prefix records
+or offsets; readers supporting the existing plain table format may ignore
+the bit.  Mixing older writing tools is not supported: they may invalidate
+the reservation while preserving its flag.
+.TP
 .BI "\-\-zfeature-bits=" #
 Toggle filesystem compression features according to given bits #.
 Each bit in the value corresponds to a specific compression feature:
mkfs/main.c +25 -2
index 8311686..78b8142 100644
@@ -105,6 +105,7 @@ static struct option long_options[] = {
 	{"MZ", optional_argument, NULL, 537},
 	{"xattr-prefix", required_argument, NULL, 538},
 	{"xattr-inode-digest", optional_argument, NULL, 539},
+	{"xattr-prefix-reserve", no_argument, NULL, 540},
 	{0, 0, 0, 0},
 };
 
@@ -260,6 +261,7 @@ static void usage(int argc, char **argv)
 #endif
 		" --xattr-inode-digest=X specify extended attribute name X (\"" EROFS_EA_INODE_DIGEST_DEFAULT "\" if omitted) to record inode digests\n"
 		" --xattr-prefix=X       X=extra xattr name prefix\n"
+		" --xattr-prefix-reserve reserve 64 KiB for a plain xattr prefix table\n"
 		" --zfeature-bits=#      toggle filesystem compression features according to given bits #\n"
 #ifdef WITH_ANDROID
 		"\n"
@@ -488,6 +490,7 @@ static struct {
 
 static bool mkfs_no_datainline;
 static bool mkfs_plain_xattr_pfx;
+static bool mkfs_xattr_prefix_reserve;
 
 static int parse_extended_opts(struct erofs_importer_params *params,
 			       const char *opts)
@@ -1497,6 +1500,9 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 				return err;
 			}
 			break;
+		case 540:
+			mkfs_xattr_prefix_reserve = true;
+			break;
 		case 'V':
 			version();
 			exit(0);
@@ -1770,6 +1776,7 @@ int main(int argc, char **argv)
 	};
 	struct erofs_inode *root = NULL;
 	bool tar_index_512b = false;
+	bool early_xattr_prefixes;
 	u8 cli_ishare_prefix_id;
 	struct timeval t;
 	FILE *blklst = NULL;
@@ -1876,6 +1883,7 @@ int main(int argc, char **argv)
 
 	err = erofs_xattr_prepare_name_prefixes(&importer, incremental_mode,
 					      mkfs_plain_xattr_pfx,
+					      mkfs_xattr_prefix_reserve,
 					      &cli_ishare_prefix_id);
 	if (err) {
 		erofs_err("failed to prepare long xattr prefixes: %s",
@@ -1962,6 +1970,19 @@ int main(int argc, char **argv)
 			goto exit;
 	}
 
+	/* Reserve after mandatory configs, before shared xattrs and file data. */
+	early_xattr_prefixes = mkfs_xattr_prefix_reserve ||
+			       erofs_sb_has_xattr_prefix_reserved(&g_sbi);
+	if (early_xattr_prefixes) {
+		err = erofs_xattr_flush_name_prefixes(&importer,
+						    mkfs_plain_xattr_pfx);
+		if (err) {
+			erofs_err("failed to flush long xattr prefixes: %s",
+				  erofs_strerror(err));
+			goto exit;
+		}
+	}
+
 	if (source_mode == EROFS_MKFS_SOURCE_LOCALDIR) {
 		err = erofs_load_shared_xattrs_from_path(&g_sbi, cfg.c_src_path,
 						mkfscfg.inlinexattr_tolerance);
@@ -1971,7 +1992,8 @@ int main(int argc, char **argv)
 			goto exit;
 		}
 
-		err = erofs_xattr_flush_name_prefixes(&importer,
+		if (!early_xattr_prefixes)
+			err = erofs_xattr_flush_name_prefixes(&importer,
 						      mkfs_plain_xattr_pfx);
 		if (err) {
 			erofs_err("failed to flush long xattr prefixes: %s",
@@ -1985,7 +2007,8 @@ int main(int argc, char **argv)
 			goto exit;
 		}
 	} else {
-		err = erofs_xattr_flush_name_prefixes(&importer,
+		if (!early_xattr_prefixes)
+			err = erofs_xattr_flush_name_prefixes(&importer,
 						      mkfs_plain_xattr_pfx);
 		if (err) {
 			erofs_err("failed to flush long xattr prefixes: %s",
Signature · 1 line
2.53.0.apsara