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/sched.h>
17 #include <keys/rxrpc-type.h>
20 DECLARE_RWSEM(afs_proc_cells_sem
);
21 LIST_HEAD(afs_proc_cells
);
23 static LIST_HEAD(afs_cells
);
24 static DEFINE_RWLOCK(afs_cells_lock
);
25 static DECLARE_RWSEM(afs_cells_sem
); /* add/remove serialisation */
26 static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq
);
27 static struct afs_cell
*afs_cell_root
;
30 * allocate a cell record and fill in its name, VL server address list and
31 * allocate an anonymous key
33 static struct afs_cell
*afs_cell_alloc(const char *name
, char *vllist
)
35 struct afs_cell
*cell
;
38 char keyname
[4 + AFS_MAXCELLNAME
+ 1], *cp
, *dp
, *next
;
41 _enter("%s,%s", name
, vllist
);
43 BUG_ON(!name
); /* TODO: want to look up "this cell" in the cache */
45 namelen
= strlen(name
);
46 if (namelen
> AFS_MAXCELLNAME
)
47 return ERR_PTR(-ENAMETOOLONG
);
49 /* allocate and initialise a cell record */
50 cell
= kzalloc(sizeof(struct afs_cell
) + namelen
+ 1, GFP_KERNEL
);
53 return ERR_PTR(-ENOMEM
);
56 memcpy(cell
->name
, name
, namelen
);
57 cell
->name
[namelen
] = 0;
59 atomic_set(&cell
->usage
, 1);
60 INIT_LIST_HEAD(&cell
->link
);
61 rwlock_init(&cell
->servers_lock
);
62 INIT_LIST_HEAD(&cell
->servers
);
63 init_rwsem(&cell
->vl_sem
);
64 INIT_LIST_HEAD(&cell
->vl_list
);
65 spin_lock_init(&cell
->vl_lock
);
67 /* fill in the VL server list from the rest of the string */
71 next
= strchr(vllist
, ':');
75 if (sscanf(vllist
, "%u.%u.%u.%u", &a
, &b
, &c
, &d
) != 4)
78 if (a
> 255 || b
> 255 || c
> 255 || d
> 255)
81 cell
->vl_addrs
[cell
->vl_naddrs
++].s_addr
=
82 htonl((a
<< 24) | (b
<< 16) | (c
<< 8) | d
);
84 } while (cell
->vl_naddrs
< AFS_CELL_MAX_ADDRS
&& (vllist
= next
));
86 /* create a key to represent an anonymous user */
87 memcpy(keyname
, "afs@", 4);
94 key
= rxrpc_get_null_key(keyname
);
100 cell
->anonymous_key
= key
;
102 _debug("anon key %p{%x}",
103 cell
->anonymous_key
, key_serial(cell
->anonymous_key
));
105 _leave(" = %p", cell
);
109 printk(KERN_ERR
"kAFS: bad VL server IP address\n");
112 key_put(cell
->anonymous_key
);
114 _leave(" = %d", ret
);
119 * create a cell record
120 * - "name" is the name of the cell
121 * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
123 struct afs_cell
*afs_cell_create(const char *name
, char *vllist
)
125 struct afs_cell
*cell
;
128 _enter("%s,%s", name
, vllist
);
130 down_write(&afs_cells_sem
);
131 read_lock(&afs_cells_lock
);
132 list_for_each_entry(cell
, &afs_cells
, link
) {
133 if (strcasecmp(cell
->name
, name
) == 0)
136 read_unlock(&afs_cells_lock
);
138 cell
= afs_cell_alloc(name
, vllist
);
140 _leave(" = %ld", PTR_ERR(cell
));
141 up_write(&afs_cells_sem
);
145 /* add a proc directory for this cell */
146 ret
= afs_proc_cell_setup(cell
);
150 #ifdef AFS_CACHING_SUPPORT
151 /* put it up for caching */
152 cachefs_acquire_cookie(afs_cache_netfs
.primary_index
,
153 &afs_vlocation_cache_index_def
,
158 /* add to the cell lists */
159 write_lock(&afs_cells_lock
);
160 list_add_tail(&cell
->link
, &afs_cells
);
161 write_unlock(&afs_cells_lock
);
163 down_write(&afs_proc_cells_sem
);
164 list_add_tail(&cell
->proc_link
, &afs_proc_cells
);
165 up_write(&afs_proc_cells_sem
);
166 up_write(&afs_cells_sem
);
168 _leave(" = %p", cell
);
172 up_write(&afs_cells_sem
);
173 key_put(cell
->anonymous_key
);
175 _leave(" = %d", ret
);
179 read_unlock(&afs_cells_lock
);
180 up_write(&afs_cells_sem
);
181 return ERR_PTR(-EEXIST
);
185 * set the root cell information
186 * - can be called with a module parameter string
187 * - can be called from a write to /proc/fs/afs/rootcell
189 int afs_cell_init(char *rootcell
)
191 struct afs_cell
*old_root
, *new_root
;
197 /* module is loaded with no parameters, or built statically.
198 * - in the future we might initialize cell DB here.
200 _leave(" = 0 [no root]");
204 cp
= strchr(rootcell
, ':');
206 printk(KERN_ERR
"kAFS: no VL server IP addresses specified\n");
207 _leave(" = -EINVAL");
211 /* allocate a cell record for the root cell */
213 new_root
= afs_cell_create(rootcell
, cp
);
214 if (IS_ERR(new_root
)) {
215 _leave(" = %ld", PTR_ERR(new_root
));
216 return PTR_ERR(new_root
);
219 /* install the new cell */
220 write_lock(&afs_cells_lock
);
221 old_root
= afs_cell_root
;
222 afs_cell_root
= new_root
;
223 write_unlock(&afs_cells_lock
);
224 afs_put_cell(old_root
);
231 * lookup a cell record
233 struct afs_cell
*afs_cell_lookup(const char *name
, unsigned namesz
)
235 struct afs_cell
*cell
;
237 _enter("\"%*.*s\",", namesz
, namesz
, name
? name
: "");
239 down_read(&afs_cells_sem
);
240 read_lock(&afs_cells_lock
);
243 /* if the cell was named, look for it in the cell record list */
244 list_for_each_entry(cell
, &afs_cells
, link
) {
245 if (strncmp(cell
->name
, name
, namesz
) == 0) {
250 cell
= ERR_PTR(-ENOENT
);
254 cell
= afs_cell_root
;
256 /* this should not happen unless user tries to mount
257 * when root cell is not set. Return an impossibly
258 * bizzare errno to alert the user. Things like
259 * ENOENT might be "more appropriate" but they happen
262 cell
= ERR_PTR(-EDESTADDRREQ
);
269 read_unlock(&afs_cells_lock
);
270 up_read(&afs_cells_sem
);
271 _leave(" = %p", cell
);
277 * try and get a cell record
279 struct afs_cell
*afs_get_cell_maybe(struct afs_cell
*cell
)
281 write_lock(&afs_cells_lock
);
283 if (cell
&& !list_empty(&cell
->link
))
288 write_unlock(&afs_cells_lock
);
294 * destroy a cell record
296 void afs_put_cell(struct afs_cell
*cell
)
301 _enter("%p{%d,%s}", cell
, atomic_read(&cell
->usage
), cell
->name
);
303 ASSERTCMP(atomic_read(&cell
->usage
), >, 0);
305 /* to prevent a race, the decrement and the dequeue must be effectively
307 write_lock(&afs_cells_lock
);
309 if (likely(!atomic_dec_and_test(&cell
->usage
))) {
310 write_unlock(&afs_cells_lock
);
315 ASSERT(list_empty(&cell
->servers
));
316 ASSERT(list_empty(&cell
->vl_list
));
318 write_unlock(&afs_cells_lock
);
320 wake_up(&afs_cells_freeable_wq
);
326 * destroy a cell record
327 * - must be called with the afs_cells_sem write-locked
328 * - cell->link should have been broken by the caller
330 static void afs_cell_destroy(struct afs_cell
*cell
)
332 _enter("%p{%d,%s}", cell
, atomic_read(&cell
->usage
), cell
->name
);
334 ASSERTCMP(atomic_read(&cell
->usage
), >=, 0);
335 ASSERT(list_empty(&cell
->link
));
337 /* wait for everyone to stop using the cell */
338 if (atomic_read(&cell
->usage
) > 0) {
339 DECLARE_WAITQUEUE(myself
, current
);
341 _debug("wait for cell %s", cell
->name
);
342 set_current_state(TASK_UNINTERRUPTIBLE
);
343 add_wait_queue(&afs_cells_freeable_wq
, &myself
);
345 while (atomic_read(&cell
->usage
) > 0) {
347 set_current_state(TASK_UNINTERRUPTIBLE
);
350 remove_wait_queue(&afs_cells_freeable_wq
, &myself
);
351 set_current_state(TASK_RUNNING
);
355 ASSERTCMP(atomic_read(&cell
->usage
), ==, 0);
356 ASSERT(list_empty(&cell
->servers
));
357 ASSERT(list_empty(&cell
->vl_list
));
359 afs_proc_cell_remove(cell
);
361 down_write(&afs_proc_cells_sem
);
362 list_del_init(&cell
->proc_link
);
363 up_write(&afs_proc_cells_sem
);
365 #ifdef AFS_CACHING_SUPPORT
366 cachefs_relinquish_cookie(cell
->cache
, 0);
369 key_put(cell
->anonymous_key
);
372 _leave(" [destroyed]");
376 * purge in-memory cell database on module unload or afs_init() failure
377 * - the timeout daemon is stopped before calling this
379 void afs_cell_purge(void)
381 struct afs_cell
*cell
;
385 afs_put_cell(afs_cell_root
);
387 down_write(&afs_cells_sem
);
389 while (!list_empty(&afs_cells
)) {
392 /* remove the next cell from the front of the list */
393 write_lock(&afs_cells_lock
);
395 if (!list_empty(&afs_cells
)) {
396 cell
= list_entry(afs_cells
.next
,
397 struct afs_cell
, link
);
398 list_del_init(&cell
->link
);
401 write_unlock(&afs_cells_lock
);
404 _debug("PURGING CELL %s (%d)",
405 cell
->name
, atomic_read(&cell
->usage
));
407 /* now the cell should be left with no references */
408 afs_cell_destroy(cell
);
412 up_write(&afs_cells_sem
);