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
);
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
);
89 afs_async_calls
= create_singlethread_workqueue("kafsd");
93 ret
= sock_create_kern(&init_net
, AF_RXRPC
, SOCK_DGRAM
, PF_INET
, &socket
);
97 socket
->sk
->sk_allocation
= GFP_NOFS
;
99 /* bind the callback manager's address to make this a server socket */
100 srx
.srx_family
= AF_RXRPC
;
101 srx
.srx_service
= CM_SERVICE
;
102 srx
.transport_type
= SOCK_DGRAM
;
103 srx
.transport_len
= sizeof(srx
.transport
.sin
);
104 srx
.transport
.sin
.sin_family
= AF_INET
;
105 srx
.transport
.sin
.sin_port
= htons(AFS_CM_PORT
);
106 memset(&srx
.transport
.sin
.sin_addr
, 0,
107 sizeof(srx
.transport
.sin
.sin_addr
));
109 ret
= kernel_bind(socket
, (struct sockaddr
*) &srx
, sizeof(srx
));
113 ret
= kernel_listen(socket
, INT_MAX
);
117 rxrpc_kernel_intercept_rx_messages(socket
, afs_rx_interceptor
);
124 sock_release(socket
);
126 destroy_workqueue(afs_async_calls
);
128 _leave(" = %d", ret
);
133 * close the RxRPC socket AFS was using
135 void afs_close_socket(void)
139 wait_on_atomic_t(&afs_outstanding_calls
, afs_wait_atomic_t
,
140 TASK_UNINTERRUPTIBLE
);
141 _debug("no outstanding calls");
143 sock_release(afs_socket
);
146 destroy_workqueue(afs_async_calls
);
148 ASSERTCMP(atomic_read(&afs_outstanding_skbs
), ==, 0);
153 * Note that the data in a socket buffer is now consumed.
155 void afs_data_consumed(struct afs_call
*call
, struct sk_buff
*skb
)
158 _debug("DLVR NULL [%d]", atomic_read(&afs_outstanding_skbs
));
161 _debug("DLVR %p{%u} [%d]",
162 skb
, skb
->mark
, atomic_read(&afs_outstanding_skbs
));
163 rxrpc_kernel_data_consumed(call
->rxcall
, skb
);
168 * free a socket buffer
170 static void afs_free_skb(struct sk_buff
*skb
)
173 _debug("FREE NULL [%d]", atomic_read(&afs_outstanding_skbs
));
176 _debug("FREE %p{%u} [%d]",
177 skb
, skb
->mark
, atomic_read(&afs_outstanding_skbs
));
178 if (atomic_dec_return(&afs_outstanding_skbs
) == -1)
180 rxrpc_kernel_free_skb(skb
);
187 static void afs_free_call(struct afs_call
*call
)
189 _debug("DONE %p{%s} [%d]",
190 call
, call
->type
->name
, atomic_read(&afs_outstanding_calls
));
192 ASSERTCMP(call
->rxcall
, ==, NULL
);
193 ASSERT(!work_pending(&call
->async_work
));
194 ASSERT(skb_queue_empty(&call
->rx_queue
));
195 ASSERT(call
->type
->name
!= NULL
);
197 kfree(call
->request
);
200 if (atomic_dec_and_test(&afs_outstanding_calls
))
201 wake_up_atomic_t(&afs_outstanding_calls
);
205 * End a call but do not free it
207 static void afs_end_call_nofree(struct afs_call
*call
)
210 rxrpc_kernel_end_call(call
->rxcall
);
213 if (call
->type
->destructor
)
214 call
->type
->destructor(call
);
218 * End a call and free it
220 static void afs_end_call(struct afs_call
*call
)
222 afs_end_call_nofree(call
);
227 * allocate a call with flat request and reply buffers
229 struct afs_call
*afs_alloc_flat_call(const struct afs_call_type
*type
,
230 size_t request_size
, size_t reply_size
)
232 struct afs_call
*call
;
234 call
= kzalloc(sizeof(*call
), GFP_NOFS
);
238 _debug("CALL %p{%s} [%d]",
239 call
, type
->name
, atomic_read(&afs_outstanding_calls
));
240 atomic_inc(&afs_outstanding_calls
);
243 call
->request_size
= request_size
;
244 call
->reply_max
= reply_size
;
247 call
->request
= kmalloc(request_size
, GFP_NOFS
);
253 call
->buffer
= kmalloc(reply_size
, GFP_NOFS
);
258 init_waitqueue_head(&call
->waitq
);
259 skb_queue_head_init(&call
->rx_queue
);
269 * clean up a call with flat buffer
271 void afs_flat_call_destructor(struct afs_call
*call
)
275 kfree(call
->request
);
276 call
->request
= NULL
;
282 * attach the data from a bunch of pages on an inode to a call
284 static int afs_send_pages(struct afs_call
*call
, struct msghdr
*msg
,
287 struct page
*pages
[8];
288 unsigned count
, n
, loop
, offset
, to
;
289 pgoff_t first
= call
->first
, last
= call
->last
;
294 offset
= call
->first_offset
;
295 call
->first_offset
= 0;
298 _debug("attach %lx-%lx", first
, last
);
300 count
= last
- first
+ 1;
301 if (count
> ARRAY_SIZE(pages
))
302 count
= ARRAY_SIZE(pages
);
303 n
= find_get_pages_contig(call
->mapping
, first
, count
, pages
);
304 ASSERTCMP(n
, ==, count
);
310 if (first
+ loop
>= last
)
313 msg
->msg_flags
= MSG_MORE
;
314 iov
->iov_base
= kmap(pages
[loop
]) + offset
;
315 iov
->iov_len
= to
- offset
;
318 _debug("- range %u-%u%s",
319 offset
, to
, msg
->msg_flags
? " [more]" : "");
320 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
,
321 iov
, 1, to
- offset
);
323 /* have to change the state *before* sending the last
324 * packet as RxRPC might give us the reply before it
325 * returns from sending the request */
326 if (first
+ loop
>= last
)
327 call
->state
= AFS_CALL_AWAIT_REPLY
;
328 ret
= rxrpc_kernel_send_data(call
->rxcall
, msg
,
333 } while (++loop
< count
);
336 for (loop
= 0; loop
< count
; loop
++)
337 put_page(pages
[loop
]);
340 } while (first
<= last
);
342 _leave(" = %d", ret
);
349 int afs_make_call(struct in_addr
*addr
, struct afs_call
*call
, gfp_t gfp
,
350 const struct afs_wait_mode
*wait_mode
)
352 struct sockaddr_rxrpc srx
;
353 struct rxrpc_call
*rxcall
;
359 _enter("%x,{%d},", addr
->s_addr
, ntohs(call
->port
));
361 ASSERT(call
->type
!= NULL
);
362 ASSERT(call
->type
->name
!= NULL
);
364 _debug("____MAKE %p{%s,%x} [%d]____",
365 call
, call
->type
->name
, key_serial(call
->key
),
366 atomic_read(&afs_outstanding_calls
));
368 call
->wait_mode
= wait_mode
;
369 call
->async_workfn
= afs_process_async_call
;
370 INIT_WORK(&call
->async_work
, afs_async_workfn
);
372 memset(&srx
, 0, sizeof(srx
));
373 srx
.srx_family
= AF_RXRPC
;
374 srx
.srx_service
= call
->service_id
;
375 srx
.transport_type
= SOCK_DGRAM
;
376 srx
.transport_len
= sizeof(srx
.transport
.sin
);
377 srx
.transport
.sin
.sin_family
= AF_INET
;
378 srx
.transport
.sin
.sin_port
= call
->port
;
379 memcpy(&srx
.transport
.sin
.sin_addr
, addr
, 4);
382 rxcall
= rxrpc_kernel_begin_call(afs_socket
, &srx
, call
->key
,
383 (unsigned long) call
, gfp
);
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 /* have to change the state *before* sending the last packet as RxRPC
405 * might give us the reply before it returns from sending the
407 if (!call
->send_pages
)
408 call
->state
= AFS_CALL_AWAIT_REPLY
;
409 ret
= rxrpc_kernel_send_data(rxcall
, &msg
, call
->request_size
);
413 if (call
->send_pages
) {
414 ret
= afs_send_pages(call
, &msg
, iov
);
419 /* at this point, an async call may no longer exist as it may have
420 * already completed */
421 return wait_mode
->wait(call
);
424 rxrpc_kernel_abort_call(rxcall
, RX_USER_ABORT
);
425 while ((skb
= skb_dequeue(&call
->rx_queue
)))
429 _leave(" = %d", ret
);
434 * Handles intercepted messages that were arriving in the socket's Rx queue.
436 * Called from the AF_RXRPC call processor in waitqueue process context. For
437 * each call, it is guaranteed this will be called in order of packet to be
440 static void afs_rx_interceptor(struct sock
*sk
, unsigned long user_call_ID
,
443 struct afs_call
*call
= (struct afs_call
*) user_call_ID
;
445 _enter("%p,,%u", call
, skb
->mark
);
447 _debug("ICPT %p{%u} [%d]",
448 skb
, skb
->mark
, atomic_read(&afs_outstanding_skbs
));
450 ASSERTCMP(sk
, ==, afs_socket
->sk
);
451 atomic_inc(&afs_outstanding_skbs
);
454 /* its an incoming call for our callback service */
455 skb_queue_tail(&afs_incoming_calls
, skb
);
456 queue_work(afs_wq
, &afs_collect_incoming_call_work
);
458 /* route the messages directly to the appropriate call */
459 skb_queue_tail(&call
->rx_queue
, skb
);
460 call
->wait_mode
->rx_wakeup(call
);
467 * deliver messages to a call
469 static void afs_deliver_to_call(struct afs_call
*call
)
478 while ((call
->state
== AFS_CALL_AWAIT_REPLY
||
479 call
->state
== AFS_CALL_AWAIT_OP_ID
||
480 call
->state
== AFS_CALL_AWAIT_REQUEST
||
481 call
->state
== AFS_CALL_AWAIT_ACK
) &&
482 (skb
= skb_dequeue(&call
->rx_queue
))) {
484 case RXRPC_SKB_MARK_DATA
:
486 last
= rxrpc_kernel_is_data_last(skb
);
487 ret
= call
->type
->deliver(call
, skb
, last
);
491 _debug("short data");
492 goto unmarshal_error
;
497 if (call
->state
== AFS_CALL_AWAIT_REPLY
)
498 call
->state
= AFS_CALL_COMPLETE
;
501 abort_code
= RX_CALL_DEAD
;
504 abort_code
= RX_INVALID_OPERATION
;
508 abort_code
= RXGEN_CC_UNMARSHAL
;
509 if (call
->state
!= AFS_CALL_AWAIT_REPLY
)
510 abort_code
= RXGEN_SS_UNMARSHAL
;
512 rxrpc_kernel_abort_call(call
->rxcall
,
515 call
->state
= AFS_CALL_ERROR
;
519 case RXRPC_SKB_MARK_FINAL_ACK
:
521 call
->state
= AFS_CALL_COMPLETE
;
523 case RXRPC_SKB_MARK_BUSY
:
525 call
->error
= -EBUSY
;
526 call
->state
= AFS_CALL_BUSY
;
528 case RXRPC_SKB_MARK_REMOTE_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("Rcv ABORT %u -> %d", abort_code
, call
->error
);
534 case RXRPC_SKB_MARK_LOCAL_ABORT
:
535 abort_code
= rxrpc_kernel_get_abort_code(skb
);
536 call
->error
= call
->type
->abort_to_error(abort_code
);
537 call
->state
= AFS_CALL_ABORTED
;
538 _debug("Loc ABORT %u -> %d", abort_code
, call
->error
);
540 case RXRPC_SKB_MARK_NET_ERROR
:
541 call
->error
= -rxrpc_kernel_get_error_number(skb
);
542 call
->state
= AFS_CALL_ERROR
;
543 _debug("Rcv NET ERROR %d", call
->error
);
545 case RXRPC_SKB_MARK_LOCAL_ERROR
:
546 call
->error
= -rxrpc_kernel_get_error_number(skb
);
547 call
->state
= AFS_CALL_ERROR
;
548 _debug("Rcv LOCAL ERROR %d", call
->error
);
558 /* make sure the queue is empty if the call is done with (we might have
559 * aborted the call early because of an unmarshalling error) */
560 if (call
->state
>= AFS_CALL_COMPLETE
) {
561 while ((skb
= skb_dequeue(&call
->rx_queue
)))
571 * wait synchronously for a call to complete
573 static int afs_wait_for_call_to_complete(struct afs_call
*call
)
578 DECLARE_WAITQUEUE(myself
, current
);
582 add_wait_queue(&call
->waitq
, &myself
);
584 set_current_state(TASK_INTERRUPTIBLE
);
586 /* deliver any messages that are in the queue */
587 if (!skb_queue_empty(&call
->rx_queue
)) {
588 __set_current_state(TASK_RUNNING
);
589 afs_deliver_to_call(call
);
594 if (call
->state
>= AFS_CALL_COMPLETE
)
597 if (signal_pending(current
))
602 remove_wait_queue(&call
->waitq
, &myself
);
603 __set_current_state(TASK_RUNNING
);
606 if (call
->state
< AFS_CALL_COMPLETE
) {
607 _debug("call incomplete");
608 rxrpc_kernel_abort_call(call
->rxcall
, RX_CALL_DEAD
);
609 while ((skb
= skb_dequeue(&call
->rx_queue
)))
613 _debug("call complete");
615 _leave(" = %d", ret
);
620 * wake up a waiting call
622 static void afs_wake_up_call_waiter(struct afs_call
*call
)
624 wake_up(&call
->waitq
);
628 * wake up an asynchronous call
630 static void afs_wake_up_async_call(struct afs_call
*call
)
633 queue_work(afs_async_calls
, &call
->async_work
);
637 * put a call into asynchronous mode
638 * - mustn't touch the call descriptor as the call my have completed by the
641 static int afs_dont_wait_for_call_to_complete(struct afs_call
*call
)
648 * delete an asynchronous call
650 static void afs_delete_async_call(struct afs_call
*call
)
660 * perform processing on an asynchronous call
661 * - on a multiple-thread workqueue this work item may try to run on several
662 * CPUs at the same time
664 static void afs_process_async_call(struct afs_call
*call
)
668 if (!skb_queue_empty(&call
->rx_queue
))
669 afs_deliver_to_call(call
);
671 if (call
->state
>= AFS_CALL_COMPLETE
&& call
->wait_mode
) {
672 if (call
->wait_mode
->async_complete
)
673 call
->wait_mode
->async_complete(call
->reply
,
678 afs_end_call_nofree(call
);
680 /* we can't just delete the call because the work item may be
682 call
->async_workfn
= afs_delete_async_call
;
683 queue_work(afs_async_calls
, &call
->async_work
);
690 * Empty a socket buffer into a flat reply buffer.
692 int afs_transfer_reply(struct afs_call
*call
, struct sk_buff
*skb
, bool last
)
694 size_t len
= skb
->len
;
696 if (len
> call
->reply_max
- call
->reply_size
) {
697 _leave(" = -EBADMSG [%zu > %u]",
698 len
, call
->reply_max
- call
->reply_size
);
703 if (skb_copy_bits(skb
, 0, call
->buffer
+ call
->reply_size
,
706 call
->reply_size
+= len
;
709 afs_data_consumed(call
, skb
);
713 if (call
->reply_size
!= call
->reply_max
) {
714 _leave(" = -EBADMSG [%u != %u]",
715 call
->reply_size
, call
->reply_max
);
722 * accept the backlog of incoming calls
724 static void afs_collect_incoming_call(struct work_struct
*work
)
726 struct rxrpc_call
*rxcall
;
727 struct afs_call
*call
= NULL
;
730 while ((skb
= skb_dequeue(&afs_incoming_calls
))) {
733 /* don't need the notification */
737 call
= kzalloc(sizeof(struct afs_call
), GFP_KERNEL
);
739 rxrpc_kernel_reject_call(afs_socket
);
743 call
->async_workfn
= afs_process_async_call
;
744 INIT_WORK(&call
->async_work
, afs_async_workfn
);
745 call
->wait_mode
= &afs_async_incoming_call
;
746 call
->type
= &afs_RXCMxxxx
;
747 init_waitqueue_head(&call
->waitq
);
748 skb_queue_head_init(&call
->rx_queue
);
749 call
->state
= AFS_CALL_AWAIT_OP_ID
;
751 _debug("CALL %p{%s} [%d]",
752 call
, call
->type
->name
,
753 atomic_read(&afs_outstanding_calls
));
754 atomic_inc(&afs_outstanding_calls
);
757 rxcall
= rxrpc_kernel_accept_call(afs_socket
,
758 (unsigned long) call
);
759 if (!IS_ERR(rxcall
)) {
760 call
->rxcall
= rxcall
;
770 * Grab the operation ID from an incoming cache manager call. The socket
771 * buffer is discarded on error or if we don't yet have sufficient data.
773 static int afs_deliver_cm_op_id(struct afs_call
*call
, struct sk_buff
*skb
,
776 size_t len
= skb
->len
;
777 void *oibuf
= (void *) &call
->operation_ID
;
779 _enter("{%u},{%zu},%d", call
->offset
, len
, last
);
781 ASSERTCMP(call
->offset
, <, 4);
783 /* the operation ID forms the first four bytes of the request data */
784 len
= min_t(size_t, len
, 4 - call
->offset
);
785 if (skb_copy_bits(skb
, 0, oibuf
+ call
->offset
, len
) < 0)
787 if (!pskb_pull(skb
, len
))
791 if (call
->offset
< 4) {
792 afs_data_consumed(call
, skb
);
793 _leave(" = -EAGAIN");
797 call
->state
= AFS_CALL_AWAIT_REQUEST
;
799 /* ask the cache manager to route the call (it'll change the call type
801 if (!afs_cm_incoming_call(call
))
804 /* pass responsibility for the remainer of this message off to the
805 * cache manager op */
806 return call
->type
->deliver(call
, skb
, last
);
810 * send an empty reply
812 void afs_send_empty_reply(struct afs_call
*call
)
820 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, NULL
, 0, 0);
821 msg
.msg_control
= NULL
;
822 msg
.msg_controllen
= 0;
825 call
->state
= AFS_CALL_AWAIT_ACK
;
826 switch (rxrpc_kernel_send_data(call
->rxcall
, &msg
, 0)) {
828 _leave(" [replied]");
833 rxrpc_kernel_abort_call(call
->rxcall
, RX_USER_ABORT
);
842 * send a simple reply
844 void afs_send_simple_reply(struct afs_call
*call
, const void *buf
, size_t len
)
852 iov
[0].iov_base
= (void *) buf
;
853 iov
[0].iov_len
= len
;
856 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, iov
, 1, len
);
857 msg
.msg_control
= NULL
;
858 msg
.msg_controllen
= 0;
861 call
->state
= AFS_CALL_AWAIT_ACK
;
862 n
= rxrpc_kernel_send_data(call
->rxcall
, &msg
, len
);
865 _leave(" [replied]");
871 rxrpc_kernel_abort_call(call
->rxcall
, RX_USER_ABORT
);
878 * Extract a piece of data from the received data socket buffers.
880 int afs_extract_data(struct afs_call
*call
, struct sk_buff
*skb
,
881 bool last
, void *buf
, size_t count
)
883 size_t len
= skb
->len
;
885 _enter("{%u},{%zu},%d,,%zu", call
->offset
, len
, last
, count
);
887 ASSERTCMP(call
->offset
, <, count
);
889 len
= min_t(size_t, len
, count
- call
->offset
);
890 if (skb_copy_bits(skb
, 0, buf
+ call
->offset
, len
) < 0 ||
891 !pskb_pull(skb
, len
))
895 if (call
->offset
< count
) {
896 afs_data_consumed(call
, skb
);
897 _leave(" = -EAGAIN");