From 01e054b34cb894bd4ad1b9d68819db3e6fce4ed4 Mon Sep 17 00:00:00 2001 From: Mr NeilBrown Date: Thu, 7 Nov 2019 08:58:04 +1100 Subject: [PATCH] LU-6142 all: use list_first_entry() where appropriate. Lustre already uses list_first_entry() in many places, but it is not consistent. Let's make it consistent. The patch was generated with sed -i 's/list_entry(([^,]*)->next,/list_first_entry(1,/' `git grep -l 'list_entry(.*->next' lustre/ lnet/ libcfs/ ` followed by some manual cleanup of indents, and adding list_first_entry() to libcfs/include/libcfs/util/list.h Test-Parameters: trivial Signed-off-by: Mr NeilBrown Change-Id: Id646fba1faf40282e66ede07c88c8db5ffadc211 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/50826 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Arshad Hussain Reviewed-by: James Simmons Reviewed-by: Andreas Dilger Reviewed-by: Oleg Drokin --- libcfs/include/libcfs/util/list.h | 15 +++++++++++++-- libcfs/libcfs/libcfs_string.c | 4 ++-- libcfs/libcfs/util/nidstrings.c | 8 ++++---- libcfs/libcfs/util/string.c | 4 ++-- lnet/lnet/lib-move.c | 4 ++-- lnet/selftest/rpc.c | 6 +++--- lnet/selftest/timer.c | 2 +- lnet/utils/lnetconfig/cyaml.c | 2 +- lnet/utils/lst.c | 2 +- lustre/fld/fld_request.c | 5 +++-- lustre/ldlm/ldlm_inodebits.c | 4 ++-- lustre/ldlm/ldlm_lockd.c | 4 ++-- lustre/lfsck/lfsck_engine.c | 24 ++++++++++++------------ lustre/obdclass/scrub.c | 5 +++-- lustre/osd-zfs/osd_handler.c | 4 ++-- 15 files changed, 53 insertions(+), 40 deletions(-) diff --git a/libcfs/include/libcfs/util/list.h b/libcfs/include/libcfs/util/list.h index ef69efe..b7e808c 100644 --- a/libcfs/include/libcfs/util/list.h +++ b/libcfs/include/libcfs/util/list.h @@ -239,6 +239,17 @@ static inline void list_splice_init(struct list_head *list, ((type *)((char *)(ptr)-(char *)(&((type *)0)->member))) /** + * list_first_entry - get the first 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_first_entry(ptr, type, member) \ + list_entry((ptr)->next, type, member) + +/** * Iterate over a list * \param pos the iterator * \param head the list to iterate over @@ -453,7 +464,7 @@ static inline void hlist_add_after(struct hlist_node *n, * \param member the name of the list_struct within the struct. */ #define list_for_each_entry(pos, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member), \ + for (pos = list_first_entry((head), typeof(*pos), member), \ prefetch(pos->member.next); \ &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member), \ @@ -478,7 +489,7 @@ static inline void hlist_add_after(struct hlist_node *n, * \param member the name of the list_struct within the struct. */ #define list_for_each_entry_safe(pos, n, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member), \ + for (pos = list_first_entry((head), typeof(*pos), member), \ n = list_entry(pos->member.next, typeof(*pos), member); \ &pos->member != (head); \ pos = n, n = list_entry(n->member.next, typeof(*n), member)) diff --git a/libcfs/libcfs/libcfs_string.c b/libcfs/libcfs/libcfs_string.c index a3ff59c..be88452 100644 --- a/libcfs/libcfs/libcfs_string.c +++ b/libcfs/libcfs/libcfs_string.c @@ -552,8 +552,8 @@ cfs_expr_list_free_list(struct list_head *list) struct cfs_expr_list *el; while (!list_empty(list)) { - el = list_entry(list->next, - struct cfs_expr_list, el_link); + el = list_first_entry(list, + struct cfs_expr_list, el_link); list_del(&el->el_link); cfs_expr_list_free(el); } diff --git a/libcfs/libcfs/util/nidstrings.c b/libcfs/libcfs/util/nidstrings.c index 88f668a..dfcd8c4 100644 --- a/libcfs/libcfs/util/nidstrings.c +++ b/libcfs/libcfs/util/nidstrings.c @@ -460,7 +460,7 @@ cfs_ip_addr_range_gen(__u32 *ip_list, int count, struct list_head *ip_addr_expr) struct cfs_expr_list *octet_el; int idx = count - 1; - octet_el = list_entry(ip_addr_expr->next, typeof(*octet_el), el_link); + octet_el = list_first_entry(ip_addr_expr, typeof(*octet_el), el_link); (void) cfs_ip_addr_range_gen_recurse(ip_list, &idx, 3, 0, &octet_el->el_link, octet_el); @@ -561,7 +561,7 @@ libcfs_num_match(__u32 addr, struct list_head *numaddr) struct cfs_expr_list *el; assert(!list_empty(numaddr)); - el = list_entry(numaddr->next, struct cfs_expr_list, el_link); + el = list_first_entry(numaddr, struct cfs_expr_list, el_link); return cfs_expr_list_match(addr, el); } @@ -1320,7 +1320,7 @@ free_addrranges(struct list_head *list) while (!list_empty(list)) { struct addrrange *ar; - ar = list_entry(list->next, struct addrrange, ar_link); + ar = list_first_entry(list, struct addrrange, ar_link); cfs_expr_list_free_list(&ar->ar_numaddr_ranges); list_del(&ar->ar_link); @@ -1649,7 +1649,7 @@ int cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid, char max_addr_str[IPSTRING_LENGTH]; int rc; - first_nidrange = list_entry(nidlist->next, struct nidrange, nr_link); + first_nidrange = list_first_entry(nidlist, struct nidrange, nr_link); netnum = first_nidrange->nr_netnum; nf = first_nidrange->nr_netstrfns; diff --git a/libcfs/libcfs/util/string.c b/libcfs/libcfs/util/string.c index 700f002..f957db6 100644 --- a/libcfs/libcfs/util/string.c +++ b/libcfs/libcfs/util/string.c @@ -438,8 +438,8 @@ cfs_expr_list_free_list(struct list_head *list) struct cfs_expr_list *el; while (!list_empty(list)) { - el = list_entry(list->next, - struct cfs_expr_list, el_link); + el = list_first_entry(list, + struct cfs_expr_list, el_link); list_del(&el->el_link); cfs_expr_list_free(el); } diff --git a/lnet/lnet/lib-move.c b/lnet/lnet/lib-move.c index d513caa..de5f10a 100644 --- a/lnet/lnet/lib-move.c +++ b/lnet/lnet/lib-move.c @@ -3407,8 +3407,8 @@ lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt) while (!list_empty(resendq)) { struct lnet_peer_ni *lpni; - msg = list_entry(resendq->next, struct lnet_msg, - msg_list); + msg = list_first_entry(resendq, struct lnet_msg, + msg_list); list_del_init(&msg->msg_list); diff --git a/lnet/selftest/rpc.c b/lnet/selftest/rpc.c index 5a43ee7..4326c7b 100644 --- a/lnet/selftest/rpc.c +++ b/lnet/selftest/rpc.c @@ -229,9 +229,9 @@ srpc_service_fini(struct srpc_service *svc) break; while (!list_empty(q)) { - buf = list_entry(q->next, - struct srpc_buffer, - buf_list); + buf = list_first_entry(q, + struct srpc_buffer, + buf_list); list_del(&buf->buf_list); LIBCFS_FREE(buf, sizeof(*buf)); } diff --git a/lnet/selftest/timer.c b/lnet/selftest/timer.c index 8a35334..7f82941 100644 --- a/lnet/selftest/timer.c +++ b/lnet/selftest/timer.c @@ -124,7 +124,7 @@ stt_expire_list(struct list_head *slot, time64_t now) struct stt_timer *timer; while (!list_empty(slot)) { - timer = list_entry(slot->next, struct stt_timer, stt_list); + timer = list_first_entry(slot, struct stt_timer, stt_list); if (timer->stt_expires > now) break; diff --git a/lnet/utils/lnetconfig/cyaml.c b/lnet/utils/lnetconfig/cyaml.c index f11a4f8..3466ca7 100644 --- a/lnet/utils/lnetconfig/cyaml.c +++ b/lnet/utils/lnetconfig/cyaml.c @@ -242,7 +242,7 @@ static struct cYAML *cYAML_ll_pop(struct list_head *list, struct cYAML *obj = NULL; if (!list_empty(list)) { - pop = list_entry(list->next, struct cYAML_ll, list); + pop = list_first_entry(list, struct cYAML_ll, list); obj = pop->obj; if (print_info != NULL) diff --git a/lnet/utils/lst.c b/lnet/utils/lst.c index 3d6cebe..4b66984 100644 --- a/lnet/utils/lst.c +++ b/lnet/utils/lst.c @@ -407,7 +407,7 @@ lst_free_rpcent(struct list_head *head) struct lstcon_rpc_ent *ent; while (!list_empty(head)) { - ent = list_entry(head->next, struct lstcon_rpc_ent, rpe_link); + ent = list_first_entry(head, struct lstcon_rpc_ent, rpe_link); list_del(&ent->rpe_link); free(ent); diff --git a/lustre/fld/fld_request.c b/lustre/fld/fld_request.c index fecf82d..28dee3d 100644 --- a/lustre/fld/fld_request.c +++ b/lustre/fld/fld_request.c @@ -488,8 +488,9 @@ again: * the target. Else retreive the next target entry. */ if (target->ft_chain.next == &fld->lcf_targets) - target = list_entry(target->ft_chain.next->next, - struct lu_fld_target, ft_chain); + target = list_first_entry(target->ft_chain.next, + struct lu_fld_target, + ft_chain); else target = list_entry(target->ft_chain.next, struct lu_fld_target, diff --git a/lustre/ldlm/ldlm_inodebits.c b/lustre/ldlm/ldlm_inodebits.c index 41257a9..42640f1 100644 --- a/lustre/ldlm/ldlm_inodebits.c +++ b/lustre/ldlm/ldlm_inodebits.c @@ -106,8 +106,8 @@ restart: if (list_empty(head) || !(mask & (1 << i))) continue; - node = list_entry(head->next, struct ldlm_ibits_node, - lin_link[i]); + node = list_first_entry(head, struct ldlm_ibits_node, + lin_link[i]); pending = node->lock; LDLM_DEBUG(pending, "Reprocessing lock from queue %d", i); diff --git a/lustre/ldlm/ldlm_lockd.c b/lustre/ldlm/ldlm_lockd.c index 440049e..ddf11e5 100644 --- a/lustre/ldlm/ldlm_lockd.c +++ b/lustre/ldlm/ldlm_lockd.c @@ -204,8 +204,8 @@ static int expired_lock_main(void *arg) struct obd_export *export; struct ldlm_lock *lock; - lock = list_entry(expired->next, struct ldlm_lock, - l_pending_chain); + lock = list_first_entry(expired, struct ldlm_lock, + l_pending_chain); if ((void *)lock < LP_POISON + PAGE_SIZE && (void *)lock >= LP_POISON) { spin_unlock_bh(&waiting_locks_spinlock); diff --git a/lustre/lfsck/lfsck_engine.c b/lustre/lfsck/lfsck_engine.c index 49e1303..29b6b44 100644 --- a/lustre/lfsck/lfsck_engine.c +++ b/lustre/lfsck/lfsck_engine.c @@ -1197,15 +1197,15 @@ again: __u32 *gen; if (com->lc_type == LFSCK_TYPE_LAYOUT) { - ltd = list_entry(phase_head->next, - struct lfsck_tgt_desc, - ltd_layout_phase_list); + ltd = list_first_entry(phase_head, + struct lfsck_tgt_desc, + ltd_layout_phase_list); phase_list = <d->ltd_layout_phase_list; gen = <d->ltd_layout_gen; } else { - ltd = list_entry(phase_head->next, - struct lfsck_tgt_desc, - ltd_namespace_phase_list); + ltd = list_first_entry(phase_head, + struct lfsck_tgt_desc, + ltd_namespace_phase_list); phase_list = <d->ltd_namespace_phase_list; gen = <d->ltd_namespace_gen; } @@ -1428,17 +1428,17 @@ again: spin_lock(<ds->ltd_lock); while (!list_empty(phase_head)) { if (com->lc_type == LFSCK_TYPE_LAYOUT) { - ltd = list_entry(phase_head->next, - struct lfsck_tgt_desc, - ltd_layout_list); + ltd = list_first_entry(phase_head, + struct lfsck_tgt_desc, + ltd_layout_list); if (!list_empty(<d->ltd_layout_phase_list)) list_del_init( <d->ltd_layout_phase_list); list_del_init(<d->ltd_layout_list); } else { - ltd = list_entry(phase_head->next, - struct lfsck_tgt_desc, - ltd_namespace_list); + ltd = list_first_entry(phase_head, + struct lfsck_tgt_desc, + ltd_namespace_list); if (!list_empty(<d->ltd_namespace_phase_list)) list_del_init( <d->ltd_namespace_phase_list); diff --git a/lustre/obdclass/scrub.c b/lustre/obdclass/scrub.c index f1da24c..daf44e3 100644 --- a/lustre/obdclass/scrub.c +++ b/lustre/obdclass/scrub.c @@ -1086,8 +1086,9 @@ void lustre_index_backup(const struct lu_env *env, struct dt_device *dev, scan: spin_lock(lock); while (!list_empty(head)) { - libu = list_entry(head->next, - struct lustre_index_backup_unit, libu_link); + libu = list_first_entry(head, + struct lustre_index_backup_unit, + libu_link); list_del_init(&libu->libu_link); spin_unlock(lock); diff --git a/lustre/osd-zfs/osd_handler.c b/lustre/osd-zfs/osd_handler.c index 2730788..d2db32a 100644 --- a/lustre/osd-zfs/osd_handler.c +++ b/lustre/osd-zfs/osd_handler.c @@ -241,8 +241,8 @@ static void osd_unlinked_list_emptify(const struct lu_env *env, uint64_t oid; while (!list_empty(list)) { - obj = list_entry(list->next, - struct osd_object, oo_unlinked_linkage); + obj = list_first_entry(list, + struct osd_object, oo_unlinked_linkage); LASSERT(obj->oo_dn != NULL); oid = obj->oo_dn->dn_object; -- 1.8.3.1