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