From f18a398634fb48cc20cd5c468f8cd0be7743ecc2 Mon Sep 17 00:00:00 2001 From: roy Date: Mon, 19 Feb 2024 14:56:15 +0800 Subject: [PATCH 1/2] Fix rm hard link problem Add the inode name compration in the unlink function Since the hard link uses the same inode number as the original file inode, it's necessary to check the name to prevent deletion of the wrong inode. --- inode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inode.c b/inode.c index 19f558c..a3057d3 100644 --- a/inode.c +++ b/inode.c @@ -472,7 +472,8 @@ static int simplefs_remove_from_dir(struct inode *dir, struct dentry *dentry) } /* Remove file from parent directory */ for (fi = 0; fi < SIMPLEFS_FILES_PER_BLOCK; fi++) { - if (dblock->files[fi].inode == inode->i_ino) { + if (dblock->files[fi].inode == inode->i_ino && + !strcmp(dblock->files[fi].filename, dentry->d_name.name) ) { found = true; if (fi != SIMPLEFS_FILES_PER_BLOCK - 1) { memmove(dblock->files + fi, dblock->files + fi + 1, From 6958909c166e054587919b1fb5a3d93518fd2131 Mon Sep 17 00:00:00 2001 From: roy Date: Wed, 21 Feb 2024 16:40:25 +0800 Subject: [PATCH 2/2] Resolve cache leak from hard link Add the 'ihold()' into 'simplefs_link' function The hard link cache and old file cache are both in the cache list, the "kill_block_super" attempts to free the inode cache, it will decrement "i_count" twice. Therefore, if 'i_count' is not 0, 'destroy_inode' will not be called. --- inode.c | 1 + 1 file changed, 1 insertion(+) diff --git a/inode.c b/inode.c index a3057d3..2d0361a 100644 --- a/inode.c +++ b/inode.c @@ -897,6 +897,7 @@ static int simplefs_link(struct dentry *old_dentry, brelse(bh); inode_inc_link_count(inode); + ihold(inode); d_instantiate(dentry, inode); return ret;