Merge tag 'afs-next-20171113' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowel...
[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
20 static int afs_deliver_cb_init_call_back_state(struct afs_call *);
21 static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
22 static int afs_deliver_cb_probe(struct afs_call *);
23 static int afs_deliver_cb_callback(struct afs_call *);
24 static int afs_deliver_cb_probe_uuid(struct afs_call *);
25 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
26 static void afs_cm_destructor(struct afs_call *);
27 static void SRXAFSCB_CallBack(struct work_struct *);
28 static void SRXAFSCB_InitCallBackState(struct work_struct *);
29 static void SRXAFSCB_Probe(struct work_struct *);
30 static void SRXAFSCB_ProbeUuid(struct work_struct *);
31 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
32
33 #define CM_NAME(name) \
34         const char afs_SRXCB##name##_name[] __tracepoint_string =       \
35                 "CB." #name
36
37 /*
38  * CB.CallBack operation type
39  */
40 static CM_NAME(CallBack);
41 static const struct afs_call_type afs_SRXCBCallBack = {
42         .name           = afs_SRXCBCallBack_name,
43         .deliver        = afs_deliver_cb_callback,
44         .destructor     = afs_cm_destructor,
45         .work           = SRXAFSCB_CallBack,
46 };
47
48 /*
49  * CB.InitCallBackState operation type
50  */
51 static CM_NAME(InitCallBackState);
52 static const struct afs_call_type afs_SRXCBInitCallBackState = {
53         .name           = afs_SRXCBInitCallBackState_name,
54         .deliver        = afs_deliver_cb_init_call_back_state,
55         .destructor     = afs_cm_destructor,
56         .work           = SRXAFSCB_InitCallBackState,
57 };
58
59 /*
60  * CB.InitCallBackState3 operation type
61  */
62 static CM_NAME(InitCallBackState3);
63 static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
64         .name           = afs_SRXCBInitCallBackState3_name,
65         .deliver        = afs_deliver_cb_init_call_back_state3,
66         .destructor     = afs_cm_destructor,
67         .work           = SRXAFSCB_InitCallBackState,
68 };
69
70 /*
71  * CB.Probe operation type
72  */
73 static CM_NAME(Probe);
74 static const struct afs_call_type afs_SRXCBProbe = {
75         .name           = afs_SRXCBProbe_name,
76         .deliver        = afs_deliver_cb_probe,
77         .destructor     = afs_cm_destructor,
78         .work           = SRXAFSCB_Probe,
79 };
80
81 /*
82  * CB.ProbeUuid operation type
83  */
84 static CM_NAME(ProbeUuid);
85 static const struct afs_call_type afs_SRXCBProbeUuid = {
86         .name           = afs_SRXCBProbeUuid_name,
87         .deliver        = afs_deliver_cb_probe_uuid,
88         .destructor     = afs_cm_destructor,
89         .work           = SRXAFSCB_ProbeUuid,
90 };
91
92 /*
93  * CB.TellMeAboutYourself operation type
94  */
95 static CM_NAME(TellMeAboutYourself);
96 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
97         .name           = afs_SRXCBTellMeAboutYourself_name,
98         .deliver        = afs_deliver_cb_tell_me_about_yourself,
99         .destructor     = afs_cm_destructor,
100         .work           = SRXAFSCB_TellMeAboutYourself,
101 };
102
103 /*
104  * route an incoming cache manager call
105  * - return T if supported, F if not
106  */
107 bool afs_cm_incoming_call(struct afs_call *call)
108 {
109         _enter("{CB.OP %u}", call->operation_ID);
110
111         switch (call->operation_ID) {
112         case CBCallBack:
113                 call->type = &afs_SRXCBCallBack;
114                 return true;
115         case CBInitCallBackState:
116                 call->type = &afs_SRXCBInitCallBackState;
117                 return true;
118         case CBInitCallBackState3:
119                 call->type = &afs_SRXCBInitCallBackState3;
120                 return true;
121         case CBProbe:
122                 call->type = &afs_SRXCBProbe;
123                 return true;
124         case CBProbeUuid:
125                 call->type = &afs_SRXCBProbeUuid;
126                 return true;
127         case CBTellMeAboutYourself:
128                 call->type = &afs_SRXCBTellMeAboutYourself;
129                 return true;
130         default:
131                 return false;
132         }
133 }
134
135 /*
136  * clean up a cache manager call
137  */
138 static void afs_cm_destructor(struct afs_call *call)
139 {
140         _enter("");
141
142         /* Break the callbacks here so that we do it after the final ACK is
143          * received.  The step number here must match the final number in
144          * afs_deliver_cb_callback().
145          */
146         if (call->unmarshall == 5) {
147                 ASSERT(call->cm_server && call->count && call->request);
148                 afs_break_callbacks(call->cm_server, call->count, call->request);
149         }
150
151         kfree(call->buffer);
152         call->buffer = NULL;
153 }
154
155 /*
156  * The server supplied a list of callbacks that it wanted to break.
157  */
158 static void SRXAFSCB_CallBack(struct work_struct *work)
159 {
160         struct afs_call *call = container_of(work, struct afs_call, work);
161
162         _enter("");
163
164         /* be sure to send the reply *before* attempting to spam the AFS server
165          * with FSFetchStatus requests on the vnodes with broken callbacks lest
166          * the AFS server get into a vicious cycle of trying to break further
167          * callbacks because it hadn't received completion of the CBCallBack op
168          * yet */
169         afs_send_empty_reply(call);
170
171         afs_break_callbacks(call->cm_server, call->count, call->request);
172         afs_put_call(call);
173         _leave("");
174 }
175
176 /*
177  * deliver request data to a CB.CallBack call
178  */
179 static int afs_deliver_cb_callback(struct afs_call *call)
180 {
181         struct sockaddr_rxrpc srx;
182         struct afs_callback *cb;
183         struct afs_server *server;
184         __be32 *bp;
185         int ret, loop;
186
187         _enter("{%u}", call->unmarshall);
188
189         switch (call->unmarshall) {
190         case 0:
191                 call->offset = 0;
192                 call->unmarshall++;
193
194                 /* extract the FID array and its count in two steps */
195         case 1:
196                 _debug("extract FID count");
197                 ret = afs_extract_data(call, &call->tmp, 4, true);
198                 if (ret < 0)
199                         return ret;
200
201                 call->count = ntohl(call->tmp);
202                 _debug("FID count: %u", call->count);
203                 if (call->count > AFSCBMAX)
204                         return -EBADMSG;
205
206                 call->buffer = kmalloc(call->count * 3 * 4, GFP_KERNEL);
207                 if (!call->buffer)
208                         return -ENOMEM;
209                 call->offset = 0;
210                 call->unmarshall++;
211
212         case 2:
213                 _debug("extract FID array");
214                 ret = afs_extract_data(call, call->buffer,
215                                        call->count * 3 * 4, true);
216                 if (ret < 0)
217                         return ret;
218
219                 _debug("unmarshall FID array");
220                 call->request = kcalloc(call->count,
221                                         sizeof(struct afs_callback),
222                                         GFP_KERNEL);
223                 if (!call->request)
224                         return -ENOMEM;
225
226                 cb = call->request;
227                 bp = call->buffer;
228                 for (loop = call->count; loop > 0; loop--, cb++) {
229                         cb->fid.vid     = ntohl(*bp++);
230                         cb->fid.vnode   = ntohl(*bp++);
231                         cb->fid.unique  = ntohl(*bp++);
232                         cb->type        = AFSCM_CB_UNTYPED;
233                 }
234
235                 call->offset = 0;
236                 call->unmarshall++;
237
238                 /* extract the callback array and its count in two steps */
239         case 3:
240                 _debug("extract CB count");
241                 ret = afs_extract_data(call, &call->tmp, 4, true);
242                 if (ret < 0)
243                         return ret;
244
245                 call->count2 = ntohl(call->tmp);
246                 _debug("CB count: %u", call->count2);
247                 if (call->count2 != call->count && call->count2 != 0)
248                         return -EBADMSG;
249                 call->offset = 0;
250                 call->unmarshall++;
251
252         case 4:
253                 _debug("extract CB array");
254                 ret = afs_extract_data(call, call->buffer,
255                                        call->count2 * 3 * 4, false);
256                 if (ret < 0)
257                         return ret;
258
259                 _debug("unmarshall CB array");
260                 cb = call->request;
261                 bp = call->buffer;
262                 for (loop = call->count2; loop > 0; loop--, cb++) {
263                         cb->version     = ntohl(*bp++);
264                         cb->expiry      = ntohl(*bp++);
265                         cb->type        = ntohl(*bp++);
266                 }
267
268                 call->offset = 0;
269                 call->unmarshall++;
270
271                 /* Record that the message was unmarshalled successfully so
272                  * that the call destructor can know do the callback breaking
273                  * work, even if the final ACK isn't received.
274                  *
275                  * If the step number changes, then afs_cm_destructor() must be
276                  * updated also.
277                  */
278                 call->unmarshall++;
279         case 5:
280                 break;
281         }
282
283         if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
284                 return -EIO;
285
286         /* we'll need the file server record as that tells us which set of
287          * vnodes to operate upon */
288         rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
289         server = afs_find_server(call->net, &srx);
290         if (!server)
291                 return -ENOTCONN;
292         call->cm_server = server;
293
294         return afs_queue_call_work(call);
295 }
296
297 /*
298  * allow the fileserver to request callback state (re-)initialisation
299  */
300 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
301 {
302         struct afs_call *call = container_of(work, struct afs_call, work);
303
304         _enter("{%p}", call->cm_server);
305
306         afs_init_callback_state(call->cm_server);
307         afs_send_empty_reply(call);
308         afs_put_call(call);
309         _leave("");
310 }
311
312 /*
313  * deliver request data to a CB.InitCallBackState call
314  */
315 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
316 {
317         struct sockaddr_rxrpc srx;
318         struct afs_server *server;
319         int ret;
320
321         _enter("");
322
323         rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
324
325         ret = afs_extract_data(call, NULL, 0, false);
326         if (ret < 0)
327                 return ret;
328
329         /* we'll need the file server record as that tells us which set of
330          * vnodes to operate upon */
331         server = afs_find_server(call->net, &srx);
332         if (!server)
333                 return -ENOTCONN;
334         call->cm_server = server;
335
336         return afs_queue_call_work(call);
337 }
338
339 /*
340  * deliver request data to a CB.InitCallBackState3 call
341  */
342 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
343 {
344         struct sockaddr_rxrpc srx;
345         struct afs_server *server;
346         struct afs_uuid *r;
347         unsigned loop;
348         __be32 *b;
349         int ret;
350
351         _enter("");
352
353         _enter("{%u}", call->unmarshall);
354
355         switch (call->unmarshall) {
356         case 0:
357                 call->offset = 0;
358                 call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL);
359                 if (!call->buffer)
360                         return -ENOMEM;
361                 call->unmarshall++;
362
363         case 1:
364                 _debug("extract UUID");
365                 ret = afs_extract_data(call, call->buffer,
366                                        11 * sizeof(__be32), false);
367                 switch (ret) {
368                 case 0:         break;
369                 case -EAGAIN:   return 0;
370                 default:        return ret;
371                 }
372
373                 _debug("unmarshall UUID");
374                 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
375                 if (!call->request)
376                         return -ENOMEM;
377
378                 b = call->buffer;
379                 r = call->request;
380                 r->time_low                     = b[0];
381                 r->time_mid                     = htons(ntohl(b[1]));
382                 r->time_hi_and_version          = htons(ntohl(b[2]));
383                 r->clock_seq_hi_and_reserved    = ntohl(b[3]);
384                 r->clock_seq_low                = ntohl(b[4]);
385
386                 for (loop = 0; loop < 6; loop++)
387                         r->node[loop] = ntohl(b[loop + 5]);
388
389                 call->offset = 0;
390                 call->unmarshall++;
391
392         case 2:
393                 break;
394         }
395
396         if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
397                 return -EIO;
398
399         /* we'll need the file server record as that tells us which set of
400          * vnodes to operate upon */
401         rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
402         server = afs_find_server(call->net, &srx);
403         if (!server)
404                 return -ENOTCONN;
405         call->cm_server = server;
406
407         return afs_queue_call_work(call);
408 }
409
410 /*
411  * allow the fileserver to see if the cache manager is still alive
412  */
413 static void SRXAFSCB_Probe(struct work_struct *work)
414 {
415         struct afs_call *call = container_of(work, struct afs_call, work);
416
417         _enter("");
418         afs_send_empty_reply(call);
419         afs_put_call(call);
420         _leave("");
421 }
422
423 /*
424  * deliver request data to a CB.Probe call
425  */
426 static int afs_deliver_cb_probe(struct afs_call *call)
427 {
428         int ret;
429
430         _enter("");
431
432         ret = afs_extract_data(call, NULL, 0, false);
433         if (ret < 0)
434                 return ret;
435
436         if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
437                 return -EIO;
438
439         return afs_queue_call_work(call);
440 }
441
442 /*
443  * allow the fileserver to quickly find out if the fileserver has been rebooted
444  */
445 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
446 {
447         struct afs_call *call = container_of(work, struct afs_call, work);
448         struct afs_uuid *r = call->request;
449
450         struct {
451                 __be32  match;
452         } reply;
453
454         _enter("");
455
456         if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
457                 reply.match = htonl(0);
458         else
459                 reply.match = htonl(1);
460
461         afs_send_simple_reply(call, &reply, sizeof(reply));
462         afs_put_call(call);
463         _leave("");
464 }
465
466 /*
467  * deliver request data to a CB.ProbeUuid call
468  */
469 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
470 {
471         struct afs_uuid *r;
472         unsigned loop;
473         __be32 *b;
474         int ret;
475
476         _enter("{%u}", call->unmarshall);
477
478         switch (call->unmarshall) {
479         case 0:
480                 call->offset = 0;
481                 call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL);
482                 if (!call->buffer)
483                         return -ENOMEM;
484                 call->unmarshall++;
485
486         case 1:
487                 _debug("extract UUID");
488                 ret = afs_extract_data(call, call->buffer,
489                                        11 * sizeof(__be32), false);
490                 switch (ret) {
491                 case 0:         break;
492                 case -EAGAIN:   return 0;
493                 default:        return ret;
494                 }
495
496                 _debug("unmarshall UUID");
497                 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
498                 if (!call->request)
499                         return -ENOMEM;
500
501                 b = call->buffer;
502                 r = call->request;
503                 r->time_low                     = ntohl(b[0]);
504                 r->time_mid                     = ntohl(b[1]);
505                 r->time_hi_and_version          = ntohl(b[2]);
506                 r->clock_seq_hi_and_reserved    = ntohl(b[3]);
507                 r->clock_seq_low                = ntohl(b[4]);
508
509                 for (loop = 0; loop < 6; loop++)
510                         r->node[loop] = ntohl(b[loop + 5]);
511
512                 call->offset = 0;
513                 call->unmarshall++;
514
515         case 2:
516                 break;
517         }
518
519         if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
520                 return -EIO;
521
522         return afs_queue_call_work(call);
523 }
524
525 /*
526  * allow the fileserver to ask about the cache manager's capabilities
527  */
528 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
529 {
530         struct afs_interface *ifs;
531         struct afs_call *call = container_of(work, struct afs_call, work);
532         int loop, nifs;
533
534         struct {
535                 struct /* InterfaceAddr */ {
536                         __be32 nifs;
537                         __be32 uuid[11];
538                         __be32 ifaddr[32];
539                         __be32 netmask[32];
540                         __be32 mtu[32];
541                 } ia;
542                 struct /* Capabilities */ {
543                         __be32 capcount;
544                         __be32 caps[1];
545                 } cap;
546         } reply;
547
548         _enter("");
549
550         nifs = 0;
551         ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
552         if (ifs) {
553                 nifs = afs_get_ipv4_interfaces(ifs, 32, false);
554                 if (nifs < 0) {
555                         kfree(ifs);
556                         ifs = NULL;
557                         nifs = 0;
558                 }
559         }
560
561         memset(&reply, 0, sizeof(reply));
562         reply.ia.nifs = htonl(nifs);
563
564         reply.ia.uuid[0] = call->net->uuid.time_low;
565         reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
566         reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
567         reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
568         reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
569         for (loop = 0; loop < 6; loop++)
570                 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
571
572         if (ifs) {
573                 for (loop = 0; loop < nifs; loop++) {
574                         reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
575                         reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
576                         reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
577                 }
578                 kfree(ifs);
579         }
580
581         reply.cap.capcount = htonl(1);
582         reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
583         afs_send_simple_reply(call, &reply, sizeof(reply));
584         afs_put_call(call);
585         _leave("");
586 }
587
588 /*
589  * deliver request data to a CB.TellMeAboutYourself call
590  */
591 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
592 {
593         int ret;
594
595         _enter("");
596
597         ret = afs_extract_data(call, NULL, 0, false);
598         if (ret < 0)
599                 return ret;
600
601         if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
602                 return -EIO;
603
604         return afs_queue_call_work(call);
605 }