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