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;
149 struct rxrpc_peer *peer;
151 peer = rxrpc_kernel_get_call_peer(call->net->socket, call->rxcall);
153 server = afs_find_server(call->net, peer);
155 trace_afs_cm_no_server(call, &srx);
159 call->server = server;
164 * Find the server record by server UUID and record a probe to the cache
165 * manager from a server.
167 static int afs_find_cm_server_by_uuid(struct afs_call *call,
168 struct afs_uuid *uuid)
170 struct afs_server *server;
173 server = afs_find_server_by_uuid(call->net, call->request);
176 trace_afs_cm_no_server_u(call, call->request);
180 call->server = server;
185 * Clean up a cache manager call.
187 static void afs_cm_destructor(struct afs_call *call)
194 * Abort a service call from within an action function.
196 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
197 enum rxrpc_abort_reason why)
199 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
200 abort_code, error, why);
201 afs_set_call_complete(call, error, 0);
205 * The server supplied a list of callbacks that it wanted to break.
207 static void SRXAFSCB_CallBack(struct work_struct *work)
209 struct afs_call *call = container_of(work, struct afs_call, work);
213 /* We need to break the callbacks before sending the reply as the
214 * server holds up change visibility till it receives our reply so as
215 * to maintain cache coherency.
218 trace_afs_server(call->server->debug_id,
219 refcount_read(&call->server->ref),
220 atomic_read(&call->server->active),
221 afs_server_trace_callback);
222 afs_break_callbacks(call->server, call->count, call->request);
225 afs_send_empty_reply(call);
231 * deliver request data to a CB.CallBack call
233 static int afs_deliver_cb_callback(struct afs_call *call)
235 struct afs_callback_break *cb;
239 _enter("{%u}", call->unmarshall);
241 switch (call->unmarshall) {
243 afs_extract_to_tmp(call);
246 /* extract the FID array and its count in two steps */
249 _debug("extract FID count");
250 ret = afs_extract_data(call, true);
254 call->count = ntohl(call->tmp);
255 _debug("FID count: %u", call->count);
256 if (call->count > AFSCBMAX)
257 return afs_protocol_error(call, afs_eproto_cb_fid_count);
259 call->buffer = kmalloc(array3_size(call->count, 3, 4),
263 afs_extract_to_buf(call, call->count * 3 * 4);
268 _debug("extract FID array");
269 ret = afs_extract_data(call, true);
273 _debug("unmarshall FID array");
274 call->request = kcalloc(call->count,
275 sizeof(struct afs_callback_break),
282 for (loop = call->count; loop > 0; loop--, cb++) {
283 cb->fid.vid = ntohl(*bp++);
284 cb->fid.vnode = ntohl(*bp++);
285 cb->fid.unique = ntohl(*bp++);
288 afs_extract_to_tmp(call);
291 /* extract the callback array and its count in two steps */
294 _debug("extract CB count");
295 ret = afs_extract_data(call, true);
299 call->count2 = ntohl(call->tmp);
300 _debug("CB count: %u", call->count2);
301 if (call->count2 != call->count && call->count2 != 0)
302 return afs_protocol_error(call, afs_eproto_cb_count);
303 call->iter = &call->def_iter;
304 iov_iter_discard(&call->def_iter, ITER_DEST, call->count2 * 3 * 4);
309 _debug("extract discard %zu/%u",
310 iov_iter_count(call->iter), call->count2 * 3 * 4);
312 ret = afs_extract_data(call, false);
323 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
324 return afs_io_error(call, afs_io_error_cm_reply);
326 /* we'll need the file server record as that tells us which set of
327 * vnodes to operate upon */
328 return afs_find_cm_server_by_peer(call);
332 * allow the fileserver to request callback state (re-)initialisation
334 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
336 struct afs_call *call = container_of(work, struct afs_call, work);
338 _enter("{%p}", call->server);
341 afs_init_callback_state(call->server);
342 afs_send_empty_reply(call);
348 * deliver request data to a CB.InitCallBackState call
350 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
356 afs_extract_discard(call, 0);
357 ret = afs_extract_data(call, false);
361 /* we'll need the file server record as that tells us which set of
362 * vnodes to operate upon */
363 return afs_find_cm_server_by_peer(call);
367 * deliver request data to a CB.InitCallBackState3 call
369 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
378 _enter("{%u}", call->unmarshall);
380 switch (call->unmarshall) {
382 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
385 afs_extract_to_buf(call, 11 * sizeof(__be32));
390 _debug("extract UUID");
391 ret = afs_extract_data(call, false);
394 case -EAGAIN: return 0;
398 _debug("unmarshall UUID");
399 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
406 r->time_mid = htons(ntohl(b[1]));
407 r->time_hi_and_version = htons(ntohl(b[2]));
408 r->clock_seq_hi_and_reserved = ntohl(b[3]);
409 r->clock_seq_low = ntohl(b[4]);
411 for (loop = 0; loop < 6; loop++)
412 r->node[loop] = ntohl(b[loop + 5]);
421 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
422 return afs_io_error(call, afs_io_error_cm_reply);
424 /* we'll need the file server record as that tells us which set of
425 * vnodes to operate upon */
426 return afs_find_cm_server_by_uuid(call, call->request);
430 * allow the fileserver to see if the cache manager is still alive
432 static void SRXAFSCB_Probe(struct work_struct *work)
434 struct afs_call *call = container_of(work, struct afs_call, work);
437 afs_send_empty_reply(call);
443 * deliver request data to a CB.Probe call
445 static int afs_deliver_cb_probe(struct afs_call *call)
451 afs_extract_discard(call, 0);
452 ret = afs_extract_data(call, false);
456 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
457 return afs_io_error(call, afs_io_error_cm_reply);
458 return afs_find_cm_server_by_peer(call);
462 * Allow the fileserver to quickly find out if the cache manager has been
465 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
467 struct afs_call *call = container_of(work, struct afs_call, work);
468 struct afs_uuid *r = call->request;
472 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
473 afs_send_empty_reply(call);
475 afs_abort_service_call(call, 1, 1, afs_abort_probeuuid_negative);
482 * deliver request data to a CB.ProbeUuid call
484 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
491 _enter("{%u}", call->unmarshall);
493 switch (call->unmarshall) {
495 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
498 afs_extract_to_buf(call, 11 * sizeof(__be32));
503 _debug("extract UUID");
504 ret = afs_extract_data(call, false);
507 case -EAGAIN: return 0;
511 _debug("unmarshall UUID");
512 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
519 r->time_mid = htons(ntohl(b[1]));
520 r->time_hi_and_version = htons(ntohl(b[2]));
521 r->clock_seq_hi_and_reserved = ntohl(b[3]);
522 r->clock_seq_low = ntohl(b[4]);
524 for (loop = 0; loop < 6; loop++)
525 r->node[loop] = ntohl(b[loop + 5]);
534 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
535 return afs_io_error(call, afs_io_error_cm_reply);
536 return afs_find_cm_server_by_peer(call);
540 * allow the fileserver to ask about the cache manager's capabilities
542 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
544 struct afs_call *call = container_of(work, struct afs_call, work);
548 struct /* InterfaceAddr */ {
555 struct /* Capabilities */ {
563 memset(&reply, 0, sizeof(reply));
565 reply.ia.uuid[0] = call->net->uuid.time_low;
566 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
567 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
568 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
569 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
570 for (loop = 0; loop < 6; loop++)
571 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
573 reply.cap.capcount = htonl(1);
574 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
575 afs_send_simple_reply(call, &reply, sizeof(reply));
581 * deliver request data to a CB.TellMeAboutYourself call
583 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
589 afs_extract_discard(call, 0);
590 ret = afs_extract_data(call, false);
594 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
595 return afs_io_error(call, afs_io_error_cm_reply);
596 return afs_find_cm_server_by_peer(call);
600 * deliver request data to a YFS CB.CallBack call
602 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
604 struct afs_callback_break *cb;
605 struct yfs_xdr_YFSFid *bp;
609 _enter("{%u}", call->unmarshall);
611 switch (call->unmarshall) {
613 afs_extract_to_tmp(call);
616 /* extract the FID array and its count in two steps */
619 _debug("extract FID count");
620 ret = afs_extract_data(call, true);
624 call->count = ntohl(call->tmp);
625 _debug("FID count: %u", call->count);
626 if (call->count > YFSCBMAX)
627 return afs_protocol_error(call, afs_eproto_cb_fid_count);
629 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
630 call->buffer = kmalloc(size, GFP_KERNEL);
633 afs_extract_to_buf(call, size);
638 _debug("extract FID array");
639 ret = afs_extract_data(call, false);
643 _debug("unmarshall FID array");
644 call->request = kcalloc(call->count,
645 sizeof(struct afs_callback_break),
652 for (loop = call->count; loop > 0; loop--, cb++) {
653 cb->fid.vid = xdr_to_u64(bp->volume);
654 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
655 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
656 cb->fid.unique = ntohl(bp->vnode.unique);
660 afs_extract_to_tmp(call);
668 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
669 return afs_io_error(call, afs_io_error_cm_reply);
671 /* We'll need the file server record as that tells us which set of
672 * vnodes to operate upon.
674 return afs_find_cm_server_by_peer(call);