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