Skip to contentSkip to conversation

Introduce EROFS support

PATCH Superseded Gao Xiang 2 participants 8 messages May 10, 2024 – May 17, 2024 Kbuild

v11 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

v11 cover Introduce EROFS support

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 v10:
- Fix coverity warnings reported by Daniel:
https://lore.kernel.org/grub-devel/[email protected]

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

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         | 1008 ++++++++++++++++++++++++++++++++++
 tests/erofs_test.in          |   20 +
 tests/util/grub-fs-tester.in |   32 +-
 8 files changed, 1072 insertions(+), 12 deletions(-)
 create mode 100644 grub-core/fs/erofs.c
 create mode 100644 tests/erofs_test.in

Interdiff against v10:

grub-core/fs/erofs.c +7 -6
index b82212b16..14c86f435 100644
@@ -396,7 +396,7 @@ erofs_map_blocks_chunkmode (grub_fshelp_node_t node,
     unit = EROFS_BLOCK_MAP_ENTRY_SIZE;
 
   chunkbits = node->data->sb.log2_blksz + (chunk_format & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
-  if (chunkbits > 64)
+  if (chunkbits > 63)
     return grub_error (GRUB_ERR_BAD_FS, "invalid chunkbits %u @ inode %" PRIuGRUB_UINT64_T,
 		       chunkbits, node->ino);
 
@@ -787,11 +787,13 @@ erofs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
 {
   struct grub_erofs_dir_ctx *ctx = data;
   struct grub_dirhook_info info = {0};
+  grub_err_t err;
 
   if (!node->inode_loaded)
     {
-      erofs_read_inode (ctx->data, node);
-      grub_errno = GRUB_ERR_NONE;
+      err = erofs_read_inode (ctx->data, node);
+      if (err != GRUB_ERR_NONE)
+        return 0;
     }
 
   if (node->inode_loaded)
@@ -953,11 +955,10 @@ grub_erofs_label (grub_device_t device, char **label)
     }
 
   *label = grub_strndup ((char *) data->sb.volume_name, sizeof (data->sb.volume_name));
-  if (!*label)
-    return grub_errno;
-
   grub_free (data);
 
+  if (!*label)
+    return grub_errno;
   return GRUB_ERR_NONE;
 }
 
@@ -1004,4 +1005,4 @@ GRUB_MOD_INIT (erofs)
 GRUB_MOD_FINI (erofs)
 {
   grub_fs_unregister (&grub_erofs_fs);
 }
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 v11 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

---
Tested-by Link: https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00001.html

 INSTALL                     |    8 +-
 Makefile.util.def           |    1 +
 docs/grub.texi              |    3 +-
 grub-core/Makefile.core.def |    5 +
 grub-core/fs/erofs.c        | 1008 +++++++++++++++++++++++++++++++++++
 5 files changed, 1020 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 +1008 -0

1,008 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 v11 1/2] fs/erofs: Add support for EROFS

Quoted 10 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.

Quoted 8 lines

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

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

In general patch LGTM except some nits...

Quoted 126 lines

---
Tested-by Link: https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00001.html

 INSTALL                     |    8 +-
 Makefile.util.def           |    1 +
 docs/grub.texi              |    3 +-
 grub-core/Makefile.core.def |    5 +
 grub-core/fs/erofs.c        | 1008 +++++++++++++++++++++++++++++++++++
 5 files changed, 1020 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 +36 -0
new file mode 100644
index 000000000..14c86f435
@@ -0,0 +1,1008 @@
+/* erofs.c - Enhanced Read-Only File System */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2024 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/fs.h>
+#include <grub/fshelp.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/safemath.h>
+#include <grub/types.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#define EROFS_SUPER_OFFSET	1024
+#define EROFS_MAGIC		0xE0F5E1E2
+#define EROFS_ISLOTBITS		5
+

