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>
20 struct workqueue_struct *afs_async_calls;
22 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
23 static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *);
24 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
25 static void afs_process_async_call(struct work_struct *);
26 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
27 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
28 static int afs_deliver_cm_op_id(struct afs_call *);
30 /* asynchronous incoming call initial processing */
31 static const struct afs_call_type afs_RXCMxxxx = {
33 .deliver = afs_deliver_cm_op_id,
37 * open an RxRPC socket and bind it to be a server for callback notifications
38 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
40 int afs_open_socket(struct afs_net *net)
42 struct sockaddr_rxrpc srx;
43 struct socket *socket;
44 unsigned int min_level;
49 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
53 socket->sk->sk_allocation = GFP_NOFS;
55 /* bind the callback manager's address to make this a server socket */
56 memset(&srx, 0, sizeof(srx));
57 srx.srx_family = AF_RXRPC;
58 srx.srx_service = CM_SERVICE;
59 srx.transport_type = SOCK_DGRAM;
60 srx.transport_len = sizeof(srx.transport.sin6);
61 srx.transport.sin6.sin6_family = AF_INET6;
62 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
64 min_level = RXRPC_SECURITY_ENCRYPT;
65 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
66 (void *)&min_level, sizeof(min_level));
70 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
71 if (ret == -EADDRINUSE) {
72 srx.transport.sin6.sin6_port = 0;
73 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
78 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
79 afs_rx_discard_new_call);
81 ret = kernel_listen(socket, INT_MAX);
86 afs_charge_preallocation(&net->charge_preallocation_work);
98 * close the RxRPC socket AFS was using
100 void afs_close_socket(struct afs_net *net)
104 kernel_listen(net->socket, 0);
105 flush_workqueue(afs_async_calls);
107 if (net->spare_incoming_call) {
108 afs_put_call(net->spare_incoming_call);
109 net->spare_incoming_call = NULL;
112 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
113 wait_var_event(&net->nr_outstanding_calls,
114 !atomic_read(&net->nr_outstanding_calls));
115 _debug("no outstanding calls");
117 kernel_sock_shutdown(net->socket, SHUT_RDWR);
118 flush_workqueue(afs_async_calls);
119 sock_release(net->socket);
128 static struct afs_call *afs_alloc_call(struct afs_net *net,
129 const struct afs_call_type *type,
132 struct afs_call *call;
135 call = kzalloc(sizeof(*call), gfp);
141 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
142 atomic_set(&call->usage, 1);
143 INIT_WORK(&call->async_work, afs_process_async_call);
144 init_waitqueue_head(&call->waitq);
145 spin_lock_init(&call->state_lock);
147 o = atomic_inc_return(&net->nr_outstanding_calls);
148 trace_afs_call(call, afs_call_trace_alloc, 1, o,
149 __builtin_return_address(0));
154 * Dispose of a reference on a call.
156 void afs_put_call(struct afs_call *call)
158 struct afs_net *net = call->net;
159 int n = atomic_dec_return(&call->usage);
160 int o = atomic_read(&net->nr_outstanding_calls);
162 trace_afs_call(call, afs_call_trace_put, n, o,
163 __builtin_return_address(0));
167 ASSERT(!work_pending(&call->async_work));
168 ASSERT(call->type->name != NULL);
171 rxrpc_kernel_end_call(net->socket, call->rxcall);
174 if (call->type->destructor)
175 call->type->destructor(call);
177 afs_put_server(call->net, call->cm_server);
178 afs_put_cb_interest(call->net, call->cbi);
179 kfree(call->request);
181 trace_afs_call(call, afs_call_trace_free, 0, o,
182 __builtin_return_address(0));
185 o = atomic_dec_return(&net->nr_outstanding_calls);
187 wake_up_var(&net->nr_outstanding_calls);
192 * Queue the call for actual work. Returns 0 unconditionally for convenience.
194 int afs_queue_call_work(struct afs_call *call)
196 int u = atomic_inc_return(&call->usage);
198 trace_afs_call(call, afs_call_trace_work, u,
199 atomic_read(&call->net->nr_outstanding_calls),
200 __builtin_return_address(0));
202 INIT_WORK(&call->work, call->type->work);
204 if (!queue_work(afs_wq, &call->work))
210 * allocate a call with flat request and reply buffers
212 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
213 const struct afs_call_type *type,
214 size_t request_size, size_t reply_max)
216 struct afs_call *call;
218 call = afs_alloc_call(net, type, GFP_NOFS);
223 call->request_size = request_size;
224 call->request = kmalloc(request_size, GFP_NOFS);
230 call->reply_max = reply_max;
231 call->buffer = kmalloc(reply_max, GFP_NOFS);
236 call->operation_ID = type->op;
237 init_waitqueue_head(&call->waitq);
247 * clean up a call with flat buffer
249 void afs_flat_call_destructor(struct afs_call *call)
253 kfree(call->request);
254 call->request = NULL;
259 #define AFS_BVEC_MAX 8
262 * Load the given bvec with the next few pages.
264 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
265 struct bio_vec *bv, pgoff_t first, pgoff_t last,
268 struct page *pages[AFS_BVEC_MAX];
269 unsigned int nr, n, i, to, bytes = 0;
271 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
272 n = find_get_pages_contig(call->mapping, first, nr, pages);
273 ASSERTCMP(n, ==, nr);
275 msg->msg_flags |= MSG_MORE;
276 for (i = 0; i < nr; i++) {
278 if (first + i >= last) {
280 msg->msg_flags &= ~MSG_MORE;
282 bv[i].bv_page = pages[i];
283 bv[i].bv_len = to - offset;
284 bv[i].bv_offset = offset;
285 bytes += to - offset;
289 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
293 * Advance the AFS call state when the RxRPC call ends the transmit phase.
295 static void afs_notify_end_request_tx(struct sock *sock,
296 struct rxrpc_call *rxcall,
297 unsigned long call_user_ID)
299 struct afs_call *call = (struct afs_call *)call_user_ID;
301 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
305 * attach the data from a bunch of pages on an inode to a call
307 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
309 struct bio_vec bv[AFS_BVEC_MAX];
310 unsigned int bytes, nr, loop, offset;
311 pgoff_t first = call->first, last = call->last;
314 offset = call->first_offset;
315 call->first_offset = 0;
318 afs_load_bvec(call, msg, bv, first, last, offset);
319 trace_afs_send_pages(call, msg, first, last, offset);
322 bytes = msg->msg_iter.count;
323 nr = msg->msg_iter.nr_segs;
325 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
326 bytes, afs_notify_end_request_tx);
327 for (loop = 0; loop < nr; loop++)
328 put_page(bv[loop].bv_page);
333 } while (first <= last);
335 trace_afs_sent_pages(call, call->first, last, first, ret);
342 long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
343 gfp_t gfp, bool async)
345 struct sockaddr_rxrpc *srx = ac->addr;
346 struct rxrpc_call *rxcall;
352 _enter(",{%pISp},", &srx->transport);
354 ASSERT(call->type != NULL);
355 ASSERT(call->type->name != NULL);
357 _debug("____MAKE %p{%s,%x} [%d]____",
358 call, call->type->name, key_serial(call->key),
359 atomic_read(&call->net->nr_outstanding_calls));
363 /* Work out the length we're going to transmit. This is awkward for
364 * calls such as FS.StoreData where there's an extra injection of data
365 * after the initial fixed part.
367 tx_total_len = call->request_size;
368 if (call->send_pages) {
369 if (call->last == call->first) {
370 tx_total_len += call->last_to - call->first_offset;
372 /* It looks mathematically like you should be able to
373 * combine the following lines with the ones above, but
374 * unsigned arithmetic is fun when it wraps...
376 tx_total_len += PAGE_SIZE - call->first_offset;
377 tx_total_len += call->last_to;
378 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
383 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
387 afs_wake_up_async_call :
388 afs_wake_up_call_waiter),
391 if (IS_ERR(rxcall)) {
392 ret = PTR_ERR(rxcall);
393 goto error_kill_call;
396 call->rxcall = rxcall;
398 /* send the request */
399 iov[0].iov_base = call->request;
400 iov[0].iov_len = call->request_size;
404 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
406 msg.msg_control = NULL;
407 msg.msg_controllen = 0;
408 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
410 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
411 &msg, call->request_size,
412 afs_notify_end_request_tx);
416 if (call->send_pages) {
417 ret = afs_send_pages(call, &msg);
422 /* at this point, an async call may no longer exist as it may have
423 * already completed */
427 return afs_wait_for_call_to_complete(call, ac);
430 call->state = AFS_CALL_COMPLETE;
431 if (ret != -ECONNABORTED) {
432 rxrpc_kernel_abort_call(call->net->socket, rxcall,
433 RX_USER_ABORT, ret, "KSD");
435 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, NULL, 0, 0);
436 rxrpc_kernel_recv_data(call->net->socket, rxcall,
437 &msg.msg_iter, false,
438 &call->abort_code, &call->service_id);
439 ac->abort_code = call->abort_code;
440 ac->responded = true;
443 trace_afs_call_done(call);
447 _leave(" = %d", ret);
452 * deliver messages to a call
454 static void afs_deliver_to_call(struct afs_call *call)
456 enum afs_call_state state;
457 u32 abort_code, remote_abort = 0;
460 _enter("%s", call->type->name);
462 while (state = READ_ONCE(call->state),
463 state == AFS_CALL_CL_AWAIT_REPLY ||
464 state == AFS_CALL_SV_AWAIT_OP_ID ||
465 state == AFS_CALL_SV_AWAIT_REQUEST ||
466 state == AFS_CALL_SV_AWAIT_ACK
468 if (state == AFS_CALL_SV_AWAIT_ACK) {
469 struct iov_iter iter;
471 iov_iter_kvec(&iter, READ | ITER_KVEC, NULL, 0, 0);
472 ret = rxrpc_kernel_recv_data(call->net->socket,
473 call->rxcall, &iter, false,
476 trace_afs_recv_data(call, 0, 0, false, ret);
478 if (ret == -EINPROGRESS || ret == -EAGAIN)
480 if (ret < 0 || ret == 1) {
488 ret = call->type->deliver(call);
489 state = READ_ONCE(call->state);
492 if (state == AFS_CALL_CL_PROC_REPLY) {
494 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
495 &call->cbi->server->flags);
498 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
504 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
507 abort_code = RXGEN_OPCODE;
508 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
509 abort_code, ret, "KIV");
512 pr_err("kAFS: Call %u in bad state %u\n",
513 call->debug_id, state);
519 abort_code = RXGEN_CC_UNMARSHAL;
520 if (state != AFS_CALL_CL_AWAIT_REPLY)
521 abort_code = RXGEN_SS_UNMARSHAL;
522 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
523 abort_code, -EBADMSG, "KUM");
529 if (state == AFS_CALL_COMPLETE && call->incoming)
538 afs_set_call_complete(call, ret, remote_abort);
539 state = AFS_CALL_COMPLETE;
544 * wait synchronously for a call to complete
546 static long afs_wait_for_call_to_complete(struct afs_call *call,
547 struct afs_addr_cursor *ac)
549 signed long rtt2, timeout;
554 DECLARE_WAITQUEUE(myself, current);
558 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
559 rtt2 = nsecs_to_jiffies64(rtt) * 2;
564 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
566 add_wait_queue(&call->waitq, &myself);
568 set_current_state(TASK_UNINTERRUPTIBLE);
570 /* deliver any messages that are in the queue */
571 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
572 call->need_attention) {
573 call->need_attention = false;
574 __set_current_state(TASK_RUNNING);
575 afs_deliver_to_call(call);
580 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
583 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
585 life == last_life && signal_pending(current))
588 if (life != last_life) {
593 timeout = schedule_timeout(timeout);
596 remove_wait_queue(&call->waitq, &myself);
597 __set_current_state(TASK_RUNNING);
599 /* Kill off the call if it's still live. */
600 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
601 _debug("call interrupted");
602 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
603 RX_USER_ABORT, -EINTR, "KWI"))
604 afs_set_call_complete(call, -EINTR, 0);
607 spin_lock_bh(&call->state_lock);
608 ac->abort_code = call->abort_code;
609 ac->error = call->error;
610 spin_unlock_bh(&call->state_lock);
615 if (call->ret_reply0) {
616 ret = (long)call->reply[0];
617 call->reply[0] = NULL;
621 ac->responded = true;
625 _debug("call complete");
627 _leave(" = %p", (void *)ret);
632 * wake up a waiting call
634 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
635 unsigned long call_user_ID)
637 struct afs_call *call = (struct afs_call *)call_user_ID;
639 call->need_attention = true;
640 wake_up(&call->waitq);
644 * wake up an asynchronous call
646 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
647 unsigned long call_user_ID)
649 struct afs_call *call = (struct afs_call *)call_user_ID;
652 trace_afs_notify_call(rxcall, call);
653 call->need_attention = true;
655 u = atomic_fetch_add_unless(&call->usage, 1, 0);
657 trace_afs_call(call, afs_call_trace_wake, u + 1,
658 atomic_read(&call->net->nr_outstanding_calls),
659 __builtin_return_address(0));
661 if (!queue_work(afs_async_calls, &call->async_work))
667 * Delete an asynchronous call. The work item carries a ref to the call struct
668 * that we need to release.
670 static void afs_delete_async_call(struct work_struct *work)
672 struct afs_call *call = container_of(work, struct afs_call, async_work);
682 * Perform I/O processing on an asynchronous call. The work item carries a ref
683 * to the call struct that we either need to release or to pass on.
685 static void afs_process_async_call(struct work_struct *work)
687 struct afs_call *call = container_of(work, struct afs_call, async_work);
691 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
692 call->need_attention = false;
693 afs_deliver_to_call(call);
696 if (call->state == AFS_CALL_COMPLETE) {
697 /* We have two refs to release - one from the alloc and one
698 * queued with the work item - and we can't just deallocate the
699 * call because the work item may be queued again.
701 call->async_work.func = afs_delete_async_call;
702 if (!queue_work(afs_async_calls, &call->async_work))
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);
733 call->state = AFS_CALL_SV_AWAIT_OP_ID;
734 init_waitqueue_head(&call->waitq);
737 if (rxrpc_kernel_charge_accept(net->socket,
738 afs_wake_up_async_call,
746 net->spare_incoming_call = call;
750 * Discard a preallocated call when a socket is shut down.
752 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
753 unsigned long user_call_ID)
755 struct afs_call *call = (struct afs_call *)user_call_ID;
762 * Notification of an incoming call.
764 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
765 unsigned long user_call_ID)
767 struct afs_net *net = afs_sock2net(sk);
769 queue_work(afs_wq, &net->charge_preallocation_work);
773 * Grab the operation ID from an incoming cache manager call. The socket
774 * buffer is discarded on error or if we don't yet have sufficient data.
776 static int afs_deliver_cm_op_id(struct afs_call *call)
780 _enter("{%zu}", call->offset);
782 ASSERTCMP(call->offset, <, 4);
784 /* the operation ID forms the first four bytes of the request data */
785 ret = afs_extract_data(call, &call->tmp, 4, 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);
793 /* ask the cache manager to route the call (it'll change the call type
795 if (!afs_cm_incoming_call(call))
798 trace_afs_cb_call(call);
800 /* pass responsibility for the remainer of this message off to the
801 * cache manager op */
802 return call->type->deliver(call);
806 * Advance the AFS call state when an RxRPC service call ends the transmit
809 static void afs_notify_end_reply_tx(struct sock *sock,
810 struct rxrpc_call *rxcall,
811 unsigned long call_user_ID)
813 struct afs_call *call = (struct afs_call *)call_user_ID;
815 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
819 * send an empty reply
821 void afs_send_empty_reply(struct afs_call *call)
823 struct afs_net *net = call->net;
828 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
832 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
833 msg.msg_control = NULL;
834 msg.msg_controllen = 0;
837 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
838 afs_notify_end_reply_tx)) {
840 _leave(" [replied]");
845 rxrpc_kernel_abort_call(net->socket, call->rxcall,
846 RX_USER_ABORT, -ENOMEM, "KOO");
854 * send a simple reply
856 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
858 struct afs_net *net = call->net;
865 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
867 iov[0].iov_base = (void *) buf;
868 iov[0].iov_len = len;
871 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
872 msg.msg_control = NULL;
873 msg.msg_controllen = 0;
876 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
877 afs_notify_end_reply_tx);
880 _leave(" [replied]");
886 rxrpc_kernel_abort_call(net->socket, call->rxcall,
887 RX_USER_ABORT, -ENOMEM, "KOO");
893 * Extract a piece of data from the received data socket buffers.
895 int afs_extract_data(struct afs_call *call, void *buf, size_t count,
898 struct afs_net *net = call->net;
899 struct iov_iter iter;
901 enum afs_call_state state;
902 u32 remote_abort = 0;
905 _enter("{%s,%zu},,%zu,%d",
906 call->type->name, call->offset, count, want_more);
908 ASSERTCMP(call->offset, <=, count);
910 iov.iov_base = buf + call->offset;
911 iov.iov_len = count - call->offset;
912 iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, count - call->offset);
914 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, &iter,
915 want_more, &remote_abort,
917 call->offset += (count - call->offset) - iov_iter_count(&iter);
918 trace_afs_recv_data(call, count, call->offset, want_more, ret);
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);
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)
949 trace_afs_protocol_error(call, error, __builtin_return_address(0));