From b10c54a12d6243ebfacd2f89ceb59c483ca17091 Mon Sep 17 00:00:00 2001 From: Stefan Schnitzler Date: Wed, 2 Sep 2026 17:23:57 +0200 Subject: [PATCH 1/3] Updated the ".." entry when a directory is moved to a new parent When a directory is renamed into a different parent directory, fx_directory_rename relinks the entry in both parents but never updates the ".." entry inside the moved directory, which keeps pointing at the old parent's starting cluster. A ".." path component inside the moved directory then resolves to the old parent and, once that cluster is reused, to an unrelated place. fx_media_check does not detect this, as it skips the first two entries of every sub-directory. Read the moved directory's ".." entry after the relink and rewrite its starting cluster in place when the parent changed (0 when the new parent is the root). The write happens inside the existing fault tolerant transaction; a rename within one parent ends without the additional write. The regression test extension covers every parent transition - between sub-directories, into and out of the root - and pins the write count: one additional directory entry write for a move, none for a rename within one parent. Fixes Issue #98 Assisted-by: Claude Code (Claude Fable 5) --- common/src/fx_directory_rename.c | 87 ++++++- .../filex_directory_rename_test.c | 227 ++++++++++++++++++ 2 files changed, 312 insertions(+), 2 deletions(-) diff --git a/common/src/fx_directory_rename.c b/common/src/fx_directory_rename.c index 48b6784..5e43ce8 100644 --- a/common/src/fx_directory_rename.c +++ b/common/src/fx_directory_rename.c @@ -9,6 +9,8 @@ * SPDX-License-Identifier: MIT **************************************************************************/ +// Portions of this file were generated with AI assistance. + /**************************************************************************/ /**************************************************************************/ @@ -51,6 +53,8 @@ /* If found, the rename request is valid and the directory will be */ /* changed to the new name. Otherwise, if the directory is not found, */ /* the appropriate error code is returned to the caller. */ +/* If the directory is moved under a different parent, the ".." */ +/* entry inside the directory is updated to name the new parent. */ /* */ /* INPUT */ /* */ @@ -64,6 +68,7 @@ /* */ /* CALLS */ /* */ +/* _fx_directory_entry_read Read entries from directory */ /* _fx_directory_entry_write Write the new directory entry */ /* _fx_directory_free_search Search for a free directory */ /* entry */ @@ -90,6 +95,7 @@ FX_DIR_ENTRY new_dir_entry; FX_DIR_ENTRY search_directory; CHAR *new_name_ptr; ULONG i; +ULONG parent_cluster; CHAR *work_ptr; CHAR alpha, beta; #ifdef FX_RENAME_PATH_INHERIT @@ -451,20 +457,97 @@ UINT j; /* Now wipe out the old directory entry. */ status = _fx_directory_entry_write(media_ptr, &old_dir_entry); + /* Determine if the write was successful. */ + if (status != FX_SUCCESS) + { + #ifdef FX_ENABLE_FAULT_TOLERANT - /* Check for a bad status. */ + FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); +#endif /* FX_ENABLE_FAULT_TOLERANT */ + + /* Release media protection. */ + FX_UNPROTECT + + /* Return the error code. */ + return(status); + } + + /* The renamed directory itself contains a ".." entry, which stores + the starting cluster of its parent directory. If the rename moved + the directory under a different parent, that stored cluster is + stale now: ".." inside the renamed directory would still lead to + the old parent and, once the old parent's cluster is reused, to an + unrelated place. Update it in place. */ + + /* Determine the starting cluster of the new parent directory. + An empty name in the search directory marks the root + directory - the name is the only root indication, the other + fields keep earlier values in that case. A ".." entry stores + its parent's starting cluster; when the parent is the root + directory, it stores cluster 0. */ + if (search_directory.fx_dir_entry_name[0]) + { + + /* New parent is a sub-directory. */ + parent_cluster = search_directory.fx_dir_entry_cluster; + } + else + { + + /* New parent is the root directory. */ + parent_cluster = 0; + } + + /* old_dir_entry is no longer needed and still holds the starting + cluster of the renamed directory - reuse it to address that + directory as the one to read from. Ensure the cluster chain is + walked from the start. */ + old_dir_entry.fx_dir_entry_last_search_cluster = 0; + + /* Read the second directory entry of the renamed directory - its + ".." entry, created together with the directory. new_dir_entry + is no longer needed either and serves as the destination. */ + i = 1; + status = _fx_directory_entry_read(media_ptr, &old_dir_entry, &i, &new_dir_entry); + + /* Update the entry only if it is the ".." entry the FAT format + places at this position - on a non-conformant media the rename + result is left as it is - and only if the stored cluster does + not match the new parent already. A rename inside one parent + ends here without an additional write. */ + if ((status == FX_SUCCESS) && + (new_dir_entry.fx_dir_entry_name[0] == '.') && + (new_dir_entry.fx_dir_entry_name[1] == '.') && + (new_dir_entry.fx_dir_entry_name[2] == 0) && + (new_dir_entry.fx_dir_entry_cluster != parent_cluster)) + { + + /* Update the stored parent cluster. Everything else of the + entry - its position included - stays untouched: the first + two entries of a directory are a format invariant other + services rely on. */ + new_dir_entry.fx_dir_entry_cluster = parent_cluster; + + /* Write the updated ".." entry back. */ + status = _fx_directory_entry_write(media_ptr, &new_dir_entry); + } + + /* Determine if the ".." update was successful. */ if (status != FX_SUCCESS) { +#ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); +#endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT - /* Return the bad status. */ + /* Return the error code. */ return(status); } +#ifdef FX_ENABLE_FAULT_TOLERANT /* End transaction. */ status = _fx_fault_tolerant_transaction_end(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ diff --git a/test/regression_test/filex_directory_rename_test.c b/test/regression_test/filex_directory_rename_test.c index 36fd181..06265c5 100644 --- a/test/regression_test/filex_directory_rename_test.c +++ b/test/regression_test/filex_directory_rename_test.c @@ -9,12 +9,15 @@ /* SPDX-License-Identifier: MIT */ /***************************************************************************/ +// Portions of this file were generated with AI assistance. + /* This FileX test concentrates on the directory rename operations. */ #ifndef FX_STANDALONE_ENABLE #include "tx_api.h" #endif #include "fx_api.h" +#include "fx_directory.h" #include "fx_ram_driver_test.h" #include "fx_fault_tolerant.h" #include "fx_utility.h" @@ -35,6 +38,7 @@ static TX_THREAD ftest_0; #endif static FX_MEDIA ram_disk; +static FX_FILE probe_file; static CHAR max_name[FX_MAX_LONG_NAME_LEN + 2]; static CHAR max_newname[FX_MAX_LONG_NAME_LEN + 1]; @@ -55,6 +59,9 @@ static UCHAR fault_tolerant_buffer[FAULT_TOLERANT_SIZE]; void filex_directory_rename_application_define(void *first_unused_memory); static void ftest_0_entry(ULONG thread_input); +static UINT dot_dot_probe(CHAR *marker_path); +static UINT directory_cluster_read(CHAR *directory_path, ULONG *cluster_ptr); +static UINT dot_dot_cluster_read(CHAR *directory_path, ULONG *cluster_ptr); VOID _fx_ram_driver(FX_MEDIA *media_ptr); void test_control_return(UINT status); @@ -102,6 +109,90 @@ void filex_directory_rename_application_define(void *first_unused_memory) +/* Open and close the file at marker_path - a path that contains a ".." + component - to report where ".." currently leads. */ +static UINT dot_dot_probe(CHAR *marker_path) +{ + +UINT status; + + status = fx_file_open(&ram_disk, &probe_file, marker_path, FX_OPEN_FOR_READ); + if (status == FX_SUCCESS) + { + fx_file_close(&probe_file); + } + + return(status); +} + + +/* Return the starting cluster of the directory at directory_path. */ +static UINT directory_cluster_read(CHAR *directory_path, ULONG *cluster_ptr) +{ + +UINT status; +FX_DIR_ENTRY dir_entry; + + /* The directory's own entry carries its starting cluster. */ + dir_entry.fx_dir_entry_name = ram_disk.fx_media_name_buffer + FX_MAX_LONG_NAME_LEN; + dir_entry.fx_dir_entry_short_name[0] = 0; + status = _fx_directory_search(&ram_disk, directory_path, &dir_entry, FX_NULL, FX_NULL); + + /* Return the cluster only on success. */ + if (status == FX_SUCCESS) + { + *cluster_ptr = dir_entry.fx_dir_entry_cluster; + } + + return(status); +} + + +/* Read the ".." entry - entry 1 - of the directory at directory_path and + return the parent starting cluster stored in it. Verifies that the + entry actually reads back as "..". */ +static UINT dot_dot_cluster_read(CHAR *directory_path, ULONG *cluster_ptr) +{ + +UINT status; +ULONG entry_index; +FX_DIR_ENTRY dir_entry; +FX_DIR_ENTRY dot_dot_entry; + + /* Find the directory itself first. */ + dir_entry.fx_dir_entry_name = ram_disk.fx_media_name_buffer + FX_MAX_LONG_NAME_LEN; + dir_entry.fx_dir_entry_short_name[0] = 0; + status = _fx_directory_search(&ram_disk, directory_path, &dir_entry, FX_NULL, FX_NULL); + if (status != FX_SUCCESS) + { + return(status); + } + + /* Read its second directory entry - the ".." entry. */ + dir_entry.fx_dir_entry_last_search_cluster = 0; + dot_dot_entry.fx_dir_entry_name = ram_disk.fx_media_name_buffer + FX_MAX_LONG_NAME_LEN * 2; + dot_dot_entry.fx_dir_entry_short_name[0] = 0; + entry_index = 1; + status = _fx_directory_entry_read(&ram_disk, &dir_entry, &entry_index, &dot_dot_entry); + if (status != FX_SUCCESS) + { + return(status); + } + + /* Anything other than ".." at this position is a failure. */ + if ((dot_dot_entry.fx_dir_entry_name[0] != '.') || + (dot_dot_entry.fx_dir_entry_name[1] != '.') || + (dot_dot_entry.fx_dir_entry_name[2] != 0)) + { + return(FX_INVALID_STATE); + } + + *cluster_ptr = dot_dot_entry.fx_dir_entry_cluster; + + return(FX_SUCCESS); +} + + /* Define the test threads. */ static void ftest_0_entry(ULONG thread_input) @@ -109,6 +200,11 @@ static void ftest_0_entry(ULONG thread_input) UINT status; UINT i; + ULONG parent_cluster; + ULONG dot_dot_cluster; +#ifndef FX_MEDIA_STATISTICS_DISABLE + ULONG entry_writes_before; +#endif FX_PARAMETER_NOT_USED(thread_input); @@ -296,6 +392,137 @@ static void ftest_0_entry(ULONG thread_input) status = fx_media_close(&ram_disk); return_if_fail( status == FX_SUCCESS); + /* Format the media again: the checks below build their own fixture + and must not disturb - or depend on - the directory layout the + error-injection steps above are calibrated against. */ + status = fx_media_format(&ram_disk, + _fx_ram_driver, // Driver entry + ram_disk_memory, // RAM disk memory pointer + cache_buffer, // Media buffer pointer + CACHE_SIZE, // Media buffer size + "MY_RAM_DISK", // Volume Name + 1, // Number of FATs + 128, // Directory Entries + 0, // Hidden sectors + +#ifdef FX_ENABLE_FAULT_TOLERANT + 4096 * 8, // Total sectors + 256, // Sector size + 8, // Sectors per cluster +#else + 4096, // Total sectors + 128, // Sector size + 1, // Sectors per cluster +#endif + + 1, // Heads + 1); // Sectors per track + status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE); + return_if_fail( status == FX_SUCCESS); + +#ifdef FX_ENABLE_FAULT_TOLERANT + status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE); + return_if_fail( status == FX_SUCCESS); +#endif + + /* Moving a directory to a different parent has to update the ".." + entry inside the moved directory: it stores the starting cluster + of the parent directory - cluster 0 when the parent is the root + directory. Exercise every parent transition and verify both the + path resolution through ".." and the on-media entry itself. */ + status = fx_directory_create(&ram_disk, "/DOTA"); + status += fx_directory_create(&ram_disk, "/DOTB"); + status += fx_file_create(&ram_disk, "/MARKR.TXT"); + status += fx_file_create(&ram_disk, "/DOTA/MARKA.TXT"); + status += fx_file_create(&ram_disk, "/DOTB/MARKB.TXT"); + status += fx_directory_create(&ram_disk, "/DOTA/SUB"); + return_if_fail( status == FX_SUCCESS); + + /* The probe itself has to work before anything is moved: ".." of a + new directory leads to the parent it was created in. */ + status = dot_dot_probe("/DOTA/SUB/../MARKA.TXT"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_probe("/DOTA/SUB/../MARKB.TXT"); + return_if_fail( status == FX_NOT_FOUND); + + /* Move the directory to a different parent directory. Exactly one + directory entry write - the ".." update - is added to the two + parent entry writes. */ +#ifndef FX_MEDIA_STATISTICS_DISABLE + entry_writes_before = ram_disk.fx_media_directory_entry_writes; +#endif + status = fx_directory_rename(&ram_disk, "/DOTA/SUB", "/DOTB/SUB"); + return_if_fail( status == FX_SUCCESS); +#ifndef FX_MEDIA_STATISTICS_DISABLE + return_if_fail( (ram_disk.fx_media_directory_entry_writes - entry_writes_before) == 3); +#endif + + /* ".." now leads to the new parent and only to the new parent. */ + status = dot_dot_probe("/DOTB/SUB/../MARKB.TXT"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_probe("/DOTB/SUB/../MARKA.TXT"); + return_if_fail( status == FX_NOT_FOUND); + + /* The on-media ".." entry stores the new parent's starting cluster. */ + status = directory_cluster_read("/DOTB", &parent_cluster); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_cluster_read("/DOTB/SUB", &dot_dot_cluster); + return_if_fail( status == FX_SUCCESS); + return_if_fail( (dot_dot_cluster == parent_cluster) && (parent_cluster != 0)); + + /* Move the directory into the root directory: ".." stores cluster 0 + there, per the FAT convention directory create itself writes. */ + status = fx_directory_rename(&ram_disk, "/DOTB/SUB", "/SUB"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_probe("/SUB/../MARKR.TXT"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_probe("/SUB/../MARKB.TXT"); + return_if_fail( status == FX_NOT_FOUND); + status = dot_dot_cluster_read("/SUB", &dot_dot_cluster); + return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == 0)); + + /* Move the directory back out of the root directory: 0 becomes the + new parent's cluster again. */ + status = fx_directory_rename(&ram_disk, "/SUB", "/DOTA/SUB"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_probe("/DOTA/SUB/../MARKA.TXT"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_probe("/DOTA/SUB/../MARKR.TXT"); + return_if_fail( status == FX_NOT_FOUND); + status = directory_cluster_read("/DOTA", &parent_cluster); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_cluster_read("/DOTA/SUB", &dot_dot_cluster); + return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == parent_cluster) && (parent_cluster != 0)); + + /* A rename inside one parent does not touch ".." - and adds no third + directory entry write. */ + status = fx_directory_create(&ram_disk, "/DOTC"); + return_if_fail( status == FX_SUCCESS); +#ifndef FX_MEDIA_STATISTICS_DISABLE + entry_writes_before = ram_disk.fx_media_directory_entry_writes; +#endif + status = fx_directory_rename(&ram_disk, "/DOTC", "/DOTD"); + return_if_fail( status == FX_SUCCESS); +#ifndef FX_MEDIA_STATISTICS_DISABLE + return_if_fail( (ram_disk.fx_media_directory_entry_writes - entry_writes_before) == 2); +#endif + status = dot_dot_probe("/DOTD/../MARKR.TXT"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_cluster_read("/DOTD", &dot_dot_cluster); + return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == 0)); + + /* Same inside a sub-directory: ".." keeps the parent's cluster. */ + status = fx_directory_rename(&ram_disk, "/DOTA/SUB", "/DOTA/SUB2"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_probe("/DOTA/SUB2/../MARKA.TXT"); + return_if_fail( status == FX_SUCCESS); + status = dot_dot_cluster_read("/DOTA/SUB2", &dot_dot_cluster); + return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == parent_cluster)); + + /* Close the media. */ + status = fx_media_close(&ram_disk); + return_if_fail( status == FX_SUCCESS); + printf("SUCCESS!\n"); test_control_return(0); } From 45117d2496462becaad5a9e5f61ff04bf8a20688 Mon Sep 17 00:00:00 2001 From: Stefan Schnitzler Date: Thu, 15 Oct 2026 12:54:03 +0200 Subject: [PATCH 2/3] Skipped the ".." read when a directory rename keeps its parent The ".." entry of the renamed directory was read on every rename, after both parent directory entries had been written. For a rename inside one parent that read is not needed: without fault tolerance, a read error at that point made fx_directory_rename report a failure for a rename that had already completed on the media. Retain the starting cluster of the source parent before the search for the new name overwrites search_directory, and read and update the ".." entry only when the parent changed. The regression test injects a directory entry read error after the last read a rename inside the root directory needs and expects success, and lets the ".." read of a move fail: the error is reported, the directory is already relinked without fault tolerance, and unchanged on the media with fault tolerance. Assisted-by: Claude Code (Claude Fable 5.1, claude-fable-5-1) --- common/src/fx_directory_rename.c | 122 +++++++++++------- .../filex_directory_rename_test.c | 49 ++++++- 2 files changed, 122 insertions(+), 49 deletions(-) diff --git a/common/src/fx_directory_rename.c b/common/src/fx_directory_rename.c index 5e43ce8..ec397e5 100644 --- a/common/src/fx_directory_rename.c +++ b/common/src/fx_directory_rename.c @@ -95,7 +95,8 @@ FX_DIR_ENTRY new_dir_entry; FX_DIR_ENTRY search_directory; CHAR *new_name_ptr; ULONG i; -ULONG parent_cluster; +ULONG old_parent_cluster; +ULONG new_parent_cluster; CHAR *work_ptr; CHAR alpha, beta; #ifdef FX_RENAME_PATH_INHERIT @@ -219,6 +220,28 @@ UINT j; return(FX_NOT_DIRECTORY); } + /* Remember the starting cluster of the parent directory in which the + renamed directory is now located, because the search for the new + name below reuses search_directory and overwrites it with the + target's parent. + + An empty name in the search directory indicates the root directory. + For FileX, this is the only indicator of the root directory. If the + search result is the root directory, the other values of + search_directory are not updated and keep their previous contents. */ + if (search_directory.fx_dir_entry_name[0]) + { + + /* The renamed directory is in a sub-directory. */ + old_parent_cluster = search_directory.fx_dir_entry_cluster; + } + else + { + + /* The renamed directory is in the root directory. */ + old_parent_cluster = 0; + } + #ifdef FX_RENAME_PATH_INHERIT /* Determine if the source directory name has a path and the target directory name does not. */ @@ -479,72 +502,77 @@ UINT j; the old parent and, once the old parent's cluster is reused, to an unrelated place. Update it in place. */ - /* Determine the starting cluster of the new parent directory. - An empty name in the search directory marks the root - directory - the name is the only root indication, the other - fields keep earlier values in that case. A ".." entry stores - its parent's starting cluster; when the parent is the root - directory, it stores cluster 0. */ + /* Determine the starting cluster of the new parent directory. The + search directory contains the result of the previous search for + the target's parent directory. */ if (search_directory.fx_dir_entry_name[0]) { /* New parent is a sub-directory. */ - parent_cluster = search_directory.fx_dir_entry_cluster; + new_parent_cluster = search_directory.fx_dir_entry_cluster; } else { /* New parent is the root directory. */ - parent_cluster = 0; + new_parent_cluster = 0; } - /* old_dir_entry is no longer needed and still holds the starting - cluster of the renamed directory - reuse it to address that - directory as the one to read from. Ensure the cluster chain is - walked from the start. */ - old_dir_entry.fx_dir_entry_last_search_cluster = 0; - - /* Read the second directory entry of the renamed directory - its - ".." entry, created together with the directory. new_dir_entry - is no longer needed either and serves as the destination. */ - i = 1; - status = _fx_directory_entry_read(media_ptr, &old_dir_entry, &i, &new_dir_entry); - - /* Update the entry only if it is the ".." entry the FAT format - places at this position - on a non-conformant media the rename - result is left as it is - and only if the stored cluster does - not match the new parent already. A rename inside one parent - ends here without an additional write. */ - if ((status == FX_SUCCESS) && - (new_dir_entry.fx_dir_entry_name[0] == '.') && - (new_dir_entry.fx_dir_entry_name[1] == '.') && - (new_dir_entry.fx_dir_entry_name[2] == 0) && - (new_dir_entry.fx_dir_entry_cluster != parent_cluster)) + /* The next read operation reads the second directory entry in the + renamed directory. Note that the old directory entry still contains + the starting cluster of the renamed directory, which is an input + parameter for the directory entry read function. + + Please note, if a directory rename operation renames and does not + move a directory, you do not need to update the parent directory + entry (".."). + + The rename operation has already been completed successfully. If + the next read fails, the operation returns a failure. For a medium + without fault tolerance, the previous steps of the rename/move + operation will not be rolled back. Returning the failure code of + the read operation would indicate to the API level that the + rename/move operation has failed, which is not the case! */ + if (new_parent_cluster != old_parent_cluster) { - /* Update the stored parent cluster. Everything else of the - entry - its position included - stays untouched: the first - two entries of a directory are a format invariant other - services rely on. */ - new_dir_entry.fx_dir_entry_cluster = parent_cluster; + old_dir_entry.fx_dir_entry_last_search_cluster = 0; + i = 1; + status = _fx_directory_entry_read(media_ptr, &old_dir_entry, &i, &new_dir_entry); + + /* Update the entry only if it is the ".." entry the FAT format + places at this position - on a non-conformant media the rename + result is left as it is. */ + if ((status == FX_SUCCESS) && + (new_dir_entry.fx_dir_entry_name[0] == '.') && + (new_dir_entry.fx_dir_entry_name[1] == '.') && + (new_dir_entry.fx_dir_entry_name[2] == 0)) + { - /* Write the updated ".." entry back. */ - status = _fx_directory_entry_write(media_ptr, &new_dir_entry); - } + /* If the renamed directory was moved from a different parent + directory, the ".." entry points to the previous parent + directory. The next write operation changes the ".." parent + directory pointer to the correct parent directory. */ + new_dir_entry.fx_dir_entry_cluster = new_parent_cluster; - /* Determine if the ".." update was successful. */ - if (status != FX_SUCCESS) - { + /* Write the updated ".." entry back. */ + status = _fx_directory_entry_write(media_ptr, &new_dir_entry); + } + + /* Determine if the ".." update was successful. */ + if (status != FX_SUCCESS) + { #ifdef FX_ENABLE_FAULT_TOLERANT - FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); + FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ - /* Release media protection. */ - FX_UNPROTECT + /* Release media protection. */ + FX_UNPROTECT - /* Return the error code. */ - return(status); + /* Return the error code. */ + return(status); + } } #ifdef FX_ENABLE_FAULT_TOLERANT diff --git a/test/regression_test/filex_directory_rename_test.c b/test/regression_test/filex_directory_rename_test.c index 06265c5..d57aff6 100644 --- a/test/regression_test/filex_directory_rename_test.c +++ b/test/regression_test/filex_directory_rename_test.c @@ -494,15 +494,25 @@ static void ftest_0_entry(ULONG thread_input) status = dot_dot_cluster_read("/DOTA/SUB", &dot_dot_cluster); return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == parent_cluster) && (parent_cluster != 0)); - /* A rename inside one parent does not touch ".." - and adds no third - directory entry write. */ + /* A rename inside one parent does not touch "..": it adds no third + directory entry write, and it does not read the entry either. Both + parent entries are on the media by then, so without fault tolerance + an I/O error on such a read would report a failure for a rename + that has completed. Let the directory entry read after the last one + the rename needs fail: the rename reads the root directory entries + up to DOTC for the old name (4), up to the first never used entry + for the new name (5) and once more for the free entry (5) - 14 + reads, so the request is left at 1 afterwards. */ status = fx_directory_create(&ram_disk, "/DOTC"); return_if_fail( status == FX_SUCCESS); #ifndef FX_MEDIA_STATISTICS_DISABLE entry_writes_before = ram_disk.fx_media_directory_entry_writes; #endif + _fx_directory_entry_read_error_request = 15; status = fx_directory_rename(&ram_disk, "/DOTC", "/DOTD"); return_if_fail( status == FX_SUCCESS); + return_if_fail( _fx_directory_entry_read_error_request == 1); + _fx_directory_entry_read_error_request = 0; #ifndef FX_MEDIA_STATISTICS_DISABLE return_if_fail( (ram_disk.fx_media_directory_entry_writes - entry_writes_before) == 2); #endif @@ -519,6 +529,41 @@ static void ftest_0_entry(ULONG thread_input) status = dot_dot_cluster_read("/DOTA/SUB2", &dot_dot_cluster); return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == parent_cluster)); + /* An I/O error on the ".." read of a move is reported. That read is + the last directory entry read of the move: the old name is served + from the search cache the checks above filled (0), the new name + search reads the root directory entries up to the first never used + one (6), the free search reads up to the entry DOTC left free (4), + then comes the ".." entry (1) - 11 reads. */ + _fx_directory_entry_read_error_request = 11; + status = fx_directory_rename(&ram_disk, "/DOTA/SUB2", "/SUB2"); + return_if_fail( status == FX_IO_ERROR); + return_if_fail( _fx_directory_entry_read_error_request == 0); +#ifdef FX_ENABLE_FAULT_TOLERANT + + /* The transaction is rolled back. The sector cache still shows the + entries the failed transaction had logged - they never reached the + media, so reopen it to look at what is there: the directory is + still where it was, ".." included. */ + status = fx_media_close(&ram_disk); + status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE); + return_if_fail( status == FX_SUCCESS); + status = directory_cluster_read("/SUB2", &dot_dot_cluster); + return_if_fail( status == FX_NOT_FOUND); + status = dot_dot_cluster_read("/DOTA/SUB2", &dot_dot_cluster); + return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == parent_cluster)); +#else + + /* Without fault tolerance the error hits after both parent entries + were written: the directory is already at its new place, with ".." + still naming the old parent - the window only a transaction can + close. */ + status = directory_cluster_read("/DOTA/SUB2", &dot_dot_cluster); + return_if_fail( status == FX_NOT_FOUND); + status = dot_dot_cluster_read("/SUB2", &dot_dot_cluster); + return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == parent_cluster)); +#endif + /* Close the media. */ status = fx_media_close(&ram_disk); return_if_fail( status == FX_SUCCESS); From afb061550a711cf8d2c3f52c8f2c7a7ced1e60eb Mon Sep 17 00:00:00 2001 From: Stefan Schnitzler Date: Thu, 15 Oct 2026 14:46:13 +0200 Subject: [PATCH 3/3] Cleared the search cache before both lookups of a directory rename A lookup answered from the directory search cache returns the found directory itself in search_directory, not its parent. The rename relies on search_directory to tell whether the parent changed, so a warm cache made a rename inside one parent read and rewrite the ".." entry. The same cache hit on the second lookup placed the new entry inside the renamed directory when a directory was renamed to its own name, and the directory became unreachable. Clear the cache before each of the two lookups so that both report the parent. The regression test injects read faults per sector instead of per read count: a driver wrapper fails every read of one logical sector, the first sector of the renamed directory. A rename inside one parent has to succeed without that sector ever being requested, with the search cache deliberately warm; a move has to fail on it. Assisted-by: Claude Code (Claude Fable 5.1, claude-fable-5-1) --- common/src/fx_directory_rename.c | 19 +++ .../filex_directory_rename_test.c | 126 ++++++++++++++---- 2 files changed, 122 insertions(+), 23 deletions(-) diff --git a/common/src/fx_directory_rename.c b/common/src/fx_directory_rename.c index ec397e5..cbaff1a 100644 --- a/common/src/fx_directory_rename.c +++ b/common/src/fx_directory_rename.c @@ -187,6 +187,15 @@ UINT j; return(FX_WRITE_PROTECT); } +#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE + + /* Invalidate the directory search cache. A search that is answered + from the cache returns the found directory itself in + search_directory instead of its parent directory, which is needed + below to detect a change of parent. */ + media_ptr -> fx_media_last_found_name[0] = FX_NULL; +#endif + /* Search the system for the supplied directory name. */ status = _fx_directory_search(media_ptr, old_directory_name, &old_dir_entry, &search_directory, FX_NULL); @@ -321,6 +330,16 @@ UINT j; } #endif +#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE + + /* Invalidate the directory search cache again. The search above has + filled it with the directory to be renamed: a new name equal to the + old name would be answered from the cache, with the directory + itself in search_directory, and the new entry would then be placed + inside the renamed directory. */ + media_ptr -> fx_media_last_found_name[0] = FX_NULL; +#endif + /* Search the media for the new directory name - including any supplied path. */ status = _fx_directory_search(media_ptr, new_directory_name, &new_dir_entry, &search_directory, &new_name_ptr); diff --git a/test/regression_test/filex_directory_rename_test.c b/test/regression_test/filex_directory_rename_test.c index d57aff6..dc058cd 100644 --- a/test/regression_test/filex_directory_rename_test.c +++ b/test/regression_test/filex_directory_rename_test.c @@ -39,6 +39,8 @@ static TX_THREAD ftest_0; #endif static FX_MEDIA ram_disk; static FX_FILE probe_file; +static ULONG read_fault_sector; /* 0: every sector is readable */ +static UINT read_fault_hits; static CHAR max_name[FX_MAX_LONG_NAME_LEN + 2]; static CHAR max_newname[FX_MAX_LONG_NAME_LEN + 1]; @@ -62,6 +64,8 @@ static void ftest_0_entry(ULONG thread_input); static UINT dot_dot_probe(CHAR *marker_path); static UINT directory_cluster_read(CHAR *directory_path, ULONG *cluster_ptr); static UINT dot_dot_cluster_read(CHAR *directory_path, ULONG *cluster_ptr); +static UINT directory_first_sector_get(CHAR *directory_path, ULONG *sector_ptr); +static VOID ram_driver_read_fault(FX_MEDIA *media_ptr); VOID _fx_ram_driver(FX_MEDIA *media_ptr); void test_control_return(UINT status); @@ -193,6 +197,49 @@ FX_DIR_ENTRY dot_dot_entry; } +/* Return the first sector of the directory at directory_path - the sector + that holds its "." and ".." entries. */ +static UINT directory_first_sector_get(CHAR *directory_path, ULONG *sector_ptr) +{ + +UINT status; +ULONG cluster; + + status = directory_cluster_read(directory_path, &cluster); + if (status == FX_SUCCESS) + { + *sector_ptr = ram_disk.fx_media_data_sector_start + + (cluster - FX_FAT_ENTRY_START) * ram_disk.fx_media_sectors_per_cluster; + } + + return(status); +} + + +/* This is a RAM driver which may fail to read one logical sector. A read + request that covers read_fault_sector will result in an FX_IO_ERROR + response and will be counted in read_fault_hits. All other driver + requests are forwarded to the RAM driver. + + read_fault_sector is used as a flag and a switch: 0 denotes the boot + sector and disables fault injection. */ +static VOID ram_driver_read_fault(FX_MEDIA *media_ptr) +{ + + if ((read_fault_sector != 0) && + (media_ptr -> fx_media_driver_request == FX_DRIVER_READ) && + (media_ptr -> fx_media_driver_logical_sector <= read_fault_sector) && + (read_fault_sector < media_ptr -> fx_media_driver_logical_sector + media_ptr -> fx_media_driver_sectors)) + { + read_fault_hits++; + media_ptr -> fx_media_driver_status = FX_IO_ERROR; + return; + } + + _fx_ram_driver(media_ptr); +} + + /* Define the test threads. */ static void ftest_0_entry(ULONG thread_input) @@ -394,7 +441,8 @@ static void ftest_0_entry(ULONG thread_input) /* Format the media again: the checks below build their own fixture and must not disturb - or depend on - the directory layout the - error-injection steps above are calibrated against. */ + error-injection steps above are calibrated against. They open the + media through a driver wrapper that can make one sector unreadable. */ status = fx_media_format(&ram_disk, _fx_ram_driver, // Driver entry ram_disk_memory, // RAM disk memory pointer @@ -417,7 +465,7 @@ static void ftest_0_entry(ULONG thread_input) 1, // Heads 1); // Sectors per track - status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE); + status += fx_media_open(&ram_disk, "RAM DISK", ram_driver_read_fault, ram_disk_memory, cache_buffer, CACHE_SIZE); return_if_fail( status == FX_SUCCESS); #ifdef FX_ENABLE_FAULT_TOLERANT @@ -494,25 +542,46 @@ static void ftest_0_entry(ULONG thread_input) status = dot_dot_cluster_read("/DOTA/SUB", &dot_dot_cluster); return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == parent_cluster) && (parent_cluster != 0)); - /* A rename inside one parent does not touch "..": it adds no third - directory entry write, and it does not read the entry either. Both - parent entries are on the media by then, so without fault tolerance - an I/O error on such a read would report a failure for a rename - that has completed. Let the directory entry read after the last one - the rename needs fail: the rename reads the root directory entries - up to DOTC for the old name (4), up to the first never used entry - for the new name (5) and once more for the free entry (5) - 14 - reads, so the request is left at 1 afterwards. */ + /* The ".." entry of the renamed directory references its parent + directory. Two cases follow from the implementation: + + Case 1: The directory is moved to a different parent. The ".." + entry has to be updated, which requires reading it first. The + moves above cover this case; a read error on that read is covered + at the end of this test. + + Case 2: The directory is only renamed within its parent. The ".." + entry stays valid and must be neither updated nor read. Both + parent entries are on the media by then, so without fault + tolerance an I/O error on that read would report a failure for a + rename that has already completed. + + The next test covers case 2 in the root directory. The sector that + holds the entries of the renamed directory itself - "." and ".." + among them - is made unreadable, and the sector cache is emptied + so that a read of it would have to reach the driver. A rename + inside one parent works on the parent's entries only: it has to + succeed without the driver ever being asked for that sector. It + adds no third directory entry write either. + + Looking up the sector leaves the directory search cache warm for + the name being renamed. That is intended: answered from the cache, + a search reports the directory itself instead of its parent, and + the rename has to cope with that. */ status = fx_directory_create(&ram_disk, "/DOTC"); return_if_fail( status == FX_SUCCESS); + status = directory_first_sector_get("/DOTC", &read_fault_sector); + return_if_fail( status == FX_SUCCESS); + status = fx_media_cache_invalidate(&ram_disk); + return_if_fail( status == FX_SUCCESS); #ifndef FX_MEDIA_STATISTICS_DISABLE entry_writes_before = ram_disk.fx_media_directory_entry_writes; #endif - _fx_directory_entry_read_error_request = 15; + read_fault_hits = 0; status = fx_directory_rename(&ram_disk, "/DOTC", "/DOTD"); + read_fault_sector = 0; return_if_fail( status == FX_SUCCESS); - return_if_fail( _fx_directory_entry_read_error_request == 1); - _fx_directory_entry_read_error_request = 0; + return_if_fail( read_fault_hits == 0); #ifndef FX_MEDIA_STATISTICS_DISABLE return_if_fail( (ram_disk.fx_media_directory_entry_writes - entry_writes_before) == 2); #endif @@ -521,24 +590,35 @@ static void ftest_0_entry(ULONG thread_input) status = dot_dot_cluster_read("/DOTD", &dot_dot_cluster); return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == 0)); - /* Same inside a sub-directory: ".." keeps the parent's cluster. */ + /* Case 2 inside a sub-directory: ".." keeps the parent's cluster, and + the directory's own sector is not needed here either. */ + status = directory_first_sector_get("/DOTA/SUB", &read_fault_sector); + return_if_fail( status == FX_SUCCESS); + status = fx_media_cache_invalidate(&ram_disk); + return_if_fail( status == FX_SUCCESS); + read_fault_hits = 0; status = fx_directory_rename(&ram_disk, "/DOTA/SUB", "/DOTA/SUB2"); + read_fault_sector = 0; return_if_fail( status == FX_SUCCESS); + return_if_fail( read_fault_hits == 0); status = dot_dot_probe("/DOTA/SUB2/../MARKA.TXT"); return_if_fail( status == FX_SUCCESS); status = dot_dot_cluster_read("/DOTA/SUB2", &dot_dot_cluster); return_if_fail( (status == FX_SUCCESS) && (dot_dot_cluster == parent_cluster)); - /* An I/O error on the ".." read of a move is reported. That read is - the last directory entry read of the move: the old name is served - from the search cache the checks above filled (0), the new name - search reads the root directory entries up to the first never used - one (6), the free search reads up to the entry DOTC left free (4), - then comes the ".." entry (1) - 11 reads. */ - _fx_directory_entry_read_error_request = 11; + /* Case 1 with a read error: a move has to read the ".." entry, and + that is the only read of the moved directory's own sector a move + needs. With that sector unreadable the move fails, and the error + is reported. */ + status = directory_first_sector_get("/DOTA/SUB2", &read_fault_sector); + return_if_fail( status == FX_SUCCESS); + status = fx_media_cache_invalidate(&ram_disk); + return_if_fail( status == FX_SUCCESS); + read_fault_hits = 0; status = fx_directory_rename(&ram_disk, "/DOTA/SUB2", "/SUB2"); + read_fault_sector = 0; return_if_fail( status == FX_IO_ERROR); - return_if_fail( _fx_directory_entry_read_error_request == 0); + return_if_fail( read_fault_hits == 1); #ifdef FX_ENABLE_FAULT_TOLERANT /* The transaction is rolled back. The sector cache still shows the