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