Skip to contentSkip to conversation

btrfs: some minor fixes around extent maps

PATCH Superseded [email protected] 3 participants 12 messages Mar 13, 2024 – Mar 14, 2024 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: some minor fixes around extent maps

From: Filipe Manana <[email protected]>

Some minor fixes around extent maps for unexpected error cases. More details in the change logs.

Filipe Manana (3):
btrfs: fix extent map leak in unexpected scenario at unpin_extent_cache()
btrfs: fix warning messages not printing interval at unpin_extent_range()
btrfs: fix message not properly printing interval when adding extent map

 fs/btrfs/extent_map.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 btrfs: fix extent map leak in unexpected scenario at unpin_extent_cache()

From: Filipe Manana <[email protected]>

At unpin_extent_cache() if we happen to find an extent map with an unexpected start offset, we jump to the 'out' label and never release the reference we added to the extent map through the call to lookup_extent_mapping(), therefore resulting in a leak. So fix this by moving the free_extent_map() under the 'out' label.

Fixes c03c89f821e5 ("btrfs: handle errors returned from unpin_extent_cache()")
---
 fs/btrfs/extent_map.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_map.c +1 -1
index 347ca13d15a9..e03953dbcd5e 100644
@@ -340,9 +340,9 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
 		em->mod_len = em->len;
 	}
 
-	free_extent_map(em);
 out:
 	write_unlock(&tree->lock);
+	free_extent_map(em);
 	return ret;
 
 }
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 btrfs: fix warning messages not printing interval at unpin_extent_range()

In reply to [email protected]

From: Filipe Manana <[email protected]>

At unpin_extent_range() we print warning messages that are supposed to print an interval in the form "[X, Y)", with the first element being an inclusive start offset and the second element being the exclusive end offset of a range. However we end up printing the range's length instead of the range's exclusive end offset, so fix that to avoid having confusing and non-sense messages in case we hit one of these unexpected scenarios.

Fixes 00deaf04df35 ("btrfs: log messages at unpin_extent_range() during unexpected cases")
---
 fs/btrfs/extent_map.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_map.c +2 -2
index e03953dbcd5e..2cfc6e8cf76f 100644
@@ -309,7 +309,7 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
 		btrfs_warn(fs_info,
 "no extent map found for inode %llu (root %lld) when unpinning extent range [%llu, %llu), generation %llu",
 			   btrfs_ino(inode), btrfs_root_id(inode->root),
-			   start, len, gen);
+			   start, start + len, gen);
 		ret = -ENOENT;
 		goto out;
 	}
@@ -318,7 +318,7 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
 		btrfs_warn(fs_info,
 "found extent map for inode %llu (root %lld) with unexpected start offset %llu when unpinning extent range [%llu, %llu), generation %llu",
 			   btrfs_ino(inode), btrfs_root_id(inode->root),
-			   em->start, start, len, gen);
+			   em->start, start, start + len, gen);
 		ret = -EUCLEAN;
 		goto out;
 	}
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 btrfs: fix message not properly printing interval when adding extent map

In reply to [email protected]

From: Filipe Manana <[email protected]>

At btrfs_add_extent_mapping(), if we are unable to merge the existing extent map, we print a warning message that suggests interval ranges in the form "[X, Y)", where the first element is the inclusive start offset of a range and the second element is the exclusive end offset. However we end up printing the length of the ranges instead of the exclusive end offsets. So fix this by printing the range end offsets.

---
 fs/btrfs/extent_map.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_map.c +2 -2
index 2cfc6e8cf76f..16685cb8a91d 100644
@@ -634,8 +634,8 @@ int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
 				*em_in = NULL;
 				WARN_ONCE(ret,
 "extent map merge error existing [%llu, %llu) with em [%llu, %llu) start %llu\n",
-					  existing->start, existing->len,
-					  orig_start, orig_len, start);
+					  existing->start, extent_map_end(existing),
+					  orig_start, orig_start + orig_len, start);
 			}
 			free_extent_map(existing);
 		}
Signature · 1 line
2.43.0
#4 lore raw

v2 cover btrfs: some minor fixes around extent maps

