rxrpc.c 19.6 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 19 20
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include <rxrpc/packet.h>
#include "internal.h"
#include "afs_cm.h"

21
struct socket *afs_socket; /* my RxRPC socket */
22
static struct workqueue_struct *afs_async_calls;
23
static struct afs_call *afs_spare_incoming_call;
24
atomic_t afs_outstanding_calls;
25

26
static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
27
static int afs_wait_for_call_to_complete(struct afs_call *);
28 29
static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
static void afs_process_async_call(struct work_struct *);
30 31
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);
32
static int afs_deliver_cm_op_id(struct afs_call *);
33 34 35

/* asynchronous incoming call initial processing */
static const struct afs_call_type afs_RXCMxxxx = {
D
David Howells 已提交
36
	.name		= "CB.xxxx",
37 38 39 40
	.deliver	= afs_deliver_cm_op_id,
	.abort_to_error	= afs_abort_to_error,
};

41
static void afs_charge_preallocation(struct work_struct *);
42

43
static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
44

45 46 47 48 49 50
static int afs_wait_atomic_t(atomic_t *p)
{
	schedule();
	return 0;
}

51 52 53 54 55 56 57 58 59 60 61 62
/*
 * 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
 */
int afs_open_socket(void)
{
	struct sockaddr_rxrpc srx;
	struct socket *socket;
	int ret;

	_enter("");

63
	ret = -ENOMEM;
64
	afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
65 66
	if (!afs_async_calls)
		goto error_0;
67

68
	ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
69 70
	if (ret < 0)
		goto error_1;
71 72 73 74 75 76 77 78 79 80 81 82 83 84

	socket->sk->sk_allocation = GFP_NOFS;

	/* bind the callback manager's address to make this a server socket */
	srx.srx_family			= AF_RXRPC;
	srx.srx_service			= CM_SERVICE;
	srx.transport_type		= SOCK_DGRAM;
	srx.transport_len		= sizeof(srx.transport.sin);
	srx.transport.sin.sin_family	= AF_INET;
	srx.transport.sin.sin_port	= htons(AFS_CM_PORT);
	memset(&srx.transport.sin.sin_addr, 0,
	       sizeof(srx.transport.sin.sin_addr));

	ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
85 86 87
	if (ret < 0)
		goto error_2;

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

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

	afs_socket = socket;
96
	afs_charge_preallocation(NULL);
97 98
	_leave(" = 0");
	return 0;
99 100 101 102 103 104 105 106

error_2:
	sock_release(socket);
error_1:
	destroy_workqueue(afs_async_calls);
error_0:
	_leave(" = %d", ret);
	return ret;
107 108 109 110 111 112 113 114 115
}

/*
 * close the RxRPC socket AFS was using
 */
void afs_close_socket(void)
{
	_enter("");

116 117 118
	kernel_listen(afs_socket, 0);
	flush_workqueue(afs_async_calls);

119
	if (afs_spare_incoming_call) {
120
		afs_put_call(afs_spare_incoming_call);
121 122 123
		afs_spare_incoming_call = NULL;
	}

124
	_debug("outstanding %u", atomic_read(&afs_outstanding_calls));
125 126 127 128
	wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
			 TASK_UNINTERRUPTIBLE);
	_debug("no outstanding calls");

129
	kernel_sock_shutdown(afs_socket, SHUT_RDWR);
130
	flush_workqueue(afs_async_calls);
131 132 133 134 135 136 137
	sock_release(afs_socket);

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

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

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

151 152 153 154
	call->type = type;
	atomic_set(&call->usage, 1);
	INIT_WORK(&call->async_work, afs_process_async_call);
	init_waitqueue_head(&call->waitq);
155

156 157 158 159
	o = atomic_inc_return(&afs_outstanding_calls);
	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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	int n = atomic_dec_return(&call->usage);
	int o = atomic_read(&afs_outstanding_calls);

	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) {
			rxrpc_kernel_end_call(afs_socket, call->rxcall);
			call->rxcall = NULL;
		}
		if (call->type->destructor)
			call->type->destructor(call);

		kfree(call->request);
		kfree(call);

		o = atomic_dec_return(&afs_outstanding_calls);
		trace_afs_call(call, afs_call_trace_free, 0, o,
			       __builtin_return_address(0));
		if (o == 0)
			wake_up_atomic_t(&afs_outstanding_calls);
