exfat: hold s_lock for cluster-chain updates in exfat_cont_expand()
PATCH 1/1 exfat: hold s_lock for cluster-chain updates in exfat_cont_expand()
From: Youling Tang <[email protected]>
exfat_cont_expand() allocates clusters, appends them to the FAT chain and updates ei->start_clu, ei->flags and i_blocks without holding sbi->s_lock. Readers such as writeback call exfat_map_cluster() with s_lock held but without inode_lock, so they are not serialized against cont_expand() and can observe the chain metadata mid-update.
Hold s_lock around the cluster allocation, chain append and i_blocks update. i_size is published via exfat_pagecache_isize_extended(), which takes the straddling folio lock; since writeback takes folio lock -> s_lock, this must happen after s_lock is released to avoid inverting the lock order. i_blocks is therefore updated first, under s_lock, which also ensures a reader never sees i_size beyond the allocated chain.
--- fs/exfat/file.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
fs/exfat/file.c +14 -5
@@ -95,6 +95,7 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)if (ret)return ret; +mutex_lock (&sbi->s_lock); num_clusters =exfat_bytes_to_cluster (sbi,exfat_ondisk_size (inode));/* integer overflow is already checked in inode_newsize_ok(). */ new_num_clusters =exfat_bytes_to_cluster_round_up (sbi, size);
@@ -106,7 +107,7 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)exfat_chain_set (&clu, ei->start_clu, num_clusters, ei->flags); ret =exfat_find_last_cluster (sb, &clu, &last_clu);if (ret) -return ret; + goto unlock; clu.dir = last_clu +1 ; }else {
@@ -120,7 +121,7 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) ret =exfat_alloc_cluster (inode, new_num_clusters - num_clusters, &clu,inode_needs_sync (inode), false);if (ret) -return ret; + goto unlock;/* Append new clusters to chain */ if (num_clusters) {
@@ -137,15 +138,20 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) ei->flags = clu.flags; out: +/* Update i_blocks before i_size so mappings never exceed the chain. */ + inode->i_blocks =round_up (size, sbi->cluster_size) >>9 ;inode_set_mtime_to_ts (inode,inode_set_ctime_current (inode)); +mutex_unlock (&sbi->s_lock); +/* * When extending file size, call exfat_pagecache_isize_extended() * to update i_size and ensure the straddling folio is properly * marked RO so page_mkwrite() is called and post-EOF area is zeroed. +* +* It takes the folio lock; call it without s_lock (writeback uses +* folio lock -> s_lock). */ exfat_pagecache_isize_extended (inode, oldsize, size); - - inode->i_blocks =round_up (size, sbi->cluster_size) >>9 ;mark_inode_dirty (inode);if (IS_SYNC (inode))
@@ -155,7 +161,10 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) free_clu:exfat_free_cluster (inode, &clu); -return -EIO ; + ret = -EIO ; +unlock: +mutex_unlock (&sbi->s_lock); +return ret; }/*
Signature · 1 line
2.43.0
Quoted 8 lines · Youling Tang
From: Youling Tang <[email protected]>
exfat_cont_expand()allocates clusters, appends them to the FAT chain and updatesei->start_clu,ei->flagsandi_blockswithout holdingsbi->s_lock. Readers such as writeback callexfat_map_cluster()withs_lockheld but withoutinode_lock, so they are not serialized againstcont_expand()and can observe the chain metadata mid-update.
Quoted 6 lines
Hold
s_lockaround the cluster allocation, chain append andi_blocksupdate.i_sizeis published viaexfat_pagecache_isize_extended(), which takes the straddling folio lock; since writeback takes folio lock ->s_lock, this must happen afters_lockis released to avoid inverting the lock order.i_blocksis therefore updated first, unders_lock, which also ensures a reader never seesi_sizebeyond the allocated chain.
Please resubmit based on the latest exfat/dev code.
Thanks.