From f85250ad8b941d8bfb91bd1ec71254de8df5e741 Mon Sep 17 00:00:00 2001 From: ibat10clw Date: Mon, 30 Mar 2026 15:12:28 +0800 Subject: [PATCH] Fix missing entries in simplefs_lookup simplefs_lookup must scan all entries in a directory before it can determine whether a file exists. The current implementation stops early when it encounters a hole in the directory, which can cause later files to be missed. In addition, fi is used as the index into dblock, so the loop should be bounded by SIMPLEFS_FILES_PER_BLOCK rather than dblock->nr_files. If a file is located near the end of the directory block, the loop may terminate early when fi >= dblock->nr_files, resulting in an incomplete scan. Fix this by skipping holes during filename comparison and continuing to advance fi until all live directory entries have been examined. --- inode.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/inode.c b/inode.c index 4ba8b73..8fe7bfb 100644 --- a/inode.c +++ b/inode.c @@ -151,21 +151,21 @@ static struct dentry *simplefs_lookup(struct inode *dir, return ERR_PTR(-EIO); dblock = (struct simplefs_dir_block *) bh2->b_data; - + int nr_files = dblock->nr_files; /* Search file in ei_block */ - for (fi = 0; fi < dblock->nr_files;) { + for (fi = 0; nr_files && fi < SIMPLEFS_FILES_PER_BLOCK;) { f = &dblock->files[fi]; - if (!f->inode) { - brelse(bh2); - goto search_end; - } - if (!strncmp(f->filename, dentry->d_name.name, - SIMPLEFS_FILENAME_LEN)) { - inode = simplefs_iget(sb, f->inode); - brelse(bh2); - goto search_end; + + if (f->inode) { + nr_files--; + if (!strncmp(f->filename, dentry->d_name.name, + SIMPLEFS_FILENAME_LEN)) { + inode = simplefs_iget(sb, f->inode); + brelse(bh2); + goto search_end; + } } - fi += dblock->files[fi].nr_blk; + fi += f->nr_blk; } brelse(bh2); bh2 = NULL;