Skip to contentSkip to conversation

btrfs: Fix off-by-one error btrfs_trim_free_extents

PATCH In review Nikolay Borisov 2 participants 2 messages Apr 24, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: Fix off-by-one error btrfs_trim_free_extents

btrfs_trim_free_extents always caps the range it's going to trim based on the size of the device. This happens if find_first_clear_extent_bit detects that untrimmed range is past the last allocated range. Since it doesn't have knowledge of the size of the device it just returns (u64)-1 for end. Then btrfs_trim_free_extents caps this to device->total_bytes. However, btrfs_trim_free_extent calculates 'len' based off of the actual usable end byte - in this case total_bytes - 1.

Fixes 4d877687cbbc ("btrfs: Switch btrfs_trim_free_extents to find_first_clear_extent_bit")

---

David you might want to squash that in the 'Fixes' commit. With this I see generic/500 passing and also full xfstest didn't uncover any new regressions.

 fs/btrfs/extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +1 -1
index d9e2e35700fd..5a4b81259413 100644
@@ -11175,7 +11175,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device,
 		 * end of the device it will set end to -1, in this case it's up
 		 * to the caller to trim the value to the size of the device.
 		 */
-		end = min(end, device->total_bytes);
+		end = min(end, device->total_bytes - 1);
 		len = end - start + 1;
 
 		/* We didn't find any extents */
Signature · 1 line
2.17.1
#1 lore raw
Quoted 8 lines · Nikolay Borisov

btrfs_trim_free_extents always caps the range it's going to trim based on the size of the device. This happens if find_first_clear_extent_bit detects that untrimmed range is past the last allocated range. Since it doesn't have knowledge of the size of the device it just returns (u64)-1 for end. Then btrfs_trim_free_extents caps this to device->total_bytes. However, btrfs_trim_free_extent calculates 'len' based off of the actual usable end byte - in this case total_bytes - 1.

Quoted 7 lines
Fixes 4d877687cbbc ("btrfs: Switch btrfs_trim_free_extents to find_first_clear_extent_bit")

---

David you might want to squash that in the 'Fixes' commit. With this I see generic/500 passing and also full xfstest didn't uncover any new regressions.

Folded, thanks.