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>
16 DECLARE_RWSEM(afs_proc_cells_sem);
17 LIST_HEAD(afs_proc_cells);
19 static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells);
20 static DEFINE_RWLOCK(afs_cells_lock);
21 static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
22 static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
23 static struct afs_cell *afs_cell_root;
26 * create a cell record
27 * - "name" is the name of the cell
28 * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
30 struct afs_cell *afs_cell_create(const char *name, char *vllist)
32 struct afs_cell *cell;
36 _enter("%s,%s", name, vllist);
38 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
40 /* allocate and initialise a cell record */
41 cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL);
44 return ERR_PTR(-ENOMEM);
47 down_write(&afs_cells_sem);
49 memset(cell, 0, sizeof(struct afs_cell));
50 atomic_set(&cell->usage, 1);
52 INIT_LIST_HEAD(&cell->link);
54 rwlock_init(&cell->servers_lock);
55 INIT_LIST_HEAD(&cell->servers);
57 init_rwsem(&cell->vl_sem);
58 INIT_LIST_HEAD(&cell->vl_list);
59 spin_lock_init(&cell->vl_lock);
61 strcpy(cell->name, name);
63 /* fill in the VL server list from the rest of the string */
68 next = strchr(vllist, ':');
72 if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
75 if (a > 255 || b > 255 || c > 255 || d > 255)
78 cell->vl_addrs[cell->vl_naddrs++].s_addr =
79 htonl((a << 24) | (b << 16) | (c << 8) | d);
81 if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS)
84 } while ((vllist = next));
86 /* add a proc directory for this cell */
87 ret = afs_proc_cell_setup(cell);
91 #ifdef AFS_CACHING_SUPPORT
92 /* put it up for caching */
93 cachefs_acquire_cookie(afs_cache_netfs.primary_index,
94 &afs_vlocation_cache_index_def,
99 /* add to the cell lists */
100 write_lock(&afs_cells_lock);
101 list_add_tail(&cell->link, &afs_cells);
102 write_unlock(&afs_cells_lock);
104 down_write(&afs_proc_cells_sem);
105 list_add_tail(&cell->proc_link, &afs_proc_cells);
106 up_write(&afs_proc_cells_sem);
107 up_write(&afs_cells_sem);
109 _leave(" = %p", cell);
113 printk(KERN_ERR "kAFS: bad VL server IP address\n");
115 up_write(&afs_cells_sem);
117 _leave(" = %d", ret);
122 * set the root cell information
123 * - can be called with a module parameter string
124 * - can be called from a write to /proc/fs/afs/rootcell
126 int afs_cell_init(char *rootcell)
128 struct afs_cell *old_root, *new_root;
134 /* module is loaded with no parameters, or built statically.
135 * - in the future we might initialize cell DB here.
137 _leave(" = 0 [no root]");
141 cp = strchr(rootcell, ':');
143 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
144 _leave(" = -EINVAL");
148 /* allocate a cell record for the root cell */
150 new_root = afs_cell_create(rootcell, cp);
151 if (IS_ERR(new_root)) {
152 _leave(" = %ld", PTR_ERR(new_root));
153 return PTR_ERR(new_root);
156 /* install the new cell */
157 write_lock(&afs_cells_lock);
158 old_root = afs_cell_root;
159 afs_cell_root = new_root;
160 write_unlock(&afs_cells_lock);
161 afs_put_cell(old_root);
168 * lookup a cell record
170 struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
172 struct afs_cell *cell;
174 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
176 down_read(&afs_cells_sem);
177 read_lock(&afs_cells_lock);
180 /* if the cell was named, look for it in the cell record list */
181 list_for_each_entry(cell, &afs_cells, link) {
182 if (strncmp(cell->name, name, namesz) == 0) {
187 cell = ERR_PTR(-ENOENT);
191 cell = afs_cell_root;
193 /* this should not happen unless user tries to mount
194 * when root cell is not set. Return an impossibly
195 * bizzare errno to alert the user. Things like
196 * ENOENT might be "more appropriate" but they happen
199 cell = ERR_PTR(-EDESTADDRREQ);
206 read_unlock(&afs_cells_lock);
207 up_read(&afs_cells_sem);
208 _leave(" = %p", cell);
213 * try and get a cell record
215 struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
217 write_lock(&afs_cells_lock);
219 if (cell && !list_empty(&cell->link))
224 write_unlock(&afs_cells_lock);
229 * destroy a cell record
231 void afs_put_cell(struct afs_cell *cell)
236 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
238 ASSERTCMP(atomic_read(&cell->usage), >, 0);
240 /* to prevent a race, the decrement and the dequeue must be effectively
242 write_lock(&afs_cells_lock);
244 if (likely(!atomic_dec_and_test(&cell->usage))) {
245 write_unlock(&afs_cells_lock);
250 ASSERT(list_empty(&cell->servers));
251 ASSERT(list_empty(&cell->vl_list));
253 write_unlock(&afs_cells_lock);
255 wake_up(&afs_cells_freeable_wq);
261 * destroy a cell record
262 * - must be called with the afs_cells_sem write-locked
263 * - cell->link should have been broken by the caller
265 static void afs_cell_destroy(struct afs_cell *cell)
267 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
269 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
270 ASSERT(list_empty(&cell->link));
272 /* wait for everyone to stop using the cell */
273 if (atomic_read(&cell->usage) > 0) {
274 DECLARE_WAITQUEUE(myself, current);
276 _debug("wait for cell %s", cell->name);
277 set_current_state(TASK_UNINTERRUPTIBLE);
278 add_wait_queue(&afs_cells_freeable_wq, &myself);
280 while (atomic_read(&cell->usage) > 0) {
282 set_current_state(TASK_UNINTERRUPTIBLE);
285 remove_wait_queue(&afs_cells_freeable_wq, &myself);
286 set_current_state(TASK_RUNNING);
290 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
291 ASSERT(list_empty(&cell->servers));
292 ASSERT(list_empty(&cell->vl_list));
294 afs_proc_cell_remove(cell);
296 down_write(&afs_proc_cells_sem);
297 list_del_init(&cell->proc_link);
298 up_write(&afs_proc_cells_sem);
300 #ifdef AFS_CACHING_SUPPORT
301 cachefs_relinquish_cookie(cell->cache, 0);
306 _leave(" [destroyed]");
310 * purge in-memory cell database on module unload or afs_init() failure
311 * - the timeout daemon is stopped before calling this
313 void afs_cell_purge(void)
315 struct afs_cell *cell;
319 afs_put_cell(afs_cell_root);
321 down_write(&afs_cells_sem);
323 while (!list_empty(&afs_cells)) {
326 /* remove the next cell from the front of the list */
327 write_lock(&afs_cells_lock);
329 if (!list_empty(&afs_cells)) {
330 cell = list_entry(afs_cells.next,
331 struct afs_cell, link);
332 list_del_init(&cell->link);
335 write_unlock(&afs_cells_lock);
338 _debug("PURGING CELL %s (%d)",
339 cell->name, atomic_read(&cell->usage));
341 /* now the cell should be left with no references */
342 afs_cell_destroy(cell);
346 up_write(&afs_cells_sem);