linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next] ext4: fix super block checksum incorrect after mount
@ 2022-05-25  1:29 Ye Bin
  2022-05-25  7:51 ` Ritesh Harjani
  2022-06-18  2:12 ` Theodore Ts'o
  0 siblings, 2 replies; 9+ messages in thread
From: Ye Bin @ 2022-05-25  1:29 UTC (permalink / raw)
  To: tytso, adilger.kernel, linux-ext4; +Cc: linux-kernel, jack, Ye Bin

We got issue as follows:
[home]# mount  /dev/sda  test
EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
[home]# dmesg
EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
EXT4-fs (sda): Errors on filesystem, clearing orphan list.
EXT4-fs (sda): recovery complete
EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
[home]# debugfs /dev/sda
debugfs 1.46.5 (30-Dec-2021)
Checksum errors in superblock!  Retrying...

Reason is ext4_orphan_cleanup will reset ‘s_last_orphan’ but not update
super block checksum.
To solve above issue, defer update super block checksum after ext4_orphan_cleanup.

Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 fs/ext4/super.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index f9a3ad683b4a..c47204029429 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -5300,14 +5300,6 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
 					  GFP_KERNEL);
 	}
-	/*
-	 * Update the checksum after updating free space/inode
-	 * counters.  Otherwise the superblock can have an incorrect
-	 * checksum in the buffer cache until it is written out and
-	 * e2fsprogs programs trying to open a file system immediately
-	 * after it is mounted can fail.
-	 */
-	ext4_superblock_csum_set(sb);
 	if (!err)
 		err = percpu_counter_init(&sbi->s_dirs_counter,
 					  ext4_count_dirs(sb), GFP_KERNEL);
@@ -5365,6 +5357,14 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
 	ext4_orphan_cleanup(sb, es);
 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
