7 * container_of - cast a member of a structure out to the containing structure
8 * @ptr: the pointer to the member.
9 * @type: the type of the container struct this is embedded in.
10 * @member: the name of the member within the struct.
13 #define container_of(ptr, type, member) ({ \
14 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
15 (type *)( (char *)__mptr - offsetof(type,member) );})
18 # define POISON_POINTER_DELTA 0
20 * These are non-NULL pointers that will result in page faults
21 * under normal circumstances, used to verify that nobody uses
22 * non-initialized list entries.
24 #define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
25 #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
27 struct list_head *next, *prev;
30 struct hlist_node *first;
34 struct hlist_node *next, **pprev;
38 * Structure used to create a linked list of streams that share the same lookup
39 * table entry. This structure may be embedded in either a dentry (for the
40 * un-named data stream) or an ads_entry (for an alternate data stream). The
41 * @type field indicates which of these structures the stream_list_head is
44 struct stream_list_head {
45 struct list_head list;
47 STREAM_TYPE_NORMAL = 0,
53 * Simple doubly linked list implementation.
55 * Some of the internal functions ("__xxx") are useful when
56 * manipulating whole lists rather than single entries, as
57 * sometimes we already know the next/prev entries and we can
58 * generate better code by using them directly rather than
59 * using the generic single-entry routines.
62 #define LIST_HEAD_INIT(name) { &(name), &(name) }
64 #define LIST_HEAD(name) \
65 struct list_head name = LIST_HEAD_INIT(name)
67 static inline void INIT_LIST_HEAD(struct list_head *list)
74 * Insert a new entry between two known consecutive entries.
76 * This is only for internal list manipulation where we know
77 * the prev/next entries already!
79 #ifndef CONFIG_DEBUG_LIST
80 static inline void __list_add(struct list_head *new,
81 struct list_head *prev,
82 struct list_head *next)
90 extern void __list_add(struct list_head *new,
91 struct list_head *prev,
92 struct list_head *next);
96 * list_add - add a new entry
97 * @new: new entry to be added
98 * @head: list head to add it after
100 * Insert a new entry after the specified head.
101 * This is good for implementing stacks.
103 static inline void list_add(struct list_head *new, struct list_head *head)
105 __list_add(new, head, head->next);
110 * list_add_tail - add a new entry
111 * @new: new entry to be added
112 * @head: list head to add it before
114 * Insert a new entry before the specified head.
115 * This is useful for implementing queues.
117 static inline void list_add_tail(struct list_head *new, struct list_head *head)
119 __list_add(new, head->prev, head);
123 * Delete a list entry by making the prev/next entries
124 * point to each other.
126 * This is only for internal list manipulation where we know
127 * the prev/next entries already!
129 static inline void __list_del(struct list_head * prev, struct list_head * next)
136 * list_del - deletes entry from list.
137 * @entry: the element to delete from the list.
138 * Note: list_empty() on entry does not return true after this, the entry is
139 * in an undefined state.
141 #ifndef CONFIG_DEBUG_LIST
142 static inline void __list_del_entry(struct list_head *entry)
144 __list_del(entry->prev, entry->next);
147 static inline void list_del(struct list_head *entry)
149 __list_del(entry->prev, entry->next);
150 entry->next = LIST_POISON1;
151 entry->prev = LIST_POISON2;
154 extern void __list_del_entry(struct list_head *entry);
155 extern void list_del(struct list_head *entry);
159 * list_replace - replace old entry by new one
160 * @old : the element to be replaced
161 * @new : the new element to insert
163 * If @old was empty, it will be overwritten.
165 static inline void list_replace(struct list_head *old,
166 struct list_head *new)
168 new->next = old->next;
169 new->next->prev = new;
170 new->prev = old->prev;
171 new->prev->next = new;
174 static inline void list_replace_init(struct list_head *old,
175 struct list_head *new)
177 list_replace(old, new);
182 * list_del_init - deletes entry from list and reinitialize it.
183 * @entry: the element to delete from the list.
185 static inline void list_del_init(struct list_head *entry)
187 __list_del_entry(entry);
188 INIT_LIST_HEAD(entry);
192 * list_move - delete from one list and add as another's head
193 * @list: the entry to move
194 * @head: the head that will precede our entry
196 static inline void list_move(struct list_head *list, struct list_head *head)
198 __list_del_entry(list);
199 list_add(list, head);
203 * list_move_tail - delete from one list and add as another's tail
204 * @list: the entry to move
205 * @head: the head that will follow our entry
207 static inline void list_move_tail(struct list_head *list,
208 struct list_head *head)
210 __list_del_entry(list);
211 list_add_tail(list, head);
215 * list_is_last - tests whether @list is the last entry in list @head
216 * @list: the entry to test
217 * @head: the head of the list
219 static inline int list_is_last(const struct list_head *list,
220 const struct list_head *head)
222 return list->next == head;
226 * list_empty - tests whether a list is empty
227 * @head: the list to test.
229 static inline int list_empty(const struct list_head *head)
231 return head->next == head;
235 * list_empty_careful - tests whether a list is empty and not being modified
236 * @head: the list to test
239 * tests whether a list is empty _and_ checks that no other CPU might be
240 * in the process of modifying either member (next or prev)
242 * NOTE: using list_empty_careful() without synchronization
243 * can only be safe if the only activity that can happen
244 * to the list entry is list_del_init(). Eg. it cannot be used
245 * if another CPU could re-list_add() it.
247 static inline int list_empty_careful(const struct list_head *head)
249 struct list_head *next = head->next;
250 return (next == head) && (next == head->prev);
254 * list_rotate_left - rotate the list to the left
255 * @head: the head of the list
257 static inline void list_rotate_left(struct list_head *head)
259 struct list_head *first;
261 if (!list_empty(head)) {
263 list_move_tail(first, head);
268 * list_is_singular - tests whether a list has just one entry.
269 * @head: the list to test.
271 static inline int list_is_singular(const struct list_head *head)
273 return !list_empty(head) && (head->next == head->prev);
276 static inline void __list_cut_position(struct list_head *list,
277 struct list_head *head, struct list_head *entry)
279 struct list_head *new_first = entry->next;
280 list->next = head->next;
281 list->next->prev = list;
284 head->next = new_first;
285 new_first->prev = head;
289 * list_cut_position - cut a list into two
290 * @list: a new list to add all removed entries
291 * @head: a list with entries
292 * @entry: an entry within head, could be the head itself
293 * and if so we won't cut the list
295 * This helper moves the initial part of @head, up to and
296 * including @entry, from @head to @list. You should
297 * pass on @entry an element you know is on @head. @list
298 * should be an empty list or a list you do not care about
302 static inline void list_cut_position(struct list_head *list,
303 struct list_head *head, struct list_head *entry)
305 if (list_empty(head))
307 if (list_is_singular(head) &&
308 (head->next != entry && head != entry))
311 INIT_LIST_HEAD(list);
313 __list_cut_position(list, head, entry);
316 static inline void __list_splice(const struct list_head *list,
317 struct list_head *prev,
318 struct list_head *next)
320 struct list_head *first = list->next;
321 struct list_head *last = list->prev;
331 * list_splice - join two lists, this is designed for stacks
332 * @list: the new list to add.
333 * @head: the place to add it in the first list.
335 static inline void list_splice(const struct list_head *list,
336 struct list_head *head)
338 if (!list_empty(list))
339 __list_splice(list, head, head->next);
343 * list_splice_tail - join two lists, each list being a queue
344 * @list: the new list to add.
345 * @head: the place to add it in the first list.
347 static inline void list_splice_tail(struct list_head *list,
348 struct list_head *head)
350 if (!list_empty(list))
351 __list_splice(list, head->prev, head);
355 * list_splice_init - join two lists and reinitialise the emptied list.
356 * @list: the new list to add.
357 * @head: the place to add it in the first list.
359 * The list at @list is reinitialised
361 static inline void list_splice_init(struct list_head *list,
362 struct list_head *head)
364 if (!list_empty(list)) {
365 __list_splice(list, head, head->next);
366 INIT_LIST_HEAD(list);
371 * list_splice_tail_init - join two lists and reinitialise the emptied list
372 * @list: the new list to add.
373 * @head: the place to add it in the first list.
375 * Each of the lists is a queue.
376 * The list at @list is reinitialised
378 static inline void list_splice_tail_init(struct list_head *list,
379 struct list_head *head)
381 if (!list_empty(list)) {
382 __list_splice(list, head->prev, head);
383 INIT_LIST_HEAD(list);
388 * list_entry - get the struct for this entry
389 * @ptr: the &struct list_head pointer.
390 * @type: the type of the struct this is embedded in.
391 * @member: the name of the list_struct within the struct.
393 #define list_entry(ptr, type, member) \
394 container_of(ptr, type, member)
397 * list_first_entry - get the first element from a list
398 * @ptr: the list head to take the element from.
399 * @type: the type of the struct this is embedded in.
400 * @member: the name of the list_struct within the struct.
402 * Note, that list is expected to be not empty.
404 #define list_first_entry(ptr, type, member) \
405 list_entry((ptr)->next, type, member)
408 * list_for_each - iterate over a list
409 * @pos: the &struct list_head to use as a loop cursor.
410 * @head: the head for your list.
412 #define list_for_each(pos, head) \
413 for (pos = (head)->next; pos != (head); pos = pos->next)
416 * __list_for_each - iterate over a list
417 * @pos: the &struct list_head to use as a loop cursor.
418 * @head: the head for your list.
420 * This variant doesn't differ from list_for_each() any more.
421 * We don't do prefetching in either case.
423 #define __list_for_each(pos, head) \
424 for (pos = (head)->next; pos != (head); pos = pos->next)
427 * list_for_each_prev - iterate over a list backwards
428 * @pos: the &struct list_head to use as a loop cursor.
429 * @head: the head for your list.
431 #define list_for_each_prev(pos, head) \
432 for (pos = (head)->prev; pos != (head); pos = pos->prev)
435 * list_for_each_safe - iterate over a list safe against removal of list entry
436 * @pos: the &struct list_head to use as a loop cursor.
437 * @n: another &struct list_head to use as temporary storage
438 * @head: the head for your list.
440 #define list_for_each_safe(pos, n, head) \
441 for (pos = (head)->next, n = pos->next; pos != (head); \
442 pos = n, n = pos->next)
445 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
446 * @pos: the &struct list_head to use as a loop cursor.
447 * @n: another &struct list_head to use as temporary storage
448 * @head: the head for your list.
450 #define list_for_each_prev_safe(pos, n, head) \
451 for (pos = (head)->prev, n = pos->prev; \
453 pos = n, n = pos->prev)
456 * list_for_each_entry - iterate over list of given type
457 * @pos: the type * to use as a loop cursor.
458 * @head: the head for your list.
459 * @member: the name of the list_struct within the struct.
461 #define list_for_each_entry(pos, head, member) \
462 for (pos = list_entry((head)->next, typeof(*pos), member); \
463 &pos->member != (head); \
464 pos = list_entry(pos->member.next, typeof(*pos), member))
467 * list_for_each_entry_reverse - iterate backwards over list of given type.
468 * @pos: the type * to use as a loop cursor.
469 * @head: the head for your list.
470 * @member: the name of the list_struct within the struct.
472 #define list_for_each_entry_reverse(pos, head, member) \
473 for (pos = list_entry((head)->prev, typeof(*pos), member); \
474 &pos->member != (head); \
475 pos = list_entry(pos->member.prev, typeof(*pos), member))
478 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
479 * @pos: the type * to use as a start point
480 * @head: the head of the list
481 * @member: the name of the list_struct within the struct.
483 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
485 #define list_prepare_entry(pos, head, member) \
486 ((pos) ? : list_entry(head, typeof(*pos), member))
489 * list_for_each_entry_continue - continue iteration over list of given type
490 * @pos: the type * to use as a loop cursor.
491 * @head: the head for your list.
492 * @member: the name of the list_struct within the struct.
494 * Continue to iterate over list of given type, continuing after
495 * the current position.
497 #define list_for_each_entry_continue(pos, head, member) \
498 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
499 &pos->member != (head); \
500 pos = list_entry(pos->member.next, typeof(*pos), member))
503 * list_for_each_entry_continue_reverse - iterate backwards from the given point
504 * @pos: the type * to use as a loop cursor.
505 * @head: the head for your list.
506 * @member: the name of the list_struct within the struct.
508 * Start to iterate over list of given type backwards, continuing after
509 * the current position.
511 #define list_for_each_entry_continue_reverse(pos, head, member) \
512 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
513 &pos->member != (head); \
514 pos = list_entry(pos->member.prev, typeof(*pos), member))
517 * list_for_each_entry_from - iterate over list of given type from the current point
518 * @pos: the type * to use as a loop cursor.
519 * @head: the head for your list.
520 * @member: the name of the list_struct within the struct.
522 * Iterate over list of given type, continuing from current position.
524 #define list_for_each_entry_from(pos, head, member) \
525 for (; &pos->member != (head); \
526 pos = list_entry(pos->member.next, typeof(*pos), member))
529 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
530 * @pos: the type * to use as a loop cursor.
531 * @n: another type * to use as temporary storage
532 * @head: the head for your list.
533 * @member: the name of the list_struct within the struct.
535 #define list_for_each_entry_safe(pos, n, head, member) \
536 for (pos = list_entry((head)->next, typeof(*pos), member), \
537 n = list_entry(pos->member.next, typeof(*pos), member); \
538 &pos->member != (head); \
539 pos = n, n = list_entry(n->member.next, typeof(*n), member))
542 * list_for_each_entry_safe_continue - continue list iteration safe against removal
543 * @pos: the type * to use as a loop cursor.
544 * @n: another type * to use as temporary storage
545 * @head: the head for your list.
546 * @member: the name of the list_struct within the struct.
548 * Iterate over list of given type, continuing after current point,
549 * safe against removal of list entry.
551 #define list_for_each_entry_safe_continue(pos, n, head, member) \
552 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
553 n = list_entry(pos->member.next, typeof(*pos), member); \
554 &pos->member != (head); \
555 pos = n, n = list_entry(n->member.next, typeof(*n), member))
558 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
559 * @pos: the type * to use as a loop cursor.
560 * @n: another type * to use as temporary storage
561 * @head: the head for your list.
562 * @member: the name of the list_struct within the struct.
564 * Iterate over list of given type from current point, safe against
565 * removal of list entry.
567 #define list_for_each_entry_safe_from(pos, n, head, member) \
568 for (n = list_entry(pos->member.next, typeof(*pos), member); \
569 &pos->member != (head); \
570 pos = n, n = list_entry(n->member.next, typeof(*n), member))
573 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
574 * @pos: the type * to use as a loop cursor.
575 * @n: another type * to use as temporary storage
576 * @head: the head for your list.
577 * @member: the name of the list_struct within the struct.
579 * Iterate backwards over list of given type, safe against removal
582 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
583 for (pos = list_entry((head)->prev, typeof(*pos), member), \
584 n = list_entry(pos->member.prev, typeof(*pos), member); \
585 &pos->member != (head); \
586 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
589 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
590 * @pos: the loop cursor used in the list_for_each_entry_safe loop
591 * @n: temporary storage used in list_for_each_entry_safe
592 * @member: the name of the list_struct within the struct.
594 * list_safe_reset_next is not safe to use in general if the list may be
595 * modified concurrently (eg. the lock is dropped in the loop body). An
596 * exception to this is if the cursor element (pos) is pinned in the list,
597 * and list_safe_reset_next is called after re-taking the lock and before
598 * completing the current iteration of the loop body.
600 #define list_safe_reset_next(pos, n, member) \
601 n = list_entry(pos->member.next, typeof(*pos), member)
604 * Double linked lists with a single pointer list head.
605 * Mostly useful for hash tables where the two pointer list head is
607 * You lose the ability to access the tail in O(1).
610 #define HLIST_HEAD_INIT { .first = NULL }
611 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
612 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
613 static inline void INIT_HLIST_NODE(struct hlist_node *h)
619 static inline int hlist_unhashed(const struct hlist_node *h)
624 static inline int hlist_empty(const struct hlist_head *h)
629 static inline void __hlist_del(struct hlist_node *n)
631 struct hlist_node *next = n->next;
632 struct hlist_node **pprev = n->pprev;
638 static inline void hlist_del(struct hlist_node *n)
641 n->next = LIST_POISON1;
642 n->pprev = LIST_POISON2;
645 static inline void hlist_del_init(struct hlist_node *n)
647 if (!hlist_unhashed(n)) {
653 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
655 struct hlist_node *first = h->first;
658 first->pprev = &n->next;
660 n->pprev = &h->first;
663 /* next must be != NULL */
664 static inline void hlist_add_before(struct hlist_node *n,
665 struct hlist_node *next)
667 n->pprev = next->pprev;
669 next->pprev = &n->next;
673 static inline void hlist_add_after(struct hlist_node *n,
674 struct hlist_node *next)
676 next->next = n->next;
678 next->pprev = &n->next;
681 next->next->pprev = &next->next;
684 /* after that we'll appear to be on some hlist and hlist_del will work */
685 static inline void hlist_add_fake(struct hlist_node *n)
691 * Move a list from one list head to another. Fixup the pprev
692 * reference of the first entry if it exists.
694 static inline void hlist_move_list(struct hlist_head *old,
695 struct hlist_head *new)
697 new->first = old->first;
699 new->first->pprev = &new->first;
703 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
705 #define hlist_for_each(pos, head) \
706 for (pos = (head)->first; pos ; pos = pos->next)
708 #define hlist_for_each_safe(pos, n, head) \
709 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
713 * hlist_for_each_entry - iterate over list of given type
714 * @tpos: the type * to use as a loop cursor.
715 * @pos: the &struct hlist_node to use as a loop cursor.
716 * @head: the head for your list.
717 * @member: the name of the hlist_node within the struct.
719 #define hlist_for_each_entry(tpos, pos, head, member) \
720 for (pos = (head)->first; \
722 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
726 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
727 * @tpos: the type * to use as a loop cursor.
728 * @pos: the &struct hlist_node to use as a loop cursor.
729 * @member: the name of the hlist_node within the struct.
731 #define hlist_for_each_entry_continue(tpos, pos, member) \
732 for (pos = (pos)->next; \
734 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
738 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
739 * @tpos: the type * to use as a loop cursor.
740 * @pos: the &struct hlist_node to use as a loop cursor.
741 * @member: the name of the hlist_node within the struct.
743 #define hlist_for_each_entry_from(tpos, pos, member) \
745 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
749 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
750 * @tpos: the type * to use as a loop cursor.
751 * @pos: the &struct hlist_node to use as a loop cursor.
752 * @n: another &struct hlist_node to use as temporary storage
753 * @head: the head for your list.
754 * @member: the name of the hlist_node within the struct.
756 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
757 for (pos = (head)->first; \
758 pos && ({ n = pos->next; 1; }) && \
759 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \