xref: /openbmc/linux/fs/afs/cmservice.c (revision 4e95bc26)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS Cache Manager Service
3  *
4  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include <linux/ip.h>
13 #include "internal.h"
14 #include "afs_cm.h"
15 #include "protocol_yfs.h"
16 
17 static int afs_deliver_cb_init_call_back_state(struct afs_call *);
18 static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
19 static int afs_deliver_cb_probe(struct afs_call *);
20 static int afs_deliver_cb_callback(struct afs_call *);
21 static int afs_deliver_cb_probe_uuid(struct afs_call *);
22 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
23 static void afs_cm_destructor(struct afs_call *);
24 static void SRXAFSCB_CallBack(struct work_struct *);
25 static void SRXAFSCB_InitCallBackState(struct work_struct *);
26 static void SRXAFSCB_Probe(struct work_struct *);
27 static void SRXAFSCB_ProbeUuid(struct work_struct *);
28 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
29 
30 static int afs_deliver_yfs_cb_callback(struct afs_call *);
31 
32 #define CM_NAME(name) \
33 	char afs_SRXCB##name##_name[] __tracepoint_string =	\
34 		"CB." #name
35 
36 /*
37  * CB.CallBack operation type
38  */
39 static CM_NAME(CallBack);
40 static const struct afs_call_type afs_SRXCBCallBack = {
41 	.name		= afs_SRXCBCallBack_name,
42 	.deliver	= afs_deliver_cb_callback,
43 	.destructor	= afs_cm_destructor,
44 	.work		= SRXAFSCB_CallBack,
45 };
46 
47 /*
48  * CB.InitCallBackState operation type
49  */
50 static CM_NAME(InitCallBackState);
51 static const struct afs_call_type afs_SRXCBInitCallBackState = {
52 	.name		= afs_SRXCBInitCallBackState_name,
53 	.deliver	= afs_deliver_cb_init_call_back_state,
54 	.destructor	= afs_cm_destructor,
55 	.work		= SRXAFSCB_InitCallBackState,
56 };
57 
58 /*
59  * CB.InitCallBackState3 operation type
60  */
61 static CM_NAME(InitCallBackState3);
62 static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
63 	.name		= afs_SRXCBInitCallBackState3_name,
64 	.deliver	= afs_deliver_cb_init_call_back_state3,
65 	.destructor	= afs_cm_destructor,
66 	.work		= SRXAFSCB_InitCallBackState,
67 };
68 
69 /*
70  * CB.Probe operation type
71  */
72 static CM_NAME(Probe);
73 static const struct afs_call_type afs_SRXCBProbe = {
74 	.name		= afs_SRXCBProbe_name,
75 	.deliver	= afs_deliver_cb_probe,
76 	.destructor	= afs_cm_destructor,
77 	.work		= SRXAFSCB_Probe,
78 };
79 
80 /*
81  * CB.ProbeUuid operation type
82  */
83 static CM_NAME(ProbeUuid);
84 static const struct afs_call_type afs_SRXCBProbeUuid = {
85 	.name		= afs_SRXCBProbeUuid_name,
86 	.deliver	= afs_deliver_cb_probe_uuid,
87 	.destructor	= afs_cm_destructor,
88 	.work		= SRXAFSCB_ProbeUuid,
89 };
90 
91 /*
92  * CB.TellMeAboutYourself operation type
93  */
94 static CM_NAME(TellMeAboutYourself);
95 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
96 	.name		= afs_SRXCBTellMeAboutYourself_name,
97 	.deliver	= afs_deliver_cb_tell_me_about_yourself,
98 	.destructor	= afs_cm_destructor,
99 	.work		= SRXAFSCB_TellMeAboutYourself,
100 };
101 
102 /*
103  * YFS CB.CallBack operation type
104  */
105 static CM_NAME(YFS_CallBack);
106 static const struct afs_call_type afs_SRXYFSCB_CallBack = {
107 	.name		= afs_SRXCBYFS_CallBack_name,
108 	.deliver	= afs_deliver_yfs_cb_callback,
109 	.destructor	= afs_cm_destructor,
110 	.work		= SRXAFSCB_CallBack,
111 };
112 
113 /*
114  * route an incoming cache manager call
115  * - return T if supported, F if not
116  */
117 bool afs_cm_incoming_call(struct afs_call *call)
118 {
119 	_enter("{%u, CB.OP %u}", call->service_id, call->operation_ID);
120 
121 	call->epoch = rxrpc_kernel_get_epoch(call->net->socket, call->rxcall);
122 
123 	switch (call->operation_ID) {
124 	case CBCallBack:
125 		call->type = &afs_SRXCBCallBack;
126 		return true;
127 	case CBInitCallBackState:
128 		call->type = &afs_SRXCBInitCallBackState;
129 		return true;
130 	case CBInitCallBackState3:
131 		call->type = &afs_SRXCBInitCallBackState3;
132 		return true;
133 	case CBProbe:
134 		call->type = &afs_SRXCBProbe;
135 		return true;
136 	case CBProbeUuid:
137 		call->type = &afs_SRXCBProbeUuid;
138 		return true;
139 	case CBTellMeAboutYourself:
140 		call->type = &afs_SRXCBTellMeAboutYourself;
141 		return true;
142 	case YFSCBCallBack:
143 		if (call->service_id != YFS_CM_SERVICE)
144 			return false;
145 		call->type = &afs_SRXYFSCB_CallBack;
146 		return true;
147 	default:
148 		return false;
149 	}
150 }
151 
152 /*
153  * Record a probe to the cache manager from a server.
154  */
155 static int afs_record_cm_probe(struct afs_call *call, struct afs_server *server)
156 {
157 	_enter("");
158 
159 	if (test_bit(AFS_SERVER_FL_HAVE_EPOCH, &server->flags) &&
160 	    !test_bit(AFS_SERVER_FL_PROBING, &server->flags)) {
161 		if (server->cm_epoch == call->epoch)
162 			return 0;
163 
164 		if (!server->probe.said_rebooted) {
165 			pr_notice("kAFS: FS rebooted %pU\n", &server->uuid);
166 			server->probe.said_rebooted = true;
167 		}
168 	}
169 
170 	spin_lock(&server->probe_lock);
171 
172 	if (!test_bit(AFS_SERVER_FL_HAVE_EPOCH, &server->flags)) {
173 		server->cm_epoch = call->epoch;
174 		server->probe.cm_epoch = call->epoch;
175 		goto out;
176 	}
177 
178 	if (server->probe.cm_probed &&
179 	    call->epoch != server->probe.cm_epoch &&
180 	    !server->probe.said_inconsistent) {
181 		pr_notice("kAFS: FS endpoints inconsistent %pU\n",
182 			  &server->uuid);
183 		server->probe.said_inconsistent = true;
184 	}
185 
186 	if (!server->probe.cm_probed || call->epoch == server->cm_epoch)
187 		server->probe.cm_epoch = server->cm_epoch;
188 
189 out:
190 	server->probe.cm_probed = true;
191 	spin_unlock(&server->probe_lock);
192 	return 0;
193 }
194 
195 /*
196  * Find the server record by peer address and record a probe to the cache
197  * manager from a server.
198  */
199 static int afs_find_cm_server_by_peer(struct afs_call *call)
200 {
201 	struct sockaddr_rxrpc srx;
202 	struct afs_server *server;
203 
204 	rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
205 
206 	server = afs_find_server(call->net, &srx);
207 	if (!server) {
208 		trace_afs_cm_no_server(call, &srx);
209 		return 0;
210 	}
211 
212 	call->server = server;
213 	return afs_record_cm_probe(call, server);
214 }
215 
216 /*
217  * Find the server record by server UUID and record a probe to the cache
218  * manager from a server.
219  */
220 static int afs_find_cm_server_by_uuid(struct afs_call *call,
221 				      struct afs_uuid *uuid)
222 {
223 	struct afs_server *server;
224 
225 	rcu_read_lock();
226 	server = afs_find_server_by_uuid(call->net, call->request);
227 	rcu_read_unlock();
228 	if (!server) {
229 		trace_afs_cm_no_server_u(call, call->request);
230 		return 0;
231 	}
232 
233 	call->server = server;
234 	return afs_record_cm_probe(call, server);
235 }
236 
237 /*
238  * Clean up a cache manager call.
239  */
240 static void afs_cm_destructor(struct afs_call *call)
241 {
242 	kfree(call->buffer);
243 	call->buffer = NULL;
244 }
245 
246 /*
247  * The server supplied a list of callbacks that it wanted to break.
248  */
249 static void SRXAFSCB_CallBack(struct work_struct *work)
250 {
251 	struct afs_call *call = container_of(work, struct afs_call, work);
252 
253 	_enter("");
254 
255 	/* We need to break the callbacks before sending the reply as the
256 	 * server holds up change visibility till it receives our reply so as
257 	 * to maintain cache coherency.
258 	 */
259 	if (call->server)
260 		afs_break_callbacks(call->server, call->count, call->request);
261 
262 	afs_send_empty_reply(call);
263 	afs_put_call(call);
264 	_leave("");
265 }
266 
267 /*
268  * deliver request data to a CB.CallBack call
269  */
270 static int afs_deliver_cb_callback(struct afs_call *call)
271 {
272 	struct afs_callback_break *cb;
273 	__be32 *bp;
274 	int ret, loop;
275 
276 	_enter("{%u}", call->unmarshall);
277 
278 	switch (call->unmarshall) {
279 	case 0:
280 		afs_extract_to_tmp(call);
281 		call->unmarshall++;
282 
283 		/* extract the FID array and its count in two steps */
284 		/* fall through */
285 	case 1:
286 		_debug("extract FID count");
287 		ret = afs_extract_data(call, true);
288 		if (ret < 0)
289 			return ret;
290 
291 		call->count = ntohl(call->tmp);
292 		_debug("FID count: %u", call->count);
293 		if (call->count > AFSCBMAX)
294 			return afs_protocol_error(call, -EBADMSG,
295 						  afs_eproto_cb_fid_count);
296 
297 		call->buffer = kmalloc(array3_size(call->count, 3, 4),
298 				       GFP_KERNEL);
299 		if (!call->buffer)
300 			return -ENOMEM;
301 		afs_extract_to_buf(call, call->count * 3 * 4);
302 		call->unmarshall++;
303 
304 		/* Fall through */
305 	case 2:
306 		_debug("extract FID array");
307 		ret = afs_extract_data(call, true);
308 		if (ret < 0)
309 			return ret;
310 
311 		_debug("unmarshall FID array");
312 		call->request = kcalloc(call->count,
313 					sizeof(struct afs_callback_break),
314 					GFP_KERNEL);
315 		if (!call->request)
316 			return -ENOMEM;
317 
318 		cb = call->request;
319 		bp = call->buffer;
320 		for (loop = call->count; loop > 0; loop--, cb++) {
321 			cb->fid.vid	= ntohl(*bp++);
322 			cb->fid.vnode	= ntohl(*bp++);
323 			cb->fid.unique	= ntohl(*bp++);
324 		}
325 
326 		afs_extract_to_tmp(call);
327 		call->unmarshall++;
328 
329 		/* extract the callback array and its count in two steps */
330 		/* fall through */
331 	case 3:
332 		_debug("extract CB count");
333 		ret = afs_extract_data(call, true);
334 		if (ret < 0)
335 			return ret;
336 
337 		call->count2 = ntohl(call->tmp);
338 		_debug("CB count: %u", call->count2);
339 		if (call->count2 != call->count && call->count2 != 0)
340 			return afs_protocol_error(call, -EBADMSG,
341 						  afs_eproto_cb_count);
342 		call->_iter = &call->iter;
343 		iov_iter_discard(&call->iter, READ, call->count2 * 3 * 4);
344 		call->unmarshall++;
345 
346 		/* Fall through */
347 	case 4:
348 		_debug("extract discard %zu/%u",
349 		       iov_iter_count(&call->iter), call->count2 * 3 * 4);
350 
351 		ret = afs_extract_data(call, false);
352 		if (ret < 0)
353 			return ret;
354 
355 		call->unmarshall++;
356 	case 5:
357 		break;
358 	}
359 
360 	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
361 		return afs_io_error(call, afs_io_error_cm_reply);
362 
363 	/* we'll need the file server record as that tells us which set of
364 	 * vnodes to operate upon */
365 	return afs_find_cm_server_by_peer(call);
366 }
367 
368 /*
369  * allow the fileserver to request callback state (re-)initialisation
370  */
371 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
372 {
373 	struct afs_call *call = container_of(work, struct afs_call, work);
374 
375 	_enter("{%p}", call->server);
376 
377 	if (call->server)
378 		afs_init_callback_state(call->server);
379 	afs_send_empty_reply(call);
380 	afs_put_call(call);
381 	_leave("");
382 }
383 
384 /*
385  * deliver request data to a CB.InitCallBackState call
386  */
387 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
388 {
389 	int ret;
390 
391 	_enter("");
392 
393 	afs_extract_discard(call, 0);
394 	ret = afs_extract_data(call, false);
395 	if (ret < 0)
396 		return ret;
397 
398 	/* we'll need the file server record as that tells us which set of
399 	 * vnodes to operate upon */
400 	return afs_find_cm_server_by_peer(call);
401 }
402 
403 /*
404  * deliver request data to a CB.InitCallBackState3 call
405  */
406 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
407 {
408 	struct afs_uuid *r;
409 	unsigned loop;
410 	__be32 *b;
411 	int ret;
412 
413 	_enter("");
414 
415 	_enter("{%u}", call->unmarshall);
416 
417 	switch (call->unmarshall) {
418 	case 0:
419 		call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
420 		if (!call->buffer)
421 			return -ENOMEM;
422 		afs_extract_to_buf(call, 11 * sizeof(__be32));
423 		call->unmarshall++;
424 
425 		/* Fall through */
426 	case 1:
427 		_debug("extract UUID");
428 		ret = afs_extract_data(call, false);
429 		switch (ret) {
430 		case 0:		break;
431 		case -EAGAIN:	return 0;
432 		default:	return ret;
433 		}
434 
435 		_debug("unmarshall UUID");
436 		call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
437 		if (!call->request)
438 			return -ENOMEM;
439 
440 		b = call->buffer;
441 		r = call->request;
442 		r->time_low			= b[0];
443 		r->time_mid			= htons(ntohl(b[1]));
444 		r->time_hi_and_version		= htons(ntohl(b[2]));
445 		r->clock_seq_hi_and_reserved 	= ntohl(b[3]);
446 		r->clock_seq_low		= ntohl(b[4]);
447 
448 		for (loop = 0; loop < 6; loop++)
449 			r->node[loop] = ntohl(b[loop + 5]);
450 
451 		call->unmarshall++;
452 
453 	case 2:
454 		break;
455 	}
456 
457 	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
458 		return afs_io_error(call, afs_io_error_cm_reply);
459 
460 	/* we'll need the file server record as that tells us which set of
461 	 * vnodes to operate upon */
462 	return afs_find_cm_server_by_uuid(call, call->request);
463 }
464 
465 /*
466  * allow the fileserver to see if the cache manager is still alive
467  */
468 static void SRXAFSCB_Probe(struct work_struct *work)
469 {
470 	struct afs_call *call = container_of(work, struct afs_call, work);
471 
472 	_enter("");
473 	afs_send_empty_reply(call);
474 	afs_put_call(call);
475 	_leave("");
476 }
477 
478 /*
479  * deliver request data to a CB.Probe call
480  */
481 static int afs_deliver_cb_probe(struct afs_call *call)
482 {
483 	int ret;
484 
485 	_enter("");
486 
487 	afs_extract_discard(call, 0);
488 	ret = afs_extract_data(call, false);
489 	if (ret < 0)
490 		return ret;
491 
492 	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
493 		return afs_io_error(call, afs_io_error_cm_reply);
494 	return afs_find_cm_server_by_peer(call);
495 }
496 
497 /*
498  * allow the fileserver to quickly find out if the fileserver has been rebooted
499  */
500 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
501 {
502 	struct afs_call *call = container_of(work, struct afs_call, work);
503 	struct afs_uuid *r = call->request;
504 
505 	struct {
506 		__be32	match;
507 	} reply;
508 
509 	_enter("");
510 
511 	if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
512 		reply.match = htonl(0);
513 	else
514 		reply.match = htonl(1);
515 
516 	afs_send_simple_reply(call, &reply, sizeof(reply));
517 	afs_put_call(call);
518 	_leave("");
519 }
520 
521 /*
522  * deliver request data to a CB.ProbeUuid call
523  */
524 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
525 {
526 	struct afs_uuid *r;
527 	unsigned loop;
528 	__be32 *b;
529 	int ret;
530 
531 	_enter("{%u}", call->unmarshall);
532 
533 	switch (call->unmarshall) {
534 	case 0:
535 		call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
536 		if (!call->buffer)
537 			return -ENOMEM;
538 		afs_extract_to_buf(call, 11 * sizeof(__be32));
539 		call->unmarshall++;
540 
541 		/* Fall through */
542 	case 1:
543 		_debug("extract UUID");
544 		ret = afs_extract_data(call, false);
545 		switch (ret) {
546 		case 0:		break;
547 		case -EAGAIN:	return 0;
548 		default:	return ret;
549 		}
550 
551 		_debug("unmarshall UUID");
552 		call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
553 		if (!call->request)
554 			return -ENOMEM;
555 
556 		b = call->buffer;
557 		r = call->request;
558 		r->time_low			= b[0];
559 		r->time_mid			= htons(ntohl(b[1]));
560 		r->time_hi_and_version		= htons(ntohl(b[2]));
561 		r->clock_seq_hi_and_reserved 	= ntohl(b[3]);
562 		r->clock_seq_low		= ntohl(b[4]);
563 
564 		for (loop = 0; loop < 6; loop++)
565 			r->node[loop] = ntohl(b[loop + 5]);
566 
567 		call->unmarshall++;
568 
569 	case 2:
570 		break;
571 	}
572 
573 	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
574 		return afs_io_error(call, afs_io_error_cm_reply);
575 	return afs_find_cm_server_by_uuid(call, call->request);
576 }
577 
578 /*
579  * allow the fileserver to ask about the cache manager's capabilities
580  */
581 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
582 {
583 	struct afs_call *call = container_of(work, struct afs_call, work);
584 	int loop;
585 
586 	struct {
587 		struct /* InterfaceAddr */ {
588 			__be32 nifs;
589 			__be32 uuid[11];
590 			__be32 ifaddr[32];
591 			__be32 netmask[32];
592 			__be32 mtu[32];
593 		} ia;
594 		struct /* Capabilities */ {
595 			__be32 capcount;
596 			__be32 caps[1];
597 		} cap;
598 	} reply;
599 
600 	_enter("");
601 
602 	memset(&reply, 0, sizeof(reply));
603 
604 	reply.ia.uuid[0] = call->net->uuid.time_low;
605 	reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
606 	reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
607 	reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
608 	reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
609 	for (loop = 0; loop < 6; loop++)
610 		reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
611 
612 	reply.cap.capcount = htonl(1);
613 	reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
614 	afs_send_simple_reply(call, &reply, sizeof(reply));
615 	afs_put_call(call);
616 	_leave("");
617 }
618 
619 /*
620  * deliver request data to a CB.TellMeAboutYourself call
621  */
622 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
623 {
624 	int ret;
625 
626 	_enter("");
627 
628 	afs_extract_discard(call, 0);
629 	ret = afs_extract_data(call, false);
630 	if (ret < 0)
631 		return ret;
632 
633 	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
634 		return afs_io_error(call, afs_io_error_cm_reply);
635 	return afs_find_cm_server_by_peer(call);
636 }
637 
638 /*
639  * deliver request data to a YFS CB.CallBack call
640  */
641 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
642 {
643 	struct afs_callback_break *cb;
644 	struct yfs_xdr_YFSFid *bp;
645 	size_t size;
646 	int ret, loop;
647 
648 	_enter("{%u}", call->unmarshall);
649 
650 	switch (call->unmarshall) {
651 	case 0:
652 		afs_extract_to_tmp(call);
653 		call->unmarshall++;
654 
655 		/* extract the FID array and its count in two steps */
656 		/* Fall through */
657 	case 1:
658 		_debug("extract FID count");
659 		ret = afs_extract_data(call, true);
660 		if (ret < 0)
661 			return ret;
662 
663 		call->count = ntohl(call->tmp);
664 		_debug("FID count: %u", call->count);
665 		if (call->count > YFSCBMAX)
666 			return afs_protocol_error(call, -EBADMSG,
667 						  afs_eproto_cb_fid_count);
668 
669 		size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
670 		call->buffer = kmalloc(size, GFP_KERNEL);
671 		if (!call->buffer)
672 			return -ENOMEM;
673 		afs_extract_to_buf(call, size);
674 		call->unmarshall++;
675 
676 		/* Fall through */
677 	case 2:
678 		_debug("extract FID array");
679 		ret = afs_extract_data(call, false);
680 		if (ret < 0)
681 			return ret;
682 
683 		_debug("unmarshall FID array");
684 		call->request = kcalloc(call->count,
685 					sizeof(struct afs_callback_break),
686 					GFP_KERNEL);
687 		if (!call->request)
688 			return -ENOMEM;
689 
690 		cb = call->request;
691 		bp = call->buffer;
692 		for (loop = call->count; loop > 0; loop--, cb++) {
693 			cb->fid.vid	= xdr_to_u64(bp->volume);
694 			cb->fid.vnode	= xdr_to_u64(bp->vnode.lo);
695 			cb->fid.vnode_hi = ntohl(bp->vnode.hi);
696 			cb->fid.unique	= ntohl(bp->vnode.unique);
697 			bp++;
698 		}
699 
700 		afs_extract_to_tmp(call);
701 		call->unmarshall++;
702 
703 	case 3:
704 		break;
705 	}
706 
707 	if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
708 		return afs_io_error(call, afs_io_error_cm_reply);
709 
710 	/* We'll need the file server record as that tells us which set of
711 	 * vnodes to operate upon.
712 	 */
713 	return afs_find_cm_server_by_peer(call);
714 }
715