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>
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 work_struct *);
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);
62 * open an RxRPC socket and bind it to be a server for callback notifications
63 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
65 int afs_open_socket(void)
67 struct sockaddr_rxrpc srx;
68 struct socket *socket;
73 skb_queue_head_init(&afs_incoming_calls);
75 afs_async_calls = create_singlethread_workqueue("kafsd");
76 if (!afs_async_calls) {
77 _leave(" = -ENOMEM [wq]");
81 ret = sock_create_kern(AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
83 destroy_workqueue(afs_async_calls);
84 _leave(" = %d [socket]", ret);
88 socket->sk->sk_allocation = GFP_NOFS;
90 /* bind the callback manager's address to make this a server socket */
91 srx.srx_family = AF_RXRPC;
92 srx.srx_service = CM_SERVICE;
93 srx.transport_type = SOCK_DGRAM;
94 srx.transport_len = sizeof(srx.transport.sin);
95 srx.transport.sin.sin_family = AF_INET;
96 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
97 memset(&srx.transport.sin.sin_addr, 0,
98 sizeof(srx.transport.sin.sin_addr));
100 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
102 sock_release(socket);
103 destroy_workqueue(afs_async_calls);
104 _leave(" = %d [bind]", ret);
108 rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor);
116 * close the RxRPC socket AFS was using
118 void afs_close_socket(void)
122 sock_release(afs_socket);
125 destroy_workqueue(afs_async_calls);
127 ASSERTCMP(atomic_read(&afs_outstanding_skbs), ==, 0);
128 ASSERTCMP(atomic_read(&afs_outstanding_calls), ==, 0);
133 * note that the data in a socket buffer is now delivered and that the buffer
136 static void afs_data_delivered(struct sk_buff *skb)
139 _debug("DLVR NULL [%d]", atomic_read(&afs_outstanding_skbs));
142 _debug("DLVR %p{%u} [%d]",
143 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
144 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
146 rxrpc_kernel_data_delivered(skb);
151 * free a socket buffer
153 static void afs_free_skb(struct sk_buff *skb)
156 _debug("FREE NULL [%d]", atomic_read(&afs_outstanding_skbs));
159 _debug("FREE %p{%u} [%d]",
160 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
161 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
163 rxrpc_kernel_free_skb(skb);
170 static void afs_free_call(struct afs_call *call)
172 _debug("DONE %p{%s} [%d]",
173 call, call->type->name, atomic_read(&afs_outstanding_calls));
174 if (atomic_dec_return(&afs_outstanding_calls) == -1)
177 ASSERTCMP(call->rxcall, ==, NULL);
178 ASSERT(!work_pending(&call->async_work));
179 ASSERT(skb_queue_empty(&call->rx_queue));
180 ASSERT(call->type->name != NULL);
182 kfree(call->request);
187 * End a call but do not free it
189 static void afs_end_call_nofree(struct afs_call *call)
192 rxrpc_kernel_end_call(call->rxcall);
195 if (call->type->destructor)
196 call->type->destructor(call);
200 * End a call and free it
202 static void afs_end_call(struct afs_call *call)
204 afs_end_call_nofree(call);
209 * allocate a call with flat request and reply buffers
211 struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
212 size_t request_size, size_t reply_size)
214 struct afs_call *call;
216 call = kzalloc(sizeof(*call), GFP_NOFS);
220 _debug("CALL %p{%s} [%d]",
221 call, type->name, atomic_read(&afs_outstanding_calls));
222 atomic_inc(&afs_outstanding_calls);
225 call->request_size = request_size;
226 call->reply_max = reply_size;
229 call->request = kmalloc(request_size, GFP_NOFS);
235 call->buffer = kmalloc(reply_size, GFP_NOFS);
240 init_waitqueue_head(&call->waitq);
241 skb_queue_head_init(&call->rx_queue);
251 * clean up a call with flat buffer
253 void afs_flat_call_destructor(struct afs_call *call)
257 kfree(call->request);
258 call->request = NULL;
264 * attach the data from a bunch of pages on an inode to a call
266 static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
269 struct page *pages[8];
270 unsigned count, n, loop, offset, to;
271 pgoff_t first = call->first, last = call->last;
276 offset = call->first_offset;
277 call->first_offset = 0;
280 _debug("attach %lx-%lx", first, last);
282 count = last - first + 1;
283 if (count > ARRAY_SIZE(pages))
284 count = ARRAY_SIZE(pages);
285 n = find_get_pages_contig(call->mapping, first, count, pages);
286 ASSERTCMP(n, ==, count);
292 if (first + loop >= last)
295 msg->msg_flags = MSG_MORE;
296 iov->iov_base = kmap(pages[loop]) + offset;
297 iov->iov_len = to - offset;
300 _debug("- range %u-%u%s",
301 offset, to, msg->msg_flags ? " [more]" : "");
302 msg->msg_iov = (struct iovec *) iov;
305 /* have to change the state *before* sending the last
306 * packet as RxRPC might give us the reply before it
307 * returns from sending the request */
308 if (first + loop >= last)
309 call->state = AFS_CALL_AWAIT_REPLY;
310 ret = rxrpc_kernel_send_data(call->rxcall, msg,
315 } while (++loop < count);
318 for (loop = 0; loop < count; loop++)
319 put_page(pages[loop]);
322 } while (first <= last);
324 _leave(" = %d", ret);
331 int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
332 const struct afs_wait_mode *wait_mode)
334 struct sockaddr_rxrpc srx;
335 struct rxrpc_call *rxcall;
341 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
343 ASSERT(call->type != NULL);
344 ASSERT(call->type->name != NULL);
346 _debug("____MAKE %p{%s,%x} [%d]____",
347 call, call->type->name, key_serial(call->key),
348 atomic_read(&afs_outstanding_calls));
350 call->wait_mode = wait_mode;
351 INIT_WORK(&call->async_work, afs_process_async_call);
353 memset(&srx, 0, sizeof(srx));
354 srx.srx_family = AF_RXRPC;
355 srx.srx_service = call->service_id;
356 srx.transport_type = SOCK_DGRAM;
357 srx.transport_len = sizeof(srx.transport.sin);
358 srx.transport.sin.sin_family = AF_INET;
359 srx.transport.sin.sin_port = call->port;
360 memcpy(&srx.transport.sin.sin_addr, addr, 4);
363 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
364 (unsigned long) call, gfp);
366 if (IS_ERR(rxcall)) {
367 ret = PTR_ERR(rxcall);
368 goto error_kill_call;
371 call->rxcall = rxcall;
373 /* send the request */
374 iov[0].iov_base = call->request;
375 iov[0].iov_len = call->request_size;
379 msg.msg_iov = (struct iovec *) iov;
381 msg.msg_control = NULL;
382 msg.msg_controllen = 0;
383 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
385 /* have to change the state *before* sending the last packet as RxRPC
386 * might give us the reply before it returns from sending the
388 if (!call->send_pages)
389 call->state = AFS_CALL_AWAIT_REPLY;
390 ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
394 if (call->send_pages) {
395 ret = afs_send_pages(call, &msg, iov);
400 /* at this point, an async call may no longer exist as it may have
401 * already completed */
402 return wait_mode->wait(call);
405 rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
406 while ((skb = skb_dequeue(&call->rx_queue)))
410 _leave(" = %d", ret);
415 * handles intercepted messages that were arriving in the socket's Rx queue
416 * - called with the socket receive queue lock held to ensure message ordering
417 * - called with softirqs disabled
419 static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
422 struct afs_call *call = (struct afs_call *) user_call_ID;
424 _enter("%p,,%u", call, skb->mark);
426 _debug("ICPT %p{%u} [%d]",
427 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
429 ASSERTCMP(sk, ==, afs_socket->sk);
430 atomic_inc(&afs_outstanding_skbs);
433 /* its an incoming call for our callback service */
434 skb_queue_tail(&afs_incoming_calls, skb);
435 queue_work(afs_wq, &afs_collect_incoming_call_work);
437 /* route the messages directly to the appropriate call */
438 skb_queue_tail(&call->rx_queue, skb);
439 call->wait_mode->rx_wakeup(call);
446 * deliver messages to a call
448 static void afs_deliver_to_call(struct afs_call *call)
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) &&
461 (skb = skb_dequeue(&call->rx_queue))) {
463 case RXRPC_SKB_MARK_DATA:
465 last = rxrpc_kernel_is_data_last(skb);
466 ret = call->type->deliver(call, skb, last);
470 call->state == AFS_CALL_AWAIT_REPLY)
471 call->state = AFS_CALL_COMPLETE;
474 abort_code = RX_CALL_DEAD;
477 abort_code = RX_INVALID_OPERATION;
480 abort_code = RXGEN_CC_UNMARSHAL;
481 if (call->state != AFS_CALL_AWAIT_REPLY)
482 abort_code = RXGEN_SS_UNMARSHAL;
484 rxrpc_kernel_abort_call(call->rxcall,
487 call->state = AFS_CALL_ERROR;
490 afs_data_delivered(skb);
493 case RXRPC_SKB_MARK_FINAL_ACK:
495 call->state = AFS_CALL_COMPLETE;
497 case RXRPC_SKB_MARK_BUSY:
499 call->error = -EBUSY;
500 call->state = AFS_CALL_BUSY;
502 case RXRPC_SKB_MARK_REMOTE_ABORT:
503 abort_code = rxrpc_kernel_get_abort_code(skb);
504 call->error = call->type->abort_to_error(abort_code);
505 call->state = AFS_CALL_ABORTED;
506 _debug("Rcv ABORT %u -> %d", abort_code, call->error);
508 case RXRPC_SKB_MARK_NET_ERROR:
509 call->error = -rxrpc_kernel_get_error_number(skb);
510 call->state = AFS_CALL_ERROR;
511 _debug("Rcv NET ERROR %d", call->error);
513 case RXRPC_SKB_MARK_LOCAL_ERROR:
514 call->error = -rxrpc_kernel_get_error_number(skb);
515 call->state = AFS_CALL_ERROR;
516 _debug("Rcv LOCAL ERROR %d", call->error);
526 /* make sure the queue is empty if the call is done with (we might have
527 * aborted the call early because of an unmarshalling error) */
528 if (call->state >= AFS_CALL_COMPLETE) {
529 while ((skb = skb_dequeue(&call->rx_queue)))
539 * wait synchronously for a call to complete
541 static int afs_wait_for_call_to_complete(struct afs_call *call)
546 DECLARE_WAITQUEUE(myself, current);
550 add_wait_queue(&call->waitq, &myself);
552 set_current_state(TASK_INTERRUPTIBLE);
554 /* deliver any messages that are in the queue */
555 if (!skb_queue_empty(&call->rx_queue)) {
556 __set_current_state(TASK_RUNNING);
557 afs_deliver_to_call(call);
562 if (call->state >= AFS_CALL_COMPLETE)
565 if (signal_pending(current))
570 remove_wait_queue(&call->waitq, &myself);
571 __set_current_state(TASK_RUNNING);
574 if (call->state < AFS_CALL_COMPLETE) {
575 _debug("call incomplete");
576 rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
577 while ((skb = skb_dequeue(&call->rx_queue)))
581 _debug("call complete");
583 _leave(" = %d", ret);
588 * wake up a waiting call
590 static void afs_wake_up_call_waiter(struct afs_call *call)
592 wake_up(&call->waitq);
596 * wake up an asynchronous call
598 static void afs_wake_up_async_call(struct afs_call *call)
601 queue_work(afs_async_calls, &call->async_work);
605 * put a call into asynchronous mode
606 * - mustn't touch the call descriptor as the call my have completed by the
609 static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
616 * delete an asynchronous call
618 static void afs_delete_async_call(struct work_struct *work)
620 struct afs_call *call =
621 container_of(work, struct afs_call, async_work);
631 * perform processing on an asynchronous call
632 * - on a multiple-thread workqueue this work item may try to run on several
633 * CPUs at the same time
635 static void afs_process_async_call(struct work_struct *work)
637 struct afs_call *call =
638 container_of(work, struct afs_call, async_work);
642 if (!skb_queue_empty(&call->rx_queue))
643 afs_deliver_to_call(call);
645 if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
646 if (call->wait_mode->async_complete)
647 call->wait_mode->async_complete(call->reply,
652 afs_end_call_nofree(call);
654 /* we can't just delete the call because the work item may be
656 call->async_workfn = afs_delete_async_call;
657 queue_work(afs_async_calls, &call->async_work);
664 * empty a socket buffer into a flat reply buffer
666 void afs_transfer_reply(struct afs_call *call, struct sk_buff *skb)
668 size_t len = skb->len;
670 if (skb_copy_bits(skb, 0, call->buffer + call->reply_size, len) < 0)
672 call->reply_size += len;
675 static void afs_async_workfn(struct work_struct *work)
677 struct afs_call *call = container_of(work, struct afs_call, async_work);
679 call->async_workfn(work);
683 * accept the backlog of incoming calls
685 static void afs_collect_incoming_call(struct work_struct *work)
687 struct rxrpc_call *rxcall;
688 struct afs_call *call = NULL;
691 while ((skb = skb_dequeue(&afs_incoming_calls))) {
694 /* don't need the notification */
698 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
700 rxrpc_kernel_reject_call(afs_socket);
704 call->async_workfn = afs_process_async_call;
705 INIT_WORK(&call->async_work, afs_async_workfn);
706 call->wait_mode = &afs_async_incoming_call;
707 call->type = &afs_RXCMxxxx;
708 init_waitqueue_head(&call->waitq);
709 skb_queue_head_init(&call->rx_queue);
710 call->state = AFS_CALL_AWAIT_OP_ID;
712 _debug("CALL %p{%s} [%d]",
713 call, call->type->name,
714 atomic_read(&afs_outstanding_calls));
715 atomic_inc(&afs_outstanding_calls);
718 rxcall = rxrpc_kernel_accept_call(afs_socket,
719 (unsigned long) call);
720 if (!IS_ERR(rxcall)) {
721 call->rxcall = rxcall;
731 * grab the operation ID from an incoming cache manager call
733 static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
736 size_t len = skb->len;
737 void *oibuf = (void *) &call->operation_ID;
739 _enter("{%u},{%zu},%d", call->offset, len, last);
741 ASSERTCMP(call->offset, <, 4);
743 /* the operation ID forms the first four bytes of the request data */
744 len = min_t(size_t, len, 4 - call->offset);
745 if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
747 if (!pskb_pull(skb, len))
751 if (call->offset < 4) {
753 _leave(" = -EBADMSG [op ID short]");
756 _leave(" = 0 [incomplete]");
760 call->state = AFS_CALL_AWAIT_REQUEST;
762 /* ask the cache manager to route the call (it'll change the call type
764 if (!afs_cm_incoming_call(call))
767 /* pass responsibility for the remainer of this message off to the
768 * cache manager op */
769 return call->type->deliver(call, skb, last);
773 * send an empty reply
775 void afs_send_empty_reply(struct afs_call *call)
782 iov[0].iov_base = NULL;
788 msg.msg_control = NULL;
789 msg.msg_controllen = 0;
792 call->state = AFS_CALL_AWAIT_ACK;
793 switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
795 _leave(" [replied]");
800 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
809 * send a simple reply
811 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
819 iov[0].iov_base = (void *) buf;
820 iov[0].iov_len = len;
825 msg.msg_control = NULL;
826 msg.msg_controllen = 0;
829 call->state = AFS_CALL_AWAIT_ACK;
830 n = rxrpc_kernel_send_data(call->rxcall, &msg, len);
833 _leave(" [replied]");
839 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
846 * extract a piece of data from the received data socket buffers
848 int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
849 bool last, void *buf, size_t count)
851 size_t len = skb->len;
853 _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
855 ASSERTCMP(call->offset, <, count);
857 len = min_t(size_t, len, count - call->offset);
858 if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
859 !pskb_pull(skb, len))
863 if (call->offset < count) {
865 _leave(" = -EBADMSG [%d < %zu]", call->offset, count);
868 _leave(" = -EAGAIN");