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 *);
29 * CB.CallBack operation type
31 static const struct afs_call_type afs_SRXCBCallBack = {
32 .name = "CB.CallBack",
33 .deliver = afs_deliver_cb_callback,
34 .abort_to_error = afs_abort_to_error,
35 .destructor = afs_cm_destructor,
39 * CB.InitCallBackState operation type
41 static const struct afs_call_type afs_SRXCBInitCallBackState = {
42 .name = "CB.InitCallBackState",
43 .deliver = afs_deliver_cb_init_call_back_state,
44 .abort_to_error = afs_abort_to_error,
45 .destructor = afs_cm_destructor,
49 * CB.InitCallBackState3 operation type
51 static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
52 .name = "CB.InitCallBackState3",
53 .deliver = afs_deliver_cb_init_call_back_state3,
54 .abort_to_error = afs_abort_to_error,
55 .destructor = afs_cm_destructor,
59 * CB.Probe operation type
61 static const struct afs_call_type afs_SRXCBProbe = {
63 .deliver = afs_deliver_cb_probe,
64 .abort_to_error = afs_abort_to_error,
65 .destructor = afs_cm_destructor,
69 * CB.ProbeUuid operation type
71 static const struct afs_call_type afs_SRXCBProbeUuid = {
72 .name = "CB.ProbeUuid",
73 .deliver = afs_deliver_cb_probe_uuid,
74 .abort_to_error = afs_abort_to_error,
75 .destructor = afs_cm_destructor,
79 * CB.TellMeAboutYourself operation type
81 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
82 .name = "CB.TellMeAboutYourself",
83 .deliver = afs_deliver_cb_tell_me_about_yourself,
84 .abort_to_error = afs_abort_to_error,
85 .destructor = afs_cm_destructor,
89 * route an incoming cache manager call
90 * - return T if supported, F if not
92 bool afs_cm_incoming_call(struct afs_call *call)
94 _enter("{CB.OP %u}", call->operation_ID);
96 switch (call->operation_ID) {
98 call->type = &afs_SRXCBCallBack;
100 case CBInitCallBackState:
101 call->type = &afs_SRXCBInitCallBackState;
103 case CBInitCallBackState3:
104 call->type = &afs_SRXCBInitCallBackState3;
107 call->type = &afs_SRXCBProbe;
109 case CBTellMeAboutYourself:
110 call->type = &afs_SRXCBTellMeAboutYourself;
118 * clean up a cache manager call
120 static void afs_cm_destructor(struct afs_call *call)
124 /* Break the callbacks here so that we do it after the final ACK is
125 * received. The step number here must match the final number in
126 * afs_deliver_cb_callback().
128 if (call->unmarshall == 5) {
129 ASSERT(call->server && call->count && call->request);
130 afs_break_callbacks(call->server, call->count, call->request);
133 afs_put_server(call->server);
140 * allow the fileserver to see if the cache manager is still alive
142 static void SRXAFSCB_CallBack(struct work_struct *work)
144 struct afs_call *call = container_of(work, struct afs_call, work);
148 /* be sure to send the reply *before* attempting to spam the AFS server
149 * with FSFetchStatus requests on the vnodes with broken callbacks lest
150 * the AFS server get into a vicious cycle of trying to break further
151 * callbacks because it hadn't received completion of the CBCallBack op
153 afs_send_empty_reply(call);
155 afs_break_callbacks(call->server, call->count, call->request);
160 * deliver request data to a CB.CallBack call
162 static int afs_deliver_cb_callback(struct afs_call *call)
164 struct sockaddr_rxrpc srx;
165 struct afs_callback *cb;
166 struct afs_server *server;
171 _enter("{%u}", call->unmarshall);
173 switch (call->unmarshall) {
175 rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx);
179 /* extract the FID array and its count in two steps */
181 _debug("extract FID count");
182 ret = afs_extract_data(call, &call->tmp, 4, true);
186 call->count = ntohl(call->tmp);
187 _debug("FID count: %u", call->count);
188 if (call->count > AFSCBMAX)
191 call->buffer = kmalloc(call->count * 3 * 4, GFP_KERNEL);
198 _debug("extract FID array");
199 ret = afs_extract_data(call, call->buffer,
200 call->count * 3 * 4, true);
204 _debug("unmarshall FID array");
205 call->request = kcalloc(call->count,
206 sizeof(struct afs_callback),
213 for (loop = call->count; loop > 0; loop--, cb++) {
214 cb->fid.vid = ntohl(*bp++);
215 cb->fid.vnode = ntohl(*bp++);
216 cb->fid.unique = ntohl(*bp++);
217 cb->type = AFSCM_CB_UNTYPED;
223 /* extract the callback array and its count in two steps */
225 _debug("extract CB count");
226 ret = afs_extract_data(call, &call->tmp, 4, true);
230 tmp = ntohl(call->tmp);
231 _debug("CB count: %u", tmp);
232 if (tmp != call->count && tmp != 0)
238 _debug("extract CB array");
239 ret = afs_extract_data(call, call->buffer,
240 call->count * 3 * 4, false);
244 _debug("unmarshall CB array");
247 for (loop = call->count; loop > 0; loop--, cb++) {
248 cb->version = ntohl(*bp++);
249 cb->expiry = ntohl(*bp++);
250 cb->type = ntohl(*bp++);
256 /* Record that the message was unmarshalled successfully so
257 * that the call destructor can know do the callback breaking
258 * work, even if the final ACK isn't received.
260 * If the step number changes, then afs_cm_destructor() must be
268 call->state = AFS_CALL_REPLYING;
270 /* we'll need the file server record as that tells us which set of
271 * vnodes to operate upon */
272 server = afs_find_server(&srx);
275 call->server = server;
277 INIT_WORK(&call->work, SRXAFSCB_CallBack);
278 queue_work(afs_wq, &call->work);
283 * allow the fileserver to request callback state (re-)initialisation
285 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
287 struct afs_call *call = container_of(work, struct afs_call, work);
289 _enter("{%p}", call->server);
291 afs_init_callback_state(call->server);
292 afs_send_empty_reply(call);
297 * deliver request data to a CB.InitCallBackState call
299 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
301 struct sockaddr_rxrpc srx;
302 struct afs_server *server;
307 rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx);
309 ret = afs_extract_data(call, NULL, 0, false);
313 /* no unmarshalling required */
314 call->state = AFS_CALL_REPLYING;
316 /* we'll need the file server record as that tells us which set of
317 * vnodes to operate upon */
318 server = afs_find_server(&srx);
321 call->server = server;
323 INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
324 queue_work(afs_wq, &call->work);
329 * deliver request data to a CB.InitCallBackState3 call
331 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
333 struct sockaddr_rxrpc srx;
334 struct afs_server *server;
342 rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx);
344 _enter("{%u}", call->unmarshall);
346 switch (call->unmarshall) {
349 call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL);
355 _debug("extract UUID");
356 ret = afs_extract_data(call, call->buffer,
357 11 * sizeof(__be32), false);
360 case -EAGAIN: return 0;
364 _debug("unmarshall UUID");
365 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
371 r->time_low = ntohl(b[0]);
372 r->time_mid = ntohl(b[1]);
373 r->time_hi_and_version = ntohl(b[2]);
374 r->clock_seq_hi_and_reserved = ntohl(b[3]);
375 r->clock_seq_low = ntohl(b[4]);
377 for (loop = 0; loop < 6; loop++)
378 r->node[loop] = ntohl(b[loop + 5]);
387 /* no unmarshalling required */
388 call->state = AFS_CALL_REPLYING;
390 /* we'll need the file server record as that tells us which set of
391 * vnodes to operate upon */
392 server = afs_find_server(&srx);
395 call->server = server;
397 INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
398 queue_work(afs_wq, &call->work);
403 * allow the fileserver to see if the cache manager is still alive
405 static void SRXAFSCB_Probe(struct work_struct *work)
407 struct afs_call *call = container_of(work, struct afs_call, work);
410 afs_send_empty_reply(call);
415 * deliver request data to a CB.Probe call
417 static int afs_deliver_cb_probe(struct afs_call *call)
423 ret = afs_extract_data(call, NULL, 0, false);
427 /* no unmarshalling required */
428 call->state = AFS_CALL_REPLYING;
430 INIT_WORK(&call->work, SRXAFSCB_Probe);
431 queue_work(afs_wq, &call->work);
436 * allow the fileserver to quickly find out if the fileserver has been rebooted
438 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
440 struct afs_call *call = container_of(work, struct afs_call, work);
441 struct afs_uuid *r = call->request;
449 if (memcmp(r, &afs_uuid, sizeof(afs_uuid)) == 0)
450 reply.match = htonl(0);
452 reply.match = htonl(1);
454 afs_send_simple_reply(call, &reply, sizeof(reply));
459 * deliver request data to a CB.ProbeUuid call
461 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
468 _enter("{%u}", call->unmarshall);
470 switch (call->unmarshall) {
473 call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL);
479 _debug("extract UUID");
480 ret = afs_extract_data(call, call->buffer,
481 11 * sizeof(__be32), false);
484 case -EAGAIN: return 0;
488 _debug("unmarshall UUID");
489 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
495 r->time_low = ntohl(b[0]);
496 r->time_mid = ntohl(b[1]);
497 r->time_hi_and_version = ntohl(b[2]);
498 r->clock_seq_hi_and_reserved = ntohl(b[3]);
499 r->clock_seq_low = ntohl(b[4]);
501 for (loop = 0; loop < 6; loop++)
502 r->node[loop] = ntohl(b[loop + 5]);
511 call->state = AFS_CALL_REPLYING;
513 INIT_WORK(&call->work, SRXAFSCB_ProbeUuid);
514 queue_work(afs_wq, &call->work);
519 * allow the fileserver to ask about the cache manager's capabilities
521 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
523 struct afs_interface *ifs;
524 struct afs_call *call = container_of(work, struct afs_call, work);
528 struct /* InterfaceAddr */ {
535 struct /* Capabilities */ {
544 ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
546 nifs = afs_get_ipv4_interfaces(ifs, 32, false);
554 memset(&reply, 0, sizeof(reply));
555 reply.ia.nifs = htonl(nifs);
557 reply.ia.uuid[0] = htonl(afs_uuid.time_low);
558 reply.ia.uuid[1] = htonl(afs_uuid.time_mid);
559 reply.ia.uuid[2] = htonl(afs_uuid.time_hi_and_version);
560 reply.ia.uuid[3] = htonl((s8) afs_uuid.clock_seq_hi_and_reserved);
561 reply.ia.uuid[4] = htonl((s8) afs_uuid.clock_seq_low);
562 for (loop = 0; loop < 6; loop++)
563 reply.ia.uuid[loop + 5] = htonl((s8) afs_uuid.node[loop]);
566 for (loop = 0; loop < nifs; loop++) {
567 reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
568 reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
569 reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
574 reply.cap.capcount = htonl(1);
575 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
576 afs_send_simple_reply(call, &reply, sizeof(reply));
582 * deliver request data to a CB.TellMeAboutYourself call
584 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
590 ret = afs_extract_data(call, NULL, 0, false);
594 /* no unmarshalling required */
595 call->state = AFS_CALL_REPLYING;
597 INIT_WORK(&call->work, SRXAFSCB_TellMeAboutYourself);
598 queue_work(afs_wq, &call->work);