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(struct list_head *entry) 121 { 122 123 __list_del(entry->prev, entry->next); 124 } 125 126 static inline void 127 list_replace(struct list_head *old, struct list_head *new) 128 { 129 new->next = old->next; 130 new->next->prev = new; 131 new->prev = old->prev; 132 new->prev->next = new; 133 } 134 135 static inline void 136 list_replace_init(struct list_head *old, struct list_head *new) 137 { 138 list_replace(old, new); 139 INIT_LIST_HEAD(old); 140 } 141 142 static inline void 143 linux_list_add(struct list_head *new, struct list_head *prev, 144 struct list_head *next) 145 { 146 147 next->prev = new; 148 new->next = next; 149 new->prev = prev; 150 prev->next = new; 151 } 152 153 static inline void 154 list_del_init(struct list_head *entry) 155 { 156 157 list_del(entry); 158 INIT_LIST_HEAD(entry); 159 } 160 161 #define list_entry(ptr, type, field) container_of(ptr, type, field) 162 163 #define list_first_entry(ptr, type, member) \ 164 list_entry((ptr)->next, type, member) 165 166 #define list_last_entry(ptr, type, member) \ 167 list_entry((ptr)->prev, type, member) 168 169 #define list_first_entry_or_null(ptr, type, member) \ 170 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) 171 172 #define list_next_entry(ptr, member) \ 173 list_entry(((ptr)->member.next), typeof(*(ptr)), member) 174 175 #define list_prev_entry(ptr, member) \ 176 list_entry(((ptr)->member.prev), typeof(*(ptr)), member) 177 178 #define list_for_each(p, head) \ 179 for (p = (head)->next; p != (head); p = (p)->next) 180 181 #define list_for_each_safe(p, n, head) \ 182 for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next) 183 184 #define list_for_each_entry(p, h, field) \ 185 for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \ 186 p = list_entry((p)->field.next, typeof(*p), field)) 187 188 #define list_for_each_entry_safe(p, n, h, field) \ 189 for (p = list_entry((h)->next, typeof(*p), field), \ 190 n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\ 191 p = n, n = list_entry(n->field.next, typeof(*n), field)) 192 193 #define list_for_each_entry_from(p, h, field) \ 194 for ( ; &(p)->field != (h); \ 195 p = list_entry((p)->field.next, typeof(*p), field)) 196 197 #define list_for_each_entry_continue(p, h, field) \ 198 for (p = list_next_entry((p), field); &(p)->field != (h); \ 199 p = list_next_entry((p), field)) 200 201 #define list_for_each_entry_safe_from(pos, n, head, member) \ 202 for (n = list_entry((pos)->member.next, typeof(*pos), member); \ 203 &(pos)->member != (head); \ 204 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 205 206 #define list_for_each_entry_reverse(p, h, field) \ 207 for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \ 208 p = list_entry((p)->field.prev, typeof(*p), field)) 209 210 #define list_for_each_entry_safe_reverse(p, n, h, field) \ 211 for (p = list_entry((h)->prev, typeof(*p), field), \ 212 n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 213 p = n, n = list_entry(n->field.prev, typeof(*n), field)) 214 215 #define list_for_each_entry_continue_reverse(p, h, field) \ 216 for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \ 217 p = list_entry((p)->field.prev, typeof(*p), field)) 218 219 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev) 220 221 static inline void 222 list_add(struct list_head *new, struct list_head *head) 223 { 224 225 linux_list_add(new, head, head->next); 226 } 227 228 static inline void 229 list_add_tail(struct list_head *new, struct list_head *head) 230 { 231 232 linux_list_add(new, head->prev, head); 233 } 234 235 static inline void 236 list_move(struct list_head *list, struct list_head *head) 237 { 238 239 list_del(list); 240 list_add(list, head); 241 } 242 243 static inline void 244 list_move_tail(struct list_head *entry, struct list_head *head) 245 { 246 247 list_del(entry); 248 list_add_tail(entry, head); 249 } 250 251 static inline void 252 linux_list_splice(const struct list_head *list, struct list_head *prev, 253 struct list_head *next) 254 { 255 struct list_head *first; 256 struct list_head *last; 257 258 if (list_empty(list)) 259 return; 260 first = list->next; 261 last = list->prev; 262 first->prev = prev; 263 prev->next = first; 264 last->next = next; 265 next->prev = last; 266 } 267 268 static inline void 269 list_splice(const struct list_head *list, struct list_head *head) 270 { 271 272 linux_list_splice(list, head, head->next); 273 } 274 275 static inline void 276 list_splice_tail(struct list_head *list, struct list_head *head) 277 { 278 279 linux_list_splice(list, head->prev, head); 280 } 281 282 static inline void 283 list_splice_init(struct list_head *list, struct list_head *head) 284 { 285 286 linux_list_splice(list, head, head->next); 287 INIT_LIST_HEAD(list); 288 } 289 290 static inline void 291 list_splice_tail_init(struct list_head *list, struct list_head *head) 292 { 293 294 linux_list_splice(list, head->prev, head); 295 INIT_LIST_HEAD(list); 296 } 297 298 #undef LIST_HEAD 299 #define LIST_HEAD(name) struct list_head name = { &(name), &(name) } 300 301 302 struct hlist_head { 303 struct hlist_node *first; 304 }; 305 306 struct hlist_node { 307 struct hlist_node *next, **pprev; 308 }; 309 310 #define HLIST_HEAD_INIT { } 311 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 312 #define INIT_HLIST_HEAD(head) (head)->first = NULL 313 #define INIT_HLIST_NODE(node) \ 314 do { \ 315 (node)->next = NULL; \ 316 (node)->pprev = NULL; \ 317 } while (0) 318 319 static inline int 320 hlist_unhashed(const struct hlist_node *h) 321 { 322 323 return !h->pprev; 324 } 325 326 static inline int 327 hlist_empty(const struct hlist_head *h) 328 { 329 330 return !h->first; 331 } 332 333 static inline void 334 hlist_del(struct hlist_node *n) 335 { 336 337 if (n->next) 338 n->next->pprev = n->pprev; 339 *n->pprev = n->next; 340 } 341 342 static inline void 343 hlist_del_init(struct hlist_node *n) 344 { 345 346 if (hlist_unhashed(n)) 347 return; 348 hlist_del(n); 349 INIT_HLIST_NODE(n); 350 } 351 352 static inline void 353 hlist_add_head(struct hlist_node *n, struct hlist_head *h) 354 { 355 356 n->next = h->first; 357 if (h->first) 358 h->first->pprev = &n->next; 359 h->first = n; 360 n->pprev = &h->first; 361 } 362 363 static inline void 364 hlist_add_before(struct hlist_node *n, struct hlist_node *next) 365 { 366 367 n->pprev = next->pprev; 368 n->next = next; 369 next->pprev = &n->next; 370 *(n->pprev) = n; 371 } 372 373 static inline void 374 hlist_add_after(struct hlist_node *n, struct hlist_node *next) 375 { 376 377 next->next = n->next; 378 n->next = next; 379 next->pprev = &n->next; 380 if (next->next) 381 next->next->pprev = &next->next; 382 } 383 384 static inline void 385 hlist_move_list(struct hlist_head *old, struct hlist_head *new) 386 { 387 388 new->first = old->first; 389 if (new->first) 390 new->first->pprev = &new->first; 391 old->first = NULL; 392 } 393 394 /** 395 * list_is_singular - tests whether a list has just one entry. 396 * @head: the list to test. 397 */ 398 static inline int list_is_singular(const struct list_head *head) 399 { 400 return !list_empty(head) && (head->next == head->prev); 401 } 402 403 static inline void __list_cut_position(struct list_head *list, 404 struct list_head *head, struct list_head *entry) 405 { 406 struct list_head *new_first = entry->next; 407 list->next = head->next; 408 list->next->prev = list; 409 list->prev = entry; 410 entry->next = list; 411 head->next = new_first; 412 new_first->prev = head; 413 } 414 415 /** 416 * list_cut_position - cut a list into two 417 * @list: a new list to add all removed entries 418 * @head: a list with entries 419 * @entry: an entry within head, could be the head itself 420 * and if so we won't cut the list 421 * 422 * This helper moves the initial part of @head, up to and 423 * including @entry, from @head to @list. You should 424 * pass on @entry an element you know is on @head. @list 425 * should be an empty list or a list you do not care about 426 * losing its data. 427 * 428 */ 429 static inline void list_cut_position(struct list_head *list, 430 struct list_head *head, struct list_head *entry) 431 { 432 if (list_empty(head)) 433 return; 434 if (list_is_singular(head) && 435 (head->next != entry && head != entry)) 436 return; 437 if (entry == head) 438 INIT_LIST_HEAD(list); 439 else 440 __list_cut_position(list, head, entry); 441 } 442 443 /** 444 * list_is_last - tests whether @list is the last entry in list @head 445 * @list: the entry to test 446 * @head: the head of the list 447 */ 448 static inline int list_is_last(const struct list_head *list, 449 const struct list_head *head) 450 { 451 return list->next == head; 452 } 453 454 #define hlist_entry(ptr, type, field) container_of(ptr, type, field) 455 456 #define hlist_for_each(p, head) \ 457 for (p = (head)->first; p; p = (p)->next) 458 459 #define hlist_for_each_safe(p, n, head) \ 460 for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n) 461 462 #define hlist_entry_safe(ptr, type, member) \ 463 ((ptr) ? hlist_entry(ptr, type, member) : NULL) 464 465 #define hlist_for_each_entry(pos, head, member) \ 466 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 467 pos; \ 468 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 469 470 #define hlist_for_each_entry_continue(pos, member) \ 471 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \ 472 (pos); \ 473 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 474 475 #define hlist_for_each_entry_from(pos, member) \ 476 for (; (pos); \ 477 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 478 479 #define hlist_for_each_entry_safe(pos, n, head, member) \ 480 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \ 481 (pos) && ({ n = (pos)->member.next; 1; }); \ 482 pos = hlist_entry_safe(n, typeof(*(pos)), member)) 483 484 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 485 struct list_head *a, struct list_head *b)); 486 487 #endif /* _LINUX_LIST_H_ */ 488