Re: [PATCH v5 3/4] fat: add renameat2 RENAME_EXCHANGE flag support
From: OGAWA Hirofumi
Date: Thu Jun 09 2022 - 15:17:49 EST
Javier Martinez Canillas <javierm@xxxxxxxxxx> writes:
> +static void vfat_update_nlink(struct inode *dir, struct inode *inode)
> +{
> + if (S_ISDIR(inode->i_mode))
> + drop_nlink(dir);
> + else
> + inc_nlink(dir);
> +}
[...]
> + vfat_update_dir_metadata(old_dir, &ts);
> + /* if directories are not the same, update new_dir as well */
> + if (old_dir != new_dir) {
> + vfat_update_dir_metadata(new_dir, &ts);
> + /* nlink only needs to be updated if the file types differ */
> + if (old_inode->i_mode != new_inode->i_mode) {
> + vfat_update_nlink(old_dir, old_inode);
> + vfat_update_nlink(new_dir, new_inode);
> + }
> + }
Looks like unnecessary complex (and comparing raw i_mode, not S_ISDIR(),
better to change before make dir dirty). How about this change, it is
only tested slightly though? Can you review and test?
Thanks.
--
OGAWA Hirofumi <hirofumi@xxxxxxxxxxxxxxxxxx>
Signed-off-by: OGAWA Hirofumi <hirofumi@xxxxxxxxxxxxxxxxxx>
---
fs/fat/namei_vfat.c | 25 ++++++++++++-------------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/fs/fat/namei_vfat.c b/fs/fat/namei_vfat.c
index c30f829..3aef834 100644
--- a/fs/fat/namei_vfat.c 2022-06-10 03:33:40.334212176 +0900
+++ b/fs/fat/namei_vfat.c 2022-06-10 04:11:30.817064192 +0900
@@ -1055,12 +1055,10 @@ static void vfat_exchange_ipos(struct in
fat_attach(new_inode, old_i_pos);
}
-static void vfat_update_nlink(struct inode *dir, struct inode *inode)
+static void vfat_move_nlink(struct inode *src, struct inode *dst)
{
- if (S_ISDIR(inode->i_mode))
- drop_nlink(dir);
- else
- inc_nlink(dir);
+ drop_nlink(src);
+ inc_nlink(dst);
}
static int vfat_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
@@ -1112,7 +1110,6 @@ static int vfat_rename_exchange(struct i
if (err)
goto error_old_dotdot;
}
-
if (new_dotdot_de) {
err = vfat_update_dotdot_de(old_dir, new_inode, new_dotdot_bh,
new_dotdot_de);
@@ -1120,16 +1117,18 @@ static int vfat_rename_exchange(struct i
goto error_new_dotdot;
}
+ /* if cross directory and only one is a directory, adjust nlink */
+ if (!old_dotdot_de != !new_dotdot_de) {
+ if (old_dotdot_de)
+ vfat_move_nlink(old_dir, new_dir);
+ else
+ vfat_move_nlink(new_dir, old_dir);
+ }
+
vfat_update_dir_metadata(old_dir, &ts);
/* if directories are not the same, update new_dir as well */
- if (old_dir != new_dir) {
+ if (old_dir != new_dir)
vfat_update_dir_metadata(new_dir, &ts);
- /* nlink only needs to be updated if the file types differ */
- if (old_inode->i_mode != new_inode->i_mode) {
- vfat_update_nlink(old_dir, old_inode);
- vfat_update_nlink(new_dir, new_inode);
- }
- }
out:
brelse(old_dotdot_bh);
_