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 const 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->cm_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->cm_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->cm_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 */
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);
308 _debug("extract FID array");
309 ret = afs_extract_data(call, true);
313 _debug("unmarshall FID array");
314 call->request = kcalloc(call->count,
315 sizeof(struct afs_callback_break),
322 for (loop = call->count; loop > 0; loop--, cb++) {
323 cb->fid.vid = ntohl(*bp++);
324 cb->fid.vnode = ntohl(*bp++);
325 cb->fid.unique = ntohl(*bp++);
328 afs_extract_to_tmp(call);
331 /* extract the callback array and its count in two steps */
333 _debug("extract CB count");
334 ret = afs_extract_data(call, true);
338 call->count2 = ntohl(call->tmp);
339 _debug("CB count: %u", call->count2);
340 if (call->count2 != call->count && call->count2 != 0)
341 return afs_protocol_error(call, -EBADMSG,
342 afs_eproto_cb_count);
343 call->_iter = &call->iter;
344 iov_iter_discard(&call->iter, READ, call->count2 * 3 * 4);
348 _debug("extract discard %zu/%u",
349 iov_iter_count(&call->iter), call->count2 * 3 * 4);
351 ret = afs_extract_data(call, false);
360 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
361 return afs_io_error(call, afs_io_error_cm_reply);
363 /* we'll need the file server record as that tells us which set of
364 * vnodes to operate upon */
365 return afs_find_cm_server_by_peer(call);
369 * allow the fileserver to request callback state (re-)initialisation
371 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
373 struct afs_call *call = container_of(work, struct afs_call, work);
375 _enter("{%p}", call->cm_server);
378 afs_init_callback_state(call->cm_server);
379 afs_send_empty_reply(call);
385 * deliver request data to a CB.InitCallBackState call
387 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
393 afs_extract_discard(call, 0);
394 ret = afs_extract_data(call, false);
398 /* we'll need the file server record as that tells us which set of
399 * vnodes to operate upon */
400 return afs_find_cm_server_by_peer(call);
404 * deliver request data to a CB.InitCallBackState3 call
406 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
415 _enter("{%u}", call->unmarshall);
417 switch (call->unmarshall) {
419 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
422 afs_extract_to_buf(call, 11 * sizeof(__be32));
426 _debug("extract UUID");
427 ret = afs_extract_data(call, false);
430 case -EAGAIN: return 0;
434 _debug("unmarshall UUID");
435 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
442 r->time_mid = htons(ntohl(b[1]));
443 r->time_hi_and_version = htons(ntohl(b[2]));
444 r->clock_seq_hi_and_reserved = ntohl(b[3]);
445 r->clock_seq_low = ntohl(b[4]);
447 for (loop = 0; loop < 6; loop++)
448 r->node[loop] = ntohl(b[loop + 5]);
456 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
457 return afs_io_error(call, afs_io_error_cm_reply);
459 /* we'll need the file server record as that tells us which set of
460 * vnodes to operate upon */
461 return afs_find_cm_server_by_uuid(call, call->request);
465 * allow the fileserver to see if the cache manager is still alive
467 static void SRXAFSCB_Probe(struct work_struct *work)
469 struct afs_call *call = container_of(work, struct afs_call, work);
472 afs_send_empty_reply(call);
478 * deliver request data to a CB.Probe call
480 static int afs_deliver_cb_probe(struct afs_call *call)
486 afs_extract_discard(call, 0);
487 ret = afs_extract_data(call, false);
491 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
492 return afs_io_error(call, afs_io_error_cm_reply);
493 return afs_find_cm_server_by_peer(call);
497 * allow the fileserver to quickly find out if the fileserver has been rebooted
499 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
501 struct afs_call *call = container_of(work, struct afs_call, work);
502 struct afs_uuid *r = call->request;
510 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
511 reply.match = htonl(0);
513 reply.match = htonl(1);
515 afs_send_simple_reply(call, &reply, sizeof(reply));
521 * deliver request data to a CB.ProbeUuid call
523 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
530 _enter("{%u}", call->unmarshall);
532 switch (call->unmarshall) {
534 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
537 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_interface *ifs;
582 struct afs_call *call = container_of(work, struct afs_call, work);
586 struct /* InterfaceAddr */ {
593 struct /* Capabilities */ {
602 ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
604 nifs = afs_get_ipv4_interfaces(call->net, ifs, 32, false);
612 memset(&reply, 0, sizeof(reply));
613 reply.ia.nifs = htonl(nifs);
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]);
624 for (loop = 0; loop < nifs; loop++) {
625 reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
626 reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
627 reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
632 reply.cap.capcount = htonl(1);
633 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
634 afs_send_simple_reply(call, &reply, sizeof(reply));
640 * deliver request data to a CB.TellMeAboutYourself call
642 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
648 afs_extract_discard(call, 0);
649 ret = afs_extract_data(call, false);
653 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
654 return afs_io_error(call, afs_io_error_cm_reply);
655 return afs_find_cm_server_by_peer(call);
659 * deliver request data to a YFS CB.CallBack call
661 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
663 struct afs_callback_break *cb;
664 struct yfs_xdr_YFSFid *bp;
668 _enter("{%u}", call->unmarshall);
670 switch (call->unmarshall) {
672 afs_extract_to_tmp(call);
675 /* extract the FID array and its count in two steps */
677 _debug("extract FID count");
678 ret = afs_extract_data(call, true);
682 call->count = ntohl(call->tmp);
683 _debug("FID count: %u", call->count);
684 if (call->count > YFSCBMAX)
685 return afs_protocol_error(call, -EBADMSG,
686 afs_eproto_cb_fid_count);
688 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
689 call->buffer = kmalloc(size, GFP_KERNEL);
692 afs_extract_to_buf(call, size);
696 _debug("extract FID array");
697 ret = afs_extract_data(call, false);
701 _debug("unmarshall FID array");
702 call->request = kcalloc(call->count,
703 sizeof(struct afs_callback_break),
710 for (loop = call->count; loop > 0; loop--, cb++) {
711 cb->fid.vid = xdr_to_u64(bp->volume);
712 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
713 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
714 cb->fid.unique = ntohl(bp->vnode.unique);
718 afs_extract_to_tmp(call);
725 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
726 return afs_io_error(call, afs_io_error_cm_reply);
728 /* We'll need the file server record as that tells us which set of
729 * vnodes to operate upon.
731 return afs_find_cm_server_by_peer(call);