rxrpc.c 22.9 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 26
static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
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
	u16 service_upgrade[2];
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 85 86 87 88 89 90 91 92
	srx.srx_service = YFS_CM_SERVICE;
	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
	if (ret < 0)
		goto error_2;

	service_upgrade[0] = CM_SERVICE;
	service_upgrade[1] = YFS_CM_SERVICE;
	ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_UPGRADEABLE_SERVICE,
				(void *)service_upgrade, sizeof(service_upgrade));
	if (ret < 0)
		goto error_2;


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

193
		afs_put_server(call->net, call->cm_server);
194
		afs_put_cb_interest(call->net, call->cbi);
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
 * Queue the call for actual work.  Returns 0 unconditionally for convenience.
209
 */
210
int afs_queue_call_work(struct afs_call *call)
211
{
212 213 214
	int u = atomic_inc_return(&call->usage);

	trace_afs_call(call, afs_call_trace_work, u,
215
		       atomic_read(&call->net->nr_outstanding_calls),
216 217 218 219 220 221 222
		       __builtin_return_address(0));

	INIT_WORK(&call->work, call->type->work);

	if (!queue_work(afs_wq, &call->work))
		afs_put_call(call);
	return 0;
223 224
}

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

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

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

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

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

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

276 277 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
#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;
	}

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

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

318
	afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
319 320
}

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

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

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

338 339 340 341
		offset = 0;
		bytes = msg->msg_iter.count;
		nr = msg->msg_iter.nr_segs;

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

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

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

356 357 358
/*
 * initiate a call
 */
D
David Howells 已提交
359
long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
360
		   gfp_t gfp, bool async)
361
{
D
David Howells 已提交
362
	struct sockaddr_rxrpc *srx = ac->addr;
363 364 365
	struct rxrpc_call *rxcall;
	struct msghdr msg;
	struct kvec iov[1];
366
	s64 tx_total_len;
367 368
	int ret;

369
	_enter(",{%pISp},", &srx->transport);
370

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

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

D
David Howells 已提交
378
	call->async = async;
379

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

399
	/* create a call */
400
	rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
401 402
					 (unsigned long)call,
					 tx_total_len, gfp,
D
David Howells 已提交
403 404
					 (async ?
					  afs_wake_up_async_call :
405
					  afs_wake_up_call_waiter),
406 407
					 call->upgrade,
					 call->debug_id);
408 409 410 411 412 413 414 415 416 417 418 419 420
	if (IS_ERR(rxcall)) {
		ret = PTR_ERR(rxcall);
		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;
421
	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
422 423
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
424
	msg.msg_flags		= MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
425

426
	ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
427 428
				     &msg, call->request_size,
				     afs_notify_end_request_tx);
429 430 431
	if (ret < 0)
		goto error_do_abort;

432
	if (call->send_pages) {
A
Al Viro 已提交
433
		ret = afs_send_pages(call, &msg);
434 435 436 437
		if (ret < 0)
			goto error_do_abort;
	}

438 439
	/* at this point, an async call may no longer exist as it may have
	 * already completed */
D
David Howells 已提交
440 441 442
	if (call->async)
		return -EINPROGRESS;

443
	return afs_wait_for_call_to_complete(call, ac);
444 445

error_do_abort:
446 447
	call->state = AFS_CALL_COMPLETE;
	if (ret != -ECONNABORTED) {
448 449
		rxrpc_kernel_abort_call(call->net->socket, rxcall,
					RX_USER_ABORT, ret, "KSD");
450
	} else {
451
		iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
452 453 454
		rxrpc_kernel_recv_data(call->net->socket, rxcall,
				       &msg.msg_iter, false,
				       &call->abort_code, &call->service_id);
455 456
		ac->abort_code = call->abort_code;
		ac->responded = true;
457
	}
458 459
	call->error = ret;
	trace_afs_call_done(call);
460
error_kill_call:
461
	afs_put_call(call);
462
	ac->error = ret;
463 464 465 466 467 468 469 470 471
	_leave(" = %d", ret);
	return ret;
}

