From: "Pedro Falcato" <pedro.falcato@gmail.com>
To: devel@edk2.groups.io
Cc: Leif Lindholm <leif@nuviainc.com>,
Michael D Kinney <michael.d.kinney@intel.com>
Subject: [edk2-platforms PATCH] Ext4Pkg: Add uninitialized extents support
Date: Fri, 29 Oct 2021 02:04:32 +0100 [thread overview]
Message-ID: <f717578af1488c73938e2a1a9917b63ced9017f8.1635469284.git.pedro.falcato@gmail.com> (raw)
Uninitialized extents are special extents that have blocks allocated,
but are specified as uninitialized and therefore, reads behave the
same as file holes (reads 0), while writes already have blocks allocated.
Cc: Leif Lindholm <leif@nuviainc.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
---
Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h | 8 ++++++++
Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h | 21 +++++++++++++++++++++
Features/Ext4Pkg/Ext4Dxe/Extents.c | 24 ++++++++++++++++++++++--
Features/Ext4Pkg/Ext4Dxe/Inode.c | 14 +++++++++++---
4 files changed, 62 insertions(+), 5 deletions(-)
diff --git a/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h b/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h
index 070eb5a9c8..756b1bbe10 100644
--- a/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h
+++ b/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h
@@ -448,6 +448,14 @@ typedef struct {
UINT32 eb_checksum;
} EXT4_EXTENT_TAIL;
+/**
+ * EXT4 has this feature called uninitialized extents:
+ * An extent has a maximum of 32768 blocks (2^15 or 1 << 15).
+ * When we find an extent with > 32768 blocks, this extent is called uninitialized.
+ * Long story short, it's an extent that behaves as a file hole but has blocks already allocated.
+ */
+#define EXT4_EXTENT_MAX_INITIALIZED (1 << 15)
+
typedef UINT64 EXT4_BLOCK_NR;
typedef UINT32 EXT4_INO_NR;
diff --git a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h b/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h
index a9b932ed52..1d9a4ac6ba 100644
--- a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h
+++ b/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h
@@ -1131,4 +1131,25 @@ Ext4SuperblockCheckMagic (
IN EFI_BLOCK_IO_PROTOCOL *BlockIo
);
+/**
+ Check if the extent is uninitialized
+
+ @param[in] Extent Pointer to the EXT4_EXTENT
+
+ @returns True if uninitialized, else false.
+**/
+#define EXT4_EXTENT_IS_UNINITIALIZED(Extent) ((Extent)->ee_len > EXT4_EXTENT_MAX_INITIALIZED)
+
+/**
+ Retrieves the extent's length, dealing with uninitialized extents in the process.
+
+ @param[in] Extent Pointer to the EXT4_EXTENT
+
+ @returns Extent's length, in filesystem blocks.
+**/
+EXT4_BLOCK_NR
+Ext4GetExtentLength (
+ IN CONST EXT4_EXTENT *Extent
+ );
+
#endif
diff --git a/Features/Ext4Pkg/Ext4Dxe/Extents.c b/Features/Ext4Pkg/Ext4Dxe/Extents.c
index 5fa2fe098d..1ae175f417 100644
--- a/Features/Ext4Pkg/Ext4Dxe/Extents.c
+++ b/Features/Ext4Pkg/Ext4Dxe/Extents.c
@@ -332,7 +332,7 @@ Ext4GetExtent (
return EFI_NO_MAPPING;
}
- if (!(LogicalBlock >= Ext->ee_block && Ext->ee_block + Ext->ee_len > LogicalBlock)) {
+ if (!(LogicalBlock >= Ext->ee_block && Ext->ee_block + Ext4GetExtentLength (Ext) > LogicalBlock)) {
// This extent does not cover the block
if (Buffer != NULL) {
FreePool (Buffer);
@@ -413,7 +413,7 @@ Ext4ExtentsMapKeyCompare (
Extent = UserStruct;
Block = (UINT32)(UINTN)StandaloneKey;
- if (Block >= Extent->ee_block && Block < Extent->ee_block + Extent->ee_len) {
+ if (Block >= Extent->ee_block && Block < Extent->ee_block + Ext4GetExtentLength (Extent)) {
return 0;
}
@@ -593,3 +593,23 @@ Ext4CheckExtentChecksum (
return Tail->eb_checksum == Ext4CalculateExtentChecksum (ExtHeader, File);
}
+
+/**
+ Retrieves the extent's length, dealing with uninitialized extents in the process.
+
+ @param[in] Extent Pointer to the EXT4_EXTENT
+
+ @returns Extent's length, in filesystem blocks.
+**/
+EXT4_BLOCK_NR
+Ext4GetExtentLength (
+ IN CONST EXT4_EXTENT *Extent
+ )
+{
+ // If it's an unintialized extent, the true length is ee_len - 2^15
+ if (EXT4_EXTENT_IS_UNINITIALIZED (Extent)) {
+ return Extent->ee_len - EXT4_EXTENT_MAX_INITIALIZED;
+ }
+
+ return Extent->ee_len;
+}
diff --git a/Features/Ext4Pkg/Ext4Dxe/Inode.c b/Features/Ext4Pkg/Ext4Dxe/Inode.c
index 63cecec1f7..48bfe026a3 100644
--- a/Features/Ext4Pkg/Ext4Dxe/Inode.c
+++ b/Features/Ext4Pkg/Ext4Dxe/Inode.c
@@ -144,11 +144,19 @@ Ext4Read (
HasBackingExtent = Status != EFI_NO_MAPPING;
- if (!HasBackingExtent) {
+ if (!HasBackingExtent || EXT4_EXTENT_IS_UNINITIALIZED (&Extent)) {
+
HoleOff = BlockOff;
- HoleLen = Partition->BlockSize - HoleOff;
+
+ if (!HasBackingExtent) {
+ HoleLen = Partition->BlockSize - HoleOff;
+ } else {
+ // Uninitialized extents behave exactly the same as file holes.
+ HoleLen = Ext4GetExtentLength (&Extent) - HoleOff;
+ }
+
WasRead = HoleLen > RemainingRead ? RemainingRead : HoleLen;
- // Potential improvement: In the future, we could get the hole's tota
+ // Potential improvement: In the future, we could get the file hole's total
// size and memset all that
SetMem (Buffer, WasRead, 0);
} else {
--
2.33.1.windows.1
reply other threads:[~2021-10-29 1:04 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f717578af1488c73938e2a1a9917b63ced9017f8.1635469284.git.pedro.falcato@gmail.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox