rxrpc.c 24.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* Maintain an RxRPC server socket to do AFS communications through
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12
#include <linux/slab.h>
13 14
#include <linux/sched/signal.h>

15 16 17 18
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include "internal.h"
#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_delete_async_call(struct work_struct *);
27
static void afs_process_async_call(struct work_struct *);
28 29
static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
30
static int afs_deliver_cm_op_id(struct afs_call *);
31 32 33

/* asynchronous incoming call initial processing */
static const struct afs_call_type afs_RXCMxxxx = {
34
	.name		= "CB.xxxx",
35 36 37 38 39 40 41
	.deliver	= afs_deliver_cm_op_id,
};

/*
 * open an RxRPC socket and bind it to be a server for callback notifications
 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
 */
42
int afs_open_socket(struct afs_net *net)
43 44 45
{
	struct sockaddr_rxrpc srx;
	struct socket *socket;
46
	unsigned int min_level;
47 48 49 50
	int ret;

	_enter("");

51
	ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
52 53
	if (ret < 0)
		goto error_1;
54 55 56 57

	socket->sk->sk_allocation = GFP_NOFS;

	/* bind the callback manager's address to make this a server socket */
58
	memset(&srx, 0, sizeof(srx));
59 60 61
	srx.srx_family			= AF_RXRPC;
	srx.srx_service			= CM_SERVICE;
	srx.transport_type		= SOCK_DGRAM;
62 63 64
	srx.transport_len		= sizeof(srx.transport.sin6);
	srx.transport.sin6.sin6_family	= AF_INET6;
	srx.transport.sin6.sin6_port	= htons(AFS_CM_PORT);
65

66 67 68 69 70 71
	min_level = RXRPC_SECURITY_ENCRYPT;
	ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
				(void *)&min_level, sizeof(min_level));
	if (ret < 0)
		goto error_2;

72
	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
73 74 75 76
	if (ret == -EADDRINUSE) {
		srx.transport.sin6.sin6_port = 0;
		ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
	}
77 78 79
	if (ret < 0)
		goto error_2;

80 81 82 83 84
	srx.srx_service = YFS_CM_SERVICE;
	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
	if (ret < 0)
		goto error_2;

85 86 87 88 89 90
	/* Ideally, we'd turn on service upgrade here, but we can't because
	 * OpenAFS is buggy and leaks the userStatus field from packet to
	 * packet and between FS packets and CB packets - so if we try to do an
	 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
	 * it sends back to us.
	 */
91

92 93
	rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
					   afs_rx_discard_new_call);
94

95 96 97
	ret = kernel_listen(socket, INT_MAX);
	if (ret < 0)
		goto error_2;
98

99 100
	net->socket = socket;
	afs_charge_preallocation(&net->charge_preallocation_work);
101 102
	_leave(" = 0");
	return 0;
103 104 105 106 107 108

error_2:
	sock_release(socket);
error_1:
	_leave(" = %d", ret);
	return ret;
109 110 111 112 113
}

/*
 * close the RxRPC socket AFS was using
 */
114
void afs_close_socket(struct afs_net *net)
115 116 117
{
	_enter("");

118
	kernel_listen(net->socket, 0);
119 120
	flush_workqueue(afs_async_calls);

121 122 123
	if (net->spare_incoming_call) {
		afs_put_call(net->spare_incoming_call);
		net->spare_incoming_call = NULL;
124 125
	}

126
	_debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
127 128
	wait_var_event(&net->nr_outstanding_calls,
		       !atomic_read(&net->nr_outstanding_calls));
129 130
	_debug("no outstanding calls");

131
	kernel_sock_shutdown(net->socket, SHUT_RDWR);
132
	flush_workqueue(afs_async_calls);
133
	sock_release(net->socket);
134 135 136 137 138

	_debug("dework");
	_leave("");
}

139
/*
140
 * Allocate a call.
141
 */
142 143
static struct afs_call *afs_alloc_call(struct afs_net *net,
				       const struct afs_call_type *type,
144
				       gfp_t gfp)
