1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/key.h>
15#include <linux/ctype.h>
16#include <linux/sched.h>
17#include <keys/rxrpc-type.h>
18#include "internal.h"
19
20DECLARE_RWSEM(afs_proc_cells_sem);
21LIST_HEAD(afs_proc_cells);
22
23static LIST_HEAD(afs_cells);
24static DEFINE_RWLOCK(afs_cells_lock);
25static DECLARE_RWSEM(afs_cells_sem);
26static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
27static struct afs_cell *afs_cell_root;
28
29
30
31
32
33static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
34{
35 struct afs_cell *cell;
36 struct key *key;
37 size_t namelen;
38 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
39 int ret;
40
41 _enter("%s,%s", name, vllist);
42
43 BUG_ON(!name);
44
45 namelen = strlen(name);
46 if (namelen > AFS_MAXCELLNAME)
47 return ERR_PTR(-ENAMETOOLONG);
48
49
50 cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
51 if (!cell) {
52 _leave(" = -ENOMEM");
53 return ERR_PTR(-ENOMEM);
54 }
55
56 memcpy(cell->name, name, namelen);
57 cell->name[namelen] = 0;
58
59 atomic_set(&cell->usage, 1);
60 INIT_LIST_HEAD(&cell->link);
61 rwlock_init(&cell->servers_lock);
62 INIT_LIST_HEAD(&cell->servers);
63 init_rwsem(&cell->vl_sem);
64 INIT_LIST_HEAD(&cell->vl_list);
65 spin_lock_init(&cell->vl_lock);
66
67
68 do {
69 unsigned a, b, c, d;
70
71 next = strchr(vllist, ':');
72 if (next)
73 *next++ = 0;
74
75 if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
76 goto bad_address;
77
78 if (a > 255 || b > 255 || c > 255 || d > 255)
79 goto bad_address;
80
81 cell->vl_addrs[cell->vl_naddrs++].s_addr =
82 htonl((a << 24) | (b << 16) | (c << 8) | d);
83
84 } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (vllist = next));
85
86
87 memcpy(keyname, "afs@", 4);
88 dp = keyname + 4;
89 cp = cell->name;
90 do {
91 *dp++ = toupper(*cp);
92 } while (*cp++);
93
94 key = rxrpc_get_null_key(keyname);
95 if (IS_ERR(key)) {
96 _debug("no key");
97 ret = PTR_ERR(key);
98 goto error;
99 }
100 cell->anonymous_key = key;
101
102 _debug("anon key %p{%x}",
103 cell->anonymous_key, key_serial(cell->anonymous_key));
104
105 _leave(" = %p", cell);
106 return cell;
107
108bad_address:
109 printk(KERN_ERR "kAFS: bad VL server IP address\n");
110 ret = -EINVAL;
111error:
112 key_put(cell->anonymous_key);
113 kfree(cell);
114 _leave(" = %d", ret);
115 return ERR_PTR(ret);
116}
117
118
119
120
121
122
123struct afs_cell *afs_cell_create(const char *name, char *vllist)
124{
125 struct afs_cell *cell;
126 int ret;
127
128 _enter("%s,%s", name, vllist);
129
130 down_write(&afs_cells_sem);
131 read_lock(&afs_cells_lock);
132 list_for_each_entry(cell, &afs_cells, link) {
133 if (strcasecmp(cell->name, name) == 0)
134 goto duplicate_name;
135 }
136 read_unlock(&afs_cells_lock);
137
138 cell = afs_cell_alloc(name, vllist);
139 if (IS_ERR(cell)) {
140 _leave(" = %ld", PTR_ERR(cell));
141 up_write(&afs_cells_sem);
142 return cell;
143 }
144
145
146 ret = afs_proc_cell_setup(cell);
147 if (ret < 0)
148 goto error;
149
150#ifdef AFS_CACHING_SUPPORT
151
152 cachefs_acquire_cookie(afs_cache_netfs.primary_index,
153 &afs_vlocation_cache_index_def,
154 cell,
155 &cell->cache);
156#endif
157
158
159 write_lock(&afs_cells_lock);
160 list_add_tail(&cell->link, &afs_cells);
161 write_unlock(&afs_cells_lock);
162
163 down_write(&afs_proc_cells_sem);
164 list_add_tail(&cell->proc_link, &afs_proc_cells);
165 up_write(&afs_proc_cells_sem);
166 up_write(&afs_cells_sem);
167
168 _leave(" = %p", cell);
169 return cell;
170
171error:
172 up_write(&afs_cells_sem);
173 key_put(cell->anonymous_key);
174 kfree(cell);
175 _leave(" = %d", ret);
176 return ERR_PTR(ret);
177
178duplicate_name:
179 read_unlock(&afs_cells_lock);
180 up_write(&afs_cells_sem);
181 return ERR_PTR(-EEXIST);
182}
183
184
185
186
187
188
189int afs_cell_init(char *rootcell)
190{
191 struct afs_cell *old_root, *new_root;
192 char *cp;
193
194 _enter("");
195
196 if (!rootcell) {
197
198
199
200 _leave(" = 0 [no root]");
201 return 0;
202 }
203
204 cp = strchr(rootcell, ':');
205 if (!cp) {
206 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
207 _leave(" = -EINVAL");
208 return -EINVAL;
209 }
210
211
212 *cp++ = 0;
213 new_root = afs_cell_create(rootcell, cp);
214 if (IS_ERR(new_root)) {
215 _leave(" = %ld", PTR_ERR(new_root));
216 return PTR_ERR(new_root);
217 }
218
219
220 write_lock(&afs_cells_lock);
221 old_root = afs_cell_root;
222 afs_cell_root = new_root;
223 write_unlock(&afs_cells_lock);
224 afs_put_cell(old_root);
225
226 _leave(" = 0");
227 return 0;
228}
229
230
231
232
233struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
234{
235 struct afs_cell *cell;
236
237 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
238
239 down_read(&afs_cells_sem);
240 read_lock(&afs_cells_lock);
241
242 if (name) {
243
244 list_for_each_entry(cell, &afs_cells, link) {
245 if (strncmp(cell->name, name, namesz) == 0) {
246 afs_get_cell(cell);
247 goto found;
248 }
249 }
250 cell = ERR_PTR(-ENOENT);
251 found:
252 ;
253 } else {
254 cell = afs_cell_root;
255 if (!cell) {
256
257
258
259
260
261
262 cell = ERR_PTR(-EDESTADDRREQ);
263 } else {
264 afs_get_cell(cell);
265 }
266
267 }
268
269 read_unlock(&afs_cells_lock);
270 up_read(&afs_cells_sem);
271 _leave(" = %p", cell);
272 return cell;
273}
274
275#if 0
276
277
278
279struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
280{
281 write_lock(&afs_cells_lock);
282
283 if (cell && !list_empty(&cell->link))
284 afs_get_cell(cell);
285 else
286 cell = NULL;
287
288 write_unlock(&afs_cells_lock);
289 return cell;
290}
291#endif
292
293
294
295
296void afs_put_cell(struct afs_cell *cell)
297{
298 if (!cell)
299 return;
300
301 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
302
303 ASSERTCMP(atomic_read(&cell->usage), >, 0);
304
305
306
307 write_lock(&afs_cells_lock);
308
309 if (likely(!atomic_dec_and_test(&cell->usage))) {
310 write_unlock(&afs_cells_lock);
311 _leave("");
312 return;
313 }
314
315 ASSERT(list_empty(&cell->servers));
316 ASSERT(list_empty(&cell->vl_list));
317
318 write_unlock(&afs_cells_lock);
319
320 wake_up(&afs_cells_freeable_wq);
321
322 _leave(" [unused]");
323}
324
325
326
327
328
329
330static void afs_cell_destroy(struct afs_cell *cell)
331{
332 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
333
334 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
335 ASSERT(list_empty(&cell->link));
336
337
338 if (atomic_read(&cell->usage) > 0) {
339 DECLARE_WAITQUEUE(myself, current);
340
341 _debug("wait for cell %s", cell->name);
342 set_current_state(TASK_UNINTERRUPTIBLE);
343 add_wait_queue(&afs_cells_freeable_wq, &myself);
344
345 while (atomic_read(&cell->usage) > 0) {
346 schedule();
347 set_current_state(TASK_UNINTERRUPTIBLE);
348 }
349
350 remove_wait_queue(&afs_cells_freeable_wq, &myself);
351 set_current_state(TASK_RUNNING);
352 }
353
354 _debug("cell dead");
355 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
356 ASSERT(list_empty(&cell->servers));
357 ASSERT(list_empty(&cell->vl_list));
358
359 afs_proc_cell_remove(cell);
360
361 down_write(&afs_proc_cells_sem);
362 list_del_init(&cell->proc_link);
363 up_write(&afs_proc_cells_sem);
364
365#ifdef AFS_CACHING_SUPPORT
366 cachefs_relinquish_cookie(cell->cache, 0);
367#endif
368
369 key_put(cell->anonymous_key);
370 kfree(cell);
371
372 _leave(" [destroyed]");
373}
374
375
376
377
378
379void afs_cell_purge(void)
380{
381 struct afs_cell *cell;
382
383 _enter("");
384
385 afs_put_cell(afs_cell_root);
386
387 down_write(&afs_cells_sem);
388
389 while (!list_empty(&afs_cells)) {
390 cell = NULL;
391
392
393 write_lock(&afs_cells_lock);
394
395 if (!list_empty(&afs_cells)) {
396 cell = list_entry(afs_cells.next,
397 struct afs_cell, link);
398 list_del_init(&cell->link);
399 }
400
401 write_unlock(&afs_cells_lock);
402
403 if (cell) {
404 _debug("PURGING CELL %s (%d)",
405 cell->name, atomic_read(&cell->usage));
406
407
408 afs_cell_destroy(cell);
409 }
410 }
411
412 up_write(&afs_cells_sem);
413 _leave("");
414}
415