cmservice.c 14.3 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 45
	.deliver	= afs_deliver_cb_callback,
	.abort_to_error	= afs_abort_to_error,
	.destructor	= afs_cm_destructor,
46
	.work		= SRXAFSCB_CallBack,
47
};
L
Linus Torvalds 已提交
48 49

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

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

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

85 86 87
/*
 * CB.ProbeUuid operation type
 */
D
David Howells 已提交
88
static CM_NAME(ProbeUuid);
89
static const struct afs_call_type afs_SRXCBProbeUuid = {
D
David Howells 已提交
90
	.name		= afs_SRXCBProbeUuid_name,
91 92 93
	.deliver	= afs_deliver_cb_probe_uuid,
	.abort_to_error	= afs_abort_to_error,
	.destructor	= afs_cm_destructor,
94
	.work		= SRXAFSCB_ProbeUuid,
95 96
};

97
/*
98
 * CB.TellMeAboutYourself operation type
99
 */
D
David Howells 已提交
100
static CM_NAME(TellMeAboutYourself);
101
static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
D
David Howells 已提交
102
	.name		= afs_SRXCBTellMeAboutYourself_name,
103
	.deliver	= afs_deliver_cb_tell_me_about_yourself,
104 105
	.abort_to_error	= afs_abort_to_error,
	.destructor	= afs_cm_destructor,
106
	.work		= SRXAFSCB_TellMeAboutYourself,
107 108
};

L
Linus Torvalds 已提交
109
/*
110 111
 * route an incoming cache manager call
 * - return T if supported, F if not
L
Linus Torvalds 已提交
112
 */
113
bool afs_cm_incoming_call(struct afs_call *call)
L
Linus Torvalds 已提交
114
{
115
	_enter("{CB.OP %u}", call->operation_ID);
116

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

/*
139
 * clean up a cache manager call
L
Linus Torvalds 已提交
140
 */
141
static void afs_cm_destructor(struct afs_call *call)
L
Linus Torvalds 已提交
142
{
143 144
	_enter("");

145 146 147 148
	/* Break the callbacks here so that we do it after the final ACK is
	 * received.  The step number here must match the final number in
	 * afs_deliver_cb_callback().
	 */
149
	if (call->unmarshall == 5) {
150 151 152 153
		ASSERT(call->server && call->count && call->request);
		afs_break_callbacks(call->server, call->count, call->request);
	}

154 155 156 157
	afs_put_server(call->server);
	call->server = NULL;
	kfree(call->buffer);
	call->buffer = NULL;
D
David Howells 已提交
158
}
L
Linus Torvalds 已提交
159 160

/*
161
 * allow the fileserver to see if the cache manager is still alive
L
Linus Torvalds 已提交
162
 */
163
static void SRXAFSCB_CallBack(struct work_struct *work)
L
Linus Torvalds 已提交
164
{
165
	struct afs_call *call = container_of(work, struct afs_call, work);
L
Linus Torvalds 已提交
166

167
	_enter("");
L
Linus Torvalds 已提交
168

169 170 171 172 173 174
	/* be sure to send the reply *before* attempting to spam the AFS server
	 * with FSFetchStatus requests on the vnodes with broken callbacks lest
	 * the AFS server get into a vicious cycle of trying to break further
	 * callbacks because it hadn't received completion of the CBCallBack op
	 * yet */
	afs_send_empty_reply(call);
L
Linus Torvalds 已提交
175

176
	afs_break_callbacks(call->server, call->count, call->request);
177
	afs_put_call(call);
178
	_leave("");
D
David Howells 已提交
179
}
L
Linus Torvalds 已提交
180 181

/*
182
 * deliver request data to a CB.CallBack call
L
Linus Torvalds 已提交
183
 */
184
static int afs_deliver_cb_callback(struct afs_call *call)
L
Linus Torvalds 已提交
185
{
186
	struct sockaddr_rxrpc srx;
187 188 189 190 191 192
	struct afs_callback *cb;
	struct afs_server *server;
	__be32 *bp;
	u32 tmp;
	int ret, loop;

193
	_enter("{%u}", call->unmarshall);
194 195 196

	switch (call->unmarshall) {
	case 0:
197
		rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx);
198 199 200 201 202 203
		call->offset = 0;
		call->unmarshall++;

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

208 209 210 211 212 213 214 215 216 217 218 219 220
		call->count = ntohl(call->tmp);
		_debug("FID count: %u", call->count);
		if (call->count > AFSCBMAX)
			return -EBADMSG;

		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");
221 222
		ret = afs_extract_data(call, call->buffer,
				       call->count * 3 * 4, true);
223 224
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239
		_debug("unmarshall FID array");
		call->request = kcalloc(call->count,
					sizeof(struct afs_callback),
					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++);
			cb->type	= AFSCM_CB_UNTYPED;
L
Linus Torvalds 已提交
240 241
		}

242 243 244 245 246 247
		call->offset = 0;
		call->unmarshall++;

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

252 253 254 255 256 257 258 259 260
		tmp = ntohl(call->tmp);
		_debug("CB count: %u", tmp);
		if (tmp != call->count && tmp != 0)
			return -EBADMSG;
		call->offset = 0;
		call->unmarshall++;

	case 4:
		_debug("extract CB array");
261 262
		ret = afs_extract_data(call, call->buffer,
				       call->count * 3 * 4, false);
263 264
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
265

266 267 268 269 270 271 272 273
		_debug("unmarshall CB array");
		cb = call->request;
		bp = call->buffer;
		for (loop = call->count; loop > 0; loop--, cb++) {
			cb->version	= ntohl(*bp++);
			cb->expiry	= ntohl(*bp++);
			cb->type	= ntohl(*bp++);
		}
L
Linus Torvalds 已提交
274

275 276
		call->offset = 0;
		call->unmarshall++;
L
Linus Torvalds 已提交
277

278 279 280 281 282 283 284 285
		/* Record that the message was unmarshalled successfully so
		 * that the call destructor can know do the callback breaking
		 * work, even if the final ACK isn't received.
		 *
		 * If the step number changes, then afs_cm_destructor() must be
		 * updated also.
		 */
		call->unmarshall++;
286
	case 5:
L
Linus Torvalds 已提交
287 288 289
		break;
	}

