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>
14 #include <net/af_rxrpc.h>
15 #include <rxrpc/packet.h>
19 static struct socket *afs_socket; /* my RxRPC socket */
20 static struct workqueue_struct *afs_async_calls;
21 static atomic_t afs_outstanding_calls;
22 static atomic_t afs_outstanding_skbs;
24 static void afs_wake_up_call_waiter(struct afs_call *);
25 static int afs_wait_for_call_to_complete(struct afs_call *);
26 static void afs_wake_up_async_call(struct afs_call *);
27 static int afs_dont_wait_for_call_to_complete(struct afs_call *);
28 static void afs_process_async_call(struct afs_call *);
29 static void afs_rx_interceptor(struct sock *, unsigned long, struct sk_buff *);
30 static int afs_deliver_cm_op_id(struct afs_call *, struct sk_buff *, bool);
32 /* synchronous call management */
33 const struct afs_wait_mode afs_sync_call = {
34 .rx_wakeup = afs_wake_up_call_waiter,
35 .wait = afs_wait_for_call_to_complete,
38 /* asynchronous call management */
39 const struct afs_wait_mode afs_async_call = {
40 .rx_wakeup = afs_wake_up_async_call,
41 .wait = afs_dont_wait_for_call_to_complete,
44 /* asynchronous incoming call management */
45 static const struct afs_wait_mode afs_async_incoming_call = {
46 .rx_wakeup = afs_wake_up_async_call,
49 /* asynchronous incoming call initial processing */
50 static const struct afs_call_type afs_RXCMxxxx = {
52 .deliver = afs_deliver_cm_op_id,
53 .abort_to_error = afs_abort_to_error,
56 static void afs_collect_incoming_call(struct work_struct *);
58 static struct sk_buff_head afs_incoming_calls;
59 static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
61 static void afs_async_workfn(struct work_struct *work)
63 struct afs_call *call = container_of(work, struct afs_call, async_work);
65 call->async_workfn(call);
68 static int afs_wait_atomic_t(atomic_t *p)
75 * open an RxRPC socket and bind it to be a server for callback notifications
76 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
78 int afs_open_socket(void)
80 struct sockaddr_rxrpc srx;
81 struct socket *socket;
86 skb_queue_head_init(&afs_incoming_calls);
88 afs_async_calls = create_singlethread_workqueue("kafsd");
89 if (!afs_async_calls) {
90 _leave(" = -ENOMEM [wq]");
94 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
96 destroy_workqueue(afs_async_calls);
97 _leave(" = %d [socket]", ret);
101 socket->sk->sk_allocation = GFP_NOFS;
103 /* bind the callback manager's address to make this a server socket */
104 srx.srx_family = AF_RXRPC;
105 srx.srx_service = CM_SERVICE;
106 srx.transport_type = SOCK_DGRAM;
107 srx.transport_len = sizeof(srx.transport.sin);
108 srx.transport.sin.sin_family = AF_INET;
109 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
110 memset(&srx.transport.sin.sin_addr, 0,
111 sizeof(srx.transport.sin.sin_addr));
113 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
115 sock_release(socket);
116 destroy_workqueue(afs_async_calls);
117 _leave(" = %d [bind]", ret);
121 rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor);
129 * close the RxRPC socket AFS was using
131 void afs_close_socket(void)
135 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
136 TASK_UNINTERRUPTIBLE);
137 _debug("no outstanding calls");
139 sock_release(afs_socket);
142 destroy_workqueue(afs_async_calls);
144 ASSERTCMP(atomic_read(&afs_outstanding_skbs), ==, 0);
149 * note that the data in a socket buffer is now delivered and that the buffer
152 static void afs_data_delivered(struct sk_buff *skb)
155 _debug("DLVR NULL [%d]", atomic_read(&afs_outstanding_skbs));
158 _debug("DLVR %p{%u} [%d]",
159 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
160 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
162 rxrpc_kernel_data_delivered(skb);
167 * free a socket buffer
169 static void afs_free_skb(struct sk_buff *skb)
172 _debug("FREE NULL [%d]", atomic_read(&afs_outstanding_skbs));
175 _debug("FREE %p{%u} [%d]",
176 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
177 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
179 rxrpc_kernel_free_skb(skb);
186 static void afs_free_call(struct afs_call *call)
188 _debug("DONE %p{%s} [%d]",
189 call, call->type->name, atomic_read(&afs_outstanding_calls));
191 ASSERTCMP(call->rxcall, ==, NULL);
192 ASSERT(!work_pending(&call->async_work));
193 ASSERT(skb_queue_empty(&call->rx_queue));
194 ASSERT(call->type->name != NULL);
196 kfree(call->request);
199 if (atomic_dec_and_test(&afs_outstanding_calls))
200 wake_up_atomic_t(&afs_outstanding_calls);
204 * End a call but do not free it
206 static void afs_end_call_nofree(struct afs_call *call)
209 rxrpc_kernel_end_call(call->rxcall);
212 if (call->type->destructor)
213 call->type->destructor(call);
217 * End a call and free it
219 static void afs_end_call(struct afs_call *call)
221 afs_end_call_nofree(call);
226 * allocate a call with flat request and reply buffers
228 struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
229 size_t request_size, size_t reply_size)
231 struct afs_call *call;
233 call = kzalloc(sizeof(*call), GFP_NOFS);
237 _debug("CALL %p{%s} [%d]",
238 call, type->name, atomic_read(&afs_outstanding_calls));
239 atomic_inc(&afs_outstanding_calls);
242 call->request_size = request_size;
243 call->reply_max = reply_size;
246 call->request = kmalloc(request_size, GFP_NOFS);
252 call->buffer = kmalloc(reply_size, GFP_NOFS);
257 init_waitqueue_head(&call->waitq);
258 skb_queue_head_init(&call->rx_queue);
268 * clean up a call with flat buffer
270 void afs_flat_call_destructor(struct afs_call *call)
274 kfree(call->request);
275 call->request = NULL;
281 * attach the data from a bunch of pages on an inode to a call
283 static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
286 struct page *pages[8];
287 unsigned count, n, loop, offset, to;
288 pgoff_t first = call->first, last = call->last;
293 offset = call->first_offset;
294 call->first_offset = 0;
297 _debug("attach %lx-%lx", first, last);
299 count = last - first + 1;
300 if (count > ARRAY_SIZE(pages))
301 count = ARRAY_SIZE(pages);
302 n = find_get_pages_contig(call->mapping, first, count, pages);
303 ASSERTCMP(n, ==, count);
309 if (first + loop >= last)
312 msg->msg_flags = MSG_MORE;
313 iov->iov_base = kmap(pages[loop]) + offset;
314 iov->iov_len = to - offset;
317 _debug("- range %u-%u%s",
318 offset, to, msg->msg_flags ? " [more]" : "");
319 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
320 iov, 1, to - offset);
322 /* have to change the state *before* sending the last
323 * packet as RxRPC might give us the reply before it
324 * returns from sending the request */
325 if (first + loop >= last)
326 call->state = AFS_CALL_AWAIT_REPLY;
327 ret = rxrpc_kernel_send_data(call->rxcall, msg,
332 } while (++loop < count);
335 for (loop = 0; loop < count; loop++)
336 put_page(pages[loop]);
339 } while (first <= last);
341 _leave(" = %d", ret);
348 int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
349 const struct afs_wait_mode *wait_mode)
351 struct sockaddr_rxrpc srx;
352 struct rxrpc_call *rxcall;
358 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
360 ASSERT(call->type != NULL);
361 ASSERT(call->type->name != NULL);
363 _debug("____MAKE %p{%s,%x} [%d]____",
364 call, call->type->name, key_serial(call->key),
365 atomic_read(&afs_outstanding_calls));
367 call->wait_mode = wait_mode;
368 call->async_workfn = afs_process_async_call;
369 INIT_WORK(&call->async_work, afs_async_workfn);
371 memset(&srx, 0, sizeof(srx));
372 srx.srx_family = AF_RXRPC;
373 srx.srx_service = call->service_id;
374 srx.transport_type = SOCK_DGRAM;
375 srx.transport_len = sizeof(srx.transport.sin);
376 srx.transport.sin.sin_family = AF_INET;
377 srx.transport.sin.sin_port = call->port;
378 memcpy(&srx.transport.sin.sin_addr, addr, 4);
381 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
382 (unsigned long) call, gfp);
384 if (IS_ERR(rxcall)) {
385 ret = PTR_ERR(rxcall);
386 goto error_kill_call;
389 call->rxcall = rxcall;
391 /* send the request */
392 iov[0].iov_base = call->request;
393 iov[0].iov_len = call->request_size;
397 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
399 msg.msg_control = NULL;
400 msg.msg_controllen = 0;
401 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
403 /* have to change the state *before* sending the last packet as RxRPC
404 * might give us the reply before it returns from sending the
406 if (!call->send_pages)
407 call->state = AFS_CALL_AWAIT_REPLY;
408 ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
412 if (call->send_pages) {
413 ret = afs_send_pages(call, &msg, iov);
418 /* at this point, an async call may no longer exist as it may have
419 * already completed */
420 return wait_mode->wait(call);
423 rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
424 while ((skb = skb_dequeue(&call->rx_queue)))
428 _leave(" = %d", ret);
433 * Handles intercepted messages that were arriving in the socket's Rx queue.
435 * Called from the AF_RXRPC call processor in waitqueue process context. For
436 * each call, it is guaranteed this will be called in order of packet to be
439 static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
442 struct afs_call *call = (struct afs_call *) user_call_ID;
444 _enter("%p,,%u", call, skb->mark);
446 _debug("ICPT %p{%u} [%d]",
447 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
449 ASSERTCMP(sk, ==, afs_socket->sk);
450 atomic_inc(&afs_outstanding_skbs);
453 /* its an incoming call for our callback service */
454 skb_queue_tail(&afs_incoming_calls, skb);
455 queue_work(afs_wq, &afs_collect_incoming_call_work);
457 /* route the messages directly to the appropriate call */
458 skb_queue_tail(&call->rx_queue, skb);
459 call->wait_mode->rx_wakeup(call);
466 * deliver messages to a call
468 static void afs_deliver_to_call(struct afs_call *call)
477 while ((call->state == AFS_CALL_AWAIT_REPLY ||
478 call->state == AFS_CALL_AWAIT_OP_ID ||
479 call->state == AFS_CALL_AWAIT_REQUEST ||
480 call->state == AFS_CALL_AWAIT_ACK) &&
481 (skb = skb_dequeue(&call->rx_queue))) {
483 case RXRPC_SKB_MARK_DATA:
485 last = rxrpc_kernel_is_data_last(skb);
486 ret = call->type->deliver(call, skb, last);
490 call->state == AFS_CALL_AWAIT_REPLY)
491 call->state = AFS_CALL_COMPLETE;
494 abort_code = RX_CALL_DEAD;
497 abort_code = RX_INVALID_OPERATION;
500 abort_code = RXGEN_CC_UNMARSHAL;
501 if (call->state != AFS_CALL_AWAIT_REPLY)
502 abort_code = RXGEN_SS_UNMARSHAL;
504 rxrpc_kernel_abort_call(call->rxcall,
507 call->state = AFS_CALL_ERROR;
510 afs_data_delivered(skb);
513 case RXRPC_SKB_MARK_FINAL_ACK:
515 call->state = AFS_CALL_COMPLETE;
517 case RXRPC_SKB_MARK_BUSY:
519 call->error = -EBUSY;
520 call->state = AFS_CALL_BUSY;
522 case RXRPC_SKB_MARK_REMOTE_ABORT:
523 abort_code = rxrpc_kernel_get_abort_code(skb);
524 call->error = call->type->abort_to_error(abort_code);
525 call->state = AFS_CALL_ABORTED;
526 _debug("Rcv ABORT %u -> %d", abort_code, call->error);
528 case RXRPC_SKB_MARK_LOCAL_ABORT:
529 abort_code = rxrpc_kernel_get_abort_code(skb);
530 call->error = call->type->abort_to_error(abort_code);
531 call->state = AFS_CALL_ABORTED;
532 _debug("Loc ABORT %u -> %d", abort_code, call->error);
534 case RXRPC_SKB_MARK_NET_ERROR:
535 call->error = -rxrpc_kernel_get_error_number(skb);
536 call->state = AFS_CALL_ERROR;
537 _debug("Rcv NET ERROR %d", call->error);
539 case RXRPC_SKB_MARK_LOCAL_ERROR:
540 call->error = -rxrpc_kernel_get_error_number(skb);
541 call->state = AFS_CALL_ERROR;
542 _debug("Rcv LOCAL ERROR %d", call->error);
552 /* make sure the queue is empty if the call is done with (we might have
553 * aborted the call early because of an unmarshalling error) */
554 if (call->state >= AFS_CALL_COMPLETE) {
555 while ((skb = skb_dequeue(&call->rx_queue)))
565 * wait synchronously for a call to complete
567 static int afs_wait_for_call_to_complete(struct afs_call *call)
572 DECLARE_WAITQUEUE(myself, current);
576 add_wait_queue(&call->waitq, &myself);
578 set_current_state(TASK_INTERRUPTIBLE);
580 /* deliver any messages that are in the queue */
581 if (!skb_queue_empty(&call->rx_queue)) {
582 __set_current_state(TASK_RUNNING);
583 afs_deliver_to_call(call);
588 if (call->state >= AFS_CALL_COMPLETE)
591 if (signal_pending(current))
596 remove_wait_queue(&call->waitq, &myself);
597 __set_current_state(TASK_RUNNING);
600 if (call->state < AFS_CALL_COMPLETE) {
601 _debug("call incomplete");
602 rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
603 while ((skb = skb_dequeue(&call->rx_queue)))
607 _debug("call complete");
609 _leave(" = %d", ret);
614 * wake up a waiting call
616 static void afs_wake_up_call_waiter(struct afs_call *call)
618 wake_up(&call->waitq);
622 * wake up an asynchronous call
624 static void afs_wake_up_async_call(struct afs_call *call)
627 queue_work(afs_async_calls, &call->async_work);
631 * put a call into asynchronous mode
632 * - mustn't touch the call descriptor as the call my have completed by the
635 static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
642 * delete an asynchronous call
644 static void afs_delete_async_call(struct afs_call *call)
654 * perform processing on an asynchronous call
655 * - on a multiple-thread workqueue this work item may try to run on several
656 * CPUs at the same time
658 static void afs_process_async_call(struct afs_call *call)
662 if (!skb_queue_empty(&call->rx_queue))
663 afs_deliver_to_call(call);
665 if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
666 if (call->wait_mode->async_complete)
667 call->wait_mode->async_complete(call->reply,
672 afs_end_call_nofree(call);
674 /* we can't just delete the call because the work item may be
676 call->async_workfn = afs_delete_async_call;
677 queue_work(afs_async_calls, &call->async_work);
684 * empty a socket buffer into a flat reply buffer
686 void afs_transfer_reply(struct afs_call *call, struct sk_buff *skb)
688 size_t len = skb->len;
690 if (skb_copy_bits(skb, 0, call->buffer + call->reply_size, len) < 0)
692 call->reply_size += len;
696 * accept the backlog of incoming calls
698 static void afs_collect_incoming_call(struct work_struct *work)
700 struct rxrpc_call *rxcall;
701 struct afs_call *call = NULL;
704 while ((skb = skb_dequeue(&afs_incoming_calls))) {
707 /* don't need the notification */
711 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
713 rxrpc_kernel_reject_call(afs_socket);
717 call->async_workfn = afs_process_async_call;
718 INIT_WORK(&call->async_work, afs_async_workfn);
719 call->wait_mode = &afs_async_incoming_call;
720 call->type = &afs_RXCMxxxx;
721 init_waitqueue_head(&call->waitq);
722 skb_queue_head_init(&call->rx_queue);
723 call->state = AFS_CALL_AWAIT_OP_ID;
725 _debug("CALL %p{%s} [%d]",
726 call, call->type->name,
727 atomic_read(&afs_outstanding_calls));
728 atomic_inc(&afs_outstanding_calls);
731 rxcall = rxrpc_kernel_accept_call(afs_socket,
732 (unsigned long) call);
733 if (!IS_ERR(rxcall)) {
734 call->rxcall = rxcall;
744 * grab the operation ID from an incoming cache manager call
746 static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
749 size_t len = skb->len;
750 void *oibuf = (void *) &call->operation_ID;
752 _enter("{%u},{%zu},%d", call->offset, len, last);
754 ASSERTCMP(call->offset, <, 4);
756 /* the operation ID forms the first four bytes of the request data */
757 len = min_t(size_t, len, 4 - call->offset);
758 if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
760 if (!pskb_pull(skb, len))
764 if (call->offset < 4) {
766 _leave(" = -EBADMSG [op ID short]");
769 _leave(" = 0 [incomplete]");
773 call->state = AFS_CALL_AWAIT_REQUEST;
775 /* ask the cache manager to route the call (it'll change the call type
777 if (!afs_cm_incoming_call(call))
780 /* pass responsibility for the remainer of this message off to the
781 * cache manager op */
782 return call->type->deliver(call, skb, last);
786 * send an empty reply
788 void afs_send_empty_reply(struct afs_call *call)
796 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
797 msg.msg_control = NULL;
798 msg.msg_controllen = 0;
801 call->state = AFS_CALL_AWAIT_ACK;
802 switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
804 _leave(" [replied]");
809 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
818 * send a simple reply
820 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
828 iov[0].iov_base = (void *) buf;
829 iov[0].iov_len = len;
832 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
833 msg.msg_control = NULL;
834 msg.msg_controllen = 0;
837 call->state = AFS_CALL_AWAIT_ACK;
838 n = rxrpc_kernel_send_data(call->rxcall, &msg, len);
841 _leave(" [replied]");
847 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
854 * extract a piece of data from the received data socket buffers
856 int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
857 bool last, void *buf, size_t count)
859 size_t len = skb->len;
861 _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
863 ASSERTCMP(call->offset, <, count);
865 len = min_t(size_t, len, count - call->offset);
866 if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
867 !pskb_pull(skb, len))
871 if (call->offset < count) {
873 _leave(" = -EBADMSG [%d < %zu]", call->offset, count);
876 _leave(" = -EAGAIN");