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>
20 static int afs_deliver_cb_init_call_back_state(struct afs_call *);
21 static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
22 static int afs_deliver_cb_probe(struct afs_call *);
23 static int afs_deliver_cb_callback(struct afs_call *);
24 static int afs_deliver_cb_probe_uuid(struct afs_call *);
25 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
26 static void afs_cm_destructor(struct afs_call *);
27 static void SRXAFSCB_CallBack(struct work_struct *);
28 static void SRXAFSCB_InitCallBackState(struct work_struct *);
29 static void SRXAFSCB_Probe(struct work_struct *);
30 static void SRXAFSCB_ProbeUuid(struct work_struct *);
31 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
33 #define CM_NAME(name) \
34 const char afs_SRXCB##name##_name[] __tracepoint_string = \
38 * CB.CallBack operation type
40 static CM_NAME(CallBack);
41 static const struct afs_call_type afs_SRXCBCallBack = {
42 .name = afs_SRXCBCallBack_name,
43 .deliver = afs_deliver_cb_callback,
44 .destructor = afs_cm_destructor,
45 .work = SRXAFSCB_CallBack,
49 * CB.InitCallBackState operation type
51 static CM_NAME(InitCallBackState);
52 static const struct afs_call_type afs_SRXCBInitCallBackState = {
53 .name = afs_SRXCBInitCallBackState_name,
54 .deliver = afs_deliver_cb_init_call_back_state,
55 .destructor = afs_cm_destructor,
56 .work = SRXAFSCB_InitCallBackState,
60 * CB.InitCallBackState3 operation type
62 static CM_NAME(InitCallBackState3);
63 static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
64 .name = afs_SRXCBInitCallBackState3_name,
65 .deliver = afs_deliver_cb_init_call_back_state3,
66 .destructor = afs_cm_destructor,
67 .work = SRXAFSCB_InitCallBackState,
71 * CB.Probe operation type
73 static CM_NAME(Probe);
74 static const struct afs_call_type afs_SRXCBProbe = {
75 .name = afs_SRXCBProbe_name,
76 .deliver = afs_deliver_cb_probe,
77 .destructor = afs_cm_destructor,
78 .work = SRXAFSCB_Probe,
82 * CB.ProbeUuid operation type
84 static CM_NAME(ProbeUuid);
85 static const struct afs_call_type afs_SRXCBProbeUuid = {
86 .name = afs_SRXCBProbeUuid_name,
87 .deliver = afs_deliver_cb_probe_uuid,
88 .destructor = afs_cm_destructor,
89 .work = SRXAFSCB_ProbeUuid,
93 * CB.TellMeAboutYourself operation type
95 static CM_NAME(TellMeAboutYourself);
96 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
97 .name = afs_SRXCBTellMeAboutYourself_name,
98 .deliver = afs_deliver_cb_tell_me_about_yourself,
99 .destructor = afs_cm_destructor,
100 .work = SRXAFSCB_TellMeAboutYourself,
104 * route an incoming cache manager call
105 * - return T if supported, F if not
107 bool afs_cm_incoming_call(struct afs_call *call)
109 _enter("{CB.OP %u}", call->operation_ID);
111 switch (call->operation_ID) {
113 call->type = &afs_SRXCBCallBack;
115 case CBInitCallBackState:
116 call->type = &afs_SRXCBInitCallBackState;
118 case CBInitCallBackState3:
119 call->type = &afs_SRXCBInitCallBackState3;
122 call->type = &afs_SRXCBProbe;
125 call->type = &afs_SRXCBProbeUuid;
127 case CBTellMeAboutYourself:
128 call->type = &afs_SRXCBTellMeAboutYourself;
136 * Clean up a cache manager call.
138 static void afs_cm_destructor(struct afs_call *call)
145 * The server supplied a list of callbacks that it wanted to break.
147 static void SRXAFSCB_CallBack(struct work_struct *work)
149 struct afs_call *call = container_of(work, struct afs_call, work);
153 /* We need to break the callbacks before sending the reply as the
154 * server holds up change visibility till it receives our reply so as
155 * to maintain cache coherency.
158 afs_break_callbacks(call->cm_server, call->count, call->request);
160 afs_send_empty_reply(call);
166 * deliver request data to a CB.CallBack call
168 static int afs_deliver_cb_callback(struct afs_call *call)
170 struct afs_callback_break *cb;
171 struct sockaddr_rxrpc srx;
175 _enter("{%u}", call->unmarshall);
177 switch (call->unmarshall) {
179 afs_extract_to_tmp(call);
182 /* extract the FID array and its count in two steps */
184 _debug("extract FID count");
185 ret = afs_extract_data(call, true);
189 call->count = ntohl(call->tmp);
190 _debug("FID count: %u", call->count);
191 if (call->count > AFSCBMAX)
192 return afs_protocol_error(call, -EBADMSG,
193 afs_eproto_cb_fid_count);
195 call->buffer = kmalloc(array3_size(call->count, 3, 4),
199 afs_extract_to_buf(call, call->count * 3 * 4);
203 _debug("extract FID array");
204 ret = afs_extract_data(call, true);
208 _debug("unmarshall FID array");
209 call->request = kcalloc(call->count,
210 sizeof(struct afs_callback_break),
217 for (loop = call->count; loop > 0; loop--, cb++) {
218 cb->fid.vid = ntohl(*bp++);
219 cb->fid.vnode = ntohl(*bp++);
220 cb->fid.unique = ntohl(*bp++);
221 cb->cb.type = AFSCM_CB_UNTYPED;
224 afs_extract_to_tmp(call);
227 /* extract the callback array and its count in two steps */
229 _debug("extract CB count");
230 ret = afs_extract_data(call, true);
234 call->count2 = ntohl(call->tmp);
235 _debug("CB count: %u", call->count2);
236 if (call->count2 != call->count && call->count2 != 0)
237 return afs_protocol_error(call, -EBADMSG,
238 afs_eproto_cb_count);
239 afs_extract_to_buf(call, call->count2 * 3 * 4);
243 _debug("extract CB array");
244 ret = afs_extract_data(call, false);
248 _debug("unmarshall CB array");
251 for (loop = call->count2; loop > 0; loop--, cb++) {
252 cb->cb.version = ntohl(*bp++);
253 cb->cb.expiry = ntohl(*bp++);
254 cb->cb.type = ntohl(*bp++);
262 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
265 /* we'll need the file server record as that tells us which set of
266 * vnodes to operate upon */
267 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
268 call->cm_server = afs_find_server(call->net, &srx);
269 if (!call->cm_server)
270 trace_afs_cm_no_server(call, &srx);
272 return afs_queue_call_work(call);
276 * allow the fileserver to request callback state (re-)initialisation
278 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
280 struct afs_call *call = container_of(work, struct afs_call, work);
282 _enter("{%p}", call->cm_server);
285 afs_init_callback_state(call->cm_server);
286 afs_send_empty_reply(call);
292 * deliver request data to a CB.InitCallBackState call
294 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
296 struct sockaddr_rxrpc srx;
301 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
303 afs_extract_discard(call, 0);
304 ret = afs_extract_data(call, false);
308 /* we'll need the file server record as that tells us which set of
309 * vnodes to operate upon */
310 call->cm_server = afs_find_server(call->net, &srx);
311 if (!call->cm_server)
312 trace_afs_cm_no_server(call, &srx);
314 return afs_queue_call_work(call);
318 * deliver request data to a CB.InitCallBackState3 call
320 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
329 _enter("{%u}", call->unmarshall);
331 switch (call->unmarshall) {
333 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
336 afs_extract_to_buf(call, 11 * sizeof(__be32));
340 _debug("extract UUID");
341 ret = afs_extract_data(call, false);
344 case -EAGAIN: return 0;
348 _debug("unmarshall UUID");
349 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
356 r->time_mid = htons(ntohl(b[1]));
357 r->time_hi_and_version = htons(ntohl(b[2]));
358 r->clock_seq_hi_and_reserved = ntohl(b[3]);
359 r->clock_seq_low = ntohl(b[4]);
361 for (loop = 0; loop < 6; loop++)
362 r->node[loop] = ntohl(b[loop + 5]);
370 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
373 /* we'll need the file server record as that tells us which set of
374 * vnodes to operate upon */
376 call->cm_server = afs_find_server_by_uuid(call->net, call->request);
378 if (!call->cm_server)
379 trace_afs_cm_no_server_u(call, call->request);
381 return afs_queue_call_work(call);
385 * allow the fileserver to see if the cache manager is still alive
387 static void SRXAFSCB_Probe(struct work_struct *work)
389 struct afs_call *call = container_of(work, struct afs_call, work);
392 afs_send_empty_reply(call);
398 * deliver request data to a CB.Probe call
400 static int afs_deliver_cb_probe(struct afs_call *call)
406 afs_extract_discard(call, 0);
407 ret = afs_extract_data(call, false);
411 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
414 return afs_queue_call_work(call);
418 * allow the fileserver to quickly find out if the fileserver has been rebooted
420 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
422 struct afs_call *call = container_of(work, struct afs_call, work);
423 struct afs_uuid *r = call->request;
431 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
432 reply.match = htonl(0);
434 reply.match = htonl(1);
436 afs_send_simple_reply(call, &reply, sizeof(reply));
442 * deliver request data to a CB.ProbeUuid call
444 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
451 _enter("{%u}", call->unmarshall);
453 switch (call->unmarshall) {
455 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
458 afs_extract_to_buf(call, 11 * sizeof(__be32));
462 _debug("extract UUID");
463 ret = afs_extract_data(call, false);
466 case -EAGAIN: return 0;
470 _debug("unmarshall UUID");
471 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
478 r->time_mid = htons(ntohl(b[1]));
479 r->time_hi_and_version = htons(ntohl(b[2]));
480 r->clock_seq_hi_and_reserved = ntohl(b[3]);
481 r->clock_seq_low = ntohl(b[4]);
483 for (loop = 0; loop < 6; loop++)
484 r->node[loop] = ntohl(b[loop + 5]);
492 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
495 return afs_queue_call_work(call);
499 * allow the fileserver to ask about the cache manager's capabilities
501 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
503 struct afs_interface *ifs;
504 struct afs_call *call = container_of(work, struct afs_call, work);
508 struct /* InterfaceAddr */ {
515 struct /* Capabilities */ {
524 ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
526 nifs = afs_get_ipv4_interfaces(call->net, ifs, 32, false);
534 memset(&reply, 0, sizeof(reply));
535 reply.ia.nifs = htonl(nifs);
537 reply.ia.uuid[0] = call->net->uuid.time_low;
538 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
539 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
540 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
541 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
542 for (loop = 0; loop < 6; loop++)
543 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
546 for (loop = 0; loop < nifs; loop++) {
547 reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
548 reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
549 reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
554 reply.cap.capcount = htonl(1);
555 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
556 afs_send_simple_reply(call, &reply, sizeof(reply));
562 * deliver request data to a CB.TellMeAboutYourself call
564 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
570 afs_extract_discard(call, 0);
571 ret = afs_extract_data(call, false);
575 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
578 return afs_queue_call_work(call);