Skip to contentSkip to conversation

btrfs: fixes for inline extent fallback and error handling

PATCH In review [email protected] 3 participants 9 messages Dec 15, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: fixes for inline extent fallback and error handling

From: Filipe Manana <[email protected]>

A couple fixes and a cleanup in the inline extent creation path. Details in the changelogs.

Filipe Manana (3):
btrfs: do not free data reservation in fallback from inline due to -ENOSPC
btrfs: fix reservation leak in some error paths when inserting inline extent
btrfs: update stale comment in __cow_file_range_inline()

 fs/btrfs/inode.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/3 btrfs: do not free data reservation in fallback from inline due to -ENOSPC

From: Filipe Manana <[email protected]>

If we fail to create an inline extent due to -ENOSPC, we will attempt to go through the normal COW path, reserve an extent, create an ordered extent, etc. However we were always freeing the reserved qgroup data, which is wrong since we will use data. Fix this by freeing the reserved qgroup data in __cow_file_range_inline() only if we are not doing the fallback (ret is <= 0).

---
 fs/btrfs/inode.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +5 -1
index 566780eb80ff..605589f29da7 100644
@@ -675,8 +675,12 @@ static noinline int __cow_file_range_inline(struct btrfs_inode *inode,
 	 * it won't count as data extent, free them directly here.
 	 * And at reserve time, it's always aligned to page size, so
 	 * just free one page here.
+	 *
+	 * If we fallback to non-inline (ret == 1) due to -ENOSPC, then we need
+	 * to keep the data reservation.
 	 */
-	btrfs_qgroup_free_data(inode, NULL, 0, fs_info->sectorsize, NULL);
+	if (ret <= 0)
+		btrfs_qgroup_free_data(inode, NULL, 0, fs_info->sectorsize, NULL);
 	btrfs_free_path(path);
 	btrfs_end_transaction(trans);
 	return ret;
Signature · 1 line
2.47.2
#3 lore raw

PATCH 2/3 btrfs: fix reservation leak in some error paths when inserting inline extent

In reply to [email protected]

From: Filipe Manana <[email protected]>

If we fail to allocate a path or join a transaction, we return from __cow_file_range_inline() without freeing the reserved qgroup data, resulting in a leak. Fix this by ensuring we call btrfs_qgroup_free_data() in such cases.

---
 fs/btrfs/inode.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
fs/btrfs/inode.c +10 -6
index 605589f29da7..f1ead789146b 100644
@@ -619,19 +619,22 @@ static noinline int __cow_file_range_inline(struct btrfs_inode *inode,
 	struct btrfs_drop_extents_args drop_args = { 0 };
 	struct btrfs_root *root = inode->root;
 	struct btrfs_fs_info *fs_info = root->fs_info;
-	struct btrfs_trans_handle *trans;
+	struct btrfs_trans_handle *trans = NULL;
 	u64 data_len = (compressed_size ?: size);
 	int ret;
 	struct btrfs_path *path;
 
 	path = btrfs_alloc_path();
-	if (!path)
-		return -ENOMEM;
+	if (!path) {
+		ret = -ENOMEM;
+		goto out;
+	}
 
 	trans = btrfs_join_transaction(root);
 	if (IS_ERR(trans)) {
-		btrfs_free_path(path);
-		return PTR_ERR(trans);
+		ret = PTR_ERR(trans);
+		trans = NULL;
+		goto out;
 	}
 	trans->block_rsv = &inode->block_rsv;
 
@@ -682,7 +685,8 @@ static noinline int __cow_file_range_inline(struct btrfs_inode *inode,
 	if (ret <= 0)
 		btrfs_qgroup_free_data(inode, NULL, 0, fs_info->sectorsize, NULL);
 	btrfs_free_path(path);
-	btrfs_end_transaction(trans);
+	if (trans)
+		btrfs_end_transaction(trans);
 	return ret;
 }
 
Signature · 1 line
2.47.2
#4 lore raw

PATCH 3/3 btrfs: update stale comment in __cow_file_range_inline()

In reply to [email protected]

From: Filipe Manana <[email protected]>

We mention that the reserved data space is page size aligned but that's not true anymore, as it's sector size aligned instead.
In commit 0bb067ca64e3 ("btrfs: fix the qgroup data free range for inline data extents") we updated the amount passed to btrfs_qgroup_free_data() from page size to sector size, but forgot to update the comment.

---
 fs/btrfs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/inode.c +1 -1
index f1ead789146b..6ae36cc5bcda 100644
@@ -676,7 +676,7 @@ static noinline int __cow_file_range_inline(struct btrfs_inode *inode,
 	/*
 	 * Don't forget to free the reserved space, as for inlined extent
 	 * it won't count as data extent, free them directly here.
-	 * And at reserve time, it's always aligned to page size, so
+	 * And at reserve time, it's always aligned to sector size, so
 	 * just free one page here.
 	 *
 	 * If we fallback to non-inline (ret == 1) due to -ENOSPC, then we need
Signature · 1 line
2.47.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] btrfs: do not free data reservation in fallback from inline due to -ENOSPC

In reply to [email protected]
Quoted 10 lines · 在 2025/12/15 20:08, [email protected] 写道:

From: Filipe Manana <[email protected]>

If we fail to create an inline extent due to -ENOSPC, we will attempt to go through the normal COW path, reserve an extent, create an ordered extent, etc. However we were always freeing the reserved qgroup data, which is wrong since we will use data. Fix this by freeing the reserved qgroup data in __cow_file_range_inline() only if we are not doing the fallback (ret is <= 0).

Thanks,
Qu

Quoted 22 lines
---
  fs/btrfs/inode.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +5 -1
index 566780eb80ff..605589f29da7 100644
@@ -675,8 +675,12 @@ static noinline int __cow_file_range_inline(struct btrfs_inode *inode,
  	 * it won't count as data extent, free them directly here.
  	 * And at reserve time, it's always aligned to page size, so
  	 * just free one page here.
+	 *
+	 * If we fallback to non-inline (ret == 1) due to -ENOSPC, then we need
+	 * to keep the data reservation.
  	 */
-	btrfs_qgroup_free_data(inode, NULL, 0, fs_info->sectorsize, NULL);
+	if (ret <= 0)
+		btrfs_qgroup_free_data(inode, NULL, 0, fs_info->sectorsize, NULL);
  	btrfs_free_path(path);
  	btrfs_end_transaction(trans);
  	return ret;
#5 lore raw

Re: [PATCH 3/3] btrfs: update stale comment in __cow_file_range_inline()

In reply to [email protected]
Quoted 18 lines · 在 2025/12/15 20:08, [email protected] 写道:

From: Filipe Manana <[email protected]>

We mention that the reserved data space is page size aligned but that's not true anymore, as it's sector size aligned instead.
In commit 0bb067ca64e3 ("btrfs: fix the qgroup data free range for inline data extents") we updated the amount passed to btrfs_qgroup_free_data() from page size to sector size, but forgot to update the comment.

---
  fs/btrfs/inode.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/inode.c +0 -0
index f1ead789146b..6ae36cc5bcda 100644

fs/btrfs/inode.c static noinline int __cow_file_range_inline(struct btrfs_inode *inode, · line 680

Quoted 6 lines
fs/btrfs/inode.c +1 -1
@@ -676,7 +676,7 @@ static noinline int __cow_file_range_inline(struct btrfs_inode *inode,
  	/*
  	 * Don't forget to free the reserved space, as for inlined extent
  	 * it won't count as data extent, free them directly here.
-	 * And at reserve time, it's always aligned to page size, so
+	 * And at reserve time, it's always aligned to sector size, so
  	 * just free one page here.

There is still a "page" reference here.

Other than that it looks good to me.

Thanks,
Qu

Quoted 2 lines
fs/btrfs/inode.c +0 -0
  	 *
  	 * If we fallback to non-inline (ret == 1) due to -ENOSPC, then we need
#6 lore raw

Re: [PATCH 3/3] btrfs: update stale comment in __cow_file_range_inline()

Quoted 17 lines · Qu Wenruo
Quoted 13 lines · 在 2025/12/15 20:08, [email protected] 写道:

From: Filipe Manana <[email protected]>

We mention that the reserved data space is page size aligned but that's not true anymore, as it's sector size aligned instead.
In commit 0bb067ca64e3 ("btrfs: fix the qgroup data free range for inline data extents") we updated the amount passed to btrfs_qgroup_free_data() from page size to sector size, but forgot to update the comment.

---
  fs/btrfs/inode.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

fs/btrfs/inode.c static noinline int __cow_file_range_inline(struct btrfs_inode *inode, · line 680

Quoted 11 lines
fs/btrfs/inode.c +1 -1
index f1ead789146b..6ae36cc5bcda 100644
@@ -676,7 +676,7 @@ static noinline int __cow_file_range_inline(struct btrfs_inode *inode,
      /*
       * Don't forget to free the reserved space, as for inlined extent
       * it won't count as data extent, free them directly here.
-      * And at reserve time, it's always aligned to page size, so
+      * And at reserve time, it's always aligned to sector size, so
       * just free one page here.

There is still a "page" reference here.

Ah yes, I will update it before pushing to for-next, thanks.

Quoted 9 lines

Other than that it looks good to me.

Thanks,
Qu

Quoted 2 lines
fs/btrfs/inode.c +0 -0
       *
       * If we fallback to non-inline (ret == 1) due to -ENOSPC, then we need
Reviewed-by
#7 lore raw

Re: [PATCH 3/3] btrfs: update stale comment in __cow_file_range_inline()

Quoted 32 lines · Filipe Manana
Quoted 17 lines · Qu Wenruo
Quoted 13 lines · 在 2025/12/15 20:08, [email protected] 写道:

From: Filipe Manana <[email protected]>

We mention that the reserved data space is page size aligned but that's not true anymore, as it's sector size aligned instead.
In commit 0bb067ca64e3 ("btrfs: fix the qgroup data free range for inline data extents") we updated the amount passed to btrfs_qgroup_free_data() from page size to sector size, but forgot to update the comment.

---
   fs/btrfs/inode.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

fs/btrfs/inode.c static noinline int __cow_file_range_inline(struct btrfs_inode *inode, · line 680

Quoted 11 lines
fs/btrfs/inode.c +1 -1
index f1ead789146b..6ae36cc5bcda 100644
@@ -676,7 +676,7 @@ static noinline int __cow_file_range_inline(struct btrfs_inode *inode,
       /*
        * Don't forget to free the reserved space, as for inlined extent
        * it won't count as data extent, free them directly here.
-      * And at reserve time, it's always aligned to page size, so
+      * And at reserve time, it's always aligned to sector size, so
        * just free one page here.

There is still a "page" reference here.

Quoted 1 line

Ah yes, I will update it before pushing to for-next, thanks.

Oh, forgot my tag.

Thanks,
Qu

Quoted 10 lines
Quoted 10 lines

Other than that it looks good to me.

Thanks,
Qu

Quoted 2 lines
fs/btrfs/inode.c +0 -0
        *
        * If we fallback to non-inline (ret == 1) due to -ENOSPC, then we need
Reviewed-by on the v1 series
#8 lore raw
In reply to [email protected]
Quoted 9 lines · On Mon, Dec 15, 2025 at 09:38:24AM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

A couple fixes and a cleanup in the inline extent creation path. Details in the changelogs.

Filipe Manana (3):
btrfs: do not free data reservation in fallback from inline due to -ENOSPC
btrfs: fix reservation leak in some error paths when inserting inline extent
btrfs: update stale comment in __cow_file_range_inline()