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"
17 struct workqueue_struct *afs_async_calls;
19 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
20 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
21 static void afs_process_async_call(struct work_struct *);
22 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
23 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
24 static int afs_deliver_cm_op_id(struct afs_call *);
26 /* asynchronous incoming call initial processing */
27 static const struct afs_call_type afs_RXCMxxxx = {
29 .deliver = afs_deliver_cm_op_id,
33 * open an RxRPC socket and bind it to be a server for callback notifications
34 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
36 int afs_open_socket(struct afs_net *net)
38 struct sockaddr_rxrpc srx;
39 struct socket *socket;
44 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
48 socket->sk->sk_allocation = GFP_NOFS;
50 /* bind the callback manager's address to make this a server socket */
51 memset(&srx, 0, sizeof(srx));
52 srx.srx_family = AF_RXRPC;
53 srx.srx_service = CM_SERVICE;
54 srx.transport_type = SOCK_DGRAM;
55 srx.transport_len = sizeof(srx.transport.sin6);
56 srx.transport.sin6.sin6_family = AF_INET6;
57 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
59 ret = rxrpc_sock_set_min_security_level(socket->sk,
60 RXRPC_SECURITY_ENCRYPT);
64 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
65 if (ret == -EADDRINUSE) {
66 srx.transport.sin6.sin6_port = 0;
67 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
72 srx.srx_service = YFS_CM_SERVICE;
73 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
77 /* Ideally, we'd turn on service upgrade here, but we can't because
78 * OpenAFS is buggy and leaks the userStatus field from packet to
79 * packet and between FS packets and CB packets - so if we try to do an
80 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
81 * it sends back to us.
84 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
85 afs_rx_discard_new_call);
87 ret = kernel_listen(socket, INT_MAX);
92 afs_charge_preallocation(&net->charge_preallocation_work);
104 * close the RxRPC socket AFS was using
106 void afs_close_socket(struct afs_net *net)
110 kernel_listen(net->socket, 0);
111 flush_workqueue(afs_async_calls);
113 if (net->spare_incoming_call) {
114 afs_put_call(net->spare_incoming_call);
115 net->spare_incoming_call = NULL;
118 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
119 wait_var_event(&net->nr_outstanding_calls,
120 !atomic_read(&net->nr_outstanding_calls));
121 _debug("no outstanding calls");
123 kernel_sock_shutdown(net->socket, SHUT_RDWR);
124 flush_workqueue(afs_async_calls);
125 sock_release(net->socket);
134 static struct afs_call *afs_alloc_call(struct afs_net *net,
135 const struct afs_call_type *type,
138 struct afs_call *call;
141 call = kzalloc(sizeof(*call), gfp);
147 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
148 refcount_set(&call->ref, 1);
149 INIT_WORK(&call->async_work, afs_process_async_call);
150 init_waitqueue_head(&call->waitq);
151 spin_lock_init(&call->state_lock);
152 call->iter = &call->def_iter;
154 o = atomic_inc_return(&net->nr_outstanding_calls);
155 trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o,
156 __builtin_return_address(0));
161 * Dispose of a reference on a call.
163 void afs_put_call(struct afs_call *call)
165 struct afs_net *net = call->net;
166 unsigned int debug_id = call->debug_id;
170 zero = __refcount_dec_and_test(&call->ref, &r);
171 o = atomic_read(&net->nr_outstanding_calls);
172 trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
173 __builtin_return_address(0));
176 ASSERT(!work_pending(&call->async_work));
177 ASSERT(call->type->name != NULL);
180 rxrpc_kernel_end_call(net->socket, call->rxcall);
183 if (call->type->destructor)
184 call->type->destructor(call);
186 afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
187 afs_put_addrlist(call->alist);
188 kfree(call->request);
190 trace_afs_call(call->debug_id, afs_call_trace_free, 0, o,
191 __builtin_return_address(0));
194 o = atomic_dec_return(&net->nr_outstanding_calls);
196 wake_up_var(&net->nr_outstanding_calls);
200 static struct afs_call *afs_get_call(struct afs_call *call,
201 enum afs_call_trace why)
205 __refcount_inc(&call->ref, &r);
207 trace_afs_call(call->debug_id, why, r + 1,
208 atomic_read(&call->net->nr_outstanding_calls),
209 __builtin_return_address(0));
214 * Queue the call for actual work.
216 static void afs_queue_call_work(struct afs_call *call)
218 if (call->type->work) {
219 INIT_WORK(&call->work, call->type->work);
221 afs_get_call(call, afs_call_trace_work);
222 if (!queue_work(afs_wq, &call->work))
228 * allocate a call with flat request and reply buffers
230 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
231 const struct afs_call_type *type,
232 size_t request_size, size_t reply_max)
234 struct afs_call *call;
236 call = afs_alloc_call(net, type, GFP_NOFS);
241 call->request_size = request_size;
242 call->request = kmalloc(request_size, GFP_NOFS);
248 call->reply_max = reply_max;
249 call->buffer = kmalloc(reply_max, GFP_NOFS);
254 afs_extract_to_buf(call, call->reply_max);
255 call->operation_ID = type->op;
256 init_waitqueue_head(&call->waitq);
266 * clean up a call with flat buffer
268 void afs_flat_call_destructor(struct afs_call *call)
272 kfree(call->request);
273 call->request = NULL;
279 * Advance the AFS call state when the RxRPC call ends the transmit phase.
281 static void afs_notify_end_request_tx(struct sock *sock,
282 struct rxrpc_call *rxcall,
283 unsigned long call_user_ID)
285 struct afs_call *call = (struct afs_call *)call_user_ID;
287 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
291 * Initiate a call and synchronously queue up the parameters for dispatch. Any
292 * error is stored into the call struct, which the caller must check for.
294 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
296 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
297 struct rxrpc_call *rxcall;
304 _enter(",{%pISp},", &srx->transport);
306 ASSERT(call->type != NULL);
307 ASSERT(call->type->name != NULL);
309 _debug("____MAKE %p{%s,%x} [%d]____",
310 call, call->type->name, key_serial(call->key),
311 atomic_read(&call->net->nr_outstanding_calls));
313 call->addr_ix = ac->index;
314 call->alist = afs_get_addrlist(ac->alist);
316 /* Work out the length we're going to transmit. This is awkward for
317 * calls such as FS.StoreData where there's an extra injection of data
318 * after the initial fixed part.
320 tx_total_len = call->request_size;
321 if (call->write_iter)
322 tx_total_len += iov_iter_count(call->write_iter);
324 /* If the call is going to be asynchronous, we need an extra ref for
325 * the call to hold itself so the caller need not hang on to its ref.
328 afs_get_call(call, afs_call_trace_get);
329 call->drop_ref = true;
333 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
337 afs_wake_up_async_call :
338 afs_wake_up_call_waiter),
340 (call->intr ? RXRPC_PREINTERRUPTIBLE :
341 RXRPC_UNINTERRUPTIBLE),
343 if (IS_ERR(rxcall)) {
344 ret = PTR_ERR(rxcall);
346 goto error_kill_call;
349 call->rxcall = rxcall;
351 if (call->max_lifespan)
352 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
354 call->issue_time = ktime_get_real();
356 /* send the request */
357 iov[0].iov_base = call->request;
358 iov[0].iov_len = call->request_size;
362 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, call->request_size);
363 msg.msg_control = NULL;
364 msg.msg_controllen = 0;
365 msg.msg_flags = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0);
367 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
368 &msg, call->request_size,
369 afs_notify_end_request_tx);
373 if (call->write_iter) {
374 msg.msg_iter = *call->write_iter;
375 msg.msg_flags &= ~MSG_MORE;
376 trace_afs_send_data(call, &msg);
378 ret = rxrpc_kernel_send_data(call->net->socket,
380 iov_iter_count(&msg.msg_iter),
381 afs_notify_end_request_tx);
382 *call->write_iter = msg.msg_iter;
384 trace_afs_sent_data(call, &msg, ret);
389 /* Note that at this point, we may have received the reply or an abort
390 * - and an asynchronous call may already have completed.
392 * afs_wait_for_call_to_complete(call, ac)
393 * must be called to synchronously clean up.
398 if (ret != -ECONNABORTED) {
399 rxrpc_kernel_abort_call(call->net->socket, rxcall,
400 RX_USER_ABORT, ret, "KSD");
403 iov_iter_kvec(&msg.msg_iter, ITER_DEST, NULL, 0, 0);
404 rxrpc_kernel_recv_data(call->net->socket, rxcall,
405 &msg.msg_iter, &len, false,
406 &call->abort_code, &call->service_id);
407 ac->abort_code = call->abort_code;
408 ac->responded = true;
411 trace_afs_call_done(call);
413 if (call->type->done)
414 call->type->done(call);
416 /* We need to dispose of the extra ref we grabbed for an async call.
417 * The call, however, might be queued on afs_async_calls and we need to
418 * make sure we don't get any more notifications that might requeue it.
421 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
425 if (cancel_work_sync(&call->async_work))
431 call->state = AFS_CALL_COMPLETE;
432 _leave(" = %d", ret);
436 * Log remote abort codes that indicate that we have a protocol disagreement
439 static void afs_log_error(struct afs_call *call, s32 remote_abort)
445 switch (remote_abort) {
446 case RX_EOF: msg = "unexpected EOF"; break;
447 case RXGEN_CC_MARSHAL: msg = "client marshalling"; break;
448 case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling"; break;
449 case RXGEN_SS_MARSHAL: msg = "server marshalling"; break;
450 case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling"; break;
451 case RXGEN_DECODE: msg = "opcode decode"; break;
452 case RXGEN_SS_XDRFREE: msg = "server XDR cleanup"; break;
453 case RXGEN_CC_XDRFREE: msg = "client XDR cleanup"; break;
454 case -32: msg = "insufficient data"; break;
462 pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n",
463 msg, call->type->name,
464 &call->alist->addrs[call->addr_ix].transport);
469 * deliver messages to a call
471 static void afs_deliver_to_call(struct afs_call *call)
473 enum afs_call_state state;
475 u32 abort_code, remote_abort = 0;
478 _enter("%s", call->type->name);
480 while (state = READ_ONCE(call->state),
481 state == AFS_CALL_CL_AWAIT_REPLY ||
482 state == AFS_CALL_SV_AWAIT_OP_ID ||
483 state == AFS_CALL_SV_AWAIT_REQUEST ||
484 state == AFS_CALL_SV_AWAIT_ACK
486 if (state == AFS_CALL_SV_AWAIT_ACK) {
488 iov_iter_kvec(&call->def_iter, ITER_DEST, NULL, 0, 0);
489 ret = rxrpc_kernel_recv_data(call->net->socket,
490 call->rxcall, &call->def_iter,
491 &len, false, &remote_abort,
493 trace_afs_receive_data(call, &call->def_iter, false, ret);
495 if (ret == -EINPROGRESS || ret == -EAGAIN)
497 if (ret < 0 || ret == 1) {
505 ret = call->type->deliver(call);
506 state = READ_ONCE(call->state);
507 if (ret == 0 && call->unmarshalling_error)
511 afs_queue_call_work(call);
512 if (state == AFS_CALL_CL_PROC_REPLY) {
514 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
515 &call->op->server->flags);
518 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
524 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
525 afs_log_error(call, call->abort_code);
528 abort_code = RXGEN_OPCODE;
529 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
530 abort_code, ret, "KIV");
533 pr_err("kAFS: Call %u in bad state %u\n",
534 call->debug_id, state);
541 abort_code = RXGEN_CC_UNMARSHAL;
542 if (state != AFS_CALL_CL_AWAIT_REPLY)
543 abort_code = RXGEN_SS_UNMARSHAL;
544 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
545 abort_code, ret, "KUM");
548 abort_code = RX_CALL_DEAD;
549 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
550 abort_code, ret, "KER");
556 if (call->type->done)
557 call->type->done(call);
565 afs_set_call_complete(call, ret, remote_abort);
566 state = AFS_CALL_COMPLETE;
571 * Wait synchronously for a call to complete and clean up the call struct.
573 long afs_wait_for_call_to_complete(struct afs_call *call,
574 struct afs_addr_cursor *ac)
577 bool rxrpc_complete = false;
579 DECLARE_WAITQUEUE(myself, current);
587 add_wait_queue(&call->waitq, &myself);
589 set_current_state(TASK_UNINTERRUPTIBLE);
591 /* deliver any messages that are in the queue */
592 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
593 call->need_attention) {
594 call->need_attention = false;
595 __set_current_state(TASK_RUNNING);
596 afs_deliver_to_call(call);
600 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
603 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
604 /* rxrpc terminated the call. */
605 rxrpc_complete = true;
612 remove_wait_queue(&call->waitq, &myself);
613 __set_current_state(TASK_RUNNING);
615 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
616 if (rxrpc_complete) {
617 afs_set_call_complete(call, call->error, call->abort_code);
619 /* Kill off the call if it's still live. */
620 _debug("call interrupted");
621 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
622 RX_USER_ABORT, -EINTR, "KWI"))
623 afs_set_call_complete(call, -EINTR, 0);
627 spin_lock_bh(&call->state_lock);
628 ac->abort_code = call->abort_code;
629 ac->error = call->error;
630 spin_unlock_bh(&call->state_lock);
640 ac->responded = true;
645 _debug("call complete");
647 _leave(" = %p", (void *)ret);
652 * wake up a waiting call
654 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
655 unsigned long call_user_ID)
657 struct afs_call *call = (struct afs_call *)call_user_ID;
659 call->need_attention = true;
660 wake_up(&call->waitq);
664 * wake up an asynchronous call
666 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
667 unsigned long call_user_ID)
669 struct afs_call *call = (struct afs_call *)call_user_ID;
672 trace_afs_notify_call(rxcall, call);
673 call->need_attention = true;
675 if (__refcount_inc_not_zero(&call->ref, &r)) {
676 trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
677 atomic_read(&call->net->nr_outstanding_calls),
678 __builtin_return_address(0));
680 if (!queue_work(afs_async_calls, &call->async_work))
686 * Perform I/O processing on an asynchronous call. The work item carries a ref
687 * to the call struct that we either need to release or to pass on.
689 static void afs_process_async_call(struct work_struct *work)
691 struct afs_call *call = container_of(work, struct afs_call, async_work);
695 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
696 call->need_attention = false;
697 afs_deliver_to_call(call);
704 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
706 struct afs_call *call = (struct afs_call *)user_call_ID;
708 call->rxcall = rxcall;
712 * Charge the incoming call preallocation.
714 void afs_charge_preallocation(struct work_struct *work)
716 struct afs_net *net =
717 container_of(work, struct afs_net, charge_preallocation_work);
718 struct afs_call *call = net->spare_incoming_call;
722 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
726 call->drop_ref = true;
728 call->state = AFS_CALL_SV_AWAIT_OP_ID;
729 init_waitqueue_head(&call->waitq);
730 afs_extract_to_tmp(call);
733 if (rxrpc_kernel_charge_accept(net->socket,
734 afs_wake_up_async_call,
742 net->spare_incoming_call = call;
746 * Discard a preallocated call when a socket is shut down.
748 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
749 unsigned long user_call_ID)
751 struct afs_call *call = (struct afs_call *)user_call_ID;
758 * Notification of an incoming call.
760 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
761 unsigned long user_call_ID)
763 struct afs_net *net = afs_sock2net(sk);
765 queue_work(afs_wq, &net->charge_preallocation_work);
769 * Grab the operation ID from an incoming cache manager call. The socket
770 * buffer is discarded on error or if we don't yet have sufficient data.
772 static int afs_deliver_cm_op_id(struct afs_call *call)
776 _enter("{%zu}", iov_iter_count(call->iter));
778 /* the operation ID forms the first four bytes of the request data */
779 ret = afs_extract_data(call, true);
783 call->operation_ID = ntohl(call->tmp);
784 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
786 /* ask the cache manager to route the call (it'll change the call type
788 if (!afs_cm_incoming_call(call))
791 trace_afs_cb_call(call);
793 /* pass responsibility for the remainer of this message off to the
794 * cache manager op */
795 return call->type->deliver(call);
799 * Advance the AFS call state when an RxRPC service call ends the transmit
802 static void afs_notify_end_reply_tx(struct sock *sock,
803 struct rxrpc_call *rxcall,
804 unsigned long call_user_ID)
806 struct afs_call *call = (struct afs_call *)call_user_ID;
808 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
812 * send an empty reply
814 void afs_send_empty_reply(struct afs_call *call)
816 struct afs_net *net = call->net;
821 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
825 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0);
826 msg.msg_control = NULL;
827 msg.msg_controllen = 0;
830 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
831 afs_notify_end_reply_tx)) {
833 _leave(" [replied]");
838 rxrpc_kernel_abort_call(net->socket, call->rxcall,
839 RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
848 * send a simple reply
850 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
852 struct afs_net *net = call->net;
859 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
861 iov[0].iov_base = (void *) buf;
862 iov[0].iov_len = len;
865 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, len);
866 msg.msg_control = NULL;
867 msg.msg_controllen = 0;
870 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
871 afs_notify_end_reply_tx);
874 _leave(" [replied]");
880 rxrpc_kernel_abort_call(net->socket, call->rxcall,
881 RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
887 * Extract a piece of data from the received data socket buffers.
889 int afs_extract_data(struct afs_call *call, bool want_more)
891 struct afs_net *net = call->net;
892 struct iov_iter *iter = call->iter;
893 enum afs_call_state state;
894 u32 remote_abort = 0;
897 _enter("{%s,%zu,%zu},%d",
898 call->type->name, call->iov_len, iov_iter_count(iter), want_more);
900 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
901 &call->iov_len, want_more, &remote_abort,
903 if (ret == 0 || ret == -EAGAIN)
906 state = READ_ONCE(call->state);
909 case AFS_CALL_CL_AWAIT_REPLY:
910 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
912 case AFS_CALL_SV_AWAIT_REQUEST:
913 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
915 case AFS_CALL_COMPLETE:
916 kdebug("prem complete %d", call->error);
917 return afs_io_error(call, afs_io_error_extract);
924 afs_set_call_complete(call, ret, remote_abort);
929 * Log protocol error production.
931 noinline int afs_protocol_error(struct afs_call *call,
932 enum afs_eproto_cause cause)
934 trace_afs_protocol_error(call, cause);
936 call->unmarshalling_error = true;