Skip to contentSkip to conversation

btrfs: don't warn if discard range is not aligned to sector

PATCH In review David Sterba 5 participants 7 messages Jan 15, 2024 – Jan 17, 2024

Subsystems (2)
lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

PATCH 1/1 btrfs: don't warn if discard range is not aligned to sector

There's a warning in btrfs_issue_discard() when the range is not aligned to 512 bytes, originally added in 4d89d377bbb0 ("btrfs:
btrfs_issue_discard ensure offset/length are aligned to sector boundaries"). We can't do sub-sector writes anyway so the adjustment is the only thing that we can do and the warning is unnecessary.

---
 fs/btrfs/extent-tree.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +2 -1
index 6d680031211a..8e8cc1111277 100644
@@ -1260,7 +1260,8 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
 	u64 bytes_left, end;
 	u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT);
 
-	if (WARN_ON(start != aligned_start)) {
+	/* Adjust the range to be aligned to 512B sectors if necessary. */
+	if (start != aligned_start) {
 		len -= aligned_start - start;
 		len = round_down(len, 1 << SECTOR_SHIFT);
 		start = aligned_start;
Signature · 1 line
2.42.1
#1 lore raw
Quoted 18 lines · David Sterba

There's a warning in btrfs_issue_discard() when the range is not aligned to 512 bytes, originally added in 4d89d377bbb0 ("btrfs:
btrfs_issue_discard ensure offset/length are aligned to sector boundaries"). We can't do sub-sector writes anyway so the adjustment is the only thing that we can do and the warning is unnecessary.

---
  fs/btrfs/extent-tree.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +0 -0
index 6d680031211a..8e8cc1111277 100644

fs/btrfs/extent-tree.c static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, · line 1267

Quoted 9 lines
fs/btrfs/extent-tree.c +2 -1
@@ -1260,7 +1260,8 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
  	u64 bytes_left, end;
  	u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT);
 
-	if (WARN_ON(start != aligned_start)) {
+	/* Adjust the range to be aligned to 512B sectors if necessary. */
+	if (start != aligned_start) {
  		len -= aligned_start - start;
  		len = round_down(len, 1 << SECTOR_SHIFT);
  		start = aligned_start;

Maybe leave a
btrfs_warn(fs_info, "adjusting discard range to 512b boundary"); in there?

Or is info a more appropriate level?

#2 lore raw
Quoted 33 lines · Johannes Thumshirn
Quoted 18 lines · David Sterba

There's a warning in btrfs_issue_discard() when the range is not aligned to 512 bytes, originally added in 4d89d377bbb0 ("btrfs:
btrfs_issue_discard ensure offset/length are aligned to sector boundaries"). We can't do sub-sector writes anyway so the adjustment is the only thing that we can do and the warning is unnecessary.

---
  fs/btrfs/extent-tree.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +0 -0
index 6d680031211a..8e8cc1111277 100644

fs/btrfs/extent-tree.c static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, · line 1267

Quoted 9 lines
fs/btrfs/extent-tree.c +2 -1
@@ -1260,7 +1260,8 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
  	u64 bytes_left, end;
  	u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT);
 
-	if (WARN_ON(start != aligned_start)) {
+	/* Adjust the range to be aligned to 512B sectors if necessary. */
+	if (start != aligned_start) {
  		len -= aligned_start - start;
  		len = round_down(len, 1 << SECTOR_SHIFT);
  		start = aligned_start;

Maybe leave a
btrfs_warn(fs_info, "adjusting discard range to 512b boundary"); in there?

Quoted 1 line

Or is info a more appropriate level?

I don't know if we should warn here at all, the warning is there since 2015 and only now we see it triggered, I guess everybody sane using the discard interface uses proper values. Warning level means that it needs user attention that something is wrong and take some action or examine the system.

The discard range could be verified at the beginning of the ioctl, but printing it here is a bit late. I've checked xfs and it silently rounds down the start and offset the same way, I find this sufficient thing to do.

Reviewed-by
#3 lore raw
Quoted 7 lines · David Sterba

I don't know if we should warn here at all, the warning is there since 2015 and only now we see it triggered, I guess everybody sane using the discard interface uses proper values. Warning level means that it needs user attention that something is wrong and take some action or examine the system.

Quoted 5 lines

The discard range could be verified at the beginning of the ioctl, but printing it here is a bit late. I've checked xfs and it silently rounds down the start and offset the same way, I find this sufficient thing to do.

Yeah thenm lets do it this way,

#4 lore raw
In reply to David Sterba
Quoted 18 lines · David Sterba

There's a warning in btrfs_issue_discard() when the range is not aligned to 512 bytes, originally added in 4d89d377bbb0 ("btrfs:
btrfs_issue_discard ensure offset/length are aligned to sector boundaries"). We can't do sub-sector writes anyway so the adjustment is the only thing that we can do and the warning is unnecessary.

---
  fs/btrfs/extent-tree.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +0 -0
index 6d680031211a..8e8cc1111277 100644

fs/btrfs/extent-tree.c static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, · line 1267

Quoted 9 lines
fs/btrfs/extent-tree.c +2 -1
@@ -1260,7 +1260,8 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
  	u64 bytes_left, end;
  	u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT);
 
-	if (WARN_ON(start != aligned_start)) {
+	/* Adjust the range to be aligned to 512B sectors if necessary. */
+	if (start != aligned_start) {
  		len -= aligned_start - start;
  		len = round_down(len, 1 << SECTOR_SHIFT);
  		start = aligned_start;

Can we do one step further in mkfs and device add, by rounding down the device size to btrfs sector boundary?

Add maybe output a warning message at mount time when adding one device?

Thanks,
Qu

#5 lore raw
Quoted 24 lines · Qu Wenruo
Quoted 21 lines · David Sterba

There's a warning in btrfs_issue_discard() when the range is not aligned to 512 bytes, originally added in 4d89d377bbb0 ("btrfs:
btrfs_issue_discard ensure offset/length are aligned to sector boundaries"). We can't do sub-sector writes anyway so the adjustment is the only thing that we can do and the warning is unnecessary.

---
  fs/btrfs/extent-tree.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +0 -0
index 6d680031211a..8e8cc1111277 100644
@@ -1260,7 +1260,8 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
  	u64 bytes_left, end;
  	u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT);
 
Quoted 8 lines

fs/btrfs/extent-tree.c static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, · line 1267

Quoted 6 lines
fs/btrfs/extent-tree.c +2 -1
-	if (WARN_ON(start != aligned_start)) {
+	/* Adjust the range to be aligned to 512B sectors if necessary. */
+	if (start != aligned_start) {
  		len -= aligned_start - start;
  		len = round_down(len, 1 << SECTOR_SHIFT);
  		start = aligned_start;

Can we do one step further in mkfs and device add, by rounding down the device size to btrfs sector boundary?

This is not device size but the range that gets passed to discard. The start and size get converted to the 512B units anyway when calling blkdev_issue_discard(), so nothing else needs to be done.

Reviewed-by on v1 1/1
#6 lore raw
In reply to David Sterba

fs/btrfs/extent-tree.c static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, · line 1264

Quoted 4 lines
fs/btrfs/extent-tree.c +2 -1
-	if (WARN_ON(start != aligned_start)) {
+	/* Adjust the range to be aligned to 512B sectors if necessary. */
+	if (start != aligned_start) {

Thanks, Anand