cell.c 10.3 KB
Newer Older
D
David Howells 已提交
1
/* AFS cell and server record management
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13
 *
 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/slab.h>
D
David Howells 已提交
14 15
#include <linux/key.h>
#include <linux/ctype.h>
16
#include <linux/dns_resolver.h>
A
Alexey Dobriyan 已提交
17
#include <linux/sched.h>
D
David Howells 已提交
18
#include <keys/rxrpc-type.h>
L
Linus Torvalds 已提交
19 20 21
#include "internal.h"

/*
D
David Howells 已提交
22 23
 * allocate a cell record and fill in its name, VL server address list and
 * allocate an anonymous key
L
Linus Torvalds 已提交
24
 */
25 26
static struct afs_cell *afs_cell_alloc(struct afs_net *net,
				       const char *name, unsigned namelen,
27
				       char *vllist)
L
Linus Torvalds 已提交
28 29
{
	struct afs_cell *cell;
30
	struct key *key;
D
David Howells 已提交
31
	char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
32 33
	char  *dvllist = NULL, *_vllist = NULL;
	char  delimiter = ':';
L
Linus Torvalds 已提交
34 35
	int ret;

36
	_enter("%*.*s,%s", namelen, namelen, name ?: "", vllist);
L
Linus Torvalds 已提交
37 38 39

	BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */

40 41
	if (namelen > AFS_MAXCELLNAME) {
		_leave(" = -ENAMETOOLONG");
D
David Howells 已提交
42
		return ERR_PTR(-ENAMETOOLONG);
43
	}
D
David Howells 已提交
44

L
Linus Torvalds 已提交
45
	/* allocate and initialise a cell record */
D
David Howells 已提交
46
	cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
47 48
	if (!cell) {
		_leave(" = -ENOMEM");
49
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
50 51
	}

D
David Howells 已提交
52 53
	memcpy(cell->name, name, namelen);
	cell->name[namelen] = 0;
L
Linus Torvalds 已提交
54

55
	atomic_set(&cell->usage, 1);
L
Linus Torvalds 已提交
56
	INIT_LIST_HEAD(&cell->link);
57
	cell->net = net;
58 59
	rwlock_init(&cell->servers_lock);
	INIT_LIST_HEAD(&cell->servers);
L
Linus Torvalds 已提交
60 61
	init_rwsem(&cell->vl_sem);
	INIT_LIST_HEAD(&cell->vl_list);
62
	spin_lock_init(&cell->vl_lock);
L
Linus Torvalds 已提交
63

64 65 66 67
	/* if the ip address is invalid, try dns query */
	if (!vllist || strlen(vllist) < 7) {
		ret = dns_query("afsdb", name, namelen, "ipv4", &dvllist, NULL);
		if (ret < 0) {
68 69 70 71
			if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY)
				/* translate these errors into something
				 * userspace might understand */
				ret = -EDESTADDRREQ;
72 73 74 75 76 77 78 79 80 81 82 83
			_leave(" = %d", ret);
			return ERR_PTR(ret);
		}
		_vllist = dvllist;

		/* change the delimiter for user-space reply */
		delimiter = ',';

	} else {
		_vllist = vllist;
	}

L
Linus Torvalds 已提交
84 85 86 87
	/* fill in the VL server list from the rest of the string */
	do {
		unsigned a, b, c, d;

88
		next = strchr(_vllist, delimiter);
L
Linus Torvalds 已提交
89 90 91
		if (next)
			*next++ = 0;

92
		if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
D
David Howells 已提交
93
			goto bad_address;
L
Linus Torvalds 已提交
94 95

		if (a > 255 || b > 255 || c > 255 || d > 255)
D
David Howells 已提交
96
			goto bad_address;
L
Linus Torvalds 已提交
97 98 99 100

		cell->vl_addrs[cell->vl_naddrs++].s_addr =
			htonl((a << 24) | (b << 16) | (c << 8) | d);

101
	} while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
D
David Howells 已提交
102 103 104 105 106 107 108 109 110

	/* create a key to represent an anonymous user */
	memcpy(keyname, "afs@", 4);
	dp = keyname + 4;
	cp = cell->name;
	do {
		*dp++ = toupper(*cp);
	} while (*cp++);

111 112 113 114
	key = rxrpc_get_null_key(keyname);
	if (IS_ERR(key)) {
		_debug("no key");
		ret = PTR_ERR(key);
D
David Howells 已提交
115 116
		goto error;
	}
117
	cell->anonymous_key = key;
