2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _LINUXKPI_LINUX_LIST_H_
32 #define _LINUXKPI_LINUX_LIST_H_
36 * Since LIST_HEAD conflicts with the Linux definition we must include any
37 * FreeBSD header which requires it here so it is resolved with the correct
38 * definition prior to the undef.
40 #include <linux/types.h>
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/queue.h>
45 #include <sys/cpuset.h>
48 #include <sys/mutex.h>
50 #include <sys/vnode.h>
52 #include <sys/socket.h>
56 #include <net/ethernet.h>
58 #include <net/if_var.h>
59 #include <net/if_types.h>
60 #include <net/if_media.h>
63 #include <netinet/in.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/tcp_lro.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/nd6.h>
71 #include <net80211/ieee80211.h>
72 #include <net80211/ieee80211_var.h>
73 #include <net80211/ieee80211_node.h>
76 #include <vm/vm_object.h>
84 #define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) }
86 #define LINUX_LIST_HEAD(name) \
87 struct list_head name = LINUX_LIST_HEAD_INIT(name)
92 struct list_head *next;
93 struct list_head *prev;
98 INIT_LIST_HEAD(struct list_head *list)
101 list->next = list->prev = list;
105 list_empty(const struct list_head *head)
108 return (head->next == head);
112 list_empty_careful(const struct list_head *head)
114 struct list_head *next = head->next;
116 return ((next == head) && (next == head->prev));
120 __list_del(struct list_head *prev, struct list_head *next)
123 WRITE_ONCE(prev->next, next);
127 __list_del_entry(struct list_head *entry)
130 __list_del(entry->prev, entry->next);
134 list_del(struct list_head *entry)
137 __list_del(entry->prev, entry->next);
141 list_replace(struct list_head *old, struct list_head *new)
143 new->next = old->next;
144 new->next->prev = new;
145 new->prev = old->prev;
146 new->prev->next = new;
150 list_replace_init(struct list_head *old, struct list_head *new)
152 list_replace(old, new);
157 linux_list_add(struct list_head *new, struct list_head *prev,
158 struct list_head *next)
168 list_del_init(struct list_head *entry)
172 INIT_LIST_HEAD(entry);
175 #define list_entry(ptr, type, field) container_of(ptr, type, field)
177 #define list_first_entry(ptr, type, member) \
178 list_entry((ptr)->next, type, member)
180 #define list_last_entry(ptr, type, member) \
181 list_entry((ptr)->prev, type, member)
183 #define list_first_entry_or_null(ptr, type, member) \
184 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
186 #define list_next_entry(ptr, member) \
187 list_entry(((ptr)->member.next), typeof(*(ptr)), member)
189 #define list_safe_reset_next(ptr, n, member) \
190 (n) = list_next_entry(ptr, member)
192 #define list_prev_entry(ptr, member) \
193 list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
195 #define list_for_each(p, head) \
196 for (p = (head)->next; p != (head); p = (p)->next)
198 #define list_for_each_safe(p, n, head) \
199 for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
201 #define list_for_each_entry(p, h, field) \
202 for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
203 p = list_entry((p)->field.next, typeof(*p), field))
205 #define list_for_each_entry_safe(p, n, h, field) \
206 for (p = list_entry((h)->next, typeof(*p), field), \
207 n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
208 p = n, n = list_entry(n->field.next, typeof(*n), field))
210 #define list_for_each_entry_from(p, h, field) \
211 for ( ; &(p)->field != (h); \
212 p = list_entry((p)->field.next, typeof(*p), field))
214 #define list_for_each_entry_continue(p, h, field) \
215 for (p = list_next_entry((p), field); &(p)->field != (h); \
216 p = list_next_entry((p), field))
218 #define list_for_each_entry_safe_from(pos, n, head, member) \
219 for (n = list_entry((pos)->member.next, typeof(*pos), member); \
220 &(pos)->member != (head); \
221 pos = n, n = list_entry(n->member.next, typeof(*n), member))
223 #define list_for_each_entry_reverse(p, h, field) \
224 for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
225 p = list_entry((p)->field.prev, typeof(*p), field))
227 #define list_for_each_entry_safe_reverse(p, n, h, field) \
228 for (p = list_entry((h)->prev, typeof(*p), field), \
229 n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
230 p = n, n = list_entry(n->field.prev, typeof(*n), field))
232 #define list_for_each_entry_continue_reverse(p, h, field) \
233 for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
234 p = list_entry((p)->field.prev, typeof(*p), field))
236 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
238 #define list_for_each_entry_from_reverse(p, h, field) \
239 for (; &p->field != (h); \
240 p = list_prev_entry(p, field))
243 list_add(struct list_head *new, struct list_head *head)
246 linux_list_add(new, head, head->next);
250 list_add_tail(struct list_head *new, struct list_head *head)
253 linux_list_add(new, head->prev, head);
257 list_move(struct list_head *list, struct list_head *head)
261 list_add(list, head);
265 list_move_tail(struct list_head *entry, struct list_head *head)
269 list_add_tail(entry, head);
273 list_bulk_move_tail(struct list_head *head, struct list_head *first,
274 struct list_head *last)
276 first->prev->next = last->next;
277 last->next->prev = first->prev;
278 head->prev->next = first;
279 first->prev = head->prev;
285 linux_list_splice(const struct list_head *list, struct list_head *prev,
286 struct list_head *next)
288 struct list_head *first;
289 struct list_head *last;
291 if (list_empty(list))
302 list_splice(const struct list_head *list, struct list_head *head)
305 linux_list_splice(list, head, head->next);
309 list_splice_tail(struct list_head *list, struct list_head *head)
312 linux_list_splice(list, head->prev, head);
316 list_splice_init(struct list_head *list, struct list_head *head)
319 linux_list_splice(list, head, head->next);
320 INIT_LIST_HEAD(list);
324 list_splice_tail_init(struct list_head *list, struct list_head *head)
327 linux_list_splice(list, head->prev, head);
328 INIT_LIST_HEAD(list);
332 #define LIST_HEAD(name) struct list_head name = { &(name), &(name) }
335 struct hlist_node *first;
339 struct hlist_node *next, **pprev;
341 #define HLIST_HEAD_INIT { }
342 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
343 #define INIT_HLIST_HEAD(head) (head)->first = NULL
344 #define INIT_HLIST_NODE(node) \
346 (node)->next = NULL; \
347 (node)->pprev = NULL; \
351 hlist_unhashed(const struct hlist_node *h)
358 hlist_empty(const struct hlist_head *h)
361 return !READ_ONCE(h->first);
365 hlist_del(struct hlist_node *n)
368 WRITE_ONCE(*(n->pprev), n->next);
370 n->next->pprev = n->pprev;
374 hlist_del_init(struct hlist_node *n)
377 if (hlist_unhashed(n))
384 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
388 if (h->first != NULL)
389 h->first->pprev = &n->next;
390 WRITE_ONCE(h->first, n);
391 n->pprev = &h->first;
395 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
398 n->pprev = next->pprev;
400 next->pprev = &n->next;
401 WRITE_ONCE(*(n->pprev), n);
405 hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
408 n->next = prev->next;
409 WRITE_ONCE(prev->next, n);
410 n->pprev = &prev->next;
413 n->next->pprev = &n->next;
417 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
420 new->first = old->first;
422 new->first->pprev = &new->first;
426 static inline int list_is_singular(const struct list_head *head)
428 return !list_empty(head) && (head->next == head->prev);
431 static inline void __list_cut_position(struct list_head *list,
432 struct list_head *head, struct list_head *entry)
434 struct list_head *new_first = entry->next;
435 list->next = head->next;
436 list->next->prev = list;
439 head->next = new_first;
440 new_first->prev = head;
443 static inline void list_cut_position(struct list_head *list,
444 struct list_head *head, struct list_head *entry)
446 if (list_empty(head))
448 if (list_is_singular(head) &&
449 (head->next != entry && head != entry))
452 INIT_LIST_HEAD(list);
454 __list_cut_position(list, head, entry);
457 static inline int list_is_first(const struct list_head *list,
458 const struct list_head *head)
461 return (list->prev == head);
464 static inline int list_is_last(const struct list_head *list,
465 const struct list_head *head)
467 return list->next == head;
470 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
472 #define hlist_for_each(p, head) \
473 for (p = (head)->first; p; p = (p)->next)
475 #define hlist_for_each_safe(p, n, head) \
476 for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
478 #define hlist_entry_safe(ptr, type, member) \
479 ((ptr) ? hlist_entry(ptr, type, member) : NULL)
481 #define hlist_for_each_entry(pos, head, member) \
482 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
484 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
486 #define hlist_for_each_entry_continue(pos, member) \
487 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
489 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
491 #define hlist_for_each_entry_from(pos, member) \
493 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
495 #define hlist_for_each_entry_safe(pos, n, head, member) \
496 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
497 (pos) && ({ n = (pos)->member.next; 1; }); \
498 pos = hlist_entry_safe(n, typeof(*(pos)), member))
500 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
501 struct list_head *a, struct list_head *b));
503 #endif /* _LINUXKPI_LINUX_LIST_H_ */