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