1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS Cache Manager Service
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
15 #include "protocol_yfs.h"
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 *);
30 static int afs_deliver_yfs_cb_callback(struct afs_call *);
32 #define CM_NAME(name) \
33 char afs_SRXCB##name##_name[] __tracepoint_string = \
37 * CB.CallBack operation type
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,
48 * CB.InitCallBackState operation type
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,
59 * CB.InitCallBackState3 operation type
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,
70 * CB.Probe operation type
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,
81 * CB.ProbeUuid operation type
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,
92 * CB.TellMeAboutYourself operation type
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,
103 * YFS CB.CallBack operation type
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,
114 * route an incoming cache manager call
115 * - return T if supported, F if not
117 bool afs_cm_incoming_call(struct afs_call *call)
119 _enter("{%u, CB.OP %u}", call->service_id, call->operation_ID);
121 switch (call->operation_ID) {
123 call->type = &afs_SRXCBCallBack;
125 case CBInitCallBackState:
126 call->type = &afs_SRXCBInitCallBackState;
128 case CBInitCallBackState3:
129 call->type = &afs_SRXCBInitCallBackState3;
132 call->type = &afs_SRXCBProbe;
135 call->type = &afs_SRXCBProbeUuid;
137 case CBTellMeAboutYourself:
138 call->type = &afs_SRXCBTellMeAboutYourself;
141 if (call->service_id != YFS_CM_SERVICE)
143 call->type = &afs_SRXYFSCB_CallBack;
151 * Find the server record by peer address and record a probe to the cache
152 * manager from a server.
154 static int afs_find_cm_server_by_peer(struct afs_call *call)
156 struct sockaddr_rxrpc srx;
157 struct afs_server *server;
159 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
161 server = afs_find_server(call->net, &srx);
163 trace_afs_cm_no_server(call, &srx);
167 call->server = server;
172 * Find the server record by server UUID and record a probe to the cache
173 * manager from a server.
175 static int afs_find_cm_server_by_uuid(struct afs_call *call,
176 struct afs_uuid *uuid)
178 struct afs_server *server;
181 server = afs_find_server_by_uuid(call->net, call->request);
184 trace_afs_cm_no_server_u(call, call->request);
188 call->server = server;
193 * Clean up a cache manager call.
195 static void afs_cm_destructor(struct afs_call *call)
202 * Abort a service call from within an action function.
204 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
207 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
208 abort_code, error, why);
209 afs_set_call_complete(call, error, 0);
213 * The server supplied a list of callbacks that it wanted to break.
215 static void SRXAFSCB_CallBack(struct work_struct *work)
217 struct afs_call *call = container_of(work, struct afs_call, work);
221 /* We need to break the callbacks before sending the reply as the
222 * server holds up change visibility till it receives our reply so as
223 * to maintain cache coherency.
226 trace_afs_server(call->server,
227 atomic_read(&call->server->ref),
228 atomic_read(&call->server->active),
229 afs_server_trace_callback);
230 afs_break_callbacks(call->server, call->count, call->request);
233 afs_send_empty_reply(call);
239 * deliver request data to a CB.CallBack call
241 static int afs_deliver_cb_callback(struct afs_call *call)
243 struct afs_callback_break *cb;
247 _enter("{%u}", call->unmarshall);
249 switch (call->unmarshall) {
251 afs_extract_to_tmp(call);
254 /* extract the FID array and its count in two steps */
257 _debug("extract FID count");
258 ret = afs_extract_data(call, true);
262 call->count = ntohl(call->tmp);
263 _debug("FID count: %u", call->count);
264 if (call->count > AFSCBMAX)
265 return afs_protocol_error(call, afs_eproto_cb_fid_count);
267 call->buffer = kmalloc(array3_size(call->count, 3, 4),
271 afs_extract_to_buf(call, call->count * 3 * 4);
276 _debug("extract FID array");
277 ret = afs_extract_data(call, true);
281 _debug("unmarshall FID array");
282 call->request = kcalloc(call->count,
283 sizeof(struct afs_callback_break),
290 for (loop = call->count; loop > 0; loop--, cb++) {
291 cb->fid.vid = ntohl(*bp++);
292 cb->fid.vnode = ntohl(*bp++);
293 cb->fid.unique = ntohl(*bp++);
296 afs_extract_to_tmp(call);
299 /* extract the callback array and its count in two steps */
302 _debug("extract CB count");
303 ret = afs_extract_data(call, true);
307 call->count2 = ntohl(call->tmp);
308 _debug("CB count: %u", call->count2);
309 if (call->count2 != call->count && call->count2 != 0)
310 return afs_protocol_error(call, afs_eproto_cb_count);
311 call->iter = &call->def_iter;
312 iov_iter_discard(&call->def_iter, READ, call->count2 * 3 * 4);
317 _debug("extract discard %zu/%u",
318 iov_iter_count(call->iter), call->count2 * 3 * 4);
320 ret = afs_extract_data(call, false);
329 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
330 return afs_io_error(call, afs_io_error_cm_reply);
332 /* we'll need the file server record as that tells us which set of
333 * vnodes to operate upon */
334 return afs_find_cm_server_by_peer(call);
338 * allow the fileserver to request callback state (re-)initialisation
340 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
342 struct afs_call *call = container_of(work, struct afs_call, work);
344 _enter("{%p}", call->server);
347 afs_init_callback_state(call->server);
348 afs_send_empty_reply(call);
354 * deliver request data to a CB.InitCallBackState call
356 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
362 afs_extract_discard(call, 0);
363 ret = afs_extract_data(call, false);
367 /* we'll need the file server record as that tells us which set of
368 * vnodes to operate upon */
369 return afs_find_cm_server_by_peer(call);
373 * deliver request data to a CB.InitCallBackState3 call
375 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
384 _enter("{%u}", call->unmarshall);
386 switch (call->unmarshall) {
388 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
391 afs_extract_to_buf(call, 11 * sizeof(__be32));
396 _debug("extract UUID");
397 ret = afs_extract_data(call, false);
400 case -EAGAIN: return 0;
404 _debug("unmarshall UUID");
405 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
412 r->time_mid = htons(ntohl(b[1]));
413 r->time_hi_and_version = htons(ntohl(b[2]));
414 r->clock_seq_hi_and_reserved = ntohl(b[3]);
415 r->clock_seq_low = ntohl(b[4]);
417 for (loop = 0; loop < 6; loop++)
418 r->node[loop] = ntohl(b[loop + 5]);
426 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
427 return afs_io_error(call, afs_io_error_cm_reply);
429 /* we'll need the file server record as that tells us which set of
430 * vnodes to operate upon */
431 return afs_find_cm_server_by_uuid(call, call->request);
435 * allow the fileserver to see if the cache manager is still alive
437 static void SRXAFSCB_Probe(struct work_struct *work)
439 struct afs_call *call = container_of(work, struct afs_call, work);
442 afs_send_empty_reply(call);
448 * deliver request data to a CB.Probe call
450 static int afs_deliver_cb_probe(struct afs_call *call)
456 afs_extract_discard(call, 0);
457 ret = afs_extract_data(call, false);
461 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
462 return afs_io_error(call, afs_io_error_cm_reply);
463 return afs_find_cm_server_by_peer(call);
467 * Allow the fileserver to quickly find out if the cache manager has been
470 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
472 struct afs_call *call = container_of(work, struct afs_call, work);
473 struct afs_uuid *r = call->request;
477 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
478 afs_send_empty_reply(call);
480 afs_abort_service_call(call, 1, 1, "K-1");
487 * deliver request data to a CB.ProbeUuid call
489 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
496 _enter("{%u}", call->unmarshall);
498 switch (call->unmarshall) {
500 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
503 afs_extract_to_buf(call, 11 * sizeof(__be32));
508 _debug("extract UUID");
509 ret = afs_extract_data(call, false);
512 case -EAGAIN: return 0;
516 _debug("unmarshall UUID");
517 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
524 r->time_mid = htons(ntohl(b[1]));
525 r->time_hi_and_version = htons(ntohl(b[2]));
526 r->clock_seq_hi_and_reserved = ntohl(b[3]);
527 r->clock_seq_low = ntohl(b[4]);
529 for (loop = 0; loop < 6; loop++)
530 r->node[loop] = ntohl(b[loop + 5]);
538 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
539 return afs_io_error(call, afs_io_error_cm_reply);
540 return afs_find_cm_server_by_peer(call);
544 * allow the fileserver to ask about the cache manager's capabilities
546 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
548 struct afs_call *call = container_of(work, struct afs_call, work);
552 struct /* InterfaceAddr */ {
559 struct /* Capabilities */ {
567 memset(&reply, 0, sizeof(reply));
569 reply.ia.uuid[0] = call->net->uuid.time_low;
570 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
571 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
572 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
573 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
574 for (loop = 0; loop < 6; loop++)
575 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
577 reply.cap.capcount = htonl(1);
578 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
579 afs_send_simple_reply(call, &reply, sizeof(reply));
585 * deliver request data to a CB.TellMeAboutYourself call
587 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
593 afs_extract_discard(call, 0);
594 ret = afs_extract_data(call, false);
598 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
599 return afs_io_error(call, afs_io_error_cm_reply);
600 return afs_find_cm_server_by_peer(call);
604 * deliver request data to a YFS CB.CallBack call
606 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
608 struct afs_callback_break *cb;
609 struct yfs_xdr_YFSFid *bp;
613 _enter("{%u}", call->unmarshall);
615 switch (call->unmarshall) {
617 afs_extract_to_tmp(call);
620 /* extract the FID array and its count in two steps */
623 _debug("extract FID count");
624 ret = afs_extract_data(call, true);
628 call->count = ntohl(call->tmp);
629 _debug("FID count: %u", call->count);
630 if (call->count > YFSCBMAX)
631 return afs_protocol_error(call, afs_eproto_cb_fid_count);
633 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
634 call->buffer = kmalloc(size, GFP_KERNEL);
637 afs_extract_to_buf(call, size);
642 _debug("extract FID array");
643 ret = afs_extract_data(call, false);
647 _debug("unmarshall FID array");
648 call->request = kcalloc(call->count,
649 sizeof(struct afs_callback_break),
656 for (loop = call->count; loop > 0; loop--, cb++) {
657 cb->fid.vid = xdr_to_u64(bp->volume);
658 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
659 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
660 cb->fid.unique = ntohl(bp->vnode.unique);
664 afs_extract_to_tmp(call);
671 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
672 return afs_io_error(call, afs_io_error_cm_reply);
674 /* We'll need the file server record as that tells us which set of
675 * vnodes to operate upon.
677 return afs_find_cm_server_by_peer(call);