rxrpc.c 23.6 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 40 41 42 43
{
	struct sockaddr_rxrpc srx;
	struct socket *socket;
	int ret;

	_enter("");

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

	socket->sk->sk_allocation = GFP_NOFS;

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

59 60
	ret = rxrpc_sock_set_min_security_level(socket->sk,
						RXRPC_SECURITY_ENCRYPT);
61 62 63
	if (ret < 0)
		goto error_2;

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

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

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

84 85
	rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
					   afs_rx_discard_new_call);
86

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

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

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

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

110
	kernel_listen(net->socket, 0);
111 112
	flush_workqueue(afs_async_calls);

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

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

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

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

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

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

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

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

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

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

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

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

184
		afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
185
		afs_put_addrlist(call->alist);
186 187 188 189
		kfree(call->request);

		trace_afs_call(call, afs_call_trace_free, 0, o,
			       __builtin_return_address(0));
190 191 192
		kfree(call);

		o = atomic_dec_return(&net->nr_outstanding_calls);
193
		if (o == 0)
194
			wake_up_var(&net->nr_outstanding_calls);
195
	}
196 197
}

198 199 200 201 202 203 204 205 206 207 208
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;
}

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

217
		afs_get_call(call, afs_call_trace_work);
218 219 220
		if (!queue_work(afs_wq, &call->work))
			afs_put_call(call);
	}
221 222
}

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

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

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

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

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

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

274 275 276 277 278 279 280 281 282
#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)
{
283
	struct afs_operation *op = call->op;
284 285 286 287
	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);
288
	n = find_get_pages_contig(op->store.mapping, first, nr, pages);
289 290 291 292 293 294
	ASSERTCMP(n, ==, nr);

	msg->msg_flags |= MSG_MORE;
	for (i = 0; i < nr; i++) {
		to = PAGE_SIZE;
		if (first + i >= last) {
295
			to = op->store.last_to;
296 297 298 299 300 301 302 303 304
			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;
	}

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

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

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

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

331 332
	offset = op->store.first_offset;
	op->store.first_offset = 0;
333 334

	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(op->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

352
	trace_afs_sent_pages(call, op->store.first, last, first, ret);
353 354 355
	return ret;
}

356
/*
357 358
 * 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.
359
 */
360
void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
361
{
362
	struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
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

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

381 382 383 384 385 386
	/* 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) {
387 388 389 390
		struct afs_operation *op = call->op;

		if (op->store.last == op->store.first) {
			tx_total_len += op->store.last_to - op->store.first_offset;
391 392 393 394 395
		} 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...
			 */
396 397 398
			tx_total_len += PAGE_SIZE - op->store.first_offset;
			tx_total_len += op->store.last_to;
			tx_total_len += (op->store.last - op->store.first - 1) * PAGE_SIZE;
399
		}
400 401
	}

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

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

	call->rxcall = rxcall;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

589
done:
590 591
	if (call->type->done)
		call->type->done(call);
592
out:
593
	_leave("");
594 595
	return;

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

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

	DECLARE_WAITQUEUE(myself, current);

	_enter("");

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

621 622
	add_wait_queue(&call->waitq, &myself);
	for (;;) {
623
		set_current_state(TASK_UNINTERRUPTIBLE);
624 625

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

634
		if (afs_check_call_state(call, AFS_CALL_COMPLETE))
635
			break;
636

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

643
		schedule();
644 645 646 647 648
	}

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

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

661
	spin_lock_bh(&call->state_lock);
662 663
	ac->abort_code = call->abort_code;
	ac->error = call->error;
664
	spin_unlock_bh(&call->state_lock);
665 666 667 668

	ret = ac->error;
	switch (ret) {
	case 0:
669 670 671
		ret = call->ret0;
		call->ret0 = 0;

672 673 674 675
		/* Fall through */
	case -ECONNABORTED:
		ac->responded = true;
		break;
676 677
	}

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

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

	call->need_attention = true;
694 695 696 697 698 699
	wake_up(&call->waitq);
}

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

D
David Howells 已提交
706
	trace_afs_notify_call(rxcall, call);
707
	call->need_attention = true;
708

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

		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
	}
718 719 720
}

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

728 729
	_enter("");

730 731
	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
		call->need_attention = false;
732
		afs_deliver_to_call(call);
733
	}
734

735
	afs_put_call(call);
736 737 738
	_leave("");
}

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

	for (;;) {
		if (!call) {
757
			call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
758 759 760
			if (!call)
				break;

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

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

/*
 * 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;
789
	afs_put_call(call);
790 791
}

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

	queue_work(afs_wq, &net->charge_preallocation_work);
801 802
}

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

811
	_enter("{%zu}", iov_iter_count(call->iter));
812 813

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

818
	call->operation_ID = ntohl(call->tmp);
819
	afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
820 821 822 823 824 825

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

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

833 834 835 836 837 838 839 840 841 842
/*
 * 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;

843
	afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
844 845
}

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

	_enter("");

856
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
857

858 859
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
860
	iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
861 862 863 864
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

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

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

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

	_enter("");

894
	rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
895

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

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

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

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

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

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

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

958
	afs_set_call_complete(call, ret, remote_abort);
959
	return ret;
960
}
D
David Howells 已提交
961 962 963 964

/*
 * Log protocol error production.
 */
965
noinline int afs_protocol_error(struct afs_call *call,
966
				enum afs_eproto_cause cause)
D
David Howells 已提交
967
{
968
	trace_afs_protocol_error(call, cause);
969 970
	if (call)
		call->unmarshalling_error = true;
971
	return -EBADMSG;
D
David Howells 已提交
972
}