aboutsummaryrefslogtreecommitdiffstats
path: root/fs/afs/cell.c
blob: bfc1fd22d5b10578088aaf12e527e87c7bf163ee (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* cell.c: AFS cell and server record management
 *
 * 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/sched.h>
#include <linux/slab.h>
#include <rxrpc/peer.h>
#include <rxrpc/connection.h>
#include "volume.h"
#include "cell.h"
#include "server.h"
#include "transport.h"
#include "vlclient.h"
#include "kafstimod.h"
#include "super.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 */
static struct afs_cell *afs_cell_root;

#ifdef AFS_CACHING_SUPPORT
static cachefs_match_val_t afs_cell_cache_match(void *target,
						const void *entry);
static void afs_cell_cache_update(void *source, void *entry);

struct cachefs_index_def afs_cache_cell_index_def = {
	.name			= "cell_ix",
	.data_size		= sizeof(struct afs_cache_cell),
	.keys[0]		= { CACHEFS_INDEX_KEYS_ASCIIZ, 64 },
	.match			= afs_cell_cache_match,
	.update			= afs_cell_cache_update,
};
#endif

/*****************************************************************************/
/*
 * 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
 */
int afs_cell_create(const char *name, char *vllist, struct afs_cell **_cell)
{
	struct afs_cell *cell;
	char *next;
	int ret;

	_enter("%s", name);

	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");
		return -ENOMEM;
	}

	down_write(&afs_cells_sem);

	memset(cell, 0, sizeof(struct afs_cell));
	atomic_set(&cell->usage, 0);

	INIT_LIST_HEAD(&cell->link);

	rwlock_init(&cell->sv_lock);
	INIT_LIST_HEAD(&cell->sv_list);
	INIT_LIST_HEAD(&cell->sv_graveyard);
	spin_lock_init(&cell->sv_gylock);

	init_rwsem(&cell->vl_sem);
	INIT_LIST_HEAD(&cell->vl_list);
	INIT_LIST_HEAD(&cell->vl_graveyard);
	spin_lock_init(&cell->vl_gylock);

	strcpy(cell->name,name);

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

	} while(vllist = next, vllist);

	/* add a proc dir for this cell */
	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);

	*_cell = cell;
	up_write(&afs_cells_sem);

	_leave(" = 0 (%p)", cell);
	return 0;

 badaddr:
	printk(KERN_ERR "kAFS: bad VL server IP address: '%s'\n", vllist);
 error:
	up_write(&afs_cells_sem);
	kfree(cell);
	_leave(" = %d", ret);
	return ret;
} /* end afs_cell_create() */

/*****************************************************************************/
/*
 * initialise the cell database from module parameters
 */
int afs_cell_init(char *rootcell)
{
	struct afs_cell *old_root, *new_root;
	char *cp;
	int ret;

	_enter("");

	if (!rootcell) {
		/* module is loaded with no parameters, or built statically.
		 * - in the future we might initialize cell DB here.
		 */
		_leave(" = 0 (but no root)");
		return 0;
	}

	cp = strchr(rootcell, ':');
	if (!cp) {
		printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
		_leave(" = %d (no colon)", -EINVAL);
		return -EINVAL;
	}

	/* allocate a cell record for the root cell */
	*cp++ = 0;
	ret = afs_cell_create(rootcell, cp, &new_root);
	if (ret < 0) {
		_leave(" = %d", ret);
		return ret;
	}

	/* as afs_put_cell() takes locks by itself, we have to do
	 * a little gymnastics to be race-free.
	 */
	afs_get_cell(new_root);

	write_lock(&afs_cells_lock);
	while (afs_cell_root) {
		old_root = afs_cell_root;
		afs_cell_root = NULL;
		write_unlock(&afs_cells_lock);
		afs_put_cell(old_root);
		write_lock(&afs_cells_lock);
	}
	afs_cell_root = new_root;
	write_unlock(&afs_cells_lock);

	_leave(" = %d", ret);
	return ret;

} /* end afs_cell_init() */

