]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/afs/rxrpc.c
rxrpc: Add tracepoints to record received packets and end of data_ready
[mirror_ubuntu-artful-kernel.git] / fs / afs / rxrpc.c
CommitLineData
08e0e7c8
DH
1/* Maintain an RxRPC server socket to do AFS communications through
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
5a0e3ad6 12#include <linux/slab.h>
08e0e7c8
DH
13#include <net/sock.h>
14#include <net/af_rxrpc.h>
15#include <rxrpc/packet.h>
16#include "internal.h"
17#include "afs_cm.h"
18
8324f0bc 19struct socket *afs_socket; /* my RxRPC socket */
08e0e7c8 20static struct workqueue_struct *afs_async_calls;
00d3b7a4 21static atomic_t afs_outstanding_calls;
08e0e7c8 22
d001648e
DH
23static void afs_free_call(struct afs_call *);
24static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
08e0e7c8 25static int afs_wait_for_call_to_complete(struct afs_call *);
d001648e 26static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
08e0e7c8 27static int afs_dont_wait_for_call_to_complete(struct afs_call *);
d001648e
DH
28static void afs_process_async_call(struct work_struct *);
29static void afs_rx_new_call(struct sock *);
30static int afs_deliver_cm_op_id(struct afs_call *);
08e0e7c8
DH
31
32/* synchronous call management */
33const struct afs_wait_mode afs_sync_call = {
d001648e 34 .notify_rx = afs_wake_up_call_waiter,
08e0e7c8
DH
35 .wait = afs_wait_for_call_to_complete,
36};
37
38/* asynchronous call management */
39const struct afs_wait_mode afs_async_call = {
d001648e 40 .notify_rx = afs_wake_up_async_call,
08e0e7c8
DH
41 .wait = afs_dont_wait_for_call_to_complete,
42};
43
44/* asynchronous incoming call management */
45static const struct afs_wait_mode afs_async_incoming_call = {
d001648e 46 .notify_rx = afs_wake_up_async_call,
08e0e7c8
DH
47};
48
49/* asynchronous incoming call initial processing */
50static const struct afs_call_type afs_RXCMxxxx = {
00d3b7a4 51 .name = "CB.xxxx",
08e0e7c8
DH
52 .deliver = afs_deliver_cm_op_id,
53 .abort_to_error = afs_abort_to_error,
54};
55
56static void afs_collect_incoming_call(struct work_struct *);
57
08e0e7c8
DH
58static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
59
2f02f7ae
DH
60static int afs_wait_atomic_t(atomic_t *p)
61{
62 schedule();
63 return 0;
64}
65
08e0e7c8
DH
66/*
67 * open an RxRPC socket and bind it to be a server for callback notifications
68 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
69 */
70int afs_open_socket(void)
71{
72 struct sockaddr_rxrpc srx;
73 struct socket *socket;
74 int ret;
75
76 _enter("");
77
0e119b41 78 ret = -ENOMEM;
69ad052a 79 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
0e119b41
DH
80 if (!afs_async_calls)
81 goto error_0;
08e0e7c8 82
eeb1bd5c 83 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
0e119b41
DH
84 if (ret < 0)
85 goto error_1;
08e0e7c8
DH
86
87 socket->sk->sk_allocation = GFP_NOFS;
88
89 /* bind the callback manager's address to make this a server socket */
90 srx.srx_family = AF_RXRPC;
91 srx.srx_service = CM_SERVICE;
92 srx.transport_type = SOCK_DGRAM;
93 srx.transport_len = sizeof(srx.transport.sin);
94 srx.transport.sin.sin_family = AF_INET;
95 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
96 memset(&srx.transport.sin.sin_addr, 0,
97 sizeof(srx.transport.sin.sin_addr));
98
99 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
0e119b41
DH
100 if (ret < 0)
101 goto error_2;
102
d001648e
DH
103 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call);
104
0e119b41
DH
105 ret = kernel_listen(socket, INT_MAX);
106 if (ret < 0)
107 goto error_2;
08e0e7c8 108
08e0e7c8
DH
109 afs_socket = socket;
110 _leave(" = 0");
111 return 0;
0e119b41
DH
112
113error_2:
114 sock_release(socket);
115error_1:
116 destroy_workqueue(afs_async_calls);
117error_0:
118 _leave(" = %d", ret);
119 return ret;
08e0e7c8
DH
120}
121
122/*
123 * close the RxRPC socket AFS was using
124 */
125void afs_close_socket(void)
126{
127 _enter("");
128
d001648e 129 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
2f02f7ae
DH
130 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
131 TASK_UNINTERRUPTIBLE);
132 _debug("no outstanding calls");
133
d001648e 134 flush_workqueue(afs_async_calls);
08e0e7c8
DH
135 sock_release(afs_socket);
136
137 _debug("dework");
138 destroy_workqueue(afs_async_calls);
139 _leave("");
140}
141
00d3b7a4
DH
142/*
143 * free a call
144 */
145static void afs_free_call(struct afs_call *call)
146{
147 _debug("DONE %p{%s} [%d]",
148 call, call->type->name, atomic_read(&afs_outstanding_calls));
00d3b7a4
DH
149
150 ASSERTCMP(call->rxcall, ==, NULL);
151 ASSERT(!work_pending(&call->async_work));
00d3b7a4
DH
152 ASSERT(call->type->name != NULL);
153
154 kfree(call->request);
155 kfree(call);
2f02f7ae
DH
156
157 if (atomic_dec_and_test(&afs_outstanding_calls))
158 wake_up_atomic_t(&afs_outstanding_calls);
00d3b7a4
DH
159}
160
6c67c7c3 161/*
6cf12869 162 * End a call but do not free it
6c67c7c3 163 */
6cf12869 164static void afs_end_call_nofree(struct afs_call *call)
6c67c7c3
DH
165{
166 if (call->rxcall) {
4de48af6 167 rxrpc_kernel_end_call(afs_socket, call->rxcall);
6c67c7c3
DH
168 call->rxcall = NULL;
169 }
6cf12869
NWF
170 if (call->type->destructor)
171 call->type->destructor(call);
172}
173
174/*
175 * End a call and free it
176 */
177static void afs_end_call(struct afs_call *call)
178{
179 afs_end_call_nofree(call);
6c67c7c3
DH
180 afs_free_call(call);
181}
182
08e0e7c8
DH
183/*
184 * allocate a call with flat request and reply buffers
185 */
186struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
d001648e 187 size_t request_size, size_t reply_max)
08e0e7c8
DH
188{
189 struct afs_call *call;
190
191 call = kzalloc(sizeof(*call), GFP_NOFS);
192 if (!call)
193 goto nomem_call;
194
00d3b7a4
DH
195 _debug("CALL %p{%s} [%d]",
196 call, type->name, atomic_read(&afs_outstanding_calls));
197 atomic_inc(&afs_outstanding_calls);
198
199 call->type = type;
200 call->request_size = request_size;
d001648e 201 call->reply_max = reply_max;
00d3b7a4 202
08e0e7c8
DH
203 if (request_size) {
204 call->request = kmalloc(request_size, GFP_NOFS);
205 if (!call->request)
00d3b7a4 206 goto nomem_free;
08e0e7c8
DH
207 }
208
d001648e
DH
209 if (reply_max) {
210 call->buffer = kmalloc(reply_max, GFP_NOFS);
08e0e7c8 211 if (!call->buffer)
00d3b7a4 212 goto nomem_free;
08e0e7c8
DH
213 }
214
08e0e7c8 215 init_waitqueue_head(&call->waitq);
08e0e7c8
DH
216 return call;
217
00d3b7a4
DH
218nomem_free:
219 afs_free_call(call);
08e0e7c8
DH
220nomem_call:
221 return NULL;
222}
223
224/*
225 * clean up a call with flat buffer
226 */
227void afs_flat_call_destructor(struct afs_call *call)
228{
229 _enter("");
230
231 kfree(call->request);
232 call->request = NULL;
233 kfree(call->buffer);
234 call->buffer = NULL;
235}
236
31143d5d
DH
237/*
238 * attach the data from a bunch of pages on an inode to a call
239 */
c1206a2c
AB
240static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
241 struct kvec *iov)
31143d5d
DH
242{
243 struct page *pages[8];
244 unsigned count, n, loop, offset, to;
245 pgoff_t first = call->first, last = call->last;
246 int ret;
247
248 _enter("");
249
250 offset = call->first_offset;
251 call->first_offset = 0;
252
253 do {
254 _debug("attach %lx-%lx", first, last);
255
256 count = last - first + 1;
257 if (count > ARRAY_SIZE(pages))
258 count = ARRAY_SIZE(pages);
259 n = find_get_pages_contig(call->mapping, first, count, pages);
260 ASSERTCMP(n, ==, count);
261
262 loop = 0;
263 do {
264 msg->msg_flags = 0;
265 to = PAGE_SIZE;
266 if (first + loop >= last)
267 to = call->last_to;
268 else
269 msg->msg_flags = MSG_MORE;
270 iov->iov_base = kmap(pages[loop]) + offset;
271 iov->iov_len = to - offset;
272 offset = 0;
273
274 _debug("- range %u-%u%s",
275 offset, to, msg->msg_flags ? " [more]" : "");
2e90b1c4
AV
276 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
277 iov, 1, to - offset);
31143d5d
DH
278
279 /* have to change the state *before* sending the last
280 * packet as RxRPC might give us the reply before it
281 * returns from sending the request */
282 if (first + loop >= last)
283 call->state = AFS_CALL_AWAIT_REPLY;
4de48af6
DH
284 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
285 msg, to - offset);
31143d5d
DH
286 kunmap(pages[loop]);
287 if (ret < 0)
288 break;
289 } while (++loop < count);
290 first += count;
291
292 for (loop = 0; loop < count; loop++)
293 put_page(pages[loop]);
294 if (ret < 0)
295 break;
5bbf5d39 296 } while (first <= last);
31143d5d
DH
297
298 _leave(" = %d", ret);
299 return ret;
300}
301
08e0e7c8
DH
302/*
303 * initiate a call
304 */
305int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
306 const struct afs_wait_mode *wait_mode)
307{
308 struct sockaddr_rxrpc srx;
309 struct rxrpc_call *rxcall;
310 struct msghdr msg;
311 struct kvec iov[1];
312 int ret;
313
314 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
315
00d3b7a4
DH
316 ASSERT(call->type != NULL);
317 ASSERT(call->type->name != NULL);
318
31143d5d
DH
319 _debug("____MAKE %p{%s,%x} [%d]____",
320 call, call->type->name, key_serial(call->key),
321 atomic_read(&afs_outstanding_calls));
00d3b7a4 322
08e0e7c8 323 call->wait_mode = wait_mode;
d001648e 324 INIT_WORK(&call->async_work, afs_process_async_call);
08e0e7c8
DH
325
326 memset(&srx, 0, sizeof(srx));
327 srx.srx_family = AF_RXRPC;
328 srx.srx_service = call->service_id;
329 srx.transport_type = SOCK_DGRAM;
330 srx.transport_len = sizeof(srx.transport.sin);
331 srx.transport.sin.sin_family = AF_INET;
332 srx.transport.sin.sin_port = call->port;
333 memcpy(&srx.transport.sin.sin_addr, addr, 4);
334
335 /* create a call */
336 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
d001648e
DH
337 (unsigned long) call, gfp,
338 wait_mode->notify_rx);
00d3b7a4 339 call->key = NULL;
08e0e7c8
DH
340 if (IS_ERR(rxcall)) {
341 ret = PTR_ERR(rxcall);
342 goto error_kill_call;
343 }
344
345 call->rxcall = rxcall;
346
347 /* send the request */
348 iov[0].iov_base = call->request;
349 iov[0].iov_len = call->request_size;
350
351 msg.msg_name = NULL;
352 msg.msg_namelen = 0;
2e90b1c4 353 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
c0371da6 354 call->request_size);
08e0e7c8
DH
355 msg.msg_control = NULL;
356 msg.msg_controllen = 0;
31143d5d 357 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
08e0e7c8
DH
358
359 /* have to change the state *before* sending the last packet as RxRPC
360 * might give us the reply before it returns from sending the
361 * request */
31143d5d
DH
362 if (!call->send_pages)
363 call->state = AFS_CALL_AWAIT_REPLY;
4de48af6
DH
364 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
365 &msg, call->request_size);
08e0e7c8
DH
366 if (ret < 0)
367 goto error_do_abort;
368
31143d5d
DH
369 if (call->send_pages) {
370 ret = afs_send_pages(call, &msg, iov);
371 if (ret < 0)
372 goto error_do_abort;
373 }
374
08e0e7c8
DH
375 /* at this point, an async call may no longer exist as it may have
376 * already completed */
377 return wait_mode->wait(call);
378
379error_do_abort:
5a42976d 380 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT, -ret, "KSD");
08e0e7c8 381error_kill_call:
6c67c7c3 382 afs_end_call(call);
08e0e7c8
DH
383 _leave(" = %d", ret);
384 return ret;
385}
386
08e0e7c8
DH
387/*
388 * deliver messages to a call
389 */
390static void afs_deliver_to_call(struct afs_call *call)
391{
08e0e7c8
DH
392 u32 abort_code;
393 int ret;
394
d001648e
DH
395 _enter("%s", call->type->name);
396
397 while (call->state == AFS_CALL_AWAIT_REPLY ||
398 call->state == AFS_CALL_AWAIT_OP_ID ||
399 call->state == AFS_CALL_AWAIT_REQUEST ||
400 call->state == AFS_CALL_AWAIT_ACK
401 ) {
402 if (call->state == AFS_CALL_AWAIT_ACK) {
403 size_t offset = 0;
404 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
405 NULL, 0, &offset, false,
406 &call->abort_code);
407 if (ret == -EINPROGRESS || ret == -EAGAIN)
408 return;
409 if (ret == 1) {
410 call->state = AFS_CALL_COMPLETE;
411 goto done;
08e0e7c8 412 }
d001648e 413 return;
08e0e7c8
DH
414 }
415
d001648e
DH
416 ret = call->type->deliver(call);
417 switch (ret) {
418 case 0:
419 if (call->state == AFS_CALL_AWAIT_REPLY)
420 call->state = AFS_CALL_COMPLETE;
421 goto done;
422 case -EINPROGRESS:
423 case -EAGAIN:
424 goto out;
425 case -ENOTCONN:
426 abort_code = RX_CALL_DEAD;
427 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 428 abort_code, -ret, "KNC");
d001648e
DH
429 goto do_abort;
430 case -ENOTSUPP:
431 abort_code = RX_INVALID_OPERATION;
432 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 433 abort_code, -ret, "KIV");
d001648e
DH
434 goto do_abort;
435 case -ENODATA:
436 case -EBADMSG:
437 case -EMSGSIZE:
438 default:
439 abort_code = RXGEN_CC_UNMARSHAL;
440 if (call->state != AFS_CALL_AWAIT_REPLY)
441 abort_code = RXGEN_SS_UNMARSHAL;
442 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 443 abort_code, EBADMSG, "KUM");
d001648e
DH
444 goto do_abort;
445 }
08e0e7c8
DH
446 }
447
d001648e
DH
448done:
449 if (call->state == AFS_CALL_COMPLETE && call->incoming)
450 afs_end_call(call);
451out:
08e0e7c8 452 _leave("");
d001648e
DH
453 return;
454
455do_abort:
456 call->error = ret;
457 call->state = AFS_CALL_COMPLETE;
458 goto done;
08e0e7c8
DH
459}
460
461/*
462 * wait synchronously for a call to complete
463 */
464static int afs_wait_for_call_to_complete(struct afs_call *call)
465{
5a42976d 466 const char *abort_why;
08e0e7c8
DH
467 int ret;
468
469 DECLARE_WAITQUEUE(myself, current);
470
471 _enter("");
472
473 add_wait_queue(&call->waitq, &myself);
474 for (;;) {
475 set_current_state(TASK_INTERRUPTIBLE);
476
477 /* deliver any messages that are in the queue */
d001648e
DH
478 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
479 call->need_attention = false;
08e0e7c8
DH
480 __set_current_state(TASK_RUNNING);
481 afs_deliver_to_call(call);
482 continue;
483 }
484
5a42976d 485 abort_why = "KWC";
08e0e7c8 486 ret = call->error;
d001648e 487 if (call->state == AFS_CALL_COMPLETE)
08e0e7c8 488 break;
5a42976d 489 abort_why = "KWI";
08e0e7c8
DH
490 ret = -EINTR;
491 if (signal_pending(current))
492 break;
493 schedule();
494 }
495
496 remove_wait_queue(&call->waitq, &myself);
497 __set_current_state(TASK_RUNNING);
498
499 /* kill the call */
500 if (call->state < AFS_CALL_COMPLETE) {
501 _debug("call incomplete");
d001648e 502 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 503 RX_CALL_DEAD, -ret, abort_why);
08e0e7c8
DH
504 }
505
506 _debug("call complete");
6c67c7c3 507 afs_end_call(call);
08e0e7c8
DH
508 _leave(" = %d", ret);
509 return ret;
510}
511
512/*
513 * wake up a waiting call
514 */
d001648e
DH
515static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
516 unsigned long call_user_ID)
08e0e7c8 517{
d001648e
DH
518 struct afs_call *call = (struct afs_call *)call_user_ID;
519
520 call->need_attention = true;
08e0e7c8
DH
521 wake_up(&call->waitq);
522}
523
524/*
525 * wake up an asynchronous call
526 */
d001648e
DH
527static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
528 unsigned long call_user_ID)
08e0e7c8 529{
d001648e
DH
530 struct afs_call *call = (struct afs_call *)call_user_ID;
531
532 call->need_attention = true;
08e0e7c8
DH
533 queue_work(afs_async_calls, &call->async_work);
534}
535
536/*
537 * put a call into asynchronous mode
538 * - mustn't touch the call descriptor as the call my have completed by the
539 * time we get here
540 */
541static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
542{
543 _enter("");
544 return -EINPROGRESS;
545}
546
547/*
548 * delete an asynchronous call
549 */
d001648e 550static void afs_delete_async_call(struct work_struct *work)
08e0e7c8 551{
d001648e
DH
552 struct afs_call *call = container_of(work, struct afs_call, async_work);
553
08e0e7c8
DH
554 _enter("");
555
00d3b7a4 556 afs_free_call(call);
08e0e7c8
DH
557
558 _leave("");
559}
560
561/*
562 * perform processing on an asynchronous call
08e0e7c8 563 */
d001648e 564static void afs_process_async_call(struct work_struct *work)
08e0e7c8 565{
d001648e
DH
566 struct afs_call *call = container_of(work, struct afs_call, async_work);
567
08e0e7c8
DH
568 _enter("");
569
d001648e
DH
570 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
571 call->need_attention = false;
08e0e7c8 572 afs_deliver_to_call(call);
d001648e 573 }
08e0e7c8 574
d001648e 575 if (call->state == AFS_CALL_COMPLETE && call->wait_mode) {
08e0e7c8
DH
576 if (call->wait_mode->async_complete)
577 call->wait_mode->async_complete(call->reply,
578 call->error);
579 call->reply = NULL;
580
581 /* kill the call */
6cf12869 582 afs_end_call_nofree(call);
08e0e7c8
DH
583
584 /* we can't just delete the call because the work item may be
585 * queued */
d001648e 586 call->async_work.func = afs_delete_async_call;
08e0e7c8
DH
587 queue_work(afs_async_calls, &call->async_work);
588 }
589
590 _leave("");
591}
592
08e0e7c8
DH
593/*
594 * accept the backlog of incoming calls
595 */
596static void afs_collect_incoming_call(struct work_struct *work)
597{
598 struct rxrpc_call *rxcall;
599 struct afs_call *call = NULL;
08e0e7c8 600
d001648e 601 _enter("");
08e0e7c8 602
d001648e 603 do {
08e0e7c8
DH
604 if (!call) {
605 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
606 if (!call) {
607 rxrpc_kernel_reject_call(afs_socket);
608 return;
609 }
610
d001648e 611 INIT_WORK(&call->async_work, afs_process_async_call);
08e0e7c8
DH
612 call->wait_mode = &afs_async_incoming_call;
613 call->type = &afs_RXCMxxxx;
614 init_waitqueue_head(&call->waitq);
08e0e7c8 615 call->state = AFS_CALL_AWAIT_OP_ID;
00d3b7a4
DH
616
617 _debug("CALL %p{%s} [%d]",
618 call, call->type->name,
619 atomic_read(&afs_outstanding_calls));
620 atomic_inc(&afs_outstanding_calls);
08e0e7c8
DH
621 }
622
623 rxcall = rxrpc_kernel_accept_call(afs_socket,
d001648e
DH
624 (unsigned long)call,
625 afs_wake_up_async_call);
08e0e7c8
DH
626 if (!IS_ERR(rxcall)) {
627 call->rxcall = rxcall;
d001648e
DH
628 call->need_attention = true;
629 queue_work(afs_async_calls, &call->async_work);
08e0e7c8
DH
630 call = NULL;
631 }
d001648e 632 } while (!call);
08e0e7c8 633
00d3b7a4
DH
634 if (call)
635 afs_free_call(call);
08e0e7c8
DH
636}
637
d001648e
DH
638/*
639 * Notification of an incoming call.
640 */
641static void afs_rx_new_call(struct sock *sk)
642{
643 queue_work(afs_wq, &afs_collect_incoming_call_work);
644}
645
08e0e7c8 646/*
372ee163
DH
647 * Grab the operation ID from an incoming cache manager call. The socket
648 * buffer is discarded on error or if we don't yet have sufficient data.
08e0e7c8 649 */
d001648e 650static int afs_deliver_cm_op_id(struct afs_call *call)
08e0e7c8 651{
d001648e 652 int ret;
08e0e7c8 653
d001648e 654 _enter("{%zu}", call->offset);
08e0e7c8
DH
655
656 ASSERTCMP(call->offset, <, 4);
657
658 /* the operation ID forms the first four bytes of the request data */
d001648e
DH
659 ret = afs_extract_data(call, &call->operation_ID, 4, true);
660 if (ret < 0)
661 return ret;
08e0e7c8
DH
662
663 call->state = AFS_CALL_AWAIT_REQUEST;
d001648e 664 call->offset = 0;
08e0e7c8
DH
665
666 /* ask the cache manager to route the call (it'll change the call type
667 * if successful) */
668 if (!afs_cm_incoming_call(call))
669 return -ENOTSUPP;
670
671 /* pass responsibility for the remainer of this message off to the
672 * cache manager op */
d001648e 673 return call->type->deliver(call);
08e0e7c8
DH
674}
675
676/*
677 * send an empty reply
678 */
679void afs_send_empty_reply(struct afs_call *call)
680{
681 struct msghdr msg;
08e0e7c8
DH
682
683 _enter("");
684
08e0e7c8
DH
685 msg.msg_name = NULL;
686 msg.msg_namelen = 0;
bfd4e956 687 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
08e0e7c8
DH
688 msg.msg_control = NULL;
689 msg.msg_controllen = 0;
690 msg.msg_flags = 0;
691
692 call->state = AFS_CALL_AWAIT_ACK;
4de48af6 693 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
08e0e7c8
DH
694 case 0:
695 _leave(" [replied]");
696 return;
697
698 case -ENOMEM:
699 _debug("oom");
4de48af6 700 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 701 RX_USER_ABORT, ENOMEM, "KOO");
08e0e7c8 702 default:
6c67c7c3 703 afs_end_call(call);
08e0e7c8
DH
704 _leave(" [error]");
705 return;
706 }
707}
708
b908fe6b
DH
709/*
710 * send a simple reply
711 */
712void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
713{
714 struct msghdr msg;
2e90b1c4 715 struct kvec iov[1];
bd6dc742 716 int n;
b908fe6b
DH
717
718 _enter("");
719
720 iov[0].iov_base = (void *) buf;
721 iov[0].iov_len = len;
722 msg.msg_name = NULL;
723 msg.msg_namelen = 0;
2e90b1c4 724 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
b908fe6b
DH
725 msg.msg_control = NULL;
726 msg.msg_controllen = 0;
727 msg.msg_flags = 0;
728
729 call->state = AFS_CALL_AWAIT_ACK;
4de48af6 730 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
bd6dc742 731 if (n >= 0) {
6c67c7c3 732 /* Success */
b908fe6b
DH
733 _leave(" [replied]");
734 return;
bd6dc742 735 }
6c67c7c3 736
bd6dc742 737 if (n == -ENOMEM) {
b908fe6b 738 _debug("oom");
4de48af6 739 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 740 RX_USER_ABORT, ENOMEM, "KOO");
b908fe6b 741 }
6c67c7c3 742 afs_end_call(call);
bd6dc742 743 _leave(" [error]");
b908fe6b
DH
744}
745
08e0e7c8 746/*
372ee163 747 * Extract a piece of data from the received data socket buffers.
08e0e7c8 748 */
d001648e
DH
749int afs_extract_data(struct afs_call *call, void *buf, size_t count,
750 bool want_more)
08e0e7c8 751{
d001648e 752 int ret;
08e0e7c8 753
d001648e
DH
754 _enter("{%s,%zu},,%zu,%d",
755 call->type->name, call->offset, count, want_more);
08e0e7c8 756
d001648e 757 ASSERTCMP(call->offset, <=, count);
08e0e7c8 758
d001648e
DH
759 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
760 buf, count, &call->offset,
761 want_more, &call->abort_code);
762 if (ret == 0 || ret == -EAGAIN)
763 return ret;
08e0e7c8 764
d001648e
DH
765 if (ret == 1) {
766 switch (call->state) {
767 case AFS_CALL_AWAIT_REPLY:
768 call->state = AFS_CALL_COMPLETE;
769 break;
770 case AFS_CALL_AWAIT_REQUEST:
771 call->state = AFS_CALL_REPLYING;
772 break;
773 default:
774 break;
775 }
776 return 0;
08e0e7c8 777 }
d001648e
DH
778
779 if (ret == -ECONNABORTED)
780 call->error = call->type->abort_to_error(call->abort_code);
781 else
782 call->error = ret;
783 call->state = AFS_CALL_COMPLETE;
784 return ret;
08e0e7c8 785}