1 /* Maintain an RxRPC server socket to do AFS communications through
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
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.
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
16 #include <net/af_rxrpc.h>
19 #include "protocol_yfs.h"
21 struct workqueue_struct *afs_async_calls;
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 *);
31 /* asynchronous incoming call initial processing */
32 static const struct afs_call_type afs_RXCMxxxx = {
34 .deliver = afs_deliver_cm_op_id,
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
41 int afs_open_socket(struct afs_net *net)
43 struct sockaddr_rxrpc srx;
44 struct socket *socket;
45 unsigned int min_level;
50 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
54 socket->sk->sk_allocation = GFP_NOFS;
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);
65 min_level = RXRPC_SECURITY_ENCRYPT;
66 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
67 (void *)&min_level, sizeof(min_level));
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));
79 srx.srx_service = YFS_CM_SERVICE;
80 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
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.
91 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
92 afs_rx_discard_new_call);
94 ret = kernel_listen(socket, INT_MAX);
99 afs_charge_preallocation(&net->charge_preallocation_work);
104 sock_release(socket);
106 _leave(" = %d", ret);
111 * close the RxRPC socket AFS was using
113 void afs_close_socket(struct afs_net *net)
117 kernel_listen(net->socket, 0);
118 flush_workqueue(afs_async_calls);
120 if (net->spare_incoming_call) {
121 afs_put_call(net->spare_incoming_call);
122 net->spare_incoming_call = NULL;
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");
130 kernel_sock_shutdown(net->socket, SHUT_RDWR);
131 flush_workqueue(afs_async_calls);
132 sock_release(net->socket);
141 static struct afs_call *afs_alloc_call(struct afs_net *net,
142 const struct afs_call_type *type,
145 struct afs_call *call;
148 call = kzalloc(sizeof(*call), gfp);
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;
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));
168 * Dispose of a reference on a call.
170 void afs_put_call(struct afs_call *call)
172 struct afs_net *net = call->net;
173 int n = atomic_dec_return(&call->usage);
174 int o = atomic_read(&net->nr_outstanding_calls);
176 trace_afs_call(call, afs_call_trace_put, n + 1, o,
177 __builtin_return_address(0));
181 ASSERT(!work_pending(&call->async_work));
182 ASSERT(call->type->name != NULL);
185 rxrpc_kernel_end_call(net->socket, call->rxcall);
188 if (call->type->destructor)
189 call->type->destructor(call);
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);
196 trace_afs_call(call, afs_call_trace_free, 0, o,
197 __builtin_return_address(0));
200 o = atomic_dec_return(&net->nr_outstanding_calls);
202 wake_up_var(&net->nr_outstanding_calls);
207 * Queue the call for actual work.
209 static void afs_queue_call_work(struct afs_call *call)
211 if (call->type->work) {
212 int u = atomic_inc_return(&call->usage);
214 trace_afs_call(call, afs_call_trace_work, u,
215 atomic_read(&call->net->nr_outstanding_calls),
216 __builtin_return_address(0));
218 INIT_WORK(&call->work, call->type->work);
220 if (!queue_work(afs_wq, &call->work))
226 * allocate a call with flat request and reply buffers
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)
232 struct afs_call *call;
234 call = afs_alloc_call(net, type, GFP_NOFS);
239 call->request_size = request_size;
240 call->request = kmalloc(request_size, GFP_NOFS);
246 call->reply_max = reply_max;
247 call->buffer = kmalloc(reply_max, GFP_NOFS);
252 afs_extract_to_buf(call, call->reply_max);
253 call->operation_ID = type->op;
254 init_waitqueue_head(&call->waitq);
264 * clean up a call with flat buffer
266 void afs_flat_call_destructor(struct afs_call *call)
270 kfree(call->request);
271 call->request = NULL;
276 #define AFS_BVEC_MAX 8
279 * Load the given bvec with the next few pages.
281 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
282 struct bio_vec *bv, pgoff_t first, pgoff_t last,
285 struct page *pages[AFS_BVEC_MAX];
286 unsigned int nr, n, i, to, bytes = 0;
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);
292 msg->msg_flags |= MSG_MORE;
293 for (i = 0; i < nr; i++) {
295 if (first + i >= last) {
297 msg->msg_flags &= ~MSG_MORE;
299 bv[i].bv_page = pages[i];
300 bv[i].bv_len = to - offset;
301 bv[i].bv_offset = offset;
302 bytes += to - offset;
306 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
310 * Advance the AFS call state when the RxRPC call ends the transmit phase.
312 static void afs_notify_end_request_tx(struct sock *sock,
313 struct rxrpc_call *rxcall,
314 unsigned long call_user_ID)
316 struct afs_call *call = (struct afs_call *)call_user_ID;
318 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
322 * attach the data from a bunch of pages on an inode to a call
324 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
326 struct bio_vec bv[AFS_BVEC_MAX];
327 unsigned int bytes, nr, loop, offset;
328 pgoff_t first = call->first, last = call->last;
331 offset = call->first_offset;
332 call->first_offset = 0;
335 afs_load_bvec(call, msg, bv, first, last, offset);
336 trace_afs_send_pages(call, msg, first, last, offset);
339 bytes = msg->msg_iter.count;
340 nr = msg->msg_iter.nr_segs;
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);
350 } while (first <= last);
352 trace_afs_sent_pages(call, call->first, last, first, ret);
359 long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
360 gfp_t gfp, bool async)
362 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
363 struct rxrpc_call *rxcall;
369 _enter(",{%pISp},", &srx->transport);
371 ASSERT(call->type != NULL);
372 ASSERT(call->type->name != NULL);
374 _debug("____MAKE %p{%s,%x} [%d]____",
375 call, call->type->name, key_serial(call->key),
376 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;
402 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
406 afs_wake_up_async_call :
407 afs_wake_up_call_waiter),
410 if (IS_ERR(rxcall)) {
411 ret = PTR_ERR(rxcall);
413 goto error_kill_call;
416 call->rxcall = rxcall;
418 /* send the request */
419 iov[0].iov_base = call->request;
420 iov[0].iov_len = call->request_size;
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);
429 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
430 &msg, call->request_size,
431 afs_notify_end_request_tx);
435 if (call->send_pages) {
436 ret = afs_send_pages(call, &msg);
441 /* at this point, an async call may no longer exist as it may have
442 * already completed */
446 return afs_wait_for_call_to_complete(call, ac);
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");
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;
462 trace_afs_call_done(call);
464 if (call->type->done)
465 call->type->done(call);
468 _leave(" = %d", ret);
473 * deliver messages to a call
475 static void afs_deliver_to_call(struct afs_call *call)
477 enum afs_call_state state;
478 u32 abort_code, remote_abort = 0;
481 _enter("%s", call->type->name);
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
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,
495 trace_afs_receive_data(call, &call->iter, false, ret);
497 if (ret == -EINPROGRESS || ret == -EAGAIN)
499 if (ret < 0 || ret == 1) {
507 if (call->want_reply_time &&
508 rxrpc_kernel_get_reply_time(call->net->socket,
511 call->want_reply_time = false;
513 ret = call->type->deliver(call);
514 state = READ_ONCE(call->state);
517 afs_queue_call_work(call);
518 if (state == AFS_CALL_CL_PROC_REPLY) {
520 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
521 &call->cbi->server->flags);
524 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
530 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
533 abort_code = RXGEN_OPCODE;
534 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
535 abort_code, ret, "KIV");
538 pr_err("kAFS: Call %u in bad state %u\n",
539 call->debug_id, state);
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");
555 if (call->type->done)
556 call->type->done(call);
557 if (state == AFS_CALL_COMPLETE && call->incoming)
566 afs_set_call_complete(call, ret, remote_abort);
567 state = AFS_CALL_COMPLETE;
572 * wait synchronously for a call to complete
574 static long afs_wait_for_call_to_complete(struct afs_call *call,
575 struct afs_addr_cursor *ac)
577 signed long rtt2, timeout;
582 DECLARE_WAITQUEUE(myself, current);
586 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
587 rtt2 = nsecs_to_jiffies64(rtt) * 2;
592 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
594 add_wait_queue(&call->waitq, &myself);
596 set_current_state(TASK_UNINTERRUPTIBLE);
598 /* deliver any messages that are in the queue */
599 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
600 call->need_attention) {
601 call->need_attention = false;
602 __set_current_state(TASK_RUNNING);
603 afs_deliver_to_call(call);
607 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
610 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
612 life == last_life && signal_pending(current))
615 if (life != last_life) {
620 timeout = schedule_timeout(timeout);
623 remove_wait_queue(&call->waitq, &myself);
624 __set_current_state(TASK_RUNNING);
626 /* Kill off the call if it's still live. */
627 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
628 _debug("call interrupted");
629 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
630 RX_USER_ABORT, -EINTR, "KWI"))
631 afs_set_call_complete(call, -EINTR, 0);
634 spin_lock_bh(&call->state_lock);
635 ac->abort_code = call->abort_code;
636 ac->error = call->error;
637 spin_unlock_bh(&call->state_lock);
642 if (call->ret_reply0) {
643 ret = (long)call->reply[0];
644 call->reply[0] = NULL;
648 ac->responded = true;
652 _debug("call complete");
654 _leave(" = %p", (void *)ret);
659 * wake up a waiting call
661 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
662 unsigned long call_user_ID)
664 struct afs_call *call = (struct afs_call *)call_user_ID;
666 call->need_attention = true;
667 wake_up(&call->waitq);
671 * wake up an asynchronous call
673 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
674 unsigned long call_user_ID)
676 struct afs_call *call = (struct afs_call *)call_user_ID;
679 trace_afs_notify_call(rxcall, call);
680 call->need_attention = true;
682 u = atomic_fetch_add_unless(&call->usage, 1, 0);
684 trace_afs_call(call, afs_call_trace_wake, u,
685 atomic_read(&call->net->nr_outstanding_calls),
686 __builtin_return_address(0));
688 if (!queue_work(afs_async_calls, &call->async_work))
694 * Delete an asynchronous call. The work item carries a ref to the call struct
695 * that we need to release.
697 static void afs_delete_async_call(struct work_struct *work)
699 struct afs_call *call = container_of(work, struct afs_call, async_work);
709 * Perform I/O processing on an asynchronous call. The work item carries a ref
710 * to the call struct that we either need to release or to pass on.
712 static void afs_process_async_call(struct work_struct *work)
714 struct afs_call *call = container_of(work, struct afs_call, async_work);
718 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
719 call->need_attention = false;
720 afs_deliver_to_call(call);
723 if (call->state == AFS_CALL_COMPLETE) {
724 /* We have two refs to release - one from the alloc and one
725 * queued with the work item - and we can't just deallocate the
726 * call because the work item may be queued again.
728 call->async_work.func = afs_delete_async_call;
729 if (!queue_work(afs_async_calls, &call->async_work))
737 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
739 struct afs_call *call = (struct afs_call *)user_call_ID;
741 call->rxcall = rxcall;
745 * Charge the incoming call preallocation.
747 void afs_charge_preallocation(struct work_struct *work)
749 struct afs_net *net =
750 container_of(work, struct afs_net, charge_preallocation_work);
751 struct afs_call *call = net->spare_incoming_call;
755 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
760 call->state = AFS_CALL_SV_AWAIT_OP_ID;
761 init_waitqueue_head(&call->waitq);
762 afs_extract_to_tmp(call);
765 if (rxrpc_kernel_charge_accept(net->socket,
766 afs_wake_up_async_call,
774 net->spare_incoming_call = call;
778 * Discard a preallocated call when a socket is shut down.
780 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
781 unsigned long user_call_ID)
783 struct afs_call *call = (struct afs_call *)user_call_ID;
790 * Notification of an incoming call.
792 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
793 unsigned long user_call_ID)
795 struct afs_net *net = afs_sock2net(sk);
797 queue_work(afs_wq, &net->charge_preallocation_work);
801 * Grab the operation ID from an incoming cache manager call. The socket
802 * buffer is discarded on error or if we don't yet have sufficient data.
804 static int afs_deliver_cm_op_id(struct afs_call *call)
808 _enter("{%zu}", iov_iter_count(call->_iter));
810 /* the operation ID forms the first four bytes of the request data */
811 ret = afs_extract_data(call, true);
815 call->operation_ID = ntohl(call->tmp);
816 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
818 /* ask the cache manager to route the call (it'll change the call type
820 if (!afs_cm_incoming_call(call))
823 trace_afs_cb_call(call);
825 /* pass responsibility for the remainer of this message off to the
826 * cache manager op */
827 return call->type->deliver(call);
831 * Advance the AFS call state when an RxRPC service call ends the transmit
834 static void afs_notify_end_reply_tx(struct sock *sock,
835 struct rxrpc_call *rxcall,
836 unsigned long call_user_ID)
838 struct afs_call *call = (struct afs_call *)call_user_ID;
840 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
844 * send an empty reply
846 void afs_send_empty_reply(struct afs_call *call)
848 struct afs_net *net = call->net;
853 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
857 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
858 msg.msg_control = NULL;
859 msg.msg_controllen = 0;
862 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
863 afs_notify_end_reply_tx)) {
865 _leave(" [replied]");
870 rxrpc_kernel_abort_call(net->socket, call->rxcall,
871 RX_USER_ABORT, -ENOMEM, "KOO");
879 * send a simple reply
881 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
883 struct afs_net *net = call->net;
890 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
892 iov[0].iov_base = (void *) buf;
893 iov[0].iov_len = len;
896 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
897 msg.msg_control = NULL;
898 msg.msg_controllen = 0;
901 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
902 afs_notify_end_reply_tx);
905 _leave(" [replied]");
911 rxrpc_kernel_abort_call(net->socket, call->rxcall,
912 RX_USER_ABORT, -ENOMEM, "KOO");
918 * Extract a piece of data from the received data socket buffers.
920 int afs_extract_data(struct afs_call *call, bool want_more)
922 struct afs_net *net = call->net;
923 struct iov_iter *iter = call->_iter;
924 enum afs_call_state state;
925 u32 remote_abort = 0;
928 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
930 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
931 want_more, &remote_abort,
933 if (ret == 0 || ret == -EAGAIN)
936 state = READ_ONCE(call->state);
939 case AFS_CALL_CL_AWAIT_REPLY:
940 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
942 case AFS_CALL_SV_AWAIT_REQUEST:
943 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
945 case AFS_CALL_COMPLETE:
946 kdebug("prem complete %d", call->error);
947 return afs_io_error(call, afs_io_error_extract);
954 afs_set_call_complete(call, ret, remote_abort);
959 * Log protocol error production.
961 noinline int afs_protocol_error(struct afs_call *call, int error,
962 enum afs_eproto_cause cause)
964 trace_afs_protocol_error(call, error, cause);