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 socket *afs_socket; /* my RxRPC socket */
21 static struct workqueue_struct *afs_async_calls;
22 static struct afs_call *afs_spare_incoming_call;
23 atomic_t afs_outstanding_calls;
25 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
26 static int afs_wait_for_call_to_complete(struct afs_call *);
27 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
28 static void afs_process_async_call(struct work_struct *);
29 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
30 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
31 static int afs_deliver_cm_op_id(struct afs_call *);
33 /* asynchronous incoming call initial processing */
34 static const struct afs_call_type afs_RXCMxxxx = {
36 .deliver = afs_deliver_cm_op_id,
37 .abort_to_error = afs_abort_to_error,
40 static void afs_charge_preallocation(struct work_struct *);
42 static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
44 static int afs_wait_atomic_t(atomic_t *p)
51 * open an RxRPC socket and bind it to be a server for callback notifications
52 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
54 int afs_open_socket(void)
56 struct sockaddr_rxrpc srx;
57 struct socket *socket;
63 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
67 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
71 socket->sk->sk_allocation = GFP_NOFS;
73 /* bind the callback manager's address to make this a server socket */
74 srx.srx_family = AF_RXRPC;
75 srx.srx_service = CM_SERVICE;
76 srx.transport_type = SOCK_DGRAM;
77 srx.transport_len = sizeof(srx.transport.sin);
78 srx.transport.sin.sin_family = AF_INET;
79 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
80 memset(&srx.transport.sin.sin_addr, 0,
81 sizeof(srx.transport.sin.sin_addr));
83 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
87 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
88 afs_rx_discard_new_call);
90 ret = kernel_listen(socket, INT_MAX);
95 afs_charge_preallocation(NULL);
100 sock_release(socket);
102 destroy_workqueue(afs_async_calls);
104 _leave(" = %d", ret);
109 * close the RxRPC socket AFS was using
111 void afs_close_socket(void)
115 kernel_listen(afs_socket, 0);
116 flush_workqueue(afs_async_calls);
118 if (afs_spare_incoming_call) {
119 afs_put_call(afs_spare_incoming_call);
120 afs_spare_incoming_call = NULL;
123 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
124 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
125 TASK_UNINTERRUPTIBLE);
126 _debug("no outstanding calls");
128 kernel_sock_shutdown(afs_socket, SHUT_RDWR);
129 flush_workqueue(afs_async_calls);
130 sock_release(afs_socket);
133 destroy_workqueue(afs_async_calls);
140 static struct afs_call *afs_alloc_call(const struct afs_call_type *type,
143 struct afs_call *call;
146 call = kzalloc(sizeof(*call), gfp);
151 atomic_set(&call->usage, 1);
152 INIT_WORK(&call->async_work, afs_process_async_call);
153 init_waitqueue_head(&call->waitq);
155 o = atomic_inc_return(&afs_outstanding_calls);
156 trace_afs_call(call, afs_call_trace_alloc, 1, o,
157 __builtin_return_address(0));
162 * Dispose of a reference on a call.
164 void afs_put_call(struct afs_call *call)
166 int n = atomic_dec_return(&call->usage);
167 int o = atomic_read(&afs_outstanding_calls);
169 trace_afs_call(call, afs_call_trace_put, n + 1, o,
170 __builtin_return_address(0));
174 ASSERT(!work_pending(&call->async_work));
175 ASSERT(call->type->name != NULL);
178 rxrpc_kernel_end_call(afs_socket, call->rxcall);
181 if (call->type->destructor)
182 call->type->destructor(call);
184 kfree(call->request);
187 o = atomic_dec_return(&afs_outstanding_calls);
188 trace_afs_call(call, afs_call_trace_free, 0, o,
189 __builtin_return_address(0));
191 wake_up_atomic_t(&afs_outstanding_calls);
196 * Queue the call for actual work. Returns 0 unconditionally for convenience.
198 int afs_queue_call_work(struct afs_call *call)
200 int u = atomic_inc_return(&call->usage);
202 trace_afs_call(call, afs_call_trace_work, u,
203 atomic_read(&afs_outstanding_calls),
204 __builtin_return_address(0));
206 INIT_WORK(&call->work, call->type->work);
208 if (!queue_work(afs_wq, &call->work))
214 * allocate a call with flat request and reply buffers
216 struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
217 size_t request_size, size_t reply_max)
219 struct afs_call *call;
221 call = afs_alloc_call(type, GFP_NOFS);
226 call->request_size = request_size;
227 call->request = kmalloc(request_size, GFP_NOFS);
233 call->reply_max = reply_max;
234 call->buffer = kmalloc(reply_max, GFP_NOFS);
239 init_waitqueue_head(&call->waitq);
249 * clean up a call with flat buffer
251 void afs_flat_call_destructor(struct afs_call *call)
255 kfree(call->request);
256 call->request = NULL;
261 #define AFS_BVEC_MAX 8
264 * Load the given bvec with the next few pages.
266 static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
267 struct bio_vec *bv, pgoff_t first, pgoff_t last,
270 struct page *pages[AFS_BVEC_MAX];
271 unsigned int nr, n, i, to, bytes = 0;
273 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
274 n = find_get_pages_contig(call->mapping, first, nr, pages);
275 ASSERTCMP(n, ==, nr);
277 msg->msg_flags |= MSG_MORE;
278 for (i = 0; i < nr; i++) {
280 if (first + i >= last) {
282 msg->msg_flags &= ~MSG_MORE;
284 bv[i].bv_page = pages[i];
285 bv[i].bv_len = to - offset;
286 bv[i].bv_offset = offset;
287 bytes += to - offset;
291 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
295 * attach the data from a bunch of pages on an inode to a call
297 static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
299 struct bio_vec bv[AFS_BVEC_MAX];
300 unsigned int bytes, nr, loop, offset;
301 pgoff_t first = call->first, last = call->last;
304 offset = call->first_offset;
305 call->first_offset = 0;
308 afs_load_bvec(call, msg, bv, first, last, offset);
310 bytes = msg->msg_iter.count;
311 nr = msg->msg_iter.nr_segs;
313 /* Have to change the state *before* sending the last
314 * packet as RxRPC might give us the reply before it
315 * returns from sending the request.
317 if (first + nr - 1 >= last)
318 call->state = AFS_CALL_AWAIT_REPLY;
319 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
321 for (loop = 0; loop < nr; loop++)
322 put_page(bv[loop].bv_page);
327 } while (first <= last);
335 int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
338 struct sockaddr_rxrpc srx;
339 struct rxrpc_call *rxcall;
347 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
349 ASSERT(call->type != NULL);
350 ASSERT(call->type->name != NULL);
352 _debug("____MAKE %p{%s,%x} [%d]____",
353 call, call->type->name, key_serial(call->key),
354 atomic_read(&afs_outstanding_calls));
358 memset(&srx, 0, sizeof(srx));
359 srx.srx_family = AF_RXRPC;
360 srx.srx_service = call->service_id;
361 srx.transport_type = SOCK_DGRAM;
362 srx.transport_len = sizeof(srx.transport.sin);
363 srx.transport.sin.sin_family = AF_INET;
364 srx.transport.sin.sin_port = call->port;
365 memcpy(&srx.transport.sin.sin_addr, addr, 4);
367 /* Work out the length we're going to transmit. This is awkward for
368 * calls such as FS.StoreData where there's an extra injection of data
369 * after the initial fixed part.
371 tx_total_len = call->request_size;
372 if (call->send_pages) {
373 tx_total_len += call->last_to - call->first_offset;
374 tx_total_len += (call->last - call->first) * PAGE_SIZE;
378 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
382 afs_wake_up_async_call :
383 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 = (call->send_pages ? MSG_MORE : 0);
404 /* We have to change the state *before* sending the last packet as
405 * rxrpc might give us the reply before it returns from sending the
406 * request. Further, if the send fails, we may already have been given
407 * a notification and may have collected it.
409 if (!call->send_pages)
410 call->state = AFS_CALL_AWAIT_REPLY;
411 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
412 &msg, call->request_size);
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);
430 call->state = AFS_CALL_COMPLETE;
431 if (ret != -ECONNABORTED) {
432 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT,
437 rxrpc_kernel_recv_data(afs_socket, rxcall, NULL, 0, &offset,
439 ret = call->type->abort_to_error(abort_code);
443 _leave(" = %d", ret);
448 * deliver messages to a call
450 static void afs_deliver_to_call(struct afs_call *call)
455 _enter("%s", call->type->name);
457 while (call->state == AFS_CALL_AWAIT_REPLY ||
458 call->state == AFS_CALL_AWAIT_OP_ID ||
459 call->state == AFS_CALL_AWAIT_REQUEST ||
460 call->state == AFS_CALL_AWAIT_ACK
462 if (call->state == AFS_CALL_AWAIT_ACK) {
464 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
465 NULL, 0, &offset, false,
467 trace_afs_recv_data(call, 0, offset, false, ret);
469 if (ret == -EINPROGRESS || ret == -EAGAIN)
471 if (ret == 1 || ret < 0) {
472 call->state = AFS_CALL_COMPLETE;
478 ret = call->type->deliver(call);
481 if (call->state == AFS_CALL_AWAIT_REPLY)
482 call->state = AFS_CALL_COMPLETE;
490 abort_code = RX_CALL_DEAD;
491 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
492 abort_code, ret, "KNC");
495 abort_code = RXGEN_OPCODE;
496 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
497 abort_code, ret, "KIV");
503 abort_code = RXGEN_CC_UNMARSHAL;
504 if (call->state != AFS_CALL_AWAIT_REPLY)
505 abort_code = RXGEN_SS_UNMARSHAL;
506 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
507 abort_code, -EBADMSG, "KUM");
513 if (call->state == AFS_CALL_COMPLETE && call->incoming)
522 call->state = AFS_CALL_COMPLETE;
527 * wait synchronously for a call to complete
529 static int afs_wait_for_call_to_complete(struct afs_call *call)
533 DECLARE_WAITQUEUE(myself, current);
537 add_wait_queue(&call->waitq, &myself);
539 set_current_state(TASK_INTERRUPTIBLE);
541 /* deliver any messages that are in the queue */
542 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
543 call->need_attention = false;
544 __set_current_state(TASK_RUNNING);
545 afs_deliver_to_call(call);
549 if (call->state == AFS_CALL_COMPLETE ||
550 signal_pending(current))
555 remove_wait_queue(&call->waitq, &myself);
556 __set_current_state(TASK_RUNNING);
558 /* Kill off the call if it's still live. */
559 if (call->state < AFS_CALL_COMPLETE) {
560 _debug("call interrupted");
561 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
562 RX_USER_ABORT, -EINTR, "KWI");
566 _debug("call complete");
568 _leave(" = %d", ret);
573 * wake up a waiting call
575 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
576 unsigned long call_user_ID)
578 struct afs_call *call = (struct afs_call *)call_user_ID;
580 call->need_attention = true;
581 wake_up(&call->waitq);
585 * wake up an asynchronous call
587 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
588 unsigned long call_user_ID)
590 struct afs_call *call = (struct afs_call *)call_user_ID;
593 trace_afs_notify_call(rxcall, call);
594 call->need_attention = true;
596 u = __atomic_add_unless(&call->usage, 1, 0);
598 trace_afs_call(call, afs_call_trace_wake, u,
599 atomic_read(&afs_outstanding_calls),
600 __builtin_return_address(0));
602 if (!queue_work(afs_async_calls, &call->async_work))
608 * Delete an asynchronous call. The work item carries a ref to the call struct
609 * that we need to release.
611 static void afs_delete_async_call(struct work_struct *work)
613 struct afs_call *call = container_of(work, struct afs_call, async_work);
623 * Perform I/O processing on an asynchronous call. The work item carries a ref
624 * to the call struct that we either need to release or to pass on.
626 static void afs_process_async_call(struct work_struct *work)
628 struct afs_call *call = container_of(work, struct afs_call, async_work);
632 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
633 call->need_attention = false;
634 afs_deliver_to_call(call);
637 if (call->state == AFS_CALL_COMPLETE) {
640 /* We have two refs to release - one from the alloc and one
641 * queued with the work item - and we can't just deallocate the
642 * call because the work item may be queued again.
644 call->async_work.func = afs_delete_async_call;
645 if (!queue_work(afs_async_calls, &call->async_work))
653 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
655 struct afs_call *call = (struct afs_call *)user_call_ID;
657 call->rxcall = rxcall;
661 * Charge the incoming call preallocation.
663 static void afs_charge_preallocation(struct work_struct *work)
665 struct afs_call *call = afs_spare_incoming_call;
669 call = afs_alloc_call(&afs_RXCMxxxx, GFP_KERNEL);
674 call->state = AFS_CALL_AWAIT_OP_ID;
675 init_waitqueue_head(&call->waitq);
678 if (rxrpc_kernel_charge_accept(afs_socket,
679 afs_wake_up_async_call,
686 afs_spare_incoming_call = call;
690 * Discard a preallocated call when a socket is shut down.
692 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
693 unsigned long user_call_ID)
695 struct afs_call *call = (struct afs_call *)user_call_ID;
702 * Notification of an incoming call.
704 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
705 unsigned long user_call_ID)
707 queue_work(afs_wq, &afs_charge_preallocation_work);
711 * Grab the operation ID from an incoming cache manager call. The socket
712 * buffer is discarded on error or if we don't yet have sufficient data.
714 static int afs_deliver_cm_op_id(struct afs_call *call)
718 _enter("{%zu}", call->offset);
720 ASSERTCMP(call->offset, <, 4);
722 /* the operation ID forms the first four bytes of the request data */
723 ret = afs_extract_data(call, &call->tmp, 4, true);
727 call->operation_ID = ntohl(call->tmp);
728 call->state = AFS_CALL_AWAIT_REQUEST;
731 /* ask the cache manager to route the call (it'll change the call type
733 if (!afs_cm_incoming_call(call))
736 trace_afs_cb_call(call);
738 /* pass responsibility for the remainer of this message off to the
739 * cache manager op */
740 return call->type->deliver(call);
744 * send an empty reply
746 void afs_send_empty_reply(struct afs_call *call)
752 rxrpc_kernel_set_tx_length(afs_socket, call->rxcall, 0);
756 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
757 msg.msg_control = NULL;
758 msg.msg_controllen = 0;
761 call->state = AFS_CALL_AWAIT_ACK;
762 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
764 _leave(" [replied]");
769 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
770 RX_USER_ABORT, -ENOMEM, "KOO");
778 * send a simple reply
780 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
788 rxrpc_kernel_set_tx_length(afs_socket, call->rxcall, len);
790 iov[0].iov_base = (void *) buf;
791 iov[0].iov_len = len;
794 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
795 msg.msg_control = NULL;
796 msg.msg_controllen = 0;
799 call->state = AFS_CALL_AWAIT_ACK;
800 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
803 _leave(" [replied]");
809 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
810 RX_USER_ABORT, -ENOMEM, "KOO");
816 * Extract a piece of data from the received data socket buffers.
818 int afs_extract_data(struct afs_call *call, void *buf, size_t count,
823 _enter("{%s,%zu},,%zu,%d",
824 call->type->name, call->offset, count, want_more);
826 ASSERTCMP(call->offset, <=, count);
828 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
829 buf, count, &call->offset,
830 want_more, &call->abort_code);
831 trace_afs_recv_data(call, count, call->offset, want_more, ret);
832 if (ret == 0 || ret == -EAGAIN)
836 switch (call->state) {
837 case AFS_CALL_AWAIT_REPLY:
838 call->state = AFS_CALL_COMPLETE;
840 case AFS_CALL_AWAIT_REQUEST:
841 call->state = AFS_CALL_REPLYING;
849 if (ret == -ECONNABORTED)
850 call->error = call->type->abort_to_error(call->abort_code);
853 call->state = AFS_CALL_COMPLETE;