grub-core/fs/erofs.c line 46

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+#define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE	0x00000004
+#define EROFS_ALL_FEATURE_INCOMPAT		EROFS_FEATURE_INCOMPAT_CHUNKED_FILE
+
+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;

May I ask you to align struct/union members in the following way?

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;

...

Please do this for all/most structs and unions definitions below.

Quoted 66 lines
grub-core/fs/erofs.c new file +66 -0
+
+  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_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_PACKED;
+
+#define EROFS_INODE_LAYOUT_COMPACT	0
+#define EROFS_INODE_LAYOUT_EXTENDED	1
+
+#define EROFS_INODE_FLAT_PLAIN		0
+#define EROFS_INODE_COMPRESSED_FULL	1
+#define EROFS_INODE_FLAT_INLINE		2
+#define EROFS_INODE_COMPRESSED_COMPACT	3
+#define EROFS_INODE_CHUNK_BASED		4
+
+#define EROFS_I_VERSION_MASKS		0x01
+#define EROFS_I_DATALAYOUT_MASKS	0x07
+
+#define EROFS_I_VERSION_BIT	0
+#define EROFS_I_DATALAYOUT_BIT	1
+
+struct grub_erofs_inode_chunk_info
+{
+  grub_uint16_t format;
+  grub_uint16_t reserved;
+} GRUB_PACKED;
+
+#define EROFS_CHUNK_FORMAT_BLKBITS_MASK	0x001F
+#define EROFS_CHUNK_FORMAT_INDEXES	0x0020
+
+#define EROFS_BLOCK_MAP_ENTRY_SIZE	4
+#define EROFS_MAP_MAPPED		0x02
+
+#define EROFS_NULL_ADDR			1
+#define EROFS_NAME_LEN			255
+#define EROFS_PATH_LEN			4096
+#define EROFS_MIN_LOG2_BLOCK_SIZE	9
+#define EROFS_MAX_LOG2_BLOCK_SIZE	16
+
+struct grub_erofs_inode_chunk_index
+{
+  grub_uint16_t advise;
+  grub_uint16_t device_id;
+  grub_uint32_t blkaddr;

grub-core/fs/erofs.c line 122

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+};
+
+union grub_erofs_inode_i_u
+{
+  grub_uint32_t compressed_blocks;
+  grub_uint32_t raw_blkaddr;
+
+  grub_uint32_t rdev;
+
+  struct grub_erofs_inode_chunk_info c;

Things like this one can be an exception...

Quoted 146 lines
grub-core/fs/erofs.c new file +146 -0
+};
+
+struct grub_erofs_inode_compact
+{
+  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;
+
+  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_PACKED;
+
+struct grub_erofs_inode_extended
+{
+  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;
+
+  union grub_erofs_inode_i_u i_u;
+
+  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_PACKED;
+
+union grub_erofs_inode
+{
+  struct grub_erofs_inode_compact c;
+  struct grub_erofs_inode_extended e;
+} GRUB_PACKED;
+
+#define EROFS_FT_UNKNOWN	0
+#define EROFS_FT_REG_FILE	1
+#define EROFS_FT_DIR		2
+#define EROFS_FT_CHRDEV		3
+#define EROFS_FT_BLKDEV		4
+#define EROFS_FT_FIFO		5
+#define EROFS_FT_SOCK		6
+#define EROFS_FT_SYMLINK	7
+
+struct grub_erofs_dirent
+{
+  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;
+};
+
+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];
+};
+
+struct grub_fshelp_node
+{
+  struct grub_erofs_data *data;
+  union grub_erofs_inode inode;
+
+  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;
+};
+
+struct grub_erofs_data
+{
+  grub_disk_t disk;
+  struct grub_erofs_super sb;
+
+  struct grub_fshelp_node inode;
+};
+
+#define erofs_blocksz(data) (((grub_uint32_t) 1) << data->sb.log2_blksz)
+
+static grub_size_t
+grub_erofs_strnlen (const char *s, grub_size_t n)
+{
+  const char *p = s;
+
+  if (n == 0)
+    return 0;
+
+  while (n-- && *p)
+    p++;
+
+  return p - s;
+}
+
+static grub_uint64_t
+erofs_iloc (grub_fshelp_node_t node)
+{
+  struct grub_erofs_super *sb = &node->data->sb;
+
+  return ((grub_uint64_t) grub_le_to_cpu32 (sb->meta_blkaddr) << sb->log2_blksz) +
+	 (node->ino << EROFS_ISLOTBITS);
+}
+
+static grub_err_t
+erofs_read_inode (struct grub_erofs_data *data, grub_fshelp_node_t node)
+{
+  union grub_erofs_inode *di;
+  grub_err_t err;
+  grub_uint16_t i_format;
+  grub_uint64_t addr = erofs_iloc (node);
+
+  di = (union grub_erofs_inode *) &node->inode;
+
+  err = grub_disk_read (data->disk, addr >> GRUB_DISK_SECTOR_BITS,
+			addr & (GRUB_DISK_SECTOR_SIZE - 1),
+			sizeof (struct grub_erofs_inode_compact), &di->c);
+  if (err != GRUB_ERR_NONE)
+    return err;
+
+  i_format = grub_le_to_cpu16 (di->c.i_format);
+  node->inode_type = (i_format >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASKS;
+  node->inode_datalayout = (i_format >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASKS;

grub-core/fs/erofs.c line 278

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+
+  switch (node->inode_type)
+    {
+    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));

This function call is unreadable. Please fix it. I am OK with lines longer than 80 chars. So, first argument for the grub_disk_read() should be immediately behind "(". Then other arguments may follow below starting from the same column with the first argument.

grub_disk_read (data->disk, addr >> GRUB_DISK_SECTOR_BITS,

                  addr & (GRUB_DISK_SECTOR_SIZE - 1),
		  ...
Quoted 54 lines
grub-core/fs/erofs.c new file +54 -0
+      if (err != GRUB_ERR_NONE)
+	return err;
+      break;
+    case EROFS_INODE_LAYOUT_COMPACT:
+      break;
+    default:
+      return grub_error (GRUB_ERR_BAD_FS, "invalid type %u @ inode %" PRIuGRUB_UINT64_T,
+			 node->inode_type, node->ino);
+    }
+
+  node->inode_loaded = true;
+
+  return 0;
+}
+
+static grub_uint64_t
+erofs_inode_size (grub_fshelp_node_t node)
+{
+  return node->inode_type == EROFS_INODE_LAYOUT_COMPACT
+	     ? sizeof (struct grub_erofs_inode_compact)
+	     : sizeof (struct grub_erofs_inode_extended);
+}
+
+static grub_uint64_t
+erofs_inode_file_size (grub_fshelp_node_t node)
+{
+  union grub_erofs_inode *di = (union grub_erofs_inode *) &node->inode;
+
+  return node->inode_type == EROFS_INODE_LAYOUT_COMPACT
+	     ? grub_le_to_cpu32 (di->c.i_size)
+	     : grub_le_to_cpu64 (di->e.i_size);
+}
+
+static grub_uint32_t
+erofs_inode_xattr_ibody_size (grub_fshelp_node_t node)
+{
+  grub_uint16_t cnt = grub_le_to_cpu16 (node->inode.e.i_xattr_icount);
+
+  if (cnt == 0)
+    return 0;
+
+  return sizeof (struct grub_erofs_xattr_ibody_header) + ((cnt - 1) * sizeof (grub_uint32_t));
+}
+
+static grub_uint64_t
+erofs_inode_mtime (grub_fshelp_node_t node)
+{
+  return node->inode_type == EROFS_INODE_LAYOUT_COMPACT
+	     ? grub_le_to_cpu64 (node->data->sb.build_time)
+	     : grub_le_to_cpu64 (node->inode.e.i_mtime);
+}
+
+static grub_err_t
+erofs_map_blocks_flatmode (grub_fshelp_node_t node,

grub-core/fs/erofs.c line 342

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+			   struct grub_erofs_map_blocks *map)
+{
+  grub_uint64_t nblocks, lastblk, file_size;
+  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 = erofs_inode_file_size (node);
+  if (grub_add (file_size, blocksz - 1, &nblocks))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");

You use "overflow is detected" message everywhere. It is not helpful. I think (almost) every message should be different, e.g. "nblocks overflow" or "invalid argument: %d..." or ...

grub-core/fs/erofs.c line 352

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+  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) */
+  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) ||

Please move this to the line with grub_mul()...

grub-core/fs/erofs.c line 354

Quoted 2 lines
grub-core/fs/erofs.c new file +2 -0
+	  grub_add (map->m_pa, map->m_la, &map->m_pa))
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");

Again, this message does not help...

grub-core/fs/erofs.c line 356

Quoted 2 lines
grub-core/fs/erofs.c new file +2 -0
+      if (grub_sub (lastblk * blocksz, map->m_la, &map->m_plen))
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");

To be precise, this is an underflow...

grub-core/fs/erofs.c line 363

Quoted 7 lines
grub-core/fs/erofs.c new file +7 -0
+    }
+  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");

Please fix all these messages...

grub-core/fs/erofs.c line 372

Quoted 9 lines
grub-core/fs/erofs.c new file +9 -0
+      if (grub_sub (file_size, map->m_la, &map->m_plen))
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+
+      /* 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);

Wrong wrapping as above. Please fix it.

grub-core/fs/erofs.c line 378

Quoted 6 lines
grub-core/fs/erofs.c new file +6 -0
+    }
+  else
+    return grub_error (GRUB_ERR_BAD_FS,
+		       "invalid map->m_la=%" PRIuGRUB_UINT64_T
+		       " @ inode %" PRIuGRUB_UINT64_T,
+		       map->m_la, node->ino);

Ditto... Please fix similar wrapping everywhere.

Quoted 17 lines
grub-core/fs/erofs.c new file +17 -0
+  map->m_llen = map->m_plen;
+  return GRUB_ERR_NONE;
+}
+
+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;

grub-core/fs/erofs.c line 406

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+
+  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) ||
+      grub_add (pos, erofs_inode_xattr_ibody_size (node), &pos))

Maybe in some cases it is worth checking overflow for every operation separately and then print relevant messages.

grub-core/fs/erofs.c line 412

Quoted 6 lines
grub-core/fs/erofs.c new file +6 -0
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+
+  /* pos = ALIGN_UP(pos, unit) */
+  if (grub_add (pos, unit - 1, &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+  pos &= ~(unit - 1);

May I ask you to add a macro which allows overflow detection, e.g. ALIGN_UP_OVF(). This should go to a separate patch.

grub-core/fs/erofs.c line 414

Quoted 1 line
grub-core/fs/erofs.c new file +1 -0
+  /* no overflow for multiplication as chunkbits >= 9 and sizeof(unit) <= 8 */

Please start all comments with upper case and end with full stop.

Quoted 67 lines
grub-core/fs/erofs.c new file +67 -0
+  if (grub_add (pos, chunknr * unit, &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+
+  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");
+  map->m_plen = grub_min (((grub_uint64_t) 1) << chunkbits,
+			  ALIGN_UP (map->m_plen, erofs_blocksz (node->data)));
+
+  if (chunk_format & EROFS_CHUNK_FORMAT_INDEXES)
+    {
+      struct grub_erofs_inode_chunk_index idx;
+
+      err = grub_disk_read (node->data->disk, pos >> GRUB_DISK_SECTOR_BITS,
+			    pos & (GRUB_DISK_SECTOR_SIZE - 1), unit, &idx);
+      if (err != GRUB_ERR_NONE)
+	return err;
+
+      blkaddr = grub_le_to_cpu32 (idx.blkaddr);
+    }
+  else
+    {
+      grub_uint32_t blkaddr_le;
+
+      err = grub_disk_read (node->data->disk, pos >> GRUB_DISK_SECTOR_BITS,
+			    pos & (GRUB_DISK_SECTOR_SIZE - 1), unit, &blkaddr_le);
+      if (err != GRUB_ERR_NONE)
+	return err;
+
+      blkaddr = grub_le_to_cpu32 (blkaddr_le);
+    }
+
+  if (blkaddr == EROFS_NULL_ADDR)
+    {
+      map->m_pa = 0;
+      map->m_flags = 0;
+    }
+  else
+    {
+      map->m_pa = blkaddr << node->data->sb.log2_blksz;
+      map->m_flags = EROFS_MAP_MAPPED;
+    }
+
+  map->m_llen = map->m_plen;
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+erofs_map_blocks (grub_fshelp_node_t node, struct grub_erofs_map_blocks *map)
+{
+  if (map->m_la >= erofs_inode_file_size (node))
+    {
+      map->m_llen = map->m_plen = 0;
+      map->m_pa = 0;
+      map->m_flags = 0;
+      return GRUB_ERR_NONE;
+    }
+
+  if (node->inode_datalayout != EROFS_INODE_CHUNK_BASED)
+    return erofs_map_blocks_flatmode (node, map);
+  else
+    return erofs_map_blocks_chunkmode (node, map);
+}
+
+static grub_err_t
+erofs_read_raw_data (grub_fshelp_node_t node, grub_uint8_t *buf, grub_uint64_t size,

grub-core/fs/erofs.c line 491

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+		     grub_uint64_t offset, grub_uint64_t *bytes)
+{
+  struct grub_erofs_map_blocks map = {0};
+  grub_uint64_t cur;
+  grub_err_t err;
+
+  if (bytes)
+    *bytes = 0;
+
+  if (!node->inode_loaded)

if (node->inode_loaded == false)

... and please fix similar things everywhere...

Quoted 61 lines
grub-core/fs/erofs.c new file +61 -0
+    {
+      err = erofs_read_inode (node->data, node);
+      if (err != GRUB_ERR_NONE)
+	return err;
+    }
+
+  cur = offset;
+  while (cur < offset + size)
+    {
+      grub_uint8_t *const estart = buf + cur - offset;
+      grub_uint64_t eend, moff = 0;
+
+      map.m_la = cur;
+      err = erofs_map_blocks (node, &map);
+      if (err != GRUB_ERR_NONE)
+	return err;
+
+      if (grub_add(map.m_la, map.m_llen, &eend))
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+
+      eend = grub_min (eend, offset + size);
+      if (!(map.m_flags & EROFS_MAP_MAPPED))
+	{
+	  if (!map.m_llen)
+	    {
+	      /* reached EOF */
+	      grub_memset (estart, 0, offset + size - cur);
+	      cur = offset + size;
+	      continue;
+	    }
+
+	  /* Hole */
+	  grub_memset (estart, 0, eend - cur);
+	  if (bytes)
+	    *bytes += eend - cur;
+	  cur = eend;
+	  continue;
+	}
+
+      if (cur > map.m_la)
+	{
+	  moff = cur - map.m_la;
+	  map.m_la = cur;
+	}
+
+      err = grub_disk_read (node->data->disk,
+			    (map.m_pa + moff) >> GRUB_DISK_SECTOR_BITS,
+			    (map.m_pa + moff) & (GRUB_DISK_SECTOR_SIZE - 1),
+			    eend - map.m_la, estart);
+      if (err != GRUB_ERR_NONE)
+	return err;
+
+      if (bytes)
+	*bytes += eend - map.m_la;
+
+      cur = eend;
+    }
+
+  return GRUB_ERR_NONE;
+}
+

grub-core/fs/erofs.c line 562

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+static int
+erofs_iterate_dir (grub_fshelp_node_t dir, grub_fshelp_iterate_dir_hook_t hook,
+		   void *hook_data)
+{
+  grub_uint64_t offset = 0, file_size;
+  grub_uint32_t blocksz = erofs_blocksz (dir->data);
+  grub_uint8_t *buf;
+  grub_err_t err;
+
+  if (!dir->inode_loaded)

Ditto.

grub-core/fs/erofs.c line 571

Quoted 9 lines
grub-core/fs/erofs.c new file +9 -0
+    {
+      err = erofs_read_inode (dir->data, dir);
+      if (err != GRUB_ERR_NONE)
+	return 0;
+    }
+
+  file_size = erofs_inode_file_size (dir);
+  buf = grub_malloc (blocksz);
+  if (!buf)

if (buf == NULL)

I know your version works but it is less readable. Please fix similar checks everywhere...

Quoted 22 lines
grub-core/fs/erofs.c new file +22 -0
+    return 0;
+
+  while (offset < file_size)
+    {
+      grub_uint64_t maxsize = grub_min (blocksz, file_size - offset);
+      struct grub_erofs_dirent *de = (void *) buf, *end;
+      grub_uint16_t nameoff;
+
+      err = erofs_read_raw_data (dir, buf, maxsize, offset, NULL);
+      if (err != GRUB_ERR_NONE)
+	goto not_found;
+
+      nameoff = grub_le_to_cpu16 (de->nameoff);
+      if (nameoff < sizeof (struct grub_erofs_dirent) || nameoff >= maxsize)
+	{
+	  grub_error (GRUB_ERR_BAD_FS,
+		      "invalid nameoff %u @ inode %" PRIuGRUB_UINT64_T,
+		      nameoff, dir->ino);
+	  goto not_found;
+	}
+
+      end = (struct grub_erofs_dirent *) ((grub_uint8_t *) de + nameoff);

grub-core/fs/erofs.c line 603

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+      while (de < end)
+	{
+	  struct grub_fshelp_node *fdiro;
+	  enum grub_fshelp_filetype type;
+	  char filename[EROFS_NAME_LEN + 1];
+	  grub_size_t de_namelen;
+	  const char *de_name;
+
+	  fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+	  if (!fdiro)

Ditto...

Daniel

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

author
#3 lore raw

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

Hi Daniel,

Quoted 1 line · Daniel Kiper

On Fri, May 10, 2024 at 08:52:55AM +0800, Gao Xiang wrote:

...

Quoted 2 lines

In general patch LGTM except some nits...

Thanks for the comments. Let me address them.

Quoted 3 lines
Quoted 3 lines

---
Tested-by Link: https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00001.html

...

Quoted 10 lines

grub-core/fs/erofs.c line 46

Quoted 7 lines
grub-core/fs/erofs.c new file +7 -0
+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;

May I ask you to align struct/union members in the following way?

Quoted 14 lines
   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;

   ...

Sorry, anyway, I'm not quite familiar with GRUB coding styles.

It seems that using TABs in the middle of names and types. Is my understanding correct? I will try to update like this.

Quoted 2 lines

Please do this for all/most structs and unions definitions below.

...

Quoted 36 lines
Quoted 20 lines
grub-core/fs/erofs.c new file +20 -0
+
+static grub_err_t
+erofs_read_inode (struct grub_erofs_data *data, grub_fshelp_node_t node)
+{
+  union grub_erofs_inode *di;
+  grub_err_t err;
+  grub_uint16_t i_format;
+  grub_uint64_t addr = erofs_iloc (node);
+
+  di = (union grub_erofs_inode *) &node->inode;
+
+  err = grub_disk_read (data->disk, addr >> GRUB_DISK_SECTOR_BITS,
+			addr & (GRUB_DISK_SECTOR_SIZE - 1),
+			sizeof (struct grub_erofs_inode_compact), &di->c);
+  if (err != GRUB_ERR_NONE)
+    return err;
+
+  i_format = grub_le_to_cpu16 (di->c.i_format);
+  node->inode_type = (i_format >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASKS;
+  node->inode_datalayout = (i_format >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASKS;

grub-core/fs/erofs.c line 278

Quoted 10 lines
grub-core/fs/erofs.c new file +10 -0
+
+  switch (node->inode_type)
+    {
+    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));

This function call is unreadable. Please fix it. I am OK with lines longer than 80 chars. So, first argument for the grub_disk_read() should be immediately behind "(". Then other arguments may follow below starting from the same column with the first argument.

Quoted 3 lines
   grub_disk_read (data->disk, addr >> GRUB_DISK_SECTOR_BITS,
                   addr & (GRUB_DISK_SECTOR_SIZE - 1),
		  ...

Okay, will fix.

Quoted 1 line

...

Quoted 9 lines

grub-core/fs/erofs.c line 342

Quoted 5 lines
grub-core/fs/erofs.c new file +5 -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");

You use "overflow is detected" message everywhere. It is not helpful. I think (almost) every message should be different, e.g. "nblocks overflow" or "invalid argument: %d..." or ...

Let me fix it as "nblocks overflow".

Quoted 13 lines

grub-core/fs/erofs.c line 352

Quoted 11 lines
grub-core/fs/erofs.c new file +10 -0
+  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) */
+  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) ||

Please move this to the line with grub_mul()...

Okay, will fix as

      if (grub_mul ((grub_uint64_t) grub_le_to_cpu32 (node->inode.e.i_u.raw_blkaddr), blocksz, &map->m_pa) ||
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+	  grub_add (map->m_pa, map->m_la, &map->m_pa))
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");

Again, this message does not help...

Will fix as "m_pa overflow".

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+      if (grub_sub (lastblk * blocksz, map->m_la, &map->m_plen))
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");

To be precise, this is an underflow...

Will fix as "m_plen underflow".

Quoted 10 lines

grub-core/fs/erofs.c line 363

Quoted 8 lines
grub-core/fs/erofs.c new file +7 -0
+    }
+  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");

Please fix all these messages...

Thanks, will fix as "m_pa overflow".

Quoted 3 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+      if (grub_sub (file_size, map->m_la, &map->m_plen))
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");

Will fix as "m_plen overflow".

Quoted 9 lines

grub-core/fs/erofs.c line 372

Quoted 7 lines
grub-core/fs/erofs.c new file +7 -0
+
+      /* 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);

Wrong wrapping as above. Please fix it.

Should I fix this in the same line?
Will try to fix.

Quoted 9 lines

grub-core/fs/erofs.c line 378

Quoted 7 lines
grub-core/fs/erofs.c new file +6 -0
+    }
+  else
+    return grub_error (GRUB_ERR_BAD_FS,
+		       "invalid map->m_la=%" PRIuGRUB_UINT64_T
+		       " @ inode %" PRIuGRUB_UINT64_T,
+		       map->m_la, node->ino);

Ditto... Please fix similar wrapping everywhere.

Should I fix this in the same line?
Will try to fix.

Quoted 23 lines
Quoted 23 lines
grub-core/fs/erofs.c new file +22 -0
+  map->m_llen = map->m_plen;
+  return GRUB_ERR_NONE;
+}
+
+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);
Quoted 8 lines

grub-core/fs/erofs.c line 406

Quoted 5 lines
grub-core/fs/erofs.c new file +5 -0
+
+  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))

Maybe in some cases it is worth checking overflow for every operation separately and then print relevant messages.

I'm not sure since they are all steps to calculate the inode chunk map physical offset (position) in bytes.

Maybe "chunkmap position overflow"?

Quoted 6 lines

grub-core/fs/erofs.c line 411

Quoted 6 lines
grub-core/fs/erofs.c new file +5 -0
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");
+
+  /* pos = ALIGN_UP(pos, unit) */
+  if (grub_add (pos, unit - 1, &pos))
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected");

I'd also update this as "chunkmap position overflow"?

Quoted 4 lines
Quoted 1 line

+ pos &= ~(unit - 1);

May I ask you to add a macro which allows overflow detection, e.g. ALIGN_UP_OVF(). This should go to a separate patch.

Not quite sure what's like? Could we handle this later?

Quoted 4 lines
Quoted 2 lines

+ /* no overflow for multiplication as chunkbits >= 9 and sizeof(unit) <= 8 */

Please start all comments with upper case and end with full stop.

Will try to fix them all.

Quoted 1 line

...

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+  if (!node->inode_loaded)

if (node->inode_loaded == false)

... and please fix similar things everywhere...

Okay, !node->inode_loaded is Linux-like style, I will try to fix what I find.

Quoted 2 lines
Quoted 2 lines

+ {

...

Quoted 5 lines
Quoted 5 lines
grub-core/fs/erofs.c new file +5 -0
+
+static int
+erofs_iterate_dir (grub_fshelp_node_t dir, grub_fshelp_iterate_dir_hook_t hook,
+		   void *hook_data)
+{
Quoted 8 lines

grub-core/fs/erofs.c line 562

Quoted 6 lines
grub-core/fs/erofs.c new file +6 -0
+  grub_uint64_t offset = 0, file_size;
+  grub_uint32_t blocksz = erofs_blocksz (dir->data);
+  grub_uint8_t *buf;
+  grub_err_t err;
+
+  if (!dir->inode_loaded)

Ditto.

Okay.

Quoted 2 lines
Quoted 2 lines

+ {

...

Quoted 8 lines

grub-core/fs/erofs.c line 571

Quoted 3 lines
grub-core/fs/erofs.c new file +3 -0
+  file_size = erofs_inode_file_size (dir);
+  buf = grub_malloc (blocksz);
+  if (!buf)

if (buf == NULL)

I know your version works but it is less readable. Please fix similar checks everywhere...

Okay.

Quoted 3 lines
Quoted 3 lines
(quoted diff) +2 -0
+    return 0;
+

...

Quoted 5 lines

grub-core/fs/erofs.c line 603

Quoted 3 lines
grub-core/fs/erofs.c new file +3 -0
+
+	  fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+	  if (!fdiro)

Ditto...

Okay..

Thanks for reviewing!

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

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

Hi Daniel,

Quoted 11 lines · Gao Xiang

Hi Daniel,

Quoted 1 line · Daniel Kiper

On Fri, May 10, 2024 at 08:52:55AM +0800, Gao Xiang wrote:

...

Quoted 2 lines

In general patch LGTM except some nits...

Thanks for the comments.  Let me address them.

I've addressed your comments as a try, please kindly take some time at the latest diff and full version...

https://lore.kernel.org/grub-devel/[email protected]

Thanks,
Gao Xiang

Quoted 3 lines
Quoted 3 lines

Daniel

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

author
#5 lore raw

PATCH v11 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.

---
Reviewed-by Link: https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00101.html

 .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
Reviewed-by Daniel Kiper on v11 2/2
#6 lore raw

Re: [PATCH v11 2/2] fs/erofs: Add tests for EROFS in grub-fs-tester

Quoted 8 lines · 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.

Daniel

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

author
#7 lore raw

Re: [PATCH v11 2/2] fs/erofs: Add tests for EROFS in grub-fs-tester

Quoted 11 lines · Daniel Kiper
Quoted 8 lines · 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.

Thanks for the review!

Thanks,
Gao Xiang

Quoted 2 lines

Daniel

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