145
{
146 147
	struct afs_call *call;
	int o;
148

149 150 151
	call = kzalloc(sizeof(*call), gfp);
	if (!call)
		return NULL;
152

153
	call->type = type;
154
	call->net = net;
155
	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
156 157 158
	atomic_set(&call->usage, 1);
	INIT_WORK(&call->async_work, afs_process_async_call);
	init_waitqueue_head(&call->waitq);
159
	spin_lock_init(&call->state_lock);
160
	call->_iter = &call->iter;
161

162
	o = atomic_inc_return(&net->nr_outstanding_calls);
163 164 165
	trace_afs_call(call, afs_call_trace_alloc, 1, o,
		       __builtin_return_address(0));
	return call;
166 167
}

168
/*
169
 * Dispose of a reference on a call.
170
 */
171
void afs_put_call(struct afs_call *call)
172
{
173
	struct afs_net *net = call->net;
174
	int n = atomic_dec_return(&call->usage);
175
	int o = atomic_read(&net->nr_outstanding_calls);
176 177 178 179 180 181 182 183 184 185

	trace_afs_call(call, afs_call_trace_put, n + 1, o,
		       __builtin_return_address(0));

	ASSERTCMP(n, >=, 0);
	if (n == 0) {
		ASSERT(!work_pending(&call->async_work));
		ASSERT(call->type->name != NULL);

		if (call->rxcall) {
186
			rxrpc_kernel_end_call(net->socket, call->rxcall);
187 188 189 190 191
			call->rxcall = NULL;
		}
		if (call->type->destructor)
			call->type->destructor(call);

192
		afs_put_server(call->net, call->cm_server);
193
		afs_put_cb_interest(call->net, call->cbi);
194
		afs_put_addrlist(call->alist);
195 196 197 198
		kfree(call->request);

		trace_afs_call(call, afs_call_trace_free, 0, o,
			       __builtin_return_address(0));
199 200 201
		kfree(call);

		o = atomic_dec_return(&net->nr_outstanding_calls);
202
		if (o == 0)
203
			wake_up_var(&net->nr_outstanding_calls);
204
	}
205 206
}

207 208 209 210 211 212 213 214 215 216 217
static struct afs_call *afs_get_call(struct afs_call *call,
				     enum afs_call_trace why)
{
	int u = atomic_inc_return(&call->usage);

	trace_afs_call(call, why, u,
		       atomic_read(&call->net->nr_outstanding_calls),
		       __builtin_return_address(0));
	return call;
}

218
/*
219
 * Queue the call for actual work.
220
 */
221
static void afs_queue_call_work(struct afs_call *call)
222
{
223 224
	if (call->type->work) {
		INIT_WORK(&call->work, call->type->work);
225

226
		afs_get_call(call, afs_call_trace_work);
227 228 229
		if (!queue_work(afs_wq, &call->work))
			afs_put_call(call);
	}
230 231
}

232 233 234
/*
 * allocate a call with flat request and reply buffers
 */
235 236
struct afs_call *afs_alloc_flat_call(struct afs_net *net,
				     const struct afs_call_type *type,
237
				     size_t request_size, size_t reply_max)
238 239 240
{
	struct afs_call *call;

241
	call = afs_alloc_call(net, type, GFP_NOFS);
242 243 244 245
	if (!call)
		goto nomem_call;

	if (request_size) {
246
		call->request_size = request_size;
247 248
		call->request = kmalloc(request_size, GFP_NOFS);
		if (!call->request)
249
			goto nomem_free;
250 251
	}

252
	if (reply_max) {
253
		call->reply_max = reply_max;
254
		call->buffer = kmalloc(reply_max, GFP_NOFS);
255
		if (!call->buffer)
256
			goto nomem_free;
257 258
	}

259
	afs_extract_to_buf(call, call->reply_max);
260
	call->operation_ID = type->op;
261 262 263
	init_waitqueue_head(&call->waitq);
	return call;

264
nomem_free:
265
	afs_put_call(call);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
nomem_call:
	return NULL;
}

/*
 * clean up a call with flat buffer
 */
