Skip to contentSkip to conversation

Introduce EROFS support

PATCH Superseded Gao Xiang 2 participants 5 messages May 17, 2024 Kbuild

v12 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

v12 cover Introduce EROFS support

Hi folks,

EROFS [1] is a lightweight read-only filesystem designed for performance which has already been shipped in most Linux distributions as well as widely used in several scenarios, such as Android system partitions, container images, and rootfs for embedded devices.

This patch brings EROFS uncompressed support together with related tests. Now, it's possible to boot directly through GRUB with an EROFS rootfs.

EROFS compressed files will be supported later since it has more work to polish.

[1] https://erofs.docs.kernel.org

changelog since v11:
- Address nits mentioned by Daniel for better reading:
https://lore.kernel.org/grub-devel/[email protected]/
- Simplify erofs_read_symlink() since erofs_inode_file_size() is checked against EROFS_PATH_LEN(4096) first.

Tested-by Link (Commit 1): https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00001.html
Reviewed-by Link (Commit 2): https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00101.html
Reviewed-by Link (Commit 2): https://lore.kernel.org/grub-devel/[email protected]/

Yifan Zhao (2):
fs/erofs: Add support for EROFS
fs/erofs: Add tests for EROFS in grub-fs-tester

 .gitignore                   |    1 +
 INSTALL                      |    8 +-
 Makefile.util.def            |    7 +
 docs/grub.texi               |    3 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/erofs.c         | 1002 ++++++++++++++++++++++++++++++++++
 tests/erofs_test.in          |   20 +
 tests/util/grub-fs-tester.in |   32 +-
 8 files changed, 1066 insertions(+), 12 deletions(-)
 create mode 100644 grub-core/fs/erofs.c
 create mode 100644 tests/erofs_test.in

Interdiff against v11:

grub-core/fs/erofs.c +135 -141
index 14c86f435..630f95e75 100644
@@ -39,37 +39,37 @@ GRUB_MOD_LICENSE ("GPLv3+");
 
 struct grub_erofs_super
 {
-  grub_uint32_t magic;
-  grub_uint32_t checksum;
-  grub_uint32_t feature_compat;
-  grub_uint8_t log2_blksz;
-  grub_uint8_t sb_extslots;
-
-  grub_uint16_t root_nid;
-  grub_uint64_t inos;
-
-  grub_uint64_t build_time;
-  grub_uint32_t build_time_nsec;
-  grub_uint32_t blocks;
-  grub_uint32_t meta_blkaddr;
-  grub_uint32_t xattr_blkaddr;
-  grub_packed_guid_t uuid;
-  grub_uint8_t volume_name[16];
-  grub_uint32_t feature_incompat;
+  grub_uint32_t		magic;
+  grub_uint32_t		checksum;
+  grub_uint32_t		feature_compat;
+  grub_uint8_t		log2_blksz;
+  grub_uint8_t		sb_extslots;
+
+  grub_uint16_t		root_nid;
+  grub_uint64_t		inos;
+
+  grub_uint64_t		build_time;
+  grub_uint32_t		build_time_nsec;
+  grub_uint32_t		blocks;
+  grub_uint32_t		meta_blkaddr;
+  grub_uint32_t		xattr_blkaddr;
+  grub_packed_guid_t	uuid;
+  grub_uint8_t		volume_name[16];
+  grub_uint32_t		feature_incompat;
 
   union
   {
-    grub_uint16_t available_compr_algs;
-    grub_uint16_t lz4_max_distance;
+    grub_uint16_t	available_compr_algs;
+    grub_uint16_t	lz4_max_distance;
   } GRUB_PACKED u1;
 
-  grub_uint16_t extra_devices;
-  grub_uint16_t devt_slotoff;
-  grub_uint8_t log2_dirblksz;
-  grub_uint8_t xattr_prefix_count;
-  grub_uint32_t xattr_prefix_start;
-  grub_uint64_t packed_nid;
-  grub_uint8_t reserved2[24];
+  grub_uint16_t		extra_devices;
+  grub_uint16_t		devt_slotoff;
+  grub_uint8_t		log2_dirblksz;
+  grub_uint8_t		xattr_prefix_count;
+  grub_uint32_t		xattr_prefix_start;
+  grub_uint64_t		packed_nid;
+  grub_uint8_t		reserved2[24];
 } GRUB_PACKED;
 
 #define EROFS_INODE_LAYOUT_COMPACT	0
@@ -89,8 +89,8 @@ struct grub_erofs_super
 
 struct grub_erofs_inode_chunk_info
 {
-  grub_uint16_t format;
-  grub_uint16_t reserved;
+  grub_uint16_t		format;
+  grub_uint16_t		reserved;
 } GRUB_PACKED;
 
 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK	0x001F
