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>
13 #include <linux/sched/signal.h>
16 #include <net/af_rxrpc.h>
17 #include <rxrpc/packet.h>
21 struct socket
*afs_socket
; /* my RxRPC socket */
22 static struct workqueue_struct
*afs_async_calls
;
23 static struct afs_call
*afs_spare_incoming_call
;
24 atomic_t afs_outstanding_calls
;
26 static void afs_wake_up_call_waiter(struct sock
*, struct rxrpc_call
*, unsigned long);
27 static int afs_wait_for_call_to_complete(struct afs_call
*);
28 static void afs_wake_up_async_call(struct sock
*, struct rxrpc_call
*, unsigned long);
29 static void afs_process_async_call(struct work_struct
*);
30 static void afs_rx_new_call(struct sock
*, struct rxrpc_call
*, unsigned long);
31 static void afs_rx_discard_new_call(struct rxrpc_call
*, unsigned long);
32 static int afs_deliver_cm_op_id(struct afs_call
*);
34 /* asynchronous incoming call initial processing */
35 static const struct afs_call_type afs_RXCMxxxx
= {
37 .deliver
= afs_deliver_cm_op_id
,
38 .abort_to_error
= afs_abort_to_error
,
41 static void afs_charge_preallocation(struct work_struct
*);
43 static DECLARE_WORK(afs_charge_preallocation_work
, afs_charge_preallocation
);
45 static int afs_wait_atomic_t(atomic_t
*p
)
52 * open an RxRPC socket and bind it to be a server for callback notifications
53 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
55 int afs_open_socket(void)
57 struct sockaddr_rxrpc srx
;
58 struct socket
*socket
;
64 afs_async_calls
= alloc_workqueue("kafsd", WQ_MEM_RECLAIM
, 0);
68 ret
= sock_create_kern(&init_net
, AF_RXRPC
, SOCK_DGRAM
, PF_INET
, &socket
);
72 socket
->sk
->sk_allocation
= GFP_NOFS
;
74 /* bind the callback manager's address to make this a server socket */
75 srx
.srx_family
= AF_RXRPC
;
76 srx
.srx_service
= CM_SERVICE
;
77 srx
.transport_type
= SOCK_DGRAM
;
78 srx
.transport_len
= sizeof(srx
.transport
.sin
);
79 srx
.transport
.sin
.sin_family
= AF_INET
;
80 srx
.transport
.sin
.sin_port
= htons(AFS_CM_PORT
);
81 memset(&srx
.transport
.sin
.sin_addr
, 0,
82 sizeof(srx
.transport
.sin
.sin_addr
));
84 ret
= kernel_bind(socket
, (struct sockaddr
*) &srx
, sizeof(srx
));
88 rxrpc_kernel_new_call_notification(socket
, afs_rx_new_call
,
89 afs_rx_discard_new_call
);
91 ret
= kernel_listen(socket
, INT_MAX
);
96 afs_charge_preallocation(NULL
);
101 sock_release(socket
);
103 destroy_workqueue(afs_async_calls
);
105 _leave(" = %d", ret
);
110 * close the RxRPC socket AFS was using
112 void afs_close_socket(void)
116 kernel_listen(afs_socket
, 0);
117 flush_workqueue(afs_async_calls
);
119 if (afs_spare_incoming_call
) {
120 afs_put_call(afs_spare_incoming_call
);
121 afs_spare_incoming_call
= NULL
;
124 _debug("outstanding %u", atomic_read(&afs_outstanding_calls
));
125 wait_on_atomic_t(&afs_outstanding_calls
, afs_wait_atomic_t
,
126 TASK_UNINTERRUPTIBLE
);
127 _debug("no outstanding calls");
129 kernel_sock_shutdown(afs_socket
, SHUT_RDWR
);
130 flush_workqueue(afs_async_calls
);
131 sock_release(afs_socket
);
134 destroy_workqueue(afs_async_calls
);
141 static struct afs_call
*afs_alloc_call(const struct afs_call_type
*type
,
144 struct afs_call
*call
;
147 call
= kzalloc(sizeof(*call
), gfp
);
152 atomic_set(&call
->usage
, 1);
153 INIT_WORK(&call
->async_work
, afs_process_async_call
);
154 init_waitqueue_head(&call
->waitq
);
156 o
= atomic_inc_return(&afs_outstanding_calls
);
157 trace_afs_call(call
, afs_call_trace_alloc
, 1, o
,
158 __builtin_return_address(0));
163 * Dispose of a reference on a call.
165 void afs_put_call(struct afs_call
*call
)
167 int n
= atomic_dec_return(&call
->usage
);
168 int o
= atomic_read(&afs_outstanding_calls
);
170 trace_afs_call(call
, afs_call_trace_put
, n
+ 1, o
,
171 __builtin_return_address(0));
175 ASSERT(!work_pending(&call
->async_work
));
176 ASSERT(call
->type
->name
!= NULL
);
179 rxrpc_kernel_end_call(afs_socket
, call
->rxcall
);
182 if (call
->type
->destructor
)
183 call
->type
->destructor(call
);
185 kfree(call
->request
);
188 o
= atomic_dec_return(&afs_outstanding_calls
);
189 trace_afs_call(call
, afs_call_trace_free
, 0, o
,
190 __builtin_return_address(0));
192 wake_up_atomic_t(&afs_outstanding_calls
);
197 * Queue the call for actual work. Returns 0 unconditionally for convenience.
199 int afs_queue_call_work(struct afs_call
*call
)
201 int u
= atomic_inc_return(&call
->usage
);
203 trace_afs_call(call
, afs_call_trace_work
, u
,
204 atomic_read(&afs_outstanding_calls
),
205 __builtin_return_address(0));
207 INIT_WORK(&call
->work
, call
->type
->work
);
209 if (!queue_work(afs_wq
, &call
->work
))
215 * allocate a call with flat request and reply buffers
217 struct afs_call
*afs_alloc_flat_call(const struct afs_call_type
*type
,
218 size_t request_size
, size_t reply_max
)
220 struct afs_call
*call
;
222 call
= afs_alloc_call(type
, GFP_NOFS
);
227 call
->request_size
= request_size
;
228 call
->request
= kmalloc(request_size
, GFP_NOFS
);
234 call
->reply_max
= reply_max
;
235 call
->buffer
= kmalloc(reply_max
, GFP_NOFS
);
240 init_waitqueue_head(&call
->waitq
);
250 * clean up a call with flat buffer
252 void afs_flat_call_destructor(struct afs_call
*call
)
256 kfree(call
->request
);
257 call
->request
= NULL
;
262 #define AFS_BVEC_MAX 8
265 * Load the given bvec with the next few pages.
267 static void afs_load_bvec(struct afs_call
*call
, struct msghdr
*msg
,
268 struct bio_vec
*bv
, pgoff_t first
, pgoff_t last
,
271 struct page
*pages
[AFS_BVEC_MAX
];
272 unsigned int nr
, n
, i
, to
, bytes
= 0;
274 nr
= min_t(pgoff_t
, last
- first
+ 1, AFS_BVEC_MAX
);
275 n
= find_get_pages_contig(call
->mapping
, first
, nr
, pages
);
276 ASSERTCMP(n
, ==, nr
);
278 msg
->msg_flags
|= MSG_MORE
;
279 for (i
= 0; i
< nr
; i
++) {
281 if (first
+ i
>= last
) {
283 msg
->msg_flags
&= ~MSG_MORE
;
285 bv
[i
].bv_page
= pages
[i
];
286 bv
[i
].bv_len
= to
- offset
;
287 bv
[i
].bv_offset
= offset
;
288 bytes
+= to
- offset
;
292 iov_iter_bvec(&msg
->msg_iter
, WRITE
| ITER_BVEC
, bv
, nr
, bytes
);
296 * attach the data from a bunch of pages on an inode to a call
298 static int afs_send_pages(struct afs_call
*call
, struct msghdr
*msg
)
300 struct bio_vec bv
[AFS_BVEC_MAX
];
301 unsigned int bytes
, nr
, loop
, offset
;
302 pgoff_t first
= call
->first
, last
= call
->last
;
305 offset
= call
->first_offset
;
306 call
->first_offset
= 0;
309 afs_load_bvec(call
, msg
, bv
, first
, last
, offset
);
311 bytes
= msg
->msg_iter
.count
;
312 nr
= msg
->msg_iter
.nr_segs
;
314 /* Have to change the state *before* sending the last
315 * packet as RxRPC might give us the reply before it
316 * returns from sending the request.
318 if (first
+ nr
- 1 >= last
)
319 call
->state
= AFS_CALL_AWAIT_REPLY
;
320 ret
= rxrpc_kernel_send_data(afs_socket
, call
->rxcall
,
322 for (loop
= 0; loop
< nr
; loop
++)
323 put_page(bv
[loop
].bv_page
);
328 } while (first
<= last
);
336 int afs_make_call(struct in_addr
*addr
, struct afs_call
*call
, gfp_t gfp
,
339 struct sockaddr_rxrpc srx
;
340 struct rxrpc_call
*rxcall
;
347 _enter("%x,{%d},", addr
->s_addr
, ntohs(call
->port
));
349 ASSERT(call
->type
!= NULL
);
350 ASSERT(call
->type
->name
!= NULL
);
352 _debug("____MAKE %p{%s,%x} [%d]____",
353 call
, call
->type
->name
, key_serial(call
->key
),
354 atomic_read(&afs_outstanding_calls
));
358 memset(&srx
, 0, sizeof(srx
));
359 srx
.srx_family
= AF_RXRPC
;
360 srx
.srx_service
= call
->service_id
;
361 srx
.transport_type
= SOCK_DGRAM
;
362 srx
.transport_len
= sizeof(srx
.transport
.sin
);
363 srx
.transport
.sin
.sin_family
= AF_INET
;
364 srx
.transport
.sin
.sin_port
= call
->port
;
365 memcpy(&srx
.transport
.sin
.sin_addr
, addr
, 4);
368 rxcall
= rxrpc_kernel_begin_call(afs_socket
, &srx
, call
->key
,
369 (unsigned long) call
, gfp
,
371 afs_wake_up_async_call
:
372 afs_wake_up_call_waiter
));
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 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, iov
, 1,
389 msg
.msg_control
= NULL
;
390 msg
.msg_controllen
= 0;
391 msg
.msg_flags
= (call
->send_pages
? MSG_MORE
: 0);
393 /* We have to change the state *before* sending the last packet as
394 * rxrpc might give us the reply before it returns from sending the
395 * request. Further, if the send fails, we may already have been given
396 * a notification and may have collected it.
398 if (!call
->send_pages
)
399 call
->state
= AFS_CALL_AWAIT_REPLY
;
400 ret
= rxrpc_kernel_send_data(afs_socket
, rxcall
,
401 &msg
, call
->request_size
);
405 if (call
->send_pages
) {
406 ret
= afs_send_pages(call
, &msg
);
411 /* at this point, an async call may no longer exist as it may have
412 * already completed */
416 return afs_wait_for_call_to_complete(call
);
419 call
->state
= AFS_CALL_COMPLETE
;
420 if (ret
!= -ECONNABORTED
) {
421 rxrpc_kernel_abort_call(afs_socket
, rxcall
, RX_USER_ABORT
,
426 rxrpc_kernel_recv_data(afs_socket
, rxcall
, NULL
, 0, &offset
,
428 ret
= call
->type
->abort_to_error(abort_code
);
432 _leave(" = %d", ret
);
437 * deliver messages to a call
439 static void afs_deliver_to_call(struct afs_call
*call
)
444 _enter("%s", call
->type
->name
);
446 while (call
->state
== AFS_CALL_AWAIT_REPLY
||
447 call
->state
== AFS_CALL_AWAIT_OP_ID
||
448 call
->state
== AFS_CALL_AWAIT_REQUEST
||
449 call
->state
== AFS_CALL_AWAIT_ACK
451 if (call
->state
== AFS_CALL_AWAIT_ACK
) {
453 ret
= rxrpc_kernel_recv_data(afs_socket
, call
->rxcall
,
454 NULL
, 0, &offset
, false,
456 trace_afs_recv_data(call
, 0, offset
, false, ret
);
458 if (ret
== -EINPROGRESS
|| ret
== -EAGAIN
)
460 if (ret
== 1 || ret
< 0) {
461 call
->state
= AFS_CALL_COMPLETE
;
467 ret
= call
->type
->deliver(call
);
470 if (call
->state
== AFS_CALL_AWAIT_REPLY
)
471 call
->state
= AFS_CALL_COMPLETE
;
479 abort_code
= RX_CALL_DEAD
;
480 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
481 abort_code
, -ret
, "KNC");
484 abort_code
= RXGEN_OPCODE
;
485 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
486 abort_code
, -ret
, "KIV");
492 abort_code
= RXGEN_CC_UNMARSHAL
;
493 if (call
->state
!= AFS_CALL_AWAIT_REPLY
)
494 abort_code
= RXGEN_SS_UNMARSHAL
;
495 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
496 abort_code
, EBADMSG
, "KUM");
502 if (call
->state
== AFS_CALL_COMPLETE
&& call
->incoming
)
511 call
->state
= AFS_CALL_COMPLETE
;
516 * wait synchronously for a call to complete
518 static int afs_wait_for_call_to_complete(struct afs_call
*call
)
522 DECLARE_WAITQUEUE(myself
, current
);
526 add_wait_queue(&call
->waitq
, &myself
);
528 set_current_state(TASK_INTERRUPTIBLE
);
530 /* deliver any messages that are in the queue */
531 if (call
->state
< AFS_CALL_COMPLETE
&& call
->need_attention
) {
532 call
->need_attention
= false;
533 __set_current_state(TASK_RUNNING
);
534 afs_deliver_to_call(call
);
538 if (call
->state
== AFS_CALL_COMPLETE
||
539 signal_pending(current
))
544 remove_wait_queue(&call
->waitq
, &myself
);
545 __set_current_state(TASK_RUNNING
);
547 /* Kill off the call if it's still live. */
548 if (call
->state
< AFS_CALL_COMPLETE
) {
549 _debug("call interrupted");
550 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
551 RX_USER_ABORT
, -EINTR
, "KWI");
555 _debug("call complete");
557 _leave(" = %d", ret
);
562 * wake up a waiting call
564 static void afs_wake_up_call_waiter(struct sock
*sk
, struct rxrpc_call
*rxcall
,
565 unsigned long call_user_ID
)
567 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
569 call
->need_attention
= true;
570 wake_up(&call
->waitq
);
574 * wake up an asynchronous call
576 static void afs_wake_up_async_call(struct sock
*sk
, struct rxrpc_call
*rxcall
,
577 unsigned long call_user_ID
)
579 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
582 trace_afs_notify_call(rxcall
, call
);
583 call
->need_attention
= true;
585 u
= __atomic_add_unless(&call
->usage
, 1, 0);
587 trace_afs_call(call
, afs_call_trace_wake
, u
,
588 atomic_read(&afs_outstanding_calls
),
589 __builtin_return_address(0));
591 if (!queue_work(afs_async_calls
, &call
->async_work
))
597 * Delete an asynchronous call. The work item carries a ref to the call struct
598 * that we need to release.
600 static void afs_delete_async_call(struct work_struct
*work
)
602 struct afs_call
*call
= container_of(work
, struct afs_call
, async_work
);
612 * Perform I/O processing on an asynchronous call. The work item carries a ref
613 * to the call struct that we either need to release or to pass on.
615 static void afs_process_async_call(struct work_struct
*work
)
617 struct afs_call
*call
= container_of(work
, struct afs_call
, async_work
);
621 if (call
->state
< AFS_CALL_COMPLETE
&& call
->need_attention
) {
622 call
->need_attention
= false;
623 afs_deliver_to_call(call
);
626 if (call
->state
== AFS_CALL_COMPLETE
) {
629 /* We have two refs to release - one from the alloc and one
630 * queued with the work item - and we can't just deallocate the
631 * call because the work item may be queued again.
633 call
->async_work
.func
= afs_delete_async_call
;
634 if (!queue_work(afs_async_calls
, &call
->async_work
))
642 static void afs_rx_attach(struct rxrpc_call
*rxcall
, unsigned long user_call_ID
)
644 struct afs_call
*call
= (struct afs_call
*)user_call_ID
;
646 call
->rxcall
= rxcall
;
650 * Charge the incoming call preallocation.
652 static void afs_charge_preallocation(struct work_struct
*work
)
654 struct afs_call
*call
= afs_spare_incoming_call
;
658 call
= afs_alloc_call(&afs_RXCMxxxx
, GFP_KERNEL
);
663 call
->state
= AFS_CALL_AWAIT_OP_ID
;
664 init_waitqueue_head(&call
->waitq
);
667 if (rxrpc_kernel_charge_accept(afs_socket
,
668 afs_wake_up_async_call
,
675 afs_spare_incoming_call
= call
;
679 * Discard a preallocated call when a socket is shut down.
681 static void afs_rx_discard_new_call(struct rxrpc_call
*rxcall
,
682 unsigned long user_call_ID
)
684 struct afs_call
*call
= (struct afs_call
*)user_call_ID
;
691 * Notification of an incoming call.
693 static void afs_rx_new_call(struct sock
*sk
, struct rxrpc_call
*rxcall
,
694 unsigned long user_call_ID
)
696 queue_work(afs_wq
, &afs_charge_preallocation_work
);
700 * Grab the operation ID from an incoming cache manager call. The socket
701 * buffer is discarded on error or if we don't yet have sufficient data.
703 static int afs_deliver_cm_op_id(struct afs_call
*call
)
707 _enter("{%zu}", call
->offset
);
709 ASSERTCMP(call
->offset
, <, 4);
711 /* the operation ID forms the first four bytes of the request data */
712 ret
= afs_extract_data(call
, &call
->tmp
, 4, true);
716 call
->operation_ID
= ntohl(call
->tmp
);
717 call
->state
= AFS_CALL_AWAIT_REQUEST
;
720 /* ask the cache manager to route the call (it'll change the call type
722 if (!afs_cm_incoming_call(call
))
725 trace_afs_cb_call(call
);
727 /* pass responsibility for the remainer of this message off to the
728 * cache manager op */
729 return call
->type
->deliver(call
);
733 * send an empty reply
735 void afs_send_empty_reply(struct afs_call
*call
)
743 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, NULL
, 0, 0);
744 msg
.msg_control
= NULL
;
745 msg
.msg_controllen
= 0;
748 call
->state
= AFS_CALL_AWAIT_ACK
;
749 switch (rxrpc_kernel_send_data(afs_socket
, call
->rxcall
, &msg
, 0)) {
751 _leave(" [replied]");
756 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
757 RX_USER_ABORT
, ENOMEM
, "KOO");
765 * send a simple reply
767 void afs_send_simple_reply(struct afs_call
*call
, const void *buf
, size_t len
)
775 iov
[0].iov_base
= (void *) buf
;
776 iov
[0].iov_len
= len
;
779 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, iov
, 1, len
);
780 msg
.msg_control
= NULL
;
781 msg
.msg_controllen
= 0;
784 call
->state
= AFS_CALL_AWAIT_ACK
;
785 n
= rxrpc_kernel_send_data(afs_socket
, call
->rxcall
, &msg
, len
);
788 _leave(" [replied]");
794 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
795 RX_USER_ABORT
, ENOMEM
, "KOO");
801 * Extract a piece of data from the received data socket buffers.
803 int afs_extract_data(struct afs_call
*call
, void *buf
, size_t count
,
808 _enter("{%s,%zu},,%zu,%d",
809 call
->type
->name
, call
->offset
, count
, want_more
);
811 ASSERTCMP(call
->offset
, <=, count
);
813 ret
= rxrpc_kernel_recv_data(afs_socket
, call
->rxcall
,
814 buf
, count
, &call
->offset
,
815 want_more
, &call
->abort_code
);
816 trace_afs_recv_data(call
, count
, call
->offset
, want_more
, ret
);
817 if (ret
== 0 || ret
== -EAGAIN
)
821 switch (call
->state
) {
822 case AFS_CALL_AWAIT_REPLY
:
823 call
->state
= AFS_CALL_COMPLETE
;
825 case AFS_CALL_AWAIT_REQUEST
:
826 call
->state
= AFS_CALL_REPLYING
;
834 if (ret
== -ECONNABORTED
)
835 call
->error
= call
->type
->abort_to_error(call
->abort_code
);
838 call
->state
= AFS_CALL_COMPLETE
;