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