Linux Audio

Check our new training course

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