void afs_flat_call_destructor(struct afs_call *call)
{
	_enter("");

	kfree(call->request);
	call->request = NULL;
	kfree(call->buffer);
	call->buffer = NULL;
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
#define AFS_BVEC_MAX 8

/*
 * Load the given bvec with the next few pages.
 */
static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
			  struct bio_vec *bv, pgoff_t first, pgoff_t last,
			  unsigned offset)
{
	struct page *pages[AFS_BVEC_MAX];
	unsigned int nr, n, i, to, bytes = 0;

	nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
	n = find_get_pages_contig(call->mapping, first, nr, pages);
	ASSERTCMP(n, ==, nr);

	msg->msg_flags |= MSG_MORE;
	for (i = 0; i < nr; i++) {
		to = PAGE_SIZE;
		if (first + i >= last) {
			to = call->last_to;
			msg->msg_flags &= ~MSG_MORE;
		}
		bv[i].bv_page = pages[i];
		bv[i].bv_len = to - offset;
		bv[i].bv_offset = offset;
		bytes += to - offset;
		offset = 0;
	}

313
	iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
314 315
}

316 317 318 319 320 321 322 323 324
/*
 * Advance the AFS call state when the RxRPC call ends the transmit phase.
 */
static void afs_notify_end_request_tx(struct sock *sock,
				      struct rxrpc_call *rxcall,
				      unsigned long call_user_ID)
{
	struct afs_call *call = (struct afs_call *)call_user_ID;

325
	afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
326 327
}

328 329 330
/*
 * attach the data from a bunch of pages on an inode to a call
 */
331
static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
332
{
333 334
	struct bio_vec bv[AFS_BVEC_MAX];
	unsigned int bytes, nr, loop, offset;
335 336 337 338 339 340 341
	pgoff_t first = call->first, last = call->last;
	int ret;

	offset = call->first_offset;
	call->first_offset = 0;

	do {
342
		afs_load_bvec(call, msg, bv, first, last, offset);
343 344
		trace_afs_send_pages(call, msg, first, last, offset);

345 346 347 348
		offset = 0;
		bytes = msg->msg_iter.count;
		nr = msg->msg_iter.nr_segs;

349
		ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
350
					     bytes, afs_notify_end_request_tx);
351 352
		for (loop = 0; loop < nr; loop++)
			put_page(bv[loop].bv_page);
353 354
		if (ret < 0)
			break;
355 356

		first += nr;
357
	} while (first <= last);
358

359
	trace_afs_sent_pages(call, call->first, last, first, ret);
360 361 362
	return ret;
}

363 364 365
/*
 * initiate a call
 */
366
long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
367
		   gfp_t gfp, bool async)
368
{
369
	struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
370 371 372
	struct rxrpc_call *rxcall;
	struct msghdr msg;
	struct kvec iov[1];
373
	s64 tx_total_len;
374 375
	int ret;

376
	_enter(",{%pISp},", &srx->transport);
377

378 379 380
	ASSERT(call->type != NULL);
	ASSERT(call->type->name != NULL);

381 382
	_debug("____MAKE %p{%s,%x} [%d]____",
	       call, call->type->name, key_serial(call->key),
383
	       atomic_read(&call->net->nr_outstanding_calls));
384

David Howells's avatar
David Howells committed
385
	call->async = async;
386 387
	call->addr_ix = ac->index;
	call->alist = afs_get_addrlist(ac->alist);
388

389 390 391 392 393 394
	/* Work out the length we're going to transmit.  This is awkward for
	 * calls such as FS.StoreData where there's an extra injection of data
	 * after the initial fixed part.
	 */
	tx_total_len = call->request_size;
	if (call->send_pages) {
395 396 397 398 399 400 401 402 403 404 405
		if (call->last == call->first) {
			tx_total_len += call->last_to - call->first_offset;
		} else {
			/* It looks mathematically like you should be able to
			 * combine the following lines with the ones above, but
			 * unsigned arithmetic is fun when it wraps...
			 */
			tx_total_len += PAGE_SIZE - call->first_offset;
			tx_total_len += call->last_to;
			tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
		}
406 407
	}

408 409 410 411 412 413
	/* If the call is going to be asynchronous, we need an extra ref for
	 * the call to hold itself so the caller need not hang on to its ref.
	 */
	if (call->async)
		afs_get_call(call, afs_call_trace_get);

414
	/* create a call */
415
	rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
416 417
					 (unsigned long)call,
					 tx_total_len, gfp,
David Howells's avatar
David Howells committed
418 419
					 (async ?
					  afs_wake_up_async_call :
420
					  afs_wake_up_call_waiter),
421 422
					 call->upgrade,
					 call->debug_id);
