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