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