blob: 28ed84ec8ff7ac8f85f9e0b74f2c10e668761f72 [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS cell and server record management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <rxrpc/peer.h>
15#include <rxrpc/connection.h>
16#include "volume.h"
17#include "cell.h"
18#include "server.h"
19#include "transport.h"
20#include "vlclient.h"
21#include "kafstimod.h"
22#include "super.h"
23#include "internal.h"
24
25DECLARE_RWSEM(afs_proc_cells_sem);
26LIST_HEAD(afs_proc_cells);
27
28static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells);
29static DEFINE_RWLOCK(afs_cells_lock);
30static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
31static struct afs_cell *afs_cell_root;
32
33#ifdef AFS_CACHING_SUPPORT
34static cachefs_match_val_t afs_cell_cache_match(void *target,
35 const void *entry);
36static void afs_cell_cache_update(void *source, void *entry);
37
38struct cachefs_index_def afs_cache_cell_index_def = {
39 .name = "cell_ix",
40 .data_size = sizeof(struct afs_cache_cell),
41 .keys[0] = { CACHEFS_INDEX_KEYS_ASCIIZ, 64 },
42 .match = afs_cell_cache_match,
43 .update = afs_cell_cache_update,
44};
45#endif
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * create a cell record
49 * - "name" is the name of the cell
50 * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
51 */
52int afs_cell_create(const char *name, char *vllist, struct afs_cell **_cell)
53{
54 struct afs_cell *cell;
55 char *next;
56 int ret;
57
58 _enter("%s", name);
59
60 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
61
62 /* allocate and initialise a cell record */
63 cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL);
64 if (!cell) {
65 _leave(" = -ENOMEM");
66 return -ENOMEM;
67 }
68
69 down_write(&afs_cells_sem);
70
71 memset(cell, 0, sizeof(struct afs_cell));
72 atomic_set(&cell->usage, 0);
73
74 INIT_LIST_HEAD(&cell->link);
75
76 rwlock_init(&cell->sv_lock);
77 INIT_LIST_HEAD(&cell->sv_list);
78 INIT_LIST_HEAD(&cell->sv_graveyard);
79 spin_lock_init(&cell->sv_gylock);
80
81 init_rwsem(&cell->vl_sem);
82 INIT_LIST_HEAD(&cell->vl_list);
83 INIT_LIST_HEAD(&cell->vl_graveyard);
84 spin_lock_init(&cell->vl_gylock);
85
86 strcpy(cell->name,name);
87
88 /* fill in the VL server list from the rest of the string */
89 ret = -EINVAL;
90 do {
91 unsigned a, b, c, d;
92
93 next = strchr(vllist, ':');
94 if (next)
95 *next++ = 0;
96
97 if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
98 goto badaddr;
99
100 if (a > 255 || b > 255 || c > 255 || d > 255)
101 goto badaddr;
102
103 cell->vl_addrs[cell->vl_naddrs++].s_addr =
104 htonl((a << 24) | (b << 16) | (c << 8) | d);
105
106 if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS)
107 break;
108
109 } while(vllist = next, vllist);
110
111 /* add a proc dir for this cell */
112 ret = afs_proc_cell_setup(cell);
113 if (ret < 0)
114 goto error;
115
116#ifdef AFS_CACHING_SUPPORT
117 /* put it up for caching */
118 cachefs_acquire_cookie(afs_cache_netfs.primary_index,
119 &afs_vlocation_cache_index_def,
120 cell,
121 &cell->cache);
122#endif
123
124 /* add to the cell lists */
125 write_lock(&afs_cells_lock);
126 list_add_tail(&cell->link, &afs_cells);
127 write_unlock(&afs_cells_lock);
128
129 down_write(&afs_proc_cells_sem);
130 list_add_tail(&cell->proc_link, &afs_proc_cells);
131 up_write(&afs_proc_cells_sem);
132
133 *_cell = cell;
134 up_write(&afs_cells_sem);
135
136 _leave(" = 0 (%p)", cell);
137 return 0;
138
David Howellsec268152007-04-26 15:49:28 -0700139badaddr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 printk(KERN_ERR "kAFS: bad VL server IP address: '%s'\n", vllist);
David Howellsec268152007-04-26 15:49:28 -0700141error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 up_write(&afs_cells_sem);
143 kfree(cell);
144 _leave(" = %d", ret);
145 return ret;
David Howellsec268152007-04-26 15:49:28 -0700146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * initialise the cell database from module parameters
150 */
151int afs_cell_init(char *rootcell)
152{
153 struct afs_cell *old_root, *new_root;
154 char *cp;
155 int ret;
156
157 _enter("");
158
159 if (!rootcell) {
160 /* module is loaded with no parameters, or built statically.
161 * - in the future we might initialize cell DB here.
162 */
163 _leave(" = 0 (but no root)");
164 return 0;
165 }
166
167 cp = strchr(rootcell, ':');
168 if (!cp) {
169 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
170 _leave(" = %d (no colon)", -EINVAL);
171 return -EINVAL;
172 }
173
174 /* allocate a cell record for the root cell */
175 *cp++ = 0;
176 ret = afs_cell_create(rootcell, cp, &new_root);
177 if (ret < 0) {
178 _leave(" = %d", ret);
179 return ret;
180 }
181
182 /* as afs_put_cell() takes locks by itself, we have to do
183 * a little gymnastics to be race-free.
184 */
185 afs_get_cell(new_root);
186
187 write_lock(&afs_cells_lock);
188 while (afs_cell_root) {
189 old_root = afs_cell_root;
190 afs_cell_root = NULL;
191 write_unlock(&afs_cells_lock);
192 afs_put_cell(old_root);
193 write_lock(&afs_cells_lock);
194 }
195 afs_cell_root = new_root;
196 write_unlock(&afs_cells_lock);
197
198 _leave(" = %d", ret);
199 return ret;
David Howellsec268152007-04-26 15:49:28 -0700200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
203 * lookup a cell record
204 */
205int afs_cell_lookup(const char *name, unsigned namesz, struct afs_cell **_cell)
206{
207 struct afs_cell *cell;
208 int ret;
209
210 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
211
212 *_cell = NULL;
213
214 if (name) {
215 /* if the cell was named, look for it in the cell record list */
216 ret = -ENOENT;
217 cell = NULL;
218 read_lock(&afs_cells_lock);
219
220 list_for_each_entry(cell, &afs_cells, link) {
221 if (strncmp(cell->name, name, namesz) == 0) {
222 afs_get_cell(cell);
223 goto found;
224 }
225 }
226 cell = NULL;
227 found:
228
229 read_unlock(&afs_cells_lock);
230
231 if (cell)
232 ret = 0;
David Howellsec268152007-04-26 15:49:28 -0700233 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 read_lock(&afs_cells_lock);
235
236 cell = afs_cell_root;
237 if (!cell) {
238 /* this should not happen unless user tries to mount
239 * when root cell is not set. Return an impossibly
240 * bizzare errno to alert the user. Things like
241 * ENOENT might be "more appropriate" but they happen
242 * for other reasons.
243 */
244 ret = -EDESTADDRREQ;
David Howellsec268152007-04-26 15:49:28 -0700245 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 afs_get_cell(cell);
247 ret = 0;
248 }
249
250 read_unlock(&afs_cells_lock);
251 }
252
253 *_cell = cell;
254 _leave(" = %d (%p)", ret, cell);
255 return ret;
David Howellsec268152007-04-26 15:49:28 -0700256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
259 * try and get a cell record
260 */
261struct afs_cell *afs_get_cell_maybe(struct afs_cell **_cell)
262{
263 struct afs_cell *cell;
264
265 write_lock(&afs_cells_lock);
266
267 cell = *_cell;
268 if (cell && !list_empty(&cell->link))
269 afs_get_cell(cell);
270 else
271 cell = NULL;
272
273 write_unlock(&afs_cells_lock);
274
275 return cell;
David Howellsec268152007-04-26 15:49:28 -0700276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
279 * destroy a cell record
280 */
281void afs_put_cell(struct afs_cell *cell)
282{
283 if (!cell)
284 return;
285
286 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
287
288 /* sanity check */
289 BUG_ON(atomic_read(&cell->usage) <= 0);
290
291 /* to prevent a race, the decrement and the dequeue must be effectively
292 * atomic */
293 write_lock(&afs_cells_lock);
294
295 if (likely(!atomic_dec_and_test(&cell->usage))) {
296 write_unlock(&afs_cells_lock);
297 _leave("");
298 return;
299 }
300
301 write_unlock(&afs_cells_lock);
302
303 BUG_ON(!list_empty(&cell->sv_list));
304 BUG_ON(!list_empty(&cell->sv_graveyard));
305 BUG_ON(!list_empty(&cell->vl_list));
306 BUG_ON(!list_empty(&cell->vl_graveyard));
307
308 _leave(" [unused]");
David Howellsec268152007-04-26 15:49:28 -0700309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
312 * destroy a cell record
313 */
314static void afs_cell_destroy(struct afs_cell *cell)
315{
316 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
317
318 /* to prevent a race, the decrement and the dequeue must be effectively
319 * atomic */
320 write_lock(&afs_cells_lock);
321
322 /* sanity check */
323 BUG_ON(atomic_read(&cell->usage) != 0);
324
325 list_del_init(&cell->link);
326
327 write_unlock(&afs_cells_lock);
328
329 down_write(&afs_cells_sem);
330
331 afs_proc_cell_remove(cell);
332
333 down_write(&afs_proc_cells_sem);
334 list_del_init(&cell->proc_link);
335 up_write(&afs_proc_cells_sem);
336
337#ifdef AFS_CACHING_SUPPORT
338 cachefs_relinquish_cookie(cell->cache, 0);
339#endif
340
341 up_write(&afs_cells_sem);
342
343 BUG_ON(!list_empty(&cell->sv_list));
344 BUG_ON(!list_empty(&cell->sv_graveyard));
345 BUG_ON(!list_empty(&cell->vl_list));
346 BUG_ON(!list_empty(&cell->vl_graveyard));
347
348 /* finish cleaning up the cell */
349 kfree(cell);
350
351 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
355 * lookup the server record corresponding to an Rx RPC peer
356 */
357int afs_server_find_by_peer(const struct rxrpc_peer *peer,
358 struct afs_server **_server)
359{
360 struct afs_server *server;
361 struct afs_cell *cell;
362
363 _enter("%p{a=%08x},", peer, ntohl(peer->addr.s_addr));
364
365 /* search the cell list */
366 read_lock(&afs_cells_lock);
367
368 list_for_each_entry(cell, &afs_cells, link) {
369
370 _debug("? cell %s",cell->name);
371
372 write_lock(&cell->sv_lock);
373
374 /* check the active list */
375 list_for_each_entry(server, &cell->sv_list, link) {
376 _debug("?? server %08x", ntohl(server->addr.s_addr));
377
378 if (memcmp(&server->addr, &peer->addr,
379 sizeof(struct in_addr)) == 0)
380 goto found_server;
381 }
382
383 /* check the inactive list */
384 spin_lock(&cell->sv_gylock);
385 list_for_each_entry(server, &cell->sv_graveyard, link) {
386 _debug("?? dead server %08x",
387 ntohl(server->addr.s_addr));
388
389 if (memcmp(&server->addr, &peer->addr,
390 sizeof(struct in_addr)) == 0)
391 goto found_dead_server;
392 }
393 spin_unlock(&cell->sv_gylock);
394
395 write_unlock(&cell->sv_lock);
396 }
397 read_unlock(&afs_cells_lock);
398
399 _leave(" = -ENOENT");
400 return -ENOENT;
401
402 /* we found it in the graveyard - resurrect it */
David Howellsec268152007-04-26 15:49:28 -0700403found_dead_server:
Akinobu Mitaf1166292006-06-26 00:24:46 -0700404 list_move_tail(&server->link, &cell->sv_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 afs_get_server(server);
406 afs_kafstimod_del_timer(&server->timeout);
407 spin_unlock(&cell->sv_gylock);
408 goto success;
409
410 /* we found it - increment its ref count and return it */
David Howellsec268152007-04-26 15:49:28 -0700411found_server:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 afs_get_server(server);
413
David Howellsec268152007-04-26 15:49:28 -0700414success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 write_unlock(&cell->sv_lock);
416 read_unlock(&afs_cells_lock);
417
418 *_server = server;
419 _leave(" = 0 (s=%p c=%p)", server, cell);
420 return 0;
David Howellsec268152007-04-26 15:49:28 -0700421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
424 * purge in-memory cell database on module unload or afs_init() failure
425 * - the timeout daemon is stopped before calling this
426 */
427void afs_cell_purge(void)
428{
429 struct afs_vlocation *vlocation;
430 struct afs_cell *cell;
431
432 _enter("");
433
434 afs_put_cell(afs_cell_root);
435
436 while (!list_empty(&afs_cells)) {
437 cell = NULL;
438
439 /* remove the next cell from the front of the list */
440 write_lock(&afs_cells_lock);
441
442 if (!list_empty(&afs_cells)) {
443 cell = list_entry(afs_cells.next,
444 struct afs_cell, link);
445 list_del_init(&cell->link);
446 }
447
448 write_unlock(&afs_cells_lock);
449
450 if (cell) {
451 _debug("PURGING CELL %s (%d)",
452 cell->name, atomic_read(&cell->usage));
453
454 BUG_ON(!list_empty(&cell->sv_list));
455 BUG_ON(!list_empty(&cell->vl_list));
456
457 /* purge the cell's VL graveyard list */
458 _debug(" - clearing VL graveyard");
459
460 spin_lock(&cell->vl_gylock);
461
462 while (!list_empty(&cell->vl_graveyard)) {
463 vlocation = list_entry(cell->vl_graveyard.next,
464 struct afs_vlocation,
465 link);
466 list_del_init(&vlocation->link);
467
468 afs_kafstimod_del_timer(&vlocation->timeout);
469
470 spin_unlock(&cell->vl_gylock);
471
472 afs_vlocation_do_timeout(vlocation);
473 /* TODO: race if move to use krxtimod instead
474 * of kafstimod */
475
476 spin_lock(&cell->vl_gylock);
477 }
478
479 spin_unlock(&cell->vl_gylock);
480
481 /* purge the cell's server graveyard list */
482 _debug(" - clearing server graveyard");
483
484 spin_lock(&cell->sv_gylock);
485
486 while (!list_empty(&cell->sv_graveyard)) {
487 struct afs_server *server;
488
489 server = list_entry(cell->sv_graveyard.next,
490 struct afs_server, link);
491 list_del_init(&server->link);
492
493 afs_kafstimod_del_timer(&server->timeout);
494
495 spin_unlock(&cell->sv_gylock);
496
497 afs_server_do_timeout(server);
498
499 spin_lock(&cell->sv_gylock);
500 }
501
502 spin_unlock(&cell->sv_gylock);
503
504 /* now the cell should be left with no references */
505 afs_cell_destroy(cell);
506 }
507 }
508
509 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700510}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/*
513 * match a cell record obtained from the cache
514 */
515#ifdef AFS_CACHING_SUPPORT
516static cachefs_match_val_t afs_cell_cache_match(void *target,
517 const void *entry)
518{
519 const struct afs_cache_cell *ccell = entry;
520 struct afs_cell *cell = target;
521
522 _enter("{%s},{%s}", ccell->name, cell->name);
523
524 if (strncmp(ccell->name, cell->name, sizeof(ccell->name)) == 0) {
525 _leave(" = SUCCESS");
526 return CACHEFS_MATCH_SUCCESS;
527 }
528
529 _leave(" = FAILED");
530 return CACHEFS_MATCH_FAILED;
David Howellsec268152007-04-26 15:49:28 -0700531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#endif
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/*
535 * update a cell record in the cache
536 */
537#ifdef AFS_CACHING_SUPPORT
538static void afs_cell_cache_update(void *source, void *entry)
539{
540 struct afs_cache_cell *ccell = entry;
541 struct afs_cell *cell = source;
542
543 _enter("%p,%p", source, entry);
544
545 strncpy(ccell->name, cell->name, sizeof(ccell->name));
546
547 memcpy(ccell->vl_servers,
548 cell->vl_addrs,
549 min(sizeof(ccell->vl_servers), sizeof(cell->vl_addrs)));
550
David Howellsec268152007-04-26 15:49:28 -0700551}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552#endif