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 22 23
#include "internal.h"

DECLARE_RWSEM(afs_proc_cells_sem);
LIST_HEAD(afs_proc_cells);

24
static LIST_HEAD(afs_cells);
L
Linus Torvalds 已提交
25 26
static DEFINE_RWLOCK(afs_cells_lock);
static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
27
static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
L
Linus Torvalds 已提交
28 29 30
static struct afs_cell *afs_cell_root;

/*
D
David Howells 已提交
31 32
 * allocate a cell record and fill in its name, VL server address list and
 * allocate an anonymous key
L
Linus Torvalds 已提交
33
 */
34 35
static struct afs_cell *afs_cell_alloc(const char *name, unsigned namelen,
				       char *vllist)
L
Linus Torvalds 已提交
36 37
{
	struct afs_cell *cell;
38
	struct key *key;
D
David Howells 已提交
39
	char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
40 41
	char  *dvllist = NULL, *_vllist = NULL;
	char  delimiter = ':';
L
Linus Torvalds 已提交
42 43
	int ret;

44
	_enter("%*.*s,%s", namelen, namelen, name ?: "", vllist);
L
Linus Torvalds 已提交
45 46 47

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

48 49
	if (namelen > AFS_MAXCELLNAME) {
		_leave(" = -ENAMETOOLONG");
D
David Howells 已提交
50
		return ERR_PTR(-ENAMETOOLONG);
51
	}
D
David Howells 已提交
52

L
Linus Torvalds 已提交
53
	/* allocate and initialise a cell record */
D
David Howells 已提交
54
	cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
55 56
	if (!cell) {
		_leave(" = -ENOMEM");
57
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
58 59
	}

D
David Howells 已提交
60 61
	memcpy(cell->name, name, namelen);
	cell->name[namelen] = 0;
L
Linus Torvalds 已提交
62

63
	atomic_set(&cell->usage, 1);
L
Linus Torvalds 已提交
64
	INIT_LIST_HEAD(&cell->link);
65 66
	rwlock_init(&cell->servers_lock);
	INIT_LIST_HEAD(&cell->servers);
L
Linus Torvalds 已提交
67 68
	init_rwsem(&cell->vl_sem);
	INIT_LIST_HEAD(&cell->vl_list);
69
	spin_lock_init(&cell->vl_lock);
L
Linus Torvalds 已提交
70

71 72 73 74
	/* 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) {
75 76 77 78
			if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY)
				/* translate these errors into something
				 * userspace might understand */
				ret = -EDESTADDRREQ;
79 80 81 82 83 84 85 86 87 88 89 90
			_leave(" = %d", ret);
			return ERR_PTR(ret);
		}
		_vllist = dvllist;

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

	} else {
		_vllist = vllist;
	}

L
Linus Torvalds 已提交
91 92 93 94
	/* fill in the VL server list from the rest of the string */
	do {
		unsigned a, b, c, d;

95
		next = strchr(_vllist, delimiter);
L
Linus Torvalds 已提交
96 97 98
		if (next)
			*next++ = 0;

99
		if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
D
David Howells 已提交
100
			goto bad_address;
L
Linus Torvalds 已提交
101 102

		if (a > 255 || b > 255 || c > 255 || d > 255)
D
David Howells 已提交
103
			goto bad_address;
L
Linus Torvalds 已提交
104 105 106 107

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

108
	} while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
D
David Howells 已提交
109 110 111 112 113 114 115 116 117

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

118 119 120 121
	key = rxrpc_get_null_key(keyname);
	if (IS_ERR(key)) {
		_debug("no key");
		ret = PTR_ERR(key);
D
David Howells 已提交
122 123
		goto error;
	}
124
	cell->anonymous_key = key;
D
David Howells 已提交
125 126 127 128 129 130 131 132 133 134 135 136

	_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);
137
	kfree(dvllist);
D
David Howells 已提交
138 139 140 141
	kfree(cell);
	_leave(" = %d", ret);
	return ERR_PTR(ret);
}
L
Linus Torvalds 已提交
142

D
David Howells 已提交
143
/*
144 145 146 147 148
 * afs_cell_crate() - create a cell record
 * @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 已提交
149
 */
