list.h revision 311802
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 311802 2017-01-09 17:20:04Z 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 73219820Sjeff#define prefetch(x) 74219820Sjeff 75311802Shselasky#define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) } 76311802Shselasky 77311802Shselasky#define LINUX_LIST_HEAD(name) \ 78311802Shselasky struct list_head name = LINUX_LIST_HEAD_INIT(name) 79311802Shselasky 80311802Shselasky#ifndef LIST_HEAD_DEF 81311802Shselasky#define LIST_HEAD_DEF 82219820Sjeffstruct list_head { 83219820Sjeff struct list_head *next; 84219820Sjeff struct list_head *prev; 85219820Sjeff}; 86311802Shselasky#endif 87219820Sjeff 88219820Sjeffstatic inline void 89219820SjeffINIT_LIST_HEAD(struct list_head *list) 90219820Sjeff{ 91219820Sjeff 92219820Sjeff list->next = list->prev = list; 93219820Sjeff} 94219820Sjeff 95219820Sjeffstatic inline int 96219820Sjefflist_empty(const struct list_head *head) 97219820Sjeff{ 98219820Sjeff 99219820Sjeff return (head->next == head); 100219820Sjeff} 101219820Sjeff 102311802Shselaskystatic inline int 103311802Shselaskylist_empty_careful(const struct list_head *head) 104311802Shselasky{ 105311802Shselasky struct list_head *next = head->next; 106311802Shselasky 107311802Shselasky return ((next == head) && (next == head->prev)); 108311802Shselasky} 109311802Shselasky 110219820Sjeffstatic inline void 111311802Shselasky__list_del(struct list_head *prev, struct list_head *next) 112311802Shselasky{ 113311802Shselasky next->prev = prev; 114311802Shselasky WRITE_ONCE(prev->next, next); 115311802Shselasky} 116311802Shselasky 117311802Shselaskystatic inline void 118219820Sjefflist_del(struct list_head *entry) 119219820Sjeff{ 120219820Sjeff 121311802Shselasky __list_del(entry->prev, entry->next); 122219820Sjeff} 123219820Sjeff 124219820Sjeffstatic inline void 125289574Shselaskylist_replace(struct list_head *old, struct list_head *new) 126289574Shselasky{ 127289574Shselasky new->next = old->next; 128289574Shselasky new->next->prev = new; 129289574Shselasky new->prev = old->prev; 130289574Shselasky new->prev->next = new; 131289574Shselasky} 132289574Shselasky 133289574Shselaskystatic inline void 134300496Shselaskylist_replace_init(struct list_head *old, struct list_head *new) 135300496Shselasky{ 136300496Shselasky list_replace(old, new); 137300496Shselasky INIT_LIST_HEAD(old); 138300496Shselasky} 139300496Shselasky 140300496Shselaskystatic inline void 141294837Shselaskylinux_list_add(struct list_head *new, struct list_head *prev, 142219820Sjeff struct list_head *next) 143219820Sjeff{ 144219820Sjeff 145219820Sjeff next->prev = new; 146219820Sjeff new->next = next; 147219820Sjeff new->prev = prev; 148219820Sjeff prev->next = new; 149219820Sjeff} 150219820Sjeff 151219820Sjeffstatic inline void 152219820Sjefflist_del_init(struct list_head *entry) 153219820Sjeff{ 154219820Sjeff 155219820Sjeff list_del(entry); 156219820Sjeff INIT_LIST_HEAD(entry); 157219820Sjeff} 158219820Sjeff 159219820Sjeff#define list_entry(ptr, type, field) container_of(ptr, type, field) 160219820Sjeff 161255932Salfred#define list_first_entry(ptr, type, member) \ 162255932Salfred list_entry((ptr)->next, type, member) 163255932Salfred 164300496Shselasky#define list_last_entry(ptr, type, member) \ 165300496Shselasky list_entry((ptr)->prev, type, member) 166300496Shselasky 167300496Shselasky#define list_first_entry_or_null(ptr, type, member) \ 168300496Shselasky (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) 169300496Shselasky 170289574Shselasky#define list_next_entry(ptr, member) \ 171289574Shselasky list_entry(((ptr)->member.next), typeof(*(ptr)), member) 172289574Shselasky 173300496Shselasky#define list_prev_entry(ptr, member) \ 174300496Shselasky list_entry(((ptr)->member.prev), typeof(*(ptr)), member) 175300496Shselasky 176219820Sjeff#define list_for_each(p, head) \ 177294837Shselasky for (p = (head)->next; p != (head); p = (p)->next) 178219820Sjeff 179219820Sjeff#define list_for_each_safe(p, n, head) \ 180294837Shselasky for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next) 181219820Sjeff 182219820Sjeff#define list_for_each_entry(p, h, field) \ 183294837Shselasky for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \ 184294837Shselasky p = list_entry((p)->field.next, typeof(*p), field)) 185219820Sjeff 186219820Sjeff#define list_for_each_entry_safe(p, n, h, field) \ 187219820Sjeff for (p = list_entry((h)->next, typeof(*p), field), \ 188294837Shselasky n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\ 189219820Sjeff p = n, n = list_entry(n->field.next, typeof(*n), field)) 190219820Sjeff 191294837Shselasky#define list_for_each_entry_from(p, h, field) \ 192294837Shselasky for ( ; &(p)->field != (h); \ 193294837Shselasky p = list_entry((p)->field.next, typeof(*p), field)) 194294837Shselasky 195289574Shselasky#define list_for_each_entry_continue(p, h, field) \ 196294837Shselasky for (p = list_next_entry((p), field); &(p)->field != (h); \ 197289574Shselasky p = list_next_entry((p), field)) 198289574Shselasky 199289574Shselasky#define list_for_each_entry_safe_from(pos, n, head, member) \ 200294837Shselasky for (n = list_entry((pos)->member.next, typeof(*pos), member); \ 201294837Shselasky &(pos)->member != (head); \ 202289574Shselasky pos = n, n = list_entry(n->member.next, typeof(*n), member)) 203289574Shselasky 204219820Sjeff#define list_for_each_entry_reverse(p, h, field) \ 205294837Shselasky for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \ 206294837Shselasky p = list_entry((p)->field.prev, typeof(*p), field)) 207219820Sjeff 208311802Shselasky#define list_for_each_entry_safe_reverse(p, n, h, field) \ 209311802Shselasky for (p = list_entry((h)->prev, typeof(*p), field), \ 210311802Shselasky n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 211311802Shselasky p = n, n = list_entry(n->field.prev, typeof(*n), field)) 212311802Shselasky 213294837Shselasky#define list_for_each_entry_continue_reverse(p, h, field) \ 214294837Shselasky for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 215294837Shselasky p = list_entry((p)->field.prev, typeof(*p), field)) 216219820Sjeff 217294837Shselasky#define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev) 218294837Shselasky 219219820Sjeffstatic inline void 220219820Sjefflist_add(struct list_head *new, struct list_head *head) 221219820Sjeff{ 222219820Sjeff 223294837Shselasky linux_list_add(new, head, head->next); 224219820Sjeff} 225219820Sjeff 226219820Sjeffstatic inline void 227219820Sjefflist_add_tail(struct list_head *new, struct list_head *head) 228219820Sjeff{ 229219820Sjeff 230294837Shselasky linux_list_add(new, head->prev, head); 231219820Sjeff} 232219820Sjeff 233219820Sjeffstatic inline void 234219820Sjefflist_move(struct list_head *list, struct list_head *head) 235219820Sjeff{ 236219820Sjeff 237219820Sjeff list_del(list); 238219820Sjeff list_add(list, head); 239219820Sjeff} 240219820Sjeff 241219820Sjeffstatic inline void 242219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head) 243219820Sjeff{ 244219820Sjeff 245219820Sjeff list_del(entry); 246219820Sjeff list_add_tail(entry, head); 247219820Sjeff} 248219820Sjeff 249219820Sjeffstatic inline void 250294837Shselaskylinux_list_splice(const struct list_head *list, struct list_head *prev, 251219820Sjeff struct list_head *next) 252219820Sjeff{ 253219820Sjeff struct list_head *first; 254219820Sjeff struct list_head *last; 255219820Sjeff 256219820Sjeff if (list_empty(list)) 257219820Sjeff return; 258219820Sjeff first = list->next; 259219820Sjeff last = list->prev; 260219820Sjeff first->prev = prev; 261219820Sjeff prev->next = first; 262219820Sjeff last->next = next; 263219820Sjeff next->prev = last; 264219820Sjeff} 265219820Sjeff 266219820Sjeffstatic inline void 267219820Sjefflist_splice(const struct list_head *list, struct list_head *head) 268219820Sjeff{ 269219820Sjeff 270294837Shselasky linux_list_splice(list, head, head->next); 271219820Sjeff} 272219820Sjeff 273219820Sjeffstatic inline void 274219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head) 275219820Sjeff{ 276219820Sjeff 277294837Shselasky linux_list_splice(list, head->prev, head); 278219820Sjeff} 279219820Sjeff 280219820Sjeffstatic inline void 281219820Sjefflist_splice_init(struct list_head *list, struct list_head *head) 282219820Sjeff{ 283219820Sjeff 284294837Shselasky linux_list_splice(list, head, head->next); 285219820Sjeff INIT_LIST_HEAD(list); 286219820Sjeff} 287219820Sjeff 288219820Sjeffstatic inline void 289219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head) 290219820Sjeff{ 291219820Sjeff 292294837Shselasky linux_list_splice(list, head->prev, head); 293219820Sjeff INIT_LIST_HEAD(list); 294219820Sjeff} 295219820Sjeff 296219820Sjeff#undef LIST_HEAD 297219820Sjeff#define LIST_HEAD(name) struct list_head name = { &(name), &(name) } 298219820Sjeff 299219820Sjeff 300219820Sjeffstruct hlist_head { 301219820Sjeff struct hlist_node *first; 302219820Sjeff}; 303219820Sjeff 304219820Sjeffstruct hlist_node { 305219820Sjeff struct hlist_node *next, **pprev; 306219820Sjeff}; 307219820Sjeff 308219820Sjeff#define HLIST_HEAD_INIT { } 309219820Sjeff#define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 310219820Sjeff#define INIT_HLIST_HEAD(head) (head)->first = NULL 311219820Sjeff#define INIT_HLIST_NODE(node) \ 312219820Sjeffdo { \ 313219820Sjeff (node)->next = NULL; \ 314219820Sjeff (node)->pprev = NULL; \ 315219820Sjeff} while (0) 316219820Sjeff 317219820Sjeffstatic inline int 318219820Sjeffhlist_unhashed(const struct hlist_node *h) 319219820Sjeff{ 320219820Sjeff 321219820Sjeff return !h->pprev; 322219820Sjeff} 323219820Sjeff 324219820Sjeffstatic inline int 325219820Sjeffhlist_empty(const struct hlist_head *h) 326219820Sjeff{ 327219820Sjeff 328219820Sjeff return !h->first; 329219820Sjeff} 330219820Sjeff 331219820Sjeffstatic inline void 332219820Sjeffhlist_del(struct hlist_node *n) 333219820Sjeff{ 334219820Sjeff 335219820Sjeff if (n->next) 336219820Sjeff n->next->pprev = n->pprev; 337219820Sjeff *n->pprev = n->next; 338219820Sjeff} 339219820Sjeff 340219820Sjeffstatic inline void 341219820Sjeffhlist_del_init(struct hlist_node *n) 342219820Sjeff{ 343219820Sjeff 344219820Sjeff if (hlist_unhashed(n)) 345219820Sjeff return; 346219820Sjeff hlist_del(n); 347219820Sjeff INIT_HLIST_NODE(n); 348219820Sjeff} 349219820Sjeff 350219820Sjeffstatic inline void 351219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h) 352219820Sjeff{ 353219820Sjeff 354219820Sjeff n->next = h->first; 355219820Sjeff if (h->first) 356219820Sjeff h->first->pprev = &n->next; 357219820Sjeff h->first = n; 358219820Sjeff n->pprev = &h->first; 359219820Sjeff} 360219820Sjeff 361219820Sjeffstatic inline void 362219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next) 363219820Sjeff{ 364219820Sjeff 365219820Sjeff n->pprev = next->pprev; 366219820Sjeff n->next = next; 367219820Sjeff next->pprev = &n->next; 368219820Sjeff *(n->pprev) = n; 369219820Sjeff} 370219820Sjeff 371219820Sjeffstatic inline void 372219820Sjeffhlist_add_after(struct hlist_node *n, struct hlist_node *next) 373219820Sjeff{ 374219820Sjeff 375219820Sjeff next->next = n->next; 376219820Sjeff n->next = next; 377219820Sjeff next->pprev = &n->next; 378219820Sjeff if (next->next) 379219820Sjeff next->next->pprev = &next->next; 380219820Sjeff} 381219820Sjeff 382219820Sjeffstatic inline void 383219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new) 384219820Sjeff{ 385219820Sjeff 386219820Sjeff new->first = old->first; 387219820Sjeff if (new->first) 388219820Sjeff new->first->pprev = &new->first; 389219820Sjeff old->first = NULL; 390219820Sjeff} 391270710Shselasky 392270710Shselasky/** 393270710Shselasky * list_is_singular - tests whether a list has just one entry. 394270710Shselasky * @head: the list to test. 395270710Shselasky */ 396270710Shselaskystatic inline int list_is_singular(const struct list_head *head) 397270710Shselasky{ 398270710Shselasky return !list_empty(head) && (head->next == head->prev); 399270710Shselasky} 400270710Shselasky 401270710Shselaskystatic inline void __list_cut_position(struct list_head *list, 402270710Shselasky struct list_head *head, struct list_head *entry) 403270710Shselasky{ 404270710Shselasky struct list_head *new_first = entry->next; 405270710Shselasky list->next = head->next; 406270710Shselasky list->next->prev = list; 407270710Shselasky list->prev = entry; 408270710Shselasky entry->next = list; 409270710Shselasky head->next = new_first; 410270710Shselasky new_first->prev = head; 411270710Shselasky} 412270710Shselasky 413270710Shselasky/** 414270710Shselasky * list_cut_position - cut a list into two 415270710Shselasky * @list: a new list to add all removed entries 416270710Shselasky * @head: a list with entries 417270710Shselasky * @entry: an entry within head, could be the head itself 418270710Shselasky * and if so we won't cut the list 419270710Shselasky * 420270710Shselasky * This helper moves the initial part of @head, up to and 421270710Shselasky * including @entry, from @head to @list. You should 422270710Shselasky * pass on @entry an element you know is on @head. @list 423270710Shselasky * should be an empty list or a list you do not care about 424270710Shselasky * losing its data. 425270710Shselasky * 426270710Shselasky */ 427270710Shselaskystatic inline void list_cut_position(struct list_head *list, 428270710Shselasky struct list_head *head, struct list_head *entry) 429270710Shselasky{ 430270710Shselasky if (list_empty(head)) 431270710Shselasky return; 432270710Shselasky if (list_is_singular(head) && 433270710Shselasky (head->next != entry && head != entry)) 434270710Shselasky return; 435270710Shselasky if (entry == head) 436270710Shselasky INIT_LIST_HEAD(list); 437270710Shselasky else 438270710Shselasky __list_cut_position(list, head, entry); 439270710Shselasky} 440270710Shselasky 441270710Shselasky/** 442270710Shselasky * list_is_last - tests whether @list is the last entry in list @head 443270710Shselasky * @list: the entry to test 444270710Shselasky * @head: the head of the list 445270710Shselasky */ 446270710Shselaskystatic inline int list_is_last(const struct list_head *list, 447270710Shselasky const struct list_head *head) 448270710Shselasky{ 449270710Shselasky return list->next == head; 450270710Shselasky} 451219820Sjeff 452219820Sjeff#define hlist_entry(ptr, type, field) container_of(ptr, type, field) 453219820Sjeff 454219820Sjeff#define hlist_for_each(p, head) \ 455294837Shselasky for (p = (head)->first; p; p = (p)->next) 456219820Sjeff 457219820Sjeff#define hlist_for_each_safe(p, n, head) \ 458294837Shselasky for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n) 459219820Sjeff 460289574Shselasky#define hlist_entry_safe(ptr, type, member) \ 461289574Shselasky ((ptr) ? hlist_entry(ptr, type, member) : NULL) 462289574Shselasky 463289574Shselasky#define hlist_for_each_entry(pos, head, member) \ 464289574Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 465289574Shselasky pos; \ 466289574Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 467289574Shselasky 468294837Shselasky#define hlist_for_each_entry_continue(pos, member) \ 469294837Shselasky for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \ 470294837Shselasky (pos); \ 471294837Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 472219820Sjeff 473294837Shselasky#define hlist_for_each_entry_from(pos, member) \ 474294837Shselasky for (; (pos); \ 475294837Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 476219820Sjeff 477294837Shselasky#define hlist_for_each_entry_safe(pos, n, head, member) \ 478294837Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \ 479294837Shselasky (pos) && ({ n = (pos)->member.next; 1; }); \ 480294837Shselasky pos = hlist_entry_safe(n, typeof(*(pos)), member)) 481219820Sjeff 482300496Shselaskyextern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 483300496Shselasky struct list_head *a, struct list_head *b)); 484300496Shselasky 485219820Sjeff#endif /* _LINUX_LIST_H_ */ 486