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: stable/11/sys/compat/linuxkpi/common/include/linux/list.h 345922 2019-04-05 11:17:27Z 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 73328653Shselasky#ifndef prefetch 74219820Sjeff#define prefetch(x) 75328653Shselasky#endif 76219820Sjeff 77311802Shselasky#define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) } 78311802Shselasky 79311802Shselasky#define LINUX_LIST_HEAD(name) \ 80311802Shselasky struct list_head name = LINUX_LIST_HEAD_INIT(name) 81311802Shselasky 82311802Shselasky#ifndef LIST_HEAD_DEF 83311802Shselasky#define LIST_HEAD_DEF 84219820Sjeffstruct list_head { 85219820Sjeff struct list_head *next; 86219820Sjeff struct list_head *prev; 87219820Sjeff}; 88311802Shselasky#endif 89219820Sjeff 90219820Sjeffstatic inline void 91219820SjeffINIT_LIST_HEAD(struct list_head *list) 92219820Sjeff{ 93219820Sjeff 94219820Sjeff list->next = list->prev = list; 95219820Sjeff} 96331756Semaste 97219820Sjeffstatic inline int 98219820Sjefflist_empty(const struct list_head *head) 99219820Sjeff{ 100219820Sjeff 101219820Sjeff return (head->next == head); 102219820Sjeff} 103219820Sjeff 104311802Shselaskystatic inline int 105311802Shselaskylist_empty_careful(const struct list_head *head) 106311802Shselasky{ 107311802Shselasky struct list_head *next = head->next; 108311802Shselasky 109311802Shselasky return ((next == head) && (next == head->prev)); 110311802Shselasky} 111311802Shselasky 112219820Sjeffstatic inline void 113311802Shselasky__list_del(struct list_head *prev, struct list_head *next) 114311802Shselasky{ 115311802Shselasky next->prev = prev; 116311802Shselasky WRITE_ONCE(prev->next, next); 117311802Shselasky} 118311802Shselasky 119311802Shselaskystatic inline void 120329968Shselasky__list_del_entry(struct list_head *entry) 121329968Shselasky{ 122329968Shselasky 123329968Shselasky __list_del(entry->prev, entry->next); 124329968Shselasky} 125329968Shselasky 126329968Shselaskystatic inline void 127219820Sjefflist_del(struct list_head *entry) 128219820Sjeff{ 129219820Sjeff 130311802Shselasky __list_del(entry->prev, entry->next); 131219820Sjeff} 132219820Sjeff 133219820Sjeffstatic inline void 134289574Shselaskylist_replace(struct list_head *old, struct list_head *new) 135289574Shselasky{ 136289574Shselasky new->next = old->next; 137289574Shselasky new->next->prev = new; 138289574Shselasky new->prev = old->prev; 139289574Shselasky new->prev->next = new; 140289574Shselasky} 141289574Shselasky 142289574Shselaskystatic inline void 143300496Shselaskylist_replace_init(struct list_head *old, struct list_head *new) 144300496Shselasky{ 145300496Shselasky list_replace(old, new); 146300496Shselasky INIT_LIST_HEAD(old); 147300496Shselasky} 148300496Shselasky 149300496Shselaskystatic inline void 150294837Shselaskylinux_list_add(struct list_head *new, struct list_head *prev, 151219820Sjeff struct list_head *next) 152219820Sjeff{ 153219820Sjeff 154219820Sjeff next->prev = new; 155219820Sjeff new->next = next; 156219820Sjeff new->prev = prev; 157219820Sjeff prev->next = new; 158219820Sjeff} 159219820Sjeff 160219820Sjeffstatic inline void 161219820Sjefflist_del_init(struct list_head *entry) 162331756Semaste{ 163219820Sjeff 164219820Sjeff list_del(entry); 165219820Sjeff INIT_LIST_HEAD(entry); 166219820Sjeff} 167219820Sjeff 168219820Sjeff#define list_entry(ptr, type, field) container_of(ptr, type, field) 169219820Sjeff 170331756Semaste#define list_first_entry(ptr, type, member) \ 171331756Semaste list_entry((ptr)->next, type, member) 172255932Salfred 173300496Shselasky#define list_last_entry(ptr, type, member) \ 174300496Shselasky list_entry((ptr)->prev, type, member) 175300496Shselasky 176300496Shselasky#define list_first_entry_or_null(ptr, type, member) \ 177300496Shselasky (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) 178300496Shselasky 179289574Shselasky#define list_next_entry(ptr, member) \ 180289574Shselasky list_entry(((ptr)->member.next), typeof(*(ptr)), member) 181289574Shselasky 182329975Shselasky#define list_safe_reset_next(ptr, n, member) \ 183329975Shselasky (n) = list_next_entry(ptr, member) 184329975Shselasky 185300496Shselasky#define list_prev_entry(ptr, member) \ 186300496Shselasky list_entry(((ptr)->member.prev), typeof(*(ptr)), member) 187300496Shselasky 188219820Sjeff#define list_for_each(p, head) \ 189294837Shselasky for (p = (head)->next; p != (head); p = (p)->next) 190219820Sjeff 191219820Sjeff#define list_for_each_safe(p, n, head) \ 192294837Shselasky for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next) 193219820Sjeff 194219820Sjeff#define list_for_each_entry(p, h, field) \ 195294837Shselasky for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \ 196294837Shselasky p = list_entry((p)->field.next, typeof(*p), field)) 197219820Sjeff 198219820Sjeff#define list_for_each_entry_safe(p, n, h, field) \ 199331756Semaste for (p = list_entry((h)->next, typeof(*p), field), \ 200294837Shselasky n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\ 201219820Sjeff p = n, n = list_entry(n->field.next, typeof(*n), field)) 202219820Sjeff 203294837Shselasky#define list_for_each_entry_from(p, h, field) \ 204294837Shselasky for ( ; &(p)->field != (h); \ 205294837Shselasky p = list_entry((p)->field.next, typeof(*p), field)) 206294837Shselasky 207289574Shselasky#define list_for_each_entry_continue(p, h, field) \ 208294837Shselasky for (p = list_next_entry((p), field); &(p)->field != (h); \ 209289574Shselasky p = list_next_entry((p), field)) 210289574Shselasky 211331756Semaste#define list_for_each_entry_safe_from(pos, n, head, member) \ 212294837Shselasky for (n = list_entry((pos)->member.next, typeof(*pos), member); \ 213294837Shselasky &(pos)->member != (head); \ 214289574Shselasky pos = n, n = list_entry(n->member.next, typeof(*n), member)) 215289574Shselasky 216219820Sjeff#define list_for_each_entry_reverse(p, h, field) \ 217294837Shselasky for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \ 218294837Shselasky p = list_entry((p)->field.prev, typeof(*p), field)) 219219820Sjeff 220311802Shselasky#define list_for_each_entry_safe_reverse(p, n, h, field) \ 221331756Semaste for (p = list_entry((h)->prev, typeof(*p), field), \ 222311802Shselasky n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 223311802Shselasky p = n, n = list_entry(n->field.prev, typeof(*n), field)) 224311802Shselasky 225294837Shselasky#define list_for_each_entry_continue_reverse(p, h, field) \ 226294837Shselasky for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 227294837Shselasky p = list_entry((p)->field.prev, typeof(*p), field)) 228219820Sjeff 229294837Shselasky#define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev) 230294837Shselasky 231345922Shselasky#define list_for_each_entry_from_reverse(p, h, field) \ 232345922Shselasky for (; &p->field != (h); \ 233345922Shselasky p = list_prev_entry(p, field)) 234345922Shselasky 235219820Sjeffstatic inline void 236219820Sjefflist_add(struct list_head *new, struct list_head *head) 237219820Sjeff{ 238219820Sjeff 239294837Shselasky linux_list_add(new, head, head->next); 240219820Sjeff} 241219820Sjeff 242219820Sjeffstatic inline void 243219820Sjefflist_add_tail(struct list_head *new, struct list_head *head) 244219820Sjeff{ 245219820Sjeff 246294837Shselasky linux_list_add(new, head->prev, head); 247219820Sjeff} 248219820Sjeff 249219820Sjeffstatic inline void 250219820Sjefflist_move(struct list_head *list, struct list_head *head) 251219820Sjeff{ 252219820Sjeff 253219820Sjeff list_del(list); 254219820Sjeff list_add(list, head); 255219820Sjeff} 256219820Sjeff 257219820Sjeffstatic inline void 258219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head) 259219820Sjeff{ 260219820Sjeff 261219820Sjeff list_del(entry); 262219820Sjeff list_add_tail(entry, head); 263219820Sjeff} 264219820Sjeff 265219820Sjeffstatic inline void 266345922Shselaskylist_bulk_move_tail(struct list_head *head, struct list_head *first, 267345922Shselasky struct list_head *last) 268345922Shselasky{ 269345922Shselasky first->prev->next = last->next; 270345922Shselasky last->next->prev = first->prev; 271345922Shselasky head->prev->next = first; 272345922Shselasky first->prev = head->prev; 273345922Shselasky last->next = head; 274345922Shselasky head->prev = last; 275345922Shselasky} 276345922Shselasky 277345922Shselaskystatic inline void 278331756Semastelinux_list_splice(const struct list_head *list, struct list_head *prev, 279219820Sjeff struct list_head *next) 280219820Sjeff{ 281219820Sjeff struct list_head *first; 282219820Sjeff struct list_head *last; 283219820Sjeff 284219820Sjeff if (list_empty(list)) 285219820Sjeff return; 286219820Sjeff first = list->next; 287219820Sjeff last = list->prev; 288219820Sjeff first->prev = prev; 289219820Sjeff prev->next = first; 290219820Sjeff last->next = next; 291219820Sjeff next->prev = last; 292219820Sjeff} 293219820Sjeff 294219820Sjeffstatic inline void 295219820Sjefflist_splice(const struct list_head *list, struct list_head *head) 296219820Sjeff{ 297219820Sjeff 298294837Shselasky linux_list_splice(list, head, head->next); 299331756Semaste} 300219820Sjeff 301219820Sjeffstatic inline void 302219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head) 303219820Sjeff{ 304219820Sjeff 305294837Shselasky linux_list_splice(list, head->prev, head); 306219820Sjeff} 307331756Semaste 308219820Sjeffstatic inline void 309219820Sjefflist_splice_init(struct list_head *list, struct list_head *head) 310219820Sjeff{ 311219820Sjeff 312294837Shselasky linux_list_splice(list, head, head->next); 313331756Semaste INIT_LIST_HEAD(list); 314219820Sjeff} 315331756Semaste 316219820Sjeffstatic inline void 317219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head) 318219820Sjeff{ 319219820Sjeff 320294837Shselasky linux_list_splice(list, head->prev, head); 321219820Sjeff INIT_LIST_HEAD(list); 322219820Sjeff} 323219820Sjeff 324219820Sjeff#undef LIST_HEAD 325219820Sjeff#define LIST_HEAD(name) struct list_head name = { &(name), &(name) } 326219820Sjeff 327219820Sjeff 328219820Sjeffstruct hlist_head { 329219820Sjeff struct hlist_node *first; 330219820Sjeff}; 331219820Sjeff 332219820Sjeffstruct hlist_node { 333219820Sjeff struct hlist_node *next, **pprev; 334219820Sjeff}; 335219820Sjeff 336219820Sjeff#define HLIST_HEAD_INIT { } 337219820Sjeff#define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 338219820Sjeff#define INIT_HLIST_HEAD(head) (head)->first = NULL 339219820Sjeff#define INIT_HLIST_NODE(node) \ 340219820Sjeffdo { \ 341219820Sjeff (node)->next = NULL; \ 342219820Sjeff (node)->pprev = NULL; \ 343219820Sjeff} while (0) 344219820Sjeff 345219820Sjeffstatic inline int 346219820Sjeffhlist_unhashed(const struct hlist_node *h) 347219820Sjeff{ 348219820Sjeff 349219820Sjeff return !h->pprev; 350219820Sjeff} 351219820Sjeff 352219820Sjeffstatic inline int 353219820Sjeffhlist_empty(const struct hlist_head *h) 354219820Sjeff{ 355219820Sjeff 356335427Shselasky return !READ_ONCE(h->first); 357219820Sjeff} 358219820Sjeff 359219820Sjeffstatic inline void 360219820Sjeffhlist_del(struct hlist_node *n) 361219820Sjeff{ 362219820Sjeff 363335427Shselasky WRITE_ONCE(*(n->pprev), n->next); 364335427Shselasky if (n->next != NULL) 365331756Semaste n->next->pprev = n->pprev; 366219820Sjeff} 367219820Sjeff 368219820Sjeffstatic inline void 369219820Sjeffhlist_del_init(struct hlist_node *n) 370219820Sjeff{ 371219820Sjeff 372219820Sjeff if (hlist_unhashed(n)) 373219820Sjeff return; 374219820Sjeff hlist_del(n); 375219820Sjeff INIT_HLIST_NODE(n); 376219820Sjeff} 377219820Sjeff 378219820Sjeffstatic inline void 379219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h) 380219820Sjeff{ 381219820Sjeff 382219820Sjeff n->next = h->first; 383335427Shselasky if (h->first != NULL) 384219820Sjeff h->first->pprev = &n->next; 385335427Shselasky WRITE_ONCE(h->first, n); 386219820Sjeff n->pprev = &h->first; 387219820Sjeff} 388219820Sjeff 389219820Sjeffstatic inline void 390219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next) 391219820Sjeff{ 392219820Sjeff 393219820Sjeff n->pprev = next->pprev; 394219820Sjeff n->next = next; 395219820Sjeff next->pprev = &n->next; 396335427Shselasky WRITE_ONCE(*(n->pprev), n); 397219820Sjeff} 398331756Semaste 399219820Sjeffstatic inline void 400335427Shselaskyhlist_add_behind(struct hlist_node *n, struct hlist_node *prev) 401219820Sjeff{ 402219820Sjeff 403335427Shselasky n->next = prev->next; 404335427Shselasky WRITE_ONCE(prev->next, n); 405335427Shselasky n->pprev = &prev->next; 406335427Shselasky 407335427Shselasky if (n->next != NULL) 408335427Shselasky n->next->pprev = &n->next; 409219820Sjeff} 410331756Semaste 411219820Sjeffstatic inline void 412219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new) 413219820Sjeff{ 414219820Sjeff 415219820Sjeff new->first = old->first; 416219820Sjeff if (new->first) 417219820Sjeff new->first->pprev = &new->first; 418219820Sjeff old->first = NULL; 419219820Sjeff} 420270710Shselasky 421270710Shselaskystatic inline int list_is_singular(const struct list_head *head) 422270710Shselasky{ 423270710Shselasky return !list_empty(head) && (head->next == head->prev); 424270710Shselasky} 425270710Shselasky 426270710Shselaskystatic inline void __list_cut_position(struct list_head *list, 427270710Shselasky struct list_head *head, struct list_head *entry) 428270710Shselasky{ 429270710Shselasky struct list_head *new_first = entry->next; 430270710Shselasky list->next = head->next; 431270710Shselasky list->next->prev = list; 432270710Shselasky list->prev = entry; 433270710Shselasky entry->next = list; 434270710Shselasky head->next = new_first; 435270710Shselasky new_first->prev = head; 436270710Shselasky} 437270710Shselasky 438270710Shselaskystatic inline void list_cut_position(struct list_head *list, 439270710Shselasky struct list_head *head, struct list_head *entry) 440270710Shselasky{ 441270710Shselasky if (list_empty(head)) 442270710Shselasky return; 443270710Shselasky if (list_is_singular(head) && 444270710Shselasky (head->next != entry && head != entry)) 445270710Shselasky return; 446270710Shselasky if (entry == head) 447270710Shselasky INIT_LIST_HEAD(list); 448270710Shselasky else 449270710Shselasky __list_cut_position(list, head, entry); 450270710Shselasky} 451270710Shselasky 452270710Shselaskystatic inline int list_is_last(const struct list_head *list, 453331756Semaste const struct list_head *head) 454270710Shselasky{ 455331756Semaste return list->next == head; 456270710Shselasky} 457331756Semaste 458219820Sjeff#define hlist_entry(ptr, type, field) container_of(ptr, type, field) 459219820Sjeff 460219820Sjeff#define hlist_for_each(p, head) \ 461294837Shselasky for (p = (head)->first; p; p = (p)->next) 462219820Sjeff 463219820Sjeff#define hlist_for_each_safe(p, n, head) \ 464294837Shselasky for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n) 465219820Sjeff 466289574Shselasky#define hlist_entry_safe(ptr, type, member) \ 467289574Shselasky ((ptr) ? hlist_entry(ptr, type, member) : NULL) 468289574Shselasky 469289574Shselasky#define hlist_for_each_entry(pos, head, member) \ 470289574Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 471289574Shselasky pos; \ 472289574Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 473289574Shselasky 474294837Shselasky#define hlist_for_each_entry_continue(pos, member) \ 475294837Shselasky for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \ 476294837Shselasky (pos); \ 477294837Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 478219820Sjeff 479294837Shselasky#define hlist_for_each_entry_from(pos, member) \ 480294837Shselasky for (; (pos); \ 481294837Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 482219820Sjeff 483294837Shselasky#define hlist_for_each_entry_safe(pos, n, head, member) \ 484294837Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \ 485294837Shselasky (pos) && ({ n = (pos)->member.next; 1; }); \ 486294837Shselasky pos = hlist_entry_safe(n, typeof(*(pos)), member)) 487219820Sjeff 488300496Shselaskyextern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 489300496Shselasky struct list_head *a, struct list_head *b)); 490300496Shselasky 491219820Sjeff#endif /* _LINUX_LIST_H_ */ 492