1 /* Maintain an RxRPC server socket to do AFS communications through
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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;
46 u16 service_upgrade[2];
51 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
55 socket->sk->sk_allocation = GFP_NOFS;
57 /* bind the callback manager's address to make this a server socket */
58 memset(&srx, 0, sizeof(srx));
59 srx.srx_family = AF_RXRPC;
60 srx.srx_service = CM_SERVICE;
61 srx.transport_type = SOCK_DGRAM;
62 srx.transport_len = sizeof(srx.transport.sin6);
63 srx.transport.sin6.sin6_family = AF_INET6;
64 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
66 min_level = RXRPC_SECURITY_ENCRYPT;
67 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
68 (void *)&min_level, sizeof(min_level));
72 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
73 if (ret == -EADDRINUSE) {
74 srx.transport.sin6.sin6_port = 0;
75 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
80 srx.srx_service = YFS_CM_SERVICE;
81 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
85 service_upgrade[0] = CM_SERVICE;
86 service_upgrade[1] = YFS_CM_SERVICE;
87 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_UPGRADEABLE_SERVICE,
88 (void *)service_upgrade, sizeof(service_upgrade));
93 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
94 afs_rx_discard_new_call);
96 ret = kernel_listen(socket, INT_MAX);
100 net->socket = socket;
101 afs_charge_preallocation(&net->charge_preallocation_work);
106 sock_release(socket);
108 _leave(" = %d", ret);
113 * close the RxRPC socket AFS was using
115 void afs_close_socket(struct afs_net *net)
119 kernel_listen(net->socket, 0);
120 flush_workqueue(afs_async_calls);
122 if (net->spare_incoming_call) {
123 afs_put_call(net->spare_incoming_call);
124 net->spare_incoming_call = NULL;
127 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
128 wait_var_event(&net->nr_outstanding_calls,
129 !atomic_read(&net->nr_outstanding_calls));
130 _debug("no outstanding calls");
132 kernel_sock_shutdown(net->socket, SHUT_RDWR);
133 flush_workqueue(afs_async_calls);
134 sock_release(net->socket);
143 static struct afs_call *afs_alloc_call(struct afs_net *net,
144 const struct afs_call_type *type,
147 struct afs_call *call;
150 call = kzalloc(sizeof(*call), gfp);
156 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
157 atomic_set(&call->usage, 1);
158 INIT_WORK(&call->async_work, afs_process_async_call);
159 init_waitqueue_head(&call->waitq);
160 spin_lock_init(&call->state_lock);
161 call->_iter = &call->iter;
163 o = atomic_inc_return(&net->nr_outstanding_calls);
164 trace_afs_call(call, afs_call_trace_alloc, 1, o,
165 __builtin_return_address(0));
170 * Dispose of a reference on a call.
172 void afs_put_call(struct afs_call *call)
174 struct afs_net *net = call->net;
175 int n = atomic_dec_return(&call->usage);
176 int o = atomic_read(&net->nr_outstanding_calls);
178 trace_afs_call(call, afs_call_trace_put, n + 1, o,
179 __builtin_return_address(0));
183 ASSERT(!work_pending(&call->async_work));
184 ASSERT(call->type->name != NULL);
187 rxrpc_kernel_end_call(net->socket, call->rxcall);
190 if (call->type->destructor)
191 call->type->destructor(call);
193 afs_put_server(call->net, call->cm_server);
194 afs_put_cb_interest(call->net, call->cbi);
195 kfree(call->request);
197 trace_afs_call(call, afs_call_trace_free, 0, o,
198 __builtin_return_address(0));
201 o = atomic_dec_return(&net->nr_outstanding_calls);
203 wake_up_var(&net->nr_outstanding_calls);
208 * Queue the call for actual work. Returns 0 unconditionally for convenience.
210 int afs_queue_call_work(struct afs_call *call)
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->addr;
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));
380 /* Work out the length we're going to transmit. This is awkward for
381 * calls such as FS.StoreData where there's an extra injection of data
382 * after the initial fixed part.
384 tx_total_len = call->request_size;
385 if (call->send_pages) {
386 if (call->last == call->first) {
387 tx_total_len += call->last_to - call->first_offset;
389 /* It looks mathematically like you should be able to
390 * combine the following lines with the ones above, but
391 * unsigned arithmetic is fun when it wraps...
393 tx_total_len += PAGE_SIZE - call->first_offset;
394 tx_total_len += call->last_to;
395 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
400 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
404 afs_wake_up_async_call :
405 afs_wake_up_call_waiter),
408 if (IS_ERR(rxcall)) {
409 ret = PTR_ERR(rxcall);
410 goto error_kill_call;
413 call->rxcall = rxcall;
415 /* send the request */
416 iov[0].iov_base = call->request;
417 iov[0].iov_len = call->request_size;
421 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
422 msg.msg_control = NULL;
423 msg.msg_controllen = 0;
424 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
426 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
427 &msg, call->request_size,
428 afs_notify_end_request_tx);
432 if (call->send_pages) {
433 ret = afs_send_pages(call, &msg);
438 /* at this point, an async call may no longer exist as it may have
439 * already completed */
443 return afs_wait_for_call_to_complete(call, ac);
446 call->state = AFS_CALL_COMPLETE;
447 if (ret != -ECONNABORTED) {
448 rxrpc_kernel_abort_call(call->net->socket, rxcall,
449 RX_USER_ABORT, ret, "KSD");
451 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
452 rxrpc_kernel_recv_data(call->net->socket, rxcall,
453 &msg.msg_iter, false,
454 &call->abort_code, &call->service_id);
455 ac->abort_code = call->abort_code;
456 ac->responded = true;
459 trace_afs_call_done(call);
463 _leave(" = %d", ret);
468 * deliver messages to a call
470 static void afs_deliver_to_call(struct afs_call *call)
472 enum afs_call_state state;
473 u32 abort_code, remote_abort = 0;
476 _enter("%s", call->type->name);
478 while (state = READ_ONCE(call->state),
479 state == AFS_CALL_CL_AWAIT_REPLY ||
480 state == AFS_CALL_SV_AWAIT_OP_ID ||
481 state == AFS_CALL_SV_AWAIT_REQUEST ||
482 state == AFS_CALL_SV_AWAIT_ACK
484 if (state == AFS_CALL_SV_AWAIT_ACK) {
485 iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
486 ret = rxrpc_kernel_recv_data(call->net->socket,
487 call->rxcall, &call->iter,
488 false, &remote_abort,
490 trace_afs_receive_data(call, &call->iter, false, ret);
492 if (ret == -EINPROGRESS || ret == -EAGAIN)
494 if (ret < 0 || ret == 1) {
502 ret = call->type->deliver(call);
503 state = READ_ONCE(call->state);
506 if (state == AFS_CALL_CL_PROC_REPLY) {
508 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
509 &call->cbi->server->flags);
512 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
518 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
521 abort_code = RXGEN_OPCODE;
522 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
523 abort_code, ret, "KIV");
526 pr_err("kAFS: Call %u in bad state %u\n",
527 call->debug_id, state);
533 abort_code = RXGEN_CC_UNMARSHAL;
534 if (state != AFS_CALL_CL_AWAIT_REPLY)
535 abort_code = RXGEN_SS_UNMARSHAL;
536 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
537 abort_code, ret, "KUM");
543 if (state == AFS_CALL_COMPLETE && call->incoming)
552 afs_set_call_complete(call, ret, remote_abort);
553 state = AFS_CALL_COMPLETE;
558 * wait synchronously for a call to complete
560 static long afs_wait_for_call_to_complete(struct afs_call *call,
561 struct afs_addr_cursor *ac)
563 signed long rtt2, timeout;
568 DECLARE_WAITQUEUE(myself, current);
572 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
573 rtt2 = nsecs_to_jiffies64(rtt) * 2;
578 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
580 add_wait_queue(&call->waitq, &myself);
582 set_current_state(TASK_UNINTERRUPTIBLE);
584 /* deliver any messages that are in the queue */
585 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
586 call->need_attention) {
587 call->need_attention = false;
588 __set_current_state(TASK_RUNNING);
589 afs_deliver_to_call(call);
593 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
596 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
598 life == last_life && signal_pending(current))
601 if (life != last_life) {
606 timeout = schedule_timeout(timeout);
609 remove_wait_queue(&call->waitq, &myself);
610 __set_current_state(TASK_RUNNING);
612 /* Kill off the call if it's still live. */
613 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
614 _debug("call interrupted");
615 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
616 RX_USER_ABORT, -EINTR, "KWI"))
617 afs_set_call_complete(call, -EINTR, 0);
620 spin_lock_bh(&call->state_lock);
621 ac->abort_code = call->abort_code;
622 ac->error = call->error;
623 spin_unlock_bh(&call->state_lock);
628 if (call->ret_reply0) {
629 ret = (long)call->reply[0];
630 call->reply[0] = NULL;
634 ac->responded = true;
638 _debug("call complete");
640 _leave(" = %p", (void *)ret);
645 * wake up a waiting call
647 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
648 unsigned long call_user_ID)
650 struct afs_call *call = (struct afs_call *)call_user_ID;
652 call->need_attention = true;
653 wake_up(&call->waitq);
657 * wake up an asynchronous call
659 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
660 unsigned long call_user_ID)
662 struct afs_call *call = (struct afs_call *)call_user_ID;
665 trace_afs_notify_call(rxcall, call);
666 call->need_attention = true;
668 u = atomic_fetch_add_unless(&call->usage, 1, 0);
670 trace_afs_call(call, afs_call_trace_wake, u,
671 atomic_read(&call->net->nr_outstanding_calls),
672 __builtin_return_address(0));
674 if (!queue_work(afs_async_calls, &call->async_work))
680 * Delete an asynchronous call. The work item carries a ref to the call struct
681 * that we need to release.
683 static void afs_delete_async_call(struct work_struct *work)
685 struct afs_call *call = container_of(work, struct afs_call, async_work);
695 * Perform I/O processing on an asynchronous call. The work item carries a ref
696 * to the call struct that we either need to release or to pass on.
698 static void afs_process_async_call(struct work_struct *work)
700 struct afs_call *call = container_of(work, struct afs_call, async_work);
704 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
705 call->need_attention = false;
706 afs_deliver_to_call(call);
709 if (call->state == AFS_CALL_COMPLETE) {
710 /* We have two refs to release - one from the alloc and one
711 * queued with the work item - and we can't just deallocate the
712 * call because the work item may be queued again.
714 call->async_work.func = afs_delete_async_call;
715 if (!queue_work(afs_async_calls, &call->async_work))
723 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
725 struct afs_call *call = (struct afs_call *)user_call_ID;
727 call->rxcall = rxcall;
731 * Charge the incoming call preallocation.
733 void afs_charge_preallocation(struct work_struct *work)
735 struct afs_net *net =
736 container_of(work, struct afs_net, charge_preallocation_work);
737 struct afs_call *call = net->spare_incoming_call;
741 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
746 call->state = AFS_CALL_SV_AWAIT_OP_ID;
747 init_waitqueue_head(&call->waitq);
748 afs_extract_to_tmp(call);
751 if (rxrpc_kernel_charge_accept(net->socket,
752 afs_wake_up_async_call,
760 net->spare_incoming_call = call;
764 * Discard a preallocated call when a socket is shut down.
766 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
767 unsigned long user_call_ID)
769 struct afs_call *call = (struct afs_call *)user_call_ID;
776 * Notification of an incoming call.
778 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
779 unsigned long user_call_ID)
781 struct afs_net *net = afs_sock2net(sk);
783 queue_work(afs_wq, &net->charge_preallocation_work);
787 * Grab the operation ID from an incoming cache manager call. The socket
788 * buffer is discarded on error or if we don't yet have sufficient data.
790 static int afs_deliver_cm_op_id(struct afs_call *call)
794 _enter("{%zu}", iov_iter_count(call->_iter));
796 /* the operation ID forms the first four bytes of the request data */
797 ret = afs_extract_data(call, true);
801 call->operation_ID = ntohl(call->tmp);
802 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
804 /* ask the cache manager to route the call (it'll change the call type
806 if (!afs_cm_incoming_call(call))
809 trace_afs_cb_call(call);
811 /* pass responsibility for the remainer of this message off to the
812 * cache manager op */
813 return call->type->deliver(call);
817 * Advance the AFS call state when an RxRPC service call ends the transmit
820 static void afs_notify_end_reply_tx(struct sock *sock,
821 struct rxrpc_call *rxcall,
822 unsigned long call_user_ID)
824 struct afs_call *call = (struct afs_call *)call_user_ID;
826 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
830 * send an empty reply
832 void afs_send_empty_reply(struct afs_call *call)
834 struct afs_net *net = call->net;
839 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
843 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
844 msg.msg_control = NULL;
845 msg.msg_controllen = 0;
848 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
849 afs_notify_end_reply_tx)) {
851 _leave(" [replied]");
856 rxrpc_kernel_abort_call(net->socket, call->rxcall,
857 RX_USER_ABORT, -ENOMEM, "KOO");
865 * send a simple reply
867 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
869 struct afs_net *net = call->net;
876 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
878 iov[0].iov_base = (void *) buf;
879 iov[0].iov_len = len;
882 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
883 msg.msg_control = NULL;
884 msg.msg_controllen = 0;
887 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
888 afs_notify_end_reply_tx);
891 _leave(" [replied]");
897 rxrpc_kernel_abort_call(net->socket, call->rxcall,
898 RX_USER_ABORT, -ENOMEM, "KOO");
904 * Extract a piece of data from the received data socket buffers.
906 int afs_extract_data(struct afs_call *call, bool want_more)
908 struct afs_net *net = call->net;
909 struct iov_iter *iter = call->_iter;
910 enum afs_call_state state;
911 u32 remote_abort = 0;
914 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
916 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
917 want_more, &remote_abort,
919 if (ret == 0 || ret == -EAGAIN)
922 state = READ_ONCE(call->state);
925 case AFS_CALL_CL_AWAIT_REPLY:
926 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
928 case AFS_CALL_SV_AWAIT_REQUEST:
929 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
931 case AFS_CALL_COMPLETE:
932 kdebug("prem complete %d", call->error);
933 return afs_io_error(call, afs_io_error_extract);
940 afs_set_call_complete(call, ret, remote_abort);
945 * Log protocol error production.
947 noinline int afs_protocol_error(struct afs_call *call, int error,
948 enum afs_eproto_cause cause)
950 trace_afs_protocol_error(call, error, cause);