rxrpc: Fix potential race in error handling in afs_make_call()
[linux-block.git] / fs / afs / rxrpc.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
08e0e7c8
DH
2/* Maintain an RxRPC server socket to do AFS communications through
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
08e0e7c8
DH
6 */
7
5a0e3ad6 8#include <linux/slab.h>
174cd4b1
IM
9#include <linux/sched/signal.h>
10
08e0e7c8
DH
11#include <net/sock.h>
12#include <net/af_rxrpc.h>
08e0e7c8
DH
13#include "internal.h"
14#include "afs_cm.h"
35dbfba3 15#include "protocol_yfs.h"
57af281e
DH
16#define RXRPC_TRACE_ONLY_DEFINE_ENUMS
17#include <trace/events/rxrpc.h>
08e0e7c8 18
f044c884 19struct workqueue_struct *afs_async_calls;
08e0e7c8 20
d001648e 21static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
d001648e 22static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
d001648e 23static void afs_process_async_call(struct work_struct *);
00e90712
DH
24static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
25static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
d001648e 26static int afs_deliver_cm_op_id(struct afs_call *);
08e0e7c8 27
08e0e7c8
DH
28/* asynchronous incoming call initial processing */
29static const struct afs_call_type afs_RXCMxxxx = {
00d3b7a4 30 .name = "CB.xxxx",
08e0e7c8 31 .deliver = afs_deliver_cm_op_id,
08e0e7c8
DH
32};
33
08e0e7c8
DH
34/*
35 * open an RxRPC socket and bind it to be a server for callback notifications
36 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
37 */
f044c884 38int afs_open_socket(struct afs_net *net)
08e0e7c8
DH
39{
40 struct sockaddr_rxrpc srx;
41 struct socket *socket;
42 int ret;
43
44 _enter("");
45
5b86d4ff 46 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
0e119b41
DH
47 if (ret < 0)
48 goto error_1;
08e0e7c8
DH
49
50 socket->sk->sk_allocation = GFP_NOFS;
51
52 /* bind the callback manager's address to make this a server socket */
3838d3ec 53 memset(&srx, 0, sizeof(srx));
08e0e7c8
DH
54 srx.srx_family = AF_RXRPC;
55 srx.srx_service = CM_SERVICE;
56 srx.transport_type = SOCK_DGRAM;
3838d3ec
DH
57 srx.transport_len = sizeof(srx.transport.sin6);
58 srx.transport.sin6.sin6_family = AF_INET6;
59 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
08e0e7c8 60
298cd88a
CH
61 ret = rxrpc_sock_set_min_security_level(socket->sk,
62 RXRPC_SECURITY_ENCRYPT);
4776cab4
DH
63 if (ret < 0)
64 goto error_2;
65
08e0e7c8 66 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
83732ec5
MD
67 if (ret == -EADDRINUSE) {
68 srx.transport.sin6.sin6_port = 0;
69 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
70 }
0e119b41
DH
71 if (ret < 0)
72 goto error_2;
73
35dbfba3
DH
74 srx.srx_service = YFS_CM_SERVICE;
75 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
76 if (ret < 0)
77 goto error_2;
78
3bf0fb6f
DH
79 /* Ideally, we'd turn on service upgrade here, but we can't because
80 * OpenAFS is buggy and leaks the userStatus field from packet to
81 * packet and between FS packets and CB packets - so if we try to do an
82 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
83 * it sends back to us.
84 */
35dbfba3 85
00e90712
DH
86 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
87 afs_rx_discard_new_call);
d001648e 88
0e119b41
DH
89 ret = kernel_listen(socket, INT_MAX);
90 if (ret < 0)
91 goto error_2;
08e0e7c8 92
f044c884
DH
93 net->socket = socket;
94 afs_charge_preallocation(&net->charge_preallocation_work);
08e0e7c8
DH
95 _leave(" = 0");
96 return 0;
0e119b41
DH
97
98error_2:
99 sock_release(socket);
100error_1:
0e119b41
DH
101 _leave(" = %d", ret);
102 return ret;
08e0e7c8
DH
103}
104
105/*
106 * close the RxRPC socket AFS was using
107 */
f044c884 108void afs_close_socket(struct afs_net *net)
08e0e7c8
DH
109{
110 _enter("");
111
f044c884 112 kernel_listen(net->socket, 0);
341f741f
DH
113 flush_workqueue(afs_async_calls);
114
f044c884
DH
115 if (net->spare_incoming_call) {
116 afs_put_call(net->spare_incoming_call);
117 net->spare_incoming_call = NULL;
00e90712
DH
118 }
119
f044c884 120 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
ab1fbe32
PZ
121 wait_var_event(&net->nr_outstanding_calls,
122 !atomic_read(&net->nr_outstanding_calls));
2f02f7ae
DH
123 _debug("no outstanding calls");
124
f044c884 125 kernel_sock_shutdown(net->socket, SHUT_RDWR);
d001648e 126 flush_workqueue(afs_async_calls);
f044c884 127 sock_release(net->socket);
08e0e7c8
DH
128
129 _debug("dework");
08e0e7c8
DH
130 _leave("");
131}
132
00d3b7a4 133/*
341f741f 134 * Allocate a call.
00d3b7a4 135 */
f044c884
DH
136static struct afs_call *afs_alloc_call(struct afs_net *net,
137 const struct afs_call_type *type,
341f741f 138 gfp_t gfp)
00d3b7a4 139{
341f741f
DH
140 struct afs_call *call;
141 int o;
00d3b7a4 142
341f741f
DH
143 call = kzalloc(sizeof(*call), gfp);
144 if (!call)
145 return NULL;
00d3b7a4 146
341f741f 147 call->type = type;
f044c884 148 call->net = net;
a25e21f0 149 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
c56f9ec8 150 refcount_set(&call->ref, 1);
341f741f
DH
151 INIT_WORK(&call->async_work, afs_process_async_call);
152 init_waitqueue_head(&call->waitq);
98bf40cd 153 spin_lock_init(&call->state_lock);
fc276122 154 call->iter = &call->def_iter;
2f02f7ae 155
f044c884 156 o = atomic_inc_return(&net->nr_outstanding_calls);
2757a4dc 157 trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o,
341f741f
DH
158 __builtin_return_address(0));
159 return call;
00d3b7a4
DH
160}
161
6c67c7c3 162/*
341f741f 163 * Dispose of a reference on a call.
6c67c7c3 164 */
341f741f 165void afs_put_call(struct afs_call *call)
6c67c7c3 166{
f044c884 167 struct afs_net *net = call->net;
2757a4dc 168 unsigned int debug_id = call->debug_id;
c56f9ec8
DH
169 bool zero;
170 int r, o;
341f741f 171
c56f9ec8
DH
172 zero = __refcount_dec_and_test(&call->ref, &r);
173 o = atomic_read(&net->nr_outstanding_calls);
2757a4dc 174 trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
341f741f
DH
175 __builtin_return_address(0));
176
c56f9ec8 177 if (zero) {
341f741f
DH
178 ASSERT(!work_pending(&call->async_work));
179 ASSERT(call->type->name != NULL);
180
181 if (call->rxcall) {
e0416e7d
DH
182 rxrpc_kernel_shutdown_call(net->socket, call->rxcall);
183 rxrpc_kernel_put_call(net->socket, call->rxcall);
341f741f
DH
184 call->rxcall = NULL;
185 }
186 if (call->type->destructor)
187 call->type->destructor(call);
188
977e5f8e 189 afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
3bf0fb6f 190 afs_put_addrlist(call->alist);
341f741f 191 kfree(call->request);
341f741f 192
2757a4dc 193 trace_afs_call(call->debug_id, afs_call_trace_free, 0, o,
341f741f 194 __builtin_return_address(0));
a25e21f0
DH
195 kfree(call);
196
197 o = atomic_dec_return(&net->nr_outstanding_calls);
341f741f 198 if (o == 0)
ab1fbe32 199 wake_up_var(&net->nr_outstanding_calls);
6c67c7c3 200 }
6cf12869
NWF
201}
202
7a75b007
DH
203static struct afs_call *afs_get_call(struct afs_call *call,
204 enum afs_call_trace why)
205{
c56f9ec8 206 int r;
7a75b007 207
c56f9ec8
DH
208 __refcount_inc(&call->ref, &r);
209
2757a4dc 210 trace_afs_call(call->debug_id, why, r + 1,
7a75b007
DH
211 atomic_read(&call->net->nr_outstanding_calls),
212 __builtin_return_address(0));
213 return call;
214}
215
6cf12869 216/*
3bf0fb6f 217 * Queue the call for actual work.
6cf12869 218 */
3bf0fb6f 219static void afs_queue_call_work(struct afs_call *call)
6cf12869 220{
3bf0fb6f 221 if (call->type->work) {
3bf0fb6f 222 INIT_WORK(&call->work, call->type->work);
341f741f 223
7a75b007 224 afs_get_call(call, afs_call_trace_work);
3bf0fb6f
DH
225 if (!queue_work(afs_wq, &call->work))
226 afs_put_call(call);
227 }
6c67c7c3
DH
228}
229
08e0e7c8
DH
230/*
231 * allocate a call with flat request and reply buffers
232 */
f044c884
DH
233struct afs_call *afs_alloc_flat_call(struct afs_net *net,
234 const struct afs_call_type *type,
d001648e 235 size_t request_size, size_t reply_max)
08e0e7c8
DH
236{
237 struct afs_call *call;
238
f044c884 239 call = afs_alloc_call(net, type, GFP_NOFS);
08e0e7c8
DH
240 if (!call)
241 goto nomem_call;
242
243 if (request_size) {
341f741f 244 call->request_size = request_size;
08e0e7c8
DH
245 call->request = kmalloc(request_size, GFP_NOFS);
246 if (!call->request)
00d3b7a4 247 goto nomem_free;
08e0e7c8
DH
248 }
249
d001648e 250 if (reply_max) {
341f741f 251 call->reply_max = reply_max;
d001648e 252 call->buffer = kmalloc(reply_max, GFP_NOFS);
08e0e7c8 253 if (!call->buffer)
00d3b7a4 254 goto nomem_free;
08e0e7c8
DH
255 }
256
12bdcf33 257 afs_extract_to_buf(call, call->reply_max);
025db80c 258 call->operation_ID = type->op;
08e0e7c8 259 init_waitqueue_head(&call->waitq);
08e0e7c8
DH
260 return call;
261
00d3b7a4 262nomem_free:
341f741f 263 afs_put_call(call);
08e0e7c8
DH
264nomem_call:
265 return NULL;
266}
267
268/*
269 * clean up a call with flat buffer
270 */
271void afs_flat_call_destructor(struct afs_call *call)
272{
273 _enter("");
274
275 kfree(call->request);
276 call->request = NULL;
277 kfree(call->buffer);
278 call->buffer = NULL;
279}
280
e833251a
DH
281/*
282 * Advance the AFS call state when the RxRPC call ends the transmit phase.
283 */
284static void afs_notify_end_request_tx(struct sock *sock,
285 struct rxrpc_call *rxcall,
286 unsigned long call_user_ID)
287{
288 struct afs_call *call = (struct afs_call *)call_user_ID;
289
98bf40cd 290 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
e833251a
DH
291}
292
08e0e7c8 293/*
0b9bf381
DH
294 * Initiate a call and synchronously queue up the parameters for dispatch. Any
295 * error is stored into the call struct, which the caller must check for.
08e0e7c8 296 */
0b9bf381 297void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
08e0e7c8 298{
2feeaf84 299 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
08e0e7c8
DH
300 struct rxrpc_call *rxcall;
301 struct msghdr msg;
302 struct kvec iov[1];
f105da1a 303 size_t len;
e754eba6 304 s64 tx_total_len;
08e0e7c8
DH
305 int ret;
306
4d9df986 307 _enter(",{%pISp},", &srx->transport);
08e0e7c8 308
00d3b7a4
DH
309 ASSERT(call->type != NULL);
310 ASSERT(call->type->name != NULL);
311
31143d5d
DH
312 _debug("____MAKE %p{%s,%x} [%d]____",
313 call, call->type->name, key_serial(call->key),
f044c884 314 atomic_read(&call->net->nr_outstanding_calls));
00d3b7a4 315
3bf0fb6f
DH
316 call->addr_ix = ac->index;
317 call->alist = afs_get_addrlist(ac->alist);
08e0e7c8 318
e754eba6
DH
319 /* Work out the length we're going to transmit. This is awkward for
320 * calls such as FS.StoreData where there's an extra injection of data
321 * after the initial fixed part.
322 */
323 tx_total_len = call->request_size;
bd80d8a8
DH
324 if (call->write_iter)
325 tx_total_len += iov_iter_count(call->write_iter);
e754eba6 326
34fa4761
DH
327 /* If the call is going to be asynchronous, we need an extra ref for
328 * the call to hold itself so the caller need not hang on to its ref.
329 */
dde9f095 330 if (call->async) {
34fa4761 331 afs_get_call(call, afs_call_trace_get);
dde9f095
DH
332 call->drop_ref = true;
333 }
34fa4761 334
08e0e7c8 335 /* create a call */
4d9df986 336 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
e754eba6
DH
337 (unsigned long)call,
338 tx_total_len, gfp,
0b9bf381 339 (call->async ?
56ff9c83 340 afs_wake_up_async_call :
a68f4a27 341 afs_wake_up_call_waiter),
a25e21f0 342 call->upgrade,
e138aa7d
DH
343 (call->intr ? RXRPC_PREINTERRUPTIBLE :
344 RXRPC_UNINTERRUPTIBLE),
a25e21f0 345 call->debug_id);
08e0e7c8
DH
346 if (IS_ERR(rxcall)) {
347 ret = PTR_ERR(rxcall);
3bf0fb6f 348 call->error = ret;
08e0e7c8
DH
349 goto error_kill_call;
350 }
351
352 call->rxcall = rxcall;
353
94f699c9
DH
354 if (call->max_lifespan)
355 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
356 call->max_lifespan);
7903192c 357 call->issue_time = ktime_get_real();
94f699c9 358
08e0e7c8
DH
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;
de4eda9d 365 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, call->request_size);
08e0e7c8
DH
366 msg.msg_control = NULL;
367 msg.msg_controllen = 0;
bd80d8a8 368 msg.msg_flags = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0);
08e0e7c8 369
f044c884 370 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
e833251a
DH
371 &msg, call->request_size,
372 afs_notify_end_request_tx);
08e0e7c8
DH
373 if (ret < 0)
374 goto error_do_abort;
375
bd80d8a8
DH
376 if (call->write_iter) {
377 msg.msg_iter = *call->write_iter;
378 msg.msg_flags &= ~MSG_MORE;
379 trace_afs_send_data(call, &msg);
380
381 ret = rxrpc_kernel_send_data(call->net->socket,
382 call->rxcall, &msg,
383 iov_iter_count(&msg.msg_iter),
384 afs_notify_end_request_tx);
385 *call->write_iter = msg.msg_iter;
386
387 trace_afs_sent_data(call, &msg, ret);
31143d5d
DH
388 if (ret < 0)
389 goto error_do_abort;
390 }
391
34fa4761
DH
392 /* Note that at this point, we may have received the reply or an abort
393 * - and an asynchronous call may already have completed.
0b9bf381
DH
394 *
395 * afs_wait_for_call_to_complete(call, ac)
396 * must be called to synchronously clean up.
34fa4761 397 */
0b9bf381 398 return;
08e0e7c8
DH
399
400error_do_abort:
70af0e3b 401 if (ret != -ECONNABORTED) {
f044c884 402 rxrpc_kernel_abort_call(call->net->socket, rxcall,
57af281e
DH
403 RX_USER_ABORT, ret,
404 afs_abort_send_data_error);
70af0e3b 405 } else {
f105da1a 406 len = 0;
de4eda9d 407 iov_iter_kvec(&msg.msg_iter, ITER_DEST, NULL, 0, 0);
eb9950eb 408 rxrpc_kernel_recv_data(call->net->socket, rxcall,
f105da1a 409 &msg.msg_iter, &len, false,
eb9950eb 410 &call->abort_code, &call->service_id);
d2ddc776
DH
411 ac->abort_code = call->abort_code;
412 ac->responded = true;
70af0e3b 413 }
025db80c
DH
414 call->error = ret;
415 trace_afs_call_done(call);
08e0e7c8 416error_kill_call:
3bf0fb6f
DH
417 if (call->type->done)
418 call->type->done(call);
34fa4761
DH
419
420 /* We need to dispose of the extra ref we grabbed for an async call.
421 * The call, however, might be queued on afs_async_calls and we need to
422 * make sure we don't get any more notifications that might requeue it.
423 */
e0416e7d
DH
424 if (call->rxcall)
425 rxrpc_kernel_shutdown_call(call->net->socket, call->rxcall);
34fa4761
DH
426 if (call->async) {
427 if (cancel_work_sync(&call->async_work))
428 afs_put_call(call);
429 afs_put_call(call);
430 }
431
d2ddc776 432 ac->error = ret;
34fa4761 433 call->state = AFS_CALL_COMPLETE;
08e0e7c8 434 _leave(" = %d", ret);
08e0e7c8
DH
435}
436
05092755
DH
437/*
438 * Log remote abort codes that indicate that we have a protocol disagreement
439 * with the server.
440 */
441static void afs_log_error(struct afs_call *call, s32 remote_abort)
442{
443 static int max = 0;
444 const char *msg;
445 int m;
446
447 switch (remote_abort) {
448 case RX_EOF: msg = "unexpected EOF"; break;
449 case RXGEN_CC_MARSHAL: msg = "client marshalling"; break;
450 case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling"; break;
451 case RXGEN_SS_MARSHAL: msg = "server marshalling"; break;
452 case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling"; break;
453 case RXGEN_DECODE: msg = "opcode decode"; break;
454 case RXGEN_SS_XDRFREE: msg = "server XDR cleanup"; break;
455 case RXGEN_CC_XDRFREE: msg = "client XDR cleanup"; break;
456 case -32: msg = "insufficient data"; break;
457 default:
458 return;
459 }
460
461 m = max;
462 if (m < 3) {
463 max = m + 1;
464 pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n",
465 msg, call->type->name,
466 &call->alist->addrs[call->addr_ix].transport);
467 }
468}
469
08e0e7c8
DH
470/*
471 * deliver messages to a call
472 */
473static void afs_deliver_to_call(struct afs_call *call)
474{
98bf40cd 475 enum afs_call_state state;
f105da1a 476 size_t len;
98bf40cd 477 u32 abort_code, remote_abort = 0;
08e0e7c8
DH
478 int ret;
479
d001648e
DH
480 _enter("%s", call->type->name);
481
98bf40cd
DH
482 while (state = READ_ONCE(call->state),
483 state == AFS_CALL_CL_AWAIT_REPLY ||
484 state == AFS_CALL_SV_AWAIT_OP_ID ||
485 state == AFS_CALL_SV_AWAIT_REQUEST ||
486 state == AFS_CALL_SV_AWAIT_ACK
d001648e 487 ) {
98bf40cd 488 if (state == AFS_CALL_SV_AWAIT_ACK) {
f105da1a 489 len = 0;
de4eda9d 490 iov_iter_kvec(&call->def_iter, ITER_DEST, NULL, 0, 0);
f044c884 491 ret = rxrpc_kernel_recv_data(call->net->socket,
fc276122 492 call->rxcall, &call->def_iter,
f105da1a 493 &len, false, &remote_abort,
a68f4a27 494 &call->service_id);
fc276122 495 trace_afs_receive_data(call, &call->def_iter, false, ret);
8e8d7f13 496
d001648e
DH
497 if (ret == -EINPROGRESS || ret == -EAGAIN)
498 return;
98bf40cd
DH
499 if (ret < 0 || ret == 1) {
500 if (ret == 1)
501 ret = 0;
025db80c 502 goto call_complete;
98bf40cd 503 }
d001648e 504 return;
08e0e7c8
DH
505 }
506
d001648e 507 ret = call->type->deliver(call);
98bf40cd 508 state = READ_ONCE(call->state);
38355eec
DH
509 if (ret == 0 && call->unmarshalling_error)
510 ret = -EBADMSG;
d001648e
DH
511 switch (ret) {
512 case 0:
3bf0fb6f 513 afs_queue_call_work(call);
f2686b09 514 if (state == AFS_CALL_CL_PROC_REPLY) {
20325960 515 if (call->op)
f2686b09 516 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
20325960 517 &call->op->server->flags);
025db80c 518 goto call_complete;
f2686b09 519 }
98bf40cd 520 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
d001648e
DH
521 goto done;
522 case -EINPROGRESS:
523 case -EAGAIN:
524 goto out;
70af0e3b 525 case -ECONNABORTED:
98bf40cd 526 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
05092755 527 afs_log_error(call, call->abort_code);
98bf40cd 528 goto done;
d001648e 529 case -ENOTSUPP:
1157f153 530 abort_code = RXGEN_OPCODE;
f044c884 531 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
57af281e
DH
532 abort_code, ret,
533 afs_abort_op_not_supported);
98bf40cd 534 goto local_abort;
4ac15ea5
DH
535 case -EIO:
536 pr_err("kAFS: Call %u in bad state %u\n",
537 call->debug_id, state);
df561f66 538 fallthrough;
d001648e
DH
539 case -ENODATA:
540 case -EBADMSG:
541 case -EMSGSIZE:
de696c47
DH
542 case -ENOMEM:
543 case -EFAULT:
d001648e 544 abort_code = RXGEN_CC_UNMARSHAL;
98bf40cd 545 if (state != AFS_CALL_CL_AWAIT_REPLY)
d001648e 546 abort_code = RXGEN_SS_UNMARSHAL;
f044c884 547 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
57af281e
DH
548 abort_code, ret,
549 afs_abort_unmarshal_error);
98bf40cd 550 goto local_abort;
8022c4b9 551 default:
de696c47 552 abort_code = RX_CALL_DEAD;
8022c4b9 553 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
57af281e
DH
554 abort_code, ret,
555 afs_abort_general_error);
8022c4b9 556 goto local_abort;
d001648e 557 }
08e0e7c8
DH
558 }
559
d001648e 560done:
3bf0fb6f
DH
561 if (call->type->done)
562 call->type->done(call);
d001648e 563out:
08e0e7c8 564 _leave("");
d001648e
DH
565 return;
566
98bf40cd
DH
567local_abort:
568 abort_code = 0;
025db80c 569call_complete:
98bf40cd
DH
570 afs_set_call_complete(call, ret, remote_abort);
571 state = AFS_CALL_COMPLETE;
d001648e 572 goto done;
08e0e7c8
DH
573}
574
575/*
0b9bf381 576 * Wait synchronously for a call to complete and clean up the call struct.
08e0e7c8 577 */
0b9bf381
DH
578long afs_wait_for_call_to_complete(struct afs_call *call,
579 struct afs_addr_cursor *ac)
08e0e7c8 580{
33cd7f2b 581 long ret;
f7f1dd31 582 bool rxrpc_complete = false;
08e0e7c8
DH
583
584 DECLARE_WAITQUEUE(myself, current);
585
586 _enter("");
587
0b9bf381
DH
588 ret = call->error;
589 if (ret < 0)
590 goto out;
591
08e0e7c8
DH
592 add_wait_queue(&call->waitq, &myself);
593 for (;;) {
bc5e3a54 594 set_current_state(TASK_UNINTERRUPTIBLE);
08e0e7c8
DH
595
596 /* deliver any messages that are in the queue */
98bf40cd
DH
597 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
598 call->need_attention) {
d001648e 599 call->need_attention = false;
08e0e7c8
DH
600 __set_current_state(TASK_RUNNING);
601 afs_deliver_to_call(call);
602 continue;
603 }
604
98bf40cd 605 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
08e0e7c8 606 break;
bc5e3a54 607
7d7587db 608 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
f7f1dd31
MD
609 /* rxrpc terminated the call. */
610 rxrpc_complete = true;
611 break;
612 }
613
7d7587db 614 schedule();
08e0e7c8
DH
615 }
616
617 remove_wait_queue(&call->waitq, &myself);
618 __set_current_state(TASK_RUNNING);
619
98bf40cd 620 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
f7f1dd31
MD
621 if (rxrpc_complete) {
622 afs_set_call_complete(call, call->error, call->abort_code);
623 } else {
624 /* Kill off the call if it's still live. */
625 _debug("call interrupted");
626 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
57af281e
DH
627 RX_USER_ABORT, -EINTR,
628 afs_abort_interrupted))
f7f1dd31
MD
629 afs_set_call_complete(call, -EINTR, 0);
630 }
08e0e7c8
DH
631 }
632
98bf40cd 633 spin_lock_bh(&call->state_lock);
d2ddc776
DH
634 ac->abort_code = call->abort_code;
635 ac->error = call->error;
98bf40cd 636 spin_unlock_bh(&call->state_lock);
d2ddc776
DH
637
638 ret = ac->error;
639 switch (ret) {
640 case 0:
ffba718e
DH
641 ret = call->ret0;
642 call->ret0 = 0;
643
df561f66 644 fallthrough;
d2ddc776
DH
645 case -ECONNABORTED:
646 ac->responded = true;
647 break;
33cd7f2b
DH
648 }
649
0b9bf381 650out:
08e0e7c8 651 _debug("call complete");
341f741f 652 afs_put_call(call);
33cd7f2b 653 _leave(" = %p", (void *)ret);
08e0e7c8
DH
654 return ret;
655}
656
657/*
658 * wake up a waiting call
659 */
d001648e
DH
660static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
661 unsigned long call_user_ID)
08e0e7c8 662{
d001648e
DH
663 struct afs_call *call = (struct afs_call *)call_user_ID;
664
665 call->need_attention = true;
08e0e7c8
DH
666 wake_up(&call->waitq);
667}
668
669/*
670 * wake up an asynchronous call
671 */
d001648e
DH
672static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
673 unsigned long call_user_ID)
08e0e7c8 674{
d001648e 675 struct afs_call *call = (struct afs_call *)call_user_ID;
c56f9ec8 676 int r;
d001648e 677
8e8d7f13 678 trace_afs_notify_call(rxcall, call);
d001648e 679 call->need_attention = true;
341f741f 680
c56f9ec8 681 if (__refcount_inc_not_zero(&call->ref, &r)) {
2757a4dc 682 trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
f044c884 683 atomic_read(&call->net->nr_outstanding_calls),
341f741f
DH
684 __builtin_return_address(0));
685
686 if (!queue_work(afs_async_calls, &call->async_work))
687 afs_put_call(call);
688 }
08e0e7c8
DH
689}
690
08e0e7c8 691/*
341f741f
DH
692 * Perform I/O processing on an asynchronous call. The work item carries a ref
693 * to the call struct that we either need to release or to pass on.
08e0e7c8 694 */
d001648e 695static void afs_process_async_call(struct work_struct *work)
08e0e7c8 696{
d001648e
DH
697 struct afs_call *call = container_of(work, struct afs_call, async_work);
698
08e0e7c8
DH
699 _enter("");
700
d001648e
DH
701 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
702 call->need_attention = false;
08e0e7c8 703 afs_deliver_to_call(call);
d001648e 704 }
08e0e7c8 705
341f741f 706 afs_put_call(call);
08e0e7c8
DH
707 _leave("");
708}
709
00e90712
DH
710static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
711{
712 struct afs_call *call = (struct afs_call *)user_call_ID;
713
714 call->rxcall = rxcall;
715}
716
717/*
718 * Charge the incoming call preallocation.
719 */
f044c884 720void afs_charge_preallocation(struct work_struct *work)
00e90712 721{
f044c884
DH
722 struct afs_net *net =
723 container_of(work, struct afs_net, charge_preallocation_work);
724 struct afs_call *call = net->spare_incoming_call;
00e90712
DH
725
726 for (;;) {
727 if (!call) {
f044c884 728 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
00e90712
DH
729 if (!call)
730 break;
731
dde9f095 732 call->drop_ref = true;
56ff9c83 733 call->async = true;
98bf40cd 734 call->state = AFS_CALL_SV_AWAIT_OP_ID;
56ff9c83 735 init_waitqueue_head(&call->waitq);
12bdcf33 736 afs_extract_to_tmp(call);
00e90712
DH
737 }
738
f044c884 739 if (rxrpc_kernel_charge_accept(net->socket,
00e90712
DH
740 afs_wake_up_async_call,
741 afs_rx_attach,
742 (unsigned long)call,
a25e21f0
DH
743 GFP_KERNEL,
744 call->debug_id) < 0)
00e90712
DH
745 break;
746 call = NULL;
747 }
f044c884 748 net->spare_incoming_call = call;
00e90712
DH
749}
750
751/*
752 * Discard a preallocated call when a socket is shut down.
753 */
754static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
755 unsigned long user_call_ID)
756{
757 struct afs_call *call = (struct afs_call *)user_call_ID;
758
00e90712 759 call->rxcall = NULL;
341f741f 760 afs_put_call(call);
00e90712
DH
761}
762
d001648e
DH
763/*
764 * Notification of an incoming call.
765 */
00e90712
DH
766static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
767 unsigned long user_call_ID)
d001648e 768{
f044c884
DH
769 struct afs_net *net = afs_sock2net(sk);
770
771 queue_work(afs_wq, &net->charge_preallocation_work);
d001648e
DH
772}
773
08e0e7c8 774/*
372ee163
DH
775 * Grab the operation ID from an incoming cache manager call. The socket
776 * buffer is discarded on error or if we don't yet have sufficient data.
08e0e7c8 777 */
d001648e 778static int afs_deliver_cm_op_id(struct afs_call *call)
08e0e7c8 779{
d001648e 780 int ret;
08e0e7c8 781
fc276122 782 _enter("{%zu}", iov_iter_count(call->iter));
08e0e7c8
DH
783
784 /* the operation ID forms the first four bytes of the request data */
12bdcf33 785 ret = afs_extract_data(call, true);
d001648e
DH
786 if (ret < 0)
787 return ret;
08e0e7c8 788
50a2c953 789 call->operation_ID = ntohl(call->tmp);
98bf40cd 790 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
08e0e7c8
DH
791
792 /* ask the cache manager to route the call (it'll change the call type
793 * if successful) */
794 if (!afs_cm_incoming_call(call))
795 return -ENOTSUPP;
796
8e8d7f13
DH
797 trace_afs_cb_call(call);
798
08e0e7c8
DH
799 /* pass responsibility for the remainer of this message off to the
800 * cache manager op */
d001648e 801 return call->type->deliver(call);
08e0e7c8
DH
802}
803
e833251a
DH
804/*
805 * Advance the AFS call state when an RxRPC service call ends the transmit
806 * phase.
807 */
808static void afs_notify_end_reply_tx(struct sock *sock,
809 struct rxrpc_call *rxcall,
810 unsigned long call_user_ID)
811{
812 struct afs_call *call = (struct afs_call *)call_user_ID;
813
98bf40cd 814 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
e833251a
DH
815}
816
08e0e7c8
DH
817/*
818 * send an empty reply
819 */
820void afs_send_empty_reply(struct afs_call *call)
821{
f044c884 822 struct afs_net *net = call->net;
08e0e7c8 823 struct msghdr msg;
08e0e7c8
DH
824
825 _enter("");
826
f044c884 827 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
e754eba6 828
08e0e7c8
DH
829 msg.msg_name = NULL;
830 msg.msg_namelen = 0;
de4eda9d 831 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0);
08e0e7c8
DH
832 msg.msg_control = NULL;
833 msg.msg_controllen = 0;
834 msg.msg_flags = 0;
835
f044c884 836 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
e833251a 837 afs_notify_end_reply_tx)) {
08e0e7c8
DH
838 case 0:
839 _leave(" [replied]");
840 return;
841
842 case -ENOMEM:
843 _debug("oom");
f044c884 844 rxrpc_kernel_abort_call(net->socket, call->rxcall,
57af281e
DH
845 RXGEN_SS_MARSHAL, -ENOMEM,
846 afs_abort_oom);
df561f66 847 fallthrough;
08e0e7c8 848 default:
08e0e7c8
DH
849 _leave(" [error]");
850 return;
851 }
852}
853
b908fe6b
DH
854/*
855 * send a simple reply
856 */
857void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
858{
f044c884 859 struct afs_net *net = call->net;
b908fe6b 860 struct msghdr msg;
2e90b1c4 861 struct kvec iov[1];
bd6dc742 862 int n;
b908fe6b
DH
863
864 _enter("");
865
f044c884 866 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
e754eba6 867
b908fe6b
DH
868 iov[0].iov_base = (void *) buf;
869 iov[0].iov_len = len;
870 msg.msg_name = NULL;
871 msg.msg_namelen = 0;
de4eda9d 872 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, len);
b908fe6b
DH
873 msg.msg_control = NULL;
874 msg.msg_controllen = 0;
875 msg.msg_flags = 0;
876
f044c884 877 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
e833251a 878 afs_notify_end_reply_tx);
bd6dc742 879 if (n >= 0) {
6c67c7c3 880 /* Success */
b908fe6b
DH
881 _leave(" [replied]");
882 return;
bd6dc742 883 }
6c67c7c3 884
bd6dc742 885 if (n == -ENOMEM) {
b908fe6b 886 _debug("oom");
f044c884 887 rxrpc_kernel_abort_call(net->socket, call->rxcall,
57af281e
DH
888 RXGEN_SS_MARSHAL, -ENOMEM,
889 afs_abort_oom);
b908fe6b 890 }
bd6dc742 891 _leave(" [error]");
b908fe6b
DH
892}
893
08e0e7c8 894/*
372ee163 895 * Extract a piece of data from the received data socket buffers.
08e0e7c8 896 */
12bdcf33 897int afs_extract_data(struct afs_call *call, bool want_more)
08e0e7c8 898{
f044c884 899 struct afs_net *net = call->net;
fc276122 900 struct iov_iter *iter = call->iter;
98bf40cd 901 enum afs_call_state state;
7888da95 902 u32 remote_abort = 0;
d001648e 903 int ret;
08e0e7c8 904
f105da1a
DH
905 _enter("{%s,%zu,%zu},%d",
906 call->type->name, call->iov_len, iov_iter_count(iter), want_more);
eb9950eb 907
12bdcf33 908 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
f105da1a 909 &call->iov_len, want_more, &remote_abort,
a68f4a27 910 &call->service_id);
93368b6b 911 trace_afs_receive_data(call, call->iter, want_more, ret);
d001648e
DH
912 if (ret == 0 || ret == -EAGAIN)
913 return ret;
08e0e7c8 914
98bf40cd 915 state = READ_ONCE(call->state);
d001648e 916 if (ret == 1) {
98bf40cd
DH
917 switch (state) {
918 case AFS_CALL_CL_AWAIT_REPLY:
919 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
d001648e 920 break;
98bf40cd
DH
921 case AFS_CALL_SV_AWAIT_REQUEST:
922 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
d001648e 923 break;
98bf40cd
DH
924 case AFS_CALL_COMPLETE:
925 kdebug("prem complete %d", call->error);
f51375cd 926 return afs_io_error(call, afs_io_error_extract);
d001648e
DH
927 default:
928 break;
929 }
930 return 0;
08e0e7c8 931 }
d001648e 932
98bf40cd 933 afs_set_call_complete(call, ret, remote_abort);
d001648e 934 return ret;
08e0e7c8 935}
5f702c8e
DH
936
937/*
938 * Log protocol error production.
939 */
7126ead9 940noinline int afs_protocol_error(struct afs_call *call,
160cb957 941 enum afs_eproto_cause cause)
5f702c8e 942{
7126ead9 943 trace_afs_protocol_error(call, cause);
38355eec
DH
944 if (call)
945 call->unmarshalling_error = true;
7126ead9 946 return -EBADMSG;
5f702c8e 947}