blob: 9b1311a1df51d328921952d9159e46d88a6fc0cd [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>
David Howells00d3b7a2007-04-26 15:57:07 -070014#include <linux/key.h>
15#include <linux/ctype.h>
16#include <keys/rxrpc-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "internal.h"
18
19DECLARE_RWSEM(afs_proc_cells_sem);
20LIST_HEAD(afs_proc_cells);
21
22static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells);
23static DEFINE_RWLOCK(afs_cells_lock);
24static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
David Howells08e0e7c2007-04-26 15:55:03 -070025static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static struct afs_cell *afs_cell_root;
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
David Howells00d3b7a2007-04-26 15:57:07 -070029 * allocate a cell record and fill in its name, VL server address list and
30 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
David Howells00d3b7a2007-04-26 15:57:07 -070032static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 struct afs_cell *cell;
David Howells00d3b7a2007-04-26 15:57:07 -070035 size_t namelen;
36 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 int ret;
38
David Howells08e0e7c2007-04-26 15:55:03 -070039 _enter("%s,%s", name, vllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
42
David Howells00d3b7a2007-04-26 15:57:07 -070043 namelen = strlen(name);
44 if (namelen > AFS_MAXCELLNAME)
45 return ERR_PTR(-ENAMETOOLONG);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 /* allocate and initialise a cell record */
David Howells00d3b7a2007-04-26 15:57:07 -070048 cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (!cell) {
50 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -070051 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
53
David Howells00d3b7a2007-04-26 15:57:07 -070054 memcpy(cell->name, name, namelen);
55 cell->name[namelen] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David Howells08e0e7c2007-04-26 15:55:03 -070057 atomic_set(&cell->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 INIT_LIST_HEAD(&cell->link);
David Howells08e0e7c2007-04-26 15:55:03 -070059 rwlock_init(&cell->servers_lock);
60 INIT_LIST_HEAD(&cell->servers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 init_rwsem(&cell->vl_sem);
62 INIT_LIST_HEAD(&cell->vl_list);
David Howells08e0e7c2007-04-26 15:55:03 -070063 spin_lock_init(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 /* fill in the VL server list from the rest of the string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 do {
67 unsigned a, b, c, d;
68
69 next = strchr(vllist, ':');
70 if (next)
71 *next++ = 0;
72
73 if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
David Howells00d3b7a2007-04-26 15:57:07 -070074 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (a > 255 || b > 255 || c > 255 || d > 255)
David Howells00d3b7a2007-04-26 15:57:07 -070077 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 cell->vl_addrs[cell->vl_naddrs++].s_addr =
80 htonl((a << 24) | (b << 16) | (c << 8) | d);
81
David Howells00d3b7a2007-04-26 15:57:07 -070082 } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (vllist = next));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
David Howells00d3b7a2007-04-26 15:57:07 -070084 /* create a key to represent an anonymous user */
85 memcpy(keyname, "afs@", 4);
86 dp = keyname + 4;
87 cp = cell->name;
88 do {
89 *dp++ = toupper(*cp);
90 } while (*cp++);
91 cell->anonymous_key = key_alloc(&key_type_rxrpc, keyname, 0, 0, current,
92 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
93 if (IS_ERR(cell->anonymous_key)) {
94 _debug("no key");
95 ret = PTR_ERR(cell->anonymous_key);
96 goto error;
97 }
98
99 ret = key_instantiate_and_link(cell->anonymous_key, NULL, 0,
100 NULL, NULL);
101 if (ret < 0) {
102 _debug("instantiate failed");
103 goto error;
104 }
105
106 _debug("anon key %p{%x}",
107 cell->anonymous_key, key_serial(cell->anonymous_key));
108
109 _leave(" = %p", cell);
110 return cell;
111
112bad_address:
113 printk(KERN_ERR "kAFS: bad VL server IP address\n");
114 ret = -EINVAL;
115error:
116 key_put(cell->anonymous_key);
117 kfree(cell);
118 _leave(" = %d", ret);
119 return ERR_PTR(ret);
120}
121
122/*
123 * create a cell record
124 * - "name" is the name of the cell
125 * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
126 */
127struct afs_cell *afs_cell_create(const char *name, char *vllist)
128{
129 struct afs_cell *cell;
130 int ret;
131
132 _enter("%s,%s", name, vllist);
133
134 cell = afs_cell_alloc(name, vllist);
135 if (IS_ERR(cell)) {
136 _leave(" = %ld", PTR_ERR(cell));
137 return cell;
138 }
139
140 down_write(&afs_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
David Howells08e0e7c2007-04-26 15:55:03 -0700142 /* add a proc directory for this cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 ret = afs_proc_cell_setup(cell);
144 if (ret < 0)
145 goto error;
146
147#ifdef AFS_CACHING_SUPPORT
148 /* put it up for caching */
149 cachefs_acquire_cookie(afs_cache_netfs.primary_index,
150 &afs_vlocation_cache_index_def,
151 cell,
152 &cell->cache);
153#endif
154
155 /* add to the cell lists */
156 write_lock(&afs_cells_lock);
157 list_add_tail(&cell->link, &afs_cells);
158 write_unlock(&afs_cells_lock);
159
160 down_write(&afs_proc_cells_sem);
161 list_add_tail(&cell->proc_link, &afs_proc_cells);
162 up_write(&afs_proc_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 up_write(&afs_cells_sem);
164
David Howells08e0e7c2007-04-26 15:55:03 -0700165 _leave(" = %p", cell);
166 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David Howellsec268152007-04-26 15:49:28 -0700168error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 up_write(&afs_cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700170 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 kfree(cell);
172 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700173 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
David Howells08e0e7c2007-04-26 15:55:03 -0700177 * set the root cell information
178 * - can be called with a module parameter string
179 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181int afs_cell_init(char *rootcell)
182{
183 struct afs_cell *old_root, *new_root;
184 char *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 _enter("");
187
188 if (!rootcell) {
189 /* module is loaded with no parameters, or built statically.
190 * - in the future we might initialize cell DB here.
191 */
David Howells08e0e7c2007-04-26 15:55:03 -0700192 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
194 }
195
196 cp = strchr(rootcell, ':');
197 if (!cp) {
198 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
David Howells08e0e7c2007-04-26 15:55:03 -0700199 _leave(" = -EINVAL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EINVAL;
201 }
202
203 /* allocate a cell record for the root cell */
204 *cp++ = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700205 new_root = afs_cell_create(rootcell, cp);
206 if (IS_ERR(new_root)) {
207 _leave(" = %ld", PTR_ERR(new_root));
208 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
David Howells08e0e7c2007-04-26 15:55:03 -0700211 /* install the new cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 write_lock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700213 old_root = afs_cell_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 afs_cell_root = new_root;
215 write_unlock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700216 afs_put_cell(old_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
David Howells08e0e7c2007-04-26 15:55:03 -0700218 _leave(" = 0");
219 return 0;
David Howellsec268152007-04-26 15:49:28 -0700220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
223 * lookup a cell record
224 */
David Howells08e0e7c2007-04-26 15:55:03 -0700225struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
230
David Howells08e0e7c2007-04-26 15:55:03 -0700231 down_read(&afs_cells_sem);
232 read_lock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (name) {
235 /* if the cell was named, look for it in the cell record list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 list_for_each_entry(cell, &afs_cells, link) {
237 if (strncmp(cell->name, name, namesz) == 0) {
238 afs_get_cell(cell);
239 goto found;
240 }
241 }
David Howells08e0e7c2007-04-26 15:55:03 -0700242 cell = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 found:
David Howells08e0e7c2007-04-26 15:55:03 -0700244 ;
David Howellsec268152007-04-26 15:49:28 -0700245 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 cell = afs_cell_root;
247 if (!cell) {
248 /* this should not happen unless user tries to mount
249 * when root cell is not set. Return an impossibly
250 * bizzare errno to alert the user. Things like
251 * ENOENT might be "more appropriate" but they happen
252 * for other reasons.
253 */
David Howells08e0e7c2007-04-26 15:55:03 -0700254 cell = ERR_PTR(-EDESTADDRREQ);
David Howellsec268152007-04-26 15:49:28 -0700255 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
David Howells08e0e7c2007-04-26 15:55:03 -0700261 read_unlock(&afs_cells_lock);
262 up_read(&afs_cells_sem);
263 _leave(" = %p", cell);
264 return cell;
David Howellsec268152007-04-26 15:49:28 -0700265}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
268 * try and get a cell record
269 */
David Howells08e0e7c2007-04-26 15:55:03 -0700270struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 write_lock(&afs_cells_lock);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (cell && !list_empty(&cell->link))
275 afs_get_cell(cell);
276 else
277 cell = NULL;
278
279 write_unlock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return cell;
David Howellsec268152007-04-26 15:49:28 -0700281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*
284 * destroy a cell record
285 */
286void afs_put_cell(struct afs_cell *cell)
287{
288 if (!cell)
289 return;
290
291 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
292
David Howells08e0e7c2007-04-26 15:55:03 -0700293 ASSERTCMP(atomic_read(&cell->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* to prevent a race, the decrement and the dequeue must be effectively
296 * atomic */
297 write_lock(&afs_cells_lock);
298
299 if (likely(!atomic_dec_and_test(&cell->usage))) {
300 write_unlock(&afs_cells_lock);
301 _leave("");
302 return;
303 }
304
David Howells08e0e7c2007-04-26 15:55:03 -0700305 ASSERT(list_empty(&cell->servers));
306 ASSERT(list_empty(&cell->vl_list));
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 write_unlock(&afs_cells_lock);
309
David Howells08e0e7c2007-04-26 15:55:03 -0700310 wake_up(&afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 _leave(" [unused]");
David Howellsec268152007-04-26 15:49:28 -0700313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
316 * destroy a cell record
David Howells08e0e7c2007-04-26 15:55:03 -0700317 * - must be called with the afs_cells_sem write-locked
318 * - cell->link should have been broken by the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320static void afs_cell_destroy(struct afs_cell *cell)
321{
322 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
323
David Howells08e0e7c2007-04-26 15:55:03 -0700324 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
325 ASSERT(list_empty(&cell->link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
David Howells08e0e7c2007-04-26 15:55:03 -0700327 /* wait for everyone to stop using the cell */
328 if (atomic_read(&cell->usage) > 0) {
329 DECLARE_WAITQUEUE(myself, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
David Howells08e0e7c2007-04-26 15:55:03 -0700331 _debug("wait for cell %s", cell->name);
332 set_current_state(TASK_UNINTERRUPTIBLE);
333 add_wait_queue(&afs_cells_freeable_wq, &myself);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
David Howells08e0e7c2007-04-26 15:55:03 -0700335 while (atomic_read(&cell->usage) > 0) {
336 schedule();
337 set_current_state(TASK_UNINTERRUPTIBLE);
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
David Howells08e0e7c2007-04-26 15:55:03 -0700340 remove_wait_queue(&afs_cells_freeable_wq, &myself);
341 set_current_state(TASK_RUNNING);
342 }
343
344 _debug("cell dead");
345 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
346 ASSERT(list_empty(&cell->servers));
347 ASSERT(list_empty(&cell->vl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 afs_proc_cell_remove(cell);
350
351 down_write(&afs_proc_cells_sem);
352 list_del_init(&cell->proc_link);
353 up_write(&afs_proc_cells_sem);
354
355#ifdef AFS_CACHING_SUPPORT
356 cachefs_relinquish_cookie(cell->cache, 0);
357#endif
358
David Howells00d3b7a2007-04-26 15:57:07 -0700359 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 kfree(cell);
361
362 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * purge in-memory cell database on module unload or afs_init() failure
367 * - the timeout daemon is stopped before calling this
368 */
369void afs_cell_purge(void)
370{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct afs_cell *cell;
372
373 _enter("");
374
375 afs_put_cell(afs_cell_root);
376
David Howells08e0e7c2007-04-26 15:55:03 -0700377 down_write(&afs_cells_sem);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 while (!list_empty(&afs_cells)) {
380 cell = NULL;
381
382 /* remove the next cell from the front of the list */
383 write_lock(&afs_cells_lock);
384
385 if (!list_empty(&afs_cells)) {
386 cell = list_entry(afs_cells.next,
387 struct afs_cell, link);
388 list_del_init(&cell->link);
389 }
390
391 write_unlock(&afs_cells_lock);
392
393 if (cell) {
394 _debug("PURGING CELL %s (%d)",
395 cell->name, atomic_read(&cell->usage));
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* now the cell should be left with no references */
398 afs_cell_destroy(cell);
399 }
400 }
401
David Howells08e0e7c2007-04-26 15:55:03 -0700402 up_write(&afs_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700404}