xref: /openbmc/linux/fs/afs/rxrpc.c (revision 83268fa6)
1 /* Maintain an RxRPC server socket to do AFS communications through
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14 
15 #include <net/sock.h>
16 #include <net/af_rxrpc.h>
17 #include "internal.h"
18 #include "afs_cm.h"
19 #include "protocol_yfs.h"
20 
21 struct workqueue_struct *afs_async_calls;
22 
23 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
24 static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *);
25 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
26 static void afs_process_async_call(struct work_struct *);
27 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
28 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
29 static int afs_deliver_cm_op_id(struct afs_call *);
30 
31 /* asynchronous incoming call initial processing */
32 static const struct afs_call_type afs_RXCMxxxx = {
33 	.name		= "CB.xxxx",
34 	.deliver	= afs_deliver_cm_op_id,
35 };
36 
37 /*
38  * open an RxRPC socket and bind it to be a server for callback notifications
39  * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
40  */
41 int afs_open_socket(struct afs_net *net)
42 {
43 	struct sockaddr_rxrpc srx;
44 	struct socket *socket;
45 	unsigned int min_level;
46 	int ret;
47 
48 	_enter("");
49 
50 	ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
51 	if (ret < 0)
52 		goto error_1;
53 
54 	socket->sk->sk_allocation = GFP_NOFS;
55 
56 	/* bind the callback manager's address to make this a server socket */
57 	memset(&srx, 0, sizeof(srx));
58 	srx.srx_family			= AF_RXRPC;
59 	srx.srx_service			= CM_SERVICE;
60 	srx.transport_type		= SOCK_DGRAM;
61 	srx.transport_len		= sizeof(srx.transport.sin6);
62 	srx.transport.sin6.sin6_family	= AF_INET6;
63 	srx.transport.sin6.sin6_port	= htons(AFS_CM_PORT);
64 
65 	min_level = RXRPC_SECURITY_ENCRYPT;
66 	ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
67 				(void *)&min_level, sizeof(min_level));
68 	if (ret < 0)
69 		goto error_2;
70 
71 	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
72 	if (ret == -EADDRINUSE) {
73 		srx.transport.sin6.sin6_port = 0;
74 		ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
75 	}
76 	if (ret < 0)
77 		goto error_2;
78 
79 	srx.srx_service = YFS_CM_SERVICE;
80 	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
81 	if (ret < 0)
82 		goto error_2;
83 
84 	/* Ideally, we'd turn on service upgrade here, but we can't because
85 	 * OpenAFS is buggy and leaks the userStatus field from packet to
86 	 * packet and between FS packets and CB packets - so if we try to do an
87 	 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
88 	 * it sends back to us.
89 	 */
90 
91 	rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
92 					   afs_rx_discard_new_call);
93 
94 	ret = kernel_listen(socket, INT_MAX);
95 	if (ret < 0)
96 		goto error_2;
97 
98 	net->socket = socket;
99 	afs_charge_preallocation(&net->charge_preallocation_work);
100 	_leave(" = 0");
101 	return 0;
102 
103 error_2:
104 	sock_release(socket);
105 error_1:
106 	_leave(" = %d", ret);
107 	return ret;
108 }
109 
110 /*
111  * close the RxRPC socket AFS was using
112  */
113 void afs_close_socket(struct afs_net *net)
114 {
115 	_enter("");
116 
117 	kernel_listen(net->socket, 0);
118 	flush_workqueue(afs_async_calls);
119 
120 	if (net->spare_incoming_call) {
121 		afs_put_call(net->spare_incoming_call);
122 		net->spare_incoming_call = NULL;
123 	}
124 
125 	_debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
126 	wait_var_event(&net->nr_outstanding_calls,
127 		       !atomic_read(&net->nr_outstanding_calls));
128 	_debug("no outstanding calls");
129 
130 	kernel_sock_shutdown(net->socket, SHUT_RDWR);
131 	flush_workqueue(afs_async_calls);
132 	sock_release(net->socket);
133 
134 	_debug("dework");
135 	_leave("");
136 }
137 
138 /*
139  * Allocate a call.
140  */
141 static struct afs_call *afs_alloc_call(struct afs_net *net,
142 				       const struct afs_call_type *type,
143 				       gfp_t gfp)
144 {
145 	struct afs_call *call;
146 	int o;
147 
148 	call = kzalloc(sizeof(*call), gfp);
149 	if (!call)
150 		return NULL;
151 
152 	call->type = type;
153 	call->net = net;
154 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
155 	atomic_set(&call->usage, 1);
156 	INIT_WORK(&call->async_work, afs_process_async_call);
157 	init_waitqueue_head(&call->waitq);
158 	spin_lock_init(&call->state_lock);
159 	call->_iter = &call->iter;
160 
161 	o = atomic_inc_return(&net->nr_outstanding_calls);
162 	trace_afs_call(call, afs_call_trace_alloc, 1, o,
163 		       __builtin_return_address(0));
164 	return call;
165 }
166 
167 /*
168  * Dispose of a reference on a call.
169  */
170 void afs_put_call(struct afs_call *call)
171 {
172 	struct afs_net *net = call->net;
173 	int n = atomic_dec_return(&call->usage);
174 	int o = atomic_read(&net->nr_outstanding_calls);
175 
176 	trace_afs_call(call, afs_call_trace_put, n + 1, o,
177 		       __builtin_return_address(0));
178 
179 	ASSERTCMP(n, >=, 0);
180 	if (n == 0) {
181 		ASSERT(!work_pending(&call->async_work));
182 		ASSERT(call->type->name != NULL);
183 
184 		if (call->rxcall) {
185 			rxrpc_kernel_end_call(net->socket, call->rxcall);
186 			call->rxcall = NULL;
187 		}
188 		if (call->type->destructor)
189 			call->type->destructor(call);
190 
191 		afs_put_server(call->net, call->cm_server);
192 		afs_put_cb_interest(call->net, call->cbi);
193 		afs_put_addrlist(call->alist);
194 		kfree(call->request);
195 
196 		trace_afs_call(call, afs_call_trace_free, 0, o,
197 			       __builtin_return_address(0));
198 		kfree(call);
199 
200 		o = atomic_dec_return(&net->nr_outstanding_calls);
201 		if (o == 0)
202 			wake_up_var(&net->nr_outstanding_calls);
203 	}
204 }
205 
206 /*
207  * Queue the call for actual work.
208  */
209 static void afs_queue_call_work(struct afs_call *call)
210 {
211 	if (call->type->work) {
212 		int u = atomic_inc_return(&call->usage);
213 
214 		trace_afs_call(call, afs_call_trace_work, u,
215 			       atomic_read(&call->net->nr_outstanding_calls),
216 			       __builtin_return_address(0));
217 
218 		INIT_WORK(&call->work, call->type->work);
219 
220 		if (!queue_work(afs_wq, &call->work))
221 			afs_put_call(call);
222 	}
223 }
224 
225 /*
226  * allocate a call with flat request and reply buffers
227  */
228 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
229 				     const struct afs_call_type *type,
230 				     size_t request_size, size_t reply_max)
231 {
232 	struct afs_call *call;
233 
234 	call = afs_alloc_call(net, type, GFP_NOFS);
235 	if (!call)
236 		goto nomem_call;
237 
238 	if (request_size) {
239 		call->request_size = request_size;
240 		call->request = kmalloc(request_size, GFP_NOFS);
241 		if (!call->request)
242 			goto nomem_free;
243 	}
244 
245 	if (reply_max) {
246 		call->reply_max = reply_max;
247 		call->buffer = kmalloc(reply_max, GFP_NOFS);
248 		if (!call->buffer)
249 			goto nomem_free;
250 	}
251 
252 	afs_extract_to_buf(call, call->reply_max);
253 	call->operation_ID = type->op;
254 	init_waitqueue_head(&call->waitq);
255 	return call;
256 
257 nomem_free:
258 	afs_put_call(call);
259 nomem_call:
260 	return NULL;
261 }
262 
263 /*
264  * clean up a call with flat buffer
265  */
266 void afs_flat_call_destructor(struct afs_call *call)
267 {
268 	_enter("");
269 
270 	kfree(call->request);
271 	call->request = NULL;
272 	kfree(call->buffer);
273 	call->buffer = NULL;
274 }
275 
276 #define AFS_BVEC_MAX 8
277 
278 /*
279  * Load the given bvec with the next few pages.
280  */
281 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
282 			  struct bio_vec *bv, pgoff_t first, pgoff_t last,
283 			  unsigned offset)
284 {
285 	struct page *pages[AFS_BVEC_MAX];
286 	unsigned int nr, n, i, to, bytes = 0;
287 
288 	nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
289 	n = find_get_pages_contig(call->mapping, first, nr, pages);
290 	ASSERTCMP(n, ==, nr);
291 
292 	msg->msg_flags |= MSG_MORE;
293 	for (i = 0; i < nr; i++) {
294 		to = PAGE_SIZE;
295 		if (first + i >= last) {
296 			to = call->last_to;
297 			msg->msg_flags &= ~MSG_MORE;
298 		}
299 		bv[i].bv_page = pages[i];
300 		bv[i].bv_len = to - offset;
301 		bv[i].bv_offset = offset;
302 		bytes += to - offset;
303 		offset = 0;
304 	}
305 
306 	iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
307 }
308 
309 /*
310  * Advance the AFS call state when the RxRPC call ends the transmit phase.
311  */
312 static void afs_notify_end_request_tx(struct sock *sock,
313 				      struct rxrpc_call *rxcall,
314 				      unsigned long call_user_ID)
315 {
316 	struct afs_call *call = (struct afs_call *)call_user_ID;
317 
318 	afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
319 }
320 
321 /*
322  * attach the data from a bunch of pages on an inode to a call
323  */
324 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
325 {
326 	struct bio_vec bv[AFS_BVEC_MAX];
327 	unsigned int bytes, nr, loop, offset;
328 	pgoff_t first = call->first, last = call->last;
329 	int ret;
330 
331 	offset = call->first_offset;
332 	call->first_offset = 0;
333 
334 	do {
335 		afs_load_bvec(call, msg, bv, first, last, offset);
336 		trace_afs_send_pages(call, msg, first, last, offset);
337 
338 		offset = 0;
339 		bytes = msg->msg_iter.count;
340 		nr = msg->msg_iter.nr_segs;
341 
342 		ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
343 					     bytes, afs_notify_end_request_tx);
344 		for (loop = 0; loop < nr; loop++)
345 			put_page(bv[loop].bv_page);
346 		if (ret < 0)
347 			break;
348 
349 		first += nr;
350 	} while (first <= last);
351 
352 	trace_afs_sent_pages(call, call->first, last, first, ret);
353 	return ret;
354 }
355 
356 /*
357  * initiate a call
358  */
359 long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
360 		   gfp_t gfp, bool async)
361 {
362 	struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
363 	struct rxrpc_call *rxcall;
364 	struct msghdr msg;
365 	struct kvec iov[1];
366 	s64 tx_total_len;
367 	int ret;
368 
369 	_enter(",{%pISp},", &srx->transport);
370 
371 	ASSERT(call->type != NULL);
372 	ASSERT(call->type->name != NULL);
373 
374 	_debug("____MAKE %p{%s,%x} [%d]____",
375 	       call, call->type->name, key_serial(call->key),
376 	       atomic_read(&call->net->nr_outstanding_calls));
377 
378 	call->async = async;
379 	call->addr_ix = ac->index;
380 	call->alist = afs_get_addrlist(ac->alist);
381 
382 	/* Work out the length we're going to transmit.  This is awkward for
383 	 * calls such as FS.StoreData where there's an extra injection of data
384 	 * after the initial fixed part.
385 	 */
386 	tx_total_len = call->request_size;
387 	if (call->send_pages) {
388 		if (call->last == call->first) {
389 			tx_total_len += call->last_to - call->first_offset;
390 		} else {
391 			/* It looks mathematically like you should be able to
392 			 * combine the following lines with the ones above, but
393 			 * unsigned arithmetic is fun when it wraps...
394 			 */
395 			tx_total_len += PAGE_SIZE - call->first_offset;
396 			tx_total_len += call->last_to;
397 			tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
398 		}
399 	}
400 
401 	/* create a call */
402 	rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
403 					 (unsigned long)call,
404 					 tx_total_len, gfp,
405 					 (async ?
406 					  afs_wake_up_async_call :
407 					  afs_wake_up_call_waiter),
408 					 call->upgrade,
409 					 call->debug_id);
410 	if (IS_ERR(rxcall)) {
411 		ret = PTR_ERR(rxcall);
412 		call->error = ret;
413 		goto error_kill_call;
414 	}
415 
416 	call->rxcall = rxcall;
417 
418 	/* send the request */
419 	iov[0].iov_base	= call->request;
420 	iov[0].iov_len	= call->request_size;
421 
422 	msg.msg_name		= NULL;
423 	msg.msg_namelen		= 0;
424 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
425 	msg.msg_control		= NULL;
426 	msg.msg_controllen	= 0;
427 	msg.msg_flags		= MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
428 
429 	ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
430 				     &msg, call->request_size,
431 				     afs_notify_end_request_tx);
432 	if (ret < 0)
433 		goto error_do_abort;
434 
435 	if (call->send_pages) {
436 		ret = afs_send_pages(call, &msg);
437 		if (ret < 0)
438 			goto error_do_abort;
439 	}
440 
441 	/* at this point, an async call may no longer exist as it may have
442 	 * already completed */
443 	if (call->async)
444 		return -EINPROGRESS;
445 
446 	return afs_wait_for_call_to_complete(call, ac);
447 
448 error_do_abort:
449 	call->state = AFS_CALL_COMPLETE;
450 	if (ret != -ECONNABORTED) {
451 		rxrpc_kernel_abort_call(call->net->socket, rxcall,
452 					RX_USER_ABORT, ret, "KSD");
453 	} else {
454 		iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
455 		rxrpc_kernel_recv_data(call->net->socket, rxcall,
456 				       &msg.msg_iter, false,
457 				       &call->abort_code, &call->service_id);
458 		ac->abort_code = call->abort_code;
459 		ac->responded = true;
460 	}
461 	call->error = ret;
462 	trace_afs_call_done(call);
463 error_kill_call:
464 	if (call->type->done)
465 		call->type->done(call);
466 	afs_put_call(call);
467 	ac->error = ret;
468 	_leave(" = %d", ret);
469 	return ret;
470 }
471 
472 /*
473  * deliver messages to a call
474  */
475 static void afs_deliver_to_call(struct afs_call *call)
476 {
477 	enum afs_call_state state;
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 			iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
491 			ret = rxrpc_kernel_recv_data(call->net->socket,
492 						     call->rxcall, &call->iter,
493 						     false, &remote_abort,
494 						     &call->service_id);
495 			trace_afs_receive_data(call, &call->iter, false, ret);
496 
497 			if (ret == -EINPROGRESS || ret == -EAGAIN)
498 				return;
499 			if (ret < 0 || ret == 1) {
500 				if (ret == 1)
501 					ret = 0;
502 				goto call_complete;
503 			}
504 			return;
505 		}
506 
507 		if (call->want_reply_time &&
508 		    rxrpc_kernel_get_reply_time(call->net->socket,
509 						call->rxcall,
510 						&call->reply_time))
511 			call->want_reply_time = false;
512 
513 		ret = call->type->deliver(call);
514 		state = READ_ONCE(call->state);
515 		switch (ret) {
516 		case 0:
517 			afs_queue_call_work(call);
518 			if (state == AFS_CALL_CL_PROC_REPLY) {
519 				if (call->cbi)
520 					set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
521 						&call->cbi->server->flags);
522 				goto call_complete;
523 			}
524 			ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
525 			goto done;
526 		case -EINPROGRESS:
527 		case -EAGAIN:
528 			goto out;
529 		case -ECONNABORTED:
530 			ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
531 			goto done;
532 		case -ENOTSUPP:
533 			abort_code = RXGEN_OPCODE;
534 			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
535 						abort_code, ret, "KIV");
536 			goto local_abort;
537 		case -EIO:
538 			pr_err("kAFS: Call %u in bad state %u\n",
539 			       call->debug_id, state);
540 			/* Fall through */
541 		case -ENODATA:
542 		case -EBADMSG:
543 		case -EMSGSIZE:
544 		default:
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, "KUM");
550 			goto local_abort;
551 		}
552 	}
553 
554 done:
555 	if (call->type->done)
556 		call->type->done(call);
557 	if (state == AFS_CALL_COMPLETE && call->incoming)
558 		afs_put_call(call);
559 out:
560 	_leave("");
561 	return;
562 
563 local_abort:
564 	abort_code = 0;
565 call_complete:
566 	afs_set_call_complete(call, ret, remote_abort);
567 	state = AFS_CALL_COMPLETE;
568 	goto done;
569 }
570 
571 /*
572  * wait synchronously for a call to complete
573  */
574 static long afs_wait_for_call_to_complete(struct afs_call *call,
575 					  struct afs_addr_cursor *ac)
576 {
577 	signed long rtt2, timeout;
578 	long ret;
579 	u64 rtt;
580 	u32 life, last_life;
581 
582 	DECLARE_WAITQUEUE(myself, current);
583 
584 	_enter("");
585 
586 	rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
587 	rtt2 = nsecs_to_jiffies64(rtt) * 2;
588 	if (rtt2 < 2)
589 		rtt2 = 2;
590 
591 	timeout = rtt2;
592 	last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
593 
594 	add_wait_queue(&call->waitq, &myself);
595 	for (;;) {
596 		set_current_state(TASK_UNINTERRUPTIBLE);
597 
598 		/* deliver any messages that are in the queue */
599 		if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
600 		    call->need_attention) {
601 			call->need_attention = false;
602 			__set_current_state(TASK_RUNNING);
603 			afs_deliver_to_call(call);
604 			continue;
605 		}
606 
607 		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
608 			break;
609 
610 		life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
611 		if (timeout == 0 &&
612 		    life == last_life && signal_pending(current))
613 				break;
614 
615 		if (life != last_life) {
616 			timeout = rtt2;
617 			last_life = life;
618 		}
619 
620 		timeout = schedule_timeout(timeout);
621 	}
622 
623 	remove_wait_queue(&call->waitq, &myself);
624 	__set_current_state(TASK_RUNNING);
625 
626 	/* Kill off the call if it's still live. */
627 	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
628 		_debug("call interrupted");
629 		if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
630 					    RX_USER_ABORT, -EINTR, "KWI"))
631 			afs_set_call_complete(call, -EINTR, 0);
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 		if (call->ret_reply0) {
643 			ret = (long)call->reply[0];
644 			call->reply[0] = NULL;
645 		}
646 		/* Fall through */
647 	case -ECONNABORTED:
648 		ac->responded = true;
649 		break;
650 	}
651 
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 u;
678 
679 	trace_afs_notify_call(rxcall, call);
680 	call->need_attention = true;
681 
682 	u = atomic_fetch_add_unless(&call->usage, 1, 0);
683 	if (u != 0) {
684 		trace_afs_call(call, afs_call_trace_wake, u,
685 			       atomic_read(&call->net->nr_outstanding_calls),
686 			       __builtin_return_address(0));
687 
688 		if (!queue_work(afs_async_calls, &call->async_work))
689 			afs_put_call(call);
690 	}
691 }
692 
693 /*
694  * Delete an asynchronous call.  The work item carries a ref to the call struct
695  * that we need to release.
696  */
697 static void afs_delete_async_call(struct work_struct *work)
698 {
699 	struct afs_call *call = container_of(work, struct afs_call, async_work);
700 
701 	_enter("");
702 
703 	afs_put_call(call);
704 
705 	_leave("");
706 }
707 
708 /*
709  * Perform I/O processing on an asynchronous call.  The work item carries a ref
710  * to the call struct that we either need to release or to pass on.
711  */
712 static void afs_process_async_call(struct work_struct *work)
713 {
714 	struct afs_call *call = container_of(work, struct afs_call, async_work);
715 
716 	_enter("");
717 
718 	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
719 		call->need_attention = false;
720 		afs_deliver_to_call(call);
721 	}
722 
723 	if (call->state == AFS_CALL_COMPLETE) {
724 		/* We have two refs to release - one from the alloc and one
725 		 * queued with the work item - and we can't just deallocate the
726 		 * call because the work item may be queued again.
727 		 */
728 		call->async_work.func = afs_delete_async_call;
729 		if (!queue_work(afs_async_calls, &call->async_work))
730 			afs_put_call(call);
731 	}
732 
733 	afs_put_call(call);
734 	_leave("");
735 }
736 
737 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
738 {
739 	struct afs_call *call = (struct afs_call *)user_call_ID;
740 
741 	call->rxcall = rxcall;
742 }
743 
744 /*
745  * Charge the incoming call preallocation.
746  */
747 void afs_charge_preallocation(struct work_struct *work)
748 {
749 	struct afs_net *net =
750 		container_of(work, struct afs_net, charge_preallocation_work);
751 	struct afs_call *call = net->spare_incoming_call;
752 
753 	for (;;) {
754 		if (!call) {
755 			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
756 			if (!call)
757 				break;
758 
759 			call->async = true;
760 			call->state = AFS_CALL_SV_AWAIT_OP_ID;
761 			init_waitqueue_head(&call->waitq);
762 			afs_extract_to_tmp(call);
763 		}
764 
765 		if (rxrpc_kernel_charge_accept(net->socket,
766 					       afs_wake_up_async_call,
767 					       afs_rx_attach,
768 					       (unsigned long)call,
769 					       GFP_KERNEL,
770 					       call->debug_id) < 0)
771 			break;
772 		call = NULL;
773 	}
774 	net->spare_incoming_call = call;
775 }
776 
777 /*
778  * Discard a preallocated call when a socket is shut down.
779  */
780 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
781 				    unsigned long user_call_ID)
782 {
783 	struct afs_call *call = (struct afs_call *)user_call_ID;
784 
785 	call->rxcall = NULL;
786 	afs_put_call(call);
787 }
788 
789 /*
790  * Notification of an incoming call.
791  */
792 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
793 			    unsigned long user_call_ID)
794 {
795 	struct afs_net *net = afs_sock2net(sk);
796 
797 	queue_work(afs_wq, &net->charge_preallocation_work);
798 }
799 
800 /*
801  * Grab the operation ID from an incoming cache manager call.  The socket
802  * buffer is discarded on error or if we don't yet have sufficient data.
803  */
804 static int afs_deliver_cm_op_id(struct afs_call *call)
805 {
806 	int ret;
807 
808 	_enter("{%zu}", iov_iter_count(call->_iter));
809 
810 	/* the operation ID forms the first four bytes of the request data */
811 	ret = afs_extract_data(call, true);
812 	if (ret < 0)
813 		return ret;
814 
815 	call->operation_ID = ntohl(call->tmp);
816 	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
817 
818 	/* ask the cache manager to route the call (it'll change the call type
819 	 * if successful) */
820 	if (!afs_cm_incoming_call(call))
821 		return -ENOTSUPP;
822 
823 	trace_afs_cb_call(call);
824 
825 	/* pass responsibility for the remainer of this message off to the
826 	 * cache manager op */
827 	return call->type->deliver(call);
828 }
829 
830 /*
831  * Advance the AFS call state when an RxRPC service call ends the transmit
832  * phase.
833  */
834 static void afs_notify_end_reply_tx(struct sock *sock,
835 				    struct rxrpc_call *rxcall,
836 				    unsigned long call_user_ID)
837 {
838 	struct afs_call *call = (struct afs_call *)call_user_ID;
839 
840 	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
841 }
842 
843 /*
844  * send an empty reply
845  */
846 void afs_send_empty_reply(struct afs_call *call)
847 {
848 	struct afs_net *net = call->net;
849 	struct msghdr msg;
850 
851 	_enter("");
852 
853 	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
854 
855 	msg.msg_name		= NULL;
856 	msg.msg_namelen		= 0;
857 	iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
858 	msg.msg_control		= NULL;
859 	msg.msg_controllen	= 0;
860 	msg.msg_flags		= 0;
861 
862 	switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
863 				       afs_notify_end_reply_tx)) {
864 	case 0:
865 		_leave(" [replied]");
866 		return;
867 
868 	case -ENOMEM:
869 		_debug("oom");
870 		rxrpc_kernel_abort_call(net->socket, call->rxcall,
871 					RX_USER_ABORT, -ENOMEM, "KOO");
872 	default:
873 		_leave(" [error]");
874 		return;
875 	}
876 }
877 
878 /*
879  * send a simple reply
880  */
881 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
882 {
883 	struct afs_net *net = call->net;
884 	struct msghdr msg;
885 	struct kvec iov[1];
886 	int n;
887 
888 	_enter("");
889 
890 	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
891 
892 	iov[0].iov_base		= (void *) buf;
893 	iov[0].iov_len		= len;
894 	msg.msg_name		= NULL;
895 	msg.msg_namelen		= 0;
896 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
897 	msg.msg_control		= NULL;
898 	msg.msg_controllen	= 0;
899 	msg.msg_flags		= 0;
900 
901 	n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
902 				   afs_notify_end_reply_tx);
903 	if (n >= 0) {
904 		/* Success */
905 		_leave(" [replied]");
906 		return;
907 	}
908 
909 	if (n == -ENOMEM) {
910 		_debug("oom");
911 		rxrpc_kernel_abort_call(net->socket, call->rxcall,
912 					RX_USER_ABORT, -ENOMEM, "KOO");
913 	}
914 	_leave(" [error]");
915 }
916 
917 /*
918  * Extract a piece of data from the received data socket buffers.
919  */
920 int afs_extract_data(struct afs_call *call, bool want_more)
921 {
922 	struct afs_net *net = call->net;
923 	struct iov_iter *iter = call->_iter;
924 	enum afs_call_state state;
925 	u32 remote_abort = 0;
926 	int ret;
927 
928 	_enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
929 
930 	ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
931 				     want_more, &remote_abort,
932 				     &call->service_id);
933 	if (ret == 0 || ret == -EAGAIN)
934 		return ret;
935 
936 	state = READ_ONCE(call->state);
937 	if (ret == 1) {
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, int error,
962 				enum afs_eproto_cause cause)
963 {
964 	trace_afs_protocol_error(call, error, cause);
965 	return error;
966 }
967