blob: d0765883430e677693abbee2378fff8a0ea74bd6 [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>
Wang Lei07567a52010-08-04 15:16:38 +010016#include <linux/dns_resolver.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040017#include <linux/sched.h>
David Howells00d3b7a2007-04-26 15:57:07 -070018#include <keys/rxrpc-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "internal.h"
20
21DECLARE_RWSEM(afs_proc_cells_sem);
22LIST_HEAD(afs_proc_cells);
23
Robert P. J. Day0ae52d62008-04-29 01:03:20 -070024static LIST_HEAD(afs_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static DEFINE_RWLOCK(afs_cells_lock);
26static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
David Howells08e0e7c2007-04-26 15:55:03 -070027static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static struct afs_cell *afs_cell_root;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
David Howells00d3b7a2007-04-26 15:57:07 -070031 * allocate a cell record and fill in its name, VL server address list and
32 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
David Howells00d3b7a2007-04-26 15:57:07 -070034static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 struct afs_cell *cell;
David Howells76181c12007-10-16 23:29:46 -070037 struct key *key;
David Howells00d3b7a2007-04-26 15:57:07 -070038 size_t namelen;
39 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
Wang Lei07567a52010-08-04 15:16:38 +010040 char *dvllist = NULL, *_vllist = NULL;
41 char delimiter = ':';
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 int ret;
43
David Howells08e0e7c2007-04-26 15:55:03 -070044 _enter("%s,%s", name, vllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
47
David Howells00d3b7a2007-04-26 15:57:07 -070048 namelen = strlen(name);
Wang Lei07567a52010-08-04 15:16:38 +010049 if (namelen > AFS_MAXCELLNAME) {
50 _leave(" = -ENAMETOOLONG");
David Howells00d3b7a2007-04-26 15:57:07 -070051 return ERR_PTR(-ENAMETOOLONG);
Wang Lei07567a52010-08-04 15:16:38 +010052 }
David Howells00d3b7a2007-04-26 15:57:07 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /* allocate and initialise a cell record */
David Howells00d3b7a2007-04-26 15:57:07 -070055 cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (!cell) {
57 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -070058 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60
David Howells00d3b7a2007-04-26 15:57:07 -070061 memcpy(cell->name, name, namelen);
62 cell->name[namelen] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
David Howells08e0e7c2007-04-26 15:55:03 -070064 atomic_set(&cell->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 INIT_LIST_HEAD(&cell->link);
David Howells08e0e7c2007-04-26 15:55:03 -070066 rwlock_init(&cell->servers_lock);
67 INIT_LIST_HEAD(&cell->servers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 init_rwsem(&cell->vl_sem);
69 INIT_LIST_HEAD(&cell->vl_list);
David Howells08e0e7c2007-04-26 15:55:03 -070070 spin_lock_init(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Wang Lei07567a52010-08-04 15:16:38 +010072 /* 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) {
Wang Lei4a2d7892010-08-11 09:37:58 +010076 if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY)
77 /* translate these errors into something
78 * userspace might understand */
79 ret = -EDESTADDRREQ;
Wang Lei07567a52010-08-04 15:16:38 +010080 _leave(" = %d", ret);
81 return ERR_PTR(ret);
82 }
83 _vllist = dvllist;
84
85 /* change the delimiter for user-space reply */
86 delimiter = ',';
87
88 } else {
89 _vllist = vllist;
90 }
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* fill in the VL server list from the rest of the string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 do {
94 unsigned a, b, c, d;
95
Wang Lei07567a52010-08-04 15:16:38 +010096 next = strchr(_vllist, delimiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (next)
98 *next++ = 0;
99
Wang Lei07567a52010-08-04 15:16:38 +0100100 if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
David Howells00d3b7a2007-04-26 15:57:07 -0700101 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 if (a > 255 || b > 255 || c > 255 || d > 255)
David Howells00d3b7a2007-04-26 15:57:07 -0700104 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 cell->vl_addrs[cell->vl_naddrs++].s_addr =
107 htonl((a << 24) | (b << 16) | (c << 8) | d);
108
Wang Lei07567a52010-08-04 15:16:38 +0100109 } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
David Howells00d3b7a2007-04-26 15:57:07 -0700111 /* 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++);
David Howells00d3b7a2007-04-26 15:57:07 -0700118
David Howells76181c12007-10-16 23:29:46 -0700119 key = rxrpc_get_null_key(keyname);
120 if (IS_ERR(key)) {
121 _debug("no key");
122 ret = PTR_ERR(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700123 goto error;
124 }
David Howells76181c12007-10-16 23:29:46 -0700125 cell->anonymous_key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700126
127 _debug("anon key %p{%x}",
128 cell->anonymous_key, key_serial(cell->anonymous_key));
129
130 _leave(" = %p", cell);
131 return cell;
132
133bad_address:
134 printk(KERN_ERR "kAFS: bad VL server IP address\n");
135 ret = -EINVAL;
136error:
137 key_put(cell->anonymous_key);
Wang Lei07567a52010-08-04 15:16:38 +0100138 kfree(dvllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700139 kfree(cell);
140 _leave(" = %d", ret);
141 return ERR_PTR(ret);
142}
143
144/*
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
148 */
149struct afs_cell *afs_cell_create(const char *name, char *vllist)
150{
151 struct afs_cell *cell;
152 int ret;
153
154 _enter("%s,%s", name, vllist);
155
Sven Schnelle5214b722008-03-28 14:15:55 -0700156 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;
161 }
162 read_unlock(&afs_cells_lock);
163
David Howells00d3b7a2007-04-26 15:57:07 -0700164 cell = afs_cell_alloc(name, vllist);
165 if (IS_ERR(cell)) {
166 _leave(" = %ld", PTR_ERR(cell));
Sven Schnellea5f37c32008-04-02 13:17:18 +0100167 up_write(&afs_cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700168 return cell;
169 }
170
David Howells08e0e7c2007-04-26 15:55:03 -0700171 /* add a proc directory for this cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ret = afs_proc_cell_setup(cell);
173 if (ret < 0)
174 goto error;
175
David Howells9b3f26c2009-04-03 16:42:41 +0100176#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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#endif
182
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);
187
188 down_write(&afs_proc_cells_sem);
189 list_add_tail(&cell->proc_link, &afs_proc_cells);
190 up_write(&afs_proc_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 up_write(&afs_cells_sem);
192
David Howells08e0e7c2007-04-26 15:55:03 -0700193 _leave(" = %p", cell);
194 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
David Howellsec268152007-04-26 15:49:28 -0700196error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 up_write(&afs_cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700198 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 kfree(cell);
200 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700201 return ERR_PTR(ret);
Sven Schnelle5214b722008-03-28 14:15:55 -0700202
203duplicate_name:
204 read_unlock(&afs_cells_lock);
205 up_write(&afs_cells_sem);
206 return ERR_PTR(-EEXIST);
David Howellsec268152007-04-26 15:49:28 -0700207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
David Howells08e0e7c2007-04-26 15:55:03 -0700210 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
214int afs_cell_init(char *rootcell)
215{
216 struct afs_cell *old_root, *new_root;
217 char *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 _enter("");
220
221 if (!rootcell) {
222 /* module is loaded with no parameters, or built statically.
223 * - in the future we might initialize cell DB here.
224 */
David Howells08e0e7c2007-04-26 15:55:03 -0700225 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
227 }
228
229 cp = strchr(rootcell, ':');
Wang Lei07567a52010-08-04 15:16:38 +0100230 if (!cp)
231 _debug("kAFS: no VL server IP addresses specified");
232 else
233 *cp++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /* allocate a cell record for the root cell */
David Howells08e0e7c2007-04-26 15:55:03 -0700236 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
David Howells08e0e7c2007-04-26 15:55:03 -0700242 /* install the new cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 write_lock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700244 old_root = afs_cell_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 afs_cell_root = new_root;
246 write_unlock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700247 afs_put_cell(old_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
David Howells08e0e7c2007-04-26 15:55:03 -0700249 _leave(" = 0");
250 return 0;
David Howellsec268152007-04-26 15:49:28 -0700251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/*
254 * lookup a cell record
255 */
David Howells08e0e7c2007-04-26 15:55:03 -0700256struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
261
David Howells08e0e7c2007-04-26 15:55:03 -0700262 down_read(&afs_cells_sem);
263 read_lock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (name) {
266 /* if the cell was named, look for it in the cell record list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 list_for_each_entry(cell, &afs_cells, link) {
268 if (strncmp(cell->name, name, namesz) == 0) {
269 afs_get_cell(cell);
270 goto found;
271 }
272 }
David Howells08e0e7c2007-04-26 15:55:03 -0700273 cell = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 found:
David Howells08e0e7c2007-04-26 15:55:03 -0700275 ;
David Howellsec268152007-04-26 15:49:28 -0700276 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 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.
284 */
David Howells08e0e7c2007-04-26 15:55:03 -0700285 cell = ERR_PTR(-EDESTADDRREQ);
David Howellsec268152007-04-26 15:49:28 -0700286 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
David Howells08e0e7c2007-04-26 15:55:03 -0700292 read_unlock(&afs_cells_lock);
293 up_read(&afs_cells_sem);
294 _leave(" = %p", cell);
295 return cell;
David Howellsec268152007-04-26 15:49:28 -0700296}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Adrian Bunkc1206a22007-10-16 23:26:41 -0700298#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * try and get a cell record
301 */
David Howells08e0e7c2007-04-26 15:55:03 -0700302struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 write_lock(&afs_cells_lock);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (cell && !list_empty(&cell->link))
307 afs_get_cell(cell);
308 else
309 cell = NULL;
310
311 write_unlock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return cell;
David Howellsec268152007-04-26 15:49:28 -0700313}
Adrian Bunkc1206a22007-10-16 23:26:41 -0700314#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
317 * destroy a cell record
318 */
319void afs_put_cell(struct afs_cell *cell)
320{
321 if (!cell)
322 return;
323
324 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
325
David Howells08e0e7c2007-04-26 15:55:03 -0700326 ASSERTCMP(atomic_read(&cell->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* to prevent a race, the decrement and the dequeue must be effectively
329 * atomic */
330 write_lock(&afs_cells_lock);
331
332 if (likely(!atomic_dec_and_test(&cell->usage))) {
333 write_unlock(&afs_cells_lock);
334 _leave("");
335 return;
336 }
337
David Howells08e0e7c2007-04-26 15:55:03 -0700338 ASSERT(list_empty(&cell->servers));
339 ASSERT(list_empty(&cell->vl_list));
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 write_unlock(&afs_cells_lock);
342
David Howells08e0e7c2007-04-26 15:55:03 -0700343 wake_up(&afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 _leave(" [unused]");
David Howellsec268152007-04-26 15:49:28 -0700346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/*
349 * destroy a cell record
David Howells08e0e7c2007-04-26 15:55:03 -0700350 * - must be called with the afs_cells_sem write-locked
351 * - cell->link should have been broken by the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
353static void afs_cell_destroy(struct afs_cell *cell)
354{
355 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
356
David Howells08e0e7c2007-04-26 15:55:03 -0700357 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
358 ASSERT(list_empty(&cell->link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Howells08e0e7c2007-04-26 15:55:03 -0700360 /* wait for everyone to stop using the cell */
361 if (atomic_read(&cell->usage) > 0) {
362 DECLARE_WAITQUEUE(myself, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
David Howells08e0e7c2007-04-26 15:55:03 -0700364 _debug("wait for cell %s", cell->name);
365 set_current_state(TASK_UNINTERRUPTIBLE);
366 add_wait_queue(&afs_cells_freeable_wq, &myself);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
David Howells08e0e7c2007-04-26 15:55:03 -0700368 while (atomic_read(&cell->usage) > 0) {
369 schedule();
370 set_current_state(TASK_UNINTERRUPTIBLE);
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David Howells08e0e7c2007-04-26 15:55:03 -0700373 remove_wait_queue(&afs_cells_freeable_wq, &myself);
374 set_current_state(TASK_RUNNING);
375 }
376
377 _debug("cell dead");
378 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
379 ASSERT(list_empty(&cell->servers));
380 ASSERT(list_empty(&cell->vl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 afs_proc_cell_remove(cell);
383
384 down_write(&afs_proc_cells_sem);
385 list_del_init(&cell->proc_link);
386 up_write(&afs_proc_cells_sem);
387
David Howells9b3f26c2009-04-03 16:42:41 +0100388#ifdef CONFIG_AFS_FSCACHE
389 fscache_relinquish_cookie(cell->cache, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#endif
David Howells00d3b7a2007-04-26 15:57:07 -0700391 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 kfree(cell);
393
394 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700395}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * purge in-memory cell database on module unload or afs_init() failure
399 * - the timeout daemon is stopped before calling this
400 */
401void afs_cell_purge(void)
402{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct afs_cell *cell;
404
405 _enter("");
406
407 afs_put_cell(afs_cell_root);
408
David Howells08e0e7c2007-04-26 15:55:03 -0700409 down_write(&afs_cells_sem);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 while (!list_empty(&afs_cells)) {
412 cell = NULL;
413
414 /* remove the next cell from the front of the list */
415 write_lock(&afs_cells_lock);
416
417 if (!list_empty(&afs_cells)) {
418 cell = list_entry(afs_cells.next,
419 struct afs_cell, link);
420 list_del_init(&cell->link);
421 }
422
423 write_unlock(&afs_cells_lock);
424
425 if (cell) {
426 _debug("PURGING CELL %s (%d)",
427 cell->name, atomic_read(&cell->usage));
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* now the cell should be left with no references */
430 afs_cell_destroy(cell);
431 }
432 }
433
David Howells08e0e7c2007-04-26 15:55:03 -0700434 up_write(&afs_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700436}