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 !afs_is_probing_server(server
)) {
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_and_set_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
,
272 atomic_read(&call
->server
->ref
),
273 atomic_read(&call
->server
->active
),
274 afs_server_trace_callback
);
275 afs_break_callbacks(call
->server
, call
->count
, call
->request
);
278 afs_send_empty_reply(call
);
284 * deliver request data to a CB.CallBack call
286 static int afs_deliver_cb_callback(struct afs_call
*call
)
288 struct afs_callback_break
*cb
;
292 _enter("{%u}", call
->unmarshall
);
294 switch (call
->unmarshall
) {
296 afs_extract_to_tmp(call
);
299 /* extract the FID array and its count in two steps */
302 _debug("extract FID count");
303 ret
= afs_extract_data(call
, true);
307 call
->count
= ntohl(call
->tmp
);
308 _debug("FID count: %u", call
->count
);
309 if (call
->count
> AFSCBMAX
)
310 return afs_protocol_error(call
, -EBADMSG
,
311 afs_eproto_cb_fid_count
);
313 call
->buffer
= kmalloc(array3_size(call
->count
, 3, 4),
317 afs_extract_to_buf(call
, call
->count
* 3 * 4);
322 _debug("extract FID array");
323 ret
= afs_extract_data(call
, true);
327 _debug("unmarshall FID array");
328 call
->request
= kcalloc(call
->count
,
329 sizeof(struct afs_callback_break
),
336 for (loop
= call
->count
; loop
> 0; loop
--, cb
++) {
337 cb
->fid
.vid
= ntohl(*bp
++);
338 cb
->fid
.vnode
= ntohl(*bp
++);
339 cb
->fid
.unique
= ntohl(*bp
++);
342 afs_extract_to_tmp(call
);
345 /* extract the callback array and its count in two steps */
348 _debug("extract CB count");
349 ret
= afs_extract_data(call
, true);
353 call
->count2
= ntohl(call
->tmp
);
354 _debug("CB count: %u", call
->count2
);
355 if (call
->count2
!= call
->count
&& call
->count2
!= 0)
356 return afs_protocol_error(call
, -EBADMSG
,
357 afs_eproto_cb_count
);
358 call
->iter
= &call
->def_iter
;
359 iov_iter_discard(&call
->def_iter
, READ
, call
->count2
* 3 * 4);
364 _debug("extract discard %zu/%u",
365 iov_iter_count(call
->iter
), call
->count2
* 3 * 4);
367 ret
= afs_extract_data(call
, false);
376 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
377 return afs_io_error(call
, afs_io_error_cm_reply
);
379 /* we'll need the file server record as that tells us which set of
380 * vnodes to operate upon */
381 return afs_find_cm_server_by_peer(call
);
385 * allow the fileserver to request callback state (re-)initialisation
387 static void SRXAFSCB_InitCallBackState(struct work_struct
*work
)
389 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
391 _enter("{%p}", call
->server
);
394 afs_init_callback_state(call
->server
);
395 afs_send_empty_reply(call
);
401 * deliver request data to a CB.InitCallBackState call
403 static int afs_deliver_cb_init_call_back_state(struct afs_call
*call
)
409 afs_extract_discard(call
, 0);
410 ret
= afs_extract_data(call
, false);
414 /* we'll need the file server record as that tells us which set of
415 * vnodes to operate upon */
416 return afs_find_cm_server_by_peer(call
);
420 * deliver request data to a CB.InitCallBackState3 call
422 static int afs_deliver_cb_init_call_back_state3(struct afs_call
*call
)
431 _enter("{%u}", call
->unmarshall
);
433 switch (call
->unmarshall
) {
435 call
->buffer
= kmalloc_array(11, sizeof(__be32
), GFP_KERNEL
);
438 afs_extract_to_buf(call
, 11 * sizeof(__be32
));
443 _debug("extract UUID");
444 ret
= afs_extract_data(call
, false);
447 case -EAGAIN
: return 0;
451 _debug("unmarshall UUID");
452 call
->request
= kmalloc(sizeof(struct afs_uuid
), GFP_KERNEL
);
459 r
->time_mid
= htons(ntohl(b
[1]));
460 r
->time_hi_and_version
= htons(ntohl(b
[2]));
461 r
->clock_seq_hi_and_reserved
= ntohl(b
[3]);
462 r
->clock_seq_low
= ntohl(b
[4]);
464 for (loop
= 0; loop
< 6; loop
++)
465 r
->node
[loop
] = ntohl(b
[loop
+ 5]);
473 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
474 return afs_io_error(call
, afs_io_error_cm_reply
);
476 /* we'll need the file server record as that tells us which set of
477 * vnodes to operate upon */
478 return afs_find_cm_server_by_uuid(call
, call
->request
);
482 * allow the fileserver to see if the cache manager is still alive
484 static void SRXAFSCB_Probe(struct work_struct
*work
)
486 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
489 afs_send_empty_reply(call
);
495 * deliver request data to a CB.Probe call
497 static int afs_deliver_cb_probe(struct afs_call
*call
)
503 afs_extract_discard(call
, 0);
504 ret
= afs_extract_data(call
, false);
508 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
509 return afs_io_error(call
, afs_io_error_cm_reply
);
510 return afs_find_cm_server_by_peer(call
);
514 * allow the fileserver to quickly find out if the fileserver has been rebooted
516 static void SRXAFSCB_ProbeUuid(struct work_struct
*work
)
518 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
519 struct afs_uuid
*r
= call
->request
;
523 if (memcmp(r
, &call
->net
->uuid
, sizeof(call
->net
->uuid
)) == 0)
524 afs_send_empty_reply(call
);
526 afs_abort_service_call(call
, 1, 1, "K-1");
533 * deliver request data to a CB.ProbeUuid call
535 static int afs_deliver_cb_probe_uuid(struct afs_call
*call
)
542 _enter("{%u}", call
->unmarshall
);
544 switch (call
->unmarshall
) {
546 call
->buffer
= kmalloc_array(11, sizeof(__be32
), GFP_KERNEL
);
549 afs_extract_to_buf(call
, 11 * sizeof(__be32
));
554 _debug("extract UUID");
555 ret
= afs_extract_data(call
, false);
558 case -EAGAIN
: return 0;
562 _debug("unmarshall UUID");
563 call
->request
= kmalloc(sizeof(struct afs_uuid
), GFP_KERNEL
);
570 r
->time_mid
= htons(ntohl(b
[1]));
571 r
->time_hi_and_version
= htons(ntohl(b
[2]));
572 r
->clock_seq_hi_and_reserved
= ntohl(b
[3]);
573 r
->clock_seq_low
= ntohl(b
[4]);
575 for (loop
= 0; loop
< 6; loop
++)
576 r
->node
[loop
] = ntohl(b
[loop
+ 5]);
584 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
585 return afs_io_error(call
, afs_io_error_cm_reply
);
586 return afs_find_cm_server_by_uuid(call
, call
->request
);
590 * allow the fileserver to ask about the cache manager's capabilities
592 static void SRXAFSCB_TellMeAboutYourself(struct work_struct
*work
)
594 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
598 struct /* InterfaceAddr */ {
605 struct /* Capabilities */ {
613 memset(&reply
, 0, sizeof(reply
));
615 reply
.ia
.uuid
[0] = call
->net
->uuid
.time_low
;
616 reply
.ia
.uuid
[1] = htonl(ntohs(call
->net
->uuid
.time_mid
));
617 reply
.ia
.uuid
[2] = htonl(ntohs(call
->net
->uuid
.time_hi_and_version
));
618 reply
.ia
.uuid
[3] = htonl((s8
) call
->net
->uuid
.clock_seq_hi_and_reserved
);
619 reply
.ia
.uuid
[4] = htonl((s8
) call
->net
->uuid
.clock_seq_low
);
620 for (loop
= 0; loop
< 6; loop
++)
621 reply
.ia
.uuid
[loop
+ 5] = htonl((s8
) call
->net
->uuid
.node
[loop
]);
623 reply
.cap
.capcount
= htonl(1);
624 reply
.cap
.caps
[0] = htonl(AFS_CAP_ERROR_TRANSLATION
);
625 afs_send_simple_reply(call
, &reply
, sizeof(reply
));
631 * deliver request data to a CB.TellMeAboutYourself call
633 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call
*call
)
639 afs_extract_discard(call
, 0);
640 ret
= afs_extract_data(call
, false);
644 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
645 return afs_io_error(call
, afs_io_error_cm_reply
);
646 return afs_find_cm_server_by_peer(call
);
650 * deliver request data to a YFS CB.CallBack call
652 static int afs_deliver_yfs_cb_callback(struct afs_call
*call
)
654 struct afs_callback_break
*cb
;
655 struct yfs_xdr_YFSFid
*bp
;
659 _enter("{%u}", call
->unmarshall
);
661 switch (call
->unmarshall
) {
663 afs_extract_to_tmp(call
);
666 /* extract the FID array and its count in two steps */
669 _debug("extract FID count");
670 ret
= afs_extract_data(call
, true);
674 call
->count
= ntohl(call
->tmp
);
675 _debug("FID count: %u", call
->count
);
676 if (call
->count
> YFSCBMAX
)
677 return afs_protocol_error(call
, -EBADMSG
,
678 afs_eproto_cb_fid_count
);
680 size
= array_size(call
->count
, sizeof(struct yfs_xdr_YFSFid
));
681 call
->buffer
= kmalloc(size
, GFP_KERNEL
);
684 afs_extract_to_buf(call
, size
);
689 _debug("extract FID array");
690 ret
= afs_extract_data(call
, false);
694 _debug("unmarshall FID array");
695 call
->request
= kcalloc(call
->count
,
696 sizeof(struct afs_callback_break
),
703 for (loop
= call
->count
; loop
> 0; loop
--, cb
++) {
704 cb
->fid
.vid
= xdr_to_u64(bp
->volume
);
705 cb
->fid
.vnode
= xdr_to_u64(bp
->vnode
.lo
);
706 cb
->fid
.vnode_hi
= ntohl(bp
->vnode
.hi
);
707 cb
->fid
.unique
= ntohl(bp
->vnode
.unique
);
711 afs_extract_to_tmp(call
);
718 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
719 return afs_io_error(call
, afs_io_error_cm_reply
);
721 /* We'll need the file server record as that tells us which set of
722 * vnodes to operate upon.
724 return afs_find_cm_server_by_peer(call
);