D
David Howells 已提交
118 119 120 121 122 123 124 125 126 127 128 129

	_debug("anon key %p{%x}",
	       cell->anonymous_key, key_serial(cell->anonymous_key));

	_leave(" = %p", cell);
	return cell;

bad_address:
	printk(KERN_ERR "kAFS: bad VL server IP address\n");
	ret = -EINVAL;
error:
	key_put(cell->anonymous_key);
130
	kfree(dvllist);
D
David Howells 已提交
131 132 133 134
	kfree(cell);
	_leave(" = %d", ret);
	return ERR_PTR(ret);
}
L
Linus Torvalds 已提交
135

D
David Howells 已提交
136
/*
137
 * afs_cell_crate() - create a cell record
138
 * @net:	The network namespace
139 140 141 142
 * @name:	is the name of the cell.
 * @namsesz:	is the strlen of the cell name.
 * @vllist:	is a colon separated list of IP addresses in "a.b.c.d" format.
 * @retref:	is T to return the cell reference when the cell exists.
D
David Howells 已提交
143
 */
144 145
struct afs_cell *afs_cell_create(struct afs_net *net,
				 const char *name, unsigned namesz,
146
				 char *vllist, bool retref)
D
David Howells 已提交
147 148 149 150
{
	struct afs_cell *cell;
	int ret;

151
	_enter("%*.*s,%s", namesz, namesz, name ?: "", vllist);
D
David Howells 已提交
152

153 154 155
	down_write(&net->cells_sem);
	read_lock(&net->cells_lock);
	list_for_each_entry(cell, &net->cells, link) {
156
		if (strncasecmp(cell->name, name, namesz) == 0)
157 158
			goto duplicate_name;
	}
159
	read_unlock(&net->cells_lock);
160

161
	cell = afs_cell_alloc(net, name, namesz, vllist);
D
David Howells 已提交
162 163
	if (IS_ERR(cell)) {
		_leave(" = %ld", PTR_ERR(cell));
164
		up_write(&net->cells_sem);
D
David Howells 已提交
165 166 167
		return cell;
	}

168
	/* add a proc directory for this cell */
169
	ret = afs_proc_cell_setup(net, cell);
L
Linus Torvalds 已提交
170 171 172
	if (ret < 0)
		goto error;

D
David Howells 已提交
173 174 175 176
#ifdef CONFIG_AFS_FSCACHE
	/* put it up for caching (this never returns an error) */
	cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
					     &afs_cell_cache_index_def,
177
					     cell, true);
L
Linus Torvalds 已提交
178 179 180
#endif

	/* add to the cell lists */
181 182 183
	write_lock(&net->cells_lock);
	list_add_tail(&cell->link, &net->cells);
	write_unlock(&net->cells_lock);
L
Linus Torvalds 已提交
184

185 186 187 188
	down_write(&net->proc_cells_sem);
	list_add_tail(&cell->proc_link, &net->proc_cells);
	up_write(&net->proc_cells_sem);
	up_write(&net->cells_sem);
L
Linus Torvalds 已提交
189

190 191
	_leave(" = %p", cell);
	return cell;
L
Linus Torvalds 已提交
192

D
David Howells 已提交
193
error:
194
	up_write(&net->cells_sem);
D
David Howells 已提交
195
	key_put(cell->anonymous_key);
L
Linus Torvalds 已提交
196 197
	kfree(cell);
	_leave(" = %d", ret);
198
	return ERR_PTR(ret);
199 200

duplicate_name:
201 202 203
	if (retref && !IS_ERR(cell))
		afs_get_cell(cell);

204 205
	read_unlock(&net->cells_lock);
	up_write(&net->cells_sem);
206 207 208 209 210 211 212

	if (retref) {
		_leave(" = %p", cell);
		return cell;
	}

	_leave(" = -EEXIST");
213
	return ERR_PTR(-EEXIST);
D
David Howells 已提交
214
}
L
Linus Torvalds 已提交
215 216

/*
217 218 219
 * set the root cell information
 * - can be called with a module parameter string
 * - can be called from a write to /proc/fs/afs/rootcell
L
Linus Torvalds 已提交
220
 */
