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>
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
;
39 char keyname
[4 + AFS_MAXCELLNAME
+ 1], *cp
, *dp
, *next
;
40 char *dvllist
= NULL
, *_vllist
= NULL
;
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
);
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
);
81 /* change the delimiter for user-space reply */
88 /* fill in the VL server list from the rest of the string */
92 next
= strchr(_vllist
, delimiter
);
96 if (sscanf(_vllist
, "%u.%u.%u.%u", &a
, &b
, &c
, &d
) != 4)
99 if (a
> 255 || b
> 255 || c
> 255 || d
> 255)
102 cell
->vl_addrs
[cell
->vl_naddrs
++].s_addr
=
103 htonl((a
<< 24) | (b
<< 16) | (c
<< 8) | d
);
105 } while (cell
->vl_naddrs
< AFS_CELL_MAX_ADDRS
&& (_vllist
= next
));
107 /* create a key to represent an anonymous user */
108 memcpy(keyname
, "afs@", 4);
112 *dp
++ = toupper(*cp
);
115 key
= rxrpc_get_null_key(keyname
);
121 cell
->anonymous_key
= key
;
123 _debug("anon key %p{%x}",
124 cell
->anonymous_key
, key_serial(cell
->anonymous_key
));
126 _leave(" = %p", cell
);
130 printk(KERN_ERR
"kAFS: bad VL server IP address\n");
133 key_put(cell
->anonymous_key
);
136 _leave(" = %d", ret
);
141 * create a cell record
142 * - "name" is the name of the cell
143 * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
145 struct afs_cell
*afs_cell_create(const char *name
, char *vllist
)
147 struct afs_cell
*cell
;
150 _enter("%s,%s", name
, vllist
);
152 down_write(&afs_cells_sem
);
153 read_lock(&afs_cells_lock
);
154 list_for_each_entry(cell
, &afs_cells
, link
) {
155 if (strcasecmp(cell
->name
, name
) == 0)
158 read_unlock(&afs_cells_lock
);
160 cell
= afs_cell_alloc(name
, vllist
);
162 _leave(" = %ld", PTR_ERR(cell
));
163 up_write(&afs_cells_sem
);
167 /* add a proc directory for this cell */
168 ret
= afs_proc_cell_setup(cell
);
172 #ifdef CONFIG_AFS_FSCACHE
173 /* put it up for caching (this never returns an error) */
174 cell
->cache
= fscache_acquire_cookie(afs_cache_netfs
.primary_index
,
175 &afs_cell_cache_index_def
,
179 /* add to the cell lists */
180 write_lock(&afs_cells_lock
);
181 list_add_tail(&cell
->link
, &afs_cells
);
182 write_unlock(&afs_cells_lock
);
184 down_write(&afs_proc_cells_sem
);
185 list_add_tail(&cell
->proc_link
, &afs_proc_cells
);
186 up_write(&afs_proc_cells_sem
);
187 up_write(&afs_cells_sem
);
189 _leave(" = %p", cell
);
193 up_write(&afs_cells_sem
);
194 key_put(cell
->anonymous_key
);
196 _leave(" = %d", ret
);
200 read_unlock(&afs_cells_lock
);
201 up_write(&afs_cells_sem
);
202 return ERR_PTR(-EEXIST
);
206 * set the root cell information
207 * - can be called with a module parameter string
208 * - can be called from a write to /proc/fs/afs/rootcell
210 int afs_cell_init(char *rootcell
)
212 struct afs_cell
*old_root
, *new_root
;
218 /* module is loaded with no parameters, or built statically.
219 * - in the future we might initialize cell DB here.
221 _leave(" = 0 [no root]");
225 cp
= strchr(rootcell
, ':');
227 _debug("kAFS: no VL server IP addresses specified");
231 /* allocate a cell record for the root cell */
232 new_root
= afs_cell_create(rootcell
, cp
);
233 if (IS_ERR(new_root
)) {
234 _leave(" = %ld", PTR_ERR(new_root
));
235 return PTR_ERR(new_root
);
238 /* install the new cell */
239 write_lock(&afs_cells_lock
);
240 old_root
= afs_cell_root
;
241 afs_cell_root
= new_root
;
242 write_unlock(&afs_cells_lock
);
243 afs_put_cell(old_root
);
250 * lookup a cell record
252 struct afs_cell
*afs_cell_lookup(const char *name
, unsigned namesz
)
254 struct afs_cell
*cell
;
256 _enter("\"%*.*s\",", namesz
, namesz
, name
? name
: "");
258 down_read(&afs_cells_sem
);
259 read_lock(&afs_cells_lock
);
262 /* if the cell was named, look for it in the cell record list */
263 list_for_each_entry(cell
, &afs_cells
, link
) {
264 if (strncmp(cell
->name
, name
, namesz
) == 0) {
269 cell
= ERR_PTR(-ENOENT
);
273 cell
= afs_cell_root
;
275 /* this should not happen unless user tries to mount
276 * when root cell is not set. Return an impossibly
277 * bizzare errno to alert the user. Things like
278 * ENOENT might be "more appropriate" but they happen
281 cell
= ERR_PTR(-EDESTADDRREQ
);
288 read_unlock(&afs_cells_lock
);
289 up_read(&afs_cells_sem
);
290 _leave(" = %p", cell
);
296 * try and get a cell record
298 struct afs_cell
*afs_get_cell_maybe(struct afs_cell
*cell
)
300 write_lock(&afs_cells_lock
);
302 if (cell
&& !list_empty(&cell
->link
))
307 write_unlock(&afs_cells_lock
);
313 * destroy a cell record
315 void afs_put_cell(struct afs_cell
*cell
)
320 _enter("%p{%d,%s}", cell
, atomic_read(&cell
->usage
), cell
->name
);
322 ASSERTCMP(atomic_read(&cell
->usage
), >, 0);
324 /* to prevent a race, the decrement and the dequeue must be effectively
326 write_lock(&afs_cells_lock
);
328 if (likely(!atomic_dec_and_test(&cell
->usage
))) {
329 write_unlock(&afs_cells_lock
);
334 ASSERT(list_empty(&cell
->servers
));
335 ASSERT(list_empty(&cell
->vl_list
));
337 write_unlock(&afs_cells_lock
);
339 wake_up(&afs_cells_freeable_wq
);
345 * destroy a cell record
346 * - must be called with the afs_cells_sem write-locked
347 * - cell->link should have been broken by the caller
349 static void afs_cell_destroy(struct afs_cell
*cell
)
351 _enter("%p{%d,%s}", cell
, atomic_read(&cell
->usage
), cell
->name
);
353 ASSERTCMP(atomic_read(&cell
->usage
), >=, 0);
354 ASSERT(list_empty(&cell
->link
));
356 /* wait for everyone to stop using the cell */
357 if (atomic_read(&cell
->usage
) > 0) {
358 DECLARE_WAITQUEUE(myself
, current
);
360 _debug("wait for cell %s", cell
->name
);
361 set_current_state(TASK_UNINTERRUPTIBLE
);
362 add_wait_queue(&afs_cells_freeable_wq
, &myself
);
364 while (atomic_read(&cell
->usage
) > 0) {
366 set_current_state(TASK_UNINTERRUPTIBLE
);
369 remove_wait_queue(&afs_cells_freeable_wq
, &myself
);
370 set_current_state(TASK_RUNNING
);
374 ASSERTCMP(atomic_read(&cell
->usage
), ==, 0);
375 ASSERT(list_empty(&cell
->servers
));
376 ASSERT(list_empty(&cell
->vl_list
));
378 afs_proc_cell_remove(cell
);
380 down_write(&afs_proc_cells_sem
);
381 list_del_init(&cell
->proc_link
);
382 up_write(&afs_proc_cells_sem
);
384 #ifdef CONFIG_AFS_FSCACHE
385 fscache_relinquish_cookie(cell
->cache
, 0);
387 key_put(cell
->anonymous_key
);
390 _leave(" [destroyed]");
394 * purge in-memory cell database on module unload or afs_init() failure
395 * - the timeout daemon is stopped before calling this
397 void afs_cell_purge(void)
399 struct afs_cell
*cell
;
403 afs_put_cell(afs_cell_root
);
405 down_write(&afs_cells_sem
);
407 while (!list_empty(&afs_cells
)) {
410 /* remove the next cell from the front of the list */
411 write_lock(&afs_cells_lock
);
413 if (!list_empty(&afs_cells
)) {
414 cell
= list_entry(afs_cells
.next
,
415 struct afs_cell
, link
);
416 list_del_init(&cell
->link
);
419 write_unlock(&afs_cells_lock
);
422 _debug("PURGING CELL %s (%d)",
423 cell
->name
, atomic_read(&cell
->usage
));
425 /* now the cell should be left with no references */
426 afs_cell_destroy(cell
);
430 up_write(&afs_cells_sem
);