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