cmservice.c 14.3 KB
Newer Older
1
/* AFS Cache Manager Service
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
15
#include <linux/sched.h>
16
#include <linux/ip.h>
Linus Torvalds's avatar
Linus Torvalds committed
17
#include "internal.h"
18
#include "afs_cm.h"
Linus Torvalds's avatar
Linus Torvalds committed
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 *);
Linus Torvalds's avatar
Linus Torvalds committed
32

David Howells's avatar
David Howells committed
33 34 35 36
#define CM_NAME(name) \
	const char afs_SRXCB##name##_name[] __tracepoint_string =	\
		"CB." #name

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

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

59 60 61
/*
 * CB.InitCallBackState3 operation type
 */
David Howells's avatar
David Howells committed
62
static CM_NAME(InitCallBackState3);
63
static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
David Howells's avatar
David Howells committed
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
};

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

81 82 83
/*
 * CB.ProbeUuid operation type
 */
David Howells's avatar
David Howells committed
84
static CM_NAME(ProbeUuid);
85
static const struct afs_call_type afs_SRXCBProbeUuid = {
David Howells's avatar
David Howells committed
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
 */
David Howells's avatar
David Howells committed
95
static CM_NAME(TellMeAboutYourself);
96
static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
David Howells's avatar
David Howells committed
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
};

Linus Torvalds's avatar
Linus Torvalds committed
103
/*
104 105
 * route an incoming cache manager call
 * - return T if supported, F if not
Linus Torvalds's avatar
Linus Torvalds committed
106
 */
107
bool afs_cm_incoming_call(struct afs_call *call)
Linus Torvalds's avatar
Linus Torvalds committed
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;
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;
Linus Torvalds's avatar
Linus Torvalds committed
132
	}
133
}
Linus Torvalds's avatar
Linus Torvalds committed
134 135

/*
136
 * clean up a cache manager call
Linus Torvalds's avatar
Linus Torvalds committed
137
 */
138
static void afs_cm_destructor(struct afs_call *call)
Linus Torvalds's avatar
Linus Torvalds committed
139
{
140 141
	_enter("");

142 143 144 145
	/* 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().
	 */
146
	if (call->unmarshall == 5) {
147 148
		ASSERT(call->cm_server && call->count && call->request);
		afs_break_callbacks(call->cm_server, call->count, call->request);
149 150
	}

151 152
	kfree(call->buffer);
	call->buffer = NULL;
153
}
Linus Torvalds's avatar
Linus Torvalds committed
154 155

/*
156
 * The server supplied a list of callbacks that it wanted to break.
Linus Torvalds's avatar
Linus Torvalds committed
157
 */
