Linux filesystem development
 help / color / mirror / Atom feed
* [PATCH] fat: hold truncate_lock in fat_write_failed()
@ 2026-10-21 16:21 Matheus Alves de Almeida
  2026-10-23  7:34 ` OGAWA Hirofumi
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Matheus Alves de Almeida @ 2026-10-21 16:21 UTC (permalink / raw)
  To: OGAWA Hirofumi; +Cc: linux-fsdevel, linux-kernel, Matheus Alves de Almeida

The current fat_write_failed() implementation calls into
fat_truncate_blocks() without holding truncate_lock. Make it so it is
held exclusive while truncating.

Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
---
 fs/fat/inode.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/fat/inode.c b/fs/fat/inode.c
index 141276fcf8db..2a16ed415631 100644
--- a/fs/fat/inode.c
+++ b/fs/fat/inode.c
@@ -216,7 +216,9 @@ static void fat_write_failed(struct address_space *mapping, loff_t to)
 
 	if (to > inode->i_size) {
 		truncate_pagecache(inode, inode->i_size);
+		down_write(&MSDOS_I(inode)->truncate_lock);
 		fat_truncate_blocks(inode, inode->i_size);
+		up_write(&MSDOS_I(inode)->truncate_lock);
 	}
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-21 16:21 [PATCH] fat: hold truncate_lock in fat_write_failed() Matheus Alves de Almeida
@ 2026-10-23  7:34 ` OGAWA Hirofumi
  2026-10-23 21:17   ` Matheus Alves de Almeida
  2026-10-24 19:26 ` OGAWA Hirofumi
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: OGAWA Hirofumi @ 2026-10-23  7:34 UTC (permalink / raw)
  To: Matheus Alves de Almeida; +Cc: linux-fsdevel, linux-kernel

Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> writes:

> The current fat_write_failed() implementation calls into
> fat_truncate_blocks() without holding truncate_lock. Make it so it is
> held exclusive while truncating.
>
> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
> ---
>  fs/fat/inode.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/fs/fat/inode.c b/fs/fat/inode.c
> index 141276fcf8db..2a16ed415631 100644
> --- a/fs/fat/inode.c
> +++ b/fs/fat/inode.c
> @@ -216,7 +216,9 @@ static void fat_write_failed(struct address_space *mapping, loff_t to)
>  
>  	if (to > inode->i_size) {
>  		truncate_pagecache(inode, inode->i_size);
> +		down_write(&MSDOS_I(inode)->truncate_lock);
>  		fat_truncate_blocks(inode, inode->i_size);
> +		up_write(&MSDOS_I(inode)->truncate_lock);

Which race this prevent?

Thanks.

>  	}
>  }

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-23  7:34 ` OGAWA Hirofumi
@ 2026-10-23 21:17   ` Matheus Alves de Almeida
  2026-10-23 23:46     ` OGAWA Hirofumi
  0 siblings, 1 reply; 10+ messages in thread
From: Matheus Alves de Almeida @ 2026-10-23 21:17 UTC (permalink / raw)
  To: OGAWA Hirofumi; +Cc: linux-fsdevel, linux-kernel

On Fri, Oct 23, 2026 at 04:34:34PM +0900, OGAWA Hirofumi wrote:
>Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> writes:
>
>> The current fat_write_failed() implementation calls into
>> fat_truncate_blocks() without holding truncate_lock. Make it so it is
>> held exclusive while truncating.
>>
>> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
>> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
>> ---
>>  fs/fat/inode.c | 2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/fs/fat/inode.c b/fs/fat/inode.c
>> index 141276fcf8db..2a16ed415631 100644
>> --- a/fs/fat/inode.c
>> +++ b/fs/fat/inode.c
>> @@ -216,7 +216,9 @@ static void fat_write_failed(struct address_space *mapping, loff_t to)
>>  
>>  	if (to > inode->i_size) {
>>  		truncate_pagecache(inode, inode->i_size);
>> +		down_write(&MSDOS_I(inode)->truncate_lock);
>>  		fat_truncate_blocks(inode, inode->i_size);
>> +		up_write(&MSDOS_I(inode)->truncate_lock);
>
>Which race this prevent?
>
>Thanks.
>
fat_write_failed() can race with the users of fat_aops.bmap(). bmap()
eventually reaches fat_get_cluster(), which assumes the file's cluster
chain will not be truncated concurrently. fat_write_failed() currently
truncates without holding truncate_lock.

For comparison, fat_setattr() correctly holds truncate_lock during
truncation:

down_write(&MSDOS_I(inode)->truncate_lock);
truncate_setsize(inode, attr->ia_size);
fat_truncate_blocks(inode, attr->ia_size);
up_write(&MSDOS_I(inode)->truncate_lock);

Thanks,
Matheus
>>  	}
>>  }
>
>-- 
>OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-23 21:17   ` Matheus Alves de Almeida
@ 2026-10-23 23:46     ` OGAWA Hirofumi
  0 siblings, 0 replies; 10+ messages in thread
From: OGAWA Hirofumi @ 2026-10-23 23:46 UTC (permalink / raw)
  To: Matheus Alves de Almeida; +Cc: linux-fsdevel, linux-kernel

Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> writes:

> On Fri, Oct 23, 2026 at 04:34:34PM +0900, OGAWA Hirofumi wrote:
>>Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> writes:
>>
>>> The current fat_write_failed() implementation calls into
>>> fat_truncate_blocks() without holding truncate_lock. Make it so it is
>>> held exclusive while truncating.
>>>
>>> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
>>> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
>>> ---
>>>  fs/fat/inode.c | 2 ++
>>>  1 file changed, 2 insertions(+)
>>>
>>> diff --git a/fs/fat/inode.c b/fs/fat/inode.c
>>> index 141276fcf8db..2a16ed415631 100644
>>> --- a/fs/fat/inode.c
>>> +++ b/fs/fat/inode.c
>>> @@ -216,7 +216,9 @@ static void fat_write_failed(struct address_space *mapping, loff_t to)
>>>  
>>>  	if (to > inode->i_size) {
>>>  		truncate_pagecache(inode, inode->i_size);
>>> +		down_write(&MSDOS_I(inode)->truncate_lock);
>>>  		fat_truncate_blocks(inode, inode->i_size);
>>> +		up_write(&MSDOS_I(inode)->truncate_lock);
>>
>>Which race this prevent?
>>
>>Thanks.
>>
> fat_write_failed() can race with the users of fat_aops.bmap(). bmap()
> eventually reaches fat_get_cluster(), which assumes the file's cluster
> chain will not be truncated concurrently. fat_write_failed() currently
> truncates without holding truncate_lock.
>
> For comparison, fat_setattr() correctly holds truncate_lock during
> truncation:
>
> down_write(&MSDOS_I(inode)->truncate_lock);
> truncate_setsize(inode, attr->ia_size);
> fat_truncate_blocks(inode, attr->ia_size);
> up_write(&MSDOS_I(inode)->truncate_lock);

Hm, looks like true.
-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-21 16:21 [PATCH] fat: hold truncate_lock in fat_write_failed() Matheus Alves de Almeida
  2026-10-23  7:34 ` OGAWA Hirofumi
@ 2026-10-24 19:26 ` OGAWA Hirofumi
  2026-10-24 21:39 ` Pedro Falcato
  2026-10-25  0:24 ` Andrew Morton
  3 siblings, 0 replies; 10+ messages in thread
From: OGAWA Hirofumi @ 2026-10-24 19:26 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Matheus Alves de Almeida, linux-fsdevel, linux-kernel

Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> writes:

> The current fat_write_failed() implementation calls into
> fat_truncate_blocks() without holding truncate_lock. Make it so it is
> held exclusive while truncating.
>
> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>

Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

> ---
>  fs/fat/inode.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/fs/fat/inode.c b/fs/fat/inode.c
> index 141276fcf8db..2a16ed415631 100644
> --- a/fs/fat/inode.c
> +++ b/fs/fat/inode.c
> @@ -216,7 +216,9 @@ static void fat_write_failed(struct address_space *mapping, loff_t to)
>  
>  	if (to > inode->i_size) {
>  		truncate_pagecache(inode, inode->i_size);
> +		down_write(&MSDOS_I(inode)->truncate_lock);
>  		fat_truncate_blocks(inode, inode->i_size);
> +		up_write(&MSDOS_I(inode)->truncate_lock);
>  	}
>  }

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-21 16:21 [PATCH] fat: hold truncate_lock in fat_write_failed() Matheus Alves de Almeida
  2026-10-23  7:34 ` OGAWA Hirofumi
  2026-10-24 19:26 ` OGAWA Hirofumi
@ 2026-10-24 21:39 ` Pedro Falcato
  2026-10-24 22:30   ` Matheus Alves de Almeida
  2026-10-25  0:24 ` Andrew Morton
  3 siblings, 1 reply; 10+ messages in thread
