Skip to contentSkip to conversation

f2fs: Reduce the scope of setting fsck tag when de->name_len is zero

PATCH In review Yangtao Li 2 participants 4 messages Jul 20, 2021 – Aug 4, 2021 F2FS file system

lore
Share & tools
lore

Yangtao Li

author
#0 lore raw

PATCH v3 1/1 f2fs: Reduce the scope of setting fsck tag when de->name_len is zero

I recently found a case where de->name_len is 0 in f2fs_fill_dentries() easily reproduced, and finally set the fsck flag.

Thread A Thread B

f2fs_readdir

	f2fs_read_inline_dir
		ctx->pos = d.max
						f2fs_add_dentry
							f2fs_add_inline_entry
								do_convert_inline_dir
							f2fs_add_regular_entry

f2fs_readdir
f2fs_fill_dentries
set_sbi_flag(sbi, SBI_NEED_FSCK)

Process A opens the folder, and has been reading without closing it. During this period, Process B created a file under the folder (occupying multiple f2fs_dir_entry, exceeding the d.max of the inline dir). After creation, process A uses the d.max of inline dir to read it again, and it will read that de->name_len is 0.

And Chao pointed out that w/o inline conversion, the race condition still can happen as below

dir_entry1: A
dir_entry2: B
dir_entry3: C
free slot: _
ctx->pos: ^

Before:
AAAABBBB___

	 ^

Thread B delete dir_entry2, and create dir_entry3.

After:
AAAACCCCC__

	 ^

In these scenarios, the file system is not damaged, and it's hard to avoid it. But we can bypass tagging FSCK flag if:
a) bit_pos (:= ctx->pos % d->max) is non-zero & b) before bit_pos moves to first valid dir_entry.

---
 fs/f2fs/dir.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
fs/f2fs/dir.c +9 -5
index 456651682daf..bfe942733b5e 100644
@@ -1000,6 +1000,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
 	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
 	struct blk_plug plug;
 	bool readdir_ra = sbi->readdir_ra == 1;
+	bool found_valid_dirent  = false;
 	int err = 0;
 
 	bit_pos = ((unsigned long)ctx->pos % d->max);
@@ -1014,13 +1015,15 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
 
 		de = &d->dentry[bit_pos];
 		if (de->name_len == 0) {
+			if (found_valid_dirent || !bit_pos) {
+				printk_ratelimited(
+					"%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
+					KERN_WARNING, sbi->sb->s_id,
+					le32_to_cpu(de->ino));
+				set_sbi_flag(sbi, SBI_NEED_FSCK);
+			}
 			bit_pos++;
 			ctx->pos = start_pos + bit_pos;
-			printk_ratelimited(
-				"%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
-				KERN_WARNING, sbi->sb->s_id,
-				le32_to_cpu(de->ino));
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
 			continue;
 		}
 
@@ -1063,6 +1066,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
 			f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
 
 		ctx->pos = start_pos + bit_pos;
+		found_valid_dirent = true;
 	}
 out:
 	if (readdir_ra)
Signature · 8 lines
2.32.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
maintainer
#1 lore raw
Quoted 29 lines · Yangtao Li

I recently found a case where de->name_len is 0 in f2fs_fill_dentries() easily reproduced, and finally set the fsck flag.

Thread A Thread B

f2fs_readdir

	f2fs_read_inline_dir
		ctx->pos = d.max
						f2fs_add_dentry
							f2fs_add_inline_entry
								do_convert_inline_dir
							f2fs_add_regular_entry

f2fs_readdir
f2fs_fill_dentries
set_sbi_flag(sbi, SBI_NEED_FSCK)

Process A opens the folder, and has been reading without closing it. During this period, Process B created a file under the folder (occupying multiple f2fs_dir_entry, exceeding the d.max of the inline dir). After creation, process A uses the d.max of inline dir to read it again, and it will read that de->name_len is 0.

And Chao pointed out that w/o inline conversion, the race condition still can happen as below

dir_entry1: A
dir_entry2: B
dir_entry3: C
free slot: _
ctx->pos: ^

Quoted 3 lines

Before:
AAAABBBB___

	 ^

please use blank instead of tab before '^'

Quoted 5 lines

Thread B delete dir_entry2, and create dir_entry3.

After:
AAAACCCCC__

	 ^

Ditto

Quoted 16 lines

In these scenarios, the file system is not damaged, and it's hard to avoid it. But we can bypass tagging FSCK flag if:
a) bit_pos (:= ctx->pos % d->max) is non-zero & b) before bit_pos moves to first valid dir_entry.

---
  fs/f2fs/dir.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
fs/f2fs/dir.c +0 -0
index 456651682daf..bfe942733b5e 100644

fs/f2fs/dir.c int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, · line 1003

Quoted 4 lines
fs/f2fs/dir.c +1 -0
@@ -1000,6 +1000,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
  	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
  	struct blk_plug plug;
  	bool readdir_ra = sbi->readdir_ra == 1;
+	bool found_valid_dirent  = false;

One more blank before '='.

bool found_valid_dirent = false;

Thanks,

Quoted 26 lines
fs/f2fs/dir.c +7 -5
  	int err = 0;
 
  	bit_pos = ((unsigned long)ctx->pos % d->max);
@@ -1014,13 +1015,15 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
 
  		de = &d->dentry[bit_pos];
  		if (de->name_len == 0) {
+			if (found_valid_dirent || !bit_pos) {
+				printk_ratelimited(
+					"%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
+					KERN_WARNING, sbi->sb->s_id,
+					le32_to_cpu(de->ino));
+				set_sbi_flag(sbi, SBI_NEED_FSCK);
+			}
  			bit_pos++;
  			ctx->pos = start_pos + bit_pos;
-			printk_ratelimited(
-				"%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
-				KERN_WARNING, sbi->sb->s_id,
-				le32_to_cpu(de->ino));
-			set_sbi_flag(sbi, SBI_NEED_FSCK);
  			continue;
  		}
 

