xref: /linux/fs/afs/rxrpc.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Maintain an RxRPC server socket to do AFS communications through
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/slab.h>
9 #include <linux/sched/signal.h>
10 
11 #include <net/sock.h>
12 #include <net/af_rxrpc.h>
13 #include "internal.h"
14 #include "afs_cm.h"
15 #include "protocol_yfs.h"
16 #define RXRPC_TRACE_ONLY_DEFINE_ENUMS
17 #include <trace/events/rxrpc.h>
18 
19 struct workqueue_struct *afs_async_calls;
20 
21 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
22 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
23 static void afs_process_async_call(struct work_struct *);
24 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
25 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
26 static int afs_deliver_cm_op_id(struct afs_call *);
27 
28 /* asynchronous incoming call initial processing */
29 static const struct afs_call_type afs_RXCMxxxx = {
30 	.name		= "CB.xxxx",
31 	.deliver	= afs_deliver_cm_op_id,
32 };
33 
34 /*
35  * open an RxRPC socket and bind it to be a server for callback notifications
36  * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
37  */
afs_open_socket(struct afs_net * net)38 int afs_open_socket(struct afs_net *net)
39 {
40 	struct sockaddr_rxrpc srx;
41 	struct socket *socket;
42 	int ret;
43 
44 	_enter("");
45 
46 	ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
47 	if (ret < 0)
48 		goto error_1;
49 
50 	socket->sk->sk_allocation = GFP_NOFS;
51 
52 	/* bind the callback manager's address to make this a server socket */
53 	memset(&srx, 0, sizeof(srx));
54 	srx.srx_family			= AF_RXRPC;
55 	srx.srx_service			= CM_SERVICE;
56 	srx.transport_type		= SOCK_DGRAM;
57 	srx.transport_len		= sizeof(srx.transport.sin6);
58 	srx.transport.sin6.sin6_family	= AF_INET6;
59 	srx.transport.sin6.sin6_port	= htons(AFS_CM_PORT);
60 
61 	ret = rxrpc_sock_set_min_security_level(socket->sk,
62 						RXRPC_SECURITY_ENCRYPT);
63 	if (ret < 0)
64 		goto error_2;
65 
66 	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
67 	if (ret == -EADDRINUSE) {
68 		srx.transport.sin6.sin6_port = 0;
69 		ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
70 	}
71 	if (ret < 0)
72 		goto error_2;
73 
74 	srx.srx_service = YFS_CM_SERVICE;
75 	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
76 	if (ret < 0)
77 		goto error_2;
78 
79 	/* Ideally, we'd turn on service upgrade here, but we can't because
80 	 * OpenAFS is buggy and leaks the userStatus field from packet to
81 	 * packet and between FS packets and CB packets - so if we try to do an
82 	 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
83 	 * it sends back to us.
84 	 */
85 
86 	rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
87 					   afs_rx_discard_new_call);
88 
89 	ret = kernel_listen(socket, INT_MAX);
90 	if (ret < 0)
91 		goto error_2;
92 
93 	net->socket = socket;
94 	afs_charge_preallocation(&net->charge_preallocation_work);
95 	_leave(" = 0");
96 	return 0;
97 
98 error_2:
99 	sock_release(socket);
100 error_1:
101 	_leave(" = %d", ret);
102 	return ret;
103 }
104 
105 /*
106  * close the RxRPC socket AFS was using
107  */
afs_close_socket(struct afs_net * net)108 void afs_close_socket(struct afs_net *net)
109 {
110 	_enter("");
111 
112 	kernel_listen(net->socket, 0);
113 	flush_workqueue(afs_async_calls);
114 
115 	if (net->spare_incoming_call) {
116 		afs_put_call(net->spare_incoming_call);
117 		net->spare_incoming_call = NULL;
118 	}
119 
120 	_debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
121 	wait_var_event(&net->nr_outstanding_calls,
122 		       !atomic_read(&net->nr_outstanding_calls));
123 	_debug("no outstanding calls");
124 
125 	kernel_sock_shutdown(net->socket, SHUT_RDWR);
126 	flush_workqueue(afs_async_calls);
127 	sock_release(net->socket);
128 
129 	_debug("dework");
130 	_leave("");
131 }
132 
133 /*
134  * Allocate a call.
135  */
afs_alloc_call(struct afs_net * net,const struct afs_call_type * type,gfp_t gfp)136 static struct afs_call *afs_alloc_call(struct afs_net *net,
137 				       const struct afs_call_type *type,
138 				       gfp_t gfp)
139 {
140 	struct afs_call *call;
141 	int o;
142 
143 	call = kzalloc(sizeof(*call), gfp);
144 	if (!call)
145 		return NULL;
146 
147 	call->type = type;
148 	call->net = net;
149 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
150 	refcount_set(&call->ref, 1);
151 	INIT_WORK(&call->async_work, afs_process_async_call);
152 	init_waitqueue_head(&call->waitq);
153 	spin_lock_init(&call->state_lock);
154 	call->iter = &call->def_iter;
155 
156 	o = atomic_inc_return(&net->nr_outstanding_calls);
157 	trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o,
158 		       __builtin_return_address(0));
159 	return call;
160 }
161 
162 /*
163  * Dispose of a reference on a call.
164  */
afs_put_call(struct afs_call * call)165 void afs_put_call(struct afs_call *call)
166 {
167 	struct afs_net *net = call->net;
168 	unsigned int debug_id = call->debug_id;
169 	bool zero;
170 	int r, o;
171 
172 	zero = __refcount_dec_and_test(&call->ref, &r);
173 	o = atomic_read(&net->nr_outstanding_calls);
174 	trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
175 		       __builtin_return_address(0));
176 
177 	if (zero) {
178 		ASSERT(!work_pending(&call->async_work));
179 		ASSERT(call->type->name != NULL);
180 
181 		rxrpc_kernel_put_peer(call->peer);
182 
183 		if (call->rxcall) {
184 			rxrpc_kernel_shutdown_call(net->socket, call->rxcall);
185 			rxrpc_kernel_put_call(net->socket, call->rxcall);
186 			call->rxcall = NULL;
187 		}
188 		if (call->type->destructor)
189 			call->type->destructor(call);
190 
191 		afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
192 		kfree(call->request);
193 
194 		trace_afs_call(call->debug_id, afs_call_trace_free, 0, o,
195 			       __builtin_return_address(0));
196 		kfree(call);
197 
198 		o = atomic_dec_return(&net->nr_outstanding_calls);
199 		if (o == 0)
200 			wake_up_var(&net->nr_outstanding_calls);
201 	}
202 }
203 
afs_get_call(struct afs_call * call,enum afs_call_trace why)204 static struct afs_call *afs_get_call(struct afs_call *call,
205 				     enum afs_call_trace why)
206 {
207 	int r;
208 
209 	__refcount_inc(&call->ref, &r);
210 
211 	trace_afs_call(call->debug_id, why, r + 1,
212 		       atomic_read(&call->net->nr_outstanding_calls),
213 		       __builtin_return_address(0));
214 	return call;
215 }
216 
217 /*
218  * Queue the call for actual work.
219  */
afs_queue_call_work(struct afs_call * call)220 static void afs_queue_call_work(struct afs_call *call)
221 {
222 	if (call->type->work) {
223 		INIT_WORK(&call->work, call->type->work);
224 
225 		afs_get_call(call, afs_call_trace_work);
226 		if (!queue_work(afs_wq, &call->work))
227 			afs_put_call(call);
228 	}
229 }
230 
231 /*
232  * allocate a call with flat request and reply buffers
233  */
afs_alloc_flat_call(struct afs_net * net,const struct afs_call_type * type,size_t request_size,size_t reply_max)234 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
235 				     const struct afs_call_type *type,
236 				     size_t request_size, size_t reply_max)
237 {
238 	struct afs_call *call;
239 
240 	call = afs_alloc_call(net, type, GFP_NOFS);
241 	if (!call)
242 		goto nomem_call;
243 
244 	if (request_size) {
245 		call->request_size = request_size;
246 		call->request = kmalloc(request_size, GFP_NOFS);
247 		if (!call->request)
248 			goto nomem_free;
249 	}
250 
251 	if (reply_max) {
252 		call->reply_max = reply_max;
253 		call->buffer = kmalloc(reply_max, GFP_NOFS);
254 		if (!call->buffer)
255 			goto nomem_free;
256 	}
257 
258 	afs_extract_to_buf(call, call->reply_max);
259 	call->operation_ID = type->op;
260 	init_waitqueue_head(&call->waitq);
261 	return call;
262 
263 nomem_free:
264 	afs_put_call(call);
265 nomem_call:
266 	return NULL;
267 }
268 
269 /*
270  * clean up a call with flat buffer
271  */
afs_flat_call_destructor(struct afs_call * call)272 void afs_flat_call_destructor(struct afs_call *call)
273 {
274 	_enter("");
275 
276 	kfree(call->request);
277 	call->request = NULL;
278 	kfree(call->buffer);
279 	call->buffer = NULL;
280 }
281 
282 /*
283  * Advance the AFS call state when the RxRPC call ends the transmit phase.
284  */
afs_notify_end_request_tx(struct sock * sock,struct rxrpc_call * rxcall,unsigned long call_user_ID)285 static void afs_notify_end_request_tx(struct sock *sock,
286 				      struct rxrpc_call *rxcall,
287 				      unsigned long call_user_ID)
288 {
289 	struct afs_call *call = (struct afs_call *)call_user_ID;
290 
291 	afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
292 }
293 
294 /*
295  * Initiate a call and synchronously queue up the parameters for dispatch.  Any
296  * error is stored into the call struct, which the caller must check for.
297  */
afs_make_call(struct afs_call * call,gfp_t gfp)298 void afs_make_call(struct afs_call *call, gfp_t gfp)
299 {
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+%u},", rxrpc_kernel_remote_addr(call->peer), call->service_id);
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 	trace_afs_make_call(call);
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, call->peer, call->key,
336 					 (unsigned long)call,
337 					 tx_total_len,
338 					 call->max_lifespan,
339 					 gfp,
340 					 (call->async ?
341 					  afs_wake_up_async_call :
342 					  afs_wake_up_call_waiter),
343 					 call->service_id,
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)
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 		call->responded = true;
410 	}
411 	call->error = ret;
412 	trace_afs_call_done(call);
413 error_kill_call:
414 	if (call->type->done)
415 		call->type->done(call);
416 
417 	/* We need to dispose of the extra ref we grabbed for an async call.
418 	 * The call, however, might be queued on afs_async_calls and we need to
419 	 * make sure we don't get any more notifications that might requeue it.
420 	 */
421 	if (call->rxcall)
422 		rxrpc_kernel_shutdown_call(call->net->socket, call->rxcall);
423 	if (call->async) {
424 		if (cancel_work_sync(&call->async_work))
425 			afs_put_call(call);
426 		afs_set_call_complete(call, ret, 0);
427 	}
428 
429 	call->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  */
afs_log_error(struct afs_call * call,s32 remote_abort)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 			  rxrpc_kernel_remote_addr(call->peer));
464 	}
465 }
466 
467 /*
468  * deliver messages to a call
469  */
afs_deliver_to_call(struct afs_call * call)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, ITER_DEST, 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 		ret = call->type->deliver(call);
505 		state = READ_ONCE(call->state);
506 		if (ret == 0 && call->unmarshalling_error)
507 			ret = -EBADMSG;
508 		switch (ret) {
509 		case 0:
510 			call->responded = true;
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 			call->responded = true;
526 			afs_log_error(call, call->abort_code);
527 			goto done;
528 		case -ENOTSUPP:
529 			call->responded = true;
530 			abort_code = RXGEN_OPCODE;
531 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
532 						abort_code, ret,
533 						afs_abort_op_not_supported);
534 			goto local_abort;
535 		case -EIO:
536 			pr_err("kAFS: Call %u in bad state %u\n",
537 			       call->debug_id, state);
538 			fallthrough;
539 		case -ENODATA:
540 		case -EBADMSG:
541 		case -EMSGSIZE:
542 		case -ENOMEM:
543 		case -EFAULT:
544 			abort_code = RXGEN_CC_UNMARSHAL;
545 			if (state != AFS_CALL_CL_AWAIT_REPLY)
546 				abort_code = RXGEN_SS_UNMARSHAL;
547 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
548 						abort_code, ret,
549 						afs_abort_unmarshal_error);
550 			goto local_abort;
551 		default:
552 			abort_code = RX_CALL_DEAD;
553 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
554 						abort_code, ret,
555 						afs_abort_general_error);
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.
577  */
afs_wait_for_call_to_complete(struct afs_call * call)578 void afs_wait_for_call_to_complete(struct afs_call *call)
579 {
580 	bool rxrpc_complete = false;
581 
582 	_enter("");
583 
584 	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
585 		DECLARE_WAITQUEUE(myself, current);
586 
587 		add_wait_queue(&call->waitq, &myself);
588 		for (;;) {
589 			set_current_state(TASK_UNINTERRUPTIBLE);
590 
591 			/* deliver any messages that are in the queue */
592 			if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
593 			    call->need_attention) {
594 				call->need_attention = false;
595 				__set_current_state(TASK_RUNNING);
596 				afs_deliver_to_call(call);
597 				continue;
598 			}
599 
600 			if (afs_check_call_state(call, AFS_CALL_COMPLETE))
601 				break;
602 
603 			if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
604 				/* rxrpc terminated the call. */
605 				rxrpc_complete = true;
606 				break;
607 			}
608 
609 			schedule();
610 		}
611 
612 		remove_wait_queue(&call->waitq, &myself);
613 		__set_current_state(TASK_RUNNING);
614 	}
615 
616 	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
617 		if (rxrpc_complete) {
618 			afs_set_call_complete(call, call->error, call->abort_code);
619 		} else {
620 			/* Kill off the call if it's still live. */
621 			_debug("call interrupted");
622 			if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
623 						    RX_USER_ABORT, -EINTR,
624 						    afs_abort_interrupted))
625 				afs_set_call_complete(call, -EINTR, 0);
626 		}
627 	}
628 }
629 
630 /*
631  * wake up a waiting call
632  */
afs_wake_up_call_waiter(struct sock * sk,struct rxrpc_call * rxcall,unsigned long call_user_ID)633 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
634 				    unsigned long call_user_ID)
635 {
636 	struct afs_call *call = (struct afs_call *)call_user_ID;
637 
638 	call->need_attention = true;
639 	wake_up(&call->waitq);
640 }
641 
642 /*
643  * wake up an asynchronous call
644  */
afs_wake_up_async_call(struct sock * sk,struct rxrpc_call * rxcall,unsigned long call_user_ID)645 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
646 				   unsigned long call_user_ID)
647 {
648 	struct afs_call *call = (struct afs_call *)call_user_ID;
649 	int r;
650 
651 	trace_afs_notify_call(rxcall, call);
652 	call->need_attention = true;
653 
654 	if (__refcount_inc_not_zero(&call->ref, &r)) {
655 		trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
656 			       atomic_read(&call->net->nr_outstanding_calls),
657 			       __builtin_return_address(0));
658 
659 		if (!queue_work(afs_async_calls, &call->async_work))
660 			afs_put_call(call);
661 	}
662 }
663 
664 /*
665  * Perform I/O processing on an asynchronous call.  The work item carries a ref
666  * to the call struct that we either need to release or to pass on.
667  */
afs_process_async_call(struct work_struct * work)668 static void afs_process_async_call(struct work_struct *work)
669 {
670 	struct afs_call *call = container_of(work, struct afs_call, async_work);
671 
672 	_enter("");
673 
674 	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
675 		call->need_attention = false;
676 		afs_deliver_to_call(call);
677 	}
678 
679 	afs_put_call(call);
680 	_leave("");
681 }
682 
afs_rx_attach(struct rxrpc_call * rxcall,unsigned long user_call_ID)683 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
684 {
685 	struct afs_call *call = (struct afs_call *)user_call_ID;
686 
687 	call->rxcall = rxcall;
688 }
689 
690 /*
691  * Charge the incoming call preallocation.
692  */
afs_charge_preallocation(struct work_struct * work)693 void afs_charge_preallocation(struct work_struct *work)
694 {
695 	struct afs_net *net =
696 		container_of(work, struct afs_net, charge_preallocation_work);
697 	struct afs_call *call = net->spare_incoming_call;
698 
699 	for (;;) {
700 		if (!call) {
701 			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
702 			if (!call)
703 				break;
704 
705 			call->drop_ref = true;
706 			call->async = true;
707 			call->state = AFS_CALL_SV_AWAIT_OP_ID;
708 			init_waitqueue_head(&call->waitq);
709 			afs_extract_to_tmp(call);
710 		}
711 
712 		if (rxrpc_kernel_charge_accept(net->socket,
713 					       afs_wake_up_async_call,
714 					       afs_rx_attach,
715 					       (unsigned long)call,
716 					       GFP_KERNEL,
717 					       call->debug_id) < 0)
718 			break;
719 		call = NULL;
720 	}
721 	net->spare_incoming_call = call;
722 }
723 
724 /*
725  * Discard a preallocated call when a socket is shut down.
726  */
afs_rx_discard_new_call(struct rxrpc_call * rxcall,unsigned long user_call_ID)727 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
728 				    unsigned long user_call_ID)
729 {
730 	struct afs_call *call = (struct afs_call *)user_call_ID;
731 
732 	call->rxcall = NULL;
733 	afs_put_call(call);
734 }
735 
736 /*
737  * Notification of an incoming call.
738  */
afs_rx_new_call(struct sock * sk,struct rxrpc_call * rxcall,unsigned long user_call_ID)739 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
740 			    unsigned long user_call_ID)
741 {
742 	struct afs_net *net = afs_sock2net(sk);
743 
744 	queue_work(afs_wq, &net->charge_preallocation_work);
745 }
746 
747 /*
748  * Grab the operation ID from an incoming cache manager call.  The socket
749  * buffer is discarded on error or if we don't yet have sufficient data.
750  */
afs_deliver_cm_op_id(struct afs_call * call)751 static int afs_deliver_cm_op_id(struct afs_call *call)
752 {
753 	int ret;
754 
755 	_enter("{%zu}", iov_iter_count(call->iter));
756 
757 	/* the operation ID forms the first four bytes of the request data */
758 	ret = afs_extract_data(call, true);
759 	if (ret < 0)
760 		return ret;
761 
762 	call->operation_ID = ntohl(call->tmp);
763 	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
764 
765 	/* ask the cache manager to route the call (it'll change the call type
766 	 * if successful) */
767 	if (!afs_cm_incoming_call(call))
768 		return -ENOTSUPP;
769 
770 	trace_afs_cb_call(call);
771 
772 	/* pass responsibility for the remainer of this message off to the
773 	 * cache manager op */
774 	return call->type->deliver(call);
775 }
776 
777 /*
778  * Advance the AFS call state when an RxRPC service call ends the transmit
779  * phase.
780  */
afs_notify_end_reply_tx(struct sock * sock,struct rxrpc_call * rxcall,unsigned long call_user_ID)781 static void afs_notify_end_reply_tx(struct sock *sock,
782 				    struct rxrpc_call *rxcall,
783 				    unsigned long call_user_ID)
784 {
785 	struct afs_call *call = (struct afs_call *)call_user_ID;
786 
787 	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
788 }
789 
790 /*
791  * send an empty reply
792  */
afs_send_empty_reply(struct afs_call * call)793 void afs_send_empty_reply(struct afs_call *call)
794 {
795 	struct afs_net *net = call->net;
796 	struct msghdr msg;
797 
798 	_enter("");
799 
800 	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
801 
802 	msg.msg_name		= NULL;
803 	msg.msg_namelen		= 0;
804 	iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0);
805 	msg.msg_control		= NULL;
806 	msg.msg_controllen	= 0;
807 	msg.msg_flags		= 0;
808 
809 	switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
810 				       afs_notify_end_reply_tx)) {
811 	case 0:
812 		_leave(" [replied]");
813 		return;
814 
815 	case -ENOMEM:
816 		_debug("oom");
817 		rxrpc_kernel_abort_call(net->socket, call->rxcall,
818 					RXGEN_SS_MARSHAL, -ENOMEM,
819 					afs_abort_oom);
820 		fallthrough;
821 	default:
822 		_leave(" [error]");
823 		return;
824 	}
825 }
826 
827 /*
828  * send a simple reply
829  */
afs_send_simple_reply(struct afs_call * call,const void * buf,size_t len)830 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
831 {
832 	struct afs_net *net = call->net;
833 	struct msghdr msg;
834 	struct kvec iov[1];
835 	int n;
836 
837 	_enter("");
838 
839 	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
840 
841 	iov[0].iov_base		= (void *) buf;
842 	iov[0].iov_len		= len;
843 	msg.msg_name		= NULL;
844 	msg.msg_namelen		= 0;
845 	iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, len);
846 	msg.msg_control		= NULL;
847 	msg.msg_controllen	= 0;
848 	msg.msg_flags		= 0;
849 
850 	n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
851 				   afs_notify_end_reply_tx);
852 	if (n >= 0) {
853 		/* Success */
854 		_leave(" [replied]");
855 		return;
856 	}
857 
858 	if (n == -ENOMEM) {
859 		_debug("oom");
860 		rxrpc_kernel_abort_call(net->socket, call->rxcall,
861 					RXGEN_SS_MARSHAL, -ENOMEM,
862 					afs_abort_oom);
863 	}
864 	_leave(" [error]");
865 }
866 
867 /*
868  * Extract a piece of data from the received data socket buffers.
869  */
afs_extract_data(struct afs_call * call,bool want_more)870 int afs_extract_data(struct afs_call *call, bool want_more)
871 {
872 	struct afs_net *net = call->net;
873 	struct iov_iter *iter = call->iter;
874 	enum afs_call_state state;
875 	u32 remote_abort = 0;
876 	int ret;
877 
878 	_enter("{%s,%zu,%zu},%d",
879 	       call->type->name, call->iov_len, iov_iter_count(iter), want_more);
880 
881 	ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
882 				     &call->iov_len, want_more, &remote_abort,
883 				     &call->service_id);
884 	trace_afs_receive_data(call, call->iter, want_more, ret);
885 	if (ret == 0 || ret == -EAGAIN)
886 		return ret;
887 
888 	state = READ_ONCE(call->state);
889 	if (ret == 1) {
890 		switch (state) {
891 		case AFS_CALL_CL_AWAIT_REPLY:
892 			afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
893 			break;
894 		case AFS_CALL_SV_AWAIT_REQUEST:
895 			afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
896 			break;
897 		case AFS_CALL_COMPLETE:
898 			kdebug("prem complete %d", call->error);
899 			return afs_io_error(call, afs_io_error_extract);
900 		default:
901 			break;
902 		}
903 		return 0;
904 	}
905 
906 	afs_set_call_complete(call, ret, remote_abort);
907 	return ret;
908 }
909 
910 /*
911  * Log protocol error production.
912  */
afs_protocol_error(struct afs_call * call,enum afs_eproto_cause cause)913 noinline int afs_protocol_error(struct afs_call *call,
914 				enum afs_eproto_cause cause)
915 {
916 	trace_afs_protocol_error(call, cause);
917 	if (call)
918 		call->unmarshalling_error = true;
919 	return -EBADMSG;
920 }
921