From: Pedro Falcato @ 2026-10-24 21:39 UTC (permalink / raw)
  To: Matheus Alves de Almeida; +Cc: OGAWA Hirofumi, linux-fsdevel, linux-kernel

On Wed, Oct 21, 2026 at 01:21:04PM -0300, Matheus Alves de Almeida wrote:
> The current fat_write_failed() implementation calls into
> fat_truncate_blocks() without holding truncate_lock. Make it so it is
> held exclusive while truncating.
> 
> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
> ---
>  fs/fat/inode.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/fat/inode.c b/fs/fat/inode.c
> index 141276fcf8db..2a16ed415631 100644
> --- a/fs/fat/inode.c
> +++ b/fs/fat/inode.c
> @@ -216,7 +216,9 @@ static void fat_write_failed(struct address_space *mapping, loff_t to)
>  
>  	if (to > inode->i_size) {
>  		truncate_pagecache(inode, inode->i_size);
> +		down_write(&MSDOS_I(inode)->truncate_lock);
>  		fat_truncate_blocks(inode, inode->i_size);
> +		up_write(&MSDOS_I(inode)->truncate_lock);

Hmm, I think there's still a race here. Nothing's stopping a sequence of
events like:

1) truncate_pagecache
2) concurrent pagecache look creates mapping for out-of-range blocks
3) fat_truncate_blocks() truncates the blocks out from the inode
4) page cache has stale pages pointing at stale blocks

-- 
Pedro

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-24 21:39 ` Pedro Falcato
@ 2026-10-24 22:30   ` Matheus Alves de Almeida
  0 siblings, 0 replies; 10+ messages in thread
From: Matheus Alves de Almeida @ 2026-10-24 22:30 UTC (permalink / raw)
  To: Pedro Falcato; +Cc: OGAWA Hirofumi, linux-fsdevel, linux-kernel

On Sat, Oct 24, 2026 at 10:39:30PM +0100, Pedro Falcato wrote:
>On Wed, Oct 21, 2026 at 01:21:04PM -0300, Matheus Alves de Almeida wrote:
>> The current fat_write_failed() implementation calls into
>> fat_truncate_blocks() without holding truncate_lock. Make it so it is
>> held exclusive while truncating.
>> 
>> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
>> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
>> ---
>>  fs/fat/inode.c | 2 ++
>>  1 file changed, 2 insertions(+)
>> 
>> diff --git a/fs/fat/inode.c b/fs/fat/inode.c
>> index 141276fcf8db..2a16ed415631 100644
>> --- a/fs/fat/inode.c
>> +++ b/fs/fat/inode.c
>> @@ -216,7 +216,9 @@ static void fat_write_failed(struct address_space *mapping, loff_t to)
>>  
>>  	if (to > inode->i_size) {
>>  		truncate_pagecache(inode, inode->i_size);
>> +		down_write(&MSDOS_I(inode)->truncate_lock);
>>  		fat_truncate_blocks(inode, inode->i_size);
>> +		up_write(&MSDOS_I(inode)->truncate_lock);
>
>Hmm, I think there's still a race here. Nothing's stopping a sequence of
>events like:
>
>1) truncate_pagecache
>2) concurrent pagecache look creates mapping for out-of-range blocks
>3) fat_truncate_blocks() truncates the blocks out from the inode
>4) page cache has stale pages pointing at stale blocks
>
>-- 
>Pedro

The read/fault path eventually reaches fat_bmap() with from_bmap = false,
which does not return mappings for blocks at or beyond i_size.

Do you have a specific path in mind that can cause this race?

Thanks,
Matheus

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-21 16:21 [PATCH] fat: hold truncate_lock in fat_write_failed() Matheus Alves de Almeida
                   ` (2 preceding siblings ...)
  2026-10-24 21:39 ` Pedro Falcato