/*****************************************************************************/
/*
 * lookup a cell record
 */
int afs_cell_lookup(const char *name, unsigned namesz, struct afs_cell **_cell)
{
	struct afs_cell *cell;
	int ret;

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

	*_cell = NULL;

	if (name) {
		/* if the cell was named, look for it in the cell record list */
		ret = -ENOENT;
		cell = NULL;
		read_lock(&afs_cells_lock);

		list_for_each_entry(cell, &afs_cells, link) {
			if (strncmp(cell->name, name, namesz) == 0) {
				afs_get_cell(cell);
				goto found;
			}
		}
		cell = NULL;
	found:

		read_unlock(&afs_cells_lock);

		if (cell)
			ret = 0;
	}
	else {
		read_lock(&afs_cells_lock);

		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.
			 */
			ret = -EDESTADDRREQ;
		}
		else {
			afs_get_cell(cell);
			ret = 0;
		}

		read_unlock(&afs_cells_lock);
	}

	*_cell = cell;
	_leave(" = %d (%p)", ret, cell);
	return ret;

} /* end afs_cell_lookup() */

/*****************************************************************************/
/*
 * try and get a cell record
 */
struct afs_cell *afs_get_cell_maybe(struct afs_cell **_cell)
{
	struct afs_cell *cell;

	write_lock(&afs_cells_lock);

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

	write_unlock(&afs_cells_lock);

	return cell;
} /* end afs_get_cell_maybe() */

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

	/* sanity check */
	BUG_ON(atomic_read(&cell->usage) <= 0);

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

	write_unlock(&afs_cells_lock);

	BUG_ON(!list_empty(&cell->sv_list));
	BUG_ON(!list_empty(&cell->sv_graveyard));
	BUG_ON(!list_empty(&cell->vl_list));
	BUG_ON(!list_empty(&cell->vl_graveyard));

	_leave(" [unused]");
} /* end afs_put_cell() */

/*****************************************************************************/
/*
 * destroy a cell record
 */
static void afs_cell_destroy(struct afs_cell *cell)
{
	_enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);

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

	/* sanity check */
	BUG_ON(atomic_read(&cell->usage) != 0);

	list_del_init(&cell->link);

	write_unlock(&afs_cells_lock);

	down_write(&afs_cells_sem);

	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

	up_write(&afs_cells_sem);

	BUG_ON(!list_empty(&cell->sv_list));
	BUG_ON(!list_empty(&cell->sv_graveyard));
	BUG_ON(!list_empty(&cell->vl_list));
	BUG_ON(!list_empty(&cell->vl_graveyard));

	/* finish cleaning up the cell */
	kfree(cell);

	_leave(" [destroyed]");
} /* end afs_cell_destroy() */

/*****************************************************************************/
/*
 * lookup the server record corresponding to an Rx RPC peer
 */
int afs_server_find_by_peer(const struct rxrpc_peer *peer,
			    struct afs_server **_server)
{
	struct afs_server *server;
	struct afs_cell *cell;

	_enter("%p{a=%08x},", peer, ntohl(peer->addr.s_addr));

	/* search the cell list */
	read_lock(&afs_cells_lock);

	list_for_each_entry(cell, &afs_cells, link) {

		_debug("? cell %s",cell->name);

		write_lock(&cell->sv_lock);

		/* check the active list */
		list_for_each_entry(server, &cell->sv_list, link) {
			_debug("?? server %08x", ntohl(server->addr.s_addr));

			if (memcmp(&server->addr, &peer->addr,
				   sizeof(struct in_addr)) == 0)
				goto found_server;
		}

		/* check the inactive list */
		spin_lock(&cell->sv_gylock);
		list_for_each_entry(server, &cell->sv_graveyard, link) {
			_debug("?? dead server %08x",
			       ntohl(server->addr.s_addr));

			if (memcmp(&server->addr, &peer->addr,
				   sizeof(struct in_addr)) == 0)
				goto found_dead_server;
		}
		spin_unlock(&cell->sv_gylock);

		write_unlock(&cell->sv_lock);
	}
	read_unlock(&afs_cells_lock);

