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
);
45 * open an RxRPC socket and bind it to be a server for callback notifications
46 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
48 int afs_open_socket(void)
50 struct sockaddr_rxrpc srx
;
51 struct socket
*socket
;
57 afs_async_calls
= alloc_workqueue("kafsd", WQ_MEM_RECLAIM
, 0);
61 ret
= sock_create_kern(&init_net
, AF_RXRPC
, SOCK_DGRAM
, PF_INET
, &socket
);
65 socket
->sk
->sk_allocation
= GFP_NOFS
;
67 /* bind the callback manager's address to make this a server socket */
68 srx
.srx_family
= AF_RXRPC
;
69 srx
.srx_service
= CM_SERVICE
;
70 srx
.transport_type
= SOCK_DGRAM
;
71 srx
.transport_len
= sizeof(srx
.transport
.sin
);
72 srx
.transport
.sin
.sin_family
= AF_INET
;
73 srx
.transport
.sin
.sin_port
= htons(AFS_CM_PORT
);
74 memset(&srx
.transport
.sin
.sin_addr
, 0,
75 sizeof(srx
.transport
.sin
.sin_addr
));
77 ret
= kernel_bind(socket
, (struct sockaddr
*) &srx
, sizeof(srx
));
81 rxrpc_kernel_new_call_notification(socket
, afs_rx_new_call
,
82 afs_rx_discard_new_call
);
84 ret
= kernel_listen(socket
, INT_MAX
);
89 afs_charge_preallocation(NULL
);
96 destroy_workqueue(afs_async_calls
);
103 * close the RxRPC socket AFS was using
105 void afs_close_socket(void)
109 kernel_listen(afs_socket
, 0);
110 flush_workqueue(afs_async_calls
);
112 if (afs_spare_incoming_call
) {
113 afs_put_call(afs_spare_incoming_call
);
114 afs_spare_incoming_call
= NULL
;
117 _debug("outstanding %u", atomic_read(&afs_outstanding_calls
));
118 wait_on_atomic_t(&afs_outstanding_calls
, atomic_t_wait
,
119 TASK_UNINTERRUPTIBLE
);
120 _debug("no outstanding calls");
122 kernel_sock_shutdown(afs_socket
, SHUT_RDWR
);
123 flush_workqueue(afs_async_calls
);
124 sock_release(afs_socket
);
127 destroy_workqueue(afs_async_calls
);
134 static struct afs_call
*afs_alloc_call(const struct afs_call_type
*type
,
137 struct afs_call
*call
;
140 call
= kzalloc(sizeof(*call
), gfp
);
145 atomic_set(&call
->usage
, 1);
146 INIT_WORK(&call
->async_work
, afs_process_async_call
);
147 init_waitqueue_head(&call
->waitq
);
149 o
= atomic_inc_return(&afs_outstanding_calls
);
150 trace_afs_call(call
, afs_call_trace_alloc
, 1, o
,
151 __builtin_return_address(0));
156 * Dispose of a reference on a call.
158 void afs_put_call(struct afs_call
*call
)
160 int n
= atomic_dec_return(&call
->usage
);
161 int o
= atomic_read(&afs_outstanding_calls
);
163 trace_afs_call(call
, afs_call_trace_put
, n
+ 1, o
,
164 __builtin_return_address(0));
168 ASSERT(!work_pending(&call
->async_work
));
169 ASSERT(call
->type
->name
!= NULL
);
172 rxrpc_kernel_end_call(afs_socket
, call
->rxcall
);
175 if (call
->type
->destructor
)
176 call
->type
->destructor(call
);
178 kfree(call
->request
);
181 o
= atomic_dec_return(&afs_outstanding_calls
);
182 trace_afs_call(call
, afs_call_trace_free
, 0, o
,
183 __builtin_return_address(0));
185 wake_up_atomic_t(&afs_outstanding_calls
);
190 * Queue the call for actual work. Returns 0 unconditionally for convenience.
192 int afs_queue_call_work(struct afs_call
*call
)
194 int u
= atomic_inc_return(&call
->usage
);
196 trace_afs_call(call
, afs_call_trace_work
, u
,
197 atomic_read(&afs_outstanding_calls
),
198 __builtin_return_address(0));
200 INIT_WORK(&call
->work
, call
->type
->work
);
202 if (!queue_work(afs_wq
, &call
->work
))
208 * allocate a call with flat request and reply buffers
210 struct afs_call
*afs_alloc_flat_call(const struct afs_call_type
*type
,
211 size_t request_size
, size_t reply_max
)
213 struct afs_call
*call
;
215 call
= afs_alloc_call(type
, GFP_NOFS
);
220 call
->request_size
= request_size
;
221 call
->request
= kmalloc(request_size
, GFP_NOFS
);
227 call
->reply_max
= reply_max
;
228 call
->buffer
= kmalloc(reply_max
, GFP_NOFS
);
233 init_waitqueue_head(&call
->waitq
);
243 * clean up a call with flat buffer
245 void afs_flat_call_destructor(struct afs_call
*call
)
249 kfree(call
->request
);
250 call
->request
= NULL
;
255 #define AFS_BVEC_MAX 8
258 * Load the given bvec with the next few pages.
260 static void afs_load_bvec(struct afs_call
*call
, struct msghdr
*msg
,
261 struct bio_vec
*bv
, pgoff_t first
, pgoff_t last
,
264 struct page
*pages
[AFS_BVEC_MAX
];
265 unsigned int nr
, n
, i
, to
, bytes
= 0;
267 nr
= min_t(pgoff_t
, last
- first
+ 1, AFS_BVEC_MAX
);
268 n
= find_get_pages_contig(call
->mapping
, first
, nr
, pages
);
269 ASSERTCMP(n
, ==, nr
);
271 msg
->msg_flags
|= MSG_MORE
;
272 for (i
= 0; i
< nr
; i
++) {
274 if (first
+ i
>= last
) {
276 msg
->msg_flags
&= ~MSG_MORE
;
278 bv
[i
].bv_page
= pages
[i
];
279 bv
[i
].bv_len
= to
- offset
;
280 bv
[i
].bv_offset
= offset
;
281 bytes
+= to
- offset
;
285 iov_iter_bvec(&msg
->msg_iter
, WRITE
| ITER_BVEC
, bv
, nr
, bytes
);
289 * Advance the AFS call state when the RxRPC call ends the transmit phase.
291 static void afs_notify_end_request_tx(struct sock
*sock
,
292 struct rxrpc_call
*rxcall
,
293 unsigned long call_user_ID
)
295 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
297 if (call
->state
== AFS_CALL_REQUESTING
)
298 call
->state
= AFS_CALL_AWAIT_REPLY
;
302 * attach the data from a bunch of pages on an inode to a call
304 static int afs_send_pages(struct afs_call
*call
, struct msghdr
*msg
)
306 struct bio_vec bv
[AFS_BVEC_MAX
];
307 unsigned int bytes
, nr
, loop
, offset
;
308 pgoff_t first
= call
->first
, last
= call
->last
;
311 offset
= call
->first_offset
;
312 call
->first_offset
= 0;
315 afs_load_bvec(call
, msg
, bv
, first
, last
, offset
);
317 bytes
= msg
->msg_iter
.count
;
318 nr
= msg
->msg_iter
.nr_segs
;
320 ret
= rxrpc_kernel_send_data(afs_socket
, call
->rxcall
, msg
,
321 bytes
, afs_notify_end_request_tx
);
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
;
348 _enter("%x,{%d},", addr
->s_addr
, ntohs(call
->port
));
350 ASSERT(call
->type
!= NULL
);
351 ASSERT(call
->type
->name
!= NULL
);
353 _debug("____MAKE %p{%s,%x} [%d]____",
354 call
, call
->type
->name
, key_serial(call
->key
),
355 atomic_read(&afs_outstanding_calls
));
359 memset(&srx
, 0, sizeof(srx
));
360 srx
.srx_family
= AF_RXRPC
;
361 srx
.srx_service
= call
->service_id
;
362 srx
.transport_type
= SOCK_DGRAM
;
363 srx
.transport_len
= sizeof(srx
.transport
.sin
);
364 srx
.transport
.sin
.sin_family
= AF_INET
;
365 srx
.transport
.sin
.sin_port
= call
->port
;
366 memcpy(&srx
.transport
.sin
.sin_addr
, addr
, 4);
368 /* Work out the length we're going to transmit. This is awkward for
369 * calls such as FS.StoreData where there's an extra injection of data
370 * after the initial fixed part.
372 tx_total_len
= call
->request_size
;
373 if (call
->send_pages
) {
374 tx_total_len
+= call
->last_to
- call
->first_offset
;
375 tx_total_len
+= (call
->last
- call
->first
) * PAGE_SIZE
;
379 rxcall
= rxrpc_kernel_begin_call(afs_socket
, &srx
, call
->key
,
383 afs_wake_up_async_call
:
384 afs_wake_up_call_waiter
),
387 if (IS_ERR(rxcall
)) {
388 ret
= PTR_ERR(rxcall
);
389 goto error_kill_call
;
392 call
->rxcall
= rxcall
;
394 /* send the request */
395 iov
[0].iov_base
= call
->request
;
396 iov
[0].iov_len
= call
->request_size
;
400 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, iov
, 1,
402 msg
.msg_control
= NULL
;
403 msg
.msg_controllen
= 0;
404 msg
.msg_flags
= MSG_WAITALL
| (call
->send_pages
? MSG_MORE
: 0);
406 /* We have to change the state *before* sending the last packet as
407 * rxrpc might give us the reply before it returns from sending the
408 * request. Further, if the send fails, we may already have been given
409 * a notification and may have collected it.
411 if (!call
->send_pages
)
412 call
->state
= AFS_CALL_AWAIT_REPLY
;
413 ret
= rxrpc_kernel_send_data(afs_socket
, rxcall
,
414 &msg
, call
->request_size
,
415 afs_notify_end_request_tx
);
419 if (call
->send_pages
) {
420 ret
= afs_send_pages(call
, &msg
);
425 /* at this point, an async call may no longer exist as it may have
426 * already completed */
430 return afs_wait_for_call_to_complete(call
);
433 call
->state
= AFS_CALL_COMPLETE
;
434 if (ret
!= -ECONNABORTED
) {
435 rxrpc_kernel_abort_call(afs_socket
, rxcall
, RX_USER_ABORT
,
440 rxrpc_kernel_recv_data(afs_socket
, rxcall
, NULL
, 0, &offset
,
441 false, &abort_code
, &call
->service_id
);
442 ret
= call
->type
->abort_to_error(abort_code
);
446 _leave(" = %d", ret
);
451 * deliver messages to a call
453 static void afs_deliver_to_call(struct afs_call
*call
)
458 _enter("%s", call
->type
->name
);
460 while (call
->state
== AFS_CALL_AWAIT_REPLY
||
461 call
->state
== AFS_CALL_AWAIT_OP_ID
||
462 call
->state
== AFS_CALL_AWAIT_REQUEST
||
463 call
->state
== AFS_CALL_AWAIT_ACK
465 if (call
->state
== AFS_CALL_AWAIT_ACK
) {
467 ret
= rxrpc_kernel_recv_data(afs_socket
, call
->rxcall
,
468 NULL
, 0, &offset
, false,
471 trace_afs_recv_data(call
, 0, offset
, false, ret
);
473 if (ret
== -EINPROGRESS
|| ret
== -EAGAIN
)
475 if (ret
== 1 || ret
< 0) {
476 call
->state
= AFS_CALL_COMPLETE
;
482 ret
= call
->type
->deliver(call
);
485 if (call
->state
== AFS_CALL_AWAIT_REPLY
)
486 call
->state
= AFS_CALL_COMPLETE
;
494 abort_code
= RX_CALL_DEAD
;
495 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
496 abort_code
, ret
, "KNC");
499 abort_code
= RXGEN_OPCODE
;
500 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
501 abort_code
, ret
, "KIV");
507 abort_code
= RXGEN_CC_UNMARSHAL
;
508 if (call
->state
!= AFS_CALL_AWAIT_REPLY
)
509 abort_code
= RXGEN_SS_UNMARSHAL
;
510 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
511 abort_code
, -EBADMSG
, "KUM");
517 if (call
->state
== AFS_CALL_COMPLETE
&& call
->incoming
)
526 call
->state
= AFS_CALL_COMPLETE
;
531 * wait synchronously for a call to complete
533 static int afs_wait_for_call_to_complete(struct afs_call
*call
)
535 signed long rtt2
, timeout
;
540 DECLARE_WAITQUEUE(myself
, current
);
544 rtt
= rxrpc_kernel_get_rtt(afs_socket
, call
->rxcall
);
545 rtt2
= nsecs_to_jiffies64(rtt
) * 2;
550 last_life
= rxrpc_kernel_check_life(afs_socket
, call
->rxcall
);
552 add_wait_queue(&call
->waitq
, &myself
);
554 set_current_state(TASK_UNINTERRUPTIBLE
);
556 /* deliver any messages that are in the queue */
557 if (call
->state
< AFS_CALL_COMPLETE
&& call
->need_attention
) {
558 call
->need_attention
= false;
559 __set_current_state(TASK_RUNNING
);
560 afs_deliver_to_call(call
);
564 if (call
->state
== AFS_CALL_COMPLETE
)
567 life
= rxrpc_kernel_check_life(afs_socket
, call
->rxcall
);
569 life
== last_life
&& signal_pending(current
))
572 if (life
!= last_life
) {
577 timeout
= schedule_timeout(timeout
);
580 remove_wait_queue(&call
->waitq
, &myself
);
581 __set_current_state(TASK_RUNNING
);
583 /* Kill off the call if it's still live. */
584 if (call
->state
< AFS_CALL_COMPLETE
) {
585 _debug("call interrupted");
586 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
587 RX_USER_ABORT
, -EINTR
, "KWI");
591 _debug("call complete");
593 _leave(" = %d", ret
);
598 * wake up a waiting call
600 static void afs_wake_up_call_waiter(struct sock
*sk
, struct rxrpc_call
*rxcall
,
601 unsigned long call_user_ID
)
603 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
605 call
->need_attention
= true;
606 wake_up(&call
->waitq
);
610 * wake up an asynchronous call
612 static void afs_wake_up_async_call(struct sock
*sk
, struct rxrpc_call
*rxcall
,
613 unsigned long call_user_ID
)
615 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
618 trace_afs_notify_call(rxcall
, call
);
619 call
->need_attention
= true;
621 u
= __atomic_add_unless(&call
->usage
, 1, 0);
623 trace_afs_call(call
, afs_call_trace_wake
, u
,
624 atomic_read(&afs_outstanding_calls
),
625 __builtin_return_address(0));
627 if (!queue_work(afs_async_calls
, &call
->async_work
))
633 * Delete an asynchronous call. The work item carries a ref to the call struct
634 * that we need to release.
636 static void afs_delete_async_call(struct work_struct
*work
)
638 struct afs_call
*call
= container_of(work
, struct afs_call
, async_work
);
648 * Perform I/O processing on an asynchronous call. The work item carries a ref
649 * to the call struct that we either need to release or to pass on.
651 static void afs_process_async_call(struct work_struct
*work
)
653 struct afs_call
*call
= container_of(work
, struct afs_call
, async_work
);
657 if (call
->state
< AFS_CALL_COMPLETE
&& call
->need_attention
) {
658 call
->need_attention
= false;
659 afs_deliver_to_call(call
);
662 if (call
->state
== AFS_CALL_COMPLETE
) {
665 /* We have two refs to release - one from the alloc and one
666 * queued with the work item - and we can't just deallocate the
667 * call because the work item may be queued again.
669 call
->async_work
.func
= afs_delete_async_call
;
670 if (!queue_work(afs_async_calls
, &call
->async_work
))
678 static void afs_rx_attach(struct rxrpc_call
*rxcall
, unsigned long user_call_ID
)
680 struct afs_call
*call
= (struct afs_call
*)user_call_ID
;
682 call
->rxcall
= rxcall
;
686 * Charge the incoming call preallocation.
688 static void afs_charge_preallocation(struct work_struct
*work
)
690 struct afs_call
*call
= afs_spare_incoming_call
;
694 call
= afs_alloc_call(&afs_RXCMxxxx
, GFP_KERNEL
);
699 call
->state
= AFS_CALL_AWAIT_OP_ID
;
700 init_waitqueue_head(&call
->waitq
);
703 if (rxrpc_kernel_charge_accept(afs_socket
,
704 afs_wake_up_async_call
,
711 afs_spare_incoming_call
= call
;
715 * Discard a preallocated call when a socket is shut down.
717 static void afs_rx_discard_new_call(struct rxrpc_call
*rxcall
,
718 unsigned long user_call_ID
)
720 struct afs_call
*call
= (struct afs_call
*)user_call_ID
;
727 * Notification of an incoming call.
729 static void afs_rx_new_call(struct sock
*sk
, struct rxrpc_call
*rxcall
,
730 unsigned long user_call_ID
)
732 queue_work(afs_wq
, &afs_charge_preallocation_work
);
736 * Grab the operation ID from an incoming cache manager call. The socket
737 * buffer is discarded on error or if we don't yet have sufficient data.
739 static int afs_deliver_cm_op_id(struct afs_call
*call
)
743 _enter("{%zu}", call
->offset
);
745 ASSERTCMP(call
->offset
, <, 4);
747 /* the operation ID forms the first four bytes of the request data */
748 ret
= afs_extract_data(call
, &call
->tmp
, 4, true);
752 call
->operation_ID
= ntohl(call
->tmp
);
753 call
->state
= AFS_CALL_AWAIT_REQUEST
;
756 /* ask the cache manager to route the call (it'll change the call type
758 if (!afs_cm_incoming_call(call
))
761 trace_afs_cb_call(call
);
763 /* pass responsibility for the remainer of this message off to the
764 * cache manager op */
765 return call
->type
->deliver(call
);
769 * Advance the AFS call state when an RxRPC service call ends the transmit
772 static void afs_notify_end_reply_tx(struct sock
*sock
,
773 struct rxrpc_call
*rxcall
,
774 unsigned long call_user_ID
)
776 struct afs_call
*call
= (struct afs_call
*)call_user_ID
;
778 if (call
->state
== AFS_CALL_REPLYING
)
779 call
->state
= AFS_CALL_AWAIT_ACK
;
783 * send an empty reply
785 void afs_send_empty_reply(struct afs_call
*call
)
791 rxrpc_kernel_set_tx_length(afs_socket
, call
->rxcall
, 0);
795 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, NULL
, 0, 0);
796 msg
.msg_control
= NULL
;
797 msg
.msg_controllen
= 0;
800 call
->state
= AFS_CALL_AWAIT_ACK
;
801 switch (rxrpc_kernel_send_data(afs_socket
, call
->rxcall
, &msg
, 0,
802 afs_notify_end_reply_tx
)) {
804 _leave(" [replied]");
809 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
810 RX_USER_ABORT
, -ENOMEM
, "KOO");
818 * send a simple reply
820 void afs_send_simple_reply(struct afs_call
*call
, const void *buf
, size_t len
)
828 rxrpc_kernel_set_tx_length(afs_socket
, call
->rxcall
, len
);
830 iov
[0].iov_base
= (void *) buf
;
831 iov
[0].iov_len
= len
;
834 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, iov
, 1, len
);
835 msg
.msg_control
= NULL
;
836 msg
.msg_controllen
= 0;
839 call
->state
= AFS_CALL_AWAIT_ACK
;
840 n
= rxrpc_kernel_send_data(afs_socket
, call
->rxcall
, &msg
, len
,
841 afs_notify_end_reply_tx
);
844 _leave(" [replied]");
850 rxrpc_kernel_abort_call(afs_socket
, call
->rxcall
,
851 RX_USER_ABORT
, -ENOMEM
, "KOO");
857 * Extract a piece of data from the received data socket buffers.
859 int afs_extract_data(struct afs_call
*call
, void *buf
, size_t count
,
864 _enter("{%s,%zu},,%zu,%d",
865 call
->type
->name
, call
->offset
, count
, want_more
);
867 ASSERTCMP(call
->offset
, <=, count
);
869 ret
= rxrpc_kernel_recv_data(afs_socket
, call
->rxcall
,
870 buf
, count
, &call
->offset
,
871 want_more
, &call
->abort_code
,
873 trace_afs_recv_data(call
, count
, call
->offset
, want_more
, ret
);
874 if (ret
== 0 || ret
== -EAGAIN
)
878 switch (call
->state
) {
879 case AFS_CALL_AWAIT_REPLY
:
880 call
->state
= AFS_CALL_COMPLETE
;
882 case AFS_CALL_AWAIT_REQUEST
:
883 call
->state
= AFS_CALL_REPLYING
;
891 if (ret
== -ECONNABORTED
)
892 call
->error
= call
->type
->abort_to_error(call
->abort_code
);
895 call
->state
= AFS_CALL_COMPLETE
;