290
	call->state = AFS_CALL_REPLYING;
L
Linus Torvalds 已提交
291

292 293
	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
294
	server = afs_find_server(&srx);
295 296 297 298
	if (!server)
		return -ENOTCONN;
	call->server = server;

299
	return afs_queue_call_work(call);
D
David Howells 已提交
300
}
L
Linus Torvalds 已提交
301 302

/*
303
 * allow the fileserver to request callback state (re-)initialisation
L
Linus Torvalds 已提交
304
 */
305
static void SRXAFSCB_InitCallBackState(struct work_struct *work)
L
Linus Torvalds 已提交
306
{
307
	struct afs_call *call = container_of(work, struct afs_call, work);
L
Linus Torvalds 已提交
308

309
	_enter("{%p}", call->server);
L
Linus Torvalds 已提交
310

311 312
	afs_init_callback_state(call->server);
	afs_send_empty_reply(call);
313
	afs_put_call(call);
314
	_leave("");
D
David Howells 已提交
315
}
L
Linus Torvalds 已提交
316 317

/*
318
 * deliver request data to a CB.InitCallBackState call
L
Linus Torvalds 已提交
319
 */
320
static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
L
Linus Torvalds 已提交
321
{
322
	struct sockaddr_rxrpc srx;
L
Linus Torvalds 已提交
323
	struct afs_server *server;
324
	int ret;
L
Linus Torvalds 已提交
325

326
	_enter("");
L
Linus Torvalds 已提交
327

328 329
	rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx);

330
	ret = afs_extract_data(call, NULL, 0, false);
331 332
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
333

334 335
	/* no unmarshalling required */
	call->state = AFS_CALL_REPLYING;
L
Linus Torvalds 已提交
336

337 338
	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
339
	server = afs_find_server(&srx);
340 341 342
	if (!server)
		return -ENOTCONN;
	call->server = server;
L
Linus Torvalds 已提交
343

344
	return afs_queue_call_work(call);
345
}
L
Linus Torvalds 已提交
346

347 348 349
/*
 * deliver request data to a CB.InitCallBackState3 call
 */
350
static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
351
{
352
	struct sockaddr_rxrpc srx;
353
	struct afs_server *server;
354
	struct uuid_v1 *r;
355 356 357
	unsigned loop;
	__be32 *b;
	int ret;
358

359
	_enter("");
360

361 362
	rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx);

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	_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");
384
		call->request = kmalloc(sizeof(struct uuid_v1), GFP_KERNEL);
385 386 387 388 389
		if (!call->request)
			return -ENOMEM;

		b = call->buffer;
		r = call->request;
390 391 392
		r->time_low			= b[0];
		r->time_mid			= htons(ntohl(b[1]));
		r->time_hi_and_version		= htons(ntohl(b[2]));
