1 /* AFS cell and server record management
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <rxrpc/peer.h>
15 #include <rxrpc/connection.h>
19 #include "transport.h"
21 #include "kafstimod.h"
25 DECLARE_RWSEM(afs_proc_cells_sem);
26 LIST_HEAD(afs_proc_cells);
28 static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells);
29 static DEFINE_RWLOCK(afs_cells_lock);
30 static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
31 static struct afs_cell *afs_cell_root;
33 #ifdef AFS_CACHING_SUPPORT
34 static cachefs_match_val_t afs_cell_cache_match(void *target,
36 static void afs_cell_cache_update(void *source, void *entry);
38 struct cachefs_index_def afs_cache_cell_index_def = {
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,
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
52 int afs_cell_create(const char *name, char *vllist, struct afs_cell **_cell)
54 struct afs_cell *cell;
60 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
62 /* allocate and initialise a cell record */
63 cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL);
69 down_write(&afs_cells_sem);
71 memset(cell, 0, sizeof(struct afs_cell));
72 atomic_set(&cell->usage, 0);
74 INIT_LIST_HEAD(&cell->link);
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);
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);
86 strcpy(cell->name,name);
88 /* fill in the VL server list from the rest of the string */
93 next = strchr(vllist, ':');
97 if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
100 if (a > 255 || b > 255 || c > 255 || d > 255)
103 cell->vl_addrs[cell->vl_naddrs++].s_addr =
104 htonl((a << 24) | (b << 16) | (c << 8) | d);
106 if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS)
109 } while(vllist = next, vllist);
111 /* add a proc dir for this cell */
112 ret = afs_proc_cell_setup(cell);
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,
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);
129 down_write(&afs_proc_cells_sem);
130 list_add_tail(&cell->proc_link, &afs_proc_cells);
131 up_write(&afs_proc_cells_sem);
134 up_write(&afs_cells_sem);
136 _leave(" = 0 (%p)", cell);
140 printk(KERN_ERR "kAFS: bad VL server IP address: '%s'\n", vllist);
142 up_write(&afs_cells_sem);
144 _leave(" = %d", ret);
149 * initialise the cell database from module parameters
151 int afs_cell_init(char *rootcell)
153 struct afs_cell *old_root, *new_root;
160 /* module is loaded with no parameters, or built statically.
161 * - in the future we might initialize cell DB here.
163 _leave(" = 0 (but no root)");
167 cp = strchr(rootcell, ':');
169 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
170 _leave(" = %d (no colon)", -EINVAL);
174 /* allocate a cell record for the root cell */
176 ret = afs_cell_create(rootcell, cp, &new_root);
178 _leave(" = %d", ret);
182 /* as afs_put_cell() takes locks by itself, we have to do
183 * a little gymnastics to be race-free.
185 afs_get_cell(new_root);
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);
195 afs_cell_root = new_root;
196 write_unlock(&afs_cells_lock);
198 _leave(" = %d", ret);
203 * lookup a cell record
205 int afs_cell_lookup(const char *name, unsigned namesz, struct afs_cell **_cell)
207 struct afs_cell *cell;
210 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
215 /* if the cell was named, look for it in the cell record list */
218 read_lock(&afs_cells_lock);
220 list_for_each_entry(cell, &afs_cells, link) {
221 if (strncmp(cell->name, name, namesz) == 0) {
229 read_unlock(&afs_cells_lock);
234 read_lock(&afs_cells_lock);
236 cell = afs_cell_root;
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
250 read_unlock(&afs_cells_lock);
254 _leave(" = %d (%p)", ret, cell);
259 * try and get a cell record
261 struct afs_cell *afs_get_cell_maybe(struct afs_cell **_cell)
263 struct afs_cell *cell;
265 write_lock(&afs_cells_lock);
268 if (cell && !list_empty(&cell->link))
273 write_unlock(&afs_cells_lock);
279 * destroy a cell record
281 void afs_put_cell(struct afs_cell *cell)
286 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
289 BUG_ON(atomic_read(&cell->usage) <= 0);
291 /* to prevent a race, the decrement and the dequeue must be effectively
293 write_lock(&afs_cells_lock);
295 if (likely(!atomic_dec_and_test(&cell->usage))) {
296 write_unlock(&afs_cells_lock);
301 write_unlock(&afs_cells_lock);
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));
312 * destroy a cell record
314 static void afs_cell_destroy(struct afs_cell *cell)
316 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
318 /* to prevent a race, the decrement and the dequeue must be effectively
320 write_lock(&afs_cells_lock);
323 BUG_ON(atomic_read(&cell->usage) != 0);
325 list_del_init(&cell->link);
327 write_unlock(&afs_cells_lock);
329 down_write(&afs_cells_sem);
331 afs_proc_cell_remove(cell);
333 down_write(&afs_proc_cells_sem);
334 list_del_init(&cell->proc_link);
335 up_write(&afs_proc_cells_sem);
337 #ifdef AFS_CACHING_SUPPORT
338 cachefs_relinquish_cookie(cell->cache, 0);
341 up_write(&afs_cells_sem);
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));
348 /* finish cleaning up the cell */
351 _leave(" [destroyed]");
355 * lookup the server record corresponding to an Rx RPC peer
357 int afs_server_find_by_peer(const struct rxrpc_peer *peer,
358 struct afs_server **_server)
360 struct afs_server *server;
361 struct afs_cell *cell;
363 _enter("%p{a=%08x},", peer, ntohl(peer->addr.s_addr));
365 /* search the cell list */
366 read_lock(&afs_cells_lock);
368 list_for_each_entry(cell, &afs_cells, link) {
370 _debug("? cell %s",cell->name);
372 write_lock(&cell->sv_lock);
374 /* check the active list */
375 list_for_each_entry(server, &cell->sv_list, link) {
376 _debug("?? server %08x", ntohl(server->addr.s_addr));
378 if (memcmp(&server->addr, &peer->addr,
379 sizeof(struct in_addr)) == 0)
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));
389 if (memcmp(&server->addr, &peer->addr,
390 sizeof(struct in_addr)) == 0)
391 goto found_dead_server;
393 spin_unlock(&cell->sv_gylock);
395 write_unlock(&cell->sv_lock);
397 read_unlock(&afs_cells_lock);
399 _leave(" = -ENOENT");
402 /* we found it in the graveyard - resurrect it */
404 list_move_tail(&server->link, &cell->sv_list);
405 afs_get_server(server);
406 afs_kafstimod_del_timer(&server->timeout);
407 spin_unlock(&cell->sv_gylock);
410 /* we found it - increment its ref count and return it */
412 afs_get_server(server);
415 write_unlock(&cell->sv_lock);
416 read_unlock(&afs_cells_lock);
419 _leave(" = 0 (s=%p c=%p)", server, cell);
424 * purge in-memory cell database on module unload or afs_init() failure
425 * - the timeout daemon is stopped before calling this
427 void afs_cell_purge(void)
429 struct afs_vlocation *vlocation;
430 struct afs_cell *cell;
434 afs_put_cell(afs_cell_root);
436 while (!list_empty(&afs_cells)) {
439 /* remove the next cell from the front of the list */
440 write_lock(&afs_cells_lock);
442 if (!list_empty(&afs_cells)) {
443 cell = list_entry(afs_cells.next,
444 struct afs_cell, link);
445 list_del_init(&cell->link);
448 write_unlock(&afs_cells_lock);
451 _debug("PURGING CELL %s (%d)",
452 cell->name, atomic_read(&cell->usage));
454 BUG_ON(!list_empty(&cell->sv_list));
455 BUG_ON(!list_empty(&cell->vl_list));
457 /* purge the cell's VL graveyard list */
458 _debug(" - clearing VL graveyard");
460 spin_lock(&cell->vl_gylock);
462 while (!list_empty(&cell->vl_graveyard)) {
463 vlocation = list_entry(cell->vl_graveyard.next,
464 struct afs_vlocation,
466 list_del_init(&vlocation->link);
468 afs_kafstimod_del_timer(&vlocation->timeout);
470 spin_unlock(&cell->vl_gylock);
472 afs_vlocation_do_timeout(vlocation);
473 /* TODO: race if move to use krxtimod instead
476 spin_lock(&cell->vl_gylock);
479 spin_unlock(&cell->vl_gylock);
481 /* purge the cell's server graveyard list */
482 _debug(" - clearing server graveyard");
484 spin_lock(&cell->sv_gylock);
486 while (!list_empty(&cell->sv_graveyard)) {
487 struct afs_server *server;
489 server = list_entry(cell->sv_graveyard.next,
490 struct afs_server, link);
491 list_del_init(&server->link);
493 afs_kafstimod_del_timer(&server->timeout);
495 spin_unlock(&cell->sv_gylock);
497 afs_server_do_timeout(server);
499 spin_lock(&cell->sv_gylock);
502 spin_unlock(&cell->sv_gylock);
504 /* now the cell should be left with no references */
505 afs_cell_destroy(cell);
513 * match a cell record obtained from the cache
515 #ifdef AFS_CACHING_SUPPORT
516 static cachefs_match_val_t afs_cell_cache_match(void *target,
519 const struct afs_cache_cell *ccell = entry;
520 struct afs_cell *cell = target;
522 _enter("{%s},{%s}", ccell->name, cell->name);
524 if (strncmp(ccell->name, cell->name, sizeof(ccell->name)) == 0) {
525 _leave(" = SUCCESS");
526 return CACHEFS_MATCH_SUCCESS;
530 return CACHEFS_MATCH_FAILED;
535 * update a cell record in the cache
537 #ifdef AFS_CACHING_SUPPORT
538 static void afs_cell_cache_update(void *source, void *entry)
540 struct afs_cache_cell *ccell = entry;
541 struct afs_cell *cell = source;
543 _enter("%p,%p", source, entry);
545 strncpy(ccell->name, cell->name, sizeof(ccell->name));
547 memcpy(ccell->vl_servers,
549 min(sizeof(ccell->vl_servers), sizeof(cell->vl_addrs)));