rxrpc.c 24.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7
/* 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)
 */

8
#include <linux/slab.h>
9 10
#include <linux/sched/signal.h>

11 12 13 14
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include "internal.h"
#include "afs_cm.h"
15
#include "protocol_yfs.h"
16

17
struct workqueue_struct *afs_async_calls;
18

19 20
static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
21
static void afs_delete_async_call(struct work_struct *);
22
static void afs_process_async_call(struct work_struct *);
23 24
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);
25
static int afs_deliver_cm_op_id(struct afs_call *);
26 27 28

/* asynchronous incoming call initial processing */
static const struct afs_call_type afs_RXCMxxxx = {
D
David Howells 已提交
29
	.name		= "CB.xxxx",
30 31 32 33 34 35 36
	.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
 */
37
int afs_open_socket(struct afs_net *net)
38 39 40
{
	struct sockaddr_rxrpc srx;
	struct socket *socket;
41
	unsigned int min_level;
42 43 44 45
	int ret;

	_enter("");

46
	ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
47 48
	if (ret < 0)
		goto error_1;
49 50 51 52

	socket->sk->sk_allocation = GFP_NOFS;

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

61 62 63 64 65 66
	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;

67
	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
68 69 70 71
	if (ret == -EADDRINUSE) {
		srx.transport.sin6.sin6_port = 0;
		ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
	}
72 73 74
	if (ret < 0)
		goto error_2;

75 76 77 78 79
	srx.srx_service = YFS_CM_SERVICE;
	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
	if (ret < 0)
		goto error_2;

80 81 82 83 84 85
	/* 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.
	 */
86

87 88
	rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
					   afs_rx_discard_new_call);
89

90 91 92
	ret = kernel_listen(socket, INT_MAX);
	if (ret < 0)
		goto error_2;
93

94 95
	net->socket = socket;
	afs_charge_preallocation(&net->charge_preallocation_work);
96 97
	_leave(" = 0");
	return 0;
98 99 100 101 102 103

error_2:
	sock_release(socket);
error_1:
	_leave(" = %d", ret);
	return ret;
104 105 106 107 108
}

/*
 * close the RxRPC socket AFS was using
 */
109
void afs_close_socket(struct afs_net *net)
110 111 112
{
	_enter("");

113
	kernel_listen(net->socket, 0);
114 115
	flush_workqueue(afs_async_calls);

116 117 118
	if (net->spare_incoming_call) {
		afs_put_call(net->spare_incoming_call);
		net->spare_incoming_call = NULL;
119 120
	}

121
	_debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
122 123
	wait_var_event(&net->nr_outstanding_calls,
		       !atomic_read(&net->nr_outstanding_calls));
124 125
	_debug("no outstanding calls");

126
	kernel_sock_shutdown(net->socket, SHUT_RDWR);
127
	flush_workqueue(afs_async_calls);
128
	sock_release(net->socket);
129 130 131 132 133

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

D
David Howells 已提交
134
/*
135
 * Allocate a call.
D
David Howells 已提交
136
 */
137 138
static struct afs_call *afs_alloc_call(struct afs_net *net,
				       const struct afs_call_type *type,
139
				       gfp_t gfp)
D
David Howells 已提交
140
{
141 142
	struct afs_call *call;
	int o;
D
David Howells 已提交
143

144 145 146
	call = kzalloc(sizeof(*call), gfp);
	if (!call)
		return NULL;
D
David Howells 已提交
147

148
	call->type = type;
149
	call->net = net;
150
	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
151 152 153
	atomic_set(&call->usage, 1);
	INIT_WORK(&call->async_work, afs_process_async_call);
	init_waitqueue_head(&call->waitq);
154
	spin_lock_init(&call->state_lock);
155
	call->iter = &call->def_iter;
156

157
	o = atomic_inc_return(&net->nr_outstanding_calls);
158 159 160
	trace_afs_call(call, afs_call_trace_alloc, 1, o,
		       __builtin_return_address(0));
	return call;
D
David Howells 已提交
161 162
}

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

	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) {
181
			rxrpc_kernel_end_call(net->socket, call->rxcall);
182 183 184 185 186
			call->rxcall = NULL;
		}
		if (call->type->destructor)
			call->type->destructor(call);

