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