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