D
David Howells 已提交
187
		afs_put_server(call->net, call->server, afs_server_trace_put_call);
188
		afs_put_cb_interest(call->net, call->cbi);
189
		afs_put_addrlist(call->alist);
190 191 192 193
		kfree(call->request);

		trace_afs_call(call, afs_call_trace_free, 0, o,
			       __builtin_return_address(0));
194 195 196
		kfree(call);

		o = atomic_dec_return(&net->nr_outstanding_calls);
197
		if (o == 0)
198
			wake_up_var(&net->nr_outstanding_calls);
199
	}
200 201
}

202 203 204 205 206 207 208 209 210 211 212
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;
}

213
/*
214
 * Queue the call for actual work.
215
 */
216
static void afs_queue_call_work(struct afs_call *call)
217
{
218 219
	if (call->type->work) {
		INIT_WORK(&call->work, call->type->work);
220

221
		afs_get_call(call, afs_call_trace_work);
222 223 224
		if (!queue_work(afs_wq, &call->work))
			afs_put_call(call);
	}
225 226
}

227 228 229
/*
 * allocate a call with flat request and reply buffers
 */
230 231
struct afs_call *afs_alloc_flat_call(struct afs_net *net,
				     const struct afs_call_type *type,
232
				     size_t request_size, size_t reply_max)
233 234 235
{
	struct afs_call *call;

236
	call = afs_alloc_call(net, type, GFP_NOFS);
237 238 239 240
	if (!call)
		goto nomem_call;

	if (request_size) {
241
		call->request_size = request_size;
242 243
		call->request = kmalloc(request_size, GFP_NOFS);
		if (!call->request)
D
David Howells 已提交
244
			goto nomem_free;
245 246
	}

247
	if (reply_max) {
248
		call->reply_max = reply_max;
249
		call->buffer = kmalloc(reply_max, GFP_NOFS);
250
		if (!call->buffer)
D
David Howells 已提交
251
			goto nomem_free;
252 253
	}

254
	afs_extract_to_buf(call, call->reply_max);
255
	call->operation_ID = type->op;
256 257 258
	init_waitqueue_head(&call->waitq);
	return call;

D
David Howells 已提交
259
nomem_free:
260
	afs_put_call(call);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
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;
}

278 279 280 281 282 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
#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;
	}

308
	iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
309 310
}

311 312 313 314 315 316 317 318 319
/*
 * 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;

320
	afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
321 322
}

323 324 325
/*
 * attach the data from a bunch of pages on an inode to a call
 */
A
Al Viro 已提交
326
static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
327
{
328 329
	struct bio_vec bv[AFS_BVEC_MAX];
	unsigned int bytes, nr, loop, offset;
330 331 332 333 334 335 336
	pgoff_t first = call->first, last = call->last;
	int ret;

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

	do {
337
		afs_load_bvec(call, msg, bv, first, last, offset);
D
David Howells 已提交
338 339
		trace_afs_send_pages(call, msg, first, last, offset);

340 341 342 343
		offset = 0;
		bytes = msg->msg_iter.count;
		nr = msg->msg_iter.nr_segs;

344
		ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
345
					     bytes, afs_notify_end_request_tx);
346 347
		for (loop = 0; loop < nr; loop++)
			put_page(bv[loop].bv_page);
348 349
		if (ret < 0)
			break;
350 351

		first += nr;
D
David Howells 已提交
352
	} while (first <= last);
353

D
David Howells 已提交
354
	trace_afs_sent_pages(call, call->first, last, first, ret);
355 356 357
	return ret;
}

358
/*
359 360
 * Initiate a call and synchronously queue up the parameters for dispatch.  Any
 * error is stored into the call struct, which the caller must check for.
361
 */
