* [PATCH v2] exfat: hold s_lock for cluster-chain updates in __exfat_cont_expand()
@ 2026-11-18 1:58 Youling Tang
2026-11-18 3:22 ` Chi Zhiling
0 siblings, 1 reply; 2+ messages in thread
From: Youling Tang @ 2026-11-18 1:58 UTC (permalink / raw)
To: Namjae Jeon, Sungjong Seo
Cc: Yuezhang Mo, Chi Zhiling, exfat, linux-kernel, youling.tang,
Youling Tang
From: Youling Tang <tangyouling@kylinos.cn>
__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
the expansion and can observe the chain metadata mid-update.
Hold s_lock around the cluster allocation, chain append and i_blocks
update. This nests inside the invalidate_lock held by
exfat_cont_expand(), matching the order used by exfat_truncate(). 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.
Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
v2: rebase onto exfat/dev and adapt to __exfat_cont_expand().
fs/exfat/file.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/fs/exfat/file.c b/fs/exfat/file.c
index 9dfd99ad9265..c7991e98a07a 100644
--- a/fs/exfat/file.c
+++ b/fs/exfat/file.c
@@ -105,6 +105,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);
@@ -116,7 +117,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 {
@@ -130,7 +131,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) {
@@ -147,15 +148,19 @@ static int __exfat_cont_expand(struct inode *inode, loff_t size)
ei->flags = clu.flags;
out:
+ 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))
@@ -165,7 +170,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;
}
static int exfat_cont_expand(struct inode *inode, loff_t size)
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v2] exfat: hold s_lock for cluster-chain updates in __exfat_cont_expand()
2026-11-18 1:58 [PATCH v2] exfat: hold s_lock for cluster-chain updates in __exfat_cont_expand() Youling Tang
@ 2026-11-18 3:22 ` Chi Zhiling
0 siblings, 0 replies; 2+ messages in thread
From: Chi Zhiling @ 2026-11-18 3:22 UTC (permalink / raw)
To: Youling Tang, Namjae Jeon, Sungjong Seo
Cc: Yuezhang Mo, exfat, linux-kernel, Youling Tang
On 11/18/26 09:58, Youling Tang wrote:
> From: Youling Tang <tangyouling@kylinos.cn>
>
> __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
> the expansion and can observe the chain metadata mid-update.
>
> Hold s_lock around the cluster allocation, chain append and i_blocks
> update. This nests inside the invalidate_lock held by
> exfat_cont_expand(), matching the order used by exfat_truncate(). 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.
>
> Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
Looks good to me:
Reviewed-by: Chi Zhiling <chizhiling@kylinos.cn>
> ---
> v2: rebase onto exfat/dev and adapt to __exfat_cont_expand().
>
> fs/exfat/file.c | 18 +++++++++++++-----
> 1 file changed, 13 insertions(+), 5 deletions(-)
>
> diff --git a/fs/exfat/file.c b/fs/exfat/file.c
> index 9dfd99ad9265..c7991e98a07a 100644
> --- a/fs/exfat/file.c
> +++ b/fs/exfat/file.c
> @@ -105,6 +105,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);
> @@ -116,7 +117,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 {
> @@ -130,7 +131,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) {
> @@ -147,15 +148,19 @@ static int __exfat_cont_expand(struct inode *inode, loff_t size)
> ei->flags = clu.flags;
>
> out:
> + 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))
> @@ -165,7 +170,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;
> }
>
> static int exfat_cont_expand(struct inode *inode, loff_t size)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-18 3:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 1:58 [PATCH v2] exfat: hold s_lock for cluster-chain updates in __exfat_cont_expand() Youling Tang
2026-11-18 3:22 ` Chi Zhiling
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.