@ 2026-10-25  0:24 ` Andrew Morton
  2026-10-25  0:52   ` Matheus Alves de Almeida
  2026-10-25  3:57   ` OGAWA Hirofumi
  3 siblings, 2 replies; 10+ messages in thread
From: Andrew Morton @ 2026-10-25  0:24 UTC (permalink / raw)
  To: Matheus Alves de Almeida; +Cc: OGAWA Hirofumi, linux-fsdevel, linux-kernel

On Wed, 21 Oct 2026 13:21:04 -0300 Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> wrote:

> The current fat_write_failed() implementation calls into
> fat_truncate_blocks() without holding truncate_lock. Make it so it is
> held exclusive while truncating.
> 
> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>

Thanks.  My AI reviewbot said:

: Please expand the changelog to describe the failed/short extending write
: racing with FIBMAP. The mapper uses i_blocks, so it can walk beyond i_size
: while fat_write_failed() is cutting that part of the chain. That can
: produce a bogus "request beyond EOF" error and make the filesystem
: read-only with the default error policy.
: 
: Also, please change the Fixes attribution to 16fab2015099:
: "fat: permit to return phy block number by fibmap in fallocated region".
: That introduced the beyond-EOF FIBMAP lookup. Before it, the lookup was
: bounded by i_size, so the removed clusters were outside its range.
: 459f6ed3b8c7 introduced the helper, but not this failure mechanism.
: 
: I don't see a need to move the lock above truncate_pagecache(). Ordinary
: reads are bounded by i_size, and the cluster containing the partial EOF
: block is retained. FIBMAP is the case which needs this exclusion.
: 
: This looks like a stable candidate once validated and merged upstream.
: Please consider adding Cc: stable@vger.kernel.org. I checked the source
: and a reduced TLA+ model, but haven't built or runtime-tested the patch.

So I made some changes and ended up with the below.  Please check.

From: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
Subject: fat: hold truncate_lock in fat_write_failed()
Date: Wed, 21 Oct 2026 13:21:04 -0300

The current fat_write_failed() implementation calls into
fat_truncate_blocks() without holding truncate_lock.

A failed or short extending write racing with FIBMAP can spuriously make a
FAT filesystem read-only.  FIBMAP bounds its lookup by i_blocks, so it can
walk allocated clusters beyond i_size while fat_write_failed() is removing
them.  If the chain is shortened during the walk, the mapper can report a
bogus "request beyond EOF" error, triggering the default read-only error
policy.  Hold truncate_lock for write around the block cleanup to
serialize it with _fat_bmap().

For comparison, fat_setattr() correctly holds truncate_lock during
truncation:

	down_write(&MSDOS_I(inode)->truncate_lock);
	truncate_setsize(inode, attr->ia_size);
	fat_truncate_blocks(inode, attr->ia_size);
	up_write(&MSDOS_I(inode)->truncate_lock);

Make it so ->truncate_lock is held exclusive while truncating.

Link: https://lore.kernel.org/20261021162105.62525-1-matheus.aalmeida@inf.ufrgs.br
Fixes: 16fab2015099 ("fat: permit to return phy block number by fibmap in fallocated region")
Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: Pedro Falcato <pfalcato@suse.de>
Cc: <stable@vger.kernel.org>
---

 fs/fat/inode.c |    2 ++
 1 file changed, 2 insertions(+)

--- a/fs/fat/inode.c~fat-hold-truncate_lock-in-fat_write_failed
+++ a/fs/fat/inode.c
@@ -217,7 +217,9 @@ static void fat_write_failed(struct addr
 
 	if (to > inode->i_size) {
 		truncate_pagecache(inode, inode->i_size);
+		down_write(&MSDOS_I(inode)->truncate_lock);
 		fat_truncate_blocks(inode, inode->i_size);
+		up_write(&MSDOS_I(inode)->truncate_lock);
 	}
 }
 
_


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-25  0:24 ` Andrew Morton
@ 2026-10-25  0:52   ` Matheus Alves de Almeida
  2026-10-25  3:57   ` OGAWA Hirofumi
  1 sibling, 0 replies; 10+ messages in thread
From: Matheus Alves de Almeida @ 2026-10-25  0:52 UTC (permalink / raw)
  To: Andrew Morton; +Cc: OGAWA Hirofumi, linux-fsdevel, linux-kernel

