cell.c 7.6 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 14 15 16 17 18 19 20 21
 *
 * 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>
#include "internal.h"

DECLARE_RWSEM(afs_proc_cells_sem);
LIST_HEAD(afs_proc_cells);

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

/*
 * create a cell record
 * - "name" is the name of the cell
 * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
 */
30
struct afs_cell *afs_cell_create(const char *name, char *vllist)
L
Linus Torvalds 已提交
31 32 33 34 35
{
	struct afs_cell *cell;
	char *next;
	int ret;

36
	_enter("%s,%s", name, vllist);
L
Linus Torvalds 已提交
37 38 39 40 41 42 43

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

	/* allocate and initialise a cell record */
	cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL);
	if (!cell) {
		_leave(" = -ENOMEM");
44
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
45 46 47 48 49
	}

	down_write(&afs_cells_sem);

	memset(cell, 0, sizeof(struct afs_cell));
50
	atomic_set(&cell->usage, 1);
L
Linus Torvalds 已提交
51 52 53

	INIT_LIST_HEAD(&cell->link);

54 55
	rwlock_init(&cell->servers_lock);
	INIT_LIST_HEAD(&cell->servers);
L
Linus Torvalds 已提交
56 57 58

	init_rwsem(&cell->vl_sem);
	INIT_LIST_HEAD(&cell->vl_list);
59
	spin_lock_init(&cell->vl_lock);
L
Linus Torvalds 已提交
60

61
	strcpy(cell->name, name);
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

	/* fill in the VL server list from the rest of the string */
	ret = -EINVAL;
	do {
		unsigned a, b, c, d;

		next = strchr(vllist, ':');
		if (next)
			*next++ = 0;

		if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
			goto badaddr;

		if (a > 255 || b > 255 || c > 255 || d > 255)
			goto badaddr;

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

		if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS)
			break;

84
	} while ((vllist = next));
L
Linus Torvalds 已提交
85

86
	/* add a proc directory for this cell */
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	ret = afs_proc_cell_setup(cell);
	if (ret < 0)
		goto error;

#ifdef AFS_CACHING_SUPPORT
	/* put it up for caching */
	cachefs_acquire_cookie(afs_cache_netfs.primary_index,
			       &afs_vlocation_cache_index_def,
			       cell,
			       &cell->cache);
#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);

109 110
	_leave(" = %p", cell);
	return cell;
L
Linus Torvalds 已提交
111

D
David Howells 已提交
112
badaddr:
113
	printk(KERN_ERR "kAFS: bad VL server IP address\n");
D
David Howells 已提交
114
error:
L
Linus Torvalds 已提交
115 116 117
	up_write(&afs_cells_sem);
	kfree(cell);
	_leave(" = %d", ret);
118
	return ERR_PTR(ret);
D
David Howells 已提交
119
}
L
Linus Torvalds 已提交
120 121

/*
122 123 124
 * 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 已提交
125 126 127 128 129 130 131 132 133 134 135 136
 */
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.
		 */
137
		_leave(" = 0 [no root]");
L
Linus Torvalds 已提交
138 139 140 141 142 143
		return 0;
	}

	cp = strchr(rootcell, ':');
	if (!cp) {
		printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
144
		_leave(" = -EINVAL");
L
Linus Torvalds 已提交
145 146 147 148 149
		return -EINVAL;
	}

	/* allocate a cell record for the root cell */
	*cp++ = 0;
150 151 152 153
	new_root = afs_cell_create(rootcell, cp);
	if (IS_ERR(new_root)) {
		_leave(" = %ld", PTR_ERR(new_root));
		return PTR_ERR(new_root);
L
Linus Torvalds 已提交
154 155
	}

156
	/* install the new cell */
L
Linus Torvalds 已提交
157
	write_lock(&afs_cells_lock);
158
	old_root = afs_cell_root;
L
Linus Torvalds 已提交
159 160
	afs_cell_root = new_root;
	write_unlock(&afs_cells_lock);
