OSDN Git Service

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