blob: 733c60246ab0dce5077e2ecddf8aa4b5428a2c54 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "internal.h"
15
16DECLARE_RWSEM(afs_proc_cells_sem);
17LIST_HEAD(afs_proc_cells);
18
19static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells);
20static DEFINE_RWLOCK(afs_cells_lock);
21static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
David Howells08e0e7c2007-04-26 15:55:03 -070022static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static struct afs_cell *afs_cell_root;
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
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
29 */
David Howells08e0e7c2007-04-26 15:55:03 -070030struct afs_cell *afs_cell_create(const char *name, char *vllist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct afs_cell *cell;
33 char *next;
34 int ret;
35
David Howells08e0e7c2007-04-26 15:55:03 -070036 _enter("%s,%s", name, vllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
39
40 /* allocate and initialise a cell record */
41 cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL);
42 if (!cell) {
43 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -070044 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46
47 down_write(&afs_cells_sem);
48
49 memset(cell, 0, sizeof(struct afs_cell));
David Howells08e0e7c2007-04-26 15:55:03 -070050 atomic_set(&cell->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 INIT_LIST_HEAD(&cell->link);
53
David Howells08e0e7c2007-04-26 15:55:03 -070054 rwlock_init(&cell->servers_lock);
55 INIT_LIST_HEAD(&cell->servers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 init_rwsem(&cell->vl_sem);
58 INIT_LIST_HEAD(&cell->vl_list);
David Howells08e0e7c2007-04-26 15:55:03 -070059 spin_lock_init(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
David Howells08e0e7c2007-04-26 15:55:03 -070061 strcpy(cell->name, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* fill in the VL server list from the rest of the string */
64 ret = -EINVAL;
65 do {
66 unsigned a, b, c, d;
67
68 next = strchr(vllist, ':');
69 if (next)
70 *next++ = 0;
71
72 if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
73 goto badaddr;
74
75 if (a > 255 || b > 255 || c > 255 || d > 255)
76 goto badaddr;
77
78 cell->vl_addrs[cell->vl_naddrs++].s_addr =
79 htonl((a << 24) | (b << 16) | (c << 8) | d);
80
81 if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS)
82 break;
83
David Howells08e0e7c2007-04-26 15:55:03 -070084 } while ((vllist = next));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
David Howells08e0e7c2007-04-26 15:55:03 -070086 /* add a proc directory for this cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ret = afs_proc_cell_setup(cell);
88 if (ret < 0)
89 goto error;
90
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,
95 cell,
96 &cell->cache);
97#endif
98
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);
103
104 down_write(&afs_proc_cells_sem);
105 list_add_tail(&cell->proc_link, &afs_proc_cells);
106 up_write(&afs_proc_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 up_write(&afs_cells_sem);
108
David Howells08e0e7c2007-04-26 15:55:03 -0700109 _leave(" = %p", cell);
110 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
David Howellsec268152007-04-26 15:49:28 -0700112badaddr:
David Howells08e0e7c2007-04-26 15:55:03 -0700113 printk(KERN_ERR "kAFS: bad VL server IP address\n");
David Howellsec268152007-04-26 15:49:28 -0700114error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 up_write(&afs_cells_sem);
116 kfree(cell);
117 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700118 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
David Howells08e0e7c2007-04-26 15:55:03 -0700122 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
126int afs_cell_init(char *rootcell)
127{
128 struct afs_cell *old_root, *new_root;
129 char *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 _enter("");
132
133 if (!rootcell) {
134 /* module is loaded with no parameters, or built statically.
135 * - in the future we might initialize cell DB here.
136 */
David Howells08e0e7c2007-04-26 15:55:03 -0700137 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139 }
140
141 cp = strchr(rootcell, ':');
142 if (!cp) {
143 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
David Howells08e0e7c2007-04-26 15:55:03 -0700144 _leave(" = -EINVAL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EINVAL;
146 }
147
148 /* allocate a cell record for the root cell */
149 *cp++ = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700150 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
David Howells08e0e7c2007-04-26 15:55:03 -0700156 /* install the new cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 write_lock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700158 old_root = afs_cell_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 afs_cell_root = new_root;
160 write_unlock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700161 afs_put_cell(old_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
David Howells08e0e7c2007-04-26 15:55:03 -0700163 _leave(" = 0");
164 return 0;
David Howellsec268152007-04-26 15:49:28 -0700165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
168 * lookup a cell record
169 */
David Howells08e0e7c2007-04-26 15:55:03 -0700170struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
175
David Howells08e0e7c2007-04-26 15:55:03 -0700176 down_read(&afs_cells_sem);
177 read_lock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (name) {
180 /* if the cell was named, look for it in the cell record list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 list_for_each_entry(cell, &afs_cells, link) {
182 if (strncmp(cell->name, name, namesz) == 0) {
183 afs_get_cell(cell);
184 goto found;
185 }
186 }
David Howells08e0e7c2007-04-26 15:55:03 -0700187 cell = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 found:
David Howells08e0e7c2007-04-26 15:55:03 -0700189 ;
David Howellsec268152007-04-26 15:49:28 -0700190 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 cell = afs_cell_root;
192 if (!cell) {
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
197 * for other reasons.
198 */
David Howells08e0e7c2007-04-26 15:55:03 -0700199 cell = ERR_PTR(-EDESTADDRREQ);
David Howellsec268152007-04-26 15:49:28 -0700200 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
David Howells08e0e7c2007-04-26 15:55:03 -0700206 read_unlock(&afs_cells_lock);
207 up_read(&afs_cells_sem);
208 _leave(" = %p", cell);
209 return cell;
David Howellsec268152007-04-26 15:49:28 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * try and get a cell record
214 */
David Howells08e0e7c2007-04-26 15:55:03 -0700215struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 write_lock(&afs_cells_lock);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (cell && !list_empty(&cell->link))
220 afs_get_cell(cell);
221 else
222 cell = NULL;
223
224 write_unlock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return cell;
David Howellsec268152007-04-26 15:49:28 -0700226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * destroy a cell record
230 */
231void afs_put_cell(struct afs_cell *cell)
232{
233 if (!cell)
234 return;
235
236 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
237
David Howells08e0e7c2007-04-26 15:55:03 -0700238 ASSERTCMP(atomic_read(&cell->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* to prevent a race, the decrement and the dequeue must be effectively
241 * atomic */
242 write_lock(&afs_cells_lock);
243
244 if (likely(!atomic_dec_and_test(&cell->usage))) {
245 write_unlock(&afs_cells_lock);
246 _leave("");
247 return;
248 }
249
David Howells08e0e7c2007-04-26 15:55:03 -0700250 ASSERT(list_empty(&cell->servers));
251 ASSERT(list_empty(&cell->vl_list));
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 write_unlock(&afs_cells_lock);
254
David Howells08e0e7c2007-04-26 15:55:03 -0700255 wake_up(&afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 _leave(" [unused]");
David Howellsec268152007-04-26 15:49:28 -0700258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*
261 * destroy a cell record
David Howells08e0e7c2007-04-26 15:55:03 -0700262 * - must be called with the afs_cells_sem write-locked
263 * - cell->link should have been broken by the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
265static void afs_cell_destroy(struct afs_cell *cell)
266{
267 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
268
David Howells08e0e7c2007-04-26 15:55:03 -0700269 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
270 ASSERT(list_empty(&cell->link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
David Howells08e0e7c2007-04-26 15:55:03 -0700272 /* wait for everyone to stop using the cell */
273 if (atomic_read(&cell->usage) > 0) {
274 DECLARE_WAITQUEUE(myself, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David Howells08e0e7c2007-04-26 15:55:03 -0700276 _debug("wait for cell %s", cell->name);
277 set_current_state(TASK_UNINTERRUPTIBLE);
278 add_wait_queue(&afs_cells_freeable_wq, &myself);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
David Howells08e0e7c2007-04-26 15:55:03 -0700280 while (atomic_read(&cell->usage) > 0) {
281 schedule();
282 set_current_state(TASK_UNINTERRUPTIBLE);
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
David Howells08e0e7c2007-04-26 15:55:03 -0700285 remove_wait_queue(&afs_cells_freeable_wq, &myself);
286 set_current_state(TASK_RUNNING);
287 }
288
289 _debug("cell dead");
290 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
291 ASSERT(list_empty(&cell->servers));
292 ASSERT(list_empty(&cell->vl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 afs_proc_cell_remove(cell);
295
296 down_write(&afs_proc_cells_sem);
297 list_del_init(&cell->proc_link);
298 up_write(&afs_proc_cells_sem);
299
300#ifdef AFS_CACHING_SUPPORT
301 cachefs_relinquish_cookie(cell->cache, 0);
302#endif
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 kfree(cell);
305
306 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * purge in-memory cell database on module unload or afs_init() failure
311 * - the timeout daemon is stopped before calling this
312 */
313void afs_cell_purge(void)
314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct afs_cell *cell;
316
317 _enter("");
318
319 afs_put_cell(afs_cell_root);
320
David Howells08e0e7c2007-04-26 15:55:03 -0700321 down_write(&afs_cells_sem);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 while (!list_empty(&afs_cells)) {
324 cell = NULL;
325
326 /* remove the next cell from the front of the list */
327 write_lock(&afs_cells_lock);
328
329 if (!list_empty(&afs_cells)) {
330 cell = list_entry(afs_cells.next,
331 struct afs_cell, link);
332 list_del_init(&cell->link);
333 }
334
335 write_unlock(&afs_cells_lock);
336
337 if (cell) {
338 _debug("PURGING CELL %s (%d)",
339 cell->name, atomic_read(&cell->usage));
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* now the cell should be left with no references */
342 afs_cell_destroy(cell);
343 }
344 }
345
David Howells08e0e7c2007-04-26 15:55:03 -0700346 up_write(&afs_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700348}