/*
 * deliver messages to a call
 */
static void afs_deliver_to_call(struct afs_call *call)
{
472 473
	enum afs_call_state state;
	u32 abort_code, remote_abort = 0;
474 475
	int ret;

476 477
	_enter("%s", call->type->name);

478 479 480 481 482
	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
483
	       ) {
484
		if (state == AFS_CALL_SV_AWAIT_ACK) {
485
			iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
486
			ret = rxrpc_kernel_recv_data(call->net->socket,
487 488
						     call->rxcall, &call->iter,
						     false, &remote_abort,
489
						     &call->service_id);
490
			trace_afs_receive_data(call, &call->iter, false, ret);
D
David Howells 已提交
491

492 493
			if (ret == -EINPROGRESS || ret == -EAGAIN)
				return;
494 495 496
			if (ret < 0 || ret == 1) {
				if (ret == 1)
					ret = 0;
497
				goto call_complete;
498
			}
499
			return;
500 501
		}

502
		ret = call->type->deliver(call);
503
		state = READ_ONCE(call->state);
504 505
		switch (ret) {
		case 0:
506 507 508 509
			if (state == AFS_CALL_CL_PROC_REPLY) {
				if (call->cbi)
					set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
						&call->cbi->server->flags);
510
				goto call_complete;
511
			}
512
			ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
513 514 515 516
			goto done;
		case -EINPROGRESS:
		case -EAGAIN:
			goto out;
517
		case -ECONNABORTED:
518 519
			ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
			goto done;
520
		case -ENOTSUPP:
521
			abort_code = RXGEN_OPCODE;
522
			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
523
						abort_code, ret, "KIV");
524
			goto local_abort;
525 526 527 528
		case -EIO:
			pr_err("kAFS: Call %u in bad state %u\n",
			       call->debug_id, state);
			/* Fall through */
529 530 531 532 533
		case -ENODATA:
		case -EBADMSG:
		case -EMSGSIZE:
		default:
			abort_code = RXGEN_CC_UNMARSHAL;
534
			if (state != AFS_CALL_CL_AWAIT_REPLY)
535
				abort_code = RXGEN_SS_UNMARSHAL;
536
			rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
537
						abort_code, ret, "KUM");
538
			goto local_abort;
539
		}
540 541
	}

542
done:
543
	if (state == AFS_CALL_COMPLETE && call->incoming)
544
		afs_put_call(call);
545
out:
546
	_leave("");
547 548
	return;

549 550
local_abort:
	abort_code = 0;
551
call_complete:
552 553
	afs_set_call_complete(call, ret, remote_abort);
	state = AFS_CALL_COMPLETE;
554
	goto done;
555 556 557 558 559
}

/*
 * wait synchronously for a call to complete
 */
560 561
static long afs_wait_for_call_to_complete(struct afs_call *call,
					  struct afs_addr_cursor *ac)
562
{
563
	signed long rtt2, timeout;
564
	long ret;
565 566
	u64 rtt;
	u32 life, last_life;
567 568 569 570 571

	DECLARE_WAITQUEUE(myself, current);

	_enter("");

572
	rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
573 574 575 576 577
	rtt2 = nsecs_to_jiffies64(rtt) * 2;
	if (rtt2 < 2)
		rtt2 = 2;

	timeout = rtt2;
578
	last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
579

580 581
	add_wait_queue(&call->waitq, &myself);
	for (;;) {
582
		set_current_state(TASK_UNINTERRUPTIBLE);
583 584

		/* deliver any messages that are in the queue */
585 586
		if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
		    call->need_attention) {
587
			call->need_attention = false;
588 589 590 591 592
			__set_current_state(TASK_RUNNING);
			afs_deliver_to_call(call);
			continue;
		}

593
		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
594
			break;
595

596
		life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
597 598 599 600 601 602 603 604 605 606
		if (timeout == 0 &&
		    life == last_life && signal_pending(current))
				break;

		if (life != last_life) {
			timeout = rtt2;
			last_life = life;
		}

		timeout = schedule_timeout(timeout);
607 608 609 610 611
	}

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

