1 // SPDX-License-Identifier: GPL-2.0-or-later
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)
6 */
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"
15 #include "protocol_yfs.h"
16
17 struct workqueue_struct *afs_async_calls;
18
19 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
20 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
21 static void afs_process_async_call(struct work_struct *);
22 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
23 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
24 static int afs_deliver_cm_op_id(struct afs_call *);
25
26 /* asynchronous incoming call initial processing */
27 static 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 */
afs_open_socket(struct afs_net * net)36 int afs_open_socket(struct afs_net *net)
37 {
38 struct sockaddr_rxrpc srx;
39 struct socket *socket;
40 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
59 ret = rxrpc_sock_set_min_security_level(socket->sk,
60 RXRPC_SECURITY_ENCRYPT);
61 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
72 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
84 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
96 error_2:
97 sock_release(socket);
98 error_1:
99 _leave(" = %d", ret);
100 return ret;
101 }
102
103 /*
104 * close the RxRPC socket AFS was using
105 */
afs_close_socket(struct afs_net * net)106 void 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 */
afs_alloc_call(struct afs_net * net,const struct afs_call_type * type,gfp_t gfp)134 static 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);
152 call->iter = &call->def_iter;
153
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 */
afs_put_call(struct afs_call * call)163 void 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
169 trace_afs_call(call, afs_call_trace_put, n, o,
170 __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
184 afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
185 afs_put_addrlist(call->alist);
186 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
afs_get_call(struct afs_call * call,enum afs_call_trace why)198 static struct afs_call *afs_get_call(struct afs_call *call,
199 enum afs_call_trace why)
200 {
201 int u = atomic_inc_return(&call->usage);
202
203 trace_afs_call(call, why, u,
204 atomic_read(&call->net->nr_outstanding_calls),
205 __builtin_return_address(0));
206 return call;
207 }
208
209 /*
210 * Queue the call for actual work.
211 */
afs_queue_call_work(struct afs_call * call)212 static void afs_queue_call_work(struct afs_call *call)
213 {
214 if (call->type->work) {
215 INIT_WORK(&call->work, call->type->work);
216
217 afs_get_call(call, afs_call_trace_work);
218 if (!queue_work(afs_wq, &call->work))
219 afs_put_call(call);
220 }
221 }
222
223 /*
224 * allocate a call with flat request and reply buffers
225 */
afs_alloc_flat_call(struct afs_net * net,const struct afs_call_type * type,size_t request_size,size_t reply_max)226 struct 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
250 afs_extract_to_buf(call, call->reply_max);
251 call->operation_ID = type->op;
252 init_waitqueue_head(&call->waitq);
253 return call;
254
255 nomem_free:
256 afs_put_call(call);
257 nomem_call:
258 return NULL;
259 }
260
261 /*
262 * clean up a call with flat buffer
263 */
afs_flat_call_destructor(struct afs_call * call)264 void 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 */
afs_load_bvec(struct afs_call * call,struct msghdr * msg,struct bio_vec * bv,pgoff_t first,pgoff_t last,unsigned offset)279 static 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 {
283 struct afs_operation *op = call->op;
284 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);
288 n = find_get_pages_contig(op->store.mapping, first, nr, pages);
289 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) {
295 to = op->store.last_to;
296 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
305 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
306 }
307
308 /*
309 * Advance the AFS call state when the RxRPC call ends the transmit phase.
310 */
afs_notify_end_request_tx(struct sock * sock,struct rxrpc_call * rxcall,unsigned long call_user_ID)311 static 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 */
afs_send_pages(struct afs_call * call,struct msghdr * msg)323 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
324 {
325 struct afs_operation *op = call->op;
326 struct bio_vec bv[AFS_BVEC_MAX];
327 unsigned int bytes, nr, loop, offset;
328 pgoff_t first = op->store.first, last = op->store.last;
329 int ret;
330
331 offset = op->store.first_offset;
332 op->store.first_offset = 0;
333
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
342 ret = rxrpc_kernel_send_data(op->net->socket, call->rxcall, msg,
343 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
352 trace_afs_sent_pages(call, op->store.first, last, first, ret);
353 return ret;
354 }
355
356 /*
357 * 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.
359 */
afs_make_call(struct afs_addr_cursor * ac,struct afs_call * call,gfp_t gfp)360 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
361 {
362 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
363 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
378 call->addr_ix = ac->index;
379 call->alist = afs_get_addrlist(ac->alist);
380
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) {
387 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;
391 } 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 */
396 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;
399 }
400 }
401
402 /* 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 */
405 if (call->async) {
406 afs_get_call(call, afs_call_trace_get);
407 call->drop_ref = true;
408 }
409
410 /* create a call */
411 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
412 (unsigned long)call,
413 tx_total_len, gfp,
414 (call->async ?
415 afs_wake_up_async_call :
416 afs_wake_up_call_waiter),
417 call->upgrade,
418 (call->intr ? RXRPC_PREINTERRUPTIBLE :
419 RXRPC_UNINTERRUPTIBLE),
420 call->debug_id);
421 if (IS_ERR(rxcall)) {
422 ret = PTR_ERR(rxcall);
423 call->error = ret;
424 goto error_kill_call;
425 }
426
427 call->rxcall = rxcall;
428
429 if (call->max_lifespan)
430 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
431 call->max_lifespan);
432 call->issue_time = ktime_get_real();
433
434 /* send the request */
435 iov[0].iov_base = call->request;
436 iov[0].iov_len = call->request_size;
437
438 msg.msg_name = NULL;
439 msg.msg_namelen = 0;
440 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
441 msg.msg_control = NULL;
442 msg.msg_controllen = 0;
443 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
444
445 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
446 &msg, call->request_size,
447 afs_notify_end_request_tx);
448 if (ret < 0)
449 goto error_do_abort;
450
451 if (call->send_pages) {
452 ret = afs_send_pages(call, &msg);
453 if (ret < 0)
454 goto error_do_abort;
455 }
456
457 /* Note that at this point, we may have received the reply or an abort
458 * - and an asynchronous call may already have completed.
459 *
460 * afs_wait_for_call_to_complete(call, ac)
461 * must be called to synchronously clean up.
462 */
463 return;
464
465 error_do_abort:
466 if (ret != -ECONNABORTED) {
467 rxrpc_kernel_abort_call(call->net->socket, rxcall,
468 RX_USER_ABORT, ret, "KSD");
469 } else {
470 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
471 rxrpc_kernel_recv_data(call->net->socket, rxcall,
472 &msg.msg_iter, false,
473 &call->abort_code, &call->service_id);
474 ac->abort_code = call->abort_code;
475 ac->responded = true;
476 }
477 call->error = ret;
478 trace_afs_call_done(call);
479 error_kill_call:
480 if (call->type->done)
481 call->type->done(call);
482
483 /* We need to dispose of the extra ref we grabbed for an async call.
484 * The call, however, might be queued on afs_async_calls and we need to
485 * make sure we don't get any more notifications that might requeue it.
486 */
487 if (call->rxcall) {
488 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
489 call->rxcall = NULL;
490 }
491 if (call->async) {
492 if (cancel_work_sync(&call->async_work))
493 afs_put_call(call);
494 afs_put_call(call);
495 }
496
497 ac->error = ret;
498 call->state = AFS_CALL_COMPLETE;
499 _leave(" = %d", ret);
500 }
501
502 /*
503 * deliver messages to a call
504 */
afs_deliver_to_call(struct afs_call * call)505 static void afs_deliver_to_call(struct afs_call *call)
506 {
507 enum afs_call_state state;
508 u32 abort_code, remote_abort = 0;
509 int ret;
510
511 _enter("%s", call->type->name);
512
513 while (state = READ_ONCE(call->state),
514 state == AFS_CALL_CL_AWAIT_REPLY ||
515 state == AFS_CALL_SV_AWAIT_OP_ID ||
516 state == AFS_CALL_SV_AWAIT_REQUEST ||
517 state == AFS_CALL_SV_AWAIT_ACK
518 ) {
519 if (state == AFS_CALL_SV_AWAIT_ACK) {
520 iov_iter_kvec(&call->def_iter, READ, NULL, 0, 0);
521 ret = rxrpc_kernel_recv_data(call->net->socket,
522 call->rxcall, &call->def_iter,
523 false, &remote_abort,
524 &call->service_id);
525 trace_afs_receive_data(call, &call->def_iter, false, ret);
526
527 if (ret == -EINPROGRESS || ret == -EAGAIN)
528 return;
529 if (ret < 0 || ret == 1) {
530 if (ret == 1)
531 ret = 0;
532 goto call_complete;
533 }
534 return;
535 }
536
537 ret = call->type->deliver(call);
538 state = READ_ONCE(call->state);
539 if (ret == 0 && call->unmarshalling_error)
540 ret = -EBADMSG;
541 switch (ret) {
542 case 0:
543 afs_queue_call_work(call);
544 if (state == AFS_CALL_CL_PROC_REPLY) {
545 if (call->op)
546 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
547 &call->op->server->flags);
548 goto call_complete;
549 }
550 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
551 goto done;
552 case -EINPROGRESS:
553 case -EAGAIN:
554 goto out;
555 case -ECONNABORTED:
556 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
557 goto done;
558 case -ENOTSUPP:
559 abort_code = RXGEN_OPCODE;
560 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
561 abort_code, ret, "KIV");
562 goto local_abort;
563 case -EIO:
564 pr_err("kAFS: Call %u in bad state %u\n",
565 call->debug_id, state);
566 fallthrough;
567 case -ENODATA:
568 case -EBADMSG:
569 case -EMSGSIZE:
570 case -ENOMEM:
571 case -EFAULT:
572 abort_code = RXGEN_CC_UNMARSHAL;
573 if (state != AFS_CALL_CL_AWAIT_REPLY)
574 abort_code = RXGEN_SS_UNMARSHAL;
575 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
576 abort_code, ret, "KUM");
577 goto local_abort;
578 default:
579 abort_code = RX_CALL_DEAD;
580 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
581 abort_code, ret, "KER");
582 goto local_abort;
583 }
584 }
585
586 done:
587 if (call->type->done)
588 call->type->done(call);
589 out:
590 _leave("");
591 return;
592
593 local_abort:
594 abort_code = 0;
595 call_complete:
596 afs_set_call_complete(call, ret, remote_abort);
597 state = AFS_CALL_COMPLETE;
598 goto done;
599 }
600
601 /*
602 * Wait synchronously for a call to complete and clean up the call struct.
603 */
afs_wait_for_call_to_complete(struct afs_call * call,struct afs_addr_cursor * ac)604 long afs_wait_for_call_to_complete(struct afs_call *call,
605 struct afs_addr_cursor *ac)
606 {
607 long ret;
608 bool rxrpc_complete = false;
609
610 DECLARE_WAITQUEUE(myself, current);
611
612 _enter("");
613
614 ret = call->error;
615 if (ret < 0)
616 goto out;
617
618 add_wait_queue(&call->waitq, &myself);
619 for (;;) {
620 set_current_state(TASK_UNINTERRUPTIBLE);
621
622 /* deliver any messages that are in the queue */
623 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
624 call->need_attention) {
625 call->need_attention = false;
626 __set_current_state(TASK_RUNNING);
627 afs_deliver_to_call(call);
628 continue;
629 }
630
631 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
632 break;
633
634 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
635 /* rxrpc terminated the call. */
636 rxrpc_complete = true;
637 break;
638 }
639
640 schedule();
641 }
642
643 remove_wait_queue(&call->waitq, &myself);
644 __set_current_state(TASK_RUNNING);
645
646 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
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 }
656 }
657
658 spin_lock_bh(&call->state_lock);
659 ac->abort_code = call->abort_code;
660 ac->error = call->error;
661 spin_unlock_bh(&call->state_lock);
662
663 ret = ac->error;
664 switch (ret) {
665 case 0:
666 ret = call->ret0;
667 call->ret0 = 0;
668
669 fallthrough;
670 case -ECONNABORTED:
671 ac->responded = true;
672 break;
673 }
674
675 out:
676 _debug("call complete");
677 afs_put_call(call);
678 _leave(" = %p", (void *)ret);
679 return ret;
680 }
681
682 /*
683 * wake up a waiting call
684 */
afs_wake_up_call_waiter(struct sock * sk,struct rxrpc_call * rxcall,unsigned long call_user_ID)685 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
686 unsigned long call_user_ID)
687 {
688 struct afs_call *call = (struct afs_call *)call_user_ID;
689
690 call->need_attention = true;
691 wake_up(&call->waitq);
692 }
693
694 /*
695 * wake up an asynchronous call
696 */
afs_wake_up_async_call(struct sock * sk,struct rxrpc_call * rxcall,unsigned long call_user_ID)697 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
698 unsigned long call_user_ID)
699 {
700 struct afs_call *call = (struct afs_call *)call_user_ID;
701 int u;
702
703 trace_afs_notify_call(rxcall, call);
704 call->need_attention = true;
705
706 u = atomic_fetch_add_unless(&call->usage, 1, 0);
707 if (u != 0) {
708 trace_afs_call(call, afs_call_trace_wake, u + 1,
709 atomic_read(&call->net->nr_outstanding_calls),
710 __builtin_return_address(0));
711
712 if (!queue_work(afs_async_calls, &call->async_work))
713 afs_put_call(call);
714 }
715 }
716
717 /*
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.
720 */
afs_process_async_call(struct work_struct * work)721 static void afs_process_async_call(struct work_struct *work)
722 {
723 struct afs_call *call = container_of(work, struct afs_call, async_work);
724
725 _enter("");
726
727 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
728 call->need_attention = false;
729 afs_deliver_to_call(call);
730 }
731
732 afs_put_call(call);
733 _leave("");
734 }
735
afs_rx_attach(struct rxrpc_call * rxcall,unsigned long user_call_ID)736 static 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 */
afs_charge_preallocation(struct work_struct * work)746 void afs_charge_preallocation(struct work_struct *work)
747 {
748 struct afs_net *net =
749 container_of(work, struct afs_net, charge_preallocation_work);
750 struct afs_call *call = net->spare_incoming_call;
751
752 for (;;) {
753 if (!call) {
754 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
755 if (!call)
756 break;
757
758 call->drop_ref = true;
759 call->async = true;
760 call->state = AFS_CALL_SV_AWAIT_OP_ID;
761 init_waitqueue_head(&call->waitq);
762 afs_extract_to_tmp(call);
763 }
764
765 if (rxrpc_kernel_charge_accept(net->socket,
766 afs_wake_up_async_call,
767 afs_rx_attach,
768 (unsigned long)call,
769 GFP_KERNEL,
770 call->debug_id) < 0)
771 break;
772 call = NULL;
773 }
774 net->spare_incoming_call = call;
775 }
776
777 /*
778 * Discard a preallocated call when a socket is shut down.
779 */
afs_rx_discard_new_call(struct rxrpc_call * rxcall,unsigned long user_call_ID)780 static 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
785 call->rxcall = NULL;
786 afs_put_call(call);
787 }
788
789 /*
790 * Notification of an incoming call.
791 */
afs_rx_new_call(struct sock * sk,struct rxrpc_call * rxcall,unsigned long user_call_ID)792 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
793 unsigned long user_call_ID)
794 {
795 struct afs_net *net = afs_sock2net(sk);
796
797 queue_work(afs_wq, &net->charge_preallocation_work);
798 }
799
800 /*
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.
803 */
afs_deliver_cm_op_id(struct afs_call * call)804 static int afs_deliver_cm_op_id(struct afs_call *call)
805 {
806 int ret;
807
808 _enter("{%zu}", iov_iter_count(call->iter));
809
810 /* the operation ID forms the first four bytes of the request data */
811 ret = afs_extract_data(call, true);
812 if (ret < 0)
813 return ret;
814
815 call->operation_ID = ntohl(call->tmp);
816 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
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
823 trace_afs_cb_call(call);
824
825 /* pass responsibility for the remainer of this message off to the
826 * cache manager op */
827 return call->type->deliver(call);
828 }
829
830 /*
831 * Advance the AFS call state when an RxRPC service call ends the transmit
832 * phase.
833 */
afs_notify_end_reply_tx(struct sock * sock,struct rxrpc_call * rxcall,unsigned long call_user_ID)834 static 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
840 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
841 }
842
843 /*
844 * send an empty reply
845 */
afs_send_empty_reply(struct afs_call * call)846 void afs_send_empty_reply(struct afs_call *call)
847 {
848 struct afs_net *net = call->net;
849 struct msghdr msg;
850
851 _enter("");
852
853 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
854
855 msg.msg_name = NULL;
856 msg.msg_namelen = 0;
857 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
858 msg.msg_control = NULL;
859 msg.msg_controllen = 0;
860 msg.msg_flags = 0;
861
862 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
863 afs_notify_end_reply_tx)) {
864 case 0:
865 _leave(" [replied]");
866 return;
867
868 case -ENOMEM:
869 _debug("oom");
870 rxrpc_kernel_abort_call(net->socket, call->rxcall,
871 RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
872 fallthrough;
873 default:
874 _leave(" [error]");
875 return;
876 }
877 }
878
879 /*
880 * send a simple reply
881 */
afs_send_simple_reply(struct afs_call * call,const void * buf,size_t len)882 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
883 {
884 struct afs_net *net = call->net;
885 struct msghdr msg;
886 struct kvec iov[1];
887 int n;
888
889 _enter("");
890
891 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
892
893 iov[0].iov_base = (void *) buf;
894 iov[0].iov_len = len;
895 msg.msg_name = NULL;
896 msg.msg_namelen = 0;
897 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
898 msg.msg_control = NULL;
899 msg.msg_controllen = 0;
900 msg.msg_flags = 0;
901
902 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
903 afs_notify_end_reply_tx);
904 if (n >= 0) {
905 /* Success */
906 _leave(" [replied]");
907 return;
908 }
909
910 if (n == -ENOMEM) {
911 _debug("oom");
912 rxrpc_kernel_abort_call(net->socket, call->rxcall,
913 RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
914 }
915 _leave(" [error]");
916 }
917
918 /*
919 * Extract a piece of data from the received data socket buffers.
920 */
afs_extract_data(struct afs_call * call,bool want_more)921 int afs_extract_data(struct afs_call *call, bool want_more)
922 {
923 struct afs_net *net = call->net;
924 struct iov_iter *iter = call->iter;
925 enum afs_call_state state;
926 u32 remote_abort = 0;
927 int ret;
928
929 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
930
931 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
932 want_more, &remote_abort,
933 &call->service_id);
934 if (ret == 0 || ret == -EAGAIN)
935 return ret;
936
937 state = READ_ONCE(call->state);
938 if (ret == 1) {
939 switch (state) {
940 case AFS_CALL_CL_AWAIT_REPLY:
941 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
942 break;
943 case AFS_CALL_SV_AWAIT_REQUEST:
944 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
945 break;
946 case AFS_CALL_COMPLETE:
947 kdebug("prem complete %d", call->error);
948 return afs_io_error(call, afs_io_error_extract);
949 default:
950 break;
951 }
952 return 0;
953 }
954
955 afs_set_call_complete(call, ret, remote_abort);
956 return ret;
957 }
958
959 /*
960 * Log protocol error production.
961 */
afs_protocol_error(struct afs_call * call,enum afs_eproto_cause cause)962 noinline int afs_protocol_error(struct afs_call *call,
963 enum afs_eproto_cause cause)
964 {
965 trace_afs_protocol_error(call, cause);
966 if (call)
967 call->unmarshalling_error = true;
968 return -EBADMSG;
969 }
970