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 _LINUX_LIST_H_
32 #define _LINUX_LIST_H_
35 * Since LIST_HEAD conflicts with the linux definition we must include any
36 * FreeBSD header which requires it here so it is resolved with the correct
37 * definition prior to the undef.
39 #include <linux/types.h>
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/queue.h>
44 #include <sys/cpuset.h>
47 #include <sys/mutex.h>
49 #include <sys/vnode.h>
51 #include <sys/socket.h>
56 #include <net/if_var.h>
57 #include <net/if_types.h>
58 #include <net/if_media.h>
61 #include <netinet/in.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/tcp_lro.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/nd6.h>
70 #include <vm/vm_object.h>
77 #define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) }
79 #define LINUX_LIST_HEAD(name) \
80 struct list_head name = LINUX_LIST_HEAD_INIT(name)
85 struct list_head *next;
86 struct list_head *prev;
91 INIT_LIST_HEAD(struct list_head *list)
94 list->next = list->prev = list;
98 list_empty(const struct list_head *head)
101 return (head->next == head);
105 list_empty_careful(const struct list_head *head)
107 struct list_head *next = head->next;
109 return ((next == head) && (next == head->prev));
113 __list_del(struct list_head *prev, struct list_head *next)
116 WRITE_ONCE(prev->next, next);
120 __list_del_entry(struct list_head *entry)
123 __list_del(entry->prev, entry->next);
127 list_del(struct list_head *entry)
130 __list_del(entry->prev, entry->next);
134 list_replace(struct list_head *old, struct list_head *new)
136 new->next = old->next;
137 new->next->prev = new;
138 new->prev = old->prev;
139 new->prev->next = new;
143 list_replace_init(struct list_head *old, struct list_head *new)
145 list_replace(old, new);
150 linux_list_add(struct list_head *new, struct list_head *prev,
151 struct list_head *next)
161 list_del_init(struct list_head *entry)
165 INIT_LIST_HEAD(entry);
168 #define list_entry(ptr, type, field) container_of(ptr, type, field)
170 #define list_first_entry(ptr, type, member) \
171 list_entry((ptr)->next, type, member)
173 #define list_last_entry(ptr, type, member) \
174 list_entry((ptr)->prev, type, member)
176 #define list_first_entry_or_null(ptr, type, member) \
177 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
179 #define list_next_entry(ptr, member) \
180 list_entry(((ptr)->member.next), typeof(*(ptr)), member)
182 #define list_safe_reset_next(ptr, n, member) \
183 (n) = list_next_entry(ptr, member)
185 #define list_prev_entry(ptr, member) \
186 list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
188 #define list_for_each(p, head) \
189 for (p = (head)->next; p != (head); p = (p)->next)
191 #define list_for_each_safe(p, n, head) \
192 for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
194 #define list_for_each_entry(p, h, field) \
195 for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
196 p = list_entry((p)->field.next, typeof(*p), field))
198 #define list_for_each_entry_safe(p, n, h, field) \
199 for (p = list_entry((h)->next, typeof(*p), field), \
200 n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
201 p = n, n = list_entry(n->field.next, typeof(*n), field))
203 #define list_for_each_entry_from(p, h, field) \
204 for ( ; &(p)->field != (h); \
205 p = list_entry((p)->field.next, typeof(*p), field))
207 #define list_for_each_entry_continue(p, h, field) \
208 for (p = list_next_entry((p), field); &(p)->field != (h); \
209 p = list_next_entry((p), field))
211 #define list_for_each_entry_safe_from(pos, n, head, member) \
212 for (n = list_entry((pos)->member.next, typeof(*pos), member); \
213 &(pos)->member != (head); \
214 pos = n, n = list_entry(n->member.next, typeof(*n), member))
216 #define list_for_each_entry_reverse(p, h, field) \
217 for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
218 p = list_entry((p)->field.prev, typeof(*p), field))
220 #define list_for_each_entry_safe_reverse(p, n, h, field) \
221 for (p = list_entry((h)->prev, typeof(*p), field), \
222 n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
223 p = n, n = list_entry(n->field.prev, typeof(*n), field))
225 #define list_for_each_entry_continue_reverse(p, h, field) \
226 for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
227 p = list_entry((p)->field.prev, typeof(*p), field))
229 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
232 list_add(struct list_head *new, struct list_head *head)
235 linux_list_add(new, head, head->next);
239 list_add_tail(struct list_head *new, struct list_head *head)
242 linux_list_add(new, head->prev, head);
246 list_move(struct list_head *list, struct list_head *head)
250 list_add(list, head);
254 list_move_tail(struct list_head *entry, struct list_head *head)
258 list_add_tail(entry, head);
262 linux_list_splice(const struct list_head *list, struct list_head *prev,
263 struct list_head *next)
265 struct list_head *first;
266 struct list_head *last;
268 if (list_empty(list))
279 list_splice(const struct list_head *list, struct list_head *head)
282 linux_list_splice(list, head, head->next);
286 list_splice_tail(struct list_head *list, struct list_head *head)
289 linux_list_splice(list, head->prev, head);
293 list_splice_init(struct list_head *list, struct list_head *head)
296 linux_list_splice(list, head, head->next);
297 INIT_LIST_HEAD(list);
301 list_splice_tail_init(struct list_head *list, struct list_head *head)
304 linux_list_splice(list, head->prev, head);
305 INIT_LIST_HEAD(list);
309 #define LIST_HEAD(name) struct list_head name = { &(name), &(name) }
313 struct hlist_node *first;
317 struct hlist_node *next, **pprev;
320 #define HLIST_HEAD_INIT { }
321 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
322 #define INIT_HLIST_HEAD(head) (head)->first = NULL
323 #define INIT_HLIST_NODE(node) \
325 (node)->next = NULL; \
326 (node)->pprev = NULL; \
330 hlist_unhashed(const struct hlist_node *h)
337 hlist_empty(const struct hlist_head *h)
340 return !READ_ONCE(h->first);
344 hlist_del(struct hlist_node *n)
347 WRITE_ONCE(*(n->pprev), n->next);
349 n->next->pprev = n->pprev;
353 hlist_del_init(struct hlist_node *n)
356 if (hlist_unhashed(n))
363 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
367 if (h->first != NULL)
368 h->first->pprev = &n->next;
369 WRITE_ONCE(h->first, n);
370 n->pprev = &h->first;
374 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
377 n->pprev = next->pprev;
379 next->pprev = &n->next;
380 WRITE_ONCE(*(n->pprev), n);
384 hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
387 n->next = prev->next;
388 WRITE_ONCE(prev->next, n);
389 n->pprev = &prev->next;
392 n->next->pprev = &n->next;
396 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
399 new->first = old->first;
401 new->first->pprev = &new->first;
405 static inline int list_is_singular(const struct list_head *head)
407 return !list_empty(head) && (head->next == head->prev);
410 static inline void __list_cut_position(struct list_head *list,
411 struct list_head *head, struct list_head *entry)
413 struct list_head *new_first = entry->next;
414 list->next = head->next;
415 list->next->prev = list;
418 head->next = new_first;
419 new_first->prev = head;
422 static inline void list_cut_position(struct list_head *list,
423 struct list_head *head, struct list_head *entry)
425 if (list_empty(head))
427 if (list_is_singular(head) &&
428 (head->next != entry && head != entry))
431 INIT_LIST_HEAD(list);
433 __list_cut_position(list, head, entry);
436 static inline int list_is_last(const struct list_head *list,
437 const struct list_head *head)
439 return list->next == head;
442 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
444 #define hlist_for_each(p, head) \
445 for (p = (head)->first; p; p = (p)->next)
447 #define hlist_for_each_safe(p, n, head) \
448 for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
450 #define hlist_entry_safe(ptr, type, member) \
451 ((ptr) ? hlist_entry(ptr, type, member) : NULL)
453 #define hlist_for_each_entry(pos, head, member) \
454 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
456 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
458 #define hlist_for_each_entry_continue(pos, member) \
459 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
461 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
463 #define hlist_for_each_entry_from(pos, member) \
465 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
467 #define hlist_for_each_entry_safe(pos, n, head, member) \
468 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
469 (pos) && ({ n = (pos)->member.next; 1; }); \
470 pos = hlist_entry_safe(n, typeof(*(pos)), member))
472 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
473 struct list_head *a, struct list_head *b));
475 #endif /* _LINUX_LIST_H_ */