On Sat, Oct 24, 2026 at 05:24:58PM -0700, Andrew Morton wrote:
>On Wed, 21 Oct 2026 13:21:04 -0300 Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> wrote:
>
>> The current fat_write_failed() implementation calls into
>> fat_truncate_blocks() without holding truncate_lock. Make it so it is
>> held exclusive while truncating.
>> 
>> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
>> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
>
>Thanks.  My AI reviewbot said:
>
>: Please expand the changelog to describe the failed/short extending write
>: racing with FIBMAP. The mapper uses i_blocks, so it can walk beyond i_size
>: while fat_write_failed() is cutting that part of the chain. That can
>: produce a bogus "request beyond EOF" error and make the filesystem
>: read-only with the default error policy.
>: 
>: Also, please change the Fixes attribution to 16fab2015099:
>: "fat: permit to return phy block number by fibmap in fallocated region".
>: That introduced the beyond-EOF FIBMAP lookup. Before it, the lookup was
>: bounded by i_size, so the removed clusters were outside its range.
>: 459f6ed3b8c7 introduced the helper, but not this failure mechanism.
>: 
>: I don't see a need to move the lock above truncate_pagecache(). Ordinary
>: reads are bounded by i_size, and the cluster containing the partial EOF
>: block is retained. FIBMAP is the case which needs this exclusion.
>: 
>: This looks like a stable candidate once validated and merged upstream.
>: Please consider adding Cc: stable@vger.kernel.org. I checked the source
>: and a reduced TLA+ model, but haven't built or runtime-tested the patch.
>
>So I made some changes and ended up with the below.  Please check.
>
>From: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
>Subject: fat: hold truncate_lock in fat_write_failed()
>Date: Wed, 21 Oct 2026 13:21:04 -0300
>
>The current fat_write_failed() implementation calls into
>fat_truncate_blocks() without holding truncate_lock.
>
>A failed or short extending write racing with FIBMAP can spuriously make a
>FAT filesystem read-only.  FIBMAP bounds its lookup by i_blocks, so it can
>walk allocated clusters beyond i_size while fat_write_failed() is removing
>them.  If the chain is shortened during the walk, the mapper can report a
>bogus "request beyond EOF" error, triggering the default read-only error
>policy.  Hold truncate_lock for write around the block cleanup to
>serialize it with _fat_bmap().
>
>For comparison, fat_setattr() correctly holds truncate_lock during
>truncation:
>
>	down_write(&MSDOS_I(inode)->truncate_lock);
>	truncate_setsize(inode, attr->ia_size);
>	fat_truncate_blocks(inode, attr->ia_size);
>	up_write(&MSDOS_I(inode)->truncate_lock);
>
>Make it so ->truncate_lock is held exclusive while truncating.
>
>Link: https://lore.kernel.org/20261021162105.62525-1-matheus.aalmeida@inf.ufrgs.br
>Fixes: 16fab2015099 ("fat: permit to return phy block number by fibmap in fallocated region")
>Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
>Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
>Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
>Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
>Cc: Pedro Falcato <pfalcato@suse.de>
>Cc: <stable@vger.kernel.org>
>---
>
> fs/fat/inode.c |    2 ++
> 1 file changed, 2 insertions(+)
>
>--- a/fs/fat/inode.c~fat-hold-truncate_lock-in-fat_write_failed
>+++ a/fs/fat/inode.c
>@@ -217,7 +217,9 @@ static void fat_write_failed(struct addr
> 
> 	if (to > inode->i_size) {
> 		truncate_pagecache(inode, inode->i_size);
>+		down_write(&MSDOS_I(inode)->truncate_lock);
> 		fat_truncate_blocks(inode, inode->i_size);
>+		up_write(&MSDOS_I(inode)->truncate_lock);
> 	}
> }
> 
>_
>

Looks good to me.

Thanks,
Matheus.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] fat: hold truncate_lock in fat_write_failed()
  2026-10-25  0:24 ` Andrew Morton
  2026-10-25  0:52   ` Matheus Alves de Almeida