423 424
	if (IS_ERR(rxcall)) {
		ret = PTR_ERR(rxcall);
425
		call->error = ret;
426 427 428 429 430 431 432 433 434 435 436
		goto error_kill_call;
	}

	call->rxcall = rxcall;

	/* send the request */
	iov[0].iov_base	= call->request;
	iov[0].iov_len	= call->request_size;

	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
437
	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
438 439
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
440
	msg.msg_flags		= MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
441

442
	ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
443 444
				     &msg, call->request_size,
				     afs_notify_end_request_tx);
445 446 447
	if (ret < 0)
		goto error_do_abort;

448
	if (call->send_pages) {
449
		ret = afs_send_pages(call, &msg);
450 451 452 453
		if (ret < 0)
			goto error_do_abort;
	}

454 455 456 457 458
	/* Note that at this point, we may have received the reply or an abort
	 * - and an asynchronous call may already have completed.
	 */
	if (call->async) {
		afs_put_call(call);
David Howells's avatar
David Howells committed
459
		return -EINPROGRESS;
460
	}
David Howells's avatar
David Howells committed
461

462
	return afs_wait_for_call_to_complete(call, ac);
463 464

error_do_abort:
465
	if (ret != -ECONNABORTED) {
466 467
		rxrpc_kernel_abort_call(call->net->socket, rxcall,
					RX_USER_ABORT, ret, "KSD");
468
	} else {
469
		iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
470 471 472
		rxrpc_kernel_recv_data(call->net->socket, rxcall,
				       &msg.msg_iter, false,
				       &call->abort_code, &call->service_id);
473 474
		ac->abort_code = call->abort_code;
		ac->responded = true;
475
	}
476 477
	call->error = ret;
	trace_afs_call_done(call);
478
error_kill_call:
479 480
	if (call->type->done)
		call->type->done(call);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

	/* We need to dispose of the extra ref we grabbed for an async call.
	 * The call, however, might be queued on afs_async_calls and we need to
	 * make sure we don't get any more notifications that might requeue it.
	 */
	if (call->rxcall) {
		rxrpc_kernel_end_call(call->net->socket, call->rxcall);
		call->rxcall = NULL;
	}
	if (call->async) {
		if (cancel_work_sync(&call->async_work))
			afs_put_call(call);
		afs_put_call(call);
	}

496
	ac->error = ret;
497 498
	call->state = AFS_CALL_COMPLETE;
	afs_put_call(call);
499 500 501 502 503 504 505 506 507
	_leave(" = %d", ret);
	return ret;
}

/*
 * deliver messages to a call
 */
static void afs_deliver_to_call(struct afs_call *call)
{
508 509
	enum afs_call_state state;
	u32 abort_code, remote_abort = 0;
510 511
	int ret;

512 513
	_enter("%s", call->type->name);

514 515 516 517 518
	while (state = READ_ONCE(call->state),
	       state == AFS_CALL_CL_AWAIT_REPLY ||
	       state == AFS_CALL_SV_AWAIT_OP_ID ||
	       state == AFS_CALL_SV_AWAIT_REQUEST ||
	       state == AFS_CALL_SV_AWAIT_ACK
519
	       ) {
520
		if (state == AFS_CALL_SV_AWAIT_ACK) {
521
			iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
522
			ret = rxrpc_kernel_recv_data(call->net->socket,
523 524
						     call->rxcall, &call->iter,
						     false, &remote_abort,
525
						     &call->service_id);
526
			trace_afs_receive_data(call, &call->iter, false, ret);
527

528 529
			if (ret == -EINPROGRESS || ret == -EAGAIN)
				return;
530 531 532
			if (ret < 0 || ret == 1) {
				if (ret == 1)
					ret = 0;
533
				goto call_complete;
534
			}
535
			return;
536 537
		}

538 539 540 541 542 543
		if (call->want_reply_time &&
		    rxrpc_kernel_get_reply_time(call->net->socket,
						call->rxcall,
						&call->reply_time))
			call->want_reply_time = false;

544
		ret = call->type->deliver(call);
545
		state = READ_ONCE(call->state);
546 547
		switch (ret) {
		case 0:
548
			afs_queue_call_work(call);
549 550 551 552
			if (state == AFS_CALL_CL_PROC_REPLY) {
				if (call->cbi)
					set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
						&call->cbi->server->flags);
553
				goto call_complete;
554
			}
555
			ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
556 557 558 559
			goto done;
		case -EINPROGRESS:
		case -EAGAIN:
			goto out;
560
		case -ECONNABORTED:
561 562
			ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
			goto done;
563
		case -ENOTSUPP:
564
			abort_code = RXGEN_OPCODE;
565
			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
566
						abort_code, ret, "KIV");
567
			goto local_abort;
568 569 570 571
		case -EIO:
			pr_err("kAFS: Call %u in bad state %u\n",
			       call->debug_id, state);
			/* Fall through */
572 573 574 575 576
		case -ENODATA:
		case -EBADMSG:
		case -EMSGSIZE:
		default:
			abort_code = RXGEN_CC_UNMARSHAL;
577
			if (state != AFS_CALL_CL_AWAIT_REPLY)
578
				abort_code = RXGEN_SS_UNMARSHAL;
579
			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
580
						abort_code, ret, "KUM");
