1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS Cache Manager Service
3 *
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include <linux/ip.h>
13 #include "internal.h"
14 #include "afs_cm.h"
15 #include "protocol_yfs.h"
16 #define RXRPC_TRACE_ONLY_DEFINE_ENUMS
17 #include <trace/events/rxrpc.h>
18
19 static int afs_deliver_cb_init_call_back_state(struct afs_call *);
20 static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
21 static int afs_deliver_cb_probe(struct afs_call *);
22 static int afs_deliver_cb_callback(struct afs_call *);
23 static int afs_deliver_cb_probe_uuid(struct afs_call *);
24 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
25 static void afs_cm_destructor(struct afs_call *);
26 static void SRXAFSCB_CallBack(struct work_struct *);
27 static void SRXAFSCB_InitCallBackState(struct work_struct *);
28 static void SRXAFSCB_Probe(struct work_struct *);
29 static void SRXAFSCB_ProbeUuid(struct work_struct *);
30 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
31
32 static int afs_deliver_yfs_cb_callback(struct afs_call *);
33
34 /*
35 * CB.CallBack operation type
36 */
37 static const struct afs_call_type afs_SRXCBCallBack = {
38 .name = "CB.CallBack",
39 .deliver = afs_deliver_cb_callback,
40 .destructor = afs_cm_destructor,
41 .work = SRXAFSCB_CallBack,
42 };
43
44 /*
45 * CB.InitCallBackState operation type
46 */
47 static const struct afs_call_type afs_SRXCBInitCallBackState = {
48 .name = "CB.InitCallBackState",
49 .deliver = afs_deliver_cb_init_call_back_state,
50 .destructor = afs_cm_destructor,
51 .work = SRXAFSCB_InitCallBackState,
52 };
53
54 /*
55 * CB.InitCallBackState3 operation type
56 */
57 static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
58 .name = "CB.InitCallBackState3",
59 .deliver = afs_deliver_cb_init_call_back_state3,
60 .destructor = afs_cm_destructor,
61 .work = SRXAFSCB_InitCallBackState,
62 };
63
64 /*
65 * CB.Probe operation type
66 */
67 static const struct afs_call_type afs_SRXCBProbe = {
68 .name = "CB.Probe",
69 .deliver = afs_deliver_cb_probe,
70 .destructor = afs_cm_destructor,
71 .work = SRXAFSCB_Probe,
72 };
73
74 /*
75 * CB.ProbeUuid operation type
76 */
77 static const struct afs_call_type afs_SRXCBProbeUuid = {
78 .name = "CB.ProbeUuid",
79 .deliver = afs_deliver_cb_probe_uuid,
80 .destructor = afs_cm_destructor,
81 .work = SRXAFSCB_ProbeUuid,
82 };
83
84 /*
85 * CB.TellMeAboutYourself operation type
86 */
87 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
88 .name = "CB.TellMeAboutYourself",
89 .deliver = afs_deliver_cb_tell_me_about_yourself,
90 .destructor = afs_cm_destructor,
91 .work = SRXAFSCB_TellMeAboutYourself,
92 };
93
94 /*
95 * YFS CB.CallBack operation type
96 */
97 static const struct afs_call_type afs_SRXYFSCB_CallBack = {
98 .name = "YFSCB.CallBack",
99 .deliver = afs_deliver_yfs_cb_callback,
100 .destructor = afs_cm_destructor,
101 .work = SRXAFSCB_CallBack,
102 };
103
104 /*
105 * route an incoming cache manager call
106 * - return T if supported, F if not
107 */
afs_cm_incoming_call(struct afs_call * call)108 bool afs_cm_incoming_call(struct afs_call *call)
109 {
110 _enter("{%u, CB.OP %u}", call->service_id, call->operation_ID);
111
112 switch (call->operation_ID) {
113 case CBCallBack:
114 call->type = &afs_SRXCBCallBack;
115 return true;
116 case CBInitCallBackState:
117 call->type = &afs_SRXCBInitCallBackState;
118 return true;
119 case CBInitCallBackState3:
120 call->type = &afs_SRXCBInitCallBackState3;
121 return true;
122 case CBProbe:
123 call->type = &afs_SRXCBProbe;
124 return true;
125 case CBProbeUuid:
126 call->type = &afs_SRXCBProbeUuid;
127 return true;
128 case CBTellMeAboutYourself:
129 call->type = &afs_SRXCBTellMeAboutYourself;
130 return true;
131 case YFSCBCallBack:
132 if (call->service_id != YFS_CM_SERVICE)
133 return false;
134 call->type = &afs_SRXYFSCB_CallBack;
135 return true;
136 default:
137 return false;
138 }
139 }
140
141 /*
142 * Clean up a cache manager call.
143 */
afs_cm_destructor(struct afs_call * call)144 static void afs_cm_destructor(struct afs_call *call)
145 {
146 kfree(call->buffer);
147 call->buffer = NULL;
148 }
149
150 /*
151 * Abort a service call from within an action function.
152 */
afs_abort_service_call(struct afs_call * call,u32 abort_code,int error,enum rxrpc_abort_reason why)153 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
154 enum rxrpc_abort_reason why)
155 {
156 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
157 abort_code, error, why);
158 afs_set_call_complete(call, error, 0);
159 }
160
161 /*
162 * The server supplied a list of callbacks that it wanted to break.
163 */
SRXAFSCB_CallBack(struct work_struct * work)164 static void SRXAFSCB_CallBack(struct work_struct *work)
165 {
166 struct afs_call *call = container_of(work, struct afs_call, work);
167
168 _enter("");
169
170 /* We need to break the callbacks before sending the reply as the
171 * server holds up change visibility till it receives our reply so as
172 * to maintain cache coherency.
173 */
174 if (call->server) {
175 trace_afs_server(call->server->debug_id,
176 refcount_read(&call->server->ref),
177 atomic_read(&call->server->active),
178 afs_server_trace_callback);
179 afs_break_callbacks(call->server, call->count, call->request);
180 }
181
182 afs_send_empty_reply(call);
183 afs_put_call(call);
184 _leave("");
185 }
186
187 /*
188 * deliver request data to a CB.CallBack call
189 */
afs_deliver_cb_callback(struct afs_call * call)190 static int afs_deliver_cb_callback(struct afs_call *call)
191 {
192 struct afs_callback_break *cb;
193 __be32 *bp;
194 int ret, loop;
195
196 _enter("{%u}", call->unmarshall);
197
198 switch (call->unmarshall) {
199 case 0:
200 afs_extract_to_tmp(call);
201 call->unmarshall++;
202
203 /* extract the FID array and its count in two steps */
204 fallthrough;
205 case 1:
206 _debug("extract FID count");
207 ret = afs_extract_data(call, true);
208 if (ret < 0)
209 return ret;
210
211 call->count = ntohl(call->tmp);
212 _debug("FID count: %u", call->count);
213 if (call->count > AFSCBMAX)
214 return afs_protocol_error(call, afs_eproto_cb_fid_count);
215
216 call->buffer = kmalloc(array3_size(call->count, 3, 4),
217 GFP_KERNEL);
218 if (!call->buffer)
219 return -ENOMEM;
220 afs_extract_to_buf(call, call->count * 3 * 4);
221 call->unmarshall++;
222
223 fallthrough;
224 case 2:
225 _debug("extract FID array");
226 ret = afs_extract_data(call, true);
227 if (ret < 0)
228 return ret;
229
230 _debug("unmarshall FID array");
231 call->request = kzalloc_objs(struct afs_callback_break,
232 call->count);
233 if (!call->request)
234 return -ENOMEM;
235
236 cb = call->request;
237 bp = call->buffer;
238 for (loop = call->count; loop > 0; loop--, cb++) {
239 cb->fid.vid = ntohl(*bp++);
240 cb->fid.vnode = ntohl(*bp++);
241 cb->fid.unique = ntohl(*bp++);
242 }
243
244 afs_extract_to_tmp(call);
245 call->unmarshall++;
246
247 /* extract the callback array and its count in two steps */
248 fallthrough;
249 case 3:
250 _debug("extract CB count");
251 ret = afs_extract_data(call, true);
252 if (ret < 0)
253 return ret;
254
255 call->count2 = ntohl(call->tmp);
256 _debug("CB count: %u", call->count2);
257 if (call->count2 != call->count && call->count2 != 0)
258 return afs_protocol_error(call, afs_eproto_cb_count);
259 call->iter = &call->def_iter;
260 iov_iter_discard(&call->def_iter, ITER_DEST, call->count2 * 3 * 4);
261 call->unmarshall++;
262
263 fallthrough;
264 case 4:
265 _debug("extract discard %zu/%u",
266 iov_iter_count(call->iter), call->count2 * 3 * 4);
267
268 ret = afs_extract_data(call, false);
269 if (ret < 0)
270 return ret;
271
272 call->unmarshall++;
273 fallthrough;
274
275 case 5:
276 break;
277 }
278
279 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
280 return afs_io_error(call, afs_io_error_cm_reply);
281 return 0;
282 }
283
284 /*
285 * allow the fileserver to request callback state (re-)initialisation
286 */
SRXAFSCB_InitCallBackState(struct work_struct * work)287 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
288 {
289 struct afs_call *call = container_of(work, struct afs_call, work);
290
291 _enter("{%p}", call->server);
292
293 if (call->server)
294 afs_init_callback_state(call->server);
295 afs_send_empty_reply(call);
296 afs_put_call(call);
297 _leave("");
298 }
299
300 /*
301 * deliver request data to a CB.InitCallBackState call
302 */
afs_deliver_cb_init_call_back_state(struct afs_call * call)303 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
304 {
305 _enter("");
306
307 afs_extract_discard(call, 0);
308 return afs_extract_data(call, false);
309 }
310
311 /*
312 * deliver request data to a CB.InitCallBackState3 call
313 */
afs_deliver_cb_init_call_back_state3(struct afs_call * call)314 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
315 {
316 struct afs_uuid *r;
317 unsigned loop;
318 __be32 *b;
319 int ret;
320
321 _enter("{%u}", call->unmarshall);
322
323 switch (call->unmarshall) {
324 case 0:
325 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
326 if (!call->buffer)
327 return -ENOMEM;
328 afs_extract_to_buf(call, 11 * sizeof(__be32));
329 call->unmarshall++;
330
331 fallthrough;
332 case 1:
333 _debug("extract UUID");
334 ret = afs_extract_data(call, false);
335 switch (ret) {
336 case 0: break;
337 case -EAGAIN: return 0;
338 default: return ret;
339 }
340
341 _debug("unmarshall UUID");
342 call->request = kmalloc_obj(struct afs_uuid);
343 if (!call->request)
344 return -ENOMEM;
345
346 b = call->buffer;
347 r = call->request;
348 r->time_low = b[0];
349 r->time_mid = htons(ntohl(b[1]));
350 r->time_hi_and_version = htons(ntohl(b[2]));
351 r->clock_seq_hi_and_reserved = ntohl(b[3]);
352 r->clock_seq_low = ntohl(b[4]);
353
354 for (loop = 0; loop < 6; loop++)
355 r->node[loop] = ntohl(b[loop + 5]);
356
357 call->unmarshall++;
358 fallthrough;
359
360 case 2:
361 break;
362 }
363
364 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
365 return afs_io_error(call, afs_io_error_cm_reply);
366
367 if (memcmp(call->request, &call->server->_uuid, sizeof(call->server->_uuid)) != 0) {
368 pr_notice("Callback UUID does not match fileserver UUID\n");
369 trace_afs_cm_no_server_u(call, call->request);
370 return 0;
371 }
372
373 return 0;
374 }
375
376 /*
377 * allow the fileserver to see if the cache manager is still alive
378 */
SRXAFSCB_Probe(struct work_struct * work)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 afs_put_call(call);
386 _leave("");
387 }
388
389 /*
390 * deliver request data to a CB.Probe call
391 */
afs_deliver_cb_probe(struct afs_call * call)392 static int afs_deliver_cb_probe(struct afs_call *call)
393 {
394 int ret;
395
396 _enter("");
397
398 afs_extract_discard(call, 0);
399 ret = afs_extract_data(call, false);
400 if (ret < 0)
401 return ret;
402
403 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
404 return afs_io_error(call, afs_io_error_cm_reply);
405 return 0;
406 }
407
408 /*
409 * Allow the fileserver to quickly find out if the cache manager has been
410 * rebooted.
411 */
SRXAFSCB_ProbeUuid(struct work_struct * work)412 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
413 {
414 struct afs_call *call = container_of(work, struct afs_call, work);
415 struct afs_uuid *r = call->request;
416
417 _enter("");
418
419 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
420 afs_send_empty_reply(call);
421 else
422 afs_abort_service_call(call, 1, 1, afs_abort_probeuuid_negative);
423
424 afs_put_call(call);
425 _leave("");
426 }
427
428 /*
429 * deliver request data to a CB.ProbeUuid call
430 */
afs_deliver_cb_probe_uuid(struct afs_call * call)431 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
432 {
433 struct afs_uuid *r;
434 unsigned loop;
435 __be32 *b;
436 int ret;
437
438 _enter("{%u}", call->unmarshall);
439
440 switch (call->unmarshall) {
441 case 0:
442 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
443 if (!call->buffer)
444 return -ENOMEM;
445 afs_extract_to_buf(call, 11 * sizeof(__be32));
446 call->unmarshall++;
447
448 fallthrough;
449 case 1:
450 _debug("extract UUID");
451 ret = afs_extract_data(call, false);
452 switch (ret) {
453 case 0: break;
454 case -EAGAIN: return 0;
455 default: return ret;
456 }
457
458 _debug("unmarshall UUID");
459 call->request = kmalloc_obj(struct afs_uuid);
460 if (!call->request)
461 return -ENOMEM;
462
463 b = call->buffer;
464 r = call->request;
465 r->time_low = b[0];
466 r->time_mid = htons(ntohl(b[1]));
467 r->time_hi_and_version = htons(ntohl(b[2]));
468 r->clock_seq_hi_and_reserved = ntohl(b[3]);
469 r->clock_seq_low = ntohl(b[4]);
470
471 for (loop = 0; loop < 6; loop++)
472 r->node[loop] = ntohl(b[loop + 5]);
473
474 call->unmarshall++;
475 fallthrough;
476
477 case 2:
478 break;
479 }
480
481 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
482 return afs_io_error(call, afs_io_error_cm_reply);
483 return 0;
484 }
485
486 /*
487 * allow the fileserver to ask about the cache manager's capabilities
488 */
SRXAFSCB_TellMeAboutYourself(struct work_struct * work)489 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
490 {
491 struct afs_call *call = container_of(work, struct afs_call, work);
492 int loop;
493
494 struct {
495 struct /* InterfaceAddr */ {
496 __be32 nifs;
497 __be32 uuid[11];
498 __be32 ifaddr[32];
499 __be32 netmask[32];
500 __be32 mtu[32];
501 } ia;
502 struct /* Capabilities */ {
503 __be32 capcount;
504 __be32 caps[1];
505 } cap;
506 } reply;
507
508 _enter("");
509
510 memset(&reply, 0, sizeof(reply));
511
512 reply.ia.uuid[0] = call->net->uuid.time_low;
513 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
514 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
515 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
516 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
517 for (loop = 0; loop < 6; loop++)
518 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
519
520 reply.cap.capcount = htonl(1);
521 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
522 afs_send_simple_reply(call, &reply, sizeof(reply));
523 afs_put_call(call);
524 _leave("");
525 }
526
527 /*
528 * deliver request data to a CB.TellMeAboutYourself call
529 */
afs_deliver_cb_tell_me_about_yourself(struct afs_call * call)530 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
531 {
532 int ret;
533
534 _enter("");
535
536 afs_extract_discard(call, 0);
537 ret = afs_extract_data(call, false);
538 if (ret < 0)
539 return ret;
540
541 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
542 return afs_io_error(call, afs_io_error_cm_reply);
543 return 0;
544 }
545
546 /*
547 * deliver request data to a YFS CB.CallBack call
548 */
afs_deliver_yfs_cb_callback(struct afs_call * call)549 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
550 {
551 struct afs_callback_break *cb;
552 struct yfs_xdr_YFSFid *bp;
553 size_t size;
554 int ret, loop;
555
556 _enter("{%u}", call->unmarshall);
557
558 switch (call->unmarshall) {
559 case 0:
560 afs_extract_to_tmp(call);
561 call->unmarshall++;
562
563 /* extract the FID array and its count in two steps */
564 fallthrough;
565 case 1:
566 _debug("extract FID count");
567 ret = afs_extract_data(call, true);
568 if (ret < 0)
569 return ret;
570
571 call->count = ntohl(call->tmp);
572 _debug("FID count: %u", call->count);
573 if (call->count > YFSCBMAX)
574 return afs_protocol_error(call, afs_eproto_cb_fid_count);
575
576 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
577 call->buffer = kmalloc(size, GFP_KERNEL);
578 if (!call->buffer)
579 return -ENOMEM;
580 afs_extract_to_buf(call, size);
581 call->unmarshall++;
582
583 fallthrough;
584 case 2:
585 _debug("extract FID array");
586 ret = afs_extract_data(call, false);
587 if (ret < 0)
588 return ret;
589
590 _debug("unmarshall FID array");
591 call->request = kzalloc_objs(struct afs_callback_break,
592 call->count);
593 if (!call->request)
594 return -ENOMEM;
595
596 cb = call->request;
597 bp = call->buffer;
598 for (loop = call->count; loop > 0; loop--, cb++) {
599 cb->fid.vid = xdr_to_u64(bp->volume);
600 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
601 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
602 cb->fid.unique = ntohl(bp->vnode.unique);
603 bp++;
604 }
605
606 afs_extract_to_tmp(call);
607 call->unmarshall++;
608 fallthrough;
609
610 case 3:
611 break;
612 }
613
614 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
615 return afs_io_error(call, afs_io_error_cm_reply);
616 return 0;
617 }
618