Whamcloud - gitweb
LU-6142 utils: use list_first/list_entry() on list heads 30/50830/7
authorMr NeilBrown <neilb@suse.de>
Wed, 6 Nov 2019 22:49:03 +0000 (09:49 +1100)
committerOleg Drokin <green@whamcloud.com>
Tue, 20 Jun 2023 03:36:53 +0000 (03:36 +0000)
This patch changes
   list_entry(foo.next, ...)
to
   list_first_entry(&foo, ...)
and
   list_entry(foo.prev, ...)
to
   list_last_entry(&foo, ...)

in cases where 'foo' is a list head - not a list member.

Test-Parameters: trivial
Signed-off-by: Mr NeilBrown <neilb@suse.de>
Change-Id: I9daaaed044af596f6407801259cfb672150bfc34
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/50830
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
libcfs/include/libcfs/util/list.h
lnet/utils/lnetconfig/liblnetconfig_udsp.c
lnet/utils/lst.c
lustre/utils/lfs_project.c
lustre/utils/liblustreapi_layout.c
lustre/utils/llsom_sync.c
lustre/utils/lsnapshot.c
lustre/utils/mount_utils.c

index b7e808c..c054925 100644 (file)
@@ -250,6 +250,17 @@ static inline void list_splice_init(struct list_head *list,
        list_entry((ptr)->next, type, member)
 
 /**
+ * list_last_entry - get the last element from a list
+ * \param ptr   the list head to take the element from.
+ * \param type  the type of the struct this is embedded in.
+ * \param member the name of the list_head within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_last_entry(ptr, type, member) \
+       list_entry((ptr)->prev, type, member)
+
+/**
  * Iterate over a list
  * \param pos  the iterator
  * \param head the list to iterate over
index 045c056..0c388c1 100644 (file)
@@ -300,8 +300,8 @@ lnet_size_marshaled_nid_descr(struct lnet_ud_nid_descr *descr)
                return size;
 
        if (!list_empty(&descr->ud_net_id.udn_net_num_range)) {
-               expr = list_entry(descr->ud_net_id.udn_net_num_range.next,
-                                 struct cfs_expr_list, el_link);
+               expr = list_first_entry(&descr->ud_net_id.udn_net_num_range,
+                                       struct cfs_expr_list, el_link);
                range_count = lnet_get_list_len(&expr->el_exprs);
        }
 
@@ -374,8 +374,8 @@ copy_nid_range(struct lnet_ud_nid_descr *nid_descr, char *type,
 
        /* copy the net information */
        if (!list_empty(&nid_descr->ud_net_id.udn_net_num_range)) {
-               expr = list_entry(nid_descr->ud_net_id.udn_net_num_range.next,
-                                 struct cfs_expr_list, el_link);
+               expr = list_first_entry(&nid_descr->ud_net_id.udn_net_num_range,
+                                       struct cfs_expr_list, el_link);
                net_expr_count = lnet_get_list_len(&expr->el_exprs);
        } else {
                net_expr_count = 0;
index 436897f..d8e7162 100644 (file)
@@ -2461,38 +2461,38 @@ lst_print_stat(char *name, struct list_head *resultp,
        INIT_LIST_HEAD(&tmp[0]);
        INIT_LIST_HEAD(&tmp[1]);
 
-        memset(&lnet_stat_result, 0, sizeof(lnet_stat_result));
+       memset(&lnet_stat_result, 0, sizeof(lnet_stat_result));
 
        while (!list_empty(&resultp[idx])) {
                if (list_empty(&resultp[1 - idx])) {
-                        fprintf(stderr, "Group is changed, re-run stat\n");
-                        break;
-                }
+                       fprintf(stderr, "Group is changed, re-run stat\n");
+                       break;
+               }
 
-               new = list_entry(resultp[idx].next, struct lstcon_rpc_ent,
-                                     rpe_link);
-               old = list_entry(resultp[1 - idx].next, struct lstcon_rpc_ent,
-                                     rpe_link);
+               new = list_first_entry(&resultp[idx], struct lstcon_rpc_ent,
+                                      rpe_link);
+               old = list_first_entry(&resultp[1 - idx], struct lstcon_rpc_ent,
+                                      rpe_link);
 
-                /* first time get stats result, can't calculate diff */
-                if (new->rpe_peer.nid == LNET_NID_ANY)
-                        break;
+               /* first time get stats result, can't calculate diff */
+               if (new->rpe_peer.nid == LNET_NID_ANY)
+                       break;
 
-                if (new->rpe_peer.nid != old->rpe_peer.nid ||
-                    new->rpe_peer.pid != old->rpe_peer.pid) {
-                        /* Something wrong. i.e, somebody change the group */
-                        break;
-                }
+               if (new->rpe_peer.nid != old->rpe_peer.nid ||
+                   new->rpe_peer.pid != old->rpe_peer.pid) {
+                       /* Something wrong. i.e, somebody change the group */
+                       break;
+               }
 
                list_move_tail(&new->rpe_link, &tmp[idx]);
 
                list_move_tail(&old->rpe_link, &tmp[1 - idx]);
 
-                if (new->rpe_rpc_errno != 0 || new->rpe_fwk_errno != 0 ||
-                    old->rpe_rpc_errno != 0 || old->rpe_fwk_errno != 0) {
-                        errcount ++;
-                        continue;
-                }
+               if (new->rpe_rpc_errno != 0 || new->rpe_fwk_errno != 0 ||
+                   old->rpe_rpc_errno != 0 || old->rpe_fwk_errno != 0) {
+                       errcount++;
+                       continue;
+               }
 
                sfwk_new = (struct sfw_counters *)&new->rpe_payload[0];
                sfwk_old = (struct sfw_counters *)&old->rpe_payload[0];
@@ -2697,21 +2697,21 @@ jt_lst_stat(int argc, char **argv)
                        lst_reset_rpcent(&srp->srp_result[1 - idx]);
                }
 
-                idx = 1 - idx;
+               idx = 1 - idx;
 
-                if (count > 0)
-                        count--;
-        } while (count == -1 || count > 0);
+               if (count > 0)
+                       count--;
+       } while (count == -1 || count > 0);
 
 out:
        while (!list_empty(&head)) {
-               srp = list_entry(head.next, lst_stat_req_param_t, srp_link);
+               srp = list_first_entry(&head, lst_stat_req_param_t, srp_link);
 
                list_del(&srp->srp_link);
-                lst_stat_req_param_free(srp);
-        }
+               lst_stat_req_param_free(srp);
+       }
 
