server.c 8.3 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
 * 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>
14
#include "afs_fs.h"
L
Linus Torvalds 已提交
15 16
#include "internal.h"

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

D
David Howells 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
static void afs_inc_servers_outstanding(struct afs_net *net)
{
	atomic_inc(&net->servers_outstanding);
}

static void afs_dec_servers_outstanding(struct afs_net *net)
{
	if (atomic_dec_and_test(&net->servers_outstanding))
		wake_up_atomic_t(&net->servers_outstanding);
}

void afs_server_timer(struct timer_list *timer)
{
	struct afs_net *net = container_of(timer, struct afs_net, server_timer);

	if (!queue_work(afs_wq, &net->server_reaper))
		afs_dec_servers_outstanding(net);
}

38 39 40 41
/*
 * install a server record in the master tree
 */
static int afs_install_server(struct afs_server *server)
L
Linus Torvalds 已提交
42
{
43
	struct afs_server *xserver;
44
	struct afs_net *net = server->cell->net;
45
	struct rb_node **pp, *p;
46
	int ret, diff;
47 48

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

50
	write_lock(&net->servers_lock);
51 52

	ret = -EEXIST;
53
	pp = &net->servers.rb_node;
54 55 56 57 58
	p = NULL;
	while (*pp) {
		p = *pp;
		_debug("- consider %p", p);
		xserver = rb_entry(p, struct afs_server, master_rb);
59 60
		diff = memcmp(&server->addr, &xserver->addr, sizeof(server->addr));
		if (diff < 0)
61
			pp = &(*pp)->rb_left;
62
		else if (diff > 0)
63 64 65 66
			pp = &(*pp)->rb_right;
		else
			goto error;
	}
L
Linus Torvalds 已提交
67

68
	rb_link_node(&server->master_rb, p, pp);
69
	rb_insert_color(&server->master_rb, &net->servers);
70
	ret = 0;
L
Linus Torvalds 已提交
71

72
error:
73
	write_unlock(&net->servers_lock);
74 75
	return ret;
}
L
Linus Torvalds 已提交
76 77

/*
78
 * allocate a new server record
L
Linus Torvalds 已提交
79
 */
80
static struct afs_server *afs_alloc_server(struct afs_cell *cell,
81
					   const struct sockaddr_rxrpc *addr)
L
Linus Torvalds 已提交
82
{
83
	struct afs_server *server;
L
Linus Torvalds 已提交
84

85
	_enter("");
L
Linus Torvalds 已提交
86

87
	server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
88 89
	if (server) {
		atomic_set(&server->usage, 1);
90
		server->net = cell->net;
91 92 93 94 95 96
		server->cell = cell;

		INIT_LIST_HEAD(&server->link);
		INIT_LIST_HEAD(&server->grave);
		init_rwsem(&server->sem);
		spin_lock_init(&server->fs_lock);
97 98
		INIT_LIST_HEAD(&server->cb_interests);
		rwlock_init(&server->cb_break_lock);
99

100
		server->addr = *addr;
D
David Howells 已提交
101
		afs_inc_servers_outstanding(cell->net);
102 103 104
		_leave(" = %p{%d}", server, atomic_read(&server->usage));
	} else {
		_leave(" = NULL [nomem]");
L
Linus Torvalds 已提交
105
	}
106 107
	return server;
}
L
Linus Torvalds 已提交
108

109 110 111 112
/*
 * get an FS-server record for a cell
 */
struct afs_server *afs_lookup_server(struct afs_cell *cell,
113
				     struct sockaddr_rxrpc *addr)
114 115
{
	struct afs_server *server, *candidate;
L
Linus Torvalds 已提交
116

117
	_enter("%p,%pIS", cell, &addr->transport);
L
Linus Torvalds 已提交
118

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

122
	list_for_each_entry(server, &cell->servers, link) {
123
		if (memcmp(&server->addr, addr, sizeof(*addr)) == 0)
124
			goto found_server_quickly;
L
Linus Torvalds 已提交
125
	}
126
	read_unlock(&cell->servers_lock);
L
Linus Torvalds 已提交
127

128 129 130 131
	candidate = afs_alloc_server(cell, addr);
	if (!candidate) {
		_leave(" = -ENOMEM");
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
132 133
	}

134
	write_lock(&cell->servers_lock);
L
Linus Torvalds 已提交
135

136 137
	/* check the cell's server list again */
	list_for_each_entry(server, &cell->servers, link) {
138
		if (memcmp(&server->addr, addr, sizeof(*addr)) == 0)
139 140 141 142 143 144 145
			goto found_server;
	}

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

147 148 149 150 151 152 153 154 155 156 157 158 159 160
	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)) {
161
		spin_lock(&cell->net->server_graveyard_lock);
162
		list_del_init(&server->grave);
163
		spin_unlock(&cell->net->server_graveyard_lock);
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	}
	_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);
D
David Howells 已提交
180
	afs_dec_servers_outstanding(cell->net);
H
Harvey Harrison 已提交
181 182
	printk(KERN_NOTICE "kAFS: Server %pI4 appears to be in two cells\n",
	       addr);
183 184 185
	_leave(" = -EEXIST");
	return ERR_PTR(-EEXIST);
}
L
Linus Torvalds 已提交
186

187 188 189
/*
 * look up a server by its IP address
 */
190 191
struct afs_server *afs_find_server(struct afs_net *net,
				   const struct sockaddr_rxrpc *srx)
