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 switch (call->operation_ID) {
112 call->type = &afs_SRXCBCallBack;
114 case CBInitCallBackState:
115 call->type = &afs_SRXCBInitCallBackState;
117 case CBInitCallBackState3:
118 call->type = &afs_SRXCBInitCallBackState3;
121 call->type = &afs_SRXCBProbe;
124 call->type = &afs_SRXCBProbeUuid;
126 case CBTellMeAboutYourself:
127 call->type = &afs_SRXCBTellMeAboutYourself;
130 if (call->service_id != YFS_CM_SERVICE)
132 call->type = &afs_SRXYFSCB_CallBack;
140 * Find the server record by peer address and record a probe to the cache
141 * manager from a server.
143 static int afs_find_cm_server_by_peer(struct afs_call *call)
145 struct sockaddr_rxrpc srx;
146 struct afs_server *server;
148 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
150 server = afs_find_server(call->net, &srx);
152 trace_afs_cm_no_server(call, &srx);
156 call->server = server;
161 * Find the server record by server UUID and record a probe to the cache
162 * manager from a server.
164 static int afs_find_cm_server_by_uuid(struct afs_call *call,
165 struct afs_uuid *uuid)
167 struct afs_server *server;
170 server = afs_find_server_by_uuid(call->net, call->request);
173 trace_afs_cm_no_server_u(call, call->request);
177 call->server = server;
182 * Clean up a cache manager call.
184 static void afs_cm_destructor(struct afs_call *call)
191 * Abort a service call from within an action function.
193 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
196 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
197 abort_code, error, why);
198 afs_set_call_complete(call, error, 0);
202 * The server supplied a list of callbacks that it wanted to break.
204 static void SRXAFSCB_CallBack(struct work_struct *work)
206 struct afs_call *call = container_of(work, struct afs_call, work);
210 /* We need to break the callbacks before sending the reply as the
211 * server holds up change visibility till it receives our reply so as
212 * to maintain cache coherency.
215 trace_afs_server(call->server,
216 atomic_read(&call->server->ref),
217 atomic_read(&call->server->active),
218 afs_server_trace_callback);
219 afs_break_callbacks(call->server, call->count, call->request);
222 afs_send_empty_reply(call);
228 * deliver request data to a CB.CallBack call
230 static int afs_deliver_cb_callback(struct afs_call *call)
232 struct afs_callback_break *cb;
236 _enter("{%u}", call->unmarshall);
238 switch (call->unmarshall) {
240 afs_extract_to_tmp(call);
243 /* extract the FID array and its count in two steps */
246 _debug("extract FID count");
247 ret = afs_extract_data(call, true);
251 call->count = ntohl(call->tmp);
252 _debug("FID count: %u", call->count);
253 if (call->count > AFSCBMAX)
254 return afs_protocol_error(call, afs_eproto_cb_fid_count);
256 call->buffer = kmalloc(array3_size(call->count, 3, 4),
260 afs_extract_to_buf(call, call->count * 3 * 4);
265 _debug("extract FID array");
266 ret = afs_extract_data(call, true);
270 _debug("unmarshall FID array");
271 call->request = kcalloc(call->count,
272 sizeof(struct afs_callback_break),
279 for (loop = call->count; loop > 0; loop--, cb++) {
280 cb->fid.vid = ntohl(*bp++);
281 cb->fid.vnode = ntohl(*bp++);
282 cb->fid.unique = ntohl(*bp++);
285 afs_extract_to_tmp(call);
288 /* extract the callback array and its count in two steps */
291 _debug("extract CB count");
292 ret = afs_extract_data(call, true);
296 call->count2 = ntohl(call->tmp);
297 _debug("CB count: %u", call->count2);
298 if (call->count2 != call->count && call->count2 != 0)
299 return afs_protocol_error(call, afs_eproto_cb_count);
300 call->iter = &call->def_iter;
301 iov_iter_discard(&call->def_iter, READ, call->count2 * 3 * 4);
306 _debug("extract discard %zu/%u",
307 iov_iter_count(call->iter), call->count2 * 3 * 4);
309 ret = afs_extract_data(call, false);
318 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
319 return afs_io_error(call, afs_io_error_cm_reply);
321 /* we'll need the file server record as that tells us which set of
322 * vnodes to operate upon */
323 return afs_find_cm_server_by_peer(call);
327 * allow the fileserver to request callback state (re-)initialisation
329 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
331 struct afs_call *call = container_of(work, struct afs_call, work);
333 _enter("{%p}", call->server);
336 afs_init_callback_state(call->server);
337 afs_send_empty_reply(call);
343 * deliver request data to a CB.InitCallBackState call
345 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
351 afs_extract_discard(call, 0);
352 ret = afs_extract_data(call, false);
356 /* we'll need the file server record as that tells us which set of
357 * vnodes to operate upon */
358 return afs_find_cm_server_by_peer(call);
362 * deliver request data to a CB.InitCallBackState3 call
364 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
373 _enter("{%u}", call->unmarshall);
375 switch (call->unmarshall) {
377 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
380 afs_extract_to_buf(call, 11 * sizeof(__be32));
385 _debug("extract UUID");
386 ret = afs_extract_data(call, false);
389 case -EAGAIN: return 0;
393 _debug("unmarshall UUID");
394 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
401 r->time_mid = htons(ntohl(b[1]));
402 r->time_hi_and_version = htons(ntohl(b[2]));
403 r->clock_seq_hi_and_reserved = ntohl(b[3]);
404 r->clock_seq_low = ntohl(b[4]);
406 for (loop = 0; loop < 6; loop++)
407 r->node[loop] = ntohl(b[loop + 5]);
415 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
416 return afs_io_error(call, afs_io_error_cm_reply);
418 /* we'll need the file server record as that tells us which set of
419 * vnodes to operate upon */
420 return afs_find_cm_server_by_uuid(call, call->request);
424 * allow the fileserver to see if the cache manager is still alive
426 static void SRXAFSCB_Probe(struct work_struct *work)
428 struct afs_call *call = container_of(work, struct afs_call, work);
431 afs_send_empty_reply(call);
437 * deliver request data to a CB.Probe call
439 static int afs_deliver_cb_probe(struct afs_call *call)
445 afs_extract_discard(call, 0);
446 ret = afs_extract_data(call, false);
450 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
451 return afs_io_error(call, afs_io_error_cm_reply);
452 return afs_find_cm_server_by_peer(call);
456 * Allow the fileserver to quickly find out if the cache manager has been
459 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
461 struct afs_call *call = container_of(work, struct afs_call, work);
462 struct afs_uuid *r = call->request;
466 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
467 afs_send_empty_reply(call);
469 afs_abort_service_call(call, 1, 1, "K-1");
476 * deliver request data to a CB.ProbeUuid call
478 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
485 _enter("{%u}", call->unmarshall);
487 switch (call->unmarshall) {
489 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
492 afs_extract_to_buf(call, 11 * sizeof(__be32));
497 _debug("extract UUID");
498 ret = afs_extract_data(call, false);
501 case -EAGAIN: return 0;
505 _debug("unmarshall UUID");
506 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
513 r->time_mid = htons(ntohl(b[1]));
514 r->time_hi_and_version = htons(ntohl(b[2]));
515 r->clock_seq_hi_and_reserved = ntohl(b[3]);
516 r->clock_seq_low = ntohl(b[4]);
518 for (loop = 0; loop < 6; loop++)
519 r->node[loop] = ntohl(b[loop + 5]);
527 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
528 return afs_io_error(call, afs_io_error_cm_reply);
529 return afs_find_cm_server_by_peer(call);
533 * allow the fileserver to ask about the cache manager's capabilities
535 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
537 struct afs_call *call = container_of(work, struct afs_call, work);
541 struct /* InterfaceAddr */ {
548 struct /* Capabilities */ {
556 memset(&reply, 0, sizeof(reply));
558 reply.ia.uuid[0] = call->net->uuid.time_low;
559 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
560 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
561 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
562 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
563 for (loop = 0; loop < 6; loop++)
564 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
566 reply.cap.capcount = htonl(1);
567 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
568 afs_send_simple_reply(call, &reply, sizeof(reply));
574 * deliver request data to a CB.TellMeAboutYourself call
576 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
582 afs_extract_discard(call, 0);
583 ret = afs_extract_data(call, false);
587 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
588 return afs_io_error(call, afs_io_error_cm_reply);
589 return afs_find_cm_server_by_peer(call);
593 * deliver request data to a YFS CB.CallBack call
595 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
597 struct afs_callback_break *cb;
598 struct yfs_xdr_YFSFid *bp;
602 _enter("{%u}", call->unmarshall);
604 switch (call->unmarshall) {
606 afs_extract_to_tmp(call);
609 /* extract the FID array and its count in two steps */
612 _debug("extract FID count");
613 ret = afs_extract_data(call, true);
617 call->count = ntohl(call->tmp);
618 _debug("FID count: %u", call->count);
619 if (call->count > YFSCBMAX)
620 return afs_protocol_error(call, afs_eproto_cb_fid_count);
622 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
623 call->buffer = kmalloc(size, GFP_KERNEL);
626 afs_extract_to_buf(call, size);
631 _debug("extract FID array");
632 ret = afs_extract_data(call, false);
636 _debug("unmarshall FID array");
637 call->request = kcalloc(call->count,
638 sizeof(struct afs_callback_break),
645 for (loop = call->count; loop > 0; loop--, cb++) {
646 cb->fid.vid = xdr_to_u64(bp->volume);
647 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
648 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
649 cb->fid.unique = ntohl(bp->vnode.unique);
653 afs_extract_to_tmp(call);
660 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
661 return afs_io_error(call, afs_io_error_cm_reply);
663 /* We'll need the file server record as that tells us which set of
664 * vnodes to operate upon.
666 return afs_find_cm_server_by_peer(call);