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