193
	}
194 195 196
}

/*
197
 * Queue the call for actual work.  Returns 0 unconditionally for convenience.
198
 */
199
int afs_queue_call_work(struct afs_call *call)
200
{
201 202 203 204 205 206 207 208 209 210 211
	int u = atomic_inc_return(&call->usage);

	trace_afs_call(call, afs_call_trace_work, u,
		       atomic_read(&afs_outstanding_calls),
		       __builtin_return_address(0));

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

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

214 215 216 217
/*
 * allocate a call with flat request and reply buffers
 */
struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
218
				     size_t request_size, size_t reply_max)
219 220 221
{
	struct afs_call *call;

222
	call = afs_alloc_call(type, GFP_NOFS);
223 224 225 226
	if (!call)
		goto nomem_call;

	if (request_size) {
227
		call->request_size = request_size;
228 229
		call->request = kmalloc(request_size, GFP_NOFS);
		if (!call->request)
D
David Howells 已提交
230
			goto nomem_free;
231 232
	}

233
	if (reply_max) {
234
		call->reply_max = reply_max;
235
		call->buffer = kmalloc(reply_max, GFP_NOFS);
236
		if (!call->buffer)
D
David Howells 已提交
237
			goto nomem_free;
238 239 240 241 242
	}

	init_waitqueue_head(&call->waitq);
	return call;

D
David Howells 已提交
243
nomem_free:
244
	afs_put_call(call);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
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;
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#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;
	}

	iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
}

295 296 297
/*
 * attach the data from a bunch of pages on an inode to a call
 */
A
Al Viro 已提交
298
static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
299
{
300 301
	struct bio_vec bv[AFS_BVEC_MAX];
	unsigned int bytes, nr, loop, offset;
302 303 304 305 306 307 308
	pgoff_t first = call->first, last = call->last;
	int ret;

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

	do {
309 310 311 312 313 314 315 316 317
		afs_load_bvec(call, msg, bv, first, last, offset);
		offset = 0;
		bytes = msg->msg_iter.count;
		nr = msg->msg_iter.nr_segs;

		/* Have to change the state *before* sending the last
		 * packet as RxRPC might give us the reply before it
		 * returns from sending the request.
		 */
318
		if (first + nr - 1 >= last)
319 320 321 322 323
			call->state = AFS_CALL_AWAIT_REPLY;
		ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
					     msg, bytes);
		for (loop = 0; loop < nr; loop++)
			put_page(bv[loop].bv_page);
324 325
		if (ret < 0)
			break;
326 327

		first += nr;
D
David Howells 已提交
328
	} while (first <= last);
329 330 331 332

	return ret;
}

333 334 335 336
/*
 * initiate a call
 */
int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
D
David Howells 已提交
337
		  bool async)
338 339 340 341 342
{
	struct sockaddr_rxrpc srx;
	struct rxrpc_call *rxcall;
	struct msghdr msg;
	struct kvec iov[1];
343 344
	size_t offset;
	u32 abort_code;
345 346 347 348
	int ret;

	_enter("%x,{%d},", addr->s_addr, ntohs(call->port));

D
David Howells 已提交
349 350 351
	ASSERT(call->type != NULL);
	ASSERT(call->type->name != NULL);

352 353 354
	_debug("____MAKE %p{%s,%x} [%d]____",
	       call, call->type->name, key_serial(call->key),
	       atomic_read(&afs_outstanding_calls));
D
David Howells 已提交
355

D
David Howells 已提交
356
	call->async = async;
357 358 359 360 361 362 363 364 365 366 367 368

	memset(&srx, 0, sizeof(srx));
	srx.srx_family = AF_RXRPC;
	srx.srx_service = call->service_id;
	srx.transport_type = SOCK_DGRAM;
	srx.transport_len = sizeof(srx.transport.sin);
	srx.transport.sin.sin_family = AF_INET;
	srx.transport.sin.sin_port = call->port;
	memcpy(&srx.transport.sin.sin_addr, addr, 4);

	/* create a call */
	rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
369
					 (unsigned long) call, gfp,
D
David Howells 已提交
370 371 372
					 (async ?
					  afs_wake_up_async_call :
					  afs_wake_up_call_waiter));
D
David Howells 已提交
373
	call->key = NULL;
374 375 376 377 378 379 380 381 382 383 384 385 386
	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;
387
	iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
