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