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