Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[sfrench/cifs-2.6.git] / include / linux / list.h
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 #ifdef __KERNEL__
5
6 #include <linux/stddef.h>
7 #include <linux/poison.h>
8 #include <linux/prefetch.h>
9 #include <asm/system.h>
10
11 /*
12  * Simple doubly linked list implementation.
13  *
14  * Some of the internal functions ("__xxx") are useful when
15  * manipulating whole lists rather than single entries, as
16  * sometimes we already know the next/prev entries and we can
17  * generate better code by using them directly rather than
18  * using the generic single-entry routines.
19  */
20
21 struct list_head {
22         struct list_head *next, *prev;
23 };
24
25 #define LIST_HEAD_INIT(name) { &(name), &(name) }
26
27 #define LIST_HEAD(name) \
28         struct list_head name = LIST_HEAD_INIT(name)
29
30 static inline void INIT_LIST_HEAD(struct list_head *list)
31 {
32         list->next = list;
33         list->prev = list;
34 }
35
36 /*
37  * Insert a new entry between two known consecutive entries.
38  *
39  * This is only for internal list manipulation where we know
40  * the prev/next entries already!
41  */
42 #ifndef CONFIG_DEBUG_LIST
43 static inline void __list_add(struct list_head *new,
44                               struct list_head *prev,
45                               struct list_head *next)
46 {
47         next->prev = new;
48         new->next = next;
49         new->prev = prev;
50         prev->next = new;
51 }
52 #else
53 extern void __list_add(struct list_head *new,
54                               struct list_head *prev,
55                               struct list_head *next);
56 #endif
57
58 /**
59  * list_add - add a new entry
60  * @new: new entry to be added
61  * @head: list head to add it after
62  *
63  * Insert a new entry after the specified head.
64  * This is good for implementing stacks.
65  */
66 #ifndef CONFIG_DEBUG_LIST
67 static inline void list_add(struct list_head *new, struct list_head *head)
68 {
69         __list_add(new, head, head->next);
70 }
71 #else
72 extern void list_add(struct list_head *new, struct list_head *head);
73 #endif
74
75
76 /**
77  * list_add_tail - add a new entry
78  * @new: new entry to be added
79  * @head: list head to add it before
80  *
81  * Insert a new entry before the specified head.
82  * This is useful for implementing queues.
83  */
84 static inline void list_add_tail(struct list_head *new, struct list_head *head)
85 {
86         __list_add(new, head->prev, head);
87 }
88
89 /*
90  * Insert a new entry between two known consecutive entries.
91  *
92  * This is only for internal list manipulation where we know
93  * the prev/next entries already!
94  */
95 static inline void __list_add_rcu(struct list_head * new,
96                 struct list_head * prev, struct list_head * next)
97 {
98         new->next = next;
99         new->prev = prev;
100         smp_wmb();
101         next->prev = new;
102         prev->next = new;
103 }
104
105 /**
106  * list_add_rcu - add a new entry to rcu-protected list
107  * @new: new entry to be added
108  * @head: list head to add it after
109  *
110  * Insert a new entry after the specified head.
111  * This is good for implementing stacks.
112  *
113  * The caller must take whatever precautions are necessary
114  * (such as holding appropriate locks) to avoid racing
115  * with another list-mutation primitive, such as list_add_rcu()
116  * or list_del_rcu(), running on this same list.
117  * However, it is perfectly legal to run concurrently with
118  * the _rcu list-traversal primitives, such as
119  * list_for_each_entry_rcu().
120  */
121 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
122 {
123         __list_add_rcu(new, head, head->next);
124 }
125
126 /**
127  * list_add_tail_rcu - add a new entry to rcu-protected list
128  * @new: new entry to be added
129  * @head: list head to add it before
130  *
131  * Insert a new entry before the specified head.
132  * This is useful for implementing queues.
133  *
134  * The caller must take whatever precautions are necessary
135  * (such as holding appropriate locks) to avoid racing
136  * with another list-mutation primitive, such as list_add_tail_rcu()
137  * or list_del_rcu(), running on this same list.
138  * However, it is perfectly legal to run concurrently with
139  * the _rcu list-traversal primitives, such as
140  * list_for_each_entry_rcu().
141  */
142 static inline void list_add_tail_rcu(struct list_head *new,
143                                         struct list_head *head)
144 {
145         __list_add_rcu(new, head->prev, head);
146 }
147
148 /*
149  * Delete a list entry by making the prev/next entries
150  * point to each other.
151  *
152  * This is only for internal list manipulation where we know
153  * the prev/next entries already!
154  */
155 static inline void __list_del(struct list_head * prev, struct list_head * next)
156 {
157         next->prev = prev;
158         prev->next = next;
159 }
160
161 /**
162  * list_del - deletes entry from list.
163  * @entry: the element to delete from the list.
164  * Note: list_empty on entry does not return true after this, the entry is
165  * in an undefined state.
166  */
167 #ifndef CONFIG_DEBUG_LIST
168 static inline void list_del(struct list_head *entry)
169 {
170         __list_del(entry->prev, entry->next);
171         entry->next = LIST_POISON1;
172         entry->prev = LIST_POISON2;
173 }
174 #else
175 extern void list_del(struct list_head *entry);
176 #endif
177
178 /**
179  * list_del_rcu - deletes entry from list without re-initialization
180  * @entry: the element to delete from the list.
181  *
182  * Note: list_empty on entry does not return true after this,
183  * the entry is in an undefined state. It is useful for RCU based
184  * lockfree traversal.
185  *
186  * In particular, it means that we can not poison the forward
187  * pointers that may still be used for walking the list.
188  *
189  * The caller must take whatever precautions are necessary
190  * (such as holding appropriate locks) to avoid racing
191  * with another list-mutation primitive, such as list_del_rcu()
192  * or list_add_rcu(), running on this same list.
193  * However, it is perfectly legal to run concurrently with
194  * the _rcu list-traversal primitives, such as
195  * list_for_each_entry_rcu().
196  *
197  * Note that the caller is not permitted to immediately free
198  * the newly deleted entry.  Instead, either synchronize_rcu()
199  * or call_rcu() must be used to defer freeing until an RCU
200  * grace period has elapsed.
201  */
202 static inline void list_del_rcu(struct list_head *entry)
203 {
204         __list_del(entry->prev, entry->next);
205         entry->prev = LIST_POISON2;
206 }
207
208 /**
209  * list_replace - replace old entry by new one
210  * @old : the element to be replaced
211  * @new : the new element to insert
212  * Note: if 'old' was empty, it will be overwritten.
213  */
214 static inline void list_replace(struct list_head *old,
215                                 struct list_head *new)
216 {
217         new->next = old->next;
218         new->next->prev = new;
219         new->prev = old->prev;
220         new->prev->next = new;
221 }
222
223 static inline void list_replace_init(struct list_head *old,
224                                         struct list_head *new)
225 {
226         list_replace(old, new);
227         INIT_LIST_HEAD(old);
228 }
229
230 /**
231  * list_replace_rcu - replace old entry by new one
232  * @old : the element to be replaced
233  * @new : the new element to insert
234  *
235  * The @old entry will be replaced with the @new entry atomically.
236  * Note: @old should not be empty.
237  */
238 static inline void list_replace_rcu(struct list_head *old,
239                                 struct list_head *new)
240 {
241         new->next = old->next;
242         new->prev = old->prev;
243         smp_wmb();
244         new->next->prev = new;
245         new->prev->next = new;
246         old->prev = LIST_POISON2;
247 }
248
249 /**
250  * list_del_init - deletes entry from list and reinitialize it.
251  * @entry: the element to delete from the list.
252  */
253 static inline void list_del_init(struct list_head *entry)
254 {
255         __list_del(entry->prev, entry->next);
256         INIT_LIST_HEAD(entry);
257 }
258
259 /**
260  * list_move - delete from one list and add as another's head
261  * @list: the entry to move
262  * @head: the head that will precede our entry
263  */
264 static inline void list_move(struct list_head *list, struct list_head *head)
265 {
266         __list_del(list->prev, list->next);
267         list_add(list, head);
268 }
269
270 /**
271  * list_move_tail - delete from one list and add as another's tail
272  * @list: the entry to move
273  * @head: the head that will follow our entry
274  */
275 static inline void list_move_tail(struct list_head *list,
276                                   struct list_head *head)
277 {
278         __list_del(list->prev, list->next);
279         list_add_tail(list, head);
280 }
281
282 /**
283  * list_is_last - tests whether @list is the last entry in list @head
284  * @list: the entry to test
285  * @head: the head of the list
286  */
287 static inline int list_is_last(const struct list_head *list,
288                                 const struct list_head *head)
289 {
290         return list->next == head;
291 }
292
293 /**
294  * list_empty - tests whether a list is empty
295  * @head: the list to test.
296  */
297 static inline int list_empty(const struct list_head *head)
298 {
299         return head->next == head;
300 }
301
302 /**
303  * list_empty_careful - tests whether a list is empty and not being modified
304  * @head: the list to test
305  *
306  * Description:
307  * tests whether a list is empty _and_ checks that no other CPU might be
308  * in the process of modifying either member (next or prev)
309  *
310  * NOTE: using list_empty_careful() without synchronization
311  * can only be safe if the only activity that can happen
312  * to the list entry is list_del_init(). Eg. it cannot be used
313  * if another CPU could re-list_add() it.
314  */
315 static inline int list_empty_careful(const struct list_head *head)
316 {
317         struct list_head *next = head->next;
318         return (next == head) && (next == head->prev);
319 }
320
321 static inline void __list_splice(struct list_head *list,
322                                  struct list_head *head)
323 {
324         struct list_head *first = list->next;
325         struct list_head *last = list->prev;
326         struct list_head *at = head->next;
327
328         first->prev = head;
329         head->next = first;
330
331         last->next = at;
332         at->prev = last;
333 }
334
335 /**
336  * list_splice - join two lists
337  * @list: the new list to add.
338  * @head: the place to add it in the first list.
339  */
340 static inline void list_splice(struct list_head *list, struct list_head *head)
341 {
342         if (!list_empty(list))
343                 __list_splice(list, head);
344 }
345
346 /**
347  * list_splice_init - join two lists and reinitialise the emptied list.
348  * @list: the new list to add.
349  * @head: the place to add it in the first list.
350  *
351  * The list at @list is reinitialised
352  */
353 static inline void list_splice_init(struct list_head *list,
354                                     struct list_head *head)
355 {
356         if (!list_empty(list)) {
357                 __list_splice(list, head);
358                 INIT_LIST_HEAD(list);
359         }
360 }
361
362 /**
363  * list_entry - get the struct for this entry
364  * @ptr:        the &struct list_head pointer.
365  * @type:       the type of the struct this is embedded in.
366  * @member:     the name of the list_struct within the struct.
367  */
368 #define list_entry(ptr, type, member) \
369         container_of(ptr, type, member)
370
371 /**
372  * list_for_each        -       iterate over a list
373  * @pos:        the &struct list_head to use as a loop cursor.
374  * @head:       the head for your list.
375  */
376 #define list_for_each(pos, head) \
377         for (pos = (head)->next; prefetch(pos->next), pos != (head); \
378                 pos = pos->next)
379
380 /**
381  * __list_for_each      -       iterate over a list
382  * @pos:        the &struct list_head to use as a loop cursor.
383  * @head:       the head for your list.
384  *
385  * This variant differs from list_for_each() in that it's the
386  * simplest possible list iteration code, no prefetching is done.
387  * Use this for code that knows the list to be very short (empty
388  * or 1 entry) most of the time.
389  */
390 #define __list_for_each(pos, head) \
391         for (pos = (head)->next; pos != (head); pos = pos->next)
392
393 /**
394  * list_for_each_prev   -       iterate over a list backwards
395  * @pos:        the &struct list_head to use as a loop cursor.
396  * @head:       the head for your list.
397  */
398 #define list_for_each_prev(pos, head) \
399         for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
400                 pos = pos->prev)
401
402 /**
403  * list_for_each_safe - iterate over a list safe against removal of list entry
404  * @pos:        the &struct list_head to use as a loop cursor.
405  * @n:          another &struct list_head to use as temporary storage
406  * @head:       the head for your list.
407  */
408 #define list_for_each_safe(pos, n, head) \
409         for (pos = (head)->next, n = pos->next; pos != (head); \
410                 pos = n, n = pos->next)
411
412 /**
413  * list_for_each_entry  -       iterate over list of given type
414  * @pos:        the type * to use as a loop cursor.
415  * @head:       the head for your list.
416  * @member:     the name of the list_struct within the struct.
417  */
418 #define list_for_each_entry(pos, head, member)                          \
419         for (pos = list_entry((head)->next, typeof(*pos), member);      \
420              prefetch(pos->member.next), &pos->member != (head);        \
421              pos = list_entry(pos->member.next, typeof(*pos), member))
422
423 /**
424  * list_for_each_entry_reverse - iterate backwards over list of given type.
425  * @pos:        the type * to use as a loop cursor.
426  * @head:       the head for your list.
427  * @member:     the name of the list_struct within the struct.
428  */
429 #define list_for_each_entry_reverse(pos, head, member)                  \
430         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
431              prefetch(pos->member.prev), &pos->member != (head);        \
432              pos = list_entry(pos->member.prev, typeof(*pos), member))
433
434 /**
435  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
436  * @pos:        the type * to use as a start point
437  * @head:       the head of the list
438  * @member:     the name of the list_struct within the struct.
439  *
440  * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
441  */
442 #define list_prepare_entry(pos, head, member) \
443         ((pos) ? : list_entry(head, typeof(*pos), member))
444
445 /**
446  * list_for_each_entry_continue - continue iteration over list of given type
447  * @pos:        the type * to use as a loop cursor.
448  * @head:       the head for your list.
449  * @member:     the name of the list_struct within the struct.
450  *
451  * Continue to iterate over list of given type, continuing after
452  * the current position.
453  */
454 #define list_for_each_entry_continue(pos, head, member)                 \
455         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
456              prefetch(pos->member.next), &pos->member != (head);        \
457              pos = list_entry(pos->member.next, typeof(*pos), member))
458
459 /**
460  * list_for_each_entry_from - iterate over list of given type from the current point
461  * @pos:        the type * to use as a loop cursor.
462  * @head:       the head for your list.
463  * @member:     the name of the list_struct within the struct.
464  *
465  * Iterate over list of given type, continuing from current position.
466  */
467 #define list_for_each_entry_from(pos, head, member)                     \
468         for (; prefetch(pos->member.next), &pos->member != (head);      \
469              pos = list_entry(pos->member.next, typeof(*pos), member))
470
471 /**
472  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
473  * @pos:        the type * to use as a loop cursor.
474  * @n:          another type * to use as temporary storage
475  * @head:       the head for your list.
476  * @member:     the name of the list_struct within the struct.
477  */
478 #define list_for_each_entry_safe(pos, n, head, member)                  \
479         for (pos = list_entry((head)->next, typeof(*pos), member),      \
480                 n = list_entry(pos->member.next, typeof(*pos), member); \
481              &pos->member != (head);                                    \
482              pos = n, n = list_entry(n->member.next, typeof(*n), member))
483
484 /**
485  * list_for_each_entry_safe_continue
486  * @pos:        the type * to use as a loop cursor.
487  * @n:          another type * to use as temporary storage
488  * @head:       the head for your list.
489  * @member:     the name of the list_struct within the struct.
490  *
491  * Iterate over list of given type, continuing after current point,
492  * safe against removal of list entry.
493  */
494 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
495         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
496                 n = list_entry(pos->member.next, typeof(*pos), member);         \
497              &pos->member != (head);                                            \
498              pos = n, n = list_entry(n->member.next, typeof(*n), member))
499
500 /**
501  * list_for_each_entry_safe_from
502  * @pos:        the type * to use as a loop cursor.
503  * @n:          another type * to use as temporary storage
504  * @head:       the head for your list.
505  * @member:     the name of the list_struct within the struct.
506  *
507  * Iterate over list of given type from current point, safe against
508  * removal of list entry.
509  */
510 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
511         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
512              &pos->member != (head);                                            \
513              pos = n, n = list_entry(n->member.next, typeof(*n), member))
514
515 /**
516  * list_for_each_entry_safe_reverse
517  * @pos:        the type * to use as a loop cursor.
518  * @n:          another type * to use as temporary storage
519  * @head:       the head for your list.
520  * @member:     the name of the list_struct within the struct.
521  *
522  * Iterate backwards over list of given type, safe against removal
523  * of list entry.
524  */
525 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
526         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
527                 n = list_entry(pos->member.prev, typeof(*pos), member); \
528              &pos->member != (head);                                    \
529              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
530
531 /**
532  * list_for_each_rcu    -       iterate over an rcu-protected list
533  * @pos:        the &struct list_head to use as a loop cursor.
534  * @head:       the head for your list.
535  *
536  * This list-traversal primitive may safely run concurrently with
537  * the _rcu list-mutation primitives such as list_add_rcu()
538  * as long as the traversal is guarded by rcu_read_lock().
539  */
540 #define list_for_each_rcu(pos, head) \
541         for (pos = (head)->next; \
542                 prefetch(rcu_dereference(pos)->next), pos != (head); \
543                 pos = pos->next)
544
545 #define __list_for_each_rcu(pos, head) \
546         for (pos = (head)->next; \
547                 rcu_dereference(pos) != (head); \
548                 pos = pos->next)
549
550 /**
551  * list_for_each_safe_rcu
552  * @pos:        the &struct list_head to use as a loop cursor.
553  * @n:          another &struct list_head to use as temporary storage
554  * @head:       the head for your list.
555  *
556  * Iterate over an rcu-protected list, safe against removal of list entry.
557  *
558  * This list-traversal primitive may safely run concurrently with
559  * the _rcu list-mutation primitives such as list_add_rcu()
560  * as long as the traversal is guarded by rcu_read_lock().
561  */
562 #define list_for_each_safe_rcu(pos, n, head) \
563         for (pos = (head)->next; \
564                 n = rcu_dereference(pos)->next, pos != (head); \
565                 pos = n)
566
567 /**
568  * list_for_each_entry_rcu      -       iterate over rcu list of given type
569  * @pos:        the type * to use as a loop cursor.
570  * @head:       the head for your list.
571  * @member:     the name of the list_struct within the struct.
572  *
573  * This list-traversal primitive may safely run concurrently with
574  * the _rcu list-mutation primitives such as list_add_rcu()
575  * as long as the traversal is guarded by rcu_read_lock().
576  */
577 #define list_for_each_entry_rcu(pos, head, member) \
578         for (pos = list_entry((head)->next, typeof(*pos), member); \
579                 prefetch(rcu_dereference(pos)->member.next), \
580                         &pos->member != (head); \
581                 pos = list_entry(pos->member.next, typeof(*pos), member))
582
583
584 /**
585  * list_for_each_continue_rcu
586  * @pos:        the &struct list_head to use as a loop cursor.
587  * @head:       the head for your list.
588  *
589  * Iterate over an rcu-protected list, continuing after current point.
590  *
591  * This list-traversal primitive may safely run concurrently with
592  * the _rcu list-mutation primitives such as list_add_rcu()
593  * as long as the traversal is guarded by rcu_read_lock().
594  */
595 #define list_for_each_continue_rcu(pos, head) \
596         for ((pos) = (pos)->next; \
597                 prefetch(rcu_dereference((pos))->next), (pos) != (head); \
598                 (pos) = (pos)->next)
599
600 /*
601  * Double linked lists with a single pointer list head.
602  * Mostly useful for hash tables where the two pointer list head is
603  * too wasteful.
604  * You lose the ability to access the tail in O(1).
605  */
606
607 struct hlist_head {
608         struct hlist_node *first;
609 };
610
611 struct hlist_node {
612         struct hlist_node *next, **pprev;
613 };
614
615 #define HLIST_HEAD_INIT { .first = NULL }
616 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
617 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
618 static inline void INIT_HLIST_NODE(struct hlist_node *h)
619 {
620         h->next = NULL;
621         h->pprev = NULL;
622 }
623
624 static inline int hlist_unhashed(const struct hlist_node *h)
625 {
626         return !h->pprev;
627 }
628
629 static inline int hlist_empty(const struct hlist_head *h)
630 {
631         return !h->first;
632 }
633
634 static inline void __hlist_del(struct hlist_node *n)
635 {
636         struct hlist_node *next = n->next;
637         struct hlist_node **pprev = n->pprev;
638         *pprev = next;
639         if (next)
640                 next->pprev = pprev;
641 }
642
643 static inline void hlist_del(struct hlist_node *n)
644 {
645         __hlist_del(n);
646         n->next = LIST_POISON1;
647         n->pprev = LIST_POISON2;
648 }
649
650 /**
651  * hlist_del_rcu - deletes entry from hash list without re-initialization
652  * @n: the element to delete from the hash list.
653  *
654  * Note: list_unhashed() on entry does not return true after this,
655  * the entry is in an undefined state. It is useful for RCU based
656  * lockfree traversal.
657  *
658  * In particular, it means that we can not poison the forward
659  * pointers that may still be used for walking the hash list.
660  *
661  * The caller must take whatever precautions are necessary
662  * (such as holding appropriate locks) to avoid racing
663  * with another list-mutation primitive, such as hlist_add_head_rcu()
664  * or hlist_del_rcu(), running on this same list.
665  * However, it is perfectly legal to run concurrently with
666  * the _rcu list-traversal primitives, such as
667  * hlist_for_each_entry().
668  */
669 static inline void hlist_del_rcu(struct hlist_node *n)
670 {
671         __hlist_del(n);
672         n->pprev = LIST_POISON2;
673 }
674
675 static inline void hlist_del_init(struct hlist_node *n)
676 {
677         if (!hlist_unhashed(n)) {
678                 __hlist_del(n);
679                 INIT_HLIST_NODE(n);
680         }
681 }
682
683 /**
684  * hlist_replace_rcu - replace old entry by new one
685  * @old : the element to be replaced
686  * @new : the new element to insert
687  *
688  * The @old entry will be replaced with the @new entry atomically.
689  */
690 static inline void hlist_replace_rcu(struct hlist_node *old,
691                                         struct hlist_node *new)
692 {
693         struct hlist_node *next = old->next;
694
695         new->next = next;
696         new->pprev = old->pprev;
697         smp_wmb();
698         if (next)
699                 new->next->pprev = &new->next;
700         *new->pprev = new;
701         old->pprev = LIST_POISON2;
702 }
703
704 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
705 {
706         struct hlist_node *first = h->first;
707         n->next = first;
708         if (first)
709                 first->pprev = &n->next;
710         h->first = n;
711         n->pprev = &h->first;
712 }
713
714
715 /**
716  * hlist_add_head_rcu
717  * @n: the element to add to the hash list.
718  * @h: the list to add to.
719  *
720  * Description:
721  * Adds the specified element to the specified hlist,
722  * while permitting racing traversals.
723  *
724  * The caller must take whatever precautions are necessary
725  * (such as holding appropriate locks) to avoid racing
726  * with another list-mutation primitive, such as hlist_add_head_rcu()
727  * or hlist_del_rcu(), running on this same list.
728  * However, it is perfectly legal to run concurrently with
729  * the _rcu list-traversal primitives, such as
730  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
731  * problems on Alpha CPUs.  Regardless of the type of CPU, the
732  * list-traversal primitive must be guarded by rcu_read_lock().
733  */
734 static inline void hlist_add_head_rcu(struct hlist_node *n,
735                                         struct hlist_head *h)
736 {
737         struct hlist_node *first = h->first;
738         n->next = first;
739         n->pprev = &h->first;
740         smp_wmb();
741         if (first)
742                 first->pprev = &n->next;
743         h->first = n;
744 }
745
746 /* next must be != NULL */
747 static inline void hlist_add_before(struct hlist_node *n,
748                                         struct hlist_node *next)
749 {
750         n->pprev = next->pprev;
751         n->next = next;
752         next->pprev = &n->next;
753         *(n->pprev) = n;
754 }
755
756 static inline void hlist_add_after(struct hlist_node *n,
757                                         struct hlist_node *next)
758 {
759         next->next = n->next;
760         n->next = next;
761         next->pprev = &n->next;
762
763         if(next->next)
764                 next->next->pprev  = &next->next;
765 }
766
767 /**
768  * hlist_add_before_rcu
769  * @n: the new element to add to the hash list.
770  * @next: the existing element to add the new element before.
771  *
772  * Description:
773  * Adds the specified element to the specified hlist
774  * before the specified node while permitting racing traversals.
775  *
776  * The caller must take whatever precautions are necessary
777  * (such as holding appropriate locks) to avoid racing
778  * with another list-mutation primitive, such as hlist_add_head_rcu()
779  * or hlist_del_rcu(), running on this same list.
780  * However, it is perfectly legal to run concurrently with
781  * the _rcu list-traversal primitives, such as
782  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
783  * problems on Alpha CPUs.
784  */
785 static inline void hlist_add_before_rcu(struct hlist_node *n,
786                                         struct hlist_node *next)
787 {
788         n->pprev = next->pprev;
789         n->next = next;
790         smp_wmb();
791         next->pprev = &n->next;
792         *(n->pprev) = n;
793 }
794
795 /**
796  * hlist_add_after_rcu
797  * @prev: the existing element to add the new element after.
798  * @n: the new element to add to the hash list.
799  *
800  * Description:
801  * Adds the specified element to the specified hlist
802  * after the specified node while permitting racing traversals.
803  *
804  * The caller must take whatever precautions are necessary
805  * (such as holding appropriate locks) to avoid racing
806  * with another list-mutation primitive, such as hlist_add_head_rcu()
807  * or hlist_del_rcu(), running on this same list.
808  * However, it is perfectly legal to run concurrently with
809  * the _rcu list-traversal primitives, such as
810  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
811  * problems on Alpha CPUs.
812  */
813 static inline void hlist_add_after_rcu(struct hlist_node *prev,
814                                        struct hlist_node *n)
815 {
816         n->next = prev->next;
817         n->pprev = &prev->next;
818         smp_wmb();
819         prev->next = n;
820         if (n->next)
821                 n->next->pprev = &n->next;
822 }
823
824 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
825
826 #define hlist_for_each(pos, head) \
827         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
828              pos = pos->next)
829
830 #define hlist_for_each_safe(pos, n, head) \
831         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
832              pos = n)
833
834 /**
835  * hlist_for_each_entry - iterate over list of given type
836  * @tpos:       the type * to use as a loop cursor.
837  * @pos:        the &struct hlist_node to use as a loop cursor.
838  * @head:       the head for your list.
839  * @member:     the name of the hlist_node within the struct.
840  */
841 #define hlist_for_each_entry(tpos, pos, head, member)                    \
842         for (pos = (head)->first;                                        \
843              pos && ({ prefetch(pos->next); 1;}) &&                      \
844                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
845              pos = pos->next)
846
847 /**
848  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
849  * @tpos:       the type * to use as a loop cursor.
850  * @pos:        the &struct hlist_node to use as a loop cursor.
851  * @member:     the name of the hlist_node within the struct.
852  */
853 #define hlist_for_each_entry_continue(tpos, pos, member)                 \
854         for (pos = (pos)->next;                                          \
855              pos && ({ prefetch(pos->next); 1;}) &&                      \
856                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
857              pos = pos->next)
858
859 /**
860  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
861  * @tpos:       the type * to use as a loop cursor.
862  * @pos:        the &struct hlist_node to use as a loop cursor.
863  * @member:     the name of the hlist_node within the struct.
864  */
865 #define hlist_for_each_entry_from(tpos, pos, member)                     \
866         for (; pos && ({ prefetch(pos->next); 1;}) &&                    \
867                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
868              pos = pos->next)
869
870 /**
871  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
872  * @tpos:       the type * to use as a loop cursor.
873  * @pos:        the &struct hlist_node to use as a loop cursor.
874  * @n:          another &struct hlist_node to use as temporary storage
875  * @head:       the head for your list.
876  * @member:     the name of the hlist_node within the struct.
877  */
878 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
879         for (pos = (head)->first;                                        \
880              pos && ({ n = pos->next; 1; }) &&                           \
881                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
882              pos = n)
883
884 /**
885  * hlist_for_each_entry_rcu - iterate over rcu list of given type
886  * @tpos:       the type * to use as a loop cursor.
887  * @pos:        the &struct hlist_node to use as a loop cursor.
888  * @head:       the head for your list.
889  * @member:     the name of the hlist_node within the struct.
890  *
891  * This list-traversal primitive may safely run concurrently with
892  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
893  * as long as the traversal is guarded by rcu_read_lock().
894  */
895 #define hlist_for_each_entry_rcu(tpos, pos, head, member)                \
896         for (pos = (head)->first;                                        \
897              rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) &&     \
898                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
899              pos = pos->next)
900
901 #else
902 #warning "don't include kernel headers in userspace"
903 #endif /* __KERNEL__ */
904 #endif