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 call->epoch = rxrpc_kernel_get_epoch(call->net->socket, call->rxcall);
123 switch (call->operation_ID) {
125 call->type = &afs_SRXCBCallBack;
127 case CBInitCallBackState:
128 call->type = &afs_SRXCBInitCallBackState;
130 case CBInitCallBackState3:
131 call->type = &afs_SRXCBInitCallBackState3;
134 call->type = &afs_SRXCBProbe;
137 call->type = &afs_SRXCBProbeUuid;
139 case CBTellMeAboutYourself:
140 call->type = &afs_SRXCBTellMeAboutYourself;
143 if (call->service_id != YFS_CM_SERVICE)
145 call->type = &afs_SRXYFSCB_CallBack;
153 * Record a probe to the cache manager from a server.
155 static int afs_record_cm_probe(struct afs_call *call, struct afs_server *server)
159 if (test_bit(AFS_SERVER_FL_HAVE_EPOCH, &server->flags) &&
160 !test_bit(AFS_SERVER_FL_PROBING, &server->flags)) {
161 if (server->cm_epoch == call->epoch)
164 if (!server->probe.said_rebooted) {
165 pr_notice("kAFS: FS rebooted %pU\n", &server->uuid);
166 server->probe.said_rebooted = true;
170 spin_lock(&server->probe_lock);
172 if (!test_bit(AFS_SERVER_FL_HAVE_EPOCH, &server->flags)) {
173 server->cm_epoch = call->epoch;
174 server->probe.cm_epoch = call->epoch;
178 if (server->probe.cm_probed &&
179 call->epoch != server->probe.cm_epoch &&
180 !server->probe.said_inconsistent) {
181 pr_notice("kAFS: FS endpoints inconsistent %pU\n",
183 server->probe.said_inconsistent = true;
186 if (!server->probe.cm_probed || call->epoch == server->cm_epoch)
187 server->probe.cm_epoch = server->cm_epoch;
190 server->probe.cm_probed = true;
191 spin_unlock(&server->probe_lock);
196 * Find the server record by peer address and record a probe to the cache
197 * manager from a server.
199 static int afs_find_cm_server_by_peer(struct afs_call *call)
201 struct sockaddr_rxrpc srx;
202 struct afs_server *server;
204 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
206 server = afs_find_server(call->net, &srx);
208 trace_afs_cm_no_server(call, &srx);
212 call->server = server;
213 return afs_record_cm_probe(call, server);
217 * Find the server record by server UUID and record a probe to the cache
218 * manager from a server.
220 static int afs_find_cm_server_by_uuid(struct afs_call *call,
221 struct afs_uuid *uuid)
223 struct afs_server *server;
226 server = afs_find_server_by_uuid(call->net, call->request);
229 trace_afs_cm_no_server_u(call, call->request);
233 call->server = server;
234 return afs_record_cm_probe(call, server);
238 * Clean up a cache manager call.
240 static void afs_cm_destructor(struct afs_call *call)
247 * The server supplied a list of callbacks that it wanted to break.
249 static void SRXAFSCB_CallBack(struct work_struct *work)
251 struct afs_call *call = container_of(work, struct afs_call, work);
255 /* We need to break the callbacks before sending the reply as the
256 * server holds up change visibility till it receives our reply so as
257 * to maintain cache coherency.
260 trace_afs_server(call->server, atomic_read(&call->server->usage),
261 afs_server_trace_callback);
262 afs_break_callbacks(call->server, call->count, call->request);
265 afs_send_empty_reply(call);
271 * deliver request data to a CB.CallBack call
273 static int afs_deliver_cb_callback(struct afs_call *call)
275 struct afs_callback_break *cb;
279 _enter("{%u}", call->unmarshall);
281 switch (call->unmarshall) {
283 afs_extract_to_tmp(call);
286 /* extract the FID array and its count in two steps */
289 _debug("extract FID count");
290 ret = afs_extract_data(call, true);
294 call->count = ntohl(call->tmp);
295 _debug("FID count: %u", call->count);
296 if (call->count > AFSCBMAX)
297 return afs_protocol_error(call, -EBADMSG,
298 afs_eproto_cb_fid_count);
300 call->buffer = kmalloc(array3_size(call->count, 3, 4),
304 afs_extract_to_buf(call, call->count * 3 * 4);
309 _debug("extract FID array");
310 ret = afs_extract_data(call, true);
314 _debug("unmarshall FID array");
315 call->request = kcalloc(call->count,
316 sizeof(struct afs_callback_break),
323 for (loop = call->count; loop > 0; loop--, cb++) {
324 cb->fid.vid = ntohl(*bp++);
325 cb->fid.vnode = ntohl(*bp++);
326 cb->fid.unique = ntohl(*bp++);
329 afs_extract_to_tmp(call);
332 /* extract the callback array and its count in two steps */
335 _debug("extract CB count");
336 ret = afs_extract_data(call, true);
340 call->count2 = ntohl(call->tmp);
341 _debug("CB count: %u", call->count2);
342 if (call->count2 != call->count && call->count2 != 0)
343 return afs_protocol_error(call, -EBADMSG,
344 afs_eproto_cb_count);
345 call->_iter = &call->iter;
346 iov_iter_discard(&call->iter, READ, call->count2 * 3 * 4);
351 _debug("extract discard %zu/%u",
352 iov_iter_count(&call->iter), call->count2 * 3 * 4);
354 ret = afs_extract_data(call, false);
363 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
364 return afs_io_error(call, afs_io_error_cm_reply);
366 /* we'll need the file server record as that tells us which set of
367 * vnodes to operate upon */
368 return afs_find_cm_server_by_peer(call);
372 * allow the fileserver to request callback state (re-)initialisation
374 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
376 struct afs_call *call = container_of(work, struct afs_call, work);
378 _enter("{%p}", call->server);
381 afs_init_callback_state(call->server);
382 afs_send_empty_reply(call);
388 * deliver request data to a CB.InitCallBackState call
390 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
396 afs_extract_discard(call, 0);
397 ret = afs_extract_data(call, false);
401 /* we'll need the file server record as that tells us which set of
402 * vnodes to operate upon */
403 return afs_find_cm_server_by_peer(call);
407 * deliver request data to a CB.InitCallBackState3 call
409 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
418 _enter("{%u}", call->unmarshall);
420 switch (call->unmarshall) {
422 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
425 afs_extract_to_buf(call, 11 * sizeof(__be32));
430 _debug("extract UUID");
431 ret = afs_extract_data(call, false);
434 case -EAGAIN: return 0;
438 _debug("unmarshall UUID");
439 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
446 r->time_mid = htons(ntohl(b[1]));
447 r->time_hi_and_version = htons(ntohl(b[2]));
448 r->clock_seq_hi_and_reserved = ntohl(b[3]);
449 r->clock_seq_low = ntohl(b[4]);
451 for (loop = 0; loop < 6; loop++)
452 r->node[loop] = ntohl(b[loop + 5]);
460 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
461 return afs_io_error(call, afs_io_error_cm_reply);
463 /* we'll need the file server record as that tells us which set of
464 * vnodes to operate upon */
465 return afs_find_cm_server_by_uuid(call, call->request);
469 * allow the fileserver to see if the cache manager is still alive
471 static void SRXAFSCB_Probe(struct work_struct *work)
473 struct afs_call *call = container_of(work, struct afs_call, work);
476 afs_send_empty_reply(call);
482 * deliver request data to a CB.Probe call
484 static int afs_deliver_cb_probe(struct afs_call *call)
490 afs_extract_discard(call, 0);
491 ret = afs_extract_data(call, false);
495 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
496 return afs_io_error(call, afs_io_error_cm_reply);
497 return afs_find_cm_server_by_peer(call);
501 * allow the fileserver to quickly find out if the fileserver has been rebooted
503 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
505 struct afs_call *call = container_of(work, struct afs_call, work);
506 struct afs_uuid *r = call->request;
510 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
511 afs_send_empty_reply(call);
513 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
521 * deliver request data to a CB.ProbeUuid call
523 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
530 _enter("{%u}", call->unmarshall);
532 switch (call->unmarshall) {
534 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
537 afs_extract_to_buf(call, 11 * sizeof(__be32));
542 _debug("extract UUID");
543 ret = afs_extract_data(call, false);
546 case -EAGAIN: return 0;
550 _debug("unmarshall UUID");
551 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
558 r->time_mid = htons(ntohl(b[1]));
559 r->time_hi_and_version = htons(ntohl(b[2]));
560 r->clock_seq_hi_and_reserved = ntohl(b[3]);
561 r->clock_seq_low = ntohl(b[4]);
563 for (loop = 0; loop < 6; loop++)
564 r->node[loop] = ntohl(b[loop + 5]);
572 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
573 return afs_io_error(call, afs_io_error_cm_reply);
574 return afs_find_cm_server_by_uuid(call, call->request);
578 * allow the fileserver to ask about the cache manager's capabilities
580 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
582 struct afs_call *call = container_of(work, struct afs_call, work);
586 struct /* InterfaceAddr */ {
593 struct /* Capabilities */ {
601 memset(&reply, 0, sizeof(reply));
603 reply.ia.uuid[0] = call->net->uuid.time_low;
604 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
605 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
606 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
607 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
608 for (loop = 0; loop < 6; loop++)
609 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
611 reply.cap.capcount = htonl(1);
612 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
613 afs_send_simple_reply(call, &reply, sizeof(reply));
619 * deliver request data to a CB.TellMeAboutYourself call
621 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
627 afs_extract_discard(call, 0);
628 ret = afs_extract_data(call, false);
632 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
633 return afs_io_error(call, afs_io_error_cm_reply);
634 return afs_find_cm_server_by_peer(call);
638 * deliver request data to a YFS CB.CallBack call
640 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
642 struct afs_callback_break *cb;
643 struct yfs_xdr_YFSFid *bp;
647 _enter("{%u}", call->unmarshall);
649 switch (call->unmarshall) {
651 afs_extract_to_tmp(call);
654 /* extract the FID array and its count in two steps */
657 _debug("extract FID count");
658 ret = afs_extract_data(call, true);
662 call->count = ntohl(call->tmp);
663 _debug("FID count: %u", call->count);
664 if (call->count > YFSCBMAX)
665 return afs_protocol_error(call, -EBADMSG,
666 afs_eproto_cb_fid_count);
668 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
669 call->buffer = kmalloc(size, GFP_KERNEL);
672 afs_extract_to_buf(call, size);
677 _debug("extract FID array");
678 ret = afs_extract_data(call, false);
682 _debug("unmarshall FID array");
683 call->request = kcalloc(call->count,
684 sizeof(struct afs_callback_break),
691 for (loop = call->count; loop > 0; loop--, cb++) {
692 cb->fid.vid = xdr_to_u64(bp->volume);
693 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
694 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
695 cb->fid.unique = ntohl(bp->vnode.unique);
699 afs_extract_to_tmp(call);
706 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
707 return afs_io_error(call, afs_io_error_cm_reply);
709 /* We'll need the file server record as that tells us which set of
710 * vnodes to operate upon.
712 return afs_find_cm_server_by_peer(call);