362
void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
363
{
364
	struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
365 366 367
	struct rxrpc_call *rxcall;
	struct msghdr msg;
	struct kvec iov[1];
368
	s64 tx_total_len;
369 370
	int ret;

371
	_enter(",{%pISp},", &srx->transport);
372

D
David Howells 已提交
373 374 375
	ASSERT(call->type != NULL);
	ASSERT(call->type->name != NULL);

376 377
	_debug("____MAKE %p{%s,%x} [%d]____",
	       call, call->type->name, key_serial(call->key),
378
	       atomic_read(&call->net->nr_outstanding_calls));
D
David Howells 已提交
379

380 381
	call->addr_ix = ac->index;
	call->alist = afs_get_addrlist(ac->alist);
382

383 384 385 386 387 388
	/* 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) {
389 390 391 392 393 394 395 396 397 398 399
		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;
		}
400 401
	}

402 403 404 405 406 407
	/* 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);

408
	/* create a call */
409
	rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
410 411
					 (unsigned long)call,
					 tx_total_len, gfp,
412
					 (call->async ?
D
David Howells 已提交
413
					  afs_wake_up_async_call :
414
					  afs_wake_up_call_waiter),
415
					 call->upgrade,
416 417
					 (call->intr ? RXRPC_PREINTERRUPTIBLE :
					  RXRPC_UNINTERRUPTIBLE),
418
					 call->debug_id);
419 420
	if (IS_ERR(rxcall)) {
		ret = PTR_ERR(rxcall);
421
		call->error = ret;
422 423 424 425 426
		goto error_kill_call;
	}

	call->rxcall = rxcall;

427 428 429 430
	if (call->max_lifespan)
		rxrpc_kernel_set_max_life(call->net->socket, rxcall,
					  call->max_lifespan);

431 432 433 434 435 436
	/* 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) {
A
Al Viro 已提交
449
		ret = afs_send_pages(call, &msg);
450 451 452 453
		if (ret < 0)
			goto error_do_abort;
	}

454 455
	/* Note that at this point, we may have received the reply or an abort
	 * - and an asynchronous call may already have completed.
456 457 458
	 *
	 * afs_wait_for_call_to_complete(call, ac)
	 * must be called to synchronously clean up.
459
	 */
460
	return;
461 462

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

	/* 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);
	}

494
	ac->error = ret;
495
	call->state = AFS_CALL_COMPLETE;
496 497 498 499 500 501 502 503
	_leave(" = %d", ret);
}

/*
 * deliver messages to a call
 */
static void afs_deliver_to_call(struct afs_call *call)
{
504 505
	enum afs_call_state state;
	u32 abort_code, remote_abort = 0;
506 507
	int ret;

508 509
	_enter("%s", call->type->name);

510 511 512 513 514
	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
515
	       ) {
516
		if (state == AFS_CALL_SV_AWAIT_ACK) {
517
			iov_iter_kvec(&call->def_iter, READ, NULL, 0, 0);
518
			ret = rxrpc_kernel_recv_data(call->net->socket,
519
						     call->rxcall, &call->def_iter,
520
						     false, &remote_abort,
521
						     &call->service_id);
522
			trace_afs_receive_data(call, &call->def_iter, false, ret);
D
David Howells 已提交
523

524 525
			if (ret == -EINPROGRESS || ret == -EAGAIN)
				return;
526 527 528
			if (ret < 0 || ret == 1) {
				if (ret == 1)
					ret = 0;
529
				goto call_complete;
530
			}
531
			return;
532 533
		}

D
David Howells 已提交
534
		if (!call->have_reply_time &&
535 536 537
		    rxrpc_kernel_get_reply_time(call->net->socket,
						call->rxcall,
						&call->reply_time))
D
David Howells 已提交
538
			call->have_reply_time = true;
539

540
		ret = call->type->deliver(call);
541
		state = READ_ONCE(call->state);
542 543
		switch (ret) {
		case 0:
544
			afs_queue_call_work(call);
545 546 547 548
			if (state == AFS_CALL_CL_PROC_REPLY) {
				if (call->cbi)
					set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
						&call->cbi->server->flags);
549
				goto call_complete;
550
			}
551
			ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
552 553 554 555
			goto done;
		case -EINPROGRESS:
		case -EAGAIN:
			goto out;
556
		case -ECONNABORTED:
557 558
			ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
			goto done;
559
		case -ENOTSUPP:
560
			abort_code = RXGEN_OPCODE;
561
			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
562
						abort_code, ret, "KIV");
