list.h revision 335427
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 335427 2018-06-20 06:50:56Z 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 231219820Sjeffstatic inline void 232219820Sjefflist_add(struct list_head *new, struct list_head *head) 233219820Sjeff{ 234219820Sjeff 235294837Shselasky linux_list_add(new, head, head->next); 236219820Sjeff} 237219820Sjeff 238219820Sjeffstatic inline void 239219820Sjefflist_add_tail(struct list_head *new, struct list_head *head) 240219820Sjeff{ 241219820Sjeff 242294837Shselasky linux_list_add(new, head->prev, head); 243219820Sjeff} 244219820Sjeff 245219820Sjeffstatic inline void 246219820Sjefflist_move(struct list_head *list, struct list_head *head) 247219820Sjeff{ 248219820Sjeff 249219820Sjeff list_del(list); 250219820Sjeff list_add(list, head); 251219820Sjeff} 252219820Sjeff 253219820Sjeffstatic inline void 254219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head) 255219820Sjeff{ 256219820Sjeff 257219820Sjeff list_del(entry); 258219820Sjeff list_add_tail(entry, head); 259219820Sjeff} 260219820Sjeff 261219820Sjeffstatic inline void 262331756Semastelinux_list_splice(const struct list_head *list, struct list_head *prev, 263219820Sjeff struct list_head *next) 264219820Sjeff{ 265219820Sjeff struct list_head *first; 266219820Sjeff struct list_head *last; 267219820Sjeff 268219820Sjeff if (list_empty(list)) 269219820Sjeff return; 270219820Sjeff first = list->next; 271219820Sjeff last = list->prev; 272219820Sjeff first->prev = prev; 273219820Sjeff prev->next = first; 274219820Sjeff last->next = next; 275219820Sjeff next->prev = last; 276219820Sjeff} 277219820Sjeff 278219820Sjeffstatic inline void 279219820Sjefflist_splice(const struct list_head *list, struct list_head *head) 280219820Sjeff{ 281219820Sjeff 282294837Shselasky linux_list_splice(list, head, head->next); 283331756Semaste} 284219820Sjeff 285219820Sjeffstatic inline void 286219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head) 287219820Sjeff{ 288219820Sjeff 289294837Shselasky linux_list_splice(list, head->prev, head); 290219820Sjeff} 291331756Semaste 292219820Sjeffstatic inline void 293219820Sjefflist_splice_init(struct list_head *list, struct list_head *head) 294219820Sjeff{ 295219820Sjeff 296294837Shselasky linux_list_splice(list, head, head->next); 297331756Semaste INIT_LIST_HEAD(list); 298219820Sjeff} 299331756Semaste 300219820Sjeffstatic inline void 301219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head) 302219820Sjeff{ 303219820Sjeff 304294837Shselasky linux_list_splice(list, head->prev, head); 305219820Sjeff INIT_LIST_HEAD(list); 306219820Sjeff} 307219820Sjeff 308219820Sjeff#undef LIST_HEAD 309219820Sjeff#define LIST_HEAD(name) struct list_head name = { &(name), &(name) } 310219820Sjeff 311219820Sjeff 312219820Sjeffstruct hlist_head { 313219820Sjeff struct hlist_node *first; 314219820Sjeff}; 315219820Sjeff 316219820Sjeffstruct hlist_node { 317219820Sjeff struct hlist_node *next, **pprev; 318219820Sjeff}; 319219820Sjeff 320219820Sjeff#define HLIST_HEAD_INIT { } 321219820Sjeff#define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 322219820Sjeff#define INIT_HLIST_HEAD(head) (head)->first = NULL 323219820Sjeff#define INIT_HLIST_NODE(node) \ 324219820Sjeffdo { \ 325219820Sjeff (node)->next = NULL; \ 326219820Sjeff (node)->pprev = NULL; \ 327219820Sjeff} while (0) 328219820Sjeff 329219820Sjeffstatic inline int 330219820Sjeffhlist_unhashed(const struct hlist_node *h) 331219820Sjeff{ 332219820Sjeff 333219820Sjeff return !h->pprev; 334219820Sjeff} 335219820Sjeff 336219820Sjeffstatic inline int 337219820Sjeffhlist_empty(const struct hlist_head *h) 338219820Sjeff{ 339219820Sjeff 340335427Shselasky return !READ_ONCE(h->first); 341219820Sjeff} 342219820Sjeff 343219820Sjeffstatic inline void 344219820Sjeffhlist_del(struct hlist_node *n) 345219820Sjeff{ 346219820Sjeff 347335427Shselasky WRITE_ONCE(*(n->pprev), n->next); 348335427Shselasky if (n->next != NULL) 349331756Semaste n->next->pprev = n->pprev; 350219820Sjeff} 351219820Sjeff 352219820Sjeffstatic inline void 353219820Sjeffhlist_del_init(struct hlist_node *n) 354219820Sjeff{ 355219820Sjeff 356219820Sjeff if (hlist_unhashed(n)) 357219820Sjeff return; 358219820Sjeff hlist_del(n); 359219820Sjeff INIT_HLIST_NODE(n); 360219820Sjeff} 361219820Sjeff 362219820Sjeffstatic inline void 363219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h) 364219820Sjeff{ 365219820Sjeff 366219820Sjeff n->next = h->first; 367335427Shselasky if (h->first != NULL) 368219820Sjeff h->first->pprev = &n->next; 369335427Shselasky WRITE_ONCE(h->first, n); 370219820Sjeff n->pprev = &h->first; 371219820Sjeff} 372219820Sjeff 373219820Sjeffstatic inline void 374219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next) 375219820Sjeff{ 376219820Sjeff 377219820Sjeff n->pprev = next->pprev; 378219820Sjeff n->next = next; 379219820Sjeff next->pprev = &n->next; 380335427Shselasky WRITE_ONCE(*(n->pprev), n); 381219820Sjeff} 382331756Semaste 383219820Sjeffstatic inline void 384335427Shselaskyhlist_add_behind(struct hlist_node *n, struct hlist_node *prev) 385219820Sjeff{ 386219820Sjeff 387335427Shselasky n->next = prev->next; 388335427Shselasky WRITE_ONCE(prev->next, n); 389335427Shselasky n->pprev = &prev->next; 390335427Shselasky 391335427Shselasky if (n->next != NULL) 392335427Shselasky n->next->pprev = &n->next; 393219820Sjeff} 394331756Semaste 395219820Sjeffstatic inline void 396219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new) 397219820Sjeff{ 398219820Sjeff 399219820Sjeff new->first = old->first; 400219820Sjeff if (new->first) 401219820Sjeff new->first->pprev = &new->first; 402219820Sjeff old->first = NULL; 403219820Sjeff} 404270710Shselasky 405270710Shselaskystatic inline int list_is_singular(const struct list_head *head) 406270710Shselasky{ 407270710Shselasky return !list_empty(head) && (head->next == head->prev); 408270710Shselasky} 409270710Shselasky 410270710Shselaskystatic inline void __list_cut_position(struct list_head *list, 411270710Shselasky struct list_head *head, struct list_head *entry) 412270710Shselasky{ 413270710Shselasky struct list_head *new_first = entry->next; 414270710Shselasky list->next = head->next; 415270710Shselasky list->next->prev = list; 416270710Shselasky list->prev = entry; 417270710Shselasky entry->next = list; 418270710Shselasky head->next = new_first; 419270710Shselasky new_first->prev = head; 420270710Shselasky} 421270710Shselasky 422270710Shselaskystatic inline void list_cut_position(struct list_head *list, 423270710Shselasky struct list_head *head, struct list_head *entry) 424270710Shselasky{ 425270710Shselasky if (list_empty(head)) 426270710Shselasky return; 427270710Shselasky if (list_is_singular(head) && 428270710Shselasky (head->next != entry && head != entry)) 429270710Shselasky return; 430270710Shselasky if (entry == head) 431270710Shselasky INIT_LIST_HEAD(list); 432270710Shselasky else 433270710Shselasky __list_cut_position(list, head, entry); 434270710Shselasky} 435270710Shselasky 436270710Shselaskystatic inline int list_is_last(const struct list_head *list, 437331756Semaste const struct list_head *head) 438270710Shselasky{ 439331756Semaste return list->next == head; 440270710Shselasky} 441331756Semaste 442219820Sjeff#define hlist_entry(ptr, type, field) container_of(ptr, type, field) 443219820Sjeff 444219820Sjeff#define hlist_for_each(p, head) \ 445294837Shselasky for (p = (head)->first; p; p = (p)->next) 446219820Sjeff 447219820Sjeff#define hlist_for_each_safe(p, n, head) \ 448294837Shselasky for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n) 449219820Sjeff 450289574Shselasky#define hlist_entry_safe(ptr, type, member) \ 451289574Shselasky ((ptr) ? hlist_entry(ptr, type, member) : NULL) 452289574Shselasky 453289574Shselasky#define hlist_for_each_entry(pos, head, member) \ 454289574Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 455289574Shselasky pos; \ 456289574Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 457289574Shselasky 458294837Shselasky#define hlist_for_each_entry_continue(pos, member) \ 459294837Shselasky for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \ 460294837Shselasky (pos); \ 461294837Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 462219820Sjeff 463294837Shselasky#define hlist_for_each_entry_from(pos, member) \ 464294837Shselasky for (; (pos); \ 465294837Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 466219820Sjeff 467294837Shselasky#define hlist_for_each_entry_safe(pos, n, head, member) \ 468294837Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \ 469294837Shselasky (pos) && ({ n = (pos)->member.next; 1; }); \ 470294837Shselasky pos = hlist_entry_safe(n, typeof(*(pos)), member)) 471219820Sjeff 472300496Shselaskyextern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 473300496Shselasky struct list_head *a, struct list_head *b)); 474300496Shselasky 475219820Sjeff#endif /* _LINUX_LIST_H_ */ 476