581
			goto local_abort;
582
		}
583 584
	}

585
done:
586 587
	if (call->type->done)
		call->type->done(call);
588
	if (state == AFS_CALL_COMPLETE && call->incoming)
589
		afs_put_call(call);
590
out:
591
	_leave("");
592 593
	return;

594 595
local_abort:
	abort_code = 0;
596
call_complete:
597 598
	afs_set_call_complete(call, ret, remote_abort);
	state = AFS_CALL_COMPLETE;
599
	goto done;
600 601 602 603 604
}

/*
 * wait synchronously for a call to complete
 */
605 606
static long afs_wait_for_call_to_complete(struct afs_call *call,
					  struct afs_addr_cursor *ac)
607
{
608
	signed long rtt2, timeout;
609
	long ret;
David Howells's avatar
David Howells committed
610
	bool stalled = false;
611 612
	u64 rtt;
	u32 life, last_life;
613 614 615 616 617

	DECLARE_WAITQUEUE(myself, current);

	_enter("");

618
	rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
619 620 621 622 623
	rtt2 = nsecs_to_jiffies64(rtt) * 2;
	if (rtt2 < 2)
		rtt2 = 2;

	timeout = rtt2;
624
	last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
625

626 627
	add_wait_queue(&call->waitq, &myself);
	for (;;) {
628
		set_current_state(TASK_UNINTERRUPTIBLE);
629 630

		/* deliver any messages that are in the queue */
631 632
		if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
		    call->need_attention) {
633
			call->need_attention = false;
634 635 636 637 638
			__set_current_state(TASK_RUNNING);
			afs_deliver_to_call(call);
			continue;
		}

639
		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
640
			break;
641

642
		life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
643
		if (timeout == 0 &&
David Howells's avatar
David Howells committed
644 645
		    life == last_life && signal_pending(current)) {
			if (stalled)
646
				break;
David Howells's avatar
David Howells committed
647 648 649 650 651 652
			__set_current_state(TASK_RUNNING);
			rxrpc_kernel_probe_life(call->net->socket, call->rxcall);
			timeout = rtt2;
			stalled = true;
			continue;
		}
653 654 655 656

		if (life != last_life) {
			timeout = rtt2;
			last_life = life;
David Howells's avatar
David Howells committed
657
			stalled = false;
658 659 660
		}

		timeout = schedule_timeout(timeout);
661 662 663 664 665
	}

	remove_wait_queue(&call->waitq, &myself);
	__set_current_state(TASK_RUNNING);

666
	/* Kill off the call if it's still live. */
667
	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
668
		_debug("call interrupted");
669
		if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
670 671
					    RX_USER_ABORT, -EINTR, "KWI"))
			afs_set_call_complete(call, -EINTR, 0);
672 673
	}

674
	spin_lock_bh(&call->state_lock);
675 676
	ac->abort_code = call->abort_code;
	ac->error = call->error;
677
	spin_unlock_bh(&call->state_lock);
678 679 680 681 682 683 684 685 686 687 688 689

	ret = ac->error;
	switch (ret) {
	case 0:
		if (call->ret_reply0) {
			ret = (long)call->reply[0];
			call->reply[0] = NULL;
		}
		/* Fall through */
	case -ECONNABORTED:
		ac->responded = true;
		break;
690 691
	}

