fat: fix parent link count when replacing a sibling directory
Subsystems (2)
PATCH 1/1 fat: fix parent link count when replacing a sibling directory
vfat_rename() and do_msdos_rename() adjust parent link counts only when updating the dotdot entry for a move between different parents. Replacing an existing empty directory in the same parent also removes a subdirectory, but leaves the parent's link count unchanged.
For example, renaming one of two sibling directories over the other leaves the parent with four links instead of three. stat and ls report the stale count, and find -links 3 does not match the parent.
Drop the parent's link count for this case after the source entry has been removed successfully. Leave cross-parent renames, regular files and the existing error paths unchanged.
--- fs/fat/namei_msdos.c | 2 ++ fs/fat/namei_vfat.c | 2 ++ 2 files changed, 4 insertions(+)
fs/fat/namei_msdos.c +2 -0
@@ -565,6 +565,8 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name, (void )sync_inode_metadata (old_dir,1 );if (new_inode) { +if (is_dir && old_dir == new_dir) +drop_nlink (old_dir);drop_nlink (new_inode);if (is_dir)drop_nlink (new_inode);
fs/fat/namei_vfat.c +2 -0
@@ -1011,6 +1011,8 @@ static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,vfat_update_dir_metadata (old_dir, &ts);if (new_inode) { +if (is_dir && old_dir == new_dir) +drop_nlink (old_dir);drop_nlink (new_inode);if (is_dir)drop_nlink (new_inode);
Signature · 1 line
2.43.0
Quoted 9 lines · Hungyu Lin
vfat_rename()anddo_msdos_rename()adjust parent link counts only when updating the dotdot entry for a move between different parents. Replacing an existing empty directory in the same parent also removes a subdirectory, but leaves the parent's link count unchanged.For example, renaming one of two sibling directories over the other leaves the parent with four links instead of three. stat and ls report the stale count, and find -links 3 does not match the parent.
Quoted 5 lines
Drop the parent's link count for this case after the source entry has been removed successfully. Leave cross-parent renames, regular files and the existing error paths unchanged.
Signed-off-by Hungyu Lin <[email protected]>
Looks good. Thanks.
Quoted 31 lines
--- fs/fat/namei_msdos.c | 2 ++ fs/fat/namei_vfat.c | 2 ++ 2 files changed, 4 insertions(+)fs/fat/namei_msdos.c +2
-0@@ -565,6 +565,8 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name, (void )sync_inode_metadata (old_dir,1 );if (new_inode) { +if (is_dir && old_dir == new_dir) +drop_nlink (old_dir);drop_nlink (new_inode);if (is_dir)drop_nlink (new_inode);fs/fat/namei_vfat.c +2
-0@@ -1011,6 +1011,8 @@ static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,vfat_update_dir_metadata (old_dir, &ts);if (new_inode) { +if (is_dir && old_dir == new_dir) +drop_nlink (old_dir);drop_nlink (new_inode);if (is_dir)drop_nlink (new_inode);
Signature · 1 line
OGAWA Hirofumi <[email protected]>
Quoted 15 lines · On Thu, 29 Oct 2026 14:57:43 +0900 OGAWA Hirofumi…
Quoted 13 lines · Hungyu Lin
vfat_rename()anddo_msdos_rename()adjust parent link counts only when updating the dotdot entry for a move between different parents. Replacing an existing empty directory in the same parent also removes a subdirectory, but leaves the parent's link count unchanged.For example, renaming one of two sibling directories over the other leaves the parent with four links instead of three. stat and ls report the stale count, and find -links 3 does not match the parent.
Drop the parent's link count for this case after the source entry has been removed successfully. Leave cross-parent renames, regular files and the existing error paths unchanged.
Quoted 5 lines
Quoted 1 line
Signed-off-by Hungyu Lin <[email protected]>Looks good. Thanks.
Acked-by OGAWA Hirofumi <[email protected]>
Thanks. Sashiko asked a thing:
https://sashiko.dev/#/patchset/[email protected]
OGAWA Hirofumi
maintainerQuoted 17 lines · Andrew Morton
Quoted 15 lines · On Thu, 29 Oct 2026 14:57:43 +0900 OGAWA Hirofumi…
Quoted 13 lines · Hungyu Lin
vfat_rename()anddo_msdos_rename()adjust parent link counts only when updating the dotdot entry for a move between different parents. Replacing an existing empty directory in the same parent also removes a subdirectory, but leaves the parent's link count unchanged.For example, renaming one of two sibling directories over the other leaves the parent with four links instead of three. stat and ls report the stale count, and find -links 3 does not match the parent.
Drop the parent's link count for this case after the source entry has been removed successfully. Leave cross-parent renames, regular files and the existing error paths unchanged.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Hungyu Lin <[email protected]>Looks good. Thanks.
Acked-by OGAWA Hirofumi <[email protected]>Thanks. Sashiko asked a thing:
https://sashiko.dev/#/patchset/[email protected]
Hm, sure. It is possible what sashiko says. This patch itself should be ok though.
Hungyu, can you add ->i_nlink check for corrupted dir?
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>