612
	/* Kill off the call if it's still live. */
613
	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
614
		_debug("call interrupted");
615
		if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
616 617
					    RX_USER_ABORT, -EINTR, "KWI"))
			afs_set_call_complete(call, -EINTR, 0);
618 619
	}

620
	spin_lock_bh(&call->state_lock);
621 622
	ac->abort_code = call->abort_code;
	ac->error = call->error;
623
	spin_unlock_bh(&call->state_lock);
624 625 626 627 628 629 630 631 632 633 634 635

	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;
636 637
	}

638
	_debug("call complete");
639
	afs_put_call(call);
640
	_leave(" = %p", (void *)ret);
641 642 643 644 645 646
	return ret;
}

/*
 * wake up a waiting call
 */
647 648
static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
				    unsigned long call_user_ID)
649
{
650 651 652
	struct afs_call *call = (struct afs_call *)call_user_ID;

	call->need_attention = true;
653 654 655 656 657 658
	wake_up(&call->waitq);
}

/*
 * wake up an asynchronous call
 */
659 660
static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
				   unsigned long call_user_ID)
661
{
662
	struct afs_call *call = (struct afs_call *)call_user_ID;
663
	int u;
664

D
David Howells 已提交
665
	trace_afs_notify_call(rxcall, call);
666
	call->need_attention = true;
667

668
	u = atomic_fetch_add_unless(&call->usage, 1, 0);
669 670
	if (u != 0) {
		trace_afs_call(call, afs_call_trace_wake, u,
671
			       atomic_read(&call->net->nr_outstanding_calls),
672 673 674 675 676
			       __builtin_return_address(0));

		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
	}
677 678 679
}

/*
680 681
 * Delete an asynchronous call.  The work item carries a ref to the call struct
 * that we need to release.
682
 */
683
static void afs_delete_async_call(struct work_struct *work)
684
{
685 686
	struct afs_call *call = container_of(work, struct afs_call, async_work);

687 688
	_enter("");

689
	afs_put_call(call);
690 691 692 693 694

	_leave("");
}

/*
695 696
 * 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.
697
 */
698
static void afs_process_async_call(struct work_struct *work)
699
{
700 701
	struct afs_call *call = container_of(work, struct afs_call, async_work);

702 703
	_enter("");

704 705
	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
		call->need_attention = false;
706
		afs_deliver_to_call(call);
707
	}
708

D
David Howells 已提交
709
	if (call->state == AFS_CALL_COMPLETE) {
710 711 712 713
		/* 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.
		 */
714
		call->async_work.func = afs_delete_async_call;
715 716
		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
717 718
	}

719
	afs_put_call(call);
720 721 722
	_leave("");
}

723 724 725 726 727 728 729 730 731 732
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.
 */
733
void afs_charge_preallocation(struct work_struct *work)
734
{
735 736 737
	struct afs_net *net =
		container_of(work, struct afs_net, charge_preallocation_work);
	struct afs_call *call = net->spare_incoming_call;
738 739 740

	for (;;) {
		if (!call) {
741
			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
742 743 744
			if (!call)
				break;

D
David Howells 已提交
745
			call->async = true;
746
			call->state = AFS_CALL_SV_AWAIT_OP_ID;
D
David Howells 已提交
747
			init_waitqueue_head(&call->waitq);
748
			afs_extract_to_tmp(call);
749 750
		}

751
		if (rxrpc_kernel_charge_accept(net->socket,
752 753 754
					       afs_wake_up_async_call,
					       afs_rx_attach,
					       (unsigned long)call,
755 756
					       GFP_KERNEL,
					       call->debug_id) < 0)
757 758 759
			break;
		call = NULL;
	}
760
	net->spare_incoming_call = call;
761 762 763 764 765 766 767 768 769 770 771
}

/*
 * 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;
772
	afs_put_call(call);
773 774
}

775 776 777
/*
 * Notification of an incoming call.
 */
778 779
static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
			    unsigned long user_call_ID)
