rxrpc.c 25.0 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 24
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);
25
static void afs_delete_async_call(struct work_struct *);
26
static void afs_process_async_call(struct work_struct *);
27 28
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);
29
static int afs_deliver_cm_op_id(struct afs_call *);
30 31 32

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

	_enter("");

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

	socket->sk->sk_allocation = GFP_NOFS;

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

65 66 67 68 69 70
	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;

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

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

84 85 86 87 88 89
	/* 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.
	 */
90

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

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

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

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

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

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

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

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

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

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

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

148 149 150
	call = kzalloc(sizeof(*call), gfp);
	if (!call)
		return NULL;
D
David Howells 已提交
151

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

161
	o = atomic_inc_return(&net->nr_outstanding_calls);
162 163 164
	trace_afs_call(call, afs_call_trace_alloc, 1, o,
		       __builtin_return_address(0));
	return call;
D
David Howells 已提交
165 166
}

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

	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) {
185
			rxrpc_kernel_end_call(net->socket, call->rxcall);
186 187 188 189 190
			call->rxcall = NULL;
		}
		if (call->type->destructor)
			call->type->destructor(call);

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

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

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

206 207 208 209 210 211 212 213 214 215 216
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;
}

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

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

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

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

	if (request_size) {
245
		call->request_size = request_size;
246 247
		call->request = kmalloc(request_size, GFP_NOFS);
		if (!call->request)
D
David Howells 已提交
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)
D
David Howells 已提交
255
			goto nomem_free;
256 257
	}

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

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

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 308 309 310 311
#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;
	}

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

315 316 317 318 319 320 321 322 323
/*
 * 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;

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

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

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

	do {
341
		afs_load_bvec(call, msg, bv, first, last, offset);
D
David Howells 已提交
342 343
		trace_afs_send_pages(call, msg, first, last, offset);

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

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

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

D
David Howells 已提交
358
	trace_afs_sent_pages(call, call->first, last, first, ret);
359 360 361
	return ret;
}

362
/*
363 364
 * 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.
365
 */
366
void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
367
{
368
	struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
369 370 371
	struct rxrpc_call *rxcall;
	struct msghdr msg;
	struct kvec iov[1];
372
	s64 tx_total_len;
373 374
	int ret;

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

D
David Howells 已提交
377 378 379
	ASSERT(call->type != NULL);
	ASSERT(call->type->name != NULL);

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

384 385
	call->addr_ix = ac->index;
	call->alist = afs_get_addrlist(ac->alist);
386

387 388 389 390 391 392
	/* 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) {
393 394 395 396 397 398 399 400 401 402 403
		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;
		}
404 405
	}

406 407 408 409 410 411
	/* 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);

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

	call->rxcall = rxcall;

430 431 432 433
	if (call->max_lifespan)
		rxrpc_kernel_set_max_life(call->net->socket, rxcall,
					  call->max_lifespan);

434 435 436 437 438 439
	/* send the request */
	iov[0].iov_base	= call->request;
	iov[0].iov_len	= call->request_size;

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

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

451
	if (call->send_pages) {
A
Al Viro 已提交
452
		ret = afs_send_pages(call, &msg);
453 454 455 456
		if (ret < 0)
			goto error_do_abort;
	}

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

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

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

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

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

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

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

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

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

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

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

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

/*
606
 * Wait synchronously for a call to complete and clean up the call struct.
607
 */
608 609
long afs_wait_for_call_to_complete(struct afs_call *call,
				   struct afs_addr_cursor *ac)
610
{
611
	signed long rtt2, timeout;
612
	long ret;
D
David Howells 已提交
613
	bool stalled = false;
614 615
	u64 rtt;
	u32 life, last_life;
616
	bool rxrpc_complete = false;
617 618 619 620 621

	DECLARE_WAITQUEUE(myself, current);

	_enter("");

622 623 624 625
	ret = call->error;
	if (ret < 0)
		goto out;

626
	rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
627 628 629 630 631
	rtt2 = nsecs_to_jiffies64(rtt) * 2;
	if (rtt2 < 2)
		rtt2 = 2;

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

634 635
	add_wait_queue(&call->waitq, &myself);
	for (;;) {
636
		set_current_state(TASK_UNINTERRUPTIBLE);
637 638

		/* deliver any messages that are in the queue */
639 640
		if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
		    call->need_attention) {
641
			call->need_attention = false;
642 643 644 645 646
			__set_current_state(TASK_RUNNING);
			afs_deliver_to_call(call);
			continue;
		}

647
		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
648
			break;
649

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

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

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

		timeout = schedule_timeout(timeout);
674 675 676 677 678
	}

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

679
	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
680 681 682 683 684 685 686 687 688
		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);
		}
689 690
	}

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

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

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

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

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

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

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

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

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

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

/*
751 752
 * Delete an asynchronous call.  The work item carries a ref to the call struct
 * that we need to release.
753
 */
754
static void afs_delete_async_call(struct work_struct *work)
755
{
756 757
	struct afs_call *call = container_of(work, struct afs_call, async_work);

758 759
	_enter("");

760
	afs_put_call(call);
761 762 763 764 765

	_leave("");
}

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

773 774
	_enter("");

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

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

790
	afs_put_call(call);
791 792 793
	_leave("");
}

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

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

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

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

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

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

	queue_work(afs_wq, &net->charge_preallocation_work);
855 856
}

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

865
	_enter("{%zu}", iov_iter_count(call->_iter));
866 867

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

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

	/* 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 已提交
880 881
	trace_afs_cb_call(call);

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

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

897
	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
898 899
}

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

	_enter("");

910
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
911

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

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

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

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

	_enter("");

948
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
949

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

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

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

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

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

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

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

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

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