1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS Cache Manager Service
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
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);
331 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
332 return afs_io_error(call, afs_io_error_cm_reply);
334 /* we'll need the file server record as that tells us which set of
335 * vnodes to operate upon */
336 return afs_find_cm_server_by_peer(call);
340 * allow the fileserver to request callback state (re-)initialisation
342 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
344 struct afs_call *call = container_of(work, struct afs_call, work);
346 _enter("{%p}", call->server);
349 afs_init_callback_state(call->server);
350 afs_send_empty_reply(call);
356 * deliver request data to a CB.InitCallBackState call
358 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
364 afs_extract_discard(call, 0);
365 ret = afs_extract_data(call, false);
369 /* we'll need the file server record as that tells us which set of
370 * vnodes to operate upon */
371 return afs_find_cm_server_by_peer(call);
375 * deliver request data to a CB.InitCallBackState3 call
377 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
386 _enter("{%u}", call->unmarshall);
388 switch (call->unmarshall) {
390 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
393 afs_extract_to_buf(call, 11 * sizeof(__be32));
398 _debug("extract UUID");
399 ret = afs_extract_data(call, false);
402 case -EAGAIN: return 0;
406 _debug("unmarshall UUID");
407 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
414 r->time_mid = htons(ntohl(b[1]));
415 r->time_hi_and_version = htons(ntohl(b[2]));
416 r->clock_seq_hi_and_reserved = ntohl(b[3]);
417 r->clock_seq_low = ntohl(b[4]);
419 for (loop = 0; loop < 6; loop++)
420 r->node[loop] = ntohl(b[loop + 5]);
429 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
430 return afs_io_error(call, afs_io_error_cm_reply);
432 /* we'll need the file server record as that tells us which set of
433 * vnodes to operate upon */
434 return afs_find_cm_server_by_uuid(call, call->request);
438 * allow the fileserver to see if the cache manager is still alive
440 static void SRXAFSCB_Probe(struct work_struct *work)
442 struct afs_call *call = container_of(work, struct afs_call, work);
445 afs_send_empty_reply(call);
451 * deliver request data to a CB.Probe call
453 static int afs_deliver_cb_probe(struct afs_call *call)
459 afs_extract_discard(call, 0);
460 ret = afs_extract_data(call, false);
464 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
465 return afs_io_error(call, afs_io_error_cm_reply);
466 return afs_find_cm_server_by_peer(call);
470 * Allow the fileserver to quickly find out if the cache manager has been
473 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
475 struct afs_call *call = container_of(work, struct afs_call, work);
476 struct afs_uuid *r = call->request;
480 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
481 afs_send_empty_reply(call);
483 afs_abort_service_call(call, 1, 1, "K-1");
490 * deliver request data to a CB.ProbeUuid call
492 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
499 _enter("{%u}", call->unmarshall);
501 switch (call->unmarshall) {
503 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
506 afs_extract_to_buf(call, 11 * sizeof(__be32));
511 _debug("extract UUID");
512 ret = afs_extract_data(call, false);
515 case -EAGAIN: return 0;
519 _debug("unmarshall UUID");
520 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
527 r->time_mid = htons(ntohl(b[1]));
528 r->time_hi_and_version = htons(ntohl(b[2]));
529 r->clock_seq_hi_and_reserved = ntohl(b[3]);
530 r->clock_seq_low = ntohl(b[4]);
532 for (loop = 0; loop < 6; loop++)
533 r->node[loop] = ntohl(b[loop + 5]);
542 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
543 return afs_io_error(call, afs_io_error_cm_reply);
544 return afs_find_cm_server_by_peer(call);
548 * allow the fileserver to ask about the cache manager's capabilities
550 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
552 struct afs_call *call = container_of(work, struct afs_call, work);
556 struct /* InterfaceAddr */ {
563 struct /* Capabilities */ {
571 memset(&reply, 0, sizeof(reply));
573 reply.ia.uuid[0] = call->net->uuid.time_low;
574 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
575 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
576 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
577 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
578 for (loop = 0; loop < 6; loop++)
579 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
581 reply.cap.capcount = htonl(1);
582 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
583 afs_send_simple_reply(call, &reply, sizeof(reply));
589 * deliver request data to a CB.TellMeAboutYourself call
591 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
597 afs_extract_discard(call, 0);
598 ret = afs_extract_data(call, false);
602 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
603 return afs_io_error(call, afs_io_error_cm_reply);
604 return afs_find_cm_server_by_peer(call);
608 * deliver request data to a YFS CB.CallBack call
610 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
612 struct afs_callback_break *cb;
613 struct yfs_xdr_YFSFid *bp;
617 _enter("{%u}", call->unmarshall);
619 switch (call->unmarshall) {
621 afs_extract_to_tmp(call);
624 /* extract the FID array and its count in two steps */
627 _debug("extract FID count");
628 ret = afs_extract_data(call, true);
632 call->count = ntohl(call->tmp);
633 _debug("FID count: %u", call->count);
634 if (call->count > YFSCBMAX)
635 return afs_protocol_error(call, afs_eproto_cb_fid_count);
637 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
638 call->buffer = kmalloc(size, GFP_KERNEL);
641 afs_extract_to_buf(call, size);
646 _debug("extract FID array");
647 ret = afs_extract_data(call, false);
651 _debug("unmarshall FID array");
652 call->request = kcalloc(call->count,
653 sizeof(struct afs_callback_break),
660 for (loop = call->count; loop > 0; loop--, cb++) {
661 cb->fid.vid = xdr_to_u64(bp->volume);
662 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
663 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
664 cb->fid.unique = ntohl(bp->vnode.unique);
668 afs_extract_to_tmp(call);
676 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
677 return afs_io_error(call, afs_io_error_cm_reply);
679 /* We'll need the file server record as that tells us which set of
680 * vnodes to operate upon.
682 return afs_find_cm_server_by_peer(call);