* [PATCH] fat: fix parent link count when replacing a sibling directory
@ 2026-10-28 21:16 Hungyu Lin
2026-10-29 5:57 ` OGAWA Hirofumi
0 siblings, 1 reply; 4+ messages in thread
From: Hungyu Lin @ 2026-10-28 21:16 UTC (permalink / raw)
To: hirofumi; +Cc: brauner, linux-fsdevel, linux-kernel, dennylin0707
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.
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
fs/fat/namei_msdos.c | 2 ++
fs/fat/namei_vfat.c | 2 ++
2 files changed, 4 insertions(+)
diff --git a/fs/fat/namei_msdos.c b/fs/fat/namei_msdos.c
index c7500c2859e0..570dd75dd275 100644
--- a/fs/fat/namei_msdos.c
+++ b/fs/fat/namei_msdos.c
@@ -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);
diff --git a/fs/fat/namei_vfat.c b/fs/fat/namei_vfat.c
index ff7652bf0410..d7e2df5bf7e5 100644
--- a/fs/fat/namei_vfat.c
+++ b/fs/fat/namei_vfat.c
@@ -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);
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] fat: fix parent link count when replacing a sibling directory
2026-10-28 21:16 [PATCH] fat: fix parent link count when replacing a sibling directory Hungyu Lin
@ 2026-10-29 5:57 ` OGAWA Hirofumi
2026-10-29 19:15 ` Andrew Morton
0 siblings, 1 reply; 4+ messages in thread
From: OGAWA Hirofumi @ 2026-10-29 5:57 UTC (permalink / raw)
To: Andrew Morton; +Cc: Hungyu Lin, brauner, linux-fsdevel, linux-kernel
Hungyu Lin <dennylin0707@gmail.com> writes:
> 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.
>
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
Looks good. Thanks.
Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
> ---
> fs/fat/namei_msdos.c | 2 ++
> fs/fat/namei_vfat.c | 2 ++
> 2 files changed, 4 insertions(+)
>
> diff --git a/fs/fat/namei_msdos.c b/fs/fat/namei_msdos.c
> index c7500c2859e0..570dd75dd275 100644
> --- a/fs/fat/namei_msdos.c
> +++ b/fs/fat/namei_msdos.c
> @@ -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);
> diff --git a/fs/fat/namei_vfat.c b/fs/fat/namei_vfat.c
> index ff7652bf0410..d7e2df5bf7e5 100644
> --- a/fs/fat/namei_vfat.c
> +++ b/fs/fat/namei_vfat.c
> @@ -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);
--
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] fat: fix parent link count when replacing a sibling directory
2026-10-29 5:57 ` OGAWA Hirofumi
@ 2026-10-29 19:15 ` Andrew Morton
2026-10-29 19:59 ` OGAWA Hirofumi
0 siblings, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2026-10-29 19:15 UTC (permalink / raw)
To: OGAWA Hirofumi; +Cc: Hungyu Lin, brauner, linux-fsdevel, linux-kernel
On Thu, 29 Oct 2026 14:57:43 +0900 OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> wrote:
> Hungyu Lin <dennylin0707@gmail.com> writes:
>
> > 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.
> >
> > Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
>
> Looks good. Thanks.
>
> Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Thanks. Sashiko asked a thing:
https://sashiko.dev/#/patchset/20261028211626.26103-1-dennylin0707@gmail.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] fat: fix parent link count when replacing a sibling directory
2026-10-29 19:15 ` Andrew Morton
@ 2026-10-29 19:59 ` OGAWA Hirofumi
0 siblings, 0 replies; 4+ messages in thread
From: OGAWA Hirofumi @ 2026-10-29 19:59 UTC (permalink / raw)
To: Andrew Morton; +Cc: Hungyu Lin, brauner, linux-fsdevel, linux-kernel
Andrew Morton <akpm@linux-foundation.org> writes:
> On Thu, 29 Oct 2026 14:57:43 +0900 OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> wrote:
>
>> Hungyu Lin <dennylin0707@gmail.com> writes:
>>
>> > 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.
>> >
>> > Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
>>
>> Looks good. Thanks.
>>
>> Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
>
> Thanks. Sashiko asked a thing:
> https://sashiko.dev/#/patchset/20261028211626.26103-1-dennylin0707@gmail.com
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.
--
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-10-29 19:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-28 21:16 [PATCH] fat: fix parent link count when replacing a sibling directory Hungyu Lin
2026-10-29 5:57 ` OGAWA Hirofumi
2026-10-29 19:15 ` Andrew Morton
2026-10-29 19:59 ` OGAWA Hirofumi
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.