[RFC PATCH v1 4/6] exfat: lock FAT2 buffer while copying mirrored FAT entries

Chi Zhiling <[email protected]>
Newsgroups dev.linux.lists.exfat,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
From: Chi Zhiling <[email protected]>

exfat_mirror_bh() updates the FAT2 buffer without serializing the copy
against writeback. Once s_lock is converted to a read-write lock,
multiple read-side operations may run concurrently with writeback.
This can allow writeback to observe a partially updated FAT2 block.

Lock the destination buffer while copying the FAT entry data to
serialize the update with writeback and prevent torn FAT2 writes.

Signed-off-by: Chi Zhiling <[email protected]>
---
 fs/exfat/fatent.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/exfat/fatent.c b/fs/exfat/fatent.c
index 2db959669563..f6ad2825ad19 100644
--- a/fs/exfat/fatent.c
+++ b/fs/exfat/fatent.c
@@ -24,7 +24,10 @@ static int exfat_mirror_bh(struct super_block *sb, struct buffer_head *bh)
 		c_bh = sb_getblk(sb, sec2);
 		if (!c_bh)
 			return -ENOMEM;
+		/* Serialize the copy with writeback to avoid a torn FAT2 write */
+		lock_buffer(c_bh);
 		memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
+		unlock_buffer(c_bh);
 		err = exfat_update_bh(c_bh, sb->s_flags & SB_SYNCHRONOUS);
 		brelse(c_bh);
 	}
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.