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