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