list.h revision 329968
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 329968 2018-02-25 10:32:26Z 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} 96219820Sjeff 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) 162219820Sjeff{ 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 170255932Salfred#define list_first_entry(ptr, type, member) \ 171255932Salfred 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 182300496Shselasky#define list_prev_entry(ptr, member) \ 183300496Shselasky list_entry(((ptr)->member.prev), typeof(*(ptr)), member) 184300496Shselasky 185219820Sjeff#define list_for_each(p, head) \ 186294837Shselasky for (p = (head)->next; p != (head); p = (p)->next) 187219820Sjeff 188219820Sjeff#define list_for_each_safe(p, n, head) \ 189294837Shselasky for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next) 190219820Sjeff 191219820Sjeff#define list_for_each_entry(p, h, field) \ 192294837Shselasky for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \ 193294837Shselasky p = list_entry((p)->field.next, typeof(*p), field)) 194219820Sjeff 195219820Sjeff#define list_for_each_entry_safe(p, n, h, field) \ 196219820Sjeff for (p = list_entry((h)->next, typeof(*p), field), \ 197294837Shselasky n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\ 198219820Sjeff p = n, n = list_entry(n->field.next, typeof(*n), field)) 199219820Sjeff 200294837Shselasky#define list_for_each_entry_from(p, h, field) \ 201294837Shselasky for ( ; &(p)->field != (h); \ 202294837Shselasky p = list_entry((p)->field.next, typeof(*p), field)) 203294837Shselasky 204289574Shselasky#define list_for_each_entry_continue(p, h, field) \ 205294837Shselasky for (p = list_next_entry((p), field); &(p)->field != (h); \ 206289574Shselasky p = list_next_entry((p), field)) 207289574Shselasky 208289574Shselasky#define list_for_each_entry_safe_from(pos, n, head, member) \ 209294837Shselasky for (n = list_entry((pos)->member.next, typeof(*pos), member); \ 210294837Shselasky &(pos)->member != (head); \ 211289574Shselasky pos = n, n = list_entry(n->member.next, typeof(*n), member)) 212289574Shselasky 213219820Sjeff#define list_for_each_entry_reverse(p, h, field) \ 214294837Shselasky for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \ 215294837Shselasky p = list_entry((p)->field.prev, typeof(*p), field)) 216219820Sjeff 217311802Shselasky#define list_for_each_entry_safe_reverse(p, n, h, field) \ 218311802Shselasky for (p = list_entry((h)->prev, typeof(*p), field), \ 219311802Shselasky n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 220311802Shselasky p = n, n = list_entry(n->field.prev, typeof(*n), field)) 221311802Shselasky 222294837Shselasky#define list_for_each_entry_continue_reverse(p, h, field) \ 223294837Shselasky for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 224294837Shselasky p = list_entry((p)->field.prev, typeof(*p), field)) 225219820Sjeff 226294837Shselasky#define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev) 227294837Shselasky 228219820Sjeffstatic inline void 229219820Sjefflist_add(struct list_head *new, struct list_head *head) 230219820Sjeff{ 231219820Sjeff 232294837Shselasky linux_list_add(new, head, head->next); 233219820Sjeff} 234219820Sjeff 235219820Sjeffstatic inline void 236219820Sjefflist_add_tail(struct list_head *new, struct list_head *head) 237219820Sjeff{ 238219820Sjeff 239294837Shselasky linux_list_add(new, head->prev, head); 240219820Sjeff} 241219820Sjeff 242219820Sjeffstatic inline void 243219820Sjefflist_move(struct list_head *list, struct list_head *head) 244219820Sjeff{ 245219820Sjeff 246219820Sjeff list_del(list); 247219820Sjeff list_add(list, head); 248219820Sjeff} 249219820Sjeff 250219820Sjeffstatic inline void 251219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head) 252219820Sjeff{ 253219820Sjeff 254219820Sjeff list_del(entry); 255219820Sjeff list_add_tail(entry, head); 256219820Sjeff} 257219820Sjeff 258219820Sjeffstatic inline void 259294837Shselaskylinux_list_splice(const struct list_head *list, struct list_head *prev, 260219820Sjeff struct list_head *next) 261219820Sjeff{ 262219820Sjeff struct list_head *first; 263219820Sjeff struct list_head *last; 264219820Sjeff 265219820Sjeff if (list_empty(list)) 266219820Sjeff return; 267219820Sjeff first = list->next; 268219820Sjeff last = list->prev; 269219820Sjeff first->prev = prev; 270219820Sjeff prev->next = first; 271219820Sjeff last->next = next; 272219820Sjeff next->prev = last; 273219820Sjeff} 274219820Sjeff 275219820Sjeffstatic inline void 276219820Sjefflist_splice(const struct list_head *list, struct list_head *head) 277219820Sjeff{ 278219820Sjeff 279294837Shselasky linux_list_splice(list, head, head->next); 280219820Sjeff} 281219820Sjeff 282219820Sjeffstatic inline void 283219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head) 284219820Sjeff{ 285219820Sjeff 286294837Shselasky linux_list_splice(list, head->prev, head); 287219820Sjeff} 288219820Sjeff 289219820Sjeffstatic inline void 290219820Sjefflist_splice_init(struct list_head *list, struct list_head *head) 291219820Sjeff{ 292219820Sjeff 293294837Shselasky linux_list_splice(list, head, head->next); 294219820Sjeff INIT_LIST_HEAD(list); 295219820Sjeff} 296219820Sjeff 297219820Sjeffstatic inline void 298219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head) 299219820Sjeff{ 300219820Sjeff 301294837Shselasky linux_list_splice(list, head->prev, head); 302219820Sjeff INIT_LIST_HEAD(list); 303219820Sjeff} 304219820Sjeff 305219820Sjeff#undef LIST_HEAD 306219820Sjeff#define LIST_HEAD(name) struct list_head name = { &(name), &(name) } 307219820Sjeff 308219820Sjeff 309219820Sjeffstruct hlist_head { 310219820Sjeff struct hlist_node *first; 311219820Sjeff}; 312219820Sjeff 313219820Sjeffstruct hlist_node { 314219820Sjeff struct hlist_node *next, **pprev; 315219820Sjeff}; 316219820Sjeff 317219820Sjeff#define HLIST_HEAD_INIT { } 318219820Sjeff#define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 319219820Sjeff#define INIT_HLIST_HEAD(head) (head)->first = NULL 320219820Sjeff#define INIT_HLIST_NODE(node) \ 321219820Sjeffdo { \ 322219820Sjeff (node)->next = NULL; \ 323219820Sjeff (node)->pprev = NULL; \ 324219820Sjeff} while (0) 325219820Sjeff 326219820Sjeffstatic inline int 327219820Sjeffhlist_unhashed(const struct hlist_node *h) 328219820Sjeff{ 329219820Sjeff 330219820Sjeff return !h->pprev; 331219820Sjeff} 332219820Sjeff 333219820Sjeffstatic inline int 334219820Sjeffhlist_empty(const struct hlist_head *h) 335219820Sjeff{ 336219820Sjeff 337219820Sjeff return !h->first; 338219820Sjeff} 339219820Sjeff 340219820Sjeffstatic inline void 341219820Sjeffhlist_del(struct hlist_node *n) 342219820Sjeff{ 343219820Sjeff 344219820Sjeff if (n->next) 345219820Sjeff n->next->pprev = n->pprev; 346219820Sjeff *n->pprev = n->next; 347219820Sjeff} 348219820Sjeff 349219820Sjeffstatic inline void 350219820Sjeffhlist_del_init(struct hlist_node *n) 351219820Sjeff{ 352219820Sjeff 353219820Sjeff if (hlist_unhashed(n)) 354219820Sjeff return; 355219820Sjeff hlist_del(n); 356219820Sjeff INIT_HLIST_NODE(n); 357219820Sjeff} 358219820Sjeff 359219820Sjeffstatic inline void 360219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h) 361219820Sjeff{ 362219820Sjeff 363219820Sjeff n->next = h->first; 364219820Sjeff if (h->first) 365219820Sjeff h->first->pprev = &n->next; 366219820Sjeff h->first = n; 367219820Sjeff n->pprev = &h->first; 368219820Sjeff} 369219820Sjeff 370219820Sjeffstatic inline void 371219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next) 372219820Sjeff{ 373219820Sjeff 374219820Sjeff n->pprev = next->pprev; 375219820Sjeff n->next = next; 376219820Sjeff next->pprev = &n->next; 377219820Sjeff *(n->pprev) = n; 378219820Sjeff} 379219820Sjeff 380219820Sjeffstatic inline void 381219820Sjeffhlist_add_after(struct hlist_node *n, struct hlist_node *next) 382219820Sjeff{ 383219820Sjeff 384219820Sjeff next->next = n->next; 385219820Sjeff n->next = next; 386219820Sjeff next->pprev = &n->next; 387219820Sjeff if (next->next) 388219820Sjeff next->next->pprev = &next->next; 389219820Sjeff} 390219820Sjeff 391219820Sjeffstatic inline void 392219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new) 393219820Sjeff{ 394219820Sjeff 395219820Sjeff new->first = old->first; 396219820Sjeff if (new->first) 397219820Sjeff new->first->pprev = &new->first; 398219820Sjeff old->first = NULL; 399219820Sjeff} 400270710Shselasky 401270710Shselaskystatic inline int list_is_singular(const struct list_head *head) 402270710Shselasky{ 403270710Shselasky return !list_empty(head) && (head->next == head->prev); 404270710Shselasky} 405270710Shselasky 406270710Shselaskystatic inline void __list_cut_position(struct list_head *list, 407270710Shselasky struct list_head *head, struct list_head *entry) 408270710Shselasky{ 409270710Shselasky struct list_head *new_first = entry->next; 410270710Shselasky list->next = head->next; 411270710Shselasky list->next->prev = list; 412270710Shselasky list->prev = entry; 413270710Shselasky entry->next = list; 414270710Shselasky head->next = new_first; 415270710Shselasky new_first->prev = head; 416270710Shselasky} 417270710Shselasky 418270710Shselaskystatic inline void list_cut_position(struct list_head *list, 419270710Shselasky struct list_head *head, struct list_head *entry) 420270710Shselasky{ 421270710Shselasky if (list_empty(head)) 422270710Shselasky return; 423270710Shselasky if (list_is_singular(head) && 424270710Shselasky (head->next != entry && head != entry)) 425270710Shselasky return; 426270710Shselasky if (entry == head) 427270710Shselasky INIT_LIST_HEAD(list); 428270710Shselasky else 429270710Shselasky __list_cut_position(list, head, entry); 430270710Shselasky} 431270710Shselasky 432270710Shselaskystatic inline int list_is_last(const struct list_head *list, 433270710Shselasky const struct list_head *head) 434270710Shselasky{ 435270710Shselasky return list->next == head; 436270710Shselasky} 437219820Sjeff 438219820Sjeff#define hlist_entry(ptr, type, field) container_of(ptr, type, field) 439219820Sjeff 440219820Sjeff#define hlist_for_each(p, head) \ 441294837Shselasky for (p = (head)->first; p; p = (p)->next) 442219820Sjeff 443219820Sjeff#define hlist_for_each_safe(p, n, head) \ 444294837Shselasky for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n) 445219820Sjeff 446289574Shselasky#define hlist_entry_safe(ptr, type, member) \ 447289574Shselasky ((ptr) ? hlist_entry(ptr, type, member) : NULL) 448289574Shselasky 449289574Shselasky#define hlist_for_each_entry(pos, head, member) \ 450289574Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 451289574Shselasky pos; \ 452289574Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 453289574Shselasky 454294837Shselasky#define hlist_for_each_entry_continue(pos, member) \ 455294837Shselasky for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \ 456294837Shselasky (pos); \ 457294837Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 458219820Sjeff 459294837Shselasky#define hlist_for_each_entry_from(pos, member) \ 460294837Shselasky for (; (pos); \ 461294837Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 462219820Sjeff 463294837Shselasky#define hlist_for_each_entry_safe(pos, n, head, member) \ 464294837Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \ 465294837Shselasky (pos) && ({ n = (pos)->member.next; 1; }); \ 466294837Shselasky pos = hlist_entry_safe(n, typeof(*(pos)), member)) 467219820Sjeff 468300496Shselaskyextern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 469300496Shselasky struct list_head *a, struct list_head *b)); 470300496Shselasky 471219820Sjeff#endif /* _LINUX_LIST_H_ */ 472