@ 2026-10-25  3:57   ` OGAWA Hirofumi
  1 sibling, 0 replies; 10+ messages in thread
From: OGAWA Hirofumi @ 2026-10-25  3:57 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Matheus Alves de Almeida, linux-fsdevel, linux-kernel

Andrew Morton <akpm@linux-foundation.org> writes:

> On Wed, 21 Oct 2026 13:21:04 -0300 Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> wrote:
>
>> The current fat_write_failed() implementation calls into
>> fat_truncate_blocks() without holding truncate_lock. Make it so it is
>> held exclusive while truncating.
>> 
>> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
>> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
>
> Thanks.  My AI reviewbot said:
>
> : Please expand the changelog to describe the failed/short extending write
> : racing with FIBMAP. The mapper uses i_blocks, so it can walk beyond i_size
> : while fat_write_failed() is cutting that part of the chain. That can
> : produce a bogus "request beyond EOF" error and make the filesystem
> : read-only with the default error policy.
> : 
> : Also, please change the Fixes attribution to 16fab2015099:
> : "fat: permit to return phy block number by fibmap in fallocated region".
> : That introduced the beyond-EOF FIBMAP lookup. Before it, the lookup was
> : bounded by i_size, so the removed clusters were outside its range.
> : 459f6ed3b8c7 introduced the helper, but not this failure mechanism.
> : 
> : I don't see a need to move the lock above truncate_pagecache(). Ordinary
> : reads are bounded by i_size, and the cluster containing the partial EOF
> : block is retained. FIBMAP is the case which needs this exclusion.
> : 
> : This looks like a stable candidate once validated and merged upstream.
> : Please consider adding Cc: stable@vger.kernel.org. I checked the source
> : and a reduced TLA+ model, but haven't built or runtime-tested the patch.
>
> So I made some changes and ended up with the below.  Please check.
>
> From: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
> Subject: fat: hold truncate_lock in fat_write_failed()
> Date: Wed, 21 Oct 2026 13:21:04 -0300
>
> The current fat_write_failed() implementation calls into
> fat_truncate_blocks() without holding truncate_lock.
>
> A failed or short extending write racing with FIBMAP can spuriously make a
> FAT filesystem read-only.  FIBMAP bounds its lookup by i_blocks, so it can
> walk allocated clusters beyond i_size while fat_write_failed() is removing
> them.  If the chain is shortened during the walk, the mapper can report a
> bogus "request beyond EOF" error, triggering the default read-only error
> policy.  Hold truncate_lock for write around the block cleanup to
> serialize it with _fat_bmap().
>
> For comparison, fat_setattr() correctly holds truncate_lock during
> truncation:
>
> 	down_write(&MSDOS_I(inode)->truncate_lock);
> 	truncate_setsize(inode, attr->ia_size);
> 	fat_truncate_blocks(inode, attr->ia_size);
> 	up_write(&MSDOS_I(inode)->truncate_lock);
>
> Make it so ->truncate_lock is held exclusive while truncating.
>
> Link: https://lore.kernel.org/20261021162105.62525-1-matheus.aalmeida@inf.ufrgs.br
> Fixes: 16fab2015099 ("fat: permit to return phy block number by fibmap in fallocated region")
> Fixes: 459f6ed3b8c7 ("fat: convert to use the new truncate convention.")
> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
> Cc: Pedro Falcato <pfalcato@suse.de>
> Cc: <stable@vger.kernel.org>

Looks good. Thanks.

> ---
>
>  fs/fat/inode.c |    2 ++
>  1 file changed, 2 insertions(+)
>
> --- a/fs/fat/inode.c~fat-hold-truncate_lock-in-fat_write_failed
> +++ a/fs/fat/inode.c
> @@ -217,7 +217,9 @@ static void fat_write_failed(struct addr
>  
>  	if (to > inode->i_size) {
>  		truncate_pagecache(inode, inode->i_size);
> +		down_write(&MSDOS_I(inode)->truncate_lock);
>  		fat_truncate_blocks(inode, inode->i_size);
> +		up_write(&MSDOS_I(inode)->truncate_lock);
>  	}
>  }
>  
> _
>

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-10-25  3:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-21 16:21 [PATCH] fat: hold truncate_lock in fat_write_failed() Matheus Alves de Almeida
2026-10-23  7:34 ` OGAWA Hirofumi
2026-10-23 21:17   ` Matheus Alves de Almeida
2026-10-23 23:46     ` OGAWA Hirofumi
2026-10-24 19:26 ` OGAWA Hirofumi
2026-10-24 21:39 ` Pedro Falcato
2026-10-24 22:30   ` Matheus Alves de Almeida
2026-10-25  0:24 ` Andrew Morton
2026-10-25  0:52   ` Matheus Alves de Almeida
2026-10-25  3:57   ` OGAWA Hirofumi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox