cboot: add patch to fix reading ext4 sparse files

Put in missing zero-fills to when there is a gap
between extents, and at the end of a file.

Signed-off-by: Matt Madison <matt@madison.systems>
This commit is contained in:
Matt Madison
2021-06-08 06:54:44 -07:00
committed by Matt Madison
parent 3ae311fc2b
commit ddb4897a4e
3 changed files with 79 additions and 1 deletions

View File

@@ -12,6 +12,7 @@ SRC_URI = "${L4T_ALT_URI_BASE}/cboot_src_t18x.tbz2;downloadfilename=cboot_src_t1
file://0008-t186-add-bootinfo-to-build.patch \
file://0009-Add-machine-ID-to-kernel-command-line.patch \
file://0012-bmp-support-A-B-slots.patch \
file://0013-Fix-ext4-sparse-file-handling.patch \
"
SRC_URI[sha256sum] = "6da8ad60d302d222c09d56bc8f7e90e08592a0471f8bcbadb30268b3b0ad320f"

View File

@@ -14,7 +14,8 @@ SRC_URI = "${L4T_ALT_URI_BASE}/cboot_src_t19x.tbz2;downloadfilename=cboot_src_t1
file://0010-t194-add-bootinfo-to-build.patch \
file://0011-Add-machine-ID-to-kernel-command-line.patch \
file://0012-bmp-support-A-B-slots.patch \
"
file://0013-Fix-ext4-sparse-file-handling.patch \
"
SRC_URI[sha256sum] = "80a5b0491d75ea8f2d5f49e93e6d5b4986c739ff29f62133585c2fe7880e8fa9"

View File

@@ -0,0 +1,76 @@
From 8f02b2dff03a2b0ce0cb0c182c3c899523969ef6 Mon Sep 17 00:00:00 2001
From: Matt Madison <matt@madison.systems>
Date: Tue, 8 Jun 2021 06:47:15 -0700
Subject: [PATCH] Fix ext4 sparse file handling
---
bootloader/partner/common/lib/fs/ext4/ext4.c | 24 +++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/bootloader/partner/common/lib/fs/ext4/ext4.c b/bootloader/partner/common/lib/fs/ext4/ext4.c
index 0fca9e2..d57fb27 100644
--- a/bootloader/partner/common/lib/fs/ext4/ext4.c
+++ b/bootloader/partner/common/lib/fs/ext4/ext4.c
@@ -188,6 +188,7 @@ static int ext4_read_extent(ext2_t *ext2, struct ext4_extent_header *extent_head
off_t extent_len_bytes = 0;
off_t bytes_read = 0;
uint16_t i;
+ uint32_t next_block_no;
off_t data_blk;
int err = 0;
@@ -197,7 +198,15 @@ static int ext4_read_extent(ext2_t *ext2, struct ext4_extent_header *extent_head
buf_ptr = (uint8_t *)buf;
extent = (struct ext4_extent *)((uintptr_t)extent_header + sizeof(struct ext4_extent_header));
+ next_block_no = 0;
for (i = 0; i < extent_header->entries; i++) {
+ LTRACEF("block_no: %u, next_block_no: %u\n", extent->block_no, next_block_no);
+ if (extent->block_no > next_block_no) {
+ off_t fillsize = (extent->block_no - next_block_no) * E2FS_BLOCK_SIZE(ext2->super_blk);
+ buf_ptr = (uint8_t *)buf + next_block_no * E2FS_BLOCK_SIZE(ext2->super_blk);
+ LTRACEF("Zero-filling %lu bytes at %p\n", fillsize, buf_ptr);
+ memset(buf_ptr, 0, fillsize);
+ }
/* Calculate destination address */
buf_ptr = (uint8_t *)(buf + (extent->block_no * E2FS_BLOCK_SIZE(ext2->super_blk)));
/* Get block number to which this extent points */
@@ -217,6 +226,7 @@ static int ext4_read_extent(ext2_t *ext2, struct ext4_extent_header *extent_head
goto fail;
}
bytes_read += extent_len_bytes;
+ next_block_no = extent->block_no + extent->len;
extent++;
}
@@ -694,6 +704,8 @@ fail:
ssize_t ext4_read_file(filecookie *fcookie, void *buf, off_t offset, size_t len)
{
ext2_file_t *file = NULL;
+ ssize_t bytes_read;
+ off_t actsize;
LTRACEF("\n");
file = (ext2_file_t *)fcookie;
@@ -704,7 +716,17 @@ ssize_t ext4_read_file(filecookie *fcookie, void *buf, off_t offset, size_t len)
return -1;
}
- return ext4_read_data_from_extent(file->ext2, &file->inode, buf);
+ actsize = file->inode.e2di_size;
+ actsize |= (off_t) file->inode.e2di_size_high << 32;
+ bytes_read = ext4_read_data_from_extent(file->ext2, &file->inode, buf);
+ if (bytes_read < 0)
+ return bytes_read;
+ if (actsize > (off_t) bytes_read) {
+ off_t fillcount = actsize - (off_t) bytes_read;
+ LTRACEF("Zero-filling %lu bytes at end of file\n", fillcount);
+ memset(buf + bytes_read, 0, fillcount);
+ }
+ return (ssize_t) actsize;
}
static const struct fs_api ext4_api = {
--
2.30.2