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