1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Maintain an RxRPC server socket to do AFS communications through
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8 #include <linux/slab.h>
9 #include <linux/sched/signal.h>
10
11 #include <net/sock.h>
12 #include <net/af_rxrpc.h>
13 #include "internal.h"
14 #include "afs_cm.h"
15 #include "protocol_yfs.h"
16
17 struct workqueue_struct *afs_async_calls;
18
19 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
20 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
21 static void afs_process_async_call(struct work_struct *);
22 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
23 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
24 static int afs_deliver_cm_op_id(struct afs_call *);
25
26 /* asynchronous incoming call initial processing */
27 static const struct afs_call_type afs_RXCMxxxx = {
28 .name = "CB.xxxx",
29 .deliver = afs_deliver_cm_op_id,
30 };
31
32 /*
33 * open an RxRPC socket and bind it to be a server for callback notifications
34 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
35 */
afs_open_socket(struct afs_net * net)36 int afs_open_socket(struct afs_net *net)
37 {
38 struct sockaddr_rxrpc srx;
39 struct socket *socket;
40 int ret;
41
42 _enter("");
43
44 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
45 if (ret < 0)
46 goto error_1;
47
48 socket->sk->sk_allocation = GFP_NOFS;
49
50 /* bind the callback manager's address to make this a server socket */
51 memset(&srx, 0, sizeof(srx));
52 srx.srx_family = AF_RXRPC;
53 srx.srx_service = CM_SERVICE;
54 srx.transport_type = SOCK_DGRAM;
55 srx.transport_len = sizeof(srx.transport.sin6);
56 srx.transport.sin6.sin6_family = AF_INET6;
57 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
58
59 ret = rxrpc_sock_set_min_security_level(socket->sk,
60 RXRPC_SECURITY_ENCRYPT);
61 if (ret < 0)
62 goto error_2;
63
64 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
65 if (ret == -EADDRINUSE) {
66 srx.transport.sin6.sin6_port = 0;
67 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
68 }
69 if (ret < 0)
70 goto error_2;
71
72 srx.srx_service = YFS_CM_SERVICE;
73 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
74 if (ret < 0)
75 goto error_2;
76
77 /* Ideally, we'd turn on service upgrade here, but we can't because
78 * OpenAFS is buggy and leaks the userStatus field from packet to
79 * packet and between FS packets and CB packets - so if we try to do an
80 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
81 * it sends back to us.
82 */
83
84 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
85 afs_rx_discard_new_call);
86
87 ret = kernel_listen(socket, INT_MAX);
88 if (ret < 0)
89 goto error_2;
90
91 net->socket = socket;
92 afs_charge_preallocation(&net->charge_preallocation_work);
93 _leave(" = 0");
94 return 0;
95
96 error_2:
97 sock_release(socket);
98 error_1:
99 _leave(" = %d", ret);
100 return ret;
101 }
102
103 /*
104 * close the RxRPC socket AFS was using
105 */
afs_close_socket(struct afs_net * net)106 void afs_close_socket(struct afs_net *net)
107 {
108 _enter("");
109
110 kernel_listen(net->socket, 0);
111 flush_workqueue(afs_async_calls);
112
113 if (net->spare_incoming_call) {
114 afs_put_call(net->spare_incoming_call);
115 net->spare_incoming_call = NULL;
116 }
117
118 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
119 wait_var_event(&net->nr_outstanding_calls,
120 !atomic_read(&net->nr_outstanding_calls));
121 _debug("no outstanding calls");
122
123 kernel_sock_shutdown(net->socket, SHUT_RDWR);
124 flush_workqueue(afs_async_calls);
125 sock_release(net->socket);
126
127 _debug("dework");
128 _leave("");
129 }
130
131 /*
132 * Allocate a call.
133 */
afs_alloc_call(struct afs_net * net,const struct afs_call_type * type,gfp_t gfp)134 static struct afs_call *afs_alloc_call(struct afs_net *net,
135 const struct afs_call_type *type,
136 gfp_t gfp)
137 {
138 struct afs_call *call;
139 int o;
140
141 call = kzalloc(sizeof(*call), gfp);
142 if (!call)
143 return NULL;
144
145 call->type = type;
146 call->net = net;
147 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
148 refcount_set(&call->ref, 1);
149 INIT_WORK(&call->async_work, afs_process_async_call);
150 init_waitqueue_head(&call->waitq);
151 spin_lock_init(&call->state_lock);
152 call->iter = &call->def_iter;
153
154 o = atomic_inc_return(&net->nr_outstanding_calls);
155 trace_afs_call(call, afs_call_trace_alloc, 1, o,
156 __builtin_return_address(0));
157 return call;
158 }
159
160 /*
161 * Dispose of a reference on a call.
162 */
afs_put_call(struct afs_call * call)163 void afs_put_call(struct afs_call *call)
164 {
165 struct afs_net *net = call->net;
166 bool zero;
167 int r, o;
168
169 zero = __refcount_dec_and_test(&call->ref, &r);
170 o = atomic_read(&net->nr_outstanding_calls);
171 trace_afs_call(call, afs_call_trace_put, r - 1, o,
172 __builtin_return_address(0));
173
174 if (zero) {
175 ASSERT(!work_pending(&call->async_work));
176 ASSERT(call->type->name != NULL);
177
178 if (call->rxcall) {
179 rxrpc_kernel_end_call(net->socket, call->rxcall);
180 call->rxcall = NULL;
181 }
182 if (call->type->destructor)
183 call->type->destructor(call);
184
185 afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
186 afs_put_addrlist(call->alist);
187 kfree(call->request);
188
189 trace_afs_call(call, afs_call_trace_free, 0, o,
190 __builtin_return_address(0));
191 kfree(call);
192
193 o = atomic_dec_return(&net->nr_outstanding_calls);
194 if (o == 0)
195 wake_up_var(&net->nr_outstanding_calls);
196 }
197 }
198
afs_get_call(struct afs_call * call,enum afs_call_trace why)199 static struct afs_call *afs_get_call(struct afs_call *call,
200 enum afs_call_trace why)
201 {
202 int r;
203
204 __refcount_inc(&call->ref, &r);
205
206 trace_afs_call(call, why, r + 1,
207 atomic_read(&call->net->nr_outstanding_calls),
208 __builtin_return_address(0));
209 return call;
210 }
211
212 /*
213 * Queue the call for actual work.
214 */
afs_queue_call_work(struct afs_call * call)215 static void afs_queue_call_work(struct afs_call *call)
216 {
217 if (call->type->work) {
218 INIT_WORK(&call->work, call->type->work);
219
220 afs_get_call(call, afs_call_trace_work);
221 if (!queue_work(afs_wq, &call->work))
222 afs_put_call(call);
223 }
224 }
225
226 /*
227 * allocate a call with flat request and reply buffers
228 */
afs_alloc_flat_call(struct afs_net * net,const struct afs_call_type * type,size_t request_size,size_t reply_max)229 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
230 const struct afs_call_type *type,
231 size_t request_size, size_t reply_max)
232 {
233 struct afs_call *call;
234
235 call = afs_alloc_call(net, type, GFP_NOFS);
236 if (!call)
237 goto nomem_call;
238
239 if (request_size) {
240 call->request_size = request_size;
241 call->request = kmalloc(request_size, GFP_NOFS);
242 if (!call->request)
243 goto nomem_free;
244 }
245
246 if (reply_max) {
247 call->reply_max = reply_max;
248 call->buffer = kmalloc(reply_max, GFP_NOFS);
249 if (!call->buffer)
250 goto nomem_free;
251 }
252
253 afs_extract_to_buf(call, call->reply_max);
254 call->operation_ID = type->op;
255 init_waitqueue_head(&call->waitq);
256 return call;
257
258 nomem_free:
259 afs_put_call(call);
260 nomem_call:
261 return NULL;
262 }
263
264 /*
265 * clean up a call with flat buffer
266 */
afs_flat_call_destructor(struct afs_call * call)267 void afs_flat_call_destructor(struct afs_call *call)
268 {
269 _enter("");
270
271 kfree(call->request);
272 call->request = NULL;
273 kfree(call->buffer);
274 call->buffer = NULL;
275 }
276
277 /*
278 * Advance the AFS call state when the RxRPC call ends the transmit phase.
279 */
afs_notify_end_request_tx(struct sock * sock,struct rxrpc_call * rxcall,unsigned long call_user_ID)280 static void afs_notify_end_request_tx(struct sock *sock,
281 struct rxrpc_call *rxcall,
282 unsigned long call_user_ID)
283 {
284 struct afs_call *call = (struct afs_call *)call_user_ID;
285
286 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
287 }
288
289 /*
290 * Initiate a call and synchronously queue up the parameters for dispatch. Any
291 * error is stored into the call struct, which the caller must check for.
292 */
afs_make_call(struct afs_addr_cursor * ac,struct afs_call * call,gfp_t gfp)293 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
294 {
295 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
296 struct rxrpc_call *rxcall;
297 struct msghdr msg;
298 struct kvec iov[1];
299 size_t len;
300 s64 tx_total_len;
301 int ret;
302
303 _enter(",{%pISp},", &srx->transport);
304
305 ASSERT(call->type != NULL);
306 ASSERT(call->type->name != NULL);
307
308 _debug("____MAKE %p{%s,%x} [%d]____",
309 call, call->type->name, key_serial(call->key),
310 atomic_read(&call->net->nr_outstanding_calls));
311
312 call->addr_ix = ac->index;
313 call->alist = afs_get_addrlist(ac->alist);
314
315 /* Work out the length we're going to transmit. This is awkward for
316 * calls such as FS.StoreData where there's an extra injection of data
317 * after the initial fixed part.
318 */
319 tx_total_len = call->request_size;
320 if (call->write_iter)
321 tx_total_len += iov_iter_count(call->write_iter);
322
323 /* If the call is going to be asynchronous, we need an extra ref for
324 * the call to hold itself so the caller need not hang on to its ref.
325 */
326 if (call->async) {
327 afs_get_call(call, afs_call_trace_get);
328 call->drop_ref = true;
329 }
330
331 /* create a call */
332 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
333 (unsigned long)call,
334 tx_total_len, gfp,
335 (call->async ?
336 afs_wake_up_async_call :
337 afs_wake_up_call_waiter),
338 call->upgrade,
339 (call->intr ? RXRPC_PREINTERRUPTIBLE :
340 RXRPC_UNINTERRUPTIBLE),
341 call->debug_id);
342 if (IS_ERR(rxcall)) {
343 ret = PTR_ERR(rxcall);
344 call->error = ret;
345 goto error_kill_call;
346 }
347
348 call->rxcall = rxcall;
349
350 if (call->max_lifespan)
351 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
352 call->max_lifespan);
353 call->issue_time = ktime_get_real();
354
355 /* send the request */
356 iov[0].iov_base = call->request;
357 iov[0].iov_len = call->request_size;
358
359 msg.msg_name = NULL;
360 msg.msg_namelen = 0;
361 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
362 msg.msg_control = NULL;
363 msg.msg_controllen = 0;
364 msg.msg_flags = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0);
365
366 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
367 &msg, call->request_size,
368 afs_notify_end_request_tx);
369 if (ret < 0)
370 goto error_do_abort;
371
372 if (call->write_iter) {
373 msg.msg_iter = *call->write_iter;
374 msg.msg_flags &= ~MSG_MORE;
375 trace_afs_send_data(call, &msg);
376
377 ret = rxrpc_kernel_send_data(call->net->socket,
378 call->rxcall, &msg,
379 iov_iter_count(&msg.msg_iter),
380 afs_notify_end_request_tx);
381 *call->write_iter = msg.msg_iter;
382
383 trace_afs_sent_data(call, &msg, ret);
384 if (ret < 0)
385 goto error_do_abort;
386 }
387
388 /* Note that at this point, we may have received the reply or an abort
389 * - and an asynchronous call may already have completed.
390 *
391 * afs_wait_for_call_to_complete(call, ac)
392 * must be called to synchronously clean up.
393 */
394 return;
395
396 error_do_abort:
397 if (ret != -ECONNABORTED) {
398 rxrpc_kernel_abort_call(call->net->socket, rxcall,
399 RX_USER_ABORT, ret, "KSD");
400 } else {
401 len = 0;
402 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
403 rxrpc_kernel_recv_data(call->net->socket, rxcall,
404 &msg.msg_iter, &len, false,
405 &call->abort_code, &call->service_id);
406 ac->abort_code = call->abort_code;
407 ac->responded = true;
408 }
409 call->error = ret;
410 trace_afs_call_done(call);
411 error_kill_call:
412 if (call->type->done)
413 call->type->done(call);
414
415 /* We need to dispose of the extra ref we grabbed for an async call.
416 * The call, however, might be queued on afs_async_calls and we need to
417 * make sure we don't get any more notifications that might requeue it.
418 */
419 if (call->rxcall) {
420 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
421 call->rxcall = NULL;
422 }
423 if (call->async) {
424 if (cancel_work_sync(&call->async_work))
425 afs_put_call(call);
426 afs_set_call_complete(call, ret, 0);
427 }
428
429 ac->error = ret;
430 call->state = AFS_CALL_COMPLETE;
431 _leave(" = %d", ret);
432 }
433
434 /*
435 * Log remote abort codes that indicate that we have a protocol disagreement
436 * with the server.
437 */
afs_log_error(struct afs_call * call,s32 remote_abort)438 static void afs_log_error(struct afs_call *call, s32 remote_abort)
439 {
440 static int max = 0;
441 const char *msg;
442 int m;
443
444 switch (remote_abort) {
445 case RX_EOF: msg = "unexpected EOF"; break;
446 case RXGEN_CC_MARSHAL: msg = "client marshalling"; break;
447 case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling"; break;
448 case RXGEN_SS_MARSHAL: msg = "server marshalling"; break;
449 case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling"; break;
450 case RXGEN_DECODE: msg = "opcode decode"; break;
451 case RXGEN_SS_XDRFREE: msg = "server XDR cleanup"; break;
452 case RXGEN_CC_XDRFREE: msg = "client XDR cleanup"; break;
453 case -32: msg = "insufficient data"; break;
454 default:
455 return;
456 }
457
458 m = max;
459 if (m < 3) {
460 max = m + 1;
461 pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n",
462 msg, call->type->name,
463 &call->alist->addrs[call->addr_ix].transport);
464 }
465 }
466
467 /*
468 * deliver messages to a call
469 */
afs_deliver_to_call(struct afs_call * call)470 static void afs_deliver_to_call(struct afs_call *call)
471 {
472 enum afs_call_state state;
473 size_t len;
474 u32 abort_code, remote_abort = 0;
475 int ret;
476
477 _enter("%s", call->type->name);
478
479 while (state = READ_ONCE(call->state),
480 state == AFS_CALL_CL_AWAIT_REPLY ||
481 state == AFS_CALL_SV_AWAIT_OP_ID ||
482 state == AFS_CALL_SV_AWAIT_REQUEST ||
483 state == AFS_CALL_SV_AWAIT_ACK
484 ) {
485 if (state == AFS_CALL_SV_AWAIT_ACK) {
486 len = 0;
487 iov_iter_kvec(&call->def_iter, READ, NULL, 0, 0);
488 ret = rxrpc_kernel_recv_data(call->net->socket,
489 call->rxcall, &call->def_iter,
490 &len, false, &remote_abort,
491 &call->service_id);
492 trace_afs_receive_data(call, &call->def_iter, false, ret);
493
494 if (ret == -EINPROGRESS || ret == -EAGAIN)
495 return;
496 if (ret < 0 || ret == 1) {
497 if (ret == 1)
498 ret = 0;
499 goto call_complete;
500 }
501 return;
502 }
503
504 ret = call->type->deliver(call);
505 state = READ_ONCE(call->state);
506 if (ret == 0 && call->unmarshalling_error)
507 ret = -EBADMSG;
508 switch (ret) {
509 case 0:
510 afs_queue_call_work(call);
511 if (state == AFS_CALL_CL_PROC_REPLY) {
512 if (call->op)
513 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
514 &call->op->server->flags);
515 goto call_complete;
516 }
517 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
518 goto done;
519 case -EINPROGRESS:
520 case -EAGAIN:
521 goto out;
522 case -ECONNABORTED:
523 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
524 afs_log_error(call, call->abort_code);
525 goto done;
526 case -ENOTSUPP:
527 abort_code = RXGEN_OPCODE;
528 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
529 abort_code, ret, "KIV");
530 goto local_abort;
531 case -EIO:
532 pr_err("kAFS: Call %u in bad state %u\n",
533 call->debug_id, state);
534 fallthrough;
535 case -ENODATA:
536 case -EBADMSG:
537 case -EMSGSIZE:
538 case -ENOMEM:
539 case -EFAULT:
540 abort_code = RXGEN_CC_UNMARSHAL;
541 if (state != AFS_CALL_CL_AWAIT_REPLY)
542 abort_code = RXGEN_SS_UNMARSHAL;
543 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
544 abort_code, ret, "KUM");
545 goto local_abort;
546 default:
547 abort_code = RX_CALL_DEAD;
548 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
549 abort_code, ret, "KER");
550 goto local_abort;
551 }
552 }
553
554 done:
555 if (call->type->done)
556 call->type->done(call);
557 out:
558 _leave("");
559 return;
560
561 local_abort:
562 abort_code = 0;
563 call_complete:
564 afs_set_call_complete(call, ret, remote_abort);
565 state = AFS_CALL_COMPLETE;
566 goto done;
567 }
568
569 /*
570 * Wait synchronously for a call to complete and clean up the call struct.
571 */
afs_wait_for_call_to_complete(struct afs_call * call,struct afs_addr_cursor * ac)572 long afs_wait_for_call_to_complete(struct afs_call *call,
573 struct afs_addr_cursor *ac)
574 {
575 long ret;
576 bool rxrpc_complete = false;
577
578 DECLARE_WAITQUEUE(myself, current);
579
580 _enter("");
581
582 ret = call->error;
583 if (ret < 0)
584 goto out;
585
586 add_wait_queue(&call->waitq, &myself);
587 for (;;) {
588 set_current_state(TASK_UNINTERRUPTIBLE);
589
590 /* deliver any messages that are in the queue */
591 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
592 call->need_attention) {
593 call->need_attention = false;
594 __set_current_state(TASK_RUNNING);
595 afs_deliver_to_call(call);
596 continue;
597 }
598
599 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
600 break;
601
602 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
603 /* rxrpc terminated the call. */
604 rxrpc_complete = true;
605 break;
606 }
607
608 schedule();
609 }
610
611 remove_wait_queue(&call->waitq, &myself);
612 __set_current_state(TASK_RUNNING);
613
614 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
615 if (rxrpc_complete) {
616 afs_set_call_complete(call, call->error, call->abort_code);
617 } else {
618 /* Kill off the call if it's still live. */
619 _debug("call interrupted");
620 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
621 RX_USER_ABORT, -EINTR, "KWI"))
622 afs_set_call_complete(call, -EINTR, 0);
623 }
624 }
625
626 spin_lock_bh(&call->state_lock);
627 ac->abort_code = call->abort_code;
628 ac->error = call->error;
629 spin_unlock_bh(&call->state_lock);
630
631 ret = ac->error;
632 switch (ret) {
633 case 0:
634 ret = call->ret0;
635 call->ret0 = 0;
636
637 fallthrough;
638 case -ECONNABORTED:
639 ac->responded = true;
640 break;
641 }
642
643 out:
644 _debug("call complete");
645 afs_put_call(call);
646 _leave(" = %p", (void *)ret);
647 return ret;
648 }
649
650 /*
651 * wake up a waiting call
652 */
afs_wake_up_call_waiter(struct sock * sk,struct rxrpc_call * rxcall,unsigned long call_user_ID)653 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
654 unsigned long call_user_ID)
655 {
656 struct afs_call *call = (struct afs_call *)call_user_ID;
657
658 call->need_attention = true;
659 wake_up(&call->waitq);
660 }
661
662 /*
663 * wake up an asynchronous call
664 */
afs_wake_up_async_call(struct sock * sk,struct rxrpc_call * rxcall,unsigned long call_user_ID)665 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
666 unsigned long call_user_ID)
667 {
668 struct afs_call *call = (struct afs_call *)call_user_ID;
669 int r;
670
671 trace_afs_notify_call(rxcall, call);
672 call->need_attention = true;
673
674 if (__refcount_inc_not_zero(&call->ref, &r)) {
675 trace_afs_call(call, afs_call_trace_wake, r + 1,
676 atomic_read(&call->net->nr_outstanding_calls),
677 __builtin_return_address(0));
678
679 if (!queue_work(afs_async_calls, &call->async_work))
680 afs_put_call(call);
681 }
682 }
683
684 /*
685 * Perform I/O processing on an asynchronous call. The work item carries a ref
686 * to the call struct that we either need to release or to pass on.
687 */
afs_process_async_call(struct work_struct * work)688 static void afs_process_async_call(struct work_struct *work)
689 {
690 struct afs_call *call = container_of(work, struct afs_call, async_work);
691
692 _enter("");
693
694 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
695 call->need_attention = false;
696 afs_deliver_to_call(call);
697 }
698
699 afs_put_call(call);
700 _leave("");
701 }
702
afs_rx_attach(struct rxrpc_call * rxcall,unsigned long user_call_ID)703 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
704 {
705 struct afs_call *call = (struct afs_call *)user_call_ID;
706
707 call->rxcall = rxcall;
708 }
709
710 /*
711 * Charge the incoming call preallocation.
712 */
afs_charge_preallocation(struct work_struct * work)713 void afs_charge_preallocation(struct work_struct *work)
714 {
715 struct afs_net *net =
716 container_of(work, struct afs_net, charge_preallocation_work);
717 struct afs_call *call = net->spare_incoming_call;
718
719 for (;;) {
720 if (!call) {
721 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
722 if (!call)
723 break;
724
725 call->drop_ref = true;
726 call->async = true;
727 call->state = AFS_CALL_SV_AWAIT_OP_ID;
728 init_waitqueue_head(&call->waitq);
729 afs_extract_to_tmp(call);
730 }
731
732 if (rxrpc_kernel_charge_accept(net->socket,
733 afs_wake_up_async_call,
734 afs_rx_attach,
735 (unsigned long)call,
736 GFP_KERNEL,
737 call->debug_id) < 0)
738 break;
739 call = NULL;
740 }
741 net->spare_incoming_call = call;
742 }
743
744 /*
745 * Discard a preallocated call when a socket is shut down.
746 */
afs_rx_discard_new_call(struct rxrpc_call * rxcall,unsigned long user_call_ID)747 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
748 unsigned long user_call_ID)
749 {
750 struct afs_call *call = (struct afs_call *)user_call_ID;
751
752 call->rxcall = NULL;
753 afs_put_call(call);
754 }
755
756 /*
757 * Notification of an incoming call.
758 */
afs_rx_new_call(struct sock * sk,struct rxrpc_call * rxcall,unsigned long user_call_ID)759 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
760 unsigned long user_call_ID)
761 {
762 struct afs_net *net = afs_sock2net(sk);
763
764 queue_work(afs_wq, &net->charge_preallocation_work);
765 }
766
767 /*
768 * Grab the operation ID from an incoming cache manager call. The socket
769 * buffer is discarded on error or if we don't yet have sufficient data.
770 */
afs_deliver_cm_op_id(struct afs_call * call)771 static int afs_deliver_cm_op_id(struct afs_call *call)
772 {
773 int ret;
774
775 _enter("{%zu}", iov_iter_count(call->iter));
776
777 /* the operation ID forms the first four bytes of the request data */
778 ret = afs_extract_data(call, true);
779 if (ret < 0)
780 return ret;
781
782 call->operation_ID = ntohl(call->tmp);
783 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
784
785 /* ask the cache manager to route the call (it'll change the call type
786 * if successful) */
787 if (!afs_cm_incoming_call(call))
788 return -ENOTSUPP;
789
790 trace_afs_cb_call(call);
791
792 /* pass responsibility for the remainer of this message off to the
793 * cache manager op */
794 return call->type->deliver(call);
795 }
796
797 /*
798 * Advance the AFS call state when an RxRPC service call ends the transmit
799 * phase.
800 */
afs_notify_end_reply_tx(struct sock * sock,struct rxrpc_call * rxcall,unsigned long call_user_ID)801 static void afs_notify_end_reply_tx(struct sock *sock,
802 struct rxrpc_call *rxcall,
803 unsigned long call_user_ID)
804 {
805 struct afs_call *call = (struct afs_call *)call_user_ID;
806
807 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
808 }
809
810 /*
811 * send an empty reply
812 */
afs_send_empty_reply(struct afs_call * call)813 void afs_send_empty_reply(struct afs_call *call)
814 {
815 struct afs_net *net = call->net;
816 struct msghdr msg;
817
818 _enter("");
819
820 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
821
822 msg.msg_name = NULL;
823 msg.msg_namelen = 0;
824 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
825 msg.msg_control = NULL;
826 msg.msg_controllen = 0;
827 msg.msg_flags = 0;
828
829 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
830 afs_notify_end_reply_tx)) {
831 case 0:
832 _leave(" [replied]");
833 return;
834
835 case -ENOMEM:
836 _debug("oom");
837 rxrpc_kernel_abort_call(net->socket, call->rxcall,
838 RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
839 fallthrough;
840 default:
841 _leave(" [error]");
842 return;
843 }
844 }
845
846 /*
847 * send a simple reply
848 */
afs_send_simple_reply(struct afs_call * call,const void * buf,size_t len)849 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
850 {
851 struct afs_net *net = call->net;
852 struct msghdr msg;
853 struct kvec iov[1];
854 int n;
855
856 _enter("");
857
858 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
859
860 iov[0].iov_base = (void *) buf;
861 iov[0].iov_len = len;
862 msg.msg_name = NULL;
863 msg.msg_namelen = 0;
864 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
865 msg.msg_control = NULL;
866 msg.msg_controllen = 0;
867 msg.msg_flags = 0;
868
869 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
870 afs_notify_end_reply_tx);
871 if (n >= 0) {
872 /* Success */
873 _leave(" [replied]");
874 return;
875 }
876
877 if (n == -ENOMEM) {
878 _debug("oom");
879 rxrpc_kernel_abort_call(net->socket, call->rxcall,
880 RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
881 }
882 _leave(" [error]");
883 }
884
885 /*
886 * Extract a piece of data from the received data socket buffers.
887 */
afs_extract_data(struct afs_call * call,bool want_more)888 int afs_extract_data(struct afs_call *call, bool want_more)
889 {
890 struct afs_net *net = call->net;
891 struct iov_iter *iter = call->iter;
892 enum afs_call_state state;
893 u32 remote_abort = 0;
894 int ret;
895
896 _enter("{%s,%zu,%zu},%d",
897 call->type->name, call->iov_len, iov_iter_count(iter), want_more);
898
899 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
900 &call->iov_len, want_more, &remote_abort,
901 &call->service_id);
902 if (ret == 0 || ret == -EAGAIN)
903 return ret;
904
905 state = READ_ONCE(call->state);
906 if (ret == 1) {
907 switch (state) {
908 case AFS_CALL_CL_AWAIT_REPLY:
909 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
910 break;
911 case AFS_CALL_SV_AWAIT_REQUEST:
912 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
913 break;
914 case AFS_CALL_COMPLETE:
915 kdebug("prem complete %d", call->error);
916 return afs_io_error(call, afs_io_error_extract);
917 default:
918 break;
919 }
920 return 0;
921 }
922
923 afs_set_call_complete(call, ret, remote_abort);
924 return ret;
925 }
926
927 /*
928 * Log protocol error production.
929 */
afs_protocol_error(struct afs_call * call,enum afs_eproto_cause cause)930 noinline int afs_protocol_error(struct afs_call *call,
931 enum afs_eproto_cause cause)
932 {
933 trace_afs_protocol_error(call, cause);
934 if (call)
935 call->unmarshalling_error = true;
936 return -EBADMSG;
937 }
938