cmservice.c 13.6 KB
Newer Older
D
David Howells 已提交
1
/* AFS Cache Manager Service
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13
 *
 * 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/init.h>
14
#include <linux/slab.h>
L
Linus Torvalds 已提交
15
#include <linux/sched.h>
16
#include <linux/ip.h>
L
Linus Torvalds 已提交
17
#include "internal.h"
18
#include "afs_cm.h"
L
Linus Torvalds 已提交
19

20 21 22 23 24 25
static int afs_deliver_cb_init_call_back_state(struct afs_call *);
static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
static int afs_deliver_cb_probe(struct afs_call *);
static int afs_deliver_cb_callback(struct afs_call *);
static int afs_deliver_cb_probe_uuid(struct afs_call *);
static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
26
static void afs_cm_destructor(struct afs_call *);
27 28 29 30 31
static void SRXAFSCB_CallBack(struct work_struct *);
static void SRXAFSCB_InitCallBackState(struct work_struct *);
static void SRXAFSCB_Probe(struct work_struct *);
static void SRXAFSCB_ProbeUuid(struct work_struct *);
static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
L
Linus Torvalds 已提交
32

D
David Howells 已提交
33 34 35 36
#define CM_NAME(name) \
	const char afs_SRXCB##name##_name[] __tracepoint_string =	\
		"CB." #name

L
Linus Torvalds 已提交
37
/*
38
 * CB.CallBack operation type
L
Linus Torvalds 已提交
39
 */
D
David Howells 已提交
40
static CM_NAME(CallBack);
41
static const struct afs_call_type afs_SRXCBCallBack = {
D
David Howells 已提交
42
	.name		= afs_SRXCBCallBack_name,
43 44
	.deliver	= afs_deliver_cb_callback,
	.destructor	= afs_cm_destructor,
45
	.work		= SRXAFSCB_CallBack,
46
};
L
Linus Torvalds 已提交
47 48

/*
49
 * CB.InitCallBackState operation type
L
Linus Torvalds 已提交
50
 */
D
David Howells 已提交
51
static CM_NAME(InitCallBackState);
52
static const struct afs_call_type afs_SRXCBInitCallBackState = {
D
David Howells 已提交
53
	.name		= afs_SRXCBInitCallBackState_name,
54 55
	.deliver	= afs_deliver_cb_init_call_back_state,
	.destructor	= afs_cm_destructor,
56
	.work		= SRXAFSCB_InitCallBackState,
57
};
L
Linus Torvalds 已提交
58

59 60 61
/*
 * CB.InitCallBackState3 operation type
 */
D
David Howells 已提交
62
static CM_NAME(InitCallBackState3);
63
static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
D
David Howells 已提交
64
	.name		= afs_SRXCBInitCallBackState3_name,
65 66
	.deliver	= afs_deliver_cb_init_call_back_state3,
	.destructor	= afs_cm_destructor,
67
	.work		= SRXAFSCB_InitCallBackState,
68 69
};

L
Linus Torvalds 已提交
70
/*
71
 * CB.Probe operation type
L
Linus Torvalds 已提交
72
 */
D
David Howells 已提交
73
static CM_NAME(Probe);
74
static const struct afs_call_type afs_SRXCBProbe = {
D
David Howells 已提交
75
	.name		= afs_SRXCBProbe_name,
76 77
	.deliver	= afs_deliver_cb_probe,
	.destructor	= afs_cm_destructor,
78
	.work		= SRXAFSCB_Probe,
79
};
L
Linus Torvalds 已提交
80

81 82 83
/*
 * CB.ProbeUuid operation type
 */
D
David Howells 已提交
84
static CM_NAME(ProbeUuid);
85
static const struct afs_call_type afs_SRXCBProbeUuid = {
D
David Howells 已提交
86
	.name		= afs_SRXCBProbeUuid_name,
87 88
	.deliver	= afs_deliver_cb_probe_uuid,
	.destructor	= afs_cm_destructor,
89
	.work		= SRXAFSCB_ProbeUuid,
90 91
};

