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