Skip to contentSkip to conversation

btrfs: mark file extent range dirty after converting prealloc extents

PATCH Superseded robbieko 2 participants 2 messages May 8 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

robbieko

author
#0 lore raw

PATCH 1/1 btrfs: mark file extent range dirty after converting prealloc extents

From: Robbie Ko <[email protected]>

When writing into a preallocated extent, ordered extent completion calls btrfs_mark_extent_written() to convert the file extent item from BTRFS_FILE_EXTENT_PREALLOC to BTRFS_FILE_EXTENT_REG.
If the preallocated extent was created beyond i_size, for example with fallocate keep-size, and the inode is evicted and loaded again before the write, the inode's file_extent_tree is initialized only up to i_size. The out-of-i_size prealloc extent is therefore not tracked there. After a write into that extent extends i_size, btrfs_mark_extent_written() updates the file extent item, but the corresponding range is not marked dirty in the inode's file_extent_tree. This can leave disk_i_size stale when the filesystem does not use the no-holes feature, so after remount the file size can go back to the old value.
Fix this by marking the written range dirty in file_extent_tree after successfully converting the prealloc extent to a regular extent. A reproducer is:
mkfs.btrfs -f -O ^no-holes $DEV
mount $DEV $MNT
touch $MNT/file
fallocate -n -l 2M $MNT/file
umount $MNT
mount $DEV $MNT
dd if=/dev/zero of=$MNT/file bs=1M count=1 conv=fsync
stat -c %s $MNT/file
umount $MNT
mount $DEV $MNT
stat -c %s $MNT/file
umount $MNT
Before the fix the first stat reports 1048576, while the second one can report 0.

Fixes 41a2ee75aab0 ("btrfs: introduce per-inode file extent tree")
---
 fs/btrfs/file.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/btrfs/file.c +8 -3
index cf1cb5c4db75..8c171ed07008 100644
@@ -633,7 +633,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
 							 trans->transid);
 			btrfs_set_file_extent_num_bytes(leaf, fi,
 							end - other_start);
-			return 0;
+			goto mark_dirty;
 		}
 	}
 
@@ -661,7 +661,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
 							other_end - start);
 			btrfs_set_file_extent_offset(leaf, fi,
 						     start - orig_offset);
-			return 0;
+			goto mark_dirty;
 		}
 	}
 
@@ -788,7 +788,12 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
 		}
 	}
 
-	return 0;
+mark_dirty:
+	ret = btrfs_inode_set_file_extent_range(inode, start, end - start);
+	if (ret)
+		btrfs_abort_transaction(trans, ret);
+
+	return ret;
 }
 
 /*
Signature · 4 lines
2.43.0


Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
#1 lore raw
Quoted 7 lines · robbieko

From: Robbie Ko <[email protected]>

When writing into a preallocated extent, ordered extent completion calls btrfs_mark_extent_written() to convert the file extent item from BTRFS_FILE_EXTENT_PREALLOC to BTRFS_FILE_EXTENT_REG.
If the preallocated extent was created beyond i_size, for example with

Remove "for example", because the only way to create extents beyond i_size is with fallocate...

Quoted 10 lines

fallocate keep-size, and the inode is evicted and loaded again before the write, the inode's file_extent_tree is initialized only up to i_size. The out-of-i_size prealloc extent is therefore not tracked there. After a write into that extent extends i_size, btrfs_mark_extent_written() updates the file extent item, but the corresponding range is not marked dirty in the inode's file_extent_tree. This can leave disk_i_size stale when the filesystem does not use the no-holes feature, so after remount the file size can go back to the old value.
Fix this by marking the written range dirty in file_extent_tree after successfully converting the prealloc extent to a regular extent.

Please use blank lines and paragraphs for better readability.

Blank line here.

Quoted 1 line

A reproducer is:

Another blank line here.

Separating groups of related commands with blank lines also improves readability.

Quoted 4 lines

mkfs.btrfs -f -O ^no-holes $DEV
mount $DEV $MNT
touch $MNT/file
fallocate -n -l 2M $MNT/file

Quoted 8 lines

umount $MNT
mount $DEV $MNT
dd if=/dev/zero of=$MNT/file bs=1M count=1 conv=fsync stat -c %s $MNT/file
umount $MNT
mount $DEV $MNT
stat -c %s $MNT/file
umount $MNT

A blank line here.

Quoted 2 lines

Before the fix the first stat reports 1048576, while the second one can report 0.

Can report? You mean it's not always the case?

I ran the test in latest for-next, and stat always reports 1M - 0 never happened.

Also, do you plan on sending a test case for fstests? Please reply - I have asked the same question for a previous patch and you did not reply.

Quoted 1 line
Fixes 41a2ee75aab0 ("btrfs: introduce per-inode file extent tree")

The correct fix tag/commit is:

Fixes 9ddc959e802b ("btrfs: use the file extent tree infrastructure")
Quoted 2 lines

And the only blank line the changelog has is between the Fixes and SOB tags, which is where none should be.

Thanks.

Quoted 46 lines
---
 fs/btrfs/file.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/btrfs/file.c +8 -4
index cf1cb5c4db75..8c171ed07008 100644
@@ -633,7 +633,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
                                                         trans->transid);
                        btrfs_set_file_extent_num_bytes(leaf, fi,
                                                        end - other_start);
-                       return 0;
+                       goto mark_dirty;
                }
        }
 
@@ -661,7 +661,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
                                                        other_end - start);
                        btrfs_set_file_extent_offset(leaf, fi,
                                                     start - orig_offset);
-                       return 0;
+                       goto mark_dirty;
                }
        }
 
@@ -788,7 +788,12 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
                }
        }
 
-       return 0;
+mark_dirty:
+       ret = btrfs_inode_set_file_extent_range(inode, start, end - start);
+       if (ret)
+               btrfs_abort_transaction(trans, ret);
+
+       return ret;
 }
 
 /*
--

2.43.0

Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.