fat: fix fat_ent_write() for reverting the value

commit 64d9183203 ("fat: restore original value when fat_ent_write
failed") try to revert the fatent value to old value when got the error on
mirror FAT.

However it didn't work if the error is when writing the fatent bh.  In
that case, the bh is cleared the uptodate flag, so reuse bh is invalid.

Fix this by reverting the fatent only if got the error on mirror FAT.

Link: https://lore.kernel.org/87ik4yz9fv.fsf_-_@mail.parknet.co.jp
Fixes: 64d9183203 ("fat: restore original value when fat_ent_write failed")
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: syzbot+e64c6472a3d96a75172a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e64c6472a3d96a75172a
Reported-by: syzbot+26461e903494e689c24f@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=26461e903494e689c24f
Cc: Yemu Lu <prcups@krgm.moe>
Cc: Ren Wei <n05ec@lzu.edu.cn>
Cc: Yuan Tan <yuantan098@gmail.com>
Cc: Yifan Wu <yifanwucs@gmail.com>
Cc: Juefei Pu <tomapufckgml@gmail.com>
Cc: Xin Liu <bird@lzu.edu.cn>
Cc: <stable@vger.kernel.org>
This commit is contained in:
OGAWA Hirofumi 2026-08-25 21:11:32 +09:00 committed by Andrew Morton
parent 405c00e43a
commit 63c4b67ee1
4 changed files with 23 additions and 9 deletions

View File

@ -392,7 +392,7 @@ extern void fat_ent_access_init(struct super_block *sb);
extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent,
int entry);
extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
int new, int wait);
int new, int old, int wait);
extern int fat_alloc_clusters(struct inode *inode, int *cluster,
int nr_cluster);
extern int fat_free_clusters(struct inode *inode, int cluster);

View File

@ -413,7 +413,7 @@ static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs,
}
int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
int new, int wait)
int new, int old, int wait)
{
struct super_block *sb = inode->i_sb;
const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
@ -422,10 +422,25 @@ int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
ops->ent_put(fatent, new);
if (wait) {
err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
if (err)
if (err) {
/*
* bhs are not uptodate after I/O error. So we
* can't simply re-dirty to revert. And it
* would not have value to write again on I/O
* error.
*/
return err;
}
}
return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
err = fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
if (err) {
/* Try to revert if got the error on mirror FAT */
ops->ent_put(fatent, old);
if (wait)
fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
}
return err;
}
static inline int fat_ent_next(struct msdos_sb_info *sbi,

View File

@ -363,7 +363,8 @@ static int fat_free(struct inode *inode, int skip)
__func__, MSDOS_I(inode)->i_pos);
ret = -EIO;
} else if (ret > 0) {
err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, ret,
wait);
if (err)
ret = err;
}

View File

@ -133,11 +133,9 @@ int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
ret = fat_ent_read(inode, &fatent, last);
if (ret >= 0) {
int wait = inode_needs_sync(inode);
int old = ret;
ret = fat_ent_write(inode, &fatent, new_dclus, wait);
if (ret < 0)
fat_ent_write(inode, &fatent, old, wait);
ret = fat_ent_write(inode, &fatent, new_dclus, ret,
wait);
fatent_brelse(&fatent);
}
if (ret < 0)