server.c 7.9 KB
Newer Older
D
David Howells 已提交
1
/* AFS server record management
L
Linus Torvalds 已提交
2
 *
3
 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15
 * 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/sched.h>
#include <linux/slab.h>
#include "internal.h"

A
Adrian Bunk 已提交
16
static unsigned afs_server_timeout = 10;	/* server timeout in seconds */
L
Linus Torvalds 已提交
17

18
static void afs_reap_server(struct work_struct *);
L
Linus Torvalds 已提交
19

20 21 22 23 24 25 26 27 28 29 30 31 32
/* tree of all the servers, indexed by IP address */
static struct rb_root afs_servers = RB_ROOT;
static DEFINE_RWLOCK(afs_servers_lock);

/* LRU list of all the servers not currently in use */
static LIST_HEAD(afs_server_graveyard);
static DEFINE_SPINLOCK(afs_server_graveyard_lock);
static DECLARE_DELAYED_WORK(afs_server_reaper, afs_reap_server);

/*
 * install a server record in the master tree
 */
static int afs_install_server(struct afs_server *server)
L
Linus Torvalds 已提交
33
{
34 35 36 37 38
	struct afs_server *xserver;
	struct rb_node **pp, *p;
	int ret;

	_enter("%p", server);
L
Linus Torvalds 已提交
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	write_lock(&afs_servers_lock);

	ret = -EEXIST;
	pp = &afs_servers.rb_node;
	p = NULL;
	while (*pp) {
		p = *pp;
		_debug("- consider %p", p);
		xserver = rb_entry(p, struct afs_server, master_rb);
		if (server->addr.s_addr < xserver->addr.s_addr)
			pp = &(*pp)->rb_left;
		else if (server->addr.s_addr > xserver->addr.s_addr)
			pp = &(*pp)->rb_right;
		else
			goto error;
	}
L
Linus Torvalds 已提交
56

57 58 59
	rb_link_node(&server->master_rb, p, pp);
	rb_insert_color(&server->master_rb, &afs_servers);
	ret = 0;
L
Linus Torvalds 已提交
60

61 62 63 64
error:
	write_unlock(&afs_servers_lock);
	return ret;
}
L
Linus Torvalds 已提交
65 66

/*
67
 * allocate a new server record
L
Linus Torvalds 已提交
68
 */
69 70
static struct afs_server *afs_alloc_server(struct afs_cell *cell,
					   const struct in_addr *addr)
L
Linus Torvalds 已提交
71
{
72
	struct afs_server *server;
L
Linus Torvalds 已提交
73

74
	_enter("");
L
Linus Torvalds 已提交
75

76
	server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	if (server) {
		atomic_set(&server->usage, 1);
		server->cell = cell;

		INIT_LIST_HEAD(&server->link);
		INIT_LIST_HEAD(&server->grave);
		init_rwsem(&server->sem);
		spin_lock_init(&server->fs_lock);
		server->fs_vnodes = RB_ROOT;
		server->cb_promises = RB_ROOT;
		spin_lock_init(&server->cb_lock);
		init_waitqueue_head(&server->cb_break_waitq);
		INIT_DELAYED_WORK(&server->cb_break_work,
				  afs_dispatch_give_up_callbacks);

		memcpy(&server->addr, addr, sizeof(struct in_addr));
		server->addr.s_addr = addr->s_addr;
94 95 96
		_leave(" = %p{%d}", server, atomic_read(&server->usage));
	} else {
		_leave(" = NULL [nomem]");
L
Linus Torvalds 已提交
97
	}
98 99
	return server;
}
L
Linus Torvalds 已提交
100

101 102 103 104 105 106 107
/*
 * get an FS-server record for a cell
 */
