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