GNU Linux-libre 5.15.137-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
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  */
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  */
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  */
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  */
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
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  */
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  */
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  */
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 /*
275  * Advance the AFS call state when the RxRPC call ends the transmit phase.
276  */
277 static void afs_notify_end_request_tx(struct sock *sock,
278                                       struct rxrpc_call *rxcall,
279                                       unsigned long call_user_ID)
280 {
281         struct afs_call *call = (struct afs_call *)call_user_ID;
282
283         afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
284 }
285
286 /*
287  * Initiate a call and synchronously queue up the parameters for dispatch.  Any
288  * error is stored into the call struct, which the caller must check for.
289  */
290 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
291 {
292         struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
293         struct rxrpc_call *rxcall;
294         struct msghdr msg;
295         struct kvec iov[1];
296         size_t len;
297         s64 tx_total_len;
298         int ret;
299
300         _enter(",{%pISp},", &srx->transport);
301
302         ASSERT(call->type != NULL);
303         ASSERT(call->type->name != NULL);
304
305         _debug("____MAKE %p{%s,%x} [%d]____",
306                call, call->type->name, key_serial(call->key),
307                atomic_read(&call->net->nr_outstanding_calls));
308
309         call->addr_ix = ac->index;
310         call->alist = afs_get_addrlist(ac->alist);
311
312         /* Work out the length we're going to transmit.  This is awkward for
313          * calls such as FS.StoreData where there's an extra injection of data
314          * after the initial fixed part.
315          */
316         tx_total_len = call->request_size;
317         if (call->write_iter)
318                 tx_total_len += iov_iter_count(call->write_iter);
319
320         /* If the call is going to be asynchronous, we need an extra ref for
321          * the call to hold itself so the caller need not hang on to its ref.
322          */
323         if (call->async) {
324                 afs_get_call(call, afs_call_trace_get);
325                 call->drop_ref = true;
326         }
327
328         /* create a call */
329         rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
330                                          (unsigned long)call,
331                                          tx_total_len, gfp,
332                                          (call->async ?
333                                           afs_wake_up_async_call :
334                                           afs_wake_up_call_waiter),
335                                          call->upgrade,
336                                          (call->intr ? RXRPC_PREINTERRUPTIBLE :
337                                           RXRPC_UNINTERRUPTIBLE),
338                                          call->debug_id);
339         if (IS_ERR(rxcall)) {
340                 ret = PTR_ERR(rxcall);
341                 call->error = ret;
342                 goto error_kill_call;
343         }
344
345         call->rxcall = rxcall;
346
347         if (call->max_lifespan)
348                 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
349                                           call->max_lifespan);
350         call->issue_time = ktime_get_real();
351
352         /* send the request */
353         iov[0].iov_base = call->request;
354         iov[0].iov_len  = call->request_size;
355
356         msg.msg_name            = NULL;
357         msg.msg_namelen         = 0;
358         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
359         msg.msg_control         = NULL;
360         msg.msg_controllen      = 0;
361         msg.msg_flags           = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0);
362
363         ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
364                                      &msg, call->request_size,
365                                      afs_notify_end_request_tx);
366         if (ret < 0)
367                 goto error_do_abort;
368
369         if (call->write_iter) {
370                 msg.msg_iter = *call->write_iter;
371                 msg.msg_flags &= ~MSG_MORE;
372                 trace_afs_send_data(call, &msg);
373
374                 ret = rxrpc_kernel_send_data(call->net->socket,
375                                              call->rxcall, &msg,
376                                              iov_iter_count(&msg.msg_iter),
377                                              afs_notify_end_request_tx);
378                 *call->write_iter = msg.msg_iter;
379
380                 trace_afs_sent_data(call, &msg, ret);
381                 if (ret < 0)
382                         goto error_do_abort;
383         }
384
385         /* Note that at this point, we may have received the reply or an abort
386          * - and an asynchronous call may already have completed.
387          *
388          * afs_wait_for_call_to_complete(call, ac)
389          * must be called to synchronously clean up.
390          */
391         return;
392
393 error_do_abort:
394         if (ret != -ECONNABORTED) {
395                 rxrpc_kernel_abort_call(call->net->socket, rxcall,
396                                         RX_USER_ABORT, ret, "KSD");
397         } else {
398                 len = 0;
399                 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
400                 rxrpc_kernel_recv_data(call->net->socket, rxcall,
401                                        &msg.msg_iter, &len, false,
402                                        &call->abort_code, &call->service_id);
403                 ac->abort_code = call->abort_code;
404                 ac->responded = true;
405         }
406         call->error = ret;
407         trace_afs_call_done(call);
408 error_kill_call:
409         if (call->type->done)
410                 call->type->done(call);
411
412         /* We need to dispose of the extra ref we grabbed for an async call.
413          * The call, however, might be queued on afs_async_calls and we need to
414          * make sure we don't get any more notifications that might requeue it.
415          */
416         if (call->rxcall) {
417                 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
418                 call->rxcall = NULL;
419         }
420         if (call->async) {
421                 if (cancel_work_sync(&call->async_work))
422                         afs_put_call(call);
423                 afs_put_call(call);
424         }
425
426         ac->error = ret;
427         call->state = AFS_CALL_COMPLETE;
428         _leave(" = %d", ret);
429 }
430
431 /*
432  * Log remote abort codes that indicate that we have a protocol disagreement
433  * with the server.
434  */
435 static void afs_log_error(struct afs_call *call, s32 remote_abort)
436 {
437         static int max = 0;
438         const char *msg;
439         int m;
440
441         switch (remote_abort) {
442         case RX_EOF:             msg = "unexpected EOF";        break;
443         case RXGEN_CC_MARSHAL:   msg = "client marshalling";    break;
444         case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling";  break;
445         case RXGEN_SS_MARSHAL:   msg = "server marshalling";    break;
446         case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling";  break;
447         case RXGEN_DECODE:       msg = "opcode decode";         break;
448         case RXGEN_SS_XDRFREE:   msg = "server XDR cleanup";    break;
449         case RXGEN_CC_XDRFREE:   msg = "client XDR cleanup";    break;
450         case -32:                msg = "insufficient data";     break;
451         default:
452                 return;
453         }
454
455         m = max;
456         if (m < 3) {
457                 max = m + 1;
458                 pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n",
459                           msg, call->type->name,
460                           &call->alist->addrs[call->addr_ix].transport);
461         }
462 }
463
464 /*
465  * deliver messages to a call
466  */
467 static void afs_deliver_to_call(struct afs_call *call)
468 {
469         enum afs_call_state state;
470         size_t len;
471         u32 abort_code, remote_abort = 0;
472         int ret;
473
474         _enter("%s", call->type->name);
475
476         while (state = READ_ONCE(call->state),
477                state == AFS_CALL_CL_AWAIT_REPLY ||
478                state == AFS_CALL_SV_AWAIT_OP_ID ||
479                state == AFS_CALL_SV_AWAIT_REQUEST ||
480                state == AFS_CALL_SV_AWAIT_ACK
481                ) {
482                 if (state == AFS_CALL_SV_AWAIT_ACK) {
483                         len = 0;
484                         iov_iter_kvec(&call->def_iter, READ, NULL, 0, 0);
485                         ret = rxrpc_kernel_recv_data(call->net->socket,
486                                                      call->rxcall, &call->def_iter,
487                                                      &len, false, &remote_abort,
488                                                      &call->service_id);
489                         trace_afs_receive_data(call, &call->def_iter, false, ret);
490
491                         if (ret == -EINPROGRESS || ret == -EAGAIN)
492                                 return;
493                         if (ret < 0 || ret == 1) {
494                                 if (ret == 1)
495                                         ret = 0;
496                                 goto call_complete;
497                         }
498                         return;
499                 }
500
501                 ret = call->type->deliver(call);
502                 state = READ_ONCE(call->state);
503                 if (ret == 0 && call->unmarshalling_error)
504                         ret = -EBADMSG;
505                 switch (ret) {
506                 case 0:
507                         afs_queue_call_work(call);
508                         if (state == AFS_CALL_CL_PROC_REPLY) {
509                                 if (call->op)
510                                         set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
511                                                 &call->op->server->flags);
512                                 goto call_complete;
513                         }
514                         ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
515                         goto done;
516                 case -EINPROGRESS:
517                 case -EAGAIN:
518                         goto out;
519                 case -ECONNABORTED:
520                         ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
521                         afs_log_error(call, call->abort_code);
522                         goto done;
523                 case -ENOTSUPP:
524                         abort_code = RXGEN_OPCODE;
525                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
526                                                 abort_code, ret, "KIV");
527                         goto local_abort;
528                 case -EIO:
529                         pr_err("kAFS: Call %u in bad state %u\n",
530                                call->debug_id, state);
531                         fallthrough;
532                 case -ENODATA:
533                 case -EBADMSG:
534                 case -EMSGSIZE:
535                 case -ENOMEM:
536                 case -EFAULT:
537                         abort_code = RXGEN_CC_UNMARSHAL;
538                         if (state != AFS_CALL_CL_AWAIT_REPLY)
539                                 abort_code = RXGEN_SS_UNMARSHAL;
540                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
541                                                 abort_code, ret, "KUM");
542                         goto local_abort;
543                 default:
544                         abort_code = RX_CALL_DEAD;
545                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
546                                                 abort_code, ret, "KER");
547                         goto local_abort;
548                 }
549         }
550
551 done:
552         if (call->type->done)
553                 call->type->done(call);
554 out:
555         _leave("");
556         return;
557
558 local_abort:
559         abort_code = 0;
560 call_complete:
561         afs_set_call_complete(call, ret, remote_abort);
562         state = AFS_CALL_COMPLETE;
563         goto done;
564 }
565
566 /*
567  * Wait synchronously for a call to complete and clean up the call struct.
568  */
569 long afs_wait_for_call_to_complete(struct afs_call *call,
570                                    struct afs_addr_cursor *ac)
571 {
572         long ret;
573         bool rxrpc_complete = false;
574
575         DECLARE_WAITQUEUE(myself, current);
576
577         _enter("");
578
579         ret = call->error;
580         if (ret < 0)
581                 goto out;
582
583         add_wait_queue(&call->waitq, &myself);
584         for (;;) {
585                 set_current_state(TASK_UNINTERRUPTIBLE);
586
587                 /* deliver any messages that are in the queue */
588                 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
589                     call->need_attention) {
590                         call->need_attention = false;
591                         __set_current_state(TASK_RUNNING);
592                         afs_deliver_to_call(call);
593                         continue;
594                 }
595
596                 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
597                         break;
598
599                 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
600                         /* rxrpc terminated the call. */
601                         rxrpc_complete = true;
602                         break;
603                 }
604
605                 schedule();
606         }
607
608         remove_wait_queue(&call->waitq, &myself);
609         __set_current_state(TASK_RUNNING);
610
611         if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
612                 if (rxrpc_complete) {
613                         afs_set_call_complete(call, call->error, call->abort_code);
614                 } else {
615                         /* Kill off the call if it's still live. */
616                         _debug("call interrupted");
617                         if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
618                                                     RX_USER_ABORT, -EINTR, "KWI"))
619                                 afs_set_call_complete(call, -EINTR, 0);
620                 }
621         }
622
623         spin_lock_bh(&call->state_lock);
624         ac->abort_code = call->abort_code;
625         ac->error = call->error;
626         spin_unlock_bh(&call->state_lock);
627
628         ret = ac->error;
629         switch (ret) {
630         case 0:
631                 ret = call->ret0;
632                 call->ret0 = 0;
633
634                 fallthrough;
635         case -ECONNABORTED:
636                 ac->responded = true;
637                 break;
638         }
639
640 out:
641         _debug("call complete");
642         afs_put_call(call);
643         _leave(" = %p", (void *)ret);
644         return ret;
645 }
646
647 /*
648  * wake up a waiting call
649  */
650 static void afs_wake_up_call_waiter(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
655         call->need_attention = true;
656         wake_up(&call->waitq);
657 }
658
659 /*
660  * wake up an asynchronous call
661  */
662 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
663                                    unsigned long call_user_ID)
664 {
665         struct afs_call *call = (struct afs_call *)call_user_ID;
666         int u;
667
668         trace_afs_notify_call(rxcall, call);
669         call->need_attention = true;
670
671         u = atomic_fetch_add_unless(&call->usage, 1, 0);
672         if (u != 0) {
673                 trace_afs_call(call, afs_call_trace_wake, u + 1,
674                                atomic_read(&call->net->nr_outstanding_calls),
675                                __builtin_return_address(0));
676
677                 if (!queue_work(afs_async_calls, &call->async_work))
678                         afs_put_call(call);
679         }
680 }
681
682 /*
683  * Perform I/O processing on an asynchronous call.  The work item carries a ref
684  * to the call struct that we either need to release or to pass on.
685  */
686 static void afs_process_async_call(struct work_struct *work)
687 {
688         struct afs_call *call = container_of(work, struct afs_call, async_work);
689
690         _enter("");
691
692         if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
693                 call->need_attention = false;
694                 afs_deliver_to_call(call);
695         }
696
697         afs_put_call(call);
698         _leave("");
699 }
700
701 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
702 {
703         struct afs_call *call = (struct afs_call *)user_call_ID;
704
705         call->rxcall = rxcall;
706 }
707
708 /*
709  * Charge the incoming call preallocation.
710  */
711 void afs_charge_preallocation(struct work_struct *work)
712 {
713         struct afs_net *net =
714                 container_of(work, struct afs_net, charge_preallocation_work);
715         struct afs_call *call = net->spare_incoming_call;
716
717         for (;;) {
718                 if (!call) {
719                         call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
720                         if (!call)
721                                 break;
722
723                         call->drop_ref = true;
724                         call->async = true;
725                         call->state = AFS_CALL_SV_AWAIT_OP_ID;
726                         init_waitqueue_head(&call->waitq);
727                         afs_extract_to_tmp(call);
728                 }
729
730                 if (rxrpc_kernel_charge_accept(net->socket,
731                                                afs_wake_up_async_call,
732                                                afs_rx_attach,
733                                                (unsigned long)call,
734                                                GFP_KERNEL,
735                                                call->debug_id) < 0)
736                         break;
737                 call = NULL;
738         }
739         net->spare_incoming_call = call;
740 }
741
742 /*
743  * Discard a preallocated call when a socket is shut down.
744  */
745 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
746                                     unsigned long user_call_ID)
747 {
748         struct afs_call *call = (struct afs_call *)user_call_ID;
749
750         call->rxcall = NULL;
751         afs_put_call(call);
752 }
753
754 /*
755  * Notification of an incoming call.
756  */
757 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
758                             unsigned long user_call_ID)
759 {
760         struct afs_net *net = afs_sock2net(sk);
761
762         queue_work(afs_wq, &net->charge_preallocation_work);
763 }
764
765 /*
766  * Grab the operation ID from an incoming cache manager call.  The socket
767  * buffer is discarded on error or if we don't yet have sufficient data.
768  */
769 static int afs_deliver_cm_op_id(struct afs_call *call)
770 {
771         int ret;
772
773         _enter("{%zu}", iov_iter_count(call->iter));
774
775         /* the operation ID forms the first four bytes of the request data */
776         ret = afs_extract_data(call, true);
777         if (ret < 0)
778                 return ret;
779
780         call->operation_ID = ntohl(call->tmp);
781         afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
782
783         /* ask the cache manager to route the call (it'll change the call type
784          * if successful) */
785         if (!afs_cm_incoming_call(call))
786                 return -ENOTSUPP;
787
788         trace_afs_cb_call(call);
789
790         /* pass responsibility for the remainer of this message off to the
791          * cache manager op */
792         return call->type->deliver(call);
793 }
794
795 /*
796  * Advance the AFS call state when an RxRPC service call ends the transmit
797  * phase.
798  */
799 static void afs_notify_end_reply_tx(struct sock *sock,
800                                     struct rxrpc_call *rxcall,
801                                     unsigned long call_user_ID)
802 {
803         struct afs_call *call = (struct afs_call *)call_user_ID;
804
805         afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
806 }
807
808 /*
809  * send an empty reply
810  */
811 void afs_send_empty_reply(struct afs_call *call)
812 {
813         struct afs_net *net = call->net;
814         struct msghdr msg;
815
816         _enter("");
817
818         rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
819
820         msg.msg_name            = NULL;
821         msg.msg_namelen         = 0;
822         iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
823         msg.msg_control         = NULL;
824         msg.msg_controllen      = 0;
825         msg.msg_flags           = 0;
826
827         switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
828                                        afs_notify_end_reply_tx)) {
829         case 0:
830                 _leave(" [replied]");
831                 return;
832
833         case -ENOMEM:
834                 _debug("oom");
835                 rxrpc_kernel_abort_call(net->socket, call->rxcall,
836                                         RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
837                 fallthrough;
838         default:
839                 _leave(" [error]");
840                 return;
841         }
842 }
843
844 /*
845  * send a simple reply
846  */
847 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
848 {
849         struct afs_net *net = call->net;
850         struct msghdr msg;
851         struct kvec iov[1];
852         int n;
853
854         _enter("");
855
856         rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
857
858         iov[0].iov_base         = (void *) buf;
859         iov[0].iov_len          = len;
860         msg.msg_name            = NULL;
861         msg.msg_namelen         = 0;
862         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
863         msg.msg_control         = NULL;
864         msg.msg_controllen      = 0;
865         msg.msg_flags           = 0;
866
867         n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
868                                    afs_notify_end_reply_tx);
869         if (n >= 0) {
870                 /* Success */
871                 _leave(" [replied]");
872                 return;
873         }
874
875         if (n == -ENOMEM) {
876                 _debug("oom");
877                 rxrpc_kernel_abort_call(net->socket, call->rxcall,
878                                         RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
879         }
880         _leave(" [error]");
881 }
882
883 /*
884  * Extract a piece of data from the received data socket buffers.
885  */
886 int afs_extract_data(struct afs_call *call, bool want_more)
887 {
888         struct afs_net *net = call->net;
889         struct iov_iter *iter = call->iter;
890         enum afs_call_state state;
891         u32 remote_abort = 0;
892         int ret;
893
894         _enter("{%s,%zu,%zu},%d",
895                call->type->name, call->iov_len, iov_iter_count(iter), want_more);
896
897         ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
898                                      &call->iov_len, want_more, &remote_abort,
899                                      &call->service_id);
900         if (ret == 0 || ret == -EAGAIN)
901                 return ret;
902
903         state = READ_ONCE(call->state);
904         if (ret == 1) {
905                 switch (state) {
906                 case AFS_CALL_CL_AWAIT_REPLY:
907                         afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
908                         break;
909                 case AFS_CALL_SV_AWAIT_REQUEST:
910                         afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
911                         break;
912                 case AFS_CALL_COMPLETE:
913                         kdebug("prem complete %d", call->error);
914                         return afs_io_error(call, afs_io_error_extract);
915                 default:
916                         break;
917                 }
918                 return 0;
919         }
920
921         afs_set_call_complete(call, ret, remote_abort);
922         return ret;
923 }
924
925 /*
926  * Log protocol error production.
927  */
928 noinline int afs_protocol_error(struct afs_call *call,
929                                 enum afs_eproto_cause cause)
930 {
931         trace_afs_protocol_error(call, cause);
932         if (call)
933                 call->unmarshalling_error = true;
934         return -EBADMSG;
935 }