Skip to contentSkip to conversation

erofs-utils: mkfs: support flatdev for multi-blob images

PATCH Superseded Jingbo Xu 2 participants 3 messages Sep 15, 2023 Library code

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jingbo Xu

author
#0 lore raw

PATCH 1/1 erofs-utils: mkfs: support flatdev for multi-blob images

Since kernel commit 8b465fecc35a ("erofs: support flattened block device for multi-blob images"), flatdev feature is introduced to support for mounting multi-blobs container image as a single block device.

Introduce "-Eflatdev" option to generate images in flatdev mode. Currently this can only work with rebuild mode, where the device tag is filled with the uuid of the corresponding source image.

---
 include/erofs/config.h   |  1 +
 include/erofs/internal.h |  1 +
 lib/blobchunk.c          | 15 +++++++++++++--
 lib/super.c              |  1 +
 mkfs/main.c              | 26 ++++++++++++++++++++++----
 5 files changed, 38 insertions(+), 6 deletions(-)
include/erofs/config.h +1 -0
index e342722..95c6aab 100644
@@ -55,6 +55,7 @@ struct erofs_configure {
 	bool c_extra_ea_name_prefixes;
 	bool c_xattr_name_filter;
 	bool c_ovlfs_strip;
+	bool c_flatdev;
 
 #ifdef HAVE_LIBSELINUX
 	struct selabel_handle *sehnd;
include/erofs/internal.h +1 -0
index 19b912b..616cd3a 100644
@@ -54,6 +54,7 @@ extern struct erofs_sb_info sbi;
 struct erofs_buffer_head;
 
 struct erofs_device_info {
+	u8 tag[64];
 	u32 blocks;
 	u32 mapped_blkaddr;
 };
lib/blobchunk.c +13 -2
index aca616e..52ec438 100644
@@ -410,20 +410,31 @@ int erofs_mkfs_dump_blobs(struct erofs_sb_info *sbi)
 	}
 
 	if (sbi->extra_devices) {
-		unsigned int i;
+		unsigned int i, ret;
+		erofs_blk_t nblocks;
 
+		nblocks = erofs_mapbh(NULL);
 		pos_out = erofs_btell(bh_devt, false);
 		i = 0;
 		do {
 			struct erofs_deviceslot dis = {
 				.blocks = cpu_to_le32(sbi->devs[i].blocks),
 			};
-			int ret;
+
+			if (cfg.c_flatdev) {
+				dis.mapped_blkaddr = cpu_to_le32(nblocks);
+				if (!sbi->devs[i].tag[0]) {
+					erofs_err("empty device tag");
+					return -EINVAL;
+				}
+				memcpy(dis.tag, sbi->devs[i].tag, sizeof(dis.tag));
+			}
 
 			ret = dev_write(sbi, &dis, pos_out, sizeof(dis));
 			if (ret)
 				return ret;
 			pos_out += sizeof(dis);
+			nblocks += sbi->devs[i].blocks;
 		} while (++i < sbi->extra_devices);
 		bh_devt->op = &erofs_drop_directly_bhops;
 		erofs_bdrop(bh_devt, false);
lib/super.c +1 -0
index ce97278..f952f7e 100644
@@ -65,6 +65,7 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 
 		sbi->devs[i].mapped_blkaddr = le32_to_cpu(dis.mapped_blkaddr);
 		sbi->devs[i].blocks = le32_to_cpu(dis.blocks);
+		memcpy(sbi->devs[i].tag, dis.tag, sizeof(dis.tag));
 		sbi->total_blocks += sbi->devs[i].blocks;
 		pos += EROFS_DEVT_SLOT_SIZE;
 	}
mkfs/main.c +22 -4
index 4fa2d92..937a0f5 100644
@@ -249,6 +249,10 @@ handle_fragment:
 			if (vallen)
 				return -EINVAL;
 			cfg.c_xattr_name_filter = !clear;
+		} else if ((MATCH_EXTENTED_OPT("flatdev", token, keylen))) {
+			if (vallen)
+				return -EINVAL;
+			cfg.c_flatdev = !clear;
 		} else {
 			erofs_err("unknown extended option %.*s",
 				  p - token, token);
@@ -620,6 +624,11 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		cfg.c_showprogress = false;
 	}
 
+	if (cfg.c_flatdev && !rebuild_mode) {
+		erofs_err("--flatdev is supported only in rebuild mode currently");
+		return -EINVAL;
+	}
+
 	if (cfg.c_compr_alg[0] && erofs_blksiz(&sbi) != getpagesize())
 		erofs_warn("Please note that subpage blocksize with compression isn't yet supported in kernel. "
 			   "This compressed image will only work with bs = ps = %u bytes",
@@ -765,8 +774,15 @@ static void erofs_mkfs_default_options(void)
 	sbi.feature_compat = EROFS_FEATURE_COMPAT_SB_CHKSUM |
 			     EROFS_FEATURE_COMPAT_MTIME;
 
-	/* generate a default uuid first */
-	erofs_uuid_generate(sbi.uuid);
+	/*
+	 * Generate a default uuid first.  In rebuild mode the uuid of the
+	 * source image is used as the device slot's tag.  The kernel will
+	 * identify the tag as empty and fail the mount if its first byte is
+	 * zero.  Apply this constraint to uuid to work around it.
+	 */
+	do {
+		erofs_uuid_generate(sbi.uuid);
+	} while (!sbi.uuid[0]);
 }
 
 /* https://reproducible-builds.org/specs/source-date-epoch/ for more details */