161
	afs_put_cell(old_root);
L
Linus Torvalds 已提交
162

163 164
	_leave(" = 0");
	return 0;
D
David Howells 已提交
165
}
L
Linus Torvalds 已提交
166 167 168 169

/*
 * lookup a cell record
 */
170
struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
L
Linus Torvalds 已提交
171 172 173 174 175
{
	struct afs_cell *cell;

	_enter("\"%*.*s\",", namesz, namesz, name ? name : "");

176 177
	down_read(&afs_cells_sem);
	read_lock(&afs_cells_lock);
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186

	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;
			}
		}
187
		cell = ERR_PTR(-ENOENT);
L
Linus Torvalds 已提交
188
	found:
189
		;
D
David Howells 已提交
190
	} else {
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198
		cell = afs_cell_root;
		if (!cell) {
			/* this should not happen unless user tries to mount
			 * when root cell is not set. Return an impossibly
			 * bizzare errno to alert the user. Things like
			 * ENOENT might be "more appropriate" but they happen
			 * for other reasons.
			 */
199
			cell = ERR_PTR(-EDESTADDRREQ);
D
David Howells 已提交
200
		} else {
L
Linus Torvalds 已提交
201 202 203 204 205
			afs_get_cell(cell);
		}

	}

206 207 208 209
	read_unlock(&afs_cells_lock);
	up_read(&afs_cells_sem);
	_leave(" = %p", cell);
	return cell;
D
David Howells 已提交
210
}
L
Linus Torvalds 已提交
211 212 213 214

/*
 * try and get a cell record
 */
215
struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225
{
	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 已提交
226
}
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235 236 237

/*
 * 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);

238
	ASSERTCMP(atomic_read(&cell->usage), >, 0);
L
Linus Torvalds 已提交
239 240 241 242 243 244 245 246 247 248 249

	/* 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;
	}

250 251 252
	ASSERT(list_empty(&cell->servers));
	ASSERT(list_empty(&cell->vl_list));

L
Linus Torvalds 已提交
253 254
	write_unlock(&afs_cells_lock);

255
	wake_up(&afs_cells_freeable_wq);
L
Linus Torvalds 已提交
256 257

	_leave(" [unused]");
D
David Howells 已提交
258
}
L
Linus Torvalds 已提交
259 260 261

/*
 * destroy a cell record
262 263
 * - must be called with the afs_cells_sem write-locked
 * - cell->link should have been broken by the caller
L
Linus Torvalds 已提交
264 265 266 267 268
 */
static void afs_cell_destroy(struct afs_cell *cell)
{
	_enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);

269 270
	ASSERTCMP(atomic_read(&cell->usage), >=, 0);
	ASSERT(list_empty(&cell->link));
L
Linus Torvalds 已提交
271

272 273 274
	/* wait for everyone to stop using the cell */
	if (atomic_read(&cell->usage) > 0) {
		DECLARE_WAITQUEUE(myself, current);
L
Linus Torvalds 已提交
275

276 277 278
		_debug("wait for cell %s", cell->name);
		set_current_state(TASK_UNINTERRUPTIBLE);
		add_wait_queue(&afs_cells_freeable_wq, &myself);
L
Linus Torvalds 已提交
279

280 281 282 283
		while (atomic_read(&cell->usage) > 0) {
			schedule();
			set_current_state(TASK_UNINTERRUPTIBLE);
		}
L
Linus Torvalds 已提交
284

285 286 287 288 289 290 291 292
		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 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306

	afs_proc_cell_remove(cell);

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

#ifdef AFS_CACHING_SUPPORT
	cachefs_relinquish_cookie(cell->cache, 0);
#endif

	kfree(cell);

	_leave(" [destroyed]");
D
David Howells 已提交
307
}
L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320

/*
 * 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);

321 322
	down_write(&afs_cells_sem);

L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	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);
		}
	}

346
	up_write(&afs_cells_sem);
L
Linus Torvalds 已提交
347
	_leave("");
D
David Howells 已提交
348
}