blob: e19c13f059ed5fa8ec0bcac9f0ce80377476b26c [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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
David Howells00d3b7a2007-04-26 15:57:07 -070017#include <keys/rxrpc-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "internal.h"
19
20DECLARE_RWSEM(afs_proc_cells_sem);
21LIST_HEAD(afs_proc_cells);
22
Robert P. J. Day0ae52d62008-04-29 01:03:20 -070023static LIST_HEAD(afs_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static DEFINE_RWLOCK(afs_cells_lock);
25static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
David Howells08e0e7c2007-04-26 15:55:03 -070026static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static struct afs_cell *afs_cell_root;
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
David Howells00d3b7a2007-04-26 15:57:07 -070030 * allocate a cell record and fill in its name, VL server address list and
31 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
David Howells00d3b7a2007-04-26 15:57:07 -070033static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct afs_cell *cell;
David Howells76181c12007-10-16 23:29:46 -070036 struct key *key;
David Howells00d3b7a2007-04-26 15:57:07 -070037 size_t namelen;
38 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int ret;
40
David Howells08e0e7c2007-04-26 15:55:03 -070041 _enter("%s,%s", name, vllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
44
David Howells00d3b7a2007-04-26 15:57:07 -070045 namelen = strlen(name);
46 if (namelen > AFS_MAXCELLNAME)
47 return ERR_PTR(-ENAMETOOLONG);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /* allocate and initialise a cell record */
David Howells00d3b7a2007-04-26 15:57:07 -070050 cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (!cell) {
52 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -070053 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
55
David Howells00d3b7a2007-04-26 15:57:07 -070056 memcpy(cell->name, name, namelen);
57 cell->name[namelen] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Howells08e0e7c2007-04-26 15:55:03 -070059 atomic_set(&cell->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 INIT_LIST_HEAD(&cell->link);
David Howells08e0e7c2007-04-26 15:55:03 -070061 rwlock_init(&cell->servers_lock);
62 INIT_LIST_HEAD(&cell->servers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 init_rwsem(&cell->vl_sem);
64 INIT_LIST_HEAD(&cell->vl_list);
David Howells08e0e7c2007-04-26 15:55:03 -070065 spin_lock_init(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /* fill in the VL server list from the rest of the string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 do {
69 unsigned a, b, c, d;
70
71 next = strchr(vllist, ':');
72 if (next)
73 *next++ = 0;
74
75 if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
David Howells00d3b7a2007-04-26 15:57:07 -070076 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (a > 255 || b > 255 || c > 255 || d > 255)
David Howells00d3b7a2007-04-26 15:57:07 -070079 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 cell->vl_addrs[cell->vl_naddrs++].s_addr =
82 htonl((a << 24) | (b << 16) | (c << 8) | d);
83
David Howells00d3b7a2007-04-26 15:57:07 -070084 } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (vllist = next));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
David Howells00d3b7a2007-04-26 15:57:07 -070086 /* create a key to represent an anonymous user */
87 memcpy(keyname, "afs@", 4);
88 dp = keyname + 4;
89 cp = cell->name;
90 do {
91 *dp++ = toupper(*cp);
92 } while (*cp++);
David Howells00d3b7a2007-04-26 15:57:07 -070093
David Howells76181c12007-10-16 23:29:46 -070094 key = rxrpc_get_null_key(keyname);
95 if (IS_ERR(key)) {
96 _debug("no key");
97 ret = PTR_ERR(key);
David Howells00d3b7a2007-04-26 15:57:07 -070098 goto error;
99 }
David Howells76181c12007-10-16 23:29:46 -0700100 cell->anonymous_key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700101
102 _debug("anon key %p{%x}",
103 cell->anonymous_key, key_serial(cell->anonymous_key));
104
105 _leave(" = %p", cell);
106 return cell;
107
108bad_address:
109 printk(KERN_ERR "kAFS: bad VL server IP address\n");
110 ret = -EINVAL;
111error:
112 key_put(cell->anonymous_key);
113 kfree(cell);
114 _leave(" = %d", ret);
115 return ERR_PTR(ret);
116}
117
118/*
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
122 */
123struct afs_cell *afs_cell_create(const char *name, char *vllist)
124{
125 struct afs_cell *cell;
126 int ret;
127
128 _enter("%s,%s", name, vllist);
129
Sven Schnelle5214b722008-03-28 14:15:55 -0700130 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)
134 goto duplicate_name;
135 }
136 read_unlock(&afs_cells_lock);
137
David Howells00d3b7a2007-04-26 15:57:07 -0700138 cell = afs_cell_alloc(name, vllist);
139 if (IS_ERR(cell)) {
140 _leave(" = %ld", PTR_ERR(cell));
Sven Schnellea5f37c32008-04-02 13:17:18 +0100141 up_write(&afs_cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700142 return cell;
143 }
144
David Howells08e0e7c2007-04-26 15:55:03 -0700145 /* add a proc directory for this cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 ret = afs_proc_cell_setup(cell);
147 if (ret < 0)
148 goto error;
149
David Howells9b3f26c2009-04-03 16:42:41 +0100150#ifdef CONFIG_AFS_FSCACHE
151 /* put it up for caching (this never returns an error) */
152 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
153 &afs_cell_cache_index_def,
154 cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#endif
156
157 /* add to the cell lists */
158 write_lock(&afs_cells_lock);
159 list_add_tail(&cell->link, &afs_cells);
160 write_unlock(&afs_cells_lock);
161
162 down_write(&afs_proc_cells_sem);
163 list_add_tail(&cell->proc_link, &afs_proc_cells);
164 up_write(&afs_proc_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 up_write(&afs_cells_sem);
166
David Howells08e0e7c2007-04-26 15:55:03 -0700167 _leave(" = %p", cell);
168 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
David Howellsec268152007-04-26 15:49:28 -0700170error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 up_write(&afs_cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700172 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kfree(cell);
174 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700175 return ERR_PTR(ret);
Sven Schnelle5214b722008-03-28 14:15:55 -0700176
177duplicate_name:
178 read_unlock(&afs_cells_lock);
179 up_write(&afs_cells_sem);
180 return ERR_PTR(-EEXIST);
David Howellsec268152007-04-26 15:49:28 -0700181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
David Howells08e0e7c2007-04-26 15:55:03 -0700184 * set the root cell information
185 * - can be called with a module parameter string
186 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188int afs_cell_init(char *rootcell)
189{
190 struct afs_cell *old_root, *new_root;
191 char *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 _enter("");
194
195 if (!rootcell) {
196 /* module is loaded with no parameters, or built statically.
197 * - in the future we might initialize cell DB here.
198 */
David Howells08e0e7c2007-04-26 15:55:03 -0700199 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201 }
202
203 cp = strchr(rootcell, ':');
204 if (!cp) {
205 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
David Howells08e0e7c2007-04-26 15:55:03 -0700206 _leave(" = -EINVAL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -EINVAL;
208 }
209
210 /* allocate a cell record for the root cell */
211 *cp++ = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700212 new_root = afs_cell_create(rootcell, cp);
213 if (IS_ERR(new_root)) {
214 _leave(" = %ld", PTR_ERR(new_root));
215 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
David Howells08e0e7c2007-04-26 15:55:03 -0700218 /* install the new cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 write_lock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700220 old_root = afs_cell_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 afs_cell_root = new_root;
222 write_unlock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700223 afs_put_cell(old_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
David Howells08e0e7c2007-04-26 15:55:03 -0700225 _leave(" = 0");
226 return 0;
David Howellsec268152007-04-26 15:49:28 -0700227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
230 * lookup a cell record
231 */
David Howells08e0e7c2007-04-26 15:55:03 -0700232struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
237
David Howells08e0e7c2007-04-26 15:55:03 -0700238 down_read(&afs_cells_sem);
239 read_lock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (name) {
242 /* if the cell was named, look for it in the cell record list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 list_for_each_entry(cell, &afs_cells, link) {
244 if (strncmp(cell->name, name, namesz) == 0) {
245 afs_get_cell(cell);
246 goto found;
247 }
248 }
David Howells08e0e7c2007-04-26 15:55:03 -0700249 cell = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 found:
David Howells08e0e7c2007-04-26 15:55:03 -0700251 ;
David Howellsec268152007-04-26 15:49:28 -0700252 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 cell = afs_cell_root;
254 if (!cell) {
255 /* this should not happen unless user tries to mount
256 * when root cell is not set. Return an impossibly
257 * bizzare errno to alert the user. Things like
258 * ENOENT might be "more appropriate" but they happen
259 * for other reasons.
260 */
David Howells08e0e7c2007-04-26 15:55:03 -0700261 cell = ERR_PTR(-EDESTADDRREQ);
David Howellsec268152007-04-26 15:49:28 -0700262 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
David Howells08e0e7c2007-04-26 15:55:03 -0700268 read_unlock(&afs_cells_lock);
269 up_read(&afs_cells_sem);
270 _leave(" = %p", cell);
271 return cell;
David Howellsec268152007-04-26 15:49:28 -0700272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Adrian Bunkc1206a22007-10-16 23:26:41 -0700274#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/*
276 * try and get a cell record
277 */
David Howells08e0e7c2007-04-26 15:55:03 -0700278struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 write_lock(&afs_cells_lock);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cell && !list_empty(&cell->link))
283 afs_get_cell(cell);
284 else
285 cell = NULL;
286
287 write_unlock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return cell;
David Howellsec268152007-04-26 15:49:28 -0700289}
Adrian Bunkc1206a22007-10-16 23:26:41 -0700290#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * destroy a cell record
294 */
295void afs_put_cell(struct afs_cell *cell)
296{
297 if (!cell)
298 return;
299
300 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
301
David Howells08e0e7c2007-04-26 15:55:03 -0700302 ASSERTCMP(atomic_read(&cell->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* to prevent a race, the decrement and the dequeue must be effectively
305 * atomic */
306 write_lock(&afs_cells_lock);
307
308 if (likely(!atomic_dec_and_test(&cell->usage))) {
309 write_unlock(&afs_cells_lock);
310 _leave("");
311 return;
312 }
313
David Howells08e0e7c2007-04-26 15:55:03 -0700314 ASSERT(list_empty(&cell->servers));
315 ASSERT(list_empty(&cell->vl_list));
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 write_unlock(&afs_cells_lock);
318
David Howells08e0e7c2007-04-26 15:55:03 -0700319 wake_up(&afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 _leave(" [unused]");
David Howellsec268152007-04-26 15:49:28 -0700322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * destroy a cell record
David Howells08e0e7c2007-04-26 15:55:03 -0700326 * - must be called with the afs_cells_sem write-locked
327 * - cell->link should have been broken by the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
329static void afs_cell_destroy(struct afs_cell *cell)
330{
331 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
332
David Howells08e0e7c2007-04-26 15:55:03 -0700333 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
334 ASSERT(list_empty(&cell->link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Howells08e0e7c2007-04-26 15:55:03 -0700336 /* wait for everyone to stop using the cell */
337 if (atomic_read(&cell->usage) > 0) {
338 DECLARE_WAITQUEUE(myself, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
David Howells08e0e7c2007-04-26 15:55:03 -0700340 _debug("wait for cell %s", cell->name);
341 set_current_state(TASK_UNINTERRUPTIBLE);
342 add_wait_queue(&afs_cells_freeable_wq, &myself);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
David Howells08e0e7c2007-04-26 15:55:03 -0700344 while (atomic_read(&cell->usage) > 0) {
345 schedule();
346 set_current_state(TASK_UNINTERRUPTIBLE);
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
David Howells08e0e7c2007-04-26 15:55:03 -0700349 remove_wait_queue(&afs_cells_freeable_wq, &myself);
350 set_current_state(TASK_RUNNING);
351 }
352
353 _debug("cell dead");
354 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
355 ASSERT(list_empty(&cell->servers));
356 ASSERT(list_empty(&cell->vl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 afs_proc_cell_remove(cell);
359
360 down_write(&afs_proc_cells_sem);
361 list_del_init(&cell->proc_link);
362 up_write(&afs_proc_cells_sem);
363
David Howells9b3f26c2009-04-03 16:42:41 +0100364#ifdef CONFIG_AFS_FSCACHE
365 fscache_relinquish_cookie(cell->cache, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#endif
David Howells00d3b7a2007-04-26 15:57:07 -0700367 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 kfree(cell);
369
370 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * purge in-memory cell database on module unload or afs_init() failure
375 * - the timeout daemon is stopped before calling this
376 */
377void afs_cell_purge(void)
378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct afs_cell *cell;
380
381 _enter("");
382
383 afs_put_cell(afs_cell_root);
384
David Howells08e0e7c2007-04-26 15:55:03 -0700385 down_write(&afs_cells_sem);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 while (!list_empty(&afs_cells)) {
388 cell = NULL;
389
390 /* remove the next cell from the front of the list */
391 write_lock(&afs_cells_lock);
392
393 if (!list_empty(&afs_cells)) {
394 cell = list_entry(afs_cells.next,
395 struct afs_cell, link);
396 list_del_init(&cell->link);
397 }
398
399 write_unlock(&afs_cells_lock);
400
401 if (cell) {
402 _debug("PURGING CELL %s (%d)",
403 cell->name, atomic_read(&cell->usage));
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* now the cell should be left with no references */
406 afs_cell_destroy(cell);
407 }
408 }
409
David Howells08e0e7c2007-04-26 15:55:03 -0700410 up_write(&afs_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700412}