A
Al Viro 已提交
388
		      call->request_size);
389 390
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
391
	msg.msg_flags		= (call->send_pages ? MSG_MORE : 0);
392

393 394 395 396 397
	/* We have to change the state *before* sending the last packet as
	 * rxrpc might give us the reply before it returns from sending the
	 * request.  Further, if the send fails, we may already have been given
	 * a notification and may have collected it.
	 */
398 399
	if (!call->send_pages)
		call->state = AFS_CALL_AWAIT_REPLY;
400 401
	ret = rxrpc_kernel_send_data(afs_socket, rxcall,
				     &msg, call->request_size);
402 403 404
	if (ret < 0)
		goto error_do_abort;

405
	if (call->send_pages) {
A
Al Viro 已提交
406
		ret = afs_send_pages(call, &msg);
407 408 409 410
		if (ret < 0)
			goto error_do_abort;
	}

411 412
	/* at this point, an async call may no longer exist as it may have
	 * already completed */
D
David Howells 已提交
413 414 415 416
	if (call->async)
		return -EINPROGRESS;

	return afs_wait_for_call_to_complete(call);
417 418

error_do_abort:
419 420 421 422 423 424 425 426 427 428 429
	call->state = AFS_CALL_COMPLETE;
	if (ret != -ECONNABORTED) {
		rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT,
					-ret, "KSD");
	} else {
		abort_code = 0;
		offset = 0;
		rxrpc_kernel_recv_data(afs_socket, rxcall, NULL, 0, &offset,
				       false, &abort_code);
		ret = call->type->abort_to_error(abort_code);
	}
430
error_kill_call:
431
	afs_put_call(call);
432 433 434 435 436 437 438 439 440 441 442 443
	_leave(" = %d", ret);
	return ret;
}

/*
 * deliver messages to a call
 */
static void afs_deliver_to_call(struct afs_call *call)
{
	u32 abort_code;
	int ret;

444 445 446 447 448 449 450 451 452 453 454 455
	_enter("%s", call->type->name);

	while (call->state == AFS_CALL_AWAIT_REPLY ||
	       call->state == AFS_CALL_AWAIT_OP_ID ||
	       call->state == AFS_CALL_AWAIT_REQUEST ||
	       call->state == AFS_CALL_AWAIT_ACK
	       ) {
		if (call->state == AFS_CALL_AWAIT_ACK) {
			size_t offset = 0;
			ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
						     NULL, 0, &offset, false,
						     &call->abort_code);
D
David Howells 已提交
456 457
			trace_afs_recv_data(call, 0, offset, false, ret);

458 459
			if (ret == -EINPROGRESS || ret == -EAGAIN)
				return;
460
			if (ret == 1 || ret < 0) {
461 462
				call->state = AFS_CALL_COMPLETE;
				goto done;
463
			}
464
			return;
465 466
		}

467 468 469 470 471 472 473 474 475
		ret = call->type->deliver(call);
		switch (ret) {
		case 0:
			if (call->state == AFS_CALL_AWAIT_REPLY)
				call->state = AFS_CALL_COMPLETE;
			goto done;
		case -EINPROGRESS:
		case -EAGAIN:
			goto out;
476 477
		case -ECONNABORTED:
			goto call_complete;
478 479 480
		case -ENOTCONN:
			abort_code = RX_CALL_DEAD;
			rxrpc_kernel_abort_call(afs_socket, call->rxcall,
481
						abort_code, -ret, "KNC");
482
			goto save_error;
483
		case -ENOTSUPP:
484
			abort_code = RXGEN_OPCODE;
485
			rxrpc_kernel_abort_call(afs_socket, call->rxcall,
486
						abort_code, -ret, "KIV");
487
			goto save_error;
488 489 490 491 492 493 494 495
		case -ENODATA:
		case -EBADMSG:
		case -EMSGSIZE:
		default:
			abort_code = RXGEN_CC_UNMARSHAL;
			if (call->state != AFS_CALL_AWAIT_REPLY)
				abort_code = RXGEN_SS_UNMARSHAL;
			rxrpc_kernel_abort_call(afs_socket, call->rxcall,
496
						abort_code, EBADMSG, "KUM");
497
			goto save_error;
498
		}
499 500
	}

501 502
done:
	if (call->state == AFS_CALL_COMPLETE && call->incoming)
503
		afs_put_call(call);
