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 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
);
69 * open an RxRPC socket and bind it to be a server for callback notifications
70 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
72 int afs_open_socket(void)
74 struct sockaddr_rxrpc srx
;
75 struct socket
*socket
;
80 skb_queue_head_init(&afs_incoming_calls
);
82 afs_async_calls
= create_singlethread_workqueue("kafsd");
83 if (!afs_async_calls
) {
84 _leave(" = -ENOMEM [wq]");
88 ret
= sock_create_kern(AF_RXRPC
, SOCK_DGRAM
, PF_INET
, &socket
);
90 destroy_workqueue(afs_async_calls
);
91 _leave(" = %d [socket]", ret
);
95 socket
->sk
->sk_allocation
= GFP_NOFS
;
97 /* bind the callback manager's address to make this a server socket */
98 srx
.srx_family
= AF_RXRPC
;
99 srx
.srx_service
= CM_SERVICE
;
100 srx
.transport_type
= SOCK_DGRAM
;
101 srx
.transport_len
= sizeof(srx
.transport
.sin
);
102 srx
.transport
.sin
.sin_family
= AF_INET
;
103 srx
.transport
.sin
.sin_port
= htons(AFS_CM_PORT
);
104 memset(&srx
.transport
.sin
.sin_addr
, 0,
105 sizeof(srx
.transport
.sin
.sin_addr
));
107 ret
= kernel_bind(socket
, (struct sockaddr
*) &srx
, sizeof(srx
));
109 sock_release(socket
);
110 destroy_workqueue(afs_async_calls
);
111 _leave(" = %d [bind]", ret
);
115 rxrpc_kernel_intercept_rx_messages(socket
, afs_rx_interceptor
);
123 * close the RxRPC socket AFS was using
125 void afs_close_socket(void)
129 sock_release(afs_socket
);
132 destroy_workqueue(afs_async_calls
);
134 ASSERTCMP(atomic_read(&afs_outstanding_skbs
), ==, 0);
135 ASSERTCMP(atomic_read(&afs_outstanding_calls
), ==, 0);
140 * note that the data in a socket buffer is now delivered and that the buffer
143 static void afs_data_delivered(struct sk_buff
*skb
)
146 _debug("DLVR NULL [%d]", atomic_read(&afs_outstanding_skbs
));
149 _debug("DLVR %p{%u} [%d]",
150 skb
, skb
->mark
, atomic_read(&afs_outstanding_skbs
));
151 if (atomic_dec_return(&afs_outstanding_skbs
) == -1)
153 rxrpc_kernel_data_delivered(skb
);
158 * free a socket buffer
160 static void afs_free_skb(struct sk_buff
*skb
)
163 _debug("FREE NULL [%d]", atomic_read(&afs_outstanding_skbs
));
166 _debug("FREE %p{%u} [%d]",
167 skb
, skb
->mark
, atomic_read(&afs_outstanding_skbs
));
168 if (atomic_dec_return(&afs_outstanding_skbs
) == -1)
170 rxrpc_kernel_free_skb(skb
);
177 static void afs_free_call(struct afs_call
*call
)
179 _debug("DONE %p{%s} [%d]",
180 call
, call
->type
->name
, atomic_read(&afs_outstanding_calls
));
181 if (atomic_dec_return(&afs_outstanding_calls
) == -1)
184 ASSERTCMP(call
->rxcall
, ==, NULL
);
185 ASSERT(!work_pending(&call
->async_work
));
186 ASSERT(skb_queue_empty(&call
->rx_queue
));
187 ASSERT(call
->type
->name
!= NULL
);
189 kfree(call
->request
);
194 * End a call but do not free it
196 static void afs_end_call_nofree(struct afs_call
*call
)
199 rxrpc_kernel_end_call(call
->rxcall
);
202 if (call
->type
->destructor
)
203 call
->type
->destructor(call
);
207 * End a call and free it
209 static void afs_end_call(struct afs_call
*call
)
211 afs_end_call_nofree(call
);
216 * allocate a call with flat request and reply buffers
218 struct afs_call
*afs_alloc_flat_call(const struct afs_call_type
*type
,
219 size_t request_size
, size_t reply_size
)
221 struct afs_call
*call
;
223 call
= kzalloc(sizeof(*call
), GFP_NOFS
);
227 _debug("CALL %p{%s} [%d]",
228 call
, type
->name
, atomic_read(&afs_outstanding_calls
));
229 atomic_inc(&afs_outstanding_calls
);
232 call
->request_size
= request_size
;
233 call
->reply_max
= reply_size
;
236 call
->request
= kmalloc(request_size
, GFP_NOFS
);
242 call
->buffer
= kmalloc(reply_size
, GFP_NOFS
);
247 init_waitqueue_head(&call
->waitq
);
248 skb_queue_head_init(&call
->rx_queue
);
258 * clean up a call with flat buffer
260 void afs_flat_call_destructor(struct afs_call
*call
)
264 kfree(call
->request
);
265 call
->request
= NULL
;
271 * attach the data from a bunch of pages on an inode to a call
273 static int afs_send_pages(struct afs_call
*call
, struct msghdr
*msg
,
276 struct page
*pages
[8];
277 unsigned count
, n
, loop
, offset
, to
;
278 pgoff_t first
= call
->first
, last
= call
->last
;
283 offset
= call
->first_offset
;
284 call
->first_offset
= 0;
287 _debug("attach %lx-%lx", first
, last
);
289 count
= last
- first
+ 1;
290 if (count
> ARRAY_SIZE(pages
))
291 count
= ARRAY_SIZE(pages
);
292 n
= find_get_pages_contig(call
->mapping
, first
, count
, pages
);
293 ASSERTCMP(n
, ==, count
);
299 if (first
+ loop
>= last
)
302 msg
->msg_flags
= MSG_MORE
;
303 iov
->iov_base
= kmap(pages
[loop
]) + offset
;
304 iov
->iov_len
= to
- offset
;
307 _debug("- range %u-%u%s",
308 offset
, to
, msg
->msg_flags
? " [more]" : "");
309 msg
->msg_iov
= (struct iovec
*) iov
;
312 /* have to change the state *before* sending the last
313 * packet as RxRPC might give us the reply before it
314 * returns from sending the request */
315 if (first
+ loop
>= last
)
316 call
->state
= AFS_CALL_AWAIT_REPLY
;
317 ret
= rxrpc_kernel_send_data(call
->rxcall
, msg
,
322 } while (++loop
< count
);
325 for (loop
= 0; loop
< count
; loop
++)
326 put_page(pages
[loop
]);
329 } while (first
<= last
);
331 _leave(" = %d", ret
);
338 int afs_make_call(struct in_addr
*addr
, struct afs_call
*call
, gfp_t gfp
,
339 const struct afs_wait_mode
*wait_mode
)
341 struct sockaddr_rxrpc srx
;
342 struct rxrpc_call
*rxcall
;
348 _enter("%x,{%d},", addr
->s_addr
, ntohs(call
->port
));
350 ASSERT(call
->type
!= NULL
);
351 ASSERT(call
->type
->name
!= NULL
);
353 _debug("____MAKE %p{%s,%x} [%d]____",
354 call
, call
->type
->name
, key_serial(call
->key
),
355 atomic_read(&afs_outstanding_calls
));
357 call
->wait_mode
= wait_mode
;
358 call
->async_workfn
= afs_process_async_call
;
359 INIT_WORK(&call
->async_work
, afs_async_workfn
);
361 memset(&srx
, 0, sizeof(srx
));
362 srx
.srx_family
= AF_RXRPC
;
363 srx
.srx_service
= call
->service_id
;
364 srx
.transport_type
= SOCK_DGRAM
;
365 srx
.transport_len
= sizeof(srx
.transport
.sin
);
366 srx
.transport
.sin
.sin_family
= AF_INET
;
367 srx
.transport
.sin
.sin_port
= call
->port
;
368 memcpy(&srx
.transport
.sin
.sin_addr
, addr
, 4);
371 rxcall
= rxrpc_kernel_begin_call(afs_socket
, &srx
, call
->key
,
372 (unsigned long) call
, gfp
);
374 if (IS_ERR(rxcall
)) {
375 ret
= PTR_ERR(rxcall
);
376 goto error_kill_call
;
379 call
->rxcall
= rxcall
;
381 /* send the request */
382 iov
[0].iov_base
= call
->request
;
383 iov
[0].iov_len
= call
->request_size
;
387 msg
.msg_iov
= (struct iovec
*) iov
;
389 msg
.msg_control
= NULL
;
390 msg
.msg_controllen
= 0;
391 msg
.msg_flags
= (call
->send_pages
? MSG_MORE
: 0);
393 /* have to change the state *before* sending the last packet as RxRPC
394 * might give us the reply before it returns from sending the
396 if (!call
->send_pages
)
397 call
->state
= AFS_CALL_AWAIT_REPLY
;
398 ret
= rxrpc_kernel_send_data(rxcall
, &msg
, call
->request_size
);
402 if (call
->send_pages
) {
403 ret
= afs_send_pages(call
, &msg
, iov
);
408 /* at this point, an async call may no longer exist as it may have
409 * already completed */
410 return wait_mode
->wait(call
);
413 rxrpc_kernel_abort_call(rxcall
, RX_USER_ABORT
);
414 while ((skb
= skb_dequeue(&call
->rx_queue
)))
418 _leave(" = %d", ret
);
423 * handles intercepted messages that were arriving in the socket's Rx queue
424 * - called with the socket receive queue lock held to ensure message ordering
425 * - called with softirqs disabled
427 static void afs_rx_interceptor(struct sock
*sk
, unsigned long user_call_ID
,
430 struct afs_call
*call
= (struct afs_call
*) user_call_ID
;
432 _enter("%p,,%u", call
, skb
->mark
);
434 _debug("ICPT %p{%u} [%d]",
435 skb
, skb
->mark
, atomic_read(&afs_outstanding_skbs
));
437 ASSERTCMP(sk
, ==, afs_socket
->sk
);
438 atomic_inc(&afs_outstanding_skbs
);
441 /* its an incoming call for our callback service */
442 skb_queue_tail(&afs_incoming_calls
, skb
);
443 queue_work(afs_wq
, &afs_collect_incoming_call_work
);
445 /* route the messages directly to the appropriate call */
446 skb_queue_tail(&call
->rx_queue
, skb
);
447 call
->wait_mode
->rx_wakeup(call
);
454 * deliver messages to a call
456 static void afs_deliver_to_call(struct afs_call
*call
)
465 while ((call
->state
== AFS_CALL_AWAIT_REPLY
||
466 call
->state
== AFS_CALL_AWAIT_OP_ID
||
467 call
->state
== AFS_CALL_AWAIT_REQUEST
||
468 call
->state
== AFS_CALL_AWAIT_ACK
) &&
469 (skb
= skb_dequeue(&call
->rx_queue
))) {
471 case RXRPC_SKB_MARK_DATA
:
473 last
= rxrpc_kernel_is_data_last(skb
);
474 ret
= call
->type
->deliver(call
, skb
, last
);
478 call
->state
== AFS_CALL_AWAIT_REPLY
)
479 call
->state
= AFS_CALL_COMPLETE
;
482 abort_code
= RX_CALL_DEAD
;
485 abort_code
= RX_INVALID_OPERATION
;
488 abort_code
= RXGEN_CC_UNMARSHAL
;
489 if (call
->state
!= AFS_CALL_AWAIT_REPLY
)
490 abort_code
= RXGEN_SS_UNMARSHAL
;
492 rxrpc_kernel_abort_call(call
->rxcall
,
495 call
->state
= AFS_CALL_ERROR
;
498 afs_data_delivered(skb
);
501 case RXRPC_SKB_MARK_FINAL_ACK
:
503 call
->state
= AFS_CALL_COMPLETE
;
505 case RXRPC_SKB_MARK_BUSY
:
507 call
->error
= -EBUSY
;
508 call
->state
= AFS_CALL_BUSY
;
510 case RXRPC_SKB_MARK_REMOTE_ABORT
:
511 abort_code
= rxrpc_kernel_get_abort_code(skb
);
512 call
->error
= call
->type
->abort_to_error(abort_code
);
513 call
->state
= AFS_CALL_ABORTED
;
514 _debug("Rcv ABORT %u -> %d", abort_code
, call
->error
);
516 case RXRPC_SKB_MARK_NET_ERROR
:
517 call
->error
= -rxrpc_kernel_get_error_number(skb
);
518 call
->state
= AFS_CALL_ERROR
;
519 _debug("Rcv NET ERROR %d", call
->error
);
521 case RXRPC_SKB_MARK_LOCAL_ERROR
:
522 call
->error
= -rxrpc_kernel_get_error_number(skb
);
523 call
->state
= AFS_CALL_ERROR
;
524 _debug("Rcv LOCAL ERROR %d", call
->error
);
534 /* make sure the queue is empty if the call is done with (we might have
535 * aborted the call early because of an unmarshalling error) */
536 if (call
->state
>= AFS_CALL_COMPLETE
) {
537 while ((skb
= skb_dequeue(&call
->rx_queue
)))
547 * wait synchronously for a call to complete
549 static int afs_wait_for_call_to_complete(struct afs_call
*call
)
554 DECLARE_WAITQUEUE(myself
, current
);
558 add_wait_queue(&call
->waitq
, &myself
);
560 set_current_state(TASK_INTERRUPTIBLE
);
562 /* deliver any messages that are in the queue */
563 if (!skb_queue_empty(&call
->rx_queue
)) {
564 __set_current_state(TASK_RUNNING
);
565 afs_deliver_to_call(call
);
570 if (call
->state
>= AFS_CALL_COMPLETE
)
573 if (signal_pending(current
))
578 remove_wait_queue(&call
->waitq
, &myself
);
579 __set_current_state(TASK_RUNNING
);
582 if (call
->state
< AFS_CALL_COMPLETE
) {
583 _debug("call incomplete");
584 rxrpc_kernel_abort_call(call
->rxcall
, RX_CALL_DEAD
);
585 while ((skb
= skb_dequeue(&call
->rx_queue
)))
589 _debug("call complete");
591 _leave(" = %d", ret
);
596 * wake up a waiting call
598 static void afs_wake_up_call_waiter(struct afs_call
*call
)
600 wake_up(&call
->waitq
);
604 * wake up an asynchronous call
606 static void afs_wake_up_async_call(struct afs_call
*call
)
609 queue_work(afs_async_calls
, &call
->async_work
);
613 * put a call into asynchronous mode
614 * - mustn't touch the call descriptor as the call my have completed by the
617 static int afs_dont_wait_for_call_to_complete(struct afs_call
*call
)
624 * delete an asynchronous call
626 static void afs_delete_async_call(struct afs_call
*call
)
636 * perform processing on an asynchronous call
637 * - on a multiple-thread workqueue this work item may try to run on several
638 * CPUs at the same time
640 static void afs_process_async_call(struct afs_call
*call
)
644 if (!skb_queue_empty(&call
->rx_queue
))
645 afs_deliver_to_call(call
);
647 if (call
->state
>= AFS_CALL_COMPLETE
&& call
->wait_mode
) {
648 if (call
->wait_mode
->async_complete
)
649 call
->wait_mode
->async_complete(call
->reply
,
654 afs_end_call_nofree(call
);
656 /* we can't just delete the call because the work item may be
658 call
->async_workfn
= afs_delete_async_call
;
659 queue_work(afs_async_calls
, &call
->async_work
);
666 * empty a socket buffer into a flat reply buffer
668 void afs_transfer_reply(struct afs_call
*call
, struct sk_buff
*skb
)
670 size_t len
= skb
->len
;
672 if (skb_copy_bits(skb
, 0, call
->buffer
+ call
->reply_size
, len
) < 0)
674 call
->reply_size
+= len
;
678 * accept the backlog of incoming calls
680 static void afs_collect_incoming_call(struct work_struct
*work
)
682 struct rxrpc_call
*rxcall
;
683 struct afs_call
*call
= NULL
;
686 while ((skb
= skb_dequeue(&afs_incoming_calls
))) {
689 /* don't need the notification */
693 call
= kzalloc(sizeof(struct afs_call
), GFP_KERNEL
);
695 rxrpc_kernel_reject_call(afs_socket
);
699 call
->async_workfn
= afs_process_async_call
;
700 INIT_WORK(&call
->async_work
, afs_async_workfn
);
701 call
->wait_mode
= &afs_async_incoming_call
;
702 call
->type
= &afs_RXCMxxxx
;
703 init_waitqueue_head(&call
->waitq
);
704 skb_queue_head_init(&call
->rx_queue
);
705 call
->state
= AFS_CALL_AWAIT_OP_ID
;
707 _debug("CALL %p{%s} [%d]",
708 call
, call
->type
->name
,
709 atomic_read(&afs_outstanding_calls
));
710 atomic_inc(&afs_outstanding_calls
);
713 rxcall
= rxrpc_kernel_accept_call(afs_socket
,
714 (unsigned long) call
);
715 if (!IS_ERR(rxcall
)) {
716 call
->rxcall
= rxcall
;
726 * grab the operation ID from an incoming cache manager call
728 static int afs_deliver_cm_op_id(struct afs_call
*call
, struct sk_buff
*skb
,
731 size_t len
= skb
->len
;
732 void *oibuf
= (void *) &call
->operation_ID
;
734 _enter("{%u},{%zu},%d", call
->offset
, len
, last
);
736 ASSERTCMP(call
->offset
, <, 4);
738 /* the operation ID forms the first four bytes of the request data */
739 len
= min_t(size_t, len
, 4 - call
->offset
);
740 if (skb_copy_bits(skb
, 0, oibuf
+ call
->offset
, len
) < 0)
742 if (!pskb_pull(skb
, len
))
746 if (call
->offset
< 4) {
748 _leave(" = -EBADMSG [op ID short]");
751 _leave(" = 0 [incomplete]");
755 call
->state
= AFS_CALL_AWAIT_REQUEST
;
757 /* ask the cache manager to route the call (it'll change the call type
759 if (!afs_cm_incoming_call(call
))
762 /* pass responsibility for the remainer of this message off to the
763 * cache manager op */
764 return call
->type
->deliver(call
, skb
, last
);
768 * send an empty reply
770 void afs_send_empty_reply(struct afs_call
*call
)
777 iov
[0].iov_base
= NULL
;
783 msg
.msg_control
= NULL
;
784 msg
.msg_controllen
= 0;
787 call
->state
= AFS_CALL_AWAIT_ACK
;
788 switch (rxrpc_kernel_send_data(call
->rxcall
, &msg
, 0)) {
790 _leave(" [replied]");
795 rxrpc_kernel_abort_call(call
->rxcall
, RX_USER_ABORT
);
804 * send a simple reply
806 void afs_send_simple_reply(struct afs_call
*call
, const void *buf
, size_t len
)
814 iov
[0].iov_base
= (void *) buf
;
815 iov
[0].iov_len
= len
;
820 msg
.msg_control
= NULL
;
821 msg
.msg_controllen
= 0;
824 call
->state
= AFS_CALL_AWAIT_ACK
;
825 n
= rxrpc_kernel_send_data(call
->rxcall
, &msg
, len
);
828 _leave(" [replied]");
834 rxrpc_kernel_abort_call(call
->rxcall
, RX_USER_ABORT
);
841 * extract a piece of data from the received data socket buffers
843 int afs_extract_data(struct afs_call
*call
, struct sk_buff
*skb
,
844 bool last
, void *buf
, size_t count
)
846 size_t len
= skb
->len
;
848 _enter("{%u},{%zu},%d,,%zu", call
->offset
, len
, last
, count
);
850 ASSERTCMP(call
->offset
, <, count
);
852 len
= min_t(size_t, len
, count
- call
->offset
);
853 if (skb_copy_bits(skb
, 0, buf
+ call
->offset
, len
) < 0 ||
854 !pskb_pull(skb
, len
))
858 if (call
->offset
< count
) {
860 _leave(" = -EBADMSG [%d < %zu]", call
->offset
, count
);
863 _leave(" = -EAGAIN");