summaryrefslogtreecommitdiff
path: root/fs/afs/server.c
blob: 662f7fbf5d050af539d3b72ddc1a825c2990622b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* AFS server record management
 *
 * Copyright (C) 2002, 2007 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/sched.h>
#include <linux/slab.h>
#include "afs_fs.h"
#include "internal.h"

static unsigned afs_server_timeout = 10;	/* server timeout in seconds */

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);
}

/*
 * install a server record in the master tree
 */
static int afs_install_server(struct afs_server *server)
{
	struct afs_server *xserver;
	struct afs_net *net = server->cell->net;
	struct rb_node **pp, *p;
	int ret, diff;

	_enter("%p", server);

	write_lock(&net->servers_lock);

	ret = -EEXIST;
	pp = &net->servers.rb_node;
	p = NULL;
	while (*pp) {
		p = *pp;
		_debug("- consider %p", p);
		xserver = rb_entry(p, struct afs_server, master_rb);
		diff = memcmp(&server->addr, &xserver->addr, sizeof(server->addr));
		if (diff < 0)
			pp = &(*pp)->rb_left;
		else if (diff > 0)
			pp = &(*pp)->rb_right;
		else
			goto error;
	}

	rb_link_node(&server->master_rb, p, pp);
	rb_insert_color(&server->master_rb, &net->servers);
	ret = 0;

error:
	write_unlock(&net->servers_lock);
	return ret;
}

/*
 * allocate a new server record
 */
static struct afs_server *afs_alloc_server(struct afs_cell *cell,
					   const struct sockaddr_rxrpc *addr)
{
	struct afs_server *server;

	_enter("");

	server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
	if (server) {
		atomic_set(&server->usage, 1);
		server->net = cell->net;
		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);

		server->addr = *addr;
		afs_inc_servers_outstanding(cell->net);
		_leave(" = %p{%d}", server, atomic_read(&server->usage));
	} else {
		_leave(" = NULL [nomem]");
	}
	return server;
}

/*
 * get an FS-server record for a cell
 */
struct afs_server *afs_lookup_server(struct afs_cell *cell,
				     struct sockaddr_rxrpc *addr)
{
	struct afs_server *server, *candidate;

	_enter("%p,%pIS", cell, &addr->transport);

	/* quick scan of the list to see if we already have the server */
	read_lock(&cell->servers_lock);

	list_for_each_entry(server, &cell->servers, link) {
		if (memcmp(&server->addr, addr, sizeof(*addr)) == 0)
			goto found_server_quickly;
	}
	read_unlock(&cell->servers_lock);

	candidate = afs_alloc_server(cell, addr);
	if (!candidate) {
		_leave(" = -ENOMEM");
		return ERR_PTR(-ENOMEM);
	}

	write_lock(&cell->servers_lock);

	/* check the cell's server list again */
	list_for_each_entry(server, &cell->servers, link) {
		if (memcmp(&server->addr, addr, sizeof(*addr)) == 0)
			goto found_server;
	}

	_debug("new");
	server = candidate;
	if (afs_install_server(server) < 0)
		goto server_in_two_cells;

	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(&cell->net->server_graveyard_lock);
		list_del_init(&server->grave);
		spin_unlock(&cell->net->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);
	afs_dec_servers_outstanding(cell->net);
	printk(KERN_NOTICE "kAFS: Server %pI4 appears to be in two cells\n",
	       addr);
	_leave(" = -EEXIST");
	return ERR_PTR(-EEXIST);
}

/*
 * look up a server by its IP address
 */
struct afs_server *afs_find_server(struct afs_net *net,
				   const struct sockaddr_rxrpc *srx)
{
	struct afs_server *server = NULL;
	struct rb_node *p;
	int diff;

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

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

	read_lock(&net->servers_lock);

	p = net->servers.rb_node;
	while (p) {
		server = rb_entry(p, struct afs_server, master_rb);

		_debug("- consider %p", p);

		diff = memcmp(srx, &server->addr, sizeof(*srx));
		if (diff < 0) {
			p = p->rb_left;
		} else if (diff > 0) {
			p = p->rb_right;
		} else {
			afs_get_server(server);
			goto found;
		}
	}

	server = NULL;
found:
	read_unlock(&net->servers_lock);
	_leave(" = %p", server);
	return server;
}

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);
	}
}

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

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

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

	ASSERTCMP(atomic_read(&server->usage), >, 0);

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

	afs_flush_callback_breaks(server);

	spin_lock(&net->server_graveyard_lock);
	if (atomic_read(&server->usage) == 0) {
		list_move_tail(&server->grave, &net->server_graveyard);
		server->time_of_death = ktime_get_real_seconds();
		afs_set_server_timer(net, afs_server_timeout);
	}
	spin_unlock(&net->server_graveyard_lock);
	_leave(" [dead]");
}

/*
 * destroy a dead server
 */
static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
{
	_enter("%p", server);

	ASSERTIF(server->cb_break_head != server->cb_break_tail,
		 delayed_work_pending(&server->cb_break_work));

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

	afs_put_cell(server->net, server->cell);
	kfree(server);
	afs_dec_servers_outstanding(net);
}

/*
 * reap dead server records
 */
void afs_reap_server(struct work_struct *work)
{
	LIST_HEAD(corpses);
	struct afs_server *server;
	struct afs_net *net = container_of(work, struct afs_net, server_reaper);
	unsigned long delay, expiry;
	time64_t now;

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

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

		/* the queue is ordered most dead first */
		if (net->live) {
			expiry = server->time_of_death + afs_server_timeout;
			if (expiry > now) {
				delay = (expiry - now);
				afs_set_server_timer(net, delay);
				break;
			}
		}

		write_lock(&server->cell->servers_lock);
		write_lock(&net->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, &net->servers);
		}
		write_unlock(&net->servers_lock);
		write_unlock(&server->cell->servers_lock);
	}

	spin_unlock(&net->server_graveyard_lock);

	/* 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(net, server);
	}

	afs_dec_servers_outstanding(net);
}

/*
 * Discard all the server records from a net namespace when it is destroyed or
 * the afs module is removed.
 */
void __net_exit afs_purge_servers(struct afs_net *net)
{
	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);
}