563
			goto local_abort;
564 565 566 567
		case -EIO:
			pr_err("kAFS: Call %u in bad state %u\n",
			       call->debug_id, state);
			/* Fall through */
568 569 570 571
		case -ENODATA:
		case -EBADMSG:
		case -EMSGSIZE:
			abort_code = RXGEN_CC_UNMARSHAL;
572
			if (state != AFS_CALL_CL_AWAIT_REPLY)
573
				abort_code = RXGEN_SS_UNMARSHAL;
574
			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
575
						abort_code, ret, "KUM");
576
			goto local_abort;
577 578 579 580 581
		default:
			abort_code = RX_USER_ABORT;
			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
						abort_code, ret, "KER");
			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
 * Wait synchronously for a call to complete and clean up the call struct.
604
 */
605 606
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;
D
David Howells 已提交
610
	bool stalled = false;
611 612
	u64 rtt;
	u32 life, last_life;
613
	bool rxrpc_complete = false;
614 615 616 617 618

	DECLARE_WAITQUEUE(myself, current);

	_enter("");

619 620 621 622
	ret = call->error;
	if (ret < 0)
		goto out;

623
	rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
624 625 626 627 628
	rtt2 = nsecs_to_jiffies64(rtt) * 2;
	if (rtt2 < 2)
		rtt2 = 2;

	timeout = rtt2;
629
	rxrpc_kernel_check_life(call->net->socket, call->rxcall, &last_life);
630

631 632
	add_wait_queue(&call->waitq, &myself);
	for (;;) {
633
		set_current_state(TASK_UNINTERRUPTIBLE);
634 635

		/* deliver any messages that are in the queue */
636 637
		if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
		    call->need_attention) {
638
			call->need_attention = false;
639 640
			__set_current_state(TASK_RUNNING);
			afs_deliver_to_call(call);
D
David Howells 已提交
641
			timeout = rtt2;
642 643 644
			continue;
		}

645
		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
646
			break;
647

648 649 650 651 652 653
		if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall, &life)) {
			/* rxrpc terminated the call. */
			rxrpc_complete = true;
			break;
		}

654
		if (call->intr && timeout == 0 &&
D
David Howells 已提交
655 656
		    life == last_life && signal_pending(current)) {
			if (stalled)
657
				break;
D
David Howells 已提交
658 659 660 661 662 663
			__set_current_state(TASK_RUNNING);
			rxrpc_kernel_probe_life(call->net->socket, call->rxcall);
			timeout = rtt2;
			stalled = true;
			continue;
		}
664 665 666 667

		if (life != last_life) {
			timeout = rtt2;
			last_life = life;
D
David Howells 已提交
668
			stalled = false;
669 670 671
		}

		timeout = schedule_timeout(timeout);
672 673 674 675 676
	}

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

677
	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
678 679 680 681 682 683 684 685 686
		if (rxrpc_complete) {
			afs_set_call_complete(call, call->error, call->abort_code);
		} else {
			/* Kill off the call if it's still live. */
			_debug("call interrupted");
			if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
						    RX_USER_ABORT, -EINTR, "KWI"))
				afs_set_call_complete(call, -EINTR, 0);
		}
687 688
	}

689
	spin_lock_bh(&call->state_lock);
