afs: Need linux/random.h
[linux-2.6-block.git] / fs / afs / cmservice.c
CommitLineData
ec26815a 1/* AFS Cache Manager Service
1da177e4
LT
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>
5a0e3ad6 14#include <linux/slab.h>
1da177e4 15#include <linux/sched.h>
08e0e7c8 16#include <linux/ip.h>
1da177e4 17#include "internal.h"
08e0e7c8 18#include "afs_cm.h"
1da177e4 19
08e0e7c8
DH
20static int afs_deliver_cb_init_call_back_state(struct afs_call *,
21 struct sk_buff *, bool);
c35eccb1
DH
22static int afs_deliver_cb_init_call_back_state3(struct afs_call *,
23 struct sk_buff *, bool);
08e0e7c8
DH
24static int afs_deliver_cb_probe(struct afs_call *, struct sk_buff *, bool);
25static int afs_deliver_cb_callback(struct afs_call *, struct sk_buff *, bool);
9396d496 26static int afs_deliver_cb_probe_uuid(struct afs_call *, struct sk_buff *, bool);
7c80bcce
DH
27static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *,
28 struct sk_buff *, bool);
08e0e7c8 29static void afs_cm_destructor(struct afs_call *);
1da177e4 30
1da177e4 31/*
08e0e7c8 32 * CB.CallBack operation type
1da177e4 33 */
08e0e7c8 34static const struct afs_call_type afs_SRXCBCallBack = {
00d3b7a4 35 .name = "CB.CallBack",
08e0e7c8
DH
36 .deliver = afs_deliver_cb_callback,
37 .abort_to_error = afs_abort_to_error,
38 .destructor = afs_cm_destructor,
39};
1da177e4 40
1da177e4 41/*
08e0e7c8 42 * CB.InitCallBackState operation type
1da177e4 43 */
08e0e7c8 44static const struct afs_call_type afs_SRXCBInitCallBackState = {
00d3b7a4 45 .name = "CB.InitCallBackState",
08e0e7c8
DH
46 .deliver = afs_deliver_cb_init_call_back_state,
47 .abort_to_error = afs_abort_to_error,
48 .destructor = afs_cm_destructor,
49};
1da177e4 50
c35eccb1
DH
51/*
52 * CB.InitCallBackState3 operation type
53 */
54static 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
1da177e4 61/*
08e0e7c8 62 * CB.Probe operation type
1da177e4 63 */
08e0e7c8 64static const struct afs_call_type afs_SRXCBProbe = {
00d3b7a4 65 .name = "CB.Probe",
08e0e7c8
DH
66 .deliver = afs_deliver_cb_probe,
67 .abort_to_error = afs_abort_to_error,
68 .destructor = afs_cm_destructor,
69};
1da177e4 70
9396d496
DH
71/*
72 * CB.ProbeUuid operation type
73 */
74static 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
b908fe6b 81/*
7c80bcce 82 * CB.TellMeAboutYourself operation type
b908fe6b 83 */
7c80bcce
DH
84static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
85 .name = "CB.TellMeAboutYourself",
86 .deliver = afs_deliver_cb_tell_me_about_yourself,
b908fe6b
DH
87 .abort_to_error = afs_abort_to_error,
88 .destructor = afs_cm_destructor,
89};
90
1da177e4 91/*
08e0e7c8
DH
92 * route an incoming cache manager call
93 * - return T if supported, F if not
1da177e4 94 */
08e0e7c8 95bool afs_cm_incoming_call(struct afs_call *call)
1da177e4 96{
08e0e7c8
DH
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;
c35eccb1
DH
108 case CBInitCallBackState3:
109 call->type = &afs_SRXCBInitCallBackState3;
110 return true;
08e0e7c8
DH
111 case CBProbe:
112 call->type = &afs_SRXCBProbe;
113 return true;
7c80bcce
DH
114 case CBTellMeAboutYourself:
115 call->type = &afs_SRXCBTellMeAboutYourself;
b908fe6b 116 return true;
08e0e7c8
DH
117 default:
118 return false;
1da177e4 119 }
ec26815a 120}
1da177e4 121
1da177e4 122/*
08e0e7c8 123 * clean up a cache manager call
1da177e4 124 */
08e0e7c8 125static void afs_cm_destructor(struct afs_call *call)
1da177e4 126{
08e0e7c8
DH
127 _enter("");
128
6c67c7c3
DH
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
08e0e7c8
DH
138 afs_put_server(call->server);
139 call->server = NULL;
140 kfree(call->buffer);
141 call->buffer = NULL;
ec26815a 142}
1da177e4 143
1da177e4 144/*
08e0e7c8 145 * allow the fileserver to see if the cache manager is still alive
1da177e4 146 */
08e0e7c8 147static void SRXAFSCB_CallBack(struct work_struct *work)
1da177e4 148{
08e0e7c8 149 struct afs_call *call = container_of(work, struct afs_call, work);
1da177e4 150
08e0e7c8 151 _enter("");
1da177e4 152
08e0e7c8
DH
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);
1da177e4 159
08e0e7c8
DH
160 afs_break_callbacks(call->server, call->count, call->request);
161 _leave("");
ec26815a 162}
1da177e4 163
1da177e4 164/*
08e0e7c8 165 * deliver request data to a CB.CallBack call
1da177e4 166 */
08e0e7c8
DH
167static int afs_deliver_cb_callback(struct afs_call *call, struct sk_buff *skb,
168 bool last)
1da177e4 169{
08e0e7c8
DH
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);
372ee163
DH
188 if (ret < 0)
189 return ret;
1da177e4 190
08e0e7c8
DH
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);
372ee163
DH
206 if (ret < 0)
207 return ret;
1da177e4 208
08e0e7c8
DH
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;
1da177e4
LT
223 }
224
08e0e7c8
DH
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);
372ee163
DH
232 if (ret < 0)
233 return ret;
1da177e4 234
08e0e7c8
DH
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);
372ee163
DH
248 if (ret < 0)
249 return ret;
1da177e4 250
08e0e7c8
DH
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 }
1da177e4 259
08e0e7c8
DH
260 empty_cb_array:
261 call->offset = 0;
262 call->unmarshall++;
1da177e4 263
08e0e7c8 264 case 5:
372ee163
DH
265 ret = afs_data_complete(call, skb, last);
266 if (ret < 0)
267 return ret;
6c67c7c3
DH
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:
1da177e4
LT
278 break;
279 }
280
08e0e7c8 281 call->state = AFS_CALL_REPLYING;
1da177e4 282
08e0e7c8
DH
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);
0ad53eee 292 queue_work(afs_wq, &call->work);
08e0e7c8 293 return 0;
ec26815a 294}
1da177e4 295
1da177e4 296/*
08e0e7c8 297 * allow the fileserver to request callback state (re-)initialisation
1da177e4 298 */
08e0e7c8 299static void SRXAFSCB_InitCallBackState(struct work_struct *work)
1da177e4 300{
08e0e7c8 301 struct afs_call *call = container_of(work, struct afs_call, work);
1da177e4 302
08e0e7c8 303 _enter("{%p}", call->server);
1da177e4 304
08e0e7c8
DH
305 afs_init_callback_state(call->server);
306 afs_send_empty_reply(call);
307 _leave("");
ec26815a 308}
1da177e4 309
1da177e4 310/*
08e0e7c8 311 * deliver request data to a CB.InitCallBackState call
1da177e4 312 */
08e0e7c8
DH
313static int afs_deliver_cb_init_call_back_state(struct afs_call *call,
314 struct sk_buff *skb,
315 bool last)
1da177e4
LT
316{
317 struct afs_server *server;
08e0e7c8 318 struct in_addr addr;
372ee163 319 int ret;
1da177e4 320
08e0e7c8 321 _enter(",{%u},%d", skb->len, last);
1da177e4 322
372ee163
DH
323 ret = afs_data_complete(call, skb, last);
324 if (ret < 0)
325 return ret;
1da177e4 326
08e0e7c8
DH
327 /* no unmarshalling required */
328 call->state = AFS_CALL_REPLYING;
1da177e4 329
08e0e7c8
DH
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;
1da177e4 337
08e0e7c8 338 INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
0ad53eee 339 queue_work(afs_wq, &call->work);
08e0e7c8
DH
340 return 0;
341}
1da177e4 342
c35eccb1
DH
343/*
344 * deliver request data to a CB.InitCallBackState3 call
345 */
346static 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
372ee163
DH
355 /* There are some arguments that we ignore */
356 afs_data_consumed(call, skb);
c35eccb1 357 if (!last)
372ee163 358 return -EAGAIN;
c35eccb1
DH
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);
0ad53eee 372 queue_work(afs_wq, &call->work);
c35eccb1
DH
373 return 0;
374}
375
08e0e7c8
DH
376/*
377 * allow the fileserver to see if the cache manager is still alive
378 */
379static void SRXAFSCB_Probe(struct work_struct *work)
380{
381 struct afs_call *call = container_of(work, struct afs_call, work);
1da177e4 382
08e0e7c8
DH
383 _enter("");
384 afs_send_empty_reply(call);
385 _leave("");
386}
1da177e4 387
08e0e7c8
DH
388/*
389 * deliver request data to a CB.Probe call
390 */
391static int afs_deliver_cb_probe(struct afs_call *call, struct sk_buff *skb,
392 bool last)
393{
372ee163
DH
394 int ret;
395
08e0e7c8 396 _enter(",{%u},%d", skb->len, last);
1da177e4 397
372ee163
DH
398 ret = afs_data_complete(call, skb, last);
399 if (ret < 0)
400 return ret;
1da177e4 401
08e0e7c8
DH
402 /* no unmarshalling required */
403 call->state = AFS_CALL_REPLYING;
1da177e4 404
08e0e7c8 405 INIT_WORK(&call->work, SRXAFSCB_Probe);
0ad53eee 406 queue_work(afs_wq, &call->work);
08e0e7c8 407 return 0;
ec26815a 408}
b908fe6b 409
9396d496
DH
410/*
411 * allow the fileserver to quickly find out if the fileserver has been rebooted
412 */
413static 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
9396d496
DH
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 */
436static 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
372ee163
DH
446 ret = afs_data_complete(call, skb, last);
447 if (ret < 0)
448 return ret;
9396d496
DH
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
372ee163
DH
494 ret = afs_data_complete(call, skb, last);
495 if (ret < 0)
496 return ret;
9396d496
DH
497
498 call->state = AFS_CALL_REPLYING;
499
500 INIT_WORK(&call->work, SRXAFSCB_ProbeUuid);
0ad53eee 501 queue_work(afs_wq, &call->work);
9396d496
DH
502 return 0;
503}
504
b908fe6b
DH
505/*
506 * allow the fileserver to ask about the cache manager's capabilities
507 */
7c80bcce 508static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
b908fe6b
DH
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 }
5b35fad9 558 kfree(ifs);
b908fe6b
DH
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/*
7c80bcce 569 * deliver request data to a CB.TellMeAboutYourself call
b908fe6b 570 */
7c80bcce
DH
571static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call,
572 struct sk_buff *skb, bool last)
b908fe6b 573{
372ee163
DH
574 int ret;
575
b908fe6b
DH
576 _enter(",{%u},%d", skb->len, last);
577
372ee163
DH
578 ret = afs_data_complete(call, skb, last);
579 if (ret < 0)
580 return ret;
b908fe6b
DH
581
582 /* no unmarshalling required */
583 call->state = AFS_CALL_REPLYING;
584
7c80bcce 585 INIT_WORK(&call->work, SRXAFSCB_TellMeAboutYourself);
0ad53eee 586 queue_work(afs_wq, &call->work);
b908fe6b
DH
587 return 0;
588}