@@ -107,64 +107,64 @@ struct grub_erofs_inode_chunk_info
 
 struct grub_erofs_inode_chunk_index
 {
-  grub_uint16_t advise;
-  grub_uint16_t device_id;
-  grub_uint32_t blkaddr;
+  grub_uint16_t		advise;
+  grub_uint16_t		device_id;
+  grub_uint32_t		blkaddr;
 };
 
 union grub_erofs_inode_i_u
 {
-  grub_uint32_t compressed_blocks;
-  grub_uint32_t raw_blkaddr;
+  grub_uint32_t				compressed_blocks;
+  grub_uint32_t				raw_blkaddr;
 
-  grub_uint32_t rdev;
+  grub_uint32_t				rdev;
 
-  struct grub_erofs_inode_chunk_info c;
+  struct grub_erofs_inode_chunk_info	c;
 };
 
 struct grub_erofs_inode_compact
 {
-  grub_uint16_t i_format;
+  grub_uint16_t			i_format;
 
-  grub_uint16_t i_xattr_icount;
-  grub_uint16_t i_mode;
-  grub_uint16_t i_nlink;
-  grub_uint32_t i_size;
-  grub_uint32_t i_reserved;
+  grub_uint16_t			i_xattr_icount;
+  grub_uint16_t			i_mode;
+  grub_uint16_t			i_nlink;
+  grub_uint32_t			i_size;
+  grub_uint32_t			i_reserved;
 
-  union grub_erofs_inode_i_u i_u;
+  union grub_erofs_inode_i_u	i_u;
 
-  grub_uint32_t i_ino;
-  grub_uint16_t i_uid;
-  grub_uint16_t i_gid;
-  grub_uint32_t i_reserved2;
+  grub_uint32_t			i_ino;
+  grub_uint16_t			i_uid;
+  grub_uint16_t			i_gid;
+  grub_uint32_t			i_reserved2;
 } GRUB_PACKED;
 
 struct grub_erofs_inode_extended
 {
-  grub_uint16_t i_format;
+  grub_uint16_t			i_format;
 
-  grub_uint16_t i_xattr_icount;
-  grub_uint16_t i_mode;
-  grub_uint16_t i_reserved;
-  grub_uint64_t i_size;
+  grub_uint16_t			i_xattr_icount;
+  grub_uint16_t			i_mode;
+  grub_uint16_t			i_reserved;
+  grub_uint64_t			i_size;
 
-  union grub_erofs_inode_i_u i_u;
+  union grub_erofs_inode_i_u	i_u;
 
-  grub_uint32_t i_ino;
+  grub_uint32_t			i_ino;
 
-  grub_uint32_t i_uid;
-  grub_uint32_t i_gid;
-  grub_uint64_t i_mtime;
-  grub_uint32_t i_mtime_nsec;
-  grub_uint32_t i_nlink;
-  grub_uint8_t i_reserved2[16];
+  grub_uint32_t			i_uid;
+  grub_uint32_t			i_gid;
+  grub_uint64_t			i_mtime;
+  grub_uint32_t			i_mtime_nsec;
+  grub_uint32_t			i_nlink;
+  grub_uint8_t			i_reserved2[16];
 } GRUB_PACKED;
 
 union grub_erofs_inode
 {
-  struct grub_erofs_inode_compact c;
-  struct grub_erofs_inode_extended e;
+  struct grub_erofs_inode_compact	c;
+  struct grub_erofs_inode_extended	e;
 } GRUB_PACKED;
 
 #define EROFS_FT_UNKNOWN	0
@@ -178,48 +178,48 @@ union grub_erofs_inode
 
 struct grub_erofs_dirent
 {
-  grub_uint64_t nid;
-  grub_uint16_t nameoff;
-  grub_uint8_t file_type;
-  grub_uint8_t reserved;
+  grub_uint64_t		nid;
+  grub_uint16_t		nameoff;
+  grub_uint8_t		file_type;
+  grub_uint8_t		reserved;
 } GRUB_PACKED;
 
 struct grub_erofs_map_blocks
 {
-  grub_uint64_t m_pa;    /* physical address */
-  grub_uint64_t m_la;    /* logical address */
-  grub_uint64_t m_plen;  /* physical length */
-  grub_uint64_t m_llen;  /* logical length */
-  grub_uint32_t m_flags;
+  grub_uint64_t		m_pa;    /* physical address */
+  grub_uint64_t		m_la;    /* logical address */
+  grub_uint64_t		m_plen;  /* physical length */
+  grub_uint64_t		m_llen;  /* logical length */
+  grub_uint32_t		m_flags;
 };
 
 struct grub_erofs_xattr_ibody_header
 {
-  grub_uint32_t h_reserved;
-  grub_uint8_t h_shared_count;
-  grub_uint8_t h_reserved2[7];
-  grub_uint32_t h_shared_xattrs[0];
+  grub_uint32_t		h_reserved;
+  grub_uint8_t		h_shared_count;
+  grub_uint8_t		h_reserved2[7];
+  grub_uint32_t		h_shared_xattrs[0];
 };
 
 struct grub_fshelp_node
 {
-  struct grub_erofs_data *data;
-  union grub_erofs_inode inode;
+  struct grub_erofs_data	*data;
+  union grub_erofs_inode	inode;
 
-  grub_uint64_t ino;
-  grub_uint8_t inode_type;
-  grub_uint8_t inode_datalayout;
+  grub_uint64_t			ino;
+  grub_uint8_t			inode_type;
+  grub_uint8_t			inode_datalayout;
 
-  /* if the inode has been read into memory? */
-  bool inode_loaded;
+  /* If the inode has been read into memory? */
+  bool				inode_loaded;
 };
 
 struct grub_erofs_data
 {
-  grub_disk_t disk;
-  struct grub_erofs_super sb;
+  grub_disk_t			disk;
+  struct grub_erofs_super	sb;
 
-  struct grub_fshelp_node inode;
+  struct grub_fshelp_node	inode;
 };
 
 #define erofs_blocksz(data) (((grub_uint32_t) 1) << data->sb.log2_blksz)