92
/*
93
 * CB.TellMeAboutYourself operation type
94
 */
D
David Howells 已提交
95
static CM_NAME(TellMeAboutYourself);
96
static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
D
David Howells 已提交
97
	.name		= afs_SRXCBTellMeAboutYourself_name,
98
	.deliver	= afs_deliver_cb_tell_me_about_yourself,
99
	.destructor	= afs_cm_destructor,
100
	.work		= SRXAFSCB_TellMeAboutYourself,
101 102
};

L
Linus Torvalds 已提交
103
/*
104 105
 * route an incoming cache manager call
 * - return T if supported, F if not
L
Linus Torvalds 已提交
106
 */
107
bool afs_cm_incoming_call(struct afs_call *call)
L
Linus Torvalds 已提交
108
{
109
	_enter("{CB.OP %u}", call->operation_ID);
110

111
	switch (call->operation_ID) {
112 113 114 115 116 117
	case CBCallBack:
		call->type = &afs_SRXCBCallBack;
		return true;
	case CBInitCallBackState:
		call->type = &afs_SRXCBInitCallBackState;
		return true;
118 119 120
	case CBInitCallBackState3:
		call->type = &afs_SRXCBInitCallBackState3;
		return true;
121 122 123
	case CBProbe:
		call->type = &afs_SRXCBProbe;
		return true;
D
David Howells 已提交
124 125 126
	case CBProbeUuid:
		call->type = &afs_SRXCBProbeUuid;
		return true;
127 128
	case CBTellMeAboutYourself:
		call->type = &afs_SRXCBTellMeAboutYourself;
129
		return true;
130 131
	default:
		return false;
L
Linus Torvalds 已提交
132
	}
D
David Howells 已提交
133
}
L
Linus Torvalds 已提交
134 135

/*
D
David Howells 已提交
136
 * Clean up a cache manager call.
L
Linus Torvalds 已提交
137
 */
138
static void afs_cm_destructor(struct afs_call *call)
L
Linus Torvalds 已提交
139
{
140 141
	kfree(call->buffer);
	call->buffer = NULL;
D
David Howells 已提交
142
}
L
Linus Torvalds 已提交
143 144

/*
145
 * The server supplied a list of callbacks that it wanted to break.
L
Linus Torvalds 已提交
146
 */
147
static void SRXAFSCB_CallBack(struct work_struct *work)
L
Linus Torvalds 已提交
148
{
149
	struct afs_call *call = container_of(work, struct afs_call, work);
L
Linus Torvalds 已提交
150

151
	_enter("");
L
Linus Torvalds 已提交
152

D
David Howells 已提交
153 154 155 156
	/* We need to break the callbacks before sending the reply as the
	 * server holds up change visibility till it receives our reply so as
	 * to maintain cache coherency.
	 */
157 158
	if (call->cm_server)
		afs_break_callbacks(call->cm_server, call->count, call->request);
D
David Howells 已提交
159 160

	afs_send_empty_reply(call);
161
	afs_put_call(call);
162
	_leave("");
D
David Howells 已提交
163
}
L
Linus Torvalds 已提交
164 165

/*
166
 * deliver request data to a CB.CallBack call
L
Linus Torvalds 已提交
167
 */
