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 *);
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));
67 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
68 afs_rx_discard_new_call);
70 ret = kernel_listen(socket, INT_MAX);
75 afs_charge_preallocation(&net->charge_preallocation_work);
87 * close the RxRPC socket AFS was using
89 void afs_close_socket(struct afs_net *net)
93 kernel_listen(net->socket, 0);
94 flush_workqueue(afs_async_calls);
96 if (net->spare_incoming_call) {
97 afs_put_call(net->spare_incoming_call);
98 net->spare_incoming_call = NULL;
101 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
102 wait_on_atomic_t(&net->nr_outstanding_calls, atomic_t_wait,
103 TASK_UNINTERRUPTIBLE);
104 _debug("no outstanding calls");
106 kernel_sock_shutdown(net->socket, SHUT_RDWR);
107 flush_workqueue(afs_async_calls);
108 sock_release(net->socket);
117 static struct afs_call *afs_alloc_call(struct afs_net *net,
118 const struct afs_call_type *type,
121 struct afs_call *call;
124 call = kzalloc(sizeof(*call), gfp);
130 atomic_set(&call->usage, 1);
131 INIT_WORK(&call->async_work, afs_process_async_call);
132 init_waitqueue_head(&call->waitq);
134 o = atomic_inc_return(&net->nr_outstanding_calls);
135 trace_afs_call(call, afs_call_trace_alloc, 1, o,
136 __builtin_return_address(0));
141 * Dispose of a reference on a call.
143 void afs_put_call(struct afs_call *call)
145 struct afs_net *net = call->net;
146 int n = atomic_dec_return(&call->usage);
147 int o = atomic_read(&net->nr_outstanding_calls);
149 trace_afs_call(call, afs_call_trace_put, n + 1, o,
150 __builtin_return_address(0));
154 ASSERT(!work_pending(&call->async_work));
155 ASSERT(call->type->name != NULL);
158 rxrpc_kernel_end_call(net->socket, call->rxcall);
161 if (call->type->destructor)
162 call->type->destructor(call);
164 afs_put_server(call->net, call->cm_server);
165 kfree(call->request);
168 o = atomic_dec_return(&net->nr_outstanding_calls);
169 trace_afs_call(call, afs_call_trace_free, 0, o,
170 __builtin_return_address(0));
172 wake_up_atomic_t(&net->nr_outstanding_calls);
177 * Queue the call for actual work. Returns 0 unconditionally for convenience.
179 int afs_queue_call_work(struct afs_call *call)
181 int u = atomic_inc_return(&call->usage);
183 trace_afs_call(call, afs_call_trace_work, u,
184 atomic_read(&call->net->nr_outstanding_calls),
185 __builtin_return_address(0));
187 INIT_WORK(&call->work, call->type->work);
189 if (!queue_work(afs_wq, &call->work))
195 * allocate a call with flat request and reply buffers
197 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
198 const struct afs_call_type *type,
199 size_t request_size, size_t reply_max)
201 struct afs_call *call;
203 call = afs_alloc_call(net, type, GFP_NOFS);
208 call->request_size = request_size;
209 call->request = kmalloc(request_size, GFP_NOFS);
215 call->reply_max = reply_max;
216 call->buffer = kmalloc(reply_max, GFP_NOFS);
221 init_waitqueue_head(&call->waitq);
231 * clean up a call with flat buffer
233 void afs_flat_call_destructor(struct afs_call *call)
237 kfree(call->request);
238 call->request = NULL;
243 #define AFS_BVEC_MAX 8
246 * Load the given bvec with the next few pages.
248 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
249 struct bio_vec *bv, pgoff_t first, pgoff_t last,
252 struct page *pages[AFS_BVEC_MAX];
253 unsigned int nr, n, i, to, bytes = 0;
255 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
256 n = find_get_pages_contig(call->mapping, first, nr, pages);
257 ASSERTCMP(n, ==, nr);
259 msg->msg_flags |= MSG_MORE;
260 for (i = 0; i < nr; i++) {
262 if (first + i >= last) {
264 msg->msg_flags &= ~MSG_MORE;
266 bv[i].bv_page = pages[i];
267 bv[i].bv_len = to - offset;
268 bv[i].bv_offset = offset;
269 bytes += to - offset;
273 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
277 * Advance the AFS call state when the RxRPC call ends the transmit phase.
279 static void afs_notify_end_request_tx(struct sock *sock,
280 struct rxrpc_call *rxcall,
281 unsigned long call_user_ID)
283 struct afs_call *call = (struct afs_call *)call_user_ID;
285 if (call->state == AFS_CALL_REQUESTING)
286 call->state = AFS_CALL_AWAIT_REPLY;
290 * attach the data from a bunch of pages on an inode to a call
292 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
294 struct bio_vec bv[AFS_BVEC_MAX];
295 unsigned int bytes, nr, loop, offset;
296 pgoff_t first = call->first, last = call->last;
299 offset = call->first_offset;
300 call->first_offset = 0;
303 afs_load_bvec(call, msg, bv, first, last, offset);
305 bytes = msg->msg_iter.count;
306 nr = msg->msg_iter.nr_segs;
308 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
309 bytes, afs_notify_end_request_tx);
310 for (loop = 0; loop < nr; loop++)
311 put_page(bv[loop].bv_page);
316 } while (first <= last);
324 long afs_make_call(struct sockaddr_rxrpc *srx, struct afs_call *call,
325 gfp_t gfp, bool async)
327 struct rxrpc_call *rxcall;
335 _enter(",{%pISp},", &srx->transport);
337 ASSERT(call->type != NULL);
338 ASSERT(call->type->name != NULL);
340 _debug("____MAKE %p{%s,%x} [%d]____",
341 call, call->type->name, key_serial(call->key),
342 atomic_read(&call->net->nr_outstanding_calls));
346 /* Work out the length we're going to transmit. This is awkward for
347 * calls such as FS.StoreData where there's an extra injection of data
348 * after the initial fixed part.
350 tx_total_len = call->request_size;
351 if (call->send_pages) {
352 tx_total_len += call->last_to - call->first_offset;
353 tx_total_len += (call->last - call->first) * PAGE_SIZE;
357 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
361 afs_wake_up_async_call :
362 afs_wake_up_call_waiter),
365 if (IS_ERR(rxcall)) {
366 ret = PTR_ERR(rxcall);
367 goto error_kill_call;
370 call->rxcall = rxcall;
372 /* send the request */
373 iov[0].iov_base = call->request;
374 iov[0].iov_len = call->request_size;
378 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
380 msg.msg_control = NULL;
381 msg.msg_controllen = 0;
382 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
384 /* We have to change the state *before* sending the last packet as
385 * rxrpc might give us the reply before it returns from sending the
386 * request. Further, if the send fails, we may already have been given
387 * a notification and may have collected it.
389 if (!call->send_pages)
390 call->state = AFS_CALL_AWAIT_REPLY;
391 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
392 &msg, call->request_size,
393 afs_notify_end_request_tx);
397 if (call->send_pages) {
398 ret = afs_send_pages(call, &msg);
403 /* at this point, an async call may no longer exist as it may have
404 * already completed */
408 return afs_wait_for_call_to_complete(call);
411 call->state = AFS_CALL_COMPLETE;
412 if (ret != -ECONNABORTED) {
413 rxrpc_kernel_abort_call(call->net->socket, rxcall,
414 RX_USER_ABORT, ret, "KSD");
418 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
419 0, &offset, false, &call->abort_code,
421 ret = afs_abort_to_error(call->abort_code);
425 _leave(" = %d", ret);
430 * deliver messages to a call
432 static void afs_deliver_to_call(struct afs_call *call)
437 _enter("%s", call->type->name);
439 while (call->state == AFS_CALL_AWAIT_REPLY ||
440 call->state == AFS_CALL_AWAIT_OP_ID ||
441 call->state == AFS_CALL_AWAIT_REQUEST ||
442 call->state == AFS_CALL_AWAIT_ACK
444 if (call->state == AFS_CALL_AWAIT_ACK) {
446 ret = rxrpc_kernel_recv_data(call->net->socket,
448 NULL, 0, &offset, false,
451 trace_afs_recv_data(call, 0, offset, false, ret);
453 if (ret == -EINPROGRESS || ret == -EAGAIN)
455 if (ret == 1 || ret < 0) {
456 call->state = AFS_CALL_COMPLETE;
462 ret = call->type->deliver(call);
465 if (call->state == AFS_CALL_AWAIT_REPLY)
466 call->state = AFS_CALL_COMPLETE;
474 abort_code = RX_CALL_DEAD;
475 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
476 abort_code, ret, "KNC");
479 abort_code = RXGEN_OPCODE;
480 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
481 abort_code, ret, "KIV");
487 abort_code = RXGEN_CC_UNMARSHAL;
488 if (call->state != AFS_CALL_AWAIT_REPLY)
489 abort_code = RXGEN_SS_UNMARSHAL;
490 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
491 abort_code, -EBADMSG, "KUM");
497 if (call->state == AFS_CALL_COMPLETE && call->incoming)
505 call->state = AFS_CALL_COMPLETE;
510 * wait synchronously for a call to complete
512 static long afs_wait_for_call_to_complete(struct afs_call *call)
514 signed long rtt2, timeout;
519 DECLARE_WAITQUEUE(myself, current);
523 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
524 rtt2 = nsecs_to_jiffies64(rtt) * 2;
529 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
531 add_wait_queue(&call->waitq, &myself);
533 set_current_state(TASK_UNINTERRUPTIBLE);
535 /* deliver any messages that are in the queue */
536 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
537 call->need_attention = false;
538 __set_current_state(TASK_RUNNING);
539 afs_deliver_to_call(call);
543 if (call->state == AFS_CALL_COMPLETE)
546 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
548 life == last_life && signal_pending(current))
551 if (life != last_life) {
556 timeout = schedule_timeout(timeout);
559 remove_wait_queue(&call->waitq, &myself);
560 __set_current_state(TASK_RUNNING);
562 /* Kill off the call if it's still live. */
563 if (call->state < AFS_CALL_COMPLETE) {
564 _debug("call interrupted");
565 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
566 RX_USER_ABORT, -EINTR, "KWI");
571 ret = afs_abort_to_error(call->abort_code);
572 } else if (ret == 0 && call->ret_reply0) {
573 ret = (long)call->reply[0];
574 call->reply[0] = NULL;
577 _debug("call complete");
579 _leave(" = %p", (void *)ret);
584 * wake up a waiting call
586 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
587 unsigned long call_user_ID)
589 struct afs_call *call = (struct afs_call *)call_user_ID;
591 call->need_attention = true;
592 wake_up(&call->waitq);
596 * wake up an asynchronous call
598 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
599 unsigned long call_user_ID)
601 struct afs_call *call = (struct afs_call *)call_user_ID;
604 trace_afs_notify_call(rxcall, call);
605 call->need_attention = true;
607 u = __atomic_add_unless(&call->usage, 1, 0);
609 trace_afs_call(call, afs_call_trace_wake, u,
610 atomic_read(&call->net->nr_outstanding_calls),
611 __builtin_return_address(0));
613 if (!queue_work(afs_async_calls, &call->async_work))
619 * Delete an asynchronous call. The work item carries a ref to the call struct
620 * that we need to release.
622 static void afs_delete_async_call(struct work_struct *work)
624 struct afs_call *call = container_of(work, struct afs_call, async_work);
634 * Perform I/O processing on an asynchronous call. The work item carries a ref
635 * to the call struct that we either need to release or to pass on.
637 static void afs_process_async_call(struct work_struct *work)
639 struct afs_call *call = container_of(work, struct afs_call, async_work);
643 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
644 call->need_attention = false;
645 afs_deliver_to_call(call);
648 if (call->state == AFS_CALL_COMPLETE) {
649 call->reply[0] = NULL;
651 /* We have two refs to release - one from the alloc and one
652 * queued with the work item - and we can't just deallocate the
653 * call because the work item may be queued again.
655 call->async_work.func = afs_delete_async_call;
656 if (!queue_work(afs_async_calls, &call->async_work))
664 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
666 struct afs_call *call = (struct afs_call *)user_call_ID;
668 call->rxcall = rxcall;
672 * Charge the incoming call preallocation.
674 void afs_charge_preallocation(struct work_struct *work)
676 struct afs_net *net =
677 container_of(work, struct afs_net, charge_preallocation_work);
678 struct afs_call *call = net->spare_incoming_call;
682 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
687 call->state = AFS_CALL_AWAIT_OP_ID;
688 init_waitqueue_head(&call->waitq);
691 if (rxrpc_kernel_charge_accept(net->socket,
692 afs_wake_up_async_call,
699 net->spare_incoming_call = call;
703 * Discard a preallocated call when a socket is shut down.
705 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
706 unsigned long user_call_ID)
708 struct afs_call *call = (struct afs_call *)user_call_ID;
715 * Notification of an incoming call.
717 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
718 unsigned long user_call_ID)
720 struct afs_net *net = afs_sock2net(sk);
722 queue_work(afs_wq, &net->charge_preallocation_work);
726 * Grab the operation ID from an incoming cache manager call. The socket
727 * buffer is discarded on error or if we don't yet have sufficient data.
729 static int afs_deliver_cm_op_id(struct afs_call *call)
733 _enter("{%zu}", call->offset);
735 ASSERTCMP(call->offset, <, 4);
737 /* the operation ID forms the first four bytes of the request data */
738 ret = afs_extract_data(call, &call->tmp, 4, true);
742 call->operation_ID = ntohl(call->tmp);
743 call->state = AFS_CALL_AWAIT_REQUEST;
746 /* ask the cache manager to route the call (it'll change the call type
748 if (!afs_cm_incoming_call(call))
751 trace_afs_cb_call(call);
753 /* pass responsibility for the remainer of this message off to the
754 * cache manager op */
755 return call->type->deliver(call);
759 * Advance the AFS call state when an RxRPC service call ends the transmit
762 static void afs_notify_end_reply_tx(struct sock *sock,
763 struct rxrpc_call *rxcall,
764 unsigned long call_user_ID)
766 struct afs_call *call = (struct afs_call *)call_user_ID;
768 if (call->state == AFS_CALL_REPLYING)
769 call->state = AFS_CALL_AWAIT_ACK;
773 * send an empty reply
775 void afs_send_empty_reply(struct afs_call *call)
777 struct afs_net *net = call->net;
782 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
786 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
787 msg.msg_control = NULL;
788 msg.msg_controllen = 0;
791 call->state = AFS_CALL_AWAIT_ACK;
792 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
793 afs_notify_end_reply_tx)) {
795 _leave(" [replied]");
800 rxrpc_kernel_abort_call(net->socket, call->rxcall,
801 RX_USER_ABORT, -ENOMEM, "KOO");
809 * send a simple reply
811 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
813 struct afs_net *net = call->net;
820 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
822 iov[0].iov_base = (void *) buf;
823 iov[0].iov_len = len;
826 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
827 msg.msg_control = NULL;
828 msg.msg_controllen = 0;
831 call->state = AFS_CALL_AWAIT_ACK;
832 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
833 afs_notify_end_reply_tx);
836 _leave(" [replied]");
842 rxrpc_kernel_abort_call(net->socket, call->rxcall,
843 RX_USER_ABORT, -ENOMEM, "KOO");
849 * Extract a piece of data from the received data socket buffers.
851 int afs_extract_data(struct afs_call *call, void *buf, size_t count,
854 struct afs_net *net = call->net;
857 _enter("{%s,%zu},,%zu,%d",
858 call->type->name, call->offset, count, want_more);
860 ASSERTCMP(call->offset, <=, count);
862 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
863 buf, count, &call->offset,
864 want_more, &call->abort_code,
866 trace_afs_recv_data(call, count, call->offset, want_more, ret);
867 if (ret == 0 || ret == -EAGAIN)
871 switch (call->state) {
872 case AFS_CALL_AWAIT_REPLY:
873 call->state = AFS_CALL_COMPLETE;
875 case AFS_CALL_AWAIT_REQUEST:
876 call->state = AFS_CALL_REPLYING;
884 if (ret == -ECONNABORTED)
885 call->error = afs_abort_to_error(call->abort_code);
888 call->state = AFS_CALL_COMPLETE;