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