1 /* AFS Cache Manager Service
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
19 #include "protocol_yfs.h"
21 static int afs_deliver_cb_init_call_back_state(struct afs_call
*);
22 static int afs_deliver_cb_init_call_back_state3(struct afs_call
*);
23 static int afs_deliver_cb_probe(struct afs_call
*);
24 static int afs_deliver_cb_callback(struct afs_call
*);
25 static int afs_deliver_cb_probe_uuid(struct afs_call
*);
26 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call
*);
27 static void afs_cm_destructor(struct afs_call
*);
28 static void SRXAFSCB_CallBack(struct work_struct
*);
29 static void SRXAFSCB_InitCallBackState(struct work_struct
*);
30 static void SRXAFSCB_Probe(struct work_struct
*);
31 static void SRXAFSCB_ProbeUuid(struct work_struct
*);
32 static void SRXAFSCB_TellMeAboutYourself(struct work_struct
*);
34 static int afs_deliver_yfs_cb_callback(struct afs_call
*);
36 #define CM_NAME(name) \
37 char afs_SRXCB##name##_name[] __tracepoint_string = \
41 * CB.CallBack operation type
43 static CM_NAME(CallBack
);
44 static const struct afs_call_type afs_SRXCBCallBack
= {
45 .name
= afs_SRXCBCallBack_name
,
46 .deliver
= afs_deliver_cb_callback
,
47 .destructor
= afs_cm_destructor
,
48 .work
= SRXAFSCB_CallBack
,
52 * CB.InitCallBackState operation type
54 static CM_NAME(InitCallBackState
);
55 static const struct afs_call_type afs_SRXCBInitCallBackState
= {
56 .name
= afs_SRXCBInitCallBackState_name
,
57 .deliver
= afs_deliver_cb_init_call_back_state
,
58 .destructor
= afs_cm_destructor
,
59 .work
= SRXAFSCB_InitCallBackState
,
63 * CB.InitCallBackState3 operation type
65 static CM_NAME(InitCallBackState3
);
66 static const struct afs_call_type afs_SRXCBInitCallBackState3
= {
67 .name
= afs_SRXCBInitCallBackState3_name
,
68 .deliver
= afs_deliver_cb_init_call_back_state3
,
69 .destructor
= afs_cm_destructor
,
70 .work
= SRXAFSCB_InitCallBackState
,
74 * CB.Probe operation type
76 static CM_NAME(Probe
);
77 static const struct afs_call_type afs_SRXCBProbe
= {
78 .name
= afs_SRXCBProbe_name
,
79 .deliver
= afs_deliver_cb_probe
,
80 .destructor
= afs_cm_destructor
,
81 .work
= SRXAFSCB_Probe
,
85 * CB.ProbeUuid operation type
87 static CM_NAME(ProbeUuid
);
88 static const struct afs_call_type afs_SRXCBProbeUuid
= {
89 .name
= afs_SRXCBProbeUuid_name
,
90 .deliver
= afs_deliver_cb_probe_uuid
,
91 .destructor
= afs_cm_destructor
,
92 .work
= SRXAFSCB_ProbeUuid
,
96 * CB.TellMeAboutYourself operation type
98 static CM_NAME(TellMeAboutYourself
);
99 static const struct afs_call_type afs_SRXCBTellMeAboutYourself
= {
100 .name
= afs_SRXCBTellMeAboutYourself_name
,
101 .deliver
= afs_deliver_cb_tell_me_about_yourself
,
102 .destructor
= afs_cm_destructor
,
103 .work
= SRXAFSCB_TellMeAboutYourself
,
107 * YFS CB.CallBack operation type
109 static CM_NAME(YFS_CallBack
);
110 static const struct afs_call_type afs_SRXYFSCB_CallBack
= {
111 .name
= afs_SRXCBYFS_CallBack_name
,
112 .deliver
= afs_deliver_yfs_cb_callback
,
113 .destructor
= afs_cm_destructor
,
114 .work
= SRXAFSCB_CallBack
,
118 * route an incoming cache manager call
119 * - return T if supported, F if not
121 bool afs_cm_incoming_call(struct afs_call
*call
)
123 _enter("{%u, CB.OP %u}", call
->service_id
, call
->operation_ID
);
125 call
->epoch
= rxrpc_kernel_get_epoch(call
->net
->socket
, call
->rxcall
);
127 switch (call
->operation_ID
) {
129 call
->type
= &afs_SRXCBCallBack
;
131 case CBInitCallBackState
:
132 call
->type
= &afs_SRXCBInitCallBackState
;
134 case CBInitCallBackState3
:
135 call
->type
= &afs_SRXCBInitCallBackState3
;
138 call
->type
= &afs_SRXCBProbe
;
141 call
->type
= &afs_SRXCBProbeUuid
;
143 case CBTellMeAboutYourself
:
144 call
->type
= &afs_SRXCBTellMeAboutYourself
;
147 if (call
->service_id
!= YFS_CM_SERVICE
)
149 call
->type
= &afs_SRXYFSCB_CallBack
;
157 * Record a probe to the cache manager from a server.
159 static int afs_record_cm_probe(struct afs_call
*call
, struct afs_server
*server
)
163 if (test_bit(AFS_SERVER_FL_HAVE_EPOCH
, &server
->flags
) &&
164 !test_bit(AFS_SERVER_FL_PROBING
, &server
->flags
)) {
165 if (server
->cm_epoch
== call
->epoch
)
168 if (!server
->probe
.said_rebooted
) {
169 pr_notice("kAFS: FS rebooted %pU\n", &server
->uuid
);
170 server
->probe
.said_rebooted
= true;
174 spin_lock(&server
->probe_lock
);
176 if (!test_bit(AFS_SERVER_FL_HAVE_EPOCH
, &server
->flags
)) {
177 server
->cm_epoch
= call
->epoch
;
178 server
->probe
.cm_epoch
= call
->epoch
;
182 if (server
->probe
.cm_probed
&&
183 call
->epoch
!= server
->probe
.cm_epoch
&&
184 !server
->probe
.said_inconsistent
) {
185 pr_notice("kAFS: FS endpoints inconsistent %pU\n",
187 server
->probe
.said_inconsistent
= true;
190 if (!server
->probe
.cm_probed
|| call
->epoch
== server
->cm_epoch
)
191 server
->probe
.cm_epoch
= server
->cm_epoch
;
194 server
->probe
.cm_probed
= true;
195 spin_unlock(&server
->probe_lock
);
200 * Find the server record by peer address and record a probe to the cache
201 * manager from a server.
203 static int afs_find_cm_server_by_peer(struct afs_call
*call
)
205 struct sockaddr_rxrpc srx
;
206 struct afs_server
*server
;
208 rxrpc_kernel_get_peer(call
->net
->socket
, call
->rxcall
, &srx
);
210 server
= afs_find_server(call
->net
, &srx
);
212 trace_afs_cm_no_server(call
, &srx
);
216 call
->server
= server
;
217 return afs_record_cm_probe(call
, server
);
221 * Find the server record by server UUID and record a probe to the cache
222 * manager from a server.
224 static int afs_find_cm_server_by_uuid(struct afs_call
*call
,
225 struct afs_uuid
*uuid
)
227 struct afs_server
*server
;
230 server
= afs_find_server_by_uuid(call
->net
, call
->request
);
233 trace_afs_cm_no_server_u(call
, call
->request
);
237 call
->server
= server
;
238 return afs_record_cm_probe(call
, server
);
242 * Clean up a cache manager call.
244 static void afs_cm_destructor(struct afs_call
*call
)
251 * The server supplied a list of callbacks that it wanted to break.
253 static void SRXAFSCB_CallBack(struct work_struct
*work
)
255 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
259 /* We need to break the callbacks before sending the reply as the
260 * server holds up change visibility till it receives our reply so as
261 * to maintain cache coherency.
264 afs_break_callbacks(call
->server
, call
->count
, call
->request
);
266 afs_send_empty_reply(call
);
272 * deliver request data to a CB.CallBack call
274 static int afs_deliver_cb_callback(struct afs_call
*call
)
276 struct afs_callback_break
*cb
;
280 _enter("{%u}", call
->unmarshall
);
282 switch (call
->unmarshall
) {
284 afs_extract_to_tmp(call
);
287 /* extract the FID array and its count in two steps */
290 _debug("extract FID count");
291 ret
= afs_extract_data(call
, true);
295 call
->count
= ntohl(call
->tmp
);
296 _debug("FID count: %u", call
->count
);
297 if (call
->count
> AFSCBMAX
)
298 return afs_protocol_error(call
, -EBADMSG
,
299 afs_eproto_cb_fid_count
);
301 call
->buffer
= kmalloc(array3_size(call
->count
, 3, 4),
305 afs_extract_to_buf(call
, call
->count
* 3 * 4);
310 _debug("extract FID array");
311 ret
= afs_extract_data(call
, true);
315 _debug("unmarshall FID array");
316 call
->request
= kcalloc(call
->count
,
317 sizeof(struct afs_callback_break
),
324 for (loop
= call
->count
; loop
> 0; loop
--, cb
++) {
325 cb
->fid
.vid
= ntohl(*bp
++);
326 cb
->fid
.vnode
= ntohl(*bp
++);
327 cb
->fid
.unique
= ntohl(*bp
++);
330 afs_extract_to_tmp(call
);
333 /* extract the callback array and its count in two steps */
336 _debug("extract CB count");
337 ret
= afs_extract_data(call
, true);
341 call
->count2
= ntohl(call
->tmp
);
342 _debug("CB count: %u", call
->count2
);
343 if (call
->count2
!= call
->count
&& call
->count2
!= 0)
344 return afs_protocol_error(call
, -EBADMSG
,
345 afs_eproto_cb_count
);
346 call
->_iter
= &call
->iter
;
347 iov_iter_discard(&call
->iter
, READ
, call
->count2
* 3 * 4);
352 _debug("extract discard %zu/%u",
353 iov_iter_count(&call
->iter
), call
->count2
* 3 * 4);
355 ret
= afs_extract_data(call
, false);
364 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
365 return afs_io_error(call
, afs_io_error_cm_reply
);
367 /* we'll need the file server record as that tells us which set of
368 * vnodes to operate upon */
369 return afs_find_cm_server_by_peer(call
);
373 * allow the fileserver to request callback state (re-)initialisation
375 static void SRXAFSCB_InitCallBackState(struct work_struct
*work
)
377 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
379 _enter("{%p}", call
->server
);
382 afs_init_callback_state(call
->server
);
383 afs_send_empty_reply(call
);
389 * deliver request data to a CB.InitCallBackState call
391 static int afs_deliver_cb_init_call_back_state(struct afs_call
*call
)
397 afs_extract_discard(call
, 0);
398 ret
= afs_extract_data(call
, false);
402 /* we'll need the file server record as that tells us which set of
403 * vnodes to operate upon */
404 return afs_find_cm_server_by_peer(call
);
408 * deliver request data to a CB.InitCallBackState3 call
410 static int afs_deliver_cb_init_call_back_state3(struct afs_call
*call
)
419 _enter("{%u}", call
->unmarshall
);
421 switch (call
->unmarshall
) {
423 call
->buffer
= kmalloc_array(11, sizeof(__be32
), GFP_KERNEL
);
426 afs_extract_to_buf(call
, 11 * sizeof(__be32
));
431 _debug("extract UUID");
432 ret
= afs_extract_data(call
, false);
435 case -EAGAIN
: return 0;
439 _debug("unmarshall UUID");
440 call
->request
= kmalloc(sizeof(struct afs_uuid
), GFP_KERNEL
);
447 r
->time_mid
= htons(ntohl(b
[1]));
448 r
->time_hi_and_version
= htons(ntohl(b
[2]));
449 r
->clock_seq_hi_and_reserved
= ntohl(b
[3]);
450 r
->clock_seq_low
= ntohl(b
[4]);
452 for (loop
= 0; loop
< 6; loop
++)
453 r
->node
[loop
] = ntohl(b
[loop
+ 5]);
461 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
462 return afs_io_error(call
, afs_io_error_cm_reply
);
464 /* we'll need the file server record as that tells us which set of
465 * vnodes to operate upon */
466 return afs_find_cm_server_by_uuid(call
, call
->request
);
470 * allow the fileserver to see if the cache manager is still alive
472 static void SRXAFSCB_Probe(struct work_struct
*work
)
474 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
477 afs_send_empty_reply(call
);
483 * deliver request data to a CB.Probe call
485 static int afs_deliver_cb_probe(struct afs_call
*call
)
491 afs_extract_discard(call
, 0);
492 ret
= afs_extract_data(call
, false);
496 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
497 return afs_io_error(call
, afs_io_error_cm_reply
);
498 return afs_find_cm_server_by_peer(call
);
502 * allow the fileserver to quickly find out if the fileserver has been rebooted
504 static void SRXAFSCB_ProbeUuid(struct work_struct
*work
)
506 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
507 struct afs_uuid
*r
= call
->request
;
515 if (memcmp(r
, &call
->net
->uuid
, sizeof(call
->net
->uuid
)) == 0)
516 reply
.match
= htonl(0);
518 reply
.match
= htonl(1);
520 afs_send_simple_reply(call
, &reply
, sizeof(reply
));
526 * deliver request data to a CB.ProbeUuid call
528 static int afs_deliver_cb_probe_uuid(struct afs_call
*call
)
535 _enter("{%u}", call
->unmarshall
);
537 switch (call
->unmarshall
) {
539 call
->buffer
= kmalloc_array(11, sizeof(__be32
), GFP_KERNEL
);
542 afs_extract_to_buf(call
, 11 * sizeof(__be32
));
547 _debug("extract UUID");
548 ret
= afs_extract_data(call
, false);
551 case -EAGAIN
: return 0;
555 _debug("unmarshall UUID");
556 call
->request
= kmalloc(sizeof(struct afs_uuid
), GFP_KERNEL
);
563 r
->time_mid
= htons(ntohl(b
[1]));
564 r
->time_hi_and_version
= htons(ntohl(b
[2]));
565 r
->clock_seq_hi_and_reserved
= ntohl(b
[3]);
566 r
->clock_seq_low
= ntohl(b
[4]);
568 for (loop
= 0; loop
< 6; loop
++)
569 r
->node
[loop
] = ntohl(b
[loop
+ 5]);
577 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
578 return afs_io_error(call
, afs_io_error_cm_reply
);
579 return afs_find_cm_server_by_uuid(call
, call
->request
);
583 * allow the fileserver to ask about the cache manager's capabilities
585 static void SRXAFSCB_TellMeAboutYourself(struct work_struct
*work
)
587 struct afs_interface
*ifs
;
588 struct afs_call
*call
= container_of(work
, struct afs_call
, work
);
592 struct /* InterfaceAddr */ {
599 struct /* Capabilities */ {
608 ifs
= kcalloc(32, sizeof(*ifs
), GFP_KERNEL
);
610 nifs
= afs_get_ipv4_interfaces(call
->net
, ifs
, 32, false);
618 memset(&reply
, 0, sizeof(reply
));
619 reply
.ia
.nifs
= htonl(nifs
);
621 reply
.ia
.uuid
[0] = call
->net
->uuid
.time_low
;
622 reply
.ia
.uuid
[1] = htonl(ntohs(call
->net
->uuid
.time_mid
));
623 reply
.ia
.uuid
[2] = htonl(ntohs(call
->net
->uuid
.time_hi_and_version
));
624 reply
.ia
.uuid
[3] = htonl((s8
) call
->net
->uuid
.clock_seq_hi_and_reserved
);
625 reply
.ia
.uuid
[4] = htonl((s8
) call
->net
->uuid
.clock_seq_low
);
626 for (loop
= 0; loop
< 6; loop
++)
627 reply
.ia
.uuid
[loop
+ 5] = htonl((s8
) call
->net
->uuid
.node
[loop
]);
630 for (loop
= 0; loop
< nifs
; loop
++) {
631 reply
.ia
.ifaddr
[loop
] = ifs
[loop
].address
.s_addr
;
632 reply
.ia
.netmask
[loop
] = ifs
[loop
].netmask
.s_addr
;
633 reply
.ia
.mtu
[loop
] = htonl(ifs
[loop
].mtu
);
638 reply
.cap
.capcount
= htonl(1);
639 reply
.cap
.caps
[0] = htonl(AFS_CAP_ERROR_TRANSLATION
);
640 afs_send_simple_reply(call
, &reply
, sizeof(reply
));
646 * deliver request data to a CB.TellMeAboutYourself call
648 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call
*call
)
654 afs_extract_discard(call
, 0);
655 ret
= afs_extract_data(call
, false);
659 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
660 return afs_io_error(call
, afs_io_error_cm_reply
);
661 return afs_find_cm_server_by_peer(call
);
665 * deliver request data to a YFS CB.CallBack call
667 static int afs_deliver_yfs_cb_callback(struct afs_call
*call
)
669 struct afs_callback_break
*cb
;
670 struct yfs_xdr_YFSFid
*bp
;
674 _enter("{%u}", call
->unmarshall
);
676 switch (call
->unmarshall
) {
678 afs_extract_to_tmp(call
);
681 /* extract the FID array and its count in two steps */
684 _debug("extract FID count");
685 ret
= afs_extract_data(call
, true);
689 call
->count
= ntohl(call
->tmp
);
690 _debug("FID count: %u", call
->count
);
691 if (call
->count
> YFSCBMAX
)
692 return afs_protocol_error(call
, -EBADMSG
,
693 afs_eproto_cb_fid_count
);
695 size
= array_size(call
->count
, sizeof(struct yfs_xdr_YFSFid
));
696 call
->buffer
= kmalloc(size
, GFP_KERNEL
);
699 afs_extract_to_buf(call
, size
);
704 _debug("extract FID array");
705 ret
= afs_extract_data(call
, false);
709 _debug("unmarshall FID array");
710 call
->request
= kcalloc(call
->count
,
711 sizeof(struct afs_callback_break
),
718 for (loop
= call
->count
; loop
> 0; loop
--, cb
++) {
719 cb
->fid
.vid
= xdr_to_u64(bp
->volume
);
720 cb
->fid
.vnode
= xdr_to_u64(bp
->vnode
.lo
);
721 cb
->fid
.vnode_hi
= ntohl(bp
->vnode
.hi
);
722 cb
->fid
.unique
= ntohl(bp
->vnode
.unique
);
726 afs_extract_to_tmp(call
);
733 if (!afs_check_call_state(call
, AFS_CALL_SV_REPLYING
))
734 return afs_io_error(call
, afs_io_error_cm_reply
);
736 /* We'll need the file server record as that tells us which set of
737 * vnodes to operate upon.
739 return afs_find_cm_server_by_peer(call
);