158
static void SRXAFSCB_CallBack(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
159
{
160
	struct afs_call *call = container_of(work, struct afs_call, work);
Linus Torvalds's avatar
Linus Torvalds committed
161

162
	_enter("");
Linus Torvalds's avatar
Linus Torvalds committed
163

164 165 166 167 168 169
	/* 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);
Linus Torvalds's avatar
Linus Torvalds committed
170

171
	afs_break_callbacks(call->cm_server, call->count, call->request);
172
	afs_put_call(call);
173
	_leave("");
174
}
Linus Torvalds's avatar
Linus Torvalds committed
175 176

/*
177
 * deliver request data to a CB.CallBack call
Linus Torvalds's avatar
Linus Torvalds committed
178
 */
179
static int afs_deliver_cb_callback(struct afs_call *call)
Linus Torvalds's avatar
Linus Torvalds committed
180
{
181
	struct sockaddr_rxrpc srx;
182 183 184 185 186
	struct afs_callback *cb;
	struct afs_server *server;
	__be32 *bp;
	int ret, loop;

187
	_enter("{%u}", call->unmarshall);
188 189 190 191 192 193 194 195 196

	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");
197
		ret = afs_extract_data(call, &call->tmp, 4, true);
198 199
		if (ret < 0)
			return ret;
Linus Torvalds's avatar
Linus Torvalds committed
200

201 202 203 204 205 206 207 208 209 210 211 212 213
		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");
214 215
		ret = afs_extract_data(call, call->buffer,
				       call->count * 3 * 4, true);
216 217
		if (ret < 0)
			return ret;
Linus Torvalds's avatar
Linus Torvalds committed
218

219 220 221 222 223 224 225 226 227 228 229 230 231 232
		_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;
Linus Torvalds's avatar
Linus Torvalds committed
233 234
		}

235 236 237 238 239 240
		call->offset = 0;
		call->unmarshall++;

		/* extract the callback array and its count in two steps */
	case 3:
		_debug("extract CB count");
241
		ret = afs_extract_data(call, &call->tmp, 4, true);
242 243
		if (ret < 0)
			return ret;
Linus Torvalds's avatar
Linus Torvalds committed
244

245 246 247
		call->count2 = ntohl(call->tmp);
		_debug("CB count: %u", call->count2);
		if (call->count2 != call->count && call->count2 != 0)
248 249 250 251 252 253
			return -EBADMSG;
		call->offset = 0;
		call->unmarshall++;

	case 4:
		_debug("extract CB array");
254
		ret = afs_extract_data(call, call->buffer,
255
				       call->count2 * 3 * 4, false);
256 257
		if (ret < 0)
			return ret;
Linus Torvalds's avatar
Linus Torvalds committed
258

259 260 261
		_debug("unmarshall CB array");
		cb = call->request;
		bp = call->buffer;
262
		for (loop = call->count2; loop > 0; loop--, cb++) {
263 264 265 266
			cb->version	= ntohl(*bp++);
			cb->expiry	= ntohl(*bp++);
			cb->type	= ntohl(*bp++);
		}
Linus Torvalds's avatar
Linus Torvalds committed
267

268 269
		call->offset = 0;
		call->unmarshall++;
Linus Torvalds's avatar
Linus Torvalds committed
270

271 272 273 274 275 276 277 278
		/* 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++;
279
	case 5:
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282
		break;
	}

283 284
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
285

286 287
	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
288
	rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
289
	server = afs_find_server(call->net, &srx);
290 291
	if (!server)
		return -ENOTCONN;
292
	call->cm_server = server;
293

294
	return afs_queue_call_work(call);
295
}
Linus Torvalds's avatar
Linus Torvalds committed
296 297

/*
298
 * allow the fileserver to request callback state (re-)initialisation
Linus Torvalds's avatar
Linus Torvalds committed
299
 */
300
static void SRXAFSCB_InitCallBackState(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
301
{
302
	struct afs_call *call = container_of(work, struct afs_call, work);
Linus Torvalds's avatar
Linus Torvalds committed
303

304
	_enter("{%p}", call->cm_server);
Linus Torvalds's avatar
Linus Torvalds committed
305

306
	afs_init_callback_state(call->cm_server);
307
	afs_send_empty_reply(call);
308
	afs_put_call(call);
309
	_leave("");
310
}
Linus Torvalds's avatar
Linus Torvalds committed
311 312

/*
313
 * deliver request data to a CB.InitCallBackState call
Linus Torvalds's avatar
Linus Torvalds committed
314
 */
315
static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
Linus Torvalds's avatar
Linus Torvalds committed
316
{
317
	struct sockaddr_rxrpc srx;
Linus Torvalds's avatar
Linus Torvalds committed
318
	struct afs_server *server;
319
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
320

321
	_enter("");
Linus Torvalds's avatar
Linus Torvalds committed
322

323
	rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
324

325
	ret = afs_extract_data(call, NULL, 0, false);
326 327
	if (ret < 0)
		return ret;
Linus Torvalds's avatar
Linus Torvalds committed
328

329 330
	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
331
	server = afs_find_server(call->net, &srx);
332 333
	if (!server)
		return -ENOTCONN;
334
	call->cm_server = server;
Linus Torvalds's avatar
Linus Torvalds committed
335

336
	return afs_queue_call_work(call);
337
}
Linus Torvalds's avatar
Linus Torvalds committed
338

339 340 341
/*
 * deliver request data to a CB.InitCallBackState3 call
 */
342
static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
343
{
344
	struct sockaddr_rxrpc srx;
345
	struct afs_server *server;
346
	struct afs_uuid *r;
347 348 349
	unsigned loop;
	__be32 *b;
	int ret;
350

351
	_enter("");
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	_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");
374
		call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
375 376 377 378 379
		if (!call->request)
			return -ENOMEM;

		b = call->buffer;
		r = call->request;
380 381 382
		r->time_low			= b[0];
		r->time_mid			= htons(ntohl(b[1]));
		r->time_hi_and_version		= htons(ntohl(b[2]));
383 384 385 386 387 388 389 390 391 392 393 394
		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;
	}
395

396 397
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
398 399 400

	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
401
	rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
402
	server = afs_find_server(call->net, &srx);
403 404
	if (!server)
		return -ENOTCONN;
405
	call->cm_server = server;
406

407
	return afs_queue_call_work(call);
408 409
}

410 411 412 413 414 415
/*
 * 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);
Linus Torvalds's avatar
Linus Torvalds committed
416

417 418
	_enter("");
	afs_send_empty_reply(call);
419
	afs_put_call(call);
420 421
	_leave("");
}
Linus Torvalds's avatar
Linus Torvalds committed
422

423 424 425
/*
 * deliver request data to a CB.Probe call
 */
426
static int afs_deliver_cb_probe(struct afs_call *call)
427
{
428 429
	int ret;

430
	_enter("");
Linus Torvalds's avatar
Linus Torvalds committed
431

432
	ret = afs_extract_data(call, NULL, 0, false);
433 434
	if (ret < 0)
		return ret;
Linus Torvalds's avatar
Linus Torvalds committed
435

436 437
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
438

439
	return afs_queue_call_work(call);
440
}
441

442 443 444 445 446 447
/*
 * 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);
448
	struct afs_uuid *r = call->request;
449 450 451 452 453 454 455

	struct {
		__be32	match;
	} reply;

	_enter("");

456
	if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
457 458 459 460 461
		reply.match = htonl(0);
	else
		reply.match = htonl(1);

	afs_send_simple_reply(call, &reply, sizeof(reply));
462
	afs_put_call(call);
463 464 465 466 467 468
	_leave("");
}

/*
 * deliver request data to a CB.ProbeUuid call
 */
469
static int afs_deliver_cb_probe_uuid(struct afs_call *call)
470
{
471
	struct afs_uuid *r;
472 473 474 475
	unsigned loop;
	__be32 *b;
	int ret;

476
	_enter("{%u}", call->unmarshall);
477 478 479 480 481 482 483 484 485 486 487

	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");
488 489
		ret = afs_extract_data(call, call->buffer,
				       11 * sizeof(__be32), false);
490 491 492 493 494 495 496
		switch (ret) {
		case 0:		break;
		case -EAGAIN:	return 0;
		default:	return ret;
		}

		_debug("unmarshall UUID");
497
		call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
498 499 500 501 502
		if (!call->request)
			return -ENOMEM;

		b = call->buffer;
		r = call->request;
David Howells's avatar
David Howells committed
503 504 505
		r->time_low			= b[0];
		r->time_mid			= htons(ntohl(b[1]));
		r->time_hi_and_version		= htons(ntohl(b[2]));
506 507 508 509 510 511 512 513 514 515 516 517 518
		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;
	}

519 520
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
521

522
	return afs_queue_call_work(call);
523 524
}

525 526 527
/*
 * allow the fileserver to ask about the cache manager's capabilities
 */
528
static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
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
{
	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);

564 565 566 567 568
	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);
569
	for (loop = 0; loop < 6; loop++)
570
		reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
571 572 573 574 575 576 577

	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);
		}
578
		kfree(ifs);
579 580 581 582 583
	}

	reply.cap.capcount = htonl(1);
	reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
	afs_send_simple_reply(call, &reply, sizeof(reply));
584
	afs_put_call(call);
585 586 587 588
	_leave("");
}

/*
589
 * deliver request data to a CB.TellMeAboutYourself call
590
 */
591
static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
592
{
593 594
	int ret;

595
	_enter("");
596

597
	ret = afs_extract_data(call, NULL, 0, false);
598 599
	if (ret < 0)
		return ret;
600

601 602
	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
		return -EIO;
603

604
	return afs_queue_call_work(call);
605
}