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 *);
33 * CB.CallBack operation type
35 static const struct afs_call_type afs_SRXCBCallBack = {
36 .name = "CB.CallBack",
37 .deliver = afs_deliver_cb_callback,
38 .destructor = afs_cm_destructor,
39 .work = SRXAFSCB_CallBack,
43 * CB.InitCallBackState operation type
45 static const struct afs_call_type afs_SRXCBInitCallBackState = {
46 .name = "CB.InitCallBackState",
47 .deliver = afs_deliver_cb_init_call_back_state,
48 .destructor = afs_cm_destructor,
49 .work = SRXAFSCB_InitCallBackState,
53 * CB.InitCallBackState3 operation type
55 static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
56 .name = "CB.InitCallBackState3",
57 .deliver = afs_deliver_cb_init_call_back_state3,
58 .destructor = afs_cm_destructor,
59 .work = SRXAFSCB_InitCallBackState,
63 * CB.Probe operation type
65 static const struct afs_call_type afs_SRXCBProbe = {
67 .deliver = afs_deliver_cb_probe,
68 .destructor = afs_cm_destructor,
69 .work = SRXAFSCB_Probe,
73 * CB.ProbeUuid operation type
75 static const struct afs_call_type afs_SRXCBProbeUuid = {
76 .name = "CB.ProbeUuid",
77 .deliver = afs_deliver_cb_probe_uuid,
78 .destructor = afs_cm_destructor,
79 .work = SRXAFSCB_ProbeUuid,
83 * CB.TellMeAboutYourself operation type
85 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
86 .name = "CB.TellMeAboutYourself",
87 .deliver = afs_deliver_cb_tell_me_about_yourself,
88 .destructor = afs_cm_destructor,
89 .work = SRXAFSCB_TellMeAboutYourself,
93 * YFS CB.CallBack operation type
95 static const struct afs_call_type afs_SRXYFSCB_CallBack = {
96 .name = "YFSCB.CallBack",
97 .deliver = afs_deliver_yfs_cb_callback,
98 .destructor = afs_cm_destructor,
99 .work = SRXAFSCB_CallBack,
103 * route an incoming cache manager call
104 * - return T if supported, F if not
106 bool afs_cm_incoming_call(struct afs_call *call)
108 _enter("{%u, CB.OP %u}", call->service_id, call->operation_ID);
110 switch (call->operation_ID) {
112 call->type = &afs_SRXCBCallBack;
114 case CBInitCallBackState:
115 call->type = &afs_SRXCBInitCallBackState;
117 case CBInitCallBackState3:
118 call->type = &afs_SRXCBInitCallBackState3;
121 call->type = &afs_SRXCBProbe;
124 call->type = &afs_SRXCBProbeUuid;
126 case CBTellMeAboutYourself:
127 call->type = &afs_SRXCBTellMeAboutYourself;
130 if (call->service_id != YFS_CM_SERVICE)
132 call->type = &afs_SRXYFSCB_CallBack;
140 * Find the server record by peer address and record a probe to the cache
141 * manager from a server.
143 static int afs_find_cm_server_by_peer(struct afs_call *call)
145 struct sockaddr_rxrpc srx;
146 struct afs_server *server;
148 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
150 server = afs_find_server(call->net, &srx);
152 trace_afs_cm_no_server(call, &srx);
156 call->server = server;
161 * Find the server record by server UUID and record a probe to the cache
162 * manager from a server.
164 static int afs_find_cm_server_by_uuid(struct afs_call *call,
165 struct afs_uuid *uuid)
167 struct afs_server *server;
170 server = afs_find_server_by_uuid(call->net, call->request);
173 trace_afs_cm_no_server_u(call, call->request);
177 call->server = server;
182 * Clean up a cache manager call.
184 static void afs_cm_destructor(struct afs_call *call)
191 * Abort a service call from within an action function.
193 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
196 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
197 abort_code, error, why);
198 afs_set_call_complete(call, error, 0);
202 * The server supplied a list of callbacks that it wanted to break.
204 static void SRXAFSCB_CallBack(struct work_struct *work)
206 struct afs_call *call = container_of(work, struct afs_call, work);
210 /* We need to break the callbacks before sending the reply as the
211 * server holds up change visibility till it receives our reply so as
212 * to maintain cache coherency.
215 trace_afs_server(call->server,
216 atomic_read(&call->server->ref),
217 atomic_read(&call->server->active),
218 afs_server_trace_callback);
219 afs_break_callbacks(call->server, call->count, call->request);
222 afs_send_empty_reply(call);
228 * deliver request data to a CB.CallBack call
230 static int afs_deliver_cb_callback(struct afs_call *call)
232 struct afs_callback_break *cb;
236 _enter("{%u}", call->unmarshall);
238 switch (call->unmarshall) {
240 afs_extract_to_tmp(call);
243 /* extract the FID array and its count in two steps */
246 _debug("extract FID count");
247 ret = afs_extract_data(call, true);
251 call->count = ntohl(call->tmp);
252 _debug("FID count: %u", call->count);
253 if (call->count > AFSCBMAX)
254 return afs_protocol_error(call, afs_eproto_cb_fid_count);
256 call->buffer = kmalloc(array3_size(call->count, 3, 4),
260 afs_extract_to_buf(call, call->count * 3 * 4);
265 _debug("extract FID array");
266 ret = afs_extract_data(call, true);
270 _debug("unmarshall FID array");
271 call->request = kcalloc(call->count,
272 sizeof(struct afs_callback_break),
279 for (loop = call->count; loop > 0; loop--, cb++) {
280 cb->fid.vid = ntohl(*bp++);
281 cb->fid.vnode = ntohl(*bp++);
282 cb->fid.unique = ntohl(*bp++);
285 afs_extract_to_tmp(call);
288 /* extract the callback array and its count in two steps */
291 _debug("extract CB count");
292 ret = afs_extract_data(call, true);
296 call->count2 = ntohl(call->tmp);
297 _debug("CB count: %u", call->count2);
298 if (call->count2 != call->count && call->count2 != 0)
299 return afs_protocol_error(call, afs_eproto_cb_count);
300 call->iter = &call->def_iter;
301 iov_iter_discard(&call->def_iter, READ, call->count2 * 3 * 4);
306 _debug("extract discard %zu/%u",
307 iov_iter_count(call->iter), call->count2 * 3 * 4);
309 ret = afs_extract_data(call, false);
320 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
321 return afs_io_error(call, afs_io_error_cm_reply);
323 /* we'll need the file server record as that tells us which set of
324 * vnodes to operate upon */
325 return afs_find_cm_server_by_peer(call);
329 * allow the fileserver to request callback state (re-)initialisation
331 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
333 struct afs_call *call = container_of(work, struct afs_call, work);
335 _enter("{%p}", call->server);
338 afs_init_callback_state(call->server);
339 afs_send_empty_reply(call);
345 * deliver request data to a CB.InitCallBackState call
347 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
353 afs_extract_discard(call, 0);
354 ret = afs_extract_data(call, false);
358 /* we'll need the file server record as that tells us which set of
359 * vnodes to operate upon */
360 return afs_find_cm_server_by_peer(call);
364 * deliver request data to a CB.InitCallBackState3 call
366 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
375 _enter("{%u}", call->unmarshall);
377 switch (call->unmarshall) {
379 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
382 afs_extract_to_buf(call, 11 * sizeof(__be32));
387 _debug("extract UUID");
388 ret = afs_extract_data(call, false);
391 case -EAGAIN: return 0;
395 _debug("unmarshall UUID");
396 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
403 r->time_mid = htons(ntohl(b[1]));
404 r->time_hi_and_version = htons(ntohl(b[2]));
405 r->clock_seq_hi_and_reserved = ntohl(b[3]);
406 r->clock_seq_low = ntohl(b[4]);
408 for (loop = 0; loop < 6; loop++)
409 r->node[loop] = ntohl(b[loop + 5]);
418 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
419 return afs_io_error(call, afs_io_error_cm_reply);
421 /* we'll need the file server record as that tells us which set of
422 * vnodes to operate upon */
423 return afs_find_cm_server_by_uuid(call, call->request);
427 * allow the fileserver to see if the cache manager is still alive
429 static void SRXAFSCB_Probe(struct work_struct *work)
431 struct afs_call *call = container_of(work, struct afs_call, work);
434 afs_send_empty_reply(call);
440 * deliver request data to a CB.Probe call
442 static int afs_deliver_cb_probe(struct afs_call *call)
448 afs_extract_discard(call, 0);
449 ret = afs_extract_data(call, false);
453 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
454 return afs_io_error(call, afs_io_error_cm_reply);
455 return afs_find_cm_server_by_peer(call);
459 * Allow the fileserver to quickly find out if the cache manager has been
462 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
464 struct afs_call *call = container_of(work, struct afs_call, work);
465 struct afs_uuid *r = call->request;
469 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
470 afs_send_empty_reply(call);
472 afs_abort_service_call(call, 1, 1, "K-1");
479 * deliver request data to a CB.ProbeUuid call
481 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
488 _enter("{%u}", call->unmarshall);
490 switch (call->unmarshall) {
492 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
495 afs_extract_to_buf(call, 11 * sizeof(__be32));
500 _debug("extract UUID");
501 ret = afs_extract_data(call, false);
504 case -EAGAIN: return 0;
508 _debug("unmarshall UUID");
509 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
516 r->time_mid = htons(ntohl(b[1]));
517 r->time_hi_and_version = htons(ntohl(b[2]));
518 r->clock_seq_hi_and_reserved = ntohl(b[3]);
519 r->clock_seq_low = ntohl(b[4]);
521 for (loop = 0; loop < 6; loop++)
522 r->node[loop] = ntohl(b[loop + 5]);
531 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
532 return afs_io_error(call, afs_io_error_cm_reply);
533 return afs_find_cm_server_by_peer(call);
537 * allow the fileserver to ask about the cache manager's capabilities
539 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
541 struct afs_call *call = container_of(work, struct afs_call, work);
545 struct /* InterfaceAddr */ {
552 struct /* Capabilities */ {
560 memset(&reply, 0, sizeof(reply));
562 reply.ia.uuid[0] = call->net->uuid.time_low;
563 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
564 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
565 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
566 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
567 for (loop = 0; loop < 6; loop++)
568 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
570 reply.cap.capcount = htonl(1);
571 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
572 afs_send_simple_reply(call, &reply, sizeof(reply));
578 * deliver request data to a CB.TellMeAboutYourself call
580 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
586 afs_extract_discard(call, 0);
587 ret = afs_extract_data(call, false);
591 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
592 return afs_io_error(call, afs_io_error_cm_reply);
593 return afs_find_cm_server_by_peer(call);
597 * deliver request data to a YFS CB.CallBack call
599 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
601 struct afs_callback_break *cb;
602 struct yfs_xdr_YFSFid *bp;
606 _enter("{%u}", call->unmarshall);
608 switch (call->unmarshall) {
610 afs_extract_to_tmp(call);
613 /* extract the FID array and its count in two steps */
616 _debug("extract FID count");
617 ret = afs_extract_data(call, true);
621 call->count = ntohl(call->tmp);
622 _debug("FID count: %u", call->count);
623 if (call->count > YFSCBMAX)
624 return afs_protocol_error(call, afs_eproto_cb_fid_count);
626 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
627 call->buffer = kmalloc(size, GFP_KERNEL);
630 afs_extract_to_buf(call, size);
635 _debug("extract FID array");
636 ret = afs_extract_data(call, false);
640 _debug("unmarshall FID array");
641 call->request = kcalloc(call->count,
642 sizeof(struct afs_callback_break),
649 for (loop = call->count; loop > 0; loop--, cb++) {
650 cb->fid.vid = xdr_to_u64(bp->volume);
651 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
652 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
653 cb->fid.unique = ntohl(bp->vnode.unique);
657 afs_extract_to_tmp(call);
665 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
666 return afs_io_error(call, afs_io_error_cm_reply);
668 /* We'll need the file server record as that tells us which set of
669 * vnodes to operate upon.
671 return afs_find_cm_server_by_peer(call);