150 151
struct afs_cell *afs_cell_create(const char *name, unsigned namesz,
				 char *vllist, bool retref)
D
David Howells 已提交
152 153 154 155
{
	struct afs_cell *cell;
	int ret;

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

158 159 160
	down_write(&afs_cells_sem);
	read_lock(&afs_cells_lock);
	list_for_each_entry(cell, &afs_cells, link) {
161
		if (strncasecmp(cell->name, name, namesz) == 0)
162 163 164 165
			goto duplicate_name;
	}
	read_unlock(&afs_cells_lock);

166
	cell = afs_cell_alloc(name, namesz, vllist);
D
David Howells 已提交
167 168
	if (IS_ERR(cell)) {
		_leave(" = %ld", PTR_ERR(cell));
169
		up_write(&afs_cells_sem);
D
David Howells 已提交
170 171 172
		return cell;
	}

173
	/* add a proc directory for this cell */
L
Linus Torvalds 已提交
174 175 176 177
	ret = afs_proc_cell_setup(cell);
	if (ret < 0)
		goto error;

D
David Howells 已提交
178 179 180 181
#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,
182
					     cell, true);
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190 191 192 193 194
#endif

	/* add to the cell lists */
	write_lock(&afs_cells_lock);
	list_add_tail(&cell->link, &afs_cells);
	write_unlock(&afs_cells_lock);

	down_write(&afs_proc_cells_sem);
	list_add_tail(&cell->proc_link, &afs_proc_cells);
	up_write(&afs_proc_cells_sem);
	up_write(&afs_cells_sem);

195 196
	_leave(" = %p", cell);
	return cell;
L
Linus Torvalds 已提交
197

D
David Howells 已提交
198
error:
L
Linus Torvalds 已提交
199
	up_write(&afs_cells_sem);
D
David Howells 已提交
200
	key_put(cell->anonymous_key);
L
Linus Torvalds 已提交
201 202
	kfree(cell);
	_leave(" = %d", ret);
203
	return ERR_PTR(ret);
204 205

duplicate_name:
206 207 208
	if (retref && !IS_ERR(cell))
		afs_get_cell(cell);

209 210
	read_unlock(&afs_cells_lock);
	up_write(&afs_cells_sem);
211 212 213 214 215 216 217

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

	_leave(" = -EEXIST");
218
	return ERR_PTR(-EEXIST);
D
David Howells 已提交
219
}
L
Linus Torvalds 已提交
220 221

/*
222 223 224
 * 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 已提交
225 226 227 228 229 230 231 232 233 234 235 236
 */
int afs_cell_init(char *rootcell)
{
	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.
		 */
237
		_leave(" = 0 [no root]");
L
Linus Torvalds 已提交
238 239 240 241
		return 0;
	}

	cp = strchr(rootcell, ':');
242 243 244 245
	if (!cp)
		_debug("kAFS: no VL server IP addresses specified");
	else
		*cp++ = 0;
L
Linus Torvalds 已提交
246 247

	/* allocate a cell record for the root cell */
248
	new_root = afs_cell_create(rootcell, strlen(rootcell), cp, false);
249 250 251
	if (IS_ERR(new_root)) {
		_leave(" = %ld", PTR_ERR(new_root));
		return PTR_ERR(new_root);
L
Linus Torvalds 已提交
252 253
	}

254
	/* install the new cell */
L
Linus Torvalds 已提交
255
	write_lock(&afs_cells_lock);
256
	old_root = afs_cell_root;
L
Linus Torvalds 已提交
257 258
	afs_cell_root = new_root;
	write_unlock(&afs_cells_lock);
259
	afs_put_cell(old_root);
L
Linus Torvalds 已提交
260

261 262
	_leave(" = 0");
	return 0;
D
David Howells 已提交
263
}
L
Linus Torvalds 已提交
264 265 266 267

/*
 * lookup a cell record
 */
268 269
struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz,
				 bool dns_cell)
L
Linus Torvalds 已提交
270 271 272
{
	struct afs_cell *cell;

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

275 276
	down_read(&afs_cells_sem);
	read_lock(&afs_cells_lock);
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285

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

	}

307 308 309 310
	read_unlock(&afs_cells_lock);
	up_read(&afs_cells_sem);
	_leave(" = %p", cell);
	return cell;
