1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5219820Sjeff * All rights reserved.
6219820Sjeff *
7219820Sjeff * Redistribution and use in source and binary forms, with or without
8219820Sjeff * modification, are permitted provided that the following conditions
9219820Sjeff * are met:
10219820Sjeff * 1. Redistributions of source code must retain the above copyright
11219820Sjeff *    notice unmodified, this list of conditions, and the following
12219820Sjeff *    disclaimer.
13219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
14219820Sjeff *    notice, this list of conditions and the following disclaimer in the
15219820Sjeff *    documentation and/or other materials provided with the distribution.
16219820Sjeff *
17219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27219820Sjeff */
28219820Sjeff#ifndef _LINUX_LIST_H_
29219820Sjeff#define _LINUX_LIST_H_
30219820Sjeff
31219820Sjeff/*
32219820Sjeff * Since LIST_HEAD conflicts with the linux definition we must include any
33219820Sjeff * FreeBSD header which requires it here so it is resolved with the correct
34219820Sjeff * definition prior to the undef.
35219820Sjeff */
36219820Sjeff#include <linux/types.h>
37219820Sjeff
38219820Sjeff#include <sys/param.h>
39219820Sjeff#include <sys/kernel.h>
40219820Sjeff#include <sys/queue.h>
41222813Sattilio#include <sys/cpuset.h>
42219820Sjeff#include <sys/lock.h>
43219820Sjeff#include <sys/mutex.h>
44219820Sjeff#include <sys/proc.h>
45219820Sjeff#include <sys/vnode.h>
46219820Sjeff#include <sys/conf.h>
47219820Sjeff#include <sys/socket.h>
48219820Sjeff#include <sys/mbuf.h>
49219820Sjeff
50219820Sjeff#include <net/bpf.h>
51219820Sjeff#include <net/if.h>
52219820Sjeff#include <net/if_types.h>
53219820Sjeff#include <net/if_media.h>
54219820Sjeff
55219820Sjeff#include <netinet/in.h>
56219820Sjeff#include <netinet/in_pcb.h>
57219820Sjeff
58219820Sjeff#include <netinet6/in6_var.h>
59219820Sjeff#include <netinet6/nd6.h>
60219820Sjeff
61219820Sjeff#include <vm/vm.h>
62219820Sjeff#include <vm/vm_object.h>
63219820Sjeff
64219820Sjeff#define	prefetch(x)
65219820Sjeff
66219820Sjeffstruct list_head {
67219820Sjeff	struct list_head *next;
68219820Sjeff	struct list_head *prev;
69219820Sjeff};
70219820Sjeff
71219820Sjeffstatic inline void
72219820SjeffINIT_LIST_HEAD(struct list_head *list)
73219820Sjeff{
74219820Sjeff
75219820Sjeff	list->next = list->prev = list;
76219820Sjeff}
77219820Sjeff
78219820Sjeffstatic inline int
79219820Sjefflist_empty(const struct list_head *head)
80219820Sjeff{
81219820Sjeff
82219820Sjeff	return (head->next == head);
83219820Sjeff}
84219820Sjeff
85219820Sjeffstatic inline void
86219820Sjefflist_del(struct list_head *entry)
87219820Sjeff{
88219820Sjeff
89219820Sjeff	entry->next->prev = entry->prev;
90219820Sjeff	entry->prev->next = entry->next;
91219820Sjeff}
92219820Sjeff
93219820Sjeffstatic inline void
94219820Sjeff_list_add(struct list_head *new, struct list_head *prev,
95219820Sjeff    struct list_head *next)
96219820Sjeff{
97219820Sjeff
98219820Sjeff	next->prev = new;
99219820Sjeff	new->next = next;
100219820Sjeff	new->prev = prev;
101219820Sjeff	prev->next = new;
102219820Sjeff}
103219820Sjeff
104219820Sjeffstatic inline void
105219820Sjefflist_del_init(struct list_head *entry)
106219820Sjeff{
107219820Sjeff
108219820Sjeff	list_del(entry);
109219820Sjeff	INIT_LIST_HEAD(entry);
110219820Sjeff}
111219820Sjeff
112219820Sjeff#define	list_entry(ptr, type, field)	container_of(ptr, type, field)
113219820Sjeff
114219820Sjeff#define	list_for_each(p, head)						\
115219820Sjeff	for (p = (head)->next; p != (head); p = p->next)
116219820Sjeff
117219820Sjeff#define	list_for_each_safe(p, n, head)					\
118219820Sjeff	for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
119219820Sjeff
120219820Sjeff#define list_for_each_entry(p, h, field)				\
121219820Sjeff	for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \
122219820Sjeff	    p = list_entry(p->field.next, typeof(*p), field))
123219820Sjeff
124219820Sjeff#define list_for_each_entry_safe(p, n, h, field)			\
125219820Sjeff	for (p = list_entry((h)->next, typeof(*p), field), 		\
126219820Sjeff	    n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
127219820Sjeff	    p = n, n = list_entry(n->field.next, typeof(*n), field))
128219820Sjeff
129219820Sjeff#define	list_for_each_entry_reverse(p, h, field)			\
130219820Sjeff	for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \
131219820Sjeff	    p = list_entry(p->field.prev, typeof(*p), field))
132219820Sjeff
133219820Sjeff#define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
134219820Sjeff
135219820Sjeffstatic inline void
136219820Sjefflist_add(struct list_head *new, struct list_head *head)
137219820Sjeff{
138219820Sjeff
139219820Sjeff	_list_add(new, head, head->next);
140219820Sjeff}
141219820Sjeff
142219820Sjeffstatic inline void
143219820Sjefflist_add_tail(struct list_head *new, struct list_head *head)
144219820Sjeff{
145219820Sjeff
146219820Sjeff	_list_add(new, head->prev, head);
147219820Sjeff}
148219820Sjeff
149219820Sjeffstatic inline void
150219820Sjefflist_move(struct list_head *list, struct list_head *head)
151219820Sjeff{
152219820Sjeff
153219820Sjeff	list_del(list);
154219820Sjeff	list_add(list, head);
155219820Sjeff}
156219820Sjeff
157219820Sjeffstatic inline void
158219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head)
159219820Sjeff{
160219820Sjeff
161219820Sjeff	list_del(entry);
162219820Sjeff	list_add_tail(entry, head);
163219820Sjeff}
164219820Sjeff
165219820Sjeffstatic inline void
166219820Sjeff_list_splice(const struct list_head *list, struct list_head *prev,
167219820Sjeff    struct list_head *next)
168219820Sjeff{
169219820Sjeff	struct list_head *first;
170219820Sjeff	struct list_head *last;
171219820Sjeff
172219820Sjeff	if (list_empty(list))
173219820Sjeff		return;
174219820Sjeff	first = list->next;
175219820Sjeff	last = list->prev;
176219820Sjeff	first->prev = prev;
177219820Sjeff	prev->next = first;
178219820Sjeff	last->next = next;
179219820Sjeff	next->prev = last;
180219820Sjeff}
181219820Sjeff
182219820Sjeffstatic inline void
183219820Sjefflist_splice(const struct list_head *list, struct list_head *head)
184219820Sjeff{
185219820Sjeff
186219820Sjeff	_list_splice(list, head, head->next);
187219820Sjeff}
188219820Sjeff
189219820Sjeffstatic inline void
190219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head)
191219820Sjeff{
192219820Sjeff
193219820Sjeff	_list_splice(list, head->prev, head);
194219820Sjeff}
195219820Sjeff
196219820Sjeffstatic inline void
197219820Sjefflist_splice_init(struct list_head *list, struct list_head *head)
198219820Sjeff{
199219820Sjeff
200219820Sjeff	_list_splice(list, head, head->next);
201219820Sjeff	INIT_LIST_HEAD(list);
202219820Sjeff}
203219820Sjeff
204219820Sjeffstatic inline void
205219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head)
206219820Sjeff{
207219820Sjeff
208219820Sjeff	_list_splice(list, head->prev, head);
209219820Sjeff	INIT_LIST_HEAD(list);
210219820Sjeff}
211219820Sjeff
212219820Sjeff#undef LIST_HEAD
213219820Sjeff#define LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
214219820Sjeff
215219820Sjeff
216219820Sjeffstruct hlist_head {
217219820Sjeff	struct hlist_node *first;
218219820Sjeff};
219219820Sjeff
220219820Sjeffstruct hlist_node {
221219820Sjeff	struct hlist_node *next, **pprev;
222219820Sjeff};
223219820Sjeff
224219820Sjeff#define	HLIST_HEAD_INIT { }
225219820Sjeff#define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
226219820Sjeff#define	INIT_HLIST_HEAD(head) (head)->first = NULL
227219820Sjeff#define	INIT_HLIST_NODE(node)						\
228219820Sjeffdo {									\
229219820Sjeff	(node)->next = NULL;						\
230219820Sjeff	(node)->pprev = NULL;						\
231219820Sjeff} while (0)
232219820Sjeff
233219820Sjeffstatic inline int
234219820Sjeffhlist_unhashed(const struct hlist_node *h)
235219820Sjeff{
236219820Sjeff
237219820Sjeff	return !h->pprev;
238219820Sjeff}
239219820Sjeff
240219820Sjeffstatic inline int
241219820Sjeffhlist_empty(const struct hlist_head *h)
242219820Sjeff{
243219820Sjeff
244219820Sjeff	return !h->first;
245219820Sjeff}
246219820Sjeff
247219820Sjeffstatic inline void
248219820Sjeffhlist_del(struct hlist_node *n)
249219820Sjeff{
250219820Sjeff
251219820Sjeff        if (n->next)
252219820Sjeff                n->next->pprev = n->pprev;
253219820Sjeff        *n->pprev = n->next;
254219820Sjeff}
255219820Sjeff
256219820Sjeffstatic inline void
257219820Sjeffhlist_del_init(struct hlist_node *n)
258219820Sjeff{
259219820Sjeff
260219820Sjeff	if (hlist_unhashed(n))
261219820Sjeff		return;
262219820Sjeff	hlist_del(n);
263219820Sjeff	INIT_HLIST_NODE(n);
264219820Sjeff}
265219820Sjeff
266219820Sjeffstatic inline void
267219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h)
268219820Sjeff{
269219820Sjeff
270219820Sjeff	n->next = h->first;
271219820Sjeff	if (h->first)
272219820Sjeff		h->first->pprev = &n->next;
273219820Sjeff	h->first = n;
274219820Sjeff	n->pprev = &h->first;
275219820Sjeff}
276219820Sjeff
277219820Sjeffstatic inline void
278219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next)
279219820Sjeff{
280219820Sjeff
281219820Sjeff	n->pprev = next->pprev;
282219820Sjeff	n->next = next;
283219820Sjeff	next->pprev = &n->next;
284219820Sjeff	*(n->pprev) = n;
285219820Sjeff}
286219820Sjeff
287219820Sjeffstatic inline void
288219820Sjeffhlist_add_after(struct hlist_node *n, struct hlist_node *next)
289219820Sjeff{
290219820Sjeff
291219820Sjeff	next->next = n->next;
292219820Sjeff	n->next = next;
293219820Sjeff	next->pprev = &n->next;
294219820Sjeff	if (next->next)
295219820Sjeff		next->next->pprev = &next->next;
296219820Sjeff}
297219820Sjeff
298219820Sjeffstatic inline void
299219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new)
300219820Sjeff{
301219820Sjeff
302219820Sjeff	new->first = old->first;
303219820Sjeff	if (new->first)
304219820Sjeff		new->first->pprev = &new->first;
305219820Sjeff	old->first = NULL;
306219820Sjeff}
307219820Sjeff
308219820Sjeff#define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
309219820Sjeff
310219820Sjeff#define	hlist_for_each(p, head)						\
311219820Sjeff	for (p = (head)->first; p; p = p->next)
312219820Sjeff
313219820Sjeff#define	hlist_for_each_safe(p, n, head)					\
314219820Sjeff	for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
315219820Sjeff
316219820Sjeff#define	hlist_for_each_entry(tp, p, head, field)			\
317219820Sjeff	for (p = (head)->first;						\
318219820Sjeff	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
319219820Sjeff
320219820Sjeff#define hlist_for_each_entry_continue(tp, p, field)			\
321219820Sjeff	for (p = (p)->next;						\
322219820Sjeff	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
323219820Sjeff
324219820Sjeff#define	hlist_for_each_entry_from(tp, p, field)				\
325219820Sjeff	for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
326219820Sjeff
327219820Sjeff#define	hlist_for_each_entry_safe(tp, p, n, head, field)		\
328219820Sjeff	for (p = (head)->first;	p ?					\
329219820Sjeff	    (n = p->next) | (tp = hlist_entry(p, typeof(*tp), field)) :	\
330219820Sjeff	    NULL; p = n)
331219820Sjeff
332219820Sjeff#endif /* _LINUX_LIST_H_ */
333