221
int afs_cell_init(struct afs_net *net, char *rootcell)
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231
{
	struct afs_cell *old_root, *new_root;
	char *cp;

	_enter("");

	if (!rootcell) {
		/* module is loaded with no parameters, or built statically.
		 * - in the future we might initialize cell DB here.
		 */
232
		_leave(" = 0 [no root]");
L
Linus Torvalds 已提交
233 234 235 236
		return 0;
	}

	cp = strchr(rootcell, ':');
237 238 239 240
	if (!cp)
		_debug("kAFS: no VL server IP addresses specified");
	else
		*cp++ = 0;
L
Linus Torvalds 已提交
241 242

	/* allocate a cell record for the root cell */
243
	new_root = afs_cell_create(net, rootcell, strlen(rootcell), cp, false);
244 245 246
	if (IS_ERR(new_root)) {
		_leave(" = %ld", PTR_ERR(new_root));
		return PTR_ERR(new_root);
L
Linus Torvalds 已提交
247 248
	}

249
	/* install the new cell */
250 251 252 253
	write_lock(&net->cells_lock);
	old_root = net->ws_cell;
	net->ws_cell = new_root;
	write_unlock(&net->cells_lock);
254
	afs_put_cell(net, old_root);
L
Linus Torvalds 已提交
255

256 257
	_leave(" = 0");
	return 0;
D
David Howells 已提交
258
}
L
Linus Torvalds 已提交
259 260 261 262

/*
 * lookup a cell record
 */
263 264
struct afs_cell *afs_cell_lookup(struct afs_net *net,
				 const char *name, unsigned namesz,
265
				 bool dns_cell)
L
Linus Torvalds 已提交
266 267 268
{
	struct afs_cell *cell;

269
	_enter("\"%*.*s\",", namesz, namesz, name ?: "");
L
Linus Torvalds 已提交
270

271 272
	down_read(&net->cells_sem);
	read_lock(&net->cells_lock);
L
Linus Torvalds 已提交
273 274 275

	if (name) {
		/* if the cell was named, look for it in the cell record list */
276
		list_for_each_entry(cell, &net->cells, link) {
L
Linus Torvalds 已提交
277 278 279 280 281
			if (strncmp(cell->name, name, namesz) == 0) {
				afs_get_cell(cell);
				goto found;
			}
		}
282
		cell = ERR_PTR(-ENOENT);
283 284
		if (dns_cell)
			goto create_cell;
L
Linus Torvalds 已提交
285
	found:
286
		;
D
David Howells 已提交
287
	} else {
288
		cell = net->ws_cell;
L
Linus Torvalds 已提交
289 290 291
		if (!cell) {
			/* this should not happen unless user tries to mount
			 * when root cell is not set. Return an impossibly
L
Lucas De Marchi 已提交
292
			 * bizarre errno to alert the user. Things like
L
Linus Torvalds 已提交
293 294 295
			 * ENOENT might be "more appropriate" but they happen
			 * for other reasons.
			 */
296
			cell = ERR_PTR(-EDESTADDRREQ);
D
David Howells 已提交
297
		} else {
L
Linus Torvalds 已提交
298 299 300 301 302
			afs_get_cell(cell);
		}

	}

303 304
	read_unlock(&net->cells_lock);
	up_read(&net->cells_sem);
305 306
	_leave(" = %p", cell);
	return cell;
307 308

create_cell:
309 310
	read_unlock(&net->cells_lock);
	up_read(&net->cells_sem);
311

312
	cell = afs_cell_create(net, name, namesz, NULL, true);
313 314 315

	_leave(" = %p", cell);
	return cell;
D
David Howells 已提交
316
}
L
Linus Torvalds 已提交
317

A
Adrian Bunk 已提交
318
#if 0
L
Linus Torvalds 已提交
319 320 321
/*
 * try and get a cell record
 */
322
struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
L
Linus Torvalds 已提交
323
{
324
	write_lock(&net->cells_lock);
L
Linus Torvalds 已提交
325 326 327 328 329 330

	if (cell && !list_empty(&cell->link))
		afs_get_cell(cell);
	else
		cell = NULL;

331
	write_unlock(&net->cells_lock);
L
Linus Torvalds 已提交
332
	return cell;
D
David Howells 已提交
333
}
A
Adrian Bunk 已提交
334
#endif  /*  0  */
L
Linus Torvalds 已提交
335 336 337 338

/*
 * destroy a cell record
 */
339
void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
L
Linus Torvalds 已提交
340 341 342 343 344 345
{
	if (!cell)
		return;

	_enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);

346
	ASSERTCMP(atomic_read(&cell->usage), >, 0);
L
Linus Torvalds 已提交
347 348 349

	/* to prevent a race, the decrement and the dequeue must be effectively
	 * atomic */
350
	write_lock(&net->cells_lock);
