Merge branch 'misc' into fixes
[platform/kernel/linux-rpi.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_delete_async_call(struct work_struct *);
22 static void afs_process_async_call(struct work_struct *);
23 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
24 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
25 static int afs_deliver_cm_op_id(struct afs_call *);
26
27 /* asynchronous incoming call initial processing */
28 static const struct afs_call_type afs_RXCMxxxx = {
29         .name           = "CB.xxxx",
30         .deliver        = afs_deliver_cm_op_id,
31 };
32
33 /*
34  * open an RxRPC socket and bind it to be a server for callback notifications
35  * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
36  */
37 int afs_open_socket(struct afs_net *net)
38 {
39         struct sockaddr_rxrpc srx;
40         struct socket *socket;
41         unsigned int min_level;
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         min_level = RXRPC_SECURITY_ENCRYPT;
62         ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
63                                 (void *)&min_level, sizeof(min_level));
64         if (ret < 0)
65                 goto error_2;
66
67         ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
68         if (ret == -EADDRINUSE) {
69                 srx.transport.sin6.sin6_port = 0;
70                 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
71         }
72         if (ret < 0)
73                 goto error_2;
74
75         srx.srx_service = YFS_CM_SERVICE;
76         ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
77         if (ret < 0)
78                 goto error_2;
79
80         /* Ideally, we'd turn on service upgrade here, but we can't because
81          * OpenAFS is buggy and leaks the userStatus field from packet to
82          * packet and between FS packets and CB packets - so if we try to do an
83          * upgrade on an FS packet, OpenAFS will leak that into the CB packet
84          * it sends back to us.
85          */
86
87         rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
88                                            afs_rx_discard_new_call);
89
90         ret = kernel_listen(socket, INT_MAX);
91         if (ret < 0)
92                 goto error_2;
93
94         net->socket = socket;
95         afs_charge_preallocation(&net->charge_preallocation_work);
96         _leave(" = 0");
97         return 0;
98
99 error_2:
100         sock_release(socket);
101 error_1:
102         _leave(" = %d", ret);
103         return ret;
104 }
105
106 /*
107  * close the RxRPC socket AFS was using
108  */
109 void afs_close_socket(struct afs_net *net)
110 {
111         _enter("");
112
113         kernel_listen(net->socket, 0);
114         flush_workqueue(afs_async_calls);
115
116         if (net->spare_incoming_call) {
117                 afs_put_call(net->spare_incoming_call);
118                 net->spare_incoming_call = NULL;
119         }
120
121         _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
122         wait_var_event(&net->nr_outstanding_calls,
123                        !atomic_read(&net->nr_outstanding_calls));
124         _debug("no outstanding calls");
125
126         kernel_sock_shutdown(net->socket, SHUT_RDWR);
127         flush_workqueue(afs_async_calls);
128         sock_release(net->socket);
129
130         _debug("dework");
131         _leave("");
132 }
133
134 /*
135  * Allocate a call.
136  */
137 static struct afs_call *afs_alloc_call(struct afs_net *net,
138                                        const struct afs_call_type *type,
139                                        gfp_t gfp)
140 {
141         struct afs_call *call;
142         int o;
143
144         call = kzalloc(sizeof(*call), gfp);
145         if (!call)
146                 return NULL;
147
148         call->type = type;
149         call->net = net;
150         call->debug_id = atomic_inc_return(&rxrpc_debug_id);
151         atomic_set(&call->usage, 1);
152         INIT_WORK(&call->async_work, afs_process_async_call);
153         init_waitqueue_head(&call->waitq);
154         spin_lock_init(&call->state_lock);
155         call->_iter = &call->iter;
156
157         o = atomic_inc_return(&net->nr_outstanding_calls);
158         trace_afs_call(call, afs_call_trace_alloc, 1, o,
159                        __builtin_return_address(0));
160         return call;
161 }
162
163 /*
164  * Dispose of a reference on a call.
165  */
166 void afs_put_call(struct afs_call *call)
167 {
168         struct afs_net *net = call->net;
169         int n = atomic_dec_return(&call->usage);
170         int o = atomic_read(&net->nr_outstanding_calls);
171
172         trace_afs_call(call, afs_call_trace_put, n + 1, o,
173                        __builtin_return_address(0));
174
175         ASSERTCMP(n, >=, 0);
176         if (n == 0) {
177                 ASSERT(!work_pending(&call->async_work));
178                 ASSERT(call->type->name != NULL);
179
180                 if (call->rxcall) {
181                         rxrpc_kernel_end_call(net->socket, call->rxcall);
182                         call->rxcall = NULL;
183                 }
184                 if (call->type->destructor)
185                         call->type->destructor(call);
186
187                 afs_put_server(call->net, call->server, afs_server_trace_put_call);
188                 afs_put_cb_interest(call->net, call->cbi);
189                 afs_put_addrlist(call->alist);
190                 kfree(call->request);
191
192                 trace_afs_call(call, afs_call_trace_free, 0, o,
193                                __builtin_return_address(0));
194                 kfree(call);
195
196                 o = atomic_dec_return(&net->nr_outstanding_calls);
197                 if (o == 0)
198                         wake_up_var(&net->nr_outstanding_calls);
199         }
200 }
201
202 static struct afs_call *afs_get_call(struct afs_call *call,
203                                      enum afs_call_trace why)
204 {
205         int u = atomic_inc_return(&call->usage);
206
207         trace_afs_call(call, why, u,
208                        atomic_read(&call->net->nr_outstanding_calls),
209                        __builtin_return_address(0));
210         return call;
211 }
212
213 /*
214  * Queue the call for actual work.
215  */
216 static void afs_queue_call_work(struct afs_call *call)
217 {
218         if (call->type->work) {
219                 INIT_WORK(&call->work, call->type->work);
220
221                 afs_get_call(call, afs_call_trace_work);
222                 if (!queue_work(afs_wq, &call->work))
223                         afs_put_call(call);
224         }
225 }
226
227 /*
228  * allocate a call with flat request and reply buffers
229  */
230 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
231                                      const struct afs_call_type *type,
232                                      size_t request_size, size_t reply_max)
233 {
234         struct afs_call *call;
235
236         call = afs_alloc_call(net, type, GFP_NOFS);
237         if (!call)
238                 goto nomem_call;
239
240         if (request_size) {
241                 call->request_size = request_size;
242                 call->request = kmalloc(request_size, GFP_NOFS);
243                 if (!call->request)
244                         goto nomem_free;
245         }
246
247         if (reply_max) {
248                 call->reply_max = reply_max;
249                 call->buffer = kmalloc(reply_max, GFP_NOFS);
250                 if (!call->buffer)
251                         goto nomem_free;
252         }
253
254         afs_extract_to_buf(call, call->reply_max);
255         call->operation_ID = type->op;
256         init_waitqueue_head(&call->waitq);
257         return call;
258
259 nomem_free:
260         afs_put_call(call);
261 nomem_call:
262         return NULL;
263 }
264
265 /*
266  * clean up a call with flat buffer
267  */
268 void afs_flat_call_destructor(struct afs_call *call)
269 {
270         _enter("");
271
272         kfree(call->request);
273         call->request = NULL;
274         kfree(call->buffer);
275         call->buffer = NULL;
276 }
277
278 #define AFS_BVEC_MAX 8
279
280 /*
281  * Load the given bvec with the next few pages.
282  */
283 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
284                           struct bio_vec *bv, pgoff_t first, pgoff_t last,
285                           unsigned offset)
286 {
287         struct page *pages[AFS_BVEC_MAX];
288         unsigned int nr, n, i, to, bytes = 0;
289
290         nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
291         n = find_get_pages_contig(call->mapping, first, nr, pages);
292         ASSERTCMP(n, ==, nr);
293
294         msg->msg_flags |= MSG_MORE;
295         for (i = 0; i < nr; i++) {
296                 to = PAGE_SIZE;
297                 if (first + i >= last) {
298                         to = call->last_to;
299                         msg->msg_flags &= ~MSG_MORE;
300                 }
301                 bv[i].bv_page = pages[i];
302                 bv[i].bv_len = to - offset;
303                 bv[i].bv_offset = offset;
304                 bytes += to - offset;
305                 offset = 0;
306         }
307
308         iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
309 }
310
311 /*
312  * Advance the AFS call state when the RxRPC call ends the transmit phase.
313  */
314 static void afs_notify_end_request_tx(struct sock *sock,
315                                       struct rxrpc_call *rxcall,
316                                       unsigned long call_user_ID)
317 {
318         struct afs_call *call = (struct afs_call *)call_user_ID;
319
320         afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
321 }
322
323 /*
324  * attach the data from a bunch of pages on an inode to a call
325  */
326 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
327 {
328         struct bio_vec bv[AFS_BVEC_MAX];
329         unsigned int bytes, nr, loop, offset;
330         pgoff_t first = call->first, last = call->last;
331         int ret;
332
333         offset = call->first_offset;
334         call->first_offset = 0;
335
336         do {
337                 afs_load_bvec(call, msg, bv, first, last, offset);
338                 trace_afs_send_pages(call, msg, first, last, offset);
339
340                 offset = 0;
341                 bytes = msg->msg_iter.count;
342                 nr = msg->msg_iter.nr_segs;
343
344                 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
345                                              bytes, afs_notify_end_request_tx);
346                 for (loop = 0; loop < nr; loop++)
347                         put_page(bv[loop].bv_page);
348                 if (ret < 0)
349                         break;
350
351                 first += nr;
352         } while (first <= last);
353
354         trace_afs_sent_pages(call, call->first, last, first, ret);
355         return ret;
356 }
357
358 /*
359  * Initiate a call and synchronously queue up the parameters for dispatch.  Any
360  * error is stored into the call struct, which the caller must check for.
361  */
362 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
363 {
364         struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
365         struct rxrpc_call *rxcall;
366         struct msghdr msg;
367         struct kvec iov[1];
368         s64 tx_total_len;
369         int ret;
370
371         _enter(",{%pISp},", &srx->transport);
372
373         ASSERT(call->type != NULL);
374         ASSERT(call->type->name != NULL);
375
376         _debug("____MAKE %p{%s,%x} [%d]____",
377                call, call->type->name, key_serial(call->key),
378                atomic_read(&call->net->nr_outstanding_calls));
379
380         call->addr_ix = ac->index;
381         call->alist = afs_get_addrlist(ac->alist);
382
383         /* Work out the length we're going to transmit.  This is awkward for
384          * calls such as FS.StoreData where there's an extra injection of data
385          * after the initial fixed part.
386          */
387         tx_total_len = call->request_size;
388         if (call->send_pages) {
389                 if (call->last == call->first) {
390                         tx_total_len += call->last_to - call->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 - call->first_offset;
397                         tx_total_len += call->last_to;
398                         tx_total_len += (call->last - call->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
408         /* create a call */
409         rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
410                                          (unsigned long)call,
411                                          tx_total_len, gfp,
412                                          (call->async ?
413                                           afs_wake_up_async_call :
414                                           afs_wake_up_call_waiter),
415                                          call->upgrade,
416                                          call->intr,
417                                          call->debug_id);
418         if (IS_ERR(rxcall)) {
419                 ret = PTR_ERR(rxcall);
420                 call->error = ret;
421                 goto error_kill_call;
422         }
423
424         call->rxcall = rxcall;
425
426         if (call->max_lifespan)
427                 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
428                                           call->max_lifespan);
429
430         /* send the request */
431         iov[0].iov_base = call->request;
432         iov[0].iov_len  = call->request_size;
433
434         msg.msg_name            = NULL;
435         msg.msg_namelen         = 0;
436         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
437         msg.msg_control         = NULL;
438         msg.msg_controllen      = 0;
439         msg.msg_flags           = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
440
441         ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
442                                      &msg, call->request_size,
443                                      afs_notify_end_request_tx);
444         if (ret < 0)
445                 goto error_do_abort;
446
447         if (call->send_pages) {
448                 ret = afs_send_pages(call, &msg);
449                 if (ret < 0)
450                         goto error_do_abort;
451         }
452
453         /* Note that at this point, we may have received the reply or an abort
454          * - and an asynchronous call may already have completed.
455          *
456          * afs_wait_for_call_to_complete(call, ac)
457          * must be called to synchronously clean up.
458          */
459         return;
460
461 error_do_abort:
462         if (ret != -ECONNABORTED) {
463                 rxrpc_kernel_abort_call(call->net->socket, rxcall,
464                                         RX_USER_ABORT, ret, "KSD");
465         } else {
466                 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
467                 rxrpc_kernel_recv_data(call->net->socket, rxcall,
468                                        &msg.msg_iter, false,
469                                        &call->abort_code, &call->service_id);
470                 ac->abort_code = call->abort_code;
471                 ac->responded = true;
472         }
473         call->error = ret;
474         trace_afs_call_done(call);
475 error_kill_call:
476         if (call->type->done)
477                 call->type->done(call);
478
479         /* We need to dispose of the extra ref we grabbed for an async call.
480          * The call, however, might be queued on afs_async_calls and we need to
481          * make sure we don't get any more notifications that might requeue it.
482          */
483         if (call->rxcall) {
484                 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
485                 call->rxcall = NULL;
486         }
487         if (call->async) {
488                 if (cancel_work_sync(&call->async_work))
489                         afs_put_call(call);
490                 afs_put_call(call);
491         }
492
493         ac->error = ret;
494         call->state = AFS_CALL_COMPLETE;
495         _leave(" = %d", ret);
496 }
497
498 /*
499  * deliver messages to a call
500  */
501 static void afs_deliver_to_call(struct afs_call *call)
502 {
503         enum afs_call_state state;
504         u32 abort_code, remote_abort = 0;
505         int ret;
506
507         _enter("%s", call->type->name);
508
509         while (state = READ_ONCE(call->state),
510                state == AFS_CALL_CL_AWAIT_REPLY ||
511                state == AFS_CALL_SV_AWAIT_OP_ID ||
512                state == AFS_CALL_SV_AWAIT_REQUEST ||
513                state == AFS_CALL_SV_AWAIT_ACK
514                ) {
515                 if (state == AFS_CALL_SV_AWAIT_ACK) {
516                         iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
517                         ret = rxrpc_kernel_recv_data(call->net->socket,
518                                                      call->rxcall, &call->iter,
519                                                      false, &remote_abort,
520                                                      &call->service_id);
521                         trace_afs_receive_data(call, &call->iter, false, ret);
522
523                         if (ret == -EINPROGRESS || ret == -EAGAIN)
524                                 return;
525                         if (ret < 0 || ret == 1) {
526                                 if (ret == 1)
527                                         ret = 0;
528                                 goto call_complete;
529                         }
530                         return;
531                 }
532
533                 if (!call->have_reply_time &&
534                     rxrpc_kernel_get_reply_time(call->net->socket,
535                                                 call->rxcall,
536                                                 &call->reply_time))
537                         call->have_reply_time = true;
538
539                 ret = call->type->deliver(call);
540                 state = READ_ONCE(call->state);
541                 switch (ret) {
542                 case 0:
543                         afs_queue_call_work(call);
544                         if (state == AFS_CALL_CL_PROC_REPLY) {
545                                 if (call->cbi)
546                                         set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
547                                                 &call->cbi->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                         /* Fall through */
567                 case -ENODATA:
568                 case -EBADMSG:
569                 case -EMSGSIZE:
570                         abort_code = RXGEN_CC_UNMARSHAL;
571                         if (state != AFS_CALL_CL_AWAIT_REPLY)
572                                 abort_code = RXGEN_SS_UNMARSHAL;
573                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
574                                                 abort_code, ret, "KUM");
575                         goto local_abort;
576                 default:
577                         abort_code = RX_USER_ABORT;
578                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
579                                                 abort_code, ret, "KER");
580                         goto local_abort;
581                 }
582         }
583
584 done:
585         if (call->type->done)
586                 call->type->done(call);
587         if (state == AFS_CALL_COMPLETE && call->incoming)
588                 afs_put_call(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  */
604 long afs_wait_for_call_to_complete(struct afs_call *call,
605                                    struct afs_addr_cursor *ac)
606 {
607         signed long rtt2, timeout;
608         long ret;
609         bool stalled = false;
610         u64 rtt;
611         u32 life, last_life;
612         bool rxrpc_complete = false;
613
614         DECLARE_WAITQUEUE(myself, current);
615
616         _enter("");
617
618         ret = call->error;
619         if (ret < 0)
620                 goto out;
621
622         rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
623         rtt2 = nsecs_to_jiffies64(rtt) * 2;
624         if (rtt2 < 2)
625                 rtt2 = 2;
626
627         timeout = rtt2;
628         rxrpc_kernel_check_life(call->net->socket, call->rxcall, &last_life);
629
630         add_wait_queue(&call->waitq, &myself);
631         for (;;) {
632                 set_current_state(TASK_UNINTERRUPTIBLE);
633
634                 /* deliver any messages that are in the queue */
635                 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
636                     call->need_attention) {
637                         call->need_attention = false;
638                         __set_current_state(TASK_RUNNING);
639                         afs_deliver_to_call(call);
640                         continue;
641                 }
642
643                 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
644                         break;
645
646                 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall, &life)) {
647                         /* rxrpc terminated the call. */
648                         rxrpc_complete = true;
649                         break;
650                 }
651
652                 if (call->intr && timeout == 0 &&
653                     life == last_life && signal_pending(current)) {
654                         if (stalled)
655                                 break;
656                         __set_current_state(TASK_RUNNING);
657                         rxrpc_kernel_probe_life(call->net->socket, call->rxcall);
658                         timeout = rtt2;
659                         stalled = true;
660                         continue;
661                 }
662
663                 if (life != last_life) {
664                         timeout = rtt2;
665                         last_life = life;
666                         stalled = false;
667                 }
668
669                 timeout = schedule_timeout(timeout);
670         }
671
672         remove_wait_queue(&call->waitq, &myself);
673         __set_current_state(TASK_RUNNING);
674
675         if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
676                 if (rxrpc_complete) {
677                         afs_set_call_complete(call, call->error, call->abort_code);
678                 } else {
679                         /* Kill off the call if it's still live. */
680                         _debug("call interrupted");
681                         if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
682                                                     RX_USER_ABORT, -EINTR, "KWI"))
683                                 afs_set_call_complete(call, -EINTR, 0);
684                 }
685         }
686
687         spin_lock_bh(&call->state_lock);
688         ac->abort_code = call->abort_code;
689         ac->error = call->error;
690         spin_unlock_bh(&call->state_lock);
691
692         ret = ac->error;
693         switch (ret) {
694         case 0:
695                 ret = call->ret0;
696                 call->ret0 = 0;
697
698                 /* Fall through */
699         case -ECONNABORTED:
700                 ac->responded = true;
701                 break;
702         }
703
704 out:
705         _debug("call complete");
706         afs_put_call(call);
707         _leave(" = %p", (void *)ret);
708         return ret;
709 }
710
711 /*
712  * wake up a waiting call
713  */
714 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
715                                     unsigned long call_user_ID)
716 {
717         struct afs_call *call = (struct afs_call *)call_user_ID;
718
719         call->need_attention = true;
720         wake_up(&call->waitq);
721 }
722
723 /*
724  * wake up an asynchronous call
725  */
726 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
727                                    unsigned long call_user_ID)
728 {
729         struct afs_call *call = (struct afs_call *)call_user_ID;
730         int u;
731
732         trace_afs_notify_call(rxcall, call);
733         call->need_attention = true;
734
735         u = atomic_fetch_add_unless(&call->usage, 1, 0);
736         if (u != 0) {
737                 trace_afs_call(call, afs_call_trace_wake, u,
738                                atomic_read(&call->net->nr_outstanding_calls),
739                                __builtin_return_address(0));
740
741                 if (!queue_work(afs_async_calls, &call->async_work))
742                         afs_put_call(call);
743         }
744 }
745
746 /*
747  * Delete an asynchronous call.  The work item carries a ref to the call struct
748  * that we need to release.
749  */
750 static void afs_delete_async_call(struct work_struct *work)
751 {
752         struct afs_call *call = container_of(work, struct afs_call, async_work);
753
754         _enter("");
755
756         afs_put_call(call);
757
758         _leave("");
759 }
760
761 /*
762  * Perform I/O processing on an asynchronous call.  The work item carries a ref
763  * to the call struct that we either need to release or to pass on.
764  */
765 static void afs_process_async_call(struct work_struct *work)
766 {
767         struct afs_call *call = container_of(work, struct afs_call, async_work);
768
769         _enter("");
770
771         if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
772                 call->need_attention = false;
773                 afs_deliver_to_call(call);
774         }
775
776         if (call->state == AFS_CALL_COMPLETE) {
777                 /* We have two refs to release - one from the alloc and one
778                  * queued with the work item - and we can't just deallocate the
779                  * call because the work item may be queued again.
780                  */
781                 call->async_work.func = afs_delete_async_call;
782                 if (!queue_work(afs_async_calls, &call->async_work))
783                         afs_put_call(call);
784         }
785
786         afs_put_call(call);
787         _leave("");
788 }
789
790 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
791 {
792         struct afs_call *call = (struct afs_call *)user_call_ID;
793
794         call->rxcall = rxcall;
795 }
796
797 /*
798  * Charge the incoming call preallocation.
799  */
800 void afs_charge_preallocation(struct work_struct *work)
801 {
802         struct afs_net *net =
803                 container_of(work, struct afs_net, charge_preallocation_work);
804         struct afs_call *call = net->spare_incoming_call;
805
806         for (;;) {
807                 if (!call) {
808                         call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
809                         if (!call)
810                                 break;
811
812                         call->async = true;
813                         call->state = AFS_CALL_SV_AWAIT_OP_ID;
814                         init_waitqueue_head(&call->waitq);
815                         afs_extract_to_tmp(call);
816                 }
817
818                 if (rxrpc_kernel_charge_accept(net->socket,
819                                                afs_wake_up_async_call,
820                                                afs_rx_attach,
821                                                (unsigned long)call,
822                                                GFP_KERNEL,
823                                                call->debug_id) < 0)
824                         break;
825                 call = NULL;
826         }
827         net->spare_incoming_call = call;
828 }
829
830 /*
831  * Discard a preallocated call when a socket is shut down.
832  */
833 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
834                                     unsigned long user_call_ID)
835 {
836         struct afs_call *call = (struct afs_call *)user_call_ID;
837
838         call->rxcall = NULL;
839         afs_put_call(call);
840 }
841
842 /*
843  * Notification of an incoming call.
844  */
845 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
846                             unsigned long user_call_ID)
847 {
848         struct afs_net *net = afs_sock2net(sk);
849
850         queue_work(afs_wq, &net->charge_preallocation_work);
851 }
852
853 /*
854  * Grab the operation ID from an incoming cache manager call.  The socket
855  * buffer is discarded on error or if we don't yet have sufficient data.
856  */
857 static int afs_deliver_cm_op_id(struct afs_call *call)
858 {
859         int ret;
860
861         _enter("{%zu}", iov_iter_count(call->_iter));
862
863         /* the operation ID forms the first four bytes of the request data */
864         ret = afs_extract_data(call, true);
865         if (ret < 0)
866                 return ret;
867
868         call->operation_ID = ntohl(call->tmp);
869         afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
870
871         /* ask the cache manager to route the call (it'll change the call type
872          * if successful) */
873         if (!afs_cm_incoming_call(call))
874                 return -ENOTSUPP;
875
876         trace_afs_cb_call(call);
877
878         /* pass responsibility for the remainer of this message off to the
879          * cache manager op */
880         return call->type->deliver(call);
881 }
882
883 /*
884  * Advance the AFS call state when an RxRPC service call ends the transmit
885  * phase.
886  */
887 static void afs_notify_end_reply_tx(struct sock *sock,
888                                     struct rxrpc_call *rxcall,
889                                     unsigned long call_user_ID)
890 {
891         struct afs_call *call = (struct afs_call *)call_user_ID;
892
893         afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
894 }
895
896 /*
897  * send an empty reply
898  */
899 void afs_send_empty_reply(struct afs_call *call)
900 {
901         struct afs_net *net = call->net;
902         struct msghdr msg;
903
904         _enter("");
905
906         rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
907
908         msg.msg_name            = NULL;
909         msg.msg_namelen         = 0;
910         iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
911         msg.msg_control         = NULL;
912         msg.msg_controllen      = 0;
913         msg.msg_flags           = 0;
914
915         switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
916                                        afs_notify_end_reply_tx)) {
917         case 0:
918                 _leave(" [replied]");
919                 return;
920
921         case -ENOMEM:
922                 _debug("oom");
923                 rxrpc_kernel_abort_call(net->socket, call->rxcall,
924                                         RX_USER_ABORT, -ENOMEM, "KOO");
925                 /* Fall through */
926         default:
927                 _leave(" [error]");
928                 return;
929         }
930 }
931
932 /*
933  * send a simple reply
934  */
935 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
936 {
937         struct afs_net *net = call->net;
938         struct msghdr msg;
939         struct kvec iov[1];
940         int n;
941
942         _enter("");
943
944         rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
945
946         iov[0].iov_base         = (void *) buf;
947         iov[0].iov_len          = len;
948         msg.msg_name            = NULL;
949         msg.msg_namelen         = 0;
950         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
951         msg.msg_control         = NULL;
952         msg.msg_controllen      = 0;
953         msg.msg_flags           = 0;
954
955         n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
956                                    afs_notify_end_reply_tx);
957         if (n >= 0) {
958                 /* Success */
959                 _leave(" [replied]");
960                 return;
961         }
962
963         if (n == -ENOMEM) {
964                 _debug("oom");
965                 rxrpc_kernel_abort_call(net->socket, call->rxcall,
966                                         RX_USER_ABORT, -ENOMEM, "KOO");
967         }
968         _leave(" [error]");
969 }
970
971 /*
972  * Extract a piece of data from the received data socket buffers.
973  */
974 int afs_extract_data(struct afs_call *call, bool want_more)
975 {
976         struct afs_net *net = call->net;
977         struct iov_iter *iter = call->_iter;
978         enum afs_call_state state;
979         u32 remote_abort = 0;
980         int ret;
981
982         _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
983
984         ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
985                                      want_more, &remote_abort,
986                                      &call->service_id);
987         if (ret == 0 || ret == -EAGAIN)
988                 return ret;
989
990         state = READ_ONCE(call->state);
991         if (ret == 1) {
992                 switch (state) {
993                 case AFS_CALL_CL_AWAIT_REPLY:
994                         afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
995                         break;
996                 case AFS_CALL_SV_AWAIT_REQUEST:
997                         afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
998                         break;
999                 case AFS_CALL_COMPLETE:
1000                         kdebug("prem complete %d", call->error);
1001                         return afs_io_error(call, afs_io_error_extract);
1002                 default:
1003                         break;
1004                 }
1005                 return 0;
1006         }
1007
1008         afs_set_call_complete(call, ret, remote_abort);
1009         return ret;
1010 }
1011
1012 /*
1013  * Log protocol error production.
1014  */
1015 noinline int afs_protocol_error(struct afs_call *call, int error,
1016                                 enum afs_eproto_cause cause)
1017 {
1018         trace_afs_protocol_error(call, error, cause);
1019         return error;
1020 }