xref: /freebsd/sys/compat/linuxkpi/common/include/linux/list.h (revision e2eeea75eb8b6dd50c1298067a0655880d186734)
1 /*-
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.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
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.
17  *
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.
28  *
29  * $FreeBSD$
30  */
31 #ifndef _LINUX_LIST_H_
32 #define _LINUX_LIST_H_
33 
34 /*
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.
38  */
39 #include <linux/types.h>
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/queue.h>
44 #include <sys/cpuset.h>
45 #include <sys/jail.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 #include <sys/conf.h>
51 #include <sys/socket.h>
52 #include <sys/mbuf.h>
53 
54 #include <net/bpf.h>
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_types.h>
58 #include <net/if_media.h>
59 #include <net/vnet.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/tcp_lro.h>
65 
66 #include <netinet6/in6_var.h>
67 #include <netinet6/nd6.h>
68 
69 #include <vm/vm.h>
70 #include <vm/vm_object.h>
71 #include <vm/pmap.h>
72 
73 #ifndef prefetch
74 #define	prefetch(x)
75 #endif
76 #define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) }
77 
78 #define LINUX_LIST_HEAD(name) \
79 	struct list_head name = LINUX_LIST_HEAD_INIT(name)
80 
81 #ifndef LIST_HEAD_DEF
82 #define	LIST_HEAD_DEF
83 struct list_head {
84 	struct list_head *next;
85 	struct list_head *prev;
86 };
87 #endif
88 
89 static inline void
90 INIT_LIST_HEAD(struct list_head *list)
91 {
92 
93 	list->next = list->prev = list;
94 }
95 
96 static inline int
97 list_empty(const struct list_head *head)
98 {
99 
100 	return (head->next == head);
101 }
102 
103 static inline int
104 list_empty_careful(const struct list_head *head)
105 {
106 	struct list_head *next = head->next;
107 
108 	return ((next == head) && (next == head->prev));
109 }
110 
111 static inline void
112 __list_del(struct list_head *prev, struct list_head *next)
113 {
114 	next->prev = prev;
115 	WRITE_ONCE(prev->next, next);
116 }
117 
118 static inline void
119 __list_del_entry(struct list_head *entry)
120 {
121 
122 	__list_del(entry->prev, entry->next);
123 }
124 
125 static inline void
126 list_del(struct list_head *entry)
127 {
128 
129 	__list_del(entry->prev, entry->next);
130 }
131 
132 static inline void
133 list_replace(struct list_head *old, struct list_head *new)
134 {
135 	new->next = old->next;
136 	new->next->prev = new;
137 	new->prev = old->prev;
138 	new->prev->next = new;
139 }
140 
141 static inline void
142 list_replace_init(struct list_head *old, struct list_head *new)
143 {
144 	list_replace(old, new);
145 	INIT_LIST_HEAD(old);
146 }
147 
148 static inline void
149 linux_list_add(struct list_head *new, struct list_head *prev,
150     struct list_head *next)
151 {
152 
153 	next->prev = new;
154 	new->next = next;
155 	new->prev = prev;
156 	prev->next = new;
157 }
158 
159 static inline void
160 list_del_init(struct list_head *entry)
161 {
162 
163 	list_del(entry);
164 	INIT_LIST_HEAD(entry);
165 }
166 
167 #define	list_entry(ptr, type, field)	container_of(ptr, type, field)
168 
169 #define	list_first_entry(ptr, type, member) \
170 	list_entry((ptr)->next, type, member)
171 
172 #define	list_last_entry(ptr, type, member)	\
173 	list_entry((ptr)->prev, type, member)
174 
175 #define	list_first_entry_or_null(ptr, type, member) \
176 	(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
177 
178 #define	list_next_entry(ptr, member)					\
179 	list_entry(((ptr)->member.next), typeof(*(ptr)), member)
180 
181 #define	list_safe_reset_next(ptr, n, member) \
182 	(n) = list_next_entry(ptr, member)
183 
184 #define	list_prev_entry(ptr, member)					\
185 	list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
186 
187 #define	list_for_each(p, head)						\
188 	for (p = (head)->next; p != (head); p = (p)->next)
189 
190 #define	list_for_each_safe(p, n, head)					\
191 	for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
192 
193 #define list_for_each_entry(p, h, field)				\
194 	for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
195 	    p = list_entry((p)->field.next, typeof(*p), field))
196 
197 #define list_for_each_entry_safe(p, n, h, field)			\
198 	for (p = list_entry((h)->next, typeof(*p), field),		\
199 	    n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
200 	    p = n, n = list_entry(n->field.next, typeof(*n), field))
201 
202 #define	list_for_each_entry_from(p, h, field) \
203 	for ( ; &(p)->field != (h); \
204 	    p = list_entry((p)->field.next, typeof(*p), field))
205 
206 #define	list_for_each_entry_continue(p, h, field)			\
207 	for (p = list_next_entry((p), field); &(p)->field != (h);	\
208 	    p = list_next_entry((p), field))
209 
210 #define	list_for_each_entry_safe_from(pos, n, head, member)			\
211 	for (n = list_entry((pos)->member.next, typeof(*pos), member);		\
212 	     &(pos)->member != (head);						\
213 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
214 
215 #define	list_for_each_entry_reverse(p, h, field)			\
216 	for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
217 	    p = list_entry((p)->field.prev, typeof(*p), field))
218 
219 #define	list_for_each_entry_safe_reverse(p, n, h, field)		\
220 	for (p = list_entry((h)->prev, typeof(*p), field),		\
221 	    n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
222 	    p = n, n = list_entry(n->field.prev, typeof(*n), field))
223 
224 #define	list_for_each_entry_continue_reverse(p, h, field) \
225 	for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
226 	    p = list_entry((p)->field.prev, typeof(*p), field))
227 
228 #define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
229 
230 #define	list_for_each_entry_from_reverse(p, h, field)	\
231 	for (; &p->field != (h);			\
232 	     p = list_prev_entry(p, field))
233 
234 static inline void
235 list_add(struct list_head *new, struct list_head *head)
236 {
237 
238 	linux_list_add(new, head, head->next);
239 }
240 
241 static inline void
242 list_add_tail(struct list_head *new, struct list_head *head)
243 {
244 
245 	linux_list_add(new, head->prev, head);
246 }
247 
248 static inline void
249 list_move(struct list_head *list, struct list_head *head)
250 {
251 
252 	list_del(list);
253 	list_add(list, head);
254 }
255 
256 static inline void
257 list_move_tail(struct list_head *entry, struct list_head *head)
258 {
259 
260 	list_del(entry);
261 	list_add_tail(entry, head);
262 }
263 
264 static inline void
265 list_bulk_move_tail(struct list_head *head, struct list_head *first,
266     struct list_head *last)
267 {
268 	first->prev->next = last->next;
269 	last->next->prev = first->prev;
270 	head->prev->next = first;
271 	first->prev = head->prev;
272 	last->next = head;
273 	head->prev = last;
274 }
275 
276 static inline void
277 linux_list_splice(const struct list_head *list, struct list_head *prev,
278     struct list_head *next)
279 {
280 	struct list_head *first;
281 	struct list_head *last;
282 
283 	if (list_empty(list))
284 		return;
285 	first = list->next;
286 	last = list->prev;
287 	first->prev = prev;
288 	prev->next = first;
289 	last->next = next;
290 	next->prev = last;
291 }
292 
293 static inline void
294 list_splice(const struct list_head *list, struct list_head *head)
295 {
296 
297 	linux_list_splice(list, head, head->next);
298 }
299 
300 static inline void
301 list_splice_tail(struct list_head *list, struct list_head *head)
302 {
303 
304 	linux_list_splice(list, head->prev, head);
305 }
306 
307 static inline void
308 list_splice_init(struct list_head *list, struct list_head *head)
309 {
310 
311 	linux_list_splice(list, head, head->next);
312 	INIT_LIST_HEAD(list);
313 }
314 
315 static inline void
316 list_splice_tail_init(struct list_head *list, struct list_head *head)
317 {
318 
319 	linux_list_splice(list, head->prev, head);
320 	INIT_LIST_HEAD(list);
321 }
322 
323 #undef LIST_HEAD
324 #define LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
325 
326 struct hlist_head {
327 	struct hlist_node *first;
328 };
329 
330 struct hlist_node {
331 	struct hlist_node *next, **pprev;
332 };
333 #define	HLIST_HEAD_INIT { }
334 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
335 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
336 #define	INIT_HLIST_NODE(node)						\
337 do {									\
338 	(node)->next = NULL;						\
339 	(node)->pprev = NULL;						\
340 } while (0)
341 
342 static inline int
343 hlist_unhashed(const struct hlist_node *h)
344 {
345 
346 	return !h->pprev;
347 }
348 
349 static inline int
350 hlist_empty(const struct hlist_head *h)
351 {
352 
353 	return !READ_ONCE(h->first);
354 }
355 
356 static inline void
357 hlist_del(struct hlist_node *n)
358 {
359 
360 	WRITE_ONCE(*(n->pprev), n->next);
361 	if (n->next != NULL)
362 		n->next->pprev = n->pprev;
363 }
364 
365 static inline void
366 hlist_del_init(struct hlist_node *n)
367 {
368 
369 	if (hlist_unhashed(n))
370 		return;
371 	hlist_del(n);
372 	INIT_HLIST_NODE(n);
373 }
374 
375 static inline void
376 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
377 {
378 
379 	n->next = h->first;
380 	if (h->first != NULL)
381 		h->first->pprev = &n->next;
382 	WRITE_ONCE(h->first, n);
383 	n->pprev = &h->first;
384 }
385 
386 static inline void
387 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
388 {
389 
390 	n->pprev = next->pprev;
391 	n->next = next;
392 	next->pprev = &n->next;
393 	WRITE_ONCE(*(n->pprev), n);
394 }
395 
396 static inline void
397 hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
398 {
399 
400 	n->next = prev->next;
401 	WRITE_ONCE(prev->next, n);
402 	n->pprev = &prev->next;
403 
404 	if (n->next != NULL)
405 		n->next->pprev = &n->next;
406 }
407 
408 static inline void
409 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
410 {
411 
412 	new->first = old->first;
413 	if (new->first)
414 		new->first->pprev = &new->first;
415 	old->first = NULL;
416 }
417 
418 static inline int list_is_singular(const struct list_head *head)
419 {
420 	return !list_empty(head) && (head->next == head->prev);
421 }
422 
423 static inline void __list_cut_position(struct list_head *list,
424 		struct list_head *head, struct list_head *entry)
425 {
426 	struct list_head *new_first = entry->next;
427 	list->next = head->next;
428 	list->next->prev = list;
429 	list->prev = entry;
430 	entry->next = list;
431 	head->next = new_first;
432 	new_first->prev = head;
433 }
434 
435 static inline void list_cut_position(struct list_head *list,
436 		struct list_head *head, struct list_head *entry)
437 {
438 	if (list_empty(head))
439 		return;
440 	if (list_is_singular(head) &&
441 		(head->next != entry && head != entry))
442 		return;
443 	if (entry == head)
444 		INIT_LIST_HEAD(list);
445 	else
446 		__list_cut_position(list, head, entry);
447 }
448 
449 static inline int list_is_first(const struct list_head *list,
450 				const struct list_head *head)
451 {
452 
453 	return (list->prev == head);
454 }
455 
456 static inline int list_is_last(const struct list_head *list,
457 				const struct list_head *head)
458 {
459 	return list->next == head;
460 }
461 
462 #define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
463 
464 #define	hlist_for_each(p, head)						\
465 	for (p = (head)->first; p; p = (p)->next)
466 
467 #define	hlist_for_each_safe(p, n, head)					\
468 	for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
469 
470 #define	hlist_entry_safe(ptr, type, member) \
471 	((ptr) ? hlist_entry(ptr, type, member) : NULL)
472 
473 #define	hlist_for_each_entry(pos, head, member)				\
474 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
475 	     pos;							\
476 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
477 
478 #define	hlist_for_each_entry_continue(pos, member)			\
479 	for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
480 	     (pos);							\
481 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
482 
483 #define	hlist_for_each_entry_from(pos, member)				\
484 	for (; (pos);								\
485 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
486 
487 #define	hlist_for_each_entry_safe(pos, n, head, member)			\
488 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
489 	     (pos) && ({ n = (pos)->member.next; 1; });			\
490 	     pos = hlist_entry_safe(n, typeof(*(pos)), member))
491 
492 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
493     struct list_head *a, struct list_head *b));
494 
495 #endif /* _LINUX_LIST_H_ */
496