xref: /linux/fs/afs/rxrpc.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
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_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 
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  */
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  */
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  */
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  */
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  */
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 	} else {
404 		len = 0;
405 		iov_iter_kvec(&msg.msg_iter, ITER_DEST, NULL, 0, 0);
406 		rxrpc_kernel_recv_data(call->net->socket, rxcall,
407 				       &msg.msg_iter, &len, false,
408 				       &call->abort_code, &call->service_id);
409 		ac->abort_code = call->abort_code;
410 		ac->responded = true;
411 	}
412 	call->error = ret;
413 	trace_afs_call_done(call);
414 error_kill_call:
415 	if (call->type->done)
416 		call->type->done(call);
417 
418 	/* We need to dispose of the extra ref we grabbed for an async call.
419 	 * The call, however, might be queued on afs_async_calls and we need to
420 	 * make sure we don't get any more notifications that might requeue it.
421 	 */
422 	if (call->rxcall)
423 		rxrpc_kernel_shutdown_call(call->net->socket, call->rxcall);
424 	if (call->async) {
425 		if (cancel_work_sync(&call->async_work))
426 			afs_put_call(call);
427 		afs_put_call(call);
428 	}
429 
430 	ac->error = ret;
431 	call->state = AFS_CALL_COMPLETE;
432 	_leave(" = %d", ret);
433 }
434 
435 /*
436  * Log remote abort codes that indicate that we have a protocol disagreement
437  * with the server.
438  */
439 static void afs_log_error(struct afs_call *call, s32 remote_abort)
440 {
441 	static int max = 0;
442 	const char *msg;
443 	int m;
444 
445 	switch (remote_abort) {
446 	case RX_EOF:		 msg = "unexpected EOF";	break;
447 	case RXGEN_CC_MARSHAL:	 msg = "client marshalling";	break;
448 	case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling";	break;
449 	case RXGEN_SS_MARSHAL:	 msg = "server marshalling";	break;
450 	case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling";	break;
451 	case RXGEN_DECODE:	 msg = "opcode decode";		break;
452 	case RXGEN_SS_XDRFREE:	 msg = "server XDR cleanup";	break;
453 	case RXGEN_CC_XDRFREE:	 msg = "client XDR cleanup";	break;
454 	case -32:		 msg = "insufficient data";	break;
455 	default:
456 		return;
457 	}
458 
459 	m = max;
460 	if (m < 3) {
461 		max = m + 1;
462 		pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n",
463 			  msg, call->type->name,
464 			  &call->alist->addrs[call->addr_ix].transport);
465 	}
466 }
467 
468 /*
469  * deliver messages to a call
470  */
471 static void afs_deliver_to_call(struct afs_call *call)
472 {
473 	enum afs_call_state state;
474 	size_t len;
475 	u32 abort_code, remote_abort = 0;
476 	int ret;
477 
478 	_enter("%s", call->type->name);
479 
480 	while (state = READ_ONCE(call->state),
481 	       state == AFS_CALL_CL_AWAIT_REPLY ||
482 	       state == AFS_CALL_SV_AWAIT_OP_ID ||
483 	       state == AFS_CALL_SV_AWAIT_REQUEST ||
484 	       state == AFS_CALL_SV_AWAIT_ACK
485 	       ) {
486 		if (state == AFS_CALL_SV_AWAIT_ACK) {
487 			len = 0;
488 			iov_iter_kvec(&call->def_iter, ITER_DEST, NULL, 0, 0);
489 			ret = rxrpc_kernel_recv_data(call->net->socket,
490 						     call->rxcall, &call->def_iter,
491 						     &len, false, &remote_abort,
492 						     &call->service_id);
493 			trace_afs_receive_data(call, &call->def_iter, false, ret);
494 
495 			if (ret == -EINPROGRESS || ret == -EAGAIN)
496 				return;
497 			if (ret < 0 || ret == 1) {
498 				if (ret == 1)
499 					ret = 0;
500 				goto call_complete;
501 			}
502 			return;
503 		}
504 
505 		ret = call->type->deliver(call);
506 		state = READ_ONCE(call->state);
507 		if (ret == 0 && call->unmarshalling_error)
508 			ret = -EBADMSG;
509 		switch (ret) {
510 		case 0:
511 			afs_queue_call_work(call);
512 			if (state == AFS_CALL_CL_PROC_REPLY) {
513 				if (call->op)
514 					set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
515 						&call->op->server->flags);
516 				goto call_complete;
517 			}
518 			ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
519 			goto done;
520 		case -EINPROGRESS:
521 		case -EAGAIN:
522 			goto out;
523 		case -ECONNABORTED:
524 			ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
525 			afs_log_error(call, call->abort_code);
526 			goto done;
527 		case -ENOTSUPP:
528 			abort_code = RXGEN_OPCODE;
529 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
530 						abort_code, ret,
531 						afs_abort_op_not_supported);
532 			goto local_abort;
533 		case -EIO:
534 			pr_err("kAFS: Call %u in bad state %u\n",
535 			       call->debug_id, state);
536 			fallthrough;
537 		case -ENODATA:
538 		case -EBADMSG:
539 		case -EMSGSIZE:
540 		case -ENOMEM:
541 		case -EFAULT:
542 			abort_code = RXGEN_CC_UNMARSHAL;
543 			if (state != AFS_CALL_CL_AWAIT_REPLY)
544 				abort_code = RXGEN_SS_UNMARSHAL;
545 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
546 						abort_code, ret,
547 						afs_abort_unmarshal_error);
548 			goto local_abort;
549 		default:
550 			abort_code = RX_CALL_DEAD;
551 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
552 						abort_code, ret,
553 						afs_abort_general_error);
554 			goto local_abort;
555 		}
556 	}
557 
558 done:
559 	if (call->type->done)
560 		call->type->done(call);
561 out:
562 	_leave("");
563 	return;
564 
565 local_abort:
566 	abort_code = 0;
567 call_complete:
568 	afs_set_call_complete(call, ret, remote_abort);
569 	state = AFS_CALL_COMPLETE;
570 	goto done;
571 }
572 
573 /*
574  * Wait synchronously for a call to complete and clean up the call struct.
575  */
576 long afs_wait_for_call_to_complete(struct afs_call *call,
577 				   struct afs_addr_cursor *ac)
578 {
579 	long ret;
580 	bool rxrpc_complete = false;
581 
582 	DECLARE_WAITQUEUE(myself, current);
583 
584 	_enter("");
585 
586 	ret = call->error;
587 	if (ret < 0)
588 		goto out;
589 
590 	add_wait_queue(&call->waitq, &myself);
591 	for (;;) {
592 		set_current_state(TASK_UNINTERRUPTIBLE);
593 
594 		/* deliver any messages that are in the queue */
595 		if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
596 		    call->need_attention) {
597 			call->need_attention = false;
598 			__set_current_state(TASK_RUNNING);
599 			afs_deliver_to_call(call);
600 			continue;
601 		}
602 
603 		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
604 			break;
605 
606 		if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
607 			/* rxrpc terminated the call. */
608 			rxrpc_complete = true;
609 			break;
610 		}
611 
612 		schedule();
613 	}
614 
615 	remove_wait_queue(&call->waitq, &myself);
616 	__set_current_state(TASK_RUNNING);
617 
618 	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
619 		if (rxrpc_complete) {
620 			afs_set_call_complete(call, call->error, call->abort_code);
621 		} else {
622 			/* Kill off the call if it's still live. */
623 			_debug("call interrupted");
624 			if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
625 						    RX_USER_ABORT, -EINTR,
626 						    afs_abort_interrupted))
627 				afs_set_call_complete(call, -EINTR, 0);
628 		}
629 	}
630 
631 	spin_lock_bh(&call->state_lock);
632 	ac->abort_code = call->abort_code;
633 	ac->error = call->error;
634 	spin_unlock_bh(&call->state_lock);
635 
636 	ret = ac->error;
637 	switch (ret) {
638 	case 0:
639 		ret = call->ret0;
640 		call->ret0 = 0;
641 
642 		fallthrough;
643 	case -ECONNABORTED:
644 		ac->responded = true;
645 		break;
646 	}
647 
648 out:
649 	_debug("call complete");
650 	afs_put_call(call);
651 	_leave(" = %p", (void *)ret);
652 	return ret;
653 }
654 
655 /*
656  * wake up a waiting call
657  */
658 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
659 				    unsigned long call_user_ID)
660 {
661 	struct afs_call *call = (struct afs_call *)call_user_ID;
662 
663 	call->need_attention = true;
664 	wake_up(&call->waitq);
665 }
666 
667 /*
668  * wake up an asynchronous call
669  */
670 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
671 				   unsigned long call_user_ID)
672 {
673 	struct afs_call *call = (struct afs_call *)call_user_ID;
674 	int r;
675 
676 	trace_afs_notify_call(rxcall, call);
677 	call->need_attention = true;
678 
679 	if (__refcount_inc_not_zero(&call->ref, &r)) {
680 		trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
681 			       atomic_read(&call->net->nr_outstanding_calls),
682 			       __builtin_return_address(0));
683 
684 		if (!queue_work(afs_async_calls, &call->async_work))
685 			afs_put_call(call);
686 	}
687 }
688 
689 /*
690  * Perform I/O processing on an asynchronous call.  The work item carries a ref
691  * to the call struct that we either need to release or to pass on.
692  */
693 static void afs_process_async_call(struct work_struct *work)
694 {
695 	struct afs_call *call = container_of(work, struct afs_call, async_work);
696 
697 	_enter("");
698 
699 	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
700 		call->need_attention = false;
701 		afs_deliver_to_call(call);
702 	}
703 
704 	afs_put_call(call);
705 	_leave("");
706 }
707 
708 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
709 {
710 	struct afs_call *call = (struct afs_call *)user_call_ID;
711 
712 	call->rxcall = rxcall;
713 }
714 
715 /*
716  * Charge the incoming call preallocation.
717  */
718 void afs_charge_preallocation(struct work_struct *work)
719 {
720 	struct afs_net *net =
721 		container_of(work, struct afs_net, charge_preallocation_work);
722 	struct afs_call *call = net->spare_incoming_call;
723 
724 	for (;;) {
725 		if (!call) {
726 			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
727 			if (!call)
728 				break;
729 
730 			call->drop_ref = true;
731 			call->async = true;
732 			call->state = AFS_CALL_SV_AWAIT_OP_ID;
733 			init_waitqueue_head(&call->waitq);
734 			afs_extract_to_tmp(call);
735 		}
736 
737 		if (rxrpc_kernel_charge_accept(net->socket,
738 					       afs_wake_up_async_call,
739 					       afs_rx_attach,
740 					       (unsigned long)call,
741 					       GFP_KERNEL,
742 					       call->debug_id) < 0)
743 			break;
744 		call = NULL;
745 	}
746 	net->spare_incoming_call = call;
747 }
748 
749 /*
750  * Discard a preallocated call when a socket is shut down.
751  */
752 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
753 				    unsigned long user_call_ID)
754 {
755 	struct afs_call *call = (struct afs_call *)user_call_ID;
756 
757 	call->rxcall = NULL;
758 	afs_put_call(call);
759 }
760 
761 /*
762  * Notification of an incoming call.
763  */
764 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
765 			    unsigned long user_call_ID)
766 {
767 	struct afs_net *net = afs_sock2net(sk);
768 
769 	queue_work(afs_wq, &net->charge_preallocation_work);
770 }
771 
772 /*
773  * Grab the operation ID from an incoming cache manager call.  The socket
774  * buffer is discarded on error or if we don't yet have sufficient data.
775  */
776 static int afs_deliver_cm_op_id(struct afs_call *call)
777 {
778 	int ret;
779 
780 	_enter("{%zu}", iov_iter_count(call->iter));
781 
782 	/* the operation ID forms the first four bytes of the request data */
783 	ret = afs_extract_data(call, true);
784 	if (ret < 0)
785 		return ret;
786 
787 	call->operation_ID = ntohl(call->tmp);
788 	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
789 
790 	/* ask the cache manager to route the call (it'll change the call type
791 	 * if successful) */
792 	if (!afs_cm_incoming_call(call))
793 		return -ENOTSUPP;
794 
795 	trace_afs_cb_call(call);
796 
797 	/* pass responsibility for the remainer of this message off to the
798 	 * cache manager op */
799 	return call->type->deliver(call);
800 }
801 
802 /*
803  * Advance the AFS call state when an RxRPC service call ends the transmit
804  * phase.
805  */
806 static void afs_notify_end_reply_tx(struct sock *sock,
807 				    struct rxrpc_call *rxcall,
808 				    unsigned long call_user_ID)
809 {
810 	struct afs_call *call = (struct afs_call *)call_user_ID;
811 
812 	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
813 }
814 
815 /*
816  * send an empty reply
817  */
818 void afs_send_empty_reply(struct afs_call *call)
819 {
820 	struct afs_net *net = call->net;
821 	struct msghdr msg;
822 
823 	_enter("");
824 
825 	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
826 
827 	msg.msg_name		= NULL;
828 	msg.msg_namelen		= 0;
829 	iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0);
830 	msg.msg_control		= NULL;
831 	msg.msg_controllen	= 0;
832 	msg.msg_flags		= 0;
833 
834 	switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
835 				       afs_notify_end_reply_tx)) {
836 	case 0:
837 		_leave(" [replied]");
838 		return;
839 
840 	case -ENOMEM:
841 		_debug("oom");
842 		rxrpc_kernel_abort_call(net->socket, call->rxcall,
843 					RXGEN_SS_MARSHAL, -ENOMEM,
844 					afs_abort_oom);
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, ITER_SOURCE, 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,
887 					afs_abort_oom);
888 	}
889 	_leave(" [error]");
890 }
891 
892 /*
893  * Extract a piece of data from the received data socket buffers.
894  */
895 int afs_extract_data(struct afs_call *call, bool want_more)
896 {
897 	struct afs_net *net = call->net;
898 	struct iov_iter *iter = call->iter;
899 	enum afs_call_state state;
900 	u32 remote_abort = 0;
901 	int ret;
902 
903 	_enter("{%s,%zu,%zu},%d",
904 	       call->type->name, call->iov_len, iov_iter_count(iter), want_more);
905 
906 	ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
907 				     &call->iov_len, want_more, &remote_abort,
908 				     &call->service_id);
909 	trace_afs_receive_data(call, call->iter, want_more, ret);
910 	if (ret == 0 || ret == -EAGAIN)
911 		return ret;
912 
913 	state = READ_ONCE(call->state);
914 	if (ret == 1) {
915 		switch (state) {
916 		case AFS_CALL_CL_AWAIT_REPLY:
917 			afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
918 			break;
919 		case AFS_CALL_SV_AWAIT_REQUEST:
920 			afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
921 			break;
922 		case AFS_CALL_COMPLETE:
923 			kdebug("prem complete %d", call->error);
924 			return afs_io_error(call, afs_io_error_extract);
925 		default:
926 			break;
927 		}
928 		return 0;
929 	}
930 
931 	afs_set_call_complete(call, ret, remote_abort);
932 	return ret;
933 }
934 
935 /*
936  * Log protocol error production.
937  */
938 noinline int afs_protocol_error(struct afs_call *call,
939 				enum afs_eproto_cause cause)
940 {
941 	trace_afs_protocol_error(call, cause);
942 	if (call)
943 		call->unmarshalling_error = true;
944 	return -EBADMSG;
945 }
946