blob: ed1644e7683f47445b48d5c78a34b0db30b3dd6d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells08e0e7c2007-04-26 15:55:03 -07002/* 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)
David Howells08e0e7c2007-04-26 15:55:03 -07006 */
7
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +01009#include <linux/sched/signal.h>
10
David Howells08e0e7c2007-04-26 15:55:03 -070011#include <net/sock.h>
12#include <net/af_rxrpc.h>
David Howells08e0e7c2007-04-26 15:55:03 -070013#include "internal.h"
14#include "afs_cm.h"
David Howells35dbfba2018-10-20 00:57:58 +010015#include "protocol_yfs.h"
David Howells57af2812022-10-06 21:45:42 +010016#define RXRPC_TRACE_ONLY_DEFINE_ENUMS
17#include <trace/events/rxrpc.h>
David Howells08e0e7c2007-04-26 15:55:03 -070018
David Howellsf044c882017-11-02 15:27:45 +000019struct workqueue_struct *afs_async_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070020
David Howellsd0016482016-08-30 20:42:14 +010021static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010022static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010023static void afs_process_async_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010024static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
25static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010026static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070027
David Howells08e0e7c2007-04-26 15:55:03 -070028/* asynchronous incoming call initial processing */
29static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070030 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070031 .deliver = afs_deliver_cm_op_id,
David Howells08e0e7c2007-04-26 15:55:03 -070032};
33
David Howells08e0e7c2007-04-26 15:55:03 -070034/*
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 */
David Howellsf044c882017-11-02 15:27:45 +000038int afs_open_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070039{
40 struct sockaddr_rxrpc srx;
41 struct socket *socket;
42 int ret;
43
44 _enter("");
45
David Howells5b86d4f2018-05-18 11:46:15 +010046 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
David Howells0e119b42016-06-10 22:30:37 +010047 if (ret < 0)
48 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070049
50 socket->sk->sk_allocation = GFP_NOFS;
51
52 /* bind the callback manager's address to make this a server socket */
David Howells3838d3e2017-11-02 15:27:47 +000053 memset(&srx, 0, sizeof(srx));
David Howells08e0e7c2007-04-26 15:55:03 -070054 srx.srx_family = AF_RXRPC;
55 srx.srx_service = CM_SERVICE;
56 srx.transport_type = SOCK_DGRAM;
David Howells3838d3e2017-11-02 15:27:47 +000057 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);
David Howells08e0e7c2007-04-26 15:55:03 -070060
Christoph Hellwig298cd882020-05-28 07:12:35 +020061 ret = rxrpc_sock_set_min_security_level(socket->sk,
62 RXRPC_SECURITY_ENCRYPT);
David Howells4776cab2018-05-10 23:10:40 +010063 if (ret < 0)
64 goto error_2;
65
David Howells08e0e7c2007-04-26 15:55:03 -070066 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
Marc Dionne83732ec2017-11-02 15:27:52 +000067 if (ret == -EADDRINUSE) {
68 srx.transport.sin6.sin6_port = 0;
69 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
70 }
David Howells0e119b42016-06-10 22:30:37 +010071 if (ret < 0)
72 goto error_2;
73
David Howells35dbfba2018-10-20 00:57:58 +010074 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
David Howells3bf0fb62018-10-20 00:57:59 +010079 /* 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 */
David Howells35dbfba2018-10-20 00:57:58 +010085
David Howells00e90712016-09-08 11:10:12 +010086 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
87 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010088
David Howells0e119b42016-06-10 22:30:37 +010089 ret = kernel_listen(socket, INT_MAX);
90 if (ret < 0)
91 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070092
David Howellsf044c882017-11-02 15:27:45 +000093 net->socket = socket;
94 afs_charge_preallocation(&net->charge_preallocation_work);
David Howells08e0e7c2007-04-26 15:55:03 -070095 _leave(" = 0");
96 return 0;
David Howells0e119b42016-06-10 22:30:37 +010097
98error_2:
99 sock_release(socket);
100error_1:
David Howells0e119b42016-06-10 22:30:37 +0100101 _leave(" = %d", ret);
102 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700103}
104
105/*
106 * close the RxRPC socket AFS was using
107 */
David Howellsf044c882017-11-02 15:27:45 +0000108void afs_close_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -0700109{
110 _enter("");
111
David Howellsf044c882017-11-02 15:27:45 +0000112 kernel_listen(net->socket, 0);
David Howells341f7412017-01-05 10:38:36 +0000113 flush_workqueue(afs_async_calls);
114
David Howellsf044c882017-11-02 15:27:45 +0000115 if (net->spare_incoming_call) {
116 afs_put_call(net->spare_incoming_call);
117 net->spare_incoming_call = NULL;
David Howells00e90712016-09-08 11:10:12 +0100118 }
119
David Howellsf044c882017-11-02 15:27:45 +0000120 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100121 wait_var_event(&net->nr_outstanding_calls,
122 !atomic_read(&net->nr_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100123 _debug("no outstanding calls");
124
David Howellsf044c882017-11-02 15:27:45 +0000125 kernel_sock_shutdown(net->socket, SHUT_RDWR);
David Howells248f2192016-09-08 11:10:12 +0100126 flush_workqueue(afs_async_calls);
David Howellsf044c882017-11-02 15:27:45 +0000127 sock_release(net->socket);
David Howells08e0e7c2007-04-26 15:55:03 -0700128
129 _debug("dework");
David Howells08e0e7c2007-04-26 15:55:03 -0700130 _leave("");
131}
132
133/*
David Howells341f7412017-01-05 10:38:36 +0000134 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700135 */
David Howellsf044c882017-11-02 15:27:45 +0000136static struct afs_call *afs_alloc_call(struct afs_net *net,
137 const struct afs_call_type *type,
David Howells341f7412017-01-05 10:38:36 +0000138 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700139{
David Howells341f7412017-01-05 10:38:36 +0000140 struct afs_call *call;
141 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700142
David Howells341f7412017-01-05 10:38:36 +0000143 call = kzalloc(sizeof(*call), gfp);
144 if (!call)
145 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700146
David Howells341f7412017-01-05 10:38:36 +0000147 call->type = type;
David Howellsf044c882017-11-02 15:27:45 +0000148 call->net = net;
David Howellsa25e21f2018-03-27 23:03:00 +0100149 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howellsc56f9ec82022-07-06 10:52:14 +0100150 refcount_set(&call->ref, 1);
David Howells341f7412017-01-05 10:38:36 +0000151 INIT_WORK(&call->async_work, afs_process_async_call);
152 init_waitqueue_head(&call->waitq);
David Howells98bf40c2017-11-02 15:27:53 +0000153 spin_lock_init(&call->state_lock);
David Howellsfc276122019-11-21 09:12:17 +0000154 call->iter = &call->def_iter;
David Howells2f02f7a2016-04-07 17:23:03 +0100155
David Howellsf044c882017-11-02 15:27:45 +0000156 o = atomic_inc_return(&net->nr_outstanding_calls);
David Howells2757a4d2022-07-06 11:26:14 +0100157 trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o,
David Howells341f7412017-01-05 10:38:36 +0000158 __builtin_return_address(0));
159 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700160}
161
162/*
David Howells341f7412017-01-05 10:38:36 +0000163 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100164 */
David Howells341f7412017-01-05 10:38:36 +0000165void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100166{
David Howellsf044c882017-11-02 15:27:45 +0000167 struct afs_net *net = call->net;
David Howells2757a4d2022-07-06 11:26:14 +0100168 unsigned int debug_id = call->debug_id;
David Howellsc56f9ec82022-07-06 10:52:14 +0100169 bool zero;
170 int r, o;
David Howells341f7412017-01-05 10:38:36 +0000171
David Howellsc56f9ec82022-07-06 10:52:14 +0100172 zero = __refcount_dec_and_test(&call->ref, &r);
173 o = atomic_read(&net->nr_outstanding_calls);
David Howells2757a4d2022-07-06 11:26:14 +0100174 trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
David Howells341f7412017-01-05 10:38:36 +0000175 __builtin_return_address(0));
176
David Howellsc56f9ec82022-07-06 10:52:14 +0100177 if (zero) {
David Howells341f7412017-01-05 10:38:36 +0000178 ASSERT(!work_pending(&call->async_work));
179 ASSERT(call->type->name != NULL);
180
181 if (call->rxcall) {
David Howellse0416e72023-04-21 23:03:46 +0100182 rxrpc_kernel_shutdown_call(net->socket, call->rxcall);
183 rxrpc_kernel_put_call(net->socket, call->rxcall);
David Howells341f7412017-01-05 10:38:36 +0000184 call->rxcall = NULL;
185 }
186 if (call->type->destructor)
187 call->type->destructor(call);
188
David Howells977e5f82020-04-17 17:31:26 +0100189 afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
David Howells3bf0fb62018-10-20 00:57:59 +0100190 afs_put_addrlist(call->alist);
David Howells341f7412017-01-05 10:38:36 +0000191 kfree(call->request);
David Howellsa25e21f2018-03-27 23:03:00 +0100192
David Howells2757a4d2022-07-06 11:26:14 +0100193 trace_afs_call(call->debug_id, afs_call_trace_free, 0, o,
David Howellsa25e21f2018-03-27 23:03:00 +0100194 __builtin_return_address(0));
David Howells341f7412017-01-05 10:38:36 +0000195 kfree(call);
196
David Howellsf044c882017-11-02 15:27:45 +0000197 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000198 if (o == 0)
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100199 wake_up_var(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100200 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100201}
202
David Howells7a75b002019-01-10 15:14:29 +0000203static struct afs_call *afs_get_call(struct afs_call *call,
204 enum afs_call_trace why)
205{
David Howellsc56f9ec82022-07-06 10:52:14 +0100206 int r;
David Howells7a75b002019-01-10 15:14:29 +0000207
David Howellsc56f9ec82022-07-06 10:52:14 +0100208 __refcount_inc(&call->ref, &r);
209
David Howells2757a4d2022-07-06 11:26:14 +0100210 trace_afs_call(call->debug_id, why, r + 1,
David Howells7a75b002019-01-10 15:14:29 +0000211 atomic_read(&call->net->nr_outstanding_calls),
212 __builtin_return_address(0));
213 return call;
214}
215
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100216/*
David Howells3bf0fb62018-10-20 00:57:59 +0100217 * Queue the call for actual work.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100218 */
David Howells3bf0fb62018-10-20 00:57:59 +0100219static void afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100220{
David Howells3bf0fb62018-10-20 00:57:59 +0100221 if (call->type->work) {
David Howells3bf0fb62018-10-20 00:57:59 +0100222 INIT_WORK(&call->work, call->type->work);
David Howells341f7412017-01-05 10:38:36 +0000223
David Howells7a75b002019-01-10 15:14:29 +0000224 afs_get_call(call, afs_call_trace_work);
David Howells3bf0fb62018-10-20 00:57:59 +0100225 if (!queue_work(afs_wq, &call->work))
226 afs_put_call(call);
227 }
David Howells6c67c7c2014-05-21 14:48:05 +0100228}
229
230/*
David Howells08e0e7c2007-04-26 15:55:03 -0700231 * allocate a call with flat request and reply buffers
232 */
David Howellsf044c882017-11-02 15:27:45 +0000233struct afs_call *afs_alloc_flat_call(struct afs_net *net,
234 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100235 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700236{
237 struct afs_call *call;
238
David Howellsf044c882017-11-02 15:27:45 +0000239 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700240 if (!call)
241 goto nomem_call;
242
David Howells00d3b7a2007-04-26 15:57:07 -0700243 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000244 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700245 call->request = kmalloc(request_size, GFP_NOFS);
246 if (!call->request)
247 goto nomem_free;
248 }
249
David Howellsd0016482016-08-30 20:42:14 +0100250 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000251 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100252 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700253 if (!call->buffer)
254 goto nomem_free;
255 }
256
David Howells12bdcf32018-10-20 00:57:56 +0100257 afs_extract_to_buf(call, call->reply_max);
David Howells025db802017-11-02 15:27:51 +0000258 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700259 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700260 return call;
261
David Howells00d3b7a2007-04-26 15:57:07 -0700262nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000263 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700264nomem_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
281/*
David Howellse8332512017-08-29 10:18:56 +0100282 * 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
David Howells98bf40c2017-11-02 15:27:53 +0000290 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
David Howellse8332512017-08-29 10:18:56 +0100291}
292
293/*
David Howells0b9bf382019-04-25 14:26:50 +0100294 * 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.
David Howells08e0e7c2007-04-26 15:55:03 -0700296 */
David Howells0b9bf382019-04-25 14:26:50 +0100297void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
David Howells08e0e7c2007-04-26 15:55:03 -0700298{
David Howells2feeaf82018-10-20 00:57:59 +0100299 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
David Howells08e0e7c2007-04-26 15:55:03 -0700300 struct rxrpc_call *rxcall;
301 struct msghdr msg;
302 struct kvec iov[1];
David Howellsf105da12020-02-06 14:22:28 +0000303 size_t len;
David Howellse754eba2017-06-07 12:40:03 +0100304 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700305 int ret;
306
David Howells4d9df982017-11-02 15:27:47 +0000307 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700308
David Howells00d3b7a2007-04-26 15:57:07 -0700309 ASSERT(call->type != NULL);
310 ASSERT(call->type->name != NULL);
311
David Howells31143d52007-05-09 02:33:46 -0700312 _debug("____MAKE %p{%s,%x} [%d]____",
313 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000314 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700315
David Howells3bf0fb62018-10-20 00:57:59 +0100316 call->addr_ix = ac->index;
317 call->alist = afs_get_addrlist(ac->alist);
David Howells08e0e7c2007-04-26 15:55:03 -0700318
David Howellse754eba2017-06-07 12:40:03 +0100319 /* 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;
David Howellsbd80d8a2020-02-06 14:22:28 +0000324 if (call->write_iter)
325 tx_total_len += iov_iter_count(call->write_iter);
David Howellse754eba2017-06-07 12:40:03 +0100326
David Howells34fa4762019-01-10 15:40:50 +0000327 /* 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 */
David Howellsdde9f092020-03-13 13:46:08 +0000330 if (call->async) {
David Howells34fa4762019-01-10 15:40:50 +0000331 afs_get_call(call, afs_call_trace_get);
David Howellsdde9f092020-03-13 13:46:08 +0000332 call->drop_ref = true;
333 }
David Howells34fa4762019-01-10 15:40:50 +0000334
David Howells08e0e7c2007-04-26 15:55:03 -0700335 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000336 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100337 (unsigned long)call,
David Howellsdb099c62023-04-28 21:27:56 +0100338 tx_total_len,
339 call->max_lifespan,
340 gfp,
David Howells0b9bf382019-04-25 14:26:50 +0100341 (call->async ?
David Howells56ff9c82017-01-05 10:38:36 +0000342 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100343 afs_wake_up_call_waiter),
David Howellsa25e21f2018-03-27 23:03:00 +0100344 call->upgrade,
David Howellse138aa72020-03-13 09:22:09 +0000345 (call->intr ? RXRPC_PREINTERRUPTIBLE :
346 RXRPC_UNINTERRUPTIBLE),
David Howellsa25e21f2018-03-27 23:03:00 +0100347 call->debug_id);
David Howells08e0e7c2007-04-26 15:55:03 -0700348 if (IS_ERR(rxcall)) {
349 ret = PTR_ERR(rxcall);
David Howells3bf0fb62018-10-20 00:57:59 +0100350 call->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700351 goto error_kill_call;
352 }
353
354 call->rxcall = rxcall;
David Howells79031922022-08-31 13:16:42 +0100355 call->issue_time = ktime_get_real();
David Howells94f699c2019-05-16 13:21:59 +0100356
David Howells08e0e7c2007-04-26 15:55:03 -0700357 /* send the request */
358 iov[0].iov_base = call->request;
359 iov[0].iov_len = call->request_size;
360
361 msg.msg_name = NULL;
362 msg.msg_namelen = 0;
Al Virode4eda92022-09-15 20:25:47 -0400363 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700364 msg.msg_control = NULL;
365 msg.msg_controllen = 0;
David Howellsbd80d8a2020-02-06 14:22:28 +0000366 msg.msg_flags = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700367
David Howellsf044c882017-11-02 15:27:45 +0000368 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100369 &msg, call->request_size,
370 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700371 if (ret < 0)
372 goto error_do_abort;
373
David Howellsbd80d8a2020-02-06 14:22:28 +0000374 if (call->write_iter) {
375 msg.msg_iter = *call->write_iter;
376 msg.msg_flags &= ~MSG_MORE;
377 trace_afs_send_data(call, &msg);
378
379 ret = rxrpc_kernel_send_data(call->net->socket,
380 call->rxcall, &msg,
381 iov_iter_count(&msg.msg_iter),
382 afs_notify_end_request_tx);
383 *call->write_iter = msg.msg_iter;
384
385 trace_afs_sent_data(call, &msg, ret);
David Howells31143d52007-05-09 02:33:46 -0700386 if (ret < 0)
387 goto error_do_abort;
388 }
389
David Howells34fa4762019-01-10 15:40:50 +0000390 /* Note that at this point, we may have received the reply or an abort
391 * - and an asynchronous call may already have completed.
David Howells0b9bf382019-04-25 14:26:50 +0100392 *
393 * afs_wait_for_call_to_complete(call, ac)
394 * must be called to synchronously clean up.
David Howells34fa4762019-01-10 15:40:50 +0000395 */
David Howells0b9bf382019-04-25 14:26:50 +0100396 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700397
398error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000399 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000400 rxrpc_kernel_abort_call(call->net->socket, rxcall,
David Howells57af2812022-10-06 21:45:42 +0100401 RX_USER_ABORT, ret,
402 afs_abort_send_data_error);
David Howells70af0e32017-03-16 16:27:47 +0000403 } else {
David Howellsf105da12020-02-06 14:22:28 +0000404 len = 0;
Al Virode4eda92022-09-15 20:25:47 -0400405 iov_iter_kvec(&msg.msg_iter, ITER_DEST, NULL, 0, 0);
David Howellseb9950e2018-08-03 17:06:56 +0100406 rxrpc_kernel_recv_data(call->net->socket, rxcall,
David Howellsf105da12020-02-06 14:22:28 +0000407 &msg.msg_iter, &len, false,
David Howellseb9950e2018-08-03 17:06:56 +0100408 &call->abort_code, &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000409 ac->abort_code = call->abort_code;
410 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000411 }
David Howells025db802017-11-02 15:27:51 +0000412 call->error = ret;
413 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700414error_kill_call:
David Howells3bf0fb62018-10-20 00:57:59 +0100415 if (call->type->done)
416 call->type->done(call);
David Howells34fa4762019-01-10 15:40:50 +0000417
418 /* We need to dispose of the extra ref we grabbed for an async call.
419 * The call, however, might be queued on afs_async_calls and we need to
420 * make sure we don't get any more notifications that might requeue it.
421 */
David Howellse0416e72023-04-21 23:03:46 +0100422 if (call->rxcall)
423 rxrpc_kernel_shutdown_call(call->net->socket, call->rxcall);
David Howells34fa4762019-01-10 15:40:50 +0000424 if (call->async) {
425 if (cancel_work_sync(&call->async_work))
426 afs_put_call(call);
427 afs_put_call(call);
428 }
429
David Howellsd2ddc772017-11-02 15:27:50 +0000430 ac->error = ret;
David Howells34fa4762019-01-10 15:40:50 +0000431 call->state = AFS_CALL_COMPLETE;
David Howells08e0e7c2007-04-26 15:55:03 -0700432 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700433}
434
435/*
David Howells05092752020-06-07 21:50:29 +0100436 * 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
468/*
David Howells08e0e7c2007-04-26 15:55:03 -0700469 * deliver messages to a call
470 */
471static void afs_deliver_to_call(struct afs_call *call)
472{
David Howells98bf40c2017-11-02 15:27:53 +0000473 enum afs_call_state state;
David Howellsf105da12020-02-06 14:22:28 +0000474 size_t len;
David Howells98bf40c2017-11-02 15:27:53 +0000475 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700476 int ret;
477
David Howellsd0016482016-08-30 20:42:14 +0100478 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700479
David Howells98bf40c2017-11-02 15:27:53 +0000480 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
David Howellsd0016482016-08-30 20:42:14 +0100485 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000486 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howellsf105da12020-02-06 14:22:28 +0000487 len = 0;
Al Virode4eda92022-09-15 20:25:47 -0400488 iov_iter_kvec(&call->def_iter, ITER_DEST, NULL, 0, 0);
David Howellsf044c882017-11-02 15:27:45 +0000489 ret = rxrpc_kernel_recv_data(call->net->socket,
David Howellsfc276122019-11-21 09:12:17 +0000490 call->rxcall, &call->def_iter,
David Howellsf105da12020-02-06 14:22:28 +0000491 &len, false, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100492 &call->service_id);
David Howellsfc276122019-11-21 09:12:17 +0000493 trace_afs_receive_data(call, &call->def_iter, false, ret);
David Howells8e8d7f12017-01-05 10:38:34 +0000494
David Howellsd0016482016-08-30 20:42:14 +0100495 if (ret == -EINPROGRESS || ret == -EAGAIN)
496 return;
David Howells98bf40c2017-11-02 15:27:53 +0000497 if (ret < 0 || ret == 1) {
498 if (ret == 1)
499 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000500 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000501 }
David Howellsd0016482016-08-30 20:42:14 +0100502 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700503 }
504
David Howellsd0016482016-08-30 20:42:14 +0100505 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000506 state = READ_ONCE(call->state);
David Howells38355ee2020-04-08 16:13:20 +0100507 if (ret == 0 && call->unmarshalling_error)
508 ret = -EBADMSG;
David Howellsd0016482016-08-30 20:42:14 +0100509 switch (ret) {
510 case 0:
David Howells3bf0fb62018-10-20 00:57:59 +0100511 afs_queue_call_work(call);
David Howellsf2686b02018-05-10 14:12:50 +0100512 if (state == AFS_CALL_CL_PROC_REPLY) {
David Howells20325962020-04-30 01:03:49 +0100513 if (call->op)
David Howellsf2686b02018-05-10 14:12:50 +0100514 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
David Howells20325962020-04-30 01:03:49 +0100515 &call->op->server->flags);
David Howells025db802017-11-02 15:27:51 +0000516 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100517 }
David Howells98bf40c2017-11-02 15:27:53 +0000518 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100519 goto done;
520 case -EINPROGRESS:
521 case -EAGAIN:
522 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000523 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000524 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
David Howells05092752020-06-07 21:50:29 +0100525 afs_log_error(call, call->abort_code);
David Howells98bf40c2017-11-02 15:27:53 +0000526 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100527 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000528 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000529 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells57af2812022-10-06 21:45:42 +0100530 abort_code, ret,
531 afs_abort_op_not_supported);
David Howells98bf40c2017-11-02 15:27:53 +0000532 goto local_abort;
David Howells4ac15ea2018-10-20 00:57:57 +0100533 case -EIO:
534 pr_err("kAFS: Call %u in bad state %u\n",
535 call->debug_id, state);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500536 fallthrough;
David Howellsd0016482016-08-30 20:42:14 +0100537 case -ENODATA:
538 case -EBADMSG:
539 case -EMSGSIZE:
David Howellsde696c472022-05-21 08:45:48 +0100540 case -ENOMEM:
541 case -EFAULT:
David Howellsd0016482016-08-30 20:42:14 +0100542 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000543 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100544 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000545 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells57af2812022-10-06 21:45:42 +0100546 abort_code, ret,
547 afs_abort_unmarshal_error);
David Howells98bf40c2017-11-02 15:27:53 +0000548 goto local_abort;
David Howells8022c4b2019-04-13 08:37:37 +0100549 default:
David Howellsde696c472022-05-21 08:45:48 +0100550 abort_code = RX_CALL_DEAD;
David Howells8022c4b2019-04-13 08:37:37 +0100551 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells57af2812022-10-06 21:45:42 +0100552 abort_code, ret,
553 afs_abort_general_error);
David Howells8022c4b2019-04-13 08:37:37 +0100554 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100555 }
David Howells08e0e7c2007-04-26 15:55:03 -0700556 }
557
David Howellsd0016482016-08-30 20:42:14 +0100558done:
David Howells3bf0fb62018-10-20 00:57:59 +0100559 if (call->type->done)
560 call->type->done(call);
David Howellsd0016482016-08-30 20:42:14 +0100561out:
David Howells08e0e7c2007-04-26 15:55:03 -0700562 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100563 return;
564
David Howells98bf40c2017-11-02 15:27:53 +0000565local_abort:
566 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000567call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000568 afs_set_call_complete(call, ret, remote_abort);
569 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100570 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700571}
572
573/*
David Howells0b9bf382019-04-25 14:26:50 +0100574 * Wait synchronously for a call to complete and clean up the call struct.
David Howells08e0e7c2007-04-26 15:55:03 -0700575 */
David Howells0b9bf382019-04-25 14:26:50 +0100576long afs_wait_for_call_to_complete(struct afs_call *call,
577 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700578{
David Howells33cd7f22017-11-02 15:27:48 +0000579 long ret;
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100580 bool rxrpc_complete = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700581
582 DECLARE_WAITQUEUE(myself, current);
583
584 _enter("");
585
David Howells0b9bf382019-04-25 14:26:50 +0100586 ret = call->error;
587 if (ret < 0)
588 goto out;
589
David Howells08e0e7c2007-04-26 15:55:03 -0700590 add_wait_queue(&call->waitq, &myself);
591 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100592 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700593
594 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000595 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
596 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100597 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700598 __set_current_state(TASK_RUNNING);
599 afs_deliver_to_call(call);
600 continue;
601 }
602
David Howells98bf40c2017-11-02 15:27:53 +0000603 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700604 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100605
David Howells7d7587d2020-03-12 21:40:06 +0000606 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100607 /* rxrpc terminated the call. */
608 rxrpc_complete = true;
609 break;
610 }
611
David Howells7d7587d2020-03-12 21:40:06 +0000612 schedule();
David Howells08e0e7c2007-04-26 15:55:03 -0700613 }
614
615 remove_wait_queue(&call->waitq, &myself);
616 __set_current_state(TASK_RUNNING);
617
David Howells98bf40c2017-11-02 15:27:53 +0000618 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100619 if (rxrpc_complete) {
620 afs_set_call_complete(call, call->error, call->abort_code);
621 } else {
622 /* Kill off the call if it's still live. */
623 _debug("call interrupted");
624 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells57af2812022-10-06 21:45:42 +0100625 RX_USER_ABORT, -EINTR,
626 afs_abort_interrupted))
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100627 afs_set_call_complete(call, -EINTR, 0);
628 }
David Howells08e0e7c2007-04-26 15:55:03 -0700629 }
630
David Howells98bf40c2017-11-02 15:27:53 +0000631 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000632 ac->abort_code = call->abort_code;
633 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000634 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000635
636 ret = ac->error;
637 switch (ret) {
638 case 0:
David Howellsffba7182019-05-09 22:22:50 +0100639 ret = call->ret0;
640 call->ret0 = 0;
641
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500642 fallthrough;
David Howellsd2ddc772017-11-02 15:27:50 +0000643 case -ECONNABORTED:
644 ac->responded = true;
645 break;
David Howells33cd7f22017-11-02 15:27:48 +0000646 }
647
David Howells0b9bf382019-04-25 14:26:50 +0100648out:
David Howells08e0e7c2007-04-26 15:55:03 -0700649 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000650 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000651 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700652 return ret;
653}
654
655/*
656 * wake up a waiting call
657 */
David Howellsd0016482016-08-30 20:42:14 +0100658static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
659 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700660{
David Howellsd0016482016-08-30 20:42:14 +0100661 struct afs_call *call = (struct afs_call *)call_user_ID;
662
663 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700664 wake_up(&call->waitq);
665}
666
667/*
668 * wake up an asynchronous call
669 */
David Howellsd0016482016-08-30 20:42:14 +0100670static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
671 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700672{
David Howellsd0016482016-08-30 20:42:14 +0100673 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howellsc56f9ec82022-07-06 10:52:14 +0100674 int r;
David Howellsd0016482016-08-30 20:42:14 +0100675
David Howells8e8d7f12017-01-05 10:38:34 +0000676 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100677 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000678
David Howellsc56f9ec82022-07-06 10:52:14 +0100679 if (__refcount_inc_not_zero(&call->ref, &r)) {
David Howells2757a4d2022-07-06 11:26:14 +0100680 trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
David Howellsf044c882017-11-02 15:27:45 +0000681 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000682 __builtin_return_address(0));
683
684 if (!queue_work(afs_async_calls, &call->async_work))
685 afs_put_call(call);
686 }
David Howells08e0e7c2007-04-26 15:55:03 -0700687}
688
689/*
David Howells341f7412017-01-05 10:38:36 +0000690 * Perform I/O processing on an asynchronous call. The work item carries a ref
691 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700692 */
David Howellsd0016482016-08-30 20:42:14 +0100693static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700694{
David Howellsd0016482016-08-30 20:42:14 +0100695 struct afs_call *call = container_of(work, struct afs_call, async_work);
696
David Howells08e0e7c2007-04-26 15:55:03 -0700697 _enter("");
698
David Howellsd0016482016-08-30 20:42:14 +0100699 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
700 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700701 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100702 }
David Howells08e0e7c2007-04-26 15:55:03 -0700703
David Howells341f7412017-01-05 10:38:36 +0000704 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700705 _leave("");
706}
707
David Howells00e90712016-09-08 11:10:12 +0100708static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
709{
710 struct afs_call *call = (struct afs_call *)user_call_ID;
711
712 call->rxcall = rxcall;
713}
714
715/*
716 * Charge the incoming call preallocation.
717 */
David Howellsf044c882017-11-02 15:27:45 +0000718void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100719{
David Howellsf044c882017-11-02 15:27:45 +0000720 struct afs_net *net =
721 container_of(work, struct afs_net, charge_preallocation_work);
722 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100723
724 for (;;) {
725 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000726 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100727 if (!call)
728 break;
729
David Howellsdde9f092020-03-13 13:46:08 +0000730 call->drop_ref = true;
David Howells56ff9c82017-01-05 10:38:36 +0000731 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000732 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000733 init_waitqueue_head(&call->waitq);
David Howells12bdcf32018-10-20 00:57:56 +0100734 afs_extract_to_tmp(call);
David Howells00e90712016-09-08 11:10:12 +0100735 }
736
David Howellsf044c882017-11-02 15:27:45 +0000737 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100738 afs_wake_up_async_call,
739 afs_rx_attach,
740 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100741 GFP_KERNEL,
742 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100743 break;
744 call = NULL;
745 }
David Howellsf044c882017-11-02 15:27:45 +0000746 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100747}
748
749/*
750 * Discard a preallocated call when a socket is shut down.
751 */
752static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
753 unsigned long user_call_ID)
754{
755 struct afs_call *call = (struct afs_call *)user_call_ID;
756
David Howells00e90712016-09-08 11:10:12 +0100757 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000758 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100759}
760
David Howells08e0e7c2007-04-26 15:55:03 -0700761/*
David Howellsd0016482016-08-30 20:42:14 +0100762 * Notification of an incoming call.
763 */
David Howells00e90712016-09-08 11:10:12 +0100764static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
765 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100766{
David Howellsf044c882017-11-02 15:27:45 +0000767 struct afs_net *net = afs_sock2net(sk);
768
769 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100770}
771
772/*
David Howells372ee162016-08-03 14:11:40 +0100773 * Grab the operation ID from an incoming cache manager call. The socket
774 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700775 */
David Howellsd0016482016-08-30 20:42:14 +0100776static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700777{
David Howellsd0016482016-08-30 20:42:14 +0100778 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700779
David Howellsfc276122019-11-21 09:12:17 +0000780 _enter("{%zu}", iov_iter_count(call->iter));
David Howells08e0e7c2007-04-26 15:55:03 -0700781
782 /* the operation ID forms the first four bytes of the request data */
David Howells12bdcf32018-10-20 00:57:56 +0100783 ret = afs_extract_data(call, true);
David Howellsd0016482016-08-30 20:42:14 +0100784 if (ret < 0)
785 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700786
David Howells50a2c952016-10-13 08:27:10 +0100787 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000788 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howells08e0e7c2007-04-26 15:55:03 -0700789
790 /* ask the cache manager to route the call (it'll change the call type
791 * if successful) */
792 if (!afs_cm_incoming_call(call))
793 return -ENOTSUPP;
794
David Howells8e8d7f12017-01-05 10:38:34 +0000795 trace_afs_cb_call(call);
796
David Howells08e0e7c2007-04-26 15:55:03 -0700797 /* pass responsibility for the remainer of this message off to the
798 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100799 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700800}
801
802/*
David Howellse8332512017-08-29 10:18:56 +0100803 * Advance the AFS call state when an RxRPC service call ends the transmit
804 * phase.
805 */
806static void afs_notify_end_reply_tx(struct sock *sock,
807 struct rxrpc_call *rxcall,
808 unsigned long call_user_ID)
809{
810 struct afs_call *call = (struct afs_call *)call_user_ID;
811
David Howells98bf40c2017-11-02 15:27:53 +0000812 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100813}
814
815/*
David Howells08e0e7c2007-04-26 15:55:03 -0700816 * send an empty reply
817 */
818void afs_send_empty_reply(struct afs_call *call)
819{
David Howellsf044c882017-11-02 15:27:45 +0000820 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700821 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700822
823 _enter("");
824
David Howellsf044c882017-11-02 15:27:45 +0000825 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100826
David Howells08e0e7c2007-04-26 15:55:03 -0700827 msg.msg_name = NULL;
828 msg.msg_namelen = 0;
Al Virode4eda92022-09-15 20:25:47 -0400829 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700830 msg.msg_control = NULL;
831 msg.msg_controllen = 0;
832 msg.msg_flags = 0;
833
David Howellsf044c882017-11-02 15:27:45 +0000834 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100835 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700836 case 0:
837 _leave(" [replied]");
838 return;
839
840 case -ENOMEM:
841 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000842 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells57af2812022-10-06 21:45:42 +0100843 RXGEN_SS_MARSHAL, -ENOMEM,
844 afs_abort_oom);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500845 fallthrough;
David Howells08e0e7c2007-04-26 15:55:03 -0700846 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700847 _leave(" [error]");
848 return;
849 }
850}
851
852/*
David Howellsb908fe62007-04-26 15:58:17 -0700853 * send a simple reply
854 */
855void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
856{
David Howellsf044c882017-11-02 15:27:45 +0000857 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700858 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500859 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100860 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700861
862 _enter("");
863
David Howellsf044c882017-11-02 15:27:45 +0000864 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100865
David Howellsb908fe62007-04-26 15:58:17 -0700866 iov[0].iov_base = (void *) buf;
867 iov[0].iov_len = len;
868 msg.msg_name = NULL;
869 msg.msg_namelen = 0;
Al Virode4eda92022-09-15 20:25:47 -0400870 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700871 msg.msg_control = NULL;
872 msg.msg_controllen = 0;
873 msg.msg_flags = 0;
874
David Howellsf044c882017-11-02 15:27:45 +0000875 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100876 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100877 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100878 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700879 _leave(" [replied]");
880 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100881 }
David Howells6c67c7c2014-05-21 14:48:05 +0100882
David Howellsbd6dc742007-07-20 10:59:41 +0100883 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700884 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000885 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells57af2812022-10-06 21:45:42 +0100886 RXGEN_SS_MARSHAL, -ENOMEM,
887 afs_abort_oom);
David Howellsb908fe62007-04-26 15:58:17 -0700888 }
David Howellsbd6dc742007-07-20 10:59:41 +0100889 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700890}
891
892/*
David Howells372ee162016-08-03 14:11:40 +0100893 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700894 */
David Howells12bdcf32018-10-20 00:57:56 +0100895int afs_extract_data(struct afs_call *call, bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700896{
David Howellsf044c882017-11-02 15:27:45 +0000897 struct afs_net *net = call->net;
David Howellsfc276122019-11-21 09:12:17 +0000898 struct iov_iter *iter = call->iter;
David Howells98bf40c2017-11-02 15:27:53 +0000899 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000900 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100901 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700902
David Howellsf105da12020-02-06 14:22:28 +0000903 _enter("{%s,%zu,%zu},%d",
904 call->type->name, call->iov_len, iov_iter_count(iter), want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700905
David Howells12bdcf32018-10-20 00:57:56 +0100906 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
David Howellsf105da12020-02-06 14:22:28 +0000907 &call->iov_len, want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100908 &call->service_id);
David Howells93368b62022-10-26 23:43:00 +0100909 trace_afs_receive_data(call, call->iter, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100910 if (ret == 0 || ret == -EAGAIN)
911 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700912
David Howells98bf40c2017-11-02 15:27:53 +0000913 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100914 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000915 switch (state) {
916 case AFS_CALL_CL_AWAIT_REPLY:
917 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100918 break;
David Howells98bf40c2017-11-02 15:27:53 +0000919 case AFS_CALL_SV_AWAIT_REQUEST:
920 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100921 break;
David Howells98bf40c2017-11-02 15:27:53 +0000922 case AFS_CALL_COMPLETE:
923 kdebug("prem complete %d", call->error);
David Howellsf51375c2018-10-20 00:57:57 +0100924 return afs_io_error(call, afs_io_error_extract);
David Howellsd0016482016-08-30 20:42:14 +0100925 default:
926 break;
927 }
928 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700929 }
David Howellsd0016482016-08-30 20:42:14 +0100930
David Howells98bf40c2017-11-02 15:27:53 +0000931 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +0100932 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700933}
David Howells5f702c82018-04-06 14:17:25 +0100934
935/*
936 * Log protocol error production.
937 */
David Howells7126ead2020-04-08 16:49:08 +0100938noinline int afs_protocol_error(struct afs_call *call,
David Howells160cb952018-10-20 00:57:56 +0100939 enum afs_eproto_cause cause)
David Howells5f702c82018-04-06 14:17:25 +0100940{
David Howells7126ead2020-04-08 16:49:08 +0100941 trace_afs_protocol_error(call, cause);
David Howells38355ee2020-04-08 16:13:20 +0100942 if (call)
943 call->unmarshalling_error = true;
David Howells7126ead2020-04-08 16:49:08 +0100944 return -EBADMSG;
David Howells5f702c82018-04-06 14:17:25 +0100945}