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