1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Maintain an RxRPC server socket to do AFS communications through
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/slab.h>
9 #include <linux/sched/signal.h>
12 #include <net/af_rxrpc.h>
15 #include "protocol_yfs.h"
16 #define RXRPC_TRACE_ONLY_DEFINE_ENUMS
17 #include <trace/events/rxrpc.h>
19 struct workqueue_struct *afs_async_calls;
21 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
22 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
23 static void afs_process_async_call(struct work_struct *);
24 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
25 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
26 static int afs_deliver_cm_op_id(struct afs_call *);
28 /* asynchronous incoming call initial processing */
29 static const struct afs_call_type afs_RXCMxxxx = {
31 .deliver = afs_deliver_cm_op_id,
35 * open an RxRPC socket and bind it to be a server for callback notifications
36 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
38 int afs_open_socket(struct afs_net *net)
40 struct sockaddr_rxrpc srx;
41 struct socket *socket;
46 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
50 socket->sk->sk_allocation = GFP_NOFS;
52 /* bind the callback manager's address to make this a server socket */
53 memset(&srx, 0, sizeof(srx));
54 srx.srx_family = AF_RXRPC;
55 srx.srx_service = CM_SERVICE;
56 srx.transport_type = SOCK_DGRAM;
57 srx.transport_len = sizeof(srx.transport.sin6);
58 srx.transport.sin6.sin6_family = AF_INET6;
59 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
61 ret = rxrpc_sock_set_min_security_level(socket->sk,
62 RXRPC_SECURITY_ENCRYPT);
66 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
67 if (ret == -EADDRINUSE) {
68 srx.transport.sin6.sin6_port = 0;
69 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
74 srx.srx_service = YFS_CM_SERVICE;
75 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
79 /* Ideally, we'd turn on service upgrade here, but we can't because
80 * OpenAFS is buggy and leaks the userStatus field from packet to
81 * packet and between FS packets and CB packets - so if we try to do an
82 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
83 * it sends back to us.
86 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
87 afs_rx_discard_new_call);
89 ret = kernel_listen(socket, INT_MAX);
94 afs_charge_preallocation(&net->charge_preallocation_work);
101 _leave(" = %d", ret);
106 * close the RxRPC socket AFS was using
108 void afs_close_socket(struct afs_net *net)
112 kernel_listen(net->socket, 0);
113 flush_workqueue(afs_async_calls);
115 if (net->spare_incoming_call) {
116 afs_put_call(net->spare_incoming_call);
117 net->spare_incoming_call = NULL;
120 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
121 wait_var_event(&net->nr_outstanding_calls,
122 !atomic_read(&net->nr_outstanding_calls));
123 _debug("no outstanding calls");
125 kernel_sock_shutdown(net->socket, SHUT_RDWR);
126 flush_workqueue(afs_async_calls);
127 sock_release(net->socket);
136 static struct afs_call *afs_alloc_call(struct afs_net *net,
137 const struct afs_call_type *type,
140 struct afs_call *call;
143 call = kzalloc(sizeof(*call), gfp);
149 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
150 refcount_set(&call->ref, 1);
151 INIT_WORK(&call->async_work, afs_process_async_call);
152 init_waitqueue_head(&call->waitq);
153 spin_lock_init(&call->state_lock);
154 call->iter = &call->def_iter;
156 o = atomic_inc_return(&net->nr_outstanding_calls);
157 trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o,
158 __builtin_return_address(0));
163 * Dispose of a reference on a call.
165 void afs_put_call(struct afs_call *call)
167 struct afs_net *net = call->net;
168 unsigned int debug_id = call->debug_id;
172 zero = __refcount_dec_and_test(&call->ref, &r);
173 o = atomic_read(&net->nr_outstanding_calls);
174 trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
175 __builtin_return_address(0));
178 ASSERT(!work_pending(&call->async_work));
179 ASSERT(call->type->name != NULL);
182 rxrpc_kernel_shutdown_call(net->socket, call->rxcall);
183 rxrpc_kernel_put_call(net->socket, call->rxcall);
186 if (call->type->destructor)
187 call->type->destructor(call);
189 afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
190 afs_put_addrlist(call->alist);
191 kfree(call->request);
193 trace_afs_call(call->debug_id, afs_call_trace_free, 0, o,
194 __builtin_return_address(0));
197 o = atomic_dec_return(&net->nr_outstanding_calls);
199 wake_up_var(&net->nr_outstanding_calls);
203 static struct afs_call *afs_get_call(struct afs_call *call,
204 enum afs_call_trace why)
208 __refcount_inc(&call->ref, &r);
210 trace_afs_call(call->debug_id, why, r + 1,
211 atomic_read(&call->net->nr_outstanding_calls),
212 __builtin_return_address(0));
217 * Queue the call for actual work.
219 static void afs_queue_call_work(struct afs_call *call)
221 if (call->type->work) {
222 INIT_WORK(&call->work, call->type->work);
224 afs_get_call(call, afs_call_trace_work);
225 if (!queue_work(afs_wq, &call->work))
231 * allocate a call with flat request and reply buffers
233 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
234 const struct afs_call_type *type,
235 size_t request_size, size_t reply_max)
237 struct afs_call *call;
239 call = afs_alloc_call(net, type, GFP_NOFS);
244 call->request_size = request_size;
245 call->request = kmalloc(request_size, GFP_NOFS);
251 call->reply_max = reply_max;
252 call->buffer = kmalloc(reply_max, GFP_NOFS);
257 afs_extract_to_buf(call, call->reply_max);
258 call->operation_ID = type->op;
259 init_waitqueue_head(&call->waitq);
269 * clean up a call with flat buffer
271 void afs_flat_call_destructor(struct afs_call *call)
275 kfree(call->request);
276 call->request = NULL;
282 * Advance the AFS call state when the RxRPC call ends the transmit phase.
284 static void afs_notify_end_request_tx(struct sock *sock,
285 struct rxrpc_call *rxcall,
286 unsigned long call_user_ID)
288 struct afs_call *call = (struct afs_call *)call_user_ID;
290 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
294 * Initiate a call and synchronously queue up the parameters for dispatch. Any
295 * error is stored into the call struct, which the caller must check for.
297 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
299 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
300 struct rxrpc_call *rxcall;
307 _enter(",{%pISp},", &srx->transport);
309 ASSERT(call->type != NULL);
310 ASSERT(call->type->name != NULL);
312 _debug("____MAKE %p{%s,%x} [%d]____",
313 call, call->type->name, key_serial(call->key),
314 atomic_read(&call->net->nr_outstanding_calls));
316 call->addr_ix = ac->index;
317 call->alist = afs_get_addrlist(ac->alist);
319 /* Work out the length we're going to transmit. This is awkward for
320 * calls such as FS.StoreData where there's an extra injection of data
321 * after the initial fixed part.
323 tx_total_len = call->request_size;
324 if (call->write_iter)
325 tx_total_len += iov_iter_count(call->write_iter);
327 /* If the call is going to be asynchronous, we need an extra ref for
328 * the call to hold itself so the caller need not hang on to its ref.
331 afs_get_call(call, afs_call_trace_get);
332 call->drop_ref = true;
336 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
340 afs_wake_up_async_call :
341 afs_wake_up_call_waiter),
343 (call->intr ? RXRPC_PREINTERRUPTIBLE :
344 RXRPC_UNINTERRUPTIBLE),
346 if (IS_ERR(rxcall)) {
347 ret = PTR_ERR(rxcall);
349 goto error_kill_call;
352 call->rxcall = rxcall;
354 if (call->max_lifespan)
355 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
357 call->issue_time = ktime_get_real();
359 /* send the request */
360 iov[0].iov_base = call->request;
361 iov[0].iov_len = call->request_size;
365 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, call->request_size);
366 msg.msg_control = NULL;
367 msg.msg_controllen = 0;
368 msg.msg_flags = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0);
370 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
371 &msg, call->request_size,
372 afs_notify_end_request_tx);
376 if (call->write_iter) {
377 msg.msg_iter = *call->write_iter;
378 msg.msg_flags &= ~MSG_MORE;
379 trace_afs_send_data(call, &msg);
381 ret = rxrpc_kernel_send_data(call->net->socket,
383 iov_iter_count(&msg.msg_iter),
384 afs_notify_end_request_tx);
385 *call->write_iter = msg.msg_iter;
387 trace_afs_sent_data(call, &msg, ret);
392 /* Note that at this point, we may have received the reply or an abort
393 * - and an asynchronous call may already have completed.
395 * afs_wait_for_call_to_complete(call, ac)
396 * must be called to synchronously clean up.
401 if (ret != -ECONNABORTED) {
402 rxrpc_kernel_abort_call(call->net->socket, rxcall,
404 afs_abort_send_data_error);
407 iov_iter_kvec(&msg.msg_iter, ITER_DEST, NULL, 0, 0);
408 rxrpc_kernel_recv_data(call->net->socket, rxcall,
409 &msg.msg_iter, &len, false,
410 &call->abort_code, &call->service_id);
411 ac->abort_code = call->abort_code;
412 ac->responded = true;
415 trace_afs_call_done(call);
417 if (call->type->done)
418 call->type->done(call);
420 /* We need to dispose of the extra ref we grabbed for an async call.
421 * The call, however, might be queued on afs_async_calls and we need to
422 * make sure we don't get any more notifications that might requeue it.
425 rxrpc_kernel_shutdown_call(call->net->socket, call->rxcall);
427 if (cancel_work_sync(&call->async_work))
433 call->state = AFS_CALL_COMPLETE;
434 _leave(" = %d", ret);
438 * Log remote abort codes that indicate that we have a protocol disagreement
441 static void afs_log_error(struct afs_call *call, s32 remote_abort)
447 switch (remote_abort) {
448 case RX_EOF: msg = "unexpected EOF"; break;
449 case RXGEN_CC_MARSHAL: msg = "client marshalling"; break;
450 case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling"; break;
451 case RXGEN_SS_MARSHAL: msg = "server marshalling"; break;
452 case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling"; break;
453 case RXGEN_DECODE: msg = "opcode decode"; break;
454 case RXGEN_SS_XDRFREE: msg = "server XDR cleanup"; break;
455 case RXGEN_CC_XDRFREE: msg = "client XDR cleanup"; break;
456 case -32: msg = "insufficient data"; break;
464 pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n",
465 msg, call->type->name,
466 &call->alist->addrs[call->addr_ix].transport);
471 * deliver messages to a call
473 static void afs_deliver_to_call(struct afs_call *call)
475 enum afs_call_state state;
477 u32 abort_code, remote_abort = 0;
480 _enter("%s", call->type->name);
482 while (state = READ_ONCE(call->state),
483 state == AFS_CALL_CL_AWAIT_REPLY ||
484 state == AFS_CALL_SV_AWAIT_OP_ID ||
485 state == AFS_CALL_SV_AWAIT_REQUEST ||
486 state == AFS_CALL_SV_AWAIT_ACK
488 if (state == AFS_CALL_SV_AWAIT_ACK) {
490 iov_iter_kvec(&call->def_iter, ITER_DEST, NULL, 0, 0);
491 ret = rxrpc_kernel_recv_data(call->net->socket,
492 call->rxcall, &call->def_iter,
493 &len, false, &remote_abort,
495 trace_afs_receive_data(call, &call->def_iter, false, ret);
497 if (ret == -EINPROGRESS || ret == -EAGAIN)
499 if (ret < 0 || ret == 1) {
507 ret = call->type->deliver(call);
508 state = READ_ONCE(call->state);
509 if (ret == 0 && call->unmarshalling_error)
513 afs_queue_call_work(call);
514 if (state == AFS_CALL_CL_PROC_REPLY) {
516 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
517 &call->op->server->flags);
520 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
526 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
527 afs_log_error(call, call->abort_code);
530 abort_code = RXGEN_OPCODE;
531 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
533 afs_abort_op_not_supported);
536 pr_err("kAFS: Call %u in bad state %u\n",
537 call->debug_id, state);
544 abort_code = RXGEN_CC_UNMARSHAL;
545 if (state != AFS_CALL_CL_AWAIT_REPLY)
546 abort_code = RXGEN_SS_UNMARSHAL;
547 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
549 afs_abort_unmarshal_error);
552 abort_code = RX_CALL_DEAD;
553 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
555 afs_abort_general_error);
561 if (call->type->done)
562 call->type->done(call);
570 afs_set_call_complete(call, ret, remote_abort);
571 state = AFS_CALL_COMPLETE;
576 * Wait synchronously for a call to complete and clean up the call struct.
578 long afs_wait_for_call_to_complete(struct afs_call *call,
579 struct afs_addr_cursor *ac)
582 bool rxrpc_complete = false;
584 DECLARE_WAITQUEUE(myself, current);
592 add_wait_queue(&call->waitq, &myself);
594 set_current_state(TASK_UNINTERRUPTIBLE);
596 /* deliver any messages that are in the queue */
597 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
598 call->need_attention) {
599 call->need_attention = false;
600 __set_current_state(TASK_RUNNING);
601 afs_deliver_to_call(call);
605 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
608 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
609 /* rxrpc terminated the call. */
610 rxrpc_complete = true;
617 remove_wait_queue(&call->waitq, &myself);
618 __set_current_state(TASK_RUNNING);
620 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
621 if (rxrpc_complete) {
622 afs_set_call_complete(call, call->error, call->abort_code);
624 /* Kill off the call if it's still live. */
625 _debug("call interrupted");
626 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
627 RX_USER_ABORT, -EINTR,
628 afs_abort_interrupted))
629 afs_set_call_complete(call, -EINTR, 0);
633 spin_lock_bh(&call->state_lock);
634 ac->abort_code = call->abort_code;
635 ac->error = call->error;
636 spin_unlock_bh(&call->state_lock);
646 ac->responded = true;
651 _debug("call complete");
653 _leave(" = %p", (void *)ret);
658 * wake up a waiting call
660 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
661 unsigned long call_user_ID)
663 struct afs_call *call = (struct afs_call *)call_user_ID;
665 call->need_attention = true;
666 wake_up(&call->waitq);
670 * wake up an asynchronous call
672 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
673 unsigned long call_user_ID)
675 struct afs_call *call = (struct afs_call *)call_user_ID;
678 trace_afs_notify_call(rxcall, call);
679 call->need_attention = true;
681 if (__refcount_inc_not_zero(&call->ref, &r)) {
682 trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
683 atomic_read(&call->net->nr_outstanding_calls),
684 __builtin_return_address(0));
686 if (!queue_work(afs_async_calls, &call->async_work))
692 * Perform I/O processing on an asynchronous call. The work item carries a ref
693 * to the call struct that we either need to release or to pass on.
695 static void afs_process_async_call(struct work_struct *work)
697 struct afs_call *call = container_of(work, struct afs_call, async_work);
701 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
702 call->need_attention = false;
703 afs_deliver_to_call(call);
710 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
712 struct afs_call *call = (struct afs_call *)user_call_ID;
714 call->rxcall = rxcall;
718 * Charge the incoming call preallocation.
720 void afs_charge_preallocation(struct work_struct *work)
722 struct afs_net *net =
723 container_of(work, struct afs_net, charge_preallocation_work);
724 struct afs_call *call = net->spare_incoming_call;
728 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
732 call->drop_ref = true;
734 call->state = AFS_CALL_SV_AWAIT_OP_ID;
735 init_waitqueue_head(&call->waitq);
736 afs_extract_to_tmp(call);
739 if (rxrpc_kernel_charge_accept(net->socket,
740 afs_wake_up_async_call,
748 net->spare_incoming_call = call;
752 * Discard a preallocated call when a socket is shut down.
754 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
755 unsigned long user_call_ID)
757 struct afs_call *call = (struct afs_call *)user_call_ID;
764 * Notification of an incoming call.
766 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
767 unsigned long user_call_ID)
769 struct afs_net *net = afs_sock2net(sk);
771 queue_work(afs_wq, &net->charge_preallocation_work);
775 * Grab the operation ID from an incoming cache manager call. The socket
776 * buffer is discarded on error or if we don't yet have sufficient data.
778 static int afs_deliver_cm_op_id(struct afs_call *call)
782 _enter("{%zu}", iov_iter_count(call->iter));
784 /* the operation ID forms the first four bytes of the request data */
785 ret = afs_extract_data(call, true);
789 call->operation_ID = ntohl(call->tmp);
790 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
792 /* ask the cache manager to route the call (it'll change the call type
794 if (!afs_cm_incoming_call(call))
797 trace_afs_cb_call(call);
799 /* pass responsibility for the remainer of this message off to the
800 * cache manager op */
801 return call->type->deliver(call);
805 * Advance the AFS call state when an RxRPC service call ends the transmit
808 static void afs_notify_end_reply_tx(struct sock *sock,
809 struct rxrpc_call *rxcall,
810 unsigned long call_user_ID)
812 struct afs_call *call = (struct afs_call *)call_user_ID;
814 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
818 * send an empty reply
820 void afs_send_empty_reply(struct afs_call *call)
822 struct afs_net *net = call->net;
827 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
831 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0);
832 msg.msg_control = NULL;
833 msg.msg_controllen = 0;
836 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
837 afs_notify_end_reply_tx)) {
839 _leave(" [replied]");
844 rxrpc_kernel_abort_call(net->socket, call->rxcall,
845 RXGEN_SS_MARSHAL, -ENOMEM,
855 * send a simple reply
857 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
859 struct afs_net *net = call->net;
866 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
868 iov[0].iov_base = (void *) buf;
869 iov[0].iov_len = len;
872 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, len);
873 msg.msg_control = NULL;
874 msg.msg_controllen = 0;
877 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
878 afs_notify_end_reply_tx);
881 _leave(" [replied]");
887 rxrpc_kernel_abort_call(net->socket, call->rxcall,
888 RXGEN_SS_MARSHAL, -ENOMEM,
895 * Extract a piece of data from the received data socket buffers.
897 int afs_extract_data(struct afs_call *call, bool want_more)
899 struct afs_net *net = call->net;
900 struct iov_iter *iter = call->iter;
901 enum afs_call_state state;
902 u32 remote_abort = 0;
905 _enter("{%s,%zu,%zu},%d",
906 call->type->name, call->iov_len, iov_iter_count(iter), want_more);
908 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
909 &call->iov_len, want_more, &remote_abort,
911 trace_afs_receive_data(call, call->iter, want_more, ret);
912 if (ret == 0 || ret == -EAGAIN)
915 state = READ_ONCE(call->state);
918 case AFS_CALL_CL_AWAIT_REPLY:
919 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
921 case AFS_CALL_SV_AWAIT_REQUEST:
922 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
924 case AFS_CALL_COMPLETE:
925 kdebug("prem complete %d", call->error);
926 return afs_io_error(call, afs_io_error_extract);
933 afs_set_call_complete(call, ret, remote_abort);
938 * Log protocol error production.
940 noinline int afs_protocol_error(struct afs_call *call,
941 enum afs_eproto_cause cause)
943 trace_afs_protocol_error(call, cause);
945 call->unmarshalling_error = true;