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 call->epoch = rxrpc_kernel_get_epoch(call->net->socket, call->rxcall);
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 * Record a probe to the cache manager from a server.
144 static int afs_record_cm_probe(struct afs_call *call, struct afs_server *server)
148 if (test_bit(AFS_SERVER_FL_HAVE_EPOCH, &server->flags) &&
149 !test_bit(AFS_SERVER_FL_PROBING, &server->flags)) {
150 if (server->cm_epoch == call->epoch)
153 if (!server->probe.said_rebooted) {
154 pr_notice("kAFS: FS rebooted %pU\n", &server->uuid);
155 server->probe.said_rebooted = true;
159 spin_lock(&server->probe_lock);
161 if (!test_and_set_bit(AFS_SERVER_FL_HAVE_EPOCH, &server->flags)) {
162 server->cm_epoch = call->epoch;
163 server->probe.cm_epoch = call->epoch;
167 if (server->probe.cm_probed &&
168 call->epoch != server->probe.cm_epoch &&
169 !server->probe.said_inconsistent) {
170 pr_notice("kAFS: FS endpoints inconsistent %pU\n",
172 server->probe.said_inconsistent = true;
175 if (!server->probe.cm_probed || call->epoch == server->cm_epoch)
176 server->probe.cm_epoch = server->cm_epoch;
179 server->probe.cm_probed = true;
180 spin_unlock(&server->probe_lock);
185 * Find the server record by peer address and record a probe to the cache
186 * manager from a server.
188 static int afs_find_cm_server_by_peer(struct afs_call *call)
190 struct sockaddr_rxrpc srx;
191 struct afs_server *server;
193 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
195 server = afs_find_server(call->net, &srx);
197 trace_afs_cm_no_server(call, &srx);
201 call->server = server;
202 return afs_record_cm_probe(call, server);
206 * Find the server record by server UUID and record a probe to the cache
207 * manager from a server.
209 static int afs_find_cm_server_by_uuid(struct afs_call *call,
210 struct afs_uuid *uuid)
212 struct afs_server *server;
215 server = afs_find_server_by_uuid(call->net, call->request);
218 trace_afs_cm_no_server_u(call, call->request);
222 call->server = server;
223 return afs_record_cm_probe(call, server);
227 * Clean up a cache manager call.
229 static void afs_cm_destructor(struct afs_call *call)
236 * Abort a service call from within an action function.
238 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
241 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
242 abort_code, error, why);
243 afs_set_call_complete(call, error, 0);
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 afs_abort_service_call(call, 1, 1, "K-1");
520 * deliver request data to a CB.ProbeUuid call
522 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
529 _enter("{%u}", call->unmarshall);
531 switch (call->unmarshall) {
533 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
536 afs_extract_to_buf(call, 11 * sizeof(__be32));
541 _debug("extract UUID");
542 ret = afs_extract_data(call, false);
545 case -EAGAIN: return 0;
549 _debug("unmarshall UUID");
550 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
557 r->time_mid = htons(ntohl(b[1]));
558 r->time_hi_and_version = htons(ntohl(b[2]));
559 r->clock_seq_hi_and_reserved = ntohl(b[3]);
560 r->clock_seq_low = ntohl(b[4]);
562 for (loop = 0; loop < 6; loop++)
563 r->node[loop] = ntohl(b[loop + 5]);
571 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
572 return afs_io_error(call, afs_io_error_cm_reply);
573 return afs_find_cm_server_by_uuid(call, call->request);
577 * allow the fileserver to ask about the cache manager's capabilities
579 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
581 struct afs_call *call = container_of(work, struct afs_call, work);
585 struct /* InterfaceAddr */ {
592 struct /* Capabilities */ {
600 memset(&reply, 0, sizeof(reply));
602 reply.ia.uuid[0] = call->net->uuid.time_low;
603 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
604 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
605 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
606 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
607 for (loop = 0; loop < 6; loop++)
608 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
610 reply.cap.capcount = htonl(1);
611 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
612 afs_send_simple_reply(call, &reply, sizeof(reply));
618 * deliver request data to a CB.TellMeAboutYourself call
620 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
626 afs_extract_discard(call, 0);
627 ret = afs_extract_data(call, false);
631 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
632 return afs_io_error(call, afs_io_error_cm_reply);
633 return afs_find_cm_server_by_peer(call);
637 * deliver request data to a YFS CB.CallBack call
639 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
641 struct afs_callback_break *cb;
642 struct yfs_xdr_YFSFid *bp;
646 _enter("{%u}", call->unmarshall);
648 switch (call->unmarshall) {
650 afs_extract_to_tmp(call);
653 /* extract the FID array and its count in two steps */
656 _debug("extract FID count");
657 ret = afs_extract_data(call, true);
661 call->count = ntohl(call->tmp);
662 _debug("FID count: %u", call->count);
663 if (call->count > YFSCBMAX)
664 return afs_protocol_error(call, -EBADMSG,
665 afs_eproto_cb_fid_count);
667 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
668 call->buffer = kmalloc(size, GFP_KERNEL);
671 afs_extract_to_buf(call, size);
676 _debug("extract FID array");
677 ret = afs_extract_data(call, false);
681 _debug("unmarshall FID array");
682 call->request = kcalloc(call->count,
683 sizeof(struct afs_callback_break),
690 for (loop = call->count; loop > 0; loop--, cb++) {
691 cb->fid.vid = xdr_to_u64(bp->volume);
692 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
693 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
694 cb->fid.unique = ntohl(bp->vnode.unique);
698 afs_extract_to_tmp(call);
705 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
706 return afs_io_error(call, afs_io_error_cm_reply);
708 /* We'll need the file server record as that tells us which set of
709 * vnodes to operate upon.
711 return afs_find_cm_server_by_peer(call);