Skip to contentSkip to conversation

btrfs: update superblock's device bytes_used when dropping chunk

PATCH In review Mark Harmstone 4 participants 4 messages May 29, 2025 – Jun 3, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: update superblock's device bytes_used when dropping chunk

Each superblock contains a copy of the device item for that device. In a transaction which drops a chunk but doesn't create any new ones, we were correctly updating the device item in the chunk tree but not copying over the new bytes_used value to the superblock.

This can be seen by doing the following:

 # cd
 # dd if=/dev/zero of=test bs=4096 count=2621440
 # mkfs.btrfs test
 # mount test /root/temp
 # cd /root/temp
 # for i in {00..10}; do dd if=/dev/zero of=$i bs=4096 count=32768; done
 # sync
 # rm *
 # sync
 # btrfs balance start -dusage=0 .
 # sync
 # cd
 # umount /root/temp
 # btrfs check test

(For btrfs-check to detect this, you will also need my patch at
https://github.com/kdave/btrfs-progs/pull/991.)

Change btrfs_remove_dev_extents() so that it adds the devices to the post_commit_list if they're not there already. This causes btrfs_commit_device_sizes() to be called, which updates the bytes_used value in the superblock.

---
 fs/btrfs/volumes.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/btrfs/volumes.c +6 -0
index e59aa0b5c4f3..ee886dc08d15 100644
@@ -3272,6 +3272,12 @@ int btrfs_remove_dev_extents(struct btrfs_trans_handle *trans,
 					device->bytes_used - dev_extent_len);
 			atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
 			btrfs_clear_space_info_full(fs_info);
+
+			if (list_empty(&device->post_commit_list)) {
+				list_add_tail(&device->post_commit_list,
+					      &trans->transaction->dev_update_list);
+			}
+
 			mutex_unlock(&fs_info->chunk_mutex);
 		}
 	}
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Mark Harmstone

Each superblock contains a copy of the device item for that device. In a transaction which drops a chunk but doesn't create any new ones, we were correctly updating the device item in the chunk tree but not copying over the new bytes_used value to the superblock.

Oh! This explains why the image in fsck/020/keyed_data_ref_with_shared_leaf.img has the mismatched result.

It's not an older kernel (almost 4 years after another related kernel fix), but really some bug in the kernel.

Quoted 23 lines

This can be seen by doing the following:

  # cd
  # dd if=/dev/zero of=test bs=4096 count=2621440
  # mkfs.btrfs test
  # mount test /root/temp
  # cd /root/temp
  # for i in {00..10}; do dd if=/dev/zero of=$i bs=4096 count=32768; done
  # sync
  # rm *
  # sync
  # btrfs balance start -dusage=0 .
  # sync
  # cd
  # umount /root/temp
  # btrfs check test

(For btrfs-check to detect this, you will also need my patch at
https://github.com/kdave/btrfs-progs/pull/991.)

Quoted 6 lines

Change btrfs_remove_dev_extents() so that it adds the devices to the post_commit_list if they're not there already. This causes btrfs_commit_device_sizes() to be called, which updates the bytes_used value in the superblock.

Looks good to me.

Thanks,
Qu> ---

Quoted 20 lines
  fs/btrfs/volumes.c | 6 ++++++
  1 file changed, 6 insertions(+)
fs/btrfs/volumes.c +6 -0
index e59aa0b5c4f3..ee886dc08d15 100644
@@ -3272,6 +3272,12 @@ int btrfs_remove_dev_extents(struct btrfs_trans_handle *trans,
  					device->bytes_used - dev_extent_len);
  			atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
  			btrfs_clear_space_info_full(fs_info);
+
+			if (list_empty(&device->post_commit_list)) {
+				list_add_tail(&device->post_commit_list,
+					      &trans->transaction->dev_update_list);
+			}
+
  			mutex_unlock(&fs_info->chunk_mutex);
  		}
  	}
Reviewed-by on v1 1/1
#2 lore raw
In reply to Mark Harmstone
Quoted 20 lines · Mark Harmstone

Each superblock contains a copy of the device item for that device. In a transaction which drops a chunk but doesn't create any new ones, we were correctly updating the device item in the chunk tree but not copying over the new bytes_used value to the superblock.

This can be seen by doing the following:

 # cd
 # dd if=/dev/zero of=test bs=4096 count=2621440
 # mkfs.btrfs test
 # mount test /root/temp
 # cd /root/temp
 # for i in {00..10}; do dd if=/dev/zero of=$i bs=4096 count=32768; done
 # sync
 # rm *
 # sync
 # btrfs balance start -dusage=0 .
 # sync
Quoted 8 lines
 # cd
 # umount /root/temp
 # btrfs check test

(For btrfs-check to detect this, you will also need my patch at
https://github.com/kdave/btrfs-progs/pull/991.)

Change btrfs_remove_dev_extents() so that it adds the devices to the

There's no btrfs_remove_dev_extents(), the code is in btrfs_remove_chunk()

Quoted 14 lines

post_commit_list if they're not there already. This causes btrfs_commit_device_sizes() to be called, which updates the bytes_used value in the superblock.

---
 fs/btrfs/volumes.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/btrfs/volumes.c +0 -0
index e59aa0b5c4f3..ee886dc08d15 100644

fs/btrfs/volumes.c int btrfs_remove_dev_extents(struct btrfs_trans_handle *trans, · line 3279

Quoted 8 lines
fs/btrfs/volumes.c +5 -0
@@ -3272,6 +3272,12 @@ int btrfs_remove_dev_extents(struct btrfs_trans_handle *trans,
 					device->bytes_used - dev_extent_len);
 			atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
 			btrfs_clear_space_info_full(fs_info);
+
+			if (list_empty(&device->post_commit_list)) {
+				list_add_tail(&device->post_commit_list,
+					      &trans->transaction->dev_update_list);
+			}

There are 2 cases, one is being fixed here. I wonder if we can add some sort of assertion or transaction commit time verification that we don't miss that in the future. The pattern seems to be after btrfs_device_set_bytes_used() and btrfs_clear_space_info_full(). We'd have to have something set a bit in btrfs_device_set_bytes_used() and check it in commit unless something unset it when adding the device to the dev_update_list.

Otherwise ok,

#3 lore raw
Quoted 26 lines · David Sterba
Quoted 1 line
Quoted 24 lines · Mark Harmstone

Each superblock contains a copy of the device item for that device. In a transaction which drops a chunk but doesn't create any new ones, we were correctly updating the device item in the chunk tree but not copying over the new bytes_used value to the superblock.

This can be seen by doing the following:

  # cd
  # dd if=/dev/zero of=test bs=4096 count=2621440
  # mkfs.btrfs test
  # mount test /root/temp
  # cd /root/temp
  # for i in {00..10}; do dd if=/dev/zero of=$i bs=4096 count=32768; done
  # sync
  # rm *
  # sync
  # btrfs balance start -dusage=0 .
  # sync
  # cd
  # umount /root/temp
  # btrfs check test
Quoted 7 lines
Quoted 4 lines

(For btrfs-check to detect this, you will also need my patch at
https://github.com/kdave/btrfs-progs/pull/991 .)

Change btrfs_remove_dev_extents() so that it adds the devices to the

There's no btrfs_remove_dev_extents(), the code is in btrfs_remove_chunk()

Sorry, that should have been remove_dev_extents(). I have it renamed in my tree so that it can be un-staticked.

Quoted 35 lines
Quoted 15 lines

post_commit_list if they're not there already. This causes btrfs_commit_device_sizes() to be called, which updates the bytes_used value in the superblock.

---
  fs/btrfs/volumes.c | 6 ++++++
  1 file changed, 6 insertions(+)
fs/btrfs/volumes.c +0 -0
index e59aa0b5c4f3..ee886dc08d15 100644

fs/btrfs/volumes.c int btrfs_remove_dev_extents(struct btrfs_trans_handle *trans, · line 3279

Quoted 8 lines
fs/btrfs/volumes.c +5 -0
@@ -3272,6 +3272,12 @@ int btrfs_remove_dev_extents(struct btrfs_trans_handle *trans,
  					device->bytes_used - dev_extent_len);
  			atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
  			btrfs_clear_space_info_full(fs_info);
+
+			if (list_empty(&device->post_commit_list)) {
+				list_add_tail(&device->post_commit_list,
+					      &trans->transaction->dev_update_list);
+			}

There are 2 cases, one is being fixed here. I wonder if we can add some sort of assertion or transaction commit time verification that we don't miss that in the future. The pattern seems to be after btrfs_device_set_bytes_used() and btrfs_clear_space_info_full(). We'd have to have something set a bit in btrfs_device_set_bytes_used() and check it in commit unless something unset it when adding the device to the dev_update_list.

Otherwise ok,