afs: Don't invalidate callback if AFS_VNODE_DIR_VALID not set
[sfrench/cifs-2.6.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->intr,
421                                          call->debug_id);
422         if (IS_ERR(rxcall)) {
423                 ret = PTR_ERR(rxcall);
424                 call->error = ret;
425                 goto error_kill_call;
426         }
427
428         call->rxcall = rxcall;
429
430         if (call->max_lifespan)
431                 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
432                                           call->max_lifespan);
433
434         /* send the request */
435         iov[0].iov_base = call->request;
436         iov[0].iov_len  = call->request_size;
437
438         msg.msg_name            = NULL;
439         msg.msg_namelen         = 0;
440         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
441         msg.msg_control         = NULL;
442         msg.msg_controllen      = 0;
443         msg.msg_flags           = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
444
445         ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
446                                      &msg, call->request_size,
447                                      afs_notify_end_request_tx);
448         if (ret < 0)
449                 goto error_do_abort;
450
451         if (call->send_pages) {
452                 ret = afs_send_pages(call, &msg);
453                 if (ret < 0)
454                         goto error_do_abort;
455         }
456
457         /* Note that at this point, we may have received the reply or an abort
458          * - and an asynchronous call may already have completed.
459          *
460          * afs_wait_for_call_to_complete(call, ac)
461          * must be called to synchronously clean up.
462          */
463         return;
464
465 error_do_abort:
466         if (ret != -ECONNABORTED) {
467                 rxrpc_kernel_abort_call(call->net->socket, rxcall,
468                                         RX_USER_ABORT, ret, "KSD");
469         } else {
470                 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
471                 rxrpc_kernel_recv_data(call->net->socket, rxcall,
472                                        &msg.msg_iter, false,
473                                        &call->abort_code, &call->service_id);
474                 ac->abort_code = call->abort_code;
475                 ac->responded = true;
476         }
477         call->error = ret;
478         trace_afs_call_done(call);
479 error_kill_call:
480         if (call->type->done)
481                 call->type->done(call);
482
483         /* We need to dispose of the extra ref we grabbed for an async call.
484          * The call, however, might be queued on afs_async_calls and we need to
485          * make sure we don't get any more notifications that might requeue it.
486          */
487         if (call->rxcall) {
488                 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
489                 call->rxcall = NULL;
490         }
491         if (call->async) {
492                 if (cancel_work_sync(&call->async_work))
493                         afs_put_call(call);
494                 afs_put_call(call);
495         }
496
497         ac->error = ret;
498         call->state = AFS_CALL_COMPLETE;
499         _leave(" = %d", ret);
500 }
501
502 /*
503  * deliver messages to a call
504  */
505 static void afs_deliver_to_call(struct afs_call *call)
506 {
507         enum afs_call_state state;
508         u32 abort_code, remote_abort = 0;
509         int ret;
510
511         _enter("%s", call->type->name);
512
513         while (state = READ_ONCE(call->state),
514                state == AFS_CALL_CL_AWAIT_REPLY ||
515                state == AFS_CALL_SV_AWAIT_OP_ID ||
516                state == AFS_CALL_SV_AWAIT_REQUEST ||
517                state == AFS_CALL_SV_AWAIT_ACK
518                ) {
519                 if (state == AFS_CALL_SV_AWAIT_ACK) {
520                         iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
521                         ret = rxrpc_kernel_recv_data(call->net->socket,
522                                                      call->rxcall, &call->iter,
523                                                      false, &remote_abort,
524                                                      &call->service_id);
525                         trace_afs_receive_data(call, &call->iter, false, ret);
526
527                         if (ret == -EINPROGRESS || ret == -EAGAIN)
528                                 return;
529                         if (ret < 0 || ret == 1) {
530                                 if (ret == 1)
531                                         ret = 0;
532                                 goto call_complete;
533                         }
534                         return;
535                 }
536
537                 if (call->want_reply_time &&
538                     rxrpc_kernel_get_reply_time(call->net->socket,
539                                                 call->rxcall,
540                                                 &call->reply_time))
541                         call->want_reply_time = false;
542
543                 ret = call->type->deliver(call);
544                 state = READ_ONCE(call->state);
545                 switch (ret) {
546                 case 0:
547                         afs_queue_call_work(call);
548                         if (state == AFS_CALL_CL_PROC_REPLY) {
549                                 if (call->cbi)
550                                         set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
551                                                 &call->cbi->server->flags);
552                                 goto call_complete;
553                         }
554                         ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
555                         goto done;
556                 case -EINPROGRESS:
557                 case -EAGAIN:
558                         goto out;
559                 case -ECONNABORTED:
560                         ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
561                         goto done;
562                 case -ENOTSUPP:
563                         abort_code = RXGEN_OPCODE;
564                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
565                                                 abort_code, ret, "KIV");
566                         goto local_abort;
567                 case -EIO:
568                         pr_err("kAFS: Call %u in bad state %u\n",
569                                call->debug_id, state);
570                         /* Fall through */
571                 case -ENODATA:
572                 case -EBADMSG:
573                 case -EMSGSIZE:
574                         abort_code = RXGEN_CC_UNMARSHAL;
575                         if (state != AFS_CALL_CL_AWAIT_REPLY)
576                                 abort_code = RXGEN_SS_UNMARSHAL;
577                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
578                                                 abort_code, ret, "KUM");
579                         goto local_abort;
580                 default:
581                         abort_code = RX_USER_ABORT;
582                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
583                                                 abort_code, ret, "KER");
584                         goto local_abort;
585                 }
586         }
587
588 done:
589         if (call->type->done)
590                 call->type->done(call);
591         if (state == AFS_CALL_COMPLETE && call->incoming)
592                 afs_put_call(call);
593 out:
594         _leave("");
595         return;
596
597 local_abort:
598         abort_code = 0;
599 call_complete:
600         afs_set_call_complete(call, ret, remote_abort);
601         state = AFS_CALL_COMPLETE;
602         goto done;
603 }
604
605 /*
606  * Wait synchronously for a call to complete and clean up the call struct.
607  */
608 long afs_wait_for_call_to_complete(struct afs_call *call,
609                                    struct afs_addr_cursor *ac)
610 {
611         signed long rtt2, timeout;
612         long ret;
613         bool stalled = false;
614         u64 rtt;
615         u32 life, last_life;
616         bool rxrpc_complete = false;
617
618         DECLARE_WAITQUEUE(myself, current);
619
620         _enter("");
621
622         ret = call->error;
623         if (ret < 0)
624                 goto out;
625
626         rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
627         rtt2 = nsecs_to_jiffies64(rtt) * 2;
628         if (rtt2 < 2)
629                 rtt2 = 2;
630
631         timeout = rtt2;
632         rxrpc_kernel_check_life(call->net->socket, call->rxcall, &last_life);
633
634         add_wait_queue(&call->waitq, &myself);
635         for (;;) {
636                 set_current_state(TASK_UNINTERRUPTIBLE);
637
638                 /* deliver any messages that are in the queue */
639                 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
640                     call->need_attention) {
641                         call->need_attention = false;
642                         __set_current_state(TASK_RUNNING);
643                         afs_deliver_to_call(call);
644                         continue;
645                 }
646
647                 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
648                         break;
649
650                 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall, &life)) {
651                         /* rxrpc terminated the call. */
652                         rxrpc_complete = true;
653                         break;
654                 }
655
656                 if (call->intr && timeout == 0 &&
657                     life == last_life && signal_pending(current)) {
658                         if (stalled)
659                                 break;
660                         __set_current_state(TASK_RUNNING);
661                         rxrpc_kernel_probe_life(call->net->socket, call->rxcall);
662                         timeout = rtt2;
663                         stalled = true;
664                         continue;
665                 }
666
667                 if (life != last_life) {
668                         timeout = rtt2;
669                         last_life = life;
670                         stalled = false;
671                 }
672
673                 timeout = schedule_timeout(timeout);
674         }
675
676         remove_wait_queue(&call->waitq, &myself);
677         __set_current_state(TASK_RUNNING);
678
679         if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
680                 if (rxrpc_complete) {
681                         afs_set_call_complete(call, call->error, call->abort_code);
682                 } else {
683                         /* Kill off the call if it's still live. */
684                         _debug("call interrupted");
685                         if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
686                                                     RX_USER_ABORT, -EINTR, "KWI"))
687                                 afs_set_call_complete(call, -EINTR, 0);
688                 }
689         }
690
691         spin_lock_bh(&call->state_lock);
692         ac->abort_code = call->abort_code;
693         ac->error = call->error;
694         spin_unlock_bh(&call->state_lock);
695
696         ret = ac->error;
697         switch (ret) {
698         case 0:
699                 if (call->ret_reply0) {
700                         ret = (long)call->reply[0];
701                         call->reply[0] = NULL;
702                 }
703                 /* Fall through */
704         case -ECONNABORTED:
705                 ac->responded = true;
706                 break;
707         }
708
709 out:
710         _debug("call complete");
711         afs_put_call(call);
712         _leave(" = %p", (void *)ret);
713         return ret;
714 }
715
716 /*
717  * wake up a waiting call
718  */
719 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
720                                     unsigned long call_user_ID)
721 {
722         struct afs_call *call = (struct afs_call *)call_user_ID;
723
724         call->need_attention = true;
725         wake_up(&call->waitq);
726 }
727
728 /*
729  * wake up an asynchronous call
730  */
731 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
732                                    unsigned long call_user_ID)
733 {
734         struct afs_call *call = (struct afs_call *)call_user_ID;
735         int u;
736
737         trace_afs_notify_call(rxcall, call);
738         call->need_attention = true;
739
740         u = atomic_fetch_add_unless(&call->usage, 1, 0);
741         if (u != 0) {
742                 trace_afs_call(call, afs_call_trace_wake, u,
743                                atomic_read(&call->net->nr_outstanding_calls),
744                                __builtin_return_address(0));
745
746                 if (!queue_work(afs_async_calls, &call->async_work))
747                         afs_put_call(call);
748         }
749 }
750
751 /*
752  * Delete an asynchronous call.  The work item carries a ref to the call struct
753  * that we need to release.
754  */
755 static void afs_delete_async_call(struct work_struct *work)
756 {
757         struct afs_call *call = container_of(work, struct afs_call, async_work);
758
759         _enter("");
760
761         afs_put_call(call);
762
763         _leave("");
764 }
765
766 /*
767  * Perform I/O processing on an asynchronous call.  The work item carries a ref
768  * to the call struct that we either need to release or to pass on.
769  */
770 static void afs_process_async_call(struct work_struct *work)
771 {
772         struct afs_call *call = container_of(work, struct afs_call, async_work);
773
774         _enter("");
775
776         if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
777                 call->need_attention = false;
778                 afs_deliver_to_call(call);
779         }
780
781         if (call->state == AFS_CALL_COMPLETE) {
782                 /* We have two refs to release - one from the alloc and one
783                  * queued with the work item - and we can't just deallocate the
784                  * call because the work item may be queued again.
785                  */
786                 call->async_work.func = afs_delete_async_call;
787                 if (!queue_work(afs_async_calls, &call->async_work))
788                         afs_put_call(call);
789         }
790
791         afs_put_call(call);
792         _leave("");
793 }
794
795 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
796 {
797         struct afs_call *call = (struct afs_call *)user_call_ID;
798
799         call->rxcall = rxcall;
800 }
801
802 /*
803  * Charge the incoming call preallocation.
804  */
805 void afs_charge_preallocation(struct work_struct *work)
806 {
807         struct afs_net *net =
808                 container_of(work, struct afs_net, charge_preallocation_work);
809         struct afs_call *call = net->spare_incoming_call;
810
811         for (;;) {
812                 if (!call) {
813                         call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
814                         if (!call)
815                                 break;
816
817                         call->async = true;
818                         call->state = AFS_CALL_SV_AWAIT_OP_ID;
819                         init_waitqueue_head(&call->waitq);
820                         afs_extract_to_tmp(call);
821                 }
822
823                 if (rxrpc_kernel_charge_accept(net->socket,
824                                                afs_wake_up_async_call,
825                                                afs_rx_attach,
826                                                (unsigned long)call,
827                                                GFP_KERNEL,
828                                                call->debug_id) < 0)
829                         break;
830                 call = NULL;
831         }
832         net->spare_incoming_call = call;
833 }
834
835 /*
836  * Discard a preallocated call when a socket is shut down.
837  */
838 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
839                                     unsigned long user_call_ID)
840 {
841         struct afs_call *call = (struct afs_call *)user_call_ID;
842
843         call->rxcall = NULL;
844         afs_put_call(call);
845 }
846
847 /*
848  * Notification of an incoming call.
849  */
850 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
851                             unsigned long user_call_ID)
852 {
853         struct afs_net *net = afs_sock2net(sk);
854
855         queue_work(afs_wq, &net->charge_preallocation_work);
856 }
857
858 /*
859  * Grab the operation ID from an incoming cache manager call.  The socket
860  * buffer is discarded on error or if we don't yet have sufficient data.
861  */
862 static int afs_deliver_cm_op_id(struct afs_call *call)
863 {
864         int ret;
865
866         _enter("{%zu}", iov_iter_count(call->_iter));
867
868         /* the operation ID forms the first four bytes of the request data */
869         ret = afs_extract_data(call, true);
870         if (ret < 0)
871                 return ret;
872
873         call->operation_ID = ntohl(call->tmp);
874         afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
875
876         /* ask the cache manager to route the call (it'll change the call type
877          * if successful) */
878         if (!afs_cm_incoming_call(call))
879                 return -ENOTSUPP;
880
881         trace_afs_cb_call(call);
882
883         /* pass responsibility for the remainer of this message off to the
884          * cache manager op */
885         return call->type->deliver(call);
886 }
887
888 /*
889  * Advance the AFS call state when an RxRPC service call ends the transmit
890  * phase.
891  */
892 static void afs_notify_end_reply_tx(struct sock *sock,
893                                     struct rxrpc_call *rxcall,
894                                     unsigned long call_user_ID)
895 {
896         struct afs_call *call = (struct afs_call *)call_user_ID;
897
898         afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
899 }
900
901 /*
902  * send an empty reply
903  */
904 void afs_send_empty_reply(struct afs_call *call)
905 {
906         struct afs_net *net = call->net;
907         struct msghdr msg;
908
909         _enter("");
910
911         rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
912
913         msg.msg_name            = NULL;
914         msg.msg_namelen         = 0;
915         iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
916         msg.msg_control         = NULL;
917         msg.msg_controllen      = 0;
918         msg.msg_flags           = 0;
919
920         switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
921                                        afs_notify_end_reply_tx)) {
922         case 0:
923                 _leave(" [replied]");
924                 return;
925
926         case -ENOMEM:
927                 _debug("oom");
928                 rxrpc_kernel_abort_call(net->socket, call->rxcall,
929                                         RX_USER_ABORT, -ENOMEM, "KOO");
930                 /* Fall through */
931         default:
932                 _leave(" [error]");
933                 return;
934         }
935 }
936
937 /*
938  * send a simple reply
939  */
940 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
941 {
942         struct afs_net *net = call->net;
943         struct msghdr msg;
944         struct kvec iov[1];
945         int n;
946
947         _enter("");
948
949         rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
950
951         iov[0].iov_base         = (void *) buf;
952         iov[0].iov_len          = len;
953         msg.msg_name            = NULL;
954         msg.msg_namelen         = 0;
955         iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
956         msg.msg_control         = NULL;
957         msg.msg_controllen      = 0;
958         msg.msg_flags           = 0;
959
960         n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
961                                    afs_notify_end_reply_tx);
962         if (n >= 0) {
963                 /* Success */
964                 _leave(" [replied]");
965                 return;
966         }
967
968         if (n == -ENOMEM) {
969                 _debug("oom");
970                 rxrpc_kernel_abort_call(net->socket, call->rxcall,
971                                         RX_USER_ABORT, -ENOMEM, "KOO");
972         }
973         _leave(" [error]");
974 }
975
976 /*
977  * Extract a piece of data from the received data socket buffers.
978  */
979 int afs_extract_data(struct afs_call *call, bool want_more)
980 {
981         struct afs_net *net = call->net;
982         struct iov_iter *iter = call->_iter;
983         enum afs_call_state state;
984         u32 remote_abort = 0;
985         int ret;
986
987         _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
988
989         ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
990                                      want_more, &remote_abort,
991                                      &call->service_id);
992         if (ret == 0 || ret == -EAGAIN)
993                 return ret;
994
995         state = READ_ONCE(call->state);
996         if (ret == 1) {
997                 switch (state) {
998                 case AFS_CALL_CL_AWAIT_REPLY:
999                         afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
1000                         break;
1001                 case AFS_CALL_SV_AWAIT_REQUEST:
1002                         afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
1003                         break;
1004                 case AFS_CALL_COMPLETE:
1005                         kdebug("prem complete %d", call->error);
1006                         return afs_io_error(call, afs_io_error_extract);
1007                 default:
1008                         break;
1009                 }
1010                 return 0;
1011         }
1012
1013         afs_set_call_complete(call, ret, remote_abort);
1014         return ret;
1015 }
1016
1017 /*
1018  * Log protocol error production.
1019  */
1020 noinline int afs_protocol_error(struct afs_call *call, int error,
1021                                 enum afs_eproto_cause cause)
1022 {
1023         trace_afs_protocol_error(call, error, cause);
1024         return error;
1025 }