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;
40 unsigned int min_level;
45 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
49 socket->sk->sk_allocation = GFP_NOFS;
51 /* bind the callback manager's address to make this a server socket */
52 memset(&srx, 0, sizeof(srx));
53 srx.srx_family = AF_RXRPC;
54 srx.srx_service = CM_SERVICE;
55 srx.transport_type = SOCK_DGRAM;
56 srx.transport_len = sizeof(srx.transport.sin6);
57 srx.transport.sin6.sin6_family = AF_INET6;
58 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
60 min_level = RXRPC_SECURITY_ENCRYPT;
61 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
62 (void *)&min_level, sizeof(min_level));
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 atomic_set(&call->usage, 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->iter;
156 o = atomic_inc_return(&net->nr_outstanding_calls);
157 trace_afs_call(call, 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 int n = atomic_dec_return(&call->usage);
169 int o = atomic_read(&net->nr_outstanding_calls);
171 trace_afs_call(call, afs_call_trace_put, n, o,
172 __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_put_server(call->net, call->server, afs_server_trace_put_call);
187 afs_put_cb_interest(call->net, call->cbi);
188 afs_put_addrlist(call->alist);
189 kfree(call->request);
191 trace_afs_call(call, afs_call_trace_free, 0, o,
192 __builtin_return_address(0));
195 o = atomic_dec_return(&net->nr_outstanding_calls);
197 wake_up_var(&net->nr_outstanding_calls);
201 static struct afs_call *afs_get_call(struct afs_call *call,
202 enum afs_call_trace why)
204 int u = atomic_inc_return(&call->usage);
206 trace_afs_call(call, why, u,
207 atomic_read(&call->net->nr_outstanding_calls),
208 __builtin_return_address(0));
213 * Queue the call for actual work.
215 static void afs_queue_call_work(struct afs_call *call)
217 if (call->type->work) {
218 INIT_WORK(&call->work, call->type->work);
220 afs_get_call(call, afs_call_trace_work);
221 if (!queue_work(afs_wq, &call->work))
227 * allocate a call with flat request and reply buffers
229 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
230 const struct afs_call_type *type,
231 size_t request_size, size_t reply_max)
233 struct afs_call *call;
235 call = afs_alloc_call(net, type, GFP_NOFS);
240 call->request_size = request_size;
241 call->request = kmalloc(request_size, GFP_NOFS);
247 call->reply_max = reply_max;
248 call->buffer = kmalloc(reply_max, GFP_NOFS);
253 afs_extract_to_buf(call, call->reply_max);
254 call->operation_ID = type->op;
255 init_waitqueue_head(&call->waitq);
265 * clean up a call with flat buffer
267 void afs_flat_call_destructor(struct afs_call *call)
271 kfree(call->request);
272 call->request = NULL;
277 #define AFS_BVEC_MAX 8
280 * Load the given bvec with the next few pages.
282 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
283 struct bio_vec *bv, pgoff_t first, pgoff_t last,
286 struct page *pages[AFS_BVEC_MAX];
287 unsigned int nr, n, i, to, bytes = 0;
289 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
290 n = find_get_pages_contig(call->mapping, first, nr, pages);
291 ASSERTCMP(n, ==, nr);
293 msg->msg_flags |= MSG_MORE;
294 for (i = 0; i < nr; i++) {
296 if (first + i >= last) {
298 msg->msg_flags &= ~MSG_MORE;
300 bv[i].bv_page = pages[i];
301 bv[i].bv_len = to - offset;
302 bv[i].bv_offset = offset;
303 bytes += to - offset;
307 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
311 * Advance the AFS call state when the RxRPC call ends the transmit phase.
313 static void afs_notify_end_request_tx(struct sock *sock,
314 struct rxrpc_call *rxcall,
315 unsigned long call_user_ID)
317 struct afs_call *call = (struct afs_call *)call_user_ID;
319 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
323 * attach the data from a bunch of pages on an inode to a call
325 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
327 struct bio_vec bv[AFS_BVEC_MAX];
328 unsigned int bytes, nr, loop, offset;
329 pgoff_t first = call->first, last = call->last;
332 offset = call->first_offset;
333 call->first_offset = 0;
336 afs_load_bvec(call, msg, bv, first, last, offset);
337 trace_afs_send_pages(call, msg, first, last, offset);
340 bytes = msg->msg_iter.count;
341 nr = msg->msg_iter.nr_segs;
343 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
344 bytes, afs_notify_end_request_tx);
345 for (loop = 0; loop < nr; loop++)
346 put_page(bv[loop].bv_page);
351 } while (first <= last);
353 trace_afs_sent_pages(call, call->first, last, first, ret);
358 * Initiate a call and synchronously queue up the parameters for dispatch. Any
359 * error is stored into the call struct, which the caller must check for.
361 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
363 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
364 struct rxrpc_call *rxcall;
370 _enter(",{%pISp},", &srx->transport);
372 ASSERT(call->type != NULL);
373 ASSERT(call->type->name != NULL);
375 _debug("____MAKE %p{%s,%x} [%d]____",
376 call, call->type->name, key_serial(call->key),
377 atomic_read(&call->net->nr_outstanding_calls));
379 call->addr_ix = ac->index;
380 call->alist = afs_get_addrlist(ac->alist);
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.
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;
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...
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;
401 /* If the call is going to be asynchronous, we need an extra ref for
402 * the call to hold itself so the caller need not hang on to its ref.
405 afs_get_call(call, afs_call_trace_get);
406 call->drop_ref = true;
410 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
414 afs_wake_up_async_call :
415 afs_wake_up_call_waiter),
417 (call->intr ? RXRPC_PREINTERRUPTIBLE :
418 RXRPC_UNINTERRUPTIBLE),
420 if (IS_ERR(rxcall)) {
421 ret = PTR_ERR(rxcall);
423 goto error_kill_call;
426 call->rxcall = rxcall;
428 if (call->max_lifespan)
429 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
431 call->issue_time = ktime_get_real();
433 /* send the request */
434 iov[0].iov_base = call->request;
435 iov[0].iov_len = call->request_size;
439 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
440 msg.msg_control = NULL;
441 msg.msg_controllen = 0;
442 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
444 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
445 &msg, call->request_size,
446 afs_notify_end_request_tx);
450 if (call->send_pages) {
451 ret = afs_send_pages(call, &msg);
456 /* Note that at this point, we may have received the reply or an abort
457 * - and an asynchronous call may already have completed.
459 * afs_wait_for_call_to_complete(call, ac)
460 * must be called to synchronously clean up.
465 if (ret != -ECONNABORTED) {
466 rxrpc_kernel_abort_call(call->net->socket, rxcall,
467 RX_USER_ABORT, ret, "KSD");
469 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
470 rxrpc_kernel_recv_data(call->net->socket, rxcall,
471 &msg.msg_iter, false,
472 &call->abort_code, &call->service_id);
473 ac->abort_code = call->abort_code;
474 ac->responded = true;
477 trace_afs_call_done(call);
479 if (call->type->done)
480 call->type->done(call);
482 /* We need to dispose of the extra ref we grabbed for an async call.
483 * The call, however, might be queued on afs_async_calls and we need to
484 * make sure we don't get any more notifications that might requeue it.
487 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
491 if (cancel_work_sync(&call->async_work))
497 call->state = AFS_CALL_COMPLETE;
498 _leave(" = %d", ret);
502 * deliver messages to a call
504 static void afs_deliver_to_call(struct afs_call *call)
506 enum afs_call_state state;
507 u32 abort_code, remote_abort = 0;
510 _enter("%s", call->type->name);
512 while (state = READ_ONCE(call->state),
513 state == AFS_CALL_CL_AWAIT_REPLY ||
514 state == AFS_CALL_SV_AWAIT_OP_ID ||
515 state == AFS_CALL_SV_AWAIT_REQUEST ||
516 state == AFS_CALL_SV_AWAIT_ACK
518 if (state == AFS_CALL_SV_AWAIT_ACK) {
519 iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
520 ret = rxrpc_kernel_recv_data(call->net->socket,
521 call->rxcall, &call->iter,
522 false, &remote_abort,
524 trace_afs_receive_data(call, &call->iter, false, ret);
526 if (ret == -EINPROGRESS || ret == -EAGAIN)
528 if (ret < 0 || ret == 1) {
536 ret = call->type->deliver(call);
537 state = READ_ONCE(call->state);
538 if (ret == 0 && call->unmarshalling_error)
542 afs_queue_call_work(call);
543 if (state == AFS_CALL_CL_PROC_REPLY) {
545 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
546 &call->cbi->server->flags);
549 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
555 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
558 abort_code = RXGEN_OPCODE;
559 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
560 abort_code, ret, "KIV");
563 pr_err("kAFS: Call %u in bad state %u\n",
564 call->debug_id, state);
569 abort_code = RXGEN_CC_UNMARSHAL;
570 if (state != AFS_CALL_CL_AWAIT_REPLY)
571 abort_code = RXGEN_SS_UNMARSHAL;
572 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
573 abort_code, ret, "KUM");
576 abort_code = RX_USER_ABORT;
577 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
578 abort_code, ret, "KER");
584 if (call->type->done)
585 call->type->done(call);
593 afs_set_call_complete(call, ret, remote_abort);
594 state = AFS_CALL_COMPLETE;
599 * Wait synchronously for a call to complete and clean up the call struct.
601 long afs_wait_for_call_to_complete(struct afs_call *call,
602 struct afs_addr_cursor *ac)
605 bool rxrpc_complete = false;
607 DECLARE_WAITQUEUE(myself, current);
615 add_wait_queue(&call->waitq, &myself);
617 set_current_state(TASK_UNINTERRUPTIBLE);
619 /* deliver any messages that are in the queue */
620 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
621 call->need_attention) {
622 call->need_attention = false;
623 __set_current_state(TASK_RUNNING);
624 afs_deliver_to_call(call);
628 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
631 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
632 /* rxrpc terminated the call. */
633 rxrpc_complete = true;
640 remove_wait_queue(&call->waitq, &myself);
641 __set_current_state(TASK_RUNNING);
643 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
644 if (rxrpc_complete) {
645 afs_set_call_complete(call, call->error, call->abort_code);
647 /* Kill off the call if it's still live. */
648 _debug("call interrupted");
649 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
650 RX_USER_ABORT, -EINTR, "KWI"))
651 afs_set_call_complete(call, -EINTR, 0);
655 spin_lock_bh(&call->state_lock);
656 ac->abort_code = call->abort_code;
657 ac->error = call->error;
658 spin_unlock_bh(&call->state_lock);
668 ac->responded = true;
673 _debug("call complete");
675 _leave(" = %p", (void *)ret);
680 * wake up a waiting call
682 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
683 unsigned long call_user_ID)
685 struct afs_call *call = (struct afs_call *)call_user_ID;
687 call->need_attention = true;
688 wake_up(&call->waitq);
692 * wake up an asynchronous call
694 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
695 unsigned long call_user_ID)
697 struct afs_call *call = (struct afs_call *)call_user_ID;
700 trace_afs_notify_call(rxcall, call);
701 call->need_attention = true;
703 u = atomic_fetch_add_unless(&call->usage, 1, 0);
705 trace_afs_call(call, afs_call_trace_wake, u + 1,
706 atomic_read(&call->net->nr_outstanding_calls),
707 __builtin_return_address(0));
709 if (!queue_work(afs_async_calls, &call->async_work))
715 * Perform I/O processing on an asynchronous call. The work item carries a ref
716 * to the call struct that we either need to release or to pass on.
718 static void afs_process_async_call(struct work_struct *work)
720 struct afs_call *call = container_of(work, struct afs_call, async_work);
724 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
725 call->need_attention = false;
726 afs_deliver_to_call(call);
733 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
735 struct afs_call *call = (struct afs_call *)user_call_ID;
737 call->rxcall = rxcall;
741 * Charge the incoming call preallocation.
743 void afs_charge_preallocation(struct work_struct *work)
745 struct afs_net *net =
746 container_of(work, struct afs_net, charge_preallocation_work);
747 struct afs_call *call = net->spare_incoming_call;
751 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
755 call->drop_ref = true;
757 call->state = AFS_CALL_SV_AWAIT_OP_ID;
758 init_waitqueue_head(&call->waitq);
759 afs_extract_to_tmp(call);
762 if (rxrpc_kernel_charge_accept(net->socket,
763 afs_wake_up_async_call,
771 net->spare_incoming_call = call;
775 * Discard a preallocated call when a socket is shut down.
777 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
778 unsigned long user_call_ID)
780 struct afs_call *call = (struct afs_call *)user_call_ID;
787 * Notification of an incoming call.
789 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
790 unsigned long user_call_ID)
792 struct afs_net *net = afs_sock2net(sk);
794 queue_work(afs_wq, &net->charge_preallocation_work);
798 * Grab the operation ID from an incoming cache manager call. The socket
799 * buffer is discarded on error or if we don't yet have sufficient data.
801 static int afs_deliver_cm_op_id(struct afs_call *call)
805 _enter("{%zu}", iov_iter_count(call->_iter));
807 /* the operation ID forms the first four bytes of the request data */
808 ret = afs_extract_data(call, true);
812 call->operation_ID = ntohl(call->tmp);
813 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
815 /* ask the cache manager to route the call (it'll change the call type
817 if (!afs_cm_incoming_call(call))
820 trace_afs_cb_call(call);
822 /* pass responsibility for the remainer of this message off to the
823 * cache manager op */
824 return call->type->deliver(call);
828 * Advance the AFS call state when an RxRPC service call ends the transmit
831 static void afs_notify_end_reply_tx(struct sock *sock,
832 struct rxrpc_call *rxcall,
833 unsigned long call_user_ID)
835 struct afs_call *call = (struct afs_call *)call_user_ID;
837 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
841 * send an empty reply
843 void afs_send_empty_reply(struct afs_call *call)
845 struct afs_net *net = call->net;
850 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
854 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
855 msg.msg_control = NULL;
856 msg.msg_controllen = 0;
859 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
860 afs_notify_end_reply_tx)) {
862 _leave(" [replied]");
867 rxrpc_kernel_abort_call(net->socket, call->rxcall,
868 RX_USER_ABORT, -ENOMEM, "KOO");
877 * send a simple reply
879 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
881 struct afs_net *net = call->net;
888 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
890 iov[0].iov_base = (void *) buf;
891 iov[0].iov_len = len;
894 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
895 msg.msg_control = NULL;
896 msg.msg_controllen = 0;
899 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
900 afs_notify_end_reply_tx);
903 _leave(" [replied]");
909 rxrpc_kernel_abort_call(net->socket, call->rxcall,
910 RX_USER_ABORT, -ENOMEM, "KOO");
916 * Extract a piece of data from the received data socket buffers.
918 int afs_extract_data(struct afs_call *call, bool want_more)
920 struct afs_net *net = call->net;
921 struct iov_iter *iter = call->_iter;
922 enum afs_call_state state;
923 u32 remote_abort = 0;
926 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
928 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
929 want_more, &remote_abort,
931 if (ret == 0 || ret == -EAGAIN)
934 state = READ_ONCE(call->state);
937 case AFS_CALL_CL_AWAIT_REPLY:
938 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
940 case AFS_CALL_SV_AWAIT_REQUEST:
941 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
943 case AFS_CALL_COMPLETE:
944 kdebug("prem complete %d", call->error);
945 return afs_io_error(call, afs_io_error_extract);
952 afs_set_call_complete(call, ret, remote_abort);
957 * Log protocol error production.
959 noinline int afs_protocol_error(struct afs_call *call, int error,
960 enum afs_eproto_cause cause)
962 trace_afs_protocol_error(call, error, cause);
964 call->unmarshalling_error = true;