+	/*
+	 * Update the checksum after updating free space/inode counters and
+	 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
+	 * checksum in the buffer cache until it is written out and
+	 * e2fsprogs programs trying to open a file system immediately
+	 * after it is mounted can fail.
+	 */
+	ext4_superblock_csum_set(sb);
 	if (needs_recovery) {
 		ext4_msg(sb, KERN_INFO, "recovery complete");
 		err = ext4_mark_recovery_complete(sb, es);
-- 
2.31.1


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

* Re: [PATCH -next] ext4: fix super block checksum incorrect after mount
  2022-05-25  1:29 [PATCH -next] ext4: fix super block checksum incorrect after mount Ye Bin
@ 2022-05-25  7:51 ` Ritesh Harjani
  2022-05-25 11:33   ` yebin
  2022-05-25 11:54   ` Jan Kara
  2022-06-18  2:12 ` Theodore Ts'o
  1 sibling, 2 replies; 9+ messages in thread
From: Ritesh Harjani @ 2022-05-25  7:51 UTC (permalink / raw)
  To: Ye Bin; +Cc: tytso, adilger.kernel, linux-ext4, linux-kernel, jack

On 22/05/25 09:29AM, Ye Bin wrote:
> We got issue as follows:
> [home]# mount  /dev/sda  test
> EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> [home]# dmesg
> EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> EXT4-fs (sda): Errors on filesystem, clearing orphan list.
> EXT4-fs (sda): recovery complete
> EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
> [home]# debugfs /dev/sda
> debugfs 1.46.5 (30-Dec-2021)
> Checksum errors in superblock!  Retrying...
>
> Reason is ext4_orphan_cleanup will reset ‘s_last_orphan’ but not update
> super block checksum.
> To solve above issue, defer update super block checksum after ext4_orphan_cleanup.

I agree with the analysis. However after [1], I think all updates to superblock
(including checksum computation) should be done within buffer lock.
(lock_buffer(), unlock_buffer()).

[1]: https://lore.kernel.org/all/20201216101844.22917-4-jack@suse.cz/

With lock changes added, feel free to add -

Reviewed-by: Ritesh Harjani <ritesh.list@gmail.com>


>
>
> Signed-off-by: Ye Bin <yebin10@huawei.com>
> ---
>  fs/ext4/super.c | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index f9a3ad683b4a..c47204029429 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -5300,14 +5300,6 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
>  		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
>  					  GFP_KERNEL);
>  	}
> -	/*
> -	 * Update the checksum after updating free space/inode
> -	 * counters.  Otherwise the superblock can have an incorrect
> -	 * checksum in the buffer cache until it is written out and
> -	 * e2fsprogs programs trying to open a file system immediately
> -	 * after it is mounted can fail.
> -	 */
> -	ext4_superblock_csum_set(sb);
>  	if (!err)
>  		err = percpu_counter_init(&sbi->s_dirs_counter,
>  					  ext4_count_dirs(sb), GFP_KERNEL);
> @@ -5365,6 +5357,14 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
>  	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
>  	ext4_orphan_cleanup(sb, es);
>  	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
> +	/*
> +	 * Update the checksum after updating free space/inode counters and
> +	 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
> +	 * checksum in the buffer cache until it is written out and
> +	 * e2fsprogs programs trying to open a file system immediately
> +	 * after it is mounted can fail.
> +	 */
> +	ext4_superblock_csum_set(sb);
>  	if (needs_recovery) {
>  		ext4_msg(sb, KERN_INFO, "recovery complete");
>  		err = ext4_mark_recovery_complete(sb, es);
> --
> 2.31.1
>

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

* Re: [PATCH -next] ext4: fix super block checksum incorrect after mount
  2022-05-25  7:51 ` Ritesh Harjani
@ 2022-05-25 11:33   ` yebin
  2022-05-25 11:54   ` Jan Kara
  1 sibling, 0 replies; 9+ messages in thread
From: yebin @ 2022-05-25 11:33 UTC (permalink / raw)
  To: Ritesh Harjani; +Cc: tytso, adilger.kernel, linux-ext4, linux-kernel, jack



On 2022/5/25 15:51, Ritesh Harjani wrote:
> On 22/05/25 09:29AM, Ye Bin wrote:
>> We got issue as follows:
>> [home]# mount  /dev/sda  test
>> EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
>> [home]# dmesg
>> EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
>> EXT4-fs (sda): Errors on filesystem, clearing orphan list.
>> EXT4-fs (sda): recovery complete
>> EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
>> [home]# debugfs /dev/sda
>> debugfs 1.46.5 (30-Dec-2021)
>> Checksum errors in superblock!  Retrying...
>>
>> Reason is ext4_orphan_cleanup will reset ‘s_last_orphan’ but not update
>> super block checksum.
>> To solve above issue, defer update super block checksum after ext4_orphan_cleanup.
> I agree with the analysis. However after [1], I think all updates to superblock
> (including checksum computation) should be done within buffer lock.
> (lock_buffer(), unlock_buffer()).
>
> [1]: https://lore.kernel.org/all/20201216101844.22917-4-jack@suse.cz/
>
> With lock changes added, feel free to add -
>
> Reviewed-by: Ritesh Harjani <ritesh.list@gmail.com>
Thanks for your reply.
I think there should be no concurrent  modification at this time.
So there's no need to hold buffer lock.
Am I missing something?
>
>>
>> Signed-off-by: Ye Bin <yebin10@huawei.com>
>> ---
>>   fs/ext4/super.c | 16 ++++++++--------
>>   1 file changed, 8 insertions(+), 8 deletions(-)
>>
>> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
>> index f9a3ad683b4a..c47204029429 100644
>> --- a/fs/ext4/super.c
>> +++ b/fs/ext4/super.c
>> @@ -5300,14 +5300,6 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
>>   		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
>>   					  GFP_KERNEL);
>>   	}
>> -	/*
>> -	 * Update the checksum after updating free space/inode
>> -	 * counters.  Otherwise the superblock can have an incorrect
>> -	 * checksum in the buffer cache until it is written out and
>> -	 * e2fsprogs programs trying to open a file system immediately
>> -	 * after it is mounted can fail.
>> -	 */
>> -	ext4_superblock_csum_set(sb);
>>   	if (!err)
>>   		err = percpu_counter_init(&sbi->s_dirs_counter,
>>   					  ext4_count_dirs(sb), GFP_KERNEL);
>> @@ -5365,6 +5357,14 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
>>   	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
>>   	ext4_orphan_cleanup(sb, es);
>>   	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
>> +	/*
>> +	 * Update the checksum after updating free space/inode counters and
>> +	 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
>> +	 * checksum in the buffer cache until it is written out and
>> +	 * e2fsprogs programs trying to open a file system immediately
>> +	 * after it is mounted can fail.
>> +	 */
>> +	ext4_superblock_csum_set(sb);
>>   	if (needs_recovery) {
>>   		ext4_msg(sb, KERN_INFO, "recovery complete");
>>   		err = ext4_mark_recovery_complete(sb, es);
>> --
>> 2.31.1
>>
> .
>


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

* Re: [PATCH -next] ext4: fix super block checksum incorrect after mount
  2022-05-25  7:51 ` Ritesh Harjani
  2022-05-25 11:33   ` yebin
@ 2022-05-25 11:54   ` Jan Kara
  2022-05-25 15:16     ` Ritesh Harjani
  2022-05-27  9:16     ` yebin
  1 sibling, 2 replies; 9+ messages in thread
From: Jan Kara @ 2022-05-25 11:54 UTC (permalink / raw)
  To: Ritesh Harjani
  Cc: Ye Bin, tytso, adilger.kernel, linux-ext4, linux-kernel, jack

On Wed 25-05-22 13:21:23, Ritesh Harjani wrote:
> On 22/05/25 09:29AM, Ye Bin wrote:
> > We got issue as follows:
> > [home]# mount  /dev/sda  test
> > EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> > [home]# dmesg
> > EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> > EXT4-fs (sda): Errors on filesystem, clearing orphan list.
> > EXT4-fs (sda): recovery complete
> > EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
> > [home]# debugfs /dev/sda
> > debugfs 1.46.5 (30-Dec-2021)
> > Checksum errors in superblock!  Retrying...
> >
> > Reason is ext4_orphan_cleanup will reset ‘s_last_orphan’ but not update
> > super block checksum.
> > To solve above issue, defer update super block checksum after ext4_orphan_cleanup.
> 
> I agree with the analysis. However after [1], I think all updates to superblock
> (including checksum computation) should be done within buffer lock.
> (lock_buffer(), unlock_buffer()).
> 
> [1]: https://lore.kernel.org/all/20201216101844.22917-4-jack@suse.cz/

So technically you're right that we should hold buffer lock all the time
from before we modify superblock buffer until we recompute the checksum (so
that we avoid writing superblock with mismatched checksum). To do this we'd
have to put checksum recomputations and superblock buffer locking into
ext4_orphan_cleanup() around setting of es->s_last_orphan (in three places
there AFAICS). A bit tedious but it would actually also fix a (theoretical)
race that someone decides to write out superblock after we set
s_last_orphan but before we set the checksum.

Overall I'm not convinced this is really necessary so I'd be OK even with
what Ye suggested. That is IMHO better than mostly pointless locking just
around checksum computation because that just makes reader wonder why is it
needed...

								Honza

> 
> With lock changes added, feel free to add -
> 
> Reviewed-by: Ritesh Harjani <ritesh.list@gmail.com>
> 
> 
> >
> >
> > Signed-off-by: Ye Bin <yebin10@huawei.com>
> > ---
> >  fs/ext4/super.c | 16 ++++++++--------
> >  1 file changed, 8 insertions(+), 8 deletions(-)
> >
> > diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> > index f9a3ad683b4a..c47204029429 100644
> > --- a/fs/ext4/super.c
> > +++ b/fs/ext4/super.c
> > @@ -5300,14 +5300,6 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
> >  		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
> >  					  GFP_KERNEL);
> >  	}
> > -	/*
> > -	 * Update the checksum after updating free space/inode
> > -	 * counters.  Otherwise the superblock can have an incorrect
> > -	 * checksum in the buffer cache until it is written out and
> > -	 * e2fsprogs programs trying to open a file system immediately
> > -	 * after it is mounted can fail.
> > -	 */
> > -	ext4_superblock_csum_set(sb);
> >  	if (!err)
> >  		err = percpu_counter_init(&sbi->s_dirs_counter,
> >  					  ext4_count_dirs(sb), GFP_KERNEL);
> > @@ -5365,6 +5357,14 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
> >  	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
> >  	ext4_orphan_cleanup(sb, es);
> >  	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
> > +	/*
> > +	 * Update the checksum after updating free space/inode counters and
> > +	 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
> > +	 * checksum in the buffer cache until it is written out and
> > +	 * e2fsprogs programs trying to open a file system immediately
> > +	 * after it is mounted can fail.
> > +	 */
> > +	ext4_superblock_csum_set(sb);
> >  	if (needs_recovery) {
> >  		ext4_msg(sb, KERN_INFO, "recovery complete");
> >  		err = ext4_mark_recovery_complete(sb, es);
> > --
> > 2.31.1
> >
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [PATCH -next] ext4: fix super block checksum incorrect after mount
  2022-05-25 11:54   ` Jan Kara
@ 2022-05-25 15:16     ` Ritesh Harjani
  2022-05-25 15:57       ` Jan Kara
  2022-05-27  9:16     ` yebin
  1 sibling, 1 reply; 9+ messages in thread
From: Ritesh Harjani @ 2022-05-25 15:16 UTC (permalink / raw)
  To: Jan Kara; +Cc: Ye Bin, tytso, adilger.kernel, linux-ext4, linux-kernel

On 22/05/25 01:54PM, Jan Kara wrote:
> On Wed 25-05-22 13:21:23, Ritesh Harjani wrote:
> > On 22/05/25 09:29AM, Ye Bin wrote:
> > > We got issue as follows:
> > > [home]# mount  /dev/sda  test
> > > EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> > > [home]# dmesg
> > > EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> > > EXT4-fs (sda): Errors on filesystem, clearing orphan list.
> > > EXT4-fs (sda): recovery complete
> > > EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
> > > [home]# debugfs /dev/sda
> > > debugfs 1.46.5 (30-Dec-2021)
> > > Checksum errors in superblock!  Retrying...
> > >
> > > Reason is ext4_orphan_cleanup will reset ‘s_last_orphan’ but not update
> > > super block checksum.
> > > To solve above issue, defer update super block checksum after ext4_orphan_cleanup.
> >
> > I agree with the analysis. However after [1], I think all updates to superblock
> > (including checksum computation) should be done within buffer lock.
> > (lock_buffer(), unlock_buffer()).
> >
> > [1]: https://lore.kernel.org/all/20201216101844.22917-4-jack@suse.cz/
>
> So technically you're right that we should hold buffer lock all the time
> from before we modify superblock buffer until we recompute the checksum (so
> that we avoid writing superblock with mismatched checksum). To do this we'd
> have to put checksum recomputations and superblock buffer locking into
> ext4_orphan_cleanup() around setting of es->s_last_orphan (in three places
> there AFAICS). A bit tedious but it would actually also fix a (theoretical)
> race that someone decides to write out superblock after we set
> s_last_orphan but before we set the checksum.

Ok. Although (I think) it can still be done at just one place before returning
from ext4_orphan_cleanup().
But I agree it is mostly a theoretical race (in fact since this is happening
during mount, I am not sure if it is even possible?) and there might not
be any value addition in doing so by complicating it too much.

>
> Overall I'm not convinced this is really necessary so I'd be OK even with
> what Ye suggested. That is IMHO better than mostly pointless locking just
> around checksum computation because that just makes reader wonder why is it
> needed...

Sure, yes. Thanks for explaining it.

-ritesh

>
> 								Honza
>
> >
> > With lock changes added, feel free to add -
> >
> > Reviewed-by: Ritesh Harjani <ritesh.list@gmail.com>
> >
> >
> > >
> > >
> > > Signed-off-by: Ye Bin <yebin10@huawei.com>
> > > ---
> > >  fs/ext4/super.c | 16 ++++++++--------
> > >  1 file changed, 8 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> > > index f9a3ad683b4a..c47204029429 100644
> > > --- a/fs/ext4/super.c
> > > +++ b/fs/ext4/super.c
> > > @@ -5300,14 +5300,6 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
> > >  		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
> > >  					  GFP_KERNEL);
> > >  	}
> > > -	/*
> > > -	 * Update the checksum after updating free space/inode
> > > -	 * counters.  Otherwise the superblock can have an incorrect
> > > -	 * checksum in the buffer cache until it is written out and
> > > -	 * e2fsprogs programs trying to open a file system immediately
> > > -	 * after it is mounted can fail.
> > > -	 */
> > > -	ext4_superblock_csum_set(sb);
> > >  	if (!err)
> > >  		err = percpu_counter_init(&sbi->s_dirs_counter,
> > >  					  ext4_count_dirs(sb), GFP_KERNEL);
> > > @@ -5365,6 +5357,14 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
> > >  	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
> > >  	ext4_orphan_cleanup(sb, es);
> > >  	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
> > > +	/*
> > > +	 * Update the checksum after updating free space/inode counters and
> > > +	 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
> > > +	 * checksum in the buffer cache until it is written out and
> > > +	 * e2fsprogs programs trying to open a file system immediately
> > > +	 * after it is mounted can fail.
> > > +	 */
> > > +	ext4_superblock_csum_set(sb);
> > >  	if (needs_recovery) {
> > >  		ext4_msg(sb, KERN_INFO, "recovery complete");
> > >  		err = ext4_mark_recovery_complete(sb, es);
> > > --
> > > 2.31.1
> > >
> --
> Jan Kara <jack@suse.com>
> SUSE Labs, CR

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

* Re: [PATCH -next] ext4: fix super block checksum incorrect after mount
  2022-05-25 15:16     ` Ritesh Harjani
@ 2022-05-25 15:57       ` Jan Kara
  0 siblings, 0 replies; 9+ messages in thread
From: Jan Kara @ 2022-05-25 15:57 UTC (permalink / raw)
  To: Ritesh Harjani
  Cc: Jan Kara, Ye Bin, tytso, adilger.kernel, linux-ext4, linux-kernel

On Wed 25-05-22 20:46:12, Ritesh Harjani wrote:
> On 22/05/25 01:54PM, Jan Kara wrote:
> > On Wed 25-05-22 13:21:23, Ritesh Harjani wrote:
> > > On 22/05/25 09:29AM, Ye Bin wrote:
> > > > We got issue as follows:
> > > > [home]# mount  /dev/sda  test
> > > > EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> > > > [home]# dmesg
> > > > EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> > > > EXT4-fs (sda): Errors on filesystem, clearing orphan list.
> > > > EXT4-fs (sda): recovery complete
> > > > EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
> > > > [home]# debugfs /dev/sda
> > > > debugfs 1.46.5 (30-Dec-2021)
> > > > Checksum errors in superblock!  Retrying...
> > > >
> > > > Reason is ext4_orphan_cleanup will reset ‘s_last_orphan’ but not update
> > > > super block checksum.
> > > > To solve above issue, defer update super block checksum after ext4_orphan_cleanup.
> > >
> > > I agree with the analysis. However after [1], I think all updates to superblock
> > > (including checksum computation) should be done within buffer lock.
> > > (lock_buffer(), unlock_buffer()).
> > >
> > > [1]: https://lore.kernel.org/all/20201216101844.22917-4-jack@suse.cz/
> >
> > So technically you're right that we should hold buffer lock all the time
> > from before we modify superblock buffer until we recompute the checksum (so
> > that we avoid writing superblock with mismatched checksum). To do this we'd
> > have to put checksum recomputations and superblock buffer locking into
> > ext4_orphan_cleanup() around setting of es->s_last_orphan (in three places
> > there AFAICS). A bit tedious but it would actually also fix a (theoretical)
> > race that someone decides to write out superblock after we set
> > s_last_orphan but before we set the checksum.
> 
> Ok. Although (I think) it can still be done at just one place before returning
> from ext4_orphan_cleanup().
> But I agree it is mostly a theoretical race (in fact since this is happening
> during mount, I am not sure if it is even possible?) and there might not
> be any value addition in doing so by complicating it too much.

Well, what could presumably happen is that if someone dirtied superblock
(say while processing orphan list), then flush worker could come just after
we set s_last_orphan and before we update the checksum. Now I don't think
we currently dirty superblock during orphan cleanup but it is certainly
slightly fragile to rely on this.

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [PATCH -next] ext4: fix super block checksum incorrect after mount
  2022-05-25 11:54   ` Jan Kara
  2022-05-25 15:16     ` Ritesh Harjani
@ 2022-05-27  9:16     ` yebin
  2022-05-27 10:18       ` Jan Kara
  1 sibling, 1 reply; 9+ messages in thread
From: yebin @ 2022-05-27  9:16 UTC (permalink / raw)
  To: Jan Kara, Ritesh Harjani; +Cc: tytso, adilger.kernel, linux-ext4, linux-kernel



On 2022/5/25 19:54, Jan Kara wrote:
> On Wed 25-05-22 13:21:23, Ritesh Harjani wrote:
>> On 22/05/25 09:29AM, Ye Bin wrote:
>>> We got issue as follows:
>>> [home]# mount  /dev/sda  test
>>> EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
>>> [home]# dmesg
>>> EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
>>> EXT4-fs (sda): Errors on filesystem, clearing orphan list.
>>> EXT4-fs (sda): recovery complete
>>> EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
>>> [home]# debugfs /dev/sda
>>> debugfs 1.46.5 (30-Dec-2021)
>>> Checksum errors in superblock!  Retrying...
>>>
>>> Reason is ext4_orphan_cleanup will reset ‘s_last_orphan’ but not update
>>> super block checksum.
>>> To solve above issue, defer update super block checksum after ext4_orphan_cleanup.
>> I agree with the analysis. However after [1], I think all updates to superblock
>> (including checksum computation) should be done within buffer lock.
>> (lock_buffer(), unlock_buffer()).
>>
>> [1]: https://lore.kernel.org/all/20201216101844.22917-4-jack@suse.cz/
> So technically you're right that we should hold buffer lock all the time
> from before we modify superblock buffer until we recompute the checksum (so
> that we avoid writing superblock with mismatched checksum). To do this we'd
> have to put checksum recomputations and superblock buffer locking into
> ext4_orphan_cleanup() around setting of es->s_last_orphan (in three places
> there AFAICS). A bit tedious but it would actually also fix a (theoretical)
> race that someone decides to write out superblock after we set
> s_last_orphan but before we set the checksum.
>
> Overall I'm not convinced this is really necessary so I'd be OK even with
> what Ye suggested. That is IMHO better than mostly pointless locking just
> around checksum computation because that just makes reader wonder why is it
> needed...
>
> 								Honza
Thanks for your reply.
Does my patch need to be adjusted?
>> With lock changes added, feel free to add -
>>
>> Reviewed-by: Ritesh Harjani <ritesh.list@gmail.com>
>>
>>
>>>
>>> Signed-off-by: Ye Bin <yebin10@huawei.com>
>>> ---
>>>   fs/ext4/super.c | 16 ++++++++--------
>>>   1 file changed, 8 insertions(+), 8 deletions(-)
>>>
>>> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
>>> index f9a3ad683b4a..c47204029429 100644
>>> --- a/fs/ext4/super.c
>>> +++ b/fs/ext4/super.c
>>> @@ -5300,14 +5300,6 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
>>>   		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
>>>   					  GFP_KERNEL);
>>>   	}
>>> -	/*
>>> -	 * Update the checksum after updating free space/inode
>>> -	 * counters.  Otherwise the superblock can have an incorrect
>>> -	 * checksum in the buffer cache until it is written out and
>>> -	 * e2fsprogs programs trying to open a file system immediately
>>> -	 * after it is mounted can fail.
>>> -	 */
>>> -	ext4_superblock_csum_set(sb);
>>>   	if (!err)
>>>   		err = percpu_counter_init(&sbi->s_dirs_counter,
>>>   					  ext4_count_dirs(sb), GFP_KERNEL);
>>> @@ -5365,6 +5357,14 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
>>>   	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
>>>   	ext4_orphan_cleanup(sb, es);
>>>   	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
>>> +	/*
>>> +	 * Update the checksum after updating free space/inode counters and
>>> +	 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
>>> +	 * checksum in the buffer cache until it is written out and
>>> +	 * e2fsprogs programs trying to open a file system immediately
>>> +	 * after it is mounted can fail.
>>> +	 */
>>> +	ext4_superblock_csum_set(sb);
>>>   	if (needs_recovery) {
>>>   		ext4_msg(sb, KERN_INFO, "recovery complete");
>>>   		err = ext4_mark_recovery_complete(sb, es);
>>> --
>>> 2.31.1
>>>


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

* Re: [PATCH -next] ext4: fix super block checksum incorrect after mount
  2022-05-27  9:16     ` yebin
@ 2022-05-27 10:18       ` Jan Kara
  0 siblings, 0 replies; 9+ messages in thread
From: Jan Kara @ 2022-05-27 10:18 UTC (permalink / raw)
  To: yebin
  Cc: Jan Kara, Ritesh Harjani, tytso, adilger.kernel, linux-ext4,
	linux-kernel

On Fri 27-05-22 17:16:42, yebin wrote:
> On 2022/5/25 19:54, Jan Kara wrote:
> > On Wed 25-05-22 13:21:23, Ritesh Harjani wrote:
> > > On 22/05/25 09:29AM, Ye Bin wrote:
> > > > We got issue as follows:
> > > > [home]# mount  /dev/sda  test
> > > > EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> > > > [home]# dmesg
> > > > EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> > > > EXT4-fs (sda): Errors on filesystem, clearing orphan list.
> > > > EXT4-fs (sda): recovery complete
> > > > EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
> > > > [home]# debugfs /dev/sda
> > > > debugfs 1.46.5 (30-Dec-2021)
> > > > Checksum errors in superblock!  Retrying...
> > > > 
> > > > Reason is ext4_orphan_cleanup will reset ‘s_last_orphan’ but not update
> > > > super block checksum.
> > > > To solve above issue, defer update super block checksum after ext4_orphan_cleanup.
> > > I agree with the analysis. However after [1], I think all updates to superblock
> > > (including checksum computation) should be done within buffer lock.
> > > (lock_buffer(), unlock_buffer()).
> > > 
> > > [1]: https://lore.kernel.org/all/20201216101844.22917-4-jack@suse.cz/
> > So technically you're right that we should hold buffer lock all the time
> > from before we modify superblock buffer until we recompute the checksum (so
> > that we avoid writing superblock with mismatched checksum). To do this we'd
> > have to put checksum recomputations and superblock buffer locking into
> > ext4_orphan_cleanup() around setting of es->s_last_orphan (in three places
> > there AFAICS). A bit tedious but it would actually also fix a (theoretical)
> > race that someone decides to write out superblock after we set
> > s_last_orphan but before we set the checksum.
> > 
> > Overall I'm not convinced this is really necessary so I'd be OK even with
> > what Ye suggested. That is IMHO better than mostly pointless locking just
> > around checksum computation because that just makes reader wonder why is it
> > needed...
> > 
> > 								Honza
> Thanks for your reply.
> Does my patch need to be adjusted?

No, I don't think so. What you did is an improvement over current state and
if in the future we find more rigorous approach for orphan cleanup is
needed we can do that. So feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> > > With lock changes added, feel free to add -
> > > 
> > > Reviewed-by: Ritesh Harjani <ritesh.list@gmail.com>
> > > 
> > > 
> > > > 
> > > > Signed-off-by: Ye Bin <yebin10@huawei.com>
> > > > ---
> > > >   fs/ext4/super.c | 16 ++++++++--------
> > > >   1 file changed, 8 insertions(+), 8 deletions(-)
> > > > 
> > > > diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> > > > index f9a3ad683b4a..c47204029429 100644
> > > > --- a/fs/ext4/super.c
> > > > +++ b/fs/ext4/super.c
> > > > @@ -5300,14 +5300,6 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
> > > >   		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
> > > >   					  GFP_KERNEL);
> > > >   	}
> > > > -	/*
> > > > -	 * Update the checksum after updating free space/inode
> > > > -	 * counters.  Otherwise the superblock can have an incorrect
> > > > -	 * checksum in the buffer cache until it is written out and
> > > > -	 * e2fsprogs programs trying to open a file system immediately
> > > > -	 * after it is mounted can fail.
> > > > -	 */
> > > > -	ext4_superblock_csum_set(sb);
> > > >   	if (!err)
> > > >   		err = percpu_counter_init(&sbi->s_dirs_counter,
> > > >   					  ext4_count_dirs(sb), GFP_KERNEL);
> > > > @@ -5365,6 +5357,14 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
> > > >   	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
> > > >   	ext4_orphan_cleanup(sb, es);
> > > >   	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
> > > > +	/*
> > > > +	 * Update the checksum after updating free space/inode counters and
> > > > +	 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
> > > > +	 * checksum in the buffer cache until it is written out and
> > > > +	 * e2fsprogs programs trying to open a file system immediately
> > > > +	 * after it is mounted can fail.
> > > > +	 */
> > > > +	ext4_superblock_csum_set(sb);
> > > >   	if (needs_recovery) {
> > > >   		ext4_msg(sb, KERN_INFO, "recovery complete");
> > > >   		err = ext4_mark_recovery_complete(sb, es);
> > > > --
> > > > 2.31.1
> > > > 
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [PATCH -next] ext4: fix super block checksum incorrect after mount
  2022-05-25  1:29 [PATCH -next] ext4: fix super block checksum incorrect after mount Ye Bin
  2022-05-25  7:51 ` Ritesh Harjani
@ 2022-06-18  2:12 ` Theodore Ts'o
  1 sibling, 0 replies; 9+ messages in thread
From: Theodore Ts'o @ 2022-06-18  2:12 UTC (permalink / raw)
  To: adilger.kernel, linux-ext4, Ye Bin; +Cc: Theodore Ts'o, jack, linux-kernel

On Wed, 25 May 2022 09:29:04 +0800, Ye Bin wrote:
> We got issue as follows:
> [home]# mount  /dev/sda  test
> EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> [home]# dmesg
> EXT4-fs (sda): warning: mounting fs with errors, running e2fsck is recommended
> EXT4-fs (sda): Errors on filesystem, clearing orphan list.
> EXT4-fs (sda): recovery complete
> EXT4-fs (sda): mounted filesystem with ordered data mode. Quota mode: none.
> [home]# debugfs /dev/sda
> debugfs 1.46.5 (30-Dec-2021)
> Checksum errors in superblock!  Retrying...
> 
> [...]

Applied, thanks!

[1/1] ext4: fix super block checksum incorrect after mount
      commit: 17217902fc4fcba1d143e59b308fa7de4c372f50

Best regards,
-- 
Theodore Ts'o <tytso@mit.edu>

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

end of thread, other threads:[~2022-06-18  2:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-25  1:29 [PATCH -next] ext4: fix super block checksum incorrect after mount Ye Bin
2022-05-25  7:51 ` Ritesh Harjani
2022-05-25 11:33   ` yebin
2022-05-25 11:54   ` Jan Kara
2022-05-25 15:16     ` Ritesh Harjani
2022-05-25 15:57       ` Jan Kara
2022-05-27  9:16     ` yebin
2022-05-27 10:18       ` Jan Kara
2022-06-18  2:12 ` Theodore Ts'o

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).