[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v12 0/2] Introduce EROFS support
From: |
Gao Xiang |
Subject: |
[PATCH v12 0/2] Introduce EROFS support |
Date: |
Fri, 17 May 2024 12:40:52 +0800 |
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:
ZkZ7xW56AwyBFZYF@tomti.i.net-space.pl/">https://lore.kernel.org/grub-devel/ZkZ7xW56AwyBFZYF@tomti.i.net-space.pl/
- 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):
ZkZ8fdd+ZCkT+t7n@tomti.i.net-space.pl/">https://lore.kernel.org/grub-devel/ZkZ8fdd+ZCkT+t7n@tomti.i.net-space.pl/
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:
diff --git a/grub-core/fs/erofs.c b/grub-core/fs/erofs.c
index 14c86f435..630f95e75 100644
--- a/grub-core/fs/erofs.c
+++ b/grub-core/fs/erofs.c
@@ -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;
--
2.39.3
- [PATCH v12 0/2] Introduce EROFS support,
Gao Xiang <=