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