btrfs: use list_first_entry() everywhere
authorDavid Sterba <dsterba@suse.com>
Tue, 22 Apr 2025 16:21:51 +0000 (18:21 +0200)
committerDavid Sterba <dsterba@suse.com>
Thu, 15 May 2025 12:30:47 +0000 (14:30 +0200)
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>
12 files changed:
fs/btrfs/async-thread.c
fs/btrfs/backref.c
fs/btrfs/block-group.c
fs/btrfs/disk-io.c
fs/btrfs/extent-io-tree.c
fs/btrfs/free-space-cache.c
fs/btrfs/inode.c
fs/btrfs/raid56.c
fs/btrfs/relocation.c
fs/btrfs/send.c
fs/btrfs/tree-log.c
fs/btrfs/volumes.c

index f3bffe08b290aae6b064e122e342e18bebbbd2f5..6c6f3bb58f4ead8b0576b2869f7d4459457ac548 100644 (file)
@@ -219,8 +219,7 @@ static void run_ordered_work(struct btrfs_workqueue *wq,
                spin_lock_irqsave(lock, flags);
                if (list_empty(list))
                        break;
-               work = list_entry(list->next, struct btrfs_work,
-                                 ordered_list);
+               work = list_first_entry(list, struct btrfs_work, ordered_list);
                if (!test_bit(WORK_DONE_BIT, &work->flags))
                        break;
                /*
index 6ee0259048cd3ec08606d805a71afbb7a3b31edd..ed497f5f8d1bbf18ae2ec2b5dc52375b1211c4d4 100644 (file)
@@ -3134,8 +3134,8 @@ void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
                return;
 
        while (!list_empty(&node->upper)) {
-               edge = list_entry(node->upper.next, struct btrfs_backref_edge,
-                                 list[LOWER]);
+               edge = list_first_entry(&node->upper, struct btrfs_backref_edge,
+                                       list[LOWER]);
                list_del(&edge->list[LOWER]);
                list_del(&edge->list[UPPER]);
                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
                 */
                ASSERT(list_is_singular(&cur->upper));
-               edge = list_entry(cur->upper.next, struct btrfs_backref_edge,
-                                 list[LOWER]);
+               edge = list_first_entry(&cur->upper, struct btrfs_backref_edge,
+                                       list[LOWER]);
                ASSERT(list_empty(&edge->list[UPPER]));
                exist = edge->node[UPPER];
                /*
index 91807d2943669947d5edcd0e6ca2011ac7bcf3d8..1a37066afccff22e2b64f6dc7fc901ca22f0bc79 100644 (file)
@@ -4425,8 +4425,8 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
 
        write_lock(&info->block_group_cache_lock);
        while (!list_empty(&info->caching_block_groups)) {
-               caching_ctl = list_entry(info->caching_block_groups.next,
-                                        struct btrfs_caching_control, list);
+               caching_ctl = list_first_entry(&info->caching_block_groups,
+                                              struct btrfs_caching_control, list);
                list_del(&caching_ctl->list);
                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);
 
        while (!list_empty(&info->space_info)) {
-               space_info = list_entry(info->space_info.next,
-                                       struct btrfs_space_info,
-                                       list);
+               space_info = list_first_entry(&info->space_info,
+                                             struct btrfs_space_info, list);
 
                /*
                 * Do not hide this behind enospc_debug, this is actually
index 1541fd19d5ce37766f735f9285567f54cd562cd3..1b2577de000ad73e0b7a7e6d76d9c878de96da9b 100644 (file)
@@ -1857,8 +1857,8 @@ void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
        int i;
 
        while (!list_empty(&fs_info->dead_roots)) {
-               gang[0] = list_entry(fs_info->dead_roots.next,
-                                    struct btrfs_root, root_list);
+               gang[0] = list_first_entry(&fs_info->dead_roots,
+                                          struct btrfs_root, root_list);
                list_del(&gang[0]->root_list);
 
                if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state))
index cae3980f4291c006648f974b21a51b07e607c76b..9f4f5829c55ca6fd77fddf57777ce2220d903848 100644 (file)
@@ -42,7 +42,7 @@ static inline void btrfs_extent_state_leak_debug_check(void)
        struct extent_state *state;
 
        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",
                       state->start, state->end, state->state,
                       extent_state_in_tree(state),
index 3f91f653c1fa4d76ca05ca50cba722e0422d4be4..18496ebdee9d3fad305b4dc1557b2b51dbd53368 100644 (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 */
        if (block_group && !list_empty(&block_group->cluster_list)) {
-               cluster = list_entry(block_group->cluster_list.next,
-                                    struct btrfs_free_cluster,
-                                    block_group_list);
+               cluster = list_first_entry(&block_group->cluster_list,
+                                          struct btrfs_free_cluster, block_group_list);
        }
 
        if (!node && cluster) {
@@ -2342,9 +2341,8 @@ again:
                struct rb_node *node;
                struct btrfs_free_space *entry;
 
-               cluster = list_entry(block_group->cluster_list.next,
-                                    struct btrfs_free_cluster,
-                                    block_group_list);
+               cluster = list_first_entry(&block_group->cluster_list,
+                                          struct btrfs_free_cluster, block_group_list);
                spin_lock(&cluster->lock);
                node = rb_first(&cluster->root);
                if (!node) {
index 1e5b9e9c2d2593c38122f484a7e4dc122b01e4e4..080052f771e90834dac98292aa85578866bd9825 100644 (file)
@@ -1568,8 +1568,8 @@ static noinline void submit_compressed_extents(struct btrfs_work *work, bool do_
                PAGE_SHIFT;
 
        while (!list_empty(&async_chunk->extents)) {
-               async_extent = list_entry(async_chunk->extents.next,
-                                         struct async_extent, list);
+               async_extent = list_first_entry(&async_chunk->extents,
+                                               struct async_extent, list);
                list_del(&async_extent->list);
                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 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);
 
index 09ae1018221e81ed4591329462a229193fac2817..99488a5407d681166612f11b289591108022b15b 100644 (file)
@@ -519,9 +519,8 @@ static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
 
        spin_lock(&table->cache_lock);
        while (!list_empty(&table->stripe_cache)) {
-               rbio = list_entry(table->stripe_cache.next,
-                                 struct btrfs_raid_bio,
-                                 stripe_cache);
+               rbio = list_first_entry(&table->stripe_cache,
+                                       struct btrfs_raid_bio, stripe_cache);
                __remove_rbio_from_cache(rbio);
        }
        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);
 
        while (!list_empty(&plug->rbio_list)) {
-               cur = list_entry(plug->rbio_list.next,
-                                struct btrfs_raid_bio, plug_list);
+               cur = list_first_entry(&plug->rbio_list,
+                                      struct btrfs_raid_bio, plug_list);
                list_del_init(&cur->plug_list);
 
                if (rbio_is_full(cur)) {
index 97c223aa90b650bd1b68b17a44ddad0e9e8dd6cc..0a6325ebf266ffd7ce0ecb64eba646dcf9294aa4 100644 (file)
@@ -196,8 +196,8 @@ static struct btrfs_backref_node *walk_up_backref(
        int idx = *index;
 
        while (!list_empty(&node->upper)) {
-               edge = list_entry(node->upper.next,
-                                 struct btrfs_backref_edge, list[LOWER]);
+               edge = list_first_entry(&node->upper, struct btrfs_backref_edge,
+                                       list[LOWER]);
                edges[idx++] = edge;
                node = edge->node[UPPER];
        }
@@ -223,8 +223,8 @@ static struct btrfs_backref_node *walk_down_backref(
                        idx--;
                        continue;
                }
-               edge = list_entry(edge->list[LOWER].next,
-                                 struct btrfs_backref_edge, list[LOWER]);
+               edge = list_first_entry(&edge->list[LOWER], struct btrfs_backref_edge,
+                                       list[LOWER]);
                edges[idx - 1] = edge;
                *index = idx;
                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_node *lower;
 
-                       edge = list_entry(cur->lower.next,
-                                       struct btrfs_backref_edge, list[UPPER]);
+                       edge = list_first_entry(&cur->lower, struct btrfs_backref_edge,
+                                               list[UPPER]);
                        list_del(&edge->list[UPPER]);
                        list_del(&edge->list[LOWER]);
                        lower = edge->node[LOWER];
@@ -1698,8 +1698,8 @@ again:
        rc->merge_reloc_tree = true;
 
        while (!list_empty(&rc->reloc_roots)) {
-               reloc_root = list_entry(rc->reloc_roots.next,
-                                       struct btrfs_root, root_list);
+               reloc_root = list_first_entry(&rc->reloc_roots,
+                                             struct btrfs_root, root_list);
                list_del_init(&reloc_root->root_list);
 
                root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
@@ -1814,8 +1814,7 @@ again:
 
        while (!list_empty(&reloc_roots)) {
                found = 1;
-               reloc_root = list_entry(reloc_roots.next,
-                                       struct btrfs_root, root_list);
+               reloc_root = list_first_entry(&reloc_roots, struct btrfs_root, root_list);
 
                root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
                                         false);
@@ -2110,8 +2109,8 @@ static noinline_for_stack u64 calcu_metadata_size(struct reloc_control *rc,
                        if (list_empty(&next->upper))
                                break;
 
-                       edge = list_entry(next->upper.next,
-                                       struct btrfs_backref_edge, list[LOWER]);
+                       edge = list_first_entry(&next->upper, struct btrfs_backref_edge,
+                                               list[LOWER]);
                        edges[index++] = edge;
                        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++) {
                while (!list_empty(&cache->pending[level])) {
-                       node = list_entry(cache->pending[level].next,
-                                         struct btrfs_backref_node, list);
+                       node = list_first_entry(&cache->pending[level],
+                                               struct btrfs_backref_node, list);
                        list_move_tail(&node->list, &list);
                        BUG_ON(!node->pending);
 
@@ -2396,8 +2395,8 @@ static void update_processed_blocks(struct reloc_control *rc,
                        if (list_empty(&next->upper))
                                break;
 
-                       edge = list_entry(next->upper.next,
-                                       struct btrfs_backref_edge, list[LOWER]);
+                       edge = list_first_entry(&next->upper, struct btrfs_backref_edge,
+                                               list[LOWER]);
                        edges[index++] = edge;
                        next = edge->node[UPPER];
                }
@@ -4183,8 +4182,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)
        rc->merge_reloc_tree = true;
 
        while (!list_empty(&reloc_roots)) {
-               reloc_root = list_entry(reloc_roots.next,
-                                       struct btrfs_root, root_list);
+               reloc_root = list_first_entry(&reloc_roots, struct btrfs_root, root_list);
                list_del(&reloc_root->root_list);
 
                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)) {
                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);
 
index 095bdd11398d66df4f6cc952e9fbc9a1e1e6549a..36ad01e345772136ef151be7c6e6a57487632512 100644 (file)
@@ -3098,7 +3098,7 @@ static void __free_recorded_refs(struct list_head *head)
        struct recorded_ref *cur;
 
        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);
        }
 }
@@ -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
                 */
-               cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
-                               list);
+               cur = list_first_entry(&sctx->deleted_refs, struct recorded_ref, list);
                ret = dup_ref(cur, &check_dirs);
                if (ret < 0)
                        goto out;
index 411dad8860a8f8428ace9bf5fc4481d7e4e0357f..97e933113b822fbe24af545efeddc7fe27a1c9a8 100644 (file)
@@ -860,9 +860,9 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
                                struct btrfs_ordered_sum *sums;
                                struct btrfs_root *csum_root;
 
-                               sums = list_entry(ordered_sums.next,
-                                               struct btrfs_ordered_sum,
-                                               list);
+                               sums = list_first_entry(&ordered_sums,
+                                                       struct btrfs_ordered_sum,
+                                                       list);
                                csum_root = btrfs_csum_root(fs_info,
                                                            sums->logical);
                                if (!ret)
@@ -4667,9 +4667,9 @@ static int log_extent_csums(struct btrfs_trans_handle *trans,
        ret = 0;
 
        while (!list_empty(&ordered_sums)) {
-               struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
-                                                  struct btrfs_ordered_sum,
-                                                  list);
+               struct btrfs_ordered_sum *sums = list_first_entry(&ordered_sums,
+                                                                 struct btrfs_ordered_sum,
+                                                                 list);
                if (!ret)
                        ret = log_csums(trans, inode, log_root, sums);
                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);
 process:
        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);
 
index 97a3831a3f9ab967773c4acea964840bf0b20ec7..34d605b2faea8a9c7753e091773497a3cf4489b6 100644 (file)
@@ -415,8 +415,8 @@ static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
 
        WARN_ON(fs_devices->opened);
        while (!list_empty(&fs_devices->devices)) {
-               device = list_entry(fs_devices->devices.next,
-                                   struct btrfs_device, dev_list);
+               device = list_first_entry(&fs_devices->devices,
+                                         struct btrfs_device, dev_list);
                list_del(&device->dev_list);
                btrfs_free_device(device);
        }
@@ -428,8 +428,8 @@ void __exit btrfs_cleanup_fs_uuids(void)
        struct btrfs_fs_devices *fs_devices;
 
        while (!list_empty(&fs_uuids)) {
-               fs_devices = list_entry(fs_uuids.next,
-                                       struct btrfs_fs_devices, fs_list);
+               fs_devices = list_first_entry(&fs_uuids, struct btrfs_fs_devices,
+                                             fs_list);
                list_del(&fs_devices->fs_list);
                free_fs_devices(fs_devices);
        }