Re: [PATCH 1/1] fat: restore original value when fat_ent_write failed

From: OGAWA Hirofumi

Date: Wed Jun 03 2026 - 02:22:04 EST


Ren Wei <n05ec@xxxxxxxxxx> writes:

> From: Yemu Lu <prcups@xxxxxxxx>
>
> fat_ent_write() may have committed the new link to the primary FAT
> but then failed on the mirror copy, leaving the chain pointing to
> new_dclus even though the caller will free it. Restore the original
> value to keep the chain consistent.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@xxxxxxxxxx
> Reported-by: Yuan Tan <yuantan098@xxxxxxxxx>
> Reported-by: Yifan Wu <yifanwucs@xxxxxxxxx>
> Reported-by: Juefei Pu <tomapufckgml@xxxxxxxxx>
> Reported-by: Xin Liu <bird@xxxxxxxxxx>
> Signed-off-by: Yemu Lu <prcups@xxxxxxxx>
> Signed-off-by: Ren Wei <n05ec@xxxxxxxxxx>

Acked-by: OGAWA Hirofumi <hirofumi@xxxxxxxxxxxxxxxxxx>

> ---
> fs/fat/misc.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/fs/fat/misc.c b/fs/fat/misc.c
> index 3027ef53af21..710e2e6aa1a5 100644
> --- a/fs/fat/misc.c
> +++ b/fs/fat/misc.c
> @@ -133,7 +133,11 @@ 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);
> fatent_brelse(&fatent);
> }
> if (ret < 0)

--
OGAWA Hirofumi <hirofumi@xxxxxxxxxxxxxxxxxx>