list.h revision 300496
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5294837Shselasky * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28289644Shselasky *
29289644Shselasky * $FreeBSD: head/sys/compat/linuxkpi/common/include/linux/list.h 300496 2016-05-23 12:03:40Z hselasky $
30219820Sjeff */
31219820Sjeff#ifndef _LINUX_LIST_H_
32219820Sjeff#define _LINUX_LIST_H_
33219820Sjeff
34219820Sjeff/*
35219820Sjeff * Since LIST_HEAD conflicts with the linux definition we must include any
36219820Sjeff * FreeBSD header which requires it here so it is resolved with the correct
37219820Sjeff * definition prior to the undef.
38219820Sjeff */
39219820Sjeff#include <linux/types.h>
40219820Sjeff
41219820Sjeff#include <sys/param.h>
42219820Sjeff#include <sys/kernel.h>
43219820Sjeff#include <sys/queue.h>
44222813Sattilio#include <sys/cpuset.h>
45275599Srodrigc#include <sys/jail.h>
46219820Sjeff#include <sys/lock.h>
47219820Sjeff#include <sys/mutex.h>
48219820Sjeff#include <sys/proc.h>
49219820Sjeff#include <sys/vnode.h>
50219820Sjeff#include <sys/conf.h>
51219820Sjeff#include <sys/socket.h>
52219820Sjeff#include <sys/mbuf.h>
53219820Sjeff
54219820Sjeff#include <net/bpf.h>
55219820Sjeff#include <net/if.h>
56257176Sglebius#include <net/if_var.h>
57219820Sjeff#include <net/if_types.h>
58219820Sjeff#include <net/if_media.h>
59275599Srodrigc#include <net/vnet.h>
60219820Sjeff
61219820Sjeff#include <netinet/in.h>
62219820Sjeff#include <netinet/in_pcb.h>
63276749Shselasky#include <netinet/in_var.h>
64297483Ssephe#include <netinet/tcp_lro.h>
65219820Sjeff
66219820Sjeff#include <netinet6/in6_var.h>
67219820Sjeff#include <netinet6/nd6.h>
68219820Sjeff
69219820Sjeff#include <vm/vm.h>
70219820Sjeff#include <vm/vm_object.h>
71290706Shselasky#include <vm/pmap.h>
72219820Sjeff
73219820Sjeff#define	prefetch(x)
74219820Sjeff
75219820Sjeffstruct list_head {
76219820Sjeff	struct list_head *next;
77219820Sjeff	struct list_head *prev;
78219820Sjeff};
79219820Sjeff
80219820Sjeffstatic inline void
81219820SjeffINIT_LIST_HEAD(struct list_head *list)
82219820Sjeff{
83219820Sjeff
84219820Sjeff	list->next = list->prev = list;
85219820Sjeff}
86219820Sjeff
87219820Sjeffstatic inline int
88219820Sjefflist_empty(const struct list_head *head)
89219820Sjeff{
90219820Sjeff
91219820Sjeff	return (head->next == head);
92219820Sjeff}
93219820Sjeff
94219820Sjeffstatic inline void
95219820Sjefflist_del(struct list_head *entry)
96219820Sjeff{
97219820Sjeff
98219820Sjeff	entry->next->prev = entry->prev;
99219820Sjeff	entry->prev->next = entry->next;
100219820Sjeff}
101219820Sjeff
102219820Sjeffstatic inline void
103289574Shselaskylist_replace(struct list_head *old, struct list_head *new)
104289574Shselasky{
105289574Shselasky	new->next = old->next;
106289574Shselasky	new->next->prev = new;
107289574Shselasky	new->prev = old->prev;
108289574Shselasky	new->prev->next = new;
109289574Shselasky}
110289574Shselasky
111289574Shselaskystatic inline void
112300496Shselaskylist_replace_init(struct list_head *old, struct list_head *new)
113300496Shselasky{
114300496Shselasky	list_replace(old, new);
115300496Shselasky	INIT_LIST_HEAD(old);
116300496Shselasky}
117300496Shselasky
118300496Shselaskystatic inline void
119294837Shselaskylinux_list_add(struct list_head *new, struct list_head *prev,
120219820Sjeff    struct list_head *next)
121219820Sjeff{
122219820Sjeff
123219820Sjeff	next->prev = new;
124219820Sjeff	new->next = next;
125219820Sjeff	new->prev = prev;
126219820Sjeff	prev->next = new;
127219820Sjeff}
128219820Sjeff
129219820Sjeffstatic inline void
130219820Sjefflist_del_init(struct list_head *entry)
131219820Sjeff{
132219820Sjeff
133219820Sjeff	list_del(entry);
134219820Sjeff	INIT_LIST_HEAD(entry);
135219820Sjeff}
136219820Sjeff
137219820Sjeff#define	list_entry(ptr, type, field)	container_of(ptr, type, field)
138219820Sjeff
139255932Salfred#define list_first_entry(ptr, type, member) \
140255932Salfred        list_entry((ptr)->next, type, member)
141255932Salfred
142300496Shselasky#define	list_last_entry(ptr, type, member)	\
143300496Shselasky	list_entry((ptr)->prev, type, member)
144300496Shselasky
145300496Shselasky#define	list_first_entry_or_null(ptr, type, member) \
146300496Shselasky	(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
147300496Shselasky
148289574Shselasky#define	list_next_entry(ptr, member)					\
149289574Shselasky	list_entry(((ptr)->member.next), typeof(*(ptr)), member)
150289574Shselasky
151300496Shselasky#define	list_prev_entry(ptr, member)					\
152300496Shselasky	list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
153300496Shselasky
154219820Sjeff#define	list_for_each(p, head)						\
155294837Shselasky	for (p = (head)->next; p != (head); p = (p)->next)
156219820Sjeff
157219820Sjeff#define	list_for_each_safe(p, n, head)					\
158294837Shselasky	for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
159219820Sjeff
160219820Sjeff#define list_for_each_entry(p, h, field)				\
161294837Shselasky	for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
162294837Shselasky	    p = list_entry((p)->field.next, typeof(*p), field))
163219820Sjeff
164219820Sjeff#define list_for_each_entry_safe(p, n, h, field)			\
165219820Sjeff	for (p = list_entry((h)->next, typeof(*p), field), 		\
166294837Shselasky	    n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
167219820Sjeff	    p = n, n = list_entry(n->field.next, typeof(*n), field))
168219820Sjeff
169294837Shselasky#define	list_for_each_entry_from(p, h, field) \
170294837Shselasky	for ( ; &(p)->field != (h); \
171294837Shselasky	    p = list_entry((p)->field.next, typeof(*p), field))
172294837Shselasky
173289574Shselasky#define	list_for_each_entry_continue(p, h, field)			\
174294837Shselasky	for (p = list_next_entry((p), field); &(p)->field != (h);	\
175289574Shselasky	    p = list_next_entry((p), field))
176289574Shselasky
177289574Shselasky#define	list_for_each_entry_safe_from(pos, n, head, member) 			\
178294837Shselasky	for (n = list_entry((pos)->member.next, typeof(*pos), member);		\
179294837Shselasky	     &(pos)->member != (head);						\
180289574Shselasky	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
181289574Shselasky
182219820Sjeff#define	list_for_each_entry_reverse(p, h, field)			\
183294837Shselasky	for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
184294837Shselasky	    p = list_entry((p)->field.prev, typeof(*p), field))
185219820Sjeff
186294837Shselasky#define	list_for_each_entry_continue_reverse(p, h, field) \
187294837Shselasky	for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
188294837Shselasky	    p = list_entry((p)->field.prev, typeof(*p), field))
189219820Sjeff
190294837Shselasky#define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
191294837Shselasky
192219820Sjeffstatic inline void
193219820Sjefflist_add(struct list_head *new, struct list_head *head)
194219820Sjeff{
195219820Sjeff
196294837Shselasky	linux_list_add(new, head, head->next);
197219820Sjeff}
198219820Sjeff
199219820Sjeffstatic inline void
200219820Sjefflist_add_tail(struct list_head *new, struct list_head *head)
201219820Sjeff{
202219820Sjeff
203294837Shselasky	linux_list_add(new, head->prev, head);
204219820Sjeff}
205219820Sjeff
206219820Sjeffstatic inline void
207219820Sjefflist_move(struct list_head *list, struct list_head *head)
208219820Sjeff{
209219820Sjeff
210219820Sjeff	list_del(list);
211219820Sjeff	list_add(list, head);
212219820Sjeff}
213219820Sjeff
214219820Sjeffstatic inline void
215219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head)
216219820Sjeff{
217219820Sjeff
218219820Sjeff	list_del(entry);
219219820Sjeff	list_add_tail(entry, head);
220219820Sjeff}
221219820Sjeff
222219820Sjeffstatic inline void
223294837Shselaskylinux_list_splice(const struct list_head *list, struct list_head *prev,
224219820Sjeff    struct list_head *next)
225219820Sjeff{
226219820Sjeff	struct list_head *first;
227219820Sjeff	struct list_head *last;
228219820Sjeff
229219820Sjeff	if (list_empty(list))
230219820Sjeff		return;
231219820Sjeff	first = list->next;
232219820Sjeff	last = list->prev;
233219820Sjeff	first->prev = prev;
234219820Sjeff	prev->next = first;
235219820Sjeff	last->next = next;
236219820Sjeff	next->prev = last;
237219820Sjeff}
238219820Sjeff
239219820Sjeffstatic inline void
240219820Sjefflist_splice(const struct list_head *list, struct list_head *head)
241219820Sjeff{
242219820Sjeff
243294837Shselasky	linux_list_splice(list, head, head->next);
244219820Sjeff}
245219820Sjeff
246219820Sjeffstatic inline void
247219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head)
248219820Sjeff{
249219820Sjeff
250294837Shselasky	linux_list_splice(list, head->prev, head);
251219820Sjeff}
252219820Sjeff
253219820Sjeffstatic inline void
254219820Sjefflist_splice_init(struct list_head *list, struct list_head *head)
255219820Sjeff{
256219820Sjeff
257294837Shselasky	linux_list_splice(list, head, head->next);
258219820Sjeff	INIT_LIST_HEAD(list);
259219820Sjeff}
260219820Sjeff
261219820Sjeffstatic inline void
262219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head)
263219820Sjeff{
264219820Sjeff
265294837Shselasky	linux_list_splice(list, head->prev, head);
266219820Sjeff	INIT_LIST_HEAD(list);
267219820Sjeff}
268219820Sjeff
269219820Sjeff#undef LIST_HEAD
270219820Sjeff#define LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
271219820Sjeff
272219820Sjeff
273219820Sjeffstruct hlist_head {
274219820Sjeff	struct hlist_node *first;
275219820Sjeff};
276219820Sjeff
277219820Sjeffstruct hlist_node {
278219820Sjeff	struct hlist_node *next, **pprev;
279219820Sjeff};
280219820Sjeff
281219820Sjeff#define	HLIST_HEAD_INIT { }
282219820Sjeff#define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
283219820Sjeff#define	INIT_HLIST_HEAD(head) (head)->first = NULL
284219820Sjeff#define	INIT_HLIST_NODE(node)						\
285219820Sjeffdo {									\
286219820Sjeff	(node)->next = NULL;						\
287219820Sjeff	(node)->pprev = NULL;						\
288219820Sjeff} while (0)
289219820Sjeff
290219820Sjeffstatic inline int
291219820Sjeffhlist_unhashed(const struct hlist_node *h)
292219820Sjeff{
293219820Sjeff
294219820Sjeff	return !h->pprev;
295219820Sjeff}
296219820Sjeff
297219820Sjeffstatic inline int
298219820Sjeffhlist_empty(const struct hlist_head *h)
299219820Sjeff{
300219820Sjeff
301219820Sjeff	return !h->first;
302219820Sjeff}
303219820Sjeff
304219820Sjeffstatic inline void
305219820Sjeffhlist_del(struct hlist_node *n)
306219820Sjeff{
307219820Sjeff
308219820Sjeff        if (n->next)
309219820Sjeff                n->next->pprev = n->pprev;
310219820Sjeff        *n->pprev = n->next;
311219820Sjeff}
312219820Sjeff
313219820Sjeffstatic inline void
314219820Sjeffhlist_del_init(struct hlist_node *n)
315219820Sjeff{
316219820Sjeff
317219820Sjeff	if (hlist_unhashed(n))
318219820Sjeff		return;
319219820Sjeff	hlist_del(n);
320219820Sjeff	INIT_HLIST_NODE(n);
321219820Sjeff}
322219820Sjeff
323219820Sjeffstatic inline void
324219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h)
325219820Sjeff{
326219820Sjeff
327219820Sjeff	n->next = h->first;
328219820Sjeff	if (h->first)
329219820Sjeff		h->first->pprev = &n->next;
330219820Sjeff	h->first = n;
331219820Sjeff	n->pprev = &h->first;
332219820Sjeff}
333219820Sjeff
334219820Sjeffstatic inline void
335219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next)
336219820Sjeff{
337219820Sjeff
338219820Sjeff	n->pprev = next->pprev;
339219820Sjeff	n->next = next;
340219820Sjeff	next->pprev = &n->next;
341219820Sjeff	*(n->pprev) = n;
342219820Sjeff}
343219820Sjeff
344219820Sjeffstatic inline void
345219820Sjeffhlist_add_after(struct hlist_node *n, struct hlist_node *next)
346219820Sjeff{
347219820Sjeff
348219820Sjeff	next->next = n->next;
349219820Sjeff	n->next = next;
350219820Sjeff	next->pprev = &n->next;
351219820Sjeff	if (next->next)
352219820Sjeff		next->next->pprev = &next->next;
353219820Sjeff}
354219820Sjeff
355219820Sjeffstatic inline void
356219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new)
357219820Sjeff{
358219820Sjeff
359219820Sjeff	new->first = old->first;
360219820Sjeff	if (new->first)
361219820Sjeff		new->first->pprev = &new->first;
362219820Sjeff	old->first = NULL;
363219820Sjeff}
364270710Shselasky
365270710Shselasky/**
366270710Shselasky * list_is_singular - tests whether a list has just one entry.
367270710Shselasky * @head: the list to test.
368270710Shselasky */
369270710Shselaskystatic inline int list_is_singular(const struct list_head *head)
370270710Shselasky{
371270710Shselasky	return !list_empty(head) && (head->next == head->prev);
372270710Shselasky}
373270710Shselasky
374270710Shselaskystatic inline void __list_cut_position(struct list_head *list,
375270710Shselasky		struct list_head *head, struct list_head *entry)
376270710Shselasky{
377270710Shselasky	struct list_head *new_first = entry->next;
378270710Shselasky	list->next = head->next;
379270710Shselasky	list->next->prev = list;
380270710Shselasky	list->prev = entry;
381270710Shselasky	entry->next = list;
382270710Shselasky	head->next = new_first;
383270710Shselasky	new_first->prev = head;
384270710Shselasky}
385270710Shselasky
386270710Shselasky/**
387270710Shselasky * list_cut_position - cut a list into two
388270710Shselasky * @list: a new list to add all removed entries
389270710Shselasky * @head: a list with entries
390270710Shselasky * @entry: an entry within head, could be the head itself
391270710Shselasky *	and if so we won't cut the list
392270710Shselasky *
393270710Shselasky * This helper moves the initial part of @head, up to and
394270710Shselasky * including @entry, from @head to @list. You should
395270710Shselasky * pass on @entry an element you know is on @head. @list
396270710Shselasky * should be an empty list or a list you do not care about
397270710Shselasky * losing its data.
398270710Shselasky *
399270710Shselasky */
400270710Shselaskystatic inline void list_cut_position(struct list_head *list,
401270710Shselasky		struct list_head *head, struct list_head *entry)
402270710Shselasky{
403270710Shselasky	if (list_empty(head))
404270710Shselasky		return;
405270710Shselasky	if (list_is_singular(head) &&
406270710Shselasky		(head->next != entry && head != entry))
407270710Shselasky		return;
408270710Shselasky	if (entry == head)
409270710Shselasky		INIT_LIST_HEAD(list);
410270710Shselasky	else
411270710Shselasky		__list_cut_position(list, head, entry);
412270710Shselasky}
413270710Shselasky
414270710Shselasky/**
415270710Shselasky *  list_is_last - tests whether @list is the last entry in list @head
416270710Shselasky *   @list: the entry to test
417270710Shselasky *    @head: the head of the list
418270710Shselasky */
419270710Shselaskystatic inline int list_is_last(const struct list_head *list,
420270710Shselasky                                const struct list_head *head)
421270710Shselasky{
422270710Shselasky        return list->next == head;
423270710Shselasky}
424219820Sjeff
425219820Sjeff#define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
426219820Sjeff
427219820Sjeff#define	hlist_for_each(p, head)						\
428294837Shselasky	for (p = (head)->first; p; p = (p)->next)
429219820Sjeff
430219820Sjeff#define	hlist_for_each_safe(p, n, head)					\
431294837Shselasky	for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
432219820Sjeff
433289574Shselasky#define	hlist_entry_safe(ptr, type, member) \
434289574Shselasky	((ptr) ? hlist_entry(ptr, type, member) : NULL)
435289574Shselasky
436289574Shselasky#define	hlist_for_each_entry(pos, head, member)				\
437289574Shselasky	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
438289574Shselasky	     pos;							\
439289574Shselasky	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
440289574Shselasky
441294837Shselasky#define	hlist_for_each_entry_continue(pos, member)			\
442294837Shselasky	for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
443294837Shselasky	     (pos);							\
444294837Shselasky	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
445219820Sjeff
446294837Shselasky#define	hlist_for_each_entry_from(pos, member)				\
447294837Shselasky	for (; (pos);								\
448294837Shselasky	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
449219820Sjeff
450294837Shselasky#define	hlist_for_each_entry_safe(pos, n, head, member)			\
451294837Shselasky	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
452294837Shselasky	     (pos) && ({ n = (pos)->member.next; 1; });			\
453294837Shselasky	     pos = hlist_entry_safe(n, typeof(*(pos)), member))
454219820Sjeff
455300496Shselaskyextern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
456300496Shselasky    struct list_head *a, struct list_head *b));
457300496Shselasky
458219820Sjeff#endif /* _LINUX_LIST_H_ */
459