In reply to [email protected]

From: Filipe Manana <[email protected]>

Some minor fixes around extent maps for unexpected error cases. More details in the change logs.

V2: Added patch 4/4.

Filipe Manana (4):
btrfs: fix extent map leak in unexpected scenario at unpin_extent_cache()
btrfs: fix warning messages not printing interval at unpin_extent_range()
btrfs: fix message not properly printing interval when adding extent map
btrfs: use btrfs_warn() to log message at btrfs_add_extent_mapping()

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

PATCH v2 1/4 btrfs: fix extent map leak in unexpected scenario at unpin_extent_cache()

From: Filipe Manana <[email protected]>

At unpin_extent_cache() if we happen to find an extent map with an unexpected start offset, we jump to the 'out' label and never release the reference we added to the extent map through the call to lookup_extent_mapping(), therefore resulting in a leak. So fix this by moving the free_extent_map() under the 'out' label.

Fixes c03c89f821e5 ("btrfs: handle errors returned from unpin_extent_cache()")
---
 fs/btrfs/extent_map.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_map.c +1 -1
index 347ca13d15a9..e03953dbcd5e 100644
@@ -340,9 +340,9 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
 		em->mod_len = em->len;
 	}
 
-	free_extent_map(em);
 out:
 	write_unlock(&tree->lock);
+	free_extent_map(em);
 	return ret;
 
 }
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 2/4 btrfs: fix warning messages not printing interval at unpin_extent_range()

In reply to [email protected]

From: Filipe Manana <[email protected]>

At unpin_extent_range() we print warning messages that are supposed to print an interval in the form "[X, Y)", with the first element being an inclusive start offset and the second element being the exclusive end offset of a range. However we end up printing the range's length instead of the range's exclusive end offset, so fix that to avoid having confusing and non-sense messages in case we hit one of these unexpected scenarios.

Fixes 00deaf04df35 ("btrfs: log messages at unpin_extent_range() during unexpected cases")
---
 fs/btrfs/extent_map.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_map.c +2 -2
index e03953dbcd5e..2cfc6e8cf76f 100644
@@ -309,7 +309,7 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
 		btrfs_warn(fs_info,
 "no extent map found for inode %llu (root %lld) when unpinning extent range [%llu, %llu), generation %llu",
 			   btrfs_ino(inode), btrfs_root_id(inode->root),
-			   start, len, gen);
+			   start, start + len, gen);
 		ret = -ENOENT;
 		goto out;
 	}
@@ -318,7 +318,7 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
 		btrfs_warn(fs_info,
 "found extent map for inode %llu (root %lld) with unexpected start offset %llu when unpinning extent range [%llu, %llu), generation %llu",
 			   btrfs_ino(inode), btrfs_root_id(inode->root),
-			   em->start, start, len, gen);
+			   em->start, start, start + len, gen);
 		ret = -EUCLEAN;
 		goto out;
 	}
Signature · 1 line
2.43.0
#8 lore raw

PATCH v2 3/4 btrfs: fix message not properly printing interval when adding extent map

In reply to [email protected]

From: Filipe Manana <[email protected]>

At btrfs_add_extent_mapping(), if we are unable to merge the existing extent map, we print a warning message that suggests interval ranges in the form "[X, Y)", where the first element is the inclusive start offset of a range and the second element is the exclusive end offset. However we end up printing the length of the ranges instead of the exclusive end offsets. So fix this by printing the range end offsets.

---
 fs/btrfs/extent_map.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_map.c +2 -2
index 2cfc6e8cf76f..16685cb8a91d 100644
@@ -634,8 +634,8 @@ int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
 				*em_in = NULL;
 				WARN_ONCE(ret,
 "extent map merge error existing [%llu, %llu) with em [%llu, %llu) start %llu\n",
-					  existing->start, existing->len,
-					  orig_start, orig_len, start);
+					  existing->start, extent_map_end(existing),
+					  orig_start, orig_start + orig_len, start);
 			}
 			free_extent_map(existing);
 		}
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 4/4 btrfs: use btrfs_warn() to log message at btrfs_add_extent_mapping()

In reply to [email protected]

