blob: 8be709cb8542aab247fb390063a51bef8eb8849c [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/* 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)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7
8#include <linux/slab.h>
9#include <linux/sched/signal.h>
10
11#include <net/sock.h>
12#include <net/af_rxrpc.h>
13#include "internal.h"
14#include "afs_cm.h"
David Brazdil0f672f62019-12-10 10:32:29 +000015#include "protocol_yfs.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016
17struct workqueue_struct *afs_async_calls;
18
19static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
21static void afs_process_async_call(struct work_struct *);
22static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
23static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
24static int afs_deliver_cm_op_id(struct afs_call *);
25
26/* asynchronous incoming call initial processing */
27static const struct afs_call_type afs_RXCMxxxx = {
28 .name = "CB.xxxx",
29 .deliver = afs_deliver_cm_op_id,
30};
31
32/*
33 * open an RxRPC socket and bind it to be a server for callback notifications
34 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
35 */
36int afs_open_socket(struct afs_net *net)
37{
38 struct sockaddr_rxrpc srx;
39 struct socket *socket;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040 int ret;
41
42 _enter("");
43
44 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
45 if (ret < 0)
46 goto error_1;
47
48 socket->sk->sk_allocation = GFP_NOFS;
49
50 /* bind the callback manager's address to make this a server socket */
51 memset(&srx, 0, sizeof(srx));
52 srx.srx_family = AF_RXRPC;
53 srx.srx_service = CM_SERVICE;
54 srx.transport_type = SOCK_DGRAM;
55 srx.transport_len = sizeof(srx.transport.sin6);
56 srx.transport.sin6.sin6_family = AF_INET6;
57 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
58
Olivier Deprez157378f2022-04-04 15:47:50 +020059 ret = rxrpc_sock_set_min_security_level(socket->sk,
60 RXRPC_SECURITY_ENCRYPT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061 if (ret < 0)
62 goto error_2;
63
64 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
65 if (ret == -EADDRINUSE) {
66 srx.transport.sin6.sin6_port = 0;
67 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
68 }
69 if (ret < 0)
70 goto error_2;
71
David Brazdil0f672f62019-12-10 10:32:29 +000072 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
77 /* Ideally, we'd turn on service upgrade here, but we can't because
78 * OpenAFS is buggy and leaks the userStatus field from packet to
79 * packet and between FS packets and CB packets - so if we try to do an
80 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
81 * it sends back to us.
82 */
83
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
85 afs_rx_discard_new_call);
86
87 ret = kernel_listen(socket, INT_MAX);
88 if (ret < 0)
89 goto error_2;
90
91 net->socket = socket;
92 afs_charge_preallocation(&net->charge_preallocation_work);
93 _leave(" = 0");
94 return 0;
95
96error_2:
97 sock_release(socket);
98error_1:
99 _leave(" = %d", ret);
100 return ret;
101}
102
103/*
104 * close the RxRPC socket AFS was using
105 */
106void afs_close_socket(struct afs_net *net)
107{
108 _enter("");
109
110 kernel_listen(net->socket, 0);
111 flush_workqueue(afs_async_calls);
112
113 if (net->spare_incoming_call) {
114 afs_put_call(net->spare_incoming_call);
115 net->spare_incoming_call = NULL;
116 }
117
118 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
119 wait_var_event(&net->nr_outstanding_calls,
120 !atomic_read(&net->nr_outstanding_calls));
121 _debug("no outstanding calls");
122
123 kernel_sock_shutdown(net->socket, SHUT_RDWR);
124 flush_workqueue(afs_async_calls);
125 sock_release(net->socket);
126
127 _debug("dework");
128 _leave("");
129}
130
131/*
132 * Allocate a call.
133 */
134static struct afs_call *afs_alloc_call(struct afs_net *net,
135 const struct afs_call_type *type,
136 gfp_t gfp)
137{
138 struct afs_call *call;
139 int o;
140
141 call = kzalloc(sizeof(*call), gfp);
142 if (!call)
143 return NULL;
144
145 call->type = type;
146 call->net = net;
147 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
148 atomic_set(&call->usage, 1);
149 INIT_WORK(&call->async_work, afs_process_async_call);
150 init_waitqueue_head(&call->waitq);
151 spin_lock_init(&call->state_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +0200152 call->iter = &call->def_iter;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153
154 o = atomic_inc_return(&net->nr_outstanding_calls);
155 trace_afs_call(call, afs_call_trace_alloc, 1, o,
156 __builtin_return_address(0));
157 return call;
158}
159
160/*
161 * Dispose of a reference on a call.
162 */
163void afs_put_call(struct afs_call *call)
164{
165 struct afs_net *net = call->net;
166 int n = atomic_dec_return(&call->usage);
167 int o = atomic_read(&net->nr_outstanding_calls);
168
Olivier Deprez0e641232021-09-23 10:07:05 +0200169 trace_afs_call(call, afs_call_trace_put, n, o,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +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) {
178 rxrpc_kernel_end_call(net->socket, call->rxcall);
179 call->rxcall = NULL;
180 }
181 if (call->type->destructor)
182 call->type->destructor(call);
183
Olivier Deprez157378f2022-04-04 15:47:50 +0200184 afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
David Brazdil0f672f62019-12-10 10:32:29 +0000185 afs_put_addrlist(call->alist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000186 kfree(call->request);
187
188 trace_afs_call(call, afs_call_trace_free, 0, o,
189 __builtin_return_address(0));
190 kfree(call);
191
192 o = atomic_dec_return(&net->nr_outstanding_calls);
193 if (o == 0)
194 wake_up_var(&net->nr_outstanding_calls);
195 }
196}
197
David Brazdil0f672f62019-12-10 10:32:29 +0000198static struct afs_call *afs_get_call(struct afs_call *call,
199 enum afs_call_trace why)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200{
201 int u = atomic_inc_return(&call->usage);
202
David Brazdil0f672f62019-12-10 10:32:29 +0000203 trace_afs_call(call, why, u,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204 atomic_read(&call->net->nr_outstanding_calls),
205 __builtin_return_address(0));
David Brazdil0f672f62019-12-10 10:32:29 +0000206 return call;
207}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000208
David Brazdil0f672f62019-12-10 10:32:29 +0000209/*
210 * Queue the call for actual work.
211 */
212static void afs_queue_call_work(struct afs_call *call)
213{
214 if (call->type->work) {
215 INIT_WORK(&call->work, call->type->work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216
David Brazdil0f672f62019-12-10 10:32:29 +0000217 afs_get_call(call, afs_call_trace_work);
218 if (!queue_work(afs_wq, &call->work))
219 afs_put_call(call);
220 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221}
222
223/*
224 * allocate a call with flat request and reply buffers
225 */
226struct afs_call *afs_alloc_flat_call(struct afs_net *net,
227 const struct afs_call_type *type,
228 size_t request_size, size_t reply_max)
229{
230 struct afs_call *call;
231
232 call = afs_alloc_call(net, type, GFP_NOFS);
233 if (!call)
234 goto nomem_call;
235
236 if (request_size) {
237 call->request_size = request_size;
238 call->request = kmalloc(request_size, GFP_NOFS);
239 if (!call->request)
240 goto nomem_free;
241 }
242
243 if (reply_max) {
244 call->reply_max = reply_max;
245 call->buffer = kmalloc(reply_max, GFP_NOFS);
246 if (!call->buffer)
247 goto nomem_free;
248 }
249
David Brazdil0f672f62019-12-10 10:32:29 +0000250 afs_extract_to_buf(call, call->reply_max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251 call->operation_ID = type->op;
252 init_waitqueue_head(&call->waitq);
253 return call;
254
255nomem_free:
256 afs_put_call(call);
257nomem_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
274#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{
Olivier Deprez157378f2022-04-04 15:47:50 +0200283 struct afs_operation *op = call->op;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +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);
Olivier Deprez157378f2022-04-04 15:47:50 +0200288 n = find_get_pages_contig(op->store.mapping, first, nr, pages);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +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) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200295 to = op->store.last_to;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +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 Brazdil0f672f62019-12-10 10:32:29 +0000305 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306}
307
308/*
309 * 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
317 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
318}
319
320/*
321 * attach the data from a bunch of pages on an inode to a call
322 */
323static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
324{
Olivier Deprez157378f2022-04-04 15:47:50 +0200325 struct afs_operation *op = call->op;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326 struct bio_vec bv[AFS_BVEC_MAX];
327 unsigned int bytes, nr, loop, offset;
Olivier Deprez157378f2022-04-04 15:47:50 +0200328 pgoff_t first = op->store.first, last = op->store.last;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329 int ret;
330
Olivier Deprez157378f2022-04-04 15:47:50 +0200331 offset = op->store.first_offset;
332 op->store.first_offset = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000333
334 do {
335 afs_load_bvec(call, msg, bv, first, last, offset);
336 trace_afs_send_pages(call, msg, first, last, offset);
337
338 offset = 0;
339 bytes = msg->msg_iter.count;
340 nr = msg->msg_iter.nr_segs;
341
Olivier Deprez157378f2022-04-04 15:47:50 +0200342 ret = rxrpc_kernel_send_data(op->net->socket, call->rxcall, msg,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000343 bytes, afs_notify_end_request_tx);
344 for (loop = 0; loop < nr; loop++)
345 put_page(bv[loop].bv_page);
346 if (ret < 0)
347 break;
348
349 first += nr;
350 } while (first <= last);
351
Olivier Deprez157378f2022-04-04 15:47:50 +0200352 trace_afs_sent_pages(call, op->store.first, last, first, ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353 return ret;
354}
355
356/*
David Brazdil0f672f62019-12-10 10:32:29 +0000357 * 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.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000359 */
David Brazdil0f672f62019-12-10 10:32:29 +0000360void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361{
David Brazdil0f672f62019-12-10 10:32:29 +0000362 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000363 struct rxrpc_call *rxcall;
364 struct msghdr msg;
365 struct kvec iov[1];
366 s64 tx_total_len;
367 int ret;
368
369 _enter(",{%pISp},", &srx->transport);
370
371 ASSERT(call->type != NULL);
372 ASSERT(call->type->name != NULL);
373
374 _debug("____MAKE %p{%s,%x} [%d]____",
375 call, call->type->name, key_serial(call->key),
376 atomic_read(&call->net->nr_outstanding_calls));
377
David Brazdil0f672f62019-12-10 10:32:29 +0000378 call->addr_ix = ac->index;
379 call->alist = afs_get_addrlist(ac->alist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000380
381 /* 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) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200387 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +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 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200396 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 }
400 }
401
David Brazdil0f672f62019-12-10 10:32:29 +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 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200405 if (call->async) {
David Brazdil0f672f62019-12-10 10:32:29 +0000406 afs_get_call(call, afs_call_trace_get);
Olivier Deprez0e641232021-09-23 10:07:05 +0200407 call->drop_ref = true;
408 }
David Brazdil0f672f62019-12-10 10:32:29 +0000409
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000410 /* create a call */
411 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
412 (unsigned long)call,
413 tx_total_len, gfp,
David Brazdil0f672f62019-12-10 10:32:29 +0000414 (call->async ?
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000415 afs_wake_up_async_call :
416 afs_wake_up_call_waiter),
417 call->upgrade,
Olivier Deprez0e641232021-09-23 10:07:05 +0200418 (call->intr ? RXRPC_PREINTERRUPTIBLE :
419 RXRPC_UNINTERRUPTIBLE),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000420 call->debug_id);
421 if (IS_ERR(rxcall)) {
422 ret = PTR_ERR(rxcall);
David Brazdil0f672f62019-12-10 10:32:29 +0000423 call->error = ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000424 goto error_kill_call;
425 }
426
427 call->rxcall = rxcall;
428
David Brazdil0f672f62019-12-10 10:32:29 +0000429 if (call->max_lifespan)
430 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
431 call->max_lifespan);
432
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000433 /* 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 Brazdil0f672f62019-12-10 10:32:29 +0000439 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000440 msg.msg_control = NULL;
441 msg.msg_controllen = 0;
442 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
443
444 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
445 &msg, call->request_size,
446 afs_notify_end_request_tx);
447 if (ret < 0)
448 goto error_do_abort;
449
450 if (call->send_pages) {
451 ret = afs_send_pages(call, &msg);
452 if (ret < 0)
453 goto error_do_abort;
454 }
455
David Brazdil0f672f62019-12-10 10:32:29 +0000456 /* Note that at this point, we may have received the reply or an abort
457 * - and an asynchronous call may already have completed.
458 *
459 * afs_wait_for_call_to_complete(call, ac)
460 * must be called to synchronously clean up.
461 */
462 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000463
464error_do_abort:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000465 if (ret != -ECONNABORTED) {
466 rxrpc_kernel_abort_call(call->net->socket, rxcall,
467 RX_USER_ABORT, ret, "KSD");
468 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000469 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000470 rxrpc_kernel_recv_data(call->net->socket, rxcall,
471 &msg.msg_iter, false,
472 &call->abort_code, &call->service_id);
473 ac->abort_code = call->abort_code;
474 ac->responded = true;
475 }
476 call->error = ret;
477 trace_afs_call_done(call);
478error_kill_call:
David Brazdil0f672f62019-12-10 10:32:29 +0000479 if (call->type->done)
480 call->type->done(call);
481
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000496 ac->error = ret;
David Brazdil0f672f62019-12-10 10:32:29 +0000497 call->state = AFS_CALL_COMPLETE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000498 _leave(" = %d", ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000499}
500
501/*
502 * deliver messages to a call
503 */
504static void afs_deliver_to_call(struct afs_call *call)
505{
506 enum afs_call_state state;
507 u32 abort_code, remote_abort = 0;
508 int ret;
509
510 _enter("%s", call->type->name);
511
512 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
517 ) {
518 if (state == AFS_CALL_SV_AWAIT_ACK) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200519 iov_iter_kvec(&call->def_iter, READ, NULL, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520 ret = rxrpc_kernel_recv_data(call->net->socket,
Olivier Deprez157378f2022-04-04 15:47:50 +0200521 call->rxcall, &call->def_iter,
David Brazdil0f672f62019-12-10 10:32:29 +0000522 false, &remote_abort,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 &call->service_id);
Olivier Deprez157378f2022-04-04 15:47:50 +0200524 trace_afs_receive_data(call, &call->def_iter, false, ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000525
526 if (ret == -EINPROGRESS || ret == -EAGAIN)
527 return;
528 if (ret < 0 || ret == 1) {
529 if (ret == 1)
530 ret = 0;
531 goto call_complete;
532 }
533 return;
534 }
535
David Brazdil0f672f62019-12-10 10:32:29 +0000536 if (!call->have_reply_time &&
537 rxrpc_kernel_get_reply_time(call->net->socket,
538 call->rxcall,
539 &call->reply_time))
540 call->have_reply_time = true;
541
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000542 ret = call->type->deliver(call);
543 state = READ_ONCE(call->state);
Olivier Deprez0e641232021-09-23 10:07:05 +0200544 if (ret == 0 && call->unmarshalling_error)
545 ret = -EBADMSG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000546 switch (ret) {
547 case 0:
David Brazdil0f672f62019-12-10 10:32:29 +0000548 afs_queue_call_work(call);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549 if (state == AFS_CALL_CL_PROC_REPLY) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200550 if (call->op)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
Olivier Deprez157378f2022-04-04 15:47:50 +0200552 &call->op->server->flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000553 goto call_complete;
554 }
555 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
556 goto done;
557 case -EINPROGRESS:
558 case -EAGAIN:
559 goto out;
560 case -ECONNABORTED:
561 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
562 goto done;
563 case -ENOTSUPP:
564 abort_code = RXGEN_OPCODE;
565 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
566 abort_code, ret, "KIV");
567 goto local_abort;
568 case -EIO:
569 pr_err("kAFS: Call %u in bad state %u\n",
570 call->debug_id, state);
Olivier Deprez157378f2022-04-04 15:47:50 +0200571 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572 case -ENODATA:
573 case -EBADMSG:
574 case -EMSGSIZE:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000575 abort_code = RXGEN_CC_UNMARSHAL;
576 if (state != AFS_CALL_CL_AWAIT_REPLY)
577 abort_code = RXGEN_SS_UNMARSHAL;
578 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Brazdil0f672f62019-12-10 10:32:29 +0000579 abort_code, ret, "KUM");
580 goto local_abort;
581 default:
582 abort_code = RX_USER_ABORT;
583 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
584 abort_code, ret, "KER");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000585 goto local_abort;
586 }
587 }
588
589done:
David Brazdil0f672f62019-12-10 10:32:29 +0000590 if (call->type->done)
591 call->type->done(call);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000592out:
593 _leave("");
594 return;
595
596local_abort:
597 abort_code = 0;
598call_complete:
599 afs_set_call_complete(call, ret, remote_abort);
600 state = AFS_CALL_COMPLETE;
601 goto done;
602}
603
604/*
David Brazdil0f672f62019-12-10 10:32:29 +0000605 * Wait synchronously for a call to complete and clean up the call struct.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000606 */
David Brazdil0f672f62019-12-10 10:32:29 +0000607long afs_wait_for_call_to_complete(struct afs_call *call,
608 struct afs_addr_cursor *ac)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000609{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000610 long ret;
David Brazdil0f672f62019-12-10 10:32:29 +0000611 bool rxrpc_complete = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000612
613 DECLARE_WAITQUEUE(myself, current);
614
615 _enter("");
616
David Brazdil0f672f62019-12-10 10:32:29 +0000617 ret = call->error;
618 if (ret < 0)
619 goto out;
620
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000621 add_wait_queue(&call->waitq, &myself);
622 for (;;) {
623 set_current_state(TASK_UNINTERRUPTIBLE);
624
625 /* deliver any messages that are in the queue */
626 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
627 call->need_attention) {
628 call->need_attention = false;
629 __set_current_state(TASK_RUNNING);
630 afs_deliver_to_call(call);
631 continue;
632 }
633
634 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
635 break;
636
Olivier Deprez0e641232021-09-23 10:07:05 +0200637 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000638 /* rxrpc terminated the call. */
639 rxrpc_complete = true;
640 break;
641 }
642
Olivier Deprez0e641232021-09-23 10:07:05 +0200643 schedule();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644 }
645
646 remove_wait_queue(&call->waitq, &myself);
647 __set_current_state(TASK_RUNNING);
648
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000650 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 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000659 }
660
661 spin_lock_bh(&call->state_lock);
662 ac->abort_code = call->abort_code;
663 ac->error = call->error;
664 spin_unlock_bh(&call->state_lock);
665
666 ret = ac->error;
667 switch (ret) {
668 case 0:
David Brazdil0f672f62019-12-10 10:32:29 +0000669 ret = call->ret0;
670 call->ret0 = 0;
671
Olivier Deprez157378f2022-04-04 15:47:50 +0200672 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000673 case -ECONNABORTED:
674 ac->responded = true;
675 break;
676 }
677
David Brazdil0f672f62019-12-10 10:32:29 +0000678out:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679 _debug("call complete");
680 afs_put_call(call);
681 _leave(" = %p", (void *)ret);
682 return ret;
683}
684
685/*
686 * wake up a waiting call
687 */
688static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
689 unsigned long call_user_ID)
690{
691 struct afs_call *call = (struct afs_call *)call_user_ID;
692
693 call->need_attention = true;
694 wake_up(&call->waitq);
695}
696
697/*
698 * wake up an asynchronous call
699 */
700static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
701 unsigned long call_user_ID)
702{
703 struct afs_call *call = (struct afs_call *)call_user_ID;
704 int u;
705
706 trace_afs_notify_call(rxcall, call);
707 call->need_attention = true;
708
709 u = atomic_fetch_add_unless(&call->usage, 1, 0);
710 if (u != 0) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200711 trace_afs_call(call, afs_call_trace_wake, u + 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000712 atomic_read(&call->net->nr_outstanding_calls),
713 __builtin_return_address(0));
714
715 if (!queue_work(afs_async_calls, &call->async_work))
716 afs_put_call(call);
717 }
718}
719
720/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +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.
723 */
724static void afs_process_async_call(struct work_struct *work)
725{
726 struct afs_call *call = container_of(work, struct afs_call, async_work);
727
728 _enter("");
729
730 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
731 call->need_attention = false;
732 afs_deliver_to_call(call);
733 }
734
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000735 afs_put_call(call);
736 _leave("");
737}
738
739static 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 */
749void afs_charge_preallocation(struct work_struct *work)
750{
751 struct afs_net *net =
752 container_of(work, struct afs_net, charge_preallocation_work);
753 struct afs_call *call = net->spare_incoming_call;
754
755 for (;;) {
756 if (!call) {
757 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
758 if (!call)
759 break;
760
Olivier Deprez0e641232021-09-23 10:07:05 +0200761 call->drop_ref = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000762 call->async = true;
763 call->state = AFS_CALL_SV_AWAIT_OP_ID;
764 init_waitqueue_head(&call->waitq);
David Brazdil0f672f62019-12-10 10:32:29 +0000765 afs_extract_to_tmp(call);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000766 }
767
768 if (rxrpc_kernel_charge_accept(net->socket,
769 afs_wake_up_async_call,
770 afs_rx_attach,
771 (unsigned long)call,
772 GFP_KERNEL,
773 call->debug_id) < 0)
774 break;
775 call = NULL;
776 }
777 net->spare_incoming_call = call;
778}
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
788 call->rxcall = NULL;
789 afs_put_call(call);
790}
791
792/*
793 * Notification of an incoming call.
794 */
795static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
796 unsigned long user_call_ID)
797{
798 struct afs_net *net = afs_sock2net(sk);
799
800 queue_work(afs_wq, &net->charge_preallocation_work);
801}
802
803/*
804 * 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.
806 */
807static int afs_deliver_cm_op_id(struct afs_call *call)
808{
809 int ret;
810
Olivier Deprez157378f2022-04-04 15:47:50 +0200811 _enter("{%zu}", iov_iter_count(call->iter));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000812
813 /* the operation ID forms the first four bytes of the request data */
David Brazdil0f672f62019-12-10 10:32:29 +0000814 ret = afs_extract_data(call, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000815 if (ret < 0)
816 return ret;
817
818 call->operation_ID = ntohl(call->tmp);
819 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820
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
826 trace_afs_cb_call(call);
827
828 /* pass responsibility for the remainer of this message off to the
829 * cache manager op */
830 return call->type->deliver(call);
831}
832
833/*
834 * Advance the AFS call state when an RxRPC service call ends the transmit
835 * phase.
836 */
837static void afs_notify_end_reply_tx(struct sock *sock,
838 struct rxrpc_call *rxcall,
839 unsigned long call_user_ID)
840{
841 struct afs_call *call = (struct afs_call *)call_user_ID;
842
843 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
844}
845
846/*
847 * send an empty reply
848 */
849void afs_send_empty_reply(struct afs_call *call)
850{
851 struct afs_net *net = call->net;
852 struct msghdr msg;
853
854 _enter("");
855
856 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
857
858 msg.msg_name = NULL;
859 msg.msg_namelen = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000860 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861 msg.msg_control = NULL;
862 msg.msg_controllen = 0;
863 msg.msg_flags = 0;
864
865 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
866 afs_notify_end_reply_tx)) {
867 case 0:
868 _leave(" [replied]");
869 return;
870
871 case -ENOMEM:
872 _debug("oom");
873 rxrpc_kernel_abort_call(net->socket, call->rxcall,
874 RX_USER_ABORT, -ENOMEM, "KOO");
Olivier Deprez157378f2022-04-04 15:47:50 +0200875 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000876 default:
877 _leave(" [error]");
878 return;
879 }
880}
881
882/*
883 * send a simple reply
884 */
885void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
886{
887 struct afs_net *net = call->net;
888 struct msghdr msg;
889 struct kvec iov[1];
890 int n;
891
892 _enter("");
893
894 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
895
896 iov[0].iov_base = (void *) buf;
897 iov[0].iov_len = len;
898 msg.msg_name = NULL;
899 msg.msg_namelen = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000900 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901 msg.msg_control = NULL;
902 msg.msg_controllen = 0;
903 msg.msg_flags = 0;
904
905 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
906 afs_notify_end_reply_tx);
907 if (n >= 0) {
908 /* Success */
909 _leave(" [replied]");
910 return;
911 }
912
913 if (n == -ENOMEM) {
914 _debug("oom");
915 rxrpc_kernel_abort_call(net->socket, call->rxcall,
916 RX_USER_ABORT, -ENOMEM, "KOO");
917 }
918 _leave(" [error]");
919}
920
921/*
922 * Extract a piece of data from the received data socket buffers.
923 */
David Brazdil0f672f62019-12-10 10:32:29 +0000924int afs_extract_data(struct afs_call *call, bool want_more)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000925{
926 struct afs_net *net = call->net;
Olivier Deprez157378f2022-04-04 15:47:50 +0200927 struct iov_iter *iter = call->iter;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000928 enum afs_call_state state;
929 u32 remote_abort = 0;
930 int ret;
931
David Brazdil0f672f62019-12-10 10:32:29 +0000932 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000933
David Brazdil0f672f62019-12-10 10:32:29 +0000934 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000935 want_more, &remote_abort,
936 &call->service_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000937 if (ret == 0 || ret == -EAGAIN)
938 return ret;
939
940 state = READ_ONCE(call->state);
941 if (ret == 1) {
942 switch (state) {
943 case AFS_CALL_CL_AWAIT_REPLY:
944 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
945 break;
946 case AFS_CALL_SV_AWAIT_REQUEST:
947 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
948 break;
949 case AFS_CALL_COMPLETE:
950 kdebug("prem complete %d", call->error);
David Brazdil0f672f62019-12-10 10:32:29 +0000951 return afs_io_error(call, afs_io_error_extract);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000952 default:
953 break;
954 }
955 return 0;
956 }
957
958 afs_set_call_complete(call, ret, remote_abort);
959 return ret;
960}
961
962/*
963 * Log protocol error production.
964 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200965noinline int afs_protocol_error(struct afs_call *call,
David Brazdil0f672f62019-12-10 10:32:29 +0000966 enum afs_eproto_cause cause)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000967{
Olivier Deprez157378f2022-04-04 15:47:50 +0200968 trace_afs_protocol_error(call, cause);
Olivier Deprez0e641232021-09-23 10:07:05 +0200969 if (call)
970 call->unmarshalling_error = true;
Olivier Deprez157378f2022-04-04 15:47:50 +0200971 return -EBADMSG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000972}