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

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jingbo Xu

author
#0 lore raw

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

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

To enable this feature, the mapped_blkaddr of each device slot needs to be set properly to the offset of the device in the flat address space.

The kernel side requires a non-empty device tag to mount an image in flatdev mode. The uuid of the source image is used as the corresponding device tag in rebuild mode.

---
v2: drop "-Eflatdev" option; always set mapped_blkaddr in device slot

---
 include/erofs/internal.h |  1 +
 lib/blobchunk.c          |  8 ++++++--
 lib/super.c              |  1 +
 mkfs/main.c              | 17 +++++++++++++----
 4 files changed, 21 insertions(+), 6 deletions(-)
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 +6 -2
index aca616e..a599f3a 100644
@@ -410,20 +410,24 @@ 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 = {
+				.mapped_blkaddr = cpu_to_le32(nblocks),
 				.blocks = cpu_to_le32(sbi->devs[i].blocks),
 			};
-			int ret;
 
+			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 +13 -4
index 4fa2d92..9327b6f 100644
@@ -765,8 +765,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 +829,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 +861,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 85 lines · Jingbo Xu

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

To enable this feature, the mapped_blkaddr of each device slot needs to be set properly to the offset of the device in the flat address space.

The kernel side requires a non-empty device tag to mount an image in flatdev mode. The uuid of the source image is used as the corresponding device tag in rebuild mode.

---
v2: drop "-Eflatdev" option; always set mapped_blkaddr in device slot

---
  include/erofs/internal.h |  1 +
  lib/blobchunk.c          |  8 ++++++--
  lib/super.c              |  1 +
  mkfs/main.c              | 17 +++++++++++++----
  4 files changed, 21 insertions(+), 6 deletions(-)
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 +6 -2
index aca616e..a599f3a 100644
@@ -410,20 +410,24 @@ 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 = {
+				.mapped_blkaddr = cpu_to_le32(nblocks),
  				.blocks = cpu_to_le32(sbi->devs[i].blocks),
  			};
-			int ret;
 
+			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 +0 -1
index 4fa2d92..9327b6f 100644
@@ -765,8 +765,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 776

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]);

I don't think we need this, since uuid need to be parsed to string.

Quoted 13 lines
mkfs/main.c +1 -1
  }
 
  /* https://reproducible-builds.org/specs/source-date-epoch/ for more details */
@@ -822,7 +829,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);

mkfs/main.c static int erofs_rebuild_load_trees(struct erofs_inode *root) · line 866

Quoted 7 lines
mkfs/main.c +3 -1
@@ -854,7 +861,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));

Should we use string conversion instead? Should we use uuid only if sbi.devs[idx].tag is empty?

Thanks,
Gao Xiang

author
#2 lore raw
Quoted 89 lines · Gao Xiang
Quoted 86 lines · Jingbo Xu

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

To enable this feature, the mapped_blkaddr of each device slot needs to be set properly to the offset of the device in the flat address space.

The kernel side requires a non-empty device tag to mount an image in flatdev mode.  The uuid of the source image is used as the corresponding device tag in rebuild mode.

---
v2: drop "-Eflatdev" option; always set mapped_blkaddr in device slot
---
  include/erofs/internal.h |  1 +
  lib/blobchunk.c          |  8 ++++++--
  lib/super.c              |  1 +
  mkfs/main.c              | 17 +++++++++++++----

  4 files changed, 21 insertions(+), 6 deletions(-)
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 +4 -2
index aca616e..a599f3a 100644
@@ -410,20 +410,24 @@ 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 = {
+                .mapped_blkaddr = cpu_to_le32(nblocks),
                   .blocks = cpu_to_le32(sbi->devs[i].blocks),
               };
-            int ret;
   +            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 +0 -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 +0 -0
index 4fa2d92..9327b6f 100644
@@ -765,8 +765,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]);

I don't think we need this, since uuid need to be parsed to string.

Alright.

Quoted 17 lines
Quoted 17 lines

  }
    /* https://reproducible-builds.org/specs/source-date-epoch/ for more details */

mkfs/main.c +1 -1
@@ -822,7 +829,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 +861,9 @@ static int erofs_rebuild_load_trees(struct
 erofs_inode *root)

          else
              nblocks = src->primarydevice_blocks;

Quoted 8 lines
Quoted 5 lines

          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));

Should we use string conversion instead? Should we use uuid only if sbi.devs[idx].tag is empty?

sbi.devs[idx].tag is always empty as sbi.devs[] is newly allocated from calloc().

Signature · 2 lines
Thanks,
Jingbo