311 312 313 314 315 316 317 318 319

create_cell:
	read_unlock(&afs_cells_lock);
	up_read(&afs_cells_sem);

	cell = afs_cell_create(name, namesz, NULL, true);

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

A
Adrian Bunk 已提交
322
#if 0
L
Linus Torvalds 已提交
323 324 325
/*
 * try and get a cell record
 */
326
struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335 336
{
	write_lock(&afs_cells_lock);

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

	write_unlock(&afs_cells_lock);
	return cell;
D
David Howells 已提交
337
}
A
Adrian Bunk 已提交
338
#endif  /*  0  */
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348 349

/*
 * destroy a cell record
 */
void afs_put_cell(struct afs_cell *cell)
{
	if (!cell)
		return;

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

350
	ASSERTCMP(atomic_read(&cell->usage), >, 0);
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360 361

	/* to prevent a race, the decrement and the dequeue must be effectively
	 * atomic */
	write_lock(&afs_cells_lock);

	if (likely(!atomic_dec_and_test(&cell->usage))) {
		write_unlock(&afs_cells_lock);
		_leave("");
		return;
	}

362 363 364
	ASSERT(list_empty(&cell->servers));
	ASSERT(list_empty(&cell->vl_list));

L
Linus Torvalds 已提交
365 366
	write_unlock(&afs_cells_lock);

367
	wake_up(&afs_cells_freeable_wq);
L
Linus Torvalds 已提交
368 369

	_leave(" [unused]");
D
David Howells 已提交
370
}
L
Linus Torvalds 已提交
371 372 373

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

381 382
	ASSERTCMP(atomic_read(&cell->usage), >=, 0);
	ASSERT(list_empty(&cell->link));
L
Linus Torvalds 已提交
383

384 385 386
	/* wait for everyone to stop using the cell */
	if (atomic_read(&cell->usage) > 0) {
		DECLARE_WAITQUEUE(myself, current);
L
Linus Torvalds 已提交
387

388 389 390
		_debug("wait for cell %s", cell->name);
		set_current_state(TASK_UNINTERRUPTIBLE);
		add_wait_queue(&afs_cells_freeable_wq, &myself);
L
Linus Torvalds 已提交
391

392 393 394 395
		while (atomic_read(&cell->usage) > 0) {
			schedule();
			set_current_state(TASK_UNINTERRUPTIBLE);
		}
L
Linus Torvalds 已提交
396

397 398 399 400 401 402 403 404
		remove_wait_queue(&afs_cells_freeable_wq, &myself);
		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 已提交
405 406 407 408 409 410 411

	afs_proc_cell_remove(cell);

	down_write(&afs_proc_cells_sem);
	list_del_init(&cell->proc_link);
	up_write(&afs_proc_cells_sem);

D
David Howells 已提交
412 413
#ifdef CONFIG_AFS_FSCACHE
	fscache_relinquish_cookie(cell->cache, 0);
L
Linus Torvalds 已提交
414
#endif
D
David Howells 已提交
415
	key_put(cell->anonymous_key);
L
Linus Torvalds 已提交
416 417 418
	kfree(cell);

	_leave(" [destroyed]");
D
David Howells 已提交
419
}
L
Linus Torvalds 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432

/*
 * purge in-memory cell database on module unload or afs_init() failure
 * - the timeout daemon is stopped before calling this
 */
void afs_cell_purge(void)
{
	struct afs_cell *cell;

	_enter("");

	afs_put_cell(afs_cell_root);

433 434
	down_write(&afs_cells_sem);

L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	while (!list_empty(&afs_cells)) {
		cell = NULL;

		/* remove the next cell from the front of the list */
		write_lock(&afs_cells_lock);

		if (!list_empty(&afs_cells)) {
			cell = list_entry(afs_cells.next,
					  struct afs_cell, link);
			list_del_init(&cell->link);
		}

		write_unlock(&afs_cells_lock);

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

			/* now the cell should be left with no references */
			afs_cell_destroy(cell);
		}
	}

458
	up_write(&afs_cells_sem);
L
Linus Torvalds 已提交
459
	_leave("");
D
David Howells 已提交
460
}