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