168
static int afs_deliver_cb_callback(struct afs_call *call)
L
Linus Torvalds 已提交
169
{
170
	struct afs_callback_break *cb;
171
	struct sockaddr_rxrpc srx;
172 173 174
	__be32 *bp;
	int ret, loop;

175
	_enter("{%u}", call->unmarshall);
176 177 178 179 180 181 182 183 184

	switch (call->unmarshall) {
	case 0:
		call->offset = 0;
		call->unmarshall++;

		/* extract the FID array and its count in two steps */
	case 1:
		_debug("extract FID count");
185
		ret = afs_extract_data(call, &call->tmp, 4, true);
186 187
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
188

189 190 191
		call->count = ntohl(call->tmp);
		_debug("FID count: %u", call->count);
		if (call->count > AFSCBMAX)
D
David Howells 已提交
192
			return afs_protocol_error(call, -EBADMSG);
193 194 195 196 197 198 199 200 201

		call->buffer = kmalloc(call->count * 3 * 4, GFP_KERNEL);
		if (!call->buffer)
			return -ENOMEM;
		call->offset = 0;
		call->unmarshall++;

	case 2:
		_debug("extract FID array");
202 203
		ret = afs_extract_data(call, call->buffer,
				       call->count * 3 * 4, true);
204 205
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
206

207 208
		_debug("unmarshall FID array");
		call->request = kcalloc(call->count,
209
					sizeof(struct afs_callback_break),
210 211 212 213 214 215 216 217 218 219
					GFP_KERNEL);
		if (!call->request)
			return -ENOMEM;

		cb = call->request;
		bp = call->buffer;
		for (loop = call->count; loop > 0; loop--, cb++) {
			cb->fid.vid	= ntohl(*bp++);
			cb->fid.vnode	= ntohl(*bp++);
			cb->fid.unique	= ntohl(*bp++);
220
			cb->cb.type	= AFSCM_CB_UNTYPED;
L
Linus Torvalds 已提交
221 222
		}

223 224 225 226 227 228
		call->offset = 0;
		call->unmarshall++;

		/* extract the callback array and its count in two steps */
	case 3:
		_debug("extract CB count");
229
		ret = afs_extract_data(call, &call->tmp, 4, true);
230 231
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
232

233 234 235
		call->count2 = ntohl(call->tmp);
		_debug("CB count: %u", call->count2);
		if (call->count2 != call->count && call->count2 != 0)
D
David Howells 已提交
236
			return afs_protocol_error(call, -EBADMSG);
237 238 239 240 241
		call->offset = 0;
		call->unmarshall++;

	case 4:
		_debug("extract CB array");
242
		ret = afs_extract_data(call, call->buffer,
243
				       call->count2 * 3 * 4, false);
244 245
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
246

247 248 249
		_debug("unmarshall CB array");
		cb = call->request;
		bp = call->buffer;
250
		for (loop = call->count2; loop > 0; loop--, cb++) {
251 252 253
			cb->cb.version	= ntohl(*bp++);
			cb->cb.expiry	= ntohl(*bp++);
			cb->cb.type	= ntohl(*bp++);
254
		}
L
Linus Torvalds 已提交
255

256 257
		call->offset = 0;
		call->unmarshall++;
258
	case 5:
L
Linus Torvalds 已提交
259 260 261
		break;
	}

262 263
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
L
Linus Torvalds 已提交
264

265 266
	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
267
	rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
268 269
	call->cm_server = afs_find_server(call->net, &srx);
	if (!call->cm_server)
270
		trace_afs_cm_no_server(call, &srx);
271

272
	return afs_queue_call_work(call);
D
David Howells 已提交
273
}
L
Linus Torvalds 已提交
274 275

/*
276
 * allow the fileserver to request callback state (re-)initialisation
L
Linus Torvalds 已提交
277
 */
278
static void SRXAFSCB_InitCallBackState(struct work_struct *work)
L
Linus Torvalds 已提交
279
{
280
	struct afs_call *call = container_of(work, struct afs_call, work);
L
Linus Torvalds 已提交
281

282
	_enter("{%p}", call->cm_server);
L
Linus Torvalds 已提交
283

284 285
	if (call->cm_server)
		afs_init_callback_state(call->cm_server);
286
	afs_send_empty_reply(call);
287
	afs_put_call(call);
288
	_leave("");
D
David Howells 已提交
289
}
L
Linus Torvalds 已提交
290 291

