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