From: Filipe Manana <[email protected]>

At btrfs_add_extent_mapping(), if we failed to merge the extent map, which is unexpected and theoretically should never happen, we use WARN_ONCE() to log a message which is not great because we don't get information about which filesystem it relates to in case we have multiple btrfs filesystems mounted. So change this to use btrfs_warn() and surround the error check with WARN_ON() so we always get a useful stack trace and the condition is flagged as "unlikely" since it's not expected to ever happen.

---
 fs/btrfs/extent_map.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/extent_map.c +5 -5
index 16685cb8a91d..445f7716f1e2 100644
@@ -629,13 +629,13 @@ int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
 			 */
 			ret = merge_extent_mapping(em_tree, existing,
 						   em, start);
-			if (ret) {
+			if (WARN_ON(ret)) {
 				free_extent_map(em);
 				*em_in = NULL;
-				WARN_ONCE(ret,
-"extent map merge error existing [%llu, %llu) with em [%llu, %llu) start %llu\n",
-					  existing->start, extent_map_end(existing),
-					  orig_start, orig_start + orig_len, start);
+				btrfs_warn(fs_info,
+"extent map merge error existing [%llu, %llu) with em [%llu, %llu) start %llu",
+					   existing->start, extent_map_end(existing),
+					   orig_start, orig_start + orig_len, start);
 			}
 			free_extent_map(existing);
 		}
Signature · 1 line
2.43.0
Reviewed-by on the v2 series
#10 lore raw
In reply to [email protected]
Quoted 5 lines · 在 2024/3/14 03:58, [email protected] 写道:

From: Filipe Manana <[email protected]>

Some minor fixes around extent maps for unexpected error cases. More details in the change logs.

Quoted 7 lines

V2: Added patch 4/4.

Filipe Manana (4):
btrfs: fix extent map leak in unexpected scenario at unpin_extent_cache()
btrfs: fix warning messages not printing interval at unpin_extent_range()
btrfs: fix message not properly printing interval when adding extent map
btrfs: use btrfs_warn() to log message at btrfs_add_extent_mapping()

Thanks,
Qu

Quoted 4 lines
  fs/btrfs/extent_map.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)
Reviewed-by on v2 1/4
#6 lore raw

Re: [PATCH v2 1/4] btrfs: fix extent map leak in unexpected scenario at unpin_extent_cache()

In reply to [email protected]
Quoted 19 lines · On 3/13/24 22:58, [email protected] wrote:

From: Filipe Manana <[email protected]>

At unpin_extent_cache() if we happen to find an extent map with an unexpected start offset, we jump to the 'out' label and never release the reference we added to the extent map through the call to lookup_extent_mapping(), therefore resulting in a leak. So fix this by moving the free_extent_map() under the 'out' label.

Fixes c03c89f821e5 ("btrfs: handle errors returned from unpin_extent_cache()")
---
  fs/btrfs/extent_map.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_map.c +0 -0
index 347ca13d15a9..e03953dbcd5e 100644

fs/btrfs/extent_map.c int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen) · line 348

Quoted 10 lines
fs/btrfs/extent_map.c +1 -1
@@ -340,9 +340,9 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
  		em->mod_len = em->len;
  	}
 
-	free_extent_map(em);
  out:
  	write_unlock(&tree->lock);
+	free_extent_map(em);
  	return ret;
 
  }

Looks good.

Thanks, Anand

Reviewed-by on the v2 series
#11 lore raw
In reply to [email protected]
Quoted 9 lines · On 3/13/24 22:58, [email protected] wrote:

From: Filipe Manana <[email protected]>

Some minor fixes around extent maps for unexpected error cases. More details in the change logs.

V2: Added patch 4/4.

Filipe Manana (4):
btrfs: fix extent map leak in unexpected scenario at unpin_extent_cache() > btrfs: fix warning messages not printing interval at

unpin_extent_range()

Quoted 2 lines

btrfs: fix message not properly printing interval when adding extent map
btrfs: use btrfs_warn() to log message at btrfs_add_extent_mapping()

For all.

Thanks, Anand

Quoted 4 lines
  fs/btrfs/extent_map.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)