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