690 691
	ac->abort_code = call->abort_code;
	ac->error = call->error;
692
	spin_unlock_bh(&call->state_lock);
693 694 695 696

	ret = ac->error;
	switch (ret) {
	case 0:
697 698 699
		ret = call->ret0;
		call->ret0 = 0;

700 701 702 703
		/* Fall through */
	case -ECONNABORTED:
		ac->responded = true;
		break;
704 705
	}

706
out:
707
	_debug("call complete");
708
	afs_put_call(call);
709
	_leave(" = %p", (void *)ret);
710 711 712 713 714 715
	return ret;
}

/*
 * wake up a waiting call
 */
716 717
static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
				    unsigned long call_user_ID)
718
{
719 720 721
	struct afs_call *call = (struct afs_call *)call_user_ID;

	call->need_attention = true;
722 723 724 725 726 727
	wake_up(&call->waitq);
}

/*
 * wake up an asynchronous call
 */
728 729
static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
				   unsigned long call_user_ID)
730
{
731
	struct afs_call *call = (struct afs_call *)call_user_ID;
732
	int u;
733

D
David Howells 已提交
734
	trace_afs_notify_call(rxcall, call);
735
	call->need_attention = true;
736

737
	u = atomic_fetch_add_unless(&call->usage, 1, 0);
738 739
	if (u != 0) {
		trace_afs_call(call, afs_call_trace_wake, u,
740
			       atomic_read(&call->net->nr_outstanding_calls),
741 742 743 744 745
			       __builtin_return_address(0));

		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
	}
746 747 748
}

/*
749 750
 * Delete an asynchronous call.  The work item carries a ref to the call struct
 * that we need to release.
751
 */
752
static void afs_delete_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
	afs_put_call(call);
759 760 761 762 763

	_leave("");
}

/*
764 765
 * 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.
766
 */
767
static void afs_process_async_call(struct work_struct *work)
768
{
769 770
	struct afs_call *call = container_of(work, struct afs_call, async_work);

771 772
	_enter("");

773 774
	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
		call->need_attention = false;
775
		afs_deliver_to_call(call);
776
	}
777

D
David Howells 已提交
778
	if (call->state == AFS_CALL_COMPLETE) {
779 780 781 782
		/* 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.
		 */
783
		call->async_work.func = afs_delete_async_call;
784 785
		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
786 787
	}

788
	afs_put_call(call);
789 790 791
	_leave("");
}

792 793 794 795 796 797 798 799 800 801
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.
 */
802
void afs_charge_preallocation(struct work_struct *work)
803
{
804 805 806
	struct afs_net *net =
		container_of(work, struct afs_net, charge_preallocation_work);
	struct afs_call *call = net->spare_incoming_call;
807 808 809

	for (;;) {
		if (!call) {
810
			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
811 812 813
			if (!call)
				break;

D
David Howells 已提交
814
			call->async = true;
815
			call->state = AFS_CALL_SV_AWAIT_OP_ID;
D
David Howells 已提交
816
			init_waitqueue_head(&call->waitq);
817
			afs_extract_to_tmp(call);
818 819
		}

820
		if (rxrpc_kernel_charge_accept(net->socket,
821 822 823
					       afs_wake_up_async_call,
					       afs_rx_attach,
					       (unsigned long)call,
824 825
					       GFP_KERNEL,
					       call->debug_id) < 0)
826 827 828
			break;
		call = NULL;
	}
829
	net->spare_incoming_call = call;
830 831 832 833 834 835 836 837 838 839 840
}

/*
 * 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;
841
	afs_put_call(call);
842 843
}

844 845 846
/*
 * Notification of an incoming call.
 */
847 848
static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
			    unsigned long user_call_ID)
849
{
850 851 852
	struct afs_net *net = afs_sock2net(sk);

	queue_work(afs_wq, &net->charge_preallocation_work);
853 854
}

855
/*
856 857
 * 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.
858
 */
