]> Git Repo - linux.git/blame - fs/afs/cell.c
afs: Update the cache index structure
[linux.git] / fs / afs / cell.c
CommitLineData
ec26815a 1/* AFS cell and server record management
1da177e4
LT
2 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells ([email protected])
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>
1da177e4 13#include <linux/slab.h>
00d3b7a4
DH
14#include <linux/key.h>
15#include <linux/ctype.h>
07567a55 16#include <linux/dns_resolver.h>
e8edc6e0 17#include <linux/sched.h>
00d3b7a4 18#include <keys/rxrpc-type.h>
1da177e4
LT
19#include "internal.h"
20
1da177e4 21/*
00d3b7a4
DH
22 * allocate a cell record and fill in its name, VL server address list and
23 * allocate an anonymous key
1da177e4 24 */
f044c884
DH
25static struct afs_cell *afs_cell_alloc(struct afs_net *net,
26 const char *name, unsigned namelen,
bec5eb61 27 char *vllist)
1da177e4
LT
28{
29 struct afs_cell *cell;
76181c13 30 struct key *key;
00d3b7a4 31 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
07567a55
WL
32 char *dvllist = NULL, *_vllist = NULL;
33 char delimiter = ':';
1da177e4
LT
34 int ret;
35
bec5eb61 36 _enter("%*.*s,%s", namelen, namelen, name ?: "", vllist);
1da177e4
LT
37
38 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
39
07567a55
WL
40 if (namelen > AFS_MAXCELLNAME) {
41 _leave(" = -ENAMETOOLONG");
00d3b7a4 42 return ERR_PTR(-ENAMETOOLONG);
07567a55 43 }
00d3b7a4 44
1da177e4 45 /* allocate and initialise a cell record */
00d3b7a4 46 cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
1da177e4
LT
47 if (!cell) {
48 _leave(" = -ENOMEM");
08e0e7c8 49 return ERR_PTR(-ENOMEM);
1da177e4
LT
50 }
51
00d3b7a4
DH
52 memcpy(cell->name, name, namelen);
53 cell->name[namelen] = 0;
1da177e4 54
08e0e7c8 55 atomic_set(&cell->usage, 1);
1da177e4 56 INIT_LIST_HEAD(&cell->link);
f044c884 57 cell->net = net;
08e0e7c8
DH
58 rwlock_init(&cell->servers_lock);
59 INIT_LIST_HEAD(&cell->servers);
1da177e4
LT
60 init_rwsem(&cell->vl_sem);
61 INIT_LIST_HEAD(&cell->vl_list);
08e0e7c8 62 spin_lock_init(&cell->vl_lock);
1da177e4 63
07567a55
WL
64 /* if the ip address is invalid, try dns query */
65 if (!vllist || strlen(vllist) < 7) {
66 ret = dns_query("afsdb", name, namelen, "ipv4", &dvllist, NULL);
67 if (ret < 0) {
4a2d7892
WL
68 if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY)
69 /* translate these errors into something
70 * userspace might understand */
71 ret = -EDESTADDRREQ;
07567a55
WL
72 _leave(" = %d", ret);
73 return ERR_PTR(ret);
74 }
75 _vllist = dvllist;
76
77 /* change the delimiter for user-space reply */
78 delimiter = ',';
79
80 } else {
81 _vllist = vllist;
82 }
83
1da177e4 84 /* fill in the VL server list from the rest of the string */
1da177e4
LT
85 do {
86 unsigned a, b, c, d;
87
07567a55 88 next = strchr(_vllist, delimiter);
1da177e4
LT
89 if (next)
90 *next++ = 0;
91
07567a55 92 if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
00d3b7a4 93 goto bad_address;
1da177e4
LT
94
95 if (a > 255 || b > 255 || c > 255 || d > 255)
00d3b7a4 96 goto bad_address;
1da177e4
LT
97
98 cell->vl_addrs[cell->vl_naddrs++].s_addr =
99 htonl((a << 24) | (b << 16) | (c << 8) | d);
100
07567a55 101 } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
00d3b7a4
DH
102
103 /* create a key to represent an anonymous user */
104 memcpy(keyname, "afs@", 4);
105 dp = keyname + 4;
106 cp = cell->name;
107 do {
108 *dp++ = toupper(*cp);
109 } while (*cp++);
00d3b7a4 110
76181c13
DH
111 key = rxrpc_get_null_key(keyname);
112 if (IS_ERR(key)) {
113 _debug("no key");
114 ret = PTR_ERR(key);
00d3b7a4
DH
115 goto error;
116 }
76181c13 117 cell->anonymous_key = key;
00d3b7a4
DH
118
119 _debug("anon key %p{%x}",
120 cell->anonymous_key, key_serial(cell->anonymous_key));
121
122 _leave(" = %p", cell);
123 return cell;
124
125bad_address:
126 printk(KERN_ERR "kAFS: bad VL server IP address\n");
127 ret = -EINVAL;
128error:
129 key_put(cell->anonymous_key);
07567a55 130 kfree(dvllist);
00d3b7a4
DH
131 kfree(cell);
132 _leave(" = %d", ret);
133 return ERR_PTR(ret);
134}
1da177e4 135
00d3b7a4 136/*
bec5eb61 137 * afs_cell_crate() - create a cell record
f044c884 138 * @net: The network namespace
bec5eb61 139 * @name: is the name of the cell.
140 * @namsesz: is the strlen of the cell name.
141 * @vllist: is a colon separated list of IP addresses in "a.b.c.d" format.
142 * @retref: is T to return the cell reference when the cell exists.
00d3b7a4 143 */
f044c884
DH
144struct afs_cell *afs_cell_create(struct afs_net *net,
145 const char *name, unsigned namesz,
bec5eb61 146 char *vllist, bool retref)
00d3b7a4
DH
147{
148 struct afs_cell *cell;
149 int ret;
150
bec5eb61 151 _enter("%*.*s,%s", namesz, namesz, name ?: "", vllist);
00d3b7a4 152
f044c884
DH
153 down_write(&net->cells_sem);
154 read_lock(&net->cells_lock);
155 list_for_each_entry(cell, &net->cells, link) {
bec5eb61 156 if (strncasecmp(cell->name, name, namesz) == 0)
5214b729
SS
157 goto duplicate_name;
158 }
f044c884 159 read_unlock(&net->cells_lock);
5214b729 160
f044c884 161 cell = afs_cell_alloc(net, name, namesz, vllist);
00d3b7a4
DH
162 if (IS_ERR(cell)) {
163 _leave(" = %ld", PTR_ERR(cell));
f044c884 164 up_write(&net->cells_sem);
00d3b7a4
DH
165 return cell;
166 }
167
08e0e7c8 168 /* add a proc directory for this cell */
f044c884 169 ret = afs_proc_cell_setup(net, cell);
1da177e4
LT
170 if (ret < 0)
171 goto error;
172
9b3f26c9
DH
173#ifdef CONFIG_AFS_FSCACHE
174 /* put it up for caching (this never returns an error) */
175 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
176 &afs_cell_cache_index_def,
94d30ae9 177 cell, true);
1da177e4
LT
178#endif
179
180 /* add to the cell lists */
f044c884
DH
181 write_lock(&net->cells_lock);
182 list_add_tail(&cell->link, &net->cells);
183 write_unlock(&net->cells_lock);
1da177e4 184
f044c884
DH
185 down_write(&net->proc_cells_sem);
186 list_add_tail(&cell->proc_link, &net->proc_cells);
187 up_write(&net->proc_cells_sem);
188 up_write(&net->cells_sem);
1da177e4 189
08e0e7c8
DH
190 _leave(" = %p", cell);
191 return cell;
1da177e4 192
ec26815a 193error:
f044c884 194 up_write(&net->cells_sem);
00d3b7a4 195 key_put(cell->anonymous_key);
1da177e4
LT
196 kfree(cell);
197 _leave(" = %d", ret);
08e0e7c8 198 return ERR_PTR(ret);
5214b729
SS
199
200duplicate_name:
bec5eb61 201 if (retref && !IS_ERR(cell))
202 afs_get_cell(cell);
203
f044c884
DH
204 read_unlock(&net->cells_lock);
205 up_write(&net->cells_sem);
bec5eb61 206
207 if (retref) {
208 _leave(" = %p", cell);
209 return cell;
210 }
211
212 _leave(" = -EEXIST");
5214b729 213 return ERR_PTR(-EEXIST);
ec26815a 214}
1da177e4 215
1da177e4 216/*
08e0e7c8
DH
217 * set the root cell information
218 * - can be called with a module parameter string
219 * - can be called from a write to /proc/fs/afs/rootcell
1da177e4 220 */
f044c884 221int afs_cell_init(struct afs_net *net, char *rootcell)
1da177e4
LT
222{
223 struct afs_cell *old_root, *new_root;
224 char *cp;
1da177e4
LT
225
226 _enter("");
227
228 if (!rootcell) {
229 /* module is loaded with no parameters, or built statically.
230 * - in the future we might initialize cell DB here.
231 */
08e0e7c8 232 _leave(" = 0 [no root]");
1da177e4
LT
233 return 0;
234 }
235
236 cp = strchr(rootcell, ':');
07567a55
WL
237 if (!cp)
238 _debug("kAFS: no VL server IP addresses specified");
239 else
240 *cp++ = 0;
1da177e4
LT
241
242 /* allocate a cell record for the root cell */
f044c884 243 new_root = afs_cell_create(net, rootcell, strlen(rootcell), cp, false);
08e0e7c8
DH
244 if (IS_ERR(new_root)) {
245 _leave(" = %ld", PTR_ERR(new_root));
246 return PTR_ERR(new_root);
1da177e4
LT
247 }
248
08e0e7c8 249 /* install the new cell */
f044c884
DH
250 write_lock(&net->cells_lock);
251 old_root = net->ws_cell;
252 net->ws_cell = new_root;
253 write_unlock(&net->cells_lock);
9ed900b1 254 afs_put_cell(net, old_root);
1da177e4 255
08e0e7c8
DH
256 _leave(" = 0");
257 return 0;
ec26815a 258}
1da177e4 259
1da177e4
LT
260/*
261 * lookup a cell record
262 */
f044c884
DH
263struct afs_cell *afs_cell_lookup(struct afs_net *net,
264 const char *name, unsigned namesz,
bec5eb61 265 bool dns_cell)
1da177e4
LT
266{
267 struct afs_cell *cell;
1da177e4 268
bec5eb61 269 _enter("\"%*.*s\",", namesz, namesz, name ?: "");
1da177e4 270
f044c884
DH
271 down_read(&net->cells_sem);
272 read_lock(&net->cells_lock);
1da177e4
LT
273
274 if (name) {
275 /* if the cell was named, look for it in the cell record list */
f044c884 276 list_for_each_entry(cell, &net->cells, link) {
1da177e4
LT
277 if (strncmp(cell->name, name, namesz) == 0) {
278 afs_get_cell(cell);
279 goto found;
280 }
281 }
08e0e7c8 282 cell = ERR_PTR(-ENOENT);
bec5eb61 283 if (dns_cell)
284 goto create_cell;
1da177e4 285 found:
08e0e7c8 286 ;
ec26815a 287 } else {
f044c884 288 cell = net->ws_cell;
1da177e4
LT
289 if (!cell) {
290 /* this should not happen unless user tries to mount
291 * when root cell is not set. Return an impossibly
25985edc 292 * bizarre errno to alert the user. Things like
1da177e4
LT
293 * ENOENT might be "more appropriate" but they happen
294 * for other reasons.
295 */
08e0e7c8 296 cell = ERR_PTR(-EDESTADDRREQ);
ec26815a 297 } else {
1da177e4 298 afs_get_cell(cell);
1da177e4
LT
299 }
300
1da177e4
LT
301 }
302
f044c884
DH
303 read_unlock(&net->cells_lock);
304 up_read(&net->cells_sem);
08e0e7c8
DH
305 _leave(" = %p", cell);
306 return cell;
bec5eb61 307
308create_cell:
f044c884
DH
309 read_unlock(&net->cells_lock);
310 up_read(&net->cells_sem);
bec5eb61 311
f044c884 312 cell = afs_cell_create(net, name, namesz, NULL, true);
bec5eb61 313
314 _leave(" = %p", cell);
315 return cell;
ec26815a 316}
1da177e4 317
c1206a2c 318#if 0
1da177e4
LT
319/*
320 * try and get a cell record
321 */
08e0e7c8 322struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
1da177e4 323{
f044c884 324 write_lock(&net->cells_lock);
1da177e4 325
1da177e4
LT
326 if (cell && !list_empty(&cell->link))
327 afs_get_cell(cell);
328 else
329 cell = NULL;
330
f044c884 331 write_unlock(&net->cells_lock);
1da177e4 332 return cell;
ec26815a 333}
c1206a2c 334#endif /* 0 */
1da177e4 335
1da177e4
LT
336/*
337 * destroy a cell record
338 */
9ed900b1 339void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
1da177e4
LT
340{
341 if (!cell)
342 return;
343
344 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
345
08e0e7c8 346 ASSERTCMP(atomic_read(&cell->usage), >, 0);
1da177e4
LT
347
348 /* to prevent a race, the decrement and the dequeue must be effectively
349 * atomic */
9ed900b1 350 write_lock(&net->cells_lock);
1da177e4
LT
351
352 if (likely(!atomic_dec_and_test(&cell->usage))) {
9ed900b1 353 write_unlock(&net->cells_lock);
1da177e4
LT
354 _leave("");
355 return;
356 }
357
08e0e7c8
DH
358 ASSERT(list_empty(&cell->servers));
359 ASSERT(list_empty(&cell->vl_list));
360
9ed900b1 361 wake_up(&net->cells_freeable_wq);
1da177e4 362
9ed900b1 363 write_unlock(&net->cells_lock);
1da177e4
LT
364
365 _leave(" [unused]");
ec26815a 366}
1da177e4 367
1da177e4
LT
368/*
369 * destroy a cell record
f044c884 370 * - must be called with the net->cells_sem write-locked
08e0e7c8 371 * - cell->link should have been broken by the caller
1da177e4 372 */
f044c884 373static void afs_cell_destroy(struct afs_net *net, struct afs_cell *cell)
1da177e4
LT
374{
375 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
376
08e0e7c8
DH
377 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
378 ASSERT(list_empty(&cell->link));
1da177e4 379
08e0e7c8
DH
380 /* wait for everyone to stop using the cell */
381 if (atomic_read(&cell->usage) > 0) {
382 DECLARE_WAITQUEUE(myself, current);
1da177e4 383
08e0e7c8
DH
384 _debug("wait for cell %s", cell->name);
385 set_current_state(TASK_UNINTERRUPTIBLE);
f044c884 386 add_wait_queue(&net->cells_freeable_wq, &myself);
1da177e4 387
08e0e7c8
DH
388 while (atomic_read(&cell->usage) > 0) {
389 schedule();
390 set_current_state(TASK_UNINTERRUPTIBLE);
391 }
1da177e4 392
f044c884 393 remove_wait_queue(&net->cells_freeable_wq, &myself);
08e0e7c8
DH
394 set_current_state(TASK_RUNNING);
395 }
396
397 _debug("cell dead");
398 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
399 ASSERT(list_empty(&cell->servers));
400 ASSERT(list_empty(&cell->vl_list));
1da177e4 401
f044c884 402 afs_proc_cell_remove(net, cell);
1da177e4 403
f044c884 404 down_write(&net->proc_cells_sem);
1da177e4 405 list_del_init(&cell->proc_link);
f044c884 406 up_write(&net->proc_cells_sem);
1da177e4 407
9b3f26c9
DH
408#ifdef CONFIG_AFS_FSCACHE
409 fscache_relinquish_cookie(cell->cache, 0);
1da177e4 410#endif
00d3b7a4 411 key_put(cell->anonymous_key);
1da177e4
LT
412 kfree(cell);
413
414 _leave(" [destroyed]");
ec26815a 415}
1da177e4 416
1da177e4
LT
417/*
418 * purge in-memory cell database on module unload or afs_init() failure
419 * - the timeout daemon is stopped before calling this
420 */
f044c884 421void afs_cell_purge(struct afs_net *net)
1da177e4 422{
1da177e4
LT
423 struct afs_cell *cell;
424
425 _enter("");
426
9ed900b1 427 afs_put_cell(net, net->ws_cell);
1da177e4 428
f044c884 429 down_write(&net->cells_sem);
08e0e7c8 430
f044c884 431 while (!list_empty(&net->cells)) {
1da177e4
LT
432 cell = NULL;
433
434 /* remove the next cell from the front of the list */
f044c884 435 write_lock(&net->cells_lock);
1da177e4 436
f044c884
DH
437 if (!list_empty(&net->cells)) {
438 cell = list_entry(net->cells.next,
1da177e4
LT
439 struct afs_cell, link);
440 list_del_init(&cell->link);
441 }
442
f044c884 443 write_unlock(&net->cells_lock);
1da177e4
LT
444
445 if (cell) {
446 _debug("PURGING CELL %s (%d)",
447 cell->name, atomic_read(&cell->usage));
448
1da177e4 449 /* now the cell should be left with no references */
f044c884 450 afs_cell_destroy(net, cell);
1da177e4
LT
451 }
452 }
453
f044c884 454 up_write(&net->cells_sem);
1da177e4 455 _leave("");
ec26815a 456}
This page took 0.857115 seconds and 4 git commands to generate.