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"
16 #define RXRPC_TRACE_ONLY_DEFINE_ENUMS
17 #include <trace/events/rxrpc.h>
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 *);
32 static int afs_deliver_yfs_cb_callback(struct afs_call *);
35 * CB.CallBack operation type
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,
45 * CB.InitCallBackState operation type
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,
55 * CB.InitCallBackState3 operation type
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,
65 * CB.Probe operation type
67 static const struct afs_call_type afs_SRXCBProbe = {
69 .deliver = afs_deliver_cb_probe,
70 .destructor = afs_cm_destructor,
71 .work = SRXAFSCB_Probe,
75 * CB.ProbeUuid operation type
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,
85 * CB.TellMeAboutYourself operation type
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,
95 * YFS CB.CallBack operation type
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,
105 * route an incoming cache manager call
106 * - return T if supported, F if not
108 bool afs_cm_incoming_call(struct afs_call *call)
110 _enter("{%u, CB.OP %u}", call->service_id, call->operation_ID);
112 switch (call->operation_ID) {
114 call->type = &afs_SRXCBCallBack;
116 case CBInitCallBackState:
117 call->type = &afs_SRXCBInitCallBackState;
119 case CBInitCallBackState3:
120 call->type = &afs_SRXCBInitCallBackState3;
123 call->type = &afs_SRXCBProbe;
126 call->type = &afs_SRXCBProbeUuid;
128 case CBTellMeAboutYourself:
129 call->type = &afs_SRXCBTellMeAboutYourself;
132 if (call->service_id != YFS_CM_SERVICE)
134 call->type = &afs_SRXYFSCB_CallBack;
142 * Find the server record by peer address and record a probe to the cache
143 * manager from a server.
145 static int afs_find_cm_server_by_peer(struct afs_call *call)
147 struct sockaddr_rxrpc srx;
148 struct afs_server *server;
150 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
152 server = afs_find_server(call->net, &srx);
154 trace_afs_cm_no_server(call, &srx);
158 call->server = server;
163 * Find the server record by server UUID and record a probe to the cache
164 * manager from a server.
166 static int afs_find_cm_server_by_uuid(struct afs_call *call,
167 struct afs_uuid *uuid)
169 struct afs_server *server;
172 server = afs_find_server_by_uuid(call->net, call->request);
175 trace_afs_cm_no_server_u(call, call->request);
179 call->server = server;
184 * Clean up a cache manager call.
186 static void afs_cm_destructor(struct afs_call *call)
193 * Abort a service call from within an action function.
195 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
196 enum rxrpc_abort_reason why)
198 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
199 abort_code, error, why);
200 afs_set_call_complete(call, error, 0);
204 * The server supplied a list of callbacks that it wanted to break.
206 static void SRXAFSCB_CallBack(struct work_struct *work)
208 struct afs_call *call = container_of(work, struct afs_call, work);
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.
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);
224 afs_send_empty_reply(call);
230 * deliver request data to a CB.CallBack call
232 static int afs_deliver_cb_callback(struct afs_call *call)
234 struct afs_callback_break *cb;
238 _enter("{%u}", call->unmarshall);
240 switch (call->unmarshall) {
242 afs_extract_to_tmp(call);
245 /* extract the FID array and its count in two steps */
248 _debug("extract FID count");
249 ret = afs_extract_data(call, true);
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);
258 call->buffer = kmalloc(array3_size(call->count, 3, 4),
262 afs_extract_to_buf(call, call->count * 3 * 4);
267 _debug("extract FID array");
268 ret = afs_extract_data(call, true);
272 _debug("unmarshall FID array");
273 call->request = kcalloc(call->count,
274 sizeof(struct afs_callback_break),
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++);
287 afs_extract_to_tmp(call);
290 /* extract the callback array and its count in two steps */
293 _debug("extract CB count");
294 ret = afs_extract_data(call, true);
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);
308 _debug("extract discard %zu/%u",
309 iov_iter_count(call->iter), call->count2 * 3 * 4);
311 ret = afs_extract_data(call, false);
322 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
323 return afs_io_error(call, afs_io_error_cm_reply);
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);
331 * allow the fileserver to request callback state (re-)initialisation
333 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
335 struct afs_call *call = container_of(work, struct afs_call, work);
337 _enter("{%p}", call->server);
340 afs_init_callback_state(call->server);
341 afs_send_empty_reply(call);
347 * deliver request data to a CB.InitCallBackState call
349 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
355 afs_extract_discard(call, 0);
356 ret = afs_extract_data(call, false);
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);
366 * deliver request data to a CB.InitCallBackState3 call
368 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
377 _enter("{%u}", call->unmarshall);
379 switch (call->unmarshall) {
381 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
384 afs_extract_to_buf(call, 11 * sizeof(__be32));
389 _debug("extract UUID");
390 ret = afs_extract_data(call, false);
393 case -EAGAIN: return 0;
397 _debug("unmarshall UUID");
398 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
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]);
410 for (loop = 0; loop < 6; loop++)
411 r->node[loop] = ntohl(b[loop + 5]);
420 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
421 return afs_io_error(call, afs_io_error_cm_reply);
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);
429 * allow the fileserver to see if the cache manager is still alive
431 static void SRXAFSCB_Probe(struct work_struct *work)
433 struct afs_call *call = container_of(work, struct afs_call, work);
436 afs_send_empty_reply(call);
442 * deliver request data to a CB.Probe call
444 static int afs_deliver_cb_probe(struct afs_call *call)
450 afs_extract_discard(call, 0);
451 ret = afs_extract_data(call, false);
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);
461 * Allow the fileserver to quickly find out if the cache manager has been
464 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
466 struct afs_call *call = container_of(work, struct afs_call, work);
467 struct afs_uuid *r = call->request;
471 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
472 afs_send_empty_reply(call);
474 afs_abort_service_call(call, 1, 1, afs_abort_probeuuid_negative);
481 * deliver request data to a CB.ProbeUuid call
483 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
490 _enter("{%u}", call->unmarshall);
492 switch (call->unmarshall) {
494 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
497 afs_extract_to_buf(call, 11 * sizeof(__be32));
502 _debug("extract UUID");
503 ret = afs_extract_data(call, false);
506 case -EAGAIN: return 0;
510 _debug("unmarshall UUID");
511 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
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]);
523 for (loop = 0; loop < 6; loop++)
524 r->node[loop] = ntohl(b[loop + 5]);
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);
539 * allow the fileserver to ask about the cache manager's capabilities
541 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
543 struct afs_call *call = container_of(work, struct afs_call, work);
547 struct /* InterfaceAddr */ {
554 struct /* Capabilities */ {
562 memset(&reply, 0, sizeof(reply));
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]);
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));
580 * deliver request data to a CB.TellMeAboutYourself call
582 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
588 afs_extract_discard(call, 0);
589 ret = afs_extract_data(call, false);
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);
599 * deliver request data to a YFS CB.CallBack call
601 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
603 struct afs_callback_break *cb;
604 struct yfs_xdr_YFSFid *bp;
608 _enter("{%u}", call->unmarshall);
610 switch (call->unmarshall) {
612 afs_extract_to_tmp(call);
615 /* extract the FID array and its count in two steps */
618 _debug("extract FID count");
619 ret = afs_extract_data(call, true);
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);
628 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
629 call->buffer = kmalloc(size, GFP_KERNEL);
632 afs_extract_to_buf(call, size);
637 _debug("extract FID array");
638 ret = afs_extract_data(call, false);
642 _debug("unmarshall FID array");
643 call->request = kcalloc(call->count,
644 sizeof(struct afs_callback_break),
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);
659 afs_extract_to_tmp(call);
667 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
668 return afs_io_error(call, afs_io_error_cm_reply);
670 /* We'll need the file server record as that tells us which set of
671 * vnodes to operate upon.
673 return afs_find_cm_server_by_peer(call);