Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* AFS Cache Manager Service
3 *
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/sched.h>
12#include <linux/ip.h>
13#include "internal.h"
14#include "afs_cm.h"
15#include "protocol_yfs.h"
16#define RXRPC_TRACE_ONLY_DEFINE_ENUMS
17#include <trace/events/rxrpc.h>
18
19static int afs_deliver_cb_init_call_back_state(struct afs_call *);
20static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
21static int afs_deliver_cb_probe(struct afs_call *);
22static int afs_deliver_cb_callback(struct afs_call *);
23static int afs_deliver_cb_probe_uuid(struct afs_call *);
24static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
25static void afs_cm_destructor(struct afs_call *);
26static void SRXAFSCB_CallBack(struct work_struct *);
27static void SRXAFSCB_InitCallBackState(struct work_struct *);
28static void SRXAFSCB_Probe(struct work_struct *);
29static void SRXAFSCB_ProbeUuid(struct work_struct *);
30static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
31
32static int afs_deliver_yfs_cb_callback(struct afs_call *);
33
34/*
35 * CB.CallBack operation type
36 */
37static const struct afs_call_type afs_SRXCBCallBack = {
38 .name = "CB.CallBack",
39 .deliver = afs_deliver_cb_callback,
40 .destructor = afs_cm_destructor,
41 .work = SRXAFSCB_CallBack,
42};
43
44/*
45 * CB.InitCallBackState operation type
46 */
47static const struct afs_call_type afs_SRXCBInitCallBackState = {
48 .name = "CB.InitCallBackState",
49 .deliver = afs_deliver_cb_init_call_back_state,
50 .destructor = afs_cm_destructor,
51 .work = SRXAFSCB_InitCallBackState,
52};
53
54/*
55 * CB.InitCallBackState3 operation type
56 */
57static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
58 .name = "CB.InitCallBackState3",
59 .deliver = afs_deliver_cb_init_call_back_state3,
60 .destructor = afs_cm_destructor,
61 .work = SRXAFSCB_InitCallBackState,
62};
63
64/*
65 * CB.Probe operation type
66 */
67static const struct afs_call_type afs_SRXCBProbe = {
68 .name = "CB.Probe",
69 .deliver = afs_deliver_cb_probe,
70 .destructor = afs_cm_destructor,
71 .work = SRXAFSCB_Probe,
72};
73
74/*
75 * CB.ProbeUuid operation type
76 */
77static const struct afs_call_type afs_SRXCBProbeUuid = {
78 .name = "CB.ProbeUuid",
79 .deliver = afs_deliver_cb_probe_uuid,
80 .destructor = afs_cm_destructor,
81 .work = SRXAFSCB_ProbeUuid,
82};
83
84/*
85 * CB.TellMeAboutYourself operation type
86 */
87static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
88 .name = "CB.TellMeAboutYourself",
89 .deliver = afs_deliver_cb_tell_me_about_yourself,
90 .destructor = afs_cm_destructor,
91 .work = SRXAFSCB_TellMeAboutYourself,
92};
93
94/*
95 * YFS CB.CallBack operation type
96 */
97static const struct afs_call_type afs_SRXYFSCB_CallBack = {
98 .name = "YFSCB.CallBack",
99 .deliver = afs_deliver_yfs_cb_callback,
100 .destructor = afs_cm_destructor,
101 .work = SRXAFSCB_CallBack,
102};
103
104/*
105 * route an incoming cache manager call
106 * - return T if supported, F if not
107 */
108bool afs_cm_incoming_call(struct afs_call *call)
109{
110 _enter("{%u, CB.OP %u}", call->service_id, call->operation_ID);
111
112 switch (call->operation_ID) {
113 case CBCallBack:
114 call->type = &afs_SRXCBCallBack;
115 return true;
116 case CBInitCallBackState:
117 call->type = &afs_SRXCBInitCallBackState;
118 return true;
119 case CBInitCallBackState3:
120 call->type = &afs_SRXCBInitCallBackState3;
121 return true;
122 case CBProbe:
123 call->type = &afs_SRXCBProbe;
124 return true;
125 case CBProbeUuid:
126 call->type = &afs_SRXCBProbeUuid;
127 return true;
128 case CBTellMeAboutYourself:
129 call->type = &afs_SRXCBTellMeAboutYourself;
130 return true;
131 case YFSCBCallBack:
132 if (call->service_id != YFS_CM_SERVICE)
133 return false;
134 call->type = &afs_SRXYFSCB_CallBack;
135 return true;
136 default:
137 return false;
138 }
139}
140
141/*
142 * Find the server record by peer address and record a probe to the cache
143 * manager from a server.
144 */
145static int afs_find_cm_server_by_peer(struct afs_call *call)
146{
147 struct sockaddr_rxrpc srx;
148 struct afs_server *server;
149 struct rxrpc_peer *peer;
150
151 peer = rxrpc_kernel_get_call_peer(call->net->socket, call->rxcall);
152
153 server = afs_find_server(call->net, peer);
154 if (!server) {
155 trace_afs_cm_no_server(call, &srx);
156 return 0;
157 }
158
159 call->server = server;
160 return 0;
161}
162
163/*
164 * Find the server record by server UUID and record a probe to the cache
165 * manager from a server.
166 */
167static int afs_find_cm_server_by_uuid(struct afs_call *call,
168 struct afs_uuid *uuid)
169{
170 struct afs_server *server;
171
172 rcu_read_lock();
173 server = afs_find_server_by_uuid(call->net, call->request);
174 rcu_read_unlock();
175 if (!server) {
176 trace_afs_cm_no_server_u(call, call->request);
177 return 0;
178 }
179
180 call->server = server;
181 return 0;
182}
183
184/*
185 * Clean up a cache manager call.
186 */
187static void afs_cm_destructor(struct afs_call *call)
188{
189 kfree(call->buffer);
190 call->buffer = NULL;
191}
192
193/*
194 * Abort a service call from within an action function.
195 */
196static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
197 enum rxrpc_abort_reason why)
198{
199 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
200 abort_code, error, why);
201 afs_set_call_complete(call, error, 0);
202}
203
204/*
205 * The server supplied a list of callbacks that it wanted to break.
206 */
207static void SRXAFSCB_CallBack(struct work_struct *work)
208{
209 struct afs_call *call = container_of(work, struct afs_call, work);
210
211 _enter("");
212
213 /* We need to break the callbacks before sending the reply as the
214 * server holds up change visibility till it receives our reply so as
215 * to maintain cache coherency.
216 */
217 if (call->server) {
218 trace_afs_server(call->server->debug_id,
219 refcount_read(&call->server->ref),
220 atomic_read(&call->server->active),
221 afs_server_trace_callback);
222 afs_break_callbacks(call->server, call->count, call->request);
223 }
224
225 afs_send_empty_reply(call);
226 afs_put_call(call);
227 _leave("");
228}
229
230/*
231 * deliver request data to a CB.CallBack call
232 */
233static int afs_deliver_cb_callback(struct afs_call *call)
234{
235 struct afs_callback_break *cb;
236 __be32 *bp;
237 int ret, loop;
238
239 _enter("{%u}", call->unmarshall);
240
241 switch (call->unmarshall) {
242 case 0:
243 afs_extract_to_tmp(call);
244 call->unmarshall++;
245
246 /* extract the FID array and its count in two steps */
247 fallthrough;
248 case 1:
249 _debug("extract FID count");
250 ret = afs_extract_data(call, true);
251 if (ret < 0)
252 return ret;
253
254 call->count = ntohl(call->tmp);
255 _debug("FID count: %u", call->count);
256 if (call->count > AFSCBMAX)
257 return afs_protocol_error(call, afs_eproto_cb_fid_count);
258
259 call->buffer = kmalloc(array3_size(call->count, 3, 4),
260 GFP_KERNEL);
261 if (!call->buffer)
262 return -ENOMEM;
263 afs_extract_to_buf(call, call->count * 3 * 4);
264 call->unmarshall++;
265
266 fallthrough;
267 case 2:
268 _debug("extract FID array");
269 ret = afs_extract_data(call, true);
270 if (ret < 0)
271 return ret;
272
273 _debug("unmarshall FID array");
274 call->request = kcalloc(call->count,
275 sizeof(struct afs_callback_break),
276 GFP_KERNEL);
277 if (!call->request)
278 return -ENOMEM;
279
280 cb = call->request;
281 bp = call->buffer;
282 for (loop = call->count; loop > 0; loop--, cb++) {
283 cb->fid.vid = ntohl(*bp++);
284 cb->fid.vnode = ntohl(*bp++);
285 cb->fid.unique = ntohl(*bp++);
286 }
287
288 afs_extract_to_tmp(call);
289 call->unmarshall++;
290
291 /* extract the callback array and its count in two steps */
292 fallthrough;
293 case 3:
294 _debug("extract CB count");
295 ret = afs_extract_data(call, true);
296 if (ret < 0)
297 return ret;
298
299 call->count2 = ntohl(call->tmp);
300 _debug("CB count: %u", call->count2);
301 if (call->count2 != call->count && call->count2 != 0)
302 return afs_protocol_error(call, afs_eproto_cb_count);
303 call->iter = &call->def_iter;
304 iov_iter_discard(&call->def_iter, ITER_DEST, call->count2 * 3 * 4);
305 call->unmarshall++;
306
307 fallthrough;
308 case 4:
309 _debug("extract discard %zu/%u",
310 iov_iter_count(call->iter), call->count2 * 3 * 4);
311
312 ret = afs_extract_data(call, false);
313 if (ret < 0)
314 return ret;
315
316 call->unmarshall++;
317 fallthrough;
318
319 case 5:
320 break;
321 }
322
323 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
324 return afs_io_error(call, afs_io_error_cm_reply);
325
326 /* we'll need the file server record as that tells us which set of
327 * vnodes to operate upon */
328 return afs_find_cm_server_by_peer(call);
329}
330
331/*
332 * allow the fileserver to request callback state (re-)initialisation
333 */
334static void SRXAFSCB_InitCallBackState(struct work_struct *work)
335{
336 struct afs_call *call = container_of(work, struct afs_call, work);
337
338 _enter("{%p}", call->server);
339
340 if (call->server)
341 afs_init_callback_state(call->server);
342 afs_send_empty_reply(call);
343 afs_put_call(call);
344 _leave("");
345}
346
347/*
348 * deliver request data to a CB.InitCallBackState call
349 */
350static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
351{
352 int ret;
353
354 _enter("");
355
356 afs_extract_discard(call, 0);
357 ret = afs_extract_data(call, false);
358 if (ret < 0)
359 return ret;
360
361 /* we'll need the file server record as that tells us which set of
362 * vnodes to operate upon */
363 return afs_find_cm_server_by_peer(call);
364}
365
366/*
367 * deliver request data to a CB.InitCallBackState3 call
368 */
369static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
370{
371 struct afs_uuid *r;
372 unsigned loop;
373 __be32 *b;
374 int ret;
375
376 _enter("");
377
378 _enter("{%u}", call->unmarshall);
379
380 switch (call->unmarshall) {
381 case 0:
382 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
383 if (!call->buffer)
384 return -ENOMEM;
385 afs_extract_to_buf(call, 11 * sizeof(__be32));
386 call->unmarshall++;
387
388 fallthrough;
389 case 1:
390 _debug("extract UUID");
391 ret = afs_extract_data(call, false);
392 switch (ret) {
393 case 0: break;
394 case -EAGAIN: return 0;
395 default: return ret;
396 }
397
398 _debug("unmarshall UUID");
399 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
400 if (!call->request)
401 return -ENOMEM;
402
403 b = call->buffer;
404 r = call->request;
405 r->time_low = b[0];
406 r->time_mid = htons(ntohl(b[1]));
407 r->time_hi_and_version = htons(ntohl(b[2]));
408 r->clock_seq_hi_and_reserved = ntohl(b[3]);
409 r->clock_seq_low = ntohl(b[4]);
410
411 for (loop = 0; loop < 6; loop++)
412 r->node[loop] = ntohl(b[loop + 5]);
413
414 call->unmarshall++;
415 fallthrough;
416
417 case 2:
418 break;
419 }
420
421 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
422 return afs_io_error(call, afs_io_error_cm_reply);
423
424 /* we'll need the file server record as that tells us which set of
425 * vnodes to operate upon */
426 return afs_find_cm_server_by_uuid(call, call->request);
427}
428
429/*
430 * allow the fileserver to see if the cache manager is still alive
431 */
432static void SRXAFSCB_Probe(struct work_struct *work)
433{
434 struct afs_call *call = container_of(work, struct afs_call, work);
435
436 _enter("");
437 afs_send_empty_reply(call);
438 afs_put_call(call);
439 _leave("");
440}
441
442/*
443 * deliver request data to a CB.Probe call
444 */
445static int afs_deliver_cb_probe(struct afs_call *call)
446{
447 int ret;
448
449 _enter("");
450
451 afs_extract_discard(call, 0);
452 ret = afs_extract_data(call, false);
453 if (ret < 0)
454 return ret;
455
456 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
457 return afs_io_error(call, afs_io_error_cm_reply);
458 return afs_find_cm_server_by_peer(call);
459}
460
461/*
462 * Allow the fileserver to quickly find out if the cache manager has been
463 * rebooted.
464 */
465static void SRXAFSCB_ProbeUuid(struct work_struct *work)
466{
467 struct afs_call *call = container_of(work, struct afs_call, work);
468 struct afs_uuid *r = call->request;
469
470 _enter("");
471
472 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
473 afs_send_empty_reply(call);
474 else
475 afs_abort_service_call(call, 1, 1, afs_abort_probeuuid_negative);
476
477 afs_put_call(call);
478 _leave("");
479}
480
481/*
482 * deliver request data to a CB.ProbeUuid call
483 */
484static int afs_deliver_cb_probe_uuid(struct afs_call *call)
485{
486 struct afs_uuid *r;
487 unsigned loop;
488 __be32 *b;
489 int ret;
490
491 _enter("{%u}", call->unmarshall);
492
493 switch (call->unmarshall) {
494 case 0:
495 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
496 if (!call->buffer)
497 return -ENOMEM;
498 afs_extract_to_buf(call, 11 * sizeof(__be32));
499 call->unmarshall++;
500
501 fallthrough;
502 case 1:
503 _debug("extract UUID");
504 ret = afs_extract_data(call, false);
505 switch (ret) {
506 case 0: break;
507 case -EAGAIN: return 0;
508 default: return ret;
509 }
510
511 _debug("unmarshall UUID");
512 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
513 if (!call->request)
514 return -ENOMEM;
515
516 b = call->buffer;
517 r = call->request;
518 r->time_low = b[0];
519 r->time_mid = htons(ntohl(b[1]));
520 r->time_hi_and_version = htons(ntohl(b[2]));
521 r->clock_seq_hi_and_reserved = ntohl(b[3]);
522 r->clock_seq_low = ntohl(b[4]);
523
524 for (loop = 0; loop < 6; loop++)
525 r->node[loop] = ntohl(b[loop + 5]);
526
527 call->unmarshall++;
528 fallthrough;
529
530 case 2:
531 break;
532 }
533
534 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
535 return afs_io_error(call, afs_io_error_cm_reply);
536 return afs_find_cm_server_by_peer(call);
537}
538
539/*
540 * allow the fileserver to ask about the cache manager's capabilities
541 */
542static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
543{
544 struct afs_call *call = container_of(work, struct afs_call, work);
545 int loop;
546
547 struct {
548 struct /* InterfaceAddr */ {
549 __be32 nifs;
550 __be32 uuid[11];
551 __be32 ifaddr[32];
552 __be32 netmask[32];
553 __be32 mtu[32];
554 } ia;
555 struct /* Capabilities */ {
556 __be32 capcount;
557 __be32 caps[1];
558 } cap;
559 } reply;
560
561 _enter("");
562
563 memset(&reply, 0, sizeof(reply));
564
565 reply.ia.uuid[0] = call->net->uuid.time_low;
566 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
567 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
568 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
569 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
570 for (loop = 0; loop < 6; loop++)
571 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
572
573 reply.cap.capcount = htonl(1);
574 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
575 afs_send_simple_reply(call, &reply, sizeof(reply));
576 afs_put_call(call);
577 _leave("");
578}
579
580/*
581 * deliver request data to a CB.TellMeAboutYourself call
582 */
583static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
584{
585 int ret;
586
587 _enter("");
588
589 afs_extract_discard(call, 0);
590 ret = afs_extract_data(call, false);
591 if (ret < 0)
592 return ret;
593
594 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
595 return afs_io_error(call, afs_io_error_cm_reply);
596 return afs_find_cm_server_by_peer(call);
597}
598
599/*
600 * deliver request data to a YFS CB.CallBack call
601 */
602static int afs_deliver_yfs_cb_callback(struct afs_call *call)
603{
604 struct afs_callback_break *cb;
605 struct yfs_xdr_YFSFid *bp;
606 size_t size;
607 int ret, loop;
608
609 _enter("{%u}", call->unmarshall);
610
611 switch (call->unmarshall) {
612 case 0:
613 afs_extract_to_tmp(call);
614 call->unmarshall++;
615
616 /* extract the FID array and its count in two steps */
617 fallthrough;
618 case 1:
619 _debug("extract FID count");
620 ret = afs_extract_data(call, true);
621 if (ret < 0)
622 return ret;
623
624 call->count = ntohl(call->tmp);
625 _debug("FID count: %u", call->count);
626 if (call->count > YFSCBMAX)
627 return afs_protocol_error(call, afs_eproto_cb_fid_count);
628
629 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
630 call->buffer = kmalloc(size, GFP_KERNEL);
631 if (!call->buffer)
632 return -ENOMEM;
633 afs_extract_to_buf(call, size);
634 call->unmarshall++;
635
636 fallthrough;
637 case 2:
638 _debug("extract FID array");
639 ret = afs_extract_data(call, false);
640 if (ret < 0)
641 return ret;
642
643 _debug("unmarshall FID array");
644 call->request = kcalloc(call->count,
645 sizeof(struct afs_callback_break),
646 GFP_KERNEL);
647 if (!call->request)
648 return -ENOMEM;
649
650 cb = call->request;
651 bp = call->buffer;
652 for (loop = call->count; loop > 0; loop--, cb++) {
653 cb->fid.vid = xdr_to_u64(bp->volume);
654 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
655 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
656 cb->fid.unique = ntohl(bp->vnode.unique);
657 bp++;
658 }
659
660 afs_extract_to_tmp(call);
661 call->unmarshall++;
662 fallthrough;
663
664 case 3:
665 break;
666 }
667
668 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
669 return afs_io_error(call, afs_io_error_cm_reply);
670
671 /* We'll need the file server record as that tells us which set of
672 * vnodes to operate upon.
673 */
674 return afs_find_cm_server_by_peer(call);
675}
1/* AFS Cache Manager Service
2 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/sched.h>
16#include <linux/ip.h>
17#include "internal.h"
18#include "afs_cm.h"
19
20#if 0
21struct workqueue_struct *afs_cm_workqueue;
22#endif /* 0 */
23
24static int afs_deliver_cb_init_call_back_state(struct afs_call *,
25 struct sk_buff *, bool);
26static int afs_deliver_cb_init_call_back_state3(struct afs_call *,
27 struct sk_buff *, bool);
28static int afs_deliver_cb_probe(struct afs_call *, struct sk_buff *, bool);
29static int afs_deliver_cb_callback(struct afs_call *, struct sk_buff *, bool);
30static int afs_deliver_cb_probe_uuid(struct afs_call *, struct sk_buff *, bool);
31static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *,
32 struct sk_buff *, bool);
33static void afs_cm_destructor(struct afs_call *);
34
35/*
36 * CB.CallBack operation type
37 */
38static const struct afs_call_type afs_SRXCBCallBack = {
39 .name = "CB.CallBack",
40 .deliver = afs_deliver_cb_callback,
41 .abort_to_error = afs_abort_to_error,
42 .destructor = afs_cm_destructor,
43};
44
45/*
46 * CB.InitCallBackState operation type
47 */
48static const struct afs_call_type afs_SRXCBInitCallBackState = {
49 .name = "CB.InitCallBackState",
50 .deliver = afs_deliver_cb_init_call_back_state,
51 .abort_to_error = afs_abort_to_error,
52 .destructor = afs_cm_destructor,
53};
54
55/*
56 * CB.InitCallBackState3 operation type
57 */
58static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
59 .name = "CB.InitCallBackState3",
60 .deliver = afs_deliver_cb_init_call_back_state3,
61 .abort_to_error = afs_abort_to_error,
62 .destructor = afs_cm_destructor,
63};
64
65/*
66 * CB.Probe operation type
67 */
68static const struct afs_call_type afs_SRXCBProbe = {
69 .name = "CB.Probe",
70 .deliver = afs_deliver_cb_probe,
71 .abort_to_error = afs_abort_to_error,
72 .destructor = afs_cm_destructor,
73};
74
75/*
76 * CB.ProbeUuid operation type
77 */
78static const struct afs_call_type afs_SRXCBProbeUuid = {
79 .name = "CB.ProbeUuid",
80 .deliver = afs_deliver_cb_probe_uuid,
81 .abort_to_error = afs_abort_to_error,
82 .destructor = afs_cm_destructor,
83};
84
85/*
86 * CB.TellMeAboutYourself operation type
87 */
88static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
89 .name = "CB.TellMeAboutYourself",
90 .deliver = afs_deliver_cb_tell_me_about_yourself,
91 .abort_to_error = afs_abort_to_error,
92 .destructor = afs_cm_destructor,
93};
94
95/*
96 * route an incoming cache manager call
97 * - return T if supported, F if not
98 */
99bool afs_cm_incoming_call(struct afs_call *call)
100{
101 u32 operation_id = ntohl(call->operation_ID);
102
103 _enter("{CB.OP %u}", operation_id);
104
105 switch (operation_id) {
106 case CBCallBack:
107 call->type = &afs_SRXCBCallBack;
108 return true;
109 case CBInitCallBackState:
110 call->type = &afs_SRXCBInitCallBackState;
111 return true;
112 case CBInitCallBackState3:
113 call->type = &afs_SRXCBInitCallBackState3;
114 return true;
115 case CBProbe:
116 call->type = &afs_SRXCBProbe;
117 return true;
118 case CBTellMeAboutYourself:
119 call->type = &afs_SRXCBTellMeAboutYourself;
120 return true;
121 default:
122 return false;
123 }
124}
125
126/*
127 * clean up a cache manager call
128 */
129static void afs_cm_destructor(struct afs_call *call)
130{
131 _enter("");
132
133 afs_put_server(call->server);
134 call->server = NULL;
135 kfree(call->buffer);
136 call->buffer = NULL;
137}
138
139/*
140 * allow the fileserver to see if the cache manager is still alive
141 */
142static void SRXAFSCB_CallBack(struct work_struct *work)
143{
144 struct afs_call *call = container_of(work, struct afs_call, work);
145
146 _enter("");
147
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
152 * yet */
153 afs_send_empty_reply(call);
154
155 afs_break_callbacks(call->server, call->count, call->request);
156 _leave("");
157}
158
159/*
160 * deliver request data to a CB.CallBack call
161 */
162static int afs_deliver_cb_callback(struct afs_call *call, struct sk_buff *skb,
163 bool last)
164{
165 struct afs_callback *cb;
166 struct afs_server *server;
167 struct in_addr addr;
168 __be32 *bp;
169 u32 tmp;
170 int ret, loop;
171
172 _enter("{%u},{%u},%d", call->unmarshall, skb->len, last);
173
174 switch (call->unmarshall) {
175 case 0:
176 call->offset = 0;
177 call->unmarshall++;
178
179 /* extract the FID array and its count in two steps */
180 case 1:
181 _debug("extract FID count");
182 ret = afs_extract_data(call, skb, last, &call->tmp, 4);
183 switch (ret) {
184 case 0: break;
185 case -EAGAIN: return 0;
186 default: return ret;
187 }
188
189 call->count = ntohl(call->tmp);
190 _debug("FID count: %u", call->count);
191 if (call->count > AFSCBMAX)
192 return -EBADMSG;
193
194 call->buffer = kmalloc(call->count * 3 * 4, GFP_KERNEL);
195 if (!call->buffer)
196 return -ENOMEM;
197 call->offset = 0;
198 call->unmarshall++;
199
200 case 2:
201 _debug("extract FID array");
202 ret = afs_extract_data(call, skb, last, call->buffer,
203 call->count * 3 * 4);
204 switch (ret) {
205 case 0: break;
206 case -EAGAIN: return 0;
207 default: return ret;
208 }
209
210 _debug("unmarshall FID array");
211 call->request = kcalloc(call->count,
212 sizeof(struct afs_callback),
213 GFP_KERNEL);
214 if (!call->request)
215 return -ENOMEM;
216
217 cb = call->request;
218 bp = call->buffer;
219 for (loop = call->count; loop > 0; loop--, cb++) {
220 cb->fid.vid = ntohl(*bp++);
221 cb->fid.vnode = ntohl(*bp++);
222 cb->fid.unique = ntohl(*bp++);
223 cb->type = AFSCM_CB_UNTYPED;
224 }
225
226 call->offset = 0;
227 call->unmarshall++;
228
229 /* extract the callback array and its count in two steps */
230 case 3:
231 _debug("extract CB count");
232 ret = afs_extract_data(call, skb, last, &call->tmp, 4);
233 switch (ret) {
234 case 0: break;
235 case -EAGAIN: return 0;
236 default: return ret;
237 }
238
239 tmp = ntohl(call->tmp);
240 _debug("CB count: %u", tmp);
241 if (tmp != call->count && tmp != 0)
242 return -EBADMSG;
243 call->offset = 0;
244 call->unmarshall++;
245 if (tmp == 0)
246 goto empty_cb_array;
247
248 case 4:
249 _debug("extract CB array");
250 ret = afs_extract_data(call, skb, last, call->request,
251 call->count * 3 * 4);
252 switch (ret) {
253 case 0: break;
254 case -EAGAIN: return 0;
255 default: return ret;
256 }
257
258 _debug("unmarshall CB array");
259 cb = call->request;
260 bp = call->buffer;
261 for (loop = call->count; loop > 0; loop--, cb++) {
262 cb->version = ntohl(*bp++);
263 cb->expiry = ntohl(*bp++);
264 cb->type = ntohl(*bp++);
265 }
266
267 empty_cb_array:
268 call->offset = 0;
269 call->unmarshall++;
270
271 case 5:
272 _debug("trailer");
273 if (skb->len != 0)
274 return -EBADMSG;
275 break;
276 }
277
278 if (!last)
279 return 0;
280
281 call->state = AFS_CALL_REPLYING;
282
283 /* we'll need the file server record as that tells us which set of
284 * vnodes to operate upon */
285 memcpy(&addr, &ip_hdr(skb)->saddr, 4);
286 server = afs_find_server(&addr);
287 if (!server)
288 return -ENOTCONN;
289 call->server = server;
290
291 INIT_WORK(&call->work, SRXAFSCB_CallBack);
292 queue_work(afs_wq, &call->work);
293 return 0;
294}
295
296/*
297 * allow the fileserver to request callback state (re-)initialisation
298 */
299static void SRXAFSCB_InitCallBackState(struct work_struct *work)
300{
301 struct afs_call *call = container_of(work, struct afs_call, work);
302
303 _enter("{%p}", call->server);
304
305 afs_init_callback_state(call->server);
306 afs_send_empty_reply(call);
307 _leave("");
308}
309
310/*
311 * deliver request data to a CB.InitCallBackState call
312 */
313static int afs_deliver_cb_init_call_back_state(struct afs_call *call,
314 struct sk_buff *skb,
315 bool last)
316{
317 struct afs_server *server;
318 struct in_addr addr;
319
320 _enter(",{%u},%d", skb->len, last);
321
322 if (skb->len > 0)
323 return -EBADMSG;
324 if (!last)
325 return 0;
326
327 /* no unmarshalling required */
328 call->state = AFS_CALL_REPLYING;
329
330 /* we'll need the file server record as that tells us which set of
331 * vnodes to operate upon */
332 memcpy(&addr, &ip_hdr(skb)->saddr, 4);
333 server = afs_find_server(&addr);
334 if (!server)
335 return -ENOTCONN;
336 call->server = server;
337
338 INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
339 queue_work(afs_wq, &call->work);
340 return 0;
341}
342
343/*
344 * deliver request data to a CB.InitCallBackState3 call
345 */
346static int afs_deliver_cb_init_call_back_state3(struct afs_call *call,
347 struct sk_buff *skb,
348 bool last)
349{
350 struct afs_server *server;
351 struct in_addr addr;
352
353 _enter(",{%u},%d", skb->len, last);
354
355 if (!last)
356 return 0;
357
358 /* no unmarshalling required */
359 call->state = AFS_CALL_REPLYING;
360
361 /* we'll need the file server record as that tells us which set of
362 * vnodes to operate upon */
363 memcpy(&addr, &ip_hdr(skb)->saddr, 4);
364 server = afs_find_server(&addr);
365 if (!server)
366 return -ENOTCONN;
367 call->server = server;
368
369 INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
370 queue_work(afs_wq, &call->work);
371 return 0;
372}
373
374/*
375 * allow the fileserver to see if the cache manager is still alive
376 */
377static void SRXAFSCB_Probe(struct work_struct *work)
378{
379 struct afs_call *call = container_of(work, struct afs_call, work);
380
381 _enter("");
382 afs_send_empty_reply(call);
383 _leave("");
384}
385
386/*
387 * deliver request data to a CB.Probe call
388 */
389static int afs_deliver_cb_probe(struct afs_call *call, struct sk_buff *skb,
390 bool last)
391{
392 _enter(",{%u},%d", skb->len, last);
393
394 if (skb->len > 0)
395 return -EBADMSG;
396 if (!last)
397 return 0;
398
399 /* no unmarshalling required */
400 call->state = AFS_CALL_REPLYING;
401
402 INIT_WORK(&call->work, SRXAFSCB_Probe);
403 queue_work(afs_wq, &call->work);
404 return 0;
405}
406
407/*
408 * allow the fileserver to quickly find out if the fileserver has been rebooted
409 */
410static void SRXAFSCB_ProbeUuid(struct work_struct *work)
411{
412 struct afs_call *call = container_of(work, struct afs_call, work);
413 struct afs_uuid *r = call->request;
414
415 struct {
416 __be32 match;
417 } reply;
418
419 _enter("");
420
421
422 if (memcmp(r, &afs_uuid, sizeof(afs_uuid)) == 0)
423 reply.match = htonl(0);
424 else
425 reply.match = htonl(1);
426
427 afs_send_simple_reply(call, &reply, sizeof(reply));
428 _leave("");
429}
430
431/*
432 * deliver request data to a CB.ProbeUuid call
433 */
434static int afs_deliver_cb_probe_uuid(struct afs_call *call, struct sk_buff *skb,
435 bool last)
436{
437 struct afs_uuid *r;
438 unsigned loop;
439 __be32 *b;
440 int ret;
441
442 _enter("{%u},{%u},%d", call->unmarshall, skb->len, last);
443
444 if (skb->len > 0)
445 return -EBADMSG;
446 if (!last)
447 return 0;
448
449 switch (call->unmarshall) {
450 case 0:
451 call->offset = 0;
452 call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL);
453 if (!call->buffer)
454 return -ENOMEM;
455 call->unmarshall++;
456
457 case 1:
458 _debug("extract UUID");
459 ret = afs_extract_data(call, skb, last, call->buffer,
460 11 * sizeof(__be32));
461 switch (ret) {
462 case 0: break;
463 case -EAGAIN: return 0;
464 default: return ret;
465 }
466
467 _debug("unmarshall UUID");
468 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
469 if (!call->request)
470 return -ENOMEM;
471
472 b = call->buffer;
473 r = call->request;
474 r->time_low = ntohl(b[0]);
475 r->time_mid = ntohl(b[1]);
476 r->time_hi_and_version = ntohl(b[2]);
477 r->clock_seq_hi_and_reserved = ntohl(b[3]);
478 r->clock_seq_low = ntohl(b[4]);
479
480 for (loop = 0; loop < 6; loop++)
481 r->node[loop] = ntohl(b[loop + 5]);
482
483 call->offset = 0;
484 call->unmarshall++;
485
486 case 2:
487 _debug("trailer");
488 if (skb->len != 0)
489 return -EBADMSG;
490 break;
491 }
492
493 if (!last)
494 return 0;
495
496 call->state = AFS_CALL_REPLYING;
497
498 INIT_WORK(&call->work, SRXAFSCB_ProbeUuid);
499 queue_work(afs_wq, &call->work);
500 return 0;
501}
502
503/*
504 * allow the fileserver to ask about the cache manager's capabilities
505 */
506static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
507{
508 struct afs_interface *ifs;
509 struct afs_call *call = container_of(work, struct afs_call, work);
510 int loop, nifs;
511
512 struct {
513 struct /* InterfaceAddr */ {
514 __be32 nifs;
515 __be32 uuid[11];
516 __be32 ifaddr[32];
517 __be32 netmask[32];
518 __be32 mtu[32];
519 } ia;
520 struct /* Capabilities */ {
521 __be32 capcount;
522 __be32 caps[1];
523 } cap;
524 } reply;
525
526 _enter("");
527
528 nifs = 0;
529 ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
530 if (ifs) {
531 nifs = afs_get_ipv4_interfaces(ifs, 32, false);
532 if (nifs < 0) {
533 kfree(ifs);
534 ifs = NULL;
535 nifs = 0;
536 }
537 }
538
539 memset(&reply, 0, sizeof(reply));
540 reply.ia.nifs = htonl(nifs);
541
542 reply.ia.uuid[0] = htonl(afs_uuid.time_low);
543 reply.ia.uuid[1] = htonl(afs_uuid.time_mid);
544 reply.ia.uuid[2] = htonl(afs_uuid.time_hi_and_version);
545 reply.ia.uuid[3] = htonl((s8) afs_uuid.clock_seq_hi_and_reserved);
546 reply.ia.uuid[4] = htonl((s8) afs_uuid.clock_seq_low);
547 for (loop = 0; loop < 6; loop++)
548 reply.ia.uuid[loop + 5] = htonl((s8) afs_uuid.node[loop]);
549
550 if (ifs) {
551 for (loop = 0; loop < nifs; loop++) {
552 reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
553 reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
554 reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
555 }
556 kfree(ifs);
557 }
558
559 reply.cap.capcount = htonl(1);
560 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
561 afs_send_simple_reply(call, &reply, sizeof(reply));
562
563 _leave("");
564}
565
566/*
567 * deliver request data to a CB.TellMeAboutYourself call
568 */
569static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call,
570 struct sk_buff *skb, bool last)
571{
572 _enter(",{%u},%d", skb->len, last);
573
574 if (skb->len > 0)
575 return -EBADMSG;
576 if (!last)
577 return 0;
578
579 /* no unmarshalling required */
580 call->state = AFS_CALL_REPLYING;
581
582 INIT_WORK(&call->work, SRXAFSCB_TellMeAboutYourself);
583 queue_work(afs_wq, &call->work);
584 return 0;
585}