rxrpc.c 23.5 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 21
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);
static void afs_process_async_call(struct work_struct *);
22 23
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);
24
static int afs_deliver_cm_op_id(struct afs_call *);
25 26 27

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

	_enter("");

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

	socket->sk->sk_allocation = GFP_NOFS;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

162
/*
163
 * Dispose of a reference on a call.
164
 */
165
void afs_put_call(struct afs_call *call)
166
{
167
	struct afs_net *net = call->net;
168
	int n = atomic_dec_return(&call->usage);
169
	int o = atomic_read(&net->nr_outstanding_calls);
170

D
David Howells 已提交
171
	trace_afs_call(call, afs_call_trace_put, n, o,
172 173 174 175 176 177 178 179
		       __builtin_return_address(0));

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

		if (call->rxcall) {
180
			rxrpc_kernel_end_call(net->socket, call->rxcall);
181 182 183 184 185
			call->rxcall = NULL;
		}
		if (call->type->destructor)
			call->type->destructor(call);

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

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

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

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

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

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

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

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

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

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

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

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

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 306
#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;
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

401 402 403
	/* 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.
	 */
404
	if (call->async) {
405
		afs_get_call(call, afs_call_trace_get);
406 407
		call->drop_ref = true;
	}
408

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

	call->rxcall = rxcall;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

586
done:
587 588
	if (call->type->done)
		call->type->done(call);
589
out:
590
	_leave("");
591 592
	return;

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

/*
602
 * Wait synchronously for a call to complete and clean up the call struct.
603
 */
604 605
long afs_wait_for_call_to_complete(struct afs_call *call,
				   struct afs_addr_cursor *ac)
606
{
607
	long ret;
608
	bool rxrpc_complete = false;
609 610 611 612 613

	DECLARE_WAITQUEUE(myself, current);

	_enter("");

614 615 616 617
	ret = call->error;
	if (ret < 0)
		goto out;

618 619
	add_wait_queue(&call->waitq, &myself);
	for (;;) {
620
		set_current_state(TASK_UNINTERRUPTIBLE);
621 622

		/* deliver any messages that are in the queue */
623 624
		if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
		    call->need_attention) {
625
			call->need_attention = false;
626 627 628 629 630
			__set_current_state(TASK_RUNNING);
			afs_deliver_to_call(call);
			continue;
		}

631
		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
632
			break;
633

634
		if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
635 636 637 638 639
			/* rxrpc terminated the call. */
			rxrpc_complete = true;
			break;
		}

640
		schedule();
641 642 643 644 645
	}

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

646
	if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
647 648 649 650 651 652 653 654 655
		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);
		}
656 657
	}

658
	spin_lock_bh(&call->state_lock);
659 660
	ac->abort_code = call->abort_code;
	ac->error = call->error;
661
	spin_unlock_bh(&call->state_lock);
662 663 664 665

	ret = ac->error;
	switch (ret) {
	case 0:
666 667 668
		ret = call->ret0;
		call->ret0 = 0;

669 670 671 672
		/* Fall through */
	case -ECONNABORTED:
		ac->responded = true;
		break;
673 674
	}

675
out:
676
	_debug("call complete");
677
	afs_put_call(call);
678
	_leave(" = %p", (void *)ret);
679 680 681 682 683 684
	return ret;
}

/*
 * wake up a waiting call
 */
685 686
static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
				    unsigned long call_user_ID)
687
{
688 689 690
	struct afs_call *call = (struct afs_call *)call_user_ID;

	call->need_attention = true;
691 692 693 694 695 696
	wake_up(&call->waitq);
}

/*
 * wake up an asynchronous call
 */
697 698
static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
				   unsigned long call_user_ID)
699
{
700
	struct afs_call *call = (struct afs_call *)call_user_ID;
701
	int u;
702

D
David Howells 已提交
703
	trace_afs_notify_call(rxcall, call);
704
	call->need_attention = true;
705

706
	u = atomic_fetch_add_unless(&call->usage, 1, 0);
707
	if (u != 0) {
D
David Howells 已提交
708
		trace_afs_call(call, afs_call_trace_wake, u + 1,
709
			       atomic_read(&call->net->nr_outstanding_calls),
710 711 712 713 714
			       __builtin_return_address(0));

		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
	}
715 716 717
}

/*
718 719
 * 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.
720
 */
721
static void afs_process_async_call(struct work_struct *work)
722
{
723 724
	struct afs_call *call = container_of(work, struct afs_call, async_work);

725 726
	_enter("");

727 728
	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
		call->need_attention = false;
729
		afs_deliver_to_call(call);
730
	}
731

732
	afs_put_call(call);
733 734 735
	_leave("");
}

736 737 738 739 740 741 742 743 744 745
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.
 */
