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