	_leave(" = -ENOENT");
	return -ENOENT;

	/* we found it in the graveyard - resurrect it */
 found_dead_server:
	list_move_tail(&server->link, &cell->sv_list);
	afs_get_server(server);
	afs_kafstimod_del_timer(&server->timeout);
	spin_unlock(&cell->sv_gylock);
	goto success;

	/* we found it - increment its ref count and return it */
 found_server:
	afs_get_server(server);

 success:
	write_unlock(&cell->sv_lock);
	read_unlock(&afs_cells_lock);

	*_server = server;
	_leave(" = 0 (s=%p c=%p)", server, cell);
	return 0;

} /* end afs_server_find_by_peer() */

/*****************************************************************************/
/*
 * 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_vlocation *vlocation;
	struct afs_cell *cell;

	_enter("");

	afs_put_cell(afs_cell_root);

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

			BUG_ON(!list_empty(&cell->sv_list));
			BUG_ON(!list_empty(&cell->vl_list));

			/* purge the cell's VL graveyard list */
			_debug(" - clearing VL graveyard");

			spin_lock(&cell->vl_gylock);

			while (!list_empty(&cell->vl_graveyard)) {
				vlocation = list_entry(cell->vl_graveyard.next,
						       struct afs_vlocation,
						       link);
				list_del_init(&vlocation->link);

				afs_kafstimod_del_timer(&vlocation->timeout);

				spin_unlock(&cell->vl_gylock);

				afs_vlocation_do_timeout(vlocation);
				/* TODO: race if move to use krxtimod instead
				 * of kafstimod */

				spin_lock(&cell->vl_gylock);
			}

			spin_unlock(&cell->vl_gylock);

			/* purge the cell's server graveyard list */
			_debug(" - clearing server graveyard");

			spin_lock(&cell->sv_gylock);

			while (!list_empty(&cell->sv_graveyard)) {
				struct afs_server *server;

				server = list_entry(cell->sv_graveyard.next,
						    struct afs_server, link);
				list_del_init(&server->link);

				afs_kafstimod_del_timer(&server->timeout);

				spin_unlock(&cell->sv_gylock);

				afs_server_do_timeout(server);

				spin_lock(&cell->sv_gylock);
			}

			spin_unlock(&cell->sv_gylock);

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

	_leave("");
} /* end afs_cell_purge() */

/*****************************************************************************/
/*
 * match a cell record obtained from the cache
 */
#ifdef AFS_CACHING_SUPPORT
static cachefs_match_val_t afs_cell_cache_match(void *target,
						const void *entry)
{
	const struct afs_cache_cell *ccell = entry;
	struct afs_cell *cell = target;

	_enter("{%s},{%s}", ccell->name, cell->name);

	if (strncmp(ccell->name, cell->name, sizeof(ccell->name)) == 0) {
		_leave(" = SUCCESS");
		return CACHEFS_MATCH_SUCCESS;
	}

	_leave(" = FAILED");
	return CACHEFS_MATCH_FAILED;
} /* end afs_cell_cache_match() */
#endif

/*****************************************************************************/
/*
 * update a cell record in the cache
 */
#ifdef AFS_CACHING_SUPPORT
static void afs_cell_cache_update(void *source, void *entry)
{
	struct afs_cache_cell *ccell = entry;
	struct afs_cell *cell = source;

	_enter("%p,%p", source, entry);

	strncpy(ccell->name, cell->name, sizeof(ccell->name));

	memcpy(ccell->vl_servers,
	       cell->vl_addrs,
	       min(sizeof(ccell->vl_servers), sizeof(cell->vl_addrs)));

} /* end afs_cell_cache_update() */
#endif