struct afs_server *afs_lookup_server(struct afs_cell *cell,
				     const struct in_addr *addr)
{
	struct afs_server *server, *candidate;
L
Linus Torvalds 已提交
108

H
Harvey Harrison 已提交
109
	_enter("%p,%pI4", cell, &addr->s_addr);
L
Linus Torvalds 已提交
110

111 112
	/* quick scan of the list to see if we already have the server */
	read_lock(&cell->servers_lock);
L
Linus Torvalds 已提交
113

114 115 116
	list_for_each_entry(server, &cell->servers, link) {
		if (server->addr.s_addr == addr->s_addr)
			goto found_server_quickly;
L
Linus Torvalds 已提交
117
	}
118
	read_unlock(&cell->servers_lock);
L
Linus Torvalds 已提交
119

120 121 122 123
	candidate = afs_alloc_server(cell, addr);
	if (!candidate) {
		_leave(" = -ENOMEM");
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
124 125
	}

126
	write_lock(&cell->servers_lock);
L
Linus Torvalds 已提交
127

128 129 130 131 132 133 134 135 136 137
	/* check the cell's server list again */
	list_for_each_entry(server, &cell->servers, link) {
		if (server->addr.s_addr == addr->s_addr)
			goto found_server;
	}

	_debug("new");
	server = candidate;
	if (afs_install_server(server) < 0)
		goto server_in_two_cells;
L
Linus Torvalds 已提交
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	afs_get_cell(cell);
	list_add_tail(&server->link, &cell->servers);

	write_unlock(&cell->servers_lock);
	_leave(" = %p{%d}", server, atomic_read(&server->usage));
	return server;

	/* found a matching server quickly */
found_server_quickly:
	_debug("found quickly");
	afs_get_server(server);
	read_unlock(&cell->servers_lock);
no_longer_unused:
	if (!list_empty(&server->grave)) {
		spin_lock(&afs_server_graveyard_lock);
		list_del_init(&server->grave);
		spin_unlock(&afs_server_graveyard_lock);
	}
	_leave(" = %p{%d}", server, atomic_read(&server->usage));
	return server;

	/* found a matching server on the second pass */
found_server:
	_debug("found");
	afs_get_server(server);
	write_unlock(&cell->servers_lock);
	kfree(candidate);
	goto no_longer_unused;

	/* found a server that seems to be in two cells */
server_in_two_cells:
	write_unlock(&cell->servers_lock);
	kfree(candidate);
H
Harvey Harrison 已提交
172 173
	printk(KERN_NOTICE "kAFS: Server %pI4 appears to be in two cells\n",
	       addr);
174 175 176
	_leave(" = -EEXIST");
	return ERR_PTR(-EEXIST);
}
L
Linus Torvalds 已提交
177

178 179 180
/*
 * look up a server by its IP address
 */
181
struct afs_server *afs_find_server(const struct sockaddr_rxrpc *srx)
182 183 184
{
	struct afs_server *server = NULL;
	struct rb_node *p;
185
	struct in_addr addr = srx->transport.sin.sin_addr;
L
Linus Torvalds 已提交
186

187 188 189 190 191 192
	_enter("{%d,%pI4}", srx->transport.family, &addr.s_addr);

	if (srx->transport.family != AF_INET) {
		WARN(true, "AFS does not yes support non-IPv4 addresses\n");
		return NULL;
	}
L
Linus Torvalds 已提交
193

194
	read_lock(&afs_servers_lock);
L
Linus Torvalds 已提交
195

196 197 198
	p = afs_servers.rb_node;
	while (p) {
		server = rb_entry(p, struct afs_server, master_rb);
L
Linus Torvalds 已提交
199

200
		_debug("- consider %p", p);
L
Linus Torvalds 已提交
201

202 203 204 205 206 207 208 209 210
		if (addr.s_addr < server->addr.s_addr) {
			p = p->rb_left;
		} else if (addr.s_addr > server->addr.s_addr) {
			p = p->rb_right;
		} else {
			afs_get_server(server);
			goto found;
		}
	}
L
Linus Torvalds 已提交
211

212 213 214 215 216 217
	server = NULL;
found:
	read_unlock(&afs_servers_lock);
	ASSERTIFCMP(server, server->addr.s_addr, ==, addr.s_addr);
	_leave(" = %p", server);
	return server;
D
David Howells 已提交
218
}
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226 227 228

/*
 * destroy a server record
 * - removes from the cell list
 */
