Skip to contentSkip to conversation

f2fs: atomic: fix to truncate pagecache before on-disk metadata truncation

PATCH Superseded Chao Yu 2 participants 6 messages Jul 26, 2024 – Jul 31, 2024 F2FS file system

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH v2 3/4 f2fs: atomic: fix to truncate pagecache before on-disk metadata truncation

We should always truncate pagecache while truncating on-disk data.

Fixes a46bebd502fe ("f2fs: synchronize atomic write aborts")

---
v2:
- fix to use cow_inode instead of inode

 fs/f2fs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/f2fs/file.c +1 -0
index 6c62f76474d1..54886ddcb8ab 100644
@@ -2202,6 +2202,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
 		F2FS_I(fi->cow_inode)->atomic_inode = inode;
 	} else {
 		/* Reuse the already created COW inode */
+		truncate_setsize(fi->cow_inode, 0);
 		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
 		if (ret) {
 			f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
Signature · 1 line
2.40.1
maintainer
#1 lore raw
Quoted 15 lines · Chao Yu

We should always truncate pagecache while truncating on-disk data.

Fixes a46bebd502fe ("f2fs: synchronize atomic write aborts")

---
v2:
- fix to use cow_inode instead of inode

 fs/f2fs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/f2fs/file.c +0 -0
index 6c62f76474d1..54886ddcb8ab 100644

fs/f2fs/file.c static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate) · line 2205

Quoted 4 lines
fs/f2fs/file.c +1 -0
@@ -2202,6 +2202,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
 		F2FS_I(fi->cow_inode)->atomic_inode = inode;
 	} else {
 		/* Reuse the already created COW inode */
+		truncate_setsize(fi->cow_inode, 0);

What if the below truncation failed?

Quoted 5 lines
fs/f2fs/file.c +0 -0
 		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
 		if (ret) {
 			f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
Signature · 1 line
2.40.1
author maintainer
#2 lore raw
Quoted 11 lines · Jaegeuk Kim
Quoted 10 lines · Chao Yu

We should always truncate pagecache while truncating on-disk data.

Fixes a46bebd502fe ("f2fs: synchronize atomic write aborts")

---
v2:
- fix to use cow_inode instead of inode

  fs/f2fs/file.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 11 lines

fs/f2fs/file.c static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate) · line 2205

Quoted 9 lines
fs/f2fs/file.c +1 -0
index 6c62f76474d1..54886ddcb8ab 100644
@@ -2202,6 +2202,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
  		F2FS_I(fi->cow_inode)->atomic_inode = inode;
  	} else {
  		/* Reuse the already created COW inode */
+		truncate_setsize(fi->cow_inode, 0);

What if the below truncation failed?

What about just dropping page cache and do not set isize to 0?

Thanks,

Quoted 6 lines
Quoted 6 lines
fs/f2fs/file.c +0 -0
  		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
  		if (ret) {
  			f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
Signature · 1 line
2.40.1
maintainer
#3 lore raw
Quoted 24 lines · Chao Yu
Quoted 11 lines · Jaegeuk Kim
Quoted 10 lines · Chao Yu

We should always truncate pagecache while truncating on-disk data.

Fixes a46bebd502fe ("f2fs: synchronize atomic write aborts")

---
v2:
- fix to use cow_inode instead of inode

  fs/f2fs/file.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 11 lines

fs/f2fs/file.c static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate) · line 2205

Quoted 9 lines
fs/f2fs/file.c +1 -0
index 6c62f76474d1..54886ddcb8ab 100644
@@ -2202,6 +2202,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
  		F2FS_I(fi->cow_inode)->atomic_inode = inode;
  	} else {
  		/* Reuse the already created COW inode */
+		truncate_setsize(fi->cow_inode, 0);

What if the below truncation failed?

Quoted 1 line

What about just dropping page cache and do not set isize to 0?

Can we also check if there's any dirty page before truncating it?

Quoted 9 lines

Thanks,

Quoted 6 lines
Quoted 6 lines
fs/f2fs/file.c +0 -0
  		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
  		if (ret) {
  			f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
Signature · 1 line
2.40.1
author maintainer
#4 lore raw
Quoted 25 lines · Jaegeuk Kim
Quoted 24 lines · Chao Yu
Quoted 22 lines · Jaegeuk Kim
Quoted 15 lines · Chao Yu

We should always truncate pagecache while truncating on-disk data.

Fixes a46bebd502fe ("f2fs: synchronize atomic write aborts")

---
v2:
- fix to use cow_inode instead of inode

   fs/f2fs/file.c | 1 +
   1 file changed, 1 insertion(+)
fs/f2fs/file.c +0 -0
index 6c62f76474d1..54886ddcb8ab 100644

fs/f2fs/file.c static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate) · line 2205

Quoted 4 lines
fs/f2fs/file.c +1 -0
@@ -2202,6 +2202,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
   		F2FS_I(fi->cow_inode)->atomic_inode = inode;
   	} else {
   		/* Reuse the already created COW inode */
+		truncate_setsize(fi->cow_inode, 0);

What if the below truncation failed?

Quoted 3 lines
Quoted 1 line

What about just dropping page cache and do not set isize to 0?

Can we also check if there's any dirty page before truncating it?

Sure, so how about this?

		/* Reuse the already created COW inode */
		if (get_dirty_pages(fi->cow_inode)) {
			ret = filemap_write_and_wait(fi->cow_inode->i_mapping);
			if (ret) {
				f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
				goto out;
			}
		}

		truncate_inode_pages(fi->cow_inode->i_mapping, 0);

		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
		if (ret) {
			f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
			goto out;
		}

		i_size_write(fi->cow_inode, 0);

Thanks,

Quoted 10 lines
Quoted 10 lines

Thanks,

Quoted 6 lines
Quoted 6 lines
fs/f2fs/file.c +0 -0
   		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
   		if (ret) {
   			f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
Signature · 1 line
2.40.1
maintainer
#5 lore raw
Quoted 26 lines · Chao Yu
Quoted 25 lines · Jaegeuk Kim
Quoted 24 lines · Chao Yu
Quoted 22 lines · Jaegeuk Kim
Quoted 15 lines · Chao Yu

We should always truncate pagecache while truncating on-disk data.

Fixes a46bebd502fe ("f2fs: synchronize atomic write aborts")

---
v2:
- fix to use cow_inode instead of inode

   fs/f2fs/file.c | 1 +
   1 file changed, 1 insertion(+)
fs/f2fs/file.c +0 -0
index 6c62f76474d1..54886ddcb8ab 100644

fs/f2fs/file.c static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate) · line 2205

Quoted 4 lines
fs/f2fs/file.c +1 -0
@@ -2202,6 +2202,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
   		F2FS_I(fi->cow_inode)->atomic_inode = inode;
   	} else {
   		/* Reuse the already created COW inode */
+		truncate_setsize(fi->cow_inode, 0);

What if the below truncation failed?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

What about just dropping page cache and do not set isize to 0?

Can we also check if there's any dirty page before truncating it?

Sure, so how about this?

		/* Reuse the already created COW inode */
		if (get_dirty_pages(fi->cow_inode)) {

Was supposed to be a bug here?

Quoted 8 lines
			ret = filemap_write_and_wait(fi->cow_inode->i_mapping);
			if (ret) {
				f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
				goto out;
			}
		}

		truncate_inode_pages(fi->cow_inode->i_mapping, 0);

invalidate_mapping_pages() would be much safer?

Quoted 21 lines
		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
		if (ret) {
			f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
			goto out;
		}

		i_size_write(fi->cow_inode, 0);

Thanks,

Quoted 10 lines
Quoted 10 lines

Thanks,

Quoted 6 lines
Quoted 6 lines
fs/f2fs/file.c +0 -0
   		ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
   		if (ret) {
   			f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
Signature · 1 line
2.40.1