L
Linus Torvalds 已提交
351 352

	if (likely(!atomic_dec_and_test(&cell->usage))) {
353
		write_unlock(&net->cells_lock);
L
Linus Torvalds 已提交
354 355 356 357
		_leave("");
		return;
	}

358 359 360
	ASSERT(list_empty(&cell->servers));
	ASSERT(list_empty(&cell->vl_list));

361
	wake_up(&net->cells_freeable_wq);
L
Linus Torvalds 已提交
362

363
	write_unlock(&net->cells_lock);
L
Linus Torvalds 已提交
364 365

	_leave(" [unused]");
D
David Howells 已提交
366
}
L
Linus Torvalds 已提交
367 368 369

/*
 * destroy a cell record
370
 * - must be called with the net->cells_sem write-locked
371
 * - cell->link should have been broken by the caller
L
Linus Torvalds 已提交
372
 */
373
static void afs_cell_destroy(struct afs_net *net, struct afs_cell *cell)
L
Linus Torvalds 已提交
374 375 376
{
	_enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);

377 378
	ASSERTCMP(atomic_read(&cell->usage), >=, 0);
	ASSERT(list_empty(&cell->link));
L
Linus Torvalds 已提交
379

380 381 382
	/* wait for everyone to stop using the cell */
	if (atomic_read(&cell->usage) > 0) {
		DECLARE_WAITQUEUE(myself, current);
L
Linus Torvalds 已提交
383

384 385
		_debug("wait for cell %s", cell->name);
		set_current_state(TASK_UNINTERRUPTIBLE);
386
		add_wait_queue(&net->cells_freeable_wq, &myself);
L
Linus Torvalds 已提交
387

388 389 390 391
		while (atomic_read(&cell->usage) > 0) {
			schedule();
			set_current_state(TASK_UNINTERRUPTIBLE);
		}
L
Linus Torvalds 已提交
392

393
		remove_wait_queue(&net->cells_freeable_wq, &myself);
394 395 396 397 398 399 400
		set_current_state(TASK_RUNNING);
	}

	_debug("cell dead");
	ASSERTCMP(atomic_read(&cell->usage), ==, 0);
	ASSERT(list_empty(&cell->servers));
	ASSERT(list_empty(&cell->vl_list));
L
Linus Torvalds 已提交
401

402
	afs_proc_cell_remove(net, cell);
L
Linus Torvalds 已提交
403

404
	down_write(&net->proc_cells_sem);
L
Linus Torvalds 已提交
405
	list_del_init(&cell->proc_link);
406
	up_write(&net->proc_cells_sem);
L
Linus Torvalds 已提交
407

D
David Howells 已提交
408 409
#ifdef CONFIG_AFS_FSCACHE
	fscache_relinquish_cookie(cell->cache, 0);
L
Linus Torvalds 已提交
410
#endif
D
David Howells 已提交
411
	key_put(cell->anonymous_key);
L
Linus Torvalds 已提交
412 413 414
	kfree(cell);

	_leave(" [destroyed]");
D
David Howells 已提交
415
}
L
Linus Torvalds 已提交
416 417 418 419 420

/*
 * purge in-memory cell database on module unload or afs_init() failure
 * - the timeout daemon is stopped before calling this
 */
421
void afs_cell_purge(struct afs_net *net)
L
Linus Torvalds 已提交
422 423 424 425 426
{
	struct afs_cell *cell;

	_enter("");

427
	afs_put_cell(net, net->ws_cell);
L
Linus Torvalds 已提交
428

429
	down_write(&net->cells_sem);
430

431
	while (!list_empty(&net->cells)) {
L
Linus Torvalds 已提交
432 433 434
		cell = NULL;

		/* remove the next cell from the front of the list */
435
		write_lock(&net->cells_lock);
L
Linus Torvalds 已提交
436

437 438
		if (!list_empty(&net->cells)) {
			cell = list_entry(net->cells.next,
L
Linus Torvalds 已提交
439 440 441 442
					  struct afs_cell, link);
			list_del_init(&cell->link);
		}

443
		write_unlock(&net->cells_lock);
L
Linus Torvalds 已提交
444 445 446 447 448 449

		if (cell) {
			_debug("PURGING CELL %s (%d)",
			       cell->name, atomic_read(&cell->usage));

			/* now the cell should be left with no references */
450
			afs_cell_destroy(net, cell);
L
Linus Torvalds 已提交
451 452 453
		}
	}

454
	up_write(&net->cells_sem);
L
Linus Torvalds 已提交
455
	_leave("");
D
David Howells 已提交
456
}