1 /* cell.c: 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,
47 /*****************************************************************************/
49 * create a cell record
50 * - "name" is the name of the cell
51 * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
53 int afs_cell_create(const char *name, char *vllist, struct afs_cell **_cell)
55 struct afs_cell *cell;
61 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
63 /* allocate and initialise a cell record */
64 cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL);
70 down_write(&afs_cells_sem);
72 memset(cell, 0, sizeof(struct afs_cell));
73 atomic_set(&cell->usage, 0);
75 INIT_LIST_HEAD(&cell->link);
77 rwlock_init(&cell->sv_lock);
78 INIT_LIST_HEAD(&cell->sv_list);
79 INIT_LIST_HEAD(&cell->sv_graveyard);
80 spin_lock_init(&cell->sv_gylock);
82 init_rwsem(&cell->vl_sem);
83 INIT_LIST_HEAD(&cell->vl_list);
84 INIT_LIST_HEAD(&cell->vl_graveyard);
85 spin_lock_init(&cell->vl_gylock);
87 strcpy(cell->name,name);
89 /* fill in the VL server list from the rest of the string */
94 next = strchr(vllist, ':');
98 if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
101 if (a > 255 || b > 255 || c > 255 || d > 255)
104 cell->vl_addrs[cell->vl_naddrs++].s_addr =
105 htonl((a << 24) | (b << 16) | (c << 8) | d);
107 if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS)
110 } while(vllist = next, vllist);
112 /* add a proc dir for this cell */
113 ret = afs_proc_cell_setup(cell);
117 #ifdef AFS_CACHING_SUPPORT
118 /* put it up for caching */
119 cachefs_acquire_cookie(afs_cache_netfs.primary_index,
120 &afs_vlocation_cache_index_def,
125 /* add to the cell lists */
126 write_lock(&afs_cells_lock);
127 list_add_tail(&cell->link, &afs_cells);
128 write_unlock(&afs_cells_lock);
130 down_write(&afs_proc_cells_sem);
131 list_add_tail(&cell->proc_link, &afs_proc_cells);
132 up_write(&afs_proc_cells_sem);
135 up_write(&afs_cells_sem);
137 _leave(" = 0 (%p)", cell);
141 printk(KERN_ERR "kAFS: bad VL server IP address: '%s'\n", vllist);
143 up_write(&afs_cells_sem);
145 _leave(" = %d", ret);
147 } /* end afs_cell_create() */
149 /*****************************************************************************/
151 * initialise the cell database from module parameters
153 int afs_cell_init(char *rootcell)
155 struct afs_cell *old_root, *new_root;
162 /* module is loaded with no parameters, or built statically.
163 * - in the future we might initialize cell DB here.
165 _leave(" = 0 (but no root)");
169 cp = strchr(rootcell, ':');
171 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
172 _leave(" = %d (no colon)", -EINVAL);
176 /* allocate a cell record for the root cell */
178 ret = afs_cell_create(rootcell, cp, &new_root);
180 _leave(" = %d", ret);
184 /* as afs_put_cell() takes locks by itself, we have to do
185 * a little gymnastics to be race-free.
187 afs_get_cell(new_root);
189 write_lock(&afs_cells_lock);
190 while (afs_cell_root) {
191 old_root = afs_cell_root;
192 afs_cell_root = NULL;
193 write_unlock(&afs_cells_lock);
194 afs_put_cell(old_root);
195 write_lock(&afs_cells_lock);
197 afs_cell_root = new_root;
198 write_unlock(&afs_cells_lock);
200 _leave(" = %d", ret);
203 } /* end afs_cell_init() */
205 /*****************************************************************************/
207 * lookup a cell record
209 int afs_cell_lookup(const char *name, unsigned namesz, struct afs_cell **_cell)
211 struct afs_cell *cell;
214 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
219 /* if the cell was named, look for it in the cell record list */
222 read_lock(&afs_cells_lock);
224 list_for_each_entry(cell, &afs_cells, link) {
225 if (strncmp(cell->name, name, namesz) == 0) {
233 read_unlock(&afs_cells_lock);
239 read_lock(&afs_cells_lock);
241 cell = afs_cell_root;
243 /* this should not happen unless user tries to mount
244 * when root cell is not set. Return an impossibly
245 * bizzare errno to alert the user. Things like
246 * ENOENT might be "more appropriate" but they happen
256 read_unlock(&afs_cells_lock);
260 _leave(" = %d (%p)", ret, cell);
263 } /* end afs_cell_lookup() */
265 /*****************************************************************************/
267 * try and get a cell record
269 struct afs_cell *afs_get_cell_maybe(struct afs_cell **_cell)
271 struct afs_cell *cell;
273 write_lock(&afs_cells_lock);
276 if (cell && !list_empty(&cell->link))
281 write_unlock(&afs_cells_lock);
284 } /* end afs_get_cell_maybe() */
286 /*****************************************************************************/
288 * destroy a cell record
290 void afs_put_cell(struct afs_cell *cell)
295 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
298 BUG_ON(atomic_read(&cell->usage) <= 0);
300 /* to prevent a race, the decrement and the dequeue must be effectively
302 write_lock(&afs_cells_lock);
304 if (likely(!atomic_dec_and_test(&cell->usage))) {
305 write_unlock(&afs_cells_lock);
310 write_unlock(&afs_cells_lock);
312 BUG_ON(!list_empty(&cell->sv_list));
313 BUG_ON(!list_empty(&cell->sv_graveyard));
314 BUG_ON(!list_empty(&cell->vl_list));
315 BUG_ON(!list_empty(&cell->vl_graveyard));
318 } /* end afs_put_cell() */
320 /*****************************************************************************/
322 * destroy a cell record
324 static void afs_cell_destroy(struct afs_cell *cell)
326 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
328 /* to prevent a race, the decrement and the dequeue must be effectively
330 write_lock(&afs_cells_lock);
333 BUG_ON(atomic_read(&cell->usage) != 0);
335 list_del_init(&cell->link);
337 write_unlock(&afs_cells_lock);
339 down_write(&afs_cells_sem);
341 afs_proc_cell_remove(cell);
343 down_write(&afs_proc_cells_sem);
344 list_del_init(&cell->proc_link);
345 up_write(&afs_proc_cells_sem);
347 #ifdef AFS_CACHING_SUPPORT
348 cachefs_relinquish_cookie(cell->cache, 0);
351 up_write(&afs_cells_sem);
353 BUG_ON(!list_empty(&cell->sv_list));
354 BUG_ON(!list_empty(&cell->sv_graveyard));
355 BUG_ON(!list_empty(&cell->vl_list));
356 BUG_ON(!list_empty(&cell->vl_graveyard));
358 /* finish cleaning up the cell */
361 _leave(" [destroyed]");
362 } /* end afs_cell_destroy() */
364 /*****************************************************************************/
366 * lookup the server record corresponding to an Rx RPC peer
368 int afs_server_find_by_peer(const struct rxrpc_peer *peer,
369 struct afs_server **_server)
371 struct afs_server *server;
372 struct afs_cell *cell;
374 _enter("%p{a=%08x},", peer, ntohl(peer->addr.s_addr));
376 /* search the cell list */
377 read_lock(&afs_cells_lock);
379 list_for_each_entry(cell, &afs_cells, link) {
381 _debug("? cell %s",cell->name);
383 write_lock(&cell->sv_lock);
385 /* check the active list */
386 list_for_each_entry(server, &cell->sv_list, link) {
387 _debug("?? server %08x", ntohl(server->addr.s_addr));
389 if (memcmp(&server->addr, &peer->addr,
390 sizeof(struct in_addr)) == 0)
394 /* check the inactive list */
395 spin_lock(&cell->sv_gylock);
396 list_for_each_entry(server, &cell->sv_graveyard, link) {
397 _debug("?? dead server %08x",
398 ntohl(server->addr.s_addr));
400 if (memcmp(&server->addr, &peer->addr,
401 sizeof(struct in_addr)) == 0)
402 goto found_dead_server;
404 spin_unlock(&cell->sv_gylock);
406 write_unlock(&cell->sv_lock);
408 read_unlock(&afs_cells_lock);
410 _leave(" = -ENOENT");
413 /* we found it in the graveyard - resurrect it */
415 list_move_tail(&server->link, &cell->sv_list);
416 afs_get_server(server);
417 afs_kafstimod_del_timer(&server->timeout);
418 spin_unlock(&cell->sv_gylock);
421 /* we found it - increment its ref count and return it */
423 afs_get_server(server);
426 write_unlock(&cell->sv_lock);
427 read_unlock(&afs_cells_lock);
430 _leave(" = 0 (s=%p c=%p)", server, cell);
433 } /* end afs_server_find_by_peer() */
435 /*****************************************************************************/
437 * purge in-memory cell database on module unload or afs_init() failure
438 * - the timeout daemon is stopped before calling this
440 void afs_cell_purge(void)
442 struct afs_vlocation *vlocation;
443 struct afs_cell *cell;
447 afs_put_cell(afs_cell_root);
449 while (!list_empty(&afs_cells)) {
452 /* remove the next cell from the front of the list */
453 write_lock(&afs_cells_lock);
455 if (!list_empty(&afs_cells)) {
456 cell = list_entry(afs_cells.next,
457 struct afs_cell, link);
458 list_del_init(&cell->link);
461 write_unlock(&afs_cells_lock);
464 _debug("PURGING CELL %s (%d)",
465 cell->name, atomic_read(&cell->usage));
467 BUG_ON(!list_empty(&cell->sv_list));
468 BUG_ON(!list_empty(&cell->vl_list));
470 /* purge the cell's VL graveyard list */
471 _debug(" - clearing VL graveyard");
473 spin_lock(&cell->vl_gylock);
475 while (!list_empty(&cell->vl_graveyard)) {
476 vlocation = list_entry(cell->vl_graveyard.next,
477 struct afs_vlocation,
479 list_del_init(&vlocation->link);
481 afs_kafstimod_del_timer(&vlocation->timeout);
483 spin_unlock(&cell->vl_gylock);
485 afs_vlocation_do_timeout(vlocation);
486 /* TODO: race if move to use krxtimod instead
489 spin_lock(&cell->vl_gylock);
492 spin_unlock(&cell->vl_gylock);
494 /* purge the cell's server graveyard list */
495 _debug(" - clearing server graveyard");
497 spin_lock(&cell->sv_gylock);
499 while (!list_empty(&cell->sv_graveyard)) {
500 struct afs_server *server;
502 server = list_entry(cell->sv_graveyard.next,
503 struct afs_server, link);
504 list_del_init(&server->link);
506 afs_kafstimod_del_timer(&server->timeout);
508 spin_unlock(&cell->sv_gylock);
510 afs_server_do_timeout(server);
512 spin_lock(&cell->sv_gylock);
515 spin_unlock(&cell->sv_gylock);
517 /* now the cell should be left with no references */
518 afs_cell_destroy(cell);
523 } /* end afs_cell_purge() */
525 /*****************************************************************************/
527 * match a cell record obtained from the cache
529 #ifdef AFS_CACHING_SUPPORT
530 static cachefs_match_val_t afs_cell_cache_match(void *target,
533 const struct afs_cache_cell *ccell = entry;
534 struct afs_cell *cell = target;
536 _enter("{%s},{%s}", ccell->name, cell->name);
538 if (strncmp(ccell->name, cell->name, sizeof(ccell->name)) == 0) {
539 _leave(" = SUCCESS");
540 return CACHEFS_MATCH_SUCCESS;
544 return CACHEFS_MATCH_FAILED;
545 } /* end afs_cell_cache_match() */
548 /*****************************************************************************/
550 * update a cell record in the cache
552 #ifdef AFS_CACHING_SUPPORT
553 static void afs_cell_cache_update(void *source, void *entry)
555 struct afs_cache_cell *ccell = entry;
556 struct afs_cell *cell = source;
558 _enter("%p,%p", source, entry);
560 strncpy(ccell->name, cell->name, sizeof(ccell->name));
562 memcpy(ccell->vl_servers,
564 min(sizeof(ccell->vl_servers), sizeof(cell->vl_addrs)));
566 } /* end afs_cell_cache_update() */