DNS: If the DNS server returns an error, allow that to be cached [ver #2]
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / afs / cell.c
blobd0765883430e677693abbee2378fff8a0ea74bd6
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 <linux/key.h>
15 #include <linux/ctype.h>
16 #include <linux/dns_resolver.h>
17 #include <linux/sched.h>
18 #include <keys/rxrpc-type.h>
19 #include "internal.h"
21 DECLARE_RWSEM(afs_proc_cells_sem);
22 LIST_HEAD(afs_proc_cells);
24 static LIST_HEAD(afs_cells);
25 static DEFINE_RWLOCK(afs_cells_lock);
26 static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
27 static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
28 static struct afs_cell *afs_cell_root;
31 * allocate a cell record and fill in its name, VL server address list and
32 * allocate an anonymous key
34 static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
36 struct afs_cell *cell;
37 struct key *key;
38 size_t namelen;
39 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
40 char *dvllist = NULL, *_vllist = NULL;
41 char delimiter = ':';
42 int ret;
44 _enter("%s,%s", name, vllist);
46 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
48 namelen = strlen(name);
49 if (namelen > AFS_MAXCELLNAME) {
50 _leave(" = -ENAMETOOLONG");
51 return ERR_PTR(-ENAMETOOLONG);
54 /* allocate and initialise a cell record */
55 cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
56 if (!cell) {
57 _leave(" = -ENOMEM");
58 return ERR_PTR(-ENOMEM);
61 memcpy(cell->name, name, namelen);
62 cell->name[namelen] = 0;
64 atomic_set(&cell->usage, 1);
65 INIT_LIST_HEAD(&cell->link);
66 rwlock_init(&cell->servers_lock);
67 INIT_LIST_HEAD(&cell->servers);
68 init_rwsem(&cell->vl_sem);
69 INIT_LIST_HEAD(&cell->vl_list);
70 spin_lock_init(&cell->vl_lock);
72 /* if the ip address is invalid, try dns query */
73 if (!vllist || strlen(vllist) < 7) {
74 ret = dns_query("afsdb", name, namelen, "ipv4", &dvllist, NULL);
75 if (ret < 0) {
76 if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY)
77 /* translate these errors into something
78 * userspace might understand */
79 ret = -EDESTADDRREQ;
80 _leave(" = %d", ret);
81 return ERR_PTR(ret);
83 _vllist = dvllist;
85 /* change the delimiter for user-space reply */
86 delimiter = ',';
88 } else {
89 _vllist = vllist;
92 /* fill in the VL server list from the rest of the string */
93 do {
94 unsigned a, b, c, d;
96 next = strchr(_vllist, delimiter);
97 if (next)
98 *next++ = 0;
100 if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
101 goto bad_address;
103 if (a > 255 || b > 255 || c > 255 || d > 255)
104 goto bad_address;
106 cell->vl_addrs[cell->vl_naddrs++].s_addr =
107 htonl((a << 24) | (b << 16) | (c << 8) | d);
109 } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
111 /* create a key to represent an anonymous user */
112 memcpy(keyname, "afs@", 4);
113 dp = keyname + 4;
114 cp = cell->name;
115 do {
116 *dp++ = toupper(*cp);
117 } while (*cp++);
119 key = rxrpc_get_null_key(keyname);
120 if (IS_ERR(key)) {
121 _debug("no key");
122 ret = PTR_ERR(key);
123 goto error;
125 cell->anonymous_key = key;
127 _debug("anon key %p{%x}",
128 cell->anonymous_key, key_serial(cell->anonymous_key));
130 _leave(" = %p", cell);
131 return cell;
133 bad_address:
134 printk(KERN_ERR "kAFS: bad VL server IP address\n");
135 ret = -EINVAL;
136 error:
137 key_put(cell->anonymous_key);
138 kfree(dvllist);
139 kfree(cell);
140 _leave(" = %d", ret);
141 return ERR_PTR(ret);
145 * create a cell record
146 * - "name" is the name of the cell
147 * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
149 struct afs_cell *afs_cell_create(const char *name, char *vllist)
151 struct afs_cell *cell;
152 int ret;
154 _enter("%s,%s", name, vllist);
156 down_write(&afs_cells_sem);
157 read_lock(&afs_cells_lock);
158 list_for_each_entry(cell, &afs_cells, link) {
159 if (strcasecmp(cell->name, name) == 0)
160 goto duplicate_name;
162 read_unlock(&afs_cells_lock);
164 cell = afs_cell_alloc(name, vllist);
165 if (IS_ERR(cell)) {
166 _leave(" = %ld", PTR_ERR(cell));
167 up_write(&afs_cells_sem);
168 return cell;
171 /* add a proc directory for this cell */
172 ret = afs_proc_cell_setup(cell);
173 if (ret < 0)
174 goto error;
176 #ifdef CONFIG_AFS_FSCACHE
177 /* put it up for caching (this never returns an error) */
178 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
179 &afs_cell_cache_index_def,
180 cell);
181 #endif
183 /* add to the cell lists */
184 write_lock(&afs_cells_lock);
185 list_add_tail(&cell->link, &afs_cells);
186 write_unlock(&afs_cells_lock);
188 down_write(&afs_proc_cells_sem);
189 list_add_tail(&cell->proc_link, &afs_proc_cells);
190 up_write(&afs_proc_cells_sem);
191 up_write(&afs_cells_sem);
193 _leave(" = %p", cell);
194 return cell;
196 error:
197 up_write(&afs_cells_sem);
198 key_put(cell->anonymous_key);
199 kfree(cell);
200 _leave(" = %d", ret);
201 return ERR_PTR(ret);
203 duplicate_name:
204 read_unlock(&afs_cells_lock);
205 up_write(&afs_cells_sem);
206 return ERR_PTR(-EEXIST);
210 * set the root cell information
211 * - can be called with a module parameter string
212 * - can be called from a write to /proc/fs/afs/rootcell
214 int afs_cell_init(char *rootcell)
216 struct afs_cell *old_root, *new_root;
217 char *cp;
219 _enter("");
221 if (!rootcell) {
222 /* module is loaded with no parameters, or built statically.
223 * - in the future we might initialize cell DB here.
225 _leave(" = 0 [no root]");
226 return 0;
229 cp = strchr(rootcell, ':');
230 if (!cp)
231 _debug("kAFS: no VL server IP addresses specified");
232 else
233 *cp++ = 0;
235 /* allocate a cell record for the root cell */
236 new_root = afs_cell_create(rootcell, cp);
237 if (IS_ERR(new_root)) {
238 _leave(" = %ld", PTR_ERR(new_root));
239 return PTR_ERR(new_root);
242 /* install the new cell */
243 write_lock(&afs_cells_lock);
244 old_root = afs_cell_root;
245 afs_cell_root = new_root;
246 write_unlock(&afs_cells_lock);
247 afs_put_cell(old_root);
249 _leave(" = 0");
250 return 0;
254 * lookup a cell record
256 struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
258 struct afs_cell *cell;
260 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
262 down_read(&afs_cells_sem);
263 read_lock(&afs_cells_lock);
265 if (name) {
266 /* if the cell was named, look for it in the cell record list */
267 list_for_each_entry(cell, &afs_cells, link) {
268 if (strncmp(cell->name, name, namesz) == 0) {
269 afs_get_cell(cell);
270 goto found;
273 cell = ERR_PTR(-ENOENT);
274 found:
276 } else {
277 cell = afs_cell_root;
278 if (!cell) {
279 /* this should not happen unless user tries to mount
280 * when root cell is not set. Return an impossibly
281 * bizzare errno to alert the user. Things like
282 * ENOENT might be "more appropriate" but they happen
283 * for other reasons.
285 cell = ERR_PTR(-EDESTADDRREQ);
286 } else {
287 afs_get_cell(cell);
292 read_unlock(&afs_cells_lock);
293 up_read(&afs_cells_sem);
294 _leave(" = %p", cell);
295 return cell;
298 #if 0
300 * try and get a cell record
302 struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
304 write_lock(&afs_cells_lock);
306 if (cell && !list_empty(&cell->link))
307 afs_get_cell(cell);
308 else
309 cell = NULL;
311 write_unlock(&afs_cells_lock);
312 return cell;
314 #endif /* 0 */
317 * destroy a cell record
319 void afs_put_cell(struct afs_cell *cell)
321 if (!cell)
322 return;
324 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
326 ASSERTCMP(atomic_read(&cell->usage), >, 0);
328 /* to prevent a race, the decrement and the dequeue must be effectively
329 * atomic */
330 write_lock(&afs_cells_lock);
332 if (likely(!atomic_dec_and_test(&cell->usage))) {
333 write_unlock(&afs_cells_lock);
334 _leave("");
335 return;
338 ASSERT(list_empty(&cell->servers));
339 ASSERT(list_empty(&cell->vl_list));
341 write_unlock(&afs_cells_lock);
343 wake_up(&afs_cells_freeable_wq);
345 _leave(" [unused]");
349 * destroy a cell record
350 * - must be called with the afs_cells_sem write-locked
351 * - cell->link should have been broken by the caller
353 static void afs_cell_destroy(struct afs_cell *cell)
355 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
357 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
358 ASSERT(list_empty(&cell->link));
360 /* wait for everyone to stop using the cell */
361 if (atomic_read(&cell->usage) > 0) {
362 DECLARE_WAITQUEUE(myself, current);
364 _debug("wait for cell %s", cell->name);
365 set_current_state(TASK_UNINTERRUPTIBLE);
366 add_wait_queue(&afs_cells_freeable_wq, &myself);
368 while (atomic_read(&cell->usage) > 0) {
369 schedule();
370 set_current_state(TASK_UNINTERRUPTIBLE);
373 remove_wait_queue(&afs_cells_freeable_wq, &myself);
374 set_current_state(TASK_RUNNING);
377 _debug("cell dead");
378 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
379 ASSERT(list_empty(&cell->servers));
380 ASSERT(list_empty(&cell->vl_list));
382 afs_proc_cell_remove(cell);
384 down_write(&afs_proc_cells_sem);
385 list_del_init(&cell->proc_link);
386 up_write(&afs_proc_cells_sem);
388 #ifdef CONFIG_AFS_FSCACHE
389 fscache_relinquish_cookie(cell->cache, 0);
390 #endif
391 key_put(cell->anonymous_key);
392 kfree(cell);
394 _leave(" [destroyed]");
398 * purge in-memory cell database on module unload or afs_init() failure
399 * - the timeout daemon is stopped before calling this
401 void afs_cell_purge(void)
403 struct afs_cell *cell;
405 _enter("");
407 afs_put_cell(afs_cell_root);
409 down_write(&afs_cells_sem);
411 while (!list_empty(&afs_cells)) {
412 cell = NULL;
414 /* remove the next cell from the front of the list */
415 write_lock(&afs_cells_lock);
417 if (!list_empty(&afs_cells)) {
418 cell = list_entry(afs_cells.next,
419 struct afs_cell, link);
420 list_del_init(&cell->link);
423 write_unlock(&afs_cells_lock);
425 if (cell) {
426 _debug("PURGING CELL %s (%d)",
427 cell->name, atomic_read(&cell->usage));
429 /* now the cell should be left with no references */
430 afs_cell_destroy(cell);
434 up_write(&afs_cells_sem);
435 _leave("");