blob: 8be709cb8542aab247fb390063a51bef8eb8849c [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 Howells08e0e7c2007-04-26 15:55:03 -070016
David Howellsf044c882017-11-02 15:27:45 +000017struct workqueue_struct *afs_async_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070018
David Howellsd0016482016-08-30 20:42:14 +010019static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010020static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010021static void afs_process_async_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010022static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
23static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010024static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070025
David Howells08e0e7c2007-04-26 15:55:03 -070026/* asynchronous incoming call initial processing */
27static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070028 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070029 .deliver = afs_deliver_cm_op_id,
David Howells08e0e7c2007-04-26 15:55:03 -070030};
31
David Howells08e0e7c2007-04-26 15:55:03 -070032/*
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 */
David Howellsf044c882017-11-02 15:27:45 +000036int afs_open_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070037{
38 struct sockaddr_rxrpc srx;
39 struct socket *socket;
40 int ret;
41
42 _enter("");
43
David Howells5b86d4f2018-05-18 11:46:15 +010044 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
David Howells0e119b42016-06-10 22:30:37 +010045 if (ret < 0)
46 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070047
48 socket->sk->sk_allocation = GFP_NOFS;
49
50 /* bind the callback manager's address to make this a server socket */
David Howells3838d3e2017-11-02 15:27:47 +000051 memset(&srx, 0, sizeof(srx));
David Howells08e0e7c2007-04-26 15:55:03 -070052 srx.srx_family = AF_RXRPC;
53 srx.srx_service = CM_SERVICE;
54 srx.transport_type = SOCK_DGRAM;
David Howells3838d3e2017-11-02 15:27:47 +000055 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);
David Howells08e0e7c2007-04-26 15:55:03 -070058
Christoph Hellwig298cd882020-05-28 07:12:35 +020059 ret = rxrpc_sock_set_min_security_level(socket->sk,
60 RXRPC_SECURITY_ENCRYPT);
David Howells4776cab2018-05-10 23:10:40 +010061 if (ret < 0)
62 goto error_2;
63
David Howells08e0e7c2007-04-26 15:55:03 -070064 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
Marc Dionne83732ec2017-11-02 15:27:52 +000065 if (ret == -EADDRINUSE) {
66 srx.transport.sin6.sin6_port = 0;
67 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
68 }
David Howells0e119b42016-06-10 22:30:37 +010069 if (ret < 0)
70 goto error_2;
71
David Howells35dbfba2018-10-20 00:57:58 +010072 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
David Howells3bf0fb62018-10-20 00:57:59 +010077 /* 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 */
David Howells35dbfba2018-10-20 00:57:58 +010083
David Howells00e90712016-09-08 11:10:12 +010084 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
85 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010086
David Howells0e119b42016-06-10 22:30:37 +010087 ret = kernel_listen(socket, INT_MAX);
88 if (ret < 0)
89 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070090
David Howellsf044c882017-11-02 15:27:45 +000091 net->socket = socket;
92 afs_charge_preallocation(&net->charge_preallocation_work);
David Howells08e0e7c2007-04-26 15:55:03 -070093 _leave(" = 0");
94 return 0;
David Howells0e119b42016-06-10 22:30:37 +010095
96error_2:
97 sock_release(socket);
98error_1:
David Howells0e119b42016-06-10 22:30:37 +010099 _leave(" = %d", ret);
100 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700101}
102
103/*
104 * close the RxRPC socket AFS was using
105 */
David Howellsf044c882017-11-02 15:27:45 +0000106void afs_close_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -0700107{
108 _enter("");
109
David Howellsf044c882017-11-02 15:27:45 +0000110 kernel_listen(net->socket, 0);
David Howells341f7412017-01-05 10:38:36 +0000111 flush_workqueue(afs_async_calls);
112
David Howellsf044c882017-11-02 15:27:45 +0000113 if (net->spare_incoming_call) {
114 afs_put_call(net->spare_incoming_call);
115 net->spare_incoming_call = NULL;
David Howells00e90712016-09-08 11:10:12 +0100116 }
117
David Howellsf044c882017-11-02 15:27:45 +0000118 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100119 wait_var_event(&net->nr_outstanding_calls,
120 !atomic_read(&net->nr_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100121 _debug("no outstanding calls");
122
David Howellsf044c882017-11-02 15:27:45 +0000123 kernel_sock_shutdown(net->socket, SHUT_RDWR);
David Howells248f2192016-09-08 11:10:12 +0100124 flush_workqueue(afs_async_calls);
David Howellsf044c882017-11-02 15:27:45 +0000125 sock_release(net->socket);
David Howells08e0e7c2007-04-26 15:55:03 -0700126
127 _debug("dework");
David Howells08e0e7c2007-04-26 15:55:03 -0700128 _leave("");
129}
130
131/*
David Howells341f7412017-01-05 10:38:36 +0000132 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700133 */
David Howellsf044c882017-11-02 15:27:45 +0000134static struct afs_call *afs_alloc_call(struct afs_net *net,
135 const struct afs_call_type *type,
David Howells341f7412017-01-05 10:38:36 +0000136 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700137{
David Howells341f7412017-01-05 10:38:36 +0000138 struct afs_call *call;
139 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700140
David Howells341f7412017-01-05 10:38:36 +0000141 call = kzalloc(sizeof(*call), gfp);
142 if (!call)
143 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700144
David Howells341f7412017-01-05 10:38:36 +0000145 call->type = type;
David Howellsf044c882017-11-02 15:27:45 +0000146 call->net = net;
David Howellsa25e21f2018-03-27 23:03:00 +0100147 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howells341f7412017-01-05 10:38:36 +0000148 atomic_set(&call->usage, 1);
149 INIT_WORK(&call->async_work, afs_process_async_call);
150 init_waitqueue_head(&call->waitq);
David Howells98bf40c2017-11-02 15:27:53 +0000151 spin_lock_init(&call->state_lock);
David Howellsfc276122019-11-21 09:12:17 +0000152 call->iter = &call->def_iter;
David Howells2f02f7a2016-04-07 17:23:03 +0100153
David Howellsf044c882017-11-02 15:27:45 +0000154 o = atomic_inc_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000155 trace_afs_call(call, afs_call_trace_alloc, 1, o,
156 __builtin_return_address(0));
157 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700158}
159
160/*
David Howells341f7412017-01-05 10:38:36 +0000161 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100162 */
David Howells341f7412017-01-05 10:38:36 +0000163void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100164{
David Howellsf044c882017-11-02 15:27:45 +0000165 struct afs_net *net = call->net;
David Howells341f7412017-01-05 10:38:36 +0000166 int n = atomic_dec_return(&call->usage);
David Howellsf044c882017-11-02 15:27:45 +0000167 int o = atomic_read(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000168
David Howells4636cf12020-03-13 13:36:01 +0000169 trace_afs_call(call, afs_call_trace_put, n, o,
David Howells341f7412017-01-05 10:38:36 +0000170 __builtin_return_address(0));
171
172 ASSERTCMP(n, >=, 0);
173 if (n == 0) {
174 ASSERT(!work_pending(&call->async_work));
175 ASSERT(call->type->name != NULL);
176
177 if (call->rxcall) {
David Howellsf044c882017-11-02 15:27:45 +0000178 rxrpc_kernel_end_call(net->socket, call->rxcall);
David Howells341f7412017-01-05 10:38:36 +0000179 call->rxcall = NULL;
180 }
181 if (call->type->destructor)
182 call->type->destructor(call);
183
David Howells977e5f82020-04-17 17:31:26 +0100184 afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
David Howells3bf0fb62018-10-20 00:57:59 +0100185 afs_put_addrlist(call->alist);
David Howells341f7412017-01-05 10:38:36 +0000186 kfree(call->request);
David Howellsa25e21f2018-03-27 23:03:00 +0100187
188 trace_afs_call(call, afs_call_trace_free, 0, o,
189 __builtin_return_address(0));
David Howells341f7412017-01-05 10:38:36 +0000190 kfree(call);
191
David Howellsf044c882017-11-02 15:27:45 +0000192 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000193 if (o == 0)
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100194 wake_up_var(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100195 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100196}
197
David Howells7a75b002019-01-10 15:14:29 +0000198static struct afs_call *afs_get_call(struct afs_call *call,
199 enum afs_call_trace why)
200{
201 int u = atomic_inc_return(&call->usage);
202
203 trace_afs_call(call, why, u,
204 atomic_read(&call->net->nr_outstanding_calls),
205 __builtin_return_address(0));
206 return call;
207}
208
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100209/*
David Howells3bf0fb62018-10-20 00:57:59 +0100210 * Queue the call for actual work.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100211 */
David Howells3bf0fb62018-10-20 00:57:59 +0100212static void afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100213{
David Howells3bf0fb62018-10-20 00:57:59 +0100214 if (call->type->work) {
David Howells3bf0fb62018-10-20 00:57:59 +0100215 INIT_WORK(&call->work, call->type->work);
David Howells341f7412017-01-05 10:38:36 +0000216
David Howells7a75b002019-01-10 15:14:29 +0000217 afs_get_call(call, afs_call_trace_work);
David Howells3bf0fb62018-10-20 00:57:59 +0100218 if (!queue_work(afs_wq, &call->work))
219 afs_put_call(call);
220 }
David Howells6c67c7c2014-05-21 14:48:05 +0100221}
222
223/*
David Howells08e0e7c2007-04-26 15:55:03 -0700224 * allocate a call with flat request and reply buffers
225 */
David Howellsf044c882017-11-02 15:27:45 +0000226struct afs_call *afs_alloc_flat_call(struct afs_net *net,
227 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100228 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700229{
230 struct afs_call *call;
231
David Howellsf044c882017-11-02 15:27:45 +0000232 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700233 if (!call)
234 goto nomem_call;
235
David Howells00d3b7a2007-04-26 15:57:07 -0700236 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000237 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700238 call->request = kmalloc(request_size, GFP_NOFS);
239 if (!call->request)
240 goto nomem_free;
241 }
242
David Howellsd0016482016-08-30 20:42:14 +0100243 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000244 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100245 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700246 if (!call->buffer)
247 goto nomem_free;
248 }
249
David Howells12bdcf32018-10-20 00:57:56 +0100250 afs_extract_to_buf(call, call->reply_max);
David Howells025db802017-11-02 15:27:51 +0000251 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700252 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700253 return call;
254
David Howells00d3b7a2007-04-26 15:57:07 -0700255nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000256 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700257nomem_call:
258 return NULL;
259}
260
261/*
262 * clean up a call with flat buffer
263 */
264void afs_flat_call_destructor(struct afs_call *call)
265{
266 _enter("");
267
268 kfree(call->request);
269 call->request = NULL;
270 kfree(call->buffer);
271 call->buffer = NULL;
272}
273
David Howells2f5705a2017-03-16 16:27:46 +0000274#define AFS_BVEC_MAX 8
275
276/*
277 * Load the given bvec with the next few pages.
278 */
279static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
280 struct bio_vec *bv, pgoff_t first, pgoff_t last,
281 unsigned offset)
282{
David Howellse49c7b22020-04-10 20:51:51 +0100283 struct afs_operation *op = call->op;
David Howells2f5705a2017-03-16 16:27:46 +0000284 struct page *pages[AFS_BVEC_MAX];
285 unsigned int nr, n, i, to, bytes = 0;
286
287 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
David Howellse49c7b22020-04-10 20:51:51 +0100288 n = find_get_pages_contig(op->store.mapping, first, nr, pages);
David Howells2f5705a2017-03-16 16:27:46 +0000289 ASSERTCMP(n, ==, nr);
290
291 msg->msg_flags |= MSG_MORE;
292 for (i = 0; i < nr; i++) {
293 to = PAGE_SIZE;
294 if (first + i >= last) {
David Howellse49c7b22020-04-10 20:51:51 +0100295 to = op->store.last_to;
David Howells2f5705a2017-03-16 16:27:46 +0000296 msg->msg_flags &= ~MSG_MORE;
297 }
298 bv[i].bv_page = pages[i];
299 bv[i].bv_len = to - offset;
300 bv[i].bv_offset = offset;
301 bytes += to - offset;
302 offset = 0;
303 }
304
David Howellsaa563d72018-10-20 00:57:56 +0100305 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
David Howells2f5705a2017-03-16 16:27:46 +0000306}
307
David Howells08e0e7c2007-04-26 15:55:03 -0700308/*
David Howellse8332512017-08-29 10:18:56 +0100309 * Advance the AFS call state when the RxRPC call ends the transmit phase.
310 */
311static void afs_notify_end_request_tx(struct sock *sock,
312 struct rxrpc_call *rxcall,
313 unsigned long call_user_ID)
314{
315 struct afs_call *call = (struct afs_call *)call_user_ID;
316
David Howells98bf40c2017-11-02 15:27:53 +0000317 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
David Howellse8332512017-08-29 10:18:56 +0100318}
319
320/*
David Howells31143d52007-05-09 02:33:46 -0700321 * attach the data from a bunch of pages on an inode to a call
322 */
Al Viro39c6ace2016-01-09 20:36:51 -0500323static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700324{
David Howellse49c7b22020-04-10 20:51:51 +0100325 struct afs_operation *op = call->op;
David Howells2f5705a2017-03-16 16:27:46 +0000326 struct bio_vec bv[AFS_BVEC_MAX];
327 unsigned int bytes, nr, loop, offset;
David Howellse49c7b22020-04-10 20:51:51 +0100328 pgoff_t first = op->store.first, last = op->store.last;
David Howells31143d52007-05-09 02:33:46 -0700329 int ret;
330
David Howellse49c7b22020-04-10 20:51:51 +0100331 offset = op->store.first_offset;
332 op->store.first_offset = 0;
David Howells31143d52007-05-09 02:33:46 -0700333
334 do {
David Howells2f5705a2017-03-16 16:27:46 +0000335 afs_load_bvec(call, msg, bv, first, last, offset);
David Howells2c099012017-11-02 15:27:51 +0000336 trace_afs_send_pages(call, msg, first, last, offset);
337
David Howells2f5705a2017-03-16 16:27:46 +0000338 offset = 0;
339 bytes = msg->msg_iter.count;
340 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700341
David Howellse49c7b22020-04-10 20:51:51 +0100342 ret = rxrpc_kernel_send_data(op->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100343 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000344 for (loop = 0; loop < nr; loop++)
345 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700346 if (ret < 0)
347 break;
David Howells2f5705a2017-03-16 16:27:46 +0000348
349 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700350 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700351
David Howellse49c7b22020-04-10 20:51:51 +0100352 trace_afs_sent_pages(call, op->store.first, last, first, ret);
David Howells31143d52007-05-09 02:33:46 -0700353 return ret;
354}
355
356/*
David Howells0b9bf382019-04-25 14:26:50 +0100357 * Initiate a call and synchronously queue up the parameters for dispatch. Any
358 * error is stored into the call struct, which the caller must check for.
David Howells08e0e7c2007-04-26 15:55:03 -0700359 */
David Howells0b9bf382019-04-25 14:26:50 +0100360void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
David Howells08e0e7c2007-04-26 15:55:03 -0700361{
David Howells2feeaf82018-10-20 00:57:59 +0100362 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
David Howells08e0e7c2007-04-26 15:55:03 -0700363 struct rxrpc_call *rxcall;
364 struct msghdr msg;
365 struct kvec iov[1];
David Howellse754eba2017-06-07 12:40:03 +0100366 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700367 int ret;
368
David Howells4d9df982017-11-02 15:27:47 +0000369 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700370
David Howells00d3b7a2007-04-26 15:57:07 -0700371 ASSERT(call->type != NULL);
372 ASSERT(call->type->name != NULL);
373
David Howells31143d52007-05-09 02:33:46 -0700374 _debug("____MAKE %p{%s,%x} [%d]____",
375 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000376 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700377
David Howells3bf0fb62018-10-20 00:57:59 +0100378 call->addr_ix = ac->index;
379 call->alist = afs_get_addrlist(ac->alist);
David Howells08e0e7c2007-04-26 15:55:03 -0700380
David Howellse754eba2017-06-07 12:40:03 +0100381 /* Work out the length we're going to transmit. This is awkward for
382 * calls such as FS.StoreData where there's an extra injection of data
383 * after the initial fixed part.
384 */
385 tx_total_len = call->request_size;
386 if (call->send_pages) {
David Howellse49c7b22020-04-10 20:51:51 +0100387 struct afs_operation *op = call->op;
388
389 if (op->store.last == op->store.first) {
390 tx_total_len += op->store.last_to - op->store.first_offset;
David Howells1199db62017-11-02 15:27:51 +0000391 } else {
392 /* It looks mathematically like you should be able to
393 * combine the following lines with the ones above, but
394 * unsigned arithmetic is fun when it wraps...
395 */
David Howellse49c7b22020-04-10 20:51:51 +0100396 tx_total_len += PAGE_SIZE - op->store.first_offset;
397 tx_total_len += op->store.last_to;
398 tx_total_len += (op->store.last - op->store.first - 1) * PAGE_SIZE;
David Howells1199db62017-11-02 15:27:51 +0000399 }
David Howellse754eba2017-06-07 12:40:03 +0100400 }
401
David Howells34fa4762019-01-10 15:40:50 +0000402 /* If the call is going to be asynchronous, we need an extra ref for
403 * the call to hold itself so the caller need not hang on to its ref.
404 */
David Howellsdde9f092020-03-13 13:46:08 +0000405 if (call->async) {
David Howells34fa4762019-01-10 15:40:50 +0000406 afs_get_call(call, afs_call_trace_get);
David Howellsdde9f092020-03-13 13:46:08 +0000407 call->drop_ref = true;
408 }
David Howells34fa4762019-01-10 15:40:50 +0000409
David Howells08e0e7c2007-04-26 15:55:03 -0700410 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000411 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100412 (unsigned long)call,
413 tx_total_len, gfp,
David Howells0b9bf382019-04-25 14:26:50 +0100414 (call->async ?
David Howells56ff9c82017-01-05 10:38:36 +0000415 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100416 afs_wake_up_call_waiter),
David Howellsa25e21f2018-03-27 23:03:00 +0100417 call->upgrade,
David Howellse138aa72020-03-13 09:22:09 +0000418 (call->intr ? RXRPC_PREINTERRUPTIBLE :
419 RXRPC_UNINTERRUPTIBLE),
David Howellsa25e21f2018-03-27 23:03:00 +0100420 call->debug_id);
David Howells08e0e7c2007-04-26 15:55:03 -0700421 if (IS_ERR(rxcall)) {
422 ret = PTR_ERR(rxcall);
David Howells3bf0fb62018-10-20 00:57:59 +0100423 call->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700424 goto error_kill_call;
425 }
426
427 call->rxcall = rxcall;
428
David Howells94f699c2019-05-16 13:21:59 +0100429 if (call->max_lifespan)
430 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
431 call->max_lifespan);
432
David Howells08e0e7c2007-04-26 15:55:03 -0700433 /* send the request */
434 iov[0].iov_base = call->request;
435 iov[0].iov_len = call->request_size;
436
437 msg.msg_name = NULL;
438 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100439 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700440 msg.msg_control = NULL;
441 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100442 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700443
David Howellsf044c882017-11-02 15:27:45 +0000444 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100445 &msg, call->request_size,
446 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700447 if (ret < 0)
448 goto error_do_abort;
449
David Howells31143d52007-05-09 02:33:46 -0700450 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500451 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700452 if (ret < 0)
453 goto error_do_abort;
454 }
455
David Howells34fa4762019-01-10 15:40:50 +0000456 /* Note that at this point, we may have received the reply or an abort
457 * - and an asynchronous call may already have completed.
David Howells0b9bf382019-04-25 14:26:50 +0100458 *
459 * afs_wait_for_call_to_complete(call, ac)
460 * must be called to synchronously clean up.
David Howells34fa4762019-01-10 15:40:50 +0000461 */
David Howells0b9bf382019-04-25 14:26:50 +0100462 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700463
464error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000465 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000466 rxrpc_kernel_abort_call(call->net->socket, rxcall,
467 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000468 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100469 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
David Howellseb9950e2018-08-03 17:06:56 +0100470 rxrpc_kernel_recv_data(call->net->socket, rxcall,
471 &msg.msg_iter, false,
472 &call->abort_code, &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000473 ac->abort_code = call->abort_code;
474 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000475 }
David Howells025db802017-11-02 15:27:51 +0000476 call->error = ret;
477 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700478error_kill_call:
David Howells3bf0fb62018-10-20 00:57:59 +0100479 if (call->type->done)
480 call->type->done(call);
David Howells34fa4762019-01-10 15:40:50 +0000481
482 /* We need to dispose of the extra ref we grabbed for an async call.
483 * The call, however, might be queued on afs_async_calls and we need to
484 * make sure we don't get any more notifications that might requeue it.
485 */
486 if (call->rxcall) {
487 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
488 call->rxcall = NULL;
489 }
490 if (call->async) {
491 if (cancel_work_sync(&call->async_work))
492 afs_put_call(call);
493 afs_put_call(call);
494 }
495
David Howellsd2ddc772017-11-02 15:27:50 +0000496 ac->error = ret;
David Howells34fa4762019-01-10 15:40:50 +0000497 call->state = AFS_CALL_COMPLETE;
David Howells08e0e7c2007-04-26 15:55:03 -0700498 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700499}
500
501/*
David Howells08e0e7c2007-04-26 15:55:03 -0700502 * deliver messages to a call
503 */
504static void afs_deliver_to_call(struct afs_call *call)
505{
David Howells98bf40c2017-11-02 15:27:53 +0000506 enum afs_call_state state;
507 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700508 int ret;
509
David Howellsd0016482016-08-30 20:42:14 +0100510 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700511
David Howells98bf40c2017-11-02 15:27:53 +0000512 while (state = READ_ONCE(call->state),
513 state == AFS_CALL_CL_AWAIT_REPLY ||
514 state == AFS_CALL_SV_AWAIT_OP_ID ||
515 state == AFS_CALL_SV_AWAIT_REQUEST ||
516 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100517 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000518 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howellsfc276122019-11-21 09:12:17 +0000519 iov_iter_kvec(&call->def_iter, READ, NULL, 0, 0);
David Howellsf044c882017-11-02 15:27:45 +0000520 ret = rxrpc_kernel_recv_data(call->net->socket,
David Howellsfc276122019-11-21 09:12:17 +0000521 call->rxcall, &call->def_iter,
David Howells12bdcf32018-10-20 00:57:56 +0100522 false, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100523 &call->service_id);
David Howellsfc276122019-11-21 09:12:17 +0000524 trace_afs_receive_data(call, &call->def_iter, false, ret);
David Howells8e8d7f12017-01-05 10:38:34 +0000525
David Howellsd0016482016-08-30 20:42:14 +0100526 if (ret == -EINPROGRESS || ret == -EAGAIN)
527 return;
David Howells98bf40c2017-11-02 15:27:53 +0000528 if (ret < 0 || ret == 1) {
529 if (ret == 1)
530 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000531 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000532 }
David Howellsd0016482016-08-30 20:42:14 +0100533 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700534 }
535
David Howells45715772019-05-14 11:44:26 +0100536 if (!call->have_reply_time &&
David Howells12d8e952018-10-20 00:57:58 +0100537 rxrpc_kernel_get_reply_time(call->net->socket,
538 call->rxcall,
539 &call->reply_time))
David Howells45715772019-05-14 11:44:26 +0100540 call->have_reply_time = true;
David Howells12d8e952018-10-20 00:57:58 +0100541
David Howellsd0016482016-08-30 20:42:14 +0100542 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000543 state = READ_ONCE(call->state);
David Howells38355ee2020-04-08 16:13:20 +0100544 if (ret == 0 && call->unmarshalling_error)
545 ret = -EBADMSG;
David Howellsd0016482016-08-30 20:42:14 +0100546 switch (ret) {
547 case 0:
David Howells3bf0fb62018-10-20 00:57:59 +0100548 afs_queue_call_work(call);
David Howellsf2686b02018-05-10 14:12:50 +0100549 if (state == AFS_CALL_CL_PROC_REPLY) {
David Howells20325962020-04-30 01:03:49 +0100550 if (call->op)
David Howellsf2686b02018-05-10 14:12:50 +0100551 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
David Howells20325962020-04-30 01:03:49 +0100552 &call->op->server->flags);
David Howells025db802017-11-02 15:27:51 +0000553 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100554 }
David Howells98bf40c2017-11-02 15:27:53 +0000555 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100556 goto done;
557 case -EINPROGRESS:
558 case -EAGAIN:
559 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000560 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000561 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
562 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100563 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000564 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000565 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100566 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000567 goto local_abort;
David Howells4ac15ea2018-10-20 00:57:57 +0100568 case -EIO:
569 pr_err("kAFS: Call %u in bad state %u\n",
570 call->debug_id, state);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500571 fallthrough;
David Howellsd0016482016-08-30 20:42:14 +0100572 case -ENODATA:
573 case -EBADMSG:
574 case -EMSGSIZE:
David Howellsd0016482016-08-30 20:42:14 +0100575 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000576 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100577 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000578 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells12bdcf32018-10-20 00:57:56 +0100579 abort_code, ret, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000580 goto local_abort;
David Howells8022c4b2019-04-13 08:37:37 +0100581 default:
582 abort_code = RX_USER_ABORT;
583 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
584 abort_code, ret, "KER");
585 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100586 }
David Howells08e0e7c2007-04-26 15:55:03 -0700587 }
588
David Howellsd0016482016-08-30 20:42:14 +0100589done:
David Howells3bf0fb62018-10-20 00:57:59 +0100590 if (call->type->done)
591 call->type->done(call);
David Howellsd0016482016-08-30 20:42:14 +0100592out:
David Howells08e0e7c2007-04-26 15:55:03 -0700593 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100594 return;
595
David Howells98bf40c2017-11-02 15:27:53 +0000596local_abort:
597 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000598call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000599 afs_set_call_complete(call, ret, remote_abort);
600 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100601 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700602}
603
604/*
David Howells0b9bf382019-04-25 14:26:50 +0100605 * Wait synchronously for a call to complete and clean up the call struct.
David Howells08e0e7c2007-04-26 15:55:03 -0700606 */
David Howells0b9bf382019-04-25 14:26:50 +0100607long afs_wait_for_call_to_complete(struct afs_call *call,
608 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700609{
David Howells33cd7f22017-11-02 15:27:48 +0000610 long ret;
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100611 bool rxrpc_complete = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700612
613 DECLARE_WAITQUEUE(myself, current);
614
615 _enter("");
616
David Howells0b9bf382019-04-25 14:26:50 +0100617 ret = call->error;
618 if (ret < 0)
619 goto out;
620
David Howells08e0e7c2007-04-26 15:55:03 -0700621 add_wait_queue(&call->waitq, &myself);
622 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100623 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700624
625 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000626 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
627 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100628 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700629 __set_current_state(TASK_RUNNING);
630 afs_deliver_to_call(call);
631 continue;
632 }
633
David Howells98bf40c2017-11-02 15:27:53 +0000634 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700635 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100636
David Howells7d7587d2020-03-12 21:40:06 +0000637 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100638 /* rxrpc terminated the call. */
639 rxrpc_complete = true;
640 break;
641 }
642
David Howells7d7587d2020-03-12 21:40:06 +0000643 schedule();
David Howells08e0e7c2007-04-26 15:55:03 -0700644 }
645
646 remove_wait_queue(&call->waitq, &myself);
647 __set_current_state(TASK_RUNNING);
648
David Howells98bf40c2017-11-02 15:27:53 +0000649 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100650 if (rxrpc_complete) {
651 afs_set_call_complete(call, call->error, call->abort_code);
652 } else {
653 /* Kill off the call if it's still live. */
654 _debug("call interrupted");
655 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
656 RX_USER_ABORT, -EINTR, "KWI"))
657 afs_set_call_complete(call, -EINTR, 0);
658 }
David Howells08e0e7c2007-04-26 15:55:03 -0700659 }
660
David Howells98bf40c2017-11-02 15:27:53 +0000661 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000662 ac->abort_code = call->abort_code;
663 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000664 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000665
666 ret = ac->error;
667 switch (ret) {
668 case 0:
David Howellsffba7182019-05-09 22:22:50 +0100669 ret = call->ret0;
670 call->ret0 = 0;
671
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500672 fallthrough;
David Howellsd2ddc772017-11-02 15:27:50 +0000673 case -ECONNABORTED:
674 ac->responded = true;
675 break;
David Howells33cd7f22017-11-02 15:27:48 +0000676 }
677
David Howells0b9bf382019-04-25 14:26:50 +0100678out:
David Howells08e0e7c2007-04-26 15:55:03 -0700679 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000680 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000681 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700682 return ret;
683}
684
685/*
686 * wake up a waiting call
687 */
David Howellsd0016482016-08-30 20:42:14 +0100688static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
689 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700690{
David Howellsd0016482016-08-30 20:42:14 +0100691 struct afs_call *call = (struct afs_call *)call_user_ID;
692
693 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700694 wake_up(&call->waitq);
695}
696
697/*
698 * wake up an asynchronous call
699 */
David Howellsd0016482016-08-30 20:42:14 +0100700static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
701 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700702{
David Howellsd0016482016-08-30 20:42:14 +0100703 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000704 int u;
David Howellsd0016482016-08-30 20:42:14 +0100705
David Howells8e8d7f12017-01-05 10:38:34 +0000706 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100707 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000708
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100709 u = atomic_fetch_add_unless(&call->usage, 1, 0);
David Howells341f7412017-01-05 10:38:36 +0000710 if (u != 0) {
David Howells4636cf12020-03-13 13:36:01 +0000711 trace_afs_call(call, afs_call_trace_wake, u + 1,
David Howellsf044c882017-11-02 15:27:45 +0000712 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000713 __builtin_return_address(0));
714
715 if (!queue_work(afs_async_calls, &call->async_work))
716 afs_put_call(call);
717 }
David Howells08e0e7c2007-04-26 15:55:03 -0700718}
719
720/*
David Howells341f7412017-01-05 10:38:36 +0000721 * Perform I/O processing on an asynchronous call. The work item carries a ref
722 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700723 */
David Howellsd0016482016-08-30 20:42:14 +0100724static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700725{
David Howellsd0016482016-08-30 20:42:14 +0100726 struct afs_call *call = container_of(work, struct afs_call, async_work);
727
David Howells08e0e7c2007-04-26 15:55:03 -0700728 _enter("");
729
David Howellsd0016482016-08-30 20:42:14 +0100730 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
731 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700732 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100733 }
David Howells08e0e7c2007-04-26 15:55:03 -0700734
David Howells341f7412017-01-05 10:38:36 +0000735 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700736 _leave("");
737}
738
David Howells00e90712016-09-08 11:10:12 +0100739static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
740{
741 struct afs_call *call = (struct afs_call *)user_call_ID;
742
743 call->rxcall = rxcall;
744}
745
746/*
747 * Charge the incoming call preallocation.
748 */
David Howellsf044c882017-11-02 15:27:45 +0000749void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100750{
David Howellsf044c882017-11-02 15:27:45 +0000751 struct afs_net *net =
752 container_of(work, struct afs_net, charge_preallocation_work);
753 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100754
755 for (;;) {
756 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000757 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100758 if (!call)
759 break;
760
David Howellsdde9f092020-03-13 13:46:08 +0000761 call->drop_ref = true;
David Howells56ff9c82017-01-05 10:38:36 +0000762 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000763 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000764 init_waitqueue_head(&call->waitq);
David Howells12bdcf32018-10-20 00:57:56 +0100765 afs_extract_to_tmp(call);
David Howells00e90712016-09-08 11:10:12 +0100766 }
767
David Howellsf044c882017-11-02 15:27:45 +0000768 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100769 afs_wake_up_async_call,
770 afs_rx_attach,
771 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100772 GFP_KERNEL,
773 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100774 break;
775 call = NULL;
776 }
David Howellsf044c882017-11-02 15:27:45 +0000777 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100778}
779
780/*
781 * Discard a preallocated call when a socket is shut down.
782 */
783static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
784 unsigned long user_call_ID)
785{
786 struct afs_call *call = (struct afs_call *)user_call_ID;
787
David Howells00e90712016-09-08 11:10:12 +0100788 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000789 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100790}
791
David Howells08e0e7c2007-04-26 15:55:03 -0700792/*
David Howellsd0016482016-08-30 20:42:14 +0100793 * Notification of an incoming call.
794 */
David Howells00e90712016-09-08 11:10:12 +0100795static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
796 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100797{
David Howellsf044c882017-11-02 15:27:45 +0000798 struct afs_net *net = afs_sock2net(sk);
799
800 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100801}
802
803/*
David Howells372ee162016-08-03 14:11:40 +0100804 * Grab the operation ID from an incoming cache manager call. The socket
805 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700806 */
David Howellsd0016482016-08-30 20:42:14 +0100807static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700808{
David Howellsd0016482016-08-30 20:42:14 +0100809 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700810
David Howellsfc276122019-11-21 09:12:17 +0000811 _enter("{%zu}", iov_iter_count(call->iter));
David Howells08e0e7c2007-04-26 15:55:03 -0700812
813 /* the operation ID forms the first four bytes of the request data */
David Howells12bdcf32018-10-20 00:57:56 +0100814 ret = afs_extract_data(call, true);
David Howellsd0016482016-08-30 20:42:14 +0100815 if (ret < 0)
816 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700817
David Howells50a2c952016-10-13 08:27:10 +0100818 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000819 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howells08e0e7c2007-04-26 15:55:03 -0700820
821 /* ask the cache manager to route the call (it'll change the call type
822 * if successful) */
823 if (!afs_cm_incoming_call(call))
824 return -ENOTSUPP;
825
David Howells8e8d7f12017-01-05 10:38:34 +0000826 trace_afs_cb_call(call);
827
David Howells08e0e7c2007-04-26 15:55:03 -0700828 /* pass responsibility for the remainer of this message off to the
829 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100830 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700831}
832
833/*
David Howellse8332512017-08-29 10:18:56 +0100834 * 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
David Howells98bf40c2017-11-02 15:27:53 +0000843 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100844}
845
846/*
David Howells08e0e7c2007-04-26 15:55:03 -0700847 * send an empty reply
848 */
849void afs_send_empty_reply(struct afs_call *call)
850{
David Howellsf044c882017-11-02 15:27:45 +0000851 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700852 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700853
854 _enter("");
855
David Howellsf044c882017-11-02 15:27:45 +0000856 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100857
David Howells08e0e7c2007-04-26 15:55:03 -0700858 msg.msg_name = NULL;
859 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100860 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700861 msg.msg_control = NULL;
862 msg.msg_controllen = 0;
863 msg.msg_flags = 0;
864
David Howellsf044c882017-11-02 15:27:45 +0000865 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100866 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700867 case 0:
868 _leave(" [replied]");
869 return;
870
871 case -ENOMEM:
872 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000873 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100874 RX_USER_ABORT, -ENOMEM, "KOO");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500875 fallthrough;
David Howells08e0e7c2007-04-26 15:55:03 -0700876 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700877 _leave(" [error]");
878 return;
879 }
880}
881
882/*
David Howellsb908fe62007-04-26 15:58:17 -0700883 * send a simple reply
884 */
885void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
886{
David Howellsf044c882017-11-02 15:27:45 +0000887 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700888 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500889 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100890 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700891
892 _enter("");
893
David Howellsf044c882017-11-02 15:27:45 +0000894 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100895
David Howellsb908fe62007-04-26 15:58:17 -0700896 iov[0].iov_base = (void *) buf;
897 iov[0].iov_len = len;
898 msg.msg_name = NULL;
899 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100900 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700901 msg.msg_control = NULL;
902 msg.msg_controllen = 0;
903 msg.msg_flags = 0;
904
David Howellsf044c882017-11-02 15:27:45 +0000905 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100906 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100907 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100908 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700909 _leave(" [replied]");
910 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100911 }
David Howells6c67c7c2014-05-21 14:48:05 +0100912
David Howellsbd6dc742007-07-20 10:59:41 +0100913 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700914 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000915 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100916 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700917 }
David Howellsbd6dc742007-07-20 10:59:41 +0100918 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700919}
920
921/*
David Howells372ee162016-08-03 14:11:40 +0100922 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700923 */
David Howells12bdcf32018-10-20 00:57:56 +0100924int afs_extract_data(struct afs_call *call, bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700925{
David Howellsf044c882017-11-02 15:27:45 +0000926 struct afs_net *net = call->net;
David Howellsfc276122019-11-21 09:12:17 +0000927 struct iov_iter *iter = call->iter;
David Howells98bf40c2017-11-02 15:27:53 +0000928 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000929 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100930 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700931
David Howells12bdcf32018-10-20 00:57:56 +0100932 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700933
David Howells12bdcf32018-10-20 00:57:56 +0100934 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
David Howells98bf40c2017-11-02 15:27:53 +0000935 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100936 &call->service_id);
David Howellsd0016482016-08-30 20:42:14 +0100937 if (ret == 0 || ret == -EAGAIN)
938 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700939
David Howells98bf40c2017-11-02 15:27:53 +0000940 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100941 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000942 switch (state) {
943 case AFS_CALL_CL_AWAIT_REPLY:
944 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100945 break;
David Howells98bf40c2017-11-02 15:27:53 +0000946 case AFS_CALL_SV_AWAIT_REQUEST:
947 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100948 break;
David Howells98bf40c2017-11-02 15:27:53 +0000949 case AFS_CALL_COMPLETE:
950 kdebug("prem complete %d", call->error);
David Howellsf51375c2018-10-20 00:57:57 +0100951 return afs_io_error(call, afs_io_error_extract);
David Howellsd0016482016-08-30 20:42:14 +0100952 default:
953 break;
954 }
955 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700956 }
David Howellsd0016482016-08-30 20:42:14 +0100957
David Howells98bf40c2017-11-02 15:27:53 +0000958 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +0100959 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700960}
David Howells5f702c82018-04-06 14:17:25 +0100961
962/*
963 * Log protocol error production.
964 */
David Howells7126ead2020-04-08 16:49:08 +0100965noinline int afs_protocol_error(struct afs_call *call,
David Howells160cb952018-10-20 00:57:56 +0100966 enum afs_eproto_cause cause)
David Howells5f702c82018-04-06 14:17:25 +0100967{
David Howells7126ead2020-04-08 16:49:08 +0100968 trace_afs_protocol_error(call, cause);
David Howells38355ee2020-04-08 16:13:20 +0100969 if (call)
970 call->unmarshalling_error = true;
David Howells7126ead2020-04-08 16:49:08 +0100971 return -EBADMSG;
David Howells5f702c82018-04-06 14:17:25 +0100972}