504
out:
505
	_leave("");
506 507
	return;

508
save_error:
509
	call->error = ret;
510
call_complete:
511 512
	call->state = AFS_CALL_COMPLETE;
	goto done;
513 514 515 516 517 518 519
}

/*
 * wait synchronously for a call to complete
 */
static int afs_wait_for_call_to_complete(struct afs_call *call)
{
520
	const char *abort_why;
521 522 523 524 525 526 527 528 529 530 531
	int ret;

	DECLARE_WAITQUEUE(myself, current);

	_enter("");

	add_wait_queue(&call->waitq, &myself);
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);

		/* deliver any messages that are in the queue */
532 533
		if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
			call->need_attention = false;
534 535 536 537 538
			__set_current_state(TASK_RUNNING);
			afs_deliver_to_call(call);
			continue;
		}

539
		abort_why = "KWC";
540
		ret = call->error;
541
		if (call->state == AFS_CALL_COMPLETE)
542
			break;
543
		abort_why = "KWI";
544 545 546 547 548 549 550 551 552 553 554 555
		ret = -EINTR;
		if (signal_pending(current))
			break;
		schedule();
	}

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

	/* kill the call */
	if (call->state < AFS_CALL_COMPLETE) {
		_debug("call incomplete");
556
		rxrpc_kernel_abort_call(afs_socket, call->rxcall,
557
					RX_CALL_DEAD, -ret, abort_why);
558 559
	} else if (call->error < 0) {
		ret = call->error;
560 561 562
	}

	_debug("call complete");
563
	afs_put_call(call);
564 565 566 567 568 569 570
	_leave(" = %d", ret);
	return ret;
}

/*
 * wake up a waiting call
 */
571 572
static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
				    unsigned long call_user_ID)
573
{
574 575 576
	struct afs_call *call = (struct afs_call *)call_user_ID;

	call->need_attention = true;
577 578 579 580 581 582
	wake_up(&call->waitq);
}

/*
 * wake up an asynchronous call
 */
583 584
static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
				   unsigned long call_user_ID)
585
{
586
	struct afs_call *call = (struct afs_call *)call_user_ID;
587
	int u;
588

D
David Howells 已提交
589
	trace_afs_notify_call(rxcall, call);
590
	call->need_attention = true;
591 592 593 594 595 596 597 598 599 600

	u = __atomic_add_unless(&call->usage, 1, 0);
	if (u != 0) {
		trace_afs_call(call, afs_call_trace_wake, u,
			       atomic_read(&afs_outstanding_calls),
			       __builtin_return_address(0));

		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
	}
601 602 603
}

/*
604 605
 * Delete an asynchronous call.  The work item carries a ref to the call struct
 * that we need to release.
606
 */
607
static void afs_delete_async_call(struct work_struct *work)
608
{
609 610
	struct afs_call *call = container_of(work, struct afs_call, async_work);

611 612
	_enter("");

613
	afs_put_call(call);
614 615 616 617 618

	_leave("");
}

/*
619 620
 * 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.
621
 */
622
static void afs_process_async_call(struct work_struct *work)
623
{
624 625
	struct afs_call *call = container_of(work, struct afs_call, async_work);

626 627
	_enter("");

628 629
	if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
		call->need_attention = false;
630
		afs_deliver_to_call(call);
631
	}
632

D
David Howells 已提交
633
	if (call->state == AFS_CALL_COMPLETE) {
634 635
		call->reply = NULL;

636 637 638 639
		/* 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.
		 */
640
		call->async_work.func = afs_delete_async_call;
641 642
		if (!queue_work(afs_async_calls, &call->async_work))
			afs_put_call(call);
643 644
	}

645
	afs_put_call(call);
646 647 648
	_leave("");
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
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.
 */
static void afs_charge_preallocation(struct work_struct *work)
{
	struct afs_call *call = afs_spare_incoming_call;

	for (;;) {
		if (!call) {
665
			call = afs_alloc_call(&afs_RXCMxxxx, GFP_KERNEL);
666 667 668
			if (!call)
				break;

D
David Howells 已提交
669
			call->async = true;
670
			call->state = AFS_CALL_AWAIT_OP_ID;
D
David Howells 已提交
671
			init_waitqueue_head(&call->waitq);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		}

		if (rxrpc_kernel_charge_accept(afs_socket,
					       afs_wake_up_async_call,
					       afs_rx_attach,
					       (unsigned long)call,
					       GFP_KERNEL) < 0)
			break;
		call = NULL;
	}
	afs_spare_incoming_call = call;
}