fs/f2fs/dir.c int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, · line 1072

Quoted 7 lines
fs/f2fs/dir.c +1 -0
@@ -1063,6 +1066,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
  			f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
 
  		ctx->pos = start_pos + bit_pos;
+		found_valid_dirent = true;
  	}
  out:
  	if (readdir_ra)

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author
#2 lore raw

HI Chao,

From: Chao Yu <[email protected]>
Date: 2021-07-20 18:23:15
To: Yangtao Li <[email protected]>,[email protected]
Cc: [email protected],[email protected]

Quoted 29 lines · Yangtao Li
Quoted 29 lines

I recently found a case where de->name_len is 0 in f2fs_fill_dentries() easily reproduced, and finally set the fsck flag.

Thread A Thread B

f2fs_readdir

	f2fs_read_inline_dir
		ctx->pos = d.max
						f2fs_add_dentry
							f2fs_add_inline_entry
								do_convert_inline_dir
							f2fs_add_regular_entry

f2fs_readdir
f2fs_fill_dentries
set_sbi_flag(sbi, SBI_NEED_FSCK)

Process A opens the folder, and has been reading without closing it. During this period, Process B created a file under the folder (occupying multiple f2fs_dir_entry, exceeding the d.max of the inline dir). After creation, process A uses the d.max of inline dir to read it again, and it will read that de->name_len is 0.

And Chao pointed out that w/o inline conversion, the race condition still can happen as below

dir_entry1: A
dir_entry2: B
dir_entry3: C
free slot: _
ctx->pos: ^

Quoted 5 lines
Quoted 3 lines

Before:
AAAABBBB___

	 ^

please use blank instead of tab before '^'

I don't know exactly what happened. In fact, in v2, spaces were used. Then it was changed to tab in v3.

Quoted 32 lines
Quoted 6 lines

Thread B delete dir_entry2, and create dir_entry3.

After:
AAAACCCCC__

	 ^

Ditto

Quoted 16 lines

In these scenarios, the file system is not damaged, and it's hard to avoid it. But we can bypass tagging FSCK flag if:
a) bit_pos (:= ctx->pos % d->max) is non-zero & b) before bit_pos moves to first valid dir_entry.

---
  fs/f2fs/dir.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
fs/f2fs/dir.c +0 -0
index 456651682daf..bfe942733b5e 100644

fs/f2fs/dir.c int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, · line 1003

Quoted 4 lines
fs/f2fs/dir.c +1 -0
@@ -1000,6 +1000,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
  	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
  	struct blk_plug plug;
  	bool readdir_ra = sbi->readdir_ra == 1;
+	bool found_valid_dirent  = false;

One more blank before '='.

Quoted 2 lines

bool found_valid_dirent = false;

OK.

Thx,

maintainer
#3 lore raw

Hello, Yangtao,

I did some cleanup in your patch, and resend for you, please note that. :)

Thanks,

Quoted 85 lines · 李扬韬

HI Chao,

From: Chao Yu <[email protected]>
Date: 2021-07-20 18:23:15
To: Yangtao Li <[email protected]>,[email protected]
Cc: [email protected],[email protected]

Quoted 29 lines · Yangtao Li
Quoted 29 lines

I recently found a case where de->name_len is 0 in f2fs_fill_dentries() easily reproduced, and finally set the fsck flag.

Thread A Thread B

f2fs_readdir

	f2fs_read_inline_dir
		ctx->pos = d.max
						f2fs_add_dentry
							f2fs_add_inline_entry
								do_convert_inline_dir
							f2fs_add_regular_entry

f2fs_readdir
f2fs_fill_dentries
set_sbi_flag(sbi, SBI_NEED_FSCK)

Process A opens the folder, and has been reading without closing it. During this period, Process B created a file under the folder (occupying multiple f2fs_dir_entry, exceeding the d.max of the inline dir). After creation, process A uses the d.max of inline dir to read it again, and it will read that de->name_len is 0.

And Chao pointed out that w/o inline conversion, the race condition still can happen as below

dir_entry1: A
dir_entry2: B
dir_entry3: C
free slot: _
ctx->pos: ^

Quoted 5 lines
Quoted 3 lines

Before:
AAAABBBB___

	 ^

please use blank instead of tab before '^'

I don't know exactly what happened. In fact, in v2, spaces were used. Then it was changed to tab in v3.

Quoted 32 lines
Quoted 6 lines

Thread B delete dir_entry2, and create dir_entry3.

After:
AAAACCCCC__

	 ^

Ditto

Quoted 16 lines

In these scenarios, the file system is not damaged, and it's hard to avoid it. But we can bypass tagging FSCK flag if:
a) bit_pos (:= ctx->pos % d->max) is non-zero & b) before bit_pos moves to first valid dir_entry.

---
   fs/f2fs/dir.c | 14 +++++++++-----
   1 file changed, 9 insertions(+), 5 deletions(-)
fs/f2fs/dir.c +0 -0
index 456651682daf..bfe942733b5e 100644

fs/f2fs/dir.c int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, · line 1003

Quoted 4 lines
fs/f2fs/dir.c +1 -0
@@ -1000,6 +1000,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
   	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
   	struct blk_plug plug;
   	bool readdir_ra = sbi->readdir_ra == 1;
+	bool found_valid_dirent  = false;

One more blank before '='.

Quoted 2 lines

bool found_valid_dirent = false;

OK.

Thx,