-        return rc;
+       return rc;
 }
 
 int
@@ -2828,13 +2828,13 @@ jt_lst_show_error(int argc, char **argv)
         }
 out:
        while (!list_empty(&head)) {
-               srp = list_entry(head.next, lst_stat_req_param_t, srp_link);
+               srp = list_first_entry(&head, lst_stat_req_param_t, srp_link);
 
                list_del(&srp->srp_link);
-                lst_stat_req_param_free(srp);
-        }
+               lst_stat_req_param_free(srp);
+       }
 
-        return rc;
+       return rc;
 }
 
 int
index ccf1c1a..83fe428 100644 (file)
@@ -371,7 +371,8 @@ static int lfs_project_iterate(const char *pathname,
                return ret;
 
        while (!list_empty(&head)) {
-               lpi = list_entry(head.next, struct lfs_project_item, lpi_list);
+               lpi = list_first_entry(&head, struct lfs_project_item,
+                                      lpi_list);
                list_del(&lpi->lpi_list);
                rc = lfs_project_handle_dir(&head, lpi->lpi_pathname,
                                             phc, func);
index 9bdeb7c..8b91e87 100644 (file)
@@ -1980,8 +1980,8 @@ int llapi_layout_comp_add(struct llapi_layout *layout)
        if (new == NULL)
                return -1;
 
-       last = list_entry(layout->llot_comp_list.prev, typeof(*last),
-                         llc_list);
+       last = list_last_entry(&layout->llot_comp_list, typeof(*last),
+                              llc_list);
 
        list_add_tail(&new->llc_list, &layout->llot_comp_list);
 
@@ -2065,7 +2065,7 @@ int llapi_layout_comp_del(struct llapi_layout *layout)
                return -1;
        }
        layout->llot_cur_comp =
-               list_entry(comp->llc_list.prev, typeof(*comp), llc_list);
+               list_last_entry(&comp->llc_list, typeof(*comp), llc_list);
        if (comp->llc_list.prev == &layout->llot_comp_list)
                layout->llot_cur_comp = NULL;
 
@@ -2144,8 +2144,10 @@ int llapi_layout_comp_use(struct llapi_layout *layout,
                return 1;
        }
 
-       head = list_entry(layout->llot_comp_list.next, typeof(*head), llc_list);
-       tail = list_entry(layout->llot_comp_list.prev, typeof(*tail), llc_list);
+       head = list_first_entry(&layout->llot_comp_list, typeof(*head),
+                               llc_list);
+       tail = list_last_entry(&layout->llot_comp_list, typeof(*tail),
+                              llc_list);
        switch (pos) {
        case LLAPI_LAYOUT_COMP_USE_FIRST:
                layout->llot_cur_comp = head;
@@ -2155,8 +2157,9 @@ int llapi_layout_comp_use(struct llapi_layout *layout,
                        errno = ENOENT;
                        return 1;
                }
-               layout->llot_cur_comp = list_entry(comp->llc_list.next,
-                                                  typeof(*comp), llc_list);
+               layout->llot_cur_comp = list_first_entry(&comp->llc_list,
+                                                        typeof(*comp),
+                                                        llc_list);
                break;
        case LLAPI_LAYOUT_COMP_USE_LAST:
                layout->llot_cur_comp = tail;
@@ -2166,8 +2169,9 @@ int llapi_layout_comp_use(struct llapi_layout *layout,
                        errno = ENOENT;
                        return 1;
                }
-               layout->llot_cur_comp = list_entry(comp->llc_list.prev,
-                                                  typeof(*comp), llc_list);
+               layout->llot_cur_comp = list_last_entry(&comp->llc_list,
+                                                       typeof(*comp),
+                                                       llc_list);
                break;
        default:
                errno = EINVAL;
@@ -2724,7 +2728,8 @@ int llapi_layout_get_last_init_comp(struct llapi_layout *layout)
        if (!layout->llot_is_composite)
                return 0;
 
-       head = list_entry(layout->llot_comp_list.next, typeof(*comp), llc_list);
+       head = list_first_entry(&layout->llot_comp_list, typeof(*comp),
+                               llc_list);
        if (head == NULL)
                return -EINVAL;
        if (head->llc_id == 0 && !(head->llc_flags & LCME_FL_INIT))
@@ -2732,11 +2737,13 @@ int llapi_layout_get_last_init_comp(struct llapi_layout *layout)
                return -EISDIR;
 
        /* traverse the components from the tail to find the last init one */
-       comp = list_entry(layout->llot_comp_list.prev, typeof(*comp), llc_list);
+       comp = list_last_entry(&layout->llot_comp_list, typeof(*comp),
+                              llc_list);
        while (comp != head) {
                if (comp->llc_flags & LCME_FL_INIT)
                        break;
-               comp = list_entry(comp->llc_list.prev, typeof(*comp), llc_list);
+               comp = list_last_entry(&comp->llc_list, typeof(*comp),
+                                      llc_list);
        }
 
        layout->llot_cur_comp = comp;
@@ -3220,14 +3227,14 @@ static int llapi_layout_sanity_cb(struct llapi_layout *layout,
        }
 
        if (comp->llc_list.prev != &layout->llot_comp_list)
-               prev = list_entry(comp->llc_list.prev, typeof(*prev),
-                                 llc_list);
+               prev = list_last_entry(&comp->llc_list, typeof(*prev),
+                                      llc_list);
        else
                prev = NULL;
 
        if (comp->llc_list.next != &layout->llot_comp_list)
-               next = list_entry(comp->llc_list.next, typeof(*next),
-                                 llc_list);
+               next = list_first_entry(&comp->llc_list, typeof(*next),
+                                       llc_list);
        else
                next = NULL;
 
index b4d03c9..e7ad285 100644 (file)
@@ -246,7 +246,7 @@ static int lsom_start_update(int count)
        while (i < count) {
                struct fid_rec *f;
 
-               f = list_entry(head.lh_list.next, struct fid_rec, fr_link);
+               f = list_first_entry(&head.lh_list, struct fid_rec, fr_link);
                rc = lsom_update_one(f);
                if (rc == 0) {
                        list_del_init(&f->fr_link);
@@ -284,7 +284,7 @@ repeated:
                 * pop the record, start to handle it immediately.
                 */
                now = time(NULL);
-               f = list_entry(head.lh_list.next, struct fid_rec, fr_link);
+               f = list_first_entry(&head.lh_list, struct fid_rec, fr_link);
                if (now > ((f->fr_time >> 30) + opt.o_min_age))
                        count = 1;
        }
index 3700eef..67d99c7 100644 (file)
@@ -624,8 +624,8 @@ static void snapshot_unload_conf(struct snapshot_instance *si)
        struct snapshot_target *st;
 
        while (!list_empty(&si->si_mdts_list)) {
-               st = list_entry(si->si_mdts_list.next,
-                               struct snapshot_target, st_list);
+               st = list_first_entry(&si->si_mdts_list,
+                                     struct snapshot_target, st_list);
                list_del(&st->st_list);
                free(st->st_host);
                free(st->st_fhost);
@@ -636,8 +636,8 @@ static void snapshot_unload_conf(struct snapshot_instance *si)
        }
 
        while (!list_empty(&si->si_osts_list)) {
-               st = list_entry(si->si_osts_list.next,
-                               struct snapshot_target, st_list);
+               st = list_first_entry(&si->si_osts_list,
+                                     struct snapshot_target, st_list);
                list_del(&st->st_list);
                free(st->st_host);
                free(st->st_fhost);
@@ -1062,8 +1062,8 @@ static int snapshot_get_mgsnode(struct snapshot_instance *si,
        int rc;
        int foreign = 0;
 
-       st = list_entry(si->si_osts_list.next, struct snapshot_target,
-                       st_list);
+       st = list_first_entry(&si->si_osts_list, struct snapshot_target,
+                             st_list);
 again:
        rc = 0;
        memset(buf, 0, sizeof(buf));
@@ -2210,8 +2210,8 @@ again:
                goto again;
        }
        while (!list_empty(&list_sub_items)) {
-               lsi = list_entry(list_sub_items.next,
-                                struct list_sub_item, lsi_list);
+               lsi = list_first_entry(&list_sub_items,
+                                      struct list_sub_item, lsi_list);
                list_del(&lsi->lsi_list);
                if (!rc) {
                        si->si_ssname = lsi->lsi_ssname;
index 663ff6b..6ba4e4a 100644 (file)
@@ -1089,8 +1089,8 @@ config:
        ret = 0;
 
        while (!list_empty(&cfg_list) && ret == 0) {
-               lce = list_entry(cfg_list.next, struct lustre_cfg_entry,
-                                lce_list);
+               lce = list_first_entry(&cfg_list, struct lustre_cfg_entry,
+                                      lce_list);
                list_del(&lce->lce_list);
                snprintf(filepnm, sizeof(filepnm), "%s/%s", cfg_dir,
                         lce->lce_name);
@@ -1126,8 +1126,8 @@ out:
                closedir(dir);
 
        while (!list_empty(&cfg_list)) {
-               lce = list_entry(cfg_list.next, struct lustre_cfg_entry,
-                                lce_list);
+               lce = list_first_entry(&cfg_list, struct lustre_cfg_entry,
+                                      lce_list);
                list_del(&lce->lce_list);
                lustre_cfg_entry_fini(lce);
        }