692
	_debug("call complete");
693
	afs_put_call(call);
694
	_leave(" = %p", (void *)ret);
695 696 697 698 699 700
	return ret;
}

/*
 * wake up a waiting call
 */
701 702
static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
				    unsigned long call_user_ID)
703
{
704 705 706
	struct afs_call *call = (struct afs_call *)call_user_ID;

	call->need_attention = true;
707 708 709 710 711 712
	wake_up(&call->waitq);
}

/*
 * wake up an asynchronous call
 */
713 714
static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
				   unsigned long call_user_ID)
715
{
716
	struct afs_call *call = (struct afs_call *)call_user_ID;
717
	int u;
718

719
	trace_afs_notify_call(rxcall, call);
720
	call->need_attention = true;
721

722
	u = atomic_fetch_add_unless(&call->usage, 1, 0);
723 724
	if (u != 0) {
		trace_afs_call(call, afs_call_trace_wake, u,
725
			       atomic_read(&call->net->nr_outstanding_calls),
726 727 728 729 730
			       __builtin_return_address(0));

		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
	}
731 732 733
}

/*
734 735
 * Delete an asynchronous call.  The work item carries a ref to the call struct
 * that we need to release.
736
 */
737
static void afs_delete_async_call(struct work_struct *work)
738
{
739 740
	struct afs_call *call = container_of(work, struct afs_call, async_work);

741 742
	_enter("");

743
	afs_put_call(call);
744 745 746 747 748

	_leave("");
}

/*
749 750
 * Perform I/O processing on an asynchronous call.  The work item carries a ref
 * to the call struct that we either need to release or to pass on.
751
 */
752
static void afs_process_async_call(struct work_struct *work)
753
{
754 755
	struct afs_call *call = container_of(work, struct afs_call, async_work);

756 757
	_enter("");

758 759
	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
		call->need_attention = false;
760
		afs_deliver_to_call(call);
761
	}
762

David Howells's avatar
David Howells committed
763
	if (call->state == AFS_CALL_COMPLETE) {
764 765 766 767
		/* We have two refs to release - one from the alloc and one
		 * queued with the work item - and we can't just deallocate the
		 * call because the work item may be queued again.
		 */
768
		call->async_work.func = afs_delete_async_call;
769 770
		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
771 772
	}

773
	afs_put_call(call);
774 775 776
	_leave("");
}

777 778 779 780 781 782 783 784 785 786
static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
{
	struct afs_call *call = (struct afs_call *)user_call_ID;

	call->rxcall = rxcall;
}

/*
 * Charge the incoming call preallocation.
 */
787
void afs_charge_preallocation(struct work_struct *work)
788
{
789 790 791
	struct afs_net *net =
		container_of(work, struct afs_net, charge_preallocation_work);
	struct afs_call *call = net->spare_incoming_call;
792 793 794

	for (;;) {
		if (!call) {
795
			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
796 797 798
			if (!call)
				break;

David Howells's avatar
David Howells committed
799
			call->async = true;
800
			call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells's avatar
David Howells committed
801
			init_waitqueue_head(&call->waitq);
802
			afs_extract_to_tmp(call);
803 804
		}

805
		if (rxrpc_kernel_charge_accept(net->socket,
806 807 808
					       afs_wake_up_async_call,
					       afs_rx_attach,
					       (unsigned long)call,
809 810
					       GFP_KERNEL,
					       call->debug_id) < 0)
811 812 813
			break;
		call = NULL;
	}
814
	net->spare_incoming_call = call;
815 816 817 818 819 820 821 822 823 824 825
}

/*
 * Discard a preallocated call when a socket is shut down.
 */
static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
				    unsigned long user_call_ID)
{
	struct afs_call *call = (struct afs_call *)user_call_ID;

	call->rxcall = NULL;
826
	afs_put_call(call);
827 828
}

829 830 831
/*
 * Notification of an incoming call.
 */
832 833
static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
			    unsigned long user_call_ID)
834
{
835 836 837
	struct afs_net *net = afs_sock2net(sk);

	queue_work(afs_wq, &net->charge_preallocation_work);
838 839
}

840
/*
841 842
 * Grab the operation ID from an incoming cache manager call.  The socket
 * buffer is discarded on error or if we don't yet have sufficient data.
843
 */