859
static int afs_deliver_cm_op_id(struct afs_call *call)
860
{
861
	int ret;
862

863
	_enter("{%zu}", iov_iter_count(call->iter));
864 865

	/* the operation ID forms the first four bytes of the request data */
866
	ret = afs_extract_data(call, true);
867 868
	if (ret < 0)
		return ret;
869

870
	call->operation_ID = ntohl(call->tmp);
871
	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
872 873 874 875 876 877

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

D
David Howells 已提交
878 879
	trace_afs_cb_call(call);

880 881
	/* pass responsibility for the remainer of this message off to the
	 * cache manager op */
882
	return call->type->deliver(call);
883 884
}

885 886 887 888 889 890 891 892 893 894
/*
 * 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;

895
	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
896 897
}

898 899 900 901 902
/*
 * send an empty reply
 */
void afs_send_empty_reply(struct afs_call *call)
{
903
	struct afs_net *net = call->net;
904 905 906 907
	struct msghdr msg;

	_enter("");

908
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
909

910 911
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
912
	iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
913 914 915 916
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

917
	switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
918
				       afs_notify_end_reply_tx)) {
919 920 921 922 923 924
	case 0:
		_leave(" [replied]");
		return;

	case -ENOMEM:
		_debug("oom");
925
		rxrpc_kernel_abort_call(net->socket, call->rxcall,
926
					RX_USER_ABORT, -ENOMEM, "KOO");
927
		/* Fall through */
928 929 930 931 932 933
	default:
		_leave(" [error]");
		return;
	}
}

934 935 936 937 938
/*
 * send a simple reply
 */
void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
{
939
	struct afs_net *net = call->net;
940
	struct msghdr msg;
941
	struct kvec iov[1];
942
	int n;
943 944 945

	_enter("");

946
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
947

948 949 950 951
	iov[0].iov_base		= (void *) buf;
	iov[0].iov_len		= len;
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
952
	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
953 954 955 956
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

957
	n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
958
				   afs_notify_end_reply_tx);
959
	if (n >= 0) {
960
		/* Success */
961 962
		_leave(" [replied]");
		return;
963
	}
964

965
	if (n == -ENOMEM) {
966
		_debug("oom");
967
		rxrpc_kernel_abort_call(net->socket, call->rxcall,
968
					RX_USER_ABORT, -ENOMEM, "KOO");
969
	}
970
	_leave(" [error]");
971 972
}

973
/*
974
 * Extract a piece of data from the received data socket buffers.
975
 */
976
int afs_extract_data(struct afs_call *call, bool want_more)
977
{
978
	struct afs_net *net = call->net;
979
	struct iov_iter *iter = call->iter;
980
	enum afs_call_state state;
981
	u32 remote_abort = 0;
982
	int ret;
983

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

986
	ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
987
				     want_more, &remote_abort,
988
				     &call->service_id);
989 990
	if (ret == 0 || ret == -EAGAIN)
		return ret;
991

992
	state = READ_ONCE(call->state);
993
	if (ret == 1) {
994 995 996
		switch (state) {
		case AFS_CALL_CL_AWAIT_REPLY:
			afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
997
			break;
998 999
		case AFS_CALL_SV_AWAIT_REQUEST:
			afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
1000
			break;
1001 1002
		case AFS_CALL_COMPLETE:
			kdebug("prem complete %d", call->error);
1003
			return afs_io_error(call, afs_io_error_extract);
1004 1005 1006 1007
		default:
			break;
		}
		return 0;
1008
	}
1009

1010
	afs_set_call_complete(call, ret, remote_abort);
1011
	return ret;
1012
}
D
David Howells 已提交
1013 1014 1015 1016

/*
 * Log protocol error production.
 */
1017 1018
noinline int afs_protocol_error(struct afs_call *call, int error,
				enum afs_eproto_cause cause)
D
David Howells 已提交
1019
{
1020
	trace_afs_protocol_error(call, error, cause);
D
David Howells 已提交
1021 1022
	return error;
}