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