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