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