780
{
781 782 783
	struct afs_net *net = afs_sock2net(sk);

	queue_work(afs_wq, &net->charge_preallocation_work);
784 785
}

786
/*
787 788
 * 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.
789
 */
790
static int afs_deliver_cm_op_id(struct afs_call *call)
791
{
792
	int ret;
793

794
	_enter("{%zu}", iov_iter_count(call->_iter));
795 796

	/* the operation ID forms the first four bytes of the request data */
797
	ret = afs_extract_data(call, true);
798 799
	if (ret < 0)
		return ret;
800

801
	call->operation_ID = ntohl(call->tmp);
802
	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
803 804 805 806 807 808

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

811 812
	/* pass responsibility for the remainer of this message off to the
	 * cache manager op */
813
	return call->type->deliver(call);
814 815
}

816 817 818 819 820 821 822 823 824 825
/*
 * 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;

826
	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
827 828
}

829 830 831 832 833
/*
 * send an empty reply
 */
void afs_send_empty_reply(struct afs_call *call)
{
834
	struct afs_net *net = call->net;
835 836 837 838
	struct msghdr msg;

	_enter("");

839
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
840

841 842
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
843
	iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
844 845 846 847
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

848
	switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
849
				       afs_notify_end_reply_tx)) {
850 851 852 853 854 855
	case 0:
		_leave(" [replied]");
		return;

	case -ENOMEM:
		_debug("oom");
856
		rxrpc_kernel_abort_call(net->socket, call->rxcall,
857
					RX_USER_ABORT, -ENOMEM, "KOO");
858 859 860 861 862 863
	default:
		_leave(" [error]");
		return;
	}
}

864 865 866 867 868
/*
 * send a simple reply
 */
void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
{
869
	struct afs_net *net = call->net;
870
	struct msghdr msg;
871
	struct kvec iov[1];
872
	int n;
873 874 875

	_enter("");

876
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
877

878 879 880 881
	iov[0].iov_base		= (void *) buf;
	iov[0].iov_len		= len;
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
882
	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
883 884 885 886
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

887
	n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
888
				   afs_notify_end_reply_tx);
889
	if (n >= 0) {
890
		/* Success */
891 892
		_leave(" [replied]");
		return;
893
	}
894

895
	if (n == -ENOMEM) {
896
		_debug("oom");
897
		rxrpc_kernel_abort_call(net->socket, call->rxcall,
898
					RX_USER_ABORT, -ENOMEM, "KOO");
899
	}
900
	_leave(" [error]");
901 902
}

903
/*
904
 * Extract a piece of data from the received data socket buffers.
905
 */
906
int afs_extract_data(struct afs_call *call, bool want_more)
907
{
908
	struct afs_net *net = call->net;
909
	struct iov_iter *iter = call->_iter;
910
	enum afs_call_state state;
911
	u32 remote_abort = 0;
912
	int ret;
913

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

916
	ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
917
				     want_more, &remote_abort,
918
				     &call->service_id);
919 920
	if (ret == 0 || ret == -EAGAIN)
		return ret;
921

922
	state = READ_ONCE(call->state);
923
	if (ret == 1) {
924 925 926
		switch (state) {
		case AFS_CALL_CL_AWAIT_REPLY:
			afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
927
			break;
928 929
		case AFS_CALL_SV_AWAIT_REQUEST:
			afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
930
			break;
931 932
		case AFS_CALL_COMPLETE:
			kdebug("prem complete %d", call->error);
933
			return afs_io_error(call, afs_io_error_extract);
934 935 936 937
		default:
			break;
		}
		return 0;
938
	}
939

940
	afs_set_call_complete(call, ret, remote_abort);
941
	return ret;
942
}
D
David Howells 已提交
943 944 945 946

/*
 * Log protocol error production.
 */
947 948
noinline int afs_protocol_error(struct afs_call *call, int error,
				enum afs_eproto_cause cause)
D
David Howells 已提交
949
{
950
	trace_afs_protocol_error(call, error, cause);
D
David Howells 已提交
951 952
	return error;
}