void afs_put_server(struct afs_server *server)
{
	if (!server)
		return;

229
	_enter("%p{%d}", server, atomic_read(&server->usage));
L
Linus Torvalds 已提交
230

231 232
	_debug("PUT SERVER %d", atomic_read(&server->usage));

233
	ASSERTCMP(atomic_read(&server->usage), >, 0);
L
Linus Torvalds 已提交
234 235 236 237 238 239

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

240
	afs_flush_callback_breaks(server);
L
Linus Torvalds 已提交
241

242 243 244 245
	spin_lock(&afs_server_graveyard_lock);
	if (atomic_read(&server->usage) == 0) {
		list_move_tail(&server->grave, &afs_server_graveyard);
		server->time_of_death = get_seconds();
246 247
		queue_delayed_work(afs_wq, &afs_server_reaper,
				   afs_server_timeout * HZ);
248 249 250
	}
	spin_unlock(&afs_server_graveyard_lock);
	_leave(" [dead]");
D
David Howells 已提交
251
}
L
Linus Torvalds 已提交
252 253

/*
254
 * destroy a dead server
L
Linus Torvalds 已提交
255
 */
256
static void afs_destroy_server(struct afs_server *server)
L
Linus Torvalds 已提交
257 258 259
{
	_enter("%p", server);

D
David Howells 已提交
260 261 262
	ASSERTIF(server->cb_break_head != server->cb_break_tail,
		 delayed_work_pending(&server->cb_break_work));

263 264 265 266
	ASSERTCMP(server->fs_vnodes.rb_node, ==, NULL);
	ASSERTCMP(server->cb_promises.rb_node, ==, NULL);
	ASSERTCMP(server->cb_break_head, ==, server->cb_break_tail);
	ASSERTCMP(atomic_read(&server->cb_break_n), ==, 0);
L
Linus Torvalds 已提交
267

268
	afs_put_cell(server->cell);
L
Linus Torvalds 已提交
269
	kfree(server);
D
David Howells 已提交
270
}
L
Linus Torvalds 已提交
271 272

/*
273
 * reap dead server records
L
Linus Torvalds 已提交
274
 */
275
static void afs_reap_server(struct work_struct *work)
L
Linus Torvalds 已提交
276
{
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	LIST_HEAD(corpses);
	struct afs_server *server;
	unsigned long delay, expiry;
	time_t now;

	now = get_seconds();
	spin_lock(&afs_server_graveyard_lock);

	while (!list_empty(&afs_server_graveyard)) {
		server = list_entry(afs_server_graveyard.next,
				    struct afs_server, grave);

		/* the queue is ordered most dead first */
		expiry = server->time_of_death + afs_server_timeout;
		if (expiry > now) {
			delay = (expiry - now) * HZ;
293
			mod_delayed_work(afs_wq, &afs_server_reaper, delay);
294
			break;
L
Linus Torvalds 已提交
295 296
		}

297 298 299 300 301 302 303 304
		write_lock(&server->cell->servers_lock);
		write_lock(&afs_servers_lock);
		if (atomic_read(&server->usage) > 0) {
			list_del_init(&server->grave);
		} else {
			list_move_tail(&server->grave, &corpses);
			list_del_init(&server->link);
			rb_erase(&server->master_rb, &afs_servers);
L
Linus Torvalds 已提交
305
		}
306 307
		write_unlock(&afs_servers_lock);
		write_unlock(&server->cell->servers_lock);
L
Linus Torvalds 已提交
308 309
	}

310
	spin_unlock(&afs_server_graveyard_lock);
L
Linus Torvalds 已提交
311

312 313 314 315 316
	/* now reap the corpses we've extracted */
	while (!list_empty(&corpses)) {
		server = list_entry(corpses.next, struct afs_server, grave);
		list_del(&server->grave);
		afs_destroy_server(server);
L
Linus Torvalds 已提交
317
	}
D
David Howells 已提交
318
}
L
Linus Torvalds 已提交
319 320

/*
321
 * discard all the server records for rmmod
L
Linus Torvalds 已提交
322
 */
323
void __exit afs_purge_servers(void)
L
Linus Torvalds 已提交
324
{
325
	afs_server_timeout = 0;
326
	mod_delayed_work(afs_wq, &afs_server_reaper, 0);
D
David Howells 已提交
327
}