746
void afs_charge_preallocation(struct work_struct *work)
747
{
748 749 750
	struct afs_net *net =
		container_of(work, struct afs_net, charge_preallocation_work);
	struct afs_call *call = net->spare_incoming_call;
751 752 753

	for (;;) {
		if (!call) {
754
			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
755 756 757
			if (!call)
				break;

758
			call->drop_ref = true;
D
David Howells 已提交
759
			call->async = true;
760
			call->state = AFS_CALL_SV_AWAIT_OP_ID;
D
David Howells 已提交
761
			init_waitqueue_head(&call->waitq);
762
			afs_extract_to_tmp(call);
763 764
		}

765
		if (rxrpc_kernel_charge_accept(net->socket,
766 767 768
					       afs_wake_up_async_call,
					       afs_rx_attach,
					       (unsigned long)call,
769 770
					       GFP_KERNEL,
					       call->debug_id) < 0)
771 772 773
			break;
		call = NULL;
	}
774
	net->spare_incoming_call = call;
775 776 777 778 779 780 781 782 783 784 785
}

/*
 * 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;
786
	afs_put_call(call);
787 788
}

789 790 791
/*
 * Notification of an incoming call.
 */
792 793
static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
			    unsigned long user_call_ID)
794
{
795 796 797
	struct afs_net *net = afs_sock2net(sk);

	queue_work(afs_wq, &net->charge_preallocation_work);
798 799
}

800
/*
801 802
 * 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.
803
 */
804
static int afs_deliver_cm_op_id(struct afs_call *call)
805
{
806
	int ret;
807

808
	_enter("{%zu}", iov_iter_count(call->iter));
809 810

	/* the operation ID forms the first four bytes of the request data */
811
	ret = afs_extract_data(call, true);
812 813
	if (ret < 0)
		return ret;
814

815
	call->operation_ID = ntohl(call->tmp);
816
	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
817 818 819 820 821 822

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

825 826
	/* pass responsibility for the remainer of this message off to the
	 * cache manager op */
827
	return call->type->deliver(call);
828 829
}

830 831 832 833 834 835 836 837 838 839
/*
 * 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;

840
	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
841 842
}

843 844 845 846 847
/*
 * send an empty reply
 */
void afs_send_empty_reply(struct afs_call *call)
{
848
	struct afs_net *net = call->net;
849 850 851 852
	struct msghdr msg;

	_enter("");

853
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
854

855 856
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
857
	iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
858 859 860 861
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

862
	switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
863
				       afs_notify_end_reply_tx)) {
864 865 866 867 868 869
	case 0:
		_leave(" [replied]");
		return;

	case -ENOMEM:
		_debug("oom");
870
		rxrpc_kernel_abort_call(net->socket, call->rxcall,
871
					RX_USER_ABORT, -ENOMEM, "KOO");
872
		/* Fall through */
873 874 875 876 877 878
	default:
		_leave(" [error]");
		return;
	}
}

879 880 881 882 883
/*
 * send a simple reply
 */
void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
{
884
	struct afs_net *net = call->net;
885
	struct msghdr msg;
886
	struct kvec iov[1];
887
	int n;
888 889 890

	_enter("");

891
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
892

893 894 895 896
	iov[0].iov_base		= (void *) buf;
	iov[0].iov_len		= len;
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
897
	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
898 899 900 901
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

902
	n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
903
				   afs_notify_end_reply_tx);
904
	if (n >= 0) {
905
		/* Success */
906 907
		_leave(" [replied]");
		return;
908
	}
909

910
	if (n == -ENOMEM) {
911
		_debug("oom");
912
		rxrpc_kernel_abort_call(net->socket, call->rxcall,
913
					RX_USER_ABORT, -ENOMEM, "KOO");
914
	}
915
	_leave(" [error]");
916 917
}

918
/*
919
 * Extract a piece of data from the received data socket buffers.
920
 */
921
int afs_extract_data(struct afs_call *call, bool want_more)
922
{
923
	struct afs_net *net = call->net;
924
	struct iov_iter *iter = call->iter;
925
	enum afs_call_state state;
926
	u32 remote_abort = 0;
927
	int ret;
928

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

931
	ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
932
				     want_more, &remote_abort,
933
				     &call->service_id);
934 935
	if (ret == 0 || ret == -EAGAIN)
		return ret;
936

937
	state = READ_ONCE(call->state);
938
	if (ret == 1) {
939 940 941
		switch (state) {
		case AFS_CALL_CL_AWAIT_REPLY:
			afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
942
			break;
943 944
		case AFS_CALL_SV_AWAIT_REQUEST:
			afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
945
			break;
946 947
		case AFS_CALL_COMPLETE:
			kdebug("prem complete %d", call->error);
948
			return afs_io_error(call, afs_io_error_extract);
949 950 951 952
		default:
			break;
		}
		return 0;
953
	}
954

955
	afs_set_call_complete(call, ret, remote_abort);
956
	return ret;
957
}
D
David Howells 已提交
958 959 960 961

/*
 * Log protocol error production.
 */
962 963
noinline int afs_protocol_error(struct afs_call *call, int error,
				enum afs_eproto_cause cause)
D
David Howells 已提交
964
{
965
	trace_afs_protocol_error(call, error, cause);
D
David Howells 已提交
966 967
	return error;
}