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