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