aboutsummaryrefslogtreecommitdiffstats
path: root/lib/klist.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/klist.c')
-rw-r--r--lib/klist.c235
1 files changed, 116 insertions, 119 deletions
diff --git a/lib/klist.c b/lib/klist.c
index 120bd175aa78..cca37f96faa2 100644
--- a/lib/klist.c
+++ b/lib/klist.c
@@ -1,38 +1,37 @@
1/* 1/*
2 * klist.c - Routines for manipulating klists. 2 * klist.c - Routines for manipulating klists.
3 * 3 *
4 * Copyright (C) 2005 Patrick Mochel
4 * 5 *
5 * This klist interface provides a couple of structures that wrap around 6 * This file is released under the GPL v2.
6 * struct list_head to provide explicit list "head" (struct klist) and
7 * list "node" (struct klist_node) objects. For struct klist, a spinlock
8 * is included that protects access to the actual list itself. struct
9 * klist_node provides a pointer to the klist that owns it and a kref
10 * reference count that indicates the number of current users of that node
11 * in the list.
12 * 7 *
13 * The entire point is to provide an interface for iterating over a list 8 * This klist interface provides a couple of structures that wrap around
14 * that is safe and allows for modification of the list during the 9 * struct list_head to provide explicit list "head" (struct klist) and list
15 * iteration (e.g. insertion and removal), including modification of the 10 * "node" (struct klist_node) objects. For struct klist, a spinlock is
16 * current node on the list. 11 * included that protects access to the actual list itself. struct
12 * klist_node provides a pointer to the klist that owns it and a kref
13 * reference count that indicates the number of current users of that node
14 * in the list.
17 * 15 *
18 * It works using a 3rd object type - struct klist_iter - that is declared 16 * The entire point is to provide an interface for iterating over a list
19 * and initialized before an iteration. klist_next() is used to acquire the 17 * that is safe and allows for modification of the list during the
20 * next element in the list. It returns NULL if there are no more items. 18 * iteration (e.g. insertion and removal), including modification of the
21 * Internally, that routine takes the klist's lock, decrements the reference 19 * current node on the list.
22 * count of the previous klist_node and increments the count of the next
23 * klist_node. It then drops the lock and returns.
24 * 20 *
25 * There are primitives for adding and removing nodes to/from a klist. 21 * It works using a 3rd object type - struct klist_iter - that is declared
26 * When deleting, klist_del() will simply decrement the reference count. 22 * and initialized before an iteration. klist_next() is used to acquire the
27 * Only when the count goes to 0 is the node removed from the list. 23 * next element in the list. It returns NULL if there are no more items.
28 * klist_remove() will try to delete the node from the list and block 24 * Internally, that routine takes the klist's lock, decrements the
29 * until it is actually removed. This is useful for objects (like devices) 25 * reference count of the previous klist_node and increments the count of
30 * that have been removed from the system and must be freed (but must wait 26 * the next klist_node. It then drops the lock and returns.
31 * until all accessors have finished).
32 * 27 *
33 * Copyright (C) 2005 Patrick Mochel 28 * There are primitives for adding and removing nodes to/from a klist.
34 * 29 * When deleting, klist_del() will simply decrement the reference count.
35 * This file is released under the GPL v2. 30 * Only when the count goes to 0 is the node removed from the list.
31 * klist_remove() will try to delete the node from the list and block until
32 * it is actually removed. This is useful for objects (like devices) that
33 * have been removed from the system and must be freed (but must wait until
34 * all accessors have finished).
36 */ 35 */
37 36
38#include <linux/klist.h> 37#include <linux/klist.h>
@@ -40,10 +39,10 @@
40 39
41 40
42/** 41/**
43 * klist_init - Initialize a klist structure. 42 * klist_init - Initialize a klist structure.
44 * @k: The klist we're initializing. 43 * @k: The klist we're initializing.
45 * @get: The get function for the embedding object (NULL if none) 44 * @get: The get function for the embedding object (NULL if none)
46 * @put: The put function for the embedding object (NULL if none) 45 * @put: The put function for the embedding object (NULL if none)
47 * 46 *
48 * Initialises the klist structure. If the klist_node structures are 47 * Initialises the klist structure. If the klist_node structures are
49 * going to be embedded in refcounted objects (necessary for safe 48 * going to be embedded in refcounted objects (necessary for safe
@@ -51,8 +50,7 @@
51 * functions that take and release references on the embedding 50 * functions that take and release references on the embedding
52 * objects. 51 * objects.
53 */ 52 */
54 53void klist_init(struct klist *k, void (*get)(struct klist_node *),
55void klist_init(struct klist * k, void (*get)(struct klist_node *),
56 void (*put)(struct klist_node *)) 54 void (*put)(struct klist_node *))
57{ 55{
58 INIT_LIST_HEAD(&k->k_list); 56 INIT_LIST_HEAD(&k->k_list);
@@ -60,26 +58,23 @@ void klist_init(struct klist * k, void (*get)(struct klist_node *),
60 k->get = get; 58 k->get = get;
61 k->put = put; 59 k->put = put;
62} 60}
63
64EXPORT_SYMBOL_GPL(klist_init); 61EXPORT_SYMBOL_GPL(klist_init);
65 62
66 63static void add_head(struct klist *k, struct klist_node *n)
67static void add_head(struct klist * k, struct klist_node * n)
68{ 64{
69 spin_lock(&k->k_lock); 65 spin_lock(&k->k_lock);
70 list_add(&n->n_node, &k->k_list); 66 list_add(&n->n_node, &k->k_list);
71 spin_unlock(&k->k_lock); 67 spin_unlock(&k->k_lock);
72} 68}
73 69
74static void add_tail(struct klist * k, struct klist_node * n) 70static void add_tail(struct klist *k, struct klist_node *n)
75{ 71{
76 spin_lock(&k->k_lock); 72 spin_lock(&k->k_lock);
77 list_add_tail(&n->n_node, &k->k_list); 73 list_add_tail(&n->n_node, &k->k_list);
78 spin_unlock(&k->k_lock); 74 spin_unlock(&k->k_lock);
79} 75}
80 76
81 77static void klist_node_init(struct klist *k, struct klist_node *n)
82static void klist_node_init(struct klist * k, struct klist_node * n)
83{ 78{
84 INIT_LIST_HEAD(&n->n_node); 79 INIT_LIST_HEAD(&n->n_node);
85 init_completion(&n->n_removed); 80 init_completion(&n->n_removed);
@@ -89,60 +84,83 @@ static void klist_node_init(struct klist * k, struct klist_node * n)
89 k->get(n); 84 k->get(n);
90} 85}
91 86
92
93/** 87/**
94 * klist_add_head - Initialize a klist_node and add it to front. 88 * klist_add_head - Initialize a klist_node and add it to front.
95 * @n: node we're adding. 89 * @n: node we're adding.
96 * @k: klist it's going on. 90 * @k: klist it's going on.
97 */ 91 */
98 92void klist_add_head(struct klist_node *n, struct klist *k)
99void klist_add_head(struct klist_node * n, struct klist * k)
100{ 93{
101 klist_node_init(k, n); 94 klist_node_init(k, n);
102 add_head(k, n); 95 add_head(k, n);
103} 96}
104
105EXPORT_SYMBOL_GPL(klist_add_head); 97EXPORT_SYMBOL_GPL(klist_add_head);
106 98
107
108/** 99/**
109 * klist_add_tail - Initialize a klist_node and add it to back. 100 * klist_add_tail - Initialize a klist_node and add it to back.
110 * @n: node we're adding. 101 * @n: node we're adding.
111 * @k: klist it's going on. 102 * @k: klist it's going on.
112 */ 103 */
113 104void klist_add_tail(struct klist_node *n, struct klist *k)
114void klist_add_tail(struct klist_node * n, struct klist * k)
115{ 105{
116 klist_node_init(k, n); 106 klist_node_init(k, n);
117 add_tail(k, n); 107 add_tail(k, n);
118} 108}
119
120EXPORT_SYMBOL_GPL(klist_add_tail); 109EXPORT_SYMBOL_GPL(klist_add_tail);
121 110
111/**
112 * klist_add_after - Init a klist_node and add it after an existing node
113 * @n: node we're adding.
114 * @pos: node to put @n after
115 */
116void klist_add_after(struct klist_node *n, struct klist_node *pos)
117{
118 struct klist *k = pos->n_klist;
119
120 klist_node_init(k, n);
121 spin_lock(&k->k_lock);
122 list_add(&n->n_node, &pos->n_node);
123 spin_unlock(&k->k_lock);
124}
125EXPORT_SYMBOL_GPL(klist_add_after);
126
127/**
128 * klist_add_before - Init a klist_node and add it before an existing node
129 * @n: node we're adding.
130 * @pos: node to put @n after
131 */
132void klist_add_before(struct klist_node *n, struct klist_node *pos)
133{
134 struct klist *k = pos->n_klist;
135
136 klist_node_init(k, n);
137 spin_lock(&k->k_lock);
138 list_add_tail(&n->n_node, &pos->n_node);
139 spin_unlock(&k->k_lock);
140}
141EXPORT_SYMBOL_GPL(klist_add_before);
122 142
123static void klist_release(struct kref * kref) 143static void klist_release(struct kref *kref)
124{ 144{
125 struct klist_node * n = container_of(kref, struct klist_node, n_ref); 145 struct klist_node *n = container_of(kref, struct klist_node, n_ref);
126 146
127 list_del(&n->n_node); 147 list_del(&n->n_node);
128 complete(&n->n_removed); 148 complete(&n->n_removed);
129 n->n_klist = NULL; 149 n->n_klist = NULL;
130} 150}
131 151
132static int klist_dec_and_del(struct klist_node * n) 152static int klist_dec_and_del(struct klist_node *n)
133{ 153{
134 return kref_put(&n->n_ref, klist_release); 154 return kref_put(&n->n_ref, klist_release);
135} 155}
136 156
137
138/** 157/**
139 * klist_del - Decrement the reference count of node and try to remove. 158 * klist_del - Decrement the reference count of node and try to remove.
140 * @n: node we're deleting. 159 * @n: node we're deleting.
141 */ 160 */
142 161void klist_del(struct klist_node *n)
143void klist_del(struct klist_node * n)
144{ 162{
145 struct klist * k = n->n_klist; 163 struct klist *k = n->n_klist;
146 void (*put)(struct klist_node *) = k->put; 164 void (*put)(struct klist_node *) = k->put;
147 165
148 spin_lock(&k->k_lock); 166 spin_lock(&k->k_lock);
@@ -152,48 +170,40 @@ void klist_del(struct klist_node * n)
152 if (put) 170 if (put)
153 put(n); 171 put(n);
154} 172}
155
156EXPORT_SYMBOL_GPL(klist_del); 173EXPORT_SYMBOL_GPL(klist_del);
157 174
158
159/** 175/**
160 * klist_remove - Decrement the refcount of node and wait for it to go away. 176 * klist_remove - Decrement the refcount of node and wait for it to go away.
161 * @n: node we're removing. 177 * @n: node we're removing.
162 */ 178 */
163 179void klist_remove(struct klist_node *n)
164void klist_remove(struct klist_node * n)
165{ 180{
166 klist_del(n); 181 klist_del(n);
167 wait_for_completion(&n->n_removed); 182 wait_for_completion(&n->n_removed);
168} 183}
169
170EXPORT_SYMBOL_GPL(klist_remove); 184EXPORT_SYMBOL_GPL(klist_remove);
171 185
172
173/** 186/**
174 * klist_node_attached - Say whether a node is bound to a list or not. 187 * klist_node_attached - Say whether a node is bound to a list or not.
175 * @n: Node that we're testing. 188 * @n: Node that we're testing.
176 */ 189 */
177 190int klist_node_attached(struct klist_node *n)
178int klist_node_attached(struct klist_node * n)
179{ 191{
180 return (n->n_klist != NULL); 192 return (n->n_klist != NULL);
181} 193}
182
183EXPORT_SYMBOL_GPL(klist_node_attached); 194EXPORT_SYMBOL_GPL(klist_node_attached);
184 195
185
186/** 196/**
187 * klist_iter_init_node - Initialize a klist_iter structure. 197 * klist_iter_init_node - Initialize a klist_iter structure.
188 * @k: klist we're iterating. 198 * @k: klist we're iterating.
189 * @i: klist_iter we're filling. 199 * @i: klist_iter we're filling.
190 * @n: node to start with. 200 * @n: node to start with.
191 * 201 *
192 * Similar to klist_iter_init(), but starts the action off with @n, 202 * Similar to klist_iter_init(), but starts the action off with @n,
193 * instead of with the list head. 203 * instead of with the list head.
194 */ 204 */
195 205void klist_iter_init_node(struct klist *k, struct klist_iter *i,
196void klist_iter_init_node(struct klist * k, struct klist_iter * i, struct klist_node * n) 206 struct klist_node *n)
197{ 207{
198 i->i_klist = k; 208 i->i_klist = k;
199 i->i_head = &k->k_list; 209 i->i_head = &k->k_list;
@@ -201,66 +211,56 @@ void klist_iter_init_node(struct klist * k, struct klist_iter * i, struct klist_
201 if (n) 211 if (n)
202 kref_get(&n->n_ref); 212 kref_get(&n->n_ref);
203} 213}
204
205EXPORT_SYMBOL_GPL(klist_iter_init_node); 214EXPORT_SYMBOL_GPL(klist_iter_init_node);
206 215
207
208/** 216/**
209 * klist_iter_init - Iniitalize a klist_iter structure. 217 * klist_iter_init - Iniitalize a klist_iter structure.
210 * @k: klist we're iterating. 218 * @k: klist we're iterating.
211 * @i: klist_iter structure we're filling. 219 * @i: klist_iter structure we're filling.
212 * 220 *
213 * Similar to klist_iter_init_node(), but start with the list head. 221 * Similar to klist_iter_init_node(), but start with the list head.
214 */ 222 */
215 223void klist_iter_init(struct klist *k, struct klist_iter *i)
216void klist_iter_init(struct klist * k, struct klist_iter * i)
217{ 224{
218 klist_iter_init_node(k, i, NULL); 225 klist_iter_init_node(k, i, NULL);
219} 226}
220
221EXPORT_SYMBOL_GPL(klist_iter_init); 227EXPORT_SYMBOL_GPL(klist_iter_init);
222 228
223
224/** 229/**
225 * klist_iter_exit - Finish a list iteration. 230 * klist_iter_exit - Finish a list iteration.
226 * @i: Iterator structure. 231 * @i: Iterator structure.
227 * 232 *
228 * Must be called when done iterating over list, as it decrements the 233 * Must be called when done iterating over list, as it decrements the
229 * refcount of the current node. Necessary in case iteration exited before 234 * refcount of the current node. Necessary in case iteration exited before
230 * the end of the list was reached, and always good form. 235 * the end of the list was reached, and always good form.
231 */ 236 */
232 237void klist_iter_exit(struct klist_iter *i)
233void klist_iter_exit(struct klist_iter * i)
234{ 238{
235 if (i->i_cur) { 239 if (i->i_cur) {
236 klist_del(i->i_cur); 240 klist_del(i->i_cur);
237 i->i_cur = NULL; 241 i->i_cur = NULL;
238 } 242 }
239} 243}
240
241EXPORT_SYMBOL_GPL(klist_iter_exit); 244EXPORT_SYMBOL_GPL(klist_iter_exit);
242 245
243 246static struct klist_node *to_klist_node(struct list_head *n)
244static struct klist_node * to_klist_node(struct list_head * n)
245{ 247{
246 return container_of(n, struct klist_node, n_node); 248 return container_of(n, struct klist_node, n_node);
247} 249}
248 250
249
250/** 251/**
251 * klist_next - Ante up next node in list. 252 * klist_next - Ante up next node in list.
252 * @i: Iterator structure. 253 * @i: Iterator structure.
253 * 254 *
254 * First grab list lock. Decrement the reference count of the previous 255 * First grab list lock. Decrement the reference count of the previous
255 * node, if there was one. Grab the next node, increment its reference 256 * node, if there was one. Grab the next node, increment its reference
256 * count, drop the lock, and return that next node. 257 * count, drop the lock, and return that next node.
257 */ 258 */
258 259struct klist_node *klist_next(struct klist_iter *i)
259struct klist_node * klist_next(struct klist_iter * i)
260{ 260{
261 struct list_head * next; 261 struct list_head *next;
262 struct klist_node * lnode = i->i_cur; 262 struct klist_node *lnode = i->i_cur;
263 struct klist_node * knode = NULL; 263 struct klist_node *knode = NULL;
264 void (*put)(struct klist_node *) = i->i_klist->put; 264 void (*put)(struct klist_node *) = i->i_klist->put;
265 265
266 spin_lock(&i->i_klist->k_lock); 266 spin_lock(&i->i_klist->k_lock);
@@ -281,7 +281,4 @@ struct klist_node * klist_next(struct klist_iter * i)
281 put(lnode); 281 put(lnode);
282 return knode; 282 return knode;
283} 283}
284
285EXPORT_SYMBOL_GPL(klist_next); 284EXPORT_SYMBOL_GPL(klist_next);
286
287