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