FAILED: patch "[PATCH] btrfs: reject refs=0 subvolume without DEAD in" failed to apply to 6.18-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x 9a58e56e70f610e29b53821e9c706600e529cca3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026112428-spearmint-famished-4675@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 9a58e56e70f610e29b53821e9c706600e529cca3 Mon Sep 17 00:00:00 2001
From: Cheng Yongkang <[email protected]>
Date: Sat, 31 Oct 2026 23:25:39 -0700
Subject: [PATCH] btrfs: reject refs=0 subvolume without DEAD in
SUBVOL_SYNC_WAIT
btrfs_ioctl_subvol_sync() assumes that a cached root with zero references has BTRFS_ROOT_SUBVOL_DEAD set, and asserts that before waiting for the cleaner. Two states break the assumption:
- A corrupt ROOT_ITEM with zero references and a matching orphan item, on a writable filesystem.
- A failed subvolume deletion. btrfs_delete_subvolume() zeroes the root references, aborts the transaction, which forces the filesystem read-only, and then clears DEAD again. No on-disk metadata is corrupted in this case.
With CONFIG_BTRFS_ASSERT=y, both reach a BUG().
Change the ASSERT() into a proper rejection to avoid triggering a BUG().
fs/btrfs/ioctl.c +6 -2
@@ -5161,8 +5161,12 @@ static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *areturn -ENOENT ; wait_for_deletion = true; -ASSERT (root_flags &BTRFS_ROOT_SUBVOL_DEAD ,"root_flags=0x%llx" , - root_flags); +if (unlikely (!(root_flags &BTRFS_ROOT_SUBVOL_DEAD ))) { +btrfs_err (fs_info, +"subvol %llu has refs 0 without DEAD flag" , + args.subvolid); +return -EUCLEAN ; + } sched_ret =schedule_timeout_interruptible (HZ );/* Early wake up or error. */ if (sched_ret !=0 )
Sasha Levin
maintainerPATCH 1/3 btrfs: check type flags in alloc_ordered_extent()
From: Qu Wenruo <[email protected]>
[ Upstream commit 232770bcf3aa65ae83fd7389961fa651f09f7b3a ]
Unlike other flags used in btrfs, BTRFS_ORDERED_* macros are different as they cannot be directly used as flags.
They are defined as bit values, thus they should be utilized with bit operations, not directly with logical operations.
Unfortunately sometimes I forgot this and passed the incorrect flags to alloc_ordered_extent() and hit weird bugs.
Enhance the type checks in alloc_ordered_extent():
- Make sure there is one and only one bit set for exclusive type flags There are four exclusive type flags, REGULAR, NOCOW, PREALLOC and COMPRESSED.
So introduce a new macro, BTRFS_ORDERED_EXCLUSIVE_FLAGS, to cover above flags.
Add an ASSERT() to check one and only one of those exclusive flags can be set for alloc_ordered_extent().
- Re-order the type bit numbers to the end of the enum This is make it much harder to get a valid false negative.
E.g., with the old code BTRFS_ORDERED_REGULAR starts at zero, we can have the following flags passing the bit uniqueness check:
* BTRFS_ORDERED_NOCOW
Be treated as BTRFS_ORDERED_REGULAR (1 == 1UL << 0).
* BTRFS_ORDERED_PREALLOC
Be treated as BTRFS_ORDERED_NOCOW (2 == 1UL << 1).
* BTRFS_ORDERED_DIRECT
Be treated as BTRFS_ORDERED_PREALLOC (4 == 1UL << 2).
Now all those types start at 8, passing any of those bit numbers as flags directly will not pass the ASSERT().
- Add a static assert to avoid overflow
To make sure all BTRFS_ORDERED_* flags can fit into an unsigned long.
Stable-dep-of: 9a58e56e70f6 ("btrfs: reject refs=0 subvolume without DEAD in SUBVOL_SYNC_WAIT")
--- fs/btrfs/ordered-data.c | 13 ++++++++++ fs/btrfs/ordered-data.h | 55 +++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 24 deletions(-)
fs/btrfs/ordered-data.c +13 -0
@@ -156,6 +156,19 @@ static struct btrfs_ordered_extent *alloc_ordered_extent(const bool is_nocow = (flags & ((1U <<BTRFS_ORDERED_NOCOW ) | (1U <<BTRFS_ORDERED_PREALLOC ))); +/* Only one type flag can be set. */ +ASSERT (has_single_bit_set (flags &BTRFS_ORDERED_EXCLUSIVE_FLAGS )); + +/* DIRECT cannot be set with COMPRESSED nor ENCODED. */ +if (test_bit (BTRFS_ORDERED_DIRECT , &flags)) { +ASSERT (!test_bit (BTRFS_ORDERED_COMPRESSED , &flags)); +ASSERT (!test_bit (BTRFS_ORDERED_ENCODED , &flags)); + } + +/* ENCODED must be set with COMPRESSED. */ +if (test_bit (BTRFS_ORDERED_ENCODED , &flags)) +ASSERT (test_bit (BTRFS_ORDERED_COMPRESSED , &flags)); +/* * For a NOCOW write we can free the qgroup reserve right now. For a COW * one we transfer the reserved space from the inode's iotree into the
fs/btrfs/ordered-data.h +31 -24
76 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 btrfs: make more ASSERTs verbose, part 3
From: David Sterba <[email protected]>
[ Upstream commit 3fc0803f1c644e60b24cdb175109fcae83b2849a ]
We have support for optional string to be printed in ASSERT() (added in 19468a623a9109 ("btrfs: enhance ASSERT() to take optional format string")), it's not yet everywhere it could be so add a few more files.
Try to finish what was left after 1c094e6ccead7a ("btrfs: make a few more ASSERTs verbose").
Stable-dep-of: 9a58e56e70f6 ("btrfs: reject refs=0 subvolume without DEAD in SUBVOL_SYNC_WAIT")
--- fs/btrfs/backref.c | 4 ++-- fs/btrfs/extent-tree.c | 15 ++++++++++----- fs/btrfs/extent_map.c | 6 +++--- fs/btrfs/fiemap.c | 2 +- fs/btrfs/file-item.c | 4 +++- fs/btrfs/file.c | 7 +++++-- fs/btrfs/ioctl.c | 3 ++- fs/btrfs/ordered-data.c | 12 +++++++----- fs/btrfs/raid-stripe-tree.c | 4 +++- fs/btrfs/reflink.c | 5 +++-- 10 files changed, 39 insertions(+), 23 deletions(-)
fs/btrfs/backref.c +2 -2
@@ -2363,7 +2363,7 @@ int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, info = (struct btrfs_tree_block_info *)(ei +1 ); *out_level =btrfs_tree_block_level (eb, info); }else { -ASSERT (key->type ==BTRFS_METADATA_ITEM_KEY ); +ASSERT (key->type ==BTRFS_METADATA_ITEM_KEY ,"key->type=%hhu" , key->type); *out_level = (u8 )key->offset; }
@@ -3189,7 +3189,7 @@ static int handle_direct_tree_backref(struct btrfs_backref_cache *cache,struct btrfs_backref_node *upper;struct rb_node *rb_node; -ASSERT (ref_key->type ==BTRFS_SHARED_BLOCK_REF_KEY ); +ASSERT (ref_key->type ==BTRFS_SHARED_BLOCK_REF_KEY ,"ref_key->type=%hhu" , ref_key->type);/* Only reloc root uses backref pointing to itself */ if (ref_key->objectid == ref_key->offset) {
fs/btrfs/extent-tree.c +10 -5
@@ -372,7 +372,7 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,return type; } }else { -ASSERT (is_data ==BTRFS_REF_TYPE_ANY ); +ASSERT (is_data ==BTRFS_REF_TYPE_ANY ,"is_data=%d" , is_data);return type; } }
@@ -2444,8 +2444,11 @@ int btrfs_cross_ref_exist(struct btrfs_inode *inode, u64 offset,struct btrfs_key key;btrfs_item_key_to_cpu (leaf, &key, path->slots[0 ]); -ASSERT (key.objectid == bytenr); -ASSERT (key.type ==BTRFS_EXTENT_ITEM_KEY ); +ASSERT (key.objectid == bytenr, +"key.objectid=%llu bytenr=%llu" , + key.objectid, bytenr); +ASSERT (key.type ==BTRFS_EXTENT_ITEM_KEY ,"key.type=%u" , + key.type); } }
@@ -4407,10 +4410,12 @@ static noinline int find_free_extent(struct btrfs_root *root,/* Use dedicated sub-space_info for dedicated block group users. */ if (ffe_ctl->for_data_reloc) { space_info = space_info->sub_group[0 ]; -ASSERT (space_info->subgroup_id ==BTRFS_SUB_GROUP_DATA_RELOC ); +ASSERT (space_info->subgroup_id ==BTRFS_SUB_GROUP_DATA_RELOC , +"space_info->subgroup_id=%d" , space_info->subgroup_id); }else if (ffe_ctl->for_treelog) { space_info = space_info->sub_group[0 ]; -ASSERT (space_info->subgroup_id ==BTRFS_SUB_GROUP_TREELOG ); +ASSERT (space_info->subgroup_id ==BTRFS_SUB_GROUP_TREELOG , +"space_info->subgroup_id=%d" , space_info->subgroup_id); } }if (!space_info) {
fs/btrfs/extent_map.c +3 -3
@@ -705,7 +705,7 @@ int btrfs_add_extent_mapping(struct btrfs_inode *inode,* file offset. Here just do a sanity check. */ if (em->disk_bytenr ==EXTENT_MAP_INLINE ) -ASSERT (em->start ==0 ); +ASSERT (em->start ==0 ,"em->start=%llu" , em->start); ret =add_extent_mapping (inode, em, false);/* it is possible that someone inserted the extent into the tree
@@ -749,7 +749,7 @@ int btrfs_add_extent_mapping(struct btrfs_inode *inode, } } -ASSERT (ret ==0 || ret == -EEXIST ); +ASSERT (ret ==0 || ret == -EEXIST ,"ret=%d" , ret);return ret; }
@@ -931,7 +931,7 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end, ret =add_extent_mapping (inode, split, modified);/* Logic error, shouldn't happen. */ -ASSERT (ret ==0 ); +ASSERT (ret ==0 ,"ret=%d" , ret);if (WARN_ON (ret !=0 ) && modified)btrfs_set_inode_full_sync (inode); }
fs/btrfs/fiemap.c +1 -1
@@ -112,7 +112,7 @@ static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,u64 cache_end;/* Set at the end of extent_fiemap(). */ -ASSERT ((flags &FIEMAP_EXTENT_LAST ) ==0 ); +ASSERT ((flags &FIEMAP_EXTENT_LAST ) ==0 ,"flags=0x%u" , flags);if (!cache->cached) goto assign;
fs/btrfs/file-item.c +3 -1
@@ -319,7 +319,9 @@ static int search_csum_tree(struct btrfs_fs_info *fs_info, csum_start = key.offset; csum_len = (itemsize / csum_size) * sectorsize; -ASSERT (in_range (disk_bytenr, csum_start, csum_len)); +ASSERT (in_range (disk_bytenr, csum_start, csum_len), +"disk_bytenr=%llu csum_start=%llu csum_len=%llu" , + disk_bytenr, csum_start, csum_len); found: ret = (min(csum_start + csum_len, disk_bytenr + len) -
fs/btrfs/file.c +5 -2
@@ -1214,8 +1214,11 @@ static int copy_one_range(struct btrfs_inode *inode, struct iov_iter *iter,return ret; reserved_len = ret;/* Write range must be inside the reserved range. */ -ASSERT (reserved_start <= start); -ASSERT (start + write_bytes <= reserved_start + reserved_len); +ASSERT (reserved_start <= start,"reserved_start=%llu start=%llu" , + reserved_start, start); +ASSERT (start + write_bytes <= reserved_start + reserved_len, +"start=%llu write_bytes=%zu reserved_start=%llu reserved_len=%llu" , + start, write_bytes, reserved_start, reserved_len); again: ret = balance_dirty_pages_ratelimited_flags(inode->vfs_inode.i_mapping,
fs/btrfs/ioctl.c +2 -1
@@ -5224,7 +5224,8 @@ static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *areturn -ENOENT ; wait_for_deletion = true; -ASSERT (root_flags &BTRFS_ROOT_SUBVOL_DEAD ); +ASSERT (root_flags &BTRFS_ROOT_SUBVOL_DEAD ,"root_flags=0x%llx" , + root_flags); sched_ret =schedule_timeout_interruptible (HZ );/* Early wake up or error. */ if (sched_ret !=0 )
fs/btrfs/ordered-data.c +7 -5
@@ -157,7 +157,8 @@ static struct btrfs_ordered_extent *alloc_ordered_extent( ((1U << BTRFS_ORDERED_NOCOW) | (1U << BTRFS_ORDERED_PREALLOC)));/* Only one type flag can be set. */ -ASSERT (has_single_bit_set (flags &BTRFS_ORDERED_EXCLUSIVE_FLAGS )); +ASSERT (has_single_bit_set (flags &BTRFS_ORDERED_EXCLUSIVE_FLAGS ), +"flags=0x%lx" , flags);/* DIRECT cannot be set with COMPRESSED nor ENCODED. */ if (test_bit (BTRFS_ORDERED_DIRECT , &flags)) {
@@ -297,7 +298,7 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( {struct btrfs_ordered_extent *entry; -ASSERT ((flags & ~BTRFS_ORDERED_TYPE_FLAGS ) ==0 ); +ASSERT ((flags & ~BTRFS_ORDERED_TYPE_FLAGS ) ==0 ,"flags=0x%lx" , flags);/* * For regular writes, we just use the members in @file_extent.
@@ -1252,7 +1253,7 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent(trace_btrfs_ordered_extent_split (inode, ordered); -ASSERT (!(flags & (1U <<BTRFS_ORDERED_COMPRESSED ))); +ASSERT (!(flags & (1U <<BTRFS_ORDERED_COMPRESSED )),"flags=0x%lx" , flags);/* * The entire bio must be covered by the ordered extent, but we can't
@@ -1274,7 +1275,7 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent( }/* We cannot split partially completed ordered extents. */ if (ordered->bytes_left) { -ASSERT (!(flags & ~BTRFS_ORDERED_TYPE_FLAGS )); +ASSERT (!(flags & ~BTRFS_ORDERED_TYPE_FLAGS ),"flags=0x%lx" , flags);if (WARN_ON_ONCE (ordered->bytes_left != ordered->disk_num_bytes))return ERR_PTR (-EINVAL ); }
@@ -1323,7 +1324,8 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent( ordered->ram_bytes -= len;if (test_bit (BTRFS_ORDERED_IO_DONE , &ordered->flags)) { -ASSERT (ordered->bytes_left ==0 ); +ASSERT (ordered->bytes_left ==0 ,"ordered->bytes_left=%llu" , + ordered->bytes_left); new->bytes_left =0 ; }else { ordered->bytes_left -= len;
fs/btrfs/raid-stripe-tree.c +3 -1
@@ -239,7 +239,9 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 lebtrfs_partially_delete_raid_extent (trans, path, &key, key.offset - length, length); -ASSERT (key.offset - diff_end == length); +ASSERT (key.offset - diff_end == length, +"key.offset=%llu diff_end=%llu length=%llu" , + key.offset, diff_end, length);break ; }
fs/btrfs/reflink.c +3 -2
@@ -68,7 +68,8 @@ static int copy_inline_to_page(struct btrfs_inode *inode,struct address_space *mapping = inode->vfs_inode.i_mapping;int ret; -ASSERT (IS_ALIGNED (file_offset, block_size)); +ASSERT (IS_ALIGNED (file_offset, block_size),"file_offset=%llu block_size=%u" , + file_offset, block_size);/* * We have flushed and locked the ranges of the source and destination
@@ -469,7 +470,7 @@ static int btrfs_clone(struct inode *src, struct inode *inode, key.objectid != btrfs_ino(BTRFS_I(src)))break ; -ASSERT (key.type ==BTRFS_EXTENT_DATA_KEY ); +ASSERT (key.type ==BTRFS_EXTENT_DATA_KEY ,"key.type=%u" , key.type); extent =btrfs_item_ptr (leaf, slot, struct btrfs_file_extent_item);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 btrfs: reject refs=0 subvolume without DEAD in SUBVOL_SYNC_WAIT
In reply to Sasha LevinFrom: Cheng Yongkang <[email protected]>
[ Upstream commit 9a58e56e70f610e29b53821e9c706600e529cca3 ]
btrfs_ioctl_subvol_sync() assumes that a cached root with zero references has BTRFS_ROOT_SUBVOL_DEAD set, and asserts that before waiting for the cleaner. Two states break the assumption:
- A corrupt ROOT_ITEM with zero references and a matching orphan item, on a writable filesystem.
- A failed subvolume deletion. btrfs_delete_subvolume() zeroes the root references, aborts the transaction, which forces the filesystem read-only, and then clears DEAD again. No on-disk metadata is corrupted in this case.
With CONFIG_BTRFS_ASSERT=y, both reach a BUG().
Change the ASSERT() into a proper rejection to avoid triggering a BUG().
--- fs/btrfs/ioctl.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/btrfs/ioctl.c +6 -2
@@ -5224,8 +5224,12 @@ static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *areturn -ENOENT ; wait_for_deletion = true; -ASSERT (root_flags &BTRFS_ROOT_SUBVOL_DEAD ,"root_flags=0x%llx" , - root_flags); +if (unlikely (!(root_flags &BTRFS_ROOT_SUBVOL_DEAD ))) { +btrfs_err (fs_info, +"subvol %llu has refs 0 without DEAD flag" , + args.subvolid); +return -EUCLEAN ; + } sched_ret =schedule_timeout_interruptible (HZ );/* Early wake up or error. */ if (sched_ret !=0 )
Signature · 1 line
2.53.0