xref: /openbmc/linux/fs/afs/rxrpc.c (revision 4e5f8465)
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  */
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  */
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  */
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->debug_id, 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  */
163 void afs_put_call(struct afs_call *call)
164 {
165 	struct afs_net *net = call->net;
166 	unsigned int debug_id = call->debug_id;
167 	bool zero;
168 	int r, o;
169 
170 	zero = __refcount_dec_and_test(&call->ref, &r);
171 	o = atomic_read(&net->nr_outstanding_calls);
172 	trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
173 		       __builtin_return_address(0));
174 
175 	if (zero) {
176 		ASSERT(!work_pending(&call->async_work));
177 		ASSERT(call->type->name != NULL);
178 
179 		if (call->rxcall) {
180 			rxrpc_kernel_end_call(net->socket, call->rxcall);
181 			call->rxcall = NULL;
182 		}
183 		if (call->type->destructor)
184 			call->type->destructor(call);
185 
186 		afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
187 		afs_put_addrlist(call->alist);
188 		kfree(call->request);
189 
190 		trace_afs_call(call->debug_id, afs_call_trace_free, 0, o,
191 			       __builtin_return_address(0));
192 		kfree(call);
193 
194 		o = atomic_dec_return(&net->nr_outstanding_calls);
195 		if (o == 0)
196 			wake_up_var(&net->nr_outstanding_calls);
197 	}
198 }
199 
200 static struct afs_call *afs_get_call(struct afs_call *call,
201 				     enum afs_call_trace why)
202 {
203 	int r;
204 
205 	__refcount_inc(&call->ref, &r);
206 
207 	trace_afs_call(call->debug_id, why, r + 1,
208 		       atomic_read(&call->net->nr_outstanding_calls),
209 		       __builtin_return_address(0));
210 	return call;
211 }
212 
213 /*
214  * Queue the call for actual work.
215  */
216 static void afs_queue_call_work(struct afs_call *call)
217 {
218 	if (call->type->work) {
219 		INIT_WORK(&call->work, call->type->work);
220 
221 		afs_get_call(call, afs_call_trace_work);
222 		if (!queue_work(afs_wq, &call->work))
223 			afs_put_call(call);
224 	}
225 }
226 
227 /*
228  * allocate a call with flat request and reply buffers
229  */
230 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
231 				     const struct afs_call_type *type,
232 				     size_t request_size, size_t reply_max)
233 {
234 	struct afs_call *call;
235 
236 	call = afs_alloc_call(net, type, GFP_NOFS);
237 	if (!call)
238 		goto nomem_call;
239 
240 	if (request_size) {
241 		call->request_size = request_size;
242 		call->request = kmalloc(request_size, GFP_NOFS);
243 		if (!call->request)
244 			goto nomem_free;
245 	}
246 
247 	if (reply_max) {
248 		call->reply_max = reply_max;
249 		call->buffer = kmalloc(reply_max, GFP_NOFS);
250 		if (!call->buffer)
251 			goto nomem_free;
252 	}
253 
254 	afs_extract_to_buf(call, call->reply_max);
255 	call->operation_ID = type->op;
256 	init_waitqueue_head(&call->waitq);
257 	return call;
258 
259 nomem_free:
260 	afs_put_call(call);
261 nomem_call:
262 	return NULL;
263 }
264 
265 /*
266  * clean up a call with flat buffer
267  */
268 void afs_flat_call_destructor(struct afs_call *call)
269 {
270 	_enter("");
271 
272 	kfree(call->request);
273 	call->request = NULL;
274 	kfree(call->buffer);
275 	call->buffer = NULL;
276 }
277 
278 /*
279  * Advance the AFS call state when the RxRPC call ends the transmit phase.
280  */
281 static void afs_notify_end_request_tx(struct sock *sock,
282 				      struct rxrpc_call *rxcall,
283 				      unsigned long call_user_ID)
284 {
285 	struct afs_call *call = (struct afs_call *)call_user_ID;
286 
287 	afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
288 }
289 
290 /*
291  * Initiate a call and synchronously queue up the parameters for dispatch.  Any
292  * error is stored into the call struct, which the caller must check for.
293  */
294 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
295 {
296 	struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
297 	struct rxrpc_call *rxcall;
298 	struct msghdr msg;
299 	struct kvec iov[1];
300 	size_t len;
301 	s64 tx_total_len;
302 	int ret;
303 
304 	_enter(",{%pISp},", &srx->transport);
305 
306 	ASSERT(call->type != NULL);
307 	ASSERT(call->type->name != NULL);
308 
309 	_debug("____MAKE %p{%s,%x} [%d]____",
310 	       call, call->type->name, key_serial(call->key),
311 	       atomic_read(&call->net->nr_outstanding_calls));
312 
313 	call->addr_ix = ac->index;
314 	call->alist = afs_get_addrlist(ac->alist);
315 
316 	/* Work out the length we're going to transmit.  This is awkward for
317 	 * calls such as FS.StoreData where there's an extra injection of data
318 	 * after the initial fixed part.
319 	 */
320 	tx_total_len = call->request_size;
321 	if (call->write_iter)
322 		tx_total_len += iov_iter_count(call->write_iter);
323 
324 	/* If the call is going to be asynchronous, we need an extra ref for
325 	 * the call to hold itself so the caller need not hang on to its ref.
326 	 */
327 	if (call->async) {
328 		afs_get_call(call, afs_call_trace_get);
329 		call->drop_ref = true;
330 	}
331 
332 	/* create a call */
333 	rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
334 					 (unsigned long)call,
335 					 tx_total_len, gfp,
336 					 (call->async ?
337 					  afs_wake_up_async_call :
338 					  afs_wake_up_call_waiter),
339 					 call->upgrade,
340 					 (call->intr ? RXRPC_PREINTERRUPTIBLE :
341 					  RXRPC_UNINTERRUPTIBLE),
342 					 call->debug_id);
343 	if (IS_ERR(rxcall)) {
344 		ret = PTR_ERR(rxcall);
345 		call->error = ret;
346 		goto error_kill_call;
347 	}
348 
349 	call->rxcall = rxcall;
350 
351 	if (call->max_lifespan)
352 		rxrpc_kernel_set_max_life(call->net->socket, rxcall,
353 					  call->max_lifespan);
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_put_call(call);
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  */
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  */
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 		if (!call->have_reply_time &&
505 		    rxrpc_kernel_get_reply_time(call->net->socket,
506 						call->rxcall,
507 						&call->reply_time))
508 			call->have_reply_time = true;
509 
510 		ret = call->type->deliver(call);
511 		state = READ_ONCE(call->state);
512 		if (ret == 0 && call->unmarshalling_error)
513 			ret = -EBADMSG;
514 		switch (ret) {
515 		case 0:
516 			afs_queue_call_work(call);
517 			if (state == AFS_CALL_CL_PROC_REPLY) {
518 				if (call->op)
519 					set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
520 						&call->op->server->flags);
521 				goto call_complete;
522 			}
523 			ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
524 			goto done;
525 		case -EINPROGRESS:
526 		case -EAGAIN:
527 			goto out;
528 		case -ECONNABORTED:
529 			ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
530 			afs_log_error(call, call->abort_code);
531 			goto done;
532 		case -ENOTSUPP:
533 			abort_code = RXGEN_OPCODE;
534 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
535 						abort_code, ret, "KIV");
536 			goto local_abort;
537 		case -EIO:
538 			pr_err("kAFS: Call %u in bad state %u\n",
539 			       call->debug_id, state);
540 			fallthrough;
541 		case -ENODATA:
542 		case -EBADMSG:
543 		case -EMSGSIZE:
544 		case -ENOMEM:
545 		case -EFAULT:
546 			abort_code = RXGEN_CC_UNMARSHAL;
547 			if (state != AFS_CALL_CL_AWAIT_REPLY)
548 				abort_code = RXGEN_SS_UNMARSHAL;
549 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
550 						abort_code, ret, "KUM");
551 			goto local_abort;
552 		default:
553 			abort_code = RX_CALL_DEAD;
554 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
555 						abort_code, ret, "KER");
556 			goto local_abort;
557 		}
558 	}
559 
560 done:
561 	if (call->type->done)
562 		call->type->done(call);
563 out:
564 	_leave("");
565 	return;
566 
567 local_abort:
568 	abort_code = 0;
569 call_complete:
570 	afs_set_call_complete(call, ret, remote_abort);
571 	state = AFS_CALL_COMPLETE;
572 	goto done;
573 }
574 
575 /*
576  * Wait synchronously for a call to complete and clean up the call struct.
577  */
578 long afs_wait_for_call_to_complete(struct afs_call *call,
579 				   struct afs_addr_cursor *ac)
580 {
581 	long ret;
582 	bool rxrpc_complete = false;
583 
584 	DECLARE_WAITQUEUE(myself, current);
585 
586 	_enter("");
587 
588 	ret = call->error;
589 	if (ret < 0)
590 		goto out;
591 
592 	add_wait_queue(&call->waitq, &myself);
593 	for (;;) {
594 		set_current_state(TASK_UNINTERRUPTIBLE);
595 
596 		/* deliver any messages that are in the queue */
597 		if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
598 		    call->need_attention) {
599 			call->need_attention = false;
600 			__set_current_state(TASK_RUNNING);
601 			afs_deliver_to_call(call);
602 			continue;
603 		}
604 
605 		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
606 			break;
607 
608 		if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
609 			/* rxrpc terminated the call. */
610 			rxrpc_complete = true;
611 			break;
612 		}
613 
614 		schedule();
615 	}
616 
617 	remove_wait_queue(&call->waitq, &myself);
618 	__set_current_state(TASK_RUNNING);
619 
620 	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
621 		if (rxrpc_complete) {
622 			afs_set_call_complete(call, call->error, call->abort_code);
623 		} else {
624 			/* Kill off the call if it's still live. */
625 			_debug("call interrupted");
626 			if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
627 						    RX_USER_ABORT, -EINTR, "KWI"))
628 				afs_set_call_complete(call, -EINTR, 0);
629 		}
630 	}
631 
632 	spin_lock_bh(&call->state_lock);
633 	ac->abort_code = call->abort_code;
634 	ac->error = call->error;
635 	spin_unlock_bh(&call->state_lock);
636 
637 	ret = ac->error;
638 	switch (ret) {
639 	case 0:
640 		ret = call->ret0;
641 		call->ret0 = 0;
642 
643 		fallthrough;
644 	case -ECONNABORTED:
645 		ac->responded = true;
646 		break;
647 	}
648 
649 out:
650 	_debug("call complete");
651 	afs_put_call(call);
652 	_leave(" = %p", (void *)ret);
653 	return ret;
654 }
655 
656 /*
657  * wake up a waiting call
658  */
659 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
660 				    unsigned long call_user_ID)
661 {
662 	struct afs_call *call = (struct afs_call *)call_user_ID;
663 
664 	call->need_attention = true;
665 	wake_up(&call->waitq);
666 }
667 
668 /*
669  * wake up an asynchronous call
670  */
671 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
672 				   unsigned long call_user_ID)
673 {
674 	struct afs_call *call = (struct afs_call *)call_user_ID;
675 	int r;
676 
677 	trace_afs_notify_call(rxcall, call);
678 	call->need_attention = true;
679 
680 	if (__refcount_inc_not_zero(&call->ref, &r)) {
681 		trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
682 			       atomic_read(&call->net->nr_outstanding_calls),
683 			       __builtin_return_address(0));
684 
685 		if (!queue_work(afs_async_calls, &call->async_work))
686 			afs_put_call(call);
687 	}
688 }
689 
690 /*
691  * Perform I/O processing on an asynchronous call.  The work item carries a ref
692  * to the call struct that we either need to release or to pass on.
693  */
694 static void afs_process_async_call(struct work_struct *work)
695 {
696 	struct afs_call *call = container_of(work, struct afs_call, async_work);
697 
698 	_enter("");
699 
700 	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
701 		call->need_attention = false;
702 		afs_deliver_to_call(call);
703 	}
704 
705 	afs_put_call(call);
706 	_leave("");
707 }
708 
709 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
710 {
711 	struct afs_call *call = (struct afs_call *)user_call_ID;
712 
713 	call->rxcall = rxcall;
714 }
715 
716 /*
717  * Charge the incoming call preallocation.
718  */
719 void afs_charge_preallocation(struct work_struct *work)
720 {
721 	struct afs_net *net =
722 		container_of(work, struct afs_net, charge_preallocation_work);
723 	struct afs_call *call = net->spare_incoming_call;
724 
725 	for (;;) {
726 		if (!call) {
727 			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
728 			if (!call)
729 				break;
730 
731 			call->drop_ref = true;
732 			call->async = true;
733 			call->state = AFS_CALL_SV_AWAIT_OP_ID;
734 			init_waitqueue_head(&call->waitq);
735 			afs_extract_to_tmp(call);
736 		}
737 
738 		if (rxrpc_kernel_charge_accept(net->socket,
739 					       afs_wake_up_async_call,
740 					       afs_rx_attach,
741 					       (unsigned long)call,
742 					       GFP_KERNEL,
743 					       call->debug_id) < 0)
744 			break;
745 		call = NULL;
746 	}
747 	net->spare_incoming_call = call;
748 }
749 
750 /*
751  * Discard a preallocated call when a socket is shut down.
752  */
753 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
754 				    unsigned long user_call_ID)
755 {
756 	struct afs_call *call = (struct afs_call *)user_call_ID;
757 
758 	call->rxcall = NULL;
759 	afs_put_call(call);
760 }
761 
762 /*
763  * Notification of an incoming call.
764  */
765 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
766 			    unsigned long user_call_ID)
767 {
768 	struct afs_net *net = afs_sock2net(sk);
769 
770 	queue_work(afs_wq, &net->charge_preallocation_work);
771 }
772 
773 /*
774  * Grab the operation ID from an incoming cache manager call.  The socket
775  * buffer is discarded on error or if we don't yet have sufficient data.
776  */
777 static int afs_deliver_cm_op_id(struct afs_call *call)
778 {
779 	int ret;
780 
781 	_enter("{%zu}", iov_iter_count(call->iter));
782 
783 	/* the operation ID forms the first four bytes of the request data */
784 	ret = afs_extract_data(call, true);
785 	if (ret < 0)
786 		return ret;
787 
788 	call->operation_ID = ntohl(call->tmp);
789 	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
790 
791 	/* ask the cache manager to route the call (it'll change the call type
792 	 * if successful) */
793 	if (!afs_cm_incoming_call(call))
794 		return -ENOTSUPP;
795 
796 	trace_afs_cb_call(call);
797 
798 	/* pass responsibility for the remainer of this message off to the
799 	 * cache manager op */
800 	return call->type->deliver(call);
801 }
802 
803 /*
804  * Advance the AFS call state when an RxRPC service call ends the transmit
805  * phase.
806  */
807 static void afs_notify_end_reply_tx(struct sock *sock,
808 				    struct rxrpc_call *rxcall,
809 				    unsigned long call_user_ID)
810 {
811 	struct afs_call *call = (struct afs_call *)call_user_ID;
812 
813 	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
814 }
815 
816 /*
817  * send an empty reply
818  */
819 void afs_send_empty_reply(struct afs_call *call)
820 {
821 	struct afs_net *net = call->net;
822 	struct msghdr msg;
823 
824 	_enter("");
825 
826 	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
827 
828 	msg.msg_name		= NULL;
829 	msg.msg_namelen		= 0;
830 	iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
831 	msg.msg_control		= NULL;
832 	msg.msg_controllen	= 0;
833 	msg.msg_flags		= 0;
834 
835 	switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
836 				       afs_notify_end_reply_tx)) {
837 	case 0:
838 		_leave(" [replied]");
839 		return;
840 
841 	case -ENOMEM:
842 		_debug("oom");
843 		rxrpc_kernel_abort_call(net->socket, call->rxcall,
844 					RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
845 		fallthrough;
846 	default:
847 		_leave(" [error]");
848 		return;
849 	}
850 }
851 
852 /*
853  * send a simple reply
854  */
855 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
856 {
857 	struct afs_net *net = call->net;
858 	struct msghdr msg;
859 	struct kvec iov[1];
860 	int n;
861 
862 	_enter("");
863 
864 	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
865 
866 	iov[0].iov_base		= (void *) buf;
867 	iov[0].iov_len		= len;
868 	msg.msg_name		= NULL;
869 	msg.msg_namelen		= 0;
870 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
871 	msg.msg_control		= NULL;
872 	msg.msg_controllen	= 0;
873 	msg.msg_flags		= 0;
874 
875 	n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
876 				   afs_notify_end_reply_tx);
877 	if (n >= 0) {
878 		/* Success */
879 		_leave(" [replied]");
880 		return;
881 	}
882 
883 	if (n == -ENOMEM) {
884 		_debug("oom");
885 		rxrpc_kernel_abort_call(net->socket, call->rxcall,
886 					RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
887 	}
888 	_leave(" [error]");
889 }
890 
891 /*
892  * Extract a piece of data from the received data socket buffers.
893  */
894 int afs_extract_data(struct afs_call *call, bool want_more)
895 {
896 	struct afs_net *net = call->net;
897 	struct iov_iter *iter = call->iter;
898 	enum afs_call_state state;
899 	u32 remote_abort = 0;
900 	int ret;
901 
902 	_enter("{%s,%zu,%zu},%d",
903 	       call->type->name, call->iov_len, iov_iter_count(iter), want_more);
904 
905 	ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
906 				     &call->iov_len, want_more, &remote_abort,
907 				     &call->service_id);
908 	if (ret == 0 || ret == -EAGAIN)
909 		return ret;
910 
911 	state = READ_ONCE(call->state);
912 	if (ret == 1) {
913 		switch (state) {
914 		case AFS_CALL_CL_AWAIT_REPLY:
915 			afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
916 			break;
917 		case AFS_CALL_SV_AWAIT_REQUEST:
918 			afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
919 			break;
920 		case AFS_CALL_COMPLETE:
921 			kdebug("prem complete %d", call->error);
922 			return afs_io_error(call, afs_io_error_extract);
923 		default:
924 			break;
925 		}
926 		return 0;
927 	}
928 
929 	afs_set_call_complete(call, ret, remote_abort);
930 	return ret;
931 }
932 
933 /*
934  * Log protocol error production.
935  */
936 noinline int afs_protocol_error(struct afs_call *call,
937 				enum afs_eproto_cause cause)
938 {
939 	trace_afs_protocol_error(call, cause);
940 	if (call)
941 		call->unmarshalling_error = true;
942 	return -EBADMSG;
943 }
944