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