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