/*
 * 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;
694
	afs_put_call(call);
695 696
}

697 698 699
/*
 * Notification of an incoming call.
 */
700 701
static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
			    unsigned long user_call_ID)
702
{
703
	queue_work(afs_wq, &afs_charge_preallocation_work);
704 705
}

706
/*
707 708
 * 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.
709
 */
710
static int afs_deliver_cm_op_id(struct afs_call *call)
711
{
712
	int ret;
713

714
	_enter("{%zu}", call->offset);
715 716 717 718

	ASSERTCMP(call->offset, <, 4);

	/* the operation ID forms the first four bytes of the request data */
719
	ret = afs_extract_data(call, &call->tmp, 4, true);
720 721
	if (ret < 0)
		return ret;
722

723
	call->operation_ID = ntohl(call->tmp);
724
	call->state = AFS_CALL_AWAIT_REQUEST;
725
	call->offset = 0;
726 727 728 729 730 731

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

734 735
	/* pass responsibility for the remainer of this message off to the
	 * cache manager op */
736
	return call->type->deliver(call);
737 738 739 740 741 742 743 744 745 746 747 748 749
}

/*
 * send an empty reply
 */
void afs_send_empty_reply(struct afs_call *call)
{
	struct msghdr msg;

	_enter("");

	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
750
	iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
751 752 753 754 755
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

	call->state = AFS_CALL_AWAIT_ACK;
756
	switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
757 758 759 760 761 762
	case 0:
		_leave(" [replied]");
		return;

	case -ENOMEM:
		_debug("oom");
763
		rxrpc_kernel_abort_call(afs_socket, call->rxcall,
764
					RX_USER_ABORT, ENOMEM, "KOO");
765 766 767 768 769 770
	default:
		_leave(" [error]");
		return;
	}
}

771 772 773 774 775 776
/*
 * send a simple reply
 */
void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
{
	struct msghdr msg;
777
	struct kvec iov[1];
778
	int n;
779 780 781 782 783 784 785

	_enter("");

	iov[0].iov_base		= (void *) buf;
	iov[0].iov_len		= len;
	msg.msg_name		= NULL;
	msg.msg_namelen		= 0;
786
	iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
787 788 789 790 791
	msg.msg_control		= NULL;
	msg.msg_controllen	= 0;
	msg.msg_flags		= 0;

	call->state = AFS_CALL_AWAIT_ACK;
792
	n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
793
	if (n >= 0) {
794
		/* Success */
795 796
		_leave(" [replied]");
		return;
797
	}
798

799
	if (n == -ENOMEM) {
800
		_debug("oom");
801
		rxrpc_kernel_abort_call(afs_socket, call->rxcall,
802
					RX_USER_ABORT, ENOMEM, "KOO");
803
	}
804
	_leave(" [error]");
805 806
}

807
/*
808
 * Extract a piece of data from the received data socket buffers.
809
 */
810 811
int afs_extract_data(struct afs_call *call, void *buf, size_t count,
		     bool want_more)
812
{
813
	int ret;
814

815 816
	_enter("{%s,%zu},,%zu,%d",
	       call->type->name, call->offset, count, want_more);
817

818
	ASSERTCMP(call->offset, <=, count);
819

820 821 822
	ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
				     buf, count, &call->offset,
				     want_more, &call->abort_code);
D
David Howells 已提交
823
	trace_afs_recv_data(call, count, call->offset, want_more, ret);
824 825
	if (ret == 0 || ret == -EAGAIN)
		return ret;
826

827 828 829 830 831 832 833 834 835 836 837 838
	if (ret == 1) {
		switch (call->state) {
		case AFS_CALL_AWAIT_REPLY:
			call->state = AFS_CALL_COMPLETE;
			break;
		case AFS_CALL_AWAIT_REQUEST:
			call->state = AFS_CALL_REPLYING;
			break;
		default:
			break;
		}
		return 0;
839
	}
840 841 842 843 844 845 846

	if (ret == -ECONNABORTED)
		call->error = call->type->abort_to_error(call->abort_code);
	else
		call->error = ret;
	call->state = AFS_CALL_COMPLETE;
	return ret;
847
}