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 * Abort a service call from within an action function.
249 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
252 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
253 abort_code, error, why);
254 afs_set_call_complete(call, error, 0);
258 * The server supplied a list of callbacks that it wanted to break.
260 static void SRXAFSCB_CallBack(struct work_struct *work)
262 struct afs_call *call = container_of(work, struct afs_call, work);
266 /* We need to break the callbacks before sending the reply as the
267 * server holds up change visibility till it receives our reply so as
268 * to maintain cache coherency.
271 trace_afs_server(call->server, atomic_read(&call->server->usage),
272 afs_server_trace_callback);
273 afs_break_callbacks(call->server, call->count, call->request);
276 afs_send_empty_reply(call);
282 * deliver request data to a CB.CallBack call
284 static int afs_deliver_cb_callback(struct afs_call *call)
286 struct afs_callback_break *cb;
290 _enter("{%u}", call->unmarshall);
292 switch (call->unmarshall) {
294 afs_extract_to_tmp(call);
297 /* extract the FID array and its count in two steps */
300 _debug("extract FID count");
301 ret = afs_extract_data(call, true);
305 call->count = ntohl(call->tmp);
306 _debug("FID count: %u", call->count);
307 if (call->count > AFSCBMAX)
308 return afs_protocol_error(call, -EBADMSG,
309 afs_eproto_cb_fid_count);
311 call->buffer = kmalloc(array3_size(call->count, 3, 4),
315 afs_extract_to_buf(call, call->count * 3 * 4);
320 _debug("extract FID array");
321 ret = afs_extract_data(call, true);
325 _debug("unmarshall FID array");
326 call->request = kcalloc(call->count,
327 sizeof(struct afs_callback_break),
334 for (loop = call->count; loop > 0; loop--, cb++) {
335 cb->fid.vid = ntohl(*bp++);
336 cb->fid.vnode = ntohl(*bp++);
337 cb->fid.unique = ntohl(*bp++);
340 afs_extract_to_tmp(call);
343 /* extract the callback array and its count in two steps */
346 _debug("extract CB count");
347 ret = afs_extract_data(call, true);
351 call->count2 = ntohl(call->tmp);
352 _debug("CB count: %u", call->count2);
353 if (call->count2 != call->count && call->count2 != 0)
354 return afs_protocol_error(call, -EBADMSG,
355 afs_eproto_cb_count);
356 call->iter = &call->def_iter;
357 iov_iter_discard(&call->def_iter, READ, call->count2 * 3 * 4);
362 _debug("extract discard %zu/%u",
363 iov_iter_count(call->iter), call->count2 * 3 * 4);
365 ret = afs_extract_data(call, false);
374 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
375 return afs_io_error(call, afs_io_error_cm_reply);
377 /* we'll need the file server record as that tells us which set of
378 * vnodes to operate upon */
379 return afs_find_cm_server_by_peer(call);
383 * allow the fileserver to request callback state (re-)initialisation
385 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
387 struct afs_call *call = container_of(work, struct afs_call, work);
389 _enter("{%p}", call->server);
392 afs_init_callback_state(call->server);
393 afs_send_empty_reply(call);
399 * deliver request data to a CB.InitCallBackState call
401 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
407 afs_extract_discard(call, 0);
408 ret = afs_extract_data(call, false);
412 /* we'll need the file server record as that tells us which set of
413 * vnodes to operate upon */
414 return afs_find_cm_server_by_peer(call);
418 * deliver request data to a CB.InitCallBackState3 call
420 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
429 _enter("{%u}", call->unmarshall);
431 switch (call->unmarshall) {
433 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
436 afs_extract_to_buf(call, 11 * sizeof(__be32));
441 _debug("extract UUID");
442 ret = afs_extract_data(call, false);
445 case -EAGAIN: return 0;
449 _debug("unmarshall UUID");
450 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
457 r->time_mid = htons(ntohl(b[1]));
458 r->time_hi_and_version = htons(ntohl(b[2]));
459 r->clock_seq_hi_and_reserved = ntohl(b[3]);
460 r->clock_seq_low = ntohl(b[4]);
462 for (loop = 0; loop < 6; loop++)
463 r->node[loop] = ntohl(b[loop + 5]);
471 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
472 return afs_io_error(call, afs_io_error_cm_reply);
474 /* we'll need the file server record as that tells us which set of
475 * vnodes to operate upon */
476 return afs_find_cm_server_by_uuid(call, call->request);
480 * allow the fileserver to see if the cache manager is still alive
482 static void SRXAFSCB_Probe(struct work_struct *work)
484 struct afs_call *call = container_of(work, struct afs_call, work);
487 afs_send_empty_reply(call);
493 * deliver request data to a CB.Probe call
495 static int afs_deliver_cb_probe(struct afs_call *call)
501 afs_extract_discard(call, 0);
502 ret = afs_extract_data(call, false);
506 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
507 return afs_io_error(call, afs_io_error_cm_reply);
508 return afs_find_cm_server_by_peer(call);
512 * allow the fileserver to quickly find out if the fileserver has been rebooted
514 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
516 struct afs_call *call = container_of(work, struct afs_call, work);
517 struct afs_uuid *r = call->request;
521 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
522 afs_send_empty_reply(call);
524 afs_abort_service_call(call, 1, 1, "K-1");
531 * deliver request data to a CB.ProbeUuid call
533 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
540 _enter("{%u}", call->unmarshall);
542 switch (call->unmarshall) {
544 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
547 afs_extract_to_buf(call, 11 * sizeof(__be32));
552 _debug("extract UUID");
553 ret = afs_extract_data(call, false);
556 case -EAGAIN: return 0;
560 _debug("unmarshall UUID");
561 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
568 r->time_mid = htons(ntohl(b[1]));
569 r->time_hi_and_version = htons(ntohl(b[2]));
570 r->clock_seq_hi_and_reserved = ntohl(b[3]);
571 r->clock_seq_low = ntohl(b[4]);
573 for (loop = 0; loop < 6; loop++)
574 r->node[loop] = ntohl(b[loop + 5]);
582 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
583 return afs_io_error(call, afs_io_error_cm_reply);
584 return afs_find_cm_server_by_uuid(call, call->request);
588 * allow the fileserver to ask about the cache manager's capabilities
590 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
592 struct afs_call *call = container_of(work, struct afs_call, work);
596 struct /* InterfaceAddr */ {
603 struct /* Capabilities */ {
611 memset(&reply, 0, sizeof(reply));
613 reply.ia.uuid[0] = call->net->uuid.time_low;
614 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
615 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
616 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
617 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
618 for (loop = 0; loop < 6; loop++)
619 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
621 reply.cap.capcount = htonl(1);
622 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
623 afs_send_simple_reply(call, &reply, sizeof(reply));
629 * deliver request data to a CB.TellMeAboutYourself call
631 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
637 afs_extract_discard(call, 0);
638 ret = afs_extract_data(call, false);
642 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
643 return afs_io_error(call, afs_io_error_cm_reply);
644 return afs_find_cm_server_by_peer(call);
648 * deliver request data to a YFS CB.CallBack call
650 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
652 struct afs_callback_break *cb;
653 struct yfs_xdr_YFSFid *bp;
657 _enter("{%u}", call->unmarshall);
659 switch (call->unmarshall) {
661 afs_extract_to_tmp(call);
664 /* extract the FID array and its count in two steps */
667 _debug("extract FID count");
668 ret = afs_extract_data(call, true);
672 call->count = ntohl(call->tmp);
673 _debug("FID count: %u", call->count);
674 if (call->count > YFSCBMAX)
675 return afs_protocol_error(call, -EBADMSG,
676 afs_eproto_cb_fid_count);
678 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
679 call->buffer = kmalloc(size, GFP_KERNEL);
682 afs_extract_to_buf(call, size);
687 _debug("extract FID array");
688 ret = afs_extract_data(call, false);
692 _debug("unmarshall FID array");
693 call->request = kcalloc(call->count,
694 sizeof(struct afs_callback_break),
701 for (loop = call->count; loop > 0; loop--, cb++) {
702 cb->fid.vid = xdr_to_u64(bp->volume);
703 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
704 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
705 cb->fid.unique = ntohl(bp->vnode.unique);
709 afs_extract_to_tmp(call);
716 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
717 return afs_io_error(call, afs_io_error_cm_reply);
719 /* We'll need the file server record as that tells us which set of
720 * vnodes to operate upon.
722 return afs_find_cm_server_by_peer(call);