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 * allocate a call with flat request and reply buffers
189 struct afs_call
*afs_alloc_flat_call(const struct afs_call_type
*type
,
190 size_t request_size
, size_t reply_size
)
192 struct afs_call
*call
;
194 call
= kzalloc(sizeof(*call
), GFP_NOFS
);
198 _debug("CALL %p{%s} [%d]",
199 call
, type
->name
, atomic_read(&afs_outstanding_calls
));
200 atomic_inc(&afs_outstanding_calls
);
203 call
->request_size
= request_size
;
204 call
->reply_max
= reply_size
;
207 call
->request
= kmalloc(request_size
, GFP_NOFS
);
213 call
->buffer
= kmalloc(reply_size
, GFP_NOFS
);
218 init_waitqueue_head(&call
->waitq
);
219 skb_queue_head_init(&call
->rx_queue
);
229 * clean up a call with flat buffer
231 void afs_flat_call_destructor(struct afs_call
*call
)
235 kfree(call
->request
);
236 call
->request
= NULL
;
242 * attach the data from a bunch of pages on an inode to a call
244 static int afs_send_pages(struct afs_call
*call
, struct msghdr
*msg
,
247 struct page
*pages
[8];
248 unsigned count
, n
, loop
, offset
, to
;
249 pgoff_t first
= call
->first
, last
= call
->last
;
254 offset
= call
->first_offset
;
255 call
->first_offset
= 0;
258 _debug("attach %lx-%lx", first
, last
);
260 count
= last
- first
+ 1;
261 if (count
> ARRAY_SIZE(pages
))
262 count
= ARRAY_SIZE(pages
);
263 n
= find_get_pages_contig(call
->mapping
, first
, count
, pages
);
264 ASSERTCMP(n
, ==, count
);
270 if (first
+ loop
>= last
)
273 msg
->msg_flags
= MSG_MORE
;
274 iov
->iov_base
= kmap(pages
[loop
]) + offset
;
275 iov
->iov_len
= to
- offset
;
278 _debug("- range %u-%u%s",
279 offset
, to
, msg
->msg_flags
? " [more]" : "");
280 msg
->msg_iov
= (struct iovec
*) iov
;
283 /* have to change the state *before* sending the last
284 * packet as RxRPC might give us the reply before it
285 * returns from sending the request */
286 if (first
+ loop
>= last
)
287 call
->state
= AFS_CALL_AWAIT_REPLY
;
288 ret
= rxrpc_kernel_send_data(call
->rxcall
, msg
,
293 } while (++loop
< count
);
296 for (loop
= 0; loop
< count
; loop
++)
297 put_page(pages
[loop
]);
300 } while (first
<= last
);
302 _leave(" = %d", ret
);
309 int afs_make_call(struct in_addr
*addr
, struct afs_call
*call
, gfp_t gfp
,
310 const struct afs_wait_mode
*wait_mode
)
312 struct sockaddr_rxrpc srx
;
313 struct rxrpc_call
*rxcall
;
319 _enter("%x,{%d},", addr
->s_addr
, ntohs(call
->port
));
321 ASSERT(call
->type
!= NULL
);
322 ASSERT(call
->type
->name
!= NULL
);
324 _debug("____MAKE %p{%s,%x} [%d]____",
325 call
, call
->type
->name
, key_serial(call
->key
),
326 atomic_read(&afs_outstanding_calls
));
328 call
->wait_mode
= wait_mode
;
329 INIT_WORK(&call
->async_work
, afs_process_async_call
);
331 memset(&srx
, 0, sizeof(srx
));
332 srx
.srx_family
= AF_RXRPC
;
333 srx
.srx_service
= call
->service_id
;
334 srx
.transport_type
= SOCK_DGRAM
;
335 srx
.transport_len
= sizeof(srx
.transport
.sin
);
336 srx
.transport
.sin
.sin_family
= AF_INET
;
337 srx
.transport
.sin
.sin_port
= call
->port
;
338 memcpy(&srx
.transport
.sin
.sin_addr
, addr
, 4);
341 rxcall
= rxrpc_kernel_begin_call(afs_socket
, &srx
, call
->key
,
342 (unsigned long) call
, gfp
);
344 if (IS_ERR(rxcall
)) {
345 ret
= PTR_ERR(rxcall
);
346 goto error_kill_call
;
349 call
->rxcall
= rxcall
;
351 /* send the request */
352 iov
[0].iov_base
= call
->request
;
353 iov
[0].iov_len
= call
->request_size
;
357 msg
.msg_iov
= (struct iovec
*) iov
;
359 msg
.msg_control
= NULL
;
360 msg
.msg_controllen
= 0;
361 msg
.msg_flags
= (call
->send_pages
? MSG_MORE
: 0);
363 /* have to change the state *before* sending the last packet as RxRPC
364 * might give us the reply before it returns from sending the
366 if (!call
->send_pages
)
367 call
->state
= AFS_CALL_AWAIT_REPLY
;
368 ret
= rxrpc_kernel_send_data(rxcall
, &msg
, call
->request_size
);
372 if (call
->send_pages
) {
373 ret
= afs_send_pages(call
, &msg
, iov
);
378 /* at this point, an async call may no longer exist as it may have
379 * already completed */
380 return wait_mode
->wait(call
);
383 rxrpc_kernel_abort_call(rxcall
, RX_USER_ABORT
);
384 while ((skb
= skb_dequeue(&call
->rx_queue
)))
386 rxrpc_kernel_end_call(rxcall
);
389 call
->type
->destructor(call
);
391 _leave(" = %d", ret
);
396 * handles intercepted messages that were arriving in the socket's Rx queue
397 * - called with the socket receive queue lock held to ensure message ordering
398 * - called with softirqs disabled
400 static void afs_rx_interceptor(struct sock
*sk
, unsigned long user_call_ID
,
403 struct afs_call
*call
= (struct afs_call
*) user_call_ID
;
405 _enter("%p,,%u", call
, skb
->mark
);
407 _debug("ICPT %p{%u} [%d]",
408 skb
, skb
->mark
, atomic_read(&afs_outstanding_skbs
));
410 ASSERTCMP(sk
, ==, afs_socket
->sk
);
411 atomic_inc(&afs_outstanding_skbs
);
414 /* its an incoming call for our callback service */
415 skb_queue_tail(&afs_incoming_calls
, skb
);
416 queue_work(afs_wq
, &afs_collect_incoming_call_work
);
418 /* route the messages directly to the appropriate call */
419 skb_queue_tail(&call
->rx_queue
, skb
);
420 call
->wait_mode
->rx_wakeup(call
);
427 * deliver messages to a call
429 static void afs_deliver_to_call(struct afs_call
*call
)
438 while ((call
->state
== AFS_CALL_AWAIT_REPLY
||
439 call
->state
== AFS_CALL_AWAIT_OP_ID
||
440 call
->state
== AFS_CALL_AWAIT_REQUEST
||
441 call
->state
== AFS_CALL_AWAIT_ACK
) &&
442 (skb
= skb_dequeue(&call
->rx_queue
))) {
444 case RXRPC_SKB_MARK_DATA
:
446 last
= rxrpc_kernel_is_data_last(skb
);
447 ret
= call
->type
->deliver(call
, skb
, last
);
451 call
->state
== AFS_CALL_AWAIT_REPLY
)
452 call
->state
= AFS_CALL_COMPLETE
;
455 abort_code
= RX_CALL_DEAD
;
458 abort_code
= RX_INVALID_OPERATION
;
461 abort_code
= RXGEN_CC_UNMARSHAL
;
462 if (call
->state
!= AFS_CALL_AWAIT_REPLY
)
463 abort_code
= RXGEN_SS_UNMARSHAL
;
465 rxrpc_kernel_abort_call(call
->rxcall
,
468 call
->state
= AFS_CALL_ERROR
;
471 afs_data_delivered(skb
);
474 case RXRPC_SKB_MARK_FINAL_ACK
:
476 call
->state
= AFS_CALL_COMPLETE
;
478 case RXRPC_SKB_MARK_BUSY
:
480 call
->error
= -EBUSY
;
481 call
->state
= AFS_CALL_BUSY
;
483 case RXRPC_SKB_MARK_REMOTE_ABORT
:
484 abort_code
= rxrpc_kernel_get_abort_code(skb
);
485 call
->error
= call
->type
->abort_to_error(abort_code
);
486 call
->state
= AFS_CALL_ABORTED
;
487 _debug("Rcv ABORT %u -> %d", abort_code
, call
->error
);
489 case RXRPC_SKB_MARK_NET_ERROR
:
490 call
->error
= -rxrpc_kernel_get_error_number(skb
);
491 call
->state
= AFS_CALL_ERROR
;
492 _debug("Rcv NET ERROR %d", call
->error
);
494 case RXRPC_SKB_MARK_LOCAL_ERROR
:
495 call
->error
= -rxrpc_kernel_get_error_number(skb
);
496 call
->state
= AFS_CALL_ERROR
;
497 _debug("Rcv LOCAL ERROR %d", call
->error
);
507 /* make sure the queue is empty if the call is done with (we might have
508 * aborted the call early because of an unmarshalling error) */
509 if (call
->state
>= AFS_CALL_COMPLETE
) {
510 while ((skb
= skb_dequeue(&call
->rx_queue
)))
512 if (call
->incoming
) {
513 rxrpc_kernel_end_call(call
->rxcall
);
515 call
->type
->destructor(call
);
524 * wait synchronously for a call to complete
526 static int afs_wait_for_call_to_complete(struct afs_call
*call
)
531 DECLARE_WAITQUEUE(myself
, current
);
535 add_wait_queue(&call
->waitq
, &myself
);
537 set_current_state(TASK_INTERRUPTIBLE
);
539 /* deliver any messages that are in the queue */
540 if (!skb_queue_empty(&call
->rx_queue
)) {
541 __set_current_state(TASK_RUNNING
);
542 afs_deliver_to_call(call
);
547 if (call
->state
>= AFS_CALL_COMPLETE
)
550 if (signal_pending(current
))
555 remove_wait_queue(&call
->waitq
, &myself
);
556 __set_current_state(TASK_RUNNING
);
559 if (call
->state
< AFS_CALL_COMPLETE
) {
560 _debug("call incomplete");
561 rxrpc_kernel_abort_call(call
->rxcall
, RX_CALL_DEAD
);
562 while ((skb
= skb_dequeue(&call
->rx_queue
)))
566 _debug("call complete");
567 rxrpc_kernel_end_call(call
->rxcall
);
569 call
->type
->destructor(call
);
571 _leave(" = %d", ret
);
576 * wake up a waiting call
578 static void afs_wake_up_call_waiter(struct afs_call
*call
)
580 wake_up(&call
->waitq
);
584 * wake up an asynchronous call
586 static void afs_wake_up_async_call(struct afs_call
*call
)
589 queue_work(afs_async_calls
, &call
->async_work
);
593 * put a call into asynchronous mode
594 * - mustn't touch the call descriptor as the call my have completed by the
597 static int afs_dont_wait_for_call_to_complete(struct afs_call
*call
)
604 * delete an asynchronous call
606 static void afs_delete_async_call(struct work_struct
*work
)
608 struct afs_call
*call
=
609 container_of(work
, struct afs_call
, async_work
);
619 * perform processing on an asynchronous call
620 * - on a multiple-thread workqueue this work item may try to run on several
621 * CPUs at the same time
623 static void afs_process_async_call(struct work_struct
*work
)
625 struct afs_call
*call
=
626 container_of(work
, struct afs_call
, async_work
);
630 if (!skb_queue_empty(&call
->rx_queue
))
631 afs_deliver_to_call(call
);
633 if (call
->state
>= AFS_CALL_COMPLETE
&& call
->wait_mode
) {
634 if (call
->wait_mode
->async_complete
)
635 call
->wait_mode
->async_complete(call
->reply
,
640 rxrpc_kernel_end_call(call
->rxcall
);
642 if (call
->type
->destructor
)
643 call
->type
->destructor(call
);
645 /* we can't just delete the call because the work item may be
647 PREPARE_WORK(&call
->async_work
, afs_delete_async_call
);
648 queue_work(afs_async_calls
, &call
->async_work
);
655 * empty a socket buffer into a flat reply buffer
657 void afs_transfer_reply(struct afs_call
*call
, struct sk_buff
*skb
)
659 size_t len
= skb
->len
;
661 if (skb_copy_bits(skb
, 0, call
->buffer
+ call
->reply_size
, len
) < 0)
663 call
->reply_size
+= len
;
667 * accept the backlog of incoming calls
669 static void afs_collect_incoming_call(struct work_struct
*work
)
671 struct rxrpc_call
*rxcall
;
672 struct afs_call
*call
= NULL
;
675 while ((skb
= skb_dequeue(&afs_incoming_calls
))) {
678 /* don't need the notification */
682 call
= kzalloc(sizeof(struct afs_call
), GFP_KERNEL
);
684 rxrpc_kernel_reject_call(afs_socket
);
688 INIT_WORK(&call
->async_work
, afs_process_async_call
);
689 call
->wait_mode
= &afs_async_incoming_call
;
690 call
->type
= &afs_RXCMxxxx
;
691 init_waitqueue_head(&call
->waitq
);
692 skb_queue_head_init(&call
->rx_queue
);
693 call
->state
= AFS_CALL_AWAIT_OP_ID
;
695 _debug("CALL %p{%s} [%d]",
696 call
, call
->type
->name
,
697 atomic_read(&afs_outstanding_calls
));
698 atomic_inc(&afs_outstanding_calls
);
701 rxcall
= rxrpc_kernel_accept_call(afs_socket
,
702 (unsigned long) call
);
703 if (!IS_ERR(rxcall
)) {
704 call
->rxcall
= rxcall
;
714 * grab the operation ID from an incoming cache manager call
716 static int afs_deliver_cm_op_id(struct afs_call
*call
, struct sk_buff
*skb
,
719 size_t len
= skb
->len
;
720 void *oibuf
= (void *) &call
->operation_ID
;
722 _enter("{%u},{%zu},%d", call
->offset
, len
, last
);
724 ASSERTCMP(call
->offset
, <, 4);
726 /* the operation ID forms the first four bytes of the request data */
727 len
= min_t(size_t, len
, 4 - call
->offset
);
728 if (skb_copy_bits(skb
, 0, oibuf
+ call
->offset
, len
) < 0)
730 if (!pskb_pull(skb
, len
))
734 if (call
->offset
< 4) {
736 _leave(" = -EBADMSG [op ID short]");
739 _leave(" = 0 [incomplete]");
743 call
->state
= AFS_CALL_AWAIT_REQUEST
;
745 /* ask the cache manager to route the call (it'll change the call type
747 if (!afs_cm_incoming_call(call
))
750 /* pass responsibility for the remainer of this message off to the
751 * cache manager op */
752 return call
->type
->deliver(call
, skb
, last
);
756 * send an empty reply
758 void afs_send_empty_reply(struct afs_call
*call
)
765 iov
[0].iov_base
= NULL
;
771 msg
.msg_control
= NULL
;
772 msg
.msg_controllen
= 0;
775 call
->state
= AFS_CALL_AWAIT_ACK
;
776 switch (rxrpc_kernel_send_data(call
->rxcall
, &msg
, 0)) {
778 _leave(" [replied]");
783 rxrpc_kernel_abort_call(call
->rxcall
, RX_USER_ABORT
);
785 rxrpc_kernel_end_call(call
->rxcall
);
787 call
->type
->destructor(call
);
795 * send a simple reply
797 void afs_send_simple_reply(struct afs_call
*call
, const void *buf
, size_t len
)
805 iov
[0].iov_base
= (void *) buf
;
806 iov
[0].iov_len
= len
;
811 msg
.msg_control
= NULL
;
812 msg
.msg_controllen
= 0;
815 call
->state
= AFS_CALL_AWAIT_ACK
;
816 n
= rxrpc_kernel_send_data(call
->rxcall
, &msg
, len
);
818 _leave(" [replied]");
823 rxrpc_kernel_abort_call(call
->rxcall
, RX_USER_ABORT
);
825 rxrpc_kernel_end_call(call
->rxcall
);
827 call
->type
->destructor(call
);
833 * extract a piece of data from the received data socket buffers
835 int afs_extract_data(struct afs_call
*call
, struct sk_buff
*skb
,
836 bool last
, void *buf
, size_t count
)
838 size_t len
= skb
->len
;
840 _enter("{%u},{%zu},%d,,%zu", call
->offset
, len
, last
, count
);
842 ASSERTCMP(call
->offset
, <, count
);
844 len
= min_t(size_t, len
, count
- call
->offset
);
845 if (skb_copy_bits(skb
, 0, buf
+ call
->offset
, len
) < 0 ||
846 !pskb_pull(skb
, len
))
850 if (call
->offset
< count
) {
852 _leave(" = -EBADMSG [%d < %zu]", call
->offset
, count
);
855 _leave(" = -EAGAIN");