Skip to contentSkip to conversation

btrfs: fix a potential hole-punching failure

PATCH In review bingjingc 3 participants 3 messages Mar 25, 2021 – Mar 29, 2021 Btrfs file system

lore
Share & tools
lore

bingjingc

author
#0 lore raw

PATCH v2 1/1 btrfs: fix a potential hole-punching failure

From: BingJing Chang <[email protected]>

In commit d77815461f04 ("btrfs: Avoid trucating page or punching hole in a already existed hole."), existed holes can be skipped by calling find_first_non_hole() to adjust *start and *len. However, if the given len is invalid and large, when an EXTENT_MAP_HOLE extent is found, the *len will not be set to zero because (em->start + em->len) is less than (*start + *len). Then the ret will be 1 but the *len will not be set to
0. The propagated non-zero ret will result in fallocate failure.

In the while-loop of btrfs_replace_file_extents(), len is not updated every time before it calls find_first_non_hole(). That is, after btrfs_drop_extents() successfully drops the last non-hole file extent, it may fail with -ENOSPC when attempting to drop a file extent item representing a hole. The problem can happen. After it calls find_first_non_hole(), the cur_offset will be adjusted to be larger than or equal to end. However, since the len is not set to zero. The break-loop condition (ret && !len) will not meet. After it leaves the while-loop, fallocate will return 1, which is an unexpected return value.

We're not able to construct a reproducible way to let btrfs_drop_extents() fail with -ENOSPC after it drops the last non-hole file extent but with remaining holes left. However, it's quite easy to fix. We just need to update and check the len every time before we call find_first_non_hole(). To make the while loop more readable, we also pull the variable updates to the bottom of loop like this:

while (cur_offset < end) {
        ...
        // update cur_offset & len
        // advance cur_offset & len in hole-punching case if needed
}
Fixes d77815461f04 ("btrfs: Avoid trucating page or punching hole in a

already existed hole.")

---
 fs/btrfs/file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/file.c +3 -3
index 0e155f0..dccb017 100644
@@ -2735,8 +2735,6 @@ int btrfs_replace_file_extents(struct inode *inode, struct btrfs_path *path,
 			extent_info->file_offset += replace_len;
 		}
 
-		cur_offset = drop_args.drop_end;
-
 		ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
 		if (ret)
 			break;
@@ -2756,7 +2754,9 @@ int btrfs_replace_file_extents(struct inode *inode, struct btrfs_path *path,
 		BUG_ON(ret);	/* shouldn't happen */
 		trans->block_rsv = rsv;
 
-		if (!extent_info) {
+		cur_offset = drop_args.drop_end;
+		len = end - cur_offset;
+		if (!extent_info && len) {
 			ret = find_first_non_hole(BTRFS_I(inode), &cur_offset,
 						  &len);
 			if (unlikely(ret < 0))
Signature · 1 line
2.7.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 34 lines · bingjingc

From: BingJing Chang <[email protected]>

In commit d77815461f04 ("btrfs: Avoid trucating page or punching hole in a already existed hole."), existed holes can be skipped by calling find_first_non_hole() to adjust *start and *len. However, if the given len is invalid and large, when an EXTENT_MAP_HOLE extent is found, the *len will not be set to zero because (em->start + em->len) is less than (*start + *len). Then the ret will be 1 but the *len will not be set to
0. The propagated non-zero ret will result in fallocate failure.

In the while-loop of btrfs_replace_file_extents(), len is not updated every time before it calls find_first_non_hole(). That is, after btrfs_drop_extents() successfully drops the last non-hole file extent, it may fail with -ENOSPC when attempting to drop a file extent item representing a hole. The problem can happen. After it calls find_first_non_hole(), the cur_offset will be adjusted to be larger than or equal to end. However, since the len is not set to zero. The break-loop condition (ret && !len) will not meet. After it leaves the while-loop, fallocate will return 1, which is an unexpected return value.

We're not able to construct a reproducible way to let btrfs_drop_extents() fail with -ENOSPC after it drops the last non-hole file extent but with remaining holes left. However, it's quite easy to fix. We just need to update and check the len every time before we call find_first_non_hole(). To make the while loop more readable, we also pull the variable updates to the bottom of loop like this:

while (cur_offset < end) {
        ...
        // update cur_offset & len
        // advance cur_offset & len in hole-punching case if needed
}
Quoted 6 lines
Fixes d77815461f04 ("btrfs: Avoid trucating page or punching hole in a

already existed hole.")

Looks good, thanks.

Quoted 31 lines
---
 fs/btrfs/file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/file.c +3 -4
index 0e155f0..dccb017 100644
@@ -2735,8 +2735,6 @@ int btrfs_replace_file_extents(struct inode *inode, struct btrfs_path *path,
                        extent_info->file_offset += replace_len;
                }
 
-               cur_offset = drop_args.drop_end;
-
                ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
                if (ret)
                        break;
@@ -2756,7 +2754,9 @@ int btrfs_replace_file_extents(struct inode *inode, struct btrfs_path *path,
                BUG_ON(ret);    /* shouldn't happen */
                trans->block_rsv = rsv;
 
-               if (!extent_info) {
+               cur_offset = drop_args.drop_end;
+               len = end - cur_offset;
+               if (!extent_info && len) {
                        ret = find_first_non_hole(BTRFS_I(inode), &cur_offset,
                                                  &len);
                        if (unlikely(ret < 0))
--

2.7.4

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#2 lore raw
In reply to bingjingc
Quoted 33 lines · bingjingc

From: BingJing Chang <[email protected]>

In commit d77815461f04 ("btrfs: Avoid trucating page or punching hole in a already existed hole."), existed holes can be skipped by calling find_first_non_hole() to adjust *start and *len. However, if the given len is invalid and large, when an EXTENT_MAP_HOLE extent is found, the *len will not be set to zero because (em->start + em->len) is less than (*start + *len). Then the ret will be 1 but the *len will not be set to
0. The propagated non-zero ret will result in fallocate failure.

In the while-loop of btrfs_replace_file_extents(), len is not updated every time before it calls find_first_non_hole(). That is, after btrfs_drop_extents() successfully drops the last non-hole file extent, it may fail with -ENOSPC when attempting to drop a file extent item representing a hole. The problem can happen. After it calls find_first_non_hole(), the cur_offset will be adjusted to be larger than or equal to end. However, since the len is not set to zero. The break-loop condition (ret && !len) will not meet. After it leaves the while-loop, fallocate will return 1, which is an unexpected return value.

We're not able to construct a reproducible way to let btrfs_drop_extents() fail with -ENOSPC after it drops the last non-hole file extent but with remaining holes left. However, it's quite easy to fix. We just need to update and check the len every time before we call find_first_non_hole(). To make the while loop more readable, we also pull the variable updates to the bottom of loop like this:

while (cur_offset < end) {
        ...
        // update cur_offset & len
        // advance cur_offset & len in hole-punching case if needed
}
Quoted 6 lines
Fixes d77815461f04 ("btrfs: Avoid trucating page or punching hole in a

already existed hole.")

Thanks, added to misc-next.