192 193 194
{
	struct afs_server *server = NULL;
	struct rb_node *p;
195
	int diff;
L
Linus Torvalds 已提交
196

197
	_enter("{%d,%pIS}", srx->transport.family, &srx->transport);
198

199
	read_lock(&net->servers_lock);
L
Linus Torvalds 已提交
200

201
	p = net->servers.rb_node;
202 203
	while (p) {
		server = rb_entry(p, struct afs_server, master_rb);
L
Linus Torvalds 已提交
204

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

207 208
		diff = memcmp(srx, &server->addr, sizeof(*srx));
		if (diff < 0) {
209
			p = p->rb_left;
210
		} else if (diff > 0) {
211 212 213 214 215 216
			p = p->rb_right;
		} else {
			afs_get_server(server);
			goto found;
		}
	}
L
Linus Torvalds 已提交
217

218 219
	server = NULL;
found:
220
	read_unlock(&net->servers_lock);
221 222
	_leave(" = %p", server);
	return server;
D
David Howells 已提交
223
}
L
Linus Torvalds 已提交
224

D
David Howells 已提交
225 226 227 228 229 230 231 232 233 234 235 236
static void afs_set_server_timer(struct afs_net *net, time64_t delay)
{
	afs_inc_servers_outstanding(net);
	if (net->live) {
		if (timer_reduce(&net->server_timer, jiffies + delay * HZ))
			afs_dec_servers_outstanding(net);
	} else {
		if (!queue_work(afs_wq, &net->server_reaper))
			afs_dec_servers_outstanding(net);
	}
}

L
Linus Torvalds 已提交
237 238 239 240
/*
 * destroy a server record
 * - removes from the cell list
 */
241
void afs_put_server(struct afs_net *net, struct afs_server *server)
L
Linus Torvalds 已提交
242 243 244 245
{
	if (!server)
		return;

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

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

250
	ASSERTCMP(atomic_read(&server->usage), >, 0);
L
Linus Torvalds 已提交
251 252 253 254 255 256

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

257
	spin_lock(&net->server_graveyard_lock);
258
	if (atomic_read(&server->usage) == 0) {
259
		list_move_tail(&server->grave, &net->server_graveyard);
260
		server->time_of_death = ktime_get_real_seconds();
D
David Howells 已提交
261
		afs_set_server_timer(net, afs_server_timeout);
262
	}
263
	spin_unlock(&net->server_graveyard_lock);
264
	_leave(" [dead]");
D
David Howells 已提交
265
}
L
Linus Torvalds 已提交
266 267

/*
268
 * destroy a dead server
L
Linus Torvalds 已提交
269
 */
D
David Howells 已提交
270
static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
L
Linus Torvalds 已提交
271 272 273
{
	_enter("%p", server);

274 275
	afs_fs_give_up_all_callbacks(server, NULL, false);
	afs_put_cell(net, server->cell);
L
Linus Torvalds 已提交
276
	kfree(server);
D
David Howells 已提交
277
	afs_dec_servers_outstanding(net);
D
David Howells 已提交
278
}
L
Linus Torvalds 已提交
279 280

/*
281
 * reap dead server records
L
Linus Torvalds 已提交
282
 */
283
void afs_reap_server(struct work_struct *work)
L
Linus Torvalds 已提交
284
{
285 286
	LIST_HEAD(corpses);
	struct afs_server *server;
D
David Howells 已提交
287
	struct afs_net *net = container_of(work, struct afs_net, server_reaper);
288
	unsigned long delay, expiry;
289
	time64_t now;
290

291
	now = ktime_get_real_seconds();
292
	spin_lock(&net->server_graveyard_lock);
293

294 295
	while (!list_empty(&net->server_graveyard)) {
		server = list_entry(net->server_graveyard.next,
296 297 298
				    struct afs_server, grave);

		/* the queue is ordered most dead first */
299 300 301
		if (net->live) {
			expiry = server->time_of_death + afs_server_timeout;
			if (expiry > now) {
D
David Howells 已提交
302 303
				delay = (expiry - now);
				afs_set_server_timer(net, delay);
304 305
				break;
			}
L
Linus Torvalds 已提交
306 307
		}

308
		write_lock(&server->cell->servers_lock);
309
		write_lock(&net->servers_lock);
310 311 312 313 314
		if (atomic_read(&server->usage) > 0) {
			list_del_init(&server->grave);
		} else {
			list_move_tail(&server->grave, &corpses);
			list_del_init(&server->link);
315
			rb_erase(&server->master_rb, &net->servers);
L
Linus Torvalds 已提交
316
		}
317
		write_unlock(&net->servers_lock);
318
		write_unlock(&server->cell->servers_lock);
L
Linus Torvalds 已提交
319 320
	}

321
	spin_unlock(&net->server_graveyard_lock);
L
Linus Torvalds 已提交
322

323 324 325 326
	/* now reap the corpses we've extracted */
	while (!list_empty(&corpses)) {
		server = list_entry(corpses.next, struct afs_server, grave);
		list_del(&server->grave);
D
David Howells 已提交
327
		afs_destroy_server(net, server);
L
Linus Torvalds 已提交
328
	}
D
David Howells 已提交
329 330

	afs_dec_servers_outstanding(net);
D
David Howells 已提交
331
}
L
Linus Torvalds 已提交
332 333

/*
334 335
 * Discard all the server records from a net namespace when it is destroyed or
 * the afs module is removed.
L
Linus Torvalds 已提交
336
 */
337
void __net_exit afs_purge_servers(struct afs_net *net)
L
Linus Torvalds 已提交
338
{
D
David Howells 已提交
339 340 341 342 343 344 345 346 347
	if (del_timer_sync(&net->server_timer))
		atomic_dec(&net->servers_outstanding);

	afs_inc_servers_outstanding(net);
	if (!queue_work(afs_wq, &net->server_reaper))
		afs_dec_servers_outstanding(net);

	wait_on_atomic_t(&net->servers_outstanding, atomic_t_wait,
			 TASK_UNINTERRUPTIBLE);
D
David Howells 已提交
348
}