@@ -271,11 +271,10 @@ erofs_read_inode (struct grub_erofs_data *data, grub_fshelp_node_t node)
     {
     case EROFS_INODE_LAYOUT_EXTENDED:
       addr += sizeof (struct grub_erofs_inode_compact);
-      err = grub_disk_read (
-	  data->disk, addr >> GRUB_DISK_SECTOR_BITS,
-	  addr & (GRUB_DISK_SECTOR_SIZE - 1),
-	  sizeof (struct grub_erofs_inode_extended) - sizeof (struct grub_erofs_inode_compact),
-	  (grub_uint8_t *) di + sizeof (struct grub_erofs_inode_compact));
+      err = grub_disk_read (data->disk, addr >> GRUB_DISK_SECTOR_BITS,
+                            addr & (GRUB_DISK_SECTOR_SIZE - 1),
+                            sizeof (struct grub_erofs_inode_extended) - sizeof (struct grub_erofs_inode_compact),
+                            (grub_uint8_t *) di + sizeof (struct grub_erofs_inode_compact));
       if (err != GRUB_ERR_NONE)
 	return err;
       break;
@@ -336,40 +335,38 @@ erofs_map_blocks_flatmode (grub_fshelp_node_t node,
   bool tailendpacking = (node->inode_datalayout == EROFS_INODE_FLAT_INLINE);
   grub_uint64_t blocksz = erofs_blocksz (node->data);
 
-  /* file_size is checked by caller and cannot be zero, hence nblocks > 0 */
+  /* `file_size` is checked by caller and cannot be zero, hence nblocks > 0. */
   file_size = erofs_inode_file_size (node);
   if (grub_add (file_size, blocksz - 1, &nblocks))
-    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "nblocks overflow");
   nblocks >>= node->data->sb.log2_blksz;
   lastblk = nblocks - tailendpacking;
 
   map->m_flags = EROFS_MAP_MAPPED;
 
-  /* no overflow as (lastblk <= nblocks) && (nblocks * blocksz <= UINT64_MAX - blocksz + 1) */
+  /* No overflow as (lastblk <= nblocks) && (nblocks * blocksz <= UINT64_MAX - blocksz + 1). */
   if (map->m_la < (lastblk * blocksz))
     {
-      if (grub_mul ((grub_uint64_t) grub_le_to_cpu32 (node->inode.e.i_u.raw_blkaddr), blocksz,
-		    &map->m_pa) ||
+      if (grub_mul ((grub_uint64_t) grub_le_to_cpu32 (node->inode.e.i_u.raw_blkaddr), blocksz, &map->m_pa) ||
 	  grub_add (map->m_pa, map->m_la, &map->m_pa))
-	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_pa overflow");
       if (grub_sub (lastblk * blocksz, map->m_la, &map->m_plen))
-	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_plen underflow");
     }
   else if (tailendpacking)
     {
       if (grub_add (erofs_iloc (node), erofs_inode_size (node), &map->m_pa) ||
 	  grub_add (map->m_pa, erofs_inode_xattr_ibody_size (node), &map->m_pa) ||
 	  grub_add (map->m_pa, map->m_la % blocksz, &map->m_pa))
-	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_pa overflow when handling tailpacking");
       if (grub_sub (file_size, map->m_la, &map->m_plen))
-	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_plen overflow when handling tailpacking");
 
-      /* no overflow as map->m_plen <= UINT64_MAX - blocksz + 1 */
+      /* No overflow as map->m_plen <= UINT64_MAX - blocksz + 1. */
       if (((map->m_pa % blocksz) + map->m_plen) > blocksz)
-	return grub_error (
-	    GRUB_ERR_BAD_FS,
-	    "inline data cross block boundary @ inode %" PRIuGRUB_UINT64_T,
-	    node->ino);
+	return grub_error (GRUB_ERR_BAD_FS,
+                           "inline data cross block boundary @ inode %" PRIuGRUB_UINT64_T,
+                           node->ino);
     }
   else
     return grub_error (GRUB_ERR_BAD_FS,
@@ -402,23 +399,25 @@ erofs_map_blocks_chunkmode (grub_fshelp_node_t node,
 
   chunknr = map->m_la >> chunkbits;
 
-  if (grub_add (erofs_iloc (node), erofs_inode_size (node), &pos) ||
-      grub_add (pos, erofs_inode_xattr_ibody_size (node), &pos))
-    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+  if (grub_add (erofs_iloc (node), erofs_inode_size (node), &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when adding inode size");
+
+  if (grub_add (pos, erofs_inode_xattr_ibody_size (node), &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when adding xattr size");
 
   /* pos = ALIGN_UP(pos, unit) */
   if (grub_add (pos, unit - 1, &pos))
-    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "position overflow when seeking at the start of chunkmap");
   pos &= ~(unit - 1);
 
-  /* no overflow for multiplication as chunkbits >= 9 and sizeof(unit) <= 8 */
+  /* No overflow for multiplication as chunkbits >= 9 and sizeof(unit) <= 8. */
   if (grub_add (pos, chunknr * unit, &pos))
-    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when finding the specific chunk");
 
   map->m_la = chunknr << chunkbits;
 
   if (grub_sub (erofs_inode_file_size (node), map->m_la, &map->m_plen))
-    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "m_plen underflow");
   map->m_plen = grub_min (((grub_uint64_t) 1) << chunkbits,
 			  ALIGN_UP (map->m_plen, erofs_blocksz (node->data)));
 
@@ -488,7 +487,7 @@ erofs_read_raw_data (grub_fshelp_node_t node, grub_uint8_t *buf, grub_uint64_t s
   if (bytes)
     *bytes = 0;
 
-  if (!node->inode_loaded)
+  if (node->inode_loaded == false)
     {
       err = erofs_read_inode (node->data, node);
       if (err != GRUB_ERR_NONE)
@@ -507,20 +506,20 @@ erofs_read_raw_data (grub_fshelp_node_t node, grub_uint8_t *buf, grub_uint64_t s
 	return err;
 
       if (grub_add(map.m_la, map.m_llen, &eend))
-	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "eend overflow");
 
       eend = grub_min (eend, offset + size);
       if (!(map.m_flags & EROFS_MAP_MAPPED))
 	{
 	  if (!map.m_llen)
 	    {
-	      /* reached EOF */
+	      /* Reached EOF. */
 	      grub_memset (estart, 0, offset + size - cur);
 	      cur = offset + size;
 	      continue;
 	    }
 
-	  /* Hole */
+	  /* It's a hole. */
 	  grub_memset (estart, 0, eend - cur);
 	  if (bytes)
 	    *bytes += eend - cur;
@@ -559,7 +558,7 @@ erofs_iterate_dir (grub_fshelp_node_t dir, grub_fshelp_iterate_dir_hook_t hook,
   grub_uint8_t *buf;
   grub_err_t err;
 
-  if (!dir->inode_loaded)
+  if (dir->inode_loaded == false)
     {
       err = erofs_read_inode (dir->data, dir);
       if (err != GRUB_ERR_NONE)
@@ -568,7 +567,7 @@ erofs_iterate_dir (grub_fshelp_node_t dir, grub_fshelp_iterate_dir_hook_t hook,
 
   file_size = erofs_inode_file_size (dir);
   buf = grub_malloc (blocksz);
-  if (!buf)
+  if (buf == NULL)
     return 0;
 
   while (offset < file_size)
@@ -600,7 +599,7 @@ erofs_iterate_dir (grub_fshelp_node_t dir, grub_fshelp_iterate_dir_hook_t hook,
 	  const char *de_name;
 
 	  fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
-	  if (!fdiro)
+	  if (fdiro == NULL)
 	    goto not_found;
 
 	  fdiro->data = dir->data;
@@ -624,7 +623,7 @@ erofs_iterate_dir (grub_fshelp_node_t dir, grub_fshelp_iterate_dir_hook_t hook,
 	    {
 	      if (grub_sub (grub_le_to_cpu16 (de[1].nameoff), nameoff, &de_namelen))
 		{
-		  grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+		  grub_error (GRUB_ERR_OUT_OF_RANGE, "de_namelen underflow");
 		  grub_free (fdiro);
 		  goto not_found;
 		}
@@ -687,38 +686,33 @@ erofs_read_symlink (grub_fshelp_node_t node)
   grub_size_t sz;
   grub_err_t err;
 
-  if (!node->inode_loaded)
+  if (node->inode_loaded == false)
     {
       err = erofs_read_inode (node->data, node);
       if (err != GRUB_ERR_NONE)
 	return NULL;
     }
 
-  if (grub_add (erofs_inode_file_size (node), 1, &sz))
-    {
-      grub_error (GRUB_ERR_OUT_OF_RANGE, N_ ("overflow is detected"));
-      return NULL;
-    }
-
-  if (sz > EROFS_PATH_LEN)
+  sz = erofs_inode_file_size (node);
+  if (sz >= EROFS_PATH_LEN)
     {
       grub_error (GRUB_ERR_BAD_FS,
 		  "symlink too long @ inode %" PRIuGRUB_UINT64_T, node->ino);
       return NULL;
     }
 
-  symlink = grub_malloc (sz);
-  if (!symlink)
+  symlink = grub_malloc (sz + 1);
+  if (symlink == NULL)
     return NULL;
 
-  err = erofs_read_raw_data (node, (grub_uint8_t *) symlink, sz - 1, 0, NULL);
+  err = erofs_read_raw_data (node, (grub_uint8_t *) symlink, sz, 0, NULL);
   if (err != GRUB_ERR_NONE)
     {
       grub_free (symlink);
       return NULL;
     }
 
-  symlink[sz - 1] = '\0';
+  symlink[sz] = '\0';
   return symlink;
 }
 
@@ -751,7 +745,7 @@ erofs_mount (grub_disk_t disk, bool read_root)
     }
 
   data = grub_malloc (sizeof (*data));
-  if (!data)
+  if (data == NULL)
     return NULL;
 
   data->disk = disk;
@@ -789,14 +783,14 @@ erofs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
   struct grub_dirhook_info info = {0};
   grub_err_t err;
 
-  if (!node->inode_loaded)
+  if (node->inode_loaded == false)
     {
       err = erofs_read_inode (ctx->data, node);
       if (err != GRUB_ERR_NONE)
         return 0;
     }
 
-  if (node->inode_loaded)
+  if (node->inode_loaded == true)
     {
       info.mtimeset = 1;
       info.mtime = erofs_inode_mtime (node);
@@ -819,7 +813,7 @@ grub_erofs_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
   };
 
   ctx.data = erofs_mount (device->disk, true);
-  if (!ctx.data)
+  if (ctx.data == NULL)
     goto fail;
 
   err = grub_fshelp_find_file (path, &ctx.data->inode, &fdiro, erofs_iterate_dir,
@@ -845,7 +839,7 @@ grub_erofs_open (grub_file_t file, const char *name)
   grub_err_t err;
 
   data = erofs_mount (file->device->disk, true);
-  if (!data)
+  if (data == NULL)
     {
       err = grub_errno;
       goto fail;
@@ -856,7 +850,7 @@ grub_erofs_open (grub_file_t file, const char *name)
   if (err != GRUB_ERR_NONE)
     goto fail;
 
-  if (!fdiro->inode_loaded)
+  if (fdiro->inode_loaded == false)
     {
       err = erofs_read_inode (data, fdiro);
       if (err != GRUB_ERR_NONE)
@@ -888,7 +882,7 @@ grub_erofs_read (grub_file_t file, char *buf, grub_size_t len)
   grub_uint64_t ret = 0, file_size;
   grub_err_t err;
 
-  if (!inode->inode_loaded)
+  if (inode->inode_loaded == false)
     {
       err = erofs_read_inode (data, inode);
       if (err != GRUB_ERR_NONE)
@@ -929,7 +923,7 @@ grub_erofs_uuid (grub_device_t device, char **uuid)
   struct grub_erofs_data *data;
 
   data = erofs_mount (device->disk, false);
-  if (!data)
+  if (data == NULL)
     {
       *uuid = NULL;
       return grub_errno;
@@ -948,7 +942,7 @@ grub_erofs_label (grub_device_t device, char **label)
   struct grub_erofs_data *data;
 
   data = erofs_mount (device->disk, false);
-  if (!data)
+  if (data == NULL)
     {
       *label = NULL;
       return grub_errno;
@@ -957,7 +951,7 @@ grub_erofs_label (grub_device_t device, char **label)
   *label = grub_strndup ((char *) data->sb.volume_name, sizeof (data->sb.volume_name));
   grub_free (data);
 
-  if (!*label)
+  if (*label == NULL)
     return grub_errno;
   return GRUB_ERR_NONE;
 }
@@ -968,7 +962,7 @@ grub_erofs_mtime (grub_device_t device, grub_int64_t *tm)
   struct grub_erofs_data *data;
 
   data = erofs_mount (device->disk, false);
-  if (!data)
+  if (data == NULL)
     {
       *tm = 0;
       return grub_errno;
Signature · 7 lines
2.39.3


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
author
#1 lore raw

PATCH v12 1/2 fs/erofs: Add support for EROFS

From: Yifan Zhao <[email protected]>

EROFS [1] is a lightweight read-only filesystem designed for performance which has already been shipped in most Linux distributions as well as widely used in several scenarios, such as Android system partitions, container images, and rootfs for embedded devices.

This patch brings EROFS uncompressed support. Now, it's possible to boot directly through GRUB with an EROFS rootfs.

EROFS compressed files will be supported later since it has more work to polish.

[1] https://erofs.docs.kernel.org

---
 INSTALL                     |    8 +-
 Makefile.util.def           |    1 +
 docs/grub.texi              |    3 +-
 grub-core/Makefile.core.def |    5 +
 grub-core/fs/erofs.c        | 1002 +++++++++++++++++++++++++++++++++++
 5 files changed, 1014 insertions(+), 5 deletions(-)
 create mode 100644 grub-core/fs/erofs.c
INSTALL +4 -4
index 8d9207c84..84030c9f4 100644
@@ -77,15 +77,15 @@ Prerequisites for make-check:
 
 * If running a Linux kernel the following modules must be loaded:
   - fuse, loop
-  - btrfs, ext4, f2fs, fat, hfs, hfsplus, jfs, mac-roman, minix, nilfs2,
+  - btrfs, erofs, ext4, f2fs, fat, hfs, hfsplus, jfs, mac-roman, minix, nilfs2,
     reiserfs, udf, xfs
   - On newer kernels, the exfat kernel modules may be used instead of the
     exfat FUSE filesystem
 * The following are Debian named packages required mostly for the full
   suite of filesystem testing (but some are needed by other tests as well):
-  - btrfs-progs, dosfstools, e2fsprogs, exfat-utils, f2fs-tools, genromfs,
-    hfsprogs, jfsutils, nilfs-tools, ntfs-3g, reiserfsprogs, squashfs-tools,
-    reiserfsprogs, udftools, xfsprogs, zfs-fuse
+  - btrfs-progs, dosfstools, e2fsprogs, erofs-utils, exfat-utils, f2fs-tools,
+    genromfs, hfsprogs, jfsutils, nilfs-tools, ntfs-3g, reiserfsprogs,
+    squashfs-tools, reiserfsprogs, udftools, xfsprogs, zfs-fuse
   - exfat-fuse, if not using the exfat kernel module
   - gzip, lzop, xz-utils
   - attr, cpio, g++, gawk, parted, recode, tar, util-linux
Makefile.util.def +1 -0
index 9432365a9..8d3bc107f 100644
@@ -98,6 +98,7 @@ library = {
   common = grub-core/fs/cpio_be.c;
   common = grub-core/fs/odc.c;
   common = grub-core/fs/newc.c;
+  common = grub-core/fs/erofs.c;
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
docs/grub.texi +2 -1
index d32266f69..b198d963d 100644
@@ -353,6 +353,7 @@ blocklist notation. The currently supported filesystem types are @dfn{Amiga
 Fast FileSystem (AFFS)}, @dfn{AtheOS fs}, @dfn{BeFS},
 @dfn{BtrFS} (including raid0, raid1, raid10, gzip and lzo),
 @dfn{cpio} (little- and big-endian bin, odc and newc variants),
+@dfn{EROFS} (only uncompressed support for now),
 @dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32},
 @dfn{exFAT}, @dfn{F2FS}, @dfn{HFS}, @dfn{HFS+},
 @dfn{ISO9660} (including Joliet, Rock-ridge and multi-chunk files),
@@ -6276,7 +6277,7 @@ assumed to be encoded in UTF-8.
 NTFS, JFS, UDF, HFS+, exFAT, long filenames in FAT, Joliet part of
 ISO9660 are treated as UTF-16 as per specification. AFS and BFS are read
 as UTF-8, again according to specification. BtrFS, cpio, tar, squash4, minix,
-minix2, minix3, ROMFS, ReiserFS, XFS, ext2, ext3, ext4, FAT (short names),
+minix2, minix3, ROMFS, ReiserFS, XFS, EROFS, ext2, ext3, ext4, FAT (short names),
 F2FS, RockRidge part of ISO9660, nilfs2, UFS1, UFS2 and ZFS are assumed
 to be UTF-8. This might be false on systems configured with legacy charset
 but as long as the charset used is superset of ASCII you should be able to
grub-core/Makefile.core.def +5 -0
index 8e1b1d9f3..7fa9446bd 100644
@@ -1442,6 +1442,11 @@ module = {
   common = fs/odc.c;
 };
 
+module = {
+  name = erofs;
+  common = fs/erofs.c;
+};
+
 module = {
   name = ext2;
   common = fs/ext2.c;
grub-core/fs/erofs.c new file +1002 -0

1,002 lines not on this page · raw message

Signature · 7 lines
2.39.3


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

Re: [PATCH v12 1/2] fs/erofs: Add support for EROFS

Quoted 19 lines · Gao Xiang

From: Yifan Zhao <[email protected]>

EROFS [1] is a lightweight read-only filesystem designed for performance which has already been shipped in most Linux distributions as well as widely used in several scenarios, such as Android system partitions, container images, and rootfs for embedded devices.

This patch brings EROFS uncompressed support. Now, it's possible to boot directly through GRUB with an EROFS rootfs.

EROFS compressed files will be supported later since it has more work to polish.

[1] https://erofs.docs.kernel.org

---

[...]

Quoted 21 lines
grub-core/fs/erofs.c new file +21 -0
+static grub_err_t
+erofs_map_blocks_chunkmode (grub_fshelp_node_t node,
+			    struct grub_erofs_map_blocks *map)
+{
+  grub_uint16_t chunk_format = grub_le_to_cpu16 (node->inode.e.i_u.c.format);
+  grub_uint64_t unit, pos, chunknr, blkaddr;
+  grub_uint8_t chunkbits;
+  grub_err_t err;
+
+  if (chunk_format & EROFS_CHUNK_FORMAT_INDEXES)
+    unit = sizeof (struct grub_erofs_inode_chunk_index);
+  else
+    unit = EROFS_BLOCK_MAP_ENTRY_SIZE;
+
+  chunkbits = node->data->sb.log2_blksz + (chunk_format & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
+  if (chunkbits > 63)
+    return grub_error (GRUB_ERR_BAD_FS, "invalid chunkbits %u @ inode %" PRIuGRUB_UINT64_T,
+		       chunkbits, node->ino);
+
+  chunknr = map->m_la >> chunkbits;
+

grub-core/fs/erofs.c line 411

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+  if (grub_add (erofs_iloc (node), erofs_inode_size (node), &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when adding inode size");
+
+  if (grub_add (pos, erofs_inode_xattr_ibody_size (node), &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when adding xattr size");
+
+  /* pos = ALIGN_UP(pos, unit) */
+  if (grub_add (pos, unit - 1, &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "position overflow when seeking at the start of chunkmap");
+  pos &= ~(unit - 1);

Please create a macro as I asked you earlier. Be careful with underflows too.

Otherwise patch set LGTM...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author
#3 lore raw

Re: [PATCH v12 1/2] fs/erofs: Add support for EROFS

Hi Daniel,

Quoted 48 lines · Daniel Kiper
Quoted 19 lines · Gao Xiang

From: Yifan Zhao <[email protected]>

EROFS [1] is a lightweight read-only filesystem designed for performance which has already been shipped in most Linux distributions as well as widely used in several scenarios, such as Android system partitions, container images, and rootfs for embedded devices.

This patch brings EROFS uncompressed support. Now, it's possible to boot directly through GRUB with an EROFS rootfs.

EROFS compressed files will be supported later since it has more work to polish.

[1] https://erofs.docs.kernel.org

---

[...]

Quoted 25 lines
grub-core/fs/erofs.c new file +25 -0
+static grub_err_t
+erofs_map_blocks_chunkmode (grub_fshelp_node_t node,
+			    struct grub_erofs_map_blocks *map)
+{
+  grub_uint16_t chunk_format = grub_le_to_cpu16 (node->inode.e.i_u.c.format);
+  grub_uint64_t unit, pos, chunknr, blkaddr;
+  grub_uint8_t chunkbits;
+  grub_err_t err;
+
+  if (chunk_format & EROFS_CHUNK_FORMAT_INDEXES)
+    unit = sizeof (struct grub_erofs_inode_chunk_index);
+  else
+    unit = EROFS_BLOCK_MAP_ENTRY_SIZE;
+
+  chunkbits = node->data->sb.log2_blksz + (chunk_format & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
+  if (chunkbits > 63)
+    return grub_error (GRUB_ERR_BAD_FS, "invalid chunkbits %u @ inode %" PRIuGRUB_UINT64_T,
+		       chunkbits, node->ino);
+
+  chunknr = map->m_la >> chunkbits;
+
+  if (grub_add (erofs_iloc (node), erofs_inode_size (node), &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when adding inode size");
+
+  if (grub_add (pos, erofs_inode_xattr_ibody_size (node), &pos))
Quoted 8 lines

grub-core/fs/erofs.c line 411

Quoted 6 lines
grub-core/fs/erofs.c new file +6 -0
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "chunkmap position overflow when adding xattr size");
+
+  /* pos = ALIGN_UP(pos, unit) */
+  if (grub_add (pos, unit - 1, &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "position overflow when seeking at the start of chunkmap");
+  pos &= ~(unit - 1);

Please create a macro as I asked you earlier. Be careful with underflows too.

Since I'm not sure how does it looks like, I wrote the following helper, hopefully it looks good to you:

#define ALIGN_UP_OVF(v, align, res) \
({ \
bool __failed; \

  typeof(v) a;				\
					\
  __failed = grub_sub (align, 1, &a);	\
  if (__failed == false)		\
    {					\
    __failed = grub_add (v, a, res);	\
    if (__failed == false)		\
      {					\
        *(res) &= ~(a);			\
      }					\
    }					\

__failed;})

Quoted 2 lines

Otherwise patch set LGTM...

Thanks! Let me send the next version soon.

Thanks,
Gao Xiang

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author
#4 lore raw

PATCH v12 2/2 fs/erofs: Add tests for EROFS in grub-fs-tester

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

In this patch, three tests of EROFS are introduced and they cover compact, extended and chunk-based inodes, respectively.

---
 .gitignore                   |  1 +
 Makefile.util.def            |  6 ++++++
 tests/erofs_test.in          | 20 ++++++++++++++++++++
 tests/util/grub-fs-tester.in | 32 +++++++++++++++++++++++++-------
 4 files changed, 52 insertions(+), 7 deletions(-)
 create mode 100644 tests/erofs_test.in
.gitignore +1 -0
index 11fcecf5c..4c1f91db8 100644
@@ -104,6 +104,7 @@ widthspec.bin
 /docs/version-dev.texi
 /docs/version.texi
 /ehci_test
+/erofs_test
 /example_grub_script_test
 /example_scripted_test
 /example_unit_test
Makefile.util.def +6 -0
index 8d3bc107f..0f74a1680 100644
@@ -764,6 +764,12 @@ script = {
   dependencies = 'garbage-gen$(BUILD_EXEEXT)';
 };
 
+script = {
+  testcase = native;
+  name = erofs_test;
+  common = tests/erofs_test.in;
+};
+
 script = {
   testcase = native;
   name = ext234_test;
tests/erofs_test.in new file +20 -0
new file mode 100644
index 000000000..51111627a
@@ -0,0 +1,20 @@
+#!@BUILD_SHEBANG@
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+  EUID=`id -u`
+fi
+
+if [ "$EUID" != 0 ] ; then
+   exit 99
+fi
+
+if ! which mkfs.erofs >/dev/null 2>&1; then
+   echo "mkfs.erofs not installed; cannot test erofs."
+   exit 99
+fi
+
+"@builddir@/grub-fs-tester" erofs_compact
+"@builddir@/grub-fs-tester" erofs_extended
+"@builddir@/grub-fs-tester" erofs_chunk
tests/util/grub-fs-tester.in +25 -7
index ea8b2d1f6..df5dc7542 100644
@@ -227,6 +227,10 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
+	x"erofs_"*)
+		MINBLKSIZE=4096
+		MAXBLKSIZE=4096
+		;;
 	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 	    # OS Limitation: GNU/Linux doesn't accept > 4096
@@ -382,8 +386,8 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		    FSLABEL="g;/_é䏌䐓䏕䎛䎾䏴кит u"
 		    #FSLABEL="g;/_é莭莽😁кит u"
 		    ;;
-		# FS LIMITATION: reiserfs, extN and jfs label is at most 16 UTF-8 characters
-		x"reiserfs_old" | x"reiserfs" | x"ext"* | x"lvm"* | x"luks"* | x"mdraid"* | x"jfs" | x"jfs_caseins")
+		# FS LIMITATION: reiserfs, extN, jfs and erofs label is at most 16 UTF-8 characters
+		x"reiserfs_old" | x"reiserfs" | x"ext"* | x"lvm"* | x"luks"* | x"mdraid"* | x"jfs" | x"jfs_caseins" | x"erofs_"*)
 		    FSLABEL="g;/éт 莭😁";;
 		# FS LIMITATION: No underscore, space, semicolon, slash or international characters in UFS* in label. Limited to 32 UTF-8 characters
 		x"ufs1" | x"ufs1_sun" | x"ufs2")
@@ -661,7 +665,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		x"tarfs" | x"cpio_"*| x"ziso9660" | x"romfs" | x"squash4_"*\
 		    | x"iso9660" | xjoliet | xrockridge | xrockridge_joliet \
 		    | x"iso9660_1999" | xjoliet_1999 | xrockridge_1999 \
-		    | xrockridge_joliet_1999)
+		    | xrockridge_joliet_1999 | x"erofs_"*)
 		    MNTPOINTRW="$MASTER"
 		    MNTPOINTRO="$MASTER"
 		    mkdir -p "$MASTER";;
@@ -805,7 +809,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		    sleep 1
 		    "zfs" create "$FSLABEL"/"grub fs"
 		    sleep 1;;
-		x"tarfs" | x"cpio_"* | x"iso9660" | xjoliet | xrockridge | xrockridge_joliet | x"iso9660_1999" | xjoliet_1999 | xrockridge_1999 | xrockridge_joliet_1999 | x"ziso9660" | x"romfs" | x"squash4_"*)
+		x"tarfs" | x"cpio_"* | x"iso9660" | xjoliet | xrockridge | xrockridge_joliet | x"iso9660_1999" | xjoliet_1999 | xrockridge_1999 | xrockridge_joliet_1999 | x"ziso9660" | x"romfs" | x"squash4_"* | x"erofs_"*)
 		    INSTDEVICE=/dev/null;;
 		x"reiserfs")
 		    "mkfs.reiserfs" --format=3.6 -b $BLKSIZE -l "$FSLABEL" -q "${MOUNTDEVICE}" ;;
@@ -990,7 +994,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		x"zfs"*)
 		    OSDIR="grub fs/"
 		    GRUBDIR="($GRUBDEVICE)/grub fs@";;
-		x"tarfs" | x"cpio_"* | x"iso9660" | xjoliet | xrockridge | xrockridge_joliet | x"iso9660_1999" | xjoliet_1999 | xrockridge_1999 | xrockridge_joliet_1999 | x"ziso9660" | x"romfs" | x"squash4_"* | xafs)
+		x"tarfs" | x"cpio_"* | x"iso9660" | xjoliet | xrockridge | xrockridge_joliet | x"iso9660_1999" | xjoliet_1999 | xrockridge_1999 | xrockridge_joliet_1999 | x"ziso9660" | x"romfs" | x"squash4_"* | xafs | x"erofs_"*)
 		    ;;
 		*)
 		    if ! mount -t "$MOUNTFS" "${MOUNTDEVICE}" "$MNTPOINTRW" -o ${MOUNTOPTS}${SELINUXOPTS}rw  ; then
@@ -1116,6 +1120,18 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		xsquash4_*)
 		    echo mksquashfs "$MASTER" "${FSIMAGEP}0.img" -always-use-fragments -comp "${fs/squash4_/}" -b $BLKSIZE
 		    mksquashfs "$MASTER" "${FSIMAGEP}0.img" -always-use-fragments -comp "${fs/squash4_/}" -b $BLKSIZE ;;
+		x"erofs_compact")
+			echo mkfs.erofs -Eforce-inode-compact -L "$FSLABEL" "${FSIMAGEP}0.img" "$MNTPOINTRW"
+			mkfs.erofs -Eforce-inode-compact -L "$FSLABEL" "${FSIMAGEP}0.img" "$MNTPOINTRW"
+			;;
+		x"erofs_extended")
+			echo mkfs.erofs -Eforce-inode-extended -L "$FSLABEL" "${FSIMAGEP}0.img" "$MNTPOINTRW"
+			mkfs.erofs -Eforce-inode-extended -L "$FSLABEL" "${FSIMAGEP}0.img" "$MNTPOINTRW"
+			;;
+		x"erofs_chunk")
+			echo mkfs.erofs --chunksize=1048576 -L "$FSLABEL" "${FSIMAGEP}0.img" "$MNTPOINTRW"
+			mkfs.erofs --chunksize=1048576 -L "$FSLABEL" "${FSIMAGEP}0.img" "$MNTPOINTRW"
+			;;
 		x"bfs")
 		    sleep 1
 		    fusermount -u "$MNTPOINTRW"
@@ -1187,6 +1203,8 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		    ;;
 		xsquash4_*)
 		    ;;
+		x"erofs_"*)
+		    ;;
 		xlvm*)
 		    vgchange -a y grub_test
 		    sleep 1
@@ -1624,7 +1642,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 			sleep 1;
 		    done
 		    sleep 5;;
-		x"tarfs" | x"cpio_"* | xrockridge | xjoliet | xrockridge_joliet | x"ziso9660" | x"romfs" | x"squash4_"* | xiso9660 | xiso9660_1999 | xrockridge_1999 | xjoliet_1999 | xrockridge_joliet_1999)
+		x"tarfs" | x"cpio_"* | xrockridge | xjoliet | xrockridge_joliet | x"ziso9660" | x"romfs" | x"squash4_"* | xiso9660 | xiso9660_1999 | xrockridge_1999 | xjoliet_1999 | xrockridge_joliet_1999 | x"erofs_"*)
 		    rm -rf "$MNTPOINTRW";;
 		x"afs")
 		    rm -rf "$MNTPOINTRO"
@@ -1651,7 +1669,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		    ;;
 	    esac
 	    case x"$fs" in
-		x"tarfs" | x"cpio_"* | x"iso9660" | xrockridge | xjoliet | xrockridge_joliet | x"ziso9660" | x"romfs" | x"squash4_"* | x"iso9660_1999" | xrockridge_1999 | xjoliet_1999 | xrockridge_joliet_1999) ;;
+		x"tarfs" | x"cpio_"* | x"iso9660" | xrockridge | xjoliet | xrockridge_joliet | x"ziso9660" | x"romfs" | x"squash4_"* | x"iso9660_1999" | xrockridge_1999 | xjoliet_1999 | xrockridge_joliet_1999 | x"erofs_"*) ;;
 		*)
 		    for lodev in $LODEVICES; do
 			while ! losetup -d "$lodev"; do
Signature · 7 lines
2.39.3


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel