btrfs: use list_first_entry() everywhere

Using the helper makes it a bit more clear that we're accessing the
first list entry.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2025-04-22 18:21:51 +02:00
parent 9e0a739a9e
commit 2d44a15afd
12 changed files with 53 additions and 61 deletions

View File

@ -219,8 +219,7 @@ static void run_ordered_work(struct btrfs_workqueue *wq,
spin_lock_irqsave(lock, flags); spin_lock_irqsave(lock, flags);
if (list_empty(list)) if (list_empty(list))
break; break;
work = list_entry(list->next, struct btrfs_work, work = list_first_entry(list, struct btrfs_work, ordered_list);
ordered_list);
if (!test_bit(WORK_DONE_BIT, &work->flags)) if (!test_bit(WORK_DONE_BIT, &work->flags))
break; break;
/* /*

View File

@ -3134,8 +3134,8 @@ void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
return; return;
while (!list_empty(&node->upper)) { while (!list_empty(&node->upper)) {
edge = list_entry(node->upper.next, struct btrfs_backref_edge, edge = list_first_entry(&node->upper, struct btrfs_backref_edge,
list[LOWER]); list[LOWER]);
list_del(&edge->list[LOWER]); list_del(&edge->list[LOWER]);
list_del(&edge->list[UPPER]); list_del(&edge->list[UPPER]);
btrfs_backref_free_edge(cache, edge); btrfs_backref_free_edge(cache, edge);
@ -3473,8 +3473,8 @@ int btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans,
* type BTRFS_TREE_BLOCK_REF_KEY * type BTRFS_TREE_BLOCK_REF_KEY
*/ */
ASSERT(list_is_singular(&cur->upper)); ASSERT(list_is_singular(&cur->upper));
edge = list_entry(cur->upper.next, struct btrfs_backref_edge, edge = list_first_entry(&cur->upper, struct btrfs_backref_edge,
list[LOWER]); list[LOWER]);
ASSERT(list_empty(&edge->list[UPPER])); ASSERT(list_empty(&edge->list[UPPER]));
exist = edge->node[UPPER]; exist = edge->node[UPPER];
/* /*

View File

@ -4425,8 +4425,8 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
write_lock(&info->block_group_cache_lock); write_lock(&info->block_group_cache_lock);
while (!list_empty(&info->caching_block_groups)) { while (!list_empty(&info->caching_block_groups)) {
caching_ctl = list_entry(info->caching_block_groups.next, caching_ctl = list_first_entry(&info->caching_block_groups,
struct btrfs_caching_control, list); struct btrfs_caching_control, list);
list_del(&caching_ctl->list); list_del(&caching_ctl->list);
btrfs_put_caching_control(caching_ctl); btrfs_put_caching_control(caching_ctl);
} }
@ -4497,9 +4497,8 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
btrfs_release_global_block_rsv(info); btrfs_release_global_block_rsv(info);
while (!list_empty(&info->space_info)) { while (!list_empty(&info->space_info)) {
space_info = list_entry(info->space_info.next, space_info = list_first_entry(&info->space_info,
struct btrfs_space_info, struct btrfs_space_info, list);
list);
/* /*
* Do not hide this behind enospc_debug, this is actually * Do not hide this behind enospc_debug, this is actually

View File

@ -1857,8 +1857,8 @@ void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
int i; int i;
while (!list_empty(&fs_info->dead_roots)) { while (!list_empty(&fs_info->dead_roots)) {
gang[0] = list_entry(fs_info->dead_roots.next, gang[0] = list_first_entry(&fs_info->dead_roots,
struct btrfs_root, root_list); struct btrfs_root, root_list);
list_del(&gang[0]->root_list); list_del(&gang[0]->root_list);
if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state))

View File

@ -42,7 +42,7 @@ static inline void btrfs_extent_state_leak_debug_check(void)
struct extent_state *state; struct extent_state *state;
while (!list_empty(&states)) { while (!list_empty(&states)) {
state = list_entry(states.next, struct extent_state, leak_list); state = list_first_entry(&states, struct extent_state, leak_list);
pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n", pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
state->start, state->end, state->state, state->start, state->end, state->state,
extent_state_in_tree(state), extent_state_in_tree(state),

View File

@ -1080,9 +1080,8 @@ int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
/* Get the cluster for this block_group if it exists */ /* Get the cluster for this block_group if it exists */
if (block_group && !list_empty(&block_group->cluster_list)) { if (block_group && !list_empty(&block_group->cluster_list)) {
cluster = list_entry(block_group->cluster_list.next, cluster = list_first_entry(&block_group->cluster_list,
struct btrfs_free_cluster, struct btrfs_free_cluster, block_group_list);
block_group_list);
} }
if (!node && cluster) { if (!node && cluster) {
@ -2342,9 +2341,8 @@ static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
struct rb_node *node; struct rb_node *node;
struct btrfs_free_space *entry; struct btrfs_free_space *entry;
cluster = list_entry(block_group->cluster_list.next, cluster = list_first_entry(&block_group->cluster_list,
struct btrfs_free_cluster, struct btrfs_free_cluster, block_group_list);
block_group_list);
spin_lock(&cluster->lock); spin_lock(&cluster->lock);
node = rb_first(&cluster->root); node = rb_first(&cluster->root);
if (!node) { if (!node) {

View File

@ -1568,8 +1568,8 @@ static noinline void submit_compressed_extents(struct btrfs_work *work, bool do_
PAGE_SHIFT; PAGE_SHIFT;
while (!list_empty(&async_chunk->extents)) { while (!list_empty(&async_chunk->extents)) {
async_extent = list_entry(async_chunk->extents.next, async_extent = list_first_entry(&async_chunk->extents,
struct async_extent, list); struct async_extent, list);
list_del(&async_extent->list); list_del(&async_extent->list);
submit_one_async_extent(async_chunk, async_extent, &alloc_hint); submit_one_async_extent(async_chunk, async_extent, &alloc_hint);
} }
@ -8520,7 +8520,7 @@ static int start_delalloc_inodes(struct btrfs_root *root,
struct btrfs_inode *inode; struct btrfs_inode *inode;
struct inode *tmp_inode; struct inode *tmp_inode;
inode = list_entry(splice.next, struct btrfs_inode, delalloc_inodes); inode = list_first_entry(&splice, struct btrfs_inode, delalloc_inodes);
list_move_tail(&inode->delalloc_inodes, &root->delalloc_inodes); list_move_tail(&inode->delalloc_inodes, &root->delalloc_inodes);

View File

@ -519,9 +519,8 @@ static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
spin_lock(&table->cache_lock); spin_lock(&table->cache_lock);
while (!list_empty(&table->stripe_cache)) { while (!list_empty(&table->stripe_cache)) {
rbio = list_entry(table->stripe_cache.next, rbio = list_first_entry(&table->stripe_cache,
struct btrfs_raid_bio, struct btrfs_raid_bio, stripe_cache);
stripe_cache);
__remove_rbio_from_cache(rbio); __remove_rbio_from_cache(rbio);
} }
spin_unlock(&table->cache_lock); spin_unlock(&table->cache_lock);
@ -1702,8 +1701,8 @@ static void raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
list_sort(NULL, &plug->rbio_list, plug_cmp); list_sort(NULL, &plug->rbio_list, plug_cmp);
while (!list_empty(&plug->rbio_list)) { while (!list_empty(&plug->rbio_list)) {
cur = list_entry(plug->rbio_list.next, cur = list_first_entry(&plug->rbio_list,
struct btrfs_raid_bio, plug_list); struct btrfs_raid_bio, plug_list);
list_del_init(&cur->plug_list); list_del_init(&cur->plug_list);
if (rbio_is_full(cur)) { if (rbio_is_full(cur)) {

View File

@ -196,8 +196,8 @@ static struct btrfs_backref_node *walk_up_backref(
int idx = *index; int idx = *index;
while (!list_empty(&node->upper)) { while (!list_empty(&node->upper)) {
edge = list_entry(node->upper.next, edge = list_first_entry(&node->upper, struct btrfs_backref_edge,
struct btrfs_backref_edge, list[LOWER]); list[LOWER]);
edges[idx++] = edge; edges[idx++] = edge;
node = edge->node[UPPER]; node = edge->node[UPPER];
} }
@ -223,8 +223,8 @@ static struct btrfs_backref_node *walk_down_backref(
idx--; idx--;
continue; continue;
} }
edge = list_entry(edge->list[LOWER].next, edge = list_first_entry(&edge->list[LOWER], struct btrfs_backref_edge,
struct btrfs_backref_edge, list[LOWER]); list[LOWER]);
edges[idx - 1] = edge; edges[idx - 1] = edge;
*index = idx; *index = idx;
return edge->node[UPPER]; return edge->node[UPPER];
@ -348,8 +348,8 @@ static bool handle_useless_nodes(struct reloc_control *rc,
struct btrfs_backref_edge *edge; struct btrfs_backref_edge *edge;
struct btrfs_backref_node *lower; struct btrfs_backref_node *lower;
edge = list_entry(cur->lower.next, edge = list_first_entry(&cur->lower, struct btrfs_backref_edge,
struct btrfs_backref_edge, list[UPPER]); list[UPPER]);
list_del(&edge->list[UPPER]); list_del(&edge->list[UPPER]);
list_del(&edge->list[LOWER]); list_del(&edge->list[LOWER]);
lower = edge->node[LOWER]; lower = edge->node[LOWER];
@ -1698,8 +1698,8 @@ int prepare_to_merge(struct reloc_control *rc, int err)
rc->merge_reloc_tree = true; rc->merge_reloc_tree = true;
while (!list_empty(&rc->reloc_roots)) { while (!list_empty(&rc->reloc_roots)) {
reloc_root = list_entry(rc->reloc_roots.next, reloc_root = list_first_entry(&rc->reloc_roots,
struct btrfs_root, root_list); struct btrfs_root, root_list);
list_del_init(&reloc_root->root_list); list_del_init(&reloc_root->root_list);
root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
@ -1814,8 +1814,7 @@ void merge_reloc_roots(struct reloc_control *rc)
while (!list_empty(&reloc_roots)) { while (!list_empty(&reloc_roots)) {
found = 1; found = 1;
reloc_root = list_entry(reloc_roots.next, reloc_root = list_first_entry(&reloc_roots, struct btrfs_root, root_list);
struct btrfs_root, root_list);
root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
false); false);
@ -2110,8 +2109,8 @@ static noinline_for_stack u64 calcu_metadata_size(struct reloc_control *rc,
if (list_empty(&next->upper)) if (list_empty(&next->upper))
break; break;
edge = list_entry(next->upper.next, edge = list_first_entry(&next->upper, struct btrfs_backref_edge,
struct btrfs_backref_edge, list[LOWER]); list[LOWER]);
edges[index++] = edge; edges[index++] = edge;
next = edge->node[UPPER]; next = edge->node[UPPER];
} }
@ -2357,8 +2356,8 @@ static int finish_pending_nodes(struct btrfs_trans_handle *trans,
for (level = 0; level < BTRFS_MAX_LEVEL; level++) { for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
while (!list_empty(&cache->pending[level])) { while (!list_empty(&cache->pending[level])) {
node = list_entry(cache->pending[level].next, node = list_first_entry(&cache->pending[level],
struct btrfs_backref_node, list); struct btrfs_backref_node, list);
list_move_tail(&node->list, &list); list_move_tail(&node->list, &list);
BUG_ON(!node->pending); BUG_ON(!node->pending);
@ -2396,8 +2395,8 @@ static void update_processed_blocks(struct reloc_control *rc,
if (list_empty(&next->upper)) if (list_empty(&next->upper))
break; break;
edge = list_entry(next->upper.next, edge = list_first_entry(&next->upper, struct btrfs_backref_edge,
struct btrfs_backref_edge, list[LOWER]); list[LOWER]);
edges[index++] = edge; edges[index++] = edge;
next = edge->node[UPPER]; next = edge->node[UPPER];
} }
@ -4183,8 +4182,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)
rc->merge_reloc_tree = true; rc->merge_reloc_tree = true;
while (!list_empty(&reloc_roots)) { while (!list_empty(&reloc_roots)) {
reloc_root = list_entry(reloc_roots.next, reloc_root = list_first_entry(&reloc_roots, struct btrfs_root, root_list);
struct btrfs_root, root_list);
list_del(&reloc_root->root_list); list_del(&reloc_root->root_list);
if (btrfs_root_refs(&reloc_root->root_item) == 0) { if (btrfs_root_refs(&reloc_root->root_item) == 0) {
@ -4277,7 +4275,7 @@ int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered)
while (!list_empty(&list)) { while (!list_empty(&list)) {
struct btrfs_ordered_sum *sums = struct btrfs_ordered_sum *sums =
list_entry(list.next, struct btrfs_ordered_sum, list); list_first_entry(&list, struct btrfs_ordered_sum, list);
list_del_init(&sums->list); list_del_init(&sums->list);

View File

@ -3098,7 +3098,7 @@ static void __free_recorded_refs(struct list_head *head)
struct recorded_ref *cur; struct recorded_ref *cur;
while (!list_empty(head)) { while (!list_empty(head)) {
cur = list_entry(head->next, struct recorded_ref, list); cur = list_first_entry(head, struct recorded_ref, list);
recorded_ref_free(cur); recorded_ref_free(cur);
} }
} }
@ -4560,8 +4560,7 @@ static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
/* /*
* We have a moved dir. Add the old parent to check_dirs * We have a moved dir. Add the old parent to check_dirs
*/ */
cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, cur = list_first_entry(&sctx->deleted_refs, struct recorded_ref, list);
list);
ret = dup_ref(cur, &check_dirs); ret = dup_ref(cur, &check_dirs);
if (ret < 0) if (ret < 0)
goto out; goto out;

View File

@ -860,9 +860,9 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
struct btrfs_ordered_sum *sums; struct btrfs_ordered_sum *sums;
struct btrfs_root *csum_root; struct btrfs_root *csum_root;
sums = list_entry(ordered_sums.next, sums = list_first_entry(&ordered_sums,
struct btrfs_ordered_sum, struct btrfs_ordered_sum,
list); list);
csum_root = btrfs_csum_root(fs_info, csum_root = btrfs_csum_root(fs_info,
sums->logical); sums->logical);
if (!ret) if (!ret)
@ -4667,9 +4667,9 @@ static int log_extent_csums(struct btrfs_trans_handle *trans,
ret = 0; ret = 0;
while (!list_empty(&ordered_sums)) { while (!list_empty(&ordered_sums)) {
struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, struct btrfs_ordered_sum *sums = list_first_entry(&ordered_sums,
struct btrfs_ordered_sum, struct btrfs_ordered_sum,
list); list);
if (!ret) if (!ret)
ret = log_csums(trans, inode, log_root, sums); ret = log_csums(trans, inode, log_root, sums);
list_del(&sums->list); list_del(&sums->list);
@ -4947,7 +4947,7 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
list_sort(NULL, &extents, extent_cmp); list_sort(NULL, &extents, extent_cmp);
process: process:
while (!list_empty(&extents)) { while (!list_empty(&extents)) {
em = list_entry(extents.next, struct extent_map, list); em = list_first_entry(&extents, struct extent_map, list);
list_del_init(&em->list); list_del_init(&em->list);

View File

@ -415,8 +415,8 @@ static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
WARN_ON(fs_devices->opened); WARN_ON(fs_devices->opened);
while (!list_empty(&fs_devices->devices)) { while (!list_empty(&fs_devices->devices)) {
device = list_entry(fs_devices->devices.next, device = list_first_entry(&fs_devices->devices,
struct btrfs_device, dev_list); struct btrfs_device, dev_list);
list_del(&device->dev_list); list_del(&device->dev_list);
btrfs_free_device(device); btrfs_free_device(device);
} }
@ -428,8 +428,8 @@ void __exit btrfs_cleanup_fs_uuids(void)
struct btrfs_fs_devices *fs_devices; struct btrfs_fs_devices *fs_devices;
while (!list_empty(&fs_uuids)) { while (!list_empty(&fs_uuids)) {
fs_devices = list_entry(fs_uuids.next, fs_devices = list_first_entry(&fs_uuids, struct btrfs_fs_devices,
struct btrfs_fs_devices, fs_list); fs_list);
list_del(&fs_devices->fs_list); list_del(&fs_devices->fs_list);
free_fs_devices(fs_devices); free_fs_devices(fs_devices);
} }