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>
20 struct socket
*afs_socket
; /* my RxRPC socket */
21 static struct workqueue_struct
*afs_async_calls
;
22 static struct afs_call
*afs_spare_incoming_call
;
23 atomic_t afs_outstanding_calls
;
25 static void afs_wake_up_call_waiter(struct sock
*, struct rxrpc_call
*, unsigned long);
26 static int afs_wait_for_call_to_complete(struct afs_call
*);
27 static void afs_wake_up_async_call(struct sock
*, struct rxrpc_call
*, unsigned long);
28 static void afs_process_async_call(struct work_struct
*);
29 static void afs_rx_new_call(struct sock
*, struct rxrpc_call
*, unsigned long);
30 static void afs_rx_discard_new_call(struct rxrpc_call
*, unsigned long);
31 static int afs_deliver_cm_op_id(struct afs_call
*);
33 /* asynchronous incoming call initial processing */
34 static const struct afs_call_type afs_RXCMxxxx
= {
36 .deliver
= afs_deliver_cm_op_id
,
37 .abort_to_error
= afs_abort_to_error
,
40 static void afs_charge_preallocation(struct work_struct
*);
42 static DECLARE_WORK(afs_charge_preallocation_work
, afs_charge_preallocation
);
44 static int afs_wait_atomic_t(atomic_t
*p
)
51 * open an RxRPC socket and bind it to be a server for callback notifications
52 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
54 int afs_open_socket(void)
56 struct sockaddr_rxrpc srx
;
57 struct socket
*socket
;
58 unsigned int min_level
;
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 min_level
= RXRPC_SECURITY_ENCRYPT
;
85 ret
= kernel_setsockopt(socket
, SOL_RXRPC
, RXRPC_MIN_SECURITY_LEVEL
,
86 (void *)&min_level
, sizeof(min_level
));
90 ret
= kernel_bind(socket
, (struct sockaddr
*) &srx
, sizeof(srx
));
94 rxrpc_kernel_new_call_notification(socket
, afs_rx_new_call
,
95 afs_rx_discard_new_call
);
97 ret
= kernel_listen(socket
, INT_MAX
);
102 afs_charge_preallocation(NULL
);
107 sock_release(socket
);
109 destroy_workqueue(afs_async_calls
);
111 _leave(" = %d", ret
);
116 * close the RxRPC socket AFS was using
118 void afs_close_socket(void)
122 kernel_listen(afs_socket
, 0);
123 flush_workqueue(afs_async_calls
);
125 if (afs_spare_incoming_call
) {
126 afs_put_call(afs_spare_incoming_call
);
127 afs_spare_incoming_call
= NULL
;
130 _debug("outstanding %u", atomic_read(&afs_outstanding_calls
));
131 wait_on_atomic_t(&afs_outstanding_calls
, afs_wait_atomic_t
,
132 TASK_UNINTERRUPTIBLE
);
133 _debug("no outstanding calls");
135 kernel_sock_shutdown(afs_socket
, SHUT_RDWR
);
136 flush_workqueue(afs_async_calls
);
137 sock_release(afs_socket
);
140 destroy_workqueue(afs_async_calls
);
147 static struct afs_call
*afs_alloc_call(const struct afs_call_type
*type
,
150 struct afs_call
*call
;
153 call
= kzalloc(sizeof(*call
), gfp
);
158 atomic_set(&call
->usage
, 1);
159 INIT_WORK(&call
->async_work
, afs_process_async_call
);
160 init_waitqueue_head(&call
->waitq
);
162 o
= atomic_inc_return(&afs_outstanding_calls
);
163 trace_afs_call(call
, afs_call_trace_alloc
, 1, o
,
164 __builtin_return_address(0));
169 * Dispose of a reference on a call.
171 void afs_put_call(struct afs_call
*call
)
173 int n
= atomic_dec_return(&call
->usage
);
174 int o
= atomic_read(&afs_outstanding_calls
);
176 trace_afs_call(call
, afs_call_trace_put
, n
+ 1, o
,
177 __builtin_return_address(0));
181 ASSERT(!work_pending(&call
->async_work
));
182 ASSERT(call
->type
->name
!= NULL
);
185 rxrpc_kernel_end_call(afs_socket
, call
->rxcall
);
188 if (call
->type
->destructor
)
189 call
->type
->destructor(call
);
191 kfree(call
->request
);
194 o
= atomic_dec_return(&afs_outstanding_calls
);
195 trace_afs_call(call
, afs_call_trace_free
, 0, o
,
196 __builtin_return_address(0));
198 wake_up_atomic_t(&afs_outstanding_calls
);
203 * Queue the call for actual work. Returns 0 unconditionally for convenience.
205 int afs_queue_call_work(struct afs_call
*call
)
207 int u
= atomic_inc_return(&call
->usage
);
209 trace_afs_call(call
, afs_call_trace_work
, u
,
210 atomic_read(&afs_outstanding_calls
),
211 __builtin_return_address(0));
213 INIT_WORK(&call
->work
, call
->type
->work
);
215 if (!queue_work(afs_wq
, &call
->work
))
221 * allocate a call with flat request and reply buffers
223 struct afs_call
*afs_alloc_flat_call(const struct afs_call_type
*type
,
224 size_t request_size
, size_t reply_max
)
226 struct afs_call
*call
;
228 call
= afs_alloc_call(type
, GFP_NOFS
);
233 call
->request_size
= request_size
;
234 call
->request
= kmalloc(request_size
, GFP_NOFS
);
240 call
->reply_max
= reply_max
;
241 call
->buffer
= kmalloc(reply_max
, GFP_NOFS
);
246 init_waitqueue_head(&call
->waitq
);
256 * clean up a call with flat buffer
258 void afs_flat_call_destructor(struct afs_call
*call
)
262 kfree(call
->request
);
263 call
->request
= NULL
;
268 #define AFS_BVEC_MAX 8
271 * Load the given bvec with the next few pages.
273 static void afs_load_bvec(struct afs_call
*call
, struct msghdr
*msg
,
274 struct bio_vec
*bv
, pgoff_t first
, pgoff_t last
,
277 struct page
*pages
[AFS_BVEC_MAX
];
278 unsigned int nr
, n
, i
, to
, bytes
= 0;
280 nr
= min_t(pgoff_t
, last
- first
+ 1, AFS_BVEC_MAX
);
281 n
= find_get_pages_contig(call
->mapping
, first
, nr
, pages
);
282 ASSERTCMP(n
, ==, nr
);
284 msg
->msg_flags
|= MSG_MORE
;
285 for (i
= 0; i
< nr
; i
++) {
287 if (first
+ i
>= last
) {
289 msg
->msg_flags
&= ~MSG_MORE
;
291 bv
[i
].bv_page
= pages
[i
];
292 bv
[i
].bv_len
= to
- offset
;
293 bv
[i
].bv_offset
= offset
;
294 bytes
+= to
- offset
;
298 iov_iter_bvec(&msg
->msg_iter
, WRITE
| ITER_BVEC
, bv
, nr
, bytes
);
302 * Advance the AFS call state when the RxRPC call ends the transmit phase.
304 static void afs_notify_end_request_tx(struct sock
*sock
,
305 struct rxrpc_call
*rxcall
,
306 unsigned long call_user_ID
)
308 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
310 if (call
->state
== AFS_CALL_REQUESTING
)
311 call
->state
= AFS_CALL_AWAIT_REPLY
;
315 * attach the data from a bunch of pages on an inode to a call
317 static int afs_send_pages(struct afs_call
*call
, struct msghdr
*msg
)
319 struct bio_vec bv
[AFS_BVEC_MAX
];
320 unsigned int bytes
, nr
, loop
, offset
;
321 pgoff_t first
= call
->first
, last
= call
->last
;
324 offset
= call
->first_offset
;
325 call
->first_offset
= 0;
328 afs_load_bvec(call
, msg
, bv
, first
, last
, offset
);
330 bytes
= msg
->msg_iter
.count
;
331 nr
= msg
->msg_iter
.nr_segs
;
333 ret
= rxrpc_kernel_send_data(afs_socket
, call
->rxcall
, msg
,
334 bytes
, afs_notify_end_request_tx
);
335 for (loop
= 0; loop
< nr
; loop
++)
336 put_page(bv
[loop
].bv_page
);
341 } while (first
<= last
);
349 int afs_make_call(struct in_addr
*addr
, struct afs_call
*call
, gfp_t gfp
,
352 struct sockaddr_rxrpc srx
;
353 struct rxrpc_call
*rxcall
;
361 _enter("%x,{%d},", addr
->s_addr
, ntohs(call
->port
));
363 ASSERT(call
->type
!= NULL
);
364 ASSERT(call
->type
->name
!= NULL
);
366 _debug("____MAKE %p{%s,%x} [%d]____",
367 call
, call
->type
->name
, key_serial(call
->key
),
368 atomic_read(&afs_outstanding_calls
));
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);
381 /* Work out the length we're going to transmit. This is awkward for
382 * calls such as FS.StoreData where there's an extra injection of data
383 * after the initial fixed part.
385 tx_total_len
= call
->request_size
;
386 if (call
->send_pages
) {
387 if (call
->last
== call
->first
) {
388 tx_total_len
+= call
->last_to
- call
->first_offset
;
390 /* It looks mathematically like you should be able to
391 * combine the following lines with the ones above, but
392 * unsigned arithmetic is fun when it wraps...
394 tx_total_len
+= PAGE_SIZE
- call
->first_offset
;
395 tx_total_len
+= call
->last_to
;
396 tx_total_len
+= (call
->last
- call
->first
- 1) * PAGE_SIZE
;
401 rxcall
= rxrpc_kernel_begin_call(afs_socket
, &srx
, call
->key
,
405 afs_wake_up_async_call
:
406 afs_wake_up_call_waiter
));
408 if (IS_ERR(rxcall
)) {
409 ret
= PTR_ERR(rxcall
);
410 goto error_kill_call
;
413 call
->rxcall
= rxcall
;
415 /* send the request */
416 iov
[0].iov_base
= call
->request
;
417 iov
[0].iov_len
= call
->request_size
;
421 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, iov
, 1,
423 msg
.msg_control
= NULL
;
424 msg
.msg_controllen
= 0;
425 msg
.msg_flags
= (call
->send_pages
? MSG_MORE
: 0);
427 /* We have to change the state *before* sending the last packet as
428 * rxrpc might give us the reply before it returns from sending the
429 * request. Further, if the send fails, we may already have been given
430 * a notification and may have collected it.
432 if (!call
->send_pages
)
433 call
->state
= AFS_CALL_AWAIT_REPLY
;
434 ret
= rxrpc_kernel_send_data(afs_socket
, rxcall
,
435 &msg
, call
->request_size
,
436 afs_notify_end_request_tx
);
440 if (call
->send_pages
) {
441 ret
= afs_send_pages(call
, &msg
);
446 /* at this point, an async call may no longer exist as it may have
447 * already completed */
451 return afs_wait_for_call_to_complete(call
);
454 call
->state
= AFS_CALL_COMPLETE
;
455 if (ret
!= -ECONNABORTED
) {
456 rxrpc_kernel_abort_call(afs_socket
, rxcall
, RX_USER_ABORT
,
461 rxrpc_kernel_recv_data(afs_socket
, rxcall
, NULL
, 0, &offset
,
463 ret
= call
->type
->abort_to_error(abort_code
);
467 _leave(" = %d", ret
);
472 * deliver messages to a call
474 static void afs_deliver_to_call(struct afs_call
*call
)
479 _enter("%s", call
->type
->name
);
481 while (call
->state
== AFS_CALL_AWAIT_REPLY
||
482 call
->state
== AFS_CALL_AWAIT_OP_ID
||
483 call
->state
== AFS_CALL_AWAIT_REQUEST
||
484 call
->state
== AFS_CALL_AWAIT_ACK
486 if (call
->state
== AFS_CALL_AWAIT_ACK
) {
488 ret
= rxrpc_kernel_recv_data(afs_socket
, call
->rxcall
,
489 NULL
, 0, &offset
, false,
491 trace_afs_recv_data(call
, 0, offset
, false, ret
);
493 if (ret
== -EINPROGRESS
|| ret
== -EAGAIN
)
495 if (ret
== 1 || ret
< 0) {
496 call
->state
= AFS_CALL_COMPLETE
;
502 ret
= call
->type
->deliver(call
);
505 if (call
->state
== AFS_CALL_AWAIT_REPLY
)
506 call
->state
= AFS_CALL_COMPLETE
;
514 abort_code
= RX_CALL_DEAD
;
515 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
516 abort_code
, ret
, "KNC");
519 abort_code
= RXGEN_OPCODE
;
520 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
521 abort_code
, ret
, "KIV");
527 abort_code
= RXGEN_CC_UNMARSHAL
;
528 if (call
->state
!= AFS_CALL_AWAIT_REPLY
)
529 abort_code
= RXGEN_SS_UNMARSHAL
;
530 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
531 abort_code
, -EBADMSG
, "KUM");
537 if (call
->state
== AFS_CALL_COMPLETE
&& call
->incoming
)
546 call
->state
= AFS_CALL_COMPLETE
;
551 * wait synchronously for a call to complete
553 static int afs_wait_for_call_to_complete(struct afs_call
*call
)
557 DECLARE_WAITQUEUE(myself
, current
);
561 add_wait_queue(&call
->waitq
, &myself
);
563 set_current_state(TASK_INTERRUPTIBLE
);
565 /* deliver any messages that are in the queue */
566 if (call
->state
< AFS_CALL_COMPLETE
&& call
->need_attention
) {
567 call
->need_attention
= false;
568 __set_current_state(TASK_RUNNING
);
569 afs_deliver_to_call(call
);
573 if (call
->state
== AFS_CALL_COMPLETE
||
574 signal_pending(current
))
579 remove_wait_queue(&call
->waitq
, &myself
);
580 __set_current_state(TASK_RUNNING
);
582 /* Kill off the call if it's still live. */
583 if (call
->state
< AFS_CALL_COMPLETE
) {
584 _debug("call interrupted");
585 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
586 RX_USER_ABORT
, -EINTR
, "KWI");
590 _debug("call complete");
592 _leave(" = %d", ret
);
597 * wake up a waiting call
599 static void afs_wake_up_call_waiter(struct sock
*sk
, struct rxrpc_call
*rxcall
,
600 unsigned long call_user_ID
)
602 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
604 call
->need_attention
= true;
605 wake_up(&call
->waitq
);
609 * wake up an asynchronous call
611 static void afs_wake_up_async_call(struct sock
*sk
, struct rxrpc_call
*rxcall
,
612 unsigned long call_user_ID
)
614 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
617 trace_afs_notify_call(rxcall
, call
);
618 call
->need_attention
= true;
620 u
= __atomic_add_unless(&call
->usage
, 1, 0);
622 trace_afs_call(call
, afs_call_trace_wake
, u
,
623 atomic_read(&afs_outstanding_calls
),
624 __builtin_return_address(0));
626 if (!queue_work(afs_async_calls
, &call
->async_work
))
632 * Delete an asynchronous call. The work item carries a ref to the call struct
633 * that we need to release.
635 static void afs_delete_async_call(struct work_struct
*work
)
637 struct afs_call
*call
= container_of(work
, struct afs_call
, async_work
);
647 * Perform I/O processing on an asynchronous call. The work item carries a ref
648 * to the call struct that we either need to release or to pass on.
650 static void afs_process_async_call(struct work_struct
*work
)
652 struct afs_call
*call
= container_of(work
, struct afs_call
, async_work
);
656 if (call
->state
< AFS_CALL_COMPLETE
&& call
->need_attention
) {
657 call
->need_attention
= false;
658 afs_deliver_to_call(call
);
661 if (call
->state
== AFS_CALL_COMPLETE
) {
664 /* We have two refs to release - one from the alloc and one
665 * queued with the work item - and we can't just deallocate the
666 * call because the work item may be queued again.
668 call
->async_work
.func
= afs_delete_async_call
;
669 if (!queue_work(afs_async_calls
, &call
->async_work
))
677 static void afs_rx_attach(struct rxrpc_call
*rxcall
, unsigned long user_call_ID
)
679 struct afs_call
*call
= (struct afs_call
*)user_call_ID
;
681 call
->rxcall
= rxcall
;
685 * Charge the incoming call preallocation.
687 static void afs_charge_preallocation(struct work_struct
*work
)
689 struct afs_call
*call
= afs_spare_incoming_call
;
693 call
= afs_alloc_call(&afs_RXCMxxxx
, GFP_KERNEL
);
698 call
->state
= AFS_CALL_AWAIT_OP_ID
;
699 init_waitqueue_head(&call
->waitq
);
702 if (rxrpc_kernel_charge_accept(afs_socket
,
703 afs_wake_up_async_call
,
710 afs_spare_incoming_call
= call
;
714 * Discard a preallocated call when a socket is shut down.
716 static void afs_rx_discard_new_call(struct rxrpc_call
*rxcall
,
717 unsigned long user_call_ID
)
719 struct afs_call
*call
= (struct afs_call
*)user_call_ID
;
726 * Notification of an incoming call.
728 static void afs_rx_new_call(struct sock
*sk
, struct rxrpc_call
*rxcall
,
729 unsigned long user_call_ID
)
731 queue_work(afs_wq
, &afs_charge_preallocation_work
);
735 * Grab the operation ID from an incoming cache manager call. The socket
736 * buffer is discarded on error or if we don't yet have sufficient data.
738 static int afs_deliver_cm_op_id(struct afs_call
*call
)
742 _enter("{%zu}", call
->offset
);
744 ASSERTCMP(call
->offset
, <, 4);
746 /* the operation ID forms the first four bytes of the request data */
747 ret
= afs_extract_data(call
, &call
->tmp
, 4, true);
751 call
->operation_ID
= ntohl(call
->tmp
);
752 call
->state
= AFS_CALL_AWAIT_REQUEST
;
755 /* ask the cache manager to route the call (it'll change the call type
757 if (!afs_cm_incoming_call(call
))
760 trace_afs_cb_call(call
);
762 /* pass responsibility for the remainer of this message off to the
763 * cache manager op */
764 return call
->type
->deliver(call
);
768 * Advance the AFS call state when an RxRPC service call ends the transmit
771 static void afs_notify_end_reply_tx(struct sock
*sock
,
772 struct rxrpc_call
*rxcall
,
773 unsigned long call_user_ID
)
775 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
777 if (call
->state
== AFS_CALL_REPLYING
)
778 call
->state
= AFS_CALL_AWAIT_ACK
;
782 * send an empty reply
784 void afs_send_empty_reply(struct afs_call
*call
)
790 rxrpc_kernel_set_tx_length(afs_socket
, call
->rxcall
, 0);
794 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, NULL
, 0, 0);
795 msg
.msg_control
= NULL
;
796 msg
.msg_controllen
= 0;
799 call
->state
= AFS_CALL_AWAIT_ACK
;
800 switch (rxrpc_kernel_send_data(afs_socket
, call
->rxcall
, &msg
, 0,
801 afs_notify_end_reply_tx
)) {
803 _leave(" [replied]");
808 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
809 RX_USER_ABORT
, -ENOMEM
, "KOO");
817 * send a simple reply
819 void afs_send_simple_reply(struct afs_call
*call
, const void *buf
, size_t len
)
827 rxrpc_kernel_set_tx_length(afs_socket
, call
->rxcall
, len
);
829 iov
[0].iov_base
= (void *) buf
;
830 iov
[0].iov_len
= len
;
833 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, iov
, 1, len
);
834 msg
.msg_control
= NULL
;
835 msg
.msg_controllen
= 0;
838 call
->state
= AFS_CALL_AWAIT_ACK
;
839 n
= rxrpc_kernel_send_data(afs_socket
, call
->rxcall
, &msg
, len
,
840 afs_notify_end_reply_tx
);
843 _leave(" [replied]");
849 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
850 RX_USER_ABORT
, -ENOMEM
, "KOO");
856 * Extract a piece of data from the received data socket buffers.
858 int afs_extract_data(struct afs_call
*call
, void *buf
, size_t count
,
863 _enter("{%s,%zu},,%zu,%d",
864 call
->type
->name
, call
->offset
, count
, want_more
);
866 ASSERTCMP(call
->offset
, <=, count
);
868 ret
= rxrpc_kernel_recv_data(afs_socket
, call
->rxcall
,
869 buf
, count
, &call
->offset
,
870 want_more
, &call
->abort_code
);
871 trace_afs_recv_data(call
, count
, call
->offset
, want_more
, ret
);
872 if (ret
== 0 || ret
== -EAGAIN
)
876 switch (call
->state
) {
877 case AFS_CALL_AWAIT_REPLY
:
878 call
->state
= AFS_CALL_COMPLETE
;
880 case AFS_CALL_AWAIT_REQUEST
:
881 call
->state
= AFS_CALL_REPLYING
;
889 if (ret
== -ECONNABORTED
)
890 call
->error
= call
->type
->abort_to_error(call
->abort_code
);
893 call
->state
= AFS_CALL_COMPLETE
;