[PATCH v6 07/37] ntfs: retain directory iteration state after errors
From: Xuewen Wang
Date: Wed Nov 18 2026 - 04:08:00 EST
From: Xuewen Wang <wangxuewen@xxxxxxxxxx>
ntfs_readdir() marks the directory exhausted after an error and returns
success. A retry can consequently miss entries. A saved resume key that
was deleted is also treated as a lookup failure.
Return traversal errors without setting the end-of-iteration flag. Resume
from the insertion position when the saved key is gone. Advance the saved
offset after consuming an entry and keep an unconsumed key for retries.
Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
Signed-off-by: Xuewen Wang <wangxuewen@xxxxxxxxxx>
---
Changes in v6:
Extract directory retry-state handling from v5 patch 20.
Validation:
- Compiled and linked the NTFS module object with W=1 at this stage.
- No undefined references to ntfs_report_* or fserror_report_*.
fs/ntfs/dir.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/fs/ntfs/dir.c b/fs/ntfs/dir.c
index a93f3566603b..ed1d4188e947 100644
--- a/fs/ntfs/dir.c
+++ b/fs/ntfs/dir.c
@@ -819,7 +819,9 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
err = ntfs_index_lookup(private->key,
le16_to_cpu(private->key_length),
ictx);
- if (!err) {
+ if (!err || err == -ENOENT) {
+ /* A deleted resume key leaves the next entry in the context. */
+ err = 0;
next = ictx->entry;
/*
* Update ie_pos with private->curr_pos
@@ -833,6 +835,8 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
goto out;
}
+ if (next->flags & INDEX_ENTRY_END)
+ goto next_entry;
goto nextdir;
} else {
goto out;
@@ -915,6 +919,7 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
goto nextdir;
while (1) {
+next_entry:
next = ntfs_index_next(next, ictx);
if (IS_ERR(next)) {
err = PTR_ERR(next);
@@ -1003,7 +1008,11 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
private->key_length = next->key_length;
break;
}
+ /* The saved key is consumed; retry from the next offset. */
+ kfree(private->key);
+ private->key = NULL;
ie_pos += le16_to_cpu(next->length);
+ actor->pos = ie_pos;
}
if (!err)
@@ -1029,9 +1038,8 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
if (err) {
if (private) {
private->curr_pos = actor->pos;
- private->end_in_iterate = true;
+ private->end_in_iterate = false;
}
- err = 0;
}
ntfs_index_ctx_put(ictx);
kfree(name);
--
2.51.0