]> Git Repo - linux.git/blob - fs/afs/rxrpc.c
afs: Overhaul permit caching
[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 ([email protected])
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 *);
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                 kfree(call->request);
166                 kfree(call);
167
168                 o = atomic_dec_return(&net->nr_outstanding_calls);
169                 trace_afs_call(call, afs_call_trace_free, 0, o,
170                                __builtin_return_address(0));
171                 if (o == 0)
172                         wake_up_atomic_t(&net->nr_outstanding_calls);
173         }
174 }
175
176 /*
177  * Queue the call for actual work.  Returns 0 unconditionally for convenience.
178  */
179 int afs_queue_call_work(struct afs_call *call)
180 {
181         int u = atomic_inc_return(&call->usage);
182
183         trace_afs_call(call, afs_call_trace_work, u,
184                        atomic_read(&call->net->nr_outstanding_calls),
185                        __builtin_return_address(0));
186
187         INIT_WORK(&call->work, call->type->work);
188
189         if (!queue_work(afs_wq, &call->work))
190                 afs_put_call(call);
191         return 0;
192 }
193
194 /*
195  * allocate a call with flat request and reply buffers
196  */
197 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
198                                      const struct afs_call_type *type,
199                                      size_t request_size, size_t reply_max)
200 {
201         struct afs_call *call;
202
203         call = afs_alloc_call(net, type, GFP_NOFS);
204         if (!call)
205                 goto nomem_call;
206
207         if (request_size) {
208                 call->request_size = request_size;
209                 call->request = kmalloc(request_size, GFP_NOFS);
210                 if (!call->request)
211                         goto nomem_free;
212         }
213
214         if (reply_max) {
215                 call->reply_max = reply_max;
216                 call->buffer = kmalloc(reply_max, GFP_NOFS);
217                 if (!call->buffer)
218                         goto nomem_free;
219         }
220
221         init_waitqueue_head(&call->waitq);
222         return call;
223
224 nomem_free:
225         afs_put_call(call);
226 nomem_call:
227         return NULL;
228 }
229
230 /*
231  * clean up a call with flat buffer
232  */
233 void afs_flat_call_destructor(struct afs_call *call)
234 {
235         _enter("");
236
237         kfree(call->request);
238         call->request = NULL;
239         kfree(call->buffer);
240         call->buffer = NULL;
241 }
242
243 #define AFS_BVEC_MAX 8
244
245 /*
246  * Load the given bvec with the next few pages.
247  */
248 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
249                           struct bio_vec *bv, pgoff_t first, pgoff_t last,
250                           unsigned offset)
251 {
252         struct page *pages[AFS_BVEC_MAX];
253         unsigned int nr, n, i, to, bytes = 0;
254
255         nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
256         n = find_get_pages_contig(call->mapping, first, nr, pages);
257         ASSERTCMP(n, ==, nr);
258
259         msg->msg_flags |= MSG_MORE;
260         for (i = 0; i < nr; i++) {
261                 to = PAGE_SIZE;
262                 if (first + i >= last) {
263                         to = call->last_to;
264                         msg->msg_flags &= ~MSG_MORE;
265                 }
266                 bv[i].bv_page = pages[i];
267                 bv[i].bv_len = to - offset;
268                 bv[i].bv_offset = offset;
269                 bytes += to - offset;
270                 offset = 0;
271         }
272
273         iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
274 }
275
276 /*
277  * Advance the AFS call state when the RxRPC call ends the transmit phase.
278  */
279 static void afs_notify_end_request_tx(struct sock *sock,
280                                       struct rxrpc_call *rxcall,
281                                       unsigned long call_user_ID)
282 {
283         struct afs_call *call = (struct afs_call *)call_user_ID;
284
285         if (call->state == AFS_CALL_REQUESTING)
286                 call->state = AFS_CALL_AWAIT_REPLY;
287 }
288
289 /*
290  * attach the data from a bunch of pages on an inode to a call
291  */
292 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
293 {
294         struct bio_vec bv[AFS_BVEC_MAX];
295         unsigned int bytes, nr, loop, offset;
296         pgoff_t first = call->first, last = call->last;
297         int ret;
298
299         offset = call->first_offset;
300         call->first_offset = 0;
301
302         do {
303                 afs_load_bvec(call, msg, bv, first, last, offset);
304                 offset = 0;
305                 bytes = msg->msg_iter.count;
306                 nr = msg->msg_iter.nr_segs;
307
308                 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
309                                              bytes, afs_notify_end_request_tx);
310                 for (loop = 0; loop < nr; loop++)
311                         put_page(bv[loop].bv_page);
312                 if (ret < 0)
313                         break;
314
315                 first += nr;
316         } while (first <= last);
317
318         return ret;
319 }
320
321 /*
322  * initiate a call
323  */
324 long afs_make_call(struct sockaddr_rxrpc *srx, struct afs_call *call,
325                    gfp_t gfp, bool async)
326 {
327         struct rxrpc_call *rxcall;
328         struct msghdr msg;
329         struct kvec iov[1];
330         size_t offset;
331         s64 tx_total_len;
332         u32 abort_code;
333         int ret;
334
335         _enter(",{%pISp},", &srx->transport);
336
337         ASSERT(call->type != NULL);
338         ASSERT(call->type->name != NULL);
339
340         _debug("____MAKE %p{%s,%x} [%d]____",
341                call, call->type->name, key_serial(call->key),
342                atomic_read(&call->net->nr_outstanding_calls));
343
344         call->async = async;
345
346         /* Work out the length we're going to transmit.  This is awkward for
347          * calls such as FS.StoreData where there's an extra injection of data
348          * after the initial fixed part.
349          */
350         tx_total_len = call->request_size;
351         if (call->send_pages) {
352                 tx_total_len += call->last_to - call->first_offset;
353                 tx_total_len += (call->last - call->first) * PAGE_SIZE;
354         }
355
356         /* create a call */
357         rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
358                                          (unsigned long)call,
359                                          tx_total_len, gfp,
360                                          (async ?
361                                           afs_wake_up_async_call :
362                                           afs_wake_up_call_waiter),
363                                          call->upgrade);
364         call->key = NULL;
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);
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                 abort_code = 0;
417                 offset = 0;
418                 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
419                                        0, &offset, false, &call->abort_code,
420                                        &call->service_id);
421                 ret = afs_abort_to_error(call->abort_code);
422         }
423 error_kill_call:
424         afs_put_call(call);
425         _leave(" = %d", ret);
426         return ret;
427 }
428
429 /*
430  * deliver messages to a call
431  */
432 static void afs_deliver_to_call(struct afs_call *call)
433 {
434         u32 abort_code;
435         int ret;
436
437         _enter("%s", call->type->name);
438
439         while (call->state == AFS_CALL_AWAIT_REPLY ||
440                call->state == AFS_CALL_AWAIT_OP_ID ||
441                call->state == AFS_CALL_AWAIT_REQUEST ||
442                call->state == AFS_CALL_AWAIT_ACK
443                ) {
444                 if (call->state == AFS_CALL_AWAIT_ACK) {
445                         size_t offset = 0;
446                         ret = rxrpc_kernel_recv_data(call->net->socket,
447                                                      call->rxcall,
448                                                      NULL, 0, &offset, false,
449                                                      &call->abort_code,
450                                                      &call->service_id);
451                         trace_afs_recv_data(call, 0, offset, false, ret);
452
453                         if (ret == -EINPROGRESS || ret == -EAGAIN)
454                                 return;
455                         if (ret == 1 || ret < 0) {
456                                 call->state = AFS_CALL_COMPLETE;
457                                 goto done;
458                         }
459                         return;
460                 }
461
462                 ret = call->type->deliver(call);
463                 switch (ret) {
464                 case 0:
465                         if (call->state == AFS_CALL_AWAIT_REPLY)
466                                 call->state = AFS_CALL_COMPLETE;
467                         goto done;
468                 case -EINPROGRESS:
469                 case -EAGAIN:
470                         goto out;
471                 case -ECONNABORTED:
472                         goto save_error;
473                 case -ENOTCONN:
474                         abort_code = RX_CALL_DEAD;
475                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
476                                                 abort_code, ret, "KNC");
477                         goto save_error;
478                 case -ENOTSUPP:
479                         abort_code = RXGEN_OPCODE;
480                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
481                                                 abort_code, ret, "KIV");
482                         goto save_error;
483                 case -ENODATA:
484                 case -EBADMSG:
485                 case -EMSGSIZE:
486                 default:
487                         abort_code = RXGEN_CC_UNMARSHAL;
488                         if (call->state != AFS_CALL_AWAIT_REPLY)
489                                 abort_code = RXGEN_SS_UNMARSHAL;
490                         rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
491                                                 abort_code, -EBADMSG, "KUM");
492                         goto save_error;
493                 }
494         }
495
496 done:
497         if (call->state == AFS_CALL_COMPLETE && call->incoming)
498                 afs_put_call(call);
499 out:
500         _leave("");
501         return;
502
503 save_error:
504         call->error = ret;
505         call->state = AFS_CALL_COMPLETE;
506         goto done;
507 }
508
509 /*
510  * wait synchronously for a call to complete
511  */
512 static long afs_wait_for_call_to_complete(struct afs_call *call)
513 {
514         signed long rtt2, timeout;
515         long ret;
516         u64 rtt;
517         u32 life, last_life;
518
519         DECLARE_WAITQUEUE(myself, current);
520
521         _enter("");
522
523         rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
524         rtt2 = nsecs_to_jiffies64(rtt) * 2;
525         if (rtt2 < 2)
526                 rtt2 = 2;
527
528         timeout = rtt2;
529         last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
530
531         add_wait_queue(&call->waitq, &myself);
532         for (;;) {
533                 set_current_state(TASK_UNINTERRUPTIBLE);
534
535                 /* deliver any messages that are in the queue */
536                 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
537                         call->need_attention = false;
538                         __set_current_state(TASK_RUNNING);
539                         afs_deliver_to_call(call);
540                         continue;
541                 }
542
543                 if (call->state == AFS_CALL_COMPLETE)
544                         break;
545
546                 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
547                 if (timeout == 0 &&
548                     life == last_life && signal_pending(current))
549                                 break;
550
551                 if (life != last_life) {
552                         timeout = rtt2;
553                         last_life = life;
554                 }
555
556                 timeout = schedule_timeout(timeout);
557         }
558
559         remove_wait_queue(&call->waitq, &myself);
560         __set_current_state(TASK_RUNNING);
561
562         /* Kill off the call if it's still live. */
563         if (call->state < AFS_CALL_COMPLETE) {
564                 _debug("call interrupted");
565                 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
566                                         RX_USER_ABORT, -EINTR, "KWI");
567         }
568
569         ret = call->error;
570         if (ret < 0) {
571                 ret = afs_abort_to_error(call->abort_code);
572         } else if (ret == 0 && call->ret_reply0) {
573                 ret = (long)call->reply[0];
574                 call->reply[0] = NULL;
575         }
576
577         _debug("call complete");
578         afs_put_call(call);
579         _leave(" = %p", (void *)ret);
580         return ret;
581 }
582
583 /*
584  * wake up a waiting call
585  */
586 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
587                                     unsigned long call_user_ID)
588 {
589         struct afs_call *call = (struct afs_call *)call_user_ID;
590
591         call->need_attention = true;
592         wake_up(&call->waitq);
593 }
594
595 /*
596  * wake up an asynchronous call
597  */
598 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
599                                    unsigned long call_user_ID)
600 {
601         struct afs_call *call = (struct afs_call *)call_user_ID;
602         int u;
603
604         trace_afs_notify_call(rxcall, call);
605         call->need_attention = true;
606
607         u = __atomic_add_unless(&call->usage, 1, 0);
608         if (u != 0) {
609                 trace_afs_call(call, afs_call_trace_wake, u,
610                                atomic_read(&call->net->nr_outstanding_calls),
611                                __builtin_return_address(0));
612
613                 if (!queue_work(afs_async_calls, &call->async_work))
614                         afs_put_call(call);
615         }
616 }
617
618 /*
619  * Delete an asynchronous call.  The work item carries a ref to the call struct
620  * that we need to release.
621  */
622 static void afs_delete_async_call(struct work_struct *work)
623 {
624         struct afs_call *call = container_of(work, struct afs_call, async_work);
625
626         _enter("");
627
628         afs_put_call(call);
629
630         _leave("");
631 }
632
633 /*
634  * Perform I/O processing on an asynchronous call.  The work item carries a ref
635  * to the call struct that we either need to release or to pass on.
636  */
637 static void afs_process_async_call(struct work_struct *work)
638 {
639         struct afs_call *call = container_of(work, struct afs_call, async_work);
640
641         _enter("");
642
643         if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
644                 call->need_attention = false;
645                 afs_deliver_to_call(call);
646         }
647
648         if (call->state == AFS_CALL_COMPLETE) {
649                 call->reply[0] = NULL;
650
651                 /* We have two refs to release - one from the alloc and one
652                  * queued with the work item - and we can't just deallocate the
653                  * call because the work item may be queued again.
654                  */
655                 call->async_work.func = afs_delete_async_call;
656                 if (!queue_work(afs_async_calls, &call->async_work))
657                         afs_put_call(call);
658         }
659
660         afs_put_call(call);
661         _leave("");
662 }
663
664 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
665 {
666         struct afs_call *call = (struct afs_call *)user_call_ID;
667
668         call->rxcall = rxcall;
669 }
670
671 /*
672  * Charge the incoming call preallocation.
673  */
674 void afs_charge_preallocation(struct work_struct *work)
675 {
676         struct afs_net *net =
677                 container_of(work, struct afs_net, charge_preallocation_work);
678         struct afs_call *call = net->spare_incoming_call;
679
680         for (;;) {
681                 if (!call) {
682                         call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
683                         if (!call)
684                                 break;
685
686                         call->async = true;
687                         call->state = AFS_CALL_AWAIT_OP_ID;
688                         init_waitqueue_head(&call->waitq);
689                 }
690
691                 if (rxrpc_kernel_charge_accept(net->socket,
692                                                afs_wake_up_async_call,
693                                                afs_rx_attach,
694                                                (unsigned long)call,
695                                                GFP_KERNEL) < 0)
696                         break;
697                 call = NULL;
698         }
699         net->spare_incoming_call = call;
700 }
701
702 /*
703  * Discard a preallocated call when a socket is shut down.
704  */
705 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
706                                     unsigned long user_call_ID)
707 {
708         struct afs_call *call = (struct afs_call *)user_call_ID;
709
710         call->rxcall = NULL;
711         afs_put_call(call);
712 }
713
714 /*
715  * Notification of an incoming call.
716  */
717 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
718                             unsigned long user_call_ID)
719 {
720         struct afs_net *net = afs_sock2net(sk);
721
722         queue_work(afs_wq, &net->charge_preallocation_work);
723 }
724
725 /*
726  * Grab the operation ID from an incoming cache manager call.  The socket
727  * buffer is discarded on error or if we don't yet have sufficient data.
728  */
729 static int afs_deliver_cm_op_id(struct afs_call *call)
730 {
731         int ret;
732
733         _enter("{%zu}", call->offset);
734
735         ASSERTCMP(call->offset, <, 4);
736
737         /* the operation ID forms the first four bytes of the request data */
738         ret = afs_extract_data(call, &call->tmp, 4, true);
739         if (ret < 0)
740                 return ret;
741
742         call->operation_ID = ntohl(call->tmp);
743         call->state = AFS_CALL_AWAIT_REQUEST;
744         call->offset = 0;
745
746         /* ask the cache manager to route the call (it'll change the call type
747          * if successful) */
748         if (!afs_cm_incoming_call(call))
749                 return -ENOTSUPP;
750
751         trace_afs_cb_call(call);
752
753         /* pass responsibility for the remainer of this message off to the
754          * cache manager op */
755         return call->type->deliver(call);
756 }
757
758 /*
759  * Advance the AFS call state when an RxRPC service call ends the transmit
760  * phase.
761  */
762 static void afs_notify_end_reply_tx(struct sock *sock,
763                                     struct rxrpc_call *rxcall,
764                                     unsigned long call_user_ID)
765 {
766         struct afs_call *call = (struct afs_call *)call_user_ID;
767
768         if (call->state == AFS_CALL_REPLYING)
769                 call->state = AFS_CALL_AWAIT_ACK;
770 }
771
772 /*
773  * send an empty reply
774  */
775 void afs_send_empty_reply(struct afs_call *call)
776 {
777         struct afs_net *net = call->net;
778         struct msghdr msg;
779
780         _enter("");
781
782         rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
783
784         msg.msg_name            = NULL;
785         msg.msg_namelen         = 0;
786         iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
787         msg.msg_control         = NULL;
788         msg.msg_controllen      = 0;
789         msg.msg_flags           = 0;
790
791         call->state = AFS_CALL_AWAIT_ACK;
792         switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
793                                        afs_notify_end_reply_tx)) {
794         case 0:
795                 _leave(" [replied]");
796                 return;
797
798         case -ENOMEM:
799                 _debug("oom");
800                 rxrpc_kernel_abort_call(net->socket, call->rxcall,
801                                         RX_USER_ABORT, -ENOMEM, "KOO");
802         default:
803                 _leave(" [error]");
804                 return;
805         }
806 }
807
808 /*
809  * send a simple reply
810  */
811 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
812 {
813         struct afs_net *net = call->net;
814         struct msghdr msg;
815         struct kvec iov[1];
816         int n;
817
818         _enter("");
819
820         rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
821
822         iov[0].iov_base         = (void *) buf;
823         iov[0].iov_len          = len;
824         msg.msg_name            = NULL;
825         msg.msg_namelen         = 0;
826         iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
827         msg.msg_control         = NULL;
828         msg.msg_controllen      = 0;
829         msg.msg_flags           = 0;
830
831         call->state = AFS_CALL_AWAIT_ACK;
832         n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
833                                    afs_notify_end_reply_tx);
834         if (n >= 0) {
835                 /* Success */
836                 _leave(" [replied]");
837                 return;
838         }
839
840         if (n == -ENOMEM) {
841                 _debug("oom");
842                 rxrpc_kernel_abort_call(net->socket, call->rxcall,
843                                         RX_USER_ABORT, -ENOMEM, "KOO");
844         }
845         _leave(" [error]");
846 }
847
848 /*
849  * Extract a piece of data from the received data socket buffers.
850  */
851 int afs_extract_data(struct afs_call *call, void *buf, size_t count,
852                      bool want_more)
853 {
854         struct afs_net *net = call->net;
855         int ret;
856
857         _enter("{%s,%zu},,%zu,%d",
858                call->type->name, call->offset, count, want_more);
859
860         ASSERTCMP(call->offset, <=, count);
861
862         ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
863                                      buf, count, &call->offset,
864                                      want_more, &call->abort_code,
865                                      &call->service_id);
866         trace_afs_recv_data(call, count, call->offset, want_more, ret);
867         if (ret == 0 || ret == -EAGAIN)
868                 return ret;
869
870         if (ret == 1) {
871                 switch (call->state) {
872                 case AFS_CALL_AWAIT_REPLY:
873                         call->state = AFS_CALL_COMPLETE;
874                         break;
875                 case AFS_CALL_AWAIT_REQUEST:
876                         call->state = AFS_CALL_REPLYING;
877                         break;
878                 default:
879                         break;
880                 }
881                 return 0;
882         }
883
884         if (ret == -ECONNABORTED)
885                 call->error = afs_abort_to_error(call->abort_code);
886         else
887                 call->error = ret;
888         call->state = AFS_CALL_COMPLETE;
889         return ret;
890 }
This page took 0.081101 seconds and 4 git commands to generate.