From da0e83c01161d0c216f3831d5be21df728691c64 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 11 May 2025 16:48:02 +0200 Subject: feat(kernel/hefs): Fixed the shortcomings and issues of HeFS. Signed-off-by: Amlal El Mahrouss --- dev/kernel/src/FS/HeFS+FileSystemParser.cc | 84 ++++++++++++++++++++++-------- dev/kernel/src/FS/NeFS+FileSystemParser.cc | 6 +-- dev/kernel/src/IndexableProperty.cc | 16 ++---- 3 files changed, 70 insertions(+), 36 deletions(-) (limited to 'dev/kernel/src') diff --git a/dev/kernel/src/FS/HeFS+FileSystemParser.cc b/dev/kernel/src/FS/HeFS+FileSystemParser.cc index e91040b2..f8208048 100644 --- a/dev/kernel/src/FS/HeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/HeFS+FileSystemParser.cc @@ -470,6 +470,16 @@ namespace Detail { (HEFS_INDEX_NODE_DIRECTORY*) mm_new_heap(sizeof(HEFS_INDEX_NODE_DIRECTORY), Yes, No); while (YES) { + if (err_global_get() == kErrorDiskIsCorrupted) { + delete dir; + dir = nullptr; + + delete node; + node = nullptr; + + return nullptr; + } + mnt->fPacket.fPacketLba = start; mnt->fPacket.fPacketSize = sizeof(HEFS_INDEX_NODE_DIRECTORY); mnt->fPacket.fPacketContent = dir; @@ -491,12 +501,16 @@ namespace Detail { (Void)(kout << hex_number(node->fHashPath) << kendl); if (hefsi_hash_64(file_name) == node->fHashPath && node->fKind == kind) { + delete dir; + dir = nullptr; + return node; } } } hefsi_traverse_tree(dir, mnt, root->fStartIND, start); + if (start == root->fStartIND || start == 0) break; } delete node; @@ -800,8 +814,8 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c /// @note all HeFS strucutres are equal to 512, so here it's fine, unless fSectoSize is 2048. const SizeT max_lba = drv_std_get_size() / root->fSectorSize; - const SizeT dir_max = max_lba / 20; // 20% for directory inodes - const SizeT inode_max = max_lba / 30; // 30% for inodes + const SizeT dir_max = max_lba / 5; // 5% for directory inodes + const SizeT inode_max = max_lba / 5; // 5% for inodes root->fStartIND = mnt->fLbaStart + kHeFSINDStartOffset; root->fEndIND = root->fStartIND + dir_max; @@ -995,13 +1009,29 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc (Void)(kout << hex_number(start->fOffsetSliceLow) << kendl); if (start->fOffsetSliceLow) { - mnt->fPacket.fPacketLba = start->fOffsetSliceLow | (UInt64) start->fOffsetSliceHigh << 32; - mnt->fPacket.fPacketSize = block_sz; + mnt->fPacket.fPacketLba = ((UInt64) start->fOffsetSliceHigh << 32) | start->fOffsetSliceLow; + mnt->fPacket.fPacketSize = block_sz; mnt->fPacket.fPacketContent = block; + if (mnt->fPacket.fPacketLba > root->fEndBlock) { + kout << "Error: Filesystem is full.\r"; + mm_delete_heap((VoidPtr) root); + delete start; + return NO; + } + if (is_input) { mnt->fInput(mnt->fPacket); } else { + if (start->fFlags & kHeFSFlagsReadOnly) { + mm_delete_heap((VoidPtr) root); + delete start; + + kout << "Error: File is read-only\r"; + + return NO; + } + mnt->fOutput(mnt->fPacket); } } @@ -1127,35 +1157,47 @@ Boolean fs_init_hefs(Void) { parser.Format(&kMountPoint, kHeFSEncodingFlagsUTF8, kHeFSDefaultVolumeName); - MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsBinary | kHeFSFlagsReadOnly, - u8"/boot", u8"bootinfo.cfg", kHeFSFileKindRegular)); + MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~0", + kHeFSFileKindRegular)); + + MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~1", + kHeFSFileKindRegular)); + + MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~2", + kHeFSFileKindRegular)); Utf8Char contents_1[kHeFSBlockLen] = {0}; urt_set_memory(contents_1, 0, kHeFSBlockLen); - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", u8"bootinfo.cfg", - kHeFSFileKindRegular, YES)); + MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", + u8"bootinfo.cfg~0", kHeFSFileKindRegular, YES)); if (*contents_1 != u'\0') { - (Void)(kout << "/boot/bootinfo.cfg:" << kendl); + (Void)(kout << "/boot/bootinfo.cfg~0:" << kendl); (Void)(kout8 << contents_1 << kendl8); + + MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", + u8"bootinfo.cfg~1", kHeFSFileKindRegular, YES)); + + MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", + u8"bootinfo.cfg~2", kHeFSFileKindRegular, YES)); } else { auto src = - u8"[boot]\r" - u8"path=bootz.efi\r" - u8"name=BootZ\r" - u8"[kernel]\r" - u8"path=krnl.efi\r" - u8"name=NeKernel\r" - u8"[chk]\r" - u8"path=chk.efi\r" - u8"name=SysChk\r"; - + u8"[boot]\r" + u8"path=bootz.efi\r" + u8"name=BootZ\r" + u8"[kernel]\r" + u8"path=krnl.efi\r" + u8"name=NeKernel\r" + u8"[chk]\r" + u8"path=chk.efi\r" + u8"name=SysChk\r"; + urt_copy_memory((VoidPtr) src, contents_1, urt_string_len(src)); - + MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg", kHeFSFileKindRegular, NO)); + u8"bootinfo.cfg~0", kHeFSFileKindRegular, NO)); } return YES; diff --git a/dev/kernel/src/FS/NeFS+FileSystemParser.cc b/dev/kernel/src/FS/NeFS+FileSystemParser.cc index 683adc7a..0b818bbb 100644 --- a/dev/kernel/src/FS/NeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/NeFS+FileSystemParser.cc @@ -150,8 +150,8 @@ _Output BOOL NeFileSystemParser::CreateFork(_Input NEFS_FORK_STRUCT& the_fork) { /// @return the newly found fork. /***********************************************************************************/ _Output NEFS_FORK_STRUCT* NeFileSystemParser::FindFork(_Input NEFS_CATALOG_STRUCT* catalog, - _Input const Char* name, - _Input Boolean is_data) { + _Input const Char* name, + _Input Boolean is_data) { auto& drive = kMountpoint.A(); NEFS_FORK_STRUCT* the_fork = nullptr; @@ -209,7 +209,7 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char /// @param kind the catalog kind. /// @return catalog pointer. /***********************************************************************************/ -_Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char* name, +_Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char* name, _Input const Int32& flags, _Input const Int32& kind) { kout << "CreateCatalog(*...*)\r"; diff --git a/dev/kernel/src/IndexableProperty.cc b/dev/kernel/src/IndexableProperty.cc index 8dd216c8..a89ea92d 100644 --- a/dev/kernel/src/IndexableProperty.cc +++ b/dev/kernel/src/IndexableProperty.cc @@ -16,21 +16,13 @@ namespace Kernel { namespace Indexer { - Index& IndexableProperty::Leak() noexcept { - return fIndex; - } + Index& IndexableProperty::Leak() noexcept { return fIndex; } - Void IndexableProperty::AddFlag(Int16 flag) { - fFlags |= flag; - } + Void IndexableProperty::AddFlag(Int16 flag) { fFlags |= flag; } - Void IndexableProperty::RemoveFlag(Int16 flag) { - fFlags &= flag; - } + Void IndexableProperty::RemoveFlag(Int16 flag) { fFlags &= flag; } - Int16 IndexableProperty::HasFlag(Int16 flag) { - return fFlags & flag; - } + Int16 IndexableProperty::HasFlag(Int16 flag) { return fFlags & flag; } /// @brief Index a file into the indexer instance. /// @param filename filesystem path to access. -- cgit v1.2.3 From 152ab95f649d57521cbb90c257b11fa0c25b6b7e Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 11 May 2025 23:23:51 +0200 Subject: dev(kernel:HeFS): Cleanup filesystem and AHCI driver. Signed-off-by: Amlal El Mahrouss --- dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 3 +- dev/kernel/src/FS/HeFS+FileSystemParser.cc | 68 ++++--------------------- 2 files changed, 12 insertions(+), 59 deletions(-) (limited to 'dev/kernel/src') diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index a3079e0c..21483560 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -264,8 +264,7 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz while (YES) { if (timeout > kTimeout) { kout << "Disk hangup!\r"; - kSATAHba->Ports[kSATAIndex].Ci = 0; - err_global_get() = kErrorDiskIsCorrupted; + err_global_get() = kErrorDiskIsCorrupted; rtl_dma_free(size_buffer); return; diff --git a/dev/kernel/src/FS/HeFS+FileSystemParser.cc b/dev/kernel/src/FS/HeFS+FileSystemParser.cc index f8208048..a285593e 100644 --- a/dev/kernel/src/FS/HeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/HeFS+FileSystemParser.cc @@ -489,6 +489,8 @@ namespace Detail { (Void)(kout << hex_number(hefsi_hash_64(dir_name)) << kendl); (Void)(kout << hex_number(dir->fHashPath) << kendl); + if (dir->fHashPath == 0) break; + if (hefsi_hash_64(dir_name) == dir->fHashPath) { for (SizeT inode_index = 0UL; inode_index < kHeFSSliceCount; ++inode_index) { mnt->fPacket.fPacketLba = dir->fINSlices[inode_index]; @@ -510,7 +512,7 @@ namespace Detail { } hefsi_traverse_tree(dir, mnt, root->fStartIND, start); - if (start == root->fStartIND || start == 0) break; + if (start == root->fStartIND || start == root->fStartIND) break; } delete node; @@ -536,6 +538,8 @@ namespace Detail { if (start > root->fEndIND) return NO; if (root->fStartIN > root->fEndIN) return NO; + ; + if (root->fStartBlock > root->fEndBlock) return NO; if (mnt) { HEFS_INDEX_NODE_DIRECTORY* dir = @@ -754,8 +758,8 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c } if (drv_std_get_size() < kHeFSMinimumDiskSize) { - err_global_get() = kErrorDiskIsTooTiny; - kout << "Error: Failed to allocate memory for boot node->\r"; + kout << "HeFS requires at least 128 GiB." << kendl; + err_global_get() = kErrorDisk; return NO; } @@ -812,10 +816,10 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c MUST_PASS(root->fSectorSize); /// @note all HeFS strucutres are equal to 512, so here it's fine, unless fSectoSize is 2048. - const SizeT max_lba = drv_std_get_size() / root->fSectorSize; + const SizeT max_lba = (drv_std_get_size()) / root->fSectorSize; - const SizeT dir_max = max_lba / 5; // 5% for directory inodes - const SizeT inode_max = max_lba / 5; // 5% for inodes + const SizeT dir_max = max_lba / 300; // 5% for directory inodes + const SizeT inode_max = max_lba / 400; // 5% for inodes root->fStartIND = mnt->fLbaStart + kHeFSINDStartOffset; root->fEndIND = root->fStartIND + dir_max; @@ -1008,18 +1012,11 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc (Void)(kout << hex_number(start->fHashPath) << kendl); (Void)(kout << hex_number(start->fOffsetSliceLow) << kendl); - if (start->fOffsetSliceLow) { + if (start->fOffsetSliceLow && start->fHashPath) { mnt->fPacket.fPacketLba = ((UInt64) start->fOffsetSliceHigh << 32) | start->fOffsetSliceLow; mnt->fPacket.fPacketSize = block_sz; mnt->fPacket.fPacketContent = block; - if (mnt->fPacket.fPacketLba > root->fEndBlock) { - kout << "Error: Filesystem is full.\r"; - mm_delete_heap((VoidPtr) root); - delete start; - return NO; - } - if (is_input) { mnt->fInput(mnt->fPacket); } else { @@ -1157,49 +1154,6 @@ Boolean fs_init_hefs(Void) { parser.Format(&kMountPoint, kHeFSEncodingFlagsUTF8, kHeFSDefaultVolumeName); - MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~0", - kHeFSFileKindRegular)); - - MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~1", - kHeFSFileKindRegular)); - - MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~2", - kHeFSFileKindRegular)); - - Utf8Char contents_1[kHeFSBlockLen] = {0}; - - urt_set_memory(contents_1, 0, kHeFSBlockLen); - - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg~0", kHeFSFileKindRegular, YES)); - - if (*contents_1 != u'\0') { - (Void)(kout << "/boot/bootinfo.cfg~0:" << kendl); - (Void)(kout8 << contents_1 << kendl8); - - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg~1", kHeFSFileKindRegular, YES)); - - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg~2", kHeFSFileKindRegular, YES)); - } else { - auto src = - u8"[boot]\r" - u8"path=bootz.efi\r" - u8"name=BootZ\r" - u8"[kernel]\r" - u8"path=krnl.efi\r" - u8"name=NeKernel\r" - u8"[chk]\r" - u8"path=chk.efi\r" - u8"name=SysChk\r"; - - urt_copy_memory((VoidPtr) src, contents_1, urt_string_len(src)); - - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg~0", kHeFSFileKindRegular, NO)); - } - return YES; } } // namespace Kernel::HeFS -- cgit v1.2.3