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.
13 #include <net/af_rxrpc.h>
14 #include <rxrpc/packet.h>
18 static struct socket
*afs_socket
; /* my RxRPC socket */
19 static struct workqueue_struct
*afs_async_calls
;
21 static void afs_wake_up_call_waiter(struct afs_call
*);
22 static int afs_wait_for_call_to_complete(struct afs_call
*);
23 static void afs_wake_up_async_call(struct afs_call
*);
24 static int afs_dont_wait_for_call_to_complete(struct afs_call
*);
25 static void afs_process_async_call(struct work_struct
*);
26 static void afs_rx_interceptor(struct sock
*, unsigned long, struct sk_buff
*);
27 static int afs_deliver_cm_op_id(struct afs_call
*, struct sk_buff
*, bool);
29 /* synchronous call management */
30 const struct afs_wait_mode afs_sync_call
= {
31 .rx_wakeup
= afs_wake_up_call_waiter
,
32 .wait
= afs_wait_for_call_to_complete
,
35 /* asynchronous call management */
36 const struct afs_wait_mode afs_async_call
= {
37 .rx_wakeup
= afs_wake_up_async_call
,
38 .wait
= afs_dont_wait_for_call_to_complete
,
41 /* asynchronous incoming call management */
42 static const struct afs_wait_mode afs_async_incoming_call
= {
43 .rx_wakeup
= afs_wake_up_async_call
,
46 /* asynchronous incoming call initial processing */
47 static const struct afs_call_type afs_RXCMxxxx
= {
48 .deliver
= afs_deliver_cm_op_id
,
49 .abort_to_error
= afs_abort_to_error
,
52 static void afs_collect_incoming_call(struct work_struct
*);
54 static struct sk_buff_head afs_incoming_calls
;
55 static DECLARE_WORK(afs_collect_incoming_call_work
, afs_collect_incoming_call
);
58 * open an RxRPC socket and bind it to be a server for callback notifications
59 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
61 int afs_open_socket(void)
63 struct sockaddr_rxrpc srx
;
64 struct socket
*socket
;
69 skb_queue_head_init(&afs_incoming_calls
);
71 afs_async_calls
= create_singlethread_workqueue("kafsd");
72 if (!afs_async_calls
) {
73 _leave(" = -ENOMEM [wq]");
77 ret
= sock_create_kern(AF_RXRPC
, SOCK_DGRAM
, PF_INET
, &socket
);
79 destroy_workqueue(afs_async_calls
);
80 _leave(" = %d [socket]", ret
);
84 socket
->sk
->sk_allocation
= GFP_NOFS
;
86 /* bind the callback manager's address to make this a server socket */
87 srx
.srx_family
= AF_RXRPC
;
88 srx
.srx_service
= CM_SERVICE
;
89 srx
.transport_type
= SOCK_DGRAM
;
90 srx
.transport_len
= sizeof(srx
.transport
.sin
);
91 srx
.transport
.sin
.sin_family
= AF_INET
;
92 srx
.transport
.sin
.sin_port
= htons(AFS_CM_PORT
);
93 memset(&srx
.transport
.sin
.sin_addr
, 0,
94 sizeof(srx
.transport
.sin
.sin_addr
));
96 ret
= kernel_bind(socket
, (struct sockaddr
*) &srx
, sizeof(srx
));
99 _leave(" = %d [bind]", ret
);
103 rxrpc_kernel_intercept_rx_messages(socket
, afs_rx_interceptor
);
111 * close the RxRPC socket AFS was using
113 void afs_close_socket(void)
117 sock_release(afs_socket
);
120 destroy_workqueue(afs_async_calls
);
125 * allocate a call with flat request and reply buffers
127 struct afs_call
*afs_alloc_flat_call(const struct afs_call_type
*type
,
128 size_t request_size
, size_t reply_size
)
130 struct afs_call
*call
;
132 call
= kzalloc(sizeof(*call
), GFP_NOFS
);
137 call
->request
= kmalloc(request_size
, GFP_NOFS
);
143 call
->buffer
= kmalloc(reply_size
, GFP_NOFS
);
149 call
->request_size
= request_size
;
150 call
->reply_max
= reply_size
;
152 init_waitqueue_head(&call
->waitq
);
153 skb_queue_head_init(&call
->rx_queue
);
157 kfree(call
->request
);
165 * clean up a call with flat buffer
167 void afs_flat_call_destructor(struct afs_call
*call
)
171 kfree(call
->request
);
172 call
->request
= NULL
;
180 int afs_make_call(struct in_addr
*addr
, struct afs_call
*call
, gfp_t gfp
,
181 const struct afs_wait_mode
*wait_mode
)
183 struct sockaddr_rxrpc srx
;
184 struct rxrpc_call
*rxcall
;
189 _enter("%x,{%d},", addr
->s_addr
, ntohs(call
->port
));
191 call
->wait_mode
= wait_mode
;
192 INIT_WORK(&call
->async_work
, afs_process_async_call
);
194 memset(&srx
, 0, sizeof(srx
));
195 srx
.srx_family
= AF_RXRPC
;
196 srx
.srx_service
= call
->service_id
;
197 srx
.transport_type
= SOCK_DGRAM
;
198 srx
.transport_len
= sizeof(srx
.transport
.sin
);
199 srx
.transport
.sin
.sin_family
= AF_INET
;
200 srx
.transport
.sin
.sin_port
= call
->port
;
201 memcpy(&srx
.transport
.sin
.sin_addr
, addr
, 4);
204 rxcall
= rxrpc_kernel_begin_call(afs_socket
, &srx
, call
->key
,
205 (unsigned long) call
, gfp
);
206 if (IS_ERR(rxcall
)) {
207 ret
= PTR_ERR(rxcall
);
208 goto error_kill_call
;
211 call
->rxcall
= rxcall
;
213 /* send the request */
214 iov
[0].iov_base
= call
->request
;
215 iov
[0].iov_len
= call
->request_size
;
219 msg
.msg_iov
= (struct iovec
*) iov
;
221 msg
.msg_control
= NULL
;
222 msg
.msg_controllen
= 0;
225 /* have to change the state *before* sending the last packet as RxRPC
226 * might give us the reply before it returns from sending the
228 call
->state
= AFS_CALL_AWAIT_REPLY
;
229 ret
= rxrpc_kernel_send_data(rxcall
, &msg
, call
->request_size
);
233 /* at this point, an async call may no longer exist as it may have
234 * already completed */
235 return wait_mode
->wait(call
);
238 rxrpc_kernel_abort_call(rxcall
, RX_USER_ABORT
);
239 rxrpc_kernel_end_call(rxcall
);
241 call
->type
->destructor(call
);
242 ASSERT(skb_queue_empty(&call
->rx_queue
));
244 _leave(" = %d", ret
);
249 * handles intercepted messages that were arriving in the socket's Rx queue
250 * - called with the socket receive queue lock held to ensure message ordering
251 * - called with softirqs disabled
253 static void afs_rx_interceptor(struct sock
*sk
, unsigned long user_call_ID
,
256 struct afs_call
*call
= (struct afs_call
*) user_call_ID
;
258 _enter("%p,,%u", call
, skb
->mark
);
260 ASSERTCMP(sk
, ==, afs_socket
->sk
);
263 /* its an incoming call for our callback service */
264 __skb_queue_tail(&afs_incoming_calls
, skb
);
265 schedule_work(&afs_collect_incoming_call_work
);
267 /* route the messages directly to the appropriate call */
268 __skb_queue_tail(&call
->rx_queue
, skb
);
269 call
->wait_mode
->rx_wakeup(call
);
276 * deliver messages to a call
278 static void afs_deliver_to_call(struct afs_call
*call
)
287 while ((call
->state
== AFS_CALL_AWAIT_REPLY
||
288 call
->state
== AFS_CALL_AWAIT_OP_ID
||
289 call
->state
== AFS_CALL_AWAIT_REQUEST
||
290 call
->state
== AFS_CALL_AWAIT_ACK
) &&
291 (skb
= skb_dequeue(&call
->rx_queue
))) {
293 case RXRPC_SKB_MARK_DATA
:
295 last
= rxrpc_kernel_is_data_last(skb
);
296 ret
= call
->type
->deliver(call
, skb
, last
);
300 call
->state
== AFS_CALL_AWAIT_REPLY
)
301 call
->state
= AFS_CALL_COMPLETE
;
304 abort_code
= RX_CALL_DEAD
;
307 abort_code
= RX_INVALID_OPERATION
;
310 abort_code
= RXGEN_CC_UNMARSHAL
;
311 if (call
->state
!= AFS_CALL_AWAIT_REPLY
)
312 abort_code
= RXGEN_SS_UNMARSHAL
;
314 rxrpc_kernel_abort_call(call
->rxcall
,
317 call
->state
= AFS_CALL_ERROR
;
320 rxrpc_kernel_data_delivered(skb
);
323 case RXRPC_SKB_MARK_FINAL_ACK
:
325 call
->state
= AFS_CALL_COMPLETE
;
327 case RXRPC_SKB_MARK_BUSY
:
329 call
->error
= -EBUSY
;
330 call
->state
= AFS_CALL_BUSY
;
332 case RXRPC_SKB_MARK_REMOTE_ABORT
:
333 abort_code
= rxrpc_kernel_get_abort_code(skb
);
334 call
->error
= call
->type
->abort_to_error(abort_code
);
335 call
->state
= AFS_CALL_ABORTED
;
336 _debug("Rcv ABORT %u -> %d", abort_code
, call
->error
);
338 case RXRPC_SKB_MARK_NET_ERROR
:
339 call
->error
= -rxrpc_kernel_get_error_number(skb
);
340 call
->state
= AFS_CALL_ERROR
;
341 _debug("Rcv NET ERROR %d", call
->error
);
343 case RXRPC_SKB_MARK_LOCAL_ERROR
:
344 call
->error
= -rxrpc_kernel_get_error_number(skb
);
345 call
->state
= AFS_CALL_ERROR
;
346 _debug("Rcv LOCAL ERROR %d", call
->error
);
353 rxrpc_kernel_free_skb(skb
);
356 /* make sure the queue is empty if the call is done with (we might have
357 * aborted the call early because of an unmarshalling error) */
358 if (call
->state
>= AFS_CALL_COMPLETE
) {
359 while ((skb
= skb_dequeue(&call
->rx_queue
)))
360 rxrpc_kernel_free_skb(skb
);
361 if (call
->incoming
) {
362 rxrpc_kernel_end_call(call
->rxcall
);
363 call
->type
->destructor(call
);
364 ASSERT(skb_queue_empty(&call
->rx_queue
));
373 * wait synchronously for a call to complete
375 static int afs_wait_for_call_to_complete(struct afs_call
*call
)
380 DECLARE_WAITQUEUE(myself
, current
);
384 add_wait_queue(&call
->waitq
, &myself
);
386 set_current_state(TASK_INTERRUPTIBLE
);
388 /* deliver any messages that are in the queue */
389 if (!skb_queue_empty(&call
->rx_queue
)) {
390 __set_current_state(TASK_RUNNING
);
391 afs_deliver_to_call(call
);
396 if (call
->state
>= AFS_CALL_COMPLETE
)
399 if (signal_pending(current
))
404 remove_wait_queue(&call
->waitq
, &myself
);
405 __set_current_state(TASK_RUNNING
);
408 if (call
->state
< AFS_CALL_COMPLETE
) {
409 _debug("call incomplete");
410 rxrpc_kernel_abort_call(call
->rxcall
, RX_CALL_DEAD
);
411 while ((skb
= skb_dequeue(&call
->rx_queue
)))
412 rxrpc_kernel_free_skb(skb
);
415 _debug("call complete");
416 rxrpc_kernel_end_call(call
->rxcall
);
417 call
->type
->destructor(call
);
418 ASSERT(skb_queue_empty(&call
->rx_queue
));
420 _leave(" = %d", ret
);
425 * wake up a waiting call
427 static void afs_wake_up_call_waiter(struct afs_call
*call
)
429 wake_up(&call
->waitq
);
433 * wake up an asynchronous call
435 static void afs_wake_up_async_call(struct afs_call
*call
)
438 queue_work(afs_async_calls
, &call
->async_work
);
442 * put a call into asynchronous mode
443 * - mustn't touch the call descriptor as the call my have completed by the
446 static int afs_dont_wait_for_call_to_complete(struct afs_call
*call
)
453 * delete an asynchronous call
455 static void afs_delete_async_call(struct work_struct
*work
)
457 struct afs_call
*call
=
458 container_of(work
, struct afs_call
, async_work
);
462 ASSERT(skb_queue_empty(&call
->rx_queue
));
463 ASSERT(!work_pending(&call
->async_work
));
470 * perform processing on an asynchronous call
471 * - on a multiple-thread workqueue this work item may try to run on several
472 * CPUs at the same time
474 static void afs_process_async_call(struct work_struct
*work
)
476 struct afs_call
*call
=
477 container_of(work
, struct afs_call
, async_work
);
481 if (!skb_queue_empty(&call
->rx_queue
))
482 afs_deliver_to_call(call
);
484 if (call
->state
>= AFS_CALL_COMPLETE
&& call
->wait_mode
) {
485 if (call
->wait_mode
->async_complete
)
486 call
->wait_mode
->async_complete(call
->reply
,
491 rxrpc_kernel_end_call(call
->rxcall
);
492 if (call
->type
->destructor
)
493 call
->type
->destructor(call
);
495 /* we can't just delete the call because the work item may be
497 PREPARE_WORK(&call
->async_work
, afs_delete_async_call
);
498 queue_work(afs_async_calls
, &call
->async_work
);
505 * empty a socket buffer into a flat reply buffer
507 void afs_transfer_reply(struct afs_call
*call
, struct sk_buff
*skb
)
509 size_t len
= skb
->len
;
511 if (skb_copy_bits(skb
, 0, call
->buffer
+ call
->reply_size
, len
) < 0)
513 call
->reply_size
+= len
;
517 * accept the backlog of incoming calls
519 static void afs_collect_incoming_call(struct work_struct
*work
)
521 struct rxrpc_call
*rxcall
;
522 struct afs_call
*call
= NULL
;
525 while ((skb
= skb_dequeue(&afs_incoming_calls
))) {
528 /* don't need the notification */
529 rxrpc_kernel_free_skb(skb
);
532 call
= kzalloc(sizeof(struct afs_call
), GFP_KERNEL
);
534 rxrpc_kernel_reject_call(afs_socket
);
538 INIT_WORK(&call
->async_work
, afs_process_async_call
);
539 call
->wait_mode
= &afs_async_incoming_call
;
540 call
->type
= &afs_RXCMxxxx
;
541 init_waitqueue_head(&call
->waitq
);
542 skb_queue_head_init(&call
->rx_queue
);
543 call
->state
= AFS_CALL_AWAIT_OP_ID
;
546 rxcall
= rxrpc_kernel_accept_call(afs_socket
,
547 (unsigned long) call
);
548 if (!IS_ERR(rxcall
)) {
549 call
->rxcall
= rxcall
;
558 * grab the operation ID from an incoming cache manager call
560 static int afs_deliver_cm_op_id(struct afs_call
*call
, struct sk_buff
*skb
,
563 size_t len
= skb
->len
;
564 void *oibuf
= (void *) &call
->operation_ID
;
566 _enter("{%u},{%zu},%d", call
->offset
, len
, last
);
568 ASSERTCMP(call
->offset
, <, 4);
570 /* the operation ID forms the first four bytes of the request data */
571 len
= min_t(size_t, len
, 4 - call
->offset
);
572 if (skb_copy_bits(skb
, 0, oibuf
+ call
->offset
, len
) < 0)
574 if (!pskb_pull(skb
, len
))
578 if (call
->offset
< 4) {
580 _leave(" = -EBADMSG [op ID short]");
583 _leave(" = 0 [incomplete]");
587 call
->state
= AFS_CALL_AWAIT_REQUEST
;
589 /* ask the cache manager to route the call (it'll change the call type
591 if (!afs_cm_incoming_call(call
))
594 /* pass responsibility for the remainer of this message off to the
595 * cache manager op */
596 return call
->type
->deliver(call
, skb
, last
);
600 * send an empty reply
602 void afs_send_empty_reply(struct afs_call
*call
)
609 iov
[0].iov_base
= NULL
;
615 msg
.msg_control
= NULL
;
616 msg
.msg_controllen
= 0;
619 call
->state
= AFS_CALL_AWAIT_ACK
;
620 switch (rxrpc_kernel_send_data(call
->rxcall
, &msg
, 0)) {
622 _leave(" [replied]");
627 rxrpc_kernel_abort_call(call
->rxcall
, RX_USER_ABORT
);
629 rxrpc_kernel_end_call(call
->rxcall
);
631 call
->type
->destructor(call
);
632 ASSERT(skb_queue_empty(&call
->rx_queue
));
640 * extract a piece of data from the received data socket buffers
642 int afs_extract_data(struct afs_call
*call
, struct sk_buff
*skb
,
643 bool last
, void *buf
, size_t count
)
645 size_t len
= skb
->len
;
647 _enter("{%u},{%zu},%d,,%zu", call
->offset
, len
, last
, count
);
649 ASSERTCMP(call
->offset
, <, count
);
651 len
= min_t(size_t, len
, count
- call
->offset
);
652 if (skb_copy_bits(skb
, 0, buf
+ call
->offset
, len
) < 0 ||
653 !pskb_pull(skb
, len
))
657 if (call
->offset
< count
) {
659 _leave(" = -EBADMSG [%d < %lu]", call
->offset
, count
);
662 _leave(" = -EAGAIN");