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>
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;
48 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
52 socket->sk->sk_allocation = GFP_NOFS;
54 /* bind the callback manager's address to make this a server socket */
55 memset(&srx, 0, sizeof(srx));
56 srx.srx_family = AF_RXRPC;
57 srx.srx_service = CM_SERVICE;
58 srx.transport_type = SOCK_DGRAM;
59 srx.transport_len = sizeof(srx.transport.sin6);
60 srx.transport.sin6.sin6_family = AF_INET6;
61 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
63 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
64 if (ret == -EADDRINUSE) {
65 srx.transport.sin6.sin6_port = 0;
66 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
71 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
72 afs_rx_discard_new_call);
74 ret = kernel_listen(socket, INT_MAX);
79 afs_charge_preallocation(&net->charge_preallocation_work);
91 * close the RxRPC socket AFS was using
93 void afs_close_socket(struct afs_net *net)
97 kernel_listen(net->socket, 0);
98 flush_workqueue(afs_async_calls);
100 if (net->spare_incoming_call) {
101 afs_put_call(net->spare_incoming_call);
102 net->spare_incoming_call = NULL;
105 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
106 wait_var_event(&net->nr_outstanding_calls,
107 !atomic_read(&net->nr_outstanding_calls));
108 _debug("no outstanding calls");
110 kernel_sock_shutdown(net->socket, SHUT_RDWR);
111 flush_workqueue(afs_async_calls);
112 sock_release(net->socket);
121 static struct afs_call *afs_alloc_call(struct afs_net *net,
122 const struct afs_call_type *type,
125 struct afs_call *call;
128 call = kzalloc(sizeof(*call), gfp);
134 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
135 atomic_set(&call->usage, 1);
136 INIT_WORK(&call->async_work, afs_process_async_call);
137 init_waitqueue_head(&call->waitq);
138 spin_lock_init(&call->state_lock);
140 o = atomic_inc_return(&net->nr_outstanding_calls);
141 trace_afs_call(call, afs_call_trace_alloc, 1, o,
142 __builtin_return_address(0));
147 * Dispose of a reference on a call.
149 void afs_put_call(struct afs_call *call)
151 struct afs_net *net = call->net;
152 int n = atomic_dec_return(&call->usage);
153 int o = atomic_read(&net->nr_outstanding_calls);
155 trace_afs_call(call, afs_call_trace_put, n + 1, o,
156 __builtin_return_address(0));
160 ASSERT(!work_pending(&call->async_work));
161 ASSERT(call->type->name != NULL);
164 rxrpc_kernel_end_call(net->socket, call->rxcall);
167 if (call->type->destructor)
168 call->type->destructor(call);
170 afs_put_server(call->net, call->cm_server);
171 afs_put_cb_interest(call->net, call->cbi);
172 kfree(call->request);
174 trace_afs_call(call, afs_call_trace_free, 0, o,
175 __builtin_return_address(0));
178 o = atomic_dec_return(&net->nr_outstanding_calls);
180 wake_up_var(&net->nr_outstanding_calls);
185 * Queue the call for actual work. Returns 0 unconditionally for convenience.
187 int afs_queue_call_work(struct afs_call *call)
189 int u = atomic_inc_return(&call->usage);
191 trace_afs_call(call, afs_call_trace_work, u,
192 atomic_read(&call->net->nr_outstanding_calls),
193 __builtin_return_address(0));
195 INIT_WORK(&call->work, call->type->work);
197 if (!queue_work(afs_wq, &call->work))
203 * allocate a call with flat request and reply buffers
205 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
206 const struct afs_call_type *type,
207 size_t request_size, size_t reply_max)
209 struct afs_call *call;
211 call = afs_alloc_call(net, type, GFP_NOFS);
216 call->request_size = request_size;
217 call->request = kmalloc(request_size, GFP_NOFS);
223 call->reply_max = reply_max;
224 call->buffer = kmalloc(reply_max, GFP_NOFS);
229 call->operation_ID = type->op;
230 init_waitqueue_head(&call->waitq);
240 * clean up a call with flat buffer
242 void afs_flat_call_destructor(struct afs_call *call)
246 kfree(call->request);
247 call->request = NULL;
252 #define AFS_BVEC_MAX 8
255 * Load the given bvec with the next few pages.
257 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
258 struct bio_vec *bv, pgoff_t first, pgoff_t last,
261 struct page *pages[AFS_BVEC_MAX];
262 unsigned int nr, n, i, to, bytes = 0;
264 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
265 n = find_get_pages_contig(call->mapping, first, nr, pages);
266 ASSERTCMP(n, ==, nr);
268 msg->msg_flags |= MSG_MORE;
269 for (i = 0; i < nr; i++) {
271 if (first + i >= last) {
273 msg->msg_flags &= ~MSG_MORE;
275 bv[i].bv_page = pages[i];
276 bv[i].bv_len = to - offset;
277 bv[i].bv_offset = offset;
278 bytes += to - offset;
282 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
286 * Advance the AFS call state when the RxRPC call ends the transmit phase.
288 static void afs_notify_end_request_tx(struct sock *sock,
289 struct rxrpc_call *rxcall,
290 unsigned long call_user_ID)
292 struct afs_call *call = (struct afs_call *)call_user_ID;
294 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
298 * attach the data from a bunch of pages on an inode to a call
300 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
302 struct bio_vec bv[AFS_BVEC_MAX];
303 unsigned int bytes, nr, loop, offset;
304 pgoff_t first = call->first, last = call->last;
307 offset = call->first_offset;
308 call->first_offset = 0;
311 afs_load_bvec(call, msg, bv, first, last, offset);
312 trace_afs_send_pages(call, msg, first, last, offset);
315 bytes = msg->msg_iter.count;
316 nr = msg->msg_iter.nr_segs;
318 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
319 bytes, afs_notify_end_request_tx);
320 for (loop = 0; loop < nr; loop++)
321 put_page(bv[loop].bv_page);
326 } while (first <= last);
328 trace_afs_sent_pages(call, call->first, last, first, ret);
335 long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
336 gfp_t gfp, bool async)
338 struct sockaddr_rxrpc *srx = ac->addr;
339 struct rxrpc_call *rxcall;
346 _enter(",{%pISp},", &srx->transport);
348 ASSERT(call->type != NULL);
349 ASSERT(call->type->name != NULL);
351 _debug("____MAKE %p{%s,%x} [%d]____",
352 call, call->type->name, key_serial(call->key),
353 atomic_read(&call->net->nr_outstanding_calls));
357 /* Work out the length we're going to transmit. This is awkward for
358 * calls such as FS.StoreData where there's an extra injection of data
359 * after the initial fixed part.
361 tx_total_len = call->request_size;
362 if (call->send_pages) {
363 if (call->last == call->first) {
364 tx_total_len += call->last_to - call->first_offset;
366 /* It looks mathematically like you should be able to
367 * combine the following lines with the ones above, but
368 * unsigned arithmetic is fun when it wraps...
370 tx_total_len += PAGE_SIZE - call->first_offset;
371 tx_total_len += call->last_to;
372 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
377 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
381 afs_wake_up_async_call :
382 afs_wake_up_call_waiter),
385 if (IS_ERR(rxcall)) {
386 ret = PTR_ERR(rxcall);
387 goto error_kill_call;
390 call->rxcall = rxcall;
392 /* send the request */
393 iov[0].iov_base = call->request;
394 iov[0].iov_len = call->request_size;
398 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
400 msg.msg_control = NULL;
401 msg.msg_controllen = 0;
402 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
404 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
405 &msg, call->request_size,
406 afs_notify_end_request_tx);
410 if (call->send_pages) {
411 ret = afs_send_pages(call, &msg);
416 /* at this point, an async call may no longer exist as it may have
417 * already completed */
421 return afs_wait_for_call_to_complete(call, ac);
424 call->state = AFS_CALL_COMPLETE;
425 if (ret != -ECONNABORTED) {
426 rxrpc_kernel_abort_call(call->net->socket, rxcall,
427 RX_USER_ABORT, ret, "KSD");
430 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
431 0, &offset, false, &call->abort_code,
433 ac->abort_code = call->abort_code;
434 ac->responded = true;
437 trace_afs_call_done(call);
441 _leave(" = %d", ret);
446 * deliver messages to a call
448 static void afs_deliver_to_call(struct afs_call *call)
450 enum afs_call_state state;
451 u32 abort_code, remote_abort = 0;
454 _enter("%s", call->type->name);
456 while (state = READ_ONCE(call->state),
457 state == AFS_CALL_CL_AWAIT_REPLY ||
458 state == AFS_CALL_SV_AWAIT_OP_ID ||
459 state == AFS_CALL_SV_AWAIT_REQUEST ||
460 state == AFS_CALL_SV_AWAIT_ACK
462 if (state == AFS_CALL_SV_AWAIT_ACK) {
464 ret = rxrpc_kernel_recv_data(call->net->socket,
466 NULL, 0, &offset, false,
469 trace_afs_recv_data(call, 0, offset, false, ret);
471 if (ret == -EINPROGRESS || ret == -EAGAIN)
473 if (ret < 0 || ret == 1) {
481 ret = call->type->deliver(call);
482 state = READ_ONCE(call->state);
485 if (state == AFS_CALL_CL_PROC_REPLY)
487 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
494 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
497 abort_code = RX_CALL_DEAD;
498 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
499 abort_code, ret, "KNC");
502 abort_code = RXGEN_OPCODE;
503 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
504 abort_code, ret, "KIV");
510 abort_code = RXGEN_CC_UNMARSHAL;
511 if (state != AFS_CALL_CL_AWAIT_REPLY)
512 abort_code = RXGEN_SS_UNMARSHAL;
513 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
514 abort_code, -EBADMSG, "KUM");
520 if (state == AFS_CALL_COMPLETE && call->incoming)
529 afs_set_call_complete(call, ret, remote_abort);
530 state = AFS_CALL_COMPLETE;
535 * wait synchronously for a call to complete
537 static long afs_wait_for_call_to_complete(struct afs_call *call,
538 struct afs_addr_cursor *ac)
540 signed long rtt2, timeout;
545 DECLARE_WAITQUEUE(myself, current);
549 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
550 rtt2 = nsecs_to_jiffies64(rtt) * 2;
555 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
557 add_wait_queue(&call->waitq, &myself);
559 set_current_state(TASK_UNINTERRUPTIBLE);
561 /* deliver any messages that are in the queue */
562 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
563 call->need_attention) {
564 call->need_attention = false;
565 __set_current_state(TASK_RUNNING);
566 afs_deliver_to_call(call);
570 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
573 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
575 life == last_life && signal_pending(current))
578 if (life != last_life) {
583 timeout = schedule_timeout(timeout);
586 remove_wait_queue(&call->waitq, &myself);
587 __set_current_state(TASK_RUNNING);
589 /* Kill off the call if it's still live. */
590 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
591 _debug("call interrupted");
592 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
593 RX_USER_ABORT, -EINTR, "KWI"))
594 afs_set_call_complete(call, -EINTR, 0);
597 spin_lock_bh(&call->state_lock);
598 ac->abort_code = call->abort_code;
599 ac->error = call->error;
600 spin_unlock_bh(&call->state_lock);
605 if (call->ret_reply0) {
606 ret = (long)call->reply[0];
607 call->reply[0] = NULL;
611 ac->responded = true;
615 _debug("call complete");
617 _leave(" = %p", (void *)ret);
622 * wake up a waiting call
624 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
625 unsigned long call_user_ID)
627 struct afs_call *call = (struct afs_call *)call_user_ID;
629 call->need_attention = true;
630 wake_up(&call->waitq);
634 * wake up an asynchronous call
636 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
637 unsigned long call_user_ID)
639 struct afs_call *call = (struct afs_call *)call_user_ID;
642 trace_afs_notify_call(rxcall, call);
643 call->need_attention = true;
645 u = __atomic_add_unless(&call->usage, 1, 0);
647 trace_afs_call(call, afs_call_trace_wake, u,
648 atomic_read(&call->net->nr_outstanding_calls),
649 __builtin_return_address(0));
651 if (!queue_work(afs_async_calls, &call->async_work))
657 * Delete an asynchronous call. The work item carries a ref to the call struct
658 * that we need to release.
660 static void afs_delete_async_call(struct work_struct *work)
662 struct afs_call *call = container_of(work, struct afs_call, async_work);
672 * Perform I/O processing on an asynchronous call. The work item carries a ref
673 * to the call struct that we either need to release or to pass on.
675 static void afs_process_async_call(struct work_struct *work)
677 struct afs_call *call = container_of(work, struct afs_call, async_work);
681 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
682 call->need_attention = false;
683 afs_deliver_to_call(call);
686 if (call->state == AFS_CALL_COMPLETE) {
687 call->reply[0] = NULL;
689 /* We have two refs to release - one from the alloc and one
690 * queued with the work item - and we can't just deallocate the
691 * call because the work item may be queued again.
693 call->async_work.func = afs_delete_async_call;
694 if (!queue_work(afs_async_calls, &call->async_work))
702 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
704 struct afs_call *call = (struct afs_call *)user_call_ID;
706 call->rxcall = rxcall;
710 * Charge the incoming call preallocation.
712 void afs_charge_preallocation(struct work_struct *work)
714 struct afs_net *net =
715 container_of(work, struct afs_net, charge_preallocation_work);
716 struct afs_call *call = net->spare_incoming_call;
720 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
725 call->state = AFS_CALL_SV_AWAIT_OP_ID;
726 init_waitqueue_head(&call->waitq);
729 if (rxrpc_kernel_charge_accept(net->socket,
730 afs_wake_up_async_call,
738 net->spare_incoming_call = call;
742 * Discard a preallocated call when a socket is shut down.
744 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
745 unsigned long user_call_ID)
747 struct afs_call *call = (struct afs_call *)user_call_ID;
754 * Notification of an incoming call.
756 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
757 unsigned long user_call_ID)
759 struct afs_net *net = afs_sock2net(sk);
761 queue_work(afs_wq, &net->charge_preallocation_work);
765 * Grab the operation ID from an incoming cache manager call. The socket
766 * buffer is discarded on error or if we don't yet have sufficient data.
768 static int afs_deliver_cm_op_id(struct afs_call *call)
772 _enter("{%zu}", call->offset);
774 ASSERTCMP(call->offset, <, 4);
776 /* the operation ID forms the first four bytes of the request data */
777 ret = afs_extract_data(call, &call->tmp, 4, true);
781 call->operation_ID = ntohl(call->tmp);
782 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
785 /* ask the cache manager to route the call (it'll change the call type
787 if (!afs_cm_incoming_call(call))
790 trace_afs_cb_call(call);
792 /* pass responsibility for the remainer of this message off to the
793 * cache manager op */
794 return call->type->deliver(call);
798 * Advance the AFS call state when an RxRPC service call ends the transmit
801 static void afs_notify_end_reply_tx(struct sock *sock,
802 struct rxrpc_call *rxcall,
803 unsigned long call_user_ID)
805 struct afs_call *call = (struct afs_call *)call_user_ID;
807 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
811 * send an empty reply
813 void afs_send_empty_reply(struct afs_call *call)
815 struct afs_net *net = call->net;
820 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
824 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
825 msg.msg_control = NULL;
826 msg.msg_controllen = 0;
829 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
830 afs_notify_end_reply_tx)) {
832 _leave(" [replied]");
837 rxrpc_kernel_abort_call(net->socket, call->rxcall,
838 RX_USER_ABORT, -ENOMEM, "KOO");
846 * send a simple reply
848 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
850 struct afs_net *net = call->net;
857 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
859 iov[0].iov_base = (void *) buf;
860 iov[0].iov_len = len;
863 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
864 msg.msg_control = NULL;
865 msg.msg_controllen = 0;
868 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
869 afs_notify_end_reply_tx);
872 _leave(" [replied]");
878 rxrpc_kernel_abort_call(net->socket, call->rxcall,
879 RX_USER_ABORT, -ENOMEM, "KOO");
885 * Extract a piece of data from the received data socket buffers.
887 int afs_extract_data(struct afs_call *call, void *buf, size_t count,
890 struct afs_net *net = call->net;
891 enum afs_call_state state;
892 u32 remote_abort = 0;
895 _enter("{%s,%zu},,%zu,%d",
896 call->type->name, call->offset, count, want_more);
898 ASSERTCMP(call->offset, <=, count);
900 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
901 buf, count, &call->offset,
902 want_more, &remote_abort,
904 trace_afs_recv_data(call, count, call->offset, want_more, ret);
905 if (ret == 0 || ret == -EAGAIN)
908 state = READ_ONCE(call->state);
911 case AFS_CALL_CL_AWAIT_REPLY:
912 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
914 case AFS_CALL_SV_AWAIT_REQUEST:
915 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
917 case AFS_CALL_COMPLETE:
918 kdebug("prem complete %d", call->error);
926 afs_set_call_complete(call, ret, remote_abort);
931 * Log protocol error production.
933 noinline int afs_protocol_error(struct afs_call *call, int error)
935 trace_afs_protocol_error(call, error, __builtin_return_address(0));