844
static int afs_deliver_cm_op_id(struct afs_call *call)
845
{
846
	int ret;
847

848
	_enter("{%zu}", iov_iter_count(call->_iter));
849 850

	/* the operation ID forms the first four bytes of the request data */
851
	ret = afs_extract_data(call, true);
852 853
	if (ret < 0)
		return ret;
854

855
	call->operation_ID = ntohl(call->tmp);
856
	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
857 858 859 860 861 862

	/* ask the cache manager to route the call (it'll change the call type
	 * if successful) */
	if (!afs_cm_incoming_call(call))
		return -ENOTSUPP;

863 864
	trace_afs_cb_call(call);

865 866
	/* pass responsibility for the remainer of this message off to the
	 * cache manager op */
867
	return call->type->deliver(call);
868 869
}

870 871 872 873 874 875 876 877 878 879
/*
 * Advance the AFS call state when an RxRPC service call ends the transmit
 * phase.
 */
static void afs_notify_end_reply_tx(struct sock *sock,
				    struct rxrpc_call *rxcall,
				    unsigned long call_user_ID)
{
	struct afs_call *call = (struct afs_call *)call_user_ID;

880
	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
881 882
}

883 884 885 886 887
/*
 * send an empty reply
 */
void afs_send_empty_reply(struct afs_call *call)
{
888
	struct afs_net *net = call->net;
889 890 891 892
	struct msghdr msg;

	_enter("");

893
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
894

895 896
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
897
	iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
898 899 900 901
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

902
	switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
903
				       afs_notify_end_reply_tx)) {
904 905 906 907 908 909
	case 0:
		_leave(" [replied]");
		return;

	case -ENOMEM:
		_debug("oom");
910
		rxrpc_kernel_abort_call(net->socket, call->rxcall,
911
					RX_USER_ABORT, -ENOMEM, "KOO");
912 913 914 915 916 917
	default:
		_leave(" [error]");
		return;
	}
}

918 919 920 921 922
/*
 * send a simple reply
 */
void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
{
923
	struct afs_net *net = call->net;
924
	struct msghdr msg;
925
	struct kvec iov[1];
926
	int n;
927 928 929

	_enter("");

930
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
931

932 933 934 935
	iov[0].iov_base		= (void *) buf;
	iov[0].iov_len		= len;
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
936
	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
937 938 939 940
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

941
	n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
942
				   afs_notify_end_reply_tx);
943
	if (n >= 0) {
944
		/* Success */
945 946
		_leave(" [replied]");
		return;
947
	}
948

949
	if (n == -ENOMEM) {
950
		_debug("oom");
951
		rxrpc_kernel_abort_call(net->socket, call->rxcall,
952
					RX_USER_ABORT, -ENOMEM, "KOO");
953
	}
954
	_leave(" [error]");
955 956
}

957
/*
958
 * Extract a piece of data from the received data socket buffers.
959
 */
960
int afs_extract_data(struct afs_call *call, bool want_more)
961
{
962
	struct afs_net *net = call->net;
963
	struct iov_iter *iter = call->_iter;
964
	enum afs_call_state state;
965
	u32 remote_abort = 0;
966
	int ret;
967

968
	_enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
969

970
	ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
971
				     want_more, &remote_abort,
972
				     &call->service_id);
973 974
	if (ret == 0 || ret == -EAGAIN)
		return ret;
975

976
	state = READ_ONCE(call->state);
977
	if (ret == 1) {
978 979 980
		switch (state) {
		case AFS_CALL_CL_AWAIT_REPLY:
			afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
981
			break;
982 983
		case AFS_CALL_SV_AWAIT_REQUEST:
			afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
984
			break;
985 986
		case AFS_CALL_COMPLETE:
			kdebug("prem complete %d", call->error);
987
			return afs_io_error(call, afs_io_error_extract);
988 989 990 991
		default:
			break;
		}
		return 0;
992
	}
993

994
	afs_set_call_complete(call, ret, remote_abort);
995
	return ret;
996
}
997 998 999 1000

/*
 * Log protocol error production.
 */
1001 1002
noinline int afs_protocol_error(struct afs_call *call, int error,
				enum afs_eproto_cause cause)
1003
{
1004
	trace_afs_protocol_error(call, error, cause);
1005 1006
	return error;
}