/*
292
 * deliver request data to a CB.InitCallBackState call
L
Linus Torvalds 已提交
293
 */
294
static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
L
Linus Torvalds 已提交
295
{
296
	struct sockaddr_rxrpc srx;
297
	int ret;
L
Linus Torvalds 已提交
298

299
	_enter("");
L
Linus Torvalds 已提交
300

301
	rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
302

303
	ret = afs_extract_data(call, NULL, 0, false);
304 305
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
306

307 308
	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
309 310
	call->cm_server = afs_find_server(call->net, &srx);
	if (!call->cm_server)
311
		trace_afs_cm_no_server(call, &srx);
L
Linus Torvalds 已提交
312

313
	return afs_queue_call_work(call);
314
}
L
Linus Torvalds 已提交
315

316 317 318
/*
 * deliver request data to a CB.InitCallBackState3 call
 */
319
static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
320
{
321
	struct afs_uuid *r;
322 323 324
	unsigned loop;
	__be32 *b;
	int ret;
325

326
	_enter("");
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	_enter("{%u}", call->unmarshall);

	switch (call->unmarshall) {
	case 0:
		call->offset = 0;
		call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL);
		if (!call->buffer)
			return -ENOMEM;
		call->unmarshall++;

	case 1:
		_debug("extract UUID");
		ret = afs_extract_data(call, call->buffer,
				       11 * sizeof(__be32), false);
		switch (ret) {
		case 0:		break;
		case -EAGAIN:	return 0;
		default:	return ret;
		}

		_debug("unmarshall UUID");
349
		call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
350 351 352 353 354
		if (!call->request)
			return -ENOMEM;

		b = call->buffer;
		r = call->request;
355 356 357
		r->time_low			= b[0];
		r->time_mid			= htons(ntohl(b[1]));
		r->time_hi_and_version		= htons(ntohl(b[2]));
358 359 360 361 362 363 364 365 366 367 368 369
		r->clock_seq_hi_and_reserved 	= ntohl(b[3]);
		r->clock_seq_low		= ntohl(b[4]);

		for (loop = 0; loop < 6; loop++)
			r->node[loop] = ntohl(b[loop + 5]);

		call->offset = 0;
		call->unmarshall++;

	case 2:
		break;
	}
370

371 372
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
373 374 375

	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
376
	rcu_read_lock();
377
	call->cm_server = afs_find_server_by_uuid(call->net, call->request);
378
	rcu_read_unlock();
379
	if (!call->cm_server)
380
		trace_afs_cm_no_server_u(call, call->request);
381

382
	return afs_queue_call_work(call);
383 384
}

385 386 387 388 389 390
/*
 * allow the fileserver to see if the cache manager is still alive
 */
static void SRXAFSCB_Probe(struct work_struct *work)
{
	struct afs_call *call = container_of(work, struct afs_call, work);
L
Linus Torvalds 已提交
391

392 393
	_enter("");
	afs_send_empty_reply(call);
394
	afs_put_call(call);
395 396
	_leave("");
}
L
Linus Torvalds 已提交
397

398 399 400
/*
 * deliver request data to a CB.Probe call
 */
401
static int afs_deliver_cb_probe(struct afs_call *call)
402
{
403 404
	int ret;

405
	_enter("");
L
Linus Torvalds 已提交
406

407
	ret = afs_extract_data(call, NULL, 0, false);
408 409
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
410

411 412
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
L
Linus Torvalds 已提交
413

414
	return afs_queue_call_work(call);
D
David Howells 已提交
415
}
416

417 418 419 420 421 422
/*
 * allow the fileserver to quickly find out if the fileserver has been rebooted
 */
static void SRXAFSCB_ProbeUuid(struct work_struct *work)
{
	struct afs_call *call = container_of(work, struct afs_call, work);
423
	struct afs_uuid *r = call->request;
424 425 426 427 428 429 430

	struct {
		__be32	match;
	} reply;

	_enter("");

431
	if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
432 433 434 435 436
		reply.match = htonl(0);
	else
		reply.match = htonl(1);

	afs_send_simple_reply(call, &reply, sizeof(reply));
437
	afs_put_call(call);
438 439 440 441 442 443
	_leave("");
}

/*
 * deliver request data to a CB.ProbeUuid call
 */
444
static int afs_deliver_cb_probe_uuid(struct afs_call *call)
445
{
446
	struct afs_uuid *r;
447 448 449 450
	unsigned loop;
	__be32 *b;
	int ret;

451
	_enter("{%u}", call->unmarshall);
452 453 454 455 456 457 458 459 460 461 462

	switch (call->unmarshall) {
	case 0:
		call->offset = 0;
		call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL);
		if (!call->buffer)
			return -ENOMEM;
		call->unmarshall++;

	case 1:
		_debug("extract UUID");
463 464
		ret = afs_extract_data(call, call->buffer,
				       11 * sizeof(__be32), false);
465 466 467 468 469 470 471
		switch (ret) {
		case 0:		break;
		case -EAGAIN:	return 0;
		default:	return ret;
		}

		_debug("unmarshall UUID");
472
		call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
473 474 475 476 477
		if (!call->request)
			return -ENOMEM;

		b = call->buffer;
		r = call->request;
D
David Howells 已提交
478 479 480
		r->time_low			= b[0];
		r->time_mid			= htons(ntohl(b[1]));
		r->time_hi_and_version		= htons(ntohl(b[2]));
481 482 483 484 485 486 487 488 489 490 491 492 493
		r->clock_seq_hi_and_reserved 	= ntohl(b[3]);
		r->clock_seq_low		= ntohl(b[4]);

		for (loop = 0; loop < 6; loop++)
			r->node[loop] = ntohl(b[loop + 5]);

		call->offset = 0;
		call->unmarshall++;

	case 2:
		break;
	}

494 495
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
496

497
	return afs_queue_call_work(call);
498 499
}

500 501 502
/*
 * allow the fileserver to ask about the cache manager's capabilities
 */
503
static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
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
{
	struct afs_interface *ifs;
	struct afs_call *call = container_of(work, struct afs_call, work);
	int loop, nifs;

	struct {
		struct /* InterfaceAddr */ {
			__be32 nifs;
			__be32 uuid[11];
			__be32 ifaddr[32];
			__be32 netmask[32];
			__be32 mtu[32];
		} ia;
		struct /* Capabilities */ {
			__be32 capcount;
			__be32 caps[1];
		} cap;
	} reply;

	_enter("");

	nifs = 0;
	ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
	if (ifs) {
		nifs = afs_get_ipv4_interfaces(ifs, 32, false);
		if (nifs < 0) {
			kfree(ifs);
			ifs = NULL;
			nifs = 0;
		}
	}

	memset(&reply, 0, sizeof(reply));
	reply.ia.nifs = htonl(nifs);

539 540 541 542 543
	reply.ia.uuid[0] = call->net->uuid.time_low;
	reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
	reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
	reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
	reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
544
	for (loop = 0; loop < 6; loop++)
545
		reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
546 547 548 549 550 551 552

	if (ifs) {
		for (loop = 0; loop < nifs; loop++) {
			reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
			reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
			reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
		}
553
		kfree(ifs);
554 555 556 557 558
	}

	reply.cap.capcount = htonl(1);
	reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
	afs_send_simple_reply(call, &reply, sizeof(reply));
559
	afs_put_call(call);
560 561 562 563
	_leave("");
}

/*
564
 * deliver request data to a CB.TellMeAboutYourself call
565
 */
566
static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
567
{
568 569
	int ret;

570
	_enter("");
571

572
	ret = afs_extract_data(call, NULL, 0, false);
573 574
	if (ret < 0)
		return ret;
575

576 577
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
578

579
	return afs_queue_call_work(call);
580
}