Skip to contentSkip to conversation

fat: fix parent link count when replacing a sibling directory

PATCH Superseded Hungyu Lin 3 participants 4 messages Oct 28 – Oct 29

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

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
index c7500c2859e0..570dd75dd275 100644
@@ -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
index ff7652bf0410..d7e2df5bf7e5 100644
@@ -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
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Hungyu Lin

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.

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.

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
index c7500c2859e0..570dd75dd275 100644
@@ -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
index ff7652bf0410..d7e2df5bf7e5 100644
@@ -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]>
#2 lore raw
Quoted 15 lines · On Thu, 29 Oct 2026 14:57:43 +0900 OGAWA Hirofumi…
Quoted 13 lines · Hungyu Lin

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.

Quoted 5 lines
Quoted 1 line

Looks good. Thanks.

Thanks. Sashiko asked a thing:

	https://sashiko.dev/#/patchset/[email protected]
maintainer
#3 lore raw
Quoted 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() 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.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Looks good. Thanks.

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]>