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 * Copyright (c) 2015-2019 François Tigeot <ftigeot@wolfpond.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifndef _LINUX_LIST_H_ 31 #define _LINUX_LIST_H_ 32 33 /* 34 * Since LIST_HEAD conflicts with the linux definition we must include any 35 * FreeBSD header which requires it here so it is resolved with the correct 36 * definition prior to the undef. 37 */ 38 #include <linux/types.h> 39 #include <linux/stddef.h> 40 #include <linux/kernel.h> 41 42 #include <sys/param.h> 43 #include <sys/kernel.h> 44 #include <sys/queue.h> 45 #include <sys/jail.h> 46 #include <sys/lock.h> 47 #include <sys/mutex.h> 48 #include <sys/proc.h> 49 #include <sys/conf.h> 50 #include <sys/socket.h> 51 #include <sys/mbuf.h> 52 53 #include <net/bpf.h> 54 #include <net/if.h> 55 #include <net/if_var.h> 56 #include <net/if_types.h> 57 #include <net/if_media.h> 58 59 #include <netinet/in.h> 60 #include <netinet/in_pcb.h> 61 #include <netinet/in_var.h> 62 63 #include <netinet6/in6_var.h> 64 #include <netinet6/nd6.h> 65 66 #include <vm/vm.h> 67 #include <vm/vm_object.h> 68 69 #define prefetch(x) 70 71 struct list_head { 72 struct list_head *next; 73 struct list_head *prev; 74 }; 75 76 #define LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) } 77 78 static inline void 79 INIT_LIST_HEAD(struct list_head *list) 80 { 81 82 list->next = list->prev = list; 83 } 84 85 static inline struct list_head * 86 list_first(const struct list_head *head) 87 { 88 return head->next; 89 } 90 91 static inline struct list_head * 92 list_last(const struct list_head *head) 93 { 94 return head->prev; 95 } 96 97 static inline int 98 list_empty(const struct list_head *head) 99 { 100 return (head->next == head); 101 } 102 103 static inline int 104 list_empty_careful(const struct list_head *head) 105 { 106 return (head == head->next) && (head == head->prev); 107 } 108 109 static inline void 110 list_del(struct list_head *entry) 111 { 112 113 entry->next->prev = entry->prev; 114 entry->prev->next = entry->next; 115 } 116 117 static inline void list_replace(struct list_head *old, 118 struct list_head *new) 119 { 120 new->next = old->next; 121 new->next->prev = new; 122 new->prev = old->prev; 123 new->prev->next = new; 124 } 125 126 static inline void list_replace_init(struct list_head *old, 127 struct list_head *new) 128 { 129 list_replace(old, new); 130 INIT_LIST_HEAD(old); 131 } 132 133 static inline void 134 _list_add(struct list_head *new, struct list_head *prev, 135 struct list_head *next) 136 { 137 138 next->prev = new; 139 new->next = next; 140 new->prev = prev; 141 prev->next = new; 142 } 143 144 static inline void 145 list_del_init(struct list_head *entry) 146 { 147 148 list_del(entry); 149 INIT_LIST_HEAD(entry); 150 } 151 152 #define list_entry(ptr, type, field) container_of(ptr, type, field) 153 154 #define list_first_entry(ptr, type, member) \ 155 list_entry((ptr)->next, type, member) 156 157 #define list_first_entry_or_null(ptr, type, member) \ 158 (list_empty(ptr) ? NULL: list_first_entry(ptr, type, member)) 159 160 #define list_last_entry(ptr, type, field) \ 161 list_entry(list_last((ptr)), type, field) 162 163 #define list_next_entry(ptr, member) \ 164 list_entry(((ptr)->member.next), typeof(*(ptr)), member) 165 166 #define list_prev_entry(ptr, member) \ 167 list_entry(((ptr)->member.prev), typeof(*(ptr)), member) 168 169 #define list_for_each(p, head) \ 170 for (p = (head)->next; p != (head); p = p->next) 171 172 #define list_for_each_safe(p, n, head) \ 173 for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next) 174 175 #define list_for_each_entry(p, h, field) \ 176 for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \ 177 p = list_entry(p->field.next, typeof(*p), field)) 178 179 #define list_for_each_entry_safe(p, n, h, field) \ 180 for (p = list_entry((h)->next, typeof(*p), field), \ 181 n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\ 182 p = n, n = list_entry(n->field.next, typeof(*n), field)) 183 184 #define list_for_each_entry_continue(p, h, field) \ 185 for (p = list_next_entry((p), field); &p->field != (h); \ 186 p = list_next_entry((p), field)) 187 188 #define list_for_each_entry_continue_reverse(pos, head, member) \ 189 for (pos = list_entry(pos->member.prev, __typeof(*pos), member); \ 190 &pos->member != (head); \ 191 pos = list_entry(pos->member.prev, __typeof(*pos), member)) 192 193 #define list_for_each_entry_safe_from(pos, n, head, member) \ 194 for (n = list_entry(pos->member.next, typeof(*pos), member); \ 195 &pos->member != (head); \ 196 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 197 198 #define list_for_each_entry_reverse(p, h, field) \ 199 for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \ 200 p = list_entry(p->field.prev, typeof(*p), field)) 201 202 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev) 203 204 static inline void 205 list_add(struct list_head *new, struct list_head *head) 206 { 207 208 _list_add(new, head, head->next); 209 } 210 211 static inline void 212 list_add_tail(struct list_head *new, struct list_head *head) 213 { 214 215 _list_add(new, head->prev, head); 216 } 217 218 static inline void 219 list_move(struct list_head *list, struct list_head *head) 220 { 221 222 list_del(list); 223 list_add(list, head); 224 } 225 226 static inline void 227 list_move_tail(struct list_head *entry, struct list_head *head) 228 { 229 230 list_del(entry); 231 list_add_tail(entry, head); 232 } 233 234 static inline void 235 _list_splice(const struct list_head *list, struct list_head *prev, 236 struct list_head *next) 237 { 238 struct list_head *first; 239 struct list_head *last; 240 241 if (list_empty(list)) 242 return; 243 first = list->next; 244 last = list->prev; 245 first->prev = prev; 246 prev->next = first; 247 last->next = next; 248 next->prev = last; 249 } 250 251 static inline void 252 list_splice(const struct list_head *list, struct list_head *head) 253 { 254 255 _list_splice(list, head, head->next); 256 } 257 258 static inline void 259 list_splice_tail(struct list_head *list, struct list_head *head) 260 { 261 262 _list_splice(list, head->prev, head); 263 } 264 265 static inline void 266 list_splice_init(struct list_head *list, struct list_head *head) 267 { 268 269 _list_splice(list, head, head->next); 270 INIT_LIST_HEAD(list); 271 } 272 273 static inline void 274 list_splice_tail_init(struct list_head *list, struct list_head *head) 275 { 276 277 _list_splice(list, head->prev, head); 278 INIT_LIST_HEAD(list); 279 } 280 281 #define LINUX_LIST_HEAD(name) struct list_head name = { &(name), &(name) } 282 283 284 struct hlist_head { 285 struct hlist_node *first; 286 }; 287 288 struct hlist_node { 289 struct hlist_node *next, **pprev; 290 }; 291 292 #define HLIST_HEAD_INIT { } 293 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 294 #define INIT_HLIST_HEAD(head) (head)->first = NULL 295 #define INIT_HLIST_NODE(node) \ 296 do { \ 297 (node)->next = NULL; \ 298 (node)->pprev = NULL; \ 299 } while (0) 300 301 static inline int 302 hlist_unhashed(const struct hlist_node *h) 303 { 304 305 return !h->pprev; 306 } 307 308 static inline int 309 hlist_empty(const struct hlist_head *h) 310 { 311 312 return !h->first; 313 } 314 315 static inline void 316 hlist_del(struct hlist_node *n) 317 { 318 319 if (n->next) 320 n->next->pprev = n->pprev; 321 *n->pprev = n->next; 322 } 323 324 static inline void 325 hlist_del_init(struct hlist_node *n) 326 { 327 328 if (hlist_unhashed(n)) 329 return; 330 hlist_del(n); 331 INIT_HLIST_NODE(n); 332 } 333 334 static inline void 335 hlist_add_head(struct hlist_node *n, struct hlist_head *h) 336 { 337 338 n->next = h->first; 339 if (h->first) 340 h->first->pprev = &n->next; 341 h->first = n; 342 n->pprev = &h->first; 343 } 344 345 static inline void 346 hlist_add_before(struct hlist_node *n, struct hlist_node *next) 347 { 348 349 n->pprev = next->pprev; 350 n->next = next; 351 next->pprev = &n->next; 352 *(n->pprev) = n; 353 } 354 355 static inline void 356 hlist_add_after(struct hlist_node *n, struct hlist_node *next) 357 { 358 359 next->next = n->next; 360 n->next = next; 361 next->pprev = &n->next; 362 if (next->next) 363 next->next->pprev = &next->next; 364 } 365 366 static inline void 367 hlist_move_list(struct hlist_head *old, struct hlist_head *new) 368 { 369 370 new->first = old->first; 371 if (new->first) 372 new->first->pprev = &new->first; 373 old->first = NULL; 374 } 375 376 /** 377 * list_is_singular - tests whether a list has just one entry. 378 * @head: the list to test. 379 */ 380 static inline int list_is_singular(const struct list_head *head) 381 { 382 return !list_empty(head) && (head->next == head->prev); 383 } 384 385 static inline void __list_cut_position(struct list_head *list, 386 struct list_head *head, struct list_head *entry) 387 { 388 struct list_head *new_first = entry->next; 389 list->next = head->next; 390 list->next->prev = list; 391 list->prev = entry; 392 entry->next = list; 393 head->next = new_first; 394 new_first->prev = head; 395 } 396 397 /** 398 * list_cut_position - cut a list into two 399 * @list: a new list to add all removed entries 400 * @head: a list with entries 401 * @entry: an entry within head, could be the head itself 402 * and if so we won't cut the list 403 * 404 * This helper moves the initial part of @head, up to and 405 * including @entry, from @head to @list. You should 406 * pass on @entry an element you know is on @head. @list 407 * should be an empty list or a list you do not care about 408 * losing its data. 409 * 410 */ 411 static inline void list_cut_position(struct list_head *list, 412 struct list_head *head, struct list_head *entry) 413 { 414 if (list_empty(head)) 415 return; 416 if (list_is_singular(head) && 417 (head->next != entry && head != entry)) 418 return; 419 if (entry == head) 420 INIT_LIST_HEAD(list); 421 else 422 __list_cut_position(list, head, entry); 423 } 424 425 /** 426 * list_is_last - tests whether @list is the last entry in list @head 427 * @list: the entry to test 428 * @head: the head of the list 429 */ 430 static inline int list_is_last(const struct list_head *list, 431 const struct list_head *head) 432 { 433 return list->next == head; 434 } 435 436 #define hlist_entry(ptr, type, field) container_of(ptr, type, field) 437 438 #define hlist_for_each(p, head) \ 439 for (p = (head)->first; p; p = p->next) 440 441 #define hlist_for_each_safe(p, n, head) \ 442 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n) 443 444 #define hlist_entry_safe(ptr, type, member) \ 445 (ptr) ? hlist_entry(ptr, type, member) : NULL 446 447 #define hlist_for_each_entry(pos, head, member) \ 448 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 449 pos; \ 450 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 451 452 #define hlist_for_each_entry_continue(tp, p, field) \ 453 for (p = (p)->next; \ 454 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 455 456 #define hlist_for_each_entry_from(tp, p, field) \ 457 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 458 459 #define hlist_for_each_entry_safe(pos, n, head, member) \ 460 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \ 461 (pos) && ({ n = (pos)->member.next; 1; }); \ 462 pos = hlist_entry_safe(n, typeof(*(pos)), member)) 463 464 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 465 struct list_head *a, struct list_head *b)); 466 467 #define hlist_add_head_rcu(n, h) hlist_add_head(n, h) 468 469 #define hlist_del_init_rcu(n) hlist_del_init(n) 470 471 #endif /* _LINUX_LIST_H_ */ 472