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