Merge tag 'afs-next-20190507' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowel...
[linux-2.6-microblaze.git] / fs / afs / rxrpc.c
1 /* Maintain an RxRPC server socket to do AFS communications through
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14
15 #include <net/sock.h>
16 #include <net/af_rxrpc.h>
17 #include "internal.h"
18 #include "afs_cm.h"
19 #include "protocol_yfs.h"
20
21 struct workqueue_struct *afs_async_calls;
22
23 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
24 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
25 static void afs_delete_async_call(struct work_struct *);
26 static void afs_process_async_call(struct work_struct *);
27 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
28 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
29 static int afs_deliver_cm_op_id(struct afs_call *);
30
31 /* asynchronous incoming call initial processing */
32 static const struct afs_call_type afs_RXCMxxxx = {
33         .name           = "CB.xxxx",
34         .deliver        = afs_deliver_cm_op_id,
35 };
36
37 /*
38  * open an RxRPC socket and bind it to be a server for callback notifications
39  * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
40  */
41 int afs_open_socket(struct afs_net *net)
42 {
43         struct sockaddr_rxrpc srx;
44         struct socket *socket;
45         unsigned int min_level;
46         int ret;
47
48         _enter("");
49
50         ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
51         if (ret < 0)
52                 goto error_1;
53
54         socket->sk->sk_allocation = GFP_NOFS;
55
56         /* bind the callback manager's address to make this a server socket */
57         memset(&srx, 0, sizeof(srx));
58         srx.srx_family                  = AF_RXRPC;
59         srx.srx_service                 = CM_SERVICE;
60         srx.transport_type              = SOCK_DGRAM;
61         srx.transport_len               = sizeof(srx.transport.sin6);
62         srx.transport.sin6.sin6_family  = AF_INET6;
63         srx.transport.sin6.sin6_port    = htons(AFS_CM_PORT);
64
65         min_level = RXRPC_SECURITY_ENCRYPT;
66         ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
67                                 (void *)&min_level, sizeof(min_level));
68         if (ret < 0)
69                 goto error_2;
70
71         ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
72         if (ret == -EADDRINUSE) {
73                 srx.transport.sin6.sin6_port = 0;
74                 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
75         }
76         if (ret < 0)
77                 goto error_2;
78
79         srx.srx_service = YFS_CM_SERVICE;
80         ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
81         if (ret < 0)
82                 goto error_2;
83
84         /* Ideally, we'd turn on service upgrade here, but we can't because
85          * OpenAFS is buggy and leaks the userStatus field from packet to
86          * packet and between FS packets and CB packets - so if we try to do an
87          * upgrade on an FS packet, OpenAFS will leak that into the CB packet
88          * it sends back to us.
89          */
90
91         rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
92                                            afs_rx_discard_new_call);
93
94         ret = kernel_listen(socket, INT_MAX);
95         if (ret < 0)
96                 goto error_2;
97
98         net->socket = socket;
99         afs_charge_preallocation(&net->charge_preallocation_work);
100         _leave(" = 0");
101         return 0;
102
103 error_2:
104         sock_release(socket);
105 error_1:
106         _leave(" = %d", ret);
107         return ret;
108 }
109
110 /*
111  * close the RxRPC socket AFS was using
112  */
113 void afs_close_socket(struct afs_net *net)
114 {
115         _enter("");
116
117         kernel_listen(net->socket, 0);
118         flush_workqueue(afs_async_calls);
119
120         if (net->spare_incoming_call) {
121                 afs_put_call(net->spare_incoming_call);
122                 net->spare_incoming_call = NULL;
123         }
124
125         _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
126         wait_var_event(&net->nr_outstanding_calls,
127                        !atomic_read(&net->nr_outstanding_calls));
128         _debug("no outstanding calls");
129
130         kernel_sock_shutdown(net->socket, SHUT_RDWR);
131         flush_workqueue(afs_async_calls);
132         sock_release(net->socket);
133
134         _debug("dework");
135         _leave("");
136 }
137
138 /*
139  * Allocate a call.
140  */
141 static struct afs_call *afs_alloc_call(struct afs_net *net,
142                                        const struct afs_call_type *type,
143                                        gfp_t gfp)
144 {
145         struct afs_call *call;
146         int o;
147
148         call = kzalloc(sizeof(*call), gfp);
149         if (!call)
150                 return NULL;
151
152         call->type = type;
153         call->net = net;
154         call->debug_id = atomic_inc_return(&rxrpc_debug_id);
155         atomic_set(&call->usage, 1);
156         INIT_WORK(&call->async_work, afs_process_async_call);
157         init_waitqueue_head(&call->waitq);
158         spin_lock_init(&call->state_lock);
159         call->_iter = &call->iter;
160
161         o = atomic_inc_return(&net->nr_outstanding_calls);
162         trace_afs_call(call, afs_call_trace_alloc, 1, o,
163                        __builtin_return_address(0));
164         return call;
165 }
166
167 /*
168  * Dispose of a reference on a call.
169  */
170 void afs_put_call(struct afs_call *call)
171 {
172         struct afs_net *net = call->net;
173         int n = atomic_dec_return(&call->usage);
174         int o = atomic_read(&net->nr_outstanding_calls);
175
176         trace_afs_call(call, afs_call_trace_put, n + 1, o,
177                        __builtin_return_address(0));
178
179         ASSERTCMP(n, >=, 0);
180         if (n == 0) {
181                 ASSERT(!work_pending(&call->async_work));
182                 ASSERT(call->type->name != NULL);
183
184                 if (call->rxcall) {
185                         rxrpc_kernel_end_call(net->socket, call->rxcall);
186                         call->rxcall = NULL;
187                 }
188                 if (call->type->destructor)
189                         call->type->destructor(call);
190
191                 afs_put_server(call->net, call->cm_server);
192                 afs_put_cb_interest(call->net, call->cbi);
193                 afs_put_addrlist(call->alist);
194                 kfree(call->request);
195
196                 trace_afs_call(call, afs_call_trace_free, 0, o,
197                                __builtin_return_address(0));
198                 kfree(call);
199
200                 o = atomic_dec_return(&net->nr_outstanding_calls);
201                 if (o == 0)
202                         wake_up_var(&net->nr_outstanding_calls);
203         }
204 }
205
206 static struct afs_call *afs_get_call(struct afs_call *call,
207                                      enum afs_call_trace why)
208 {
209         int u = atomic_inc_return(&call->usage);
210
211         trace_afs_call(call, why, u,
212                        atomic_read(&call->net->nr_outstanding_calls),
213                        __builtin_return_address(0));
214         return call;
215 }
216
217 /*
218  * Queue the call for actual work.
219  */
220 static void afs_queue_call_work(struct afs_call *call)
221 {
222         if (call->type->work) {
223                 INIT_WORK(&call->work, call->type->work);
224
225                 afs_get_call(call, afs_call_trace_work);
226                 if (!queue_work(afs_wq, &call->work))
227                         afs_put_call(call);
228         }
229 }
230
231 /*
232  * allocate a call with flat request and reply buffers
233  */
234 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
235                                      const struct afs_call_type *type,
236                                      size_t request_size, size_t reply_max)
237 {
238         struct afs_call *call;
239
240         call = afs_alloc_call(net, type, GFP_NOFS);
241         if (!call)
242                 goto nomem_call;
243
244         if (request_size) {
245                 call->request_size = request_size;
246                 call->request = kmalloc(request_size, GFP_NOFS);
247                 if (!call->request)
248                         goto nomem_free;
249         }
250
251         if (reply_max) {
252                 call->reply_max = reply_max;
253                 call->buffer = kmalloc(reply_max, GFP_NOFS);
254                 if (!call->buffer)
255                         goto nomem_free;
256         }
257
258         afs_extract_to_buf(call, call->reply_max);
259         call->operation_ID = type->op;
260         init_waitqueue_head(&call->waitq);
261         return call;
262
263 nomem_free:
264         afs_put_call(call);
265 nomem_call:
266         return NULL;
267 }
268
269 /*
270  * clean up a call with flat buffer
271  */
272 void afs_flat_call_destructor(struct afs_call *call)
273 {
274         _enter("");
275
276         kfree(call->request);
277         call->request = NULL;
278         kfree(call->buffer);
279         call->buffer = NULL;
280 }
281
282 #define AFS_BVEC_MAX 8
283
284 /*
285  * Load the given bvec with the next few pages.
286  */
287 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
288                           struct bio_vec *bv, pgoff_t first, pgoff_t last,
289                           unsigned offset)
290 {
291         struct page *pages[AFS_BVEC_MAX];
292         unsigned int nr, n, i, to, bytes = 0;
293
294         nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
295         n = find_get_pages_contig(call->mapping, first, nr, pages);
296         ASSERTCMP(n, ==, nr);
297
298         msg->msg_flags |= MSG_MORE;
299         for (i = 0; i < nr; i++) {
300                 to = PAGE_SIZE;
301                 if (first + i >= last) {
302                         to = call->last_to;
303                         msg->msg_flags &= ~MSG_MORE;
304                 }
305                 bv[i].bv_page = pages[i];
306                 bv[i].bv_len = to - offset;
307                 bv[i].bv_offset = offset;
308                 bytes += to - offset;
309                 offset = 0;
310         }
311
312         iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
313 }
314
315 /*
316  * Advance the AFS call state when the RxRPC call ends the transmit phase.
317  */
318 static void afs_notify_end_request_tx(struct sock *sock,
319                                       struct rxrpc_call *rxcall,
320                                       unsigned long call_user_ID)
321 {
322         struct afs_call *call = (struct afs_call *)call_user_ID;
323
324         afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
325 }
326
327 /*
328  * attach the data from a bunch of pages on an inode to a call
329  */
330 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
331 {
332         struct bio_vec bv[AFS_BVEC_MAX];
333         unsigned int bytes, nr, loop, offset;
334         pgoff_t first = call->first, last = call->last;
335         int ret;
336
337         offset = call->first_offset;
338         call->first_offset = 0;
339
340         do {
341                 afs_load_bvec(call, msg, bv, first, last, offset);
342                 trace_afs_send_pages(call, msg, first, last, offset);
343
344                 offset = 0;
345                 bytes = msg->msg_iter.count;
346                 nr = msg->msg_iter.nr_segs;
347
348                 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
349                                              bytes, afs_notify_end_request_tx);
350                 for (loop = 0; loop < nr; loop++)
351                         put_page(bv[loop].bv_page);
352                 if (ret < 0)
353                         break;
354
355                 first += nr;
356         } while (first <= last);
357
358         trace_afs_sent_pages(call, call->first, last, first, ret);
359         return ret;
360 }
361
362 /*
363  * Initiate a call and synchronously queue up the parameters for dispatch.  Any
364  * error is stored into the call struct, which the caller must check for.
365  */
366 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
367 {
368         struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
369         struct rxrpc_call *rxcall;
370         struct msghdr msg;
371         struct kvec iov[1];
372         s64 tx_total_len;
373         int ret;
374
375         _enter(",{%pISp},", &srx->transport);
376
377         ASSERT(call->type != NULL);
378         ASSERT(call->type->name != NULL);
379
380         _debug("____MAKE %p{%s,%x} [%d]____",
381                call, call->type->name, key_serial(call->key),
382                atomic_read(&call->net->nr_outstanding_calls));
383
384         call->addr_ix = ac->index;
385         call->alist = afs_get_addrlist(ac->alist);
386
387         /* Work out the length we're going to transmit.  This is awkward for
388          * calls such as FS.StoreData where there's an extra injection of data
389          * after the initial fixed part.
390          */
391         tx_total_len = call->request_size;
392         if (call->send_pages) {
393                 if (call->last == call->first) {
394                         tx_total_len += call->last_to - call->first_offset;
395                 } else {
396                         /* It looks mathematically like you should be able to
397                          * combine the following lines with the ones above, but
398                          * unsigned arithmetic is fun when it wraps...
399                          */
400                         tx_total_len += PAGE_SIZE - call->first_offset;
401                         tx_total_len += call->last_to;
402                         tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
403                 }
404         }
405
406         /* If the call is going to be asynchronous, we need an extra ref for
407          * the call to hold itself so the caller need not hang on to its ref.
408          */
409         if (call->async)
410                 afs_get_call(call, afs_call_trace_get);
411
412         /* create a call */
413         rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
414                                          (unsigned long)call,
415                                          tx_total_len, gfp,
416                                          (call->async ?
417                                           afs_wake_up_async_call :
418                                           afs_wake_up_call_waiter),
419                                          call->upgrade,
420                                          call->debug_id);
421         if (IS_ERR(rxcall)) {
422                 ret = PTR_ERR(rxcall);
423                 call->error = ret;
424                 goto error_kill_call;
425         }
426
427         call->rxcall = rxcall;
428
429         /* send the request */
430         iov[0].iov_base = call->request;
431         iov[0].iov_len  = call->request_size;
432
433         msg.msg_name            = NULL;
434         msg.msg_namelen         = 0;
435         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
436         msg.msg_control         = NULL;
437         msg.msg_controllen      = 0;
438         msg.msg_flags           = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
439
440         ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
441                                      &msg, call->request_size,
442                                      afs_notify_end_request_tx);
443         if (ret < 0)
444                 goto error_do_abort;
445
446         if (call->send_pages) {
447                 ret = afs_send_pages(call, &msg);
448                 if (ret < 0)
449                         goto error_do_abort;
450         }
451
452         /* Note that at this point, we may have received the reply or an abort
453          * - and an asynchronous call may already have completed.
454          *
455          * afs_wait_for_call_to_complete(call, ac)
456          * must be called to synchronously clean up.
457          */
458         return;
459
460 error_do_abort:
461         if (ret != -ECONNABORTED) {
462                 rxrpc_kernel_abort_call(call->net->socket, rxcall,
463                                         RX_USER_ABORT, ret, "KSD");
464         } else {
465                 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
466                 rxrpc_kernel_recv_data(call->net->socket, rxcall,
467                                        &msg.msg_iter, false,
468                                        &call->abort_code, &call->service_id);
469                 ac->abort_code = call->abort_code;
470                 ac->responded = true;
471         }
472         call->error = ret;
473         trace_afs_call_done(call);
474 error_kill_call:
475         if (call->type->done)
476                 call->type->done(call);
477
478         /* We need to dispose of the extra ref we grabbed for an async call.
479          * The call, however, might be queued on afs_async_calls and we need to
480          * make sure we don't get any more notifications that might requeue it.
481          */
482         if (call->rxcall) {
483                 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
484                 call->rxcall = NULL;
485         }
486         if (call->async) {
487                 if (cancel_work_sync(&call->async_work))
488                         afs_put_call(call);
489                 afs_put_call(call);
490         }
491
492         ac->error = ret;
493         call->state = AFS_CALL_COMPLETE;
494         _leave(" = %d", ret);
495 }
496
497 /*
498  * deliver messages to a call
499  */
500 static void afs_deliver_to_call(struct afs_call *call)
501 {
502         enum afs_call_state state;
503         u32 abort_code, remote_abort = 0;
504         int ret;
505
506         _enter("%s", call->type->name);
507
508         while (state = READ_ONCE(call->state),
509                state == AFS_CALL_CL_AWAIT_REPLY ||
510                state == AFS_CALL_SV_AWAIT_OP_ID ||
511                state == AFS_CALL_SV_AWAIT_REQUEST ||
512                state == AFS_CALL_SV_AWAIT_ACK
513                ) {
514                 if (state == AFS_CALL_SV_AWAIT_ACK) {
515                         iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
516                         ret = rxrpc_kernel_recv_data(call->net->socket,
517                                                      call->rxcall, &call->iter,
518                                                      false, &remote_abort,
519                                                      &call->service_id);
520                         trace_afs_receive_data(call, &call->iter, false, ret);
521
522                         if (ret == -EINPROGRESS || ret == -EAGAIN)
523                                 return;
524                         if (ret < 0 || ret == 1) {
525                                 if (ret == 1)
526                                         ret = 0;
527                                 goto call_complete;
528                         }
529                         return;
530                 }
531
532                 if (call->want_reply_time &&
533                     rxrpc_kernel_get_reply_time(call->net->socket,
534                                                 call->rxcall,
535                                                 &call->reply_time))
536                         call->want_reply_time = false;
537
538                 ret = call->type->deliver(call);
539                 state = READ_ONCE(call->state);
540                 switch (ret) {
541                 case 0:
542                         afs_queue_call_work(call);
543                         if (state == AFS_CALL_CL_PROC_REPLY) {
544                                 if (call->cbi)
545                                         set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
546                                                 &call->cbi->server->flags);
547                                 goto call_complete;
548                         }
549                         ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
550                         goto done;
551                 case -EINPROGRESS:
552                 case -EAGAIN:
553                         goto out;
554                 case -ECONNABORTED:
555                         ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
556                         goto done;
557                 case -ENOTSUPP:
558                         abort_code = RXGEN_OPCODE;
559                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
560                                                 abort_code, ret, "KIV");
561                         goto local_abort;
562                 case -EIO:
563                         pr_err("kAFS: Call %u in bad state %u\n",
564                                call->debug_id, state);
565                         /* Fall through */
566                 case -ENODATA:
567                 case -EBADMSG:
568                 case -EMSGSIZE:
569                         abort_code = RXGEN_CC_UNMARSHAL;
570                         if (state != AFS_CALL_CL_AWAIT_REPLY)
571                                 abort_code = RXGEN_SS_UNMARSHAL;
572                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
573                                                 abort_code, ret, "KUM");
574                         goto local_abort;
575                 default:
576                         abort_code = RX_USER_ABORT;
577                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
578                                                 abort_code, ret, "KER");
579                         goto local_abort;
580                 }
581         }
582
583 done:
584         if (call->type->done)
585                 call->type->done(call);
586         if (state == AFS_CALL_COMPLETE && call->incoming)
587                 afs_put_call(call);
588 out:
589         _leave("");
590         return;
591
592 local_abort:
593         abort_code = 0;
594 call_complete:
595         afs_set_call_complete(call, ret, remote_abort);
596         state = AFS_CALL_COMPLETE;
597         goto done;
598 }
599
600 /*
601  * Wait synchronously for a call to complete and clean up the call struct.
602  */
603 long afs_wait_for_call_to_complete(struct afs_call *call,
604                                    struct afs_addr_cursor *ac)
605 {
606         signed long rtt2, timeout;
607         long ret;
608         bool stalled = false;
609         u64 rtt;
610         u32 life, last_life;
611         bool rxrpc_complete = false;
612
613         DECLARE_WAITQUEUE(myself, current);
614
615         _enter("");
616
617         ret = call->error;
618         if (ret < 0)
619                 goto out;
620
621         rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
622         rtt2 = nsecs_to_jiffies64(rtt) * 2;
623         if (rtt2 < 2)
624                 rtt2 = 2;
625
626         timeout = rtt2;
627         rxrpc_kernel_check_life(call->net->socket, call->rxcall, &last_life);
628
629         add_wait_queue(&call->waitq, &myself);
630         for (;;) {
631                 set_current_state(TASK_UNINTERRUPTIBLE);
632
633                 /* deliver any messages that are in the queue */
634                 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
635                     call->need_attention) {
636                         call->need_attention = false;
637                         __set_current_state(TASK_RUNNING);
638                         afs_deliver_to_call(call);
639                         continue;
640                 }
641
642                 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
643                         break;
644
645                 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall, &life)) {
646                         /* rxrpc terminated the call. */
647                         rxrpc_complete = true;
648                         break;
649                 }
650
651                 if (timeout == 0 &&
652                     life == last_life && signal_pending(current)) {
653                         if (stalled)
654                                 break;
655                         __set_current_state(TASK_RUNNING);
656                         rxrpc_kernel_probe_life(call->net->socket, call->rxcall);
657                         timeout = rtt2;
658                         stalled = true;
659                         continue;
660                 }
661
662                 if (life != last_life) {
663                         timeout = rtt2;
664                         last_life = life;
665                         stalled = false;
666                 }
667
668                 timeout = schedule_timeout(timeout);
669         }
670
671         remove_wait_queue(&call->waitq, &myself);
672         __set_current_state(TASK_RUNNING);
673
674         if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
675                 if (rxrpc_complete) {
676                         afs_set_call_complete(call, call->error, call->abort_code);
677                 } else {
678                         /* Kill off the call if it's still live. */
679                         _debug("call interrupted");
680                         if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
681                                                     RX_USER_ABORT, -EINTR, "KWI"))
682                                 afs_set_call_complete(call, -EINTR, 0);
683                 }
684         }
685
686         spin_lock_bh(&call->state_lock);
687         ac->abort_code = call->abort_code;
688         ac->error = call->error;
689         spin_unlock_bh(&call->state_lock);
690
691         ret = ac->error;
692         switch (ret) {
693         case 0:
694                 if (call->ret_reply0) {
695                         ret = (long)call->reply[0];
696                         call->reply[0] = NULL;
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 }