@@ -822,7 +838,7 @@ static int erofs_rebuild_load_trees(struct erofs_inode *root)
 	struct erofs_sb_info *src;
 	unsigned int extra_devices = 0;
 	erofs_blk_t nblocks;
-	int ret;
+	int ret, idx;
 
 	list_for_each_entry(src, &rebuild_src_list, list) {
 		ret = erofs_rebuild_load_tree(root, src);
@@ -854,7 +870,9 @@ static int erofs_rebuild_load_trees(struct erofs_inode *root)
 		else
 			nblocks = src->primarydevice_blocks;
 		DBG_BUGON(src->dev < 1);
-		sbi.devs[src->dev - 1].blocks = nblocks;
+		idx = src->dev - 1;
+		sbi.devs[idx].blocks = nblocks;
+		memcpy(sbi.devs[idx].tag, src->uuid, sizeof(src->uuid));
 	}
 	return 0;
 }
Signature · 1 line
2.19.1.6.gb485710b
#1 lore raw
Quoted 5 lines · Jingbo Xu

Since kernel commit 8b465fecc35a ("erofs: support flattened block device for multi-blob images"), flatdev feature is introduced to support for mounting multi-blobs container image as a single block device.

Introduce "-Eflatdev" option to generate images in flatdev mode.

Why need introducing such an option

mapped_blkaddr cannot be filled/fixed up all the time?

Quoted 2 lines

Currently this can only work with rebuild mode, where the device tag is filled with the uuid of the corresponding source image.

...

mkfs/main.c static int mkfs_parse_options_cfg(int argc, char *argv[]) · line 628

Quoted 2 lines
mkfs/main.c +2 -0
+	if (cfg.c_flatdev && !rebuild_mode) {
+		erofs_err("--flatdev is supported only in rebuild mode currently");

I don't think we need this, all multiple device mode can use this.

Quoted 11 lines
mkfs/main.c +3 -1
+		return -EINVAL;
+	}
+
  	if (cfg.c_compr_alg[0] && erofs_blksiz(&sbi) != getpagesize())
  		erofs_warn("Please note that subpage blocksize with compression isn't yet supported in kernel. "
  			   "This compressed image will only work with bs = ps = %u bytes",
@@ -765,8 +774,15 @@ static void erofs_mkfs_default_options(void)
  	sbi.feature_compat = EROFS_FEATURE_COMPAT_SB_CHKSUM |
  			     EROFS_FEATURE_COMPAT_MTIME;
 
-	/* generate a default uuid first */

mkfs/main.c static void erofs_mkfs_default_options(void) · line 785

Quoted 10 lines
mkfs/main.c +9 -1
-	erofs_uuid_generate(sbi.uuid);
+	/*
+	 * Generate a default uuid first.  In rebuild mode the uuid of the
+	 * source image is used as the device slot's tag.  The kernel will
+	 * identify the tag as empty and fail the mount if its first byte is
+	 * zero.  Apply this constraint to uuid to work around it.
+	 */
+	do {
+		erofs_uuid_generate(sbi.uuid);
+	} while (!sbi.uuid[0]);

why need a loop here?

Review static void erofs_mkfs_default_options(void) · line 786

Quoted 1 line
(quoted diff) +0 -0
  }

Thanks,
Gao Xiang

author
#2 lore raw
Quoted 7 lines · Gao Xiang
Quoted 4 lines · Jingbo Xu

Since kernel commit 8b465fecc35a ("erofs: support flattened block device for multi-blob images"), flatdev feature is introduced to support for mounting multi-blobs container image as a single block device.

Quoted 5 lines
Quoted 1 line

Introduce "-Eflatdev" option to generate images in flatdev mode.

Why need introducing such an option

mapped_blkaddr cannot be filled/fixed up all the time?

Okay. Will be updated in the next version.

Quoted 30 lines
Quoted 3 lines

Currently this can only work with rebuild mode, where the device tag is filled with the uuid of the corresponding source image.

...

Quoted 3 lines

+    if (cfg.c_flatdev && !rebuild_mode) {
+        erofs_err("--flatdev is supported only in rebuild mode currently");

I don't think we need this, all multiple device mode can use this.

Quoted 16 lines

+        return -EINVAL;
+    }
+
      if (cfg.c_compr_alg[0] && erofs_blksiz(&sbi) != getpagesize()) erofs_warn("Please note that subpage blocksize with compression isn't yet supported in kernel. "
                 "This compressed image will only work with bs = ps = %u bytes",

(quoted diff) +0 -0
@@ -765,8 +774,15 @@ static void erofs_mkfs_default_options(void)

      sbi.feature_compat = EROFS_FEATURE_COMPAT_SB_CHKSUM | EROFS_FEATURE_COMPAT_MTIME;
  -    /* generate a default uuid first */
-    erofs_uuid_generate(sbi.uuid);
+    /*
+     * Generate a default uuid first.  In rebuild mode the uuid of the +     * source image is used as the device slot's tag.  The kernel will

Quoted 8 lines
Quoted 6 lines

+     * identify the tag as empty and fail the mount if its first byte is +     * zero.  Apply this constraint to uuid to work around it. +     */
+    do {
+        erofs_uuid_generate(sbi.uuid);
+    } while (!sbi.uuid[0]);

why need a loop here?

To ensure the first byte of the device tag (i.e. the uuid of the source image) is not zero, so that it won't block the mounting in flatdev mode.

Signature · 2 lines
Thanks,
Jingbo