393 394 395 396 397 398 399 400 401 402 403 404
		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;
	}
405 406 407 408 409 410

	/* no unmarshalling required */
	call->state = AFS_CALL_REPLYING;

	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
411
	server = afs_find_server(&srx);
412 413 414 415
	if (!server)
		return -ENOTCONN;
	call->server = server;

416
	return afs_queue_call_work(call);
417 418
}

419 420 421 422 423 424
/*
 * 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 已提交
425

426 427
	_enter("");
	afs_send_empty_reply(call);
428
	afs_put_call(call);
429 430
	_leave("");
}
L
Linus Torvalds 已提交
431

432 433 434
/*
 * deliver request data to a CB.Probe call
 */
435
static int afs_deliver_cb_probe(struct afs_call *call)
436
{
437 438
	int ret;

439
	_enter("");
L
Linus Torvalds 已提交
440

441
	ret = afs_extract_data(call, NULL, 0, false);
442 443
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
444

445 446
	/* no unmarshalling required */
	call->state = AFS_CALL_REPLYING;
L
Linus Torvalds 已提交
447

448
	return afs_queue_call_work(call);
D
David Howells 已提交
449
}
450

451 452 453 454 455 456
/*
 * 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);
457
	struct uuid_v1 *r = call->request;
458 459 460 461 462 463 464 465 466 467 468 469 470

	struct {
		__be32	match;
	} reply;

	_enter("");

	if (memcmp(r, &afs_uuid, sizeof(afs_uuid)) == 0)
		reply.match = htonl(0);
	else
		reply.match = htonl(1);

	afs_send_simple_reply(call, &reply, sizeof(reply));
471
	afs_put_call(call);
472 473 474 475 476 477
	_leave("");
}

/*
 * deliver request data to a CB.ProbeUuid call
 */
478
static int afs_deliver_cb_probe_uuid(struct afs_call *call)
479
{
480
	struct uuid_v1 *r;
481 482 483 484
	unsigned loop;
	__be32 *b;
	int ret;

485
	_enter("{%u}", call->unmarshall);
486 487 488 489 490 491 492 493 494 495 496

	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");
497 498
		ret = afs_extract_data(call, call->buffer,
				       11 * sizeof(__be32), false);
499 500 501 502 503 504 505
		switch (ret) {
		case 0:		break;
		case -EAGAIN:	return 0;
		default:	return ret;
		}

		_debug("unmarshall UUID");
506
		call->request = kmalloc(sizeof(struct uuid_v1), GFP_KERNEL);
507 508 509 510 511
		if (!call->request)
			return -ENOMEM;

		b = call->buffer;
		r = call->request;
512 513 514
		r->time_low			= b[0];
		r->time_mid			= htons(ntohl(b[1]));
		r->time_hi_and_version		= htons(ntohl(b[2]));
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
		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;
	}

	call->state = AFS_CALL_REPLYING;

530
	return afs_queue_call_work(call);
531 532
}

533 534 535
/*
 * allow the fileserver to ask about the cache manager's capabilities
 */
536
static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
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 569 570 571
{
	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);

572 573 574
	reply.ia.uuid[0] = afs_uuid.time_low;
	reply.ia.uuid[1] = htonl(ntohs(afs_uuid.time_mid));
	reply.ia.uuid[2] = htonl(ntohs(afs_uuid.time_hi_and_version));
575 576 577 578 579 580 581 582 583 584 585
	reply.ia.uuid[3] = htonl((s8) afs_uuid.clock_seq_hi_and_reserved);
	reply.ia.uuid[4] = htonl((s8) afs_uuid.clock_seq_low);
	for (loop = 0; loop < 6; loop++)
		reply.ia.uuid[loop + 5] = htonl((s8) afs_uuid.node[loop]);

	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);
		}
586
		kfree(ifs);
587 588 589 590 591
	}

	reply.cap.capcount = htonl(1);
	reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
	afs_send_simple_reply(call, &reply, sizeof(reply));
592
	afs_put_call(call);
593 594 595 596
	_leave("");
}

/*
597
 * deliver request data to a CB.TellMeAboutYourself call
598
 */
599
static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
600
{
601 602
	int ret;

603
	_enter("");
604

605
	ret = afs_extract_data(call, NULL, 0, false);
606 607
	if (ret < 0)
		return ret;
608 609 610 611

	/* no unmarshalling